[llvm] 728803e - [BasicAA] Use index difference to detect GEPs with identical indexes

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 14 08:25:10 PST 2021


Author: Nikita Popov
Date: 2021-02-14T17:11:03+01:00
New Revision: 728803ed74e26b370600002dcffe4994ce3ec37a

URL: https://github.com/llvm/llvm-project/commit/728803ed74e26b370600002dcffe4994ce3ec37a
DIFF: https://github.com/llvm/llvm-project/commit/728803ed74e26b370600002dcffe4994ce3ec37a.diff

LOG: [BasicAA] Use index difference to detect GEPs with identical indexes

We currently detect GEPs that have exactly the same indexes by
comparing the Offsets and VarIndices. However, the latter implicitly
performs equality comparisons between two values, which is not
generally legal inside BasicAA, due to the possibility of comparisons
across phi cycles.

I believe that in this particular instance this actually ends up being
unproblematic, at least I wasn't able to come up with any cases that
could result in an incorrect root query result.

In the interest of being defensive, compute GetIndexDifference earlier
(which knows how to handle phi cycles properly) and use the result of
that to determine whether the offsets are identical.

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/BasicAliasAnalysis.h
    llvm/lib/Analysis/BasicAliasAnalysis.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h
index 46b8cd1f3a88..dbd169b5517b 100644
--- a/llvm/include/llvm/Analysis/BasicAliasAnalysis.h
+++ b/llvm/include/llvm/Analysis/BasicAliasAnalysis.h
@@ -120,15 +120,6 @@ class BasicAAResult : public AAResultBase<BasicAAResult> {
     // Context instruction to use when querying information about this index.
     const Instruction *CxtI;
 
-    bool operator==(const VariableGEPIndex &Other) const {
-      return V == Other.V && ZExtBits == Other.ZExtBits &&
-             SExtBits == Other.SExtBits && Scale == Other.Scale;
-    }
-
-    bool operator!=(const VariableGEPIndex &Other) const {
-      return !operator==(Other);
-    }
-
     void dump() const {
       print(dbgs());
       dbgs() << "\n";

diff  --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index a3add55a4f1c..8f07aa664392 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1110,6 +1110,12 @@ AliasResult BasicAAResult::aliasGEP(
     // direction.
     if (isGEPBaseAtNegativeOffset(GEP2, DecompGEP2, DecompGEP1, V1Size))
       return NoAlias;
+
+    // Subtract the GEP2 pointer from the GEP1 pointer to find out their
+    // symbolic 
diff erence.
+    DecompGEP1.Offset -= DecompGEP2.Offset;
+    GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
+
     // Do the base pointers alias?
     AliasResult BaseAlias = getBestAAResults().alias(
         MemoryLocation::getBeforeOrAfter(UnderlyingV1),
@@ -1117,8 +1123,8 @@ AliasResult BasicAAResult::aliasGEP(
 
     // For GEPs with identical offsets, we can preserve the size and AAInfo
     // when performing the alias check on the underlying objects.
-    if (BaseAlias == MayAlias && DecompGEP1.Offset == DecompGEP2.Offset &&
-        DecompGEP1.VarIndices == DecompGEP2.VarIndices) {
+    if (BaseAlias == MayAlias && DecompGEP1.Offset == 0 &&
+        DecompGEP1.VarIndices.empty()) {
       AliasResult PreciseBaseAlias = getBestAAResults().alias(
           MemoryLocation(UnderlyingV1, V1Size, V1AAInfo),
           MemoryLocation(UnderlyingV2, V2Size, V2AAInfo), AAQI);
@@ -1132,12 +1138,6 @@ AliasResult BasicAAResult::aliasGEP(
       assert(BaseAlias == NoAlias || BaseAlias == MayAlias);
       return BaseAlias;
     }
-
-    // Subtract the GEP2 pointer from the GEP1 pointer to find out their
-    // symbolic 
diff erence.
-    DecompGEP1.Offset -= DecompGEP2.Offset;
-    GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
-
   } else {
     // Check to see if these two pointers are related by the getelementptr
     // instruction.  If one pointer is a GEP with a non-zero index of the other


        


More information about the llvm-commits mailing list