Issue 169763
Summary [InstCombine] Fold icmp ult (uadd.sat X, C), C2 to icmp ult X, (C2 - C)
Labels new issue
Assignees
Reporter ParkHanbum
    
Description

I would like to propose a transformation that simplifies a check on uadd.sat result 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 > C, we can elide the saturation logic entirely.

Pattern:
uadd.sat(X, C) < C2

Current Transform:
uadd_sat(X, Y) pred Z  into 
(-1 pred Z) == true  => (X >= ~Y) || ((X + Y) pred Z) 
(-1 pred Z) == false => (X < ~Y)  && ((X + Y) pred Z)


Preconditions:
    C and C2 are constants.
    C2 u> C (Unsigned comparison).
    SIGN_MAX u< C2 is false.

Reasoning:
(-1 u< C2) == false => (X u< ~C)  && ((X +C)  u< C2)
(X u< ~C)  && ((X +C)  u< C2) -> ((X + C) u< -1) && ((X + C) u< C2)
; Since C2 <= -1 (Unsigned Max) is always true, 
; the condition ((X + C) u< C2) implies ((X + C) u< -1).
; Therefore, the intersection is simply:
=> (X + C) u< C2
=> X u< C2 - C

Alive Proof : https://alive2.llvm.org/ce/z/czW5Pj

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to