1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 19:12:56 +02:00

[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.
This commit is contained in:
Sanjay Patel 2021-06-08 16:45:29 -04:00
parent 86c02600f6
commit 26c9404ebd
2 changed files with 30 additions and 20 deletions

View File

@ -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<SelectInst>(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;
}
}

View File

@ -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