435 lines · plain
1// RUN: llvm-tblgen -I %S/Inputs -I %p/../../../include -gen-global-isel-combiner \2// RUN: -gicombiner-stop-after-parse -combiners=MyCombiner %s | \3// RUN: FileCheck %s4 5include "llvm/Target/Target.td"6include "llvm/Target/GlobalISel/Combine.td"7 8include "test-intrinsics.td"9 10def MyTargetISA : InstrInfo;11def MyTarget : Target { let InstructionSet = MyTargetISA; }12 13def dummy;14 15def HasAnswerToEverything : Predicate<"Subtarget->getAnswerToUniverse() == 42 && Subtarget->getAnswerToLife() == 42">;16def reg_matchinfo : GIDefMatchData<"Register">;17 18// CHECK: (CombineRule name:WipOpcodeTest0 id:0 root:d19// CHECK-NEXT: (MatchPats20// CHECK-NEXT: <match_root>d:(AnyOpcodePattern [G_TRUNC])21// CHECK-NEXT: )22// CHECK-NEXT: (ApplyPats23// CHECK-NEXT: __WipOpcodeTest0_apply_0:(CXXPattern apply code:"APPLY")24// CHECK-NEXT: )25// CHECK-NEXT: (OperandTable MatchPats <empty>)26// CHECK-NEXT: (OperandTable ApplyPats <empty>)27// CHECK-NEXT: )28def WipOpcodeTest0 : GICombineRule<29 (defs root:$d),30 (match (wip_match_opcode G_TRUNC):$d),31 (apply [{ APPLY }])>;32 33// CHECK: (CombineRule name:WipOpcodeTest1 id:1 root:d34// CHECK-NEXT: (MatchPats35// CHECK-NEXT: <match_root>d:(AnyOpcodePattern [G_TRUNC, G_SEXT])36// CHECK-NEXT: )37// CHECK-NEXT: (ApplyPats38// CHECK-NEXT: __WipOpcodeTest1_apply_0:(CXXPattern apply code:"APPLY")39// CHECK-NEXT: )40// CHECK-NEXT: (OperandTable MatchPats <empty>)41// CHECK-NEXT: (OperandTable ApplyPats <empty>)42// CHECK-NEXT: )43def WipOpcodeTest1 : GICombineRule<44 (defs root:$d),45 (match (wip_match_opcode G_TRUNC, G_SEXT):$d),46 (apply [{ APPLY }])>;47 48// CHECK: (CombineRule name:InstTest0 id:2 root:d49// CHECK-NEXT: (MatchPats50// CHECK-NEXT: <match_root>d:(CodeGenInstructionPattern COPY operands:[<def>$a, $b])51// CHECK-NEXT: )52// CHECK-NEXT: (ApplyPats53// CHECK-NEXT: __InstTest0_apply_0:(CXXPattern apply code:"APPLY")54// CHECK-NEXT: )55// CHECK-NEXT: (OperandTable MatchPats56// CHECK-NEXT: a -> d57// CHECK-NEXT: b -> <live-in>58// CHECK-NEXT: )59// CHECK-NEXT: (OperandTable ApplyPats <empty>)60// CHECK-NEXT: )61def InstTest0 : GICombineRule<62 (defs root:$d),63 (match (COPY $a, $b):$d),64 (apply [{ APPLY }])>;65 66// CHECK: (CombineRule name:InstTest1 id:3 root:d67// CHECK-NEXT: (MatchDatas68// CHECK-NEXT: (MatchDataDef symbol:r0 type:Register)69// CHECK-NEXT: )70// CHECK-NEXT: (MatchPats71// CHECK-NEXT: <match_root>d:(CodeGenInstructionPattern COPY operands:[<def>$a, i32:$b])72// CHECK-NEXT: __InstTest1_match_1:(CodeGenInstructionPattern G_ZEXT operands:[<def>$x, 0])73// CHECK-NEXT: )74// CHECK-NEXT: (ApplyPats75// CHECK-NEXT: __InstTest1_apply_0:(CXXPattern apply code:"APPLY")76// CHECK-NEXT: )77// CHECK-NEXT: (OperandTable MatchPats78// CHECK-NEXT: a -> d79// CHECK-NEXT: b -> <live-in>80// CHECK-NEXT: x -> __InstTest1_match_181// CHECK-NEXT: )82// CHECK-NEXT: (OperandTable ApplyPats <empty>)83// CHECK-NEXT: )84let Predicates = [HasAnswerToEverything] in85def InstTest1 : GICombineRule<86 (defs root:$d, reg_matchinfo:$r0),87 (match (COPY $a, i32:$b):$d,88 (G_ZEXT $x, 0)),89 (apply [{ APPLY }])>;90 91// CHECK: (CombineRule name:InstTest2 id:4 root:d92// CHECK-NEXT: (MatchDatas93// CHECK-NEXT: (MatchDataDef symbol:r0 type:Register)94// CHECK-NEXT: )95// CHECK-NEXT: (MatchPats96// CHECK-NEXT: <match_root>__InstTest2_match_0:(CodeGenInstructionPattern COPY operands:[<def>$d, (i32 0):$x])97// CHECK-NEXT: )98// CHECK-NEXT: (ApplyPats99// CHECK-NEXT: __InstTest2_apply_0:(CXXPattern apply code:"APPLY")100// CHECK-NEXT: )101// CHECK-NEXT: (OperandTable MatchPats102// CHECK-NEXT: d -> __InstTest2_match_0103// CHECK-NEXT: x -> <live-in>104// CHECK-NEXT: )105// CHECK-NEXT: (OperandTable ApplyPats <empty>)106// CHECK-NEXT: )107def InstTest2 : GICombineRule<108 (defs root:$d, reg_matchinfo:$r0),109 (match (COPY $d, (i32 0):$x)),110 (apply [{ APPLY }])>;111 112// CHECK: (CombineRule name:InOutInstTest0 id:5 root:dst113// CHECK-NEXT: (MatchPats114// CHECK-NEXT: <match_root>__InOutInstTest0_match_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, $tmp])115// CHECK-NEXT: __InOutInstTest0_match_1:(CodeGenInstructionPattern G_ZEXT operands:[<def>$tmp, $src])116// CHECK-NEXT: )117// CHECK-NEXT: (ApplyPats118// CHECK-NEXT: <apply_root>__InOutInstTest0_apply_0:(CodeGenInstructionPattern G_TRUNC operands:[<def>$dst, $src])119// CHECK-NEXT: )120// CHECK-NEXT: (OperandTable MatchPats121// CHECK-NEXT: dst -> __InOutInstTest0_match_0122// CHECK-NEXT: src -> <live-in>123// CHECK-NEXT: tmp -> __InOutInstTest0_match_1124// CHECK-NEXT: )125// CHECK-NEXT: (OperandTable ApplyPats126// CHECK-NEXT: dst -> __InOutInstTest0_apply_0127// CHECK-NEXT: src -> <live-in>128// CHECK-NEXT: )129// CHECK-NEXT: )130def InOutInstTest0 : GICombineRule<131 (defs root:$dst),132 (match (COPY $dst, $tmp),133 (G_ZEXT $tmp, $src)),134 (apply (G_TRUNC $dst, $src))>;135 136def MatchICst: GICombinePatFrag<137 (outs),138 (ins gi_mo:$foo, gi_imm:$cst),139 [(pattern "return matchIConstant(${foo}, ${cst})")]>;140 141// CHECK: (CombineRule name:PatFragTest0 id:6 root:dst142// CHECK-NEXT: (PatFrags143// CHECK-NEXT: (PatFrag name:MatchICst144// CHECK-NEXT: (ins [foo:machine_operand, cst:imm])145// CHECK-NEXT: (alternatives [146// CHECK-NEXT: [147// CHECK-NEXT: (CXXPattern name:__MatchICst_alt0_pattern_0 match code:"return matchIConstant(${foo}, ${cst})"),148// CHECK-NEXT: ],149// CHECK-NEXT: ])150// CHECK-NEXT: )151// CHECK-NEXT: )152// CHECK-NEXT: (MatchPats153// CHECK-NEXT: <match_root>__PatFragTest0_match_0:(CodeGenInstructionPattern G_ZEXT operands:[<def>$dst, $cst])154// CHECK-NEXT: __PatFragTest0_match_1:(PatFragPattern MatchICst operands:[$cst, (i32 0)])155// CHECK-NEXT: )156// CHECK-NEXT: (ApplyPats157// CHECK-NEXT: <apply_root>__PatFragTest0_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])158// CHECK-NEXT: )159// CHECK-NEXT: (OperandTable MatchPats160// CHECK-NEXT: cst -> <live-in>161// CHECK-NEXT: dst -> __PatFragTest0_match_0162// CHECK-NEXT: )163// CHECK-NEXT: (OperandTable ApplyPats164// CHECK-NEXT: dst -> __PatFragTest0_apply_0165// CHECK-NEXT: )166// CHECK-NEXT: )167def PatFragTest0 : GICombineRule<168 (defs root:$dst),169 (match (G_ZEXT $dst, $cst), (MatchICst $cst, (i32 0))),170 (apply (COPY $dst, (i32 0)))>;171 172def 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 ]>;180 181// CHECK: (CombineRule name:PatFragTest1 id:7 root:dst182// CHECK-NEXT: (PatFrags183// CHECK-NEXT: (PatFrag name:MatchFooPerms184// 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: (MatchPats199// 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: (ApplyPats205// CHECK-NEXT: <apply_root>__PatFragTest1_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])206// CHECK-NEXT: )207// CHECK-NEXT: (OperandTable MatchPats208// CHECK-NEXT: cst -> <live-in>209// CHECK-NEXT: dst -> __PatFragTest1_match_0210// CHECK-NEXT: )211// CHECK-NEXT: (OperandTable ApplyPats212// CHECK-NEXT: dst -> __PatFragTest1_apply_0213// CHECK-NEXT: )214// CHECK-NEXT: (PermutationsToEmit215// CHECK-NEXT: [a[0], b[0], c[0]],216// CHECK-NEXT: [a[0], b[0], c[1]],217// CHECK-NEXT: [a[0], b[0], c[2]],218// CHECK-NEXT: [a[0], b[1], c[0]],219// CHECK-NEXT: [a[0], b[1], c[1]],220// CHECK-NEXT: [a[0], b[1], c[2]],221// CHECK-NEXT: [a[0], b[2], c[0]],222// CHECK-NEXT: [a[0], b[2], c[1]],223// CHECK-NEXT: [a[0], b[2], c[2]],224// CHECK-NEXT: [a[1], b[0], c[0]],225// CHECK-NEXT: [a[1], b[0], c[1]],226// CHECK-NEXT: [a[1], b[0], c[2]],227// CHECK-NEXT: [a[1], b[1], c[0]],228// CHECK-NEXT: [a[1], b[1], c[1]],229// CHECK-NEXT: [a[1], b[1], c[2]],230// CHECK-NEXT: [a[1], b[2], c[0]],231// CHECK-NEXT: [a[1], b[2], c[1]],232// CHECK-NEXT: [a[1], b[2], c[2]],233// CHECK-NEXT: [a[2], b[0], c[0]],234// CHECK-NEXT: [a[2], b[0], c[1]],235// CHECK-NEXT: [a[2], b[0], c[2]],236// CHECK-NEXT: [a[2], b[1], c[0]],237// CHECK-NEXT: [a[2], b[1], c[1]],238// CHECK-NEXT: [a[2], b[1], c[2]],239// CHECK-NEXT: [a[2], b[2], c[0]],240// CHECK-NEXT: [a[2], b[2], c[1]],241// CHECK-NEXT: [a[2], b[2], c[2]],242// CHECK-NEXT: )243// CHECK-NEXT: )244let MaxPermutations = -1 in245def PatFragTest1 : GICombineRule<246 (defs root:$dst),247 (match (G_ZEXT $dst, $cst),248 (MatchFooPerms $cst, (i32 0)):$a,249 (MatchFooPerms $cst, (i32 0)):$b,250 (MatchFooPerms $cst, (i32 0)):$c251 ),252 (apply (COPY $dst, (i32 0)))>;253 254// CHECK: (CombineRule name:VariadicsInTest id:8 root:dst255// CHECK-NEXT: (MatchPats256// CHECK-NEXT: <match_root>__VariadicsInTest_match_0:(CodeGenInstructionPattern G_BUILD_VECTOR operands:[<def>$dst, $a, $b])257// CHECK-NEXT: )258// CHECK-NEXT: (ApplyPats259// CHECK-NEXT: <apply_root>__VariadicsInTest_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, (i32 0)])260// CHECK-NEXT: )261// CHECK-NEXT: (OperandTable MatchPats262// CHECK-NEXT: a -> <live-in>263// CHECK-NEXT: b -> <live-in>264// CHECK-NEXT: dst -> __VariadicsInTest_match_0265// CHECK-NEXT: )266// CHECK-NEXT: (OperandTable ApplyPats267// CHECK-NEXT: dst -> __VariadicsInTest_apply_0268// CHECK-NEXT: )269// CHECK-NEXT: )270def VariadicsInTest : GICombineRule<271 (defs root:$dst),272 (match (G_BUILD_VECTOR $dst, $a, $b)),273 (apply (COPY $dst, (i32 0)))>;274 275// CHECK: (CombineRule name:VariadicsOutTest id:9 root:a276// CHECK-NEXT: (MatchPats277// CHECK-NEXT: <match_root>__VariadicsOutTest_match_0:(CodeGenInstructionPattern G_UNMERGE_VALUES operands:[<def>$a, <def>$b, $src])278// CHECK-NEXT: )279// CHECK-NEXT: (ApplyPats280// CHECK-NEXT: <apply_root>__VariadicsOutTest_apply_0:(CodeGenInstructionPattern COPY operands:[<def>$a, (i32 0)])281// CHECK-NEXT: <apply_root>__VariadicsOutTest_apply_1:(CodeGenInstructionPattern COPY operands:[<def>$b, (i32 0)])282// CHECK-NEXT: )283// CHECK-NEXT: (OperandTable MatchPats284// CHECK-NEXT: a -> __VariadicsOutTest_match_0285// CHECK-NEXT: b -> __VariadicsOutTest_match_0286// CHECK-NEXT: src -> <live-in>287// CHECK-NEXT: )288// CHECK-NEXT: (OperandTable ApplyPats289// CHECK-NEXT: a -> __VariadicsOutTest_apply_0290// CHECK-NEXT: b -> __VariadicsOutTest_apply_1291// CHECK-NEXT: )292// CHECK-NEXT: )293def VariadicsOutTest : GICombineRule<294 (defs root:$a),295 (match (G_UNMERGE_VALUES $a, $b, $src)),296 (apply (COPY $a, (i32 0)),297 (COPY $b, (i32 0)))>;298 299// CHECK: (CombineRule name:TypeOfTest id:10 root:dst300// CHECK-NEXT: (MatchPats301// CHECK-NEXT: <match_root>__TypeOfTest_match_0:(CodeGenInstructionPattern COPY operands:[<def>$dst, $tmp])302// CHECK-NEXT: __TypeOfTest_match_1:(CodeGenInstructionPattern G_ZEXT operands:[<def>$tmp, $src])303// CHECK-NEXT: )304// CHECK-NEXT: (ApplyPats305// CHECK-NEXT: <apply_root>__TypeOfTest_apply_0:(CodeGenInstructionPattern G_MUL operands:[<def>$dst, (GITypeOf<$src> 0), (GITypeOf<$dst> -1)])306// CHECK-NEXT: )307// CHECK-NEXT: (OperandTable MatchPats308// CHECK-NEXT: dst -> __TypeOfTest_match_0309// CHECK-NEXT: src -> <live-in>310// CHECK-NEXT: tmp -> __TypeOfTest_match_1311// CHECK-NEXT: )312// CHECK-NEXT: (OperandTable ApplyPats313// CHECK-NEXT: dst -> __TypeOfTest_apply_0314// CHECK-NEXT: )315// CHECK-NEXT: )316def TypeOfTest : GICombineRule<317 (defs root:$dst),318 (match (COPY $dst, $tmp),319 (G_ZEXT $tmp, $src)),320 (apply (G_MUL $dst, (GITypeOf<"$src"> 0), (GITypeOf<"$dst"> -1)))>;321 322 323// CHECK: (CombineRule name:MIFlagsTest id:11 root:dst324// CHECK-NEXT: (MatchPats325// CHECK-NEXT: <match_root>mi:(CodeGenInstructionPattern G_ZEXT operands:[<def>$dst, $src] (MIFlags (set MachineInstr::FmReassoc) (unset MachineInstr::FmNoNans, MachineInstr::FmArcp)))326// CHECK-NEXT: )327// CHECK-NEXT: (ApplyPats328// CHECK-NEXT: <apply_root>__MIFlagsTest_apply_0:(CodeGenInstructionPattern G_MUL operands:[<def>$dst, $src, $src] (MIFlags (set MachineInstr::FmReassoc) (unset MachineInstr::FmNsz, MachineInstr::FmArcp) (copy mi)))329// CHECK-NEXT: )330// CHECK-NEXT: (OperandTable MatchPats331// CHECK-NEXT: dst -> mi332// CHECK-NEXT: src -> <live-in>333// CHECK-NEXT: )334// CHECK-NEXT: (OperandTable ApplyPats335// CHECK-NEXT: dst -> __MIFlagsTest_apply_0336// CHECK-NEXT: src -> <live-in>337// CHECK-NEXT: )338// CHECK-NEXT: )339def MIFlagsTest : GICombineRule<340 (defs root:$dst),341 (match (G_ZEXT $dst, $src, (MIFlags FmReassoc, (not FmNoNans, FmArcp))):$mi),342 (apply (G_MUL $dst, $src, $src, (MIFlags $mi, FmReassoc, (not FmNsz, FmArcp))))>;343 344// CHECK-NEXT: (CombineRule name:IntrinTest0 id:12 root:a345// CHECK-NEXT: (MatchPats346// CHECK-NEXT: <match_root>__IntrinTest0_match_0:(CodeGenInstructionPattern G_INTRINSIC operands:[<def>$a, $b] intrinsic(@llvm.1in.1out))347// CHECK-NEXT: )348// CHECK-NEXT: (ApplyPats349// CHECK-NEXT: <apply_root>__IntrinTest0_apply_0:(CodeGenInstructionPattern G_INTRINSIC_W_SIDE_EFFECTS operands:[<def>$a, $b] intrinsic(@llvm.sideeffects.1in.1out))350// CHECK-NEXT: )351// CHECK-NEXT: (OperandTable MatchPats352// CHECK-NEXT: a -> __IntrinTest0_match_0353// CHECK-NEXT: b -> <live-in>354// CHECK-NEXT: )355// CHECK-NEXT: (OperandTable ApplyPats356// CHECK-NEXT: a -> __IntrinTest0_apply_0357// CHECK-NEXT: b -> <live-in>358// CHECK-NEXT: )359// CHECK-NEXT: )360def IntrinTest0 : GICombineRule<361 (defs root:$a),362 (match (int_1in_1out $a, $b)),363 (apply (int_sideeffects_1in_1out $a, $b))>;364 365// CHECK: (CombineRule name:IntrinTest1 id:13 root:a366// CHECK-NEXT: (MatchPats367// CHECK-NEXT: <match_root>__IntrinTest1_match_0:(CodeGenInstructionPattern G_INTRINSIC_CONVERGENT operands:[<def>$a, $b] intrinsic(@llvm.convergent.1in.1out))368// CHECK-NEXT: )369// CHECK-NEXT: (ApplyPats370// CHECK-NEXT: <apply_root>__IntrinTest1_apply_0:(CodeGenInstructionPattern G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS operands:[<def>$a, $b] intrinsic(@llvm.convergent.sideeffects.1in.1out))371// CHECK-NEXT: )372// CHECK-NEXT: (OperandTable MatchPats373// CHECK-NEXT: a -> __IntrinTest1_match_0374// CHECK-NEXT: b -> <live-in>375// CHECK-NEXT: )376// CHECK-NEXT: (OperandTable ApplyPats377// CHECK-NEXT: a -> __IntrinTest1_apply_0378// CHECK-NEXT: b -> <live-in>379// CHECK-NEXT: )380// CHECK-NEXT: )381def IntrinTest1 : GICombineRule<382 (defs root:$a),383 (match (int_convergent_1in_1out $a, $b)),384 (apply (int_convergent_sideeffects_1in_1out $a, $b))>;385 386// CHECK: (CombineRule name:CombineOperator0 id:14 root:d387// CHECK-NEXT: (MatchPats388// CHECK-NEXT: <match_root>d:(AnyOpcodePattern [G_TRUNC])389// CHECK-NEXT: )390// CHECK-NEXT: (ApplyPats391// CHECK-NEXT: __CombineOperator0_combine_1:(CXXPattern apply code:"return APPLY;")392// CHECK-NEXT: )393// CHECK-NEXT: (OperandTable MatchPats <empty>)394// CHECK-NEXT: (OperandTable ApplyPats <empty>)395// CHECK-NEXT: )396def CombineOperator0 : GICombineRule<397 (defs root:$d),398 (combine (wip_match_opcode G_TRUNC):$d, "return APPLY;")>;399 400// CHECK: (CombineRule name:CombineOperator1 id:15 root:a401// CHECK-NEXT: (MatchPats402// CHECK-NEXT: <match_root>__CombineOperator1_combine_0:(CodeGenInstructionPattern G_TRUNC operands:[<def>$a, $b])403// CHECK-NEXT: )404// CHECK-NEXT: (ApplyPats405// CHECK-NEXT: __CombineOperator1_combine_1:(CXXPattern apply code:"return APPLY ${a} ${b};")406// CHECK-NEXT: )407// CHECK-NEXT: (OperandTable MatchPats408// CHECK-NEXT: a -> __CombineOperator1_combine_0409// CHECK-NEXT: b -> <live-in>410// CHECK-NEXT: )411// CHECK-NEXT: (OperandTable ApplyPats <empty>)412// CHECK-NEXT: )413def CombineOperator1 : GICombineRule<414 (defs root:$a),415 (combine (G_TRUNC $a, $b), "return APPLY ${a} ${b};")>;416 417def MyCombiner: GICombiner<"GenMyCombiner", [418 WipOpcodeTest0,419 WipOpcodeTest1,420 InstTest0,421 InstTest1,422 InstTest2,423 InOutInstTest0,424 PatFragTest0,425 PatFragTest1,426 VariadicsInTest,427 VariadicsOutTest,428 TypeOfTest,429 MIFlagsTest,430 IntrinTest0,431 IntrinTest1,432 CombineOperator0,433 CombineOperator1434]>;435