Skip to content

Commit 7b0e889

Browse files
WIP
1 parent 68ea908 commit 7b0e889

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ CMakeUserPresets.json
6161

6262
# files related to clangd cache
6363
.cache/*
64+
65+
.venv/

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)