Skip to content

Commit c00e9c6

Browse files
committed
[BasicAA] Check known access sizes earlier (NFC)
All heuristics for variable accesses require both access sizes to be known, so check this once at the start, rather than for each particular heuristic.
1 parent 0b6ed92 commit c00e9c6

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,10 @@ AliasResult BasicAAResult::aliasGEP(
12291229
return AliasResult::NoAlias;
12301230
}
12311231

1232+
// We need to know both acess sizes for all the following heuristics.
1233+
if (!V1Size.hasValue() || !V2Size.hasValue())
1234+
return AliasResult::MayAlias;
1235+
12321236
APInt GCD;
12331237
ConstantRange OffsetRange = ConstantRange(DecompGEP1.Offset);
12341238
for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
@@ -1270,58 +1274,53 @@ AliasResult BasicAAResult::aliasGEP(
12701274
APInt ModOffset = DecompGEP1.Offset.srem(GCD);
12711275
if (ModOffset.isNegative())
12721276
ModOffset += GCD; // We want mod, not rem.
1273-
if (V1Size.hasValue() && V2Size.hasValue() &&
1274-
ModOffset.uge(V2Size.getValue()) &&
1277+
if (ModOffset.uge(V2Size.getValue()) &&
12751278
(GCD - ModOffset).uge(V1Size.getValue()))
12761279
return AliasResult::NoAlias;
12771280

1278-
if (V1Size.hasValue() && V2Size.hasValue()) {
1279-
// Compute ranges of potentially accessed bytes for both accesses. If the
1280-
// interseciton is empty, there can be no overlap.
1281-
unsigned BW = OffsetRange.getBitWidth();
1282-
ConstantRange Range1 = OffsetRange.add(
1283-
ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
1284-
ConstantRange Range2 =
1285-
ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
1286-
if (Range1.intersectWith(Range2).isEmptySet())
1287-
return AliasResult::NoAlias;
1288-
}
1281+
// Compute ranges of potentially accessed bytes for both accesses. If the
1282+
// interseciton is empty, there can be no overlap.
1283+
unsigned BW = OffsetRange.getBitWidth();
1284+
ConstantRange Range1 = OffsetRange.add(
1285+
ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
1286+
ConstantRange Range2 =
1287+
ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
1288+
if (Range1.intersectWith(Range2).isEmptySet())
1289+
return AliasResult::NoAlias;
12891290

1290-
if (V1Size.hasValue() && V2Size.hasValue()) {
1291-
// Try to determine the range of values for VarIndex such that
1292-
// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1293-
Optional<APInt> MinAbsVarIndex;
1294-
if (DecompGEP1.VarIndices.size() == 1) {
1295-
// VarIndex = Scale*V.
1296-
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1297-
if (Var.Val.TruncBits == 0 &&
1298-
isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
1299-
// If V != 0 then abs(VarIndex) >= abs(Scale).
1300-
MinAbsVarIndex = Var.Scale.abs();
1301-
}
1302-
} else if (DecompGEP1.VarIndices.size() == 2) {
1303-
// VarIndex = Scale*V0 + (-Scale)*V1.
1304-
// If V0 != V1 then abs(VarIndex) >= abs(Scale).
1305-
// Check that VisitedPhiBBs is empty, to avoid reasoning about
1306-
// inequality of values across loop iterations.
1307-
const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1308-
const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1309-
if (Var0.Scale == -Var1.Scale && Var0.Val.TruncBits == 0 &&
1310-
Var0.Val.hasSameCastsAs(Var1.Val) && VisitedPhiBBs.empty() &&
1311-
isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr,
1312-
DT))
1313-
MinAbsVarIndex = Var0.Scale.abs();
1291+
// Try to determine the range of values for VarIndex such that
1292+
// VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1293+
Optional<APInt> MinAbsVarIndex;
1294+
if (DecompGEP1.VarIndices.size() == 1) {
1295+
// VarIndex = Scale*V.
1296+
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1297+
if (Var.Val.TruncBits == 0 &&
1298+
isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
1299+
// If V != 0 then abs(VarIndex) >= abs(Scale).
1300+
MinAbsVarIndex = Var.Scale.abs();
13141301
}
1302+
} else if (DecompGEP1.VarIndices.size() == 2) {
1303+
// VarIndex = Scale*V0 + (-Scale)*V1.
1304+
// If V0 != V1 then abs(VarIndex) >= abs(Scale).
1305+
// Check that VisitedPhiBBs is empty, to avoid reasoning about
1306+
// inequality of values across loop iterations.
1307+
const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1308+
const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1309+
if (Var0.Scale == -Var1.Scale && Var0.Val.TruncBits == 0 &&
1310+
Var0.Val.hasSameCastsAs(Var1.Val) && VisitedPhiBBs.empty() &&
1311+
isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr,
1312+
DT))
1313+
MinAbsVarIndex = Var0.Scale.abs();
1314+
}
13151315

1316-
if (MinAbsVarIndex) {
1317-
// The constant offset will have added at least +/-MinAbsVarIndex to it.
1318-
APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1319-
APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1320-
// We know that Offset <= OffsetLo || Offset >= OffsetHi
1321-
if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
1322-
OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
1323-
return AliasResult::NoAlias;
1324-
}
1316+
if (MinAbsVarIndex) {
1317+
// The constant offset will have added at least +/-MinAbsVarIndex to it.
1318+
APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1319+
APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1320+
// We know that Offset <= OffsetLo || Offset >= OffsetHi
1321+
if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
1322+
OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
1323+
return AliasResult::NoAlias;
13251324
}
13261325

13271326
if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT))

0 commit comments

Comments
 (0)