[InstCombine] Fold icmps comparing usub_sat with a constant
authorDhruv Chawla <44582521+dc03@users.noreply.github.com>
Fri, 30 Jun 2023 11:28:11 +0000 (16:58 +0530)
committerDhruv Chawla <44582521+dc03@users.noreply.github.com>
Wed, 5 Jul 2023 13:25:52 +0000 (18:55 +0530)
commitb66006fbf78929b7b2c03462770c90eb2efded9f
tree28c2dd2c661d55e662cc47057a64d22b02e6751b
parent0e71bb33f8273ffdeab5b97d28c8341b914deb53
[InstCombine] Fold icmps comparing usub_sat with a constant

This patch introduces a fold for the operation "usub_sat(X, C) pred C2"
where "C" and "C2" are constants. The fold is:

usub_sat(X, C) pred C2
=> (X < C)  || ((X - C) pred C2) -> when (0 pred C2) is true
=> (X >= C) && ((X - C) pred C2) -> when (0 pred C2) is false

These expressions can generally be folded into a simpler expression. As
they can sometimes emit more than one instruction, they are limited to
cases where the "usub_sat" has only one user.

Fixes #58342.

Proofs: https://alive2.llvm.org/ce/z/ws_N2J

Differential Revision: https://reviews.llvm.org/D154206
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-usub-sat.ll