[PATCH] D16343: [BasicAA] Fix for missing must alias information

Gerolf Hoflehner via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 19 19:18:56 PST 2016


Gerolf created this revision.
Gerolf added a reviewer: hfinkel.
Gerolf added a subscriber: llvm-commits.

BasicAA performs offset computations in 64b mode independent of the
target. For 32b mode this can result in missing must alias information when
the array offsets wrap-around. The fix checks for the target pointersize and
ensures offsets are computed as 32b offsets for 32b targets.

http://reviews.llvm.org/D16343

Files:
  lib/Analysis/BasicAliasAnalysis.cpp
  test/Analysis/BasicAA/noalias-wraparound-bug.ll

Index: test/Analysis/BasicAA/noalias-wraparound-bug.ll
===================================================================
--- /dev/null
+++ test/Analysis/BasicAA/noalias-wraparound-bug.ll
@@ -0,0 +1,24 @@
+; 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"
+
+; We incorrectly returned noalias in the example below for "tmp5" and
+; "tmp12" returning i32 32, since basicaa converted the offsets to 64b
+; and missed the wrap-around
+
+define i32 @foo(i8* %buffer) {
+entry:
+  %tmp2 = getelementptr i8, i8* %buffer, i32 -2071408432
+  %tmp3 = bitcast i8* %tmp2 to i32*
+  %tmp4 = getelementptr i8, i8* %buffer, i32 128
+  %tmp5 = bitcast i8* %tmp4 to i32*
+  store i32 32, i32* %tmp5, align 4
+  %tmp12 = getelementptr i32, i32* %tmp3, i32 -1629631508
+  store i32 28, i32* %tmp12, align 4
+  %tmp13 = getelementptr i8, i8* %buffer, i32 128
+  %tmp14 = bitcast i8* %tmp13 to i32*
+  %tmp2083 = load i32, i32* %tmp14, align 4
+; CHECK: ret i32 28
+  ret i32 %tmp2083
+}
Index: lib/Analysis/BasicAliasAnalysis.cpp
===================================================================
--- lib/Analysis/BasicAliasAnalysis.cpp
+++ lib/Analysis/BasicAliasAnalysis.cpp
@@ -42,11 +42,10 @@
 /// Enable analysis of recursive PHI nodes.
 static cl::opt<bool> EnableRecPhiAnalysis("basicaa-recphi", cl::Hidden,
                                           cl::init(false));
-
+#define DEBUG_TYPE "basicaa"
 /// SearchLimitReached / SearchTimes shows how often the limit of
 /// to decompose GEPs is reached. It will affect the precision
 /// of basic alias analysis.
-#define DEBUG_TYPE "basicaa"
 STATISTIC(SearchLimitReached, "Number of times the limit to "
                               "decompose GEPs is reached");
 STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
@@ -387,6 +386,7 @@
     unsigned AS = GEPOp->getPointerAddressSpace();
     // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
     gep_type_iterator GTI = gep_type_begin(GEPOp);
+    unsigned PointerSize = DL.getPointerSizeInBits(AS);
     for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
          I != E; ++I) {
       const Value *Index = *I;
@@ -398,14 +398,22 @@
           continue;
 
         BaseOffs += DL.getStructLayout(STy)->getElementOffset(FieldNo);
+        if (PointerSize == 32) {
+          int Cast = (int)BaseOffs;
+          BaseOffs = Cast;
+        }
         continue;
       }
 
       // For an array/pointer, add the element offset, explicitly scaled.
       if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
         if (CIdx->isZero())
           continue;
         BaseOffs += DL.getTypeAllocSize(*GTI) * CIdx->getSExtValue();
+        if (PointerSize == 32) {
+          int Cast = (int)BaseOffs;
+          BaseOffs = Cast;
+        }
         continue;
       }
 
@@ -415,7 +423,6 @@
       // If the integer type is smaller than the pointer size, it is implicitly
       // sign extended to pointer size.
       unsigned Width = Index->getType()->getIntegerBitWidth();
-      unsigned PointerSize = DL.getPointerSizeInBits(AS);
       if (PointerSize > Width)
         SExtBits += PointerSize - Width;
 
@@ -996,6 +1003,11 @@
     if (GEP2MaxLookupReached || GEP1MaxLookupReached)
       return MayAlias;
 
+    if (GEP1BaseOffset == GEP2BaseOffset && GEP1VariableIndices.empty() &&
+        GEP2VariableIndices.empty()) {
+      return MustAlias;
+    }
+
     // Subtract the GEP2 pointer from the GEP1 pointer to find out their
     // symbolic difference.
     GEP1BaseOffset -= GEP2BaseOffset;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16343.45336.patch
Type: text/x-patch
Size: 3681 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160120/028c56b4/attachment.bin>


More information about the llvm-commits mailing list