[llvm] [BasicAA] Use nuw attribute of GEPs (PR #98608)

Hari Limaye via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 01:42:15 PDT 2024


================
@@ -1233,6 +1232,32 @@ AliasResult BasicAAResult::aliasGEP(
     }
   }
 
+  // If the difference between pointers is Offset +<nuw> Indices (the variable
+  // indices all come from nuw GEPs) then we know that the addition does not
+  // wrap the pointer index type (add nuw) and the constant Offset is a lower
+  // bound on the distance between the pointers. We can then prove NoAlias via
+  // Offset u>= VLeftSize.
+  //    +                +                     +
+  //    | BaseOffset     |   +<nuw> Indices    |
+  //    ---------------->|-------------------->|
+  //    |-->VLeftSize    |                     |-------> VRightSize
+  //   LHS                                    RHS
+  if (!DecompGEP1.VarIndices.empty() &&
+      llvm::all_of(DecompGEP1.VarIndices, [&](const VariableGEPIndex &V) {
+        return V.IsNegated == DecompGEP1.VarIndices.front().IsNegated;
+      })) {
+    APInt Off = DecompGEP1.Offset;
+    bool Swapped = Off.isNegative();
+    LocationSize VLeftSize = Swapped ? V1Size : V2Size;
+    DecomposedGEP &DecompRight = Swapped ? DecompGEP2 : DecompGEP1;
+
+    bool IndicesFromRight = DecompGEP1.VarIndices.front().IsNegated == Swapped;
+    if (IndicesFromRight && DecompRight.NWFlags->hasNoUnsignedWrap())
+      if (!VLeftSize.isScalable() && VLeftSize.hasValue() &&
----------------
hazzlim wrote:

I think you are right - this is the order of the check above for example - I've changed to this order here.

As a related note - I think that this could be extended to support cases where `VLeftSize` is scalable, in the same way as this case for constant-only offsets: https://github.com/llvm/llvm-project/blob/9f4a25e2a7cd176bd4f946dc651bc18c7a2e8c92/llvm/lib/Analysis/BasicAliasAnalysis.cpp#L1195

However it seemed to make sense to leave this to a later commit!

https://github.com/llvm/llvm-project/pull/98608


More information about the llvm-commits mailing list