aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/IntermediateRepresentation/BasicBlock.cs
blob: 2aca118b741cdb8af65d254906bfb8bb0ac4fdb8 (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
using System.Collections.Generic;

namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
{
    class BasicBlock
    {
        public int Index { get; set; }

        public LinkedList<INode> Operations { get; }

        private BasicBlock _next;
        private BasicBlock _branch;

        public BasicBlock Next
        {
            get => _next;
            set => _next = AddSuccessor(_next, value);
        }

        public BasicBlock Branch
        {
            get => _branch;
            set => _branch = AddSuccessor(_branch, value);
        }

        public bool HasBranch => _branch != null;
        public bool Reachable => Index == 0 || Predecessors.Count != 0;

        public List<BasicBlock> Predecessors { get; }

        public HashSet<BasicBlock> DominanceFrontiers { get; }

        public BasicBlock ImmediateDominator { get; set; }

        public BasicBlock()
        {
            Operations = new LinkedList<INode>();

            Predecessors = new List<BasicBlock>();

            DominanceFrontiers = new HashSet<BasicBlock>();
        }

        public BasicBlock(int index) : this()
        {
            Index = index;
        }

        private BasicBlock AddSuccessor(BasicBlock oldBlock, BasicBlock newBlock)
        {
            oldBlock?.Predecessors.Remove(this);
            newBlock?.Predecessors.Add(this);

            return newBlock;
        }

        public INode GetLastOp()
        {
            return Operations.Last?.Value;
        }

        public void Append(INode node)
        {
            INode lastOp = GetLastOp();

            if (lastOp is Operation operation && IsControlFlowInst(operation.Inst))
            {
                Operations.AddBefore(Operations.Last, node);
            }
            else
            {
                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;
        }
    }
}