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 = false; + if (ICmpInst::isEquality(Pred)) + IsEq = Pred == ICmpInst::ICMP_EQ; + else if (FCmpInst::isEquality(Pred)) + IsEq = Pred == FCmpInst::FCMP_OEQ || 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 @@ -140,12 +140,10 @@ ret i32 %C } -; TODO: FP opcodes support 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 @@ -157,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 @@ -170,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 [[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 one float %x, -0.0 @@ -183,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 [[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 une float %x, -0.0 @@ -196,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