<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Dec 1, 2014 at 3:38 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: hfinkel<br>
Date: Mon Dec  1 17:38:06 2014<br>
New Revision: 223093<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=223093&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=223093&view=rev</a><br>
Log:<br>
Simplify pointer comparisons involving memory allocation functions<br>
<br>
System memory allocation functions, which are identified at the IR level by the<br>
noalias attribute on the return value, must return a pointer into a memory region<br>
disjoint from any other memory accessible to the caller. We can use this<br>
property to simplify pointer comparisons between allocated memory and local<br>
stack addresses and the addresses of global variables. Neither the stack nor<br>
global variables can overlap with the region used by the memory allocator.<br></blockquote><div><br></div><div>I don't believe this change is correct (at least, in theory) as it stands. Here are some examples:</div><div><br></div><div>1) I allocate memory with malloc, then use that memory as the stack for a thread. Per POSIX application-managed stack semantics (<a href="http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_09.html#tag_02_09_08">http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_09.html#tag_02_09_08</a>), the application isn't allowed to use the memory allocated with malloc any more (except through the thread's stack), but there doesn't seem to be any prohibition against comparing pointers to it. I could compare an alloca pointer to the malloc'd buffer to determine whether I'm in that thread, for instance.</div><div><br></div><div>1b) I allocate memory with malloc, then I free it, then I perform a dynamic alloca. On my platform, that alloca is lowered to a malloc call (either because my backend feels like doing so, or because I'm using cactus stacks, or whatever).</div><div><br></div><div>2) I allocate memory with malloc. Then I free it. Then I compare it to a global. That global lives in a lazily-loaded DSO, and the DSO gets loaded into the memory I just freed. The pointers are now equal.</div><div><br></div><div>For this to be correct, I think you need to prove that both allocations are live at the same time.</div><div><br></div><div>For an alloca, it seems to suffice for the alloca to dominate the allocation call, but it's not sufficient for the allocation call to dominate the alloca unless you can prove the allocation hasn't been freed. Simple fix: require the alloca to be a static alloca.</div><div><br></div><div>For global variables, this seems harder to fix, but the optimization seems correct if the global is defined in the current module or is marked unnamed_addr.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Fixes PR21556.<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll<br>
Modified:<br>
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp<br>
<br>
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=223093&r1=223092&r2=223093&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=223093&r1=223092&r2=223093&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)<br>
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon Dec  1 17:38:06 2014<br>
@@ -20,6 +20,7 @@<br>
 #include "llvm/Analysis/InstructionSimplify.h"<br>
 #include "llvm/ADT/SetVector.h"<br>
 #include "llvm/ADT/Statistic.h"<br>
+#include "llvm/Analysis/AliasAnalysis.h"<br>
 #include "llvm/Analysis/ConstantFolding.h"<br>
 #include "llvm/Analysis/MemoryBuiltins.h"<br>
 #include "llvm/Analysis/ValueTracking.h"<br>
@@ -31,6 +32,7 @@<br>
 #include "llvm/IR/Operator.h"<br>
 #include "llvm/IR/PatternMatch.h"<br>
 #include "llvm/IR/ValueHandle.h"<br>
+#include <algorithm><br>
 using namespace llvm;<br>
 using namespace llvm::PatternMatch;<br>
<br>
@@ -2007,6 +2009,39 @@ static Constant *computePointerICmp(cons<br>
       return ConstantExpr::getICmp(Pred,<br>
                                    ConstantExpr::getAdd(LHSOffset, LHSNoBound),<br>
                                    ConstantExpr::getAdd(RHSOffset, RHSNoBound));<br>
+<br>
+    // If one side of the equality comparison must come from a noalias call<br>
+    // (meaning a system memory allocation function), and the other side must<br>
+    // come from a pointer that cannot overlap with dynamically-allocated<br>
+    // memory within the lifetime of the current function (allocas, byval<br>
+    // arguments, globals), then determine the comparison result here.<br>
+    SmallVector<Value *, 8> LHSUObjs, RHSUObjs;<br>
+    GetUnderlyingObjects(LHS, LHSUObjs, DL);<br>
+    GetUnderlyingObjects(RHS, RHSUObjs, DL);<br>
+<br>
+    // Is the set of underlying objects all noalias calls?<br>
+    auto IsNAC = [](SmallVectorImpl<Value *> &Objects) {<br>
+      return std::all_of(Objects.begin(), Objects.end(),<br>
+                         [](Value *V){ return isNoAliasCall(V); });<br>
+    };<br>
+<br>
+    // Is the set of underlying objects all things which must be disjoint from<br>
+    // noalias calls.<br>
+    auto IsAllocDisjoint = [](SmallVectorImpl<Value *> &Objects) {<br>
+      return std::all_of(Objects.begin(), Objects.end(),<br>
+                         [](Value *V){<br>
+                           if (isa<AllocaInst>(V) || isa<GlobalValue>(V))<br>
+                             return true;<br>
+                           if (const Argument *A = dyn_cast<Argument>(V))<br>
+                             return A->hasByValAttr();<br>
+                           return false;<br>
+                         });<br>
+    };<br>
+<br>
+    if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||<br>
+        (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))<br>
+        return ConstantInt::get(GetCompareTy(LHS),<br>
+                                !CmpInst::isTrueWhenEqual(Pred));<br>
   }<br>
<br>
   // Otherwise, fail.<br>
<br>
Added: llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll?rev=223093&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll?rev=223093&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll (added)<br>
+++ llvm/trunk/test/Transforms/InstSimplify/noalias-ptr.ll Mon Dec  1 17:38:06 2014<br>
@@ -0,0 +1,108 @@<br>
+; RUN: opt -instsimplify -S < %s | FileCheck %s<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+@g1 = global i32 0, align 4<br>
+<br>
+; Make sure we can simplify away a pointer comparison between<br>
+; dynamically-allocated memory and a local stack allocation.<br>
+;   void p()<br>
+;   {<br>
+;     int *mData;<br>
+;     int mStackData[10];<br>
+;     mData = new int[12];<br>
+;     if (mData != mStackData) {<br>
+;       delete[] mData;<br>
+;     }<br>
+;   }<br>
+<br>
+define void @_Z2p1v() #0 {<br>
+  %mStackData = alloca [10 x i32], align 16<br>
+  %1 = bitcast [10 x i32]* %mStackData to i8*<br>
+  %2 = tail call noalias i8* @_Znam(i64 48) #4<br>
+  %3 = bitcast i8* %2 to i32*<br>
+  %4 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0<br>
+  %5 = icmp eq i32* %3, %4<br>
+  br i1 %5, label %7, label %6<br>
+<br>
+; CHECK-LABEL: @_Z2p1v<br>
+; CHECK-NOT: icmp<br>
+; CHECK: ret void<br>
+<br>
+; <label>:6                                       ; preds = %0<br>
+  call void @_ZdaPv(i8* %2) #5<br>
+  br label %7<br>
+<br>
+; <label>:7                                       ; preds = %0, %6<br>
+  ret void<br>
+}<br>
+<br>
+; Also check a more-complicated case with multiple underlying objects.<br>
+<br>
+define void @_Z2p2bb(i1 zeroext %b1, i1 zeroext %b2) #0 {<br>
+  %mStackData = alloca [10 x i32], align 16<br>
+  %1 = bitcast [10 x i32]* %mStackData to i8*<br>
+  %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0<br>
+  %3 = select i1 %b1, i32* %2, i32* @g1<br>
+  %4 = tail call noalias i8* @_Znam(i64 48) #4<br>
+  %5 = tail call noalias i8* @_Znam(i64 48) #4<br>
+  %.v = select i1 %b2, i8* %4, i8* %5<br>
+  %6 = bitcast i8* %.v to i32*<br>
+  %7 = icmp eq i32* %6, %3<br>
+  br i1 %7, label %9, label %8<br>
+<br>
+; CHECK-LABEL: @_Z2p2bb<br>
+; CHECK-NOT: icmp<br>
+; CHECK: ret void<br>
+<br>
+; <label>:8                                       ; preds = %0<br>
+  call void @_ZdaPv(i8* %4) #5<br>
+  call void @_ZdaPv(i8* %5) #5<br>
+  br label %9<br>
+<br>
+; <label>:9                                       ; preds = %0, %8<br>
+  ret void<br>
+}<br>
+<br>
+; Here's another case involving multiple underlying objects, but this time we<br>
+; must keep the comparison (it might involve a regular pointer-typed function<br>
+; argument).<br>
+<br>
+define void @_Z4nopebbPi(i1 zeroext %b1, i1 zeroext %b2, i32* readnone %q) #0 {<br>
+  %mStackData = alloca [10 x i32], align 16<br>
+  %1 = bitcast [10 x i32]* %mStackData to i8*<br>
+  %2 = getelementptr inbounds [10 x i32]* %mStackData, i64 0, i64 0<br>
+  %3 = select i1 %b1, i32* %2, i32* %q<br>
+  %4 = tail call noalias i8* @_Znam(i64 48) #4<br>
+  %5 = tail call noalias i8* @_Znam(i64 48) #4<br>
+  %.v = select i1 %b2, i8* %4, i8* %5<br>
+  %6 = bitcast i8* %.v to i32*<br>
+  %7 = icmp eq i32* %6, %3<br>
+  br i1 %7, label %9, label %8<br>
+<br>
+; CHECK-LABEL: @_Z4nopebbPi<br>
+; CHECK: icmp<br>
+; CHECK: ret void<br>
+<br>
+; <label>:8                                       ; preds = %0<br>
+  call void @_ZdaPv(i8* %4) #5<br>
+  call void @_ZdaPv(i8* %5) #5<br>
+  br label %9<br>
+<br>
+; <label>:9                                       ; preds = %0, %8<br>
+  ret void<br>
+}<br>
+<br>
+; Function Attrs: nobuiltin<br>
+declare noalias i8* @_Znam(i64) #2<br>
+<br>
+; Function Attrs: nobuiltin nounwind<br>
+declare void @_ZdaPv(i8*) #3<br>
+<br>
+attributes #0 = { uwtable }<br>
+attributes #1 = { nounwind }<br>
+attributes #2 = { nobuiltin }<br>
+attributes #3 = { nobuiltin nounwind }<br>
+attributes #4 = { builtin }<br>
+attributes #5 = { builtin nounwind }<br>
+<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>