InstSimplify: Ignore pointer casts when constant folding compares between pointers.
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 16 Feb 2012 13:49:39 +0000 (13:49 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 16 Feb 2012 13:49:39 +0000 (13:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@150690 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7656220ba1fb32748e082d4e267585672648d71e..9d5b74c8cf2c3277c9dfb47369428d38227fcce7 100644 (file)
@@ -1591,8 +1591,11 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   // to the case where LHS is a global variable address or null is pointless,
   // since if both LHS and RHS are constants then we already constant folded
   // the compare, and if only one of them is then we moved it to RHS already.
-  if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
-                               isa<ConstantPointerNull>(RHS)))
+  Value *LHSPtr = LHS->stripPointerCasts();
+  Value *RHSPtr = RHS->stripPointerCasts();
+  if (isa<AllocaInst>(LHSPtr) && (isa<GlobalValue>(RHSPtr) ||
+                                  isa<AllocaInst>(RHSPtr)  ||
+                                  isa<ConstantPointerNull>(RHSPtr)))
     // We already know that LHS != RHS.
     return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
 
index 0c4153fa3082fdd486eae5802d932969c7c6f9be..60f5990840b16389afd448801014ff1b43de085a 100644 (file)
@@ -10,6 +10,26 @@ define i1 @ptrtoint() {
 ; CHECK: ret i1 false
 }
 
+define i1 @bitcast() {
+; CHECK: @bitcast
+  %a = alloca i32
+  %b = alloca i64
+  %x = bitcast i32* %a to i8*
+  %y = bitcast i64* %b to i8*
+  %cmp = icmp eq i8* %x, %y
+  ret i1 %cmp
+; CHECK-NEXT: ret i1 false
+}
+
+define i1 @gep() {
+; CHECK: @gep
+  %a = alloca [3 x i8], align 8
+  %x = getelementptr inbounds [3 x i8]* %a, i32 0, i32 0
+  %cmp = icmp eq i8* %x, null
+  ret i1 %cmp
+; CHECK-NEXT: ret i1 false
+}
+
 define i1 @zext(i32 %x) {
 ; CHECK: @zext
   %e1 = zext i32 %x to i64