[PATCH] D93162: [BasicAA] Handle known non-zero variable index

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 12 06:14:05 PST 2020


nikic created this revision.
nikic added reviewers: reames, jdoerfert, asbirlea.
Herald added subscribers: arphaman, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

BasicAA currently handles cases like `Scale*V0 + (-Scale)*V1` where `V0 != V1`, but does not handle the simpler case of `Scale*V` with `V != 0`. Add it based on an `isKnownNonZero()` call.

I'm not passing a context instruction for now, because the existing approach of always using `GEP1` for context could result in symmetry issues.

Compile-time impact is close to zero: https://llvm-compile-time-tracker.com/compare.php?from=5e69e2ebad9db2af44362ebe1755c08af002798f&to=2dffc47368b820c651c34156a503943234ea0252&stat=instructions


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93162

Files:
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/test/Analysis/BasicAA/bug.23626.ll
  llvm/test/Analysis/BasicAA/sequential-gep.ll


Index: llvm/test/Analysis/BasicAA/sequential-gep.ll
===================================================================
--- llvm/test/Analysis/BasicAA/sequential-gep.ll
+++ llvm/test/Analysis/BasicAA/sequential-gep.ll
@@ -111,10 +111,9 @@
 }
 
 ; CHECK-LABEL: non_zero_index_simple
-; CHECK: MayAlias: i32* %gep, i32* %p
-; CHECK: MayAlias: i16* %gep.16, i32* %p
+; CHECK: NoAlias: i32* %gep, i32* %p
+; CHECK: NoAlias: i16* %gep.16, i32* %p
 ; CHECK: MayAlias: i32* %p, i64* %gep.64
-; TODO: First two could be NoAlias.
 define void @non_zero_index_simple(i32* %p, i32* %q) {
   %knownnonzero = load i32, i32* %q, !range !0
   %gep = getelementptr i32, i32* %p, i32 %knownnonzero
@@ -125,8 +124,7 @@
 
 ; CHECK-LABEL: non_zero_index_with_offset
 ; CHECK: MayAlias: i32* %gep, i32* %p
-; CHECK: MayAlias: i16* %gep.16, i32* %p
-; TODO: Last could be NoAlias.
+; CHECK: NoAlias: i16* %gep.16, i32* %p
 define void @non_zero_index_with_offset(i32* %p, i32* %q) {
   %knownnonzero = load i32, i32* %q, !range !0
   %p.8 = bitcast i32* %p to i8*
Index: llvm/test/Analysis/BasicAA/bug.23626.ll
===================================================================
--- llvm/test/Analysis/BasicAA/bug.23626.ll
+++ llvm/test/Analysis/BasicAA/bug.23626.ll
@@ -4,9 +4,9 @@
 
 ; CHECK-LABEL: compute1
 ; CHECK: MayAlias:	i32* %arrayidx8, i32* %out
-; CHECK: MayAlias:	i32* %arrayidx11, i32* %out
+; CHECK: NoAlias:	i32* %arrayidx11, i32* %out
 ; CHECK: MayAlias:	i32* %arrayidx11, i32* %arrayidx8
-; CHECK: MayAlias:	i32* %arrayidx14, i32* %out
+; CHECK: NoAlias:	i32* %arrayidx14, i32* %out
 ; CHECK: MayAlias:	i32* %arrayidx14, i32* %arrayidx8
 ; CHECK: MayAlias:	i32* %arrayidx11, i32* %arrayidx14
 define void @compute1(i32 %num.0.lcssa, i32* %out) {
Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1288,8 +1288,12 @@
     if (V1Size.hasValue() && V2Size.hasValue()) {
       // Try to determine whether abs(VarIndex) > 0.
       Optional<APInt> MinAbsVarIndex;
-      // TODO: Could handle single non-zero index as well.
-      if (DecompGEP1.VarIndices.size() == 2) {
+      if (DecompGEP1.VarIndices.size() == 1) {
+        // VarIndex = Scale*V. If V != 0 then abs(VarIndex) >= abs(Scale).
+        const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
+        if (isKnownNonZero(Var.V, DL))
+          MinAbsVarIndex = Var.Scale.abs();
+      } else if (DecompGEP1.VarIndices.size() == 2) {
         // VarIndex = Scale*V0 + (-Scale)*V1.
         // If V0 != V1 then abs(VarIndex) >= abs(Scale).
         // Check that VisitedPhiBBs is empty, to avoid reasoning about


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93162.311389.patch
Type: text/x-patch
Size: 2729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201212/e773d427/attachment.bin>


More information about the llvm-commits mailing list