[llvm] [InstCombine] Fold max(max(x, c1) << c2, c3) —> max(x << c2, c3) when c3 >= c1 * 2 ^ c2 (PR #140526)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat May 24 01:39:33 PDT 2025


================
@@ -1174,6 +1174,86 @@ static Instruction *moveAddAfterMinMax(IntrinsicInst *II,
   return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
                   : BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
 }
+
+// Try canonicalize max(max(X,C1) binop C2, C3) -> max(X binop C2, C3)
+static Instruction *moveShiftAfterMinMax(IntrinsicInst *II,
----------------
dtcxzyw wrote:

`max(X, C1) binop C2 -> max(X binop C2, C1 binop C2)` is not always safe for all binops. You can reuse the helper `leftDistributesOverRight`.


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


More information about the llvm-commits mailing list