[llvm] [InstCombine] Remove the canonicalization of `trunc` to `i1` (PR #84628)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 9 09:49:17 PST 2024


goldsteinn wrote:

> May I ask a question about fixing this regression?
> 
> https://github.com/llvm/llvm-project/pull/84628/files#diff-8029a15d86a03b733e3d58345f03e5b45ec2c335e399cf6be07ac4247a67021cL249-R254
> 
> With the original canonicalization, `trunc X to i1` would be canonicalized to `Y = and X, 1` and `icmp ne Y, 0`, which enables some optimization like the folding of `(icmp Pred1 V1, C1) | (icmp Pred2 V2, C2)`. But without the canonicalization, it failed to match `trunc` as `icmp`, which results in regression.
> 
> https://github.com/llvm/llvm-project/blob/b4001e32b1aa4df07dc6babefba19f2b77f487c6/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L3678-L3682
> 
> The problem is that, the parameters for that series of functions are passed using `ICmpInst`. In order to reuse the code, it might be necessary to make changes to the signature and the body of the functions, to make them accept `TruncInst`. I don't know whether it's elegant or acceptable, or there is a better way to handle it. Thanks.

It is a limitation that these folds take completed `ICmpInst` instead of decomposed components. And I would fully support an NFC patch to change that.

Another approach might be to create a pattern match `m_TruncLike` and match the `(icmp ne (and X, 1), 0)` in it. Then modify the places you see use us matching `m_Trunc` to to use `m_TruncLike`.

https://github.com/llvm/llvm-project/pull/84628


More information about the llvm-commits mailing list