1 /*
2 * Copyright 2018 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 #ifndef wasm_passes_opt_utils_h
18 #define wasm_passes_opt_utils_h
19
20 #include <functional>
21 #include <unordered_set>
22
23 #include <pass.h>
24 #include <wasm.h>
25
26 namespace wasm {
27
28 namespace OptUtils {
29
30 // Run useful optimizations after inlining new code into a set
31 // of functions.
optimizeAfterInlining(std::unordered_set<Function * > & funcs,Module * module,PassRunner * parentRunner)32 inline void optimizeAfterInlining(std::unordered_set<Function*>& funcs,
33 Module* module,
34 PassRunner* parentRunner) {
35 // save the full list of functions on the side
36 std::vector<std::unique_ptr<Function>> all;
37 all.swap(module->functions);
38 module->updateMaps();
39 for (auto& func : funcs) {
40 module->addFunction(func);
41 }
42 PassRunner runner(module, parentRunner->options);
43 runner.setIsNested(true);
44 runner.setValidateGlobally(false); // not a full valid module
45 // this is especially useful after inlining
46 runner.add("precompute-propagate");
47 runner.addDefaultFunctionOptimizationPasses(); // do all the usual stuff
48 runner.run();
49 // restore all the funcs
50 for (auto& func : module->functions) {
51 func.release();
52 }
53 all.swap(module->functions);
54 module->updateMaps();
55 }
56
57 struct FunctionRefReplacer
58 : public WalkerPass<PostWalker<FunctionRefReplacer>> {
isFunctionParallelFunctionRefReplacer59 bool isFunctionParallel() override { return true; }
60
61 using MaybeReplace = std::function<void(Name&)>;
62
FunctionRefReplacerFunctionRefReplacer63 FunctionRefReplacer(MaybeReplace maybeReplace) : maybeReplace(maybeReplace) {}
64
createFunctionRefReplacer65 FunctionRefReplacer* create() override {
66 return new FunctionRefReplacer(maybeReplace);
67 }
68
visitCallFunctionRefReplacer69 void visitCall(Call* curr) { maybeReplace(curr->target); }
70
visitRefFuncFunctionRefReplacer71 void visitRefFunc(RefFunc* curr) { maybeReplace(curr->func); }
72
73 private:
74 MaybeReplace maybeReplace;
75 };
76
replaceFunctions(PassRunner * runner,Module & module,const std::map<Name,Name> & replacements)77 inline void replaceFunctions(PassRunner* runner,
78 Module& module,
79 const std::map<Name, Name>& replacements) {
80 auto maybeReplace = [&](Name& name) {
81 auto iter = replacements.find(name);
82 if (iter != replacements.end()) {
83 name = iter->second;
84 }
85 };
86 // replace direct calls
87 FunctionRefReplacer(maybeReplace).run(runner, &module);
88 // replace in table
89 for (auto& segment : module.table.segments) {
90 for (auto& name : segment.data) {
91 maybeReplace(name);
92 }
93 }
94 // replace in start
95 if (module.start.is()) {
96 maybeReplace(module.start);
97 }
98 // replace in exports
99 for (auto& exp : module.exports) {
100 if (exp->kind == ExternalKind::Function) {
101 maybeReplace(exp->value);
102 }
103 }
104 }
105
106 } // namespace OptUtils
107 } // namespace wasm
108
109 #endif // wasm_passes_opt_utils_h
110