diff options
author | Alon Zakai <alonzakai@gmail.com> | 2018-01-17 21:20:53 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-17 21:20:53 -0800 |
commit | 14cb0c01ee22fbcd2923db0502e11b1fc51df05d (patch) | |
tree | eb4c6c854a803482c2891e7235ea8700886d289c /src/passes/OptimizeInstructions.cpp | |
parent | 692069c6eef63754c27e815fd948fea6185d7619 (diff) | |
download | binaryen-14cb0c01ee22fbcd2923db0502e11b1fc51df05d.tar.gz binaryen-14cb0c01ee22fbcd2923db0502e11b1fc51df05d.tar.bz2 binaryen-14cb0c01ee22fbcd2923db0502e11b1fc51df05d.zip |
optimize out 0-x, a zero only used to negate an int, when possible (#1365)
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 06f3d5b02..efea99247 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -506,7 +506,43 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, } } // note that both left and right may be consts, but then we let precompute compute the constant result - } else if (binary->op == AddInt32 || binary->op == SubInt32) { + } else if (binary->op == AddInt32) { + // try to get rid of (0 - ..), that is, a zero only used to negate an + // int. an add of a subtract can be flipped in order to remove it: + // (i32.add + // (i32.sub + // (i32.const 0) + // X + // ) + // Y + // ) + // => + // (i32.sub + // Y + // X + // ) + if (auto* sub = binary->left->dynCast<Binary>()) { + if (sub->op == SubInt32) { + if (auto* subZero = sub->left->dynCast<Const>()) { + if (subZero->value.geti32() == 0) { + sub->left = binary->right; + return sub; + } + } + } + } + if (auto* sub = binary->right->dynCast<Binary>()) { + if (sub->op == SubInt32) { + if (auto* subZero = sub->left->dynCast<Const>()) { + if (subZero->value.geti32() == 0) { + sub->left = binary->left; + return sub; + } + } + } + } + return optimizeAddedConstants(binary); + } else if (binary->op == SubInt32) { return optimizeAddedConstants(binary); } // a bunch of operations on a constant right side can be simplified |