From 26c9404ebd14d371e4994d0ab9307d76aef05b70 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 8 Jun 2021 16:45:29 -0400 Subject: [PATCH] [InstCombine] fix nsz (fast-math) propagation from fneg-of-select As discussed in the post-commit comments for: 3cdd05e519dd It seems to be safe to propagate all flags from the final fneg except for 'nsz' to the new select: https://alive2.llvm.org/ce/z/J_APDc nsz has unique FMF semantics: it is not poison, it is only "insignificant" in the calculation according to the LangRef. --- .../InstCombine/InstCombineAddSub.cpp | 26 +++++++++++++------ test/Transforms/InstCombine/fneg.ll | 24 ++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 2ed616f4ed7..79b5531b776 100644 --- a/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -2200,22 +2200,32 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) { if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) return R; + // Try to eliminate fneg if at least 1 arm of the select is negated. Value *Cond; if (match(Op, m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(Y))))) { + // Unlike most transforms, this one is not safe to propagate nsz unless + // it is present on the original select. (We are conservatively intersecting + // the nsz flags from the select and root fneg instruction.) + auto propagateSelectFMF = [&](SelectInst *S) { + S->copyFastMathFlags(&I); + if (auto *OldSel = dyn_cast(Op)) + if (!OldSel->hasNoSignedZeros()) + S->setHasNoSignedZeros(false); + }; + // -(Cond ? -P : Y) --> Cond ? P : -Y Value *P; if (match(X, m_FNeg(m_Value(P)))) { - IRBuilder<>::FastMathFlagGuard FMFG(Builder); - Builder.setFastMathFlags(I.getFastMathFlags()); Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg"); - Value *NewSel = Builder.CreateSelect(Cond, P, NegY); - return replaceInstUsesWith(I, NewSel); + SelectInst *NewSel = SelectInst::Create(Cond, P, NegY); + propagateSelectFMF(NewSel); + return NewSel; } + // -(Cond ? X : -P) --> Cond ? -X : P if (match(Y, m_FNeg(m_Value(P)))) { - IRBuilder<>::FastMathFlagGuard FMFG(Builder); - Builder.setFastMathFlags(I.getFastMathFlags()); Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg"); - Value *NewSel = Builder.CreateSelect(Cond, NegX, P); - return replaceInstUsesWith(I, NewSel); + SelectInst *NewSel = SelectInst::Create(Cond, NegX, P); + propagateSelectFMF(NewSel); + return NewSel; } } diff --git a/test/Transforms/InstCombine/fneg.ll b/test/Transforms/InstCombine/fneg.ll index 6d507bdcd90..60aea51de01 100644 --- a/test/Transforms/InstCombine/fneg.ll +++ b/test/Transforms/InstCombine/fneg.ll @@ -567,8 +567,8 @@ define float @fake_fneg_nsz_fadd_constant_expr(float %x) { define float @select_fneg_true(float %x, float %y, i1 %b) { ; CHECK-LABEL: @select_fneg_true( ; CHECK-NEXT: [[Y_NEG:%.*]] = fneg float [[Y:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]] -; CHECK-NEXT: ret float [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]] +; CHECK-NEXT: ret float [[R]] ; %nx = fneg float %x %s = select i1 %b, float %nx, float %y @@ -579,8 +579,8 @@ define float @select_fneg_true(float %x, float %y, i1 %b) { define <2 x float> @select_fneg_false(<2 x float> %x, <2 x float> %y, <2 x i1> %b) { ; CHECK-LABEL: @select_fneg_false( ; CHECK-NEXT: [[X_NEG:%.*]] = fneg nnan nsz <2 x float> [[X:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = select nnan nsz <2 x i1> [[B:%.*]], <2 x float> [[X_NEG]], <2 x float> [[Y:%.*]] -; CHECK-NEXT: ret <2 x float> [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select nnan <2 x i1> [[B:%.*]], <2 x float> [[X_NEG]], <2 x float> [[Y:%.*]] +; CHECK-NEXT: ret <2 x float> [[R]] ; %ny = fneg nnan <2 x float> %y %s = select ninf <2 x i1> %b, <2 x float> %x, <2 x float> %ny @@ -591,8 +591,8 @@ define <2 x float> @select_fneg_false(<2 x float> %x, <2 x float> %y, <2 x i1> % define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) { ; CHECK-LABEL: @select_fneg_false_no_nsz( ; CHECK-NEXT: [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]] -; CHECK-NEXT: ret float [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select nnan ninf i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[R]] ; %ny = fneg float %y %s = select i1 %b, float %x, float %ny @@ -603,8 +603,8 @@ define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) { define float @select_fneg_false_nsz(float %x, float %y, i1 %b) { ; CHECK-LABEL: @select_fneg_false_nsz( ; CHECK-NEXT: [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]] -; CHECK-NEXT: ret float [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select nnan ninf nsz i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]] +; CHECK-NEXT: ret float [[R]] ; %ny = fneg float %y %s = select nsz i1 %b, float %x, float %ny @@ -629,8 +629,8 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) { ; CHECK-NEXT: [[NX:%.*]] = fneg ninf float [[X:%.*]] ; CHECK-NEXT: call void @use(float [[NX]]) ; CHECK-NEXT: [[Y_NEG:%.*]] = fneg float [[Y:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]] -; CHECK-NEXT: ret float [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]] +; CHECK-NEXT: ret float [[R]] ; %nx = fneg ninf float %x call void @use(float %nx) @@ -643,8 +643,8 @@ define float @select_fneg_use2(float %x, float %y, i1 %b) { ; CHECK-LABEL: @select_fneg_use2( ; CHECK-NEXT: call void @use(float [[Y:%.*]]) ; CHECK-NEXT: [[Y_NEG:%.*]] = fneg fast float [[Y]] -; CHECK-NEXT: [[TMP1:%.*]] = select fast i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]] -; CHECK-NEXT: ret float [[TMP1]] +; CHECK-NEXT: [[R:%.*]] = select reassoc nnan ninf arcp contract afn i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]] +; CHECK-NEXT: ret float [[R]] ; call void @use(float %y) %nx = fneg nsz float %x