Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/include/Luau/IrAnalysis.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/CodeGenCommon.h"
5
6
#include <bitset>
7
#include <queue>
8
#include <utility>
9
#include <vector>
10
11
#include <stdint.h>
12
13
namespace Luau
14
{
15
namespace CodeGen
16
{
17
18
struct IrBlock;
19
struct IrFunction;
20
21
void updateUseCounts(IrFunction& function);
22
23
void updateLastUseLocations(IrFunction& function, const std::vector<uint32_t>& sortedBlocks);
24
25
uint32_t getNextInstUse(IrFunction& function, uint32_t targetInstIdx, uint32_t startInstIdx);
26
27
// Returns how many values are coming into the block (live in) and how many are coming out of the block (live out)
28
std::pair<uint32_t, uint32_t> getLiveInOutValueCount(IrFunction& function, IrBlock& start, bool visitChain);
29
uint32_t getLiveInValueCount(IrFunction& function, IrBlock& block);
30
uint32_t getLiveOutValueCount(IrFunction& function, IrBlock& block);
31
32
struct RegisterSet
33
{
34
std::bitset<256> regs;
35
36
// If variadic sequence is active, we track register from which it starts
37
bool varargSeq = false;
38
uint8_t varargStart = 0;
39
};
40
41
void requireVariadicSequence(RegisterSet& sourceRs, const RegisterSet& defRs, uint8_t varargStart);
42
43
struct BlockOrdering
44
{
45
uint32_t depth = 0;
46
47
uint32_t preOrder = ~0u;
48
uint32_t postOrder = ~0u;
49
50
bool visited = false;
51
};
52
53
struct CfgInfo
54
{
55
std::vector<uint32_t> predecessors;
56
std::vector<uint32_t> predecessorsOffsets;
57
58
std::vector<uint32_t> successors;
59
std::vector<uint32_t> successorsOffsets;
60
61
// Immediate dominators (unique parent in the dominator tree)
62
std::vector<uint32_t> idoms;
63
64
// Children in the dominator tree
65
std::vector<uint32_t> domChildren;
66
std::vector<uint32_t> domChildrenOffsets;
67
68
std::vector<BlockOrdering> domOrdering;
69
70
// VM registers that are live when the block is entered
71
// Additionally, an active variadic sequence can exist at the entry of the block
72
std::vector<RegisterSet> in;
73
74
// VM registers that are defined inside the block
75
// It can also contain a variadic sequence definition if that hasn't been consumed inside the block
76
// Note that this means that checking 'def' set might not be enough to say that register has not been written to
77
std::vector<RegisterSet> def;
78
79
// VM registers that are coming out from the block
80
// These might be registers that are defined inside the block or have been defined at the entry of the block
81
// Additionally, an active variadic sequence can exist at the exit of the block
82
std::vector<RegisterSet> out;
83
84
// VM registers captured by nested closures
85
// This set can never have an active variadic sequence
86
RegisterSet captured;
87
88
// VM registers defined in any block
89
// Lowest variadic sequence start is stored to mark variadic writes
90
RegisterSet written;
91
};
92
93
// Calculate lists of block predecessors and successors
94
void computeCfgBlockEdges(IrFunction& function);
95
96
// A quick refresher on dominance and dominator trees:
97
// * If A is a dominator of B (A dom B), you can never execute B without executing A first
98
// * A is a strict dominator of B (A sdom B) is similar to previous one but A != B
99
// * Immediate dominator node N (idom N) is a unique node T so that T sdom N,
100
// but T does not strictly dominate any other node that dominates N.
101
// * Dominance frontier is a set of nodes where dominance of a node X ends.
102
// In practice this is where values established by node X might no longer hold because of join edges from other nodes coming in.
103
// This is also where PHI instructions in SSA are placed.
104
void computeCfgImmediateDominators(IrFunction& function);
105
void computeCfgDominanceTreeChildren(IrFunction& function);
106
107
struct IdfContext
108
{
109
struct BlockAndOrdering
110
{
111
uint32_t blockIdx;
112
BlockOrdering ordering;
113
114
bool operator<(const BlockAndOrdering& rhs) const
115
{
116
if (ordering.depth != rhs.ordering.depth)
117
return ordering.depth < rhs.ordering.depth;
118
119
return ordering.preOrder < rhs.ordering.preOrder;
120
}
121
};
122
123
// Using priority queue to work on nodes in the order from the bottom of the dominator tree to the top
124
// If the depth of keys is equal, DFS order is used to provide strong ordering
125
std::priority_queue<BlockAndOrdering> queue;
126
std::vector<uint32_t> worklist;
127
128
struct IdfVisitMarks
129
{
130
bool seenInQueue = false;
131
bool seenInWorklist = false;
132
};
133
134
std::vector<IdfVisitMarks> visits;
135
136
std::vector<uint32_t> idf;
137
};
138
139
// Compute iterated dominance frontier (IDF or DF+) for a variable, given the set of blocks where that variable is defined
140
// Providing a set of blocks where the variable is a live-in at the entry helps produce a pruned SSA form (inserted phi nodes will not be dead)
141
//
142
// 'Iterated' comes from the definition where we recompute the IDFn+1 = DF(S) while adding IDFn to S until a fixed point is reached
143
// Iterated dominance frontier has been shown to be equal to the set of nodes where phi instructions have to be inserted
144
void computeIteratedDominanceFrontierForDefs(
145
IdfContext& ctx,
146
const IrFunction& function,
147
const std::vector<uint32_t>& defBlocks,
148
const std::vector<uint32_t>& liveInBlocks
149
);
150
151
// Function used to update all CFG data
152
void computeCfgInfo(IrFunction& function);
153
154
struct BlockIteratorWrapper
155
{
156
const uint32_t* itBegin = nullptr;
157
const uint32_t* itEnd = nullptr;
158
159
bool empty() const
160
{
161
return itBegin == itEnd;
162
}
163
164
size_t size() const
165
{
166
return size_t(itEnd - itBegin);
167
}
168
169
const uint32_t* begin() const
170
{
171
return itBegin;
172
}
173
174
const uint32_t* end() const
175
{
176
return itEnd;
177
}
178
179
uint32_t operator[](size_t pos) const
180
{
181
CODEGEN_ASSERT(pos < size_t(itEnd - itBegin));
182
return itBegin[pos];
183
}
184
};
185
186
BlockIteratorWrapper predecessors(const CfgInfo& cfg, uint32_t blockIdx);
187
BlockIteratorWrapper successors(const CfgInfo& cfg, uint32_t blockIdx);
188
BlockIteratorWrapper domChildren(const CfgInfo& cfg, uint32_t blockIdx);
189
190
} // namespace CodeGen
191
} // namespace Luau
192
193