Skip to content

Commit 242fed9

Browse files
committed
[InstCombine] convert fsub nsz with fneg operand to -(X + Y)
This was noted in D72521 - we need to match fneg specifically to consistently handle that pattern along with (-0.0 - X).
1 parent bcfa0f5 commit 242fed9

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,13 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
21592159
}
21602160
}
21612161

2162+
// (-X) - Op1 --> -(X + Op1)
2163+
if (I.hasNoSignedZeros() && !isa<ConstantExpr>(Op0) &&
2164+
match(Op0, m_OneUse(m_FNeg(m_Value(X))))) {
2165+
Value *FAdd = Builder.CreateFAddFMF(X, Op1, &I);
2166+
return UnaryOperator::CreateFNegFMF(FAdd, &I);
2167+
}
2168+
21622169
if (isa<Constant>(Op0))
21632170
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
21642171
if (Instruction *NV = FoldOpIntoSelect(I, SI))

llvm/test/Transforms/InstCombine/fsub.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,8 @@ define float @fneg_fsub(float %x, float %y) {
731731

732732
define float @fneg_fsub_nsz(float %x, float %y) {
733733
; CHECK-LABEL: @fneg_fsub_nsz(
734-
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
735-
; CHECK-NEXT: [[SUB:%.*]] = fsub nsz float [[NEGX]], [[Y:%.*]]
734+
; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz float [[X:%.*]], [[Y:%.*]]
735+
; CHECK-NEXT: [[SUB:%.*]] = fneg nsz float [[TMP1]]
736736
; CHECK-NEXT: ret float [[SUB]]
737737
;
738738
%negx = fneg float %x
@@ -743,7 +743,7 @@ define float @fneg_fsub_nsz(float %x, float %y) {
743743
define float @fake_fneg_fsub_fast(float %x, float %y) {
744744
; CHECK-LABEL: @fake_fneg_fsub_fast(
745745
; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[X:%.*]], [[Y:%.*]]
746-
; CHECK-NEXT: [[SUB:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
746+
; CHECK-NEXT: [[SUB:%.*]] = fneg fast float [[TMP1]]
747747
; CHECK-NEXT: ret float [[SUB]]
748748
;
749749
%negx = fsub float -0.0, %x
@@ -766,8 +766,8 @@ define float @fake_fneg_fsub_fast_extra_use(float %x, float %y) {
766766

767767
define <2 x float> @fake_fneg_fsub_vec(<2 x float> %x, <2 x float> %y) {
768768
; CHECK-LABEL: @fake_fneg_fsub_vec(
769-
; CHECK-NEXT: [[NEGX:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
770-
; CHECK-NEXT: [[SUB:%.*]] = fsub nsz <2 x float> [[NEGX]], [[Y:%.*]]
769+
; CHECK-NEXT: [[TMP1:%.*]] = fadd nsz <2 x float> [[X:%.*]], [[Y:%.*]]
770+
; CHECK-NEXT: [[SUB:%.*]] = fneg nsz <2 x float> [[TMP1]]
771771
; CHECK-NEXT: ret <2 x float> [[SUB]]
772772
;
773773
%negx = fsub <2 x float> <float -0.0, float -0.0>, %x

llvm/test/Transforms/Reassociate/fast-basictest.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ define float @test19_reassoc(float %A, float %B) {
671671
define float @test20(float %a, float %b, float %c) nounwind {
672672
; CHECK-LABEL: @test20(
673673
; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[C:%.*]]
674-
; CHECK-NEXT: [[T7:%.*]] = fsub fast float -0.000000e+00, [[TMP1]]
674+
; CHECK-NEXT: [[T7:%.*]] = fneg fast float [[TMP1]]
675675
; CHECK-NEXT: ret float [[T7]]
676676
;
677677
%t3 = fsub fast float %a, %b

0 commit comments

Comments
 (0)