diff options
author | Max Graey <maxgraey@gmail.com> | 2022-07-26 18:42:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-26 08:42:45 -0700 |
commit | e95d6592d5a89485200f4822198ec13993a3e302 (patch) | |
tree | 0472f9fa904a7b4a8c5257a1222d095b3cac36c5 /src/passes/OptimizeInstructions.cpp | |
parent | a1464d9e004a43ef6d3f2fa83444e7906d9b620f (diff) | |
download | binaryen-e95d6592d5a89485200f4822198ec13993a3e302.tar.gz binaryen-e95d6592d5a89485200f4822198ec13993a3e302.tar.bz2 binaryen-e95d6592d5a89485200f4822198ec13993a3e302.zip |
[OptimizeInstructions] Add folding for mixed left shift and mul with constants on RHS (#4808)
(x * C1) << C2 -> x * (C1 << C2)
(x << C1) * C2 -> x * (C2 << C1)
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 9ab8e20d9..63f47abd1 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -716,6 +716,23 @@ struct OptimizeInstructions } } } + if (left->op == Abstract::getBinary(left->type, Abstract::Shl) && + curr->op == Abstract::getBinary(curr->type, Abstract::Mul)) { + if (auto* leftRight = left->right->dynCast<Const>()) { + left->op = Abstract::getBinary(left->type, Abstract::Mul); + // (x << C1) * C2 -> x * (C2 << C1) + leftRight->value = right->value.shl(leftRight->value); + return replaceCurrent(left); + } + } + if (left->op == Abstract::getBinary(left->type, Abstract::Mul) && + curr->op == Abstract::getBinary(curr->type, Abstract::Shl)) { + if (auto* leftRight = left->right->dynCast<Const>()) { + // (x * C1) << C2 -> x * (C1 << C2) + leftRight->value = leftRight->value.shl(right->value); + return replaceCurrent(left); + } + } } if (right->type == Type::i32) { BinaryOp op; |