summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index ec714c1e1..097e0299a 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -2037,20 +2037,29 @@ struct OptimizeInstructions
Builder builder(*getModule());
+ if (curr->ref->type.isNull()) {
+ // The input is null, so we know whether this will succeed or fail.
+ int32_t result = curr->castType.isNullable() ? 1 : 0;
+ replaceCurrent(builder.makeBlock(
+ {builder.makeDrop(curr->ref), builder.makeConst(int32_t(result))}));
+ return;
+ }
+
auto refType = curr->ref->type.getHeapType();
- auto intendedType = curr->intendedType;
+ auto intendedType = curr->castType.getHeapType();
// See above in RefCast.
- if (!canBeCastTo(refType, intendedType)) {
+ if (!canBeCastTo(refType, intendedType) &&
+ (curr->castType.isNonNullable() || curr->ref->type.isNonNullable())) {
// This test cannot succeed, and will definitely return 0.
replaceCurrent(builder.makeSequence(builder.makeDrop(curr->ref),
builder.makeConst(int32_t(0))));
return;
}
- if (curr->ref->type.isNonNullable() &&
- HeapType::isSubType(refType, intendedType)) {
- // This static test will definitely succeed.
+ if (HeapType::isSubType(refType, intendedType) &&
+ (curr->castType.isNullable() || curr->ref->type.isNonNullable())) {
+ // This test will definitely succeed and return 1.
replaceCurrent(builder.makeBlock(
{builder.makeDrop(curr->ref), builder.makeConst(int32_t(1))}));
return;