Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -58,17 +58,22 @@ /// constant of a binop. static Instruction *foldSelectBinOpIdentity(SelectInst &Sel) { // The select condition must be an equality compare with a constant operand. - // TODO: Support FP compares. Value *X; Constant *C; CmpInst::Predicate Pred; - if (!match(Sel.getCondition(), m_ICmp(Pred, m_Value(X), m_Constant(C))) || - !ICmpInst::isEquality(Pred)) + if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C)))) + return nullptr; + + bool IsEq; + if (ICmpInst::isEquality(Pred)) + IsEq = Pred == ICmpInst::ICMP_EQ; + else if (FCmpInst::isEquality(Pred)) + IsEq = Pred == FCmpInst::FCMP_OEQ || Pred == FCmpInst::FCMP_UEQ; + else return nullptr; // A select operand must be a binop, and the compare constant must be the // identity constant for that binop. - bool IsEq = Pred == ICmpInst::ICMP_EQ; BinaryOperator *BO; if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)) || ConstantExpr::getBinOpIdentity(BO->getOpcode(), BO->getType(), true) != C) Index: test/Transforms/InstCombine/select-binop-cmp.ll =================================================================== --- test/Transforms/InstCombine/select-binop-cmp.ll +++ test/Transforms/InstCombine/select-binop-cmp.ll @@ -143,8 +143,7 @@ define float @select_fadd_fcmp(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fadd_fcmp( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fadd float [[X]], [[Z:%.*]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, -0.0 @@ -156,8 +155,7 @@ define float @select_fadd_fcmp_2(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fadd_fcmp_2( ; CHECK-NEXT: [[A:%.*]] = fcmp ueq float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fadd float [[X]], [[Z:%.*]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp ueq float %x, -0.0 @@ -169,8 +167,7 @@ define float @select_fadd_fcmp_3(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fadd_fcmp_3( ; CHECK-NEXT: [[A:%.*]] = fcmp one float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fadd float [[Z:%.*]], [[X]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[B]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[Z:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp one float %x, -0.0 @@ -182,8 +179,7 @@ define float @select_fadd_fcmp_4(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fadd_fcmp_4( ; CHECK-NEXT: [[A:%.*]] = fcmp une float [[X:%.*]], -0.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fadd float [[Z:%.*]], [[X]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[B]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[Z:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp une float %x, -0.0 @@ -195,8 +191,7 @@ define float @select_fmul_fcmp(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fmul_fcmp( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fmul float [[X]], [[Z:%.*]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, 1.0 @@ -208,8 +203,7 @@ define float @select_fsub_fcmp(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fsub_fcmp( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 0.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fsub float [[Z:%.*]], [[X]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, 0.0 @@ -221,8 +215,7 @@ define float @select_fdiv_fcmp(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fdiv_fcmp( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fdiv float [[Z:%.*]], [[X]] -; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, 1.0 @@ -349,6 +342,62 @@ ret i32 %C } +define float @select_fadd_fcmp_nan_1(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_nan_1( +; CHECK-NEXT: [[A:%.*]] = fcmp nnan oeq float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp nnan oeq float %x, -0.0 + %B = fadd float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + +define float @select_fadd_fcmp_nan_2(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_nan_2( +; CHECK-NEXT: ret float [[Y:%.*]] +; + %A = fcmp oeq float %x, 0x7FF8000000000000 + %B = fadd float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + +define float @select_fadd_fcmp_nan_3(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_nan_3( +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float 0x7FF8000000000000, float [[Y:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp oeq float %x, -0.0 + %B = fadd float %x, 0x7FF8000000000000 + %C = select i1 %A, float %B, float %y + ret float %C +} + +define float @select_fadd_fcmp_nan_4(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_nan_4( +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Z:%.*]], float 0x7FF8000000000000 +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp oeq float %x, -0.0 + %B = fadd float %x, %z + %C = select i1 %A, float %B, float 0x7FF8000000000000 + ret float %C +} + +define float @select_fadd_fcmp_nan_5(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_nan_5( +; CHECK-NEXT: ret float [[Y:%.*]] +; + %A = fcmp oeq float 0x7FF8000000000000, -0.0 + %B = fadd float 0x7FF8000000000000, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + ; Negative tests define i32 @select_xor_icmp_bad_1(i32 %x, i32 %y, i32 %z, i32 %k) { ; CHECK-LABEL: @select_xor_icmp_bad_1(