Skip to content

Commit 796de11

Browse files
committed
[InstCombine] Update fptrunc (fneg x)) -> (fneg (fptrunc x) for unary FNeg
Differential Revision: https://reviews.llvm.org/D62629 llvm-svn: 363080
1 parent 9d51fa5 commit 796de11

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,12 +1615,20 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &FPT) {
16151615
return CastInst::CreateFPCast(ExactResult, Ty);
16161616
}
16171617
}
1618+
}
16181619

1619-
// (fptrunc (fneg x)) -> (fneg (fptrunc x))
1620-
Value *X;
1621-
if (match(OpI, m_FNeg(m_Value(X)))) {
1620+
// (fptrunc (fneg x)) -> (fneg (fptrunc x))
1621+
Value *X;
1622+
Instruction *Op = dyn_cast<Instruction>(FPT.getOperand(0));
1623+
if (Op && Op->hasOneUse()) {
1624+
if (match(Op, m_FNeg(m_Value(X)))) {
16221625
Value *InnerTrunc = Builder.CreateFPTrunc(X, Ty);
1623-
return BinaryOperator::CreateFNegFMF(InnerTrunc, OpI);
1626+
1627+
// FIXME: Once we're sure that unary FNeg optimizations are on par with
1628+
// binary FNeg, this should always return a unary operator.
1629+
if (isa<BinaryOperator>(Op))
1630+
return BinaryOperator::CreateFNegFMF(InnerTrunc, Op);
1631+
return UnaryOperator::CreateFNegFMF(InnerTrunc, Op);
16241632
}
16251633
}
16261634

llvm/test/Transforms/InstCombine/fpcast.ll

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ define half @fneg_fptrunc(float %a) {
4040
ret half %c
4141
}
4242

43-
; FIXME: This combine isn't working.
4443
define half @unary_fneg_fptrunc(float %a) {
4544
; CHECK-LABEL: @unary_fneg_fptrunc(
46-
; CHECK-NEXT: [[B:%.*]] = fneg float [[A:%.*]]
47-
; CHECK-NEXT: [[C:%.*]] = fptrunc float [[B]] to half
45+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
46+
; CHECK-NEXT: [[C:%.*]] = fneg half [[TMP1]]
4847
; CHECK-NEXT: ret half [[C]]
4948
;
5049
%b = fneg float %a
@@ -63,11 +62,10 @@ define <2 x half> @fneg_fptrunc_vec_undef(<2 x float> %a) {
6362
ret <2 x half> %c
6463
}
6564

66-
; FIXME: This combine isn't working.
6765
define <2 x half> @unary_fneg_fptrunc_vec(<2 x float> %a) {
6866
; CHECK-LABEL: @unary_fneg_fptrunc_vec(
69-
; CHECK-NEXT: [[B:%.*]] = fneg <2 x float> [[A:%.*]]
70-
; CHECK-NEXT: [[C:%.*]] = fptrunc <2 x float> [[B]] to <2 x half>
67+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc <2 x float> [[A:%.*]] to <2 x half>
68+
; CHECK-NEXT: [[C:%.*]] = fneg <2 x half> [[TMP1]]
7169
; CHECK-NEXT: ret <2 x half> [[C]]
7270
;
7371
%b = fneg <2 x float> %a
@@ -86,11 +84,10 @@ define half @test4-fast(float %a) {
8684
ret half %c
8785
}
8886

89-
; FIXME: This combine isn't working.
9087
define half @test4_unary_fneg-fast(float %a) {
9188
; CHECK-LABEL: @test4_unary_fneg-fast(
92-
; CHECK-NEXT: [[B:%.*]] = fneg fast float [[A:%.*]]
93-
; CHECK-NEXT: [[C:%.*]] = fptrunc float [[B]] to half
89+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc float [[A:%.*]] to half
90+
; CHECK-NEXT: [[C:%.*]] = fneg fast half [[TMP1]]
9491
; CHECK-NEXT: ret half [[C]]
9592
;
9693
%b = fneg fast float %a

llvm/test/Transforms/InstCombine/fpextend.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ entry:
5555
ret float %tmp34
5656
}
5757

58-
; FIXME: This combine isn't working.
5958
define float @test4_unary_fneg(float %x) nounwind {
6059
; CHECK-LABEL: @test4_unary_fneg(
6160
; CHECK-NEXT: entry:
62-
; CHECK-NEXT: [[TMP1:%.*]] = fpext float [[X:%.*]] to double
63-
; CHECK-NEXT: [[TMP2:%.*]] = fneg double [[TMP1]]
64-
; CHECK-NEXT: [[TMP34:%.*]] = fptrunc double [[TMP2]] to float
61+
; CHECK-NEXT: [[TMP34:%.*]] = fneg float [[X:%.*]]
6562
; CHECK-NEXT: ret float [[TMP34]]
6663
;
6764
entry:

llvm/test/Transforms/InstCombine/fsub.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,11 @@ define float @neg_trunc_op1_extra_use(double %a, float %b) {
345345
ret float %t3
346346
}
347347

348-
; FIXME: This combine isn't working.
349348
define float @unary_neg_trunc_op1_extra_use(double %a, float %b) {
350349
; CHECK-LABEL: @unary_neg_trunc_op1_extra_use(
351-
; CHECK-NEXT: [[T1:%.*]] = fneg double [[A:%.*]]
352-
; CHECK-NEXT: [[T2:%.*]] = fptrunc double [[T1]] to float
353-
; CHECK-NEXT: [[T3:%.*]] = fsub float [[B:%.*]], [[T2]]
350+
; CHECK-NEXT: [[TMP1:%.*]] = fptrunc double [[A:%.*]] to float
351+
; CHECK-NEXT: [[T2:%.*]] = fneg float [[TMP1]]
352+
; CHECK-NEXT: [[T3:%.*]] = fadd float [[TMP1]], [[B:%.*]]
354353
; CHECK-NEXT: call void @use(float [[T2]])
355354
; CHECK-NEXT: ret float [[T3]]
356355
;

0 commit comments

Comments
 (0)