|
| 1 | +/* |
| 2 | + * Copyright 2025 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_ir_glbs_h |
| 18 | +#define wasm_ir_glbs_h |
| 19 | + |
| 20 | +#include "ir/module-utils.h" |
| 21 | +#include "wasm.h" |
| 22 | + |
| 23 | +namespace wasm { |
| 24 | + |
| 25 | +// |
| 26 | +// Similar to LUBFinder, but for GLBs. |
| 27 | +// |
| 28 | +struct GLBFinder { |
| 29 | + GLBFinder() {} |
| 30 | + |
| 31 | + GLBFinder(Type initialType) { note(initialType); } |
| 32 | + |
| 33 | + // Note another type to take into account in the GLB. |
| 34 | + void note(Type type) { |
| 35 | + // We only compute GLBs of concrete types in our IR. |
| 36 | + assert(type != Type::none); |
| 37 | + |
| 38 | + if (type != Type::unreachable) { |
| 39 | + if (glb == Type::unreachable) { |
| 40 | + // This is the first thing we see. |
| 41 | + glb = type; |
| 42 | + } else { |
| 43 | + glb = Type::getGreatestLowerBound(glb, type); |
| 44 | + // If the result is unreachable, when neither of the inputs was, then we |
| 45 | + // have combined things from different hierarchies, which we do not |
| 46 | + // allow here: We focus on computing GLBs for concrete places in our IR. |
| 47 | + assert(glb != Type::unreachable); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Returns whether we noted any (reachable) value. |
| 53 | + bool noted() { return glb != Type::unreachable; } |
| 54 | + |
| 55 | + // Returns the GLB. |
| 56 | + Type getGLB() { return glb; } |
| 57 | + |
| 58 | + // Combines the information in another GLBFinder into this one, and returns |
| 59 | + // whether we changed anything. |
| 60 | + bool combine(const GLBFinder& other) { |
| 61 | + // Check if the GLB was changed. |
| 62 | + auto old = glb; |
| 63 | + note(other.glb); |
| 64 | + return old != glb; |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + // The greatest lower bound. As we go this always contains the latest value |
| 69 | + // based on everything we've seen so far. |
| 70 | + Type glb = Type::unreachable; |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace wasm |
| 74 | + |
| 75 | +#endif // wasm_ir_glbs_h |
0 commit comments