aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/Arm64/Block.cs
blob: 188630a1c65afd918b0d13d0aae75033dc806c35 (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
using Ryujinx.Cpu.LightningJit.Graph;
using System.Collections.Generic;
using System.Diagnostics;

namespace Ryujinx.Cpu.LightningJit.Arm64
{
    class Block : IBlock
    {
        public int Index { get; private set; }

        private readonly List<Block> _predecessors;
        private readonly List<Block> _successors;

        public int PredecessorsCount => _predecessors.Count;
        public int SuccessorsCount => _successors.Count;

        public readonly ulong Address;
        public readonly ulong EndAddress;
        public readonly List<InstInfo> Instructions;
        public readonly bool EndsWithBranch;
        public readonly bool IsTruncated;
        public readonly bool IsLoopEnd;

        public Block(ulong address, ulong endAddress, List<InstInfo> instructions, bool endsWithBranch, bool isTruncated, bool isLoopEnd)
        {
            Debug.Assert((int)((endAddress - address) / 4) == instructions.Count);

            _predecessors = new();
            _successors = new();
            Address = address;
            EndAddress = endAddress;
            Instructions = instructions;
            EndsWithBranch = endsWithBranch;
            IsTruncated = isTruncated;
            IsLoopEnd = isLoopEnd;
        }

        public (Block, Block) SplitAtAddress(ulong address)
        {
            int splitIndex = (int)((address - Address) / 4);
            int splitCount = Instructions.Count - splitIndex;

            // Technically those are valid, but we don't want to create empty blocks.
            Debug.Assert(splitIndex != 0);
            Debug.Assert(splitCount != 0);

            Block leftBlock = new(
                Address,
                address,
                Instructions.GetRange(0, splitIndex),
                false,
                false,
                false);

            Block rightBlock = new(
                address,
                EndAddress,
                Instructions.GetRange(splitIndex, splitCount),
                EndsWithBranch,
                IsTruncated,
                IsLoopEnd);

            return (leftBlock, rightBlock);
        }

        public void Number(int index)
        {
            Index = index;
        }

        public void AddSuccessor(Block block)
        {
            if (!_successors.Contains(block))
            {
                _successors.Add(block);
            }
        }

        public void AddPredecessor(Block block)
        {
            if (!_predecessors.Contains(block))
            {
                _predecessors.Add(block);
            }
        }

        public IBlock GetSuccessor(int index)
        {
            return _successors[index];
        }

        public IBlock GetPredecessor(int index)
        {
            return _predecessors[index];
        }

        public RegisterUse ComputeUseMasks()
        {
            if (Instructions.Count == 0)
            {
                return new(0u, 0u, 0u, 0u, 0u, 0u);
            }

            RegisterUse use = Instructions[0].RegisterUse;

            for (int index = 1; index < Instructions.Count; index++)
            {
                RegisterUse currentUse = Instructions[index].RegisterUse;

                use = new(use.Read | (currentUse.Read & ~use.Write), use.Write | currentUse.Write);
            }

            return use;
        }

        public bool EndsWithContextLoad()
        {
            return !IsTruncated && EndsWithContextStoreAndLoad();
        }

        public bool EndsWithContextStore()
        {
            return EndsWithContextStoreAndLoad();
        }

        private bool EndsWithContextStoreAndLoad()
        {
            if (Instructions.Count == 0)
            {
                return false;
            }

            InstName lastInstructionName = Instructions[^1].Name;

            return lastInstructionName.IsCall() || lastInstructionName.IsException();
        }
    }
}