From f01fd8cad1cb1a6a80ef12bcfc19502bd51ddf46 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 22 Jul 2021 21:16:45 +0300 Subject: [Optimize Instructions] Simplify zero/sign extentions (special case) (#4009) For signed or unsigned extension to 64-bits after lowering from partially filled 64-bit arguments: ```rust i64.extend_i32_u(i32.wrap_i64(x)) => x // where maxBits(x) <= 32 i64.extend_i32_s(i32.wrap_i64(x)) => x // where maxBits(x) <= 31 ``` --- src/passes/OptimizeInstructions.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 31ed9348a..2e6a2d48d 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -841,19 +841,35 @@ struct OptimizeInstructions } } { - Unary* inner; + // i64.extend_i32_s(i32.wrap_i64(x)) => x + // where maxBits(x) <= 31 + // + // i64.extend_i32_u(i32.wrap_i64(x)) => x + // where maxBits(x) <= 32 Expression* x; - if (getModule()->features.hasSignExt()) { - // i64.extend_i32_s(i32.wrap_i64(x)) => i64.extend32_s(x) - if (matches(curr, - unary(ExtendSInt32, unary(&inner, WrapInt64, any(&x))))) { - inner->op = ExtendS32Int64; - inner->type = Type::i64; - inner->value = x; - return replaceCurrent(inner); + UnaryOp unaryOp; + if (matches(curr, unary(&unaryOp, unary(WrapInt64, any(&x))))) { + if (unaryOp == ExtendSInt32 || unaryOp == ExtendUInt32) { + auto maxBits = Bits::getMaxBits(x, this); + if ((unaryOp == ExtendSInt32 && maxBits <= 31) || + (unaryOp == ExtendUInt32 && maxBits <= 32)) { + return replaceCurrent(x); + } } } } + if (getModule()->features.hasSignExt()) { + // i64.extend_i32_s(i32.wrap_i64(x)) => i64.extend32_s(x) + Unary* inner; + Expression* x; + if (matches(curr, + unary(ExtendSInt32, unary(&inner, WrapInt64, any(&x))))) { + inner->op = ExtendS32Int64; + inner->type = Type::i64; + inner->value = x; + return replaceCurrent(inner); + } + } } if (Abstract::hasAnyReinterpret(curr->op)) { -- cgit v1.2.3