aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Shader/StructuredIr/PhiFunctions.cs
blob: 53391b6268dde7a39c93d3e49f1b2c3a133e6a4d (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
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System.Collections.Generic;

namespace Ryujinx.Graphics.Shader.StructuredIr
{
    static class PhiFunctions
    {
        public static void Remove(BasicBlock[] blocks)
        {
            for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
            {
                BasicBlock block = blocks[blkIndex];

                LinkedListNode<INode> node = block.Operations.First;

                while (node != null)
                {
                    LinkedListNode<INode> nextNode = node.Next;

                    if (!(node.Value is PhiNode phi))
                    {
                        node = nextNode;

                        continue;
                    }

                    for (int index = 0; index < phi.SourcesCount; index++)
                    {
                        Operand src = phi.GetSource(index);

                        BasicBlock srcBlock = phi.GetBlock(index);

                        Operation copyOp = new Operation(Instruction.Copy, phi.Dest, src);

                        AddBeforeBranch(srcBlock, copyOp);
                    }

                    block.Operations.Remove(node);

                    node = nextNode;
                }
            }
        }

        private static void AddBeforeBranch(BasicBlock block, INode node)
        {
            INode lastOp = block.GetLastOp();

            if (lastOp is Operation operation && IsControlFlowInst(operation.Inst))
            {
                block.Operations.AddBefore(block.Operations.Last, node);
            }
            else
            {
                block.Operations.AddLast(node);
            }
        }

        private static bool IsControlFlowInst(Instruction inst)
        {
            switch (inst)
            {
                case Instruction.Branch:
                case Instruction.BranchIfFalse:
                case Instruction.BranchIfTrue:
                case Instruction.Discard:
                case Instruction.Return:
                    return true;
            }

            return false;
        }
    }
}