Skip to content

[clang][bytecode] Implement lzcnt/tzcnt/bzhi builtins #110639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,45 @@ static bool interp__builtin_ia32_bextr(InterpState &S, CodePtr OpPC,
return true;
}

static bool interp__builtin_ia32_bzhi(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const Function *Func,
const CallExpr *Call) {
PrimType ValT = *S.Ctx.classify(Call->getArg(0));
PrimType IndexT = *S.Ctx.classify(Call->getArg(1));

APSInt Val = peekToAPSInt(S.Stk, ValT,
align(primSize(ValT)) + align(primSize(IndexT)));
APSInt Idx = peekToAPSInt(S.Stk, IndexT);

unsigned BitWidth = Val.getBitWidth();
uint64_t Index = Idx.extractBitsAsZExtValue(8, 0);

if (Index < BitWidth)
Val.clearHighBits(BitWidth - Index);

pushInteger(S, Val, Call->getType());
return true;
}

static bool interp__builtin_ia32_lzcnt(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const Function *Func,
const CallExpr *Call) {
APSInt Val = peekToAPSInt(S.Stk, *S.Ctx.classify(Call->getArg(0)));
pushInteger(S, Val.countLeadingZeros(), Call->getType());
return true;
}

static bool interp__builtin_ia32_tzcnt(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const Function *Func,
const CallExpr *Call) {
APSInt Val = peekToAPSInt(S.Stk, *S.Ctx.classify(Call->getArg(0)));
pushInteger(S, Val.countTrailingZeros(), Call->getType());
return true;
}

static bool interp__builtin_os_log_format_buffer_size(InterpState &S,
CodePtr OpPC,
const InterpFrame *Frame,
Expand Down Expand Up @@ -1773,6 +1812,26 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
return false;
break;

case clang::X86::BI__builtin_ia32_bzhi_si:
case clang::X86::BI__builtin_ia32_bzhi_di:
if (!interp__builtin_ia32_bzhi(S, OpPC, Frame, F, Call))
return false;
break;

case clang::X86::BI__builtin_ia32_lzcnt_u16:
case clang::X86::BI__builtin_ia32_lzcnt_u32:
case clang::X86::BI__builtin_ia32_lzcnt_u64:
if (!interp__builtin_ia32_lzcnt(S, OpPC, Frame, F, Call))
return false;
break;

case clang::X86::BI__builtin_ia32_tzcnt_u16:
case clang::X86::BI__builtin_ia32_tzcnt_u32:
case clang::X86::BI__builtin_ia32_tzcnt_u64:
if (!interp__builtin_ia32_tzcnt(S, OpPC, Frame, F, Call))
return false;
break;

case Builtin::BI__builtin_os_log_format_buffer_size:
if (!interp__builtin_os_log_format_buffer_size(S, OpPC, Frame, F, Call))
return false;
Expand Down
Loading