1 // RUN: llvm-tblgen -I %p/../../../include -gen-global-isel-combiner \
2 // RUN: -gicombiner-stop-after-parse -combiners=MyCombiner %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; }
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])
20 // CHECK-NEXT: (ApplyPats
21 // CHECK-NEXT: __WipOpcodeTest0_apply_0:(CXXPattern apply code:"APPLY")
23 // CHECK-NEXT: (OperandTable MatchPats <empty>)
24 // CHECK-NEXT: (OperandTable ApplyPats <empty>)
26 def WipOpcodeTest0 : GICombineRule<
28 (match (wip_match_opcode G_TRUNC):$d),
31 // CHECK: (CombineRule name:WipOpcodeTest1 id:1 root:d
32 // CHECK-NEXT: (MatchPats
33 // CHECK-NEXT: <match_root>d:(AnyOpcodePattern [G_TRUNC, G_SEXT])
35 // CHECK-NEXT: (ApplyPats
36 // CHECK-NEXT: __WipOpcodeTest1_apply_0:(CXXPattern apply code:"APPLY")
38 // CHECK-NEXT: (OperandTable MatchPats <empty>)
39 // CHECK-NEXT: (OperandTable ApplyPats <empty>)
41 def WipOpcodeTest1 : GICombineRule<
43 (match (wip_match_opcode G_TRUNC, G_SEXT):$d),
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])
50 // CHECK-NEXT: (ApplyPats
51 // CHECK-NEXT: __InstTest0_apply_0:(CXXPattern apply code:"APPLY")
53 // CHECK-NEXT: (OperandTable MatchPats
55 // CHECK-NEXT: b -> <live-in>
57 // CHECK-NEXT: (OperandTable ApplyPats <empty>)
59 def InstTest0 : GICombineRule<
61 (match (COPY $a, $b):$d),
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)
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])
72 // CHECK-NEXT: (ApplyPats
73 // CHECK-NEXT: __InstTest1_apply_0:(CXXPattern apply code:"APPLY")
75 // CHECK-NEXT: (OperandTable MatchPats
77 // CHECK-NEXT: b -> <live-in>
78 // CHECK-NEXT: x -> __InstTest1_match_1
80 // CHECK-NEXT: (OperandTable ApplyPats <empty>)
82 let Predicates = [HasAnswerToEverything] in
83 def InstTest1 : GICombineRule<
84 (defs root:$d, reg_matchinfo:$r0),
85 (match (COPY $a, i32:$b):$d,
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)
93 // CHECK-NEXT: (MatchPats
94 // CHECK-NEXT: <match_root>__InstTest2_match_0:(CodeGenInstructionPattern COPY operands:[<def>$d, (i32 0):$x])
96 // CHECK-NEXT: (ApplyPats
97 // CHECK-NEXT: __InstTest2_apply_0:(CXXPattern apply code:"APPLY")
99 // CHECK-NEXT: (OperandTable MatchPats
100 // CHECK-NEXT: d -> __InstTest2_match_0
101 // CHECK-NEXT: x -> <live-in>
103 // CHECK-NEXT: (OperandTable ApplyPats <empty>)
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])
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}")
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
124 // CHECK-NEXT: (OperandTable ApplyPats
125 // CHECK-NEXT: dst -> __InOutInstTest0_apply_0
126 // CHECK-NEXT: src -> <live-in>
129 def InOutInstTest0 : GICombineRule<
131 (match (COPY $dst, $tmp),
132 (G_ZEXT $tmp, $src)),
133 (apply (G_TRUNC $dst, $src), "APPLY ${src}")>;
135 def MatchICst: GICombinePatFrag<
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 [
146 // CHECK-NEXT: (CXXPattern name:__MatchICst_alt0_pattern_0 match code:"return matchIConstant(${foo}, ${cst})"),
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)])
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}")
159 // CHECK-NEXT: (OperandTable MatchPats
160 // CHECK-NEXT: cst -> <live-in>
161 // CHECK-NEXT: dst -> __PatFragTest0_match_0
163 // CHECK-NEXT: (OperandTable ApplyPats
164 // CHECK-NEXT: dst -> __PatFragTest0_apply_0
167 def PatFragTest0 : GICombineRule<
169 (match (G_ZEXT $dst, $cst), (MatchICst $cst, (i32 0))),
170 (apply (COPY $dst, (i32 0)), "APPLY ${src}")>;
172 def MatchFooPerms: GICombinePatFrag<
174 (ins gi_mo:$foo, gi_imm:$cst),
176 (pattern "return foo(${foo}, ${cst})"),
177 (pattern "return bar(${foo}, ${cst})"),
178 (pattern "return bux(${foo}, ${cst})"),
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 [
187 // CHECK-NEXT: (CXXPattern name:__MatchFooPerms_alt0_pattern_0 match code:"return foo(${foo}, ${cst})"),
190 // CHECK-NEXT: (CXXPattern name:__MatchFooPerms_alt1_pattern_0 match code:"return bar(${foo}, ${cst})"),
193 // CHECK-NEXT: (CXXPattern name:__MatchFooPerms_alt2_pattern_0 match code:"return bux(${foo}, ${cst})"),
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)])
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}")
208 // CHECK-NEXT: (OperandTable MatchPats
209 // CHECK-NEXT: cst -> <live-in>
210 // CHECK-NEXT: dst -> __PatFragTest1_match_0
212 // CHECK-NEXT: (OperandTable ApplyPats
213 // CHECK-NEXT: dst -> __PatFragTest1_apply_0
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]],
245 let MaxPermutations = -1 in
246 def PatFragTest1 : GICombineRule<
248 (match (G_ZEXT $dst, $cst),
249 (MatchFooPerms $cst, (i32 0)):$a,
250 (MatchFooPerms $cst, (i32 0)):$b,
251 (MatchFooPerms $cst, (i32 0)):$c
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])
259 // CHECK-NEXT: (ApplyPats
260 // CHECK-NEXT: <apply_root>__VariadicsInTest_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])
262 // CHECK-NEXT: (OperandTable MatchPats
263 // CHECK-NEXT: a -> <live-in>
264 // CHECK-NEXT: b -> <live-in>
265 // CHECK-NEXT: dst -> __VariadicsInTest_match_0
267 // CHECK-NEXT: (OperandTable ApplyPats
268 // CHECK-NEXT: dst -> __VariadicsInTest_apply_0
271 def VariadicsInTest : GICombineRule<
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])
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)])
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>
289 // CHECK-NEXT: (OperandTable ApplyPats
290 // CHECK-NEXT: a -> __VariadicsOutTest_apply_0
291 // CHECK-NEXT: b -> __VariadicsOutTest_apply_1
294 def VariadicsOutTest : GICombineRule<
296 (match (G_UNMERGE_VALUES $a, $b, $src)),
297 (apply (COPY $a, (i32 0)),
298 (COPY $b, (i32 0)))>;
301 def MyCombiner: GICombiner<"GenMyCombiner", [