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.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index c6f1b0f3b..7c450ac06 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -907,6 +907,46 @@ struct OptimizeInstructions
}
}
+ if (curr->op == ExtendUInt32 || curr->op == ExtendSInt32) {
+ if (auto* load = curr->value->dynCast<Load>()) {
+ // i64.extend_i32_s(i32.load(_8|_16)(_u|_s)(x)) =>
+ // i64.load(_8|_16|_32)(_u|_s)(x)
+ //
+ // i64.extend_i32_u(i32.load(_8|_16)(_u|_s)(x)) =>
+ // i64.load(_8|_16|_32)(_u|_s)(x)
+ //
+ // but we can't do this in following cases:
+ //
+ // i64.extend_i32_u(i32.load8_s(x))
+ // i64.extend_i32_u(i32.load16_s(x))
+ //
+ // this mixed sign/zero extensions can't represent in single
+ // signed or unsigned 64-bit load operation. For example if `load8_s(x)`
+ // return i8(-1) (0xFF) than sign extended result will be
+ // i32(-1) (0xFFFFFFFF) and with zero extension to i64 we got
+ // finally 0x00000000FFFFFFFF. However with `i64.load8_s` in this
+ // situation we got `i64(-1)` (all ones) and with `i64.load8_u` it
+ // will be 0x00000000000000FF.
+ //
+ // Another limitation is atomics which only have unsigned loads.
+ // So we also avoid this only case:
+ //
+ // i64.extend_i32_s(i32.atomic.load(x))
+
+ // Special case for i32.load. In this case signedness depends on
+ // extend operation.
+ bool willBeSigned = curr->op == ExtendSInt32 && load->bytes == 4;
+ if (!(curr->op == ExtendUInt32 && load->bytes <= 2 && load->signed_) &&
+ !(willBeSigned && load->isAtomic)) {
+ if (willBeSigned) {
+ load->signed_ = true;
+ }
+ load->type = Type::i64;
+ return replaceCurrent(load);
+ }
+ }
+ }
+
if (Abstract::hasAnyReinterpret(curr->op)) {
// i32.reinterpret_f32(f32.reinterpret_i32(x)) => x
// i64.reinterpret_f64(f64.reinterpret_i64(x)) => x