summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp67
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) {