[llvm] r284908 - [BasicAA] Fix - missed alias in GEP expressions

Gerolf Hoflehner via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 21 19:41:39 PDT 2016


Author: ghoflehner
Date: Fri Oct 21 21:41:39 2016
New Revision: 284908

URL: http://llvm.org/viewvc/llvm-project?rev=284908&view=rev
Log:
[BasicAA] Fix -  missed alias in GEP expressions

In BasicAA GEP operand values get adjusted ("wrap-around") based on the
pointersize. Otherwise, in non-64b modes, AA could report false negatives.
However, a wrap-around is valid only for a fully evaluated expression.
It had been introduced to fix an alias problem in
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160118/326163.html.
This commit restricts the wrap-around to constant gep operands only where the
value is known at compile-time.


Added:
    llvm/trunk/test/Analysis/BasicAA/gep-and-alias.ll
Modified:
    llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp

Modified: llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=284908&r1=284907&r2=284908&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp Fri Oct 21 21:41:39 2016
@@ -409,6 +409,8 @@ bool BasicAAResult::DecomposeGEPExpressi
     // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
     gep_type_iterator GTI = gep_type_begin(GEPOp);
     unsigned PointerSize = DL.getPointerSizeInBits(AS);
+    // Assume all GEP operands are constants until proven otherwise.
+    bool GepHasConstantOffset = true;
     for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
          I != E; ++I) {
       const Value *Index = *I;
@@ -433,6 +435,8 @@ bool BasicAAResult::DecomposeGEPExpressi
         continue;
       }
 
+      GepHasConstantOffset = false;
+
       uint64_t Scale = DL.getTypeAllocSize(*GTI);
       unsigned ZExtBits = 0, SExtBits = 0;
 
@@ -458,7 +462,7 @@ bool BasicAAResult::DecomposeGEPExpressi
       //   A[x][x] -> x*16 + x*4 -> x*20
       // This also ensures that 'x' only appears in the index list once.
       for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
-        if (Decomposed.VarIndices[i].V == Index && 
+        if (Decomposed.VarIndices[i].V == Index &&
             Decomposed.VarIndices[i].ZExtBits == ZExtBits &&
             Decomposed.VarIndices[i].SExtBits == SExtBits) {
           Scale += Decomposed.VarIndices[i].Scale;
@@ -479,10 +483,12 @@ bool BasicAAResult::DecomposeGEPExpressi
     }
 
     // Take care of wrap-arounds
-    Decomposed.StructOffset =
-      adjustToPointerSize(Decomposed.StructOffset, PointerSize);
-    Decomposed.OtherOffset =
-      adjustToPointerSize(Decomposed.OtherOffset, PointerSize);
+    if (GepHasConstantOffset) {
+      Decomposed.StructOffset =
+          adjustToPointerSize(Decomposed.StructOffset, PointerSize);
+      Decomposed.OtherOffset =
+          adjustToPointerSize(Decomposed.OtherOffset, PointerSize);
+    }
 
     // Analyze the base pointer next.
     V = GEPOp->getOperand(0);

Added: llvm/trunk/test/Analysis/BasicAA/gep-and-alias.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BasicAA/gep-and-alias.ll?rev=284908&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/BasicAA/gep-and-alias.ll (added)
+++ llvm/trunk/test/Analysis/BasicAA/gep-and-alias.ll Fri Oct 21 21:41:39 2016
@@ -0,0 +1,43 @@
+; RUN: opt -S -basicaa -gvn < %s | FileCheck %s
+
+target datalayout = "e-m:o-p:32:32-f64:32:64-f80:128-n8:16:32-S128"
+target triple = "i386-apple-macosx10.6.0"
+
+; The load and store address in the loop body could alias so the load
+; can't be hoisted above the store and out of the loop.
+
+declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i32, i1)
+
+define i32 @foo(i32 %x, i32 %z, i32 %n) {
+entry:
+  %pool = alloca [59 x i32], align 4
+  %tmp = bitcast [59 x i32]* %pool to i8*
+  call void @llvm.memset.p0i8.i32(i8* nonnull %tmp, i8 0, i32 236, i32 4, i1 false)
+  %cmp3 = icmp eq i32 %n, 0
+  br i1 %cmp3, label %for.end, label %for.body.lr.ph
+
+for.body.lr.ph:                                   ; preds = %entry
+  %add = add i32 %z, %x
+  %and = and i32 %add, 2147483647
+  %sub = add nsw i32 %and, -2137521902
+  %arrayidx = getelementptr inbounds [59 x i32], [59 x i32]* %pool, i32 0, i32 %sub
+  %arrayidx1 = getelementptr inbounds [59 x i32], [59 x i32]* %pool, i32 0, i32 42
+  br label %for.body
+
+for.body:                                         ; preds = %for.body.lr.ph, %for.body
+  %i.04 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
+  store i32 %i.04, i32* %arrayidx, align 4
+  %tmp1 = load i32, i32* %arrayidx1, align 4
+  %inc = add nuw i32 %i.04, 1
+  %exitcond = icmp ne i32 %inc, %n
+  br i1 %exitcond, label %for.body, label %for.end.loopexit
+
+for.end.loopexit:                                 ; preds = %for.body
+  %lcssa = phi i32 [ %tmp1, %for.body ]
+  br label %for.end
+
+for.end:                                          ; preds = %for.end.loopexit, %entry
+  %s = phi i32 [ 0, %entry ], [ %lcssa, %for.end.loopexit ]
+; CHECK: ret i32 %s
+  ret i32 %s
+}




More information about the llvm-commits mailing list