[clang] [clang][bytecode][NFC] Stop using Function in InterpBuiltin (PR #137597)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 02:00:45 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Prepare for the ultimate removal of Function instances for builtin functions.
---
Patch is 57.71 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/137597.diff
1 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+173-238)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 770511ff76bb0..e93d08bfd9d30 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -33,45 +33,6 @@ static unsigned callArgSize(const InterpState &S, const CallExpr *C) {
return O;
}
-template <typename T>
-static T getParam(const InterpFrame *Frame, unsigned Index) {
- assert(Frame->getFunction()->getNumParams() > Index);
- unsigned Offset = Frame->getFunction()->getParamOffset(Index);
- return Frame->getParam<T>(Offset);
-}
-
-static APSInt getAPSIntParam(const InterpFrame *Frame, unsigned Index) {
- APSInt R;
- unsigned Offset = Frame->getFunction()->getParamOffset(Index);
- INT_TYPE_SWITCH(Frame->getFunction()->getParamType(Index),
- R = Frame->getParam<T>(Offset).toAPSInt());
- return R;
-}
-
-static PrimType getIntPrimType(const InterpState &S) {
- const TargetInfo &TI = S.getASTContext().getTargetInfo();
- unsigned IntWidth = TI.getIntWidth();
-
- if (IntWidth == 32)
- return PT_Sint32;
- else if (IntWidth == 16)
- return PT_Sint16;
- llvm_unreachable("Int isn't 16 or 32 bit?");
-}
-
-static PrimType getLongPrimType(const InterpState &S) {
- const TargetInfo &TI = S.getASTContext().getTargetInfo();
- unsigned LongWidth = TI.getLongWidth();
-
- if (LongWidth == 64)
- return PT_Sint64;
- else if (LongWidth == 32)
- return PT_Sint32;
- else if (LongWidth == 16)
- return PT_Sint16;
- llvm_unreachable("long isn't 16, 32 or 64 bit?");
-}
-
/// Peek an integer value from the stack into an APSInt.
static APSInt peekToAPSInt(InterpStack &Stk, PrimType T, size_t Offset = 0) {
if (Offset == 0)
@@ -206,10 +167,14 @@ static bool interp__builtin_is_constant_evaluated(InterpState &S, CodePtr OpPC,
static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
- unsigned ID = Func->getBuiltinID();
- const Pointer &A = getParam<Pointer>(Frame, 0);
- const Pointer &B = getParam<Pointer>(Frame, 1);
+ const CallExpr *Call, unsigned ID) {
+ unsigned LimitSize =
+ Call->getNumArgs() == 2
+ ? 0
+ : align(primSize(*S.getContext().classify(Call->getArg(2))));
+ const Pointer &A =
+ S.Stk.peek<Pointer>(align(primSize(PT_Ptr)) * 2 + LimitSize);
+ const Pointer &B = S.Stk.peek<Pointer>(align(primSize(PT_Ptr)) + LimitSize);
if (ID == Builtin::BIstrcmp || ID == Builtin::BIstrncmp ||
ID == Builtin::BIwcscmp || ID == Builtin::BIwcsncmp)
@@ -290,9 +255,8 @@ static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC,
static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
- unsigned ID = Func->getBuiltinID();
- const Pointer &StrPtr = getParam<Pointer>(Frame, 0);
+ const CallExpr *Call, unsigned ID) {
+ const Pointer &StrPtr = S.Stk.peek<Pointer>();
if (ID == Builtin::BIstrlen || ID == Builtin::BIwcslen)
diagnoseNonConstexprBuiltin(S, OpPC, ID);
@@ -345,9 +309,9 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_nan(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
+ const InterpFrame *Frame, const CallExpr *Call,
bool Signaling) {
- const Pointer &Arg = getParam<Pointer>(Frame, 0);
+ const Pointer &Arg = S.Stk.peek<Pointer>();
if (!CheckLoad(S, OpPC, Arg))
return false;
@@ -377,7 +341,8 @@ static bool interp__builtin_nan(InterpState &S, CodePtr OpPC,
return false;
const llvm::fltSemantics &TargetSemantics =
- S.getASTContext().getFloatTypeSemantics(F->getDecl()->getReturnType());
+ S.getASTContext().getFloatTypeSemantics(
+ Call->getDirectCallee()->getReturnType());
Floating Result;
if (S.getASTContext().getTargetInfo().isNan2008()) {
@@ -406,19 +371,20 @@ static bool interp__builtin_nan(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_inf(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F) {
+ const InterpFrame *Frame,
+ const CallExpr *Call) {
const llvm::fltSemantics &TargetSemantics =
- S.getASTContext().getFloatTypeSemantics(F->getDecl()->getReturnType());
+ S.getASTContext().getFloatTypeSemantics(
+ Call->getDirectCallee()->getReturnType());
S.Stk.push<Floating>(Floating::getInf(TargetSemantics));
return true;
}
static bool interp__builtin_copysign(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const Function *F) {
- const Floating &Arg1 = getParam<Floating>(Frame, 0);
- const Floating &Arg2 = getParam<Floating>(Frame, 1);
+ const InterpFrame *Frame) {
+ const Floating &Arg1 = S.Stk.peek<Floating>(align(primSize(PT_Float)) * 2);
+ const Floating &Arg2 = S.Stk.peek<Floating>();
APFloat Copy = Arg1.getAPFloat();
Copy.copySign(Arg2.getAPFloat());
@@ -428,10 +394,9 @@ static bool interp__builtin_copysign(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
- bool IsNumBuiltin) {
- const Floating &LHS = getParam<Floating>(Frame, 0);
- const Floating &RHS = getParam<Floating>(Frame, 1);
+ const InterpFrame *Frame, bool IsNumBuiltin) {
+ const Floating &LHS = S.Stk.peek<Floating>(align(primSize(PT_Float)) * 2);
+ const Floating &RHS = S.Stk.peek<Floating>();
if (IsNumBuiltin)
S.Stk.push<Floating>(llvm::minimumnum(LHS.getAPFloat(), RHS.getAPFloat()));
@@ -441,10 +406,9 @@ static bool interp__builtin_fmin(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *Func,
- bool IsNumBuiltin) {
- const Floating &LHS = getParam<Floating>(Frame, 0);
- const Floating &RHS = getParam<Floating>(Frame, 1);
+ const InterpFrame *Frame, bool IsNumBuiltin) {
+ const Floating &LHS = S.Stk.peek<Floating>(align(primSize(PT_Float)) * 2);
+ const Floating &RHS = S.Stk.peek<Floating>();
if (IsNumBuiltin)
S.Stk.push<Floating>(llvm::maximumnum(LHS.getAPFloat(), RHS.getAPFloat()));
@@ -457,7 +421,7 @@ static bool interp__builtin_fmax(InterpState &S, CodePtr OpPC,
/// take a float, double, long double, etc.
/// But for us, that's all a Floating anyway.
static bool interp__builtin_isnan(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
+ const InterpFrame *Frame,
const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
@@ -467,7 +431,6 @@ static bool interp__builtin_isnan(InterpState &S, CodePtr OpPC,
static bool interp__builtin_issignaling(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *F,
const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
@@ -476,8 +439,8 @@ static bool interp__builtin_issignaling(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_isinf(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
- bool CheckSign, const CallExpr *Call) {
+ const InterpFrame *Frame, bool CheckSign,
+ const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
bool IsInf = Arg.isInf();
@@ -490,7 +453,7 @@ static bool interp__builtin_isinf(InterpState &S, CodePtr OpPC,
static bool interp__builtin_isfinite(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *F, const CallExpr *Call) {
+ const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
pushInteger(S, Arg.isFinite(), Call->getType());
@@ -499,7 +462,7 @@ static bool interp__builtin_isfinite(InterpState &S, CodePtr OpPC,
static bool interp__builtin_isnormal(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *F, const CallExpr *Call) {
+ const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
pushInteger(S, Arg.isNormal(), Call->getType());
@@ -508,7 +471,6 @@ static bool interp__builtin_isnormal(InterpState &S, CodePtr OpPC,
static bool interp__builtin_issubnormal(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *F,
const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
@@ -517,7 +479,7 @@ static bool interp__builtin_issubnormal(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_iszero(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
+ const InterpFrame *Frame,
const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
@@ -526,7 +488,7 @@ static bool interp__builtin_iszero(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_signbit(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *F,
+ const InterpFrame *Frame,
const CallExpr *Call) {
const Floating &Arg = S.Stk.peek<Floating>();
@@ -535,12 +497,9 @@ static bool interp__builtin_signbit(InterpState &S, CodePtr OpPC,
}
static bool interp_floating_comparison(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const Function *F,
- const CallExpr *Call) {
+ const CallExpr *Call, unsigned ID) {
const Floating &RHS = S.Stk.peek<Floating>();
const Floating &LHS = S.Stk.peek<Floating>(align(2u * primSize(PT_Float)));
- unsigned ID = F->getBuiltinID();
pushInteger(
S,
@@ -574,7 +533,6 @@ static bool interp_floating_comparison(InterpState &S, CodePtr OpPC,
/// second one is an integral value.
static bool interp__builtin_isfpclass(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
PrimType FPClassArgT = *S.getContext().classify(Call->getArg(1)->getType());
APSInt FPClassArg = peekToAPSInt(S.Stk, FPClassArgT);
@@ -591,7 +549,6 @@ static bool interp__builtin_isfpclass(InterpState &S, CodePtr OpPC,
/// Five int values followed by one floating value.
static bool interp__builtin_fpclassify(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
const Floating &Val = S.Stk.peek<Floating>();
@@ -613,11 +570,12 @@ static bool interp__builtin_fpclassify(InterpState &S, CodePtr OpPC,
// The last argument is first on the stack.
assert(Index <= 4);
- unsigned IntSize = primSize(getIntPrimType(S));
+ PrimType IntT = *S.getContext().classify(Call->getArg(0));
+ unsigned IntSize = primSize(IntT);
unsigned Offset =
align(primSize(PT_Float)) + ((1 + (4 - Index)) * align(IntSize));
- APSInt I = peekToAPSInt(S.Stk, getIntPrimType(S), Offset);
+ APSInt I = peekToAPSInt(S.Stk, IntT, Offset);
pushInteger(S, I, Call->getType());
return true;
}
@@ -628,16 +586,15 @@ static bool interp__builtin_fpclassify(InterpState &S, CodePtr OpPC,
// proceed without regard to the floating point settings.
// Reference, WG14 N2478 F.10.4.3
static bool interp__builtin_fabs(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const Function *Func) {
- const Floating &Val = getParam<Floating>(Frame, 0);
+ const InterpFrame *Frame) {
+ const Floating &Val = S.Stk.peek<Floating>();
S.Stk.push<Floating>(Floating::abs(Val));
return true;
}
static bool interp__builtin_abs(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *Func,
+ const InterpFrame *Frame,
const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Val = peekToAPSInt(S.Stk, ArgT);
@@ -652,7 +609,6 @@ static bool interp__builtin_abs(InterpState &S, CodePtr OpPC,
static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Val = peekToAPSInt(S.Stk, ArgT);
@@ -662,7 +618,7 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
static bool interp__builtin_parity(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
+ const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Val = peekToAPSInt(S.Stk, ArgT);
pushInteger(S, Val.popcount() % 2, Call->getType());
@@ -671,7 +627,7 @@ static bool interp__builtin_parity(InterpState &S, CodePtr OpPC,
static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
+ const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Val = peekToAPSInt(S.Stk, ArgT);
pushInteger(S, Val.getBitWidth() - Val.getSignificantBits(), Call->getType());
@@ -680,7 +636,6 @@ static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC,
static bool interp__builtin_bitreverse(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Val = peekToAPSInt(S.Stk, ArgT);
@@ -690,7 +645,6 @@ static bool interp__builtin_bitreverse(InterpState &S, CodePtr OpPC,
static bool interp__builtin_classify_type(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
// This is an unevaluated call, so there are no arguments on the stack.
assert(Call->getNumArgs() == 1);
@@ -707,14 +661,14 @@ static bool interp__builtin_classify_type(InterpState &S, CodePtr OpPC,
// __builtin_expect_with_probability(long, long, double)
static bool interp__builtin_expect(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
+ const CallExpr *Call) {
// The return value is simply the value of the first parameter.
// We ignore the probability.
unsigned NumArgs = Call->getNumArgs();
assert(NumArgs == 2 || NumArgs == 3);
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
- unsigned Offset = align(primSize(getLongPrimType(S))) * 2;
+ unsigned Offset = align(ArgT) * 2;
if (NumArgs == 3)
Offset += align(primSize(PT_Float));
@@ -726,8 +680,7 @@ static bool interp__builtin_expect(InterpState &S, CodePtr OpPC,
/// rotateleft(value, amount)
static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call,
- bool Right) {
+ const CallExpr *Call, bool Right) {
PrimType AmountT = *S.getContext().classify(Call->getArg(1)->getType());
PrimType ValueT = *S.getContext().classify(Call->getArg(0)->getType());
@@ -748,7 +701,7 @@ static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_ffs(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *Func,
+ const InterpFrame *Frame,
const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Value = peekToAPSInt(S.Stk, ArgT);
@@ -760,7 +713,6 @@ static bool interp__builtin_ffs(InterpState &S, CodePtr OpPC,
static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
assert(Call->getArg(0)->isLValue());
PrimType PtrT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr);
@@ -775,19 +727,18 @@ static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC,
}
static bool interp__builtin_move(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame, const Function *Func,
+ const InterpFrame *Frame,
const CallExpr *Call) {
PrimType ArgT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr);
TYPE_SWITCH(ArgT, const T &Arg = S.Stk.peek<T>(); S.Stk.push<T>(Arg););
- return Func->getDecl()->isConstexpr();
+ return Call->getDirectCallee()->isConstexpr();
}
static bool interp__builtin_eh_return_data_regno(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
- const Function *Func,
const CallExpr *Call) {
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
APSInt Arg = peekToAPSInt(S.Stk, ArgT);
@@ -799,8 +750,7 @@ static bool interp__builtin_eh_return_data_regno(InterpState &S, CodePtr OpPC,
}
/// Just takes the first Argument to the call and puts it on the stack.
-static bool noopPointer(InterpState &S, CodePtr OpPC, const InterpFrame *Frame,
- const Function *Func, const CallExpr *Call) {
+static bool noopPointer(InterpState &S) {
const Pointer &Arg = S.Stk.peek<Pointer>();
S.Stk.push<Pointer>(Arg);
return true;
@@ -808,14 +758,12 @@ static bool noopPointer(InterpState &S, CodePtr OpPC, const InterpFrame *Frame,
// Two integral values followed by a pointer (lhs, rhs, resultOut)
static bool interp__builtin_overflowop(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const Function *Func,
- const CallExpr *Call) {
+ const CallExpr *Call,
+...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/137597
More information about the cfe-commits
mailing list