[InstCombine] fold fnegs around select
This is one of the folds requested in: https://llvm.org/PR39480 https://alive2.llvm.org/ce/z/NczU3V Note - this uses the normal FMF propagation logic (flags transfer from the final value to new/intermediate ops). It's not clear if this matches what Alive2 implements, so we may want to adjust one or the other.
This commit is contained in:
@@ -2187,6 +2187,25 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
|
||||
if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder))
|
||||
return R;
|
||||
|
||||
Value *Cond;
|
||||
if (match(Op, m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -506,10 +506,9 @@ 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: [[NX:%.*]] = fneg float [[X:%.*]]
|
||||
; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], float [[NX]], float [[Y:%.*]]
|
||||
; CHECK-NEXT: [[R:%.*]] = fneg float [[S]]
|
||||
; CHECK-NEXT: ret float [[R]]
|
||||
; CHECK-NEXT: [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
|
||||
; CHECK-NEXT: ret float [[TMP1]]
|
||||
;
|
||||
%nx = fneg float %x
|
||||
%s = select i1 %b, float %nx, float %y
|
||||
@@ -519,10 +518,9 @@ 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: [[NY:%.*]] = fneg nnan <2 x float> [[Y:%.*]]
|
||||
; CHECK-NEXT: [[S:%.*]] = select ninf <2 x i1> [[B:%.*]], <2 x float> [[X:%.*]], <2 x float> [[NY]]
|
||||
; CHECK-NEXT: [[R:%.*]] = fneg nnan nsz <2 x float> [[S]]
|
||||
; CHECK-NEXT: ret <2 x float> [[R]]
|
||||
; 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]]
|
||||
;
|
||||
%ny = fneg nnan <2 x float> %y
|
||||
%s = select ninf <2 x i1> %b, <2 x float> %x, <2 x float> %ny
|
||||
@@ -546,9 +544,9 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) {
|
||||
; CHECK-LABEL: @select_fneg_use1(
|
||||
; CHECK-NEXT: [[NX:%.*]] = fneg ninf float [[X:%.*]]
|
||||
; CHECK-NEXT: call void @use(float [[NX]])
|
||||
; CHECK-NEXT: [[S:%.*]] = select fast i1 [[B:%.*]], float [[NX]], float [[Y:%.*]]
|
||||
; CHECK-NEXT: [[R:%.*]] = fneg float [[S]]
|
||||
; CHECK-NEXT: ret float [[R]]
|
||||
; CHECK-NEXT: [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], float [[X]], float [[Y_NEG]]
|
||||
; CHECK-NEXT: ret float [[TMP1]]
|
||||
;
|
||||
%nx = fneg ninf float %x
|
||||
call void @use(float %nx)
|
||||
@@ -560,10 +558,9 @@ define float @select_fneg_use1(float %x, float %y, i1 %b) {
|
||||
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: [[NX:%.*]] = fneg nsz float [[X:%.*]]
|
||||
; CHECK-NEXT: [[S:%.*]] = select ninf i1 [[B:%.*]], float [[NX]], float [[Y]]
|
||||
; CHECK-NEXT: [[R:%.*]] = fneg fast float [[S]]
|
||||
; CHECK-NEXT: ret float [[R]]
|
||||
; 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]]
|
||||
;
|
||||
call void @use(float %y)
|
||||
%nx = fneg nsz float %x
|
||||
@@ -572,6 +569,8 @@ define float @select_fneg_use2(float %x, float %y, i1 %b) {
|
||||
ret float %r
|
||||
}
|
||||
|
||||
; Negative test
|
||||
|
||||
define float @select_fneg_use3(float %x, float %y, i1 %b) {
|
||||
; CHECK-LABEL: @select_fneg_use3(
|
||||
; CHECK-NEXT: [[NX:%.*]] = fneg float [[X:%.*]]
|
||||
|
||||
Reference in New Issue
Block a user