Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -58,22 +58,32 @@ /// 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 (Pred == FCmpInst::FCMP_OEQ) + IsEq = true; + else if (Pred == FCmpInst::FCMP_ONE) + IsEq = false; + 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) return nullptr; + // Check for signed zeros + if (isa(BO) && !BO->hasNoSignedZeros()) + return nullptr; // Last, match the compare variable operand with a binop operand. Value *Y; if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X)))) Index: test/Transforms/InstCombine/select-binop-cmp.ll =================================================================== --- test/Transforms/InstCombine/select-binop-cmp.ll +++ test/Transforms/InstCombine/select-binop-cmp.ll @@ -143,51 +143,23 @@ 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 - %B = fadd float %x, %z + %B = fadd nsz float %x, %z %C = select i1 %A, float %B, float %y ret float %C } 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: ret float [[C]] -; - %A = fcmp ueq 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_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 - %B = fadd float %z, %x - %C = select i1 %A, float %y, float %B - ret float %C -} - -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: ret float [[C]] -; - %A = fcmp une float %x, -0.0 - %B = fadd float %z, %x + %B = fadd nsz float %z, %x %C = select i1 %A, float %y, float %B ret float %C } @@ -195,12 +167,11 @@ 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 - %B = fmul float %x, %z + %B = fmul nsz float %x, %z %C = select i1 %A, float %B, float %y ret float %C } @@ -208,12 +179,11 @@ 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 - %B = fsub float %z, %x + %B = fsub nsz float %z, %x %C = select i1 %A, float %B, float %y ret float %C } @@ -221,12 +191,11 @@ 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 - %B = fdiv float %z, %x + %B = fdiv nsz float %z, %x %C = select i1 %A, float %B, float %y ret float %C } @@ -510,12 +479,12 @@ define float @select_fadd_fcmp_bad(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fadd_fcmp_bad( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], -1.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fadd float [[X]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = fadd nsz float [[X]], [[Z:%.*]] ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, -1.0 - %B = fadd float %x, %z + %B = fadd nsz float %x, %z %C = select i1 %A, float %B, float %y ret float %C } @@ -559,14 +528,65 @@ ret float %C } +define float @select_fadd_fcmp_bad_5(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_bad_5( +; CHECK-NEXT: [[A:%.*]] = fcmp one float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[Z:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp one float %x, -0.0 + %B = fadd nsz float %z, %x + %C = select i1 %A, float %y, float %B + ret float %C +} + +define float @select_fadd_fcmp_bad_6(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_bad_6( +; CHECK-NEXT: [[A:%.*]] = fcmp une float [[X:%.*]], -0.000000e+00 +; CHECK-NEXT: [[B:%.*]] = fadd nsz float [[Z:%.*]], [[X]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[Y:%.*]], float [[B]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp une float %x, -0.0 + %B = fadd nsz float %z, %x + %C = select i1 %A, float %y, float %B + ret float %C +} + +define float @select_fadd_fcmp_bad_7(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fadd_fcmp_bad_7( +; 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: ret float [[C]] +; + %A = fcmp oeq float %x, -0.0 + %B = fadd float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + define float @select_fmul_fcmp_bad(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fmul_fcmp_bad( ; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 3.000000e+00 -; CHECK-NEXT: [[B:%.*]] = fmul float [[X]], [[Z:%.*]] +; CHECK-NEXT: [[B:%.*]] = fmul nsz float [[X]], [[Z:%.*]] ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; %A = fcmp oeq float %x, 3.0 + %B = fmul nsz float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + +define float @select_fmul_fcmp_bad_2(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fmul_fcmp_bad_2( +; 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: ret float [[C]] +; + %A = fcmp oeq float %x, 1.0 %B = fmul float %x, %z %C = select i1 %A, float %B, float %y ret float %C @@ -585,32 +605,71 @@ ret float %C } +define float @select_fmul_icmp_bad_2(float %x, float %y, float %z, i32 %k) { +; CHECK-LABEL: @select_fmul_icmp_bad_2( +; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[K:%.*]], 0 +; CHECK-NEXT: [[B:%.*]] = fmul nsz float [[X:%.*]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = icmp eq i32 %k, 0 + %B = fmul nsz float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + define float @select_fdiv_fcmp_bad(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fdiv_fcmp_bad( -; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 3.000000e+00 +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00 ; CHECK-NEXT: [[B:%.*]] = fdiv float [[X]], [[Z:%.*]] ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; - %A = fcmp oeq float %x, 3.0 + %A = fcmp oeq float %x, 1.0 %B = fdiv float %x, %z %C = select i1 %A, float %B, float %y ret float %C } +define float @select_fdiv_fcmp_bad_2(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fdiv_fcmp_bad_2( +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 3.000000e+00 +; CHECK-NEXT: [[B:%.*]] = fdiv nsz float [[X]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp oeq float %x, 3.0 + %B = fdiv nsz float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + define float @select_fsub_fcmp_bad(float %x, float %y, float %z) { ; CHECK-LABEL: @select_fsub_fcmp_bad( -; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00 +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 0.000000e+00 ; CHECK-NEXT: [[B:%.*]] = fsub float [[X]], [[Z:%.*]] ; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] ; CHECK-NEXT: ret float [[C]] ; - %A = fcmp oeq float %x, 1.0 + %A = fcmp oeq float %x, 0.0 %B = fsub float %x, %z %C = select i1 %A, float %B, float %y ret float %C } +define float @select_fsub_fcmp_bad_2(float %x, float %y, float %z) { +; CHECK-LABEL: @select_fsub_fcmp_bad_2( +; CHECK-NEXT: [[A:%.*]] = fcmp oeq float [[X:%.*]], 1.000000e+00 +; CHECK-NEXT: [[B:%.*]] = fsub nsz float [[X]], [[Z:%.*]] +; CHECK-NEXT: [[C:%.*]] = select i1 [[A]], float [[B]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[C]] +; + %A = fcmp oeq float %x, 1.0 + %B = fsub nsz float %x, %z + %C = select i1 %A, float %B, float %y + ret float %C +} + define i32 @select_sub_icmp_bad(i32 %x, i32 %y, i32 %z) { ; CHECK-LABEL: @select_sub_icmp_bad( ; CHECK-NEXT: [[A:%.*]] = icmp eq i32 [[X:%.*]], 0