Skip to content

Commit

Permalink
run multiple cycles of RemoveUnusedBrs
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Apr 24, 2016
1 parent 7dcb613 commit 1867653
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/passes/RemoveUnusedBrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace wasm {
struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<RemoveUnusedBrs>>> {
bool isFunctionParallel() { return true; }

bool anotherCycle;

typedef std::vector<Break*> Flows;

// list of breaks that are currently flowing. if they reach their target without
Expand Down Expand Up @@ -68,6 +70,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
if (flows[i]->name == name) {
ExpressionManipulator::nop(flows[i]);
skip++;
self->anotherCycle = true;
} else if (skip > 0) {
flows[i - skip] = flows[i];
}
Expand All @@ -79,6 +82,8 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
} else if (curr->is<Loop>()) {
// TODO we might optimize branches out of here
flows.clear();
} else if (curr->is<Nop>()) {
// ignore (could be result of a previous cycle)
} else {
// anything else stops the flow
flows.clear();
Expand All @@ -100,6 +105,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
if (br && !br->condition) { // TODO: if there is a condition, join them
br->condition = curr->condition;
replaceCurrent(br);
anotherCycle = true;
}
}
}
Expand All @@ -125,7 +131,15 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<R
}
}

// TODO: multiple rounds?
void walk(Expression*& root) {
// multiple cycles may be needed
do {
anotherCycle = false;
WalkerPass<PostWalker<RemoveUnusedBrs, Visitor<RemoveUnusedBrs>>>::walk(root);
assert(ifStack.empty());
assert(flows.empty());
} while (anotherCycle);
}
};

static RegisterPass<RemoveUnusedBrs> registerPass("remove-unused-brs", "removes breaks from locations that are not needed");
Expand Down
1 change: 0 additions & 1 deletion test/emcc_hello_world.fromasm
Original file line number Diff line number Diff line change
Expand Up @@ -10864,7 +10864,6 @@
(get_local $$arg)
(get_local $$110)
)
(br $label$break$L1)
)
)
)
Expand Down
1 change: 0 additions & 1 deletion test/emcc_hello_world.fromasm.imprecise
Original file line number Diff line number Diff line change
Expand Up @@ -10862,7 +10862,6 @@
(get_local $$arg)
(get_local $$110)
)
(br $label$break$L1)
)
)
)
Expand Down
8 changes: 4 additions & 4 deletions test/passes/remove-unused-brs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@
(block $a
(block $b
(block $c
(br $a)
(nop)
)
(br $a)
(nop)
)
(nop)
)
Expand All @@ -187,9 +187,9 @@
(block $a
(block $b
(block $c
(br $b)
(nop)
)
(br $a)
(nop)
)
(nop)
)
Expand Down

0 comments on commit 1867653

Please sign in to comment.