Skip to content

Commit 77802a6

Browse files
authored
Merge pull request #1007 from WebAssembly/brs
Misc minor remove-unused-brs improvements
2 parents 6efa4a9 + b3c97a9 commit 77802a6

15 files changed

+1177
-1208
lines changed

src/passes/RemoveUnusedBrs.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
122122
} else if (curr->is<Nop>()) {
123123
// ignore (could be result of a previous cycle)
124124
self->valueCanFlow = false;
125+
} else if (curr->is<Loop>()) {
126+
// do nothing - it's ok for values to flow out
125127
} else {
126128
// anything else stops the flow
127129
flows.clear();
@@ -323,10 +325,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
323325
// map of all value-less breaks going to a block (and not a loop)
324326
std::map<Block*, std::vector<Break*>> breaksToBlock;
325327

326-
// number of definitions of each name - when a name is defined more than once, it is not trivially safe to do this
327-
std::map<Name, Index> numDefs;
328-
329-
// the names to update, when we can (just one def)
328+
// the names to update
330329
std::map<Break*, Name> newNames;
331330

332331
void visitBreak(Break* curr) {
@@ -338,8 +337,6 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
338337
}
339338
// TODO: Switch?
340339
void visitBlock(Block* curr) {
341-
if (curr->name.is()) numDefs[curr->name]++;
342-
343340
auto& list = curr->list;
344341
if (list.size() == 1 && curr->name.is()) {
345342
// if this block has just one child, a sub-block, then jumps to the former are jumps to us, really
@@ -372,17 +369,12 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
372369
}
373370
}
374371
}
375-
void visitLoop(Loop* curr) {
376-
if (curr->name.is()) numDefs[curr->name]++;
377-
}
378372

379373
void finish() {
380374
for (auto& iter : newNames) {
381375
auto* br = iter.first;
382376
auto name = iter.second;
383-
if (numDefs[name] == 1) {
384-
br->name = name;
385-
}
377+
br->name = name;
386378
}
387379
}
388380
};

src/passes/pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ void PassRunner::addDefaultFunctionOptimizationPasses() {
135135
add("simplify-locals");
136136
add("vacuum"); // previous pass creates garbage
137137
add("reorder-locals");
138+
add("merge-blocks"); // makes remove-unused-brs more effective
138139
add("remove-unused-brs"); // coalesce-locals opens opportunities for optimizations
139-
add("merge-blocks");
140+
add("merge-blocks"); // clean up remove-unused-brs new blocks
140141
add("optimize-instructions");
141142
add("precompute");
142143
if (options.shrinkLevel >= 2) {

0 commit comments

Comments
 (0)