[llvm] [InstCombine] Fold (trunc X to i1) & !iszero(X & Pow2)) -> (X & (1 | Pow2)) == (1 | Pow2) (PR #119196)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 6 13:33:00 PST 2025


andjo403 wrote:

To have some better information I have done some searching in the code to see how many places I think `trunc X to i1` need to be handled to have no regressions from https://github.com/llvm/llvm-project/pull/84628 

one reason for adding handling of the trunc pattern is that vectors already is [folded to trunc ](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L1776) so that will be better optimized.

I find about 13 places that I think need some work:
[computeKnownBitsFromCond for dominating conditions in ValueTracking](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Analysis/ValueTracking.cpp#L763)
and then also [findValuesAffectedByCondition only adds trunc for assume](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Analysis/ValueTracking.cpp#L10099)

[computeKnownBitsFromContext for assumes in ValueTracking](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Analysis/ValueTracking.cpp#L847) handled in https://github.com/llvm/llvm-project/pull/118406 but have the ephemeral value problem for the not( trunc) pattern.

[getValueFromCondition in LazyValueInfo](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Analysis/LazyValueInfo.cpp#L1196)

[simplifySelectWithFakeICmpEq in InstructionSimplify](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Analysis/InstructionSimplify.cpp#L4586)

[foldSelectICmpAnd in InstCombineSelect](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L122)

[foldSelectICmpAndAnd in InstCombineSelect](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L594) looks like it will result in more instructions so maybe not. maybe the reason that decomposeBitTestICmp is not handled in this function also.

[foldSelectICmpAndZeroShl in InstCombineSelect](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L642) unclear why decomposeBitTestICmp is not handled in this function looks like it will save instructions.

[foldSelectICmpAndBinOp in InstCombineSelect](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L735) maybe shall have a common foldSelectICmpBitTest function that have the bit test code so it is not repeated.

[fold to alignment assume bundle in InstCombineCalls](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp#L3239)

[getMaskedTypeForICmpPair in InstCombineAndOrXor](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L202) if foldAndOrOfICmpsOfAndWithPow2 is moved in to foldLogOpOfMaskedICmps that is the only fold in this file that need update.

[matchLeftShift in HexagonLoopIdiomRecognition](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Target/Hexagon/HexagonLoopIdiomRecognition.cpp#L701)

[detectShiftUntilBitTestIdiom in LoopIdiomRecognize](https://github.com/llvm/llvm-project/blob/266bb4971b7cb8481bf9f3aa7a9fbba08ec89d98/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp#L2463)

many of the places already have decomposeBitTestICmp so maybe good to make a decomposeBitTest that only have a Value as input that handle trunc and then call decomposeBitTestICmp for icmp.




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


More information about the llvm-commits mailing list