Skip to content

Commit dc2254e

Browse files
WIP
1 parent 68ea908 commit dc2254e

3 files changed

Lines changed: 32 additions & 23 deletions

File tree

src/ir/branch-utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef wasm_ir_branch_h
1818
#define wasm_ir_branch_h
1919

20+
#include <unordered_set>
21+
2022
#include "ir/iteration.h"
2123
#include "wasm-traversal.h"
2224
#include "wasm.h"
@@ -160,7 +162,7 @@ template<typename T> void operateOnScopeNameDefs(Expression* expr, T func) {
160162
#include "wasm-delegations-fields.def"
161163
}
162164

163-
using NameSet = std::set<Name>;
165+
using NameSet = std::unordered_set<Name>;
164166

165167
inline NameSet getUniqueTargets(Expression* expr) {
166168
NameSet ret;

src/ir/effects.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define wasm_ir_effects_h
1919

2020
#include <cassert>
21+
#include <unordered_set>
2122

2223
#include "ir/intrinsics.h"
2324
#include "pass.h"
@@ -141,8 +142,8 @@ class EffectAnalyzer {
141142

142143
std::set<Index> localsRead;
143144
std::set<Index> localsWritten;
144-
std::set<Name> mutableGlobalsRead;
145-
std::set<Name> globalsWritten;
145+
std::unordered_set<Name> mutableGlobalsRead;
146+
std::unordered_set<Name> globalsWritten;
146147

147148
// The nested depth of try-catch_all. If an instruction that may throw is
148149
// inside an inner try-catch_all, we don't mark it as 'throws_', because it
@@ -513,8 +514,8 @@ class EffectAnalyzer {
513514
return hasAnything();
514515
}
515516

516-
std::set<Name> breakTargets;
517-
std::set<Name> delegateTargets;
517+
std::unordered_set<Name> breakTargets;
518+
std::unordered_set<Name> delegateTargets;
518519

519520
private:
520521
struct InternalAnalyzer

src/passes/CodeFolding.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
// to the same thing, and after merging it can still reach it).
5656
//
5757

58-
#include <iterator>
58+
#include <unordered_map>
59+
#include <unordered_set>
5960

6061
#include "ir/branch-utils.h"
6162
#include "ir/effects.h"
@@ -74,9 +75,9 @@ static const Index WORTH_ADDING_BLOCK_TO_REMOVE_THIS_MUCH = 3;
7475
struct ExpressionMarker
7576
: public PostWalker<ExpressionMarker,
7677
UnifiedExpressionVisitor<ExpressionMarker>> {
77-
std::set<Expression*>& marked;
78+
std::unordered_set<Expression*>& marked;
7879

79-
ExpressionMarker(std::set<Expression*>& marked, Expression* expr)
80+
ExpressionMarker(std::unordered_set<Expression*>& marked, Expression* expr)
8081
: marked(marked) {
8182
walk(expr);
8283
}
@@ -122,13 +123,16 @@ struct CodeFolding
122123

123124
// pass state
124125

125-
std::map<Name, std::vector<Tail>> breakTails; // break target name => tails
126-
// that reach it
126+
std::unordered_map<Name, std::vector<Tail>>
127+
breakTails; // break target name => tails
128+
// that reach it
127129
std::vector<Tail> unreachableTails; // tails leading to (unreachable)
128130
std::vector<Tail> returnTails; // tails leading to (return)
129-
std::set<Name> unoptimizables; // break target names that we can't handle
130-
std::set<Expression*> modifieds; // modified code should not be processed
131-
// again, wait for next pass
131+
std::unordered_set<Name>
132+
unoptimizables; // break target names that we can't handle
133+
std::unordered_set<Expression*>
134+
modifieds; // modified code should not be processed
135+
// again, wait for next pass
132136

133137
// walking
134138

@@ -308,13 +312,14 @@ struct CodeFolding
308312
auto allTargets = BranchUtils::getBranchTargets(outOf);
309313
for (auto* item : items) {
310314
auto exiting = BranchUtils::getExitingBranches(item);
311-
std::vector<Name> intersection;
312-
std::set_intersection(allTargets.begin(),
313-
allTargets.end(),
314-
exiting.begin(),
315-
exiting.end(),
316-
std::back_inserter(intersection));
317-
if (intersection.size() > 0) {
315+
bool hasIntersection = false;
316+
for (auto& name : exiting) {
317+
if (allTargets.count(name)) {
318+
hasIntersection = true;
319+
break;
320+
}
321+
}
322+
if (hasIntersection) {
318323
// anything exiting that is in all targets is something bad
319324
return false;
320325
}
@@ -644,17 +649,18 @@ struct CodeFolding
644649
if (next.size() >= 2) {
645650
// now we want to find a mergeable item - any item that is equal among a
646651
// subset
647-
std::map<Expression*, size_t> hashes; // expression => hash value
652+
std::unordered_map<Expression*, size_t>
653+
hashes; // expression => hash value
648654
// hash value => expressions with that hash
649-
std::map<size_t, std::vector<Expression*>> hashed;
655+
std::unordered_map<size_t, std::vector<Expression*>> hashed;
650656
for (auto& tail : next) {
651657
auto* item = getItem(tail, num);
652658
auto hash = hashes[item] = ExpressionAnalyzer::hash(item);
653659
hashed[hash].push_back(item);
654660
}
655661
// look at each hash value exactly once. we do this in a deterministic
656662
// order by iterating over a vector retaining insertion order.
657-
std::set<size_t> seen;
663+
std::unordered_set<size_t> seen;
658664
for (auto& tail : next) {
659665
auto* item = getItem(tail, num);
660666
auto digest = hashes[item];

0 commit comments

Comments
 (0)