summaryrefslogtreecommitdiff
path: root/src/cfg
diff options
context:
space:
mode:
authorhobby8 <hobby8@users.noreply.github.com>2019-06-07 17:31:07 +0000
committerAlon Zakai <azakai@google.com>2019-06-07 10:31:07 -0700
commit5f252c3a6c2129a3f86e00401806b74bc2f266df (patch)
tree3a8bf48591fca3a875859cf89e439eda214835fc /src/cfg
parent1578cec27d45cf5104480f533fb092e81f0dbeb5 (diff)
downloadbinaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.tar.gz
binaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.tar.bz2
binaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.zip
Fix bug and leak in relooper merge consecutive blocks (#2159)
Fixes in Relooper merge consecutive blocks: Entry block getting removed when it is part of a loop: bb1->AddBranchTo(bb2, nullptr); bb1->AddBranchTo(bb3, ...); bb2->AddBranchTo(bb1, nullptr); bb3->AddBranchTo(bb4, nullptr); relooper.AddBlock(bb1); relooper.AddBlock(bb2); relooper.AddBlock(bb3); relooper.AddBlock(bb4); relooper.Calculate(bb1); Branches memory leak
Diffstat (limited to 'src/cfg')
-rw-r--r--src/cfg/Relooper.cpp11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/cfg/Relooper.cpp b/src/cfg/Relooper.cpp
index 87dd4e379..08f28bc30 100644
--- a/src/cfg/Relooper.cpp
+++ b/src/cfg/Relooper.cpp
@@ -533,7 +533,10 @@ struct Liveness : public RelooperRecursor {
typedef std::pair<Branch*, Block*> BranchBlock;
struct Optimizer : public RelooperRecursor {
- Optimizer(Relooper* Parent) : RelooperRecursor(Parent) {
+ Block* Entry;
+
+ Optimizer(Relooper* Parent, Block* EntryInit)
+ : RelooperRecursor(Parent), Entry(EntryInit) {
// TODO: there are likely some rare but possible O(N^2) cases with this
// looping
bool More = true;
@@ -726,6 +729,7 @@ struct Optimizer : public RelooperRecursor {
NumPredecessors[NextBlock]++;
}
}
+ NumPredecessors[Entry]++;
for (auto* CurrBlock : Parent->Blocks) {
if (CurrBlock->BranchesOut.size() == 1) {
auto iter = CurrBlock->BranchesOut.begin();
@@ -744,6 +748,9 @@ struct Optimizer : public RelooperRecursor {
Builder.makeSequence(CurrBlock->Code, NextBlock->Code);
// Use the next block's branching behavior
CurrBlock->BranchesOut.swap(NextBlock->BranchesOut);
+ for (auto& iter : NextBlock->BranchesOut) {
+ delete iter.second;
+ }
NextBlock->BranchesOut.clear();
CurrBlock->SwitchCondition = NextBlock->SwitchCondition;
// The next block now has no predecessors.
@@ -1032,7 +1039,7 @@ private:
void Relooper::Calculate(Block* Entry) {
// Optimize.
- Optimizer(this);
+ Optimizer(this, Entry);
// Find live blocks.
Liveness Live(this);