diff options
-rw-r--r-- | src/ir/abstract.h | 5 | ||||
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 32 | ||||
-rw-r--r-- | test/lit/passes/optimize-instructions-atomics.wast | 20 | ||||
-rw-r--r-- | test/lit/passes/optimize-instructions.wast | 120 | ||||
-rw-r--r-- | test/wasm2js/reinterpret.2asm.js.opt | 10 |
5 files changed, 178 insertions, 9 deletions
diff --git a/src/ir/abstract.h b/src/ir/abstract.h index e5dfd57e0..2308c47ba 100644 --- a/src/ir/abstract.h +++ b/src/ir/abstract.h @@ -67,6 +67,11 @@ inline bool hasAnyShift(BinaryOp op) { op == RotRInt64; } +inline bool hasAnyReinterpret(UnaryOp op) { + return op == ReinterpretInt32 || op == ReinterpretInt64 || + op == ReinterpretFloat32 || op == ReinterpretFloat64; +} + // Provide a wasm type and an abstract op and get the concrete one. For example, // you can provide i32 and Add and receive the specific opcode for a 32-bit // addition, AddInt32. If the op does not exist, it returns Invalid. diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 59b1130d1..9f095b333 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -856,6 +856,30 @@ struct OptimizeInstructions } } + if (Abstract::hasAnyReinterpret(curr->op)) { + // i32.reinterpret_f32(f32.reinterpret_i32(x)) => x + // i64.reinterpret_f64(f64.reinterpret_i64(x)) => x + // f32.reinterpret_i32(i32.reinterpret_f32(x)) => x + // f64.reinterpret_i64(i64.reinterpret_f64(x)) => x + if (auto* inner = curr->value->dynCast<Unary>()) { + if (Abstract::hasAnyReinterpret(inner->op)) { + if (inner->value->type == curr->type) { + return replaceCurrent(inner->value); + } + } + } + // f32.reinterpret_i32(i32.load(x)) => f32.load(x) + // f64.reinterpret_i64(i64.load(x)) => f64.load(x) + // i32.reinterpret_f32(f32.load(x)) => i32.load(x) + // i64.reinterpret_f64(f64.load(x)) => i64.load(x) + if (auto* load = curr->value->dynCast<Load>()) { + if (!load->isAtomic && load->bytes == curr->type.getByteSize()) { + load->type = curr->type; + return replaceCurrent(load); + } + } + } + if (curr->op == EqZInt32) { if (auto* inner = curr->value->dynCast<Binary>()) { // Try to invert a relational operation using De Morgan's law @@ -1016,6 +1040,14 @@ struct OptimizeInstructions // instead of wrapping to 32, just store some of the bits in the i64 curr->valueType = Type::i64; curr->value = unary->value; + } else if (!curr->isAtomic && Abstract::hasAnyReinterpret(unary->op) && + curr->bytes == curr->valueType.getByteSize()) { + // f32.store(y, f32.reinterpret_i32(x)) => i32.store(y, x) + // f64.store(y, f64.reinterpret_i64(x)) => i64.store(y, x) + // i32.store(y, i32.reinterpret_f32(x)) => f32.store(y, x) + // i64.store(y, i64.reinterpret_f64(x)) => f64.store(y, x) + curr->valueType = unary->value->type; + curr->value = unary->value; } } } diff --git a/test/lit/passes/optimize-instructions-atomics.wast b/test/lit/passes/optimize-instructions-atomics.wast index d029942fe..519c8399d 100644 --- a/test/lit/passes/optimize-instructions-atomics.wast +++ b/test/lit/passes/optimize-instructions-atomics.wast @@ -31,4 +31,24 @@ ) ) ) + + ;; CHECK: (func $dont_simplify_reinterpret_atomic_load_store (param $x i32) (param $y f32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.reinterpret_i64 + ;; CHECK-NEXT: (i64.atomic.load + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i32.atomic.store + ;; CHECK-NEXT: (i32.const 8) + ;; CHECK-NEXT: (i32.reinterpret_f32 + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $dont_simplify_reinterpret_atomic_load_store (param $x i32) (param $y f32) + (drop (f64.reinterpret_i64 (i64.atomic.load (local.get $x)))) ;; skip + (i32.atomic.store (i32.const 8) (i32.reinterpret_f32 (local.get $y))) ;; skip + ) ) diff --git a/test/lit/passes/optimize-instructions.wast b/test/lit/passes/optimize-instructions.wast index 5fa703bd2..07f246b49 100644 --- a/test/lit/passes/optimize-instructions.wast +++ b/test/lit/passes/optimize-instructions.wast @@ -12387,4 +12387,124 @@ ) ) ) + + ;; f32.reinterpret_i32(i32.load(x)) => f32.load(x) + ;; f64.reinterpret_i64(i64.load(x)) => f64.load(x) + ;; i32.reinterpret_f32(f32.load(x)) => i32.load(x) + ;; i64.reinterpret_f64(f64.load(x)) => i64.load(x) + + ;; CHECK: (func $simplify_reinterpret_and_load (param $x i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.load + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.load + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.load + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.load + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.reinterpret_i32 + ;; CHECK-NEXT: (i32.load8_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.reinterpret_i64 + ;; CHECK-NEXT: (i64.load32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_reinterpret_and_load (param $x i32) + (drop (f32.reinterpret_i32 (i32.load (local.get $x)))) + (drop (f64.reinterpret_i64 (i64.load (local.get $x)))) + (drop (i32.reinterpret_f32 (f32.load (local.get $x)))) + (drop (i64.reinterpret_f64 (f64.load (local.get $x)))) + (drop (f32.reinterpret_i32 (i32.load8_s (local.get $x)))) ;; skip + (drop (f64.reinterpret_i64 (i64.load32_u (local.get $x)))) ;; skip + ) + + ;; f32.store(y, f32.reinterpret_i32(x)) => i32.store(y, x) + ;; f64.store(y, f64.reinterpret_i64(x)) => i64.store(y, x) + ;; i32.store(y, i32.reinterpret_f32(x)) => f32.store(y, x) + ;; i64.store(y, i64.reinterpret_f64(x)) => f64.store(y, x) + + ;; CHECK: (func $simplify_store_and_reinterpret (param $x i32) (param $y i64) (param $z f32) (param $w f64) + ;; CHECK-NEXT: (i32.store + ;; CHECK-NEXT: (i32.const 8) + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i64.store + ;; CHECK-NEXT: (i32.const 16) + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (f32.store + ;; CHECK-NEXT: (i32.const 24) + ;; CHECK-NEXT: (local.get $z) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (f64.store + ;; CHECK-NEXT: (i32.const 32) + ;; CHECK-NEXT: (local.get $w) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i32.store8 + ;; CHECK-NEXT: (i32.const 40) + ;; CHECK-NEXT: (i32.reinterpret_f32 + ;; CHECK-NEXT: (local.get $z) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i64.store32 + ;; CHECK-NEXT: (i32.const 44) + ;; CHECK-NEXT: (i64.reinterpret_f64 + ;; CHECK-NEXT: (local.get $w) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_store_and_reinterpret (param $x i32) (param $y i64) (param $z f32) (param $w f64) + (f32.store (i32.const 8) (f32.reinterpret_i32 (local.get $x))) + (f64.store (i32.const 16) (f64.reinterpret_i64 (local.get $y))) + (i32.store (i32.const 24) (i32.reinterpret_f32 (local.get $z))) + (i64.store (i32.const 32) (i64.reinterpret_f64 (local.get $w))) + (i32.store8 (i32.const 40) (i32.reinterpret_f32 (local.get $z))) ;; skip + (i64.store32 (i32.const 44) (i64.reinterpret_f64 (local.get $w))) ;; skip + ) + + ;; i32.reinterpret_f32(f32.reinterpret_i32(x)) => x + ;; i64.reinterpret_f64(f64.reinterpret_i64(x)) => x + ;; f32.reinterpret_i32(i32.reinterpret_f32(x)) => x + ;; f64.reinterpret_i64(i64.reinterpret_f64(x)) => x + + ;; CHECK: (func $eliminate_reinterpret_reinterpret (param $x i32) (param $y i64) (param $z f32) (param $w f64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $z) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $w) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $eliminate_reinterpret_reinterpret (param $x i32) (param $y i64) (param $z f32) (param $w f64) + (drop (i32.reinterpret_f32 (f32.reinterpret_i32 (local.get $x)))) + (drop (i64.reinterpret_f64 (f64.reinterpret_i64 (local.get $y)))) + (drop (f32.reinterpret_i32 (i32.reinterpret_f32 (local.get $z)))) + (drop (f64.reinterpret_i64 (i64.reinterpret_f64 (local.get $w)))) + ) ) diff --git a/test/wasm2js/reinterpret.2asm.js.opt b/test/wasm2js/reinterpret.2asm.js.opt index cc5d4e984..f8d27101b 100644 --- a/test/wasm2js/reinterpret.2asm.js.opt +++ b/test/wasm2js/reinterpret.2asm.js.opt @@ -21,14 +21,6 @@ f64ScratchView[0] = value; } - function wasm2js_scratch_store_f32(value) { - f32ScratchView[2] = value; - } - - function wasm2js_scratch_load_f32() { - return f32ScratchView[2]; - } - function asmFunc(env) { var Math_imul = Math.imul; var Math_fround = Math.fround; @@ -45,7 +37,7 @@ function asmFunc(env) { var infinity = Infinity; function $1($0) { $0 = $0 | 0; - return ((wasm2js_scratch_store_f32((wasm2js_scratch_store_i32(2, $0), wasm2js_scratch_load_f32())), wasm2js_scratch_load_i32(2)) | 0) == ($0 | 0) | 0; + return 1; } function legalstub$2($0, $1_1) { |