diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/gc-type-utils.h | 22 | ||||
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 7 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/ir/gc-type-utils.h b/src/ir/gc-type-utils.h index 1fecca22e..53cc6feb2 100644 --- a/src/ir/gc-type-utils.h +++ b/src/ir/gc-type-utils.h @@ -105,6 +105,28 @@ inline EvaluationResult evaluateCastCheck(Type refType, Type castType) { return Unknown; } +// Given a reference and a field index, return the field for it, if one exists. +// One may not exist if the reference is unreachable, or a bottom type. +// +// The index is optional as it does not matter for an array. +// +// TODO: use in more places +inline std::optional<Field> getField(HeapType type, Index index = 0) { + if (type.isStruct()) { + return type.getStruct().fields[index]; + } else if (type.isArray()) { + return type.getArray().element; + } + return {}; +} + +inline std::optional<Field> getField(Type type, Index index = 0) { + if (type.isRef()) { + return getField(type.getHeapType(), index); + } + return {}; +} + } // namespace wasm::GCTypeUtils #endif // wasm_ir_gc_type_utils_h diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index e71519012..e6b19a6de 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1941,9 +1941,10 @@ struct OptimizeInstructions return; } - if (curr->ref->type != Type::unreachable && curr->value->type.isInteger()) { - auto element = curr->ref->type.getHeapType().getArray().element; - optimizeStoredValue(curr->value, element.getByteSize()); + if (curr->value->type.isInteger()) { + if (auto field = GCTypeUtils::getField(curr->ref->type)) { + optimizeStoredValue(curr->value, field->getByteSize()); + } } } |