llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[InstCombine] Fold icmp ult (uadd.sat X, C), C2 to icmp ult X, (C2 - C)

Open ParkHanbum opened this issue 1 year ago • 2 comments

Description

I would like to propose a transformation that simplifies a check on the result of uadd.sat into a simple range check on the input, when the operands are constants.

If we check whether a saturated unsigned addition is strictly less than a constant C2, and we know that C2 u> C, we can elide the saturation logic entirely.

Pattern:

%sat = call i8 @llvm.uadd.sat.i8(i8 %x, i8 %c)
%cmp = icmp ult i8 %sat, %c2

Preconditions:

C and C2 are constants.
X+C will not overflow 
C2 u> C (Unsigned comparison).
(Note: If C2 u<= C, the result is always false because the minimum value of uadd.sat(X, C) is C.)

Reasoning

LLVM currently expands uadd.sat(X, Y) pred Z based on whether the saturation value (-1) satisfies the predicate.

Current logic for ult: Since (-1 u< C2) is always false (because C2 <= -1 for any integer type), the expression expands to the "no saturation" check:

(X u< ~C) && ((X + C) u< C2)

Proposed Simplification: We can simplify this conjunction based on the relationship between C2 and -1 (MAX):

The term ((X + C) u< C2) implies ((X + C) u< -1) because C2 <= -1 is always true.

The term (X u< ~C) is equivalent to (X + C) does not overflow, which effectively means (X + C) u<= -1 (and not wrapped).

Since C2 acts as a tighter upper bound than -1, the condition ((X + C) u< C2) strictly subsumes (X u< ~C).

Derivation:

(X u< ~C) && ((X + C) u< C2)
=> ((X + C) u< -1) && ((X + C) u< C2)  ; (X u< ~C) implies sum is not overflowed/saturated
=> (X + C) u< C2                       ; Intersection of (Val < MAX) and (Val < C2) is (Val < C2)
=> X u< C2 - C                         ; Valid because we established no overflow occurs in this range

Example IR

Source:

define i1 @src(i8 %x) {
  ; C = 10, C2 = 30
  ; uadd.sat(X, 10) < 30
  %add = call i8 @llvm.uadd.sat.i8(i8 %x, i8 10)
  %cmp = icmp ult i8 %add, 30
  ret i1 %cmp
}

Target:

define i1 @tgt(i8 %x) {
  ; X < 30 - 10 => X < 20
  %cmp = icmp ult i8 %x, 20
  ret i1 %cmp
}

Alive proof : https://alive2.llvm.org/ce/z/P4JmoP

ParkHanbum avatar Nov 27 '25 06:11 ParkHanbum

Hi @ParkHanbum, thanks for your contribution! I think this transformation can be generalized into this form:

For integer type iN:

0 ≤s x ∧ c ≤s c2 ≤s -1 |= uadd_sat_N(x, c) ≥s c2 ⇒ x ≥s sub_N(c2, c)

The alive2 proof in i8 is provided: https://alive2.llvm.org/ce/z/S-pKdp

Compared with the original generalization result

(-1 ≥s c2) ∧ NoUAddOverflow_8(x, c) ∧ NoSSubOverflow_8(c2, c) ∧ (c2 - c) ≥s 0 |= uadd_sat_8(x, c) ≥s c2 ⇒ x ≥s (c2 - c) in https://alive2.llvm.org/ce/z/P4JmoP, The additional coverage comes from cases where x + c has unsigned overflow.

The old rule in https://alive2.llvm.org/ce/z/P4JmoP only covers the no-overflow branch. The new rule removes the NoUAddOverflow_8(x, c) restriction and the0 ≤s x condition is already implied by the old preconditions. In the old rule, c2 ≤s -1 and (c2 - c) ≥s 0 imply c ≤s c2 ≤s -1, so c is signed-negative; combined with NoUAddOverflow_8(x, c), this forces x ≤u 127, i.e. 0 ≤s x.

Therefore, the new generalization rule covers all old no-overflow cases, and additionally covers the unsigned-overflow/saturation cases. When x + c overflows unsigned, uadd_sat_8(x, c) saturates to 255, which is -1 as signed i8. Since c2 ≤s -1, the comparison still holds on that path.

hiandgodblessyou avatar Jun 10 '26 20:06 hiandgodblessyou

Hi, @ParkHanbum I'd like to work on this — is it okay if I take it on?

I'll also try optimizing for non-constant conditions by using assume.

https://alive2.llvm.org/ce/z/B4BZQC

dongwooklee96 avatar Jul 29 '26 10:07 dongwooklee96