Skip to content

Commit 9c60010

Browse files
author
Thorsten Schütt
authored
[GlobalIsel] Combine G_ADD and G_SUB (#92879)
1 parent 0bf181e commit 9c60010

File tree

2 files changed

+326
-1
lines changed

2 files changed

+326
-1
lines changed

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,78 @@ extract_vector_element_shuffle_vector,
16411641
insert_vector_element_extract_vector_element
16421642
]>;
16431643

1644+
1645+
// fold ((0-A) + B) -> B-A
1646+
def ZeroMinusAPlusB : GICombineRule<
1647+
(defs root:$root),
1648+
(match (G_SUB $sub, 0, $A),
1649+
(G_ADD $root, $sub, $B)),
1650+
(apply (G_SUB $root, $B, $A))>;
1651+
1652+
// fold (A + (0-B)) -> A-B
1653+
def APlusZeroMinusB : GICombineRule<
1654+
(defs root:$root),
1655+
(match (G_SUB $sub, 0, $B),
1656+
(G_ADD $root, $A, $sub)),
1657+
(apply (G_SUB $root, $A, $B))>;
1658+
1659+
// fold (A+(B-A)) -> B
1660+
def APlusBMinusB : GICombineRule<
1661+
(defs root:$root),
1662+
(match (G_SUB $sub, $B, $A),
1663+
(G_ADD $root, $A, $sub)),
1664+
(apply (GIReplaceReg $root, $B))>;
1665+
1666+
// fold ((B-A)+A) -> B
1667+
def BMinusAPlusA : GICombineRule<
1668+
(defs root:$root),
1669+
(match (G_SUB $sub, $B, $A),
1670+
(G_ADD $root, $sub, $A)),
1671+
(apply (GIReplaceReg $root, $B))>;
1672+
1673+
// fold ((A-B)+(C-A)) -> (C-B)
1674+
def AMinusBPlusCMinusA : GICombineRule<
1675+
(defs root:$root),
1676+
(match (G_SUB $sub1, $A, $B),
1677+
(G_SUB $sub2, $C, $A),
1678+
(G_ADD $root, $sub1, $sub2)),
1679+
(apply (G_SUB $root, $C, $B))>;
1680+
1681+
// fold ((A-B)+(B-C)) -> (A-C)
1682+
def AMinusBPlusBMinusC : GICombineRule<
1683+
(defs root:$root),
1684+
(match (G_SUB $sub1, $A, $B),
1685+
(G_SUB $sub2, $B, $C),
1686+
(G_ADD $root, $sub1, $sub2)),
1687+
(apply (G_SUB $root, $A, $C))>;
1688+
1689+
// fold (A+(B-(A+C))) to (B-C)
1690+
def APlusBMinusAplusC : GICombineRule<
1691+
(defs root:$root),
1692+
(match (G_ADD $add1, $A, $C),
1693+
(G_SUB $sub1, $B, $add1),
1694+
(G_ADD $root, $A, $sub1)),
1695+
(apply (G_SUB $root, $B, $C))>;
1696+
1697+
// fold (A+(B-(C+A))) to (B-C)
1698+
def APlusBMinusCPlusA : GICombineRule<
1699+
(defs root:$root),
1700+
(match (G_ADD $add1, $C, $A),
1701+
(G_SUB $sub1, $B, $add1),
1702+
(G_ADD $root, $A, $sub1)),
1703+
(apply (G_SUB $root, $B, $C))>;
1704+
1705+
def integer_reassoc_combines: GICombineGroup<[
1706+
ZeroMinusAPlusB,
1707+
APlusZeroMinusB,
1708+
APlusBMinusB,
1709+
BMinusAPlusA,
1710+
AMinusBPlusCMinusA,
1711+
AMinusBPlusBMinusC,
1712+
APlusBMinusAplusC,
1713+
APlusBMinusCPlusA
1714+
]>;
1715+
16441716
// FIXME: These should use the custom predicate feature once it lands.
16451717
def undef_combines : GICombineGroup<[undef_to_fp_zero, undef_to_int_zero,
16461718
undef_to_negative_one,
@@ -1698,7 +1770,8 @@ def fma_combines : GICombineGroup<[combine_fadd_fmul_to_fmad_or_fma,
16981770
def constant_fold_binops : GICombineGroup<[constant_fold_binop,
16991771
constant_fold_fp_binop]>;
17001772

1701-
def all_combines : GICombineGroup<[trivial_combines, vector_ops_combines,
1773+
def all_combines : GICombineGroup<[integer_reassoc_combines, trivial_combines,
1774+
vector_ops_combines,
17021775
insert_vec_elt_combines, extract_vec_elt_combines, combines_for_extload,
17031776
combine_extracted_vector_load,
17041777
undef_combines, identity_combines, phi_combines,
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner %s -o - | FileCheck %s
3+
4+
5+
---
6+
name: ZeroMinusAPlusB
7+
body: |
8+
bb.0:
9+
liveins: $w0, $w1
10+
11+
; CHECK-LABEL: name: ZeroMinusAPlusB
12+
; CHECK: liveins: $w0, $w1
13+
; CHECK-NEXT: {{ $}}
14+
; CHECK-NEXT: %a:_(s32) = COPY $w0
15+
; CHECK-NEXT: %b:_(s32) = COPY $w0
16+
; CHECK-NEXT: %add:_(s32) = G_SUB %b, %a
17+
; CHECK-NEXT: $w0 = COPY %add(s32)
18+
; CHECK-NEXT: RET_ReallyLR implicit $w0
19+
%x:_(s32) = COPY $w0
20+
%a:_(s32) = COPY $w0
21+
%b:_(s32) = COPY $w0
22+
%zero:_(s32) = G_CONSTANT i32 0
23+
%sub:_(s32) = G_SUB %zero, %a
24+
%add:_(s32) = G_ADD %sub, %b
25+
$w0 = COPY %add
26+
RET_ReallyLR implicit $w0
27+
28+
...
29+
---
30+
name: ZeroMinusAPlusB_multi_use
31+
body: |
32+
bb.0:
33+
liveins: $w0, $w1
34+
35+
; CHECK-LABEL: name: ZeroMinusAPlusB_multi_use
36+
; CHECK: liveins: $w0, $w1
37+
; CHECK-NEXT: {{ $}}
38+
; CHECK-NEXT: %a:_(s32) = COPY $w0
39+
; CHECK-NEXT: %b:_(s32) = COPY $w0
40+
; CHECK-NEXT: %zero:_(s32) = G_CONSTANT i32 0
41+
; CHECK-NEXT: %sub:_(s32) = G_SUB %zero, %a
42+
; CHECK-NEXT: %add:_(s32) = G_SUB %b, %a
43+
; CHECK-NEXT: $w0 = COPY %add(s32)
44+
; CHECK-NEXT: $w0 = COPY %sub(s32)
45+
; CHECK-NEXT: RET_ReallyLR implicit $w0
46+
%x:_(s32) = COPY $w0
47+
%a:_(s32) = COPY $w0
48+
%b:_(s32) = COPY $w0
49+
%zero:_(s32) = G_CONSTANT i32 0
50+
%sub:_(s32) = G_SUB %zero, %a
51+
%add:_(s32) = G_ADD %sub, %b
52+
$w0 = COPY %add
53+
$w0 = COPY %sub
54+
RET_ReallyLR implicit $w0
55+
56+
...
57+
---
58+
name: APlusZeroMiunusB
59+
body: |
60+
bb.0:
61+
liveins: $w0, $w1
62+
63+
; CHECK-LABEL: name: APlusZeroMiunusB
64+
; CHECK: liveins: $w0, $w1
65+
; CHECK-NEXT: {{ $}}
66+
; CHECK-NEXT: %a:_(s64) = COPY $x1
67+
; CHECK-NEXT: %b:_(s64) = COPY $x2
68+
; CHECK-NEXT: %add:_(s64) = G_SUB %a, %b
69+
; CHECK-NEXT: $x0 = COPY %add(s64)
70+
; CHECK-NEXT: RET_ReallyLR implicit $x0
71+
%x:_(s64) = COPY $x0
72+
%a:_(s64) = COPY $x1
73+
%b:_(s64) = COPY $x2
74+
%zero:_(s64) = G_CONSTANT i64 0
75+
%sub:_(s64) = G_SUB %zero, %b
76+
%add:_(s64) = G_ADD %a, %sub
77+
$x0 = COPY %add
78+
RET_ReallyLR implicit $x0
79+
80+
...
81+
---
82+
name: APlusBMinusB
83+
body: |
84+
bb.0:
85+
liveins: $w0, $w1
86+
87+
; CHECK-LABEL: name: APlusBMinusB
88+
; CHECK: liveins: $w0, $w1
89+
; CHECK-NEXT: {{ $}}
90+
; CHECK-NEXT: %b:_(s64) = COPY $x1
91+
; CHECK-NEXT: $x0 = COPY %b(s64)
92+
; CHECK-NEXT: RET_ReallyLR implicit $x0
93+
%a:_(s64) = COPY $x0
94+
%b:_(s64) = COPY $x1
95+
%zero:_(s64) = G_CONSTANT i64 0
96+
%sub:_(s64) = G_SUB %b, %a
97+
%add:_(s64) = G_ADD %a, %sub
98+
$x0 = COPY %add
99+
RET_ReallyLR implicit $x0
100+
101+
...
102+
---
103+
name: BMinusAPlusA
104+
body: |
105+
bb.0:
106+
liveins: $w0, $w1
107+
108+
; CHECK-LABEL: name: BMinusAPlusA
109+
; CHECK: liveins: $w0, $w1
110+
; CHECK-NEXT: {{ $}}
111+
; CHECK-NEXT: %b:_(s64) = COPY $x1
112+
; CHECK-NEXT: $x0 = COPY %b(s64)
113+
; CHECK-NEXT: RET_ReallyLR implicit $x0
114+
%a:_(s64) = COPY $x0
115+
%b:_(s64) = COPY $x1
116+
%zero:_(s64) = G_CONSTANT i64 0
117+
%sub:_(s64) = G_SUB %b, %a
118+
%add:_(s64) = G_ADD %sub, %a
119+
$x0 = COPY %add
120+
RET_ReallyLR implicit $x0
121+
122+
...
123+
---
124+
name: AMinusBPlusCMinusA
125+
body: |
126+
bb.0:
127+
liveins: $w0, $w1
128+
129+
; CHECK-LABEL: name: AMinusBPlusCMinusA
130+
; CHECK: liveins: $w0, $w1
131+
; CHECK-NEXT: {{ $}}
132+
; CHECK-NEXT: %b:_(s64) = COPY $x1
133+
; CHECK-NEXT: %c:_(s64) = COPY $x2
134+
; CHECK-NEXT: %add:_(s64) = G_SUB %c, %b
135+
; CHECK-NEXT: $x0 = COPY %add(s64)
136+
; CHECK-NEXT: RET_ReallyLR implicit $x0
137+
%a:_(s64) = COPY $x0
138+
%b:_(s64) = COPY $x1
139+
%c:_(s64) = COPY $x2
140+
%zero:_(s64) = G_CONSTANT i64 0
141+
%sub2:_(s64) = G_SUB %c, %a
142+
%sub1:_(s64) = G_SUB %a, %b
143+
%add:_(s64) = G_ADD %sub1, %sub2
144+
$x0 = COPY %add
145+
RET_ReallyLR implicit $x0
146+
147+
...
148+
---
149+
name: AMinusBPlusBMinusC
150+
body: |
151+
bb.0:
152+
liveins: $w0, $w1
153+
154+
; CHECK-LABEL: name: AMinusBPlusBMinusC
155+
; CHECK: liveins: $w0, $w1
156+
; CHECK-NEXT: {{ $}}
157+
; CHECK-NEXT: %a:_(s64) = COPY $x0
158+
; CHECK-NEXT: %c:_(s64) = COPY $x2
159+
; CHECK-NEXT: %add:_(s64) = G_SUB %a, %c
160+
; CHECK-NEXT: $x0 = COPY %add(s64)
161+
; CHECK-NEXT: RET_ReallyLR implicit $x0
162+
%a:_(s64) = COPY $x0
163+
%b:_(s64) = COPY $x1
164+
%c:_(s64) = COPY $x2
165+
%zero:_(s64) = G_CONSTANT i64 0
166+
%sub2:_(s64) = G_SUB %b, %c
167+
%sub1:_(s64) = G_SUB %a, %b
168+
%add:_(s64) = G_ADD %sub1, %sub2
169+
$x0 = COPY %add
170+
RET_ReallyLR implicit $x0
171+
172+
173+
...
174+
---
175+
name: APlusBMinusAplusC
176+
body: |
177+
bb.0:
178+
liveins: $w0, $w1
179+
180+
; CHECK-LABEL: name: APlusBMinusAplusC
181+
; CHECK: liveins: $w0, $w1
182+
; CHECK-NEXT: {{ $}}
183+
; CHECK-NEXT: %b:_(s64) = COPY $x1
184+
; CHECK-NEXT: %c:_(s64) = COPY $x2
185+
; CHECK-NEXT: %add:_(s64) = G_SUB %b, %c
186+
; CHECK-NEXT: $x0 = COPY %add(s64)
187+
; CHECK-NEXT: RET_ReallyLR implicit $x0
188+
%a:_(s64) = COPY $x0
189+
%b:_(s64) = COPY $x1
190+
%c:_(s64) = COPY $x2
191+
%zero:_(s64) = G_CONSTANT i64 0
192+
%add1:_(s64) = G_ADD %a, %c
193+
%sub1:_(s64) = G_SUB %b, %add1
194+
%add:_(s64) = G_ADD %a, %sub1
195+
$x0 = COPY %add
196+
RET_ReallyLR implicit $x0
197+
198+
...
199+
---
200+
name: APlusBMinusCPlusA
201+
body: |
202+
bb.0:
203+
liveins: $w0, $w1
204+
205+
; CHECK-LABEL: name: APlusBMinusCPlusA
206+
; CHECK: liveins: $w0, $w1
207+
; CHECK-NEXT: {{ $}}
208+
; CHECK-NEXT: %b:_(s64) = COPY $x1
209+
; CHECK-NEXT: %c:_(s64) = COPY $x2
210+
; CHECK-NEXT: %add:_(s64) = G_SUB %b, %c
211+
; CHECK-NEXT: $x0 = COPY %add(s64)
212+
; CHECK-NEXT: RET_ReallyLR implicit $x0
213+
%a:_(s64) = COPY $x0
214+
%b:_(s64) = COPY $x1
215+
%c:_(s64) = COPY $x2
216+
%zero:_(s64) = G_CONSTANT i64 0
217+
%add1:_(s64) = G_ADD %c, %a
218+
%sub1:_(s64) = G_SUB %b, %add1
219+
%add:_(s64) = G_ADD %a, %sub1
220+
$x0 = COPY %add
221+
RET_ReallyLR implicit $x0
222+
223+
...
224+
---
225+
name: APlusBMinusCPlusA_BV
226+
body: |
227+
bb.0:
228+
liveins: $w0, $w1
229+
230+
; CHECK-LABEL: name: APlusBMinusCPlusA_BV
231+
; CHECK: liveins: $w0, $w1
232+
; CHECK-NEXT: {{ $}}
233+
; CHECK-NEXT: %a1:_(s64) = COPY $x0
234+
; CHECK-NEXT: %b1:_(s64) = COPY $x1
235+
; CHECK-NEXT: %c1:_(s64) = COPY $x2
236+
; CHECK-NEXT: %b:_(<2 x s64>) = G_BUILD_VECTOR %b1(s64), %ba:_(s64)
237+
; CHECK-NEXT: %c:_(<2 x s64>) = G_BUILD_VECTOR %a1(s64), %c1(s64)
238+
; CHECK-NEXT: %add:_(<2 x s64>) = G_SUB %b, %c
239+
; CHECK-NEXT: $q0 = COPY %add(<2 x s64>)
240+
; CHECK-NEXT: RET_ReallyLR implicit $x0
241+
%a1:_(s64) = COPY $x0
242+
%b1:_(s64) = COPY $x1
243+
%c1:_(s64) = COPY $x2
244+
%a:_(<2 x s64>) = G_BUILD_VECTOR %a1:_(s64), %b1:_(s64)
245+
%b:_(<2 x s64>) = G_BUILD_VECTOR %b1:_(s64), %ba:_(s64)
246+
%c:_(<2 x s64>) = G_BUILD_VECTOR %a1:_(s64), %c1:_(s64)
247+
%zero:_(s64) = G_CONSTANT i64 0
248+
%add1:_(<2 x s64>) = G_ADD %c, %a
249+
%sub1:_(<2 x s64>) = G_SUB %b, %add1
250+
%add:_(<2 x s64>) = G_ADD %a, %sub1
251+
$q0 = COPY %add
252+
RET_ReallyLR implicit $x0

0 commit comments

Comments
 (0)