forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.cpp
More file actions
264 lines (234 loc) · 8.59 KB
/
Analysis.cpp
File metadata and controls
264 lines (234 loc) · 8.59 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/IR/Analysis.h"
#include "hermes/IR/CFG.h"
#include "hermes/IR/IR.h"
#include "hermes/Utils/Dumper.h"
#include "llvh/ADT/PriorityQueue.h"
#include "llvh/Support/Debug.h"
#include <utility>
#ifdef DEBUG_TYPE
#undef DEBUG_TYPE
#endif
#define DEBUG_TYPE "IR Analysis"
using namespace hermes;
using llvh::dbgs;
using llvh::isa;
using llvh::Optional;
using llvh::outs;
void PostOrderAnalysis::visitPostOrder(BasicBlock *BB, BlockList &order) {
struct State {
BasicBlock *BB;
succ_iterator cur, end;
explicit State(BasicBlock *BB)
: BB(BB), cur(succ_begin(BB)), end(succ_end(BB)) {}
};
llvh::SmallPtrSet<BasicBlock *, 16> visited{};
llvh::SmallVector<State, 32> stack{};
stack.emplace_back(BB);
do {
while (stack.back().cur != stack.back().end) {
BB = *stack.back().cur++;
if (visited.insert(BB).second)
stack.emplace_back(BB);
}
order.push_back(stack.back().BB);
stack.pop_back();
} while (!stack.empty());
}
PostOrderAnalysis::PostOrderAnalysis(Function *F) : ctx_(F->getContext()) {
assert(Order.empty() && "vector must be empty");
BasicBlock *entry = &*F->begin();
// Finally, do an PO scan from the entry block.
visitPostOrder(entry, Order);
assert(
!Order.empty() && Order[Order.size() - 1] == entry &&
"Entry block must be the last element in the vector");
}
void PostOrderAnalysis::dump() {
IRPrinter D(ctx_, outs());
D.visit(*Order[0]->getParent());
outs() << "Blocks: ";
for (auto &BB : Order) {
outs() << "BB" << D.BBNamer.getNumber(BB) << " ";
}
outs() << "\n";
}
// Perform depth-first search to identify loops. The loop header of a block B is
// its DFS ancestor H such that a descendent of B has a back edge to H. (In the
// case of a self-loop, the ancestor and descendant are B itself.) When there
// are multiple such blocks, we take the one with the maximum DFS discovery time
// to get the innermost loop. The preheader is the DFS parent of H. We use
// DominanceInfo to ensure that preheaders dominate headers and headers dominate
// all blocks in the loop.
LoopAnalysis::LoopAnalysis(Function *F, const DominanceInfo &dominanceInfo) {
// BlockMap (defined in Analysis.h) and BlockSet are used for most cases.
// TinyBlockSet is only used for headerSets (defined below); it has a smaller
// inline size because we store BLOCK -> {SET OF HEADERS} for every block, and
// very deeply nested loops (leading to many headers) are not that common.
using BlockSet = llvh::SmallPtrSet<const BasicBlock *, 16>;
using TinyBlockSet = llvh::SmallPtrSet<BasicBlock *, 2>;
int dfsTime = 0;
// Maps each block to its DFS discovery time (value of dfsTime).
BlockMap<int> discovered;
// Set of blocks we have finished visiting.
BlockSet finished;
// Maps each block to its parent in the DFS tree.
BlockMap<BasicBlock *> parent;
// Maps each block to a set of header blocks of loops that enclose it.
BlockMap<TinyBlockSet> headerSets;
// Explicit stack for depth-first search.
llvh::SmallVector<BasicBlock *, 16> stack;
stack.push_back(&*F->begin());
while (stack.size()) {
BasicBlock *BB = stack.back();
// If it's the first time visiting, record the discovery time and push all
// undiscovered successors onto the stack. Leave BB on the stack so that
// after visiting all descendants, we come back to it and resume below.
if (discovered.try_emplace(BB, dfsTime).second) {
++dfsTime;
for (auto it = succ_begin(BB), e = succ_end(BB); it != e; ++it) {
BasicBlock *succ = *it;
if (!discovered.count(succ)) {
stack.push_back(succ);
parent[succ] = BB;
}
}
continue;
}
stack.pop_back();
if (finished.count(BB)) {
// BB was duplicated on the stack and we already finished visiting it.
continue;
}
// Check back/forward/cross edges to find loops BB is in.
TinyBlockSet headers;
for (auto it = succ_begin(BB), e = succ_end(BB); it != e; ++it) {
BasicBlock *succ = *it;
assert(discovered.count(succ) && "Unexpected undiscovered successor");
if (!finished.count(succ)) {
// Found a back edge to a header block.
headers.insert(succ);
} else {
// Either a forward edge or cross edge. Headers of succ are also headers
// of BB if we haven't finished visiting them.
auto entry = headerSets.find(succ);
if (entry != headerSets.end()) {
for (BasicBlock *headerOfSucc : entry->second) {
if (!finished.count(headerOfSucc)) {
headers.insert(headerOfSucc);
}
}
}
}
}
if (!headers.empty()) {
auto insert = headerSets.try_emplace(BB, std::move(headers));
(void)insert;
assert(insert.second && "Inserting headers for same block twice!");
}
finished.insert(BB);
}
// Determine which headers are good/bad and populate headerToPreheader_ using
// the parent mapping. A header is good if it dominates every block in the
// loop (that is, it is the only entry point).
BlockSet badHeaders;
for (auto &entry : headerSets) {
const BasicBlock *BB = entry.first;
for (const BasicBlock *header : entry.second) {
if (badHeaders.count(header)) {
continue;
}
if (!dominanceInfo.dominates(header, BB)) {
badHeaders.insert(header);
} else if (!headerToPreheader_.count(header)) {
BasicBlock *preheader = parent[header];
if (dominanceInfo.properlyDominates(preheader, header)) {
headerToPreheader_[header] = preheader;
}
}
}
}
// Populate blockToHeader_ with the innermost loop header for each block.
for (auto &entry : headerSets) {
const BasicBlock *BB = entry.first;
TinyBlockSet &headers = entry.second;
if (!headers.empty()) {
BasicBlock *innerHeader = nullptr;
int maxDiscovery = -1;
for (BasicBlock *header : headers) {
int discovery = discovered[header];
if (discovery > maxDiscovery && !badHeaders.count(header)) {
maxDiscovery = discovery;
innerHeader = header;
}
}
blockToHeader_[BB] = innerHeader;
}
}
}
BasicBlock *LoopAnalysis::getLoopHeader(const BasicBlock *BB) const {
return blockToHeader_.lookup(BB);
}
BasicBlock *LoopAnalysis::getLoopPreheader(const BasicBlock *BB) const {
BasicBlock *header = getLoopHeader(BB);
if (header) {
return headerToPreheader_.lookup(header);
}
return nullptr;
}
FunctionScopeAnalysis::ScopeData
FunctionScopeAnalysis::calculateFunctionScopeData(Function *F) {
if (lexicalScopeMap_.find(F) == lexicalScopeMap_.end()) {
// If the function is a CommonJS module,
// then it won't have a CreateFunctionInst, so calculate the depth manually.
Module *module = F->getParent();
if (module->findCJSModule(F)) {
return ScopeData{module->getTopLevelFunction(), 1, false};
}
// To find the scope data of a function, we try to locate the
// CreateFunctionInst that creates this function. The scope of F
// will be 1 more than the scope depth of the CreateFunctionInst.
const CreateFunctionInst *Inst = nullptr;
for (auto *user : F->getUsers()) {
if (llvh::isa<CreateFunctionInst>(user)) {
assert(Inst == nullptr && "Function has multiple CreateFunctionInst");
Inst = llvh::dyn_cast<CreateFunctionInst>(user);
}
}
// Because the calculation is done lazily, any function requested
// must have a CreateFunctionInst.
if (Inst == nullptr) {
LLVM_DEBUG(
dbgs() << "Function \"" << F->getInternalName()
<< "\" has no CreateFunctionInst\n");
lexicalScopeMap_[F] = ScopeData::orphan();
return lexicalScopeMap_[F];
}
Function *Parent = Inst->getParent()->getParent();
ScopeData parentData = calculateFunctionScopeData(Parent);
if (!parentData.orphaned) {
lexicalScopeMap_[F] = ScopeData(Parent, parentData.depth + 1);
} else {
lexicalScopeMap_[F] = ScopeData::orphan();
}
}
return lexicalScopeMap_[F];
}
Optional<int32_t> FunctionScopeAnalysis::getScopeDepth(VariableScope *VS) {
if (ExternalScope *ES = llvh::dyn_cast<ExternalScope>(VS)) {
return ES->getDepth();
} else {
ScopeData sd = calculateFunctionScopeData(VS->getFunction());
if (sd.orphaned)
return llvh::None;
return sd.depth;
}
}
Function *FunctionScopeAnalysis::getLexicalParent(Function *F) {
return calculateFunctionScopeData(F).parent;
}