Index: lib/Analysis/InstructionSimplify.cpp =================================================================== --- lib/Analysis/InstructionSimplify.cpp +++ lib/Analysis/InstructionSimplify.cpp @@ -3040,7 +3040,8 @@ if (Pred == FCmpInst::FCMP_TRUE) return ConstantInt::get(GetCompareTy(LHS), 1); - if (isa(RHS)) // fcmp pred X, undef -> undef + // fcmp pred undef, undef -> undef + if (isa(RHS) && isa(LHS)) return UndefValue::get(GetCompareTy(LHS)); // fcmp x,x -> true/false. Not all compares are foldable. Index: test/Transforms/InstCombine/fcmp.ll =================================================================== --- test/Transforms/InstCombine/fcmp.ll +++ test/Transforms/InstCombine/fcmp.ll @@ -240,3 +240,29 @@ %conv = zext i1 %cmp to i32 ret i32 %conv } + +; Can't fold fcmp with undef on one side +; To be able to fold +; fcmp pred x, undef -> undef +; one need to prove that for all possible value of x, there exist +; one value for undef that makes the comparison false, and another +; value value for undef that makes the comparison true +; The possibility of NaN prevents the folding +define i32 @test18_undef(float %a) nounwind { +; CHECK-LABEL: @test18_undef +; CHECK-NOT: ret 0 +; CHECK-NOT: ret 1 + %cmp = fcmp ueq float %a, undef + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; Can fold fcmp with undef on both side +; fcmp pred undef, undef -> undef +; even if one NaN is undef, you can alway chose +define i1 @test19_undef() nounwind { +; CHECK-LABEL: @test19_undef +; CHECK: ret i1 undef + %cmp = fcmp ueq float undef, undef + ret i1 %cmp +}