diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 916991ecb..ed99705ae 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1963,7 +1963,7 @@ struct OptimizeInstructions } // Check whether the cast will definitely succeed. - if (HeapType::isSubType(curr->ref->type.getHeapType(), intendedType)) { + if (Type::isSubType(curr->ref->type, curr->type)) { replaceCurrent(curr->ref); // We must refinalize here, as we may be returning a more specific @@ -1987,6 +1987,24 @@ struct OptimizeInstructions return; } + // The cast will not definitely succeed, but perhaps the heap type part of + // the cast will, at least. That would leave only nullability as an issue, + // that is, this means that the input ref is nullable but we are casting to + // non-null. + if (HeapType::isSubType(curr->ref->type.getHeapType(), intendedType)) { + assert(curr->ref->type.isNullable()); + assert(curr->type.isNonNullable()); + + // Given the heap type will cast ok, all we need to do is check for a null + // here. + // + // As above, we must refinalize as we may now be emitting a more refined + // type (specifically a more refined heap type). + replaceCurrent(Builder(*getModule()).makeRefAs(RefAsNonNull, curr->ref)); + refinalize = true; + return; + } + // Repeated identical ref.cast operations are unnecessary. First, find the // immediate child cast, if there is one. // TODO: Look even further through incompatible casts? |