Skip to content

Commit 5db49f7

Browse files
authored
[GlobalISel] replace right identity X * -1.0 with fneg(x) (#80526)
follow up patch to #78673 @Pierre-vh @jayfoad @arsenm Could you review when you have a chance.
1 parent 7242896 commit 5db49f7

File tree

4 files changed

+253
-153
lines changed

4 files changed

+253
-153
lines changed

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ def right_identity_one_fp: GICombineRule<
493493
(apply (GIReplaceReg $dst, $x))
494494
>;
495495

496+
def right_identity_neg_one_fp: GICombineRule<
497+
(defs root:$dst),
498+
(match (G_FMUL $dst, $x, $y):$root,
499+
[{ return Helper.matchConstantFPOp(${y}, -1.0); }]),
500+
(apply (G_FNEG $dst, $x))
501+
>;
502+
496503
def right_identity_one : GICombineGroup<[right_identity_one_int, right_identity_one_fp]>;
497504

498505
// Fold (x op x) - > x
@@ -1283,7 +1290,8 @@ def identity_combines : GICombineGroup<[select_same_val, right_identity_zero,
12831290
trunc_buildvector_fold,
12841291
trunc_lshr_buildvector_fold,
12851292
bitcast_bitcast_fold, fptrunc_fpext_fold,
1286-
right_identity_neg_zero_fp]>;
1293+
right_identity_neg_zero_fp,
1294+
right_identity_neg_one_fp]>;
12871295

12881296
def const_combines : GICombineGroup<[constant_fold_fp_ops, const_ptradd_to_i2p,
12891297
overlapping_and, mulo_by_2, mulo_by_0,
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
2+
# RUN: llc -march=amdgcn -mcpu=gfx1010 -run-pass=amdgpu-postlegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s --check-prefixes=CHECK
3+
4+
---
5+
name: test_neg_one_f16_sgpr
6+
tracksRegLiveness: true
7+
body: |
8+
bb.0:
9+
liveins: $sgpr0
10+
11+
; CHECK-LABEL: name: test_neg_one_f16_sgpr
12+
; CHECK: liveins: $sgpr0
13+
; CHECK-NEXT: {{ $}}
14+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $sgpr0
15+
; CHECK-NEXT: %x:_(s16) = G_TRUNC [[COPY]](s32)
16+
; CHECK-NEXT: %d:_(s16) = G_FNEG %x
17+
; CHECK-NEXT: %ext:_(s32) = G_ANYEXT %d(s16)
18+
; CHECK-NEXT: $sgpr0 = COPY %ext(s32)
19+
%0:_(s32) = COPY $sgpr0
20+
%x:_(s16) = G_TRUNC %0:_(s32)
21+
%y:_(s16) = G_FCONSTANT half -1.0
22+
%d:_(s16) = G_FMUL %x, %y
23+
%ext:_(s32) = G_ANYEXT %d:_(s16)
24+
$sgpr0 = COPY %ext
25+
26+
...
27+
28+
---
29+
name: test_neg_one_f32_sgpr
30+
body: |
31+
bb.0:
32+
liveins: $sgpr0
33+
34+
; CHECK-LABEL: name: test_neg_one_f32_sgpr
35+
; CHECK: liveins: $sgpr0
36+
; CHECK-NEXT: {{ $}}
37+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $sgpr0
38+
; CHECK-NEXT: [[FNEG:%[0-9]+]]:_(s32) = G_FNEG [[COPY]]
39+
; CHECK-NEXT: $sgpr0 = COPY [[FNEG]](s32)
40+
%0:_(s32) = COPY $sgpr0
41+
%1:_(s32) = G_FCONSTANT float -1.0
42+
%2:_(s32) = G_FMUL %0, %1
43+
$sgpr0 = COPY %2(s32)
44+
45+
...
46+
47+
---
48+
name: test_neg_one_f64_sgpr
49+
tracksRegLiveness: true
50+
body: |
51+
bb.0:
52+
liveins: $sgpr0
53+
54+
; CHECK-LABEL: name: test_neg_one_f64_sgpr
55+
; CHECK: liveins: $sgpr0
56+
; CHECK-NEXT: {{ $}}
57+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $sgpr0
58+
; CHECK-NEXT: %x:_(s64) = G_ANYEXT [[COPY]](s32)
59+
; CHECK-NEXT: %d:_(s64) = G_FNEG %x
60+
; CHECK-NEXT: %ext:_(s32) = G_TRUNC %d(s64)
61+
; CHECK-NEXT: $sgpr0 = COPY %ext(s32)
62+
%0:_(s32) = COPY $sgpr0
63+
%x:_(s64) = G_ANYEXT %0:_(s32)
64+
%y:_(s64) = G_FCONSTANT double -1.0
65+
%d:_(s64) = G_FMUL %x, %y
66+
%ext:_(s32) = G_TRUNC %d:_(s64)
67+
$sgpr0 = COPY %ext
68+
69+
...
70+
71+
---
72+
name: test_neg_ten_f32_sgpr
73+
body: |
74+
bb.0:
75+
liveins: $sgpr0
76+
77+
; CHECK-LABEL: name: test_neg_ten_f32_sgpr
78+
; CHECK: liveins: $sgpr0
79+
; CHECK-NEXT: {{ $}}
80+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $sgpr0
81+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float -1.000000e+01
82+
; CHECK-NEXT: [[FMUL:%[0-9]+]]:_(s32) = G_FMUL [[COPY]], [[C]]
83+
; CHECK-NEXT: $sgpr0 = COPY [[FMUL]](s32)
84+
%0:_(s32) = COPY $sgpr0
85+
%1:_(s32) = G_FCONSTANT float -10.0
86+
%2:_(s32) = G_FMUL %0, %1
87+
$sgpr0 = COPY %2(s32)
88+
89+
...
90+
91+
---
92+
name: test_neg_fract_f32_sgpr
93+
body: |
94+
bb.0:
95+
liveins: $sgpr0
96+
97+
; CHECK-LABEL: name: test_neg_fract_f32_sgpr
98+
; CHECK: liveins: $sgpr0
99+
; CHECK-NEXT: {{ $}}
100+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $sgpr0
101+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float -5.000000e-01
102+
; CHECK-NEXT: [[FMUL:%[0-9]+]]:_(s32) = G_FMUL [[COPY]], [[C]]
103+
; CHECK-NEXT: $sgpr0 = COPY [[FMUL]](s32)
104+
%0:_(s32) = COPY $sgpr0
105+
%1:_(s32) = G_FCONSTANT float -0.5
106+
%2:_(s32) = G_FMUL %0, %1
107+
$sgpr0 = COPY %2(s32)
108+
109+
...
110+
111+
---
112+
name: test_neg_one_f16_vgpr
113+
tracksRegLiveness: true
114+
body: |
115+
bb.0:
116+
liveins: $vgpr0
117+
118+
; CHECK-LABEL: name: test_neg_one_f16_vgpr
119+
; CHECK: liveins: $vgpr0
120+
; CHECK-NEXT: {{ $}}
121+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
122+
; CHECK-NEXT: %x:_(s16) = G_TRUNC [[COPY]](s32)
123+
; CHECK-NEXT: %d:_(s16) = G_FNEG %x
124+
; CHECK-NEXT: %ext:_(s32) = G_ANYEXT %d(s16)
125+
; CHECK-NEXT: $vgpr0 = COPY %ext(s32)
126+
%0:_(s32) = COPY $vgpr0
127+
%x:_(s16) = G_TRUNC %0:_(s32)
128+
%y:_(s16) = G_FCONSTANT half -1.0
129+
%d:_(s16) = G_FMUL %x, %y
130+
%ext:_(s32) = G_ANYEXT %d:_(s16)
131+
$vgpr0 = COPY %ext
132+
133+
...
134+
135+
---
136+
name: test_neg_one_f32_vgpr
137+
body: |
138+
bb.0:
139+
liveins: $vgpr0
140+
141+
; CHECK-LABEL: name: test_neg_one_f32_vgpr
142+
; CHECK: liveins: $vgpr0
143+
; CHECK-NEXT: {{ $}}
144+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
145+
; CHECK-NEXT: [[FNEG:%[0-9]+]]:_(s32) = G_FNEG [[COPY]]
146+
; CHECK-NEXT: $vgpr0 = COPY [[FNEG]](s32)
147+
%0:_(s32) = COPY $vgpr0
148+
%1:_(s32) = G_FCONSTANT float -1.0
149+
%2:_(s32) = G_FMUL %0, %1
150+
$vgpr0 = COPY %2(s32)
151+
152+
...
153+
154+
---
155+
name: test_neg_one_f64_vgpr
156+
tracksRegLiveness: true
157+
body: |
158+
bb.0:
159+
liveins: $vgpr0
160+
161+
; CHECK-LABEL: name: test_neg_one_f64_vgpr
162+
; CHECK: liveins: $vgpr0
163+
; CHECK-NEXT: {{ $}}
164+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
165+
; CHECK-NEXT: %x:_(s64) = G_ANYEXT [[COPY]](s32)
166+
; CHECK-NEXT: %d:_(s64) = G_FNEG %x
167+
; CHECK-NEXT: %ext:_(s32) = G_TRUNC %d(s64)
168+
; CHECK-NEXT: $vgpr0 = COPY %ext(s32)
169+
%0:_(s32) = COPY $vgpr0
170+
%x:_(s64) = G_ANYEXT %0:_(s32)
171+
%y:_(s64) = G_FCONSTANT double -1.0
172+
%d:_(s64) = G_FMUL %x, %y
173+
%ext:_(s32) = G_TRUNC %d:_(s64)
174+
$vgpr0 = COPY %ext
175+
176+
...
177+
178+
---
179+
name: test_neg_ten_f32_vgpr
180+
body: |
181+
bb.0:
182+
liveins: $vgpr0
183+
184+
; CHECK-LABEL: name: test_neg_ten_f32_vgpr
185+
; CHECK: liveins: $vgpr0
186+
; CHECK-NEXT: {{ $}}
187+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
188+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float -1.000000e+01
189+
; CHECK-NEXT: [[FMUL:%[0-9]+]]:_(s32) = G_FMUL [[COPY]], [[C]]
190+
; CHECK-NEXT: $vgpr0 = COPY [[FMUL]](s32)
191+
%0:_(s32) = COPY $vgpr0
192+
%1:_(s32) = G_FCONSTANT float -10.0
193+
%2:_(s32) = G_FMUL %0, %1
194+
$vgpr0 = COPY %2(s32)
195+
196+
...
197+
198+
---
199+
name: test_neg_fract_f32_vgpr
200+
body: |
201+
bb.0:
202+
liveins: $vgpr0
203+
204+
; CHECK-LABEL: name: test_neg_fract_f32_vgpr
205+
; CHECK: liveins: $vgpr0
206+
; CHECK-NEXT: {{ $}}
207+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
208+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_FCONSTANT float -5.000000e-01
209+
; CHECK-NEXT: [[FMUL:%[0-9]+]]:_(s32) = G_FMUL [[COPY]], [[C]]
210+
; CHECK-NEXT: $vgpr0 = COPY [[FMUL]](s32)
211+
%0:_(s32) = COPY $vgpr0
212+
%1:_(s32) = G_FCONSTANT float -0.5
213+
%2:_(s32) = G_FMUL %0, %1
214+
$vgpr0 = COPY %2(s32)
215+
216+
...

0 commit comments

Comments
 (0)