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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 05:04:42 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() &&
----------------
david-arm wrote:

nit: I must admit I haven't looked into the LocationSize class in great detail, but  it looks like asking the question `isScalable` only makes sense if `hasValue` returns true? So even though there is nothing technically wrong with the code it might be more logical to reorder the checks, i.e.

```
if (VLeftSize.hasValue() && !VLeftSize.isScalable()
```

However, I could have misunderstood the semantics!

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


More information about the llvm-commits mailing list