summaryrefslogtreecommitdiff
path: root/src/passes/DuplicateFunctionElimination.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/DuplicateFunctionElimination.cpp')
-rw-r--r--src/passes/DuplicateFunctionElimination.cpp45
1 files changed, 27 insertions, 18 deletions
diff --git a/src/passes/DuplicateFunctionElimination.cpp b/src/passes/DuplicateFunctionElimination.cpp
index b7fcb556c..3caa43e1d 100644
--- a/src/passes/DuplicateFunctionElimination.cpp
+++ b/src/passes/DuplicateFunctionElimination.cpp
@@ -20,19 +20,20 @@
// identical when finally lowered into concrete wasm code.
//
-#include "wasm.h"
-#include "pass.h"
-#include "ir/utils.h"
#include "ir/function-utils.h"
#include "ir/hashed.h"
#include "ir/module-utils.h"
+#include "ir/utils.h"
+#include "pass.h"
+#include "wasm.h"
namespace wasm {
struct FunctionReplacer : public WalkerPass<PostWalker<FunctionReplacer>> {
bool isFunctionParallel() override { return true; }
- FunctionReplacer(std::map<Name, Name>* replacements) : replacements(replacements) {}
+ FunctionReplacer(std::map<Name, Name>* replacements)
+ : replacements(replacements) {}
FunctionReplacer* create() override {
return new FunctionReplacer(replacements);
@@ -51,15 +52,17 @@ private:
struct DuplicateFunctionElimination : public Pass {
void run(PassRunner* runner, Module* module) override {
- // Multiple iterations may be necessary: A and B may be identical only after we
- // see the functions C1 and C2 that they call are in fact identical. Rarely, such
- // "chains" can be very long, so we limit how many we do.
+ // Multiple iterations may be necessary: A and B may be identical only after
+ // we see the functions C1 and C2 that they call are in fact identical.
+ // Rarely, such "chains" can be very long, so we limit how many we do.
auto& options = runner->options;
Index limit;
if (options.optimizeLevel >= 3 || options.shrinkLevel >= 1) {
limit = module->functions.size(); // no limit
} else if (options.optimizeLevel >= 2) {
- limit = 10; // 10 passes usually does most of the work, as this is typically logarithmic
+ // 10 passes usually does most of the work, as this is typically
+ // logarithmic
+ limit = 10;
} else {
limit = 1;
}
@@ -82,16 +85,19 @@ struct DuplicateFunctionElimination : public Pass {
for (auto& pair : hashGroups) {
auto& group = pair.second;
Index size = group.size();
- if (size == 1) continue;
- // The groups should be fairly small, and even if a group is large we should
- // have almost all of them identical, so we should not hit actual O(N^2)
- // here unless the hash is quite poor.
+ if (size == 1)
+ continue;
+ // The groups should be fairly small, and even if a group is large we
+ // should have almost all of them identical, so we should not hit actual
+ // O(N^2) here unless the hash is quite poor.
for (Index i = 0; i < size - 1; i++) {
auto* first = group[i];
- if (duplicates.count(first->name)) continue;
+ if (duplicates.count(first->name))
+ continue;
for (Index j = i + 1; j < size; j++) {
auto* second = group[j];
- if (duplicates.count(second->name)) continue;
+ if (duplicates.count(second->name))
+ continue;
if (FunctionUtils::equal(first, second)) {
// great, we can replace the second with the first!
replacements[second->name] = first->name;
@@ -104,9 +110,12 @@ struct DuplicateFunctionElimination : public Pass {
if (replacements.size() > 0) {
// remove the duplicates
auto& v = module->functions;
- v.erase(std::remove_if(v.begin(), v.end(), [&](const std::unique_ptr<Function>& curr) {
- return duplicates.count(curr->name) > 0;
- }), v.end());
+ v.erase(std::remove_if(v.begin(),
+ v.end(),
+ [&](const std::unique_ptr<Function>& curr) {
+ return duplicates.count(curr->name) > 0;
+ }),
+ v.end());
module->updateMaps();
// replace direct calls
PassRunner replacerRunner(module);
@@ -143,7 +152,7 @@ struct DuplicateFunctionElimination : public Pass {
}
};
-Pass *createDuplicateFunctionEliminationPass() {
+Pass* createDuplicateFunctionEliminationPass() {
return new DuplicateFunctionElimination();
}