[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
Fri May 30 06:54:13 PDT 2025


================
@@ -1174,6 +1174,163 @@ static Instruction *moveAddAfterMinMax(IntrinsicInst *II,
   return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
                   : BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
 }
+
+
+static bool rightDistributesOverLeft(Instruction::BinaryOps ROp, bool HasNUW,
+                                     bool HasNSW, Intrinsic::ID LOp) {
+  switch (LOp) {
+  case Intrinsic::umax:
+  case Intrinsic::umin:
+    // Unsigned min/max distribute over addition and left shift if no unsigned
+    // wrap.
+    if (HasNUW && (ROp == Instruction::Add || ROp == Instruction::Shl))
+      return true;
+    // Multiplication preserves order for unsigned min/max with no unsigned
+    // wrap.
+    if (HasNUW && ROp == Instruction::Mul)
+      return true;
+    return false;
+  case Intrinsic::smax:
+  case Intrinsic::smin:
+    // Signed min/max distribute over addition if no signed wrap.
+    if (HasNSW && ROp == Instruction::Add)
+      return true;
----------------
dtcxzyw wrote:

It doesn't hold for smin/smax: https://alive2.llvm.org/ce/z/XFf_U8


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


More information about the llvm-commits mailing list