diff options
author | Alon Zakai <azakai@google.com> | 2021-03-30 08:53:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-30 08:53:43 -0700 |
commit | a3ea08bd0588099225b5f2b1fb7e6f0ad49dddcb (patch) | |
tree | 273c434c5f87c95f1ab43eccbdf708f38de439a7 /src/passes/OptimizeInstructions.cpp | |
parent | 9b38e4394034d6f25f54fc471bb9ac8812815331 (diff) | |
download | binaryen-a3ea08bd0588099225b5f2b1fb7e6f0ad49dddcb.tar.gz binaryen-a3ea08bd0588099225b5f2b1fb7e6f0ad49dddcb.tar.bz2 binaryen-a3ea08bd0588099225b5f2b1fb7e6f0ad49dddcb.zip |
OptimizeInstructions: bool(x) xor 1 ==> !bool(x) (#3741)
This was noticed by samparker on LLVM:
https://reviews.llvm.org/D99171
This is apparently a pattern LLVM emits, and doing it there helps by 1-2%
on the real-world Bullet Physics codebase. Seems worthwhile doing here
as well.
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 7e6b00212..be89257a1 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1698,6 +1698,20 @@ private: Bits::getMaxBits(left, this) == 1) { return builder.makeUnary(Abstract::getUnary(type, EqZ), left); } + // bool(x) ^ 1 ==> !bool(x) + if (matches(curr, binary(Xor, any(&left), ival(1))) && + Bits::getMaxBits(left, this) == 1) { + auto* result = builder.makeUnary(Abstract::getUnary(type, EqZ), left); + if (left->type == Type::i64) { + // Xor's result is also an i64 in this case, but EqZ returns i32, so we + // must expand it so that we keep returning the same value as before. + // This means we replace a xor and a const with a xor and an extend, + // which is still smaller (the const is 2 bytes, the extend just 1), and + // also the extend may be removed by further work. + result = builder.makeUnary(ExtendUInt32, result); + } + return result; + } // bool(x) | 1 ==> 1 if (matches(curr, binary(Or, pure(&left), ival(1))) && Bits::getMaxBits(left, this) == 1) { |