Skip to content

Commit 538cd20

Browse files
authored
[clang][bytecode][NFC] Simplify a few builtin implementations (#160910)
1 parent dee28f9 commit 538cd20

File tree

1 file changed

+31
-56
lines changed

1 file changed

+31
-56
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,9 @@ static bool interp_floating_comparison(InterpState &S, CodePtr OpPC,
563563
case Builtin::BI__builtin_islessequal:
564564
return LHS <= RHS;
565565
case Builtin::BI__builtin_islessgreater: {
566-
ComparisonCategoryResult cmp = LHS.compare(RHS);
567-
return cmp == ComparisonCategoryResult::Less ||
568-
cmp == ComparisonCategoryResult::Greater;
566+
ComparisonCategoryResult Cmp = LHS.compare(RHS);
567+
return Cmp == ComparisonCategoryResult::Less ||
568+
Cmp == ComparisonCategoryResult::Greater;
569569
}
570570
case Builtin::BI__builtin_isunordered:
571571
return LHS.compare(RHS) == ComparisonCategoryResult::Unordered;
@@ -583,8 +583,7 @@ static bool interp_floating_comparison(InterpState &S, CodePtr OpPC,
583583
static bool interp__builtin_isfpclass(InterpState &S, CodePtr OpPC,
584584
const InterpFrame *Frame,
585585
const CallExpr *Call) {
586-
PrimType FPClassArgT = *S.getContext().classify(Call->getArg(1)->getType());
587-
APSInt FPClassArg = popToAPSInt(S.Stk, FPClassArgT);
586+
APSInt FPClassArg = popToAPSInt(S, Call->getArg(1));
588587
const Floating &F = S.Stk.pop<Floating>();
589588

590589
int32_t Result = static_cast<int32_t>(
@@ -655,8 +654,7 @@ static bool interp__builtin_fabs(InterpState &S, CodePtr OpPC,
655654
static bool interp__builtin_abs(InterpState &S, CodePtr OpPC,
656655
const InterpFrame *Frame,
657656
const CallExpr *Call) {
658-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
659-
APSInt Val = popToAPSInt(S.Stk, ArgT);
657+
APSInt Val = popToAPSInt(S, Call->getArg(0));
660658
if (Val ==
661659
APSInt(APInt::getSignedMinValue(Val.getBitWidth()), /*IsUnsigned=*/false))
662660
return false;
@@ -674,8 +672,7 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
674672
const Pointer &Arg = S.Stk.pop<Pointer>();
675673
Val = convertBoolVectorToInt(Arg);
676674
} else {
677-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
678-
Val = popToAPSInt(S.Stk, ArgT);
675+
Val = popToAPSInt(S, Call->getArg(0));
679676
}
680677
pushInteger(S, Val.popcount(), Call->getType());
681678
return true;
@@ -684,26 +681,23 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
684681
static bool interp__builtin_parity(InterpState &S, CodePtr OpPC,
685682
const InterpFrame *Frame,
686683
const CallExpr *Call) {
687-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
688-
APSInt Val = popToAPSInt(S.Stk, ArgT);
684+
APSInt Val = popToAPSInt(S, Call->getArg(0));
689685
pushInteger(S, Val.popcount() % 2, Call->getType());
690686
return true;
691687
}
692688

693689
static bool interp__builtin_clrsb(InterpState &S, CodePtr OpPC,
694690
const InterpFrame *Frame,
695691
const CallExpr *Call) {
696-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
697-
APSInt Val = popToAPSInt(S.Stk, ArgT);
692+
APSInt Val = popToAPSInt(S, Call->getArg(0));
698693
pushInteger(S, Val.getBitWidth() - Val.getSignificantBits(), Call->getType());
699694
return true;
700695
}
701696

702697
static bool interp__builtin_bitreverse(InterpState &S, CodePtr OpPC,
703698
const InterpFrame *Frame,
704699
const CallExpr *Call) {
705-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
706-
APSInt Val = popToAPSInt(S.Stk, ArgT);
700+
APSInt Val = popToAPSInt(S, Call->getArg(0));
707701
pushInteger(S, Val.reverseBits(), Call->getType());
708702
return true;
709703
}
@@ -746,11 +740,8 @@ static bool interp__builtin_expect(InterpState &S, CodePtr OpPC,
746740
static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC,
747741
const InterpFrame *Frame,
748742
const CallExpr *Call, bool Right) {
749-
PrimType AmountT = *S.getContext().classify(Call->getArg(1)->getType());
750-
PrimType ValueT = *S.getContext().classify(Call->getArg(0)->getType());
751-
752-
APSInt Amount = popToAPSInt(S.Stk, AmountT);
753-
APSInt Value = popToAPSInt(S.Stk, ValueT);
743+
APSInt Amount = popToAPSInt(S, Call->getArg(1));
744+
APSInt Value = popToAPSInt(S, Call->getArg(0));
754745

755746
APSInt Result;
756747
if (Right)
@@ -767,8 +758,7 @@ static bool interp__builtin_rotate(InterpState &S, CodePtr OpPC,
767758
static bool interp__builtin_ffs(InterpState &S, CodePtr OpPC,
768759
const InterpFrame *Frame,
769760
const CallExpr *Call) {
770-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
771-
APSInt Value = popToAPSInt(S.Stk, ArgT);
761+
APSInt Value = popToAPSInt(S, Call->getArg(0));
772762

773763
uint64_t N = Value.countr_zero();
774764
pushInteger(S, N == Value.getBitWidth() ? 0 : N + 1, Call->getType());
@@ -796,8 +786,7 @@ static bool interp__builtin_move(InterpState &S, CodePtr OpPC,
796786
static bool interp__builtin_eh_return_data_regno(InterpState &S, CodePtr OpPC,
797787
const InterpFrame *Frame,
798788
const CallExpr *Call) {
799-
PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
800-
APSInt Arg = popToAPSInt(S.Stk, ArgT);
789+
APSInt Arg = popToAPSInt(S, Call->getArg(0));
801790

802791
int Result = S.getASTContext().getTargetInfo().getEHDataRegisterNumber(
803792
Arg.getZExtValue());
@@ -971,17 +960,15 @@ static bool interp__builtin_clz(InterpState &S, CodePtr OpPC,
971960
unsigned BuiltinOp) {
972961

973962
std::optional<APSInt> Fallback;
974-
if (BuiltinOp == Builtin::BI__builtin_clzg && Call->getNumArgs() == 2) {
975-
PrimType FallbackT = *S.getContext().classify(Call->getArg(1));
976-
Fallback = popToAPSInt(S.Stk, FallbackT);
977-
}
963+
if (BuiltinOp == Builtin::BI__builtin_clzg && Call->getNumArgs() == 2)
964+
Fallback = popToAPSInt(S, Call->getArg(1));
965+
978966
APSInt Val;
979967
if (Call->getArg(0)->getType()->isExtVectorBoolType()) {
980968
const Pointer &Arg = S.Stk.pop<Pointer>();
981969
Val = convertBoolVectorToInt(Arg);
982970
} else {
983-
PrimType ValT = *S.getContext().classify(Call->getArg(0));
984-
Val = popToAPSInt(S.Stk, ValT);
971+
Val = popToAPSInt(S, Call->getArg(0));
985972
}
986973

987974
// When the argument is 0, the result of GCC builtins is undefined, whereas
@@ -1008,17 +995,15 @@ static bool interp__builtin_ctz(InterpState &S, CodePtr OpPC,
1008995
const InterpFrame *Frame, const CallExpr *Call,
1009996
unsigned BuiltinID) {
1010997
std::optional<APSInt> Fallback;
1011-
if (BuiltinID == Builtin::BI__builtin_ctzg && Call->getNumArgs() == 2) {
1012-
PrimType FallbackT = *S.getContext().classify(Call->getArg(1));
1013-
Fallback = popToAPSInt(S.Stk, FallbackT);
1014-
}
998+
if (BuiltinID == Builtin::BI__builtin_ctzg && Call->getNumArgs() == 2)
999+
Fallback = popToAPSInt(S, Call->getArg(1));
1000+
10151001
APSInt Val;
10161002
if (Call->getArg(0)->getType()->isExtVectorBoolType()) {
10171003
const Pointer &Arg = S.Stk.pop<Pointer>();
10181004
Val = convertBoolVectorToInt(Arg);
10191005
} else {
1020-
PrimType ValT = *S.getContext().classify(Call->getArg(0));
1021-
Val = popToAPSInt(S.Stk, ValT);
1006+
Val = popToAPSInt(S, Call->getArg(0));
10221007
}
10231008

10241009
if (Val == 0) {
@@ -1036,13 +1021,10 @@ static bool interp__builtin_ctz(InterpState &S, CodePtr OpPC,
10361021
static bool interp__builtin_bswap(InterpState &S, CodePtr OpPC,
10371022
const InterpFrame *Frame,
10381023
const CallExpr *Call) {
1039-
PrimType ReturnT = *S.getContext().classify(Call->getType());
1040-
PrimType ValT = *S.getContext().classify(Call->getArg(0));
1041-
const APSInt &Val = popToAPSInt(S.Stk, ValT);
1024+
const APSInt &Val = popToAPSInt(S, Call->getArg(0));
10421025
assert(Val.getActiveBits() <= 64);
10431026

1044-
INT_TYPE_SWITCH(ReturnT,
1045-
{ S.Stk.push<T>(T::from(Val.byteSwap().getZExtValue())); });
1027+
pushInteger(S, Val.byteSwap(), Call->getType());
10461028
return true;
10471029
}
10481030

@@ -1057,9 +1039,8 @@ static bool interp__builtin_atomic_lock_free(InterpState &S, CodePtr OpPC,
10571039
return true;
10581040
};
10591041

1060-
PrimType ValT = *S.getContext().classify(Call->getArg(0));
10611042
const Pointer &Ptr = S.Stk.pop<Pointer>();
1062-
const APSInt &SizeVal = popToAPSInt(S.Stk, ValT);
1043+
const APSInt &SizeVal = popToAPSInt(S, Call->getArg(0));
10631044

10641045
// For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
10651046
// of two less than or equal to the maximum inline atomic width, we know it
@@ -1125,21 +1106,17 @@ static bool interp__builtin_c11_atomic_is_lock_free(InterpState &S,
11251106
CodePtr OpPC,
11261107
const InterpFrame *Frame,
11271108
const CallExpr *Call) {
1128-
PrimType ValT = *S.getContext().classify(Call->getArg(0));
1129-
const APSInt &SizeVal = popToAPSInt(S.Stk, ValT);
1130-
1131-
auto returnBool = [&S](bool Value) -> bool {
1132-
S.Stk.push<Boolean>(Value);
1133-
return true;
1134-
};
1109+
const APSInt &SizeVal = popToAPSInt(S, Call->getArg(0));
11351110

11361111
CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
11371112
if (Size.isPowerOfTwo()) {
11381113
// Check against inlining width.
11391114
unsigned InlineWidthBits =
11401115
S.getASTContext().getTargetInfo().getMaxAtomicInlineWidth();
1141-
if (Size <= S.getASTContext().toCharUnitsFromBits(InlineWidthBits))
1142-
return returnBool(true);
1116+
if (Size <= S.getASTContext().toCharUnitsFromBits(InlineWidthBits)) {
1117+
S.Stk.push<Boolean>(true);
1118+
return true;
1119+
}
11431120
}
11441121

11451122
return false; // returnBool(false);
@@ -1324,10 +1301,8 @@ static bool interp__builtin_ia32_bextr(InterpState &S, CodePtr OpPC,
13241301
!Call->getArg(1)->getType()->isIntegerType())
13251302
return false;
13261303

1327-
PrimType ValT = *S.Ctx.classify(Call->getArg(0));
1328-
PrimType IndexT = *S.Ctx.classify(Call->getArg(1));
1329-
APSInt Index = popToAPSInt(S.Stk, IndexT);
1330-
APSInt Val = popToAPSInt(S.Stk, ValT);
1304+
APSInt Index = popToAPSInt(S, Call->getArg(1));
1305+
APSInt Val = popToAPSInt(S, Call->getArg(0));
13311306

13321307
unsigned BitWidth = Val.getBitWidth();
13331308
uint64_t Shift = Index.extractBitsAsZExtValue(8, 0);

0 commit comments

Comments
 (0)