1 //===- SLPVectorizer.cpp - A bottom up SLP Vectorizer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This pass implements the Bottom Up SLP vectorizer. It detects consecutive
10 // stores that can be put together into vector-stores. Next, it attempts to
11 // construct vectorizable tree using the use-def chains. If a profitable tree
12 // was found, the SLP vectorizer performs vectorization on the tree.
14 // The pass is inspired by the work described in the paper:
15 // "Loop-Aware SLP in GCC" by Ira Rosen, Dorit Nuzman, Ayal Zaks.
17 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Vectorize.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/PostOrderIterator.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Analysis/ScalarEvolution.h"
25 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
26 #include "llvm/Analysis/TargetTransformInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/IRBuilder.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/NoFolder.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Transforms/Utils/VectorUtils.h"
48 #define SV_NAME "slp-vectorizer"
49 #define DEBUG_TYPE "SLP"
52 SLPCostThreshold("slp-threshold", cl::init(0), cl::Hidden,
53 cl::desc("Only vectorize if you gain more than this "
57 ShouldVectorizeHor("slp-vectorize-hor", cl::init(false), cl::Hidden,
58 cl::desc("Attempt to vectorize horizontal reductions"));
60 static cl::opt<bool> ShouldStartVectorizeHorAtStore(
61 "slp-vectorize-hor-store", cl::init(false), cl::Hidden,
63 "Attempt to vectorize horizontal reductions feeding into a store"));
67 static const unsigned MinVecRegSize = 128;
69 static const unsigned RecursionMaxDepth = 12;
71 /// A helper class for numbering instructions in multiple blocks.
72 /// Numbers start at zero for each basic block.
73 struct BlockNumbering {
75 BlockNumbering(BasicBlock *Bb) : BB(Bb), Valid(false) {}
77 void numberInstructions() {
81 // Number the instructions in the block.
82 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
84 InstrVec.push_back(it);
85 assert(InstrVec[InstrIdx[it]] == it && "Invalid allocation");
90 int getIndex(Instruction *I) {
91 assert(I->getParent() == BB && "Invalid instruction");
94 assert(InstrIdx.count(I) && "Unknown instruction");
98 Instruction *getInstruction(unsigned loc) {
100 numberInstructions();
101 assert(InstrVec.size() > loc && "Invalid Index");
102 return InstrVec[loc];
105 void forget() { Valid = false; }
108 /// The block we are numbering.
110 /// Is the block numbered.
112 /// Maps instructions to numbers and back.
113 SmallDenseMap<Instruction *, int> InstrIdx;
114 /// Maps integers to Instructions.
115 SmallVector<Instruction *, 32> InstrVec;
118 /// \returns the parent basic block if all of the instructions in \p VL
119 /// are in the same block or null otherwise.
120 static BasicBlock *getSameBlock(ArrayRef<Value *> VL) {
121 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
124 BasicBlock *BB = I0->getParent();
125 for (int i = 1, e = VL.size(); i < e; i++) {
126 Instruction *I = dyn_cast<Instruction>(VL[i]);
130 if (BB != I->getParent())
136 /// \returns True if all of the values in \p VL are constants.
137 static bool allConstant(ArrayRef<Value *> VL) {
138 for (unsigned i = 0, e = VL.size(); i < e; ++i)
139 if (!isa<Constant>(VL[i]))
144 /// \returns True if all of the values in \p VL are identical.
145 static bool isSplat(ArrayRef<Value *> VL) {
146 for (unsigned i = 1, e = VL.size(); i < e; ++i)
152 ///\returns Opcode that can be clubbed with \p Op to create an alternate
153 /// sequence which can later be merged as a ShuffleVector instruction.
154 static unsigned getAltOpcode(unsigned Op) {
156 case Instruction::FAdd:
157 return Instruction::FSub;
158 case Instruction::FSub:
159 return Instruction::FAdd;
160 case Instruction::Add:
161 return Instruction::Sub;
162 case Instruction::Sub:
163 return Instruction::Add;
169 ///\returns bool representing if Opcode \p Op can be part
170 /// of an alternate sequence which can later be merged as
171 /// a ShuffleVector instruction.
172 static bool canCombineAsAltInst(unsigned Op) {
173 if (Op == Instruction::FAdd || Op == Instruction::FSub ||
174 Op == Instruction::Sub || Op == Instruction::Add)
179 /// \returns ShuffleVector instruction if intructions in \p VL have
180 /// alternate fadd,fsub / fsub,fadd/add,sub/sub,add sequence.
181 /// (i.e. e.g. opcodes of fadd,fsub,fadd,fsub...)
182 static unsigned isAltInst(ArrayRef<Value *> VL) {
183 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
184 unsigned Opcode = I0->getOpcode();
185 unsigned AltOpcode = getAltOpcode(Opcode);
186 for (int i = 1, e = VL.size(); i < e; i++) {
187 Instruction *I = dyn_cast<Instruction>(VL[i]);
188 if (!I || I->getOpcode() != ((i & 1) ? AltOpcode : Opcode))
191 return Instruction::ShuffleVector;
194 /// \returns The opcode if all of the Instructions in \p VL have the same
196 static unsigned getSameOpcode(ArrayRef<Value *> VL) {
197 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
200 unsigned Opcode = I0->getOpcode();
201 for (int i = 1, e = VL.size(); i < e; i++) {
202 Instruction *I = dyn_cast<Instruction>(VL[i]);
203 if (!I || Opcode != I->getOpcode()) {
204 if (canCombineAsAltInst(Opcode) && i == 1)
205 return isAltInst(VL);
212 /// \returns \p I after propagating metadata from \p VL.
213 static Instruction *propagateMetadata(Instruction *I, ArrayRef<Value *> VL) {
214 Instruction *I0 = cast<Instruction>(VL[0]);
215 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
216 I0->getAllMetadataOtherThanDebugLoc(Metadata);
218 for (unsigned i = 0, n = Metadata.size(); i != n; ++i) {
219 unsigned Kind = Metadata[i].first;
220 MDNode *MD = Metadata[i].second;
222 for (int i = 1, e = VL.size(); MD && i != e; i++) {
223 Instruction *I = cast<Instruction>(VL[i]);
224 MDNode *IMD = I->getMetadata(Kind);
228 MD = nullptr; // Remove unknown metadata
230 case LLVMContext::MD_tbaa:
231 MD = MDNode::getMostGenericTBAA(MD, IMD);
233 case LLVMContext::MD_fpmath:
234 MD = MDNode::getMostGenericFPMath(MD, IMD);
238 I->setMetadata(Kind, MD);
243 /// \returns The type that all of the values in \p VL have or null if there
244 /// are different types.
245 static Type* getSameType(ArrayRef<Value *> VL) {
246 Type *Ty = VL[0]->getType();
247 for (int i = 1, e = VL.size(); i < e; i++)
248 if (VL[i]->getType() != Ty)
254 /// \returns True if the ExtractElement instructions in VL can be vectorized
255 /// to use the original vector.
256 static bool CanReuseExtract(ArrayRef<Value *> VL) {
257 assert(Instruction::ExtractElement == getSameOpcode(VL) && "Invalid opcode");
258 // Check if all of the extracts come from the same vector and from the
261 ExtractElementInst *E0 = cast<ExtractElementInst>(VL0);
262 Value *Vec = E0->getOperand(0);
264 // We have to extract from the same vector type.
265 unsigned NElts = Vec->getType()->getVectorNumElements();
267 if (NElts != VL.size())
270 // Check that all of the indices extract from the correct offset.
271 ConstantInt *CI = dyn_cast<ConstantInt>(E0->getOperand(1));
272 if (!CI || CI->getZExtValue())
275 for (unsigned i = 1, e = VL.size(); i < e; ++i) {
276 ExtractElementInst *E = cast<ExtractElementInst>(VL[i]);
277 ConstantInt *CI = dyn_cast<ConstantInt>(E->getOperand(1));
279 if (!CI || CI->getZExtValue() != i || E->getOperand(0) != Vec)
286 static void reorderInputsAccordingToOpcode(ArrayRef<Value *> VL,
287 SmallVectorImpl<Value *> &Left,
288 SmallVectorImpl<Value *> &Right) {
290 SmallVector<Value *, 16> OrigLeft, OrigRight;
292 bool AllSameOpcodeLeft = true;
293 bool AllSameOpcodeRight = true;
294 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
295 Instruction *I = cast<Instruction>(VL[i]);
296 Value *V0 = I->getOperand(0);
297 Value *V1 = I->getOperand(1);
299 OrigLeft.push_back(V0);
300 OrigRight.push_back(V1);
302 Instruction *I0 = dyn_cast<Instruction>(V0);
303 Instruction *I1 = dyn_cast<Instruction>(V1);
305 // Check whether all operands on one side have the same opcode. In this case
306 // we want to preserve the original order and not make things worse by
308 AllSameOpcodeLeft = I0;
309 AllSameOpcodeRight = I1;
311 if (i && AllSameOpcodeLeft) {
312 if(Instruction *P0 = dyn_cast<Instruction>(OrigLeft[i-1])) {
313 if(P0->getOpcode() != I0->getOpcode())
314 AllSameOpcodeLeft = false;
316 AllSameOpcodeLeft = false;
318 if (i && AllSameOpcodeRight) {
319 if(Instruction *P1 = dyn_cast<Instruction>(OrigRight[i-1])) {
320 if(P1->getOpcode() != I1->getOpcode())
321 AllSameOpcodeRight = false;
323 AllSameOpcodeRight = false;
326 // Sort two opcodes. In the code below we try to preserve the ability to use
327 // broadcast of values instead of individual inserts.
334 // If we just sorted according to opcode we would leave the first line in
335 // tact but we would swap vl2 with vr2 because opcode(phi) > opcode(load).
338 // Because vr2 and vr1 are from the same load we loose the opportunity of a
339 // broadcast for the packed right side in the backend: we have [vr1, vl2]
340 // instead of [vr1, vr2=vr1].
342 if(!i && I0->getOpcode() > I1->getOpcode()) {
345 } else if (i && I0->getOpcode() > I1->getOpcode() && Right[i-1] != I1) {
346 // Try not to destroy a broad cast for no apparent benefit.
349 } else if (i && I0->getOpcode() == I1->getOpcode() && Right[i-1] == I0) {
350 // Try preserve broadcasts.
353 } else if (i && I0->getOpcode() == I1->getOpcode() && Left[i-1] == I1) {
354 // Try preserve broadcasts.
363 // One opcode, put the instruction on the right.
373 bool LeftBroadcast = isSplat(Left);
374 bool RightBroadcast = isSplat(Right);
376 // Don't reorder if the operands where good to begin with.
377 if (!(LeftBroadcast || RightBroadcast) &&
378 (AllSameOpcodeRight || AllSameOpcodeLeft)) {
384 /// Bottom Up SLP Vectorizer.
387 typedef SmallVector<Value *, 8> ValueList;
388 typedef SmallVector<Instruction *, 16> InstrList;
389 typedef SmallPtrSet<Value *, 16> ValueSet;
390 typedef SmallVector<StoreInst *, 8> StoreList;
392 BoUpSLP(Function *Func, ScalarEvolution *Se, const DataLayout *Dl,
393 TargetTransformInfo *Tti, TargetLibraryInfo *TLi, AliasAnalysis *Aa,
394 LoopInfo *Li, DominatorTree *Dt)
395 : F(Func), SE(Se), DL(Dl), TTI(Tti), TLI(TLi), AA(Aa), LI(Li), DT(Dt),
396 Builder(Se->getContext()) {}
398 /// \brief Vectorize the tree that starts with the elements in \p VL.
399 /// Returns the vectorized root.
400 Value *vectorizeTree();
402 /// \returns the vectorization cost of the subtree that starts at \p VL.
403 /// A negative number means that this is profitable.
406 /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
407 /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
408 void buildTree(ArrayRef<Value *> Roots,
409 ArrayRef<Value *> UserIgnoreLst = None);
411 /// Clear the internal data structures that are created by 'buildTree'.
413 VectorizableTree.clear();
414 ScalarToTreeEntry.clear();
416 ExternalUses.clear();
417 MemBarrierIgnoreList.clear();
420 /// \returns true if the memory operations A and B are consecutive.
421 bool isConsecutiveAccess(Value *A, Value *B);
423 /// \brief Perform LICM and CSE on the newly generated gather sequences.
424 void optimizeGatherSequence();
429 /// \returns the cost of the vectorizable entry.
430 int getEntryCost(TreeEntry *E);
432 /// This is the recursive part of buildTree.
433 void buildTree_rec(ArrayRef<Value *> Roots, unsigned Depth);
435 /// Vectorize a single entry in the tree.
436 Value *vectorizeTree(TreeEntry *E);
438 /// Vectorize a single entry in the tree, starting in \p VL.
439 Value *vectorizeTree(ArrayRef<Value *> VL);
441 /// \returns the pointer to the vectorized value if \p VL is already
442 /// vectorized, or NULL. They may happen in cycles.
443 Value *alreadyVectorized(ArrayRef<Value *> VL) const;
445 /// \brief Take the pointer operand from the Load/Store instruction.
446 /// \returns NULL if this is not a valid Load/Store instruction.
447 static Value *getPointerOperand(Value *I);
449 /// \brief Take the address space operand from the Load/Store instruction.
450 /// \returns -1 if this is not a valid Load/Store instruction.
451 static unsigned getAddressSpaceOperand(Value *I);
453 /// \returns the scalarization cost for this type. Scalarization in this
454 /// context means the creation of vectors from a group of scalars.
455 int getGatherCost(Type *Ty);
457 /// \returns the scalarization cost for this list of values. Assuming that
458 /// this subtree gets vectorized, we may need to extract the values from the
459 /// roots. This method calculates the cost of extracting the values.
460 int getGatherCost(ArrayRef<Value *> VL);
462 /// \returns the AA location that is being access by the instruction.
463 AliasAnalysis::Location getLocation(Instruction *I);
465 /// \brief Checks if it is possible to sink an instruction from
466 /// \p Src to \p Dst.
467 /// \returns the pointer to the barrier instruction if we can't sink.
468 Value *getSinkBarrier(Instruction *Src, Instruction *Dst);
470 /// \returns the index of the last instruction in the BB from \p VL.
471 int getLastIndex(ArrayRef<Value *> VL);
473 /// \returns the Instruction in the bundle \p VL.
474 Instruction *getLastInstruction(ArrayRef<Value *> VL);
476 /// \brief Set the Builder insert point to one after the last instruction in
478 void setInsertPointAfterBundle(ArrayRef<Value *> VL);
480 /// \returns a vector from a collection of scalars in \p VL.
481 Value *Gather(ArrayRef<Value *> VL, VectorType *Ty);
483 /// \returns whether the VectorizableTree is fully vectoriable and will
484 /// be beneficial even the tree height is tiny.
485 bool isFullyVectorizableTinyTree();
488 TreeEntry() : Scalars(), VectorizedValue(nullptr), LastScalarIndex(0),
491 /// \returns true if the scalars in VL are equal to this entry.
492 bool isSame(ArrayRef<Value *> VL) const {
493 assert(VL.size() == Scalars.size() && "Invalid size");
494 return std::equal(VL.begin(), VL.end(), Scalars.begin());
497 /// A vector of scalars.
500 /// The Scalars are vectorized into this value. It is initialized to Null.
501 Value *VectorizedValue;
503 /// The index in the basic block of the last scalar.
506 /// Do we need to gather this sequence ?
510 /// Create a new VectorizableTree entry.
511 TreeEntry *newTreeEntry(ArrayRef<Value *> VL, bool Vectorized) {
512 VectorizableTree.push_back(TreeEntry());
513 int idx = VectorizableTree.size() - 1;
514 TreeEntry *Last = &VectorizableTree[idx];
515 Last->Scalars.insert(Last->Scalars.begin(), VL.begin(), VL.end());
516 Last->NeedToGather = !Vectorized;
518 Last->LastScalarIndex = getLastIndex(VL);
519 for (int i = 0, e = VL.size(); i != e; ++i) {
520 assert(!ScalarToTreeEntry.count(VL[i]) && "Scalar already in tree!");
521 ScalarToTreeEntry[VL[i]] = idx;
524 Last->LastScalarIndex = 0;
525 MustGather.insert(VL.begin(), VL.end());
530 /// -- Vectorization State --
531 /// Holds all of the tree entries.
532 std::vector<TreeEntry> VectorizableTree;
534 /// Maps a specific scalar to its tree entry.
535 SmallDenseMap<Value*, int> ScalarToTreeEntry;
537 /// A list of scalars that we found that we need to keep as scalars.
540 /// This POD struct describes one external user in the vectorized tree.
541 struct ExternalUser {
542 ExternalUser (Value *S, llvm::User *U, int L) :
543 Scalar(S), User(U), Lane(L){};
544 // Which scalar in our function.
546 // Which user that uses the scalar.
548 // Which lane does the scalar belong to.
551 typedef SmallVector<ExternalUser, 16> UserList;
553 /// A list of values that need to extracted out of the tree.
554 /// This list holds pairs of (Internal Scalar : External User).
555 UserList ExternalUses;
557 /// A list of instructions to ignore while sinking
558 /// memory instructions. This map must be reset between runs of getCost.
559 ValueSet MemBarrierIgnoreList;
561 /// Holds all of the instructions that we gathered.
562 SetVector<Instruction *> GatherSeq;
563 /// A list of blocks that we are going to CSE.
564 SetVector<BasicBlock *> CSEBlocks;
566 /// Numbers instructions in different blocks.
567 DenseMap<BasicBlock *, BlockNumbering> BlocksNumbers;
569 /// \brief Get the corresponding instruction numbering list for a given
570 /// BasicBlock. The list is allocated lazily.
571 BlockNumbering &getBlockNumbering(BasicBlock *BB) {
572 auto I = BlocksNumbers.insert(std::make_pair(BB, BlockNumbering(BB)));
573 return I.first->second;
576 /// List of users to ignore during scheduling and that don't need extracting.
577 ArrayRef<Value *> UserIgnoreList;
579 // Analysis and block reference.
582 const DataLayout *DL;
583 TargetTransformInfo *TTI;
584 TargetLibraryInfo *TLI;
588 /// Instruction builder to construct the vectorized tree.
592 void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
593 ArrayRef<Value *> UserIgnoreLst) {
595 UserIgnoreList = UserIgnoreLst;
596 if (!getSameType(Roots))
598 buildTree_rec(Roots, 0);
600 // Collect the values that we need to extract from the tree.
601 for (int EIdx = 0, EE = VectorizableTree.size(); EIdx < EE; ++EIdx) {
602 TreeEntry *Entry = &VectorizableTree[EIdx];
605 for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
606 Value *Scalar = Entry->Scalars[Lane];
608 // No need to handle users of gathered values.
609 if (Entry->NeedToGather)
612 for (User *U : Scalar->users()) {
613 DEBUG(dbgs() << "SLP: Checking user:" << *U << ".\n");
615 // Skip in-tree scalars that become vectors.
616 if (ScalarToTreeEntry.count(U)) {
617 DEBUG(dbgs() << "SLP: \tInternal user will be removed:" <<
619 int Idx = ScalarToTreeEntry[U]; (void) Idx;
620 assert(!VectorizableTree[Idx].NeedToGather && "Bad state");
623 Instruction *UserInst = dyn_cast<Instruction>(U);
627 // Ignore users in the user ignore list.
628 if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UserInst) !=
629 UserIgnoreList.end())
632 DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " <<
633 Lane << " from " << *Scalar << ".\n");
634 ExternalUses.push_back(ExternalUser(Scalar, U, Lane));
641 void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth) {
642 bool SameTy = getSameType(VL); (void)SameTy;
643 bool isAltShuffle = false;
644 assert(SameTy && "Invalid types!");
646 if (Depth == RecursionMaxDepth) {
647 DEBUG(dbgs() << "SLP: Gathering due to max recursion depth.\n");
648 newTreeEntry(VL, false);
652 // Don't handle vectors.
653 if (VL[0]->getType()->isVectorTy()) {
654 DEBUG(dbgs() << "SLP: Gathering due to vector type.\n");
655 newTreeEntry(VL, false);
659 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
660 if (SI->getValueOperand()->getType()->isVectorTy()) {
661 DEBUG(dbgs() << "SLP: Gathering due to store vector type.\n");
662 newTreeEntry(VL, false);
665 unsigned Opcode = getSameOpcode(VL);
667 // Check that this shuffle vector refers to the alternate
668 // sequence of opcodes.
669 if (Opcode == Instruction::ShuffleVector) {
670 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
671 unsigned Op = I0->getOpcode();
672 if (Op != Instruction::ShuffleVector)
676 // If all of the operands are identical or constant we have a simple solution.
677 if (allConstant(VL) || isSplat(VL) || !getSameBlock(VL) || !Opcode) {
678 DEBUG(dbgs() << "SLP: Gathering due to C,S,B,O. \n");
679 newTreeEntry(VL, false);
683 // We now know that this is a vector of instructions of the same type from
686 // Check if this is a duplicate of another entry.
687 if (ScalarToTreeEntry.count(VL[0])) {
688 int Idx = ScalarToTreeEntry[VL[0]];
689 TreeEntry *E = &VectorizableTree[Idx];
690 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
691 DEBUG(dbgs() << "SLP: \tChecking bundle: " << *VL[i] << ".\n");
692 if (E->Scalars[i] != VL[i]) {
693 DEBUG(dbgs() << "SLP: Gathering due to partial overlap.\n");
694 newTreeEntry(VL, false);
698 DEBUG(dbgs() << "SLP: Perfect diamond merge at " << *VL[0] << ".\n");
702 // Check that none of the instructions in the bundle are already in the tree.
703 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
704 if (ScalarToTreeEntry.count(VL[i])) {
705 DEBUG(dbgs() << "SLP: The instruction (" << *VL[i] <<
706 ") is already in tree.\n");
707 newTreeEntry(VL, false);
712 // If any of the scalars appears in the table OR it is marked as a value that
713 // needs to stat scalar then we need to gather the scalars.
714 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
715 if (ScalarToTreeEntry.count(VL[i]) || MustGather.count(VL[i])) {
716 DEBUG(dbgs() << "SLP: Gathering due to gathered scalar. \n");
717 newTreeEntry(VL, false);
722 // Check that all of the users of the scalars that we want to vectorize are
724 Instruction *VL0 = cast<Instruction>(VL[0]);
725 int MyLastIndex = getLastIndex(VL);
726 BasicBlock *BB = cast<Instruction>(VL0)->getParent();
728 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
729 Instruction *Scalar = cast<Instruction>(VL[i]);
730 DEBUG(dbgs() << "SLP: Checking users of " << *Scalar << ". \n");
731 for (User *U : Scalar->users()) {
732 DEBUG(dbgs() << "SLP: \tUser " << *U << ". \n");
733 Instruction *UI = dyn_cast<Instruction>(U);
735 DEBUG(dbgs() << "SLP: Gathering due unknown user. \n");
736 newTreeEntry(VL, false);
740 // We don't care if the user is in a different basic block.
741 BasicBlock *UserBlock = UI->getParent();
742 if (UserBlock != BB) {
743 DEBUG(dbgs() << "SLP: User from a different basic block "
748 // If this is a PHINode within this basic block then we can place the
749 // extract wherever we want.
750 if (isa<PHINode>(*UI)) {
751 DEBUG(dbgs() << "SLP: \tWe can schedule PHIs:" << *UI << ". \n");
755 // Check if this is a safe in-tree user.
756 if (ScalarToTreeEntry.count(UI)) {
757 int Idx = ScalarToTreeEntry[UI];
758 int VecLocation = VectorizableTree[Idx].LastScalarIndex;
759 if (VecLocation <= MyLastIndex) {
760 DEBUG(dbgs() << "SLP: Gathering due to unschedulable vector. \n");
761 newTreeEntry(VL, false);
764 DEBUG(dbgs() << "SLP: In-tree user (" << *UI << ") at #" <<
765 VecLocation << " vector value (" << *Scalar << ") at #"
766 << MyLastIndex << ".\n");
770 // Ignore users in the user ignore list.
771 if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UI) !=
772 UserIgnoreList.end())
775 // Make sure that we can schedule this unknown user.
776 BlockNumbering &BN = getBlockNumbering(BB);
777 int UserIndex = BN.getIndex(UI);
778 if (UserIndex < MyLastIndex) {
780 DEBUG(dbgs() << "SLP: Can't schedule extractelement for "
782 newTreeEntry(VL, false);
788 // Check that every instructions appears once in this bundle.
789 for (unsigned i = 0, e = VL.size(); i < e; ++i)
790 for (unsigned j = i+1; j < e; ++j)
791 if (VL[i] == VL[j]) {
792 DEBUG(dbgs() << "SLP: Scalar used twice in bundle.\n");
793 newTreeEntry(VL, false);
797 // Check that instructions in this bundle don't reference other instructions.
798 // The runtime of this check is O(N * N-1 * uses(N)) and a typical N is 4.
799 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
800 for (User *U : VL[i]->users()) {
801 for (unsigned j = 0; j < e; ++j) {
802 if (i != j && U == VL[j]) {
803 DEBUG(dbgs() << "SLP: Intra-bundle dependencies!" << *U << ". \n");
804 newTreeEntry(VL, false);
811 DEBUG(dbgs() << "SLP: We are able to schedule this bundle.\n");
813 // Check if it is safe to sink the loads or the stores.
814 if (Opcode == Instruction::Load || Opcode == Instruction::Store) {
815 Instruction *Last = getLastInstruction(VL);
817 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
820 Value *Barrier = getSinkBarrier(cast<Instruction>(VL[i]), Last);
822 DEBUG(dbgs() << "SLP: Can't sink " << *VL[i] << "\n down to " << *Last
823 << "\n because of " << *Barrier << ". Gathering.\n");
824 newTreeEntry(VL, false);
831 case Instruction::PHI: {
832 PHINode *PH = dyn_cast<PHINode>(VL0);
834 // Check for terminator values (e.g. invoke).
835 for (unsigned j = 0; j < VL.size(); ++j)
836 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
837 TerminatorInst *Term = dyn_cast<TerminatorInst>(
838 cast<PHINode>(VL[j])->getIncomingValueForBlock(PH->getIncomingBlock(i)));
840 DEBUG(dbgs() << "SLP: Need to swizzle PHINodes (TerminatorInst use).\n");
841 newTreeEntry(VL, false);
846 newTreeEntry(VL, true);
847 DEBUG(dbgs() << "SLP: added a vector of PHINodes.\n");
849 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
851 // Prepare the operand vector.
852 for (unsigned j = 0; j < VL.size(); ++j)
853 Operands.push_back(cast<PHINode>(VL[j])->getIncomingValueForBlock(
854 PH->getIncomingBlock(i)));
856 buildTree_rec(Operands, Depth + 1);
860 case Instruction::ExtractElement: {
861 bool Reuse = CanReuseExtract(VL);
863 DEBUG(dbgs() << "SLP: Reusing extract sequence.\n");
865 newTreeEntry(VL, Reuse);
868 case Instruction::Load: {
869 // Check if the loads are consecutive or of we need to swizzle them.
870 for (unsigned i = 0, e = VL.size() - 1; i < e; ++i) {
871 LoadInst *L = cast<LoadInst>(VL[i]);
872 if (!L->isSimple() || !isConsecutiveAccess(VL[i], VL[i + 1])) {
873 newTreeEntry(VL, false);
874 DEBUG(dbgs() << "SLP: Need to swizzle loads.\n");
878 newTreeEntry(VL, true);
879 DEBUG(dbgs() << "SLP: added a vector of loads.\n");
882 case Instruction::ZExt:
883 case Instruction::SExt:
884 case Instruction::FPToUI:
885 case Instruction::FPToSI:
886 case Instruction::FPExt:
887 case Instruction::PtrToInt:
888 case Instruction::IntToPtr:
889 case Instruction::SIToFP:
890 case Instruction::UIToFP:
891 case Instruction::Trunc:
892 case Instruction::FPTrunc:
893 case Instruction::BitCast: {
894 Type *SrcTy = VL0->getOperand(0)->getType();
895 for (unsigned i = 0; i < VL.size(); ++i) {
896 Type *Ty = cast<Instruction>(VL[i])->getOperand(0)->getType();
897 if (Ty != SrcTy || Ty->isAggregateType() || Ty->isVectorTy()) {
898 newTreeEntry(VL, false);
899 DEBUG(dbgs() << "SLP: Gathering casts with different src types.\n");
903 newTreeEntry(VL, true);
904 DEBUG(dbgs() << "SLP: added a vector of casts.\n");
906 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
908 // Prepare the operand vector.
909 for (unsigned j = 0; j < VL.size(); ++j)
910 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
912 buildTree_rec(Operands, Depth+1);
916 case Instruction::ICmp:
917 case Instruction::FCmp: {
918 // Check that all of the compares have the same predicate.
919 CmpInst::Predicate P0 = dyn_cast<CmpInst>(VL0)->getPredicate();
920 Type *ComparedTy = cast<Instruction>(VL[0])->getOperand(0)->getType();
921 for (unsigned i = 1, e = VL.size(); i < e; ++i) {
922 CmpInst *Cmp = cast<CmpInst>(VL[i]);
923 if (Cmp->getPredicate() != P0 ||
924 Cmp->getOperand(0)->getType() != ComparedTy) {
925 newTreeEntry(VL, false);
926 DEBUG(dbgs() << "SLP: Gathering cmp with different predicate.\n");
931 newTreeEntry(VL, true);
932 DEBUG(dbgs() << "SLP: added a vector of compares.\n");
934 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
936 // Prepare the operand vector.
937 for (unsigned j = 0; j < VL.size(); ++j)
938 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
940 buildTree_rec(Operands, Depth+1);
944 case Instruction::Select:
945 case Instruction::Add:
946 case Instruction::FAdd:
947 case Instruction::Sub:
948 case Instruction::FSub:
949 case Instruction::Mul:
950 case Instruction::FMul:
951 case Instruction::UDiv:
952 case Instruction::SDiv:
953 case Instruction::FDiv:
954 case Instruction::URem:
955 case Instruction::SRem:
956 case Instruction::FRem:
957 case Instruction::Shl:
958 case Instruction::LShr:
959 case Instruction::AShr:
960 case Instruction::And:
961 case Instruction::Or:
962 case Instruction::Xor: {
963 newTreeEntry(VL, true);
964 DEBUG(dbgs() << "SLP: added a vector of bin op.\n");
966 // Sort operands of the instructions so that each side is more likely to
967 // have the same opcode.
968 if (isa<BinaryOperator>(VL0) && VL0->isCommutative()) {
969 ValueList Left, Right;
970 reorderInputsAccordingToOpcode(VL, Left, Right);
971 BasicBlock *LeftBB = getSameBlock(Left);
972 BasicBlock *RightBB = getSameBlock(Right);
973 // If we have common uses on separate paths in the tree make sure we
974 // process the one with greater common depth first.
975 // We can use block numbering to determine the subtree traversal as
976 // earler user has to come in between the common use and the later user.
977 if (LeftBB && RightBB && LeftBB == RightBB &&
978 getLastIndex(Right) > getLastIndex(Left)) {
979 buildTree_rec(Right, Depth + 1);
980 buildTree_rec(Left, Depth + 1);
982 buildTree_rec(Left, Depth + 1);
983 buildTree_rec(Right, Depth + 1);
988 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
990 // Prepare the operand vector.
991 for (unsigned j = 0; j < VL.size(); ++j)
992 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
994 buildTree_rec(Operands, Depth+1);
998 case Instruction::GetElementPtr: {
999 // We don't combine GEPs with complicated (nested) indexing.
1000 for (unsigned j = 0; j < VL.size(); ++j) {
1001 if (cast<Instruction>(VL[j])->getNumOperands() != 2) {
1002 DEBUG(dbgs() << "SLP: not-vectorizable GEP (nested indexes).\n");
1003 newTreeEntry(VL, false);
1008 // We can't combine several GEPs into one vector if they operate on
1010 Type *Ty0 = cast<Instruction>(VL0)->getOperand(0)->getType();
1011 for (unsigned j = 0; j < VL.size(); ++j) {
1012 Type *CurTy = cast<Instruction>(VL[j])->getOperand(0)->getType();
1014 DEBUG(dbgs() << "SLP: not-vectorizable GEP (different types).\n");
1015 newTreeEntry(VL, false);
1020 // We don't combine GEPs with non-constant indexes.
1021 for (unsigned j = 0; j < VL.size(); ++j) {
1022 auto Op = cast<Instruction>(VL[j])->getOperand(1);
1023 if (!isa<ConstantInt>(Op)) {
1025 dbgs() << "SLP: not-vectorizable GEP (non-constant indexes).\n");
1026 newTreeEntry(VL, false);
1031 newTreeEntry(VL, true);
1032 DEBUG(dbgs() << "SLP: added a vector of GEPs.\n");
1033 for (unsigned i = 0, e = 2; i < e; ++i) {
1035 // Prepare the operand vector.
1036 for (unsigned j = 0; j < VL.size(); ++j)
1037 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1039 buildTree_rec(Operands, Depth + 1);
1043 case Instruction::Store: {
1044 // Check if the stores are consecutive or of we need to swizzle them.
1045 for (unsigned i = 0, e = VL.size() - 1; i < e; ++i)
1046 if (!isConsecutiveAccess(VL[i], VL[i + 1])) {
1047 newTreeEntry(VL, false);
1048 DEBUG(dbgs() << "SLP: Non-consecutive store.\n");
1052 newTreeEntry(VL, true);
1053 DEBUG(dbgs() << "SLP: added a vector of stores.\n");
1056 for (unsigned j = 0; j < VL.size(); ++j)
1057 Operands.push_back(cast<Instruction>(VL[j])->getOperand(0));
1059 // We can ignore these values because we are sinking them down.
1060 MemBarrierIgnoreList.insert(VL.begin(), VL.end());
1061 buildTree_rec(Operands, Depth + 1);
1064 case Instruction::Call: {
1065 // Check if the calls are all to the same vectorizable intrinsic.
1066 CallInst *CI = cast<CallInst>(VL[0]);
1067 // Check if this is an Intrinsic call or something that can be
1068 // represented by an intrinsic call
1069 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
1070 if (!isTriviallyVectorizable(ID)) {
1071 newTreeEntry(VL, false);
1072 DEBUG(dbgs() << "SLP: Non-vectorizable call.\n");
1075 Function *Int = CI->getCalledFunction();
1076 Value *A1I = nullptr;
1077 if (hasVectorInstrinsicScalarOpd(ID, 1))
1078 A1I = CI->getArgOperand(1);
1079 for (unsigned i = 1, e = VL.size(); i != e; ++i) {
1080 CallInst *CI2 = dyn_cast<CallInst>(VL[i]);
1081 if (!CI2 || CI2->getCalledFunction() != Int ||
1082 getIntrinsicIDForCall(CI2, TLI) != ID) {
1083 newTreeEntry(VL, false);
1084 DEBUG(dbgs() << "SLP: mismatched calls:" << *CI << "!=" << *VL[i]
1088 // ctlz,cttz and powi are special intrinsics whose second argument
1089 // should be same in order for them to be vectorized.
1090 if (hasVectorInstrinsicScalarOpd(ID, 1)) {
1091 Value *A1J = CI2->getArgOperand(1);
1093 newTreeEntry(VL, false);
1094 DEBUG(dbgs() << "SLP: mismatched arguments in call:" << *CI
1095 << " argument "<< A1I<<"!=" << A1J
1102 newTreeEntry(VL, true);
1103 for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) {
1105 // Prepare the operand vector.
1106 for (unsigned j = 0; j < VL.size(); ++j) {
1107 CallInst *CI2 = dyn_cast<CallInst>(VL[j]);
1108 Operands.push_back(CI2->getArgOperand(i));
1110 buildTree_rec(Operands, Depth + 1);
1114 case Instruction::ShuffleVector: {
1115 // If this is not an alternate sequence of opcode like add-sub
1116 // then do not vectorize this instruction.
1117 if (!isAltShuffle) {
1118 newTreeEntry(VL, false);
1119 DEBUG(dbgs() << "SLP: ShuffleVector are not vectorized.\n");
1122 newTreeEntry(VL, true);
1123 DEBUG(dbgs() << "SLP: added a ShuffleVector op.\n");
1124 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1126 // Prepare the operand vector.
1127 for (unsigned j = 0; j < VL.size(); ++j)
1128 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1130 buildTree_rec(Operands, Depth + 1);
1135 newTreeEntry(VL, false);
1136 DEBUG(dbgs() << "SLP: Gathering unknown instruction.\n");
1141 int BoUpSLP::getEntryCost(TreeEntry *E) {
1142 ArrayRef<Value*> VL = E->Scalars;
1144 Type *ScalarTy = VL[0]->getType();
1145 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1146 ScalarTy = SI->getValueOperand()->getType();
1147 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1149 if (E->NeedToGather) {
1150 if (allConstant(VL))
1153 return TTI->getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy, 0);
1155 return getGatherCost(E->Scalars);
1157 unsigned Opcode = getSameOpcode(VL);
1158 assert(Opcode && getSameType(VL) && getSameBlock(VL) && "Invalid VL");
1159 Instruction *VL0 = cast<Instruction>(VL[0]);
1161 case Instruction::PHI: {
1164 case Instruction::ExtractElement: {
1165 if (CanReuseExtract(VL)) {
1167 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
1168 ExtractElementInst *E = cast<ExtractElementInst>(VL[i]);
1170 // Take credit for instruction that will become dead.
1172 TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, i);
1176 return getGatherCost(VecTy);
1178 case Instruction::ZExt:
1179 case Instruction::SExt:
1180 case Instruction::FPToUI:
1181 case Instruction::FPToSI:
1182 case Instruction::FPExt:
1183 case Instruction::PtrToInt:
1184 case Instruction::IntToPtr:
1185 case Instruction::SIToFP:
1186 case Instruction::UIToFP:
1187 case Instruction::Trunc:
1188 case Instruction::FPTrunc:
1189 case Instruction::BitCast: {
1190 Type *SrcTy = VL0->getOperand(0)->getType();
1192 // Calculate the cost of this instruction.
1193 int ScalarCost = VL.size() * TTI->getCastInstrCost(VL0->getOpcode(),
1194 VL0->getType(), SrcTy);
1196 VectorType *SrcVecTy = VectorType::get(SrcTy, VL.size());
1197 int VecCost = TTI->getCastInstrCost(VL0->getOpcode(), VecTy, SrcVecTy);
1198 return VecCost - ScalarCost;
1200 case Instruction::FCmp:
1201 case Instruction::ICmp:
1202 case Instruction::Select:
1203 case Instruction::Add:
1204 case Instruction::FAdd:
1205 case Instruction::Sub:
1206 case Instruction::FSub:
1207 case Instruction::Mul:
1208 case Instruction::FMul:
1209 case Instruction::UDiv:
1210 case Instruction::SDiv:
1211 case Instruction::FDiv:
1212 case Instruction::URem:
1213 case Instruction::SRem:
1214 case Instruction::FRem:
1215 case Instruction::Shl:
1216 case Instruction::LShr:
1217 case Instruction::AShr:
1218 case Instruction::And:
1219 case Instruction::Or:
1220 case Instruction::Xor: {
1221 // Calculate the cost of this instruction.
1224 if (Opcode == Instruction::FCmp || Opcode == Instruction::ICmp ||
1225 Opcode == Instruction::Select) {
1226 VectorType *MaskTy = VectorType::get(Builder.getInt1Ty(), VL.size());
1227 ScalarCost = VecTy->getNumElements() *
1228 TTI->getCmpSelInstrCost(Opcode, ScalarTy, Builder.getInt1Ty());
1229 VecCost = TTI->getCmpSelInstrCost(Opcode, VecTy, MaskTy);
1231 // Certain instructions can be cheaper to vectorize if they have a
1232 // constant second vector operand.
1233 TargetTransformInfo::OperandValueKind Op1VK =
1234 TargetTransformInfo::OK_AnyValue;
1235 TargetTransformInfo::OperandValueKind Op2VK =
1236 TargetTransformInfo::OK_UniformConstantValue;
1238 // If all operands are exactly the same ConstantInt then set the
1239 // operand kind to OK_UniformConstantValue.
1240 // If instead not all operands are constants, then set the operand kind
1241 // to OK_AnyValue. If all operands are constants but not the same,
1242 // then set the operand kind to OK_NonUniformConstantValue.
1243 ConstantInt *CInt = nullptr;
1244 for (unsigned i = 0; i < VL.size(); ++i) {
1245 const Instruction *I = cast<Instruction>(VL[i]);
1246 if (!isa<ConstantInt>(I->getOperand(1))) {
1247 Op2VK = TargetTransformInfo::OK_AnyValue;
1251 CInt = cast<ConstantInt>(I->getOperand(1));
1254 if (Op2VK == TargetTransformInfo::OK_UniformConstantValue &&
1255 CInt != cast<ConstantInt>(I->getOperand(1)))
1256 Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
1260 VecTy->getNumElements() *
1261 TTI->getArithmeticInstrCost(Opcode, ScalarTy, Op1VK, Op2VK);
1262 VecCost = TTI->getArithmeticInstrCost(Opcode, VecTy, Op1VK, Op2VK);
1264 return VecCost - ScalarCost;
1266 case Instruction::GetElementPtr: {
1267 TargetTransformInfo::OperandValueKind Op1VK =
1268 TargetTransformInfo::OK_AnyValue;
1269 TargetTransformInfo::OperandValueKind Op2VK =
1270 TargetTransformInfo::OK_UniformConstantValue;
1273 VecTy->getNumElements() *
1274 TTI->getArithmeticInstrCost(Instruction::Add, ScalarTy, Op1VK, Op2VK);
1276 TTI->getArithmeticInstrCost(Instruction::Add, VecTy, Op1VK, Op2VK);
1278 return VecCost - ScalarCost;
1280 case Instruction::Load: {
1281 // Cost of wide load - cost of scalar loads.
1282 int ScalarLdCost = VecTy->getNumElements() *
1283 TTI->getMemoryOpCost(Instruction::Load, ScalarTy, 1, 0);
1284 int VecLdCost = TTI->getMemoryOpCost(Instruction::Load, VecTy, 1, 0);
1285 return VecLdCost - ScalarLdCost;
1287 case Instruction::Store: {
1288 // We know that we can merge the stores. Calculate the cost.
1289 int ScalarStCost = VecTy->getNumElements() *
1290 TTI->getMemoryOpCost(Instruction::Store, ScalarTy, 1, 0);
1291 int VecStCost = TTI->getMemoryOpCost(Instruction::Store, VecTy, 1, 0);
1292 return VecStCost - ScalarStCost;
1294 case Instruction::Call: {
1295 CallInst *CI = cast<CallInst>(VL0);
1296 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
1298 // Calculate the cost of the scalar and vector calls.
1299 SmallVector<Type*, 4> ScalarTys, VecTys;
1300 for (unsigned op = 0, opc = CI->getNumArgOperands(); op!= opc; ++op) {
1301 ScalarTys.push_back(CI->getArgOperand(op)->getType());
1302 VecTys.push_back(VectorType::get(CI->getArgOperand(op)->getType(),
1303 VecTy->getNumElements()));
1306 int ScalarCallCost = VecTy->getNumElements() *
1307 TTI->getIntrinsicInstrCost(ID, ScalarTy, ScalarTys);
1309 int VecCallCost = TTI->getIntrinsicInstrCost(ID, VecTy, VecTys);
1311 DEBUG(dbgs() << "SLP: Call cost "<< VecCallCost - ScalarCallCost
1312 << " (" << VecCallCost << "-" << ScalarCallCost << ")"
1313 << " for " << *CI << "\n");
1315 return VecCallCost - ScalarCallCost;
1317 case Instruction::ShuffleVector: {
1318 TargetTransformInfo::OperandValueKind Op1VK =
1319 TargetTransformInfo::OK_AnyValue;
1320 TargetTransformInfo::OperandValueKind Op2VK =
1321 TargetTransformInfo::OK_AnyValue;
1324 for (unsigned i = 0; i < VL.size(); ++i) {
1325 Instruction *I = cast<Instruction>(VL[i]);
1329 TTI->getArithmeticInstrCost(I->getOpcode(), ScalarTy, Op1VK, Op2VK);
1331 // VecCost is equal to sum of the cost of creating 2 vectors
1332 // and the cost of creating shuffle.
1333 Instruction *I0 = cast<Instruction>(VL[0]);
1335 TTI->getArithmeticInstrCost(I0->getOpcode(), VecTy, Op1VK, Op2VK);
1336 Instruction *I1 = cast<Instruction>(VL[1]);
1338 TTI->getArithmeticInstrCost(I1->getOpcode(), VecTy, Op1VK, Op2VK);
1340 TTI->getShuffleCost(TargetTransformInfo::SK_Alternate, VecTy, 0);
1341 return VecCost - ScalarCost;
1344 llvm_unreachable("Unknown instruction");
1348 bool BoUpSLP::isFullyVectorizableTinyTree() {
1349 DEBUG(dbgs() << "SLP: Check whether the tree with height " <<
1350 VectorizableTree.size() << " is fully vectorizable .\n");
1352 // We only handle trees of height 2.
1353 if (VectorizableTree.size() != 2)
1356 // Handle splat stores.
1357 if (!VectorizableTree[0].NeedToGather && isSplat(VectorizableTree[1].Scalars))
1360 // Gathering cost would be too much for tiny trees.
1361 if (VectorizableTree[0].NeedToGather || VectorizableTree[1].NeedToGather)
1367 int BoUpSLP::getTreeCost() {
1369 DEBUG(dbgs() << "SLP: Calculating cost for tree of size " <<
1370 VectorizableTree.size() << ".\n");
1372 // We only vectorize tiny trees if it is fully vectorizable.
1373 if (VectorizableTree.size() < 3 && !isFullyVectorizableTinyTree()) {
1374 if (!VectorizableTree.size()) {
1375 assert(!ExternalUses.size() && "We should not have any external users");
1380 unsigned BundleWidth = VectorizableTree[0].Scalars.size();
1382 for (unsigned i = 0, e = VectorizableTree.size(); i != e; ++i) {
1383 int C = getEntryCost(&VectorizableTree[i]);
1384 DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle that starts with "
1385 << *VectorizableTree[i].Scalars[0] << " .\n");
1389 SmallSet<Value *, 16> ExtractCostCalculated;
1390 int ExtractCost = 0;
1391 for (UserList::iterator I = ExternalUses.begin(), E = ExternalUses.end();
1393 // We only add extract cost once for the same scalar.
1394 if (!ExtractCostCalculated.insert(I->Scalar))
1397 VectorType *VecTy = VectorType::get(I->Scalar->getType(), BundleWidth);
1398 ExtractCost += TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy,
1402 DEBUG(dbgs() << "SLP: Total Cost " << Cost + ExtractCost<< ".\n");
1403 return Cost + ExtractCost;
1406 int BoUpSLP::getGatherCost(Type *Ty) {
1408 for (unsigned i = 0, e = cast<VectorType>(Ty)->getNumElements(); i < e; ++i)
1409 Cost += TTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
1413 int BoUpSLP::getGatherCost(ArrayRef<Value *> VL) {
1414 // Find the type of the operands in VL.
1415 Type *ScalarTy = VL[0]->getType();
1416 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1417 ScalarTy = SI->getValueOperand()->getType();
1418 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1419 // Find the cost of inserting/extracting values from the vector.
1420 return getGatherCost(VecTy);
1423 AliasAnalysis::Location BoUpSLP::getLocation(Instruction *I) {
1424 if (StoreInst *SI = dyn_cast<StoreInst>(I))
1425 return AA->getLocation(SI);
1426 if (LoadInst *LI = dyn_cast<LoadInst>(I))
1427 return AA->getLocation(LI);
1428 return AliasAnalysis::Location();
1431 Value *BoUpSLP::getPointerOperand(Value *I) {
1432 if (LoadInst *LI = dyn_cast<LoadInst>(I))
1433 return LI->getPointerOperand();
1434 if (StoreInst *SI = dyn_cast<StoreInst>(I))
1435 return SI->getPointerOperand();
1439 unsigned BoUpSLP::getAddressSpaceOperand(Value *I) {
1440 if (LoadInst *L = dyn_cast<LoadInst>(I))
1441 return L->getPointerAddressSpace();
1442 if (StoreInst *S = dyn_cast<StoreInst>(I))
1443 return S->getPointerAddressSpace();
1447 bool BoUpSLP::isConsecutiveAccess(Value *A, Value *B) {
1448 Value *PtrA = getPointerOperand(A);
1449 Value *PtrB = getPointerOperand(B);
1450 unsigned ASA = getAddressSpaceOperand(A);
1451 unsigned ASB = getAddressSpaceOperand(B);
1453 // Check that the address spaces match and that the pointers are valid.
1454 if (!PtrA || !PtrB || (ASA != ASB))
1457 // Make sure that A and B are different pointers of the same type.
1458 if (PtrA == PtrB || PtrA->getType() != PtrB->getType())
1461 unsigned PtrBitWidth = DL->getPointerSizeInBits(ASA);
1462 Type *Ty = cast<PointerType>(PtrA->getType())->getElementType();
1463 APInt Size(PtrBitWidth, DL->getTypeStoreSize(Ty));
1465 APInt OffsetA(PtrBitWidth, 0), OffsetB(PtrBitWidth, 0);
1466 PtrA = PtrA->stripAndAccumulateInBoundsConstantOffsets(*DL, OffsetA);
1467 PtrB = PtrB->stripAndAccumulateInBoundsConstantOffsets(*DL, OffsetB);
1469 APInt OffsetDelta = OffsetB - OffsetA;
1471 // Check if they are based on the same pointer. That makes the offsets
1474 return OffsetDelta == Size;
1476 // Compute the necessary base pointer delta to have the necessary final delta
1477 // equal to the size.
1478 APInt BaseDelta = Size - OffsetDelta;
1480 // Otherwise compute the distance with SCEV between the base pointers.
1481 const SCEV *PtrSCEVA = SE->getSCEV(PtrA);
1482 const SCEV *PtrSCEVB = SE->getSCEV(PtrB);
1483 const SCEV *C = SE->getConstant(BaseDelta);
1484 const SCEV *X = SE->getAddExpr(PtrSCEVA, C);
1485 return X == PtrSCEVB;
1488 Value *BoUpSLP::getSinkBarrier(Instruction *Src, Instruction *Dst) {
1489 assert(Src->getParent() == Dst->getParent() && "Not the same BB");
1490 BasicBlock::iterator I = Src, E = Dst;
1491 /// Scan all of the instruction from SRC to DST and check if
1492 /// the source may alias.
1493 for (++I; I != E; ++I) {
1494 // Ignore store instructions that are marked as 'ignore'.
1495 if (MemBarrierIgnoreList.count(I))
1497 if (Src->mayWriteToMemory()) /* Write */ {
1498 if (!I->mayReadOrWriteMemory())
1501 if (!I->mayWriteToMemory())
1504 AliasAnalysis::Location A = getLocation(&*I);
1505 AliasAnalysis::Location B = getLocation(Src);
1507 if (!A.Ptr || !B.Ptr || AA->alias(A, B))
1513 int BoUpSLP::getLastIndex(ArrayRef<Value *> VL) {
1514 BasicBlock *BB = cast<Instruction>(VL[0])->getParent();
1515 assert(BB == getSameBlock(VL) && "Invalid block");
1516 BlockNumbering &BN = getBlockNumbering(BB);
1518 int MaxIdx = BN.getIndex(BB->getFirstNonPHI());
1519 for (unsigned i = 0, e = VL.size(); i < e; ++i)
1520 MaxIdx = std::max(MaxIdx, BN.getIndex(cast<Instruction>(VL[i])));
1524 Instruction *BoUpSLP::getLastInstruction(ArrayRef<Value *> VL) {
1525 BasicBlock *BB = cast<Instruction>(VL[0])->getParent();
1526 assert(BB == getSameBlock(VL) && "Invalid block");
1527 BlockNumbering &BN = getBlockNumbering(BB);
1529 int MaxIdx = BN.getIndex(cast<Instruction>(VL[0]));
1530 for (unsigned i = 1, e = VL.size(); i < e; ++i)
1531 MaxIdx = std::max(MaxIdx, BN.getIndex(cast<Instruction>(VL[i])));
1532 Instruction *I = BN.getInstruction(MaxIdx);
1533 assert(I && "bad location");
1537 void BoUpSLP::setInsertPointAfterBundle(ArrayRef<Value *> VL) {
1538 Instruction *VL0 = cast<Instruction>(VL[0]);
1539 Instruction *LastInst = getLastInstruction(VL);
1540 BasicBlock::iterator NextInst = LastInst;
1542 Builder.SetInsertPoint(VL0->getParent(), NextInst);
1543 Builder.SetCurrentDebugLocation(VL0->getDebugLoc());
1546 Value *BoUpSLP::Gather(ArrayRef<Value *> VL, VectorType *Ty) {
1547 Value *Vec = UndefValue::get(Ty);
1548 // Generate the 'InsertElement' instruction.
1549 for (unsigned i = 0; i < Ty->getNumElements(); ++i) {
1550 Vec = Builder.CreateInsertElement(Vec, VL[i], Builder.getInt32(i));
1551 if (Instruction *Insrt = dyn_cast<Instruction>(Vec)) {
1552 GatherSeq.insert(Insrt);
1553 CSEBlocks.insert(Insrt->getParent());
1555 // Add to our 'need-to-extract' list.
1556 if (ScalarToTreeEntry.count(VL[i])) {
1557 int Idx = ScalarToTreeEntry[VL[i]];
1558 TreeEntry *E = &VectorizableTree[Idx];
1559 // Find which lane we need to extract.
1561 for (unsigned Lane = 0, LE = VL.size(); Lane != LE; ++Lane) {
1562 // Is this the lane of the scalar that we are looking for ?
1563 if (E->Scalars[Lane] == VL[i]) {
1568 assert(FoundLane >= 0 && "Could not find the correct lane");
1569 ExternalUses.push_back(ExternalUser(VL[i], Insrt, FoundLane));
1577 Value *BoUpSLP::alreadyVectorized(ArrayRef<Value *> VL) const {
1578 SmallDenseMap<Value*, int>::const_iterator Entry
1579 = ScalarToTreeEntry.find(VL[0]);
1580 if (Entry != ScalarToTreeEntry.end()) {
1581 int Idx = Entry->second;
1582 const TreeEntry *En = &VectorizableTree[Idx];
1583 if (En->isSame(VL) && En->VectorizedValue)
1584 return En->VectorizedValue;
1589 Value *BoUpSLP::vectorizeTree(ArrayRef<Value *> VL) {
1590 if (ScalarToTreeEntry.count(VL[0])) {
1591 int Idx = ScalarToTreeEntry[VL[0]];
1592 TreeEntry *E = &VectorizableTree[Idx];
1594 return vectorizeTree(E);
1597 Type *ScalarTy = VL[0]->getType();
1598 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1599 ScalarTy = SI->getValueOperand()->getType();
1600 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1602 return Gather(VL, VecTy);
1605 Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
1606 IRBuilder<>::InsertPointGuard Guard(Builder);
1608 if (E->VectorizedValue) {
1609 DEBUG(dbgs() << "SLP: Diamond merged for " << *E->Scalars[0] << ".\n");
1610 return E->VectorizedValue;
1613 Instruction *VL0 = cast<Instruction>(E->Scalars[0]);
1614 Type *ScalarTy = VL0->getType();
1615 if (StoreInst *SI = dyn_cast<StoreInst>(VL0))
1616 ScalarTy = SI->getValueOperand()->getType();
1617 VectorType *VecTy = VectorType::get(ScalarTy, E->Scalars.size());
1619 if (E->NeedToGather) {
1620 setInsertPointAfterBundle(E->Scalars);
1621 return Gather(E->Scalars, VecTy);
1623 unsigned Opcode = getSameOpcode(E->Scalars);
1626 case Instruction::PHI: {
1627 PHINode *PH = dyn_cast<PHINode>(VL0);
1628 Builder.SetInsertPoint(PH->getParent()->getFirstNonPHI());
1629 Builder.SetCurrentDebugLocation(PH->getDebugLoc());
1630 PHINode *NewPhi = Builder.CreatePHI(VecTy, PH->getNumIncomingValues());
1631 E->VectorizedValue = NewPhi;
1633 // PHINodes may have multiple entries from the same block. We want to
1634 // visit every block once.
1635 SmallSet<BasicBlock*, 4> VisitedBBs;
1637 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1639 BasicBlock *IBB = PH->getIncomingBlock(i);
1641 if (!VisitedBBs.insert(IBB)) {
1642 NewPhi->addIncoming(NewPhi->getIncomingValueForBlock(IBB), IBB);
1646 // Prepare the operand vector.
1647 for (unsigned j = 0; j < E->Scalars.size(); ++j)
1648 Operands.push_back(cast<PHINode>(E->Scalars[j])->
1649 getIncomingValueForBlock(IBB));
1651 Builder.SetInsertPoint(IBB->getTerminator());
1652 Builder.SetCurrentDebugLocation(PH->getDebugLoc());
1653 Value *Vec = vectorizeTree(Operands);
1654 NewPhi->addIncoming(Vec, IBB);
1657 assert(NewPhi->getNumIncomingValues() == PH->getNumIncomingValues() &&
1658 "Invalid number of incoming values");
1662 case Instruction::ExtractElement: {
1663 if (CanReuseExtract(E->Scalars)) {
1664 Value *V = VL0->getOperand(0);
1665 E->VectorizedValue = V;
1668 return Gather(E->Scalars, VecTy);
1670 case Instruction::ZExt:
1671 case Instruction::SExt:
1672 case Instruction::FPToUI:
1673 case Instruction::FPToSI:
1674 case Instruction::FPExt:
1675 case Instruction::PtrToInt:
1676 case Instruction::IntToPtr:
1677 case Instruction::SIToFP:
1678 case Instruction::UIToFP:
1679 case Instruction::Trunc:
1680 case Instruction::FPTrunc:
1681 case Instruction::BitCast: {
1683 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
1684 INVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1686 setInsertPointAfterBundle(E->Scalars);
1688 Value *InVec = vectorizeTree(INVL);
1690 if (Value *V = alreadyVectorized(E->Scalars))
1693 CastInst *CI = dyn_cast<CastInst>(VL0);
1694 Value *V = Builder.CreateCast(CI->getOpcode(), InVec, VecTy);
1695 E->VectorizedValue = V;
1698 case Instruction::FCmp:
1699 case Instruction::ICmp: {
1700 ValueList LHSV, RHSV;
1701 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1702 LHSV.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1703 RHSV.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1706 setInsertPointAfterBundle(E->Scalars);
1708 Value *L = vectorizeTree(LHSV);
1709 Value *R = vectorizeTree(RHSV);
1711 if (Value *V = alreadyVectorized(E->Scalars))
1714 CmpInst::Predicate P0 = dyn_cast<CmpInst>(VL0)->getPredicate();
1716 if (Opcode == Instruction::FCmp)
1717 V = Builder.CreateFCmp(P0, L, R);
1719 V = Builder.CreateICmp(P0, L, R);
1721 E->VectorizedValue = V;
1724 case Instruction::Select: {
1725 ValueList TrueVec, FalseVec, CondVec;
1726 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1727 CondVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1728 TrueVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1729 FalseVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(2));
1732 setInsertPointAfterBundle(E->Scalars);
1734 Value *Cond = vectorizeTree(CondVec);
1735 Value *True = vectorizeTree(TrueVec);
1736 Value *False = vectorizeTree(FalseVec);
1738 if (Value *V = alreadyVectorized(E->Scalars))
1741 Value *V = Builder.CreateSelect(Cond, True, False);
1742 E->VectorizedValue = V;
1745 case Instruction::Add:
1746 case Instruction::FAdd:
1747 case Instruction::Sub:
1748 case Instruction::FSub:
1749 case Instruction::Mul:
1750 case Instruction::FMul:
1751 case Instruction::UDiv:
1752 case Instruction::SDiv:
1753 case Instruction::FDiv:
1754 case Instruction::URem:
1755 case Instruction::SRem:
1756 case Instruction::FRem:
1757 case Instruction::Shl:
1758 case Instruction::LShr:
1759 case Instruction::AShr:
1760 case Instruction::And:
1761 case Instruction::Or:
1762 case Instruction::Xor: {
1763 ValueList LHSVL, RHSVL;
1764 if (isa<BinaryOperator>(VL0) && VL0->isCommutative())
1765 reorderInputsAccordingToOpcode(E->Scalars, LHSVL, RHSVL);
1767 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1768 LHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1769 RHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1772 setInsertPointAfterBundle(E->Scalars);
1774 Value *LHS = vectorizeTree(LHSVL);
1775 Value *RHS = vectorizeTree(RHSVL);
1777 if (LHS == RHS && isa<Instruction>(LHS)) {
1778 assert((VL0->getOperand(0) == VL0->getOperand(1)) && "Invalid order");
1781 if (Value *V = alreadyVectorized(E->Scalars))
1784 BinaryOperator *BinOp = cast<BinaryOperator>(VL0);
1785 Value *V = Builder.CreateBinOp(BinOp->getOpcode(), LHS, RHS);
1786 E->VectorizedValue = V;
1788 if (Instruction *I = dyn_cast<Instruction>(V))
1789 return propagateMetadata(I, E->Scalars);
1793 case Instruction::Load: {
1794 // Loads are inserted at the head of the tree because we don't want to
1795 // sink them all the way down past store instructions.
1796 setInsertPointAfterBundle(E->Scalars);
1798 LoadInst *LI = cast<LoadInst>(VL0);
1799 unsigned AS = LI->getPointerAddressSpace();
1801 Value *VecPtr = Builder.CreateBitCast(LI->getPointerOperand(),
1802 VecTy->getPointerTo(AS));
1803 unsigned Alignment = LI->getAlignment();
1804 LI = Builder.CreateLoad(VecPtr);
1806 Alignment = DL->getABITypeAlignment(LI->getPointerOperand()->getType());
1807 LI->setAlignment(Alignment);
1808 E->VectorizedValue = LI;
1809 return propagateMetadata(LI, E->Scalars);
1811 case Instruction::Store: {
1812 StoreInst *SI = cast<StoreInst>(VL0);
1813 unsigned Alignment = SI->getAlignment();
1814 unsigned AS = SI->getPointerAddressSpace();
1817 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
1818 ValueOp.push_back(cast<StoreInst>(E->Scalars[i])->getValueOperand());
1820 setInsertPointAfterBundle(E->Scalars);
1822 Value *VecValue = vectorizeTree(ValueOp);
1823 Value *VecPtr = Builder.CreateBitCast(SI->getPointerOperand(),
1824 VecTy->getPointerTo(AS));
1825 StoreInst *S = Builder.CreateStore(VecValue, VecPtr);
1827 Alignment = DL->getABITypeAlignment(SI->getPointerOperand()->getType());
1828 S->setAlignment(Alignment);
1829 E->VectorizedValue = S;
1830 return propagateMetadata(S, E->Scalars);
1832 case Instruction::GetElementPtr: {
1833 setInsertPointAfterBundle(E->Scalars);
1836 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
1837 Op0VL.push_back(cast<GetElementPtrInst>(E->Scalars[i])->getOperand(0));
1839 Value *Op0 = vectorizeTree(Op0VL);
1841 std::vector<Value *> OpVecs;
1842 for (int j = 1, e = cast<GetElementPtrInst>(VL0)->getNumOperands(); j < e;
1845 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
1846 OpVL.push_back(cast<GetElementPtrInst>(E->Scalars[i])->getOperand(j));
1848 Value *OpVec = vectorizeTree(OpVL);
1849 OpVecs.push_back(OpVec);
1852 Value *V = Builder.CreateGEP(Op0, OpVecs);
1853 E->VectorizedValue = V;
1855 if (Instruction *I = dyn_cast<Instruction>(V))
1856 return propagateMetadata(I, E->Scalars);
1860 case Instruction::Call: {
1861 CallInst *CI = cast<CallInst>(VL0);
1862 setInsertPointAfterBundle(E->Scalars);
1864 Intrinsic::ID IID = Intrinsic::not_intrinsic;
1865 if (CI && (FI = CI->getCalledFunction())) {
1866 IID = (Intrinsic::ID) FI->getIntrinsicID();
1868 std::vector<Value *> OpVecs;
1869 for (int j = 0, e = CI->getNumArgOperands(); j < e; ++j) {
1871 // ctlz,cttz and powi are special intrinsics whose second argument is
1872 // a scalar. This argument should not be vectorized.
1873 if (hasVectorInstrinsicScalarOpd(IID, 1) && j == 1) {
1874 CallInst *CEI = cast<CallInst>(E->Scalars[0]);
1875 OpVecs.push_back(CEI->getArgOperand(j));
1878 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1879 CallInst *CEI = cast<CallInst>(E->Scalars[i]);
1880 OpVL.push_back(CEI->getArgOperand(j));
1883 Value *OpVec = vectorizeTree(OpVL);
1884 DEBUG(dbgs() << "SLP: OpVec[" << j << "]: " << *OpVec << "\n");
1885 OpVecs.push_back(OpVec);
1888 Module *M = F->getParent();
1889 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
1890 Type *Tys[] = { VectorType::get(CI->getType(), E->Scalars.size()) };
1891 Function *CF = Intrinsic::getDeclaration(M, ID, Tys);
1892 Value *V = Builder.CreateCall(CF, OpVecs);
1893 E->VectorizedValue = V;
1896 case Instruction::ShuffleVector: {
1897 ValueList LHSVL, RHSVL;
1898 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1899 LHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1900 RHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1902 setInsertPointAfterBundle(E->Scalars);
1904 Value *LHS = vectorizeTree(LHSVL);
1905 Value *RHS = vectorizeTree(RHSVL);
1907 if (Value *V = alreadyVectorized(E->Scalars))
1910 // Create a vector of LHS op1 RHS
1911 BinaryOperator *BinOp0 = cast<BinaryOperator>(VL0);
1912 Value *V0 = Builder.CreateBinOp(BinOp0->getOpcode(), LHS, RHS);
1914 // Create a vector of LHS op2 RHS
1915 Instruction *VL1 = cast<Instruction>(E->Scalars[1]);
1916 BinaryOperator *BinOp1 = cast<BinaryOperator>(VL1);
1917 Value *V1 = Builder.CreateBinOp(BinOp1->getOpcode(), LHS, RHS);
1919 // Create appropriate shuffle to take alternative operations from
1921 std::vector<Constant *> Mask(E->Scalars.size());
1922 unsigned e = E->Scalars.size();
1923 for (unsigned i = 0; i < e; ++i) {
1925 Mask[i] = Builder.getInt32(e + i);
1927 Mask[i] = Builder.getInt32(i);
1930 Value *ShuffleMask = ConstantVector::get(Mask);
1932 Value *V = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
1933 E->VectorizedValue = V;
1934 if (Instruction *I = dyn_cast<Instruction>(V))
1935 return propagateMetadata(I, E->Scalars);
1940 llvm_unreachable("unknown inst");
1945 Value *BoUpSLP::vectorizeTree() {
1946 Builder.SetInsertPoint(F->getEntryBlock().begin());
1947 vectorizeTree(&VectorizableTree[0]);
1949 DEBUG(dbgs() << "SLP: Extracting " << ExternalUses.size() << " values .\n");
1951 // Extract all of the elements with the external uses.
1952 for (UserList::iterator it = ExternalUses.begin(), e = ExternalUses.end();
1954 Value *Scalar = it->Scalar;
1955 llvm::User *User = it->User;
1957 // Skip users that we already RAUW. This happens when one instruction
1958 // has multiple uses of the same value.
1959 if (std::find(Scalar->user_begin(), Scalar->user_end(), User) ==
1962 assert(ScalarToTreeEntry.count(Scalar) && "Invalid scalar");
1964 int Idx = ScalarToTreeEntry[Scalar];
1965 TreeEntry *E = &VectorizableTree[Idx];
1966 assert(!E->NeedToGather && "Extracting from a gather list");
1968 Value *Vec = E->VectorizedValue;
1969 assert(Vec && "Can't find vectorizable value");
1971 Value *Lane = Builder.getInt32(it->Lane);
1972 // Generate extracts for out-of-tree users.
1973 // Find the insertion point for the extractelement lane.
1974 if (isa<Instruction>(Vec)){
1975 if (PHINode *PH = dyn_cast<PHINode>(User)) {
1976 for (int i = 0, e = PH->getNumIncomingValues(); i != e; ++i) {
1977 if (PH->getIncomingValue(i) == Scalar) {
1978 Builder.SetInsertPoint(PH->getIncomingBlock(i)->getTerminator());
1979 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
1980 CSEBlocks.insert(PH->getIncomingBlock(i));
1981 PH->setOperand(i, Ex);
1985 Builder.SetInsertPoint(cast<Instruction>(User));
1986 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
1987 CSEBlocks.insert(cast<Instruction>(User)->getParent());
1988 User->replaceUsesOfWith(Scalar, Ex);
1991 Builder.SetInsertPoint(F->getEntryBlock().begin());
1992 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
1993 CSEBlocks.insert(&F->getEntryBlock());
1994 User->replaceUsesOfWith(Scalar, Ex);
1997 DEBUG(dbgs() << "SLP: Replaced:" << *User << ".\n");
2000 // For each vectorized value:
2001 for (int EIdx = 0, EE = VectorizableTree.size(); EIdx < EE; ++EIdx) {
2002 TreeEntry *Entry = &VectorizableTree[EIdx];
2005 for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
2006 Value *Scalar = Entry->Scalars[Lane];
2007 // No need to handle users of gathered values.
2008 if (Entry->NeedToGather)
2011 assert(Entry->VectorizedValue && "Can't find vectorizable value");
2013 Type *Ty = Scalar->getType();
2014 if (!Ty->isVoidTy()) {
2016 for (User *U : Scalar->users()) {
2017 DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
2019 assert((ScalarToTreeEntry.count(U) ||
2020 // It is legal to replace users in the ignorelist by undef.
2021 (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), U) !=
2022 UserIgnoreList.end())) &&
2023 "Replacing out-of-tree value with undef");
2026 Value *Undef = UndefValue::get(Ty);
2027 Scalar->replaceAllUsesWith(Undef);
2029 DEBUG(dbgs() << "SLP: \tErasing scalar:" << *Scalar << ".\n");
2030 cast<Instruction>(Scalar)->eraseFromParent();
2034 for (auto &BN : BlocksNumbers)
2037 Builder.ClearInsertionPoint();
2039 return VectorizableTree[0].VectorizedValue;
2042 void BoUpSLP::optimizeGatherSequence() {
2043 DEBUG(dbgs() << "SLP: Optimizing " << GatherSeq.size()
2044 << " gather sequences instructions.\n");
2045 // LICM InsertElementInst sequences.
2046 for (SetVector<Instruction *>::iterator it = GatherSeq.begin(),
2047 e = GatherSeq.end(); it != e; ++it) {
2048 InsertElementInst *Insert = dyn_cast<InsertElementInst>(*it);
2053 // Check if this block is inside a loop.
2054 Loop *L = LI->getLoopFor(Insert->getParent());
2058 // Check if it has a preheader.
2059 BasicBlock *PreHeader = L->getLoopPreheader();
2063 // If the vector or the element that we insert into it are
2064 // instructions that are defined in this basic block then we can't
2065 // hoist this instruction.
2066 Instruction *CurrVec = dyn_cast<Instruction>(Insert->getOperand(0));
2067 Instruction *NewElem = dyn_cast<Instruction>(Insert->getOperand(1));
2068 if (CurrVec && L->contains(CurrVec))
2070 if (NewElem && L->contains(NewElem))
2073 // We can hoist this instruction. Move it to the pre-header.
2074 Insert->moveBefore(PreHeader->getTerminator());
2077 // Make a list of all reachable blocks in our CSE queue.
2078 SmallVector<const DomTreeNode *, 8> CSEWorkList;
2079 CSEWorkList.reserve(CSEBlocks.size());
2080 for (BasicBlock *BB : CSEBlocks)
2081 if (DomTreeNode *N = DT->getNode(BB)) {
2082 assert(DT->isReachableFromEntry(N));
2083 CSEWorkList.push_back(N);
2086 // Sort blocks by domination. This ensures we visit a block after all blocks
2087 // dominating it are visited.
2088 std::stable_sort(CSEWorkList.begin(), CSEWorkList.end(),
2089 [this](const DomTreeNode *A, const DomTreeNode *B) {
2090 return DT->properlyDominates(A, B);
2093 // Perform O(N^2) search over the gather sequences and merge identical
2094 // instructions. TODO: We can further optimize this scan if we split the
2095 // instructions into different buckets based on the insert lane.
2096 SmallVector<Instruction *, 16> Visited;
2097 for (auto I = CSEWorkList.begin(), E = CSEWorkList.end(); I != E; ++I) {
2098 assert((I == CSEWorkList.begin() || !DT->dominates(*I, *std::prev(I))) &&
2099 "Worklist not sorted properly!");
2100 BasicBlock *BB = (*I)->getBlock();
2101 // For all instructions in blocks containing gather sequences:
2102 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e;) {
2103 Instruction *In = it++;
2104 if (!isa<InsertElementInst>(In) && !isa<ExtractElementInst>(In))
2107 // Check if we can replace this instruction with any of the
2108 // visited instructions.
2109 for (SmallVectorImpl<Instruction *>::iterator v = Visited.begin(),
2112 if (In->isIdenticalTo(*v) &&
2113 DT->dominates((*v)->getParent(), In->getParent())) {
2114 In->replaceAllUsesWith(*v);
2115 In->eraseFromParent();
2121 assert(std::find(Visited.begin(), Visited.end(), In) == Visited.end());
2122 Visited.push_back(In);
2130 /// The SLPVectorizer Pass.
2131 struct SLPVectorizer : public FunctionPass {
2132 typedef SmallVector<StoreInst *, 8> StoreList;
2133 typedef MapVector<Value *, StoreList> StoreListMap;
2135 /// Pass identification, replacement for typeid
2138 explicit SLPVectorizer() : FunctionPass(ID) {
2139 initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
2142 ScalarEvolution *SE;
2143 const DataLayout *DL;
2144 TargetTransformInfo *TTI;
2145 TargetLibraryInfo *TLI;
2150 bool runOnFunction(Function &F) override {
2151 if (skipOptnoneFunction(F))
2154 SE = &getAnalysis<ScalarEvolution>();
2155 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
2156 DL = DLP ? &DLP->getDataLayout() : nullptr;
2157 TTI = &getAnalysis<TargetTransformInfo>();
2158 TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
2159 AA = &getAnalysis<AliasAnalysis>();
2160 LI = &getAnalysis<LoopInfo>();
2161 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2164 bool Changed = false;
2166 // If the target claims to have no vector registers don't attempt
2168 if (!TTI->getNumberOfRegisters(true))
2171 // Must have DataLayout. We can't require it because some tests run w/o
2176 // Don't vectorize when the attribute NoImplicitFloat is used.
2177 if (F.hasFnAttribute(Attribute::NoImplicitFloat))
2180 DEBUG(dbgs() << "SLP: Analyzing blocks in " << F.getName() << ".\n");
2182 // Use the bottom up slp vectorizer to construct chains that start with
2183 // store instructions.
2184 BoUpSLP R(&F, SE, DL, TTI, TLI, AA, LI, DT);
2186 // Scan the blocks in the function in post order.
2187 for (po_iterator<BasicBlock*> it = po_begin(&F.getEntryBlock()),
2188 e = po_end(&F.getEntryBlock()); it != e; ++it) {
2189 BasicBlock *BB = *it;
2190 // Vectorize trees that end at stores.
2191 if (unsigned count = collectStores(BB, R)) {
2193 DEBUG(dbgs() << "SLP: Found " << count << " stores to vectorize.\n");
2194 Changed |= vectorizeStoreChains(R);
2197 // Vectorize trees that end at reductions.
2198 Changed |= vectorizeChainsInBlock(BB, R);
2202 R.optimizeGatherSequence();
2203 DEBUG(dbgs() << "SLP: vectorized \"" << F.getName() << "\"\n");
2204 DEBUG(verifyFunction(F));
2209 void getAnalysisUsage(AnalysisUsage &AU) const override {
2210 FunctionPass::getAnalysisUsage(AU);
2211 AU.addRequired<ScalarEvolution>();
2212 AU.addRequired<AliasAnalysis>();
2213 AU.addRequired<TargetTransformInfo>();
2214 AU.addRequired<LoopInfo>();
2215 AU.addRequired<DominatorTreeWrapperPass>();
2216 AU.addPreserved<LoopInfo>();
2217 AU.addPreserved<DominatorTreeWrapperPass>();
2218 AU.setPreservesCFG();
2223 /// \brief Collect memory references and sort them according to their base
2224 /// object. We sort the stores to their base objects to reduce the cost of the
2225 /// quadratic search on the stores. TODO: We can further reduce this cost
2226 /// if we flush the chain creation every time we run into a memory barrier.
2227 unsigned collectStores(BasicBlock *BB, BoUpSLP &R);
2229 /// \brief Try to vectorize a chain that starts at two arithmetic instrs.
2230 bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R);
2232 /// \brief Try to vectorize a list of operands.
2233 /// \@param BuildVector A list of users to ignore for the purpose of
2234 /// scheduling and that don't need extracting.
2235 /// \returns true if a value was vectorized.
2236 bool tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
2237 ArrayRef<Value *> BuildVector = None);
2239 /// \brief Try to vectorize a chain that may start at the operands of \V;
2240 bool tryToVectorize(BinaryOperator *V, BoUpSLP &R);
2242 /// \brief Vectorize the stores that were collected in StoreRefs.
2243 bool vectorizeStoreChains(BoUpSLP &R);
2245 /// \brief Scan the basic block and look for patterns that are likely to start
2246 /// a vectorization chain.
2247 bool vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R);
2249 bool vectorizeStoreChain(ArrayRef<Value *> Chain, int CostThreshold,
2252 bool vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold,
2255 StoreListMap StoreRefs;
2258 /// \brief Check that the Values in the slice in VL array are still existent in
2259 /// the WeakVH array.
2260 /// Vectorization of part of the VL array may cause later values in the VL array
2261 /// to become invalid. We track when this has happened in the WeakVH array.
2262 static bool hasValueBeenRAUWed(ArrayRef<Value *> &VL,
2263 SmallVectorImpl<WeakVH> &VH,
2264 unsigned SliceBegin,
2265 unsigned SliceSize) {
2266 for (unsigned i = SliceBegin; i < SliceBegin + SliceSize; ++i)
2273 bool SLPVectorizer::vectorizeStoreChain(ArrayRef<Value *> Chain,
2274 int CostThreshold, BoUpSLP &R) {
2275 unsigned ChainLen = Chain.size();
2276 DEBUG(dbgs() << "SLP: Analyzing a store chain of length " << ChainLen
2278 Type *StoreTy = cast<StoreInst>(Chain[0])->getValueOperand()->getType();
2279 unsigned Sz = DL->getTypeSizeInBits(StoreTy);
2280 unsigned VF = MinVecRegSize / Sz;
2282 if (!isPowerOf2_32(Sz) || VF < 2)
2285 // Keep track of values that were deleted by vectorizing in the loop below.
2286 SmallVector<WeakVH, 8> TrackValues(Chain.begin(), Chain.end());
2288 bool Changed = false;
2289 // Look for profitable vectorizable trees at all offsets, starting at zero.
2290 for (unsigned i = 0, e = ChainLen; i < e; ++i) {
2294 // Check that a previous iteration of this loop did not delete the Value.
2295 if (hasValueBeenRAUWed(Chain, TrackValues, i, VF))
2298 DEBUG(dbgs() << "SLP: Analyzing " << VF << " stores at offset " << i
2300 ArrayRef<Value *> Operands = Chain.slice(i, VF);
2302 R.buildTree(Operands);
2304 int Cost = R.getTreeCost();
2306 DEBUG(dbgs() << "SLP: Found cost=" << Cost << " for VF=" << VF << "\n");
2307 if (Cost < CostThreshold) {
2308 DEBUG(dbgs() << "SLP: Decided to vectorize cost=" << Cost << "\n");
2311 // Move to the next bundle.
2320 bool SLPVectorizer::vectorizeStores(ArrayRef<StoreInst *> Stores,
2321 int costThreshold, BoUpSLP &R) {
2322 SetVector<Value *> Heads, Tails;
2323 SmallDenseMap<Value *, Value *> ConsecutiveChain;
2325 // We may run into multiple chains that merge into a single chain. We mark the
2326 // stores that we vectorized so that we don't visit the same store twice.
2327 BoUpSLP::ValueSet VectorizedStores;
2328 bool Changed = false;
2330 // Do a quadratic search on all of the given stores and find
2331 // all of the pairs of stores that follow each other.
2332 for (unsigned i = 0, e = Stores.size(); i < e; ++i) {
2333 for (unsigned j = 0; j < e; ++j) {
2337 if (R.isConsecutiveAccess(Stores[i], Stores[j])) {
2338 Tails.insert(Stores[j]);
2339 Heads.insert(Stores[i]);
2340 ConsecutiveChain[Stores[i]] = Stores[j];
2345 // For stores that start but don't end a link in the chain:
2346 for (SetVector<Value *>::iterator it = Heads.begin(), e = Heads.end();
2348 if (Tails.count(*it))
2351 // We found a store instr that starts a chain. Now follow the chain and try
2353 BoUpSLP::ValueList Operands;
2355 // Collect the chain into a list.
2356 while (Tails.count(I) || Heads.count(I)) {
2357 if (VectorizedStores.count(I))
2359 Operands.push_back(I);
2360 // Move to the next value in the chain.
2361 I = ConsecutiveChain[I];
2364 bool Vectorized = vectorizeStoreChain(Operands, costThreshold, R);
2366 // Mark the vectorized stores so that we don't vectorize them again.
2368 VectorizedStores.insert(Operands.begin(), Operands.end());
2369 Changed |= Vectorized;
2376 unsigned SLPVectorizer::collectStores(BasicBlock *BB, BoUpSLP &R) {
2379 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
2380 StoreInst *SI = dyn_cast<StoreInst>(it);
2384 // Don't touch volatile stores.
2385 if (!SI->isSimple())
2388 // Check that the pointer points to scalars.
2389 Type *Ty = SI->getValueOperand()->getType();
2390 if (Ty->isAggregateType() || Ty->isVectorTy())
2393 // Find the base pointer.
2394 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), DL);
2396 // Save the store locations.
2397 StoreRefs[Ptr].push_back(SI);
2403 bool SLPVectorizer::tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) {
2406 Value *VL[] = { A, B };
2407 return tryToVectorizeList(VL, R);
2410 bool SLPVectorizer::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
2411 ArrayRef<Value *> BuildVector) {
2415 DEBUG(dbgs() << "SLP: Vectorizing a list of length = " << VL.size() << ".\n");
2417 // Check that all of the parts are scalar instructions of the same type.
2418 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
2422 unsigned Opcode0 = I0->getOpcode();
2424 Type *Ty0 = I0->getType();
2425 unsigned Sz = DL->getTypeSizeInBits(Ty0);
2426 unsigned VF = MinVecRegSize / Sz;
2428 for (int i = 0, e = VL.size(); i < e; ++i) {
2429 Type *Ty = VL[i]->getType();
2430 if (Ty->isAggregateType() || Ty->isVectorTy())
2432 Instruction *Inst = dyn_cast<Instruction>(VL[i]);
2433 if (!Inst || Inst->getOpcode() != Opcode0)
2437 bool Changed = false;
2439 // Keep track of values that were deleted by vectorizing in the loop below.
2440 SmallVector<WeakVH, 8> TrackValues(VL.begin(), VL.end());
2442 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
2443 unsigned OpsWidth = 0;
2450 if (!isPowerOf2_32(OpsWidth) || OpsWidth < 2)
2453 // Check that a previous iteration of this loop did not delete the Value.
2454 if (hasValueBeenRAUWed(VL, TrackValues, i, OpsWidth))
2457 DEBUG(dbgs() << "SLP: Analyzing " << OpsWidth << " operations "
2459 ArrayRef<Value *> Ops = VL.slice(i, OpsWidth);
2461 ArrayRef<Value *> BuildVectorSlice;
2462 if (!BuildVector.empty())
2463 BuildVectorSlice = BuildVector.slice(i, OpsWidth);
2465 R.buildTree(Ops, BuildVectorSlice);
2466 int Cost = R.getTreeCost();
2468 if (Cost < -SLPCostThreshold) {
2469 DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
2470 Value *VectorizedRoot = R.vectorizeTree();
2472 // Reconstruct the build vector by extracting the vectorized root. This
2473 // way we handle the case where some elements of the vector are undefined.
2474 // (return (inserelt <4 xi32> (insertelt undef (opd0) 0) (opd1) 2))
2475 if (!BuildVectorSlice.empty()) {
2476 // The insert point is the last build vector instruction. The vectorized
2477 // root will precede it. This guarantees that we get an instruction. The
2478 // vectorized tree could have been constant folded.
2479 Instruction *InsertAfter = cast<Instruction>(BuildVectorSlice.back());
2480 unsigned VecIdx = 0;
2481 for (auto &V : BuildVectorSlice) {
2482 IRBuilder<true, NoFolder> Builder(
2483 ++BasicBlock::iterator(InsertAfter));
2484 InsertElementInst *IE = cast<InsertElementInst>(V);
2485 Instruction *Extract = cast<Instruction>(Builder.CreateExtractElement(
2486 VectorizedRoot, Builder.getInt32(VecIdx++)));
2487 IE->setOperand(1, Extract);
2488 IE->removeFromParent();
2489 IE->insertAfter(Extract);
2493 // Move to the next bundle.
2502 bool SLPVectorizer::tryToVectorize(BinaryOperator *V, BoUpSLP &R) {
2506 // Try to vectorize V.
2507 if (tryToVectorizePair(V->getOperand(0), V->getOperand(1), R))
2510 BinaryOperator *A = dyn_cast<BinaryOperator>(V->getOperand(0));
2511 BinaryOperator *B = dyn_cast<BinaryOperator>(V->getOperand(1));
2513 if (B && B->hasOneUse()) {
2514 BinaryOperator *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
2515 BinaryOperator *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
2516 if (tryToVectorizePair(A, B0, R)) {
2520 if (tryToVectorizePair(A, B1, R)) {
2527 if (A && A->hasOneUse()) {
2528 BinaryOperator *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
2529 BinaryOperator *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
2530 if (tryToVectorizePair(A0, B, R)) {
2534 if (tryToVectorizePair(A1, B, R)) {
2542 /// \brief Generate a shuffle mask to be used in a reduction tree.
2544 /// \param VecLen The length of the vector to be reduced.
2545 /// \param NumEltsToRdx The number of elements that should be reduced in the
2547 /// \param IsPairwise Whether the reduction is a pairwise or splitting
2548 /// reduction. A pairwise reduction will generate a mask of
2549 /// <0,2,...> or <1,3,..> while a splitting reduction will generate
2550 /// <2,3, undef,undef> for a vector of 4 and NumElts = 2.
2551 /// \param IsLeft True will generate a mask of even elements, odd otherwise.
2552 static Value *createRdxShuffleMask(unsigned VecLen, unsigned NumEltsToRdx,
2553 bool IsPairwise, bool IsLeft,
2554 IRBuilder<> &Builder) {
2555 assert((IsPairwise || !IsLeft) && "Don't support a <0,1,undef,...> mask");
2557 SmallVector<Constant *, 32> ShuffleMask(
2558 VecLen, UndefValue::get(Builder.getInt32Ty()));
2561 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right).
2562 for (unsigned i = 0; i != NumEltsToRdx; ++i)
2563 ShuffleMask[i] = Builder.getInt32(2 * i + !IsLeft);
2565 // Move the upper half of the vector to the lower half.
2566 for (unsigned i = 0; i != NumEltsToRdx; ++i)
2567 ShuffleMask[i] = Builder.getInt32(NumEltsToRdx + i);
2569 return ConstantVector::get(ShuffleMask);
2573 /// Model horizontal reductions.
2575 /// A horizontal reduction is a tree of reduction operations (currently add and
2576 /// fadd) that has operations that can be put into a vector as its leaf.
2577 /// For example, this tree:
2584 /// This tree has "mul" as its reduced values and "+" as its reduction
2585 /// operations. A reduction might be feeding into a store or a binary operation
2600 class HorizontalReduction {
2601 SmallVector<Value *, 16> ReductionOps;
2602 SmallVector<Value *, 32> ReducedVals;
2604 BinaryOperator *ReductionRoot;
2605 PHINode *ReductionPHI;
2607 /// The opcode of the reduction.
2608 unsigned ReductionOpcode;
2609 /// The opcode of the values we perform a reduction on.
2610 unsigned ReducedValueOpcode;
2611 /// The width of one full horizontal reduction operation.
2612 unsigned ReduxWidth;
2613 /// Should we model this reduction as a pairwise reduction tree or a tree that
2614 /// splits the vector in halves and adds those halves.
2615 bool IsPairwiseReduction;
2618 HorizontalReduction()
2619 : ReductionRoot(nullptr), ReductionPHI(nullptr), ReductionOpcode(0),
2620 ReducedValueOpcode(0), ReduxWidth(0), IsPairwiseReduction(false) {}
2622 /// \brief Try to find a reduction tree.
2623 bool matchAssociativeReduction(PHINode *Phi, BinaryOperator *B,
2624 const DataLayout *DL) {
2626 std::find(Phi->op_begin(), Phi->op_end(), B) != Phi->op_end()) &&
2627 "Thi phi needs to use the binary operator");
2629 // We could have a initial reductions that is not an add.
2630 // r *= v1 + v2 + v3 + v4
2631 // In such a case start looking for a tree rooted in the first '+'.
2633 if (B->getOperand(0) == Phi) {
2635 B = dyn_cast<BinaryOperator>(B->getOperand(1));
2636 } else if (B->getOperand(1) == Phi) {
2638 B = dyn_cast<BinaryOperator>(B->getOperand(0));
2645 Type *Ty = B->getType();
2646 if (Ty->isVectorTy())
2649 ReductionOpcode = B->getOpcode();
2650 ReducedValueOpcode = 0;
2651 ReduxWidth = MinVecRegSize / DL->getTypeSizeInBits(Ty);
2658 // We currently only support adds.
2659 if (ReductionOpcode != Instruction::Add &&
2660 ReductionOpcode != Instruction::FAdd)
2663 // Post order traverse the reduction tree starting at B. We only handle true
2664 // trees containing only binary operators.
2665 SmallVector<std::pair<BinaryOperator *, unsigned>, 32> Stack;
2666 Stack.push_back(std::make_pair(B, 0));
2667 while (!Stack.empty()) {
2668 BinaryOperator *TreeN = Stack.back().first;
2669 unsigned EdgeToVist = Stack.back().second++;
2670 bool IsReducedValue = TreeN->getOpcode() != ReductionOpcode;
2672 // Only handle trees in the current basic block.
2673 if (TreeN->getParent() != B->getParent())
2676 // Each tree node needs to have one user except for the ultimate
2678 if (!TreeN->hasOneUse() && TreeN != B)
2682 if (EdgeToVist == 2 || IsReducedValue) {
2683 if (IsReducedValue) {
2684 // Make sure that the opcodes of the operations that we are going to
2686 if (!ReducedValueOpcode)
2687 ReducedValueOpcode = TreeN->getOpcode();
2688 else if (ReducedValueOpcode != TreeN->getOpcode())
2690 ReducedVals.push_back(TreeN);
2692 // We need to be able to reassociate the adds.
2693 if (!TreeN->isAssociative())
2695 ReductionOps.push_back(TreeN);
2702 // Visit left or right.
2703 Value *NextV = TreeN->getOperand(EdgeToVist);
2704 BinaryOperator *Next = dyn_cast<BinaryOperator>(NextV);
2706 Stack.push_back(std::make_pair(Next, 0));
2707 else if (NextV != Phi)
2713 /// \brief Attempt to vectorize the tree found by
2714 /// matchAssociativeReduction.
2715 bool tryToReduce(BoUpSLP &V, TargetTransformInfo *TTI) {
2716 if (ReducedVals.empty())
2719 unsigned NumReducedVals = ReducedVals.size();
2720 if (NumReducedVals < ReduxWidth)
2723 Value *VectorizedTree = nullptr;
2724 IRBuilder<> Builder(ReductionRoot);
2725 FastMathFlags Unsafe;
2726 Unsafe.setUnsafeAlgebra();
2727 Builder.SetFastMathFlags(Unsafe);
2730 for (; i < NumReducedVals - ReduxWidth + 1; i += ReduxWidth) {
2731 ArrayRef<Value *> ValsToReduce(&ReducedVals[i], ReduxWidth);
2732 V.buildTree(ValsToReduce, ReductionOps);
2735 int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]);
2736 if (Cost >= -SLPCostThreshold)
2739 DEBUG(dbgs() << "SLP: Vectorizing horizontal reduction at cost:" << Cost
2742 // Vectorize a tree.
2743 DebugLoc Loc = cast<Instruction>(ReducedVals[i])->getDebugLoc();
2744 Value *VectorizedRoot = V.vectorizeTree();
2746 // Emit a reduction.
2747 Value *ReducedSubTree = emitReduction(VectorizedRoot, Builder);
2748 if (VectorizedTree) {
2749 Builder.SetCurrentDebugLocation(Loc);
2750 VectorizedTree = createBinOp(Builder, ReductionOpcode, VectorizedTree,
2751 ReducedSubTree, "bin.rdx");
2753 VectorizedTree = ReducedSubTree;
2756 if (VectorizedTree) {
2757 // Finish the reduction.
2758 for (; i < NumReducedVals; ++i) {
2759 Builder.SetCurrentDebugLocation(
2760 cast<Instruction>(ReducedVals[i])->getDebugLoc());
2761 VectorizedTree = createBinOp(Builder, ReductionOpcode, VectorizedTree,
2766 assert(ReductionRoot && "Need a reduction operation");
2767 ReductionRoot->setOperand(0, VectorizedTree);
2768 ReductionRoot->setOperand(1, ReductionPHI);
2770 ReductionRoot->replaceAllUsesWith(VectorizedTree);
2772 return VectorizedTree != nullptr;
2777 /// \brief Calcuate the cost of a reduction.
2778 int getReductionCost(TargetTransformInfo *TTI, Value *FirstReducedVal) {
2779 Type *ScalarTy = FirstReducedVal->getType();
2780 Type *VecTy = VectorType::get(ScalarTy, ReduxWidth);
2782 int PairwiseRdxCost = TTI->getReductionCost(ReductionOpcode, VecTy, true);
2783 int SplittingRdxCost = TTI->getReductionCost(ReductionOpcode, VecTy, false);
2785 IsPairwiseReduction = PairwiseRdxCost < SplittingRdxCost;
2786 int VecReduxCost = IsPairwiseReduction ? PairwiseRdxCost : SplittingRdxCost;
2788 int ScalarReduxCost =
2789 ReduxWidth * TTI->getArithmeticInstrCost(ReductionOpcode, VecTy);
2791 DEBUG(dbgs() << "SLP: Adding cost " << VecReduxCost - ScalarReduxCost
2792 << " for reduction that starts with " << *FirstReducedVal
2794 << (IsPairwiseReduction ? "pairwise" : "splitting")
2795 << " reduction)\n");
2797 return VecReduxCost - ScalarReduxCost;
2800 static Value *createBinOp(IRBuilder<> &Builder, unsigned Opcode, Value *L,
2801 Value *R, const Twine &Name = "") {
2802 if (Opcode == Instruction::FAdd)
2803 return Builder.CreateFAdd(L, R, Name);
2804 return Builder.CreateBinOp((Instruction::BinaryOps)Opcode, L, R, Name);
2807 /// \brief Emit a horizontal reduction of the vectorized value.
2808 Value *emitReduction(Value *VectorizedValue, IRBuilder<> &Builder) {
2809 assert(VectorizedValue && "Need to have a vectorized tree node");
2810 Instruction *ValToReduce = dyn_cast<Instruction>(VectorizedValue);
2811 assert(isPowerOf2_32(ReduxWidth) &&
2812 "We only handle power-of-two reductions for now");
2814 Value *TmpVec = ValToReduce;
2815 for (unsigned i = ReduxWidth / 2; i != 0; i >>= 1) {
2816 if (IsPairwiseReduction) {
2818 createRdxShuffleMask(ReduxWidth, i, true, true, Builder);
2820 createRdxShuffleMask(ReduxWidth, i, true, false, Builder);
2822 Value *LeftShuf = Builder.CreateShuffleVector(
2823 TmpVec, UndefValue::get(TmpVec->getType()), LeftMask, "rdx.shuf.l");
2824 Value *RightShuf = Builder.CreateShuffleVector(
2825 TmpVec, UndefValue::get(TmpVec->getType()), (RightMask),
2827 TmpVec = createBinOp(Builder, ReductionOpcode, LeftShuf, RightShuf,
2831 createRdxShuffleMask(ReduxWidth, i, false, false, Builder);
2832 Value *Shuf = Builder.CreateShuffleVector(
2833 TmpVec, UndefValue::get(TmpVec->getType()), UpperHalf, "rdx.shuf");
2834 TmpVec = createBinOp(Builder, ReductionOpcode, TmpVec, Shuf, "bin.rdx");
2838 // The result is in the first element of the vector.
2839 return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
2843 /// \brief Recognize construction of vectors like
2844 /// %ra = insertelement <4 x float> undef, float %s0, i32 0
2845 /// %rb = insertelement <4 x float> %ra, float %s1, i32 1
2846 /// %rc = insertelement <4 x float> %rb, float %s2, i32 2
2847 /// %rd = insertelement <4 x float> %rc, float %s3, i32 3
2849 /// Returns true if it matches
2851 static bool findBuildVector(InsertElementInst *FirstInsertElem,
2852 SmallVectorImpl<Value *> &BuildVector,
2853 SmallVectorImpl<Value *> &BuildVectorOpds) {
2854 if (!isa<UndefValue>(FirstInsertElem->getOperand(0)))
2857 InsertElementInst *IE = FirstInsertElem;
2859 BuildVector.push_back(IE);
2860 BuildVectorOpds.push_back(IE->getOperand(1));
2862 if (IE->use_empty())
2865 InsertElementInst *NextUse = dyn_cast<InsertElementInst>(IE->user_back());
2869 // If this isn't the final use, make sure the next insertelement is the only
2870 // use. It's OK if the final constructed vector is used multiple times
2871 if (!IE->hasOneUse())
2880 static bool PhiTypeSorterFunc(Value *V, Value *V2) {
2881 return V->getType() < V2->getType();
2884 bool SLPVectorizer::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
2885 bool Changed = false;
2886 SmallVector<Value *, 4> Incoming;
2887 SmallSet<Value *, 16> VisitedInstrs;
2889 bool HaveVectorizedPhiNodes = true;
2890 while (HaveVectorizedPhiNodes) {
2891 HaveVectorizedPhiNodes = false;
2893 // Collect the incoming values from the PHIs.
2895 for (BasicBlock::iterator instr = BB->begin(), ie = BB->end(); instr != ie;
2897 PHINode *P = dyn_cast<PHINode>(instr);
2901 if (!VisitedInstrs.count(P))
2902 Incoming.push_back(P);
2906 std::stable_sort(Incoming.begin(), Incoming.end(), PhiTypeSorterFunc);
2908 // Try to vectorize elements base on their type.
2909 for (SmallVector<Value *, 4>::iterator IncIt = Incoming.begin(),
2913 // Look for the next elements with the same type.
2914 SmallVector<Value *, 4>::iterator SameTypeIt = IncIt;
2915 while (SameTypeIt != E &&
2916 (*SameTypeIt)->getType() == (*IncIt)->getType()) {
2917 VisitedInstrs.insert(*SameTypeIt);
2921 // Try to vectorize them.
2922 unsigned NumElts = (SameTypeIt - IncIt);
2923 DEBUG(errs() << "SLP: Trying to vectorize starting at PHIs (" << NumElts << ")\n");
2925 tryToVectorizeList(ArrayRef<Value *>(IncIt, NumElts), R)) {
2926 // Success start over because instructions might have been changed.
2927 HaveVectorizedPhiNodes = true;
2932 // Start over at the next instruction of a different type (or the end).
2937 VisitedInstrs.clear();
2939 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; it++) {
2940 // We may go through BB multiple times so skip the one we have checked.
2941 if (!VisitedInstrs.insert(it))
2944 if (isa<DbgInfoIntrinsic>(it))
2947 // Try to vectorize reductions that use PHINodes.
2948 if (PHINode *P = dyn_cast<PHINode>(it)) {
2949 // Check that the PHI is a reduction PHI.
2950 if (P->getNumIncomingValues() != 2)
2953 (P->getIncomingBlock(0) == BB
2954 ? (P->getIncomingValue(0))
2955 : (P->getIncomingBlock(1) == BB ? P->getIncomingValue(1)
2957 // Check if this is a Binary Operator.
2958 BinaryOperator *BI = dyn_cast_or_null<BinaryOperator>(Rdx);
2962 // Try to match and vectorize a horizontal reduction.
2963 HorizontalReduction HorRdx;
2964 if (ShouldVectorizeHor &&
2965 HorRdx.matchAssociativeReduction(P, BI, DL) &&
2966 HorRdx.tryToReduce(R, TTI)) {
2973 Value *Inst = BI->getOperand(0);
2975 Inst = BI->getOperand(1);
2977 if (tryToVectorize(dyn_cast<BinaryOperator>(Inst), R)) {
2978 // We would like to start over since some instructions are deleted
2979 // and the iterator may become invalid value.
2989 // Try to vectorize horizontal reductions feeding into a store.
2990 if (ShouldStartVectorizeHorAtStore)
2991 if (StoreInst *SI = dyn_cast<StoreInst>(it))
2992 if (BinaryOperator *BinOp =
2993 dyn_cast<BinaryOperator>(SI->getValueOperand())) {
2994 HorizontalReduction HorRdx;
2995 if (((HorRdx.matchAssociativeReduction(nullptr, BinOp, DL) &&
2996 HorRdx.tryToReduce(R, TTI)) ||
2997 tryToVectorize(BinOp, R))) {
3005 // Try to vectorize trees that start at compare instructions.
3006 if (CmpInst *CI = dyn_cast<CmpInst>(it)) {
3007 if (tryToVectorizePair(CI->getOperand(0), CI->getOperand(1), R)) {
3009 // We would like to start over since some instructions are deleted
3010 // and the iterator may become invalid value.
3016 for (int i = 0; i < 2; ++i) {
3017 if (BinaryOperator *BI = dyn_cast<BinaryOperator>(CI->getOperand(i))) {
3018 if (tryToVectorizePair(BI->getOperand(0), BI->getOperand(1), R)) {
3020 // We would like to start over since some instructions are deleted
3021 // and the iterator may become invalid value.
3030 // Try to vectorize trees that start at insertelement instructions.
3031 if (InsertElementInst *FirstInsertElem = dyn_cast<InsertElementInst>(it)) {
3032 SmallVector<Value *, 16> BuildVector;
3033 SmallVector<Value *, 16> BuildVectorOpds;
3034 if (!findBuildVector(FirstInsertElem, BuildVector, BuildVectorOpds))
3037 // Vectorize starting with the build vector operands ignoring the
3038 // BuildVector instructions for the purpose of scheduling and user
3040 if (tryToVectorizeList(BuildVectorOpds, R, BuildVector)) {
3053 bool SLPVectorizer::vectorizeStoreChains(BoUpSLP &R) {
3054 bool Changed = false;
3055 // Attempt to sort and vectorize each of the store-groups.
3056 for (StoreListMap::iterator it = StoreRefs.begin(), e = StoreRefs.end();
3058 if (it->second.size() < 2)
3061 DEBUG(dbgs() << "SLP: Analyzing a store chain of length "
3062 << it->second.size() << ".\n");
3064 // Process the stores in chunks of 16.
3065 for (unsigned CI = 0, CE = it->second.size(); CI < CE; CI+=16) {
3066 unsigned Len = std::min<unsigned>(CE - CI, 16);
3067 ArrayRef<StoreInst *> Chunk(&it->second[CI], Len);
3068 Changed |= vectorizeStores(Chunk, -SLPCostThreshold, R);
3074 } // end anonymous namespace
3076 char SLPVectorizer::ID = 0;
3077 static const char lv_name[] = "SLP Vectorizer";
3078 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
3079 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3080 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
3081 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
3082 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
3083 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
3086 Pass *createSLPVectorizerPass() { return new SLPVectorizer(); }