From 3afc0721c766db6942e79597a9b2e10fe0824194 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 3 Mar 2010 19:46:03 +0000 Subject: [PATCH] fix incorrect folding of icmp with undef, PR6481. llvm-svn: 97659 --- llvm/lib/Analysis/InstructionSimplify.cpp | 7 +++---- llvm/lib/VMCore/ConstantFold.cpp | 2 +- llvm/test/Transforms/InstCombine/icmp.ll | 12 +++++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 1f8053afe949..8288e96eb775 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -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(RHS)) return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); - - if (isa(RHS)) // X icmp undef -> undef - return UndefValue::get(ITy); // icmp , - Global/Stack value // addresses never equal each other! We already know that Op0 != Op1. diff --git a/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp index 194a6d4d8c06..47244a0e3239 100644 --- a/llvm/lib/VMCore/ConstantFold.cpp +++ b/llvm/lib/VMCore/ConstantFold.cpp @@ -1818,7 +1818,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, // Handle some degenerate cases first if (isa(C1) || isa(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()) diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll index c2234a10e5b7..29997bf8c41e 100644 --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -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> } 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 +} +