[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
Thu Jul 24 10:21:38 PDT 2025
================
@@ -1174,6 +1174,109 @@ static Instruction *moveAddAfterMinMax(IntrinsicInst *II,
return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
: BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
}
+
+/// Returns weather the it holds for (X LOp Y) ROp Z -> (X ROp Z) LOp (Y ROp Z)
+
+static bool rightDistributesOverLeft(Instruction::BinaryOps ROp, bool HasNUW,
+ bool HasNSW, Intrinsic::ID LOp) {
+ switch (LOp) {
+ case Intrinsic::umax:
+ if (HasNUW && (ROp == Instruction::AShr || ROp == Instruction::LShr ||
+ ROp == Instruction::UDiv || ROp == Instruction::Mul ||
+ ROp == Instruction::Add))
+ return true;
+ return false;
+ case Intrinsic::umin:
+ if (HasNUW && (ROp == Instruction::AShr || ROp == Instruction::LShr ||
+ ROp == Instruction::UDiv || ROp == Instruction::Sub))
+ return true;
+ return false;
+ case Intrinsic::smax:
+ case Intrinsic::smin:
+ if (HasNSW && ROp == Instruction::AShr)
+ return true;
+ return false;
+ default:
+ return false;
+ }
+}
+
+/// Try canonicalize max(max(X,C1) binop C2, C3) -> max(X binop C2, max(C1
+/// binop C2, C3))
+/// -> max(X binop C2, C4) //
----------------
dtcxzyw wrote:
```suggestion
/// -> max(X binop C2, C4)
```
https://github.com/llvm/llvm-project/pull/140526
More information about the llvm-commits
mailing list