@@ -1301,6 +1301,23 @@ AliasResult BasicAAResult::aliasGEP(
1301
1301
if (Range1.intersectWith (Range2).isEmptySet ())
1302
1302
return AliasResult::NoAlias;
1303
1303
1304
+ // Check if abs(V*Scale) >= abs(Scale) holds in the presence of
1305
+ // potentially wrapping math.
1306
+ auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
1307
+ if (Var.IsNSW )
1308
+ return true ;
1309
+
1310
+ int ValOrigBW = Var.Val .V ->getType ()->getPrimitiveSizeInBits ();
1311
+ // If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
1312
+ // The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
1313
+ // constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
1314
+ int MaxScaleValueBW = Var.Val .getBitWidth () - ValOrigBW;
1315
+ if (MaxScaleValueBW <= 0 )
1316
+ return false ;
1317
+ return Var.Scale .ule (
1318
+ APInt::getMaxValue (MaxScaleValueBW).zext (Var.Scale .getBitWidth ()));
1319
+ };
1320
+
1304
1321
// Try to determine the range of values for VarIndex such that
1305
1322
// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1306
1323
std::optional<APInt> MinAbsVarIndex;
@@ -1309,22 +1326,6 @@ AliasResult BasicAAResult::aliasGEP(
1309
1326
const VariableGEPIndex &Var = DecompGEP1.VarIndices [0 ];
1310
1327
if (Var.Val .TruncBits == 0 &&
1311
1328
isKnownNonZero (Var.Val .V , SimplifyQuery (DL, DT, &AC, Var.CxtI ))) {
1312
- // Check if abs(V*Scale) >= abs(Scale) holds in the presence of
1313
- // potentially wrapping math.
1314
- auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
1315
- if (Var.IsNSW )
1316
- return true ;
1317
-
1318
- int ValOrigBW = Var.Val .V ->getType ()->getPrimitiveSizeInBits ();
1319
- // If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
1320
- // The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
1321
- // constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
1322
- int MaxScaleValueBW = Var.Val .getBitWidth () - ValOrigBW;
1323
- if (MaxScaleValueBW <= 0 )
1324
- return false ;
1325
- return Var.Scale .ule (
1326
- APInt::getMaxValue (MaxScaleValueBW).zext (Var.Scale .getBitWidth ()));
1327
- };
1328
1329
// Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the
1329
1330
// presence of potentially wrapping math.
1330
1331
if (MultiplyByScaleNoWrap (Var)) {
@@ -1341,6 +1342,7 @@ AliasResult BasicAAResult::aliasGEP(
1341
1342
const VariableGEPIndex &Var1 = DecompGEP1.VarIndices [1 ];
1342
1343
if (Var0.hasNegatedScaleOf (Var1) && Var0.Val .TruncBits == 0 &&
1343
1344
Var0.Val .hasSameCastsAs (Var1.Val ) && !AAQI.MayBeCrossIteration &&
1345
+ MultiplyByScaleNoWrap (Var0) && MultiplyByScaleNoWrap (Var1) &&
1344
1346
isKnownNonEqual (Var0.Val .V , Var1.Val .V ,
1345
1347
SimplifyQuery (DL, DT, &AC, /* CxtI=*/ Var0.CxtI
1346
1348
? Var0.CxtI
0 commit comments