diff options
author | Max Graey <maxgraey@gmail.com> | 2022-08-19 00:31:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-18 14:31:51 -0700 |
commit | fe188d28dad8a71bc905939306d64eaba6567a40 (patch) | |
tree | 049d44fc9bfa50b04858002d423504a1eaca344f /src | |
parent | 296a63eaaabd28990d6b3acf63af3d9d21190b8d (diff) | |
download | binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.tar.gz binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.tar.bz2 binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.zip |
[OptimizeInstructions] Simplify rounding or conversions after int to float casts (#4720)
i32 -> f64 -> i32 rountripping optimizations:
```rust
i32(f64(i32(x))) -> x // i32.trunc(_sat)_f64_s(f64.convert_i32_s(x)) -> x
u32(f64(u32(x))) -> x // i32.trunc(_sat)_f64_u(f64.convert_i32_u(x)) -> x
// note assymetric signed / unsigned or unsigned / signed cases can't be simplified in similar way
```
and rounding after integer to float casts:
```rust
ceil(float(int(x))) -> float(int(x))
floor(float(int(x))) -> float(int(x))
trunc(float(int(x))) -> float(int(x))
nearest(float(int(x))) -> float(int(x))
```
where `float = f32 | f64`, `int = i32 | i64 | u32 | u64`
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 4fe26e8ee..394a7c998 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1039,6 +1039,10 @@ struct OptimizeInstructions if (auto* ret = deduplicateUnary(curr)) { return replaceCurrent(ret); } + + if (auto* ret = simplifyRoundingsAndConversions(curr)) { + return replaceCurrent(ret); + } } void visitSelect(Select* curr) { @@ -3619,6 +3623,69 @@ private: return nullptr; } + Expression* simplifyRoundingsAndConversions(Unary* curr) { + using namespace Abstract; + using namespace Match; + + switch (curr->op) { + case TruncSFloat64ToInt32: + case TruncSatSFloat64ToInt32: { + // i32 -> f64 -> i32 rountripping optimization: + // i32.trunc(_sat)_f64_s(f64.convert_i32_s(x)) ==> x + Expression* x; + if (matches(curr->value, unary(ConvertSInt32ToFloat64, any(&x)))) { + return x; + } + break; + } + case TruncUFloat64ToInt32: + case TruncSatUFloat64ToInt32: { + // u32 -> f64 -> u32 rountripping optimization: + // i32.trunc(_sat)_f64_u(f64.convert_i32_u(x)) ==> x + Expression* x; + if (matches(curr->value, unary(ConvertUInt32ToFloat64, any(&x)))) { + return x; + } + break; + } + case CeilFloat32: + case CeilFloat64: + case FloorFloat32: + case FloorFloat64: + case TruncFloat32: + case TruncFloat64: + case NearestFloat32: + case NearestFloat64: { + // Rounding after integer to float conversion may be skipped + // ceil(float(int(x))) ==> float(int(x)) + // floor(float(int(x))) ==> float(int(x)) + // trunc(float(int(x))) ==> float(int(x)) + // nearest(float(int(x))) ==> float(int(x)) + Unary* inner; + if (matches(curr->value, unary(&inner, any()))) { + switch (inner->op) { + case ConvertSInt32ToFloat32: + case ConvertSInt32ToFloat64: + case ConvertUInt32ToFloat32: + case ConvertUInt32ToFloat64: + case ConvertSInt64ToFloat32: + case ConvertSInt64ToFloat64: + case ConvertUInt64ToFloat32: + case ConvertUInt64ToFloat64: { + return inner; + } + default: { + } + } + } + break; + } + default: { + } + } + return nullptr; + } + Expression* deduplicateUnary(Unary* unaryOuter) { if (auto* unaryInner = unaryOuter->value->dynCast<Unary>()) { if (unaryInner->op == unaryOuter->op) { |