llvm.org GIT mirror llvm / b48eb2c
SLPVectorizer: Ignore users that are insertelements we can reschedule them Patch by Arch D. Robison! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205018 91177308-0d34-0410-b5e6-96231b3b80d8 Arnold Schwaighofer 5 years ago
2 changed file(s) with 89 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
364364 /// A negative number means that this is profitable.
365365 int getTreeCost();
366366
367 /// Construct a vectorizable tree that starts at \p Roots and is possibly
368 /// used by a reduction of \p RdxOps.
369 void buildTree(ArrayRef Roots, ValueSet *RdxOps = 0);
367 /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
368 /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
369 void buildTree(ArrayRef Roots,
370 ArrayRef UserIgnoreLst = None);
370371
371372 /// Clear the internal data structures that are created by 'buildTree'.
372373 void deleteTree() {
373 RdxOps = 0;
374374 VectorizableTree.clear();
375375 ScalarToTreeEntry.clear();
376376 MustGather.clear();
526526 /// Numbers instructions in different blocks.
527527 DenseMap BlocksNumbers;
528528
529 /// Reduction operators.
530 ValueSet *RdxOps;
529 /// List of users to ignore during scheduling and that don't need extracting.
530 ArrayRef UserIgnoreList;
531531
532532 // Analysis and block reference.
533533 Function *F;
541541 IRBuilder<> Builder;
542542 };
543543
544 void BoUpSLP::buildTree(ArrayRef Roots, ValueSet *Rdx) {
544 void BoUpSLP::buildTree(ArrayRef Roots,
545 ArrayRef UserIgnoreLst) {
545546 deleteTree();
546 RdxOps = Rdx;
547 UserIgnoreList = UserIgnoreLst;
547548 if (!getSameType(Roots))
548549 return;
549550 buildTree_rec(Roots, 0);
575576 if (!UserInst)
576577 continue;
577578
578 // Ignore uses that are part of the reduction.
579 if (Rdx && std::find(Rdx->begin(), Rdx->end(), UserInst) != Rdx->end())
579 // Ignore users in the user ignore list.
580 if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UserInst) !=
581 UserIgnoreList.end())
580582 continue;
581583
582584 DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " <<
707709 continue;
708710 }
709711
710 // This user is part of the reduction.
711 if (RdxOps && RdxOps->count(UI))
712 // Ignore users in the user ignore list.
713 if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UI) !=
714 UserIgnoreList.end())
712715 continue;
713716
714717 // Make sure that we can schedule this unknown user.
17361739 DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
17371740
17381741 assert((ScalarToTreeEntry.count(U) ||
1739 // It is legal to replace the reduction users by undef.
1740 (RdxOps && RdxOps->count(U))) &&
1742 // It is legal to replace users in the ignorelist by undef.
1743 (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), U) !=
1744 UserIgnoreList.end())) &&
17411745 "Replacing out-of-tree value with undef");
17421746 }
17431747 #endif
19411945 bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R);
19421946
19431947 /// \brief Try to vectorize a list of operands.
1948 /// \@param BuildVector A list of users to ignore for the purpose of
1949 /// scheduling and that don't need extracting.
19441950 /// \returns true if a value was vectorized.
1945 bool tryToVectorizeList(ArrayRef VL, BoUpSLP &R);
1951 bool tryToVectorizeList(ArrayRef VL, BoUpSLP &R,
1952 ArrayRef BuildVector = None);
19461953
19471954 /// \brief Try to vectorize a chain that may start at the operands of \V;
19481955 bool tryToVectorize(BinaryOperator *V, BoUpSLP &R);
21152122 return tryToVectorizeList(VL, R);
21162123 }
21172124
2118 bool SLPVectorizer::tryToVectorizeList(ArrayRef VL, BoUpSLP &R) {
2125 bool SLPVectorizer::tryToVectorizeList(ArrayRef VL, BoUpSLP &R,
2126 ArrayRef BuildVector) {
21192127 if (VL.size() < 2)
21202128 return false;
21212129
21652173 << "\n");
21662174 ArrayRef Ops = VL.slice(i, OpsWidth);
21672175
2168 R.buildTree(Ops);
2176 ArrayRef BuildVectorSlice;
2177 if (!BuildVector.empty())
2178 BuildVectorSlice = BuildVector.slice(i, OpsWidth);
2179
2180 R.buildTree(Ops, BuildVectorSlice);
21692181 int Cost = R.getTreeCost();
21702182
21712183 if (Cost < -SLPCostThreshold) {
21722184 DEBUG(dbgs() << "SLP: Vectorizing pair at cost:" << Cost << ".\n");
2173 R.vectorizeTree();
2174
2185 Value *VectorizedRoot = R.vectorizeTree();
2186
2187 // Reconstruct the build vector by extracting the vectorized root. This
2188 // way we handle the case where some elements of the vector are undefined.
2189 // (return (inserelt <4 xi32> (insertelt undef (opd0) 0) (opd1) 2))
2190 if (!BuildVectorSlice.empty()) {
2191 Instruction *InsertAfter = cast(VectorizedRoot);
2192 for (auto &V : BuildVectorSlice) {
2193 InsertElementInst *IE = cast(V);
2194 IRBuilder<> Builder(++BasicBlock::iterator(InsertAfter));
2195 Instruction *Extract = cast(
2196 Builder.CreateExtractElement(VectorizedRoot, IE->getOperand(2)));
2197 IE->setOperand(1, Extract);
2198 IE->removeFromParent();
2199 IE->insertAfter(Extract);
2200 InsertAfter = IE;
2201 }
2202 }
21752203 // Move to the next bundle.
21762204 i += VF - 1;
21772205 Changed = true;
22802308 /// *p =
22812309 ///
22822310 class HorizontalReduction {
2283 SmallPtrSet ReductionOps;
2311 SmallVector ReductionOps;
22842312 SmallVector ReducedVals;
22852313
22862314 BinaryOperator *ReductionRoot;
23742402 // We need to be able to reassociate the adds.
23752403 if (!TreeN->isAssociative())
23762404 return false;
2377 ReductionOps.insert(TreeN);
2405 ReductionOps.push_back(TreeN);
23782406 }
23792407 // Retract.
23802408 Stack.pop_back();
24112439
24122440 for (; i < NumReducedVals - ReduxWidth + 1; i += ReduxWidth) {
24132441 ArrayRef ValsToReduce(&ReducedVals[i], ReduxWidth);
2414 V.buildTree(ValsToReduce, &ReductionOps);
2442 V.buildTree(ValsToReduce, ReductionOps);
24152443
24162444 // Estimate cost.
24172445 int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]);
25302558 ///
25312559 /// Returns true if it matches
25322560 ///
2533 static bool findBuildVector(InsertElementInst *IE,
2534 SmallVectorImpl &Ops) {
2535 if (!isa(IE->getOperand(0)))
2561 static bool findBuildVector(InsertElementInst *FirstInsertElem,
2562 SmallVectorImpl &BuildVector,
2563 SmallVectorImpl &BuildVectorOpds) {
2564 if (!isa(FirstInsertElem->getOperand(0)))
25362565 return false;
25372566
2567 InsertElementInst *IE = FirstInsertElem;
25382568 while (true) {
2539 Ops.push_back(IE->getOperand(1));
2569 BuildVector.push_back(IE);
2570 BuildVectorOpds.push_back(IE->getOperand(1));
25402571
25412572 if (IE->use_empty())
25422573 return false;
27062737 }
27072738
27082739 // Try to vectorize trees that start at insertelement instructions.
2709 if (InsertElementInst *IE = dyn_cast(it)) {
2710 SmallVector Ops;
2711 if (!findBuildVector(IE, Ops))
2740 if (InsertElementInst *FirstInsertElem = dyn_cast(it)) {
2741 SmallVector BuildVector;
2742 SmallVector BuildVectorOpds;
2743 if (!findBuildVector(FirstInsertElem, BuildVector, BuildVectorOpds))
27122744 continue;
27132745
2714 if (tryToVectorizeList(Ops, R)) {
2746 // Vectorize starting with the build vector operands ignoring the
2747 // BuildVector instructions for the purpose of scheduling and user
2748 // extraction.
2749 if (tryToVectorizeList(BuildVectorOpds, R, BuildVector)) {
27152750 Changed = true;
27162751 it = BB->begin();
27172752 e = BB->end();
193193 ret <4 x float> %rb
194194 }
195195
196 ; Make sure that vectorization happens even if insertelements operations
197 ; must be rescheduled. The case here is from compiling Julia.
198 define <4 x float> @reschedule_extract(<4 x float> %a, <4 x float> %b) {
199 ; CHECK-LABEL: @reschedule_extract(
200 ; CHECK: %1 = fadd <4 x float> %a, %b
201 %a0 = extractelement <4 x float> %a, i32 0
202 %b0 = extractelement <4 x float> %b, i32 0
203 %c0 = fadd float %a0, %b0
204 %v0 = insertelement <4 x float> undef, float %c0, i32 0
205 %a1 = extractelement <4 x float> %a, i32 1
206 %b1 = extractelement <4 x float> %b, i32 1
207 %c1 = fadd float %a1, %b1
208 %v1 = insertelement <4 x float> %v0, float %c1, i32 1
209 %a2 = extractelement <4 x float> %a, i32 2
210 %b2 = extractelement <4 x float> %b, i32 2
211 %c2 = fadd float %a2, %b2
212 %v2 = insertelement <4 x float> %v1, float %c2, i32 2
213 %a3 = extractelement <4 x float> %a, i32 3
214 %b3 = extractelement <4 x float> %b, i32 3
215 %c3 = fadd float %a3, %b3
216 %v3 = insertelement <4 x float> %v2, float %c3, i32 3
217 ret <4 x float> %v3
218 }
219
196220 attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }