Index: llvm/include/llvm/IR/Constants.h =================================================================== --- llvm/include/llvm/IR/Constants.h +++ llvm/include/llvm/IR/Constants.h @@ -1120,9 +1120,12 @@ /// commutative, callers can acquire the operand 1 identity constant by /// setting AllowRHSConstant to true. For example, any shift has a zero /// identity constant for operand 1: X shift 0 = X. + /// If this is a fadd/fsub operation and we don't care about signed zeros, + /// then setting NSZ to true returns the identity +0.0 instead of -0.0. /// Return nullptr if the operator does not have an identity constant. static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty, - bool AllowRHSConstant = false); + bool AllowRHSConstant = false, + bool NSZ = false); /// Return the absorbing element for the given binary /// operation, i.e. a constant C such that X op C = C and C op X = C for Index: llvm/lib/IR/Constants.cpp =================================================================== --- llvm/lib/IR/Constants.cpp +++ llvm/lib/IR/Constants.cpp @@ -2835,7 +2835,7 @@ } Constant *ConstantExpr::getBinOpIdentity(unsigned Opcode, Type *Ty, - bool AllowRHSConstant) { + bool AllowRHSConstant, bool NSZ) { assert(Instruction::isBinaryOp(Opcode) && "Only binops allowed"); // Commutative opcodes: it does not matter if AllowRHSConstant is set. @@ -2851,7 +2851,8 @@ return Constant::getAllOnesValue(Ty); case Instruction::FAdd: // X + -0.0 = X // TODO: If the fadd has 'nsz', should we return +0.0? - return ConstantFP::getNegativeZero(Ty); + return NSZ ? Constant::getNullValue(Ty) + : ConstantFP::getNegativeZero(Ty); case Instruction::FMul: // X * 1.0 = X return ConstantFP::get(Ty, 1.0); default: Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -450,8 +450,11 @@ } if (OpToFold) { - Constant *C = ConstantExpr::getBinOpIdentity(TVI->getOpcode(), - TVI->getType(), true); + FastMathFlags FMF; + if (isa(&SI)) + FMF = SI.getFastMathFlags(); + Constant *C = ConstantExpr::getBinOpIdentity( + TVI->getOpcode(), TVI->getType(), true, FMF.noSignedZeros()); Value *OOp = TVI->getOperand(2-OpToFold); // Avoid creating select between 2 constants unless it's selecting // between 0, 1 and -1. @@ -460,6 +463,8 @@ if (!isa(OOp) || (OOpIsAPInt && isSelect01(C->getUniqueInteger(), *OOpC))) { Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C); + if (isa(&SI)) + cast(NewSel)->setFastMathFlags(FMF); NewSel->takeName(TVI); BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(), FalseVal, NewSel); @@ -482,8 +487,11 @@ } if (OpToFold) { - Constant *C = ConstantExpr::getBinOpIdentity(FVI->getOpcode(), - FVI->getType(), true); + FastMathFlags FMF; + if (isa(&SI)) + FMF = SI.getFastMathFlags(); + Constant *C = ConstantExpr::getBinOpIdentity( + FVI->getOpcode(), FVI->getType(), true, FMF.noSignedZeros()); Value *OOp = FVI->getOperand(2-OpToFold); // Avoid creating select between 2 constants unless it's selecting // between 0, 1 and -1. @@ -492,6 +500,8 @@ if (!isa(OOp) || (OOpIsAPInt && isSelect01(C->getUniqueInteger(), *OOpC))) { Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp); + if (isa(&SI)) + cast(NewSel)->setFastMathFlags(FMF); NewSel->takeName(FVI); BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(), TrueVal, NewSel); Index: llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll =================================================================== --- llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll +++ llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll @@ -45,6 +45,28 @@ ret float %D } +define <4 x float> @select_nsz_fadd_v4f32(<4 x i1> %cond, <4 x float> %A, <4 x float> %B) { +; CHECK-LABEL: @select_nsz_fadd_v4f32( +; CHECK-NEXT: [[C:%.*]] = select nsz <4 x i1> [[COND:%.*]], <4 x float> [[B:%.*]], <4 x float> zeroinitializer +; CHECK-NEXT: [[D:%.*]] = fadd <4 x float> [[C]], [[A:%.*]] +; CHECK-NEXT: ret <4 x float> [[D]] +; + %C = fadd <4 x float> %A, %B + %D = select nsz <4 x i1> %cond, <4 x float> %C, <4 x float> %A + ret <4 x float> %D +} + +define @select_nsz_fadd_nxv4f32( %cond, %A, %B) { +; CHECK-LABEL: @select_nsz_fadd_nxv4f32( +; CHECK-NEXT: [[C:%.*]] = select nsz [[COND:%.*]], [[B:%.*]], zeroinitializer +; CHECK-NEXT: [[D:%.*]] = fadd [[C]], [[A:%.*]] +; CHECK-NEXT: ret [[D]] +; + %C = fadd %A, %B + %D = select nsz %cond, %C, %A + ret %D +} + define float @select_fmul(i1 %cond, float %A, float %B) { ; CHECK-LABEL: @select_fmul( ; CHECK-NEXT: [[C:%.*]] = select i1 [[COND:%.*]], float [[B:%.*]], float 1.000000e+00 @@ -133,6 +155,28 @@ ret float %D } +define <4 x float> @select_nsz_fsub_v4f32(<4 x i1> %cond, <4 x float> %A, <4 x float> %B) { +; CHECK-LABEL: @select_nsz_fsub_v4f32( +; CHECK-NEXT: [[C:%.*]] = select nsz <4 x i1> [[COND:%.*]], <4 x float> [[B:%.*]], <4 x float> zeroinitializer +; CHECK-NEXT: [[D:%.*]] = fsub <4 x float> [[A:%.*]], [[C]] +; CHECK-NEXT: ret <4 x float> [[D]] +; + %C = fsub <4 x float> %A, %B + %D = select nsz <4 x i1> %cond, <4 x float> %C, <4 x float> %A + ret <4 x float> %D +} + +define @select_nsz_fsub_nxv4f32( %cond, %A, %B) { +; CHECK-LABEL: @select_nsz_fsub_nxv4f32( +; CHECK-NEXT: [[C:%.*]] = select nsz [[COND:%.*]], [[B:%.*]], zeroinitializer +; CHECK-NEXT: [[D:%.*]] = fsub [[A:%.*]], [[C]] +; CHECK-NEXT: ret [[D]] +; + %C = fsub %A, %B + %D = select nsz %cond, %C, %A + ret %D +} + ; 'fsub' can only fold on the amount subtracted. define float @select_fsub_invalid(i1 %cond, float %A, float %B) { ; CHECK-LABEL: @select_fsub_invalid(