summaryrefslogtreecommitdiff
path: root/src/passes/CodeFolding.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/CodeFolding.cpp')
-rw-r--r--src/passes/CodeFolding.cpp63
1 files changed, 42 insertions, 21 deletions
diff --git a/src/passes/CodeFolding.cpp b/src/passes/CodeFolding.cpp
index 0479472d8..d60bc76d1 100644
--- a/src/passes/CodeFolding.cpp
+++ b/src/passes/CodeFolding.cpp
@@ -179,19 +179,23 @@ struct CodeFolding : public WalkerPass<ControlFlowWalker<CodeFolding>> {
}
void visitBlock(Block* curr) {
- if (curr->list.empty())
+ if (curr->list.empty()) {
return;
- if (!curr->name.is())
+ }
+ if (!curr->name.is()) {
return;
- if (unoptimizables.count(curr->name) > 0)
+ }
+ if (unoptimizables.count(curr->name) > 0) {
return;
+ }
// we can't optimize a fallthrough value
if (isConcreteType(curr->list.back()->type)) {
return;
}
auto iter = breakTails.find(curr->name);
- if (iter == breakTails.end())
+ if (iter == breakTails.end()) {
return;
+ }
// looks promising
auto& tails = iter->second;
// see if there is a fallthrough
@@ -208,8 +212,9 @@ struct CodeFolding : public WalkerPass<ControlFlowWalker<CodeFolding>> {
}
void visitIf(If* curr) {
- if (!curr->ifFalse)
+ if (!curr->ifFalse) {
return;
+ }
// if both sides are identical, this is easy to fold
if (ExpressionAnalyzer::equal(curr->ifTrue, curr->ifFalse)) {
Builder builder(*getModule());
@@ -304,14 +309,17 @@ private:
// identical in all paths leading to the block exit can be merged.
template<typename T>
void optimizeExpressionTails(std::vector<Tail>& tails, T* curr) {
- if (tails.size() < 2)
+ if (tails.size() < 2) {
return;
+ }
// see if anything is untoward, and we should not do this
for (auto& tail : tails) {
- if (tail.expr && modifieds.count(tail.expr) > 0)
+ if (tail.expr && modifieds.count(tail.expr) > 0) {
return;
- if (modifieds.count(tail.block) > 0)
+ }
+ if (modifieds.count(tail.block) > 0) {
return;
+ }
// if we were not modified, then we should be valid for processing
tail.validate();
}
@@ -345,8 +353,9 @@ private:
break;
}
}
- if (stop)
+ if (stop) {
break;
+ }
auto* item = getMergeable(tails[0], num);
for (auto& tail : tails) {
if (!ExpressionAnalyzer::equal(item, getMergeable(tail, num))) {
@@ -355,18 +364,21 @@ private:
break;
}
}
- if (stop)
+ if (stop) {
break;
+ }
// we may have found another one we can merge - can we move it?
- if (!canMove({item}, curr))
+ if (!canMove({item}, curr)) {
break;
+ }
// we found another one we can merge
mergeable.push_back(item);
num++;
saved += Measurer::measure(item);
}
- if (saved == 0)
+ if (saved == 0) {
return;
+ }
// we may be able to save enough.
if (saved < WORTH_ADDING_BLOCK_TO_REMOVE_THIS_MUCH) {
// it's not obvious we can save enough. see if we get rid
@@ -463,17 +475,20 @@ private:
// deeper merges first.
// returns whether we optimized something.
bool optimizeTerminatingTails(std::vector<Tail>& tails, Index num = 0) {
- if (tails.size() < 2)
+ if (tails.size() < 2) {
return false;
+ }
// remove things that are untoward and cannot be optimized
tails.erase(
std::remove_if(tails.begin(),
tails.end(),
[&](Tail& tail) {
- if (tail.expr && modifieds.count(tail.expr) > 0)
+ if (tail.expr && modifieds.count(tail.expr) > 0) {
return true;
- if (tail.block && modifieds.count(tail.block) > 0)
+ }
+ if (tail.block && modifieds.count(tail.block) > 0) {
return true;
+ }
// if we were not modified, then we should be valid for
// processing
tail.validate();
@@ -544,8 +559,9 @@ private:
next.erase(std::remove_if(next.begin(),
next.end(),
[&](Tail& tail) {
- if (effectiveSize(tail) < num + 1)
+ if (effectiveSize(tail) < num + 1) {
return true;
+ }
auto* newItem = getItem(tail, num);
// ignore tails that break to outside blocks. we
// want to move code to the very outermost
@@ -578,12 +594,14 @@ private:
for (auto& tail : next) {
auto* item = getItem(tail, num);
auto hash = hashes[item];
- if (seen.count(hash))
+ if (seen.count(hash)) {
continue;
+ }
seen.insert(hash);
auto& items = hashed[hash];
- if (items.size() == 1)
+ if (items.size() == 1) {
continue;
+ }
assert(items.size() > 0);
// look for an item that has another match.
while (items.size() >= 2) {
@@ -632,11 +650,13 @@ private:
// we explored deeper (higher num) options, but perhaps there
// was nothing there while there is something we can do at this level
// but if we are at num == 0, then we found nothing at all
- if (num == 0)
+ if (num == 0) {
return false;
+ }
// if not worth it, stop
- if (!worthIt(num, tails))
+ if (!worthIt(num, tails)) {
return false;
+ }
// this is worth doing, do it!
auto mergeable = getTailItems(num, tails); // the elements we can merge
// since we managed a merge, then it might open up more opportunities later
@@ -680,8 +700,9 @@ private:
// rules, and now it won't be toplevel in the function, it can
// change)
auto* toplevel = old->dynCast<Block>();
- if (toplevel)
+ if (toplevel) {
toplevel->finalize();
+ }
if (old->type != unreachable) {
inner->list.push_back(builder.makeReturn(old));
} else {