Fixes https://github.com/llvm/llvm-project/issues/55599
X * Y --> X & Y, iff X, Y can be only {0, 1}.
Differential D126040
[InstCombine] Fold a mul with bool value into and Allen on May 19 2022, 8:23 PM. Authored by
Details Fixes https://github.com/llvm/llvm-project/issues/55599 X * Y --> X & Y, iff X, Y can be only {0, 1}.
Diff Detail
Unit Tests Event Timeline
Comment Actions Place the new match rule after the following rule can void to touch other cases // (zext bool X) * Y --> X ? Y : 0 // Y * (zext bool X) --> X ? Y : 0
Comment Actions Done, thanks
Comment Actions LG
Comment Actions What is the real-world motivation for this change? This seems very niche for a transform that introduces two unconditional computeKnownBits() calls. Comment Actions Botan AES-128 benchmark, maybe others.. GCC implements this as well. https://gcc.gnu.org/pipermail/gcc-patches/2022-May/595219.html Possible follow up: https://github.com/llvm/llvm-project/issues/55618#issuecomment-1133433655
Comment Actions As far as I can tell, this wouldn't help Botan AES-128, because it needs the variant where only one of the operands is 0/1. That does look more generally useful, but is also somewhat unclear from a canonicalization perspective, because it increases instruction count. (We'd probably want to represent it as trunc(X) ? Y : 0 rather than -X & Y, but it's increasing the count either way.) Comment Actions Yes, note that we do have these related folds already: // (zext bool X) * Y --> X ? Y : 0 // (lshr X, 31) * Y --> (ashr X, 31) & Y With these comments on the 2nd fold: // TODO: We are not checking one-use because the elimination of the multiply // is better for analysis? // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be // more similar to what we're doing above. If we really want this fold in IR using known bits, then it could be implemented to reduce cost in 2 ways:
But given that we already have the zext/shift patterns covered, maybe we just want to add the specific match for and 1 as this patch was originally written. None of the tests are covering other patterns because we already get all of those? Comment Actions Thanks all. Based on the disccusion above, I roll back to the previous conservative handing method, so that further improvements can be made base on more special actual scenarious. Comment Actions Given the existing folds near here, I'm fine with the small and direct match. But let's make sure others agree with the direction.
Comment Actions hi @spatel , I'm sorry to trouble you again. Now I already address all your comment before, should we wait another reviewer to also agree with this direction? Comment Actions I added a couple of minor comments.
|
I think it would be better to add the new match to this clause since they are folding to the same new value: