[llvm] [InstCombine] Pattern match minmax calls for unsigned saturation. (PR #99250)
Huihui Zhang via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 23 20:58:43 PDT 2024
================
@@ -1117,68 +1117,108 @@ static Instruction *moveAddAfterMinMax(IntrinsicInst *II,
return IsSigned ? BinaryOperator::CreateNSWAdd(NewMinMax, Add->getOperand(1))
: BinaryOperator::CreateNUWAdd(NewMinMax, Add->getOperand(1));
}
-/// Match a sadd_sat or ssub_sat which is using min/max to clamp the value.
-Instruction *InstCombinerImpl::matchSAddSubSat(IntrinsicInst &MinMax1) {
+/// Match a [s|u]add_sat or [s|u]sub_sat which is using min/max to clamp the
+/// value.
+Instruction *InstCombinerImpl::matchAddSubSat(IntrinsicInst &MinMax1) {
Type *Ty = MinMax1.getType();
- // We are looking for a tree of:
- // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B))))
- // Where the min and max could be reversed
- Instruction *MinMax2;
+ // 1. We are looking for a tree of signed saturation:
+ // smax(SINT_MIN, smin(SINT_MAX, add|sub(sext(A), sext(B))))
+ // Where the smin and smax could be reversed.
+ // 2. A tree of unsigned saturation:
+ // smax(UINT_MIN, sub(zext(A), zext(B)))
+ // Or umin(UINT_MAX, add(zext(A), zext(B))).
+ Instruction *MinMax2 = nullptr;
BinaryOperator *AddSub;
- const APInt *MinValue, *MaxValue;
- if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) {
- if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue))))
+ const APInt *MinValue = nullptr, *MaxValue = nullptr;
+ bool IsUnsignedSaturate = false;
+ // Pattern match for unsigned saturation.
+ if (match(&MinMax1, m_UMin(m_BinOp(AddSub), m_APInt(MaxValue)))) {
+ // Bail out if AddSub could be negative.
+ if (!isKnownNonNegative(AddSub, SQ.getWithInstruction(AddSub)))
----------------
huihzhang wrote:
This check was used to reject "umin(UINT_MAX, sub) -> usub_sat".
I pushed a new update to check for BinOp opcode 'Add', so that !isKnownNonNegative check can be deleted.
https://github.com/llvm/llvm-project/pull/99250
More information about the llvm-commits
mailing list