[NFC][RemoveDIs] Prefer iterators over inst-pointers in InstCombine
[llvm-project.git] / llvm / test / TableGen / GlobalISelCombinerEmitter / pattern-parsing.td
blobbc75b15233b55195cfe9ec860e7faa2fd3928169
1 // RUN: llvm-tblgen -I %p/../../../include -gen-global-isel-combiner \
2 // RUN:     -gicombiner-stop-after-parse -combiners=MyCombiner %s | \
3 // RUN: FileCheck %s
5 include "llvm/Target/Target.td"
6 include "llvm/Target/GlobalISel/Combine.td"
8 def MyTargetISA : InstrInfo;
9 def MyTarget : Target { let InstructionSet = MyTargetISA; }
11 def dummy;
13 def HasAnswerToEverything : Predicate<"Subtarget->getAnswerToUniverse() == 42 && Subtarget->getAnswerToLife() == 42">;
14 def reg_matchinfo : GIDefMatchData<"Register">;
16 // CHECK:      (CombineRule name:WipOpcodeTest0 id:0 root:d
17 // CHECK-NEXT:   (MatchPats
18 // CHECK-NEXT:     <match_root>d:(AnyOpcodePattern [G_TRUNC])
19 // CHECK-NEXT:   )
20 // CHECK-NEXT:   (ApplyPats
21 // CHECK-NEXT:     __WipOpcodeTest0_apply_0:(CXXPattern apply code:"APPLY")
22 // CHECK-NEXT:   )
23 // CHECK-NEXT:   (OperandTable MatchPats <empty>)
24 // CHECK-NEXT:   (OperandTable ApplyPats <empty>)
25 // CHECK-NEXT: )
26 def WipOpcodeTest0 : GICombineRule<
27   (defs root:$d),
28   (match (wip_match_opcode G_TRUNC):$d),
29   (apply [{ APPLY }])>;
31 // CHECK:     (CombineRule name:WipOpcodeTest1 id:1 root:d
32 // CHECK-NEXT:   (MatchPats
33 // CHECK-NEXT:     <match_root>d:(AnyOpcodePattern [G_TRUNC, G_SEXT])
34 // CHECK-NEXT:   )
35 // CHECK-NEXT:   (ApplyPats
36 // CHECK-NEXT:     __WipOpcodeTest1_apply_0:(CXXPattern apply code:"APPLY")
37 // CHECK-NEXT:   )
38 // CHECK-NEXT:   (OperandTable MatchPats <empty>)
39 // CHECK-NEXT:   (OperandTable ApplyPats <empty>)
40 // CHECK-NEXT: )
41 def WipOpcodeTest1 : GICombineRule<
42   (defs root:$d),
43   (match (wip_match_opcode G_TRUNC, G_SEXT):$d),
44   (apply [{ APPLY }])>;
46 // CHECK:     (CombineRule name:InstTest0 id:2 root:d
47 // CHECK-NEXT:   (MatchPats
48 // CHECK-NEXT:     <match_root>d:(CodeGenInstructionPattern COPY operands:[<def>$a, $b])
49 // CHECK-NEXT:   )
50 // CHECK-NEXT:   (ApplyPats
51 // CHECK-NEXT:     __InstTest0_apply_0:(CXXPattern apply code:"APPLY")
52 // CHECK-NEXT:   )
53 // CHECK-NEXT:   (OperandTable MatchPats
54 // CHECK-NEXT:     a -> d
55 // CHECK-NEXT:     b -> <live-in>
56 // CHECK-NEXT:   )
57 // CHECK-NEXT:   (OperandTable ApplyPats <empty>)
58 // CHECK-NEXT: )
59 def InstTest0 : GICombineRule<
60   (defs root:$d),
61   (match (COPY $a, $b):$d),
62   (apply [{ APPLY }])>;
64 // CHECK:      (CombineRule name:InstTest1 id:3 root:d
65 // CHECK-NEXT:   (MatchDatas
66 // CHECK-NEXT:      (MatchDataInfo pattern_symbol:r0 type:'Register' var_name:MDInfo0)
67 // CHECK-NEXT:   )
68 // CHECK-NEXT:   (MatchPats
69 // CHECK-NEXT:     <match_root>d:(CodeGenInstructionPattern COPY operands:[<def>$a, i32:$b])
70 // CHECK-NEXT:     __InstTest1_match_1:(CodeGenInstructionPattern G_ZEXT operands:[<def>$x, 0])
71 // CHECK-NEXT:   )
72 // CHECK-NEXT:   (ApplyPats
73 // CHECK-NEXT:     __InstTest1_apply_0:(CXXPattern apply code:"APPLY")
74 // CHECK-NEXT:   )
75 // CHECK-NEXT:   (OperandTable MatchPats
76 // CHECK-NEXT:     a -> d
77 // CHECK-NEXT:     b -> <live-in>
78 // CHECK-NEXT:     x -> __InstTest1_match_1
79 // CHECK-NEXT:   )
80 // CHECK-NEXT:   (OperandTable ApplyPats <empty>)
81 // CHECK-NEXT: )
82 let Predicates = [HasAnswerToEverything] in
83 def InstTest1 : GICombineRule<
84   (defs root:$d, reg_matchinfo:$r0),
85   (match (COPY $a, i32:$b):$d,
86          (G_ZEXT $x, 0)),
87   (apply [{ APPLY }])>;
89 // CHECK:      (CombineRule name:InstTest2 id:4 root:d
90 // CHECK-NEXT:   (MatchDatas
91 // CHECK-NEXT:     (MatchDataInfo pattern_symbol:r0 type:'Register' var_name:MDInfo0)
92 // CHECK-NEXT:   )
93 // CHECK-NEXT:   (MatchPats
94 // CHECK-NEXT:     <match_root>__InstTest2_match_0:(CodeGenInstructionPattern COPY operands:[<def>$d, (i32 0):$x])
95 // CHECK-NEXT:   )
96 // CHECK-NEXT:   (ApplyPats
97 // CHECK-NEXT:     __InstTest2_apply_0:(CXXPattern apply code:"APPLY")
98 // CHECK-NEXT:   )
99 // CHECK-NEXT:   (OperandTable MatchPats
100 // CHECK-NEXT:     d -> __InstTest2_match_0
101 // CHECK-NEXT:     x -> <live-in>
102 // CHECK-NEXT:   )
103 // CHECK-NEXT:   (OperandTable ApplyPats <empty>)
104 // CHECK-NEXT: )
105 def InstTest2 : GICombineRule<
106   (defs root:$d, reg_matchinfo:$r0),
107   (match (COPY $d, (i32 0):$x)),
108   (apply [{ APPLY }])>;
110 // CHECK:      (CombineRule name:InOutInstTest0 id:5 root:dst
111 // CHECK-NEXT:   (MatchPats
112 // CHECK-NEXT:     <match_root>__InOutInstTest0_match_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, $tmp])
113 // CHECK-NEXT:     __InOutInstTest0_match_1:(CodeGenInstructionPattern G_ZEXT operands:[<def>$tmp, $src])
114 // CHECK-NEXT:   )
115 // CHECK-NEXT:   (ApplyPats
116 // CHECK-NEXT:     <apply_root>__InOutInstTest0_apply_0:(CodeGenInstructionPattern G_TRUNC operands:[<def>$dst, $src])
117 // CHECK-NEXT:     __InOutInstTest0_apply_1:(CXXPattern apply code:"APPLY ${src}")
118 // CHECK-NEXT:   )
119 // CHECK-NEXT:   (OperandTable MatchPats
120 // CHECK-NEXT:     dst -> __InOutInstTest0_match_0
121 // CHECK-NEXT:     src -> <live-in>
122 // CHECK-NEXT:     tmp -> __InOutInstTest0_match_1
123 // CHECK-NEXT:   )
124 // CHECK-NEXT:   (OperandTable ApplyPats
125 // CHECK-NEXT:     dst -> __InOutInstTest0_apply_0
126 // CHECK-NEXT:     src -> <live-in>
127 // CHECK-NEXT:   )
128 // CHECK-NEXT: )
129 def InOutInstTest0 : GICombineRule<
130   (defs root:$dst),
131   (match (COPY $dst, $tmp),
132          (G_ZEXT $tmp, $src)),
133   (apply (G_TRUNC $dst, $src), "APPLY ${src}")>;
135 def MatchICst: GICombinePatFrag<
136     (outs),
137     (ins gi_mo:$foo, gi_imm:$cst),
138     [(pattern "return matchIConstant(${foo}, ${cst})")]>;
140 // CHECK:      (CombineRule name:PatFragTest0 id:6 root:dst
141 // CHECK-NEXT:   (PatFrags
142 // CHECK-NEXT:     (PatFrag name:MatchICst
143 // CHECK-NEXT:       (ins [foo:machine_operand, cst:imm])
144 // CHECK-NEXT:       (alternatives [
145 // CHECK-NEXT:         [
146 // CHECK-NEXT:           (CXXPattern name:__MatchICst_alt0_pattern_0 match code:"return matchIConstant(${foo}, ${cst})"),
147 // CHECK-NEXT:         ],
148 // CHECK-NEXT:       ])
149 // CHECK-NEXT:     )
150 // CHECK-NEXT:   )
151 // CHECK-NEXT:   (MatchPats
152 // CHECK-NEXT:     <match_root>__PatFragTest0_match_0:(CodeGenInstructionPattern G_ZEXT operands:[<def>$dst, $cst])
153 // CHECK-NEXT:     __PatFragTest0_match_1:(PatFragPattern MatchICst operands:[$cst, (i32 0)])
154 // CHECK-NEXT:   )
155 // CHECK-NEXT:   (ApplyPats
156 // CHECK-NEXT:     <apply_root>__PatFragTest0_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])
157 // CHECK-NEXT:     __PatFragTest0_apply_1:(CXXPattern apply code:"APPLY ${src}")
158 // CHECK-NEXT:   )
159 // CHECK-NEXT:   (OperandTable MatchPats
160 // CHECK-NEXT:     cst -> <live-in>
161 // CHECK-NEXT:     dst -> __PatFragTest0_match_0
162 // CHECK-NEXT:   )
163 // CHECK-NEXT:   (OperandTable ApplyPats
164 // CHECK-NEXT:     dst -> __PatFragTest0_apply_0
165 // CHECK-NEXT:   )
166 // CHECK-NEXT: )
167 def PatFragTest0 : GICombineRule<
168   (defs root:$dst),
169   (match (G_ZEXT $dst, $cst), (MatchICst $cst, (i32 0))),
170   (apply (COPY $dst, (i32 0)), "APPLY ${src}")>;
172 def MatchFooPerms: GICombinePatFrag<
173     (outs),
174     (ins gi_mo:$foo, gi_imm:$cst),
175     [
176       (pattern "return foo(${foo}, ${cst})"),
177       (pattern "return bar(${foo}, ${cst})"),
178       (pattern "return bux(${foo}, ${cst})"),
179     ]>;
181 // CHECK:      (CombineRule name:PatFragTest1 id:7 root:dst
182 // CHECK-NEXT:   (PatFrags
183 // CHECK-NEXT:     (PatFrag name:MatchFooPerms
184 // CHECK-NEXT:       (ins [foo:machine_operand, cst:imm])
185 // CHECK-NEXT:       (alternatives [
186 // CHECK-NEXT:         [
187 // CHECK-NEXT:           (CXXPattern name:__MatchFooPerms_alt0_pattern_0 match code:"return foo(${foo}, ${cst})"),
188 // CHECK-NEXT:         ],
189 // CHECK-NEXT:         [
190 // CHECK-NEXT:           (CXXPattern name:__MatchFooPerms_alt1_pattern_0 match code:"return bar(${foo}, ${cst})"),
191 // CHECK-NEXT:         ],
192 // CHECK-NEXT:         [
193 // CHECK-NEXT:           (CXXPattern name:__MatchFooPerms_alt2_pattern_0 match code:"return bux(${foo}, ${cst})"),
194 // CHECK-NEXT:         ],
195 // CHECK-NEXT:       ])
196 // CHECK-NEXT:     )
197 // CHECK-NEXT:   )
198 // CHECK-NEXT:   (MatchPats
199 // CHECK-NEXT:     <match_root>__PatFragTest1_match_0:(CodeGenInstructionPattern G_ZEXT operands:[<def>$dst, $cst])
200 // CHECK-NEXT:     a:(PatFragPattern MatchFooPerms operands:[$cst, (i32 0)])
201 // CHECK-NEXT:     b:(PatFragPattern MatchFooPerms operands:[$cst, (i32 0)])
202 // CHECK-NEXT:     c:(PatFragPattern MatchFooPerms operands:[$cst, (i32 0)])
203 // CHECK-NEXT:   )
204 // CHECK-NEXT:   (ApplyPats
205 // CHECK-NEXT:     <apply_root>__PatFragTest1_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])
206 // CHECK-NEXT:     __PatFragTest1_apply_1:(CXXPattern apply code:"APPLY ${src}")
207 // CHECK-NEXT:   )
208 // CHECK-NEXT:   (OperandTable MatchPats
209 // CHECK-NEXT:     cst -> <live-in>
210 // CHECK-NEXT:     dst -> __PatFragTest1_match_0
211 // CHECK-NEXT:   )
212 // CHECK-NEXT:   (OperandTable ApplyPats
213 // CHECK-NEXT:     dst -> __PatFragTest1_apply_0
214 // CHECK-NEXT:   )
215 // CHECK-NEXT:   (PermutationsToEmit
216 // CHECK-NEXT:     [a[0], b[0], c[0]],
217 // CHECK-NEXT:     [a[0], b[0], c[1]],
218 // CHECK-NEXT:     [a[0], b[0], c[2]],
219 // CHECK-NEXT:     [a[0], b[1], c[0]],
220 // CHECK-NEXT:     [a[0], b[1], c[1]],
221 // CHECK-NEXT:     [a[0], b[1], c[2]],
222 // CHECK-NEXT:     [a[0], b[2], c[0]],
223 // CHECK-NEXT:     [a[0], b[2], c[1]],
224 // CHECK-NEXT:     [a[0], b[2], c[2]],
225 // CHECK-NEXT:     [a[1], b[0], c[0]],
226 // CHECK-NEXT:     [a[1], b[0], c[1]],
227 // CHECK-NEXT:     [a[1], b[0], c[2]],
228 // CHECK-NEXT:     [a[1], b[1], c[0]],
229 // CHECK-NEXT:     [a[1], b[1], c[1]],
230 // CHECK-NEXT:     [a[1], b[1], c[2]],
231 // CHECK-NEXT:     [a[1], b[2], c[0]],
232 // CHECK-NEXT:     [a[1], b[2], c[1]],
233 // CHECK-NEXT:     [a[1], b[2], c[2]],
234 // CHECK-NEXT:     [a[2], b[0], c[0]],
235 // CHECK-NEXT:     [a[2], b[0], c[1]],
236 // CHECK-NEXT:     [a[2], b[0], c[2]],
237 // CHECK-NEXT:     [a[2], b[1], c[0]],
238 // CHECK-NEXT:     [a[2], b[1], c[1]],
239 // CHECK-NEXT:     [a[2], b[1], c[2]],
240 // CHECK-NEXT:     [a[2], b[2], c[0]],
241 // CHECK-NEXT:     [a[2], b[2], c[1]],
242 // CHECK-NEXT:     [a[2], b[2], c[2]],
243 // CHECK-NEXT:   )
244 // CHECK-NEXT: )
245 let MaxPermutations = -1 in
246 def PatFragTest1 : GICombineRule<
247   (defs root:$dst),
248   (match (G_ZEXT $dst, $cst),
249          (MatchFooPerms $cst, (i32 0)):$a,
250          (MatchFooPerms $cst, (i32 0)):$b,
251          (MatchFooPerms $cst, (i32 0)):$c
252   ),
253   (apply (COPY $dst, (i32 0)), "APPLY ${src}")>;
255 // CHECK:      (CombineRule name:VariadicsInTest id:8 root:dst
256 // CHECK-NEXT:   (MatchPats
257 // CHECK-NEXT:     <match_root>__VariadicsInTest_match_0:(CodeGenInstructionPattern G_BUILD_VECTOR operands:[<def>$dst, $a, $b])
258 // CHECK-NEXT:   )
259 // CHECK-NEXT:   (ApplyPats
260 // CHECK-NEXT:     <apply_root>__VariadicsInTest_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])
261 // CHECK-NEXT:   )
262 // CHECK-NEXT:   (OperandTable MatchPats
263 // CHECK-NEXT:     a -> <live-in>
264 // CHECK-NEXT:     b -> <live-in>
265 // CHECK-NEXT:     dst -> __VariadicsInTest_match_0
266 // CHECK-NEXT:   )
267 // CHECK-NEXT:   (OperandTable ApplyPats
268 // CHECK-NEXT:     dst -> __VariadicsInTest_apply_0
269 // CHECK-NEXT:   )
270 // CHECK-NEXT: )
271 def VariadicsInTest : GICombineRule<
272   (defs root:$dst),
273   (match (G_BUILD_VECTOR $dst, $a, $b)),
274   (apply (COPY $dst, (i32 0)))>;
276 // CHECK:      (CombineRule name:VariadicsOutTest id:9 root:a
277 // CHECK-NEXT:   (MatchPats
278 // CHECK-NEXT:     <match_root>__VariadicsOutTest_match_0:(CodeGenInstructionPattern G_UNMERGE_VALUES operands:[<def>$a, <def>$b, $src])
279 // CHECK-NEXT:   )
280 // CHECK-NEXT:   (ApplyPats
281 // CHECK-NEXT:     <apply_root>__VariadicsOutTest_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$a, (i32 0)])
282 // CHECK-NEXT:     <apply_root>__VariadicsOutTest_apply_1:(CodeGenInstructionPattern COPY operands:[<def>$b, (i32 0)])
283 // CHECK-NEXT:   )
284 // CHECK-NEXT:   (OperandTable MatchPats
285 // CHECK-NEXT:     a -> __VariadicsOutTest_match_0
286 // CHECK-NEXT:     b -> __VariadicsOutTest_match_0
287 // CHECK-NEXT:     src -> <live-in>
288 // CHECK-NEXT:   )
289 // CHECK-NEXT:   (OperandTable ApplyPats
290 // CHECK-NEXT:     a -> __VariadicsOutTest_apply_0
291 // CHECK-NEXT:     b -> __VariadicsOutTest_apply_1
292 // CHECK-NEXT:   )
293 // CHECK-NEXT: )
294 def VariadicsOutTest : GICombineRule<
295   (defs root:$a),
296   (match (G_UNMERGE_VALUES $a, $b, $src)),
297   (apply (COPY $a, (i32 0)),
298          (COPY $b, (i32 0)))>;
301 def MyCombiner: GICombiner<"GenMyCombiner", [
302   WipOpcodeTest0,
303   WipOpcodeTest1,
304   InstTest0,
305   InstTest1,
306   InstTest2,
307   InOutInstTest0,
308   PatFragTest0,
309   PatFragTest1,
310   VariadicsInTest,
311   VariadicsOutTest