forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg-traversal.h
344 lines (303 loc) · 11.3 KB
/
cfg-traversal.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Convert the AST to a CFG, while traversing it.
//
// Note that this is not the same as the relooper CFG. The relooper is
// designed for compilation to an AST, this is for processing. There is
// no built-in support for transforming this CFG into the AST back
// again, it is just metadata on the side for computation purposes.
//
// Usage: As the traversal proceeds, you can note information and add it to
// the current basic block using currBasicBlock, on the contents
// property, whose type is user-defined.
//
#ifndef cfg_traversal_h
#define cfg_traversal_h
#include "wasm.h"
#include "wasm-traversal.h"
namespace wasm {
template<typename SubType, typename VisitorType, typename Contents>
struct CFGWalker : public ControlFlowWalker<SubType, VisitorType> {
// public interface
struct BasicBlock {
Contents contents; // custom contents
std::vector<BasicBlock*> out, in;
};
BasicBlock* entry; // the entry block
BasicBlock* makeBasicBlock() { // override this with code to create a BasicBlock if necessary
return new BasicBlock();
}
// internal details
std::vector<std::unique_ptr<BasicBlock>> basicBlocks; // all the blocks
std::vector<BasicBlock*> loopTops; // blocks that are the tops of loops, i.e., have backedges to them
// traversal state
BasicBlock* currBasicBlock; // the current block in play during traversal. can be nullptr if unreachable,
// but note that we don't do a deep unreachability analysis - just enough
// to avoid constructing obviously-unreachable blocks (we do a full reachability
// analysis on the CFG once it is constructed).
std::map<Expression*, std::vector<BasicBlock*>> branches; // a block or loop => its branches
std::vector<BasicBlock*> ifStack;
std::vector<BasicBlock*> loopStack;
void startBasicBlock() {
currBasicBlock = makeBasicBlock();
basicBlocks.push_back(std::unique_ptr<BasicBlock>(currBasicBlock));
}
void startUnreachableBlock() {
currBasicBlock = nullptr;
}
static void doStartUnreachableBlock(SubType* self, Expression** currp) {
self->startUnreachableBlock();
}
void link(BasicBlock* from, BasicBlock* to) {
if (!from || !to) return; // if one of them is not reachable, ignore
from->out.push_back(to);
to->in.push_back(from);
}
static void doEndBlock(SubType* self, Expression** currp) {
auto* curr = (*currp)->cast<Block>();
if (!curr->name.is()) return;
auto iter = self->branches.find(curr);
if (iter == self->branches.end()) return;
auto& origins = iter->second;
if (origins.size() == 0) return;
// we have branches to here, so we need a new block
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->link(last, self->currBasicBlock); // fallthrough
// branches to the new one
for (auto* origin : origins) {
self->link(origin, self->currBasicBlock);
}
self->branches.erase(curr);
}
static void doStartIfTrue(SubType* self, Expression** currp) {
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->link(last, self->currBasicBlock); // ifTrue
self->ifStack.push_back(last); // the block before the ifTrue
}
static void doStartIfFalse(SubType* self, Expression** currp) {
self->ifStack.push_back(self->currBasicBlock); // the ifTrue fallthrough
self->startBasicBlock();
self->link(self->ifStack[self->ifStack.size() - 2], self->currBasicBlock); // before if -> ifFalse
}
static void doEndIf(SubType* self, Expression** currp) {
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->link(last, self->currBasicBlock); // last one is ifFalse's fallthrough if there was one, otherwise it's the ifTrue fallthrough
if ((*currp)->cast<If>()->ifFalse) {
// we just linked ifFalse, need to link ifTrue to the end
self->link(self->ifStack.back(), self->currBasicBlock);
self->ifStack.pop_back();
} else {
// no ifFalse, so add a fallthrough for if the if is not taken
self->link(self->ifStack.back(), self->currBasicBlock);
}
self->ifStack.pop_back();
}
static void doStartLoop(SubType* self, Expression** currp) {
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->loopTops.push_back(self->currBasicBlock); // a loop with no backedges would still be counted here, but oh well
self->link(last, self->currBasicBlock);
self->loopStack.push_back(self->currBasicBlock);
}
static void doEndLoop(SubType* self, Expression** currp) {
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->link(last, self->currBasicBlock); // fallthrough
auto* curr = (*currp)->cast<Loop>();
// branches to the top of the loop
if (curr->name.is()) {
auto* loopStart = self->loopStack.back();
auto& origins = self->branches[curr];
for (auto* origin : origins) {
self->link(origin, loopStart);
}
self->branches.erase(curr);
}
self->loopStack.pop_back();
}
static void doEndBreak(SubType* self, Expression** currp) {
auto* curr = (*currp)->cast<Break>();
self->branches[self->findBreakTarget(curr->name)].push_back(self->currBasicBlock); // branch to the target
if (curr->condition) {
auto* last = self->currBasicBlock;
self->startBasicBlock();
self->link(last, self->currBasicBlock); // we might fall through
} else {
self->startUnreachableBlock();
}
}
static void doEndSwitch(SubType* self, Expression** currp) {
auto* curr = (*currp)->cast<Switch>();
std::set<Name> seen; // we might see the same label more than once; do not spam branches
for (Name target : curr->targets) {
if (!seen.count(target)) {
self->branches[self->findBreakTarget(target)].push_back(self->currBasicBlock); // branch to the target
seen.insert(target);
}
}
if (!seen.count(curr->default_)) {
self->branches[self->findBreakTarget(curr->default_)].push_back(self->currBasicBlock); // branch to the target
}
self->startUnreachableBlock();
}
static void scan(SubType* self, Expression** currp) {
Expression* curr = *currp;
switch (curr->_id) {
case Expression::Id::BlockId: {
self->pushTask(SubType::doEndBlock, currp);
break;
}
case Expression::Id::IfId: {
self->pushTask(SubType::doEndIf, currp);
auto* ifFalse = curr->cast<If>()->ifFalse;
if (ifFalse) {
self->pushTask(SubType::scan, &curr->cast<If>()->ifFalse);
self->pushTask(SubType::doStartIfFalse, currp);
}
self->pushTask(SubType::scan, &curr->cast<If>()->ifTrue);
self->pushTask(SubType::doStartIfTrue, currp);
self->pushTask(SubType::scan, &curr->cast<If>()->condition);
return; // don't do anything else
}
case Expression::Id::LoopId: {
self->pushTask(SubType::doEndLoop, currp);
break;
}
case Expression::Id::BreakId: {
self->pushTask(SubType::doEndBreak, currp);
break;
}
case Expression::Id::SwitchId: {
self->pushTask(SubType::doEndSwitch, currp);
break;
}
case Expression::Id::ReturnId: {
self->pushTask(SubType::doStartUnreachableBlock, currp);
break;
}
case Expression::Id::UnreachableId: {
self->pushTask(SubType::doStartUnreachableBlock, currp);
break;
}
default: {}
}
ControlFlowWalker<SubType, VisitorType>::scan(self, currp);
switch (curr->_id) {
case Expression::Id::LoopId: {
self->pushTask(SubType::doStartLoop, currp);
break;
}
default: {}
}
}
void doWalkFunction(Function* func) {
basicBlocks.clear();
startBasicBlock();
entry = currBasicBlock;
ControlFlowWalker<SubType, VisitorType>::doWalkFunction(func);
assert(branches.size() == 0);
assert(ifStack.size() == 0);
assert(loopStack.size() == 0);
}
std::unordered_set<BasicBlock*> findLiveBlocks() {
std::unordered_set<BasicBlock*> alive;
std::unordered_set<BasicBlock*> queue;
queue.insert(entry);
while (queue.size() > 0) {
auto iter = queue.begin();
auto* curr = *iter;
queue.erase(iter);
alive.insert(curr);
for (auto* out : curr->out) {
if (!alive.count(out)) queue.insert(out);
}
}
return alive;
}
void unlinkDeadBlocks(std::unordered_set<BasicBlock*> alive) {
for (auto& block : basicBlocks) {
if (!alive.count(block.get())) {
block->in.clear();
block->out.clear();
continue;
}
block->in.erase(std::remove_if(block->in.begin(), block->in.end(), [&alive](BasicBlock* other) {
return !alive.count(other);
}), block->in.end());
block->out.erase(std::remove_if(block->out.begin(), block->out.end(), [&alive](BasicBlock* other) {
return !alive.count(other);
}), block->out.end());
}
}
// TODO: utility method for optimizing cfg, removing empty blocks depending on their .content
std::map<BasicBlock*, size_t> debugIds;
void generateDebugIds() {
if (debugIds.size() > 0) return;
for (auto& block : basicBlocks) {
debugIds[block.get()] = debugIds.size();
}
}
void dumpCFG(std::string message) {
std::cout << "<==\nCFG [" << message << "]:\n";
generateDebugIds();
for (auto& block : basicBlocks) {
assert(debugIds.count(block.get()) > 0);
std::cout << " block " << debugIds[block.get()] << ":\n";
block->contents.dump(static_cast<SubType*>(this)->getFunction());
for (auto& in : block->in) {
assert(debugIds.count(in) > 0);
assert(std::find(in->out.begin(), in->out.end(), block.get()) != in->out.end()); // must be a parallel link back
}
for (auto& out : block->out) {
assert(debugIds.count(out) > 0);
std::cout << " out: " << debugIds[out] << "\n";
assert(std::find(out->in.begin(), out->in.end(), block.get()) != out->in.end()); // must be a parallel link back
}
checkDuplicates(block->in);
checkDuplicates(block->out);
}
std::cout << "==>\n";
}
private:
// links in out and in must be unique
void checkDuplicates(std::vector<BasicBlock*>& list) {
std::unordered_set<BasicBlock*> seen;
for (auto* curr : list) {
assert(seen.count(curr) == 0);
seen.insert(curr);
}
}
void removeLink(std::vector<BasicBlock*>& list, BasicBlock* toRemove) {
if (list.size() == 1) {
list.clear();
return;
}
for (size_t i = 0; i < list.size(); i++) {
if (list[i] == toRemove) {
list[i] = list.back();
list.pop_back();
return;
}
}
WASM_UNREACHABLE();
}
};
} // namespace wasm
#endif // cfg_traversal_h