@@ -49,6 +49,13 @@ static APSInt popToAPSInt(InterpStack &Stk, PrimType T) {
49
49
INT_TYPE_SWITCH (T, return Stk.pop <T>().toAPSInt ());
50
50
}
51
51
52
+ static APSInt popToAPSInt (InterpState &S, const Expr *E) {
53
+ return popToAPSInt (S.Stk , *S.getContext ().classify (E->getType ()));
54
+ }
55
+ static APSInt popToAPSInt (InterpState &S, QualType T) {
56
+ return popToAPSInt (S.Stk , *S.getContext ().classify (T));
57
+ }
58
+
52
59
// / Pushes \p Val on the stack as the type given by \p QT.
53
60
static void pushInteger (InterpState &S, const APSInt &Val, QualType QT) {
54
61
assert (QT->isSignedIntegerOrEnumerationType () ||
@@ -1350,11 +1357,8 @@ static bool interp__builtin_ia32_bzhi(InterpState &S, CodePtr OpPC,
1350
1357
!CallType->isIntegerType ())
1351
1358
return false ;
1352
1359
1353
- PrimType ValT = *S.Ctx .classify (Call->getArg (0 ));
1354
- PrimType IndexT = *S.Ctx .classify (Call->getArg (1 ));
1355
-
1356
- APSInt Idx = popToAPSInt (S.Stk , IndexT);
1357
- APSInt Val = popToAPSInt (S.Stk , ValT);
1360
+ APSInt Idx = popToAPSInt (S, Call->getArg (1 ));
1361
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1358
1362
1359
1363
unsigned BitWidth = Val.getBitWidth ();
1360
1364
uint64_t Index = Idx.extractBitsAsZExtValue (8 , 0 );
@@ -1374,7 +1378,7 @@ static bool interp__builtin_ia32_lzcnt(InterpState &S, CodePtr OpPC,
1374
1378
!Call->getArg (0 )->getType ()->isIntegerType ())
1375
1379
return false ;
1376
1380
1377
- APSInt Val = popToAPSInt (S. Stk , *S. Ctx . classify ( Call->getArg (0 ) ));
1381
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1378
1382
pushInteger (S, Val.countLeadingZeros (), CallType);
1379
1383
return true ;
1380
1384
}
@@ -1387,7 +1391,7 @@ static bool interp__builtin_ia32_tzcnt(InterpState &S, CodePtr OpPC,
1387
1391
!Call->getArg (0 )->getType ()->isIntegerType ())
1388
1392
return false ;
1389
1393
1390
- APSInt Val = popToAPSInt (S. Stk , *S. Ctx . classify ( Call->getArg (0 ) ));
1394
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1391
1395
pushInteger (S, Val.countTrailingZeros (), CallType);
1392
1396
return true ;
1393
1397
}
@@ -1399,11 +1403,8 @@ static bool interp__builtin_ia32_pdep(InterpState &S, CodePtr OpPC,
1399
1403
!Call->getArg (1 )->getType ()->isIntegerType ())
1400
1404
return false ;
1401
1405
1402
- PrimType ValT = *S.Ctx .classify (Call->getArg (0 ));
1403
- PrimType MaskT = *S.Ctx .classify (Call->getArg (1 ));
1404
-
1405
- APSInt Mask = popToAPSInt (S.Stk , MaskT);
1406
- APSInt Val = popToAPSInt (S.Stk , ValT);
1406
+ APSInt Mask = popToAPSInt (S, Call->getArg (1 ));
1407
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1407
1408
1408
1409
unsigned BitWidth = Val.getBitWidth ();
1409
1410
APInt Result = APInt::getZero (BitWidth);
@@ -1422,11 +1423,8 @@ static bool interp__builtin_ia32_pext(InterpState &S, CodePtr OpPC,
1422
1423
!Call->getArg (1 )->getType ()->isIntegerType ())
1423
1424
return false ;
1424
1425
1425
- PrimType ValT = *S.Ctx .classify (Call->getArg (0 ));
1426
- PrimType MaskT = *S.Ctx .classify (Call->getArg (1 ));
1427
-
1428
- APSInt Mask = popToAPSInt (S.Stk , MaskT);
1429
- APSInt Val = popToAPSInt (S.Stk , ValT);
1426
+ APSInt Mask = popToAPSInt (S, Call->getArg (1 ));
1427
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1430
1428
1431
1429
unsigned BitWidth = Val.getBitWidth ();
1432
1430
APInt Result = APInt::getZero (BitWidth);
@@ -1451,12 +1449,9 @@ static bool interp__builtin_ia32_addcarry_subborrow(InterpState &S,
1451
1449
1452
1450
const Pointer &CarryOutPtr = S.Stk .pop <Pointer>();
1453
1451
1454
- PrimType CarryInT = *S.getContext ().classify (Call->getArg (0 ));
1455
- PrimType LHST = *S.getContext ().classify (Call->getArg (1 ));
1456
- PrimType RHST = *S.getContext ().classify (Call->getArg (2 ));
1457
- APSInt RHS = popToAPSInt (S.Stk , RHST);
1458
- APSInt LHS = popToAPSInt (S.Stk , LHST);
1459
- APSInt CarryIn = popToAPSInt (S.Stk , CarryInT);
1452
+ APSInt RHS = popToAPSInt (S, Call->getArg (2 ));
1453
+ APSInt LHS = popToAPSInt (S, Call->getArg (1 ));
1454
+ APSInt CarryIn = popToAPSInt (S, Call->getArg (0 ));
1460
1455
1461
1456
bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
1462
1457
BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
@@ -1546,7 +1541,7 @@ static bool interp__builtin_operator_new(InterpState &S, CodePtr OpPC,
1546
1541
discard (S.Stk , *S.getContext ().classify (Arg));
1547
1542
}
1548
1543
1549
- APSInt Bytes = popToAPSInt (S. Stk , *S. getContext (). classify ( Call->getArg (0 ) ));
1544
+ APSInt Bytes = popToAPSInt (S, Call->getArg (0 ));
1550
1545
CharUnits ElemSize = S.getASTContext ().getTypeSizeInChars (ElemType);
1551
1546
assert (!ElemSize.isZero ());
1552
1547
// Divide the number of bytes by sizeof(ElemType), so we get the number of
@@ -1740,9 +1735,7 @@ static bool interp__builtin_elementwise_abs(InterpState &S, CodePtr OpPC,
1740
1735
assert (Call->getNumArgs () == 1 );
1741
1736
QualType Ty = Call->getArg (0 )->getType ();
1742
1737
if (Ty->isIntegerType ()) {
1743
- PrimType ArgT = *S.getContext ().classify (Call->getArg (0 )->getType ());
1744
- APSInt Val = popToAPSInt (S.Stk , ArgT);
1745
-
1738
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1746
1739
pushInteger (S, Val.abs (), Call->getType ());
1747
1740
return true ;
1748
1741
}
@@ -1791,8 +1784,7 @@ static bool interp__builtin_elementwise_popcount(InterpState &S, CodePtr OpPC,
1791
1784
unsigned BuiltinID) {
1792
1785
assert (Call->getNumArgs () == 1 );
1793
1786
if (Call->getArg (0 )->getType ()->isIntegerType ()) {
1794
- PrimType ArgT = *S.getContext ().classify (Call->getArg (0 )->getType ());
1795
- APSInt Val = popToAPSInt (S.Stk , ArgT);
1787
+ APSInt Val = popToAPSInt (S, Call->getArg (0 ));
1796
1788
1797
1789
if (BuiltinID == Builtin::BI__builtin_elementwise_popcount) {
1798
1790
pushInteger (S, Val.popcount (), Call->getType ());
@@ -1923,8 +1915,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
1923
1915
const CallExpr *Call, unsigned ID) {
1924
1916
assert (Call->getNumArgs () == 3 );
1925
1917
const ASTContext &ASTCtx = S.getASTContext ();
1926
- PrimType SizeT = *S.getContext ().classify (Call->getArg (2 ));
1927
- APSInt Size = popToAPSInt (S.Stk , SizeT);
1918
+ APSInt Size = popToAPSInt (S, Call->getArg (2 ));
1928
1919
const Pointer SrcPtr = S.Stk .pop <Pointer>();
1929
1920
const Pointer DestPtr = S.Stk .pop <Pointer>();
1930
1921
@@ -2090,8 +2081,7 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
2090
2081
const InterpFrame *Frame,
2091
2082
const CallExpr *Call, unsigned ID) {
2092
2083
assert (Call->getNumArgs () == 3 );
2093
- PrimType SizeT = *S.getContext ().classify (Call->getArg (2 ));
2094
- const APSInt &Size = popToAPSInt (S.Stk , SizeT);
2084
+ const APSInt &Size = popToAPSInt (S, Call->getArg (2 ));
2095
2085
const Pointer &PtrB = S.Stk .pop <Pointer>();
2096
2086
const Pointer &PtrA = S.Stk .pop <Pointer>();
2097
2087
@@ -2206,12 +2196,10 @@ static bool interp__builtin_memchr(InterpState &S, CodePtr OpPC,
2206
2196
diagnoseNonConstexprBuiltin (S, OpPC, ID);
2207
2197
2208
2198
std::optional<APSInt> MaxLength;
2209
- PrimType DesiredT = *S.getContext ().classify (Call->getArg (1 ));
2210
- if (Call->getNumArgs () == 3 ) {
2211
- PrimType MaxT = *S.getContext ().classify (Call->getArg (2 ));
2212
- MaxLength = popToAPSInt (S.Stk , MaxT);
2213
- }
2214
- APSInt Desired = popToAPSInt (S.Stk , DesiredT);
2199
+ if (Call->getNumArgs () == 3 )
2200
+ MaxLength = popToAPSInt (S, Call->getArg (2 ));
2201
+
2202
+ APSInt Desired = popToAPSInt (S, Call->getArg (1 ));
2215
2203
const Pointer &Ptr = S.Stk .pop <Pointer>();
2216
2204
2217
2205
if (MaxLength && MaxLength->isZero ()) {
@@ -2428,13 +2416,12 @@ static bool interp__builtin_object_size(InterpState &S, CodePtr OpPC,
2428
2416
const InterpFrame *Frame,
2429
2417
const CallExpr *Call) {
2430
2418
const ASTContext &ASTCtx = S.getASTContext ();
2431
- PrimType KindT = *S.getContext ().classify (Call->getArg (1 ));
2432
2419
// From the GCC docs:
2433
2420
// Kind is an integer constant from 0 to 3. If the least significant bit is
2434
2421
// clear, objects are whole variables. If it is set, a closest surrounding
2435
2422
// subobject is considered the object a pointer points to. The second bit
2436
2423
// determines if maximum or minimum of remaining bytes is computed.
2437
- unsigned Kind = popToAPSInt (S. Stk , KindT ).getZExtValue ();
2424
+ unsigned Kind = popToAPSInt (S, Call-> getArg ( 1 ) ).getZExtValue ();
2438
2425
assert (Kind <= 3 && " unexpected kind" );
2439
2426
bool UseFieldDesc = (Kind & 1u );
2440
2427
bool ReportMinimum = (Kind & 2u );
@@ -2562,10 +2549,8 @@ static bool interp__builtin_elementwise_int_binop(
2562
2549
// Single integer case.
2563
2550
if (!Call->getArg (0 )->getType ()->isVectorType ()) {
2564
2551
assert (!Call->getArg (1 )->getType ()->isVectorType ());
2565
- APSInt RHS = popToAPSInt (
2566
- S.Stk , *S.getContext ().classify (Call->getArg (1 )->getType ()));
2567
- APSInt LHS = popToAPSInt (
2568
- S.Stk , *S.getContext ().classify (Call->getArg (0 )->getType ()));
2552
+ APSInt RHS = popToAPSInt (S, Call->getArg (1 ));
2553
+ APSInt LHS = popToAPSInt (S, Call->getArg (0 ));
2569
2554
APInt Result = Fn (LHS, RHS);
2570
2555
pushInteger (S, APSInt (std::move (Result), !LHS.isSigned ()), Call->getType ());
2571
2556
return true ;
@@ -2581,8 +2566,7 @@ static bool interp__builtin_elementwise_int_binop(
2581
2566
if (!Call->getArg (1 )->getType ()->isVectorType ()) {
2582
2567
assert (Call->getArg (1 )->getType ()->isIntegralOrEnumerationType ());
2583
2568
2584
- APSInt RHS = popToAPSInt (
2585
- S.Stk , *S.getContext ().classify (Call->getArg (1 )->getType ()));
2569
+ APSInt RHS = popToAPSInt (S, Call->getArg (1 ));
2586
2570
const Pointer &LHS = S.Stk .pop <Pointer>();
2587
2571
const Pointer &Dst = S.Stk .peek <Pointer>();
2588
2572
@@ -2635,10 +2619,8 @@ static bool interp__builtin_elementwise_maxmin(InterpState &S, CodePtr OpPC,
2635
2619
2636
2620
if (!Arg0Type->isVectorType ()) {
2637
2621
assert (!Call->getArg (1 )->getType ()->isVectorType ());
2638
- APSInt RHS = popToAPSInt (
2639
- S.Stk , *S.getContext ().classify (Call->getArg (1 )->getType ()));
2640
- APSInt LHS = popToAPSInt (
2641
- S.Stk , *S.getContext ().classify (Call->getArg (0 )->getType ()));
2622
+ APSInt RHS = popToAPSInt (S, Call->getArg (1 ));
2623
+ APSInt LHS = popToAPSInt (S, Arg0Type);
2642
2624
APInt Result;
2643
2625
if (BuiltinID == Builtin::BI__builtin_elementwise_max) {
2644
2626
Result = std::max (LHS, RHS);
@@ -2808,8 +2790,7 @@ static bool interp__builtin_select(InterpState &S, CodePtr OpPC,
2808
2790
const CallExpr *Call) {
2809
2791
const Pointer &RHS = S.Stk .pop <Pointer>();
2810
2792
const Pointer &LHS = S.Stk .pop <Pointer>();
2811
- PrimType MaskT = *S.getContext ().classify (Call->getArg (0 ));
2812
- APSInt Mask = popToAPSInt (S.Stk , MaskT);
2793
+ APSInt Mask = popToAPSInt (S, Call->getArg (0 ));
2813
2794
const Pointer &Dst = S.Stk .peek <Pointer>();
2814
2795
2815
2796
assert (LHS.getNumElems () == RHS.getNumElems ());
@@ -2839,8 +2820,7 @@ static bool interp__builtin_select(InterpState &S, CodePtr OpPC,
2839
2820
2840
2821
static bool interp__builtin_blend (InterpState &S, CodePtr OpPC,
2841
2822
const CallExpr *Call) {
2842
- PrimType MaskT = *S.getContext ().classify (Call->getArg (2 ));
2843
- APSInt Mask = popToAPSInt (S.Stk , MaskT);
2823
+ APSInt Mask = popToAPSInt (S, Call->getArg (2 ));
2844
2824
const Pointer &TrueVec = S.Stk .pop <Pointer>();
2845
2825
const Pointer &FalseVec = S.Stk .pop <Pointer>();
2846
2826
const Pointer &Dst = S.Stk .peek <Pointer>();
@@ -2878,14 +2858,12 @@ static bool interp__builtin_elementwise_triop(
2878
2858
assert (Call->getNumArgs () == 3 );
2879
2859
2880
2860
QualType Arg0Type = Call->getArg (0 )->getType ();
2881
- QualType Arg1Type = Call->getArg (1 )->getType ();
2882
2861
QualType Arg2Type = Call->getArg (2 )->getType ();
2883
-
2884
2862
// Non-vector integer types.
2885
2863
if (!Arg0Type->isVectorType ()) {
2886
- const APSInt &Op2 = popToAPSInt (S. Stk , *S. getContext (). classify ( Arg2Type) );
2887
- const APSInt &Op1 = popToAPSInt (S. Stk , *S. getContext (). classify (Arg1Type ));
2888
- const APSInt &Op0 = popToAPSInt (S. Stk , *S. getContext (). classify ( Arg0Type) );
2864
+ const APSInt &Op2 = popToAPSInt (S, Arg2Type);
2865
+ const APSInt &Op1 = popToAPSInt (S, Call-> getArg ( 1 ));
2866
+ const APSInt &Op0 = popToAPSInt (S, Arg0Type);
2889
2867
APSInt Result = APSInt (Fn (Op0, Op1, Op2), Op0.isUnsigned ());
2890
2868
pushInteger (S, Result, Call->getType ());
2891
2869
return true ;
@@ -2898,8 +2876,7 @@ static bool interp__builtin_elementwise_triop(
2898
2876
2899
2877
// Vector + Vector + Scalar case.
2900
2878
if (!Arg2Type->isVectorType ()) {
2901
- APSInt Op2 = popToAPSInt (
2902
- S.Stk , *S.getContext ().classify (Call->getArg (2 )->getType ()));
2879
+ APSInt Op2 = popToAPSInt (S, Arg2Type);
2903
2880
2904
2881
const Pointer &Op1 = S.Stk .pop <Pointer>();
2905
2882
const Pointer &Op0 = S.Stk .pop <Pointer>();
0 commit comments