[llvm] [InstCombine] Fold trunc(lshr(add(shl(X, ShAmt), C), ShrAmt)) (PR #214562)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 10 03:00:11 PDT 2026
Ineshmcw wrote:
> seems like the trunc is only used to limit the bits demanded in the result so shall this maybe be in `InstCombinerImpl::SimplifyDemandedUseBits`? eg if the `trunc i32 %x to i8 ` is replaced by `and i32 %x, 255` the same fold seems to work.
The transform is indeed valid in the and-mask framing too, and you're right that this is conceptually a demanded-bits fact. But I checked what `SimplifyDemandedBits` actually does with the equivalent `and i32 %x, 255 form`, and it doesn't perform this restructuring [Alive2](https://alive2.llvm.org/ce/z/248yqm). So the capability doesn't exist. In conclusion i think it would need new code either way. Given that, and since only `trunc` can also shrink the type down to `i8` (which `and` can't), I think it's better to keep keep this in `visitTrunc` for now.
>also shall it be more general then only for add? eg or, xor, and also works
For this one, I only implemented it for `add` in this particular case. Generalizing to `or/xor/and` as well is probably the better long term solution. I was a bit cautious about scope since this is my first contribution here. I've drafted the change to cover those cases and will push an update to the PR shortly.
Thanks for mentioning this!
https://github.com/llvm/llvm-project/pull/214562
More information about the llvm-commits
mailing list