aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/Translation/FunctionMatch.cs
blob: 073e120a321d1e08e0c028094782935f2b3f77e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
using Ryujinx.Graphics.Shader.Decoders;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Ryujinx.Graphics.Shader.Translation
{
    static class FunctionMatch
    {
        private static IPatternTreeNode[] _fsiGetAddressTree = PatternTrees.GetFsiGetAddress();
        private static IPatternTreeNode[] _fsiGetAddressV2Tree = PatternTrees.GetFsiGetAddressV2();
        private static IPatternTreeNode[] _fsiIsLastWarpThreadPatternTree = PatternTrees.GetFsiIsLastWarpThread();
        private static IPatternTreeNode[] _fsiBeginPatternTree = PatternTrees.GetFsiBeginPattern();
        private static IPatternTreeNode[] _fsiEndPatternTree = PatternTrees.GetFsiEndPattern();

        public static void RunPass(DecodedProgram program)
        {
            byte[] externalRegs = new byte[4];
            bool hasGetAddress = false;

            foreach (DecodedFunction function in program)
            {
                if (function == program.MainFunction)
                {
                    continue;
                }

                int externalReg4 = 0;

                TreeNode[] functionTree = BuildTree(function.Blocks);

                if (Matches(_fsiGetAddressTree, functionTree))
                {
                    externalRegs[1] = functionTree[0].GetRd();
                    externalRegs[2] = functionTree[2].GetRd();
                    externalRegs[3] = functionTree[1].GetRd();
                    externalReg4 = functionTree[3].GetRd();
                }
                else if (Matches(_fsiGetAddressV2Tree, functionTree))
                {
                    externalRegs[1] = functionTree[2].GetRd();
                    externalRegs[2] = functionTree[1].GetRd();
                    externalRegs[3] = functionTree[0].GetRd();
                    externalReg4 = functionTree[3].GetRd();
                }

                // Ensure the register allocation is valid.
                // If so, then we have a match.
                if (externalRegs[1] != externalRegs[2] &&
                    externalRegs[2] != externalRegs[3] &&
                    externalRegs[1] != externalRegs[3] &&
                    externalRegs[1] + 1 != externalRegs[2] &&
                    externalRegs[1] + 1 != externalRegs[3] &&
                    externalRegs[1] + 1 == externalReg4 &&
                    externalRegs[2] != RegisterConsts.RegisterZeroIndex &&
                    externalRegs[3] != RegisterConsts.RegisterZeroIndex &&
                    externalReg4 != RegisterConsts.RegisterZeroIndex)
                {
                    hasGetAddress = true;
                    function.Type = FunctionType.Unused;
                    break;
                }
            }

            foreach (DecodedFunction function in program)
            {
                if (function.IsCompilerGenerated || function == program.MainFunction)
                {
                    continue;
                }

                if (hasGetAddress)
                {
                    TreeNode[] functionTree = BuildTree(function.Blocks);

                    if (MatchesFsi(_fsiBeginPatternTree, program, function, functionTree, externalRegs))
                    {
                        function.Type = FunctionType.BuiltInFSIBegin;
                        continue;
                    }
                    else if (MatchesFsi(_fsiEndPatternTree, program, function, functionTree, externalRegs))
                    {
                        function.Type = FunctionType.BuiltInFSIEnd;
                        continue;
                    }
                }
            }
        }

        private readonly struct TreeNodeUse
        {
            public TreeNode Node { get; }
            public int Index { get; }
            public bool Inverted { get; }

            private TreeNodeUse(int index, bool inverted, TreeNode node)
            {
                Index = index;
                Inverted = inverted;
                Node = node;
            }

            public TreeNodeUse(int index, TreeNode node) : this(index, false, node)
            {
            }

            public TreeNodeUse Flip()
            {
                return new TreeNodeUse(Index, !Inverted, Node);
            }
        }

        private enum TreeNodeType : byte
        {
            Op,
            Label
        }

        private class TreeNode
        {
            public readonly InstOp Op;
            public readonly List<TreeNodeUse> Uses;
            public TreeNodeType Type { get; }
            public byte Order { get; }

            public TreeNode(byte order)
            {
                Type = TreeNodeType.Label;
                Order = order;
            }

            public TreeNode(InstOp op, byte order)
            {
                Op = op;
                Uses = new List<TreeNodeUse>();
                Type = TreeNodeType.Op;
                Order = order;
            }

            public byte GetPd()
            {
                return (byte)((Op.RawOpCode >> 3) & 7);
            }

            public byte GetRd()
            {
                return (byte)Op.RawOpCode;
            }
        }

        private static TreeNode[] BuildTree(Block[] blocks)
        {
            List<TreeNode> nodes = new List<TreeNode>();

            Dictionary<ulong, TreeNode> labels = new Dictionary<ulong, TreeNode>();

            TreeNodeUse[] predDefs = new TreeNodeUse[RegisterConsts.PredsCount];
            TreeNodeUse[] gprDefs = new TreeNodeUse[RegisterConsts.GprsCount];

            void DefPred(byte predIndex, int index, TreeNode node)
            {
                if (predIndex != RegisterConsts.PredicateTrueIndex)
                {
                    predDefs[predIndex] = new TreeNodeUse(index, node);
                }
            }

            void DefGpr(byte regIndex, int index, TreeNode node)
            {
                if (regIndex != RegisterConsts.RegisterZeroIndex)
                {
                    gprDefs[regIndex] = new TreeNodeUse(index, node);
                }
            }

            TreeNodeUse UsePred(byte predIndex, bool predInv)
            {
                if (predIndex != RegisterConsts.PredicateTrueIndex)
                {
                    TreeNodeUse use = predDefs[predIndex];

                    if (use.Node != null)
                    {
                        nodes.Remove(use.Node);
                    }
                    else
                    {
                        use = new TreeNodeUse(-(predIndex + 2), null);
                    }

                    return predInv ? use.Flip() : use;
                }

                return new TreeNodeUse(-1, null);
            }

            TreeNodeUse UseGpr(byte regIndex)
            {
                if (regIndex != RegisterConsts.RegisterZeroIndex)
                {
                    TreeNodeUse use = gprDefs[regIndex];

                    if (use.Node != null)
                    {
                        nodes.Remove(use.Node);
                    }
                    else
                    {
                        use = new TreeNodeUse(-(regIndex + 2), null);
                    }

                    return use;
                }

                return new TreeNodeUse(-1, null);
            }

            byte order = 0;

            for (int index = 0; index < blocks.Length; index++)
            {
                Block block = blocks[index];

                if (block.Predecessors.Count > 1)
                {
                    TreeNode label = new TreeNode(order++);
                    nodes.Add(label);
                    labels.Add(block.Address, label);
                }

                for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++)
                {
                    InstOp op = block.OpCodes[opIndex];

                    TreeNode node = new TreeNode(op, IsOrderDependant(op.Name) ? order : (byte)0);

                    // Add uses.

                    if (!op.Props.HasFlag(InstProps.NoPred))
                    {
                        byte predIndex = (byte)((op.RawOpCode >> 16) & 7);
                        bool predInv = (op.RawOpCode & 0x80000) != 0;
                        node.Uses.Add(UsePred(predIndex, predInv));
                    }

                    if (op.Props.HasFlag(InstProps.Ps))
                    {
                        byte predIndex = (byte)((op.RawOpCode >> 39) & 7);
                        bool predInv = (op.RawOpCode & 0x40000000000) != 0;
                        node.Uses.Add(UsePred(predIndex, predInv));
                    }

                    if (op.Props.HasFlag(InstProps.Ra))
                    {
                        byte ra = (byte)(op.RawOpCode >> 8);
                        node.Uses.Add(UseGpr(ra));
                    }

                    if ((op.Props & (InstProps.Rb | InstProps.Rb2)) != 0)
                    {
                        byte rb = op.Props.HasFlag(InstProps.Rb2) ? (byte)op.RawOpCode : (byte)(op.RawOpCode >> 20);
                        node.Uses.Add(UseGpr(rb));
                    }

                    if (op.Props.HasFlag(InstProps.Rc))
                    {
                        byte rc = (byte)(op.RawOpCode >> 39);
                        node.Uses.Add(UseGpr(rc));
                    }

                    if (op.Name == InstName.Bra && labels.TryGetValue(op.GetAbsoluteAddress(), out TreeNode label))
                    {
                        node.Uses.Add(new TreeNodeUse(0, label));
                    }

                    // Make definitions.

                    int defIndex = 0;

                    InstProps pdType = op.Props & InstProps.PdMask;

                    if (pdType != 0)
                    {
                        int bit = pdType switch
                        {
                            InstProps.Pd => 3,
                            InstProps.LPd => 48,
                            InstProps.SPd => 30,
                            InstProps.TPd => 51,
                            InstProps.VPd => 45,
                            _ => throw new InvalidOperationException($"Table has unknown predicate destination {pdType}.")
                        };

                        byte predIndex = (byte)((op.RawOpCode >> bit) & 7);
                        DefPred(predIndex, defIndex++, node);
                    }

                    if (op.Props.HasFlag(InstProps.Rd))
                    {
                        byte rd = (byte)op.RawOpCode;
                        DefGpr(rd, defIndex++, node);
                    }

                    nodes.Add(node);
                }
            }

            return nodes.ToArray();
        }

        private static bool IsOrderDependant(InstName name)
        {
            switch (name)
            {
                case InstName.Atom:
                case InstName.AtomCas:
                case InstName.Atoms:
                case InstName.AtomsCas:
                case InstName.Ld:
                case InstName.Ldg:
                case InstName.Ldl:
                case InstName.Lds:
                case InstName.Suatom:
                case InstName.SuatomB:
                case InstName.SuatomB2:
                case InstName.SuatomCas:
                case InstName.SuatomCasB:
                case InstName.Suld:
                case InstName.SuldB:
                case InstName.SuldD:
                case InstName.SuldDB:
                    return true;
            }

            return false;
        }

        private interface IPatternTreeNode
        {
            List<PatternTreeNodeUse> Uses { get; }
            InstName Name { get; }
            TreeNodeType Type { get; }
            byte Order { get; }
            bool IsImm { get; }
            bool Matches(in InstOp opInfo);
        }

        private readonly struct PatternTreeNodeUse
        {
            public IPatternTreeNode Node { get; }
            public int Index { get; }
            public bool Inverted { get; }
            public PatternTreeNodeUse Inv => new PatternTreeNodeUse(Index, !Inverted, Node);

            private PatternTreeNodeUse(int index, bool inverted, IPatternTreeNode node)
            {
                Index = index;
                Inverted = inverted;
                Node = node;
            }

            public PatternTreeNodeUse(int index, IPatternTreeNode node) : this(index, false, node)
            {
            }
        }

        private class PatternTreeNode<T> : IPatternTreeNode
        {
            public List<PatternTreeNodeUse> Uses { get; }
            private readonly Func<T, bool> _match;

            public InstName Name { get; }
            public TreeNodeType Type { get; }
            public byte Order { get; }
            public bool IsImm { get; }
            public PatternTreeNodeUse Out => new PatternTreeNodeUse(0, this);

            public PatternTreeNode(InstName name, Func<T, bool> match, TreeNodeType type = TreeNodeType.Op, byte order = 0, bool isImm = false)
            {
                Name = name;
                _match = match;
                Type = type;
                Order = order;
                IsImm = isImm;
                Uses = new List<PatternTreeNodeUse>();
            }

            public PatternTreeNode<T> Use(PatternTreeNodeUse use)
            {
                Uses.Add(use);
                return this;
            }

            public PatternTreeNodeUse OutAt(int index)
            {
                return new PatternTreeNodeUse(index, this);
            }

            public bool Matches(in InstOp opInfo)
            {
                if (opInfo.Name != Name)
                {
                    return false;
                }

                ulong rawOp = opInfo.RawOpCode;
                T op = Unsafe.As<ulong, T>(ref rawOp);

                if (!_match(op))
                {
                    return false;
                }

                return true;
            }
        }

        private static bool MatchesFsi(
            IPatternTreeNode[] pattern,
            DecodedProgram program,
            DecodedFunction function,
            TreeNode[] functionTree,
            byte[] externalRegs)
        {
            if (function.Blocks.Length == 0)
            {
                return false;
            }

            InstOp callOp = function.Blocks[0].GetLastOp();

            if (callOp.Name != InstName.Cal)
            {
                return false;
            }

            DecodedFunction callTarget = program.GetFunctionByAddress(callOp.GetAbsoluteAddress());
            TreeNode[] callTargetTree = null;

            if (callTarget == null || !Matches(_fsiIsLastWarpThreadPatternTree, callTargetTree = BuildTree(callTarget.Blocks)))
            {
                return false;
            }

            externalRegs[0] = callTargetTree[0].GetPd();

            if (Matches(pattern, functionTree, externalRegs))
            {
                callTarget.RemoveCaller(function);
                return true;
            }

            return false;
        }

        private static bool Matches(IPatternTreeNode[] pTree, TreeNode[] cTree, byte[] externalRegs = null)
        {
            if (pTree.Length != cTree.Length)
            {
                return false;
            }

            for (int index = 0; index < pTree.Length; index++)
            {
                if (!Matches(pTree[index], cTree[index], externalRegs))
                {
                    return false;
                }
            }

            return true;
        }

        private static bool Matches(IPatternTreeNode pTreeNode, TreeNode cTreeNode, byte[] externalRegs)
        {
            if (!pTreeNode.Matches(in cTreeNode.Op) ||
                pTreeNode.Type != cTreeNode.Type ||
                pTreeNode.Order != cTreeNode.Order ||
                pTreeNode.IsImm != cTreeNode.Op.Props.HasFlag(InstProps.Ib))
            {
                return false;
            }

            if (pTreeNode.Type == TreeNodeType.Op)
            {
                if (pTreeNode.Uses.Count != cTreeNode.Uses.Count)
                {
                    return false;
                }

                for (int index = 0; index < pTreeNode.Uses.Count; index++)
                {
                    var pUse = pTreeNode.Uses[index];
                    var cUse = cTreeNode.Uses[index];

                    if (pUse.Index <= -2)
                    {
                        if (externalRegs[-pUse.Index - 2] != (-cUse.Index - 2))
                        {
                            return false;
                        }
                    }
                    else if (pUse.Index != cUse.Index)
                    {
                        return false;
                    }

                    if (pUse.Inverted != cUse.Inverted || (pUse.Node == null) != (cUse.Node == null))
                    {
                        return false;
                    }

                    if (pUse.Node != null && !Matches(pUse.Node, cUse.Node, externalRegs))
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        private static class PatternTrees
        {
            public static IPatternTreeNode[] GetFsiGetAddress()
            {
                var affinityValue = S2r(SReg.Affinity).Use(PT).Out;
                var orderingTicketValue = S2r(SReg.OrderingTicket).Use(PT).Out;

                return new IPatternTreeNode[]
                {
                    Iscadd(cc: true, 2, 0, 404)
                        .Use(PT)
                        .Use(Iscadd(cc: false, 8)
                            .Use(PT)
                            .Use(Lop32i(LogicOp.And, 0xff)
                                .Use(PT)
                                .Use(affinityValue).Out)
                            .Use(Lop32i(LogicOp.And, 0xff)
                                .Use(PT)
                                .Use(orderingTicketValue).Out).Out),
                    ShrU32W(16)
                        .Use(PT)
                        .Use(orderingTicketValue),
                    Iadd32i(0x200)
                        .Use(PT)
                        .Use(Lop32i(LogicOp.And, 0xfe00)
                            .Use(PT)
                            .Use(orderingTicketValue).Out),
                    Iadd(x: true, 0, 405).Use(PT).Use(RZ),
                    Ret().Use(PT)
                };
            }

            public static IPatternTreeNode[] GetFsiGetAddressV2()
            {
                var affinityValue = S2r(SReg.Affinity).Use(PT).Out;
                var orderingTicketValue = S2r(SReg.OrderingTicket).Use(PT).Out;

                return new IPatternTreeNode[]
                {
                    ShrU32W(16)
                        .Use(PT)
                        .Use(orderingTicketValue),
                    Iadd32i(0x200)
                        .Use(PT)
                        .Use(Lop32i(LogicOp.And, 0xfe00)
                            .Use(PT)
                            .Use(orderingTicketValue).Out),
                    Iscadd(cc: true, 2, 0, 404)
                        .Use(PT)
                        .Use(Bfi(0x808)
                            .Use(PT)
                            .Use(affinityValue)
                            .Use(Lop32i(LogicOp.And, 0xff)
                                .Use(PT)
                                .Use(orderingTicketValue).Out).Out),
                    Iadd(x: true, 0, 405).Use(PT).Use(RZ),
                    Ret().Use(PT)
                };
            }

            public static IPatternTreeNode[] GetFsiIsLastWarpThread()
            {
                var threadKillValue = S2r(SReg.ThreadKill).Use(PT).Out;
                var laneIdValue = S2r(SReg.LaneId).Use(PT).Out;

                return new IPatternTreeNode[]
                {
                    IsetpU32(IComp.Eq)
                        .Use(PT)
                        .Use(PT)
                        .Use(FloU32()
                            .Use(PT)
                            .Use(Vote(VoteMode.Any)
                                .Use(PT)
                                .Use(IsetpU32(IComp.Ne)
                                    .Use(PT)
                                    .Use(PT)
                                    .Use(Lop(negB: true, LogicOp.PassB)
                                        .Use(PT)
                                        .Use(RZ)
                                        .Use(threadKillValue).OutAt(1))
                                    .Use(RZ).Out).OutAt(1)).Out)
                        .Use(laneIdValue),
                    Ret().Use(PT)
                };
            }

            public static IPatternTreeNode[] GetFsiBeginPattern()
            {
                var addressLowValue = CallArg(1);

                static PatternTreeNodeUse HighU16Equals(PatternTreeNodeUse x)
                {
                    var expectedValue = CallArg(3);

                    return IsetpU32(IComp.Eq)
                        .Use(PT)
                        .Use(PT)
                        .Use(ShrU32W(16).Use(PT).Use(x).Out)
                        .Use(expectedValue).Out;
                }

                PatternTreeNode<byte> label;

                return new IPatternTreeNode[]
                {
                    Cal(),
                    Ret().Use(CallArg(0).Inv),
                    Ret()
                        .Use(HighU16Equals(LdgE(CacheOpLd.Cg, LsSize.B32)
                            .Use(PT)
                            .Use(addressLowValue).Out)),
                    label = Label(),
                    Bra()
                        .Use(HighU16Equals(LdgE(CacheOpLd.Cg, LsSize.B32, 1)
                            .Use(PT)
                            .Use(addressLowValue).Out).Inv)
                        .Use(label.Out),
                    Ret().Use(PT)
                };
            }

            public static IPatternTreeNode[] GetFsiEndPattern()
            {
                var voteResult = Vote(VoteMode.All).Use(PT).Use(PT).OutAt(1);
                var popcResult = Popc().Use(PT).Use(voteResult).Out;
                var threadKillValue = S2r(SReg.ThreadKill).Use(PT).Out;
                var laneIdValue = S2r(SReg.LaneId).Use(PT).Out;

                var addressLowValue = CallArg(1);
                var incrementValue = CallArg(2);

                return new IPatternTreeNode[]
                {
                    Cal(),
                    Ret().Use(CallArg(0).Inv),
                    Membar(Decoders.Membar.Vc).Use(PT),
                    Ret().Use(IsetpU32(IComp.Ne)
                        .Use(PT)
                        .Use(PT)
                        .Use(threadKillValue)
                        .Use(RZ).Out),
                    RedE(RedOp.Add, AtomSize.U32)
                        .Use(IsetpU32(IComp.Eq)
                            .Use(PT)
                            .Use(PT)
                            .Use(FloU32()
                                .Use(PT)
                                .Use(voteResult).Out)
                            .Use(laneIdValue).Out)
                        .Use(addressLowValue)
                        .Use(Xmad(XmadCop.Cbcc, psl: true, hiloA: true, hiloB: true)
                            .Use(PT)
                            .Use(incrementValue)
                            .Use(Xmad(XmadCop.Cfull, mrg: true, hiloB: true)
                                .Use(PT)
                                .Use(incrementValue)
                                .Use(popcResult)
                                .Use(RZ).Out)
                            .Use(Xmad(XmadCop.Cfull)
                                .Use(PT)
                                .Use(incrementValue)
                                .Use(popcResult)
                                .Use(RZ).Out).Out),
                    Ret().Use(PT)
                };
            }

            private static PatternTreeNode<InstBfiI> Bfi(int imm)
            {
                return new(InstName.Bfi, (op) => !op.WriteCC && op.Imm20 == imm, isImm: true);
            }

            private static PatternTreeNode<InstBra> Bra()
            {
                return new(InstName.Bra, (op) => op.Ccc == Ccc.T && !op.Ca);
            }

            private static PatternTreeNode<InstCal> Cal()
            {
                return new(InstName.Cal, (op) => !op.Ca && op.Inc);
            }

            private static PatternTreeNode<InstFloR> FloU32()
            {
                return new(InstName.Flo, (op) => !op.Signed && !op.Sh && !op.NegB && !op.WriteCC);
            }

            private static PatternTreeNode<InstIaddC> Iadd(bool x, int cbufSlot, int cbufOffset)
            {
                return new(InstName.Iadd, (op) =>
                    !op.Sat &&
                    !op.WriteCC &&
                    op.X == x &&
                    op.AvgMode == AvgMode.NoNeg &&
                    op.CbufSlot == cbufSlot &&
                    op.CbufOffset == cbufOffset);
            }

            private static PatternTreeNode<InstIadd32i> Iadd32i(int imm)
            {
                return new(InstName.Iadd32i, (op) => !op.Sat && !op.WriteCC && !op.X && op.AvgMode == AvgMode.NoNeg && op.Imm32 == imm);
            }

            private static PatternTreeNode<InstIscaddR> Iscadd(bool cc, int imm)
            {
                return new(InstName.Iscadd, (op) => op.WriteCC == cc && op.AvgMode == AvgMode.NoNeg && op.Imm5 == imm);
            }

            private static PatternTreeNode<InstIscaddC> Iscadd(bool cc, int imm, int cbufSlot, int cbufOffset)
            {
                return new(InstName.Iscadd, (op) =>
                    op.WriteCC == cc &&
                    op.AvgMode == AvgMode.NoNeg &&
                    op.Imm5 == imm &&
                    op.CbufSlot == cbufSlot &&
                    op.CbufOffset == cbufOffset);
            }

            private static PatternTreeNode<InstIsetpR> IsetpU32(IComp comp)
            {
                return new(InstName.Isetp, (op) => !op.Signed && op.IComp == comp && op.Bop == BoolOp.And);
            }

            private static PatternTreeNode<byte> Label()
            {
                return new(InstName.Invalid, (op) => true, type: TreeNodeType.Label);
            }

            private static PatternTreeNode<InstLopR> Lop(bool negB, LogicOp logicOp)
            {
                return new(InstName.Lop, (op) => !op.NegA && op.NegB == negB && !op.WriteCC && !op.X && op.Lop == logicOp && op.PredicateOp == PredicateOp.F);
            }

            private static PatternTreeNode<InstLop32i> Lop32i(LogicOp logicOp, int imm)
            {
                return new(InstName.Lop32i, (op) => !op.NegA && !op.NegB && !op.X && !op.WriteCC && op.LogicOp == logicOp && op.Imm32 == imm);
            }

            private static PatternTreeNode<InstMembar> Membar(Membar membar)
            {
                return new(InstName.Membar, (op) => op.Membar == membar);
            }

            private static PatternTreeNode<InstPopcR> Popc()
            {
                return new(InstName.Popc, (op) => !op.NegB);
            }

            private static PatternTreeNode<InstRet> Ret()
            {
                return new(InstName.Ret, (op) => op.Ccc == Ccc.T);
            }

            private static PatternTreeNode<InstS2r> S2r(SReg reg)
            {
                return new(InstName.S2r, (op) => op.SReg == reg);
            }

            private static PatternTreeNode<InstShrI> ShrU32W(int imm)
            {
                return new(InstName.Shr, (op) => !op.Signed && !op.Brev && op.M && op.XMode == 0 && op.Imm20 == imm, isImm: true);
            }

            private static PatternTreeNode<InstLdg> LdgE(CacheOpLd cacheOp, LsSize size, byte order = 0)
            {
                return new(InstName.Ldg, (op) => op.E && op.CacheOp == cacheOp && op.LsSize == size, order: order);
            }

            private static PatternTreeNode<InstRed> RedE(RedOp redOp, AtomSize size, byte order = 0)
            {
                return new(InstName.Red, (op) => op.E && op.RedOp == redOp && op.RedSize == size, order: order);
            }

            private static PatternTreeNode<InstVote> Vote(VoteMode mode)
            {
                return new(InstName.Vote, (op) => op.VoteMode == mode);
            }

            private static PatternTreeNode<InstXmadR> Xmad(XmadCop cop, bool psl = false, bool mrg = false, bool hiloA = false, bool hiloB = false)
            {
                return new(InstName.Xmad, (op) => op.XmadCop == cop && op.Psl == psl && op.Mrg == mrg && op.HiloA == hiloA && op.HiloB == hiloB);
            }

            private static PatternTreeNodeUse PT => PTOrRZ();
            private static PatternTreeNodeUse RZ => PTOrRZ();
            private static PatternTreeNodeUse Undef => new PatternTreeNodeUse(0, null);

            private static PatternTreeNodeUse CallArg(int index)
            {
                return new PatternTreeNodeUse(-(index + 2), null);
            }

            private static PatternTreeNodeUse PTOrRZ()
            {
                return new PatternTreeNodeUse(-1, null);
            }
        }

        private static void PrintTreeNode(TreeNode node, string indentation)
        {
            Console.WriteLine($" {node.Op.Name}");

            for (int i = 0; i < node.Uses.Count; i++)
            {
                TreeNodeUse use = node.Uses[i];
                bool last = i == node.Uses.Count - 1;
                char separator = last ? '`' : '|';

                if (use.Node != null)
                {
                    Console.Write($"{indentation} {separator}- ({(use.Inverted ? "INV " : "")}{use.Index})");
                    PrintTreeNode(use.Node, indentation + (last ? "       " : " |     "));
                }
                else
                {
                    Console.WriteLine($"{indentation} {separator}- ({(use.Inverted ? "INV " : "")}{use.Index}) NULL");
                }
            }
        }

        private static void PrintTreeNode(IPatternTreeNode node, string indentation)
        {
            Console.WriteLine($" {node.Name}");

            for (int i = 0; i < node.Uses.Count; i++)
            {
                PatternTreeNodeUse use = node.Uses[i];
                bool last = i == node.Uses.Count - 1;
                char separator = last ? '`' : '|';

                if (use.Node != null)
                {
                    Console.Write($"{indentation} {separator}- ({(use.Inverted ? "INV " : "")}{use.Index})");
                    PrintTreeNode(use.Node, indentation + (last ? "       " : " |     "));
                }
                else
                {
                    Console.WriteLine($"{indentation} {separator}- ({(use.Inverted ? "INV " : "")}{use.Index}) NULL");
                }
            }
        }
    }
}