|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | +// Simplify and optimize globals and their use. |
| 19 | +// |
| 20 | +// * Turns never-written and unwritable (not imported or exported) |
| 21 | +// globals immutable. |
| 22 | +// * If an immutable global is a copy of another, use the earlier one, |
| 23 | +// to allow removal of the copies later. |
| 24 | +// |
| 25 | + |
| 26 | +#include <atomic> |
| 27 | + |
| 28 | +#include "pass.h" |
| 29 | +#include "wasm.h" |
| 30 | + |
| 31 | +namespace wasm { |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +struct GlobalInfo { |
| 36 | + bool imported = false; |
| 37 | + bool exported = false; |
| 38 | + std::atomic<bool> written; |
| 39 | +}; |
| 40 | + |
| 41 | +using GlobalInfoMap = std::map<Name, GlobalInfo>; |
| 42 | + |
| 43 | +struct GlobalUseScanner : public WalkerPass<PostWalker<GlobalUseScanner>> { |
| 44 | + bool isFunctionParallel() override { return true; } |
| 45 | + |
| 46 | + GlobalUseScanner(GlobalInfoMap* infos) : infos(infos) {} |
| 47 | + |
| 48 | + GlobalUseScanner* create() override { return new GlobalUseScanner(infos); } |
| 49 | + |
| 50 | + void visitSetGlobal(SetGlobal* curr) { (*infos)[curr->name].written = true; } |
| 51 | + |
| 52 | +private: |
| 53 | + GlobalInfoMap* infos; |
| 54 | +}; |
| 55 | + |
| 56 | +using NameNameMap = std::map<Name, Name>; |
| 57 | + |
| 58 | +struct GlobalUseModifier : public WalkerPass<PostWalker<GlobalUseModifier>> { |
| 59 | + bool isFunctionParallel() override { return true; } |
| 60 | + |
| 61 | + GlobalUseModifier(NameNameMap* copiedParentMap) |
| 62 | + : copiedParentMap(copiedParentMap) {} |
| 63 | + |
| 64 | + GlobalUseModifier* create() override { |
| 65 | + return new GlobalUseModifier(copiedParentMap); |
| 66 | + } |
| 67 | + |
| 68 | + void visitGetGlobal(GetGlobal* curr) { |
| 69 | + auto iter = copiedParentMap->find(curr->name); |
| 70 | + if (iter != copiedParentMap->end()) { |
| 71 | + curr->name = iter->second; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +private: |
| 76 | + NameNameMap* copiedParentMap; |
| 77 | +}; |
| 78 | + |
| 79 | +} // anonymous namespace |
| 80 | + |
| 81 | +struct SimplifyGlobals : public Pass { |
| 82 | + void run(PassRunner* runner, Module* module) override { |
| 83 | + // First, find out all the relevant info. |
| 84 | + GlobalInfoMap map; |
| 85 | + for (auto& global : module->globals) { |
| 86 | + auto& info = map[global->name]; |
| 87 | + if (global->imported()) { |
| 88 | + info.imported = true; |
| 89 | + } |
| 90 | + } |
| 91 | + for (auto& ex : module->exports) { |
| 92 | + if (ex->kind == ExternalKind::Global) { |
| 93 | + map[ex->value].exported = true; |
| 94 | + } |
| 95 | + } |
| 96 | + { |
| 97 | + PassRunner subRunner(module, runner->options); |
| 98 | + subRunner.add<GlobalUseScanner>(&map); |
| 99 | + subRunner.run(); |
| 100 | + } |
| 101 | + // We now know which are immutable in practice. |
| 102 | + for (auto& global : module->globals) { |
| 103 | + auto& info = map[global->name]; |
| 104 | + if (global->mutable_ && !info.imported && !info.exported && |
| 105 | + !info.written) { |
| 106 | + global->mutable_ = false; |
| 107 | + } |
| 108 | + } |
| 109 | + // Optimize uses of immutable globals, prefer the earlier import when |
| 110 | + // there is a copy. |
| 111 | + NameNameMap copiedParentMap; |
| 112 | + for (auto& global : module->globals) { |
| 113 | + auto child = global->name; |
| 114 | + if (!global->mutable_ && !global->imported()) { |
| 115 | + if (auto* get = global->init->dynCast<GetGlobal>()) { |
| 116 | + auto parent = get->name; |
| 117 | + if (!module->getGlobal(get->name)->mutable_) { |
| 118 | + copiedParentMap[child] = parent; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if (!copiedParentMap.empty()) { |
| 124 | + // Go all the way back. |
| 125 | + for (auto& global : module->globals) { |
| 126 | + auto child = global->name; |
| 127 | + if (copiedParentMap.count(child)) { |
| 128 | + while (copiedParentMap.count(copiedParentMap[child])) { |
| 129 | + copiedParentMap[child] = copiedParentMap[copiedParentMap[child]]; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + // Apply to the gets. |
| 134 | + PassRunner subRunner(module, runner->options); |
| 135 | + subRunner.add<GlobalUseModifier>(&copiedParentMap); |
| 136 | + subRunner.run(); |
| 137 | + } |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +Pass* createSimplifyGlobalsPass() { return new SimplifyGlobals(); } |
| 142 | + |
| 143 | +} // namespace wasm |
0 commit comments