|
| 1 | +/* |
| 2 | + * Copyright 2016 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// Stops using return values from set_local and store nodes. |
| 19 | +// |
| 20 | + |
| 21 | +#include <wasm.h> |
| 22 | +#include <pass.h> |
| 23 | +#include <ast_utils.h> |
| 24 | +#include <wasm-builder.h> |
| 25 | + |
| 26 | +namespace wasm { |
| 27 | + |
| 28 | +struct DropReturnValues : public WalkerPass<PostWalker<DropReturnValues, Visitor<DropReturnValues>>> { |
| 29 | + bool isFunctionParallel() { return true; } |
| 30 | + |
| 31 | + std::vector<Expression*> expressionStack; |
| 32 | + |
| 33 | + void visitSetLocal(SetLocal* curr) { |
| 34 | + if (ExpressionAnalyzer::isResultUsed(expressionStack, getFunction())) { |
| 35 | + Builder builder(*getModule()); |
| 36 | + replaceCurrent(builder.makeSequence( |
| 37 | + curr, |
| 38 | + builder.makeGetLocal(curr->index, curr->type) |
| 39 | + )); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + void visitStore(Store* curr) { |
| 44 | + if (ExpressionAnalyzer::isResultUsed(expressionStack, getFunction())) { |
| 45 | + Index index = getFunction()->getNumLocals(); |
| 46 | + getFunction()->vars.emplace_back(curr->type); |
| 47 | + Builder builder(*getModule()); |
| 48 | + replaceCurrent(builder.makeSequence( |
| 49 | + builder.makeSequence( |
| 50 | + builder.makeSetLocal(index, curr->value), |
| 51 | + curr |
| 52 | + ), |
| 53 | + builder.makeGetLocal(index, curr->type) |
| 54 | + )); |
| 55 | + curr->value = builder.makeGetLocal(index, curr->type); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static void visitPre(DropReturnValues* self, Expression** currp) { |
| 60 | + self->expressionStack.push_back(*currp); |
| 61 | + } |
| 62 | + |
| 63 | + static void visitPost(DropReturnValues* self, Expression** currp) { |
| 64 | + self->expressionStack.pop_back(); |
| 65 | + } |
| 66 | + |
| 67 | + static void scan(DropReturnValues* self, Expression** currp) { |
| 68 | + self->pushTask(visitPost, currp); |
| 69 | + |
| 70 | + WalkerPass<PostWalker<DropReturnValues, Visitor<DropReturnValues>>>::scan(self, currp); |
| 71 | + |
| 72 | + self->pushTask(visitPre, currp); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +static RegisterPass<DropReturnValues> registerPass("drop-return-values", "stops relying on return values from set_local and store"); |
| 77 | + |
| 78 | +} // namespace wasm |
| 79 | + |
0 commit comments