From ed97c68294a8d95c57972eae22906cc0bea1aae8 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 22 Jul 2021 00:10:48 +0300 Subject: [Optimize Instructions] Combine reinterprets, loads and stores (#4006) Fixes #3973 Loads: 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) Stores: 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) Also optimize reinterprets that are undone: 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 --- src/ir/abstract.h | 5 +++++ src/passes/OptimizeInstructions.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'src') 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()) { + 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()) { + 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()) { // 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; } } } -- cgit v1.2.3