Skip to content

Commit 669d844

Browse files
committed
[X86] Convert truncsat clamping patterns to use SDPatternMatch. NFC.
Inspired by #99418 (which hopefully we can replace this code with at some point)
1 parent f4d5b14 commit 669d844

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50995,38 +50995,31 @@ static SDValue foldVectorXorShiftIntoCmp(SDNode *N, SelectionDAG &DAG,
5099550995
/// pattern was not matched.
5099650996
static SDValue detectUSatPattern(SDValue In, EVT VT, SelectionDAG &DAG,
5099750997
const SDLoc &DL) {
50998+
using namespace llvm::SDPatternMatch;
5099850999
EVT InVT = In.getValueType();
5099951000

5100051001
// Saturation with truncation. We truncate from InVT to VT.
5100151002
assert(InVT.getScalarSizeInBits() > VT.getScalarSizeInBits() &&
5100251003
"Unexpected types for truncate operation");
5100351004

51004-
// Match min/max and return limit value as a parameter.
51005-
auto MatchMinMax = [](SDValue V, unsigned Opcode, APInt &Limit) -> SDValue {
51006-
if (V.getOpcode() == Opcode &&
51007-
ISD::isConstantSplatVector(V.getOperand(1).getNode(), Limit))
51008-
return V.getOperand(0);
51009-
return SDValue();
51010-
};
51011-
5101251005
APInt C1, C2;
51013-
if (SDValue UMin = MatchMinMax(In, ISD::UMIN, C2))
51014-
// C2 should be equal to UINT32_MAX / UINT16_MAX / UINT8_MAX according
51015-
// the element size of the destination type.
51016-
if (C2.isMask(VT.getScalarSizeInBits()))
51017-
return UMin;
51006+
SDValue UMin, SMin, SMax;
5101851007

51019-
if (SDValue SMin = MatchMinMax(In, ISD::SMIN, C2))
51020-
if (MatchMinMax(SMin, ISD::SMAX, C1))
51021-
if (C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits()))
51022-
return SMin;
51008+
// C2 should be equal to UINT32_MAX / UINT16_MAX / UINT8_MAX according
51009+
// the element size of the destination type.
51010+
if (sd_match(In, m_UMin(m_Value(UMin), m_ConstInt(C2))) &&
51011+
C2.isMask(VT.getScalarSizeInBits()))
51012+
return UMin;
5102351013

51024-
if (SDValue SMax = MatchMinMax(In, ISD::SMAX, C1))
51025-
if (SDValue SMin = MatchMinMax(SMax, ISD::SMIN, C2))
51026-
if (C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits()) &&
51027-
C2.uge(C1)) {
51028-
return DAG.getNode(ISD::SMAX, DL, InVT, SMin, In.getOperand(1));
51029-
}
51014+
if (sd_match(In, m_SMin(m_Value(SMin), m_ConstInt(C2))) &&
51015+
sd_match(SMin, m_SMax(m_Value(SMax), m_ConstInt(C1))) &&
51016+
C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits()))
51017+
return SMin;
51018+
51019+
if (sd_match(In, m_SMax(m_Value(SMax), m_ConstInt(C1))) &&
51020+
sd_match(SMax, m_SMin(m_Value(SMin), m_ConstInt(C2))) &&
51021+
C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits()) && C2.uge(C1))
51022+
return DAG.getNode(ISD::SMAX, DL, InVT, SMin, In.getOperand(1));
5103051023

5103151024
return SDValue();
5103251025
}
@@ -51041,35 +51034,28 @@ static SDValue detectUSatPattern(SDValue In, EVT VT, SelectionDAG &DAG,
5104151034
/// Return the source value to be truncated or SDValue() if the pattern was not
5104251035
/// matched.
5104351036
static SDValue detectSSatPattern(SDValue In, EVT VT, bool MatchPackUS = false) {
51037+
using namespace llvm::SDPatternMatch;
5104451038
unsigned NumDstBits = VT.getScalarSizeInBits();
5104551039
unsigned NumSrcBits = In.getScalarValueSizeInBits();
5104651040
assert(NumSrcBits > NumDstBits && "Unexpected types for truncate operation");
5104751041

51048-
auto MatchMinMax = [](SDValue V, unsigned Opcode,
51049-
const APInt &Limit) -> SDValue {
51050-
APInt C;
51051-
if (V.getOpcode() == Opcode &&
51052-
ISD::isConstantSplatVector(V.getOperand(1).getNode(), C) && C == Limit)
51053-
return V.getOperand(0);
51054-
return SDValue();
51055-
};
51056-
5105751042
APInt SignedMax, SignedMin;
5105851043
if (MatchPackUS) {
5105951044
SignedMax = APInt::getAllOnes(NumDstBits).zext(NumSrcBits);
51060-
SignedMin = APInt(NumSrcBits, 0);
51045+
SignedMin = APInt::getZero(NumSrcBits);
5106151046
} else {
5106251047
SignedMax = APInt::getSignedMaxValue(NumDstBits).sext(NumSrcBits);
5106351048
SignedMin = APInt::getSignedMinValue(NumDstBits).sext(NumSrcBits);
5106451049
}
5106551050

51066-
if (SDValue SMin = MatchMinMax(In, ISD::SMIN, SignedMax))
51067-
if (SDValue SMax = MatchMinMax(SMin, ISD::SMAX, SignedMin))
51068-
return SMax;
51051+
SDValue SMin, SMax;
51052+
if (sd_match(In, m_SMin(m_Value(SMin), m_SpecificInt(SignedMax))) &&
51053+
sd_match(SMin, m_SMax(m_Value(SMax), m_SpecificInt(SignedMin))))
51054+
return SMax;
5106951055

51070-
if (SDValue SMax = MatchMinMax(In, ISD::SMAX, SignedMin))
51071-
if (SDValue SMin = MatchMinMax(SMax, ISD::SMIN, SignedMax))
51072-
return SMin;
51056+
if (sd_match(In, m_SMax(m_Value(SMax), m_SpecificInt(SignedMin))) &&
51057+
sd_match(SMax, m_SMin(m_Value(SMin), m_SpecificInt(SignedMax))))
51058+
return SMin;
5107351059

5107451060
return SDValue();
5107551061
}

0 commit comments

Comments
 (0)