diff options
author | Alon Zakai <azakai@google.com> | 2021-12-09 08:28:35 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-09 08:28:35 -0800 |
commit | 2689bc1233a6aee97ca04d5f60af0a8c663479c8 (patch) | |
tree | aac86d3e4884e7ddb55235fcd5d6f37ddc0ed265 /src/passes/DeadArgumentElimination.cpp | |
parent | f7be757a71e5562afad898992574681d50a67dbf (diff) | |
download | binaryen-2689bc1233a6aee97ca04d5f60af0a8c663479c8.tar.gz binaryen-2689bc1233a6aee97ca04d5f60af0a8c663479c8.tar.bz2 binaryen-2689bc1233a6aee97ca04d5f60af0a8c663479c8.zip |
[NFC] Refactor result type LUB computation into a helper function (#4379)
Diffstat (limited to 'src/passes/DeadArgumentElimination.cpp')
-rw-r--r-- | src/passes/DeadArgumentElimination.cpp | 92 |
1 files changed, 10 insertions, 82 deletions
diff --git a/src/passes/DeadArgumentElimination.cpp b/src/passes/DeadArgumentElimination.cpp index 89e47708b..999607ae2 100644 --- a/src/passes/DeadArgumentElimination.cpp +++ b/src/passes/DeadArgumentElimination.cpp @@ -611,94 +611,22 @@ private: bool refineReturnTypes(Function* func, const std::vector<Call*>& calls, Module* module) { - if (!module->features.hasGC()) { - return false; - } - - Type originalType = func->getResults(); - if (!originalType.hasRef()) { - // Nothing to refine. - return false; - } - - // Before we do anything, we must refinalize the function, because otherwise - // its body may contain a block with a forced type, - // - // (func (result X) - // (block (result X) - // (..content with more specific type Y..) - // ) - ReFinalize().walkFunctionInModule(func, module); - - LUBFinder lub; - lub.noteUpdatableExpression(func->body); - if (lub.getBestPossible() == originalType) { - return false; - } - - // Scan the body and look at the returns. First, return expressions. - for (auto* ret : FindAll<Return>(func->body).list) { - lub.noteUpdatableExpression(ret->value); - if (lub.getBestPossible() == originalType) { - return false; - } - } - - // Process return_calls and call_refs. Unlike return expressions which we - // just handled, these only get a type to update, not a value. - auto processReturnType = [&](Type type) { - // Return whether we still look ok to do the optimization. If this is - // false then we can stop here. - lub.note(type); - return lub.getBestPossible() != originalType; - }; - - for (auto* call : FindAll<Call>(func->body).list) { - if (call->isReturn && - !processReturnType(module->getFunction(call->target)->getResults())) { - return false; - } - } - for (auto* call : FindAll<CallIndirect>(func->body).list) { - if (call->isReturn && - !processReturnType(call->heapType.getSignature().results)) { - return false; - } - } - for (auto* call : FindAll<CallRef>(func->body).list) { - if (call->isReturn) { - auto targetType = call->target->type; - if (targetType == Type::unreachable) { - continue; - } - if (!processReturnType( - targetType.getHeapType().getSignature().results)) { - return false; - } - } - } - - // If the refined type is unreachable then nothing actually returns from - // this function. - // TODO: We can propagate that to the outside, and not just for GC. + auto lub = LUB::getResultsLUB(func, *module); if (!lub.noted()) { return false; } - auto newType = lub.getBestPossible(); - if (newType == originalType) { - return false; - } - - // Success. Update the type, and the calls. - func->setResults(newType); - for (auto* call : calls) { - if (call->type != Type::unreachable) { - call->type = newType; + if (newType != func->getResults()) { + lub.updateNulls(); + func->setResults(newType); + for (auto* call : calls) { + if (call->type != Type::unreachable) { + call->type = newType; + } } + return true; } - lub.updateNulls(); - return true; + return false; } }; |