Index: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -763,12 +763,16 @@ 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)))) { bool SelectsHaveOneUse = LHS->hasOneUse() && RHS->hasOneUse(); + + FastMathFlags FMF; BuilderTy::FastMathFlagGuard Guard(Builder); - if (isa(&I)) - Builder.setFastMathFlags(I.getFastMathFlags()); + if (isa(&I)) { + FMF = I.getFastMathFlags(); + Builder.setFastMathFlags(FMF); + } - Value *V1 = SimplifyBinOp(Opcode, C, E, SQ.getWithInstruction(&I)); - Value *V2 = SimplifyBinOp(Opcode, B, D, SQ.getWithInstruction(&I)); + Value *V1 = SimplifyBinOp(Opcode, C, E, FMF, SQ.getWithInstruction(&I)); + Value *V2 = SimplifyBinOp(Opcode, B, D, FMF, SQ.getWithInstruction(&I)); if (V1 && V2) SI = Builder.CreateSelect(A, V2, V1); else if (V2 && SelectsHaveOneUse) Index: llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll =================================================================== --- llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll +++ llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll @@ -94,10 +94,7 @@ define float @fmul_nnan_nsz(i1 %cond, float %val) { ; CHECK-LABEL: @fmul_nnan_nsz( -; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], float [[VAL:%.*]], float 0.000000e+00 -; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], float -0.000000e+00, float [[VAL]] -; CHECK-NEXT: [[MUL:%.*]] = fmul nnan nsz float [[LHS]], [[RHS]] -; CHECK-NEXT: ret float [[MUL]] +; CHECK-NEXT: ret float 0.000000e+00 ; %lhs = select i1 %cond, float %val, float +0.0 %rhs = select i1 %cond, float -0.0, float %val @@ -107,10 +104,7 @@ define <2 x float> @fadd_nsz(<2 x i1> %cond, <2 x float> %val) { ; CHECK-LABEL: @fadd_nsz( -; CHECK-NEXT: [[LHS:%.*]] = select <2 x i1> [[COND:%.*]], <2 x float> [[VAL:%.*]], <2 x float> zeroinitializer -; CHECK-NEXT: [[RHS:%.*]] = select <2 x i1> [[COND]], <2 x float> zeroinitializer, <2 x float> [[VAL]] -; CHECK-NEXT: [[ADD:%.*]] = fadd nsz <2 x float> [[LHS]], [[RHS]] -; CHECK-NEXT: ret <2 x float> [[ADD]] +; CHECK-NEXT: ret <2 x float> [[VAL:%.*]] ; %lhs = select <2 x i1> %cond, <2 x float> %val, <2 x float> %rhs = select <2 x i1> %cond, <2 x float> , <2 x float> %val @@ -120,9 +114,8 @@ define double @fsub_nnan(i1 %cond, double %val, double %val2) { ; CHECK-LABEL: @fsub_nnan( -; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], double [[VAL:%.*]], double [[VAL2:%.*]] -; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], double [[VAL]], double 7.000000e+00 -; CHECK-NEXT: [[ADD:%.*]] = fsub nnan double [[LHS]], [[RHS]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd nnan double [[VAL2:%.*]], -7.000000e+00 +; CHECK-NEXT: [[ADD:%.*]] = select nnan i1 [[COND:%.*]], double 0.000000e+00, double [[TMP1]] ; CHECK-NEXT: ret double [[ADD]] ; %lhs = select i1 %cond, double %val, double %val2