Skip to content

Commit fb4aef6

Browse files
authored
[NFC] Avoid multiple scans in LUBFinder::getResultsLUB (#8104)
Rather than FindAll multiple times, do a single scan with visitors. This makes DAE 2% faster (and likely other passes that also use this utility).
1 parent 9d94003 commit fb4aef6

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

src/ir/lubs.cpp

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
#include "ir/lubs.h"
18-
#include "ir/find_all.h"
1918
#include "ir/utils.h"
2019
#include "wasm-type.h"
2120
#include "wasm.h"
@@ -51,51 +50,36 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) {
5150
return lub;
5251
}
5352

54-
// Scan the body and look at the returns. First, return expressions.
55-
for (auto* ret : FindAll<Return>(func->body).list) {
56-
lub.note(ret->value->type);
57-
if (lub.getLUB() == originalType) {
58-
return lub;
59-
}
60-
}
53+
// Scan the body and look at the returns.
54+
struct Finder : public PostWalker<Finder> {
55+
Module& wasm;
56+
LUBFinder& lub;
6157

62-
// Process return_calls and call_refs. Unlike return expressions which we
63-
// just handled, these only get a type to update, not a value.
64-
auto processReturnType = [&](Type type) {
65-
// Return whether we still look ok to do the optimization. If this is
66-
// false then we can stop here.
67-
lub.note(type);
68-
return lub.getLUB() != originalType;
69-
};
58+
Finder(Module& wasm, LUBFinder& lub) : wasm(wasm), lub(lub) {}
7059

71-
for (auto* call : FindAll<Call>(func->body).list) {
72-
if (call->isReturn &&
73-
!processReturnType(wasm.getFunction(call->target)->getResults())) {
74-
return lub;
75-
}
76-
}
77-
for (auto* call : FindAll<CallIndirect>(func->body).list) {
78-
if (call->isReturn &&
79-
!processReturnType(call->heapType.getSignature().results)) {
80-
return lub;
81-
}
82-
}
83-
for (auto* call : FindAll<CallRef>(func->body).list) {
84-
if (call->isReturn) {
85-
auto targetType = call->target->type;
86-
// We can skip unreachable code and calls to bottom types, as both trap.
87-
if (targetType == Type::unreachable) {
88-
continue;
60+
void visitReturn(Return* curr) { lub.note(curr->value->type); }
61+
void visitCall(Call* curr) {
62+
if (curr->isReturn) {
63+
lub.note(wasm.getFunction(curr->target)->getResults());
8964
}
90-
auto targetHeapType = targetType.getHeapType();
91-
if (targetHeapType.isBottom()) {
92-
continue;
65+
}
66+
void visitCallIndirect(CallIndirect* curr) {
67+
if (curr->isReturn) {
68+
lub.note(curr->heapType.getSignature().results);
9369
}
94-
if (!processReturnType(targetHeapType.getSignature().results)) {
95-
return lub;
70+
}
71+
void visitCallRef(CallRef* curr) {
72+
if (curr->isReturn) {
73+
auto targetType = curr->target->type;
74+
// We can skip unreachable code and calls to bottom types, as both trap.
75+
if (!targetType.isSignature()) {
76+
return;
77+
}
78+
lub.note(targetType.getHeapType().getSignature().results);
9679
}
9780
}
98-
}
81+
} finder(wasm, lub);
82+
finder.walk(func->body);
9983

10084
return lub;
10185
}

0 commit comments

Comments
 (0)