Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -729,8 +729,8 @@ // c, e))) Value *A, *B, *C, *D, *E; Value *SI = nullptr; - if (match(LHS, m_Select(m_Value(A), m_Value(B), m_Value(C))) && - match(RHS, m_Select(m_Specific(A), m_Value(D), m_Value(E)))) { + if (match(LHS, m_OneUse(m_Select(m_Value(A), m_Value(B), m_Value(C)))) && + match(RHS, m_OneUse(m_Select(m_Specific(A), m_Value(D), m_Value(E))))) { BuilderTy::FastMathFlagGuard Guard(Builder); if (isa(&I)) Builder.setFastMathFlags(I.getFastMathFlags()); Index: test/Transforms/InstCombine/select_arithmetic.ll =================================================================== --- test/Transforms/InstCombine/select_arithmetic.ll +++ test/Transforms/InstCombine/select_arithmetic.ll @@ -38,3 +38,16 @@ ; CHECK: ret float %tmp2 } +; Tests not folding constants if the selects have more than one use. +declare void @use_double(double) +define double @test4(i1 zeroext %arg, double %div) { + %tmp = select i1 %arg, double %div, double 5.000000e-03 + %mul = fmul contract double %tmp, %tmp + call void @use_double(double %tmp) + ret double %mul +; CHECK-LABEL: @test4( +; CHECK: [[TMP:%.*]] = select i1 %arg, double %div, double 5.000000e-03 +; CHECK-NEXT: [[MUL:%.*]] = fmul contract double [[TMP]], [[TMP]] +; CHECK-NOT: fmul contract double %div, %div +; CHECK: ret double [[MUL]] +}