aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/Graph/DataFlow.cs
blob: e5b369d66c22630efc880cefaf503ff171c5cd74 (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
namespace Ryujinx.Cpu.LightningJit.Graph
{
    static class DataFlow
    {
        public static (RegisterMask[], RegisterMask[]) GetGlobalUses(IBlockList blocks)
        {
            // Compute local register inputs and outputs used inside blocks.
            RegisterMask[] localInputs = new RegisterMask[blocks.Count];
            RegisterMask[] localOutputs = new RegisterMask[blocks.Count];

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

                RegisterUse use = block.ComputeUseMasks();

                localInputs[block.Index] = use.Read;
                localOutputs[block.Index] = use.Write;
            }

            // Compute global register inputs and outputs used across blocks.
            RegisterMask[] globalInputs = new RegisterMask[blocks.Count];
            RegisterMask[] globalOutputs = new RegisterMask[blocks.Count];

            bool modified;

            // Compute register outputs.
            do
            {
                modified = false;

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

                    int firstPIndex = GetFirstPredecessorIndex(block);
                    if (firstPIndex >= 0)
                    {
                        IBlock predecessor = block.GetPredecessor(firstPIndex);

                        RegisterMask outputs = globalOutputs[predecessor.Index];

                        for (int pIndex = firstPIndex + 1; pIndex < block.PredecessorsCount; pIndex++)
                        {
                            predecessor = block.GetPredecessor(pIndex);

                            if (predecessor.EndsWithContextStore())
                            {
                                // If a block ended with a context store, then we don't need to care
                                // about any of it's outputs, as they have already been saved to the context.
                                // Common outputs must be reset as doing a context stores indicates we will
                                // do a function call and wipe all register values.

                                continue;
                            }

                            outputs |= globalOutputs[predecessor.Index];
                        }

                        outputs |= localOutputs[block.Index];
                        modified |= Exchange(globalOutputs, block.Index, globalOutputs[block.Index] | outputs);
                    }
                    else
                    {
                        modified |= Exchange(globalOutputs, block.Index, localOutputs[block.Index]);
                    }
                }
            }
            while (modified);

            // Compute register inputs.
            do
            {
                modified = false;

                for (int index = blocks.Count - 1; index >= 0; index--)
                {
                    IBlock block = blocks[index];

                    RegisterMask cmnOutputs = RegisterMask.Zero;
                    RegisterMask allOutputs = RegisterMask.Zero;

                    int firstPIndex = GetFirstPredecessorIndex(block);
                    if (firstPIndex == 0)
                    {
                        IBlock predecessor = block.GetPredecessor(0);

                        // Assumes that block index 0 is the entry block.
                        cmnOutputs = block.Index != 0 ? globalOutputs[predecessor.Index] : RegisterMask.Zero;
                        allOutputs = globalOutputs[predecessor.Index];

                        for (int pIndex = 1; pIndex < block.PredecessorsCount; pIndex++)
                        {
                            predecessor = block.GetPredecessor(pIndex);

                            if (!predecessor.EndsWithContextStore())
                            {
                                RegisterMask outputs = globalOutputs[predecessor.Index];

                                cmnOutputs &= outputs;
                                allOutputs |= outputs;
                            }
                            else
                            {
                                cmnOutputs = RegisterMask.Zero;
                            }
                        }
                    }
                    else if (firstPIndex > 0)
                    {
                        IBlock predecessor = block.GetPredecessor(firstPIndex);

                        allOutputs = globalOutputs[predecessor.Index];

                        for (int pIndex = firstPIndex + 1; pIndex < block.PredecessorsCount; pIndex++)
                        {
                            predecessor = block.GetPredecessor(pIndex);

                            if (!predecessor.EndsWithContextStore())
                            {
                                allOutputs |= globalOutputs[predecessor.Index];
                            }
                        }
                    }

                    RegisterMask inputs = localInputs[block.Index];

                    // If this block will load from context at the end,
                    // we don't need to care about what comes next.
                    if (!block.EndsWithContextLoad())
                    {
                        for (int sIndex = 0; sIndex < block.SuccessorsCount; sIndex++)
                        {
                            inputs |= globalInputs[block.GetSuccessor(sIndex).Index] & ~localOutputs[block.Index];
                        }
                    }

                    inputs |= allOutputs & ~localOutputs[block.Index];
                    inputs &= ~cmnOutputs;

                    modified |= Exchange(globalInputs, block.Index, globalInputs[block.Index] | inputs);
                }
            }
            while (modified);

            return (globalInputs, globalOutputs);
        }

        private static bool Exchange(RegisterMask[] masks, int blkIndex, RegisterMask value)
        {
            ref RegisterMask curValue = ref masks[blkIndex];
            bool changed = curValue != value;
            curValue = value;

            return changed;
        }

        private static int GetFirstPredecessorIndex(IBlock block)
        {
            for (int pIndex = 0; pIndex < block.PredecessorsCount; pIndex++)
            {
                if (!block.GetPredecessor(pIndex).EndsWithContextStore())
                {
                    return pIndex;
                }
            }

            return -1;
        }
    }
}