diff options
author | Max Graey <maxgraey@gmail.com> | 2021-11-12 23:58:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-12 13:58:05 -0800 |
commit | 5597d1c03e25684b999035e77578db97e91c16eb (patch) | |
tree | b83b598162fe94156044fac56ba1dc079ec512e0 /src/passes/OptimizeInstructions.cpp | |
parent | 25a2b5fc420023a94f0aa3c71b1fb915be8a58d1 (diff) | |
download | binaryen-5597d1c03e25684b999035e77578db97e91c16eb.tar.gz binaryen-5597d1c03e25684b999035e77578db97e91c16eb.tar.bz2 binaryen-5597d1c03e25684b999035e77578db97e91c16eb.zip |
[OptimizeInstructions] Combine extend into i64 and 32-bit load operations (#4307)
i64.extend_i32_u(i32.load8_u(x)) -> i64.load8_u(x)
i64.extend_i32_u(i32.load16_u(x)) -> i64.load16_u(x)
i64.extend_i32_s(i32.load8_u(x)) -> i64.load8_u(x)
i64.extend_i32_s(i32.load16_u(x)) -> i64.load16_u(x)
i64.extend_i32_s(i32.load8_s(x)) -> i64.load8_s(x)
i64.extend_i32_s(i32.load16_s(x)) -> i64.load16_s(x)
i64.extend_i32_u(i32.load(x))) -> i64.load32_u(x)
i64.extend_i32_s(i32.load(x))) -> i64.load32_s(x)
don't apply to
i64.extend_i32_u(i32.load8_s(x)) -> skip
i64.extend_i32_u(i32.load16_s(x)) -> skip
i64.extend_i32_s(i32.atomic.load(x)) -> skip
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 40 |
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 |