Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 87 additions & 3 deletions src/passes/CodeFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "ir/effects.h"
#include "ir/eh-utils.h"
#include "ir/find_all.h"
#include "ir/iteration.h"
#include "ir/label-utils.h"
#include "ir/utils.h"
#include "pass.h"
Expand Down Expand Up @@ -299,13 +300,98 @@ struct CodeFolding
returnTails.clear();
unoptimizables.clear();
modifieds.clear();
hasExitingBranchCache.clear();
if (needEHFixups) {
EHUtils::handleBlockNestedPops(func, *getModule());
}
}
}

private:
// Cache of hasExitingBranches() bool results, populated on demand.
std::unordered_map<Expression*, bool> hasExitingBranchCache;

bool hasExitingBranches(Expression* expr) {
auto it = hasExitingBranchCache.find(expr);
if (it != hasExitingBranchCache.end()) {
return it->second;
}
return populateExitingBranchCache(expr);
}

// Walk |root| bottom-up computing exiting branches. Name sets are kept
// transiently (moved from children, erased after merge). Only the root's
// bool result is persisted to keep the cache small. Already-cached
// subtrees are skipped via scan().
bool populateExitingBranchCache(Expression* root) {
struct CachePopulator
: public PostWalker<CachePopulator,
UnifiedExpressionVisitor<CachePopulator>> {
std::unordered_map<Expression*, bool>& resultCache;
Expression* root;
bool rootResult = false;
std::unordered_map<Expression*, std::unordered_set<Name>> nameSets;

CachePopulator(std::unordered_map<Expression*, bool>& resultCache,
Expression* root)
: resultCache(resultCache), root(root) {}

static void scan(CachePopulator* self, Expression** currp) {
auto* curr = *currp;
if (self->resultCache.count(curr)) {
return;
}
PostWalker<CachePopulator,
UnifiedExpressionVisitor<CachePopulator>>::scan(self, currp);
}

void visitExpression(Expression* curr) {
std::unordered_set<Name> targets;

bool childFromPriorWalkHasExiting = false;
ChildIterator children(curr);
for (auto* child : children) {
auto it = nameSets.find(child);
if (it != nameSets.end()) {
if (targets.empty()) {
targets = std::move(it->second);
} else {
targets.merge(it->second);
}
nameSets.erase(it);
} else {
// Conservatively propagate prior-cached children's results.
auto cacheIt = resultCache.find(child);
if (cacheIt != resultCache.end() && cacheIt->second) {
childFromPriorWalkHasExiting = true;
}
}
}

BranchUtils::operateOnScopeNameUses(
curr, [&](Name& name) { targets.insert(name); });

BranchUtils::operateOnScopeNameDefs(curr, [&](Name& name) {
if (name.is()) {
targets.erase(name);
}
});

bool hasExiting = !targets.empty() || childFromPriorWalkHasExiting;
if (!targets.empty()) {
nameSets[curr] = std::move(targets);
}
if (curr == root) {
resultCache[curr] = hasExiting;
rootResult = hasExiting;
}
}
};
CachePopulator populator(hasExitingBranchCache, root);
populator.walk(root);
return populator.rootResult;
}

// check if we can move a list of items out of another item. we can't do so
// if one of the items has a branch to something inside outOf that is not
// inside that item
Expand Down Expand Up @@ -637,9 +723,7 @@ struct CodeFolding
// TODO: this should not be a problem in
// *non*-terminating tails, but
// double-verify that
if (EffectAnalyzer(
getPassOptions(), *getModule(), newItem)
.hasExternalBreakTargets()) {
if (hasExitingBranches(newItem)) {
return true;
}
return false;
Expand Down
Loading