|
| 1 | +/* |
| 2 | + * Copyright 2017 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_ast_effects_h |
| 18 | +#define wasm_ast_effects_h |
| 19 | + |
| 20 | +namespace wasm { |
| 21 | + |
| 22 | +// Look for side effects, including control flow |
| 23 | +// TODO: optimize |
| 24 | + |
| 25 | +struct EffectAnalyzer : public PostWalker<EffectAnalyzer> { |
| 26 | + EffectAnalyzer(PassOptions& passOptions, Expression *ast = nullptr) { |
| 27 | + ignoreImplicitTraps = passOptions.ignoreImplicitTraps; |
| 28 | + debugInfo = passOptions.debugInfo; |
| 29 | + if (ast) analyze(ast); |
| 30 | + } |
| 31 | + |
| 32 | + bool ignoreImplicitTraps; |
| 33 | + bool debugInfo; |
| 34 | + |
| 35 | + void analyze(Expression *ast) { |
| 36 | + breakNames.clear(); |
| 37 | + walk(ast); |
| 38 | + // if we are left with breaks, they are external |
| 39 | + if (breakNames.size() > 0) branches = true; |
| 40 | + } |
| 41 | + |
| 42 | + bool branches = false; // branches out of this expression |
| 43 | + bool calls = false; |
| 44 | + std::set<Index> localsRead; |
| 45 | + std::set<Index> localsWritten; |
| 46 | + std::set<Name> globalsRead; |
| 47 | + std::set<Name> globalsWritten; |
| 48 | + bool readsMemory = false; |
| 49 | + bool writesMemory = false; |
| 50 | + bool implicitTrap = false; // a load or div/rem, which may trap. we ignore trap |
| 51 | + // differences, so it is ok to reorder these, and we |
| 52 | + // also allow reordering them with other effects |
| 53 | + // (so a trap may occur later or earlier, if it is |
| 54 | + // going to occur anyhow), but we can't remove them, |
| 55 | + // they count as side effects |
| 56 | + |
| 57 | + bool accessesLocal() { return localsRead.size() + localsWritten.size() > 0; } |
| 58 | + bool accessesGlobal() { return globalsRead.size() + globalsWritten.size() > 0; } |
| 59 | + bool accessesMemory() { return calls || readsMemory || writesMemory; } |
| 60 | + bool hasSideEffects() { return calls || localsWritten.size() > 0 || writesMemory || branches || globalsWritten.size() > 0 || implicitTrap; } |
| 61 | + bool hasAnything() { return branches || calls || accessesLocal() || readsMemory || writesMemory || accessesGlobal() || implicitTrap; } |
| 62 | + |
| 63 | + // checks if these effects would invalidate another set (e.g., if we write, we invalidate someone that reads, they can't be moved past us) |
| 64 | + bool invalidates(EffectAnalyzer& other) { |
| 65 | + if (branches || other.branches |
| 66 | + || ((writesMemory || calls) && other.accessesMemory()) |
| 67 | + || (accessesMemory() && (other.writesMemory || other.calls))) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + for (auto local : localsWritten) { |
| 71 | + if (other.localsWritten.count(local) || other.localsRead.count(local)) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + for (auto local : localsRead) { |
| 76 | + if (other.localsWritten.count(local)) return true; |
| 77 | + } |
| 78 | + if ((accessesGlobal() && other.calls) || (other.accessesGlobal() && calls)) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + for (auto global : globalsWritten) { |
| 82 | + if (other.globalsWritten.count(global) || other.globalsRead.count(global)) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + for (auto global : globalsRead) { |
| 87 | + if (other.globalsWritten.count(global)) return true; |
| 88 | + } |
| 89 | + // we are ok to reorder implicit traps, but not conditionalize them |
| 90 | + if ((implicitTrap && other.branches) || (other.implicitTrap && branches)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + void mergeIn(EffectAnalyzer& other) { |
| 97 | + branches = branches || other.branches; |
| 98 | + calls = calls || other.calls; |
| 99 | + readsMemory = readsMemory || other.readsMemory; |
| 100 | + writesMemory = writesMemory || other.writesMemory; |
| 101 | + for (auto i : other.localsRead) localsRead.insert(i); |
| 102 | + for (auto i : other.localsWritten) localsWritten.insert(i); |
| 103 | + for (auto i : other.globalsRead) globalsRead.insert(i); |
| 104 | + for (auto i : other.globalsWritten) globalsWritten.insert(i); |
| 105 | + } |
| 106 | + |
| 107 | + // the checks above happen after the node's children were processed, in the order of execution |
| 108 | + // we must also check for control flow that happens before the children, i.e., loops |
| 109 | + bool checkPre(Expression* curr) { |
| 110 | + if (curr->is<Loop>()) { |
| 111 | + branches = true; |
| 112 | + return true; |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + bool checkPost(Expression* curr) { |
| 118 | + visit(curr); |
| 119 | + if (curr->is<Loop>()) { |
| 120 | + branches = true; |
| 121 | + } |
| 122 | + return hasAnything(); |
| 123 | + } |
| 124 | + |
| 125 | + std::set<Name> breakNames; |
| 126 | + |
| 127 | + void visitBreak(Break *curr) { |
| 128 | + breakNames.insert(curr->name); |
| 129 | + } |
| 130 | + void visitSwitch(Switch *curr) { |
| 131 | + for (auto name : curr->targets) { |
| 132 | + breakNames.insert(name); |
| 133 | + } |
| 134 | + breakNames.insert(curr->default_); |
| 135 | + } |
| 136 | + void visitBlock(Block* curr) { |
| 137 | + if (curr->name.is()) breakNames.erase(curr->name); // these were internal breaks |
| 138 | + } |
| 139 | + void visitLoop(Loop* curr) { |
| 140 | + if (curr->name.is()) breakNames.erase(curr->name); // these were internal breaks |
| 141 | + } |
| 142 | + |
| 143 | + void visitCall(Call *curr) { calls = true; } |
| 144 | + void visitCallImport(CallImport *curr) { |
| 145 | + calls = true; |
| 146 | + if (debugInfo) { |
| 147 | + // debugInfo call imports must be preserved very strongly, do not |
| 148 | + // move code around them |
| 149 | + branches = true; // ! |
| 150 | + } |
| 151 | + } |
| 152 | + void visitCallIndirect(CallIndirect *curr) { calls = true; } |
| 153 | + void visitGetLocal(GetLocal *curr) { |
| 154 | + localsRead.insert(curr->index); |
| 155 | + } |
| 156 | + void visitSetLocal(SetLocal *curr) { |
| 157 | + localsWritten.insert(curr->index); |
| 158 | + } |
| 159 | + void visitGetGlobal(GetGlobal *curr) { |
| 160 | + globalsRead.insert(curr->name); |
| 161 | + } |
| 162 | + void visitSetGlobal(SetGlobal *curr) { |
| 163 | + globalsWritten.insert(curr->name); |
| 164 | + } |
| 165 | + void visitLoad(Load *curr) { |
| 166 | + readsMemory = true; |
| 167 | + if (!ignoreImplicitTraps) implicitTrap = true; |
| 168 | + } |
| 169 | + void visitStore(Store *curr) { |
| 170 | + writesMemory = true; |
| 171 | + if (!ignoreImplicitTraps) implicitTrap = true; |
| 172 | + } |
| 173 | + void visitUnary(Unary *curr) { |
| 174 | + if (!ignoreImplicitTraps) { |
| 175 | + switch (curr->op) { |
| 176 | + case TruncSFloat32ToInt32: |
| 177 | + case TruncSFloat32ToInt64: |
| 178 | + case TruncUFloat32ToInt32: |
| 179 | + case TruncUFloat32ToInt64: |
| 180 | + case TruncSFloat64ToInt32: |
| 181 | + case TruncSFloat64ToInt64: |
| 182 | + case TruncUFloat64ToInt32: |
| 183 | + case TruncUFloat64ToInt64: { |
| 184 | + implicitTrap = true; |
| 185 | + } |
| 186 | + default: {} |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + void visitBinary(Binary *curr) { |
| 191 | + if (!ignoreImplicitTraps) { |
| 192 | + switch (curr->op) { |
| 193 | + case DivSInt32: |
| 194 | + case DivUInt32: |
| 195 | + case RemSInt32: |
| 196 | + case RemUInt32: |
| 197 | + case DivSInt64: |
| 198 | + case DivUInt64: |
| 199 | + case RemSInt64: |
| 200 | + case RemUInt64: { |
| 201 | + implicitTrap = true; |
| 202 | + } |
| 203 | + default: {} |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + void visitReturn(Return *curr) { branches = true; } |
| 208 | + void visitHost(Host *curr) { calls = true; } |
| 209 | + void visitUnreachable(Unreachable *curr) { branches = true; } |
| 210 | +}; |
| 211 | + |
| 212 | +} // namespace wasm |
| 213 | + |
| 214 | +#endif // wasm_ast_effects_h |
| 215 | + |
0 commit comments