[PATCH] D112486: [BasicAA] Make range check more precise

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 27 03:41:22 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG9bc7e543b4ef: [BasicAA] Make range check more precise (authored by nikic).

Changed prior to commit:
  https://reviews.llvm.org/D112486?vs=382104&id=382592#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112486/new/

https://reviews.llvm.org/D112486

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
@@ -196,13 +196,13 @@
   ret void
 }
 
-; TODO: p.neg1 and p.o.1 don't alias, even though the addition o+1 may overflow.
+; p.neg1 and p.o.1 don't alias, even though the addition o+1 may overflow.
 ; While it makes INT_MIN a possible offset, offset -1 is not possible.
 ; CHECK-LABEL: Function: benign_overflow
 ; CHECK: MayAlias: i8* %p, i8* %p.o
 ; CHECK: MayAlias: i8* %p.neg1, i8* %p.o
 ; CHECK: MayAlias: i8* %p, i8* %p.o.1
-; CHECK: MayAlias: i8* %p.neg1, i8* %p.o.1
+; CHECK: NoAlias: i8* %p.neg1, i8* %p.o.1
 ; CHECK: NoAlias:  i8* %p.o, i8* %p.o.1
 define void @benign_overflow(i8* %p, i64 %o) {
   %c = icmp sge i64 %o, -1
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1252,7 +1252,7 @@
     APInt GCD;
     bool AllNonNegative = DecompGEP1.Offset.isNonNegative();
     bool AllNonPositive = DecompGEP1.Offset.isNonPositive();
-    ConstantRange Range = ConstantRange(DecompGEP1.Offset);
+    ConstantRange OffsetRange = 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;
@@ -1277,12 +1277,12 @@
                           (SignKnownOne && Scale.isNonNegative());
       }
 
-      assert(Range.getBitWidth() == Scale.getBitWidth() &&
+      assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
              "Bit widths are normalized to MaxPointerSize");
-      Range = Range.add(Index.Val
+      OffsetRange = OffsetRange.add(Index.Val
                             .evaluateWith(computeConstantRange(
                                 Index.Val.V, true, &AC, Index.CxtI))
-                            .sextOrTrunc(Range.getBitWidth())
+                            .sextOrTrunc(OffsetRange.getBitWidth())
                             .smul_fast(ConstantRange(Scale)));
     }
 
@@ -1315,15 +1315,15 @@
         (-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()))
+    if (V1Size.hasValue() && V2Size.hasValue()) {
+      // Compute ranges of potentially accessed bytes for both accesses. If the
+      // interseciton is empty, there can be no overlap.
+      unsigned BW = OffsetRange.getBitWidth();
+      ConstantRange Range1 = OffsetRange.add(
+          ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
+      ConstantRange Range2 =
+          ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
+      if (Range1.intersectWith(Range2).isEmptySet())
         return AliasResult::NoAlias;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112486.382592.patch
Type: text/x-patch
Size: 3336 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211027/94cdb0e6/attachment.bin>


More information about the llvm-commits mailing list