diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 13 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 56 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.wast | 16 |
3 files changed, 85 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 982f99a04..0ce1f5f95 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -826,6 +826,19 @@ struct OptimizeInstructions optimizeMemoryAccess(load->ptr, load->offset); } else if (auto* store = curr->dynCast<Store>()) { optimizeMemoryAccess(store->ptr, store->offset); + if (store->valueType.isInteger()) { + // truncates constant values during stores + // (i32|i64).store(8|16|32)(p, C) ==> + // (i32|i64).store(8|16|32)(p, C & mask) + if (auto* c = store->value->dynCast<Const>()) { + if (store->valueType == Type::i64 && store->bytes == 4) { + c->value = c->value.and_(Literal(uint64_t(0xffffffff))); + } else { + c->value = c->value.and_(Literal::makeFromInt32( + Bits::lowBitMask(store->bytes * 8), store->valueType)); + } + } + } // stores of fewer bits truncates anyhow if (auto* binary = store->value->dynCast<Binary>()) { if (binary->op == AndInt32) { diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 7eebb3867..27daa5ce1 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -379,6 +379,62 @@ (i32.const 11) (i64.const 3) ) + (i32.store8 + (i32.const 7) + (i32.const 255) + ) + (i32.store8 + (i32.const 8) + (i32.const 255) + ) + (i32.store8 + (i32.const 9) + (i32.const 0) + ) + (i32.store16 + (i32.const 10) + (i32.const 65535) + ) + (i32.store16 + (i32.const 11) + (i32.const 0) + ) + (i32.store16 + (i32.const 13) + (i32.const 65535) + ) + (i32.store + (i32.const 14) + (i32.const 65536) + ) + (i64.store8 + (i32.const 8) + (i64.const 255) + ) + (i64.store8 + (i32.const 9) + (i64.const 0) + ) + (i64.store16 + (i32.const 10) + (i64.const 65535) + ) + (i64.store16 + (i32.const 11) + (i64.const 0) + ) + (i64.store32 + (i32.const 12) + (i64.const 4294967295) + ) + (i64.store32 + (i32.const 13) + (i64.const 0) + ) + (i64.store + (i32.const 14) + (i64.const 4294967296) + ) ) (func $and-neg1 (drop diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index c2c1609ba..ad422bd98 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -358,6 +358,22 @@ (i32.store8 (i32.const 11) (i32.wrap_i64 (i64.const 1))) (i32.store16 (i32.const 11) (i32.wrap_i64 (i64.const 2))) (i32.store (i32.const 11) (i32.wrap_i64 (i64.const 3))) + ;; + (i32.store8 (i32.const 7) (i32.const -1)) ;; 255 + (i32.store8 (i32.const 8) (i32.const 255)) + (i32.store8 (i32.const 9) (i32.const 256)) ;; 0 + (i32.store16 (i32.const 10) (i32.const 65535)) + (i32.store16 (i32.const 11) (i32.const 65536)) ;; 0 + (i32.store16 (i32.const 13) (i32.const -1)) ;; 65535 + (i32.store (i32.const 14) (i32.const 65536)) + ;; + (i64.store8 (i32.const 8) (i64.const 255)) + (i64.store8 (i32.const 9) (i64.const 256)) ;; 0 + (i64.store16 (i32.const 10) (i64.const 65535)) + (i64.store16 (i32.const 11) (i64.const 65536)) ;; 0 + (i64.store32 (i32.const 12) (i64.const 4294967295)) + (i64.store32 (i32.const 13) (i64.const 4294967296)) ;; 0 + (i64.store (i32.const 14) (i64.const 4294967296)) ) (func $and-neg1 (drop (i32.and (i32.const 100) (i32.const -1))) |