fix incorrect folding of icmp with undef, PR6481.
authorChris Lattner <sabre@nondot.org>
Wed, 3 Mar 2010 19:46:03 +0000 (19:46 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 3 Mar 2010 19:46:03 +0000 (19:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97659 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
lib/VMCore/ConstantFold.cpp
test/Transforms/InstCombine/icmp.ll

index 1f8053afe94963ab5718bef5b7a3ac5e6d309760..8288e96eb775e41df8e4fa4d448e60e298082d5d 100644 (file)
@@ -194,11 +194,10 @@ Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
   const Type *ITy = GetCompareTy(LHS);
   
   // icmp X, X -> true/false
-  if (LHS == RHS)
+  // X icmp undef -> true/false.  For example, icmp ugt %X, undef -> false
+  // because X could be 0.
+  if (LHS == RHS || isa<UndefValue>(RHS))
     return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
-
-  if (isa<UndefValue>(RHS))                  // X icmp undef -> undef
-    return UndefValue::get(ITy);
   
   // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
   // addresses never equal each other!  We already know that Op0 != Op1.
index 194a6d4d8c06c8113f92d26fedd7a2fc0f608cd6..47244a0e323973962e93aa654b1f50be9b8fbcae 100644 (file)
@@ -1818,7 +1818,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
 
   // Handle some degenerate cases first
   if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
-    return UndefValue::get(ResultTy);
+    return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
 
   // No compile-time operations on this type yet.
   if (C1->getType()->isPPC_FP128Ty())
index c2234a10e5b7a51c32667e37e80824419859fc40..29997bf8c41e1c4a3fc81b1b581bb1da4527167f 100644 (file)
@@ -48,7 +48,7 @@ entry:
   %V = icmp eq <2 x i64> %x, undef
   ret <2 x i1> %V
 ; CHECK: @test5
-; CHECK: ret <2 x i1> undef
+; CHECK: ret <2 x i1> <i1 true, i1 true>
 }
 
 define i32 @test6(i32 %a, i32 %b) {
@@ -121,3 +121,13 @@ define i1 @test12(i1 %A) {
 ; CHECK-NEXT: %B = select i1
 ; CHECK-NEXT: ret i1 %B
 }
+
+; PR6481
+define i1 @test13(i8 %X) nounwind readnone {
+entry:
+        %cmp = icmp slt i8 undef, %X
+        ret i1 %cmp
+; CHECK: @test13
+; CHECK: ret i1 false
+}
+