[llvm] [InstCombine] Recognize non-negative subtraction patterns (PR #182597)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 21 14:21:14 PST 2026
user1342234 wrote:
> InstCombine converts `sext` to `zext nneg` when `isKnownNonNegative(Src)` returns true:
>
> https://github.com/llvm/llvm-project/blob/1aaa33883b0407d9e427bc105d4014b3a9ae8801/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L1765-L1769
>
> In `computeKnownBitsAddSub`, we can infer `sub nsw` is non-negative if `icmp sle op1, op0` is implied by the dominating condition:
>
> https://github.com/llvm/llvm-project/blob/af9ca0e5bea319a976e57cf92d6aa033ffce44b2/llvm/lib/Analysis/ValueTracking.cpp#L501-L504
>
> I'd suggest matching the pattern in `computeKnownBitsAddSub`, as it benefits all computeKnownBits users. Although we have handled `icmp sle smin(x, y), x -> true` in `simplifyICmpWithMinMax`, it cannot be reused because ValueTracking cannot depend on InstructionSimplify. We can leave a todo to move the folding logic into ValueTracking.
Hello! Matching the pattern for `smin` works fine inside `computeKnownBitsAddSub` , but I would have to add logic in `computeKnownBitsFromOperator` at the `Instruction::Select` case to handle when `select` is used. Is this ok? It seems a little messy.
https://github.com/llvm/llvm-project/pull/182597
More information about the llvm-commits
mailing list