diff options
author | Heejin Ahn <aheejin@gmail.com> | 2019-12-02 16:34:03 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-02 16:34:03 -0800 |
commit | 9d4acee666dfa46e81838d385a663b4c4730c852 (patch) | |
tree | eaab2d8a8244e9259fa6af8545bc792be6f55c2c /src/passes/RemoveUnusedModuleElements.cpp | |
parent | 86492f7b3d48c9ec5f27ca7d3abd90ec02972a40 (diff) | |
download | binaryen-9d4acee666dfa46e81838d385a663b4c4730c852.tar.gz binaryen-9d4acee666dfa46e81838d385a663b4c4730c852.tar.bz2 binaryen-9d4acee666dfa46e81838d385a663b4c4730c852.zip |
Refactor removing module elements (#2489)
This creates utility functions for removing module elements: removing
one element by name, and removing multiple elements using a predicate
function. And makes other parts of code use it. I think this is a
light-handed approach than calling `Module::updateMaps` after removing
only a part of module elements.
This also fixes a bug in the inlining pass: it didn't call
`Module::updateMaps` after removing functions. After this patch callers
don't need to additionally call it anyway.
Diffstat (limited to 'src/passes/RemoveUnusedModuleElements.cpp')
-rw-r--r-- | src/passes/RemoveUnusedModuleElements.cpp | 56 |
1 files changed, 14 insertions, 42 deletions
diff --git a/src/passes/RemoveUnusedModuleElements.cpp b/src/passes/RemoveUnusedModuleElements.cpp index 20ce284f7..cd69894f3 100644 --- a/src/passes/RemoveUnusedModuleElements.cpp +++ b/src/passes/RemoveUnusedModuleElements.cpp @@ -211,40 +211,18 @@ struct RemoveUnusedModuleElements : public Pass { // Compute reachability starting from the root set. ReachabilityAnalyzer analyzer(module, roots); // Remove unreachable elements. - { - auto& v = module->functions; - v.erase(std::remove_if(v.begin(), - v.end(), - [&](const std::unique_ptr<Function>& curr) { - return analyzer.reachable.count(ModuleElement( - ModuleElementKind::Function, - curr->name)) == 0; - }), - v.end()); - } - { - auto& v = module->globals; - v.erase(std::remove_if(v.begin(), - v.end(), - [&](const std::unique_ptr<Global>& curr) { - return analyzer.reachable.count( - ModuleElement(ModuleElementKind::Global, - curr->name)) == 0; - }), - v.end()); - } - { - auto& v = module->events; - v.erase(std::remove_if(v.begin(), - v.end(), - [&](const std::unique_ptr<Event>& curr) { - return analyzer.reachable.count( - ModuleElement(ModuleElementKind::Event, - curr->name)) == 0; - }), - v.end()); - } - module->updateMaps(); + module->removeFunctions([&](Function* curr) { + return analyzer.reachable.count( + ModuleElement(ModuleElementKind::Function, curr->name)) == 0; + }); + module->removeGlobals([&](Global* curr) { + return analyzer.reachable.count( + ModuleElement(ModuleElementKind::Global, curr->name)) == 0; + }); + module->removeEvents([&](Event* curr) { + return analyzer.reachable.count( + ModuleElement(ModuleElementKind::Event, curr->name)) == 0; + }); // Handle the memory and table if (!exportsMemory && !analyzer.usesMemory) { if (!importsMemory) { @@ -302,14 +280,8 @@ struct RemoveUnusedModuleElements : public Pass { call->fullType = canonicalize(call->fullType); } // remove no-longer used types - module->functionTypes.erase( - std::remove_if(module->functionTypes.begin(), - module->functionTypes.end(), - [&needed](std::unique_ptr<FunctionType>& type) { - return needed.count(type.get()) == 0; - }), - module->functionTypes.end()); - module->updateMaps(); + module->removeFunctionTypes( + [&](FunctionType* type) { return needed.count(type) == 0; }); } }; |