[PATCH] D112378: [BasicAA] Use ranges for more than one index
Nikita Popov via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 24 07:20:42 PDT 2021
nikic created this revision.
nikic added reviewers: courbet, fhahn, jdoerfert, reames.
Herald added subscribers: jeroen.dobbelaere, arphaman, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
D109746 <https://reviews.llvm.org/D109746> made BasicAA use range information to determine the minimum/maximum GEP offset. However, it was limited to the case of a single variable index. This patch extends support to multiple indices by adding all the ranges together.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D112378
Files:
llvm/lib/Analysis/BasicAliasAnalysis.cpp
llvm/test/Analysis/BasicAA/range.ll
Index: llvm/test/Analysis/BasicAA/range.ll
===================================================================
--- llvm/test/Analysis/BasicAA/range.ll
+++ llvm/test/Analysis/BasicAA/range.ll
@@ -185,8 +185,7 @@
; CHECK: NoAlias: i32* %p.01, i32* %p.2
; CHECK: MayAlias: i32* %p.02, i32* %p.2
; CHECK: NoAlias: i32* %p.01, i32* %p.3
-; TODO: This can be NoAlias.
-; CHECK: MayAlias: i32* %p.02, i32* %p.3
+; CHECK: NoAlias: i32* %p.02, i32* %p.3
define void @multiple(i32* %p, i32* %o1_ptr, i32* %o2_ptr) {
%o1 = load i32, i32* %o1_ptr, !range !0
%o2 = load i32, i32* %o2_ptr, !range !0
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1252,6 +1252,7 @@
APInt GCD;
bool AllNonNegative = DecompGEP1.Offset.isNonNegative();
bool AllNonPositive = DecompGEP1.Offset.isNonPositive();
+ ConstantRange Range = ConstantRange(DecompGEP1.Offset);
for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
const VariableGEPIndex &Index = DecompGEP1.VarIndices[i];
const APInt &Scale = Index.Scale;
@@ -1275,6 +1276,11 @@
AllNonPositive &= (SignKnownZero && Scale.isNonPositive()) ||
(SignKnownOne && Scale.isNonNegative());
}
+
+ Range = Range.add(
+ Index.Val.evaluateWith(computeConstantRange(Index.Val.V, true, &AC,
+ Index.CxtI))
+ .sextOrTrunc(Range.getBitWidth()).smul_fast(ConstantRange(Scale)));
}
// We now have accesses at two offsets from the same base:
@@ -1306,13 +1312,22 @@
(-DecompGEP1.Offset).uge(V1Size.getValue()))
return AliasResult::NoAlias;
+ if (!Range.isEmptySet()) {
+ // We know that Offset >= MinOffset.
+ // (MinOffset >= V2Size) => (Offset >= V2Size) => NoAlias.
+ if (V2Size.hasValue() && Range.getSignedMin().sge(V2Size.getValue()))
+ return AliasResult::NoAlias;
+
+ // We know that Offset <= MaxOffset.
+ // (MaxOffset <= -V1Size) => (Offset <= -V1Size) => NoAlias.
+ if (V1Size.hasValue() && Range.getSignedMax().sle(-V1Size.getValue()))
+ return AliasResult::NoAlias;
+ }
+
if (V1Size.hasValue() && V2Size.hasValue()) {
- // Try to determine the range of values for VarIndex.
- // VarIndexRange is such that:
- // (VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex) &&
- // VarIndexRange.contains(VarIndex)
+ // Try to determine the range of values for VarIndex such that
+ // VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
Optional<APInt> MinAbsVarIndex;
- Optional<ConstantRange> VarIndexRange;
if (DecompGEP1.VarIndices.size() == 1) {
// VarIndex = Scale*V.
const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
@@ -1321,11 +1336,6 @@
// If V != 0 then abs(VarIndex) >= abs(Scale).
MinAbsVarIndex = Var.Scale.abs();
}
- ConstantRange R = Var.Val.evaluateWith(
- computeConstantRange(Var.Val.V, true, &AC, Var.CxtI));
- if (!R.isFullSet() && !R.isEmptySet())
- VarIndexRange = R.sextOrTrunc(Var.Scale.getBitWidth())
- .smul_fast(ConstantRange(Var.Scale));
} else if (DecompGEP1.VarIndices.size() == 2) {
// VarIndex = Scale*V0 + (-Scale)*V1.
// If V0 != V1 then abs(VarIndex) >= abs(Scale).
@@ -1349,21 +1359,6 @@
OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
return AliasResult::NoAlias;
}
-
- if (VarIndexRange) {
- ConstantRange OffsetRange =
- VarIndexRange->add(ConstantRange(DecompGEP1.Offset));
-
- // We know that Offset >= MinOffset.
- // (MinOffset >= V2Size) => (Offset >= V2Size) => NoAlias.
- if (OffsetRange.getSignedMin().sge(V2Size.getValue()))
- return AliasResult::NoAlias;
-
- // We know that Offset <= MaxOffset.
- // (MaxOffset <= -V1Size) => (Offset <= -V1Size) => NoAlias.
- if (OffsetRange.getSignedMax().sle(-V1Size.getValue()))
- return AliasResult::NoAlias;
- }
}
if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112378.381782.patch
Type: text/x-patch
Size: 4369 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211024/46ce0126/attachment.bin>
More information about the llvm-commits
mailing list