[llvm] [InstCombine] Recognize non-negative subtraction patterns (PR #182597)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 20 22:59:52 PST 2026


https://github.com/dtcxzyw commented:

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.


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


More information about the llvm-commits mailing list