An alloca can be equal to an argument. It can't *alias* an alloca, but it could
authorDan Gohman <dan433584@gmail.com>
Thu, 31 Jan 2013 23:49:33 +0000 (23:49 +0000)
committerDan Gohman <dan433584@gmail.com>
Thu, 31 Jan 2013 23:49:33 +0000 (23:49 +0000)
be equal, since there's nothing preventing a caller from correctly predicting
the stack location of an alloca.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@174119 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/compare.ll

index d5e38e5c112493c53b1fd7ea928b01060059b282..f8e76ca22077c501a751dc9a70a6c9598291049d 100644 (file)
@@ -1828,18 +1828,6 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
       else if (Pred == CmpInst::ICMP_NE)
         return ConstantInt::get(ITy, true);
     }
-  } else if (Argument *LHSArg = dyn_cast<Argument>(LHSPtr)) {
-    RHSPtr = RHSPtr->stripInBoundsOffsets();
-    // An alloca can't be equal to an argument unless they come from separate
-    // functions via inlining.
-    if (AllocaInst *RHSInst = dyn_cast<AllocaInst>(RHSPtr)) {
-      if (LHSArg->getParent() == RHSInst->getParent()->getParent()) {
-        if (Pred == CmpInst::ICMP_EQ)
-          return ConstantInt::get(ITy, false);
-        else if (Pred == CmpInst::ICMP_NE)
-          return ConstantInt::get(ITy, true);
-      }
-    }
   }
 
   // If we are comparing with zero then try hard since this is a common case.
index 56627b99a476c709dc7b395d22adbdb672f9c2f7..a6d7a64b914b84fd01b909ecffe23995f51ec0fb 100644 (file)
@@ -647,3 +647,16 @@ unreachableblock:
   %Y = icmp eq i32* %X, null
   ret i1 %Y
 }
+
+; It's not valid to fold a comparison of an argument with an alloca, even though
+; that's tempting. An argument can't *alias* an alloca, however the aliasing rule
+; relies on restrictions against guessing an object's address and dereferencing.
+; There are no restrictions against guessing an object's address and comparing.
+
+define i1 @alloca_argument_compare(i64* %arg) {
+  %alloc = alloca i64
+  %cmp = icmp eq i64* %arg, %alloc
+  ret i1 %cmp
+  ; CHECK: alloca_argument_compare
+  ; CHECK: ret i1 %cmp
+}