diff options
author | Michael Bebenita <mbebenita@gmail.com> | 2016-05-25 19:26:57 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-05-25 19:26:57 -0700 |
commit | 948f6fe7c4d2a9256310c07c54724685f27874ac (patch) | |
tree | 2431e7bc59cf70c65ffdb8d84cc44ee7a313a79d /src/ast_utils.h | |
parent | 43a9ecd084b828bb715211d91bf412727ff58cff (diff) | |
download | binaryen-948f6fe7c4d2a9256310c07c54724685f27874ac.tar.gz binaryen-948f6fe7c4d2a9256310c07c54724685f27874ac.tar.bz2 binaryen-948f6fe7c4d2a9256310c07c54724685f27874ac.zip |
Add remove unused functions pass. (#463)
Diffstat (limited to 'src/ast_utils.h')
-rw-r--r-- | src/ast_utils.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/ast_utils.h b/src/ast_utils.h index 730ef35a6..a43fc6b2f 100644 --- a/src/ast_utils.h +++ b/src/ast_utils.h @@ -46,6 +46,34 @@ struct BreakSeeker : public PostWalker<BreakSeeker, Visitor<BreakSeeker>> { } }; +// Finds all functions that are reachable via direct calls. + +struct DirectCallGraphAnalyzer : public PostWalker<DirectCallGraphAnalyzer, Visitor<DirectCallGraphAnalyzer>> { + Module *module; + std::vector<Function*> queue; + std::unordered_set<Function*> reachable; + + DirectCallGraphAnalyzer(Module* module, const std::vector<Function*>& root) : module(module) { + for (auto* curr : root) { + queue.push_back(curr); + } + while (queue.size()) { + auto* curr = queue.back(); + queue.pop_back(); + if (reachable.count(curr) == 0) { + reachable.insert(curr); + walk(curr->body); + } + } + } + void visitCall(Call *curr) { + auto* target = module->getFunction(curr->target); + if (reachable.count(target) == 0) { + queue.push_back(target); + } + } +}; + // Look for side effects, including control flow // TODO: optimize |