Reapply "SLPVectorizer: Ignore users that are insertelements we can reschedule them"

This commit reapplies 205018. After 205855 we should correctly vectorize
intrinsics.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205965 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Arnold Schwaighofer 2014-04-10 13:41:35 +00:00
parent feb96be96e
commit e2d124d396
2 changed files with 88 additions and 29 deletions

View File

@ -366,13 +366,13 @@ public:
/// A negative number means that this is profitable. /// A negative number means that this is profitable.
int getTreeCost(); int getTreeCost();
/// Construct a vectorizable tree that starts at \p Roots and is possibly /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
/// used by a reduction of \p RdxOps. /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
void buildTree(ArrayRef<Value *> Roots, ValueSet *RdxOps = 0); void buildTree(ArrayRef<Value *> Roots,
ArrayRef<Value *> UserIgnoreLst = None);
/// Clear the internal data structures that are created by 'buildTree'. /// Clear the internal data structures that are created by 'buildTree'.
void deleteTree() { void deleteTree() {
RdxOps = 0;
VectorizableTree.clear(); VectorizableTree.clear();
ScalarToTreeEntry.clear(); ScalarToTreeEntry.clear();
MustGather.clear(); MustGather.clear();
@ -528,8 +528,8 @@ private:
/// Numbers instructions in different blocks. /// Numbers instructions in different blocks.
DenseMap<BasicBlock *, BlockNumbering> BlocksNumbers; DenseMap<BasicBlock *, BlockNumbering> BlocksNumbers;
/// Reduction operators. /// List of users to ignore during scheduling and that don't need extracting.
ValueSet *RdxOps; ArrayRef<Value *> UserIgnoreList;
// Analysis and block reference. // Analysis and block reference.
Function *F; Function *F;
@ -543,9 +543,10 @@ private:
IRBuilder<> Builder; IRBuilder<> Builder;
}; };
void BoUpSLP::buildTree(ArrayRef<Value *> Roots, ValueSet *Rdx) { void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
ArrayRef<Value *> UserIgnoreLst) {
deleteTree(); deleteTree();
RdxOps = Rdx; UserIgnoreList = UserIgnoreLst;
if (!getSameType(Roots)) if (!getSameType(Roots))
return; return;
buildTree_rec(Roots, 0); buildTree_rec(Roots, 0);
@ -577,8 +578,9 @@ void BoUpSLP::buildTree(ArrayRef<Value *> Roots, ValueSet *Rdx) {
if (!UserInst) if (!UserInst)
continue; continue;
// Ignore uses that are part of the reduction. // Ignore users in the user ignore list.
if (Rdx && std::find(Rdx->begin(), Rdx->end(), UserInst) != Rdx->end()) if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UserInst) !=
UserIgnoreList.end())
continue; continue;
DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " << DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " <<
@ -709,8 +711,9 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth) {
continue; continue;
} }
// This user is part of the reduction. // Ignore users in the user ignore list.
if (RdxOps && RdxOps->count(UI)) if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UI) !=
UserIgnoreList.end())
continue; continue;
// Make sure that we can schedule this unknown user. // Make sure that we can schedule this unknown user.
@ -1749,8 +1752,9 @@ Value *BoUpSLP::vectorizeTree() {
DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n"); DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
assert((ScalarToTreeEntry.count(U) || assert((ScalarToTreeEntry.count(U) ||
// It is legal to replace the reduction users by undef. // It is legal to replace users in the ignorelist by undef.
(RdxOps && RdxOps->count(U))) && (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), U) !=
UserIgnoreList.end())) &&
"Replacing out-of-tree value with undef"); "Replacing out-of-tree value with undef");
} }
#endif #endif
@ -1954,8 +1958,11 @@ private:
bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R); bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R);
/// \brief Try to vectorize a list of operands. /// \brief Try to vectorize a list of operands.
/// \@param BuildVector A list of users to ignore for the purpose of
/// scheduling and that don't need extracting.
/// \returns true if a value was vectorized. /// \returns true if a value was vectorized.
bool tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R); bool tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
ArrayRef<Value *> BuildVector = None);
/// \brief Try to vectorize a chain that may start at the operands of \V; /// \brief Try to vectorize a chain that may start at the operands of \V;
bool tryToVectorize(BinaryOperator *V, BoUpSLP &R); bool tryToVectorize(BinaryOperator *V, BoUpSLP &R);
@ -2128,7 +2135,8 @@ bool SLPVectorizer::tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) {
return tryToVectorizeList(VL, R); return tryToVectorizeList(VL, R);
} }
bool SLPVectorizer::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R) { bool SLPVectorizer::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
ArrayRef<Value *> BuildVector) {
if (VL.size() < 2) if (VL.size() < 2)
return false; return false;
@ -2178,13 +2186,33 @@ bool SLPVectorizer::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R) {
<< "\n"); << "\n");
ArrayRef<Value *> Ops = VL.slice(i, OpsWidth); ArrayRef<Value *> Ops = VL.slice(i, OpsWidth);
R.buildTree(Ops); ArrayRef<Value *> BuildVectorSlice;
if (!BuildVector.empty())
BuildVectorSlice = BuildVector.slice(i, OpsWidth);
R.buildTree(Ops, BuildVectorSlice);
int Cost = R.getTreeCost(); int Cost = R.getTreeCost();
if (Cost < -SLPCostThreshold) { if (Cost < -SLPCostThreshold) {
DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n"); DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
R.vectorizeTree(); Value *VectorizedRoot = R.vectorizeTree();
// Reconstruct the build vector by extracting the vectorized root. This
// way we handle the case where some elements of the vector are undefined.
// (return (inserelt <4 xi32> (insertelt undef (opd0) 0) (opd1) 2))
if (!BuildVectorSlice.empty()) {
Instruction *InsertAfter = cast<Instruction>(VectorizedRoot);
for (auto &V : BuildVectorSlice) {
InsertElementInst *IE = cast<InsertElementInst>(V);
IRBuilder<> Builder(++BasicBlock::iterator(InsertAfter));
Instruction *Extract = cast<Instruction>(
Builder.CreateExtractElement(VectorizedRoot, IE->getOperand(2)));
IE->setOperand(1, Extract);
IE->removeFromParent();
IE->insertAfter(Extract);
InsertAfter = IE;
}
}
// Move to the next bundle. // Move to the next bundle.
i += VF - 1; i += VF - 1;
Changed = true; Changed = true;
@ -2293,7 +2321,7 @@ static Value *createRdxShuffleMask(unsigned VecLen, unsigned NumEltsToRdx,
/// *p = /// *p =
/// ///
class HorizontalReduction { class HorizontalReduction {
SmallPtrSet<Value *, 16> ReductionOps; SmallVector<Value *, 16> ReductionOps;
SmallVector<Value *, 32> ReducedVals; SmallVector<Value *, 32> ReducedVals;
BinaryOperator *ReductionRoot; BinaryOperator *ReductionRoot;
@ -2387,7 +2415,7 @@ public:
// We need to be able to reassociate the adds. // We need to be able to reassociate the adds.
if (!TreeN->isAssociative()) if (!TreeN->isAssociative())
return false; return false;
ReductionOps.insert(TreeN); ReductionOps.push_back(TreeN);
} }
// Retract. // Retract.
Stack.pop_back(); Stack.pop_back();
@ -2424,7 +2452,7 @@ public:
for (; i < NumReducedVals - ReduxWidth + 1; i += ReduxWidth) { for (; i < NumReducedVals - ReduxWidth + 1; i += ReduxWidth) {
ArrayRef<Value *> ValsToReduce(&ReducedVals[i], ReduxWidth); ArrayRef<Value *> ValsToReduce(&ReducedVals[i], ReduxWidth);
V.buildTree(ValsToReduce, &ReductionOps); V.buildTree(ValsToReduce, ReductionOps);
// Estimate cost. // Estimate cost.
int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]); int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]);
@ -2543,13 +2571,16 @@ private:
/// ///
/// Returns true if it matches /// Returns true if it matches
/// ///
static bool findBuildVector(InsertElementInst *IE, static bool findBuildVector(InsertElementInst *FirstInsertElem,
SmallVectorImpl<Value *> &Ops) { SmallVectorImpl<Value *> &BuildVector,
if (!isa<UndefValue>(IE->getOperand(0))) SmallVectorImpl<Value *> &BuildVectorOpds) {
if (!isa<UndefValue>(FirstInsertElem->getOperand(0)))
return false; return false;
InsertElementInst *IE = FirstInsertElem;
while (true) { while (true) {
Ops.push_back(IE->getOperand(1)); BuildVector.push_back(IE);
BuildVectorOpds.push_back(IE->getOperand(1));
if (IE->use_empty()) if (IE->use_empty())
return false; return false;
@ -2719,12 +2750,16 @@ bool SLPVectorizer::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
} }
// Try to vectorize trees that start at insertelement instructions. // Try to vectorize trees that start at insertelement instructions.
if (InsertElementInst *IE = dyn_cast<InsertElementInst>(it)) { if (InsertElementInst *FirstInsertElem = dyn_cast<InsertElementInst>(it)) {
SmallVector<Value *, 8> Ops; SmallVector<Value *, 16> BuildVector;
if (!findBuildVector(IE, Ops)) SmallVector<Value *, 16> BuildVectorOpds;
if (!findBuildVector(FirstInsertElem, BuildVector, BuildVectorOpds))
continue; continue;
if (tryToVectorizeList(Ops, R)) { // Vectorize starting with the build vector operands ignoring the
// BuildVector instructions for the purpose of scheduling and user
// extraction.
if (tryToVectorizeList(BuildVectorOpds, R, BuildVector)) {
Changed = true; Changed = true;
it = BB->begin(); it = BB->begin();
e = BB->end(); e = BB->end();

View File

@ -195,6 +195,30 @@ define <4 x float> @simple_select_partial_vector(<4 x float> %a, <4 x float> %b,
ret <4 x float> %rb ret <4 x float> %rb
} }
; Make sure that vectorization happens even if insertelements operations
; must be rescheduled. The case here is from compiling Julia.
define <4 x float> @reschedule_extract(<4 x float> %a, <4 x float> %b) {
; CHECK-LABEL: @reschedule_extract(
; CHECK: %1 = fadd <4 x float> %a, %b
%a0 = extractelement <4 x float> %a, i32 0
%b0 = extractelement <4 x float> %b, i32 0
%c0 = fadd float %a0, %b0
%v0 = insertelement <4 x float> undef, float %c0, i32 0
%a1 = extractelement <4 x float> %a, i32 1
%b1 = extractelement <4 x float> %b, i32 1
%c1 = fadd float %a1, %b1
%v1 = insertelement <4 x float> %v0, float %c1, i32 1
%a2 = extractelement <4 x float> %a, i32 2
%b2 = extractelement <4 x float> %b, i32 2
%c2 = fadd float %a2, %b2
%v2 = insertelement <4 x float> %v1, float %c2, i32 2
%a3 = extractelement <4 x float> %a, i32 3
%b3 = extractelement <4 x float> %b, i32 3
%c3 = fadd float %a3, %b3
%v3 = insertelement <4 x float> %v2, float %c3, i32 3
ret <4 x float> %v3
}
; Check that cost model for vectorization takes credit for ; Check that cost model for vectorization takes credit for
; instructions that are erased. ; instructions that are erased.
define <4 x float> @take_credit(<4 x float> %a, <4 x float> %b) { define <4 x float> @take_credit(<4 x float> %a, <4 x float> %b) {