Skip to content

Commit b8b90ac

Browse files
committedAug 25, 2019
[X86][DAGCombiner] Teach narrowShuffle to use concat_vectors instead of inserting into undef
Summary: Concat_vectors is more canonical during early DAG combine. For example, its what's used by SelectionDAGBuilder when converting IR shuffles into SelectionDAG shuffles when element counts between inputs and mask don't match. We also have combines in DAGCombiner than can pull concat_vectors through a shuffle. See partitionShuffleOfConcats. So it seems like concat_vectors is a better operation to use here. I had to teach DAGCombiner's SimplifyVBinOp to also handle concat_vectors with undef. I haven't checked yet if we can remove the INSERT_SUBVECTOR version in there or not. I didn't want to mess with the other caller of getShuffleHalfVectors that's used during shuffle lowering where insert_subvector probably is what we want to produce so I've enabled this via a boolean passed to the function. Reviewers: spatel, RKSimon Reviewed By: RKSimon Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D66504 llvm-svn: 369872
1 parent 1475fad commit b8b90ac

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed
 

‎llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -19537,6 +19537,37 @@ SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
1953719537
}
1953819538
}
1953919539

19540+
// Make sure all but the first op are undef.
19541+
auto ConcatWithUndef = [](SDValue Concat) {
19542+
return Concat.getOpcode() == ISD::CONCAT_VECTORS &&
19543+
std::all_of(std::next(Concat->op_begin()), Concat->op_end(),
19544+
[](const SDValue &Op) {
19545+
return Op.isUndef();
19546+
});
19547+
};
19548+
19549+
// The following pattern is likely to emerge with vector reduction ops. Moving
19550+
// the binary operation ahead of the concat may allow using a narrower vector
19551+
// instruction that has better performance than the wide version of the op:
19552+
// VBinOp (concat X, undef), (concat Y, undef) --> concat (VBinOp X, Y), VecC
19553+
if (ConcatWithUndef(LHS) && ConcatWithUndef(RHS) &&
19554+
(LHS.hasOneUse() || RHS.hasOneUse())) {
19555+
SDValue X = LHS.getOperand(0);
19556+
SDValue Y = RHS.getOperand(0);
19557+
EVT NarrowVT = X.getValueType();
19558+
if (NarrowVT == Y.getValueType() &&
19559+
TLI.isOperationLegalOrCustomOrPromote(Opcode, NarrowVT)) {
19560+
// (binop undef, undef) may not return undef, so compute that result.
19561+
SDLoc DL(N);
19562+
SDValue VecC =
19563+
DAG.getNode(Opcode, DL, NarrowVT, DAG.getUNDEF(NarrowVT),
19564+
DAG.getUNDEF(NarrowVT));
19565+
SmallVector<SDValue, 4> Ops(LHS.getNumOperands(), VecC);
19566+
Ops[0] = DAG.getNode(Opcode, DL, NarrowVT, X, Y);
19567+
return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Ops);
19568+
}
19569+
}
19570+
1954019571
if (SDValue V = scalarizeBinOpOfSplats(N, DAG))
1954119572
return V;
1954219573

‎llvm/lib/Target/X86/X86ISelLowering.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -15013,7 +15013,7 @@ getHalfShuffleMask(ArrayRef<int> Mask, MutableArrayRef<int> HalfMask,
1501315013
static SDValue getShuffleHalfVectors(const SDLoc &DL, SDValue V1, SDValue V2,
1501415014
ArrayRef<int> HalfMask, int HalfIdx1,
1501515015
int HalfIdx2, bool UndefLower,
15016-
SelectionDAG &DAG) {
15016+
SelectionDAG &DAG, bool UseConcat = false) {
1501715017
assert(V1.getValueType() == V2.getValueType() && "Different sized vectors?");
1501815018
assert(V1.getValueType().isSimple() && "Expecting only simple types");
1501915019

@@ -15034,6 +15034,14 @@ static SDValue getShuffleHalfVectors(const SDLoc &DL, SDValue V1, SDValue V2,
1503415034
SDValue Half1 = getHalfVector(HalfIdx1);
1503515035
SDValue Half2 = getHalfVector(HalfIdx2);
1503615036
SDValue V = DAG.getVectorShuffle(HalfVT, DL, Half1, Half2, HalfMask);
15037+
if (UseConcat) {
15038+
SDValue Op0 = V;
15039+
SDValue Op1 = DAG.getUNDEF(HalfVT);
15040+
if (UndefLower)
15041+
std::swap(Op0, Op1);
15042+
return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Op0, Op1);
15043+
}
15044+
1503715045
unsigned Offset = UndefLower ? HalfNumElts : 0;
1503815046
return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), V,
1503915047
DAG.getIntPtrConstant(Offset, DL));
@@ -33974,7 +33982,7 @@ static SDValue narrowShuffle(ShuffleVectorSDNode *Shuf, SelectionDAG &DAG) {
3397433982
// the wide shuffle that we started with.
3397533983
return getShuffleHalfVectors(SDLoc(Shuf), Shuf->getOperand(0),
3397633984
Shuf->getOperand(1), HalfMask, HalfIdx1,
33977-
HalfIdx2, false, DAG);
33985+
HalfIdx2, false, DAG, /*UseConcat*/true);
3397833986
}
3397933987

3398033988
static SDValue combineShuffle(SDNode *N, SelectionDAG &DAG,

0 commit comments

Comments
 (0)
Please sign in to comment.