diff options
author | Alon Zakai (kripken) <alonzakai@gmail.com> | 2017-07-30 11:07:51 -0700 |
---|---|---|
committer | Alon Zakai (kripken) <alonzakai@gmail.com> | 2017-07-30 11:07:51 -0700 |
commit | 6cdffee098cb891c7309eb372aea63c0baa7a2c5 (patch) | |
tree | dbab872c51cfec5bd482030199bccdf1f2c2dde9 /src/passes/OptimizeInstructions.cpp | |
parent | 56e49752b4258b89660825f2970a7e55067d7122 (diff) | |
download | binaryen-6cdffee098cb891c7309eb372aea63c0baa7a2c5.tar.gz binaryen-6cdffee098cb891c7309eb372aea63c0baa7a2c5.tar.bz2 binaryen-6cdffee098cb891c7309eb372aea63c0baa7a2c5.zip |
fix optimizing two shifts into one; if the number of effective shifts overflows, it is not vali to just add them
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 424cbcc67..60222c3cd 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -29,6 +29,7 @@ #include <ast/effects.h> #include <ast/manipulation.h> #include <ast/properties.h> +#include <ast/literal-utils.h> namespace wasm { @@ -533,9 +534,16 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, } else if (left->op == OrInt32) { leftRight->value = leftRight->value.or_(right->value); return left; - } else if (left->op == ShlInt32 || left->op == ShrUInt32 || left->op == ShrSInt32) { - leftRight->value = leftRight->value.add(right->value); - return left; + } else if (left->op == ShlInt32 || left->op == ShrUInt32 || left->op == ShrSInt32 || + left->op == ShlInt64 || left->op == ShrUInt64 || left->op == ShrSInt64) { + // shifts only use an effective amount from the constant, so adding must + // be done carefully + auto total = Bits::getEffectiveShifts(leftRight) + Bits::getEffectiveShifts(right); + if (total == Bits::getEffectiveShifts(total, left->type)) { + // no overflow, we can do this + leftRight->value = LiteralUtils::makeLiteralFromInt32(total, left->type); + return left; + } // TODO: handle overflows } } } |