Skip to content
Merged
Changes from 1 commit
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
53 changes: 25 additions & 28 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,32 +1380,6 @@ static bool interp__builtin_ia32_bzhi(InterpState &S, CodePtr OpPC,
return true;
}

static bool interp__builtin_ia32_lzcnt(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const CallExpr *Call) {
QualType CallType = Call->getType();
if (!CallType->isIntegerType() ||
!Call->getArg(0)->getType()->isIntegerType())
return false;

APSInt Val = popToAPSInt(S, Call->getArg(0));
pushInteger(S, Val.countLeadingZeros(), CallType);
return true;
}

static bool interp__builtin_ia32_tzcnt(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const CallExpr *Call) {
QualType CallType = Call->getType();
if (!CallType->isIntegerType() ||
!Call->getArg(0)->getType()->isIntegerType())
return false;

APSInt Val = popToAPSInt(S, Call->getArg(0));
pushInteger(S, Val.countTrailingZeros(), CallType);
return true;
}

static bool interp__builtin_ia32_pdep(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const CallExpr *Call) {
Expand Down Expand Up @@ -2551,6 +2525,23 @@ static bool interp__builtin_is_within_lifetime(InterpState &S, CodePtr OpPC,
return true;
}

static bool interp__builtin_elementwise_int_unaryop(
InterpState &S, CodePtr OpPC, const CallExpr *Call,
llvm::function_ref<APInt(const APSInt &)> Fn) {
assert(Call->getType()->isIntegerType() && Call->getNumArgs() == 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be two separate asserts so we know what the problem is if it fails


// Single integer case.
if (!Call->getArg(0)->getType()->isVectorType()) {
APSInt Src = popToAPSInt(S, Call->getArg(0));
APInt Result = Fn(Src);
pushInteger(S, APSInt(std::move(Result), !Src.isSigned()), Call->getType());
return true;
}

// TODO: Add vector integer handling.
return false;
}

static bool interp__builtin_elementwise_int_binop(
InterpState &S, CodePtr OpPC, const CallExpr *Call,
llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
Expand Down Expand Up @@ -3283,12 +3274,18 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
case clang::X86::BI__builtin_ia32_lzcnt_u16:
case clang::X86::BI__builtin_ia32_lzcnt_u32:
case clang::X86::BI__builtin_ia32_lzcnt_u64:
return interp__builtin_ia32_lzcnt(S, OpPC, Frame, Call);
return interp__builtin_elementwise_int_unaryop(
S, OpPC, Call, [](const APSInt &Src) {
return APInt(Src.getBitWidth(), Src.countLeadingZeros());
});

case clang::X86::BI__builtin_ia32_tzcnt_u16:
case clang::X86::BI__builtin_ia32_tzcnt_u32:
case clang::X86::BI__builtin_ia32_tzcnt_u64:
return interp__builtin_ia32_tzcnt(S, OpPC, Frame, Call);
return interp__builtin_elementwise_int_unaryop(
S, OpPC, Call, [](const APSInt &Src) {
return APInt(Src.getBitWidth(), Src.countTrailingZeros());
});

case clang::X86::BI__builtin_ia32_pdep_si:
case clang::X86::BI__builtin_ia32_pdep_di:
Expand Down
Loading