[llvm] r231741 - Added special handling for inttoptr in CFLAA.

George Burgess IV george.burgess.iv at gmail.com
Mon Mar 9 19:40:06 PDT 2015


Author: gbiv
Date: Mon Mar  9 21:40:06 2015
New Revision: 231741

URL: http://llvm.org/viewvc/llvm-project?rev=231741&view=rev
Log:
Added special handling for inttoptr in CFLAA.

We now treat pointers given to ptrtoint and pointers retrieved from
inttoptr as similar to arguments or globals (can alias anything, etc.)

This solves some of the problems we were having with giving incorrect
results.


Added:
    llvm/trunk/test/Analysis/CFLAliasAnalysis/branch-alias.ll
Modified:
    llvm/trunk/lib/Analysis/CFLAliasAnalysis.cpp

Modified: llvm/trunk/lib/Analysis/CFLAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/CFLAliasAnalysis.cpp?rev=231741&r1=231740&r2=231741&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/CFLAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/CFLAliasAnalysis.cpp Mon Mar  9 21:40:06 2015
@@ -85,11 +85,13 @@ typedef unsigned StratifiedAttr;
 LLVM_CONSTEXPR unsigned MaxStratifiedAttrIndex = NumStratifiedAttrs;
 LLVM_CONSTEXPR unsigned AttrAllIndex = 0;
 LLVM_CONSTEXPR unsigned AttrGlobalIndex = 1;
-LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 2;
+LLVM_CONSTEXPR unsigned AttrUnknownIndex = 2;
+LLVM_CONSTEXPR unsigned AttrFirstArgIndex = 3;
 LLVM_CONSTEXPR unsigned AttrLastArgIndex = MaxStratifiedAttrIndex;
 LLVM_CONSTEXPR unsigned AttrMaxNumArgs = AttrLastArgIndex - AttrFirstArgIndex;
 
 LLVM_CONSTEXPR StratifiedAttr AttrNone = 0;
+LLVM_CONSTEXPR StratifiedAttr AttrUnknown = 1 << AttrUnknownIndex;
 LLVM_CONSTEXPR StratifiedAttr AttrAll = ~AttrNone;
 
 // \brief StratifiedSets call for knowledge of "direction", so this is how we
@@ -263,6 +265,16 @@ public:
     llvm_unreachable("Unsupported instruction encountered");
   }
 
+  void visitPtrToIntInst(PtrToIntInst &Inst) {
+    auto *Ptr = Inst.getOperand(0);
+    Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
+  }
+
+  void visitIntToPtrInst(IntToPtrInst &Inst) {
+    auto *Ptr = &Inst;
+    Output.push_back(Edge(Ptr, Ptr, EdgeType::Assign, AttrUnknown));
+  }
+
   void visitCastInst(CastInst &Inst) {
     Output.push_back(Edge(&Inst, Inst.getOperand(0), EdgeType::Assign,
                           AttrNone));
@@ -931,16 +943,16 @@ static FunctionInfo buildSetsFrom(CFLAli
           break;
         }
 
-        if (Added) {
-          auto Aliasing = Weight.second;
-          if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
-            Aliasing.set(*MaybeCurIndex);
-          if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
-            Aliasing.set(*MaybeOtherIndex);
-          Builder.noteAttributes(CurValue, Aliasing);
-          Builder.noteAttributes(OtherValue, Aliasing);
+        auto Aliasing = Weight.second;
+        if (auto MaybeCurIndex = valueToAttrIndex(CurValue))
+          Aliasing.set(*MaybeCurIndex);
+        if (auto MaybeOtherIndex = valueToAttrIndex(OtherValue))
+          Aliasing.set(*MaybeOtherIndex);
+        Builder.noteAttributes(CurValue, Aliasing);
+        Builder.noteAttributes(OtherValue, Aliasing);
+
+        if (Added)
           Worklist.push_back(OtherNode);
-        }
       }
     }
   }

Added: llvm/trunk/test/Analysis/CFLAliasAnalysis/branch-alias.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/CFLAliasAnalysis/branch-alias.ll?rev=231741&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/CFLAliasAnalysis/branch-alias.ll (added)
+++ llvm/trunk/test/Analysis/CFLAliasAnalysis/branch-alias.ll Mon Mar  9 21:40:06 2015
@@ -0,0 +1,73 @@
+; Makes sure that we give up on some pathological cases with inttoptr/ptrtoint
+;
+; @ptr_test was generated from the following C code:
+;  void ptr_test() {
+;    int* A;
+;    unsigned long RefCopy = 0;
+;    for (int i = 0; i < 8*sizeof(&A); ++i) {
+;      if ((unsigned long)&A & (1UL << i))
+;        RefCopy |= 1UL << i;
+;    }
+;
+;    int** AliasA1 = (int**)RefCopy;
+;    int* ShouldAliasA = *AliasA1;
+;  }
+
+; RUN: opt < %s -cfl-aa -aa-eval -print-may-aliases -disable-output 2>&1 | FileCheck %s
+
+; CHECK: Function: ptr_test
+define void @ptr_test() #0 {
+  ; CHECK: MayAlias: i32** %A, i32** %ShouldAliasA
+  ; CHECK-NOT: %AliasA1
+entry:
+  %A = alloca i32*, align 8
+  %RefCopy = alloca i64, align 8
+  %i = alloca i32, align 4
+  %AliasA1 = alloca i32**, align 8
+  %ShouldAliasA = alloca i32*, align 8
+  store i64 0, i64* %RefCopy, align 8
+  store i32 0, i32* %i, align 4
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.inc, %entry
+  %0 = load i32, i32* %i, align 4
+  %conv = sext i32 %0 to i64
+  %cmp = icmp ult i64 %conv, 64
+  br i1 %cmp, label %for.body, label %for.end
+
+for.body:                                         ; preds = %for.cond
+  %1 = ptrtoint i32** %A to i64
+  %2 = load i32, i32* %i, align 4
+  %sh_prom = zext i32 %2 to i64
+  %shl = shl i64 1, %sh_prom
+  %and = and i64 %1, %shl
+  %tobool = icmp ne i64 %and, 0
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:                                          ; preds = %for.body
+  %3 = load i32, i32* %i, align 4
+  %sh_prom2 = zext i32 %3 to i64
+  %shl3 = shl i64 1, %sh_prom2
+  %4 = load i64, i64* %RefCopy, align 8
+  %or = or i64 %4, %shl3
+  store i64 %or, i64* %RefCopy, align 8
+  br label %if.end
+
+if.end:                                           ; preds = %if.then, %for.body
+  br label %for.inc
+
+for.inc:                                          ; preds = %if.end
+  %5 = load i32, i32* %i, align 4
+  %inc = add nsw i32 %5, 1
+  store i32 %inc, i32* %i, align 4
+  br label %for.cond
+
+for.end:                                          ; preds = %for.cond
+  %6 = load i64, i64* %RefCopy, align 8
+  %7 = inttoptr i64 %6 to i32**
+  store i32** %7, i32*** %AliasA1, align 8
+  %8 = load i32**, i32*** %AliasA1, align 8
+  %9 = load i32*, i32** %8, align 8
+  store i32* %9, i32** %ShouldAliasA, align 8
+  ret void
+}





More information about the llvm-commits mailing list