[llvm] [InstCombine] Fold smax(smax(x<<C1, x+C2), C3) -> smax(x<<C1, C3) (PR #199163)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 23 00:59:43 PDT 2026


FathimaHaris wrote:

The intuition is that X + C2 can be removed only if it can never determine the result of :
smax(smax(x << C1, x + C2), C3).

The only interesting case is when X + C2 > X<<C1 , since otherwise the shift already dominates.

Assuming non-wrapping arithmetic,

	X+C2 > X * 2^C1 ==> X < C2 / (2^C1 - 1)
	
Within this region, X + C2 is maximized at the boundary X = C2 / (2^C1 - 1)

Substituting this into X + C2 gives:

	C2 / (2^C1 - 1) + C2 ==> C2 * 2^C1 / (2^C1 - 1)

Therefore, if

C2 * 2^C1 / (2^C1 - 1) <= C3 ,

or equivalently C2 <= C3 * (2^C1 - 1) / 2^C1,

then even in the worst case where X + C2 exceeds X << C1, it is still bounded by C3.

Hence under this condition , X + C2 can never affect the final result and may be removed.

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


More information about the llvm-commits mailing list