|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | #include "ir/lubs.h" |
18 | | -#include "ir/find_all.h" |
19 | 18 | #include "ir/utils.h" |
20 | 19 | #include "wasm-type.h" |
21 | 20 | #include "wasm.h" |
@@ -51,51 +50,36 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { |
51 | 50 | return lub; |
52 | 51 | } |
53 | 52 |
|
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; |
61 | 57 |
|
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) {} |
70 | 59 |
|
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()); |
89 | 64 | } |
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); |
93 | 69 | } |
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); |
96 | 79 | } |
97 | 80 | } |
98 | | - } |
| 81 | + } finder(wasm, lub); |
| 82 | + finder.walk(func->body); |
99 | 83 |
|
100 | 84 | return lub; |
101 | 85 | } |
|
0 commit comments