Skip to content

Commit 85e5195

Browse files
committed
Add checks before custom lowering
1 parent ebb3ba0 commit 85e5195

File tree

2 files changed

+40
-56
lines changed

2 files changed

+40
-56
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,16 +1436,18 @@ X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
14361436
}
14371437

14381438
// Vector min/max reductions
1439-
if (Subtarget.hasSSE41())
1440-
{
1439+
// These are lowered to PHMINPOSUW if possible,
1440+
// otherwise they are expaned to shuffles + binops.
1441+
if (Subtarget.hasSSE41()) {
14411442
for (MVT VT : MVT::vector_valuetypes()) {
1442-
if (VT.getScalarType() == MVT::i8 || VT.getScalarType() == MVT::i16)
1443-
{
1444-
setOperationAction(ISD::VECREDUCE_UMIN, VT, Custom);
1445-
setOperationAction(ISD::VECREDUCE_UMAX, VT, Custom);
1446-
setOperationAction(ISD::VECREDUCE_SMIN, VT, Custom);
1447-
setOperationAction(ISD::VECREDUCE_SMAX, VT, Custom);
1448-
}
1443+
if (!VT.isFixedLengthVector() || (VT.getSizeInBits() % 128) != 0 ||
1444+
!(VT.getScalarType() == MVT::i8 || VT.getScalarType() == MVT::i16))
1445+
continue;
1446+
1447+
setOperationAction(ISD::VECREDUCE_UMIN, VT, Custom);
1448+
setOperationAction(ISD::VECREDUCE_UMAX, VT, Custom);
1449+
setOperationAction(ISD::VECREDUCE_SMIN, VT, Custom);
1450+
setOperationAction(ISD::VECREDUCE_SMAX, VT, Custom);
14491451
}
14501452
}
14511453

@@ -25426,9 +25428,11 @@ static SDValue LowerEXTEND_VECTOR_INREG(SDValue Op,
2542625428
// Create a min/max v8i16/v16i8 horizontal reduction with PHMINPOSUW.
2542725429
static SDValue createMinMaxReduction(SDValue Src, EVT TargetVT, SDLoc DL,
2542825430
ISD::NodeType BinOp, SelectionDAG &DAG,
25429-
const X86Subtarget &Subtarget)
25430-
{
25431-
assert(Subtarget.hasSSE41() && "The caller must check if SSE4.1 is available");
25431+
const X86Subtarget &Subtarget) {
25432+
assert(Subtarget.hasSSE41() &&
25433+
"The caller must check if SSE4.1 is available");
25434+
assert(TargetVT == MVT::i16 ||
25435+
TargetVT == MVT::i8 && "Unexpected return type");
2543225436

2543325437
EVT SrcVT = Src.getValueType();
2543425438
EVT SrcSVT = SrcVT.getScalarType();
@@ -25484,31 +25488,11 @@ static SDValue createMinMaxReduction(SDValue Src, EVT TargetVT, SDLoc DL,
2548425488
}
2548525489

2548625490
static SDValue LowerVECTOR_REDUCE_MINMAX(SDValue Op,
25487-
const X86Subtarget& Subtarget,
25488-
SelectionDAG& DAG)
25489-
{
25490-
ISD::NodeType BinOp;
25491-
switch (Op.getOpcode())
25492-
{
25493-
default:
25494-
assert(false && "Expected min/max reduction");
25495-
break;
25496-
case ISD::VECREDUCE_UMIN:
25497-
BinOp = ISD::UMIN;
25498-
break;
25499-
case ISD::VECREDUCE_UMAX:
25500-
BinOp = ISD::UMAX;
25501-
break;
25502-
case ISD::VECREDUCE_SMIN:
25503-
BinOp = ISD::SMIN;
25504-
break;
25505-
case ISD::VECREDUCE_SMAX:
25506-
BinOp = ISD::SMAX;
25507-
break;
25508-
}
25509-
25491+
const X86Subtarget &Subtarget,
25492+
SelectionDAG &DAG) {
25493+
ISD::NodeType BinOp = ISD::getVecReduceBaseOpcode(Op.getOpcode());
2551025494
return createMinMaxReduction(Op->getOperand(0), Op.getValueType(), SDLoc(Op),
25511-
BinOp, DAG, Subtarget);
25495+
BinOp, DAG, Subtarget);
2551225496
}
2551325497

2551425498
static SDValue LowerSIGN_EXTEND(SDValue Op, const X86Subtarget &Subtarget,
@@ -46299,8 +46283,8 @@ static SDValue combineMinMaxReduction(SDNode *Extract, SelectionDAG &DAG,
4629946283
if (!Src)
4630046284
return SDValue();
4630146285

46302-
return createMinMaxReduction(Src, ExtractVT, SDLoc(Extract),
46303-
BinOp, DAG, Subtarget);
46286+
return createMinMaxReduction(Src, ExtractVT, SDLoc(Extract), BinOp, DAG,
46287+
Subtarget);
4630446288
}
4630546289

4630646290
// Attempt to replace an all_of/any_of/parity style horizontal reduction with a MOVMSK.
@@ -47136,8 +47120,8 @@ static SDValue combineArithReduction(SDNode *ExtElt, SelectionDAG &DAG,
4713647120
/// scalars back, while for x64 we should use 64-bit extracts and shifts.
4713747121
static SDValue combineExtractVectorElt(SDNode *N, SelectionDAG &DAG,
4713847122
TargetLowering::DAGCombinerInfo &DCI,
47139-
const X86Subtarget &Subtarget,
47140-
bool& TransformedBinOpReduction) {
47123+
const X86Subtarget &Subtarget,
47124+
bool &TransformedBinOpReduction) {
4714147125
if (SDValue NewOp = combineExtractWithShuffle(N, DAG, DCI, Subtarget))
4714247126
return NewOp;
4714347127

@@ -47321,26 +47305,27 @@ static SDValue combineExtractVectorElt(SDNode *N, SelectionDAG &DAG,
4732147305
return SDValue();
4732247306
}
4732347307

47324-
static SDValue combineExtractVectorEltAndOperand(SDNode* N, SelectionDAG& DAG,
47325-
TargetLowering::DAGCombinerInfo& DCI,
47326-
const X86Subtarget& Subtarget)
47327-
{
47308+
static SDValue
47309+
combineExtractVectorEltAndOperand(SDNode *N, SelectionDAG &DAG,
47310+
TargetLowering::DAGCombinerInfo &DCI,
47311+
const X86Subtarget &Subtarget) {
4732847312
bool TransformedBinOpReduction = false;
47329-
auto Op = combineExtractVectorElt(N, DAG, DCI, Subtarget, TransformedBinOpReduction);
47313+
auto Op = combineExtractVectorElt(N, DAG, DCI, Subtarget,
47314+
TransformedBinOpReduction);
4733047315

47331-
if (TransformedBinOpReduction)
47332-
{
47316+
if (TransformedBinOpReduction) {
4733347317
// In case we simplified N = extract_vector_element(V, 0) with Op and V
4733447318
// resulted from a reduction, then we need to replace all uses of V with
4733547319
// scalar_to_vector(Op) to make sure that we eliminated the binop + shuffle
47336-
// pyramid. This is safe to do, because the elements of V are undefined except
47337-
// for the zeroth element and Op does not depend on V.
47320+
// pyramid. This is safe to do, because the elements of V are undefined
47321+
// except for the zeroth element and Op does not depend on V.
4733847322

4733947323
auto OldV = N->getOperand(0);
47340-
assert(!Op.getNode()->hasPredecessor(OldV.getNode()) &&
47341-
"Op must not depend on the converted reduction");
47324+
assert(!Op.getNode()->hasPredecessor(OldV.getNode()) &&
47325+
"Op must not depend on the converted reduction");
4734247326

47343-
auto NewV = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), OldV->getValueType(0), Op);
47327+
auto NewV =
47328+
DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), OldV->getValueType(0), Op);
4734447329

4734547330
auto NV = DCI.CombineTo(N, Op);
4734647331
DCI.CombineTo(OldV.getNode(), NewV);

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6586,10 +6586,9 @@ bool llvm::X86TTIImpl::shouldExpandReduction(const IntrinsicInst *II) const {
65866586
case Intrinsic::vector_reduce_smax:
65876587
auto *VType = cast<FixedVectorType>(II->getOperand(0)->getType());
65886588
auto SType = VType->getScalarType();
6589-
bool CanUsePHMINPOSUW =
6590-
ST->hasSSE41() && II->getType() == SType &&
6591-
(VType->getPrimitiveSizeInBits() % 128) == 0 &&
6592-
(SType->isIntegerTy(8) || SType->isIntegerTy(16));
6589+
bool CanUsePHMINPOSUW = ST->hasSSE41() && II->getType() == SType &&
6590+
(VType->getPrimitiveSizeInBits() % 128) == 0 &&
6591+
(SType->isIntegerTy(8) || SType->isIntegerTy(16));
65936592
return !CanUsePHMINPOSUW;
65946593
}
65956594
}

0 commit comments

Comments
 (0)