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/ADT/Statistic.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Analysis/ScalarEvolution.h"
26 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
27 #include "llvm/Analysis/TargetTransformInfo.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/NoFolder.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/IR/Verifier.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Transforms/Utils/VectorUtils.h"
50 #define SV_NAME "slp-vectorizer"
51 #define DEBUG_TYPE "SLP"
53 STATISTIC(NumVectorInstructions, "Number of vector instructions generated");
56 SLPCostThreshold("slp-threshold", cl::init(0), cl::Hidden,
57 cl::desc("Only vectorize if you gain more than this "
61 ShouldVectorizeHor("slp-vectorize-hor", cl::init(false), cl::Hidden,
62 cl::desc("Attempt to vectorize horizontal reductions"));
64 static cl::opt<bool> ShouldStartVectorizeHorAtStore(
65 "slp-vectorize-hor-store", cl::init(false), cl::Hidden,
67 "Attempt to vectorize horizontal reductions feeding into a store"));
71 static const unsigned MinVecRegSize = 128;
73 static const unsigned RecursionMaxDepth = 12;
75 /// \returns the parent basic block if all of the instructions in \p VL
76 /// are in the same block or null otherwise.
77 static BasicBlock *getSameBlock(ArrayRef<Value *> VL) {
78 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
81 BasicBlock *BB = I0->getParent();
82 for (int i = 1, e = VL.size(); i < e; i++) {
83 Instruction *I = dyn_cast<Instruction>(VL[i]);
87 if (BB != I->getParent())
93 /// \returns True if all of the values in \p VL are constants.
94 static bool allConstant(ArrayRef<Value *> VL) {
95 for (unsigned i = 0, e = VL.size(); i < e; ++i)
96 if (!isa<Constant>(VL[i]))
101 /// \returns True if all of the values in \p VL are identical.
102 static bool isSplat(ArrayRef<Value *> VL) {
103 for (unsigned i = 1, e = VL.size(); i < e; ++i)
109 ///\returns Opcode that can be clubbed with \p Op to create an alternate
110 /// sequence which can later be merged as a ShuffleVector instruction.
111 static unsigned getAltOpcode(unsigned Op) {
113 case Instruction::FAdd:
114 return Instruction::FSub;
115 case Instruction::FSub:
116 return Instruction::FAdd;
117 case Instruction::Add:
118 return Instruction::Sub;
119 case Instruction::Sub:
120 return Instruction::Add;
126 ///\returns bool representing if Opcode \p Op can be part
127 /// of an alternate sequence which can later be merged as
128 /// a ShuffleVector instruction.
129 static bool canCombineAsAltInst(unsigned Op) {
130 if (Op == Instruction::FAdd || Op == Instruction::FSub ||
131 Op == Instruction::Sub || Op == Instruction::Add)
136 /// \returns ShuffleVector instruction if intructions in \p VL have
137 /// alternate fadd,fsub / fsub,fadd/add,sub/sub,add sequence.
138 /// (i.e. e.g. opcodes of fadd,fsub,fadd,fsub...)
139 static unsigned isAltInst(ArrayRef<Value *> VL) {
140 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
141 unsigned Opcode = I0->getOpcode();
142 unsigned AltOpcode = getAltOpcode(Opcode);
143 for (int i = 1, e = VL.size(); i < e; i++) {
144 Instruction *I = dyn_cast<Instruction>(VL[i]);
145 if (!I || I->getOpcode() != ((i & 1) ? AltOpcode : Opcode))
148 return Instruction::ShuffleVector;
151 /// \returns The opcode if all of the Instructions in \p VL have the same
153 static unsigned getSameOpcode(ArrayRef<Value *> VL) {
154 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
157 unsigned Opcode = I0->getOpcode();
158 for (int i = 1, e = VL.size(); i < e; i++) {
159 Instruction *I = dyn_cast<Instruction>(VL[i]);
160 if (!I || Opcode != I->getOpcode()) {
161 if (canCombineAsAltInst(Opcode) && i == 1)
162 return isAltInst(VL);
169 /// Get the intersection (logical and) of all of the potential IR flags
170 /// of each scalar operation (VL) that will be converted into a vector (I).
171 /// Flag set: NSW, NUW, exact, and all of fast-math.
172 static void propagateIRFlags(Value *I, ArrayRef<Value *> VL) {
173 if (auto *VecOp = dyn_cast<BinaryOperator>(I)) {
174 if (auto *Intersection = dyn_cast<BinaryOperator>(VL[0])) {
175 // Intersection is initialized to the 0th scalar,
176 // so start counting from index '1'.
177 for (int i = 1, e = VL.size(); i < e; ++i) {
178 if (auto *Scalar = dyn_cast<BinaryOperator>(VL[i]))
179 Intersection->andIRFlags(Scalar);
181 VecOp->copyIRFlags(Intersection);
186 /// \returns \p I after propagating metadata from \p VL.
187 static Instruction *propagateMetadata(Instruction *I, ArrayRef<Value *> VL) {
188 Instruction *I0 = cast<Instruction>(VL[0]);
189 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
190 I0->getAllMetadataOtherThanDebugLoc(Metadata);
192 for (unsigned i = 0, n = Metadata.size(); i != n; ++i) {
193 unsigned Kind = Metadata[i].first;
194 MDNode *MD = Metadata[i].second;
196 for (int i = 1, e = VL.size(); MD && i != e; i++) {
197 Instruction *I = cast<Instruction>(VL[i]);
198 MDNode *IMD = I->getMetadata(Kind);
202 MD = nullptr; // Remove unknown metadata
204 case LLVMContext::MD_tbaa:
205 MD = MDNode::getMostGenericTBAA(MD, IMD);
207 case LLVMContext::MD_alias_scope:
208 case LLVMContext::MD_noalias:
209 MD = MDNode::intersect(MD, IMD);
211 case LLVMContext::MD_fpmath:
212 MD = MDNode::getMostGenericFPMath(MD, IMD);
216 I->setMetadata(Kind, MD);
221 /// \returns The type that all of the values in \p VL have or null if there
222 /// are different types.
223 static Type* getSameType(ArrayRef<Value *> VL) {
224 Type *Ty = VL[0]->getType();
225 for (int i = 1, e = VL.size(); i < e; i++)
226 if (VL[i]->getType() != Ty)
232 /// \returns True if the ExtractElement instructions in VL can be vectorized
233 /// to use the original vector.
234 static bool CanReuseExtract(ArrayRef<Value *> VL) {
235 assert(Instruction::ExtractElement == getSameOpcode(VL) && "Invalid opcode");
236 // Check if all of the extracts come from the same vector and from the
239 ExtractElementInst *E0 = cast<ExtractElementInst>(VL0);
240 Value *Vec = E0->getOperand(0);
242 // We have to extract from the same vector type.
243 unsigned NElts = Vec->getType()->getVectorNumElements();
245 if (NElts != VL.size())
248 // Check that all of the indices extract from the correct offset.
249 ConstantInt *CI = dyn_cast<ConstantInt>(E0->getOperand(1));
250 if (!CI || CI->getZExtValue())
253 for (unsigned i = 1, e = VL.size(); i < e; ++i) {
254 ExtractElementInst *E = cast<ExtractElementInst>(VL[i]);
255 ConstantInt *CI = dyn_cast<ConstantInt>(E->getOperand(1));
257 if (!CI || CI->getZExtValue() != i || E->getOperand(0) != Vec)
264 static void reorderInputsAccordingToOpcode(ArrayRef<Value *> VL,
265 SmallVectorImpl<Value *> &Left,
266 SmallVectorImpl<Value *> &Right) {
268 SmallVector<Value *, 16> OrigLeft, OrigRight;
270 bool AllSameOpcodeLeft = true;
271 bool AllSameOpcodeRight = true;
272 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
273 Instruction *I = cast<Instruction>(VL[i]);
274 Value *V0 = I->getOperand(0);
275 Value *V1 = I->getOperand(1);
277 OrigLeft.push_back(V0);
278 OrigRight.push_back(V1);
280 Instruction *I0 = dyn_cast<Instruction>(V0);
281 Instruction *I1 = dyn_cast<Instruction>(V1);
283 // Check whether all operands on one side have the same opcode. In this case
284 // we want to preserve the original order and not make things worse by
286 AllSameOpcodeLeft = I0;
287 AllSameOpcodeRight = I1;
289 if (i && AllSameOpcodeLeft) {
290 if(Instruction *P0 = dyn_cast<Instruction>(OrigLeft[i-1])) {
291 if(P0->getOpcode() != I0->getOpcode())
292 AllSameOpcodeLeft = false;
294 AllSameOpcodeLeft = false;
296 if (i && AllSameOpcodeRight) {
297 if(Instruction *P1 = dyn_cast<Instruction>(OrigRight[i-1])) {
298 if(P1->getOpcode() != I1->getOpcode())
299 AllSameOpcodeRight = false;
301 AllSameOpcodeRight = false;
304 // Sort two opcodes. In the code below we try to preserve the ability to use
305 // broadcast of values instead of individual inserts.
312 // If we just sorted according to opcode we would leave the first line in
313 // tact but we would swap vl2 with vr2 because opcode(phi) > opcode(load).
316 // Because vr2 and vr1 are from the same load we loose the opportunity of a
317 // broadcast for the packed right side in the backend: we have [vr1, vl2]
318 // instead of [vr1, vr2=vr1].
320 if(!i && I0->getOpcode() > I1->getOpcode()) {
323 } else if (i && I0->getOpcode() > I1->getOpcode() && Right[i-1] != I1) {
324 // Try not to destroy a broad cast for no apparent benefit.
327 } else if (i && I0->getOpcode() == I1->getOpcode() && Right[i-1] == I0) {
328 // Try preserve broadcasts.
331 } else if (i && I0->getOpcode() == I1->getOpcode() && Left[i-1] == I1) {
332 // Try preserve broadcasts.
341 // One opcode, put the instruction on the right.
351 bool LeftBroadcast = isSplat(Left);
352 bool RightBroadcast = isSplat(Right);
354 // Don't reorder if the operands where good to begin with.
355 if (!(LeftBroadcast || RightBroadcast) &&
356 (AllSameOpcodeRight || AllSameOpcodeLeft)) {
362 /// \returns True if in-tree use also needs extract. This refers to
363 /// possible scalar operand in vectorized instruction.
364 static bool InTreeUserNeedToExtract(Value *Scalar, Instruction *UserInst,
365 TargetLibraryInfo *TLI) {
367 unsigned Opcode = UserInst->getOpcode();
369 case Instruction::Load: {
370 LoadInst *LI = cast<LoadInst>(UserInst);
371 return (LI->getPointerOperand() == Scalar);
373 case Instruction::Store: {
374 StoreInst *SI = cast<StoreInst>(UserInst);
375 return (SI->getPointerOperand() == Scalar);
377 case Instruction::Call: {
378 CallInst *CI = cast<CallInst>(UserInst);
379 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
380 if (hasVectorInstrinsicScalarOpd(ID, 1)) {
381 return (CI->getArgOperand(1) == Scalar);
389 /// Bottom Up SLP Vectorizer.
392 typedef SmallVector<Value *, 8> ValueList;
393 typedef SmallVector<Instruction *, 16> InstrList;
394 typedef SmallPtrSet<Value *, 16> ValueSet;
395 typedef SmallVector<StoreInst *, 8> StoreList;
397 BoUpSLP(Function *Func, ScalarEvolution *Se, const DataLayout *Dl,
398 TargetTransformInfo *Tti, TargetLibraryInfo *TLi, AliasAnalysis *Aa,
399 LoopInfo *Li, DominatorTree *Dt)
400 : NumLoadsWantToKeepOrder(0), NumLoadsWantToChangeOrder(0),
401 F(Func), SE(Se), DL(Dl), TTI(Tti), TLI(TLi), AA(Aa), LI(Li), DT(Dt),
402 Builder(Se->getContext()) {}
404 /// \brief Vectorize the tree that starts with the elements in \p VL.
405 /// Returns the vectorized root.
406 Value *vectorizeTree();
408 /// \returns the cost incurred by unwanted spills and fills, caused by
409 /// holding live values over call sites.
412 /// \returns the vectorization cost of the subtree that starts at \p VL.
413 /// A negative number means that this is profitable.
416 /// Construct a vectorizable tree that starts at \p Roots, ignoring users for
417 /// the purpose of scheduling and extraction in the \p UserIgnoreLst.
418 void buildTree(ArrayRef<Value *> Roots,
419 ArrayRef<Value *> UserIgnoreLst = None);
421 /// Clear the internal data structures that are created by 'buildTree'.
423 VectorizableTree.clear();
424 ScalarToTreeEntry.clear();
426 ExternalUses.clear();
427 NumLoadsWantToKeepOrder = 0;
428 NumLoadsWantToChangeOrder = 0;
429 for (auto &Iter : BlocksSchedules) {
430 BlockScheduling *BS = Iter.second.get();
435 /// \returns true if the memory operations A and B are consecutive.
436 bool isConsecutiveAccess(Value *A, Value *B);
438 /// \brief Perform LICM and CSE on the newly generated gather sequences.
439 void optimizeGatherSequence();
441 /// \returns true if it is benefitial to reverse the vector order.
442 bool shouldReorder() const {
443 return NumLoadsWantToChangeOrder > NumLoadsWantToKeepOrder;
449 /// \returns the cost of the vectorizable entry.
450 int getEntryCost(TreeEntry *E);
452 /// This is the recursive part of buildTree.
453 void buildTree_rec(ArrayRef<Value *> Roots, unsigned Depth);
455 /// Vectorize a single entry in the tree.
456 Value *vectorizeTree(TreeEntry *E);
458 /// Vectorize a single entry in the tree, starting in \p VL.
459 Value *vectorizeTree(ArrayRef<Value *> VL);
461 /// \returns the pointer to the vectorized value if \p VL is already
462 /// vectorized, or NULL. They may happen in cycles.
463 Value *alreadyVectorized(ArrayRef<Value *> VL) const;
465 /// \brief Take the pointer operand from the Load/Store instruction.
466 /// \returns NULL if this is not a valid Load/Store instruction.
467 static Value *getPointerOperand(Value *I);
469 /// \brief Take the address space operand from the Load/Store instruction.
470 /// \returns -1 if this is not a valid Load/Store instruction.
471 static unsigned getAddressSpaceOperand(Value *I);
473 /// \returns the scalarization cost for this type. Scalarization in this
474 /// context means the creation of vectors from a group of scalars.
475 int getGatherCost(Type *Ty);
477 /// \returns the scalarization cost for this list of values. Assuming that
478 /// this subtree gets vectorized, we may need to extract the values from the
479 /// roots. This method calculates the cost of extracting the values.
480 int getGatherCost(ArrayRef<Value *> VL);
482 /// \brief Set the Builder insert point to one after the last instruction in
484 void setInsertPointAfterBundle(ArrayRef<Value *> VL);
486 /// \returns a vector from a collection of scalars in \p VL.
487 Value *Gather(ArrayRef<Value *> VL, VectorType *Ty);
489 /// \returns whether the VectorizableTree is fully vectoriable and will
490 /// be beneficial even the tree height is tiny.
491 bool isFullyVectorizableTinyTree();
494 TreeEntry() : Scalars(), VectorizedValue(nullptr),
497 /// \returns true if the scalars in VL are equal to this entry.
498 bool isSame(ArrayRef<Value *> VL) const {
499 assert(VL.size() == Scalars.size() && "Invalid size");
500 return std::equal(VL.begin(), VL.end(), Scalars.begin());
503 /// A vector of scalars.
506 /// The Scalars are vectorized into this value. It is initialized to Null.
507 Value *VectorizedValue;
509 /// Do we need to gather this sequence ?
513 /// Create a new VectorizableTree entry.
514 TreeEntry *newTreeEntry(ArrayRef<Value *> VL, bool Vectorized) {
515 VectorizableTree.push_back(TreeEntry());
516 int idx = VectorizableTree.size() - 1;
517 TreeEntry *Last = &VectorizableTree[idx];
518 Last->Scalars.insert(Last->Scalars.begin(), VL.begin(), VL.end());
519 Last->NeedToGather = !Vectorized;
521 for (int i = 0, e = VL.size(); i != e; ++i) {
522 assert(!ScalarToTreeEntry.count(VL[i]) && "Scalar already in tree!");
523 ScalarToTreeEntry[VL[i]] = idx;
526 MustGather.insert(VL.begin(), VL.end());
531 /// -- Vectorization State --
532 /// Holds all of the tree entries.
533 std::vector<TreeEntry> VectorizableTree;
535 /// Maps a specific scalar to its tree entry.
536 SmallDenseMap<Value*, int> ScalarToTreeEntry;
538 /// A list of scalars that we found that we need to keep as scalars.
541 /// This POD struct describes one external user in the vectorized tree.
542 struct ExternalUser {
543 ExternalUser (Value *S, llvm::User *U, int L) :
544 Scalar(S), User(U), Lane(L){};
545 // Which scalar in our function.
547 // Which user that uses the scalar.
549 // Which lane does the scalar belong to.
552 typedef SmallVector<ExternalUser, 16> UserList;
554 /// A list of values that need to extracted out of the tree.
555 /// This list holds pairs of (Internal Scalar : External User).
556 UserList ExternalUses;
558 /// Holds all of the instructions that we gathered.
559 SetVector<Instruction *> GatherSeq;
560 /// A list of blocks that we are going to CSE.
561 SetVector<BasicBlock *> CSEBlocks;
563 /// Contains all scheduling relevant data for an instruction.
564 /// A ScheduleData either represents a single instruction or a member of an
565 /// instruction bundle (= a group of instructions which is combined into a
566 /// vector instruction).
567 struct ScheduleData {
569 // The initial value for the dependency counters. It means that the
570 // dependencies are not calculated yet.
571 enum { InvalidDeps = -1 };
574 : Inst(nullptr), FirstInBundle(nullptr), NextInBundle(nullptr),
575 NextLoadStore(nullptr), SchedulingRegionID(0), SchedulingPriority(0),
576 Dependencies(InvalidDeps), UnscheduledDeps(InvalidDeps),
577 UnscheduledDepsInBundle(InvalidDeps), IsScheduled(false) {}
579 void init(int BlockSchedulingRegionID) {
580 FirstInBundle = this;
581 NextInBundle = nullptr;
582 NextLoadStore = nullptr;
584 SchedulingRegionID = BlockSchedulingRegionID;
585 UnscheduledDepsInBundle = UnscheduledDeps;
589 /// Returns true if the dependency information has been calculated.
590 bool hasValidDependencies() const { return Dependencies != InvalidDeps; }
592 /// Returns true for single instructions and for bundle representatives
593 /// (= the head of a bundle).
594 bool isSchedulingEntity() const { return FirstInBundle == this; }
596 /// Returns true if it represents an instruction bundle and not only a
597 /// single instruction.
598 bool isPartOfBundle() const {
599 return NextInBundle != nullptr || FirstInBundle != this;
602 /// Returns true if it is ready for scheduling, i.e. it has no more
603 /// unscheduled depending instructions/bundles.
604 bool isReady() const {
605 assert(isSchedulingEntity() &&
606 "can't consider non-scheduling entity for ready list");
607 return UnscheduledDepsInBundle == 0 && !IsScheduled;
610 /// Modifies the number of unscheduled dependencies, also updating it for
611 /// the whole bundle.
612 int incrementUnscheduledDeps(int Incr) {
613 UnscheduledDeps += Incr;
614 return FirstInBundle->UnscheduledDepsInBundle += Incr;
617 /// Sets the number of unscheduled dependencies to the number of
619 void resetUnscheduledDeps() {
620 incrementUnscheduledDeps(Dependencies - UnscheduledDeps);
623 /// Clears all dependency information.
624 void clearDependencies() {
625 Dependencies = InvalidDeps;
626 resetUnscheduledDeps();
627 MemoryDependencies.clear();
630 void dump(raw_ostream &os) const {
631 if (!isSchedulingEntity()) {
633 } else if (NextInBundle) {
635 ScheduleData *SD = NextInBundle;
637 os << ';' << *SD->Inst;
638 SD = SD->NextInBundle;
648 /// Points to the head in an instruction bundle (and always to this for
649 /// single instructions).
650 ScheduleData *FirstInBundle;
652 /// Single linked list of all instructions in a bundle. Null if it is a
653 /// single instruction.
654 ScheduleData *NextInBundle;
656 /// Single linked list of all memory instructions (e.g. load, store, call)
657 /// in the block - until the end of the scheduling region.
658 ScheduleData *NextLoadStore;
660 /// The dependent memory instructions.
661 /// This list is derived on demand in calculateDependencies().
662 SmallVector<ScheduleData *, 4> MemoryDependencies;
664 /// This ScheduleData is in the current scheduling region if this matches
665 /// the current SchedulingRegionID of BlockScheduling.
666 int SchedulingRegionID;
668 /// Used for getting a "good" final ordering of instructions.
669 int SchedulingPriority;
671 /// The number of dependencies. Constitutes of the number of users of the
672 /// instruction plus the number of dependent memory instructions (if any).
673 /// This value is calculated on demand.
674 /// If InvalidDeps, the number of dependencies is not calculated yet.
678 /// The number of dependencies minus the number of dependencies of scheduled
679 /// instructions. As soon as this is zero, the instruction/bundle gets ready
681 /// Note that this is negative as long as Dependencies is not calculated.
684 /// The sum of UnscheduledDeps in a bundle. Equals to UnscheduledDeps for
685 /// single instructions.
686 int UnscheduledDepsInBundle;
688 /// True if this instruction is scheduled (or considered as scheduled in the
694 friend raw_ostream &operator<<(raw_ostream &os,
695 const BoUpSLP::ScheduleData &SD);
698 /// Contains all scheduling data for a basic block.
700 struct BlockScheduling {
702 BlockScheduling(BasicBlock *BB)
703 : BB(BB), ChunkSize(BB->size()), ChunkPos(ChunkSize),
704 ScheduleStart(nullptr), ScheduleEnd(nullptr),
705 FirstLoadStoreInRegion(nullptr), LastLoadStoreInRegion(nullptr),
706 // Make sure that the initial SchedulingRegionID is greater than the
707 // initial SchedulingRegionID in ScheduleData (which is 0).
708 SchedulingRegionID(1) {}
712 ScheduleStart = nullptr;
713 ScheduleEnd = nullptr;
714 FirstLoadStoreInRegion = nullptr;
715 LastLoadStoreInRegion = nullptr;
717 // Make a new scheduling region, i.e. all existing ScheduleData is not
718 // in the new region yet.
719 ++SchedulingRegionID;
722 ScheduleData *getScheduleData(Value *V) {
723 ScheduleData *SD = ScheduleDataMap[V];
724 if (SD && SD->SchedulingRegionID == SchedulingRegionID)
729 bool isInSchedulingRegion(ScheduleData *SD) {
730 return SD->SchedulingRegionID == SchedulingRegionID;
733 /// Marks an instruction as scheduled and puts all dependent ready
734 /// instructions into the ready-list.
735 template <typename ReadyListType>
736 void schedule(ScheduleData *SD, ReadyListType &ReadyList) {
737 SD->IsScheduled = true;
738 DEBUG(dbgs() << "SLP: schedule " << *SD << "\n");
740 ScheduleData *BundleMember = SD;
741 while (BundleMember) {
742 // Handle the def-use chain dependencies.
743 for (Use &U : BundleMember->Inst->operands()) {
744 ScheduleData *OpDef = getScheduleData(U.get());
745 if (OpDef && OpDef->hasValidDependencies() &&
746 OpDef->incrementUnscheduledDeps(-1) == 0) {
747 // There are no more unscheduled dependencies after decrementing,
748 // so we can put the dependent instruction into the ready list.
749 ScheduleData *DepBundle = OpDef->FirstInBundle;
750 assert(!DepBundle->IsScheduled &&
751 "already scheduled bundle gets ready");
752 ReadyList.insert(DepBundle);
753 DEBUG(dbgs() << "SLP: gets ready (def): " << *DepBundle << "\n");
756 // Handle the memory dependencies.
757 for (ScheduleData *MemoryDepSD : BundleMember->MemoryDependencies) {
758 if (MemoryDepSD->incrementUnscheduledDeps(-1) == 0) {
759 // There are no more unscheduled dependencies after decrementing,
760 // so we can put the dependent instruction into the ready list.
761 ScheduleData *DepBundle = MemoryDepSD->FirstInBundle;
762 assert(!DepBundle->IsScheduled &&
763 "already scheduled bundle gets ready");
764 ReadyList.insert(DepBundle);
765 DEBUG(dbgs() << "SLP: gets ready (mem): " << *DepBundle << "\n");
768 BundleMember = BundleMember->NextInBundle;
772 /// Put all instructions into the ReadyList which are ready for scheduling.
773 template <typename ReadyListType>
774 void initialFillReadyList(ReadyListType &ReadyList) {
775 for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
776 ScheduleData *SD = getScheduleData(I);
777 if (SD->isSchedulingEntity() && SD->isReady()) {
778 ReadyList.insert(SD);
779 DEBUG(dbgs() << "SLP: initially in ready list: " << *I << "\n");
784 /// Checks if a bundle of instructions can be scheduled, i.e. has no
785 /// cyclic dependencies. This is only a dry-run, no instructions are
786 /// actually moved at this stage.
787 bool tryScheduleBundle(ArrayRef<Value *> VL, AliasAnalysis *AA);
789 /// Un-bundles a group of instructions.
790 void cancelScheduling(ArrayRef<Value *> VL);
792 /// Extends the scheduling region so that V is inside the region.
793 void extendSchedulingRegion(Value *V);
795 /// Initialize the ScheduleData structures for new instructions in the
796 /// scheduling region.
797 void initScheduleData(Instruction *FromI, Instruction *ToI,
798 ScheduleData *PrevLoadStore,
799 ScheduleData *NextLoadStore);
801 /// Updates the dependency information of a bundle and of all instructions/
802 /// bundles which depend on the original bundle.
803 void calculateDependencies(ScheduleData *SD, bool InsertInReadyList,
806 /// Sets all instruction in the scheduling region to un-scheduled.
807 void resetSchedule();
811 /// Simple memory allocation for ScheduleData.
812 std::vector<std::unique_ptr<ScheduleData[]>> ScheduleDataChunks;
814 /// The size of a ScheduleData array in ScheduleDataChunks.
817 /// The allocator position in the current chunk, which is the last entry
818 /// of ScheduleDataChunks.
821 /// Attaches ScheduleData to Instruction.
822 /// Note that the mapping survives during all vectorization iterations, i.e.
823 /// ScheduleData structures are recycled.
824 DenseMap<Value *, ScheduleData *> ScheduleDataMap;
826 struct ReadyList : SmallVector<ScheduleData *, 8> {
827 void insert(ScheduleData *SD) { push_back(SD); }
830 /// The ready-list for scheduling (only used for the dry-run).
831 ReadyList ReadyInsts;
833 /// The first instruction of the scheduling region.
834 Instruction *ScheduleStart;
836 /// The first instruction _after_ the scheduling region.
837 Instruction *ScheduleEnd;
839 /// The first memory accessing instruction in the scheduling region
841 ScheduleData *FirstLoadStoreInRegion;
843 /// The last memory accessing instruction in the scheduling region
845 ScheduleData *LastLoadStoreInRegion;
847 /// The ID of the scheduling region. For a new vectorization iteration this
848 /// is incremented which "removes" all ScheduleData from the region.
849 int SchedulingRegionID;
852 /// Attaches the BlockScheduling structures to basic blocks.
853 DenseMap<BasicBlock *, std::unique_ptr<BlockScheduling>> BlocksSchedules;
855 /// Performs the "real" scheduling. Done before vectorization is actually
856 /// performed in a basic block.
857 void scheduleBlock(BlockScheduling *BS);
859 /// List of users to ignore during scheduling and that don't need extracting.
860 ArrayRef<Value *> UserIgnoreList;
862 // Number of load-bundles, which contain consecutive loads.
863 int NumLoadsWantToKeepOrder;
865 // Number of load-bundles of size 2, which are consecutive loads if reversed.
866 int NumLoadsWantToChangeOrder;
868 // Analysis and block reference.
871 const DataLayout *DL;
872 TargetTransformInfo *TTI;
873 TargetLibraryInfo *TLI;
877 /// Instruction builder to construct the vectorized tree.
882 raw_ostream &operator<<(raw_ostream &os, const BoUpSLP::ScheduleData &SD) {
888 void BoUpSLP::buildTree(ArrayRef<Value *> Roots,
889 ArrayRef<Value *> UserIgnoreLst) {
891 UserIgnoreList = UserIgnoreLst;
892 if (!getSameType(Roots))
894 buildTree_rec(Roots, 0);
896 // Collect the values that we need to extract from the tree.
897 for (int EIdx = 0, EE = VectorizableTree.size(); EIdx < EE; ++EIdx) {
898 TreeEntry *Entry = &VectorizableTree[EIdx];
901 for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
902 Value *Scalar = Entry->Scalars[Lane];
904 // No need to handle users of gathered values.
905 if (Entry->NeedToGather)
908 for (User *U : Scalar->users()) {
909 DEBUG(dbgs() << "SLP: Checking user:" << *U << ".\n");
911 Instruction *UserInst = dyn_cast<Instruction>(U);
915 // Skip in-tree scalars that become vectors
916 if (ScalarToTreeEntry.count(U)) {
917 int Idx = ScalarToTreeEntry[U];
918 TreeEntry *UseEntry = &VectorizableTree[Idx];
919 Value *UseScalar = UseEntry->Scalars[0];
920 // Some in-tree scalars will remain as scalar in vectorized
921 // instructions. If that is the case, the one in Lane 0 will
923 if (UseScalar != U ||
924 !InTreeUserNeedToExtract(Scalar, UserInst, TLI)) {
925 DEBUG(dbgs() << "SLP: \tInternal user will be removed:" << *U
927 assert(!VectorizableTree[Idx].NeedToGather && "Bad state");
932 // Ignore users in the user ignore list.
933 if (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), UserInst) !=
934 UserIgnoreList.end())
937 DEBUG(dbgs() << "SLP: Need to extract:" << *U << " from lane " <<
938 Lane << " from " << *Scalar << ".\n");
939 ExternalUses.push_back(ExternalUser(Scalar, U, Lane));
946 void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth) {
947 bool SameTy = getSameType(VL); (void)SameTy;
948 bool isAltShuffle = false;
949 assert(SameTy && "Invalid types!");
951 if (Depth == RecursionMaxDepth) {
952 DEBUG(dbgs() << "SLP: Gathering due to max recursion depth.\n");
953 newTreeEntry(VL, false);
957 // Don't handle vectors.
958 if (VL[0]->getType()->isVectorTy()) {
959 DEBUG(dbgs() << "SLP: Gathering due to vector type.\n");
960 newTreeEntry(VL, false);
964 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
965 if (SI->getValueOperand()->getType()->isVectorTy()) {
966 DEBUG(dbgs() << "SLP: Gathering due to store vector type.\n");
967 newTreeEntry(VL, false);
970 unsigned Opcode = getSameOpcode(VL);
972 // Check that this shuffle vector refers to the alternate
973 // sequence of opcodes.
974 if (Opcode == Instruction::ShuffleVector) {
975 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
976 unsigned Op = I0->getOpcode();
977 if (Op != Instruction::ShuffleVector)
981 // If all of the operands are identical or constant we have a simple solution.
982 if (allConstant(VL) || isSplat(VL) || !getSameBlock(VL) || !Opcode) {
983 DEBUG(dbgs() << "SLP: Gathering due to C,S,B,O. \n");
984 newTreeEntry(VL, false);
988 // We now know that this is a vector of instructions of the same type from
991 // Check if this is a duplicate of another entry.
992 if (ScalarToTreeEntry.count(VL[0])) {
993 int Idx = ScalarToTreeEntry[VL[0]];
994 TreeEntry *E = &VectorizableTree[Idx];
995 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
996 DEBUG(dbgs() << "SLP: \tChecking bundle: " << *VL[i] << ".\n");
997 if (E->Scalars[i] != VL[i]) {
998 DEBUG(dbgs() << "SLP: Gathering due to partial overlap.\n");
999 newTreeEntry(VL, false);
1003 DEBUG(dbgs() << "SLP: Perfect diamond merge at " << *VL[0] << ".\n");
1007 // Check that none of the instructions in the bundle are already in the tree.
1008 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1009 if (ScalarToTreeEntry.count(VL[i])) {
1010 DEBUG(dbgs() << "SLP: The instruction (" << *VL[i] <<
1011 ") is already in tree.\n");
1012 newTreeEntry(VL, false);
1017 // If any of the scalars appears in the table OR it is marked as a value that
1018 // needs to stat scalar then we need to gather the scalars.
1019 for (unsigned i = 0, e = VL.size(); i != e; ++i) {
1020 if (ScalarToTreeEntry.count(VL[i]) || MustGather.count(VL[i])) {
1021 DEBUG(dbgs() << "SLP: Gathering due to gathered scalar. \n");
1022 newTreeEntry(VL, false);
1027 // Check that all of the users of the scalars that we want to vectorize are
1029 Instruction *VL0 = cast<Instruction>(VL[0]);
1030 BasicBlock *BB = cast<Instruction>(VL0)->getParent();
1032 if (!DT->isReachableFromEntry(BB)) {
1033 // Don't go into unreachable blocks. They may contain instructions with
1034 // dependency cycles which confuse the final scheduling.
1035 DEBUG(dbgs() << "SLP: bundle in unreachable block.\n");
1036 newTreeEntry(VL, false);
1040 // Check that every instructions appears once in this bundle.
1041 for (unsigned i = 0, e = VL.size(); i < e; ++i)
1042 for (unsigned j = i+1; j < e; ++j)
1043 if (VL[i] == VL[j]) {
1044 DEBUG(dbgs() << "SLP: Scalar used twice in bundle.\n");
1045 newTreeEntry(VL, false);
1049 auto &BSRef = BlocksSchedules[BB];
1051 BSRef = llvm::make_unique<BlockScheduling>(BB);
1053 BlockScheduling &BS = *BSRef.get();
1055 if (!BS.tryScheduleBundle(VL, AA)) {
1056 DEBUG(dbgs() << "SLP: We are not able to schedule this bundle!\n");
1057 BS.cancelScheduling(VL);
1058 newTreeEntry(VL, false);
1061 DEBUG(dbgs() << "SLP: We are able to schedule this bundle.\n");
1064 case Instruction::PHI: {
1065 PHINode *PH = dyn_cast<PHINode>(VL0);
1067 // Check for terminator values (e.g. invoke).
1068 for (unsigned j = 0; j < VL.size(); ++j)
1069 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1070 TerminatorInst *Term = dyn_cast<TerminatorInst>(
1071 cast<PHINode>(VL[j])->getIncomingValueForBlock(PH->getIncomingBlock(i)));
1073 DEBUG(dbgs() << "SLP: Need to swizzle PHINodes (TerminatorInst use).\n");
1074 BS.cancelScheduling(VL);
1075 newTreeEntry(VL, false);
1080 newTreeEntry(VL, true);
1081 DEBUG(dbgs() << "SLP: added a vector of PHINodes.\n");
1083 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1085 // Prepare the operand vector.
1086 for (unsigned j = 0; j < VL.size(); ++j)
1087 Operands.push_back(cast<PHINode>(VL[j])->getIncomingValueForBlock(
1088 PH->getIncomingBlock(i)));
1090 buildTree_rec(Operands, Depth + 1);
1094 case Instruction::ExtractElement: {
1095 bool Reuse = CanReuseExtract(VL);
1097 DEBUG(dbgs() << "SLP: Reusing extract sequence.\n");
1099 BS.cancelScheduling(VL);
1101 newTreeEntry(VL, Reuse);
1104 case Instruction::Load: {
1105 // Check if the loads are consecutive or of we need to swizzle them.
1106 for (unsigned i = 0, e = VL.size() - 1; i < e; ++i) {
1107 LoadInst *L = cast<LoadInst>(VL[i]);
1108 if (!L->isSimple()) {
1109 BS.cancelScheduling(VL);
1110 newTreeEntry(VL, false);
1111 DEBUG(dbgs() << "SLP: Gathering non-simple loads.\n");
1114 if (!isConsecutiveAccess(VL[i], VL[i + 1])) {
1115 if (VL.size() == 2 && isConsecutiveAccess(VL[1], VL[0])) {
1116 ++NumLoadsWantToChangeOrder;
1118 BS.cancelScheduling(VL);
1119 newTreeEntry(VL, false);
1120 DEBUG(dbgs() << "SLP: Gathering non-consecutive loads.\n");
1124 ++NumLoadsWantToKeepOrder;
1125 newTreeEntry(VL, true);
1126 DEBUG(dbgs() << "SLP: added a vector of loads.\n");
1129 case Instruction::ZExt:
1130 case Instruction::SExt:
1131 case Instruction::FPToUI:
1132 case Instruction::FPToSI:
1133 case Instruction::FPExt:
1134 case Instruction::PtrToInt:
1135 case Instruction::IntToPtr:
1136 case Instruction::SIToFP:
1137 case Instruction::UIToFP:
1138 case Instruction::Trunc:
1139 case Instruction::FPTrunc:
1140 case Instruction::BitCast: {
1141 Type *SrcTy = VL0->getOperand(0)->getType();
1142 for (unsigned i = 0; i < VL.size(); ++i) {
1143 Type *Ty = cast<Instruction>(VL[i])->getOperand(0)->getType();
1144 if (Ty != SrcTy || Ty->isAggregateType() || Ty->isVectorTy()) {
1145 BS.cancelScheduling(VL);
1146 newTreeEntry(VL, false);
1147 DEBUG(dbgs() << "SLP: Gathering casts with different src types.\n");
1151 newTreeEntry(VL, true);
1152 DEBUG(dbgs() << "SLP: added a vector of casts.\n");
1154 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1156 // Prepare the operand vector.
1157 for (unsigned j = 0; j < VL.size(); ++j)
1158 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1160 buildTree_rec(Operands, Depth+1);
1164 case Instruction::ICmp:
1165 case Instruction::FCmp: {
1166 // Check that all of the compares have the same predicate.
1167 CmpInst::Predicate P0 = dyn_cast<CmpInst>(VL0)->getPredicate();
1168 Type *ComparedTy = cast<Instruction>(VL[0])->getOperand(0)->getType();
1169 for (unsigned i = 1, e = VL.size(); i < e; ++i) {
1170 CmpInst *Cmp = cast<CmpInst>(VL[i]);
1171 if (Cmp->getPredicate() != P0 ||
1172 Cmp->getOperand(0)->getType() != ComparedTy) {
1173 BS.cancelScheduling(VL);
1174 newTreeEntry(VL, false);
1175 DEBUG(dbgs() << "SLP: Gathering cmp with different predicate.\n");
1180 newTreeEntry(VL, true);
1181 DEBUG(dbgs() << "SLP: added a vector of compares.\n");
1183 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1185 // Prepare the operand vector.
1186 for (unsigned j = 0; j < VL.size(); ++j)
1187 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1189 buildTree_rec(Operands, Depth+1);
1193 case Instruction::Select:
1194 case Instruction::Add:
1195 case Instruction::FAdd:
1196 case Instruction::Sub:
1197 case Instruction::FSub:
1198 case Instruction::Mul:
1199 case Instruction::FMul:
1200 case Instruction::UDiv:
1201 case Instruction::SDiv:
1202 case Instruction::FDiv:
1203 case Instruction::URem:
1204 case Instruction::SRem:
1205 case Instruction::FRem:
1206 case Instruction::Shl:
1207 case Instruction::LShr:
1208 case Instruction::AShr:
1209 case Instruction::And:
1210 case Instruction::Or:
1211 case Instruction::Xor: {
1212 newTreeEntry(VL, true);
1213 DEBUG(dbgs() << "SLP: added a vector of bin op.\n");
1215 // Sort operands of the instructions so that each side is more likely to
1216 // have the same opcode.
1217 if (isa<BinaryOperator>(VL0) && VL0->isCommutative()) {
1218 ValueList Left, Right;
1219 reorderInputsAccordingToOpcode(VL, Left, Right);
1220 buildTree_rec(Left, Depth + 1);
1221 buildTree_rec(Right, Depth + 1);
1225 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1227 // Prepare the operand vector.
1228 for (unsigned j = 0; j < VL.size(); ++j)
1229 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1231 buildTree_rec(Operands, Depth+1);
1235 case Instruction::GetElementPtr: {
1236 // We don't combine GEPs with complicated (nested) indexing.
1237 for (unsigned j = 0; j < VL.size(); ++j) {
1238 if (cast<Instruction>(VL[j])->getNumOperands() != 2) {
1239 DEBUG(dbgs() << "SLP: not-vectorizable GEP (nested indexes).\n");
1240 BS.cancelScheduling(VL);
1241 newTreeEntry(VL, false);
1246 // We can't combine several GEPs into one vector if they operate on
1248 Type *Ty0 = cast<Instruction>(VL0)->getOperand(0)->getType();
1249 for (unsigned j = 0; j < VL.size(); ++j) {
1250 Type *CurTy = cast<Instruction>(VL[j])->getOperand(0)->getType();
1252 DEBUG(dbgs() << "SLP: not-vectorizable GEP (different types).\n");
1253 BS.cancelScheduling(VL);
1254 newTreeEntry(VL, false);
1259 // We don't combine GEPs with non-constant indexes.
1260 for (unsigned j = 0; j < VL.size(); ++j) {
1261 auto Op = cast<Instruction>(VL[j])->getOperand(1);
1262 if (!isa<ConstantInt>(Op)) {
1264 dbgs() << "SLP: not-vectorizable GEP (non-constant indexes).\n");
1265 BS.cancelScheduling(VL);
1266 newTreeEntry(VL, false);
1271 newTreeEntry(VL, true);
1272 DEBUG(dbgs() << "SLP: added a vector of GEPs.\n");
1273 for (unsigned i = 0, e = 2; i < e; ++i) {
1275 // Prepare the operand vector.
1276 for (unsigned j = 0; j < VL.size(); ++j)
1277 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1279 buildTree_rec(Operands, Depth + 1);
1283 case Instruction::Store: {
1284 // Check if the stores are consecutive or of we need to swizzle them.
1285 for (unsigned i = 0, e = VL.size() - 1; i < e; ++i)
1286 if (!isConsecutiveAccess(VL[i], VL[i + 1])) {
1287 BS.cancelScheduling(VL);
1288 newTreeEntry(VL, false);
1289 DEBUG(dbgs() << "SLP: Non-consecutive store.\n");
1293 newTreeEntry(VL, true);
1294 DEBUG(dbgs() << "SLP: added a vector of stores.\n");
1297 for (unsigned j = 0; j < VL.size(); ++j)
1298 Operands.push_back(cast<Instruction>(VL[j])->getOperand(0));
1300 buildTree_rec(Operands, Depth + 1);
1303 case Instruction::Call: {
1304 // Check if the calls are all to the same vectorizable intrinsic.
1305 CallInst *CI = cast<CallInst>(VL[0]);
1306 // Check if this is an Intrinsic call or something that can be
1307 // represented by an intrinsic call
1308 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
1309 if (!isTriviallyVectorizable(ID)) {
1310 BS.cancelScheduling(VL);
1311 newTreeEntry(VL, false);
1312 DEBUG(dbgs() << "SLP: Non-vectorizable call.\n");
1315 Function *Int = CI->getCalledFunction();
1316 Value *A1I = nullptr;
1317 if (hasVectorInstrinsicScalarOpd(ID, 1))
1318 A1I = CI->getArgOperand(1);
1319 for (unsigned i = 1, e = VL.size(); i != e; ++i) {
1320 CallInst *CI2 = dyn_cast<CallInst>(VL[i]);
1321 if (!CI2 || CI2->getCalledFunction() != Int ||
1322 getIntrinsicIDForCall(CI2, TLI) != ID) {
1323 BS.cancelScheduling(VL);
1324 newTreeEntry(VL, false);
1325 DEBUG(dbgs() << "SLP: mismatched calls:" << *CI << "!=" << *VL[i]
1329 // ctlz,cttz and powi are special intrinsics whose second argument
1330 // should be same in order for them to be vectorized.
1331 if (hasVectorInstrinsicScalarOpd(ID, 1)) {
1332 Value *A1J = CI2->getArgOperand(1);
1334 BS.cancelScheduling(VL);
1335 newTreeEntry(VL, false);
1336 DEBUG(dbgs() << "SLP: mismatched arguments in call:" << *CI
1337 << " argument "<< A1I<<"!=" << A1J
1344 newTreeEntry(VL, true);
1345 for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) {
1347 // Prepare the operand vector.
1348 for (unsigned j = 0; j < VL.size(); ++j) {
1349 CallInst *CI2 = dyn_cast<CallInst>(VL[j]);
1350 Operands.push_back(CI2->getArgOperand(i));
1352 buildTree_rec(Operands, Depth + 1);
1356 case Instruction::ShuffleVector: {
1357 // If this is not an alternate sequence of opcode like add-sub
1358 // then do not vectorize this instruction.
1359 if (!isAltShuffle) {
1360 BS.cancelScheduling(VL);
1361 newTreeEntry(VL, false);
1362 DEBUG(dbgs() << "SLP: ShuffleVector are not vectorized.\n");
1365 newTreeEntry(VL, true);
1366 DEBUG(dbgs() << "SLP: added a ShuffleVector op.\n");
1367 for (unsigned i = 0, e = VL0->getNumOperands(); i < e; ++i) {
1369 // Prepare the operand vector.
1370 for (unsigned j = 0; j < VL.size(); ++j)
1371 Operands.push_back(cast<Instruction>(VL[j])->getOperand(i));
1373 buildTree_rec(Operands, Depth + 1);
1378 BS.cancelScheduling(VL);
1379 newTreeEntry(VL, false);
1380 DEBUG(dbgs() << "SLP: Gathering unknown instruction.\n");
1385 int BoUpSLP::getEntryCost(TreeEntry *E) {
1386 ArrayRef<Value*> VL = E->Scalars;
1388 Type *ScalarTy = VL[0]->getType();
1389 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1390 ScalarTy = SI->getValueOperand()->getType();
1391 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1393 if (E->NeedToGather) {
1394 if (allConstant(VL))
1397 return TTI->getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy, 0);
1399 return getGatherCost(E->Scalars);
1401 unsigned Opcode = getSameOpcode(VL);
1402 assert(Opcode && getSameType(VL) && getSameBlock(VL) && "Invalid VL");
1403 Instruction *VL0 = cast<Instruction>(VL[0]);
1405 case Instruction::PHI: {
1408 case Instruction::ExtractElement: {
1409 if (CanReuseExtract(VL)) {
1411 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
1412 ExtractElementInst *E = cast<ExtractElementInst>(VL[i]);
1414 // Take credit for instruction that will become dead.
1416 TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy, i);
1420 return getGatherCost(VecTy);
1422 case Instruction::ZExt:
1423 case Instruction::SExt:
1424 case Instruction::FPToUI:
1425 case Instruction::FPToSI:
1426 case Instruction::FPExt:
1427 case Instruction::PtrToInt:
1428 case Instruction::IntToPtr:
1429 case Instruction::SIToFP:
1430 case Instruction::UIToFP:
1431 case Instruction::Trunc:
1432 case Instruction::FPTrunc:
1433 case Instruction::BitCast: {
1434 Type *SrcTy = VL0->getOperand(0)->getType();
1436 // Calculate the cost of this instruction.
1437 int ScalarCost = VL.size() * TTI->getCastInstrCost(VL0->getOpcode(),
1438 VL0->getType(), SrcTy);
1440 VectorType *SrcVecTy = VectorType::get(SrcTy, VL.size());
1441 int VecCost = TTI->getCastInstrCost(VL0->getOpcode(), VecTy, SrcVecTy);
1442 return VecCost - ScalarCost;
1444 case Instruction::FCmp:
1445 case Instruction::ICmp:
1446 case Instruction::Select:
1447 case Instruction::Add:
1448 case Instruction::FAdd:
1449 case Instruction::Sub:
1450 case Instruction::FSub:
1451 case Instruction::Mul:
1452 case Instruction::FMul:
1453 case Instruction::UDiv:
1454 case Instruction::SDiv:
1455 case Instruction::FDiv:
1456 case Instruction::URem:
1457 case Instruction::SRem:
1458 case Instruction::FRem:
1459 case Instruction::Shl:
1460 case Instruction::LShr:
1461 case Instruction::AShr:
1462 case Instruction::And:
1463 case Instruction::Or:
1464 case Instruction::Xor: {
1465 // Calculate the cost of this instruction.
1468 if (Opcode == Instruction::FCmp || Opcode == Instruction::ICmp ||
1469 Opcode == Instruction::Select) {
1470 VectorType *MaskTy = VectorType::get(Builder.getInt1Ty(), VL.size());
1471 ScalarCost = VecTy->getNumElements() *
1472 TTI->getCmpSelInstrCost(Opcode, ScalarTy, Builder.getInt1Ty());
1473 VecCost = TTI->getCmpSelInstrCost(Opcode, VecTy, MaskTy);
1475 // Certain instructions can be cheaper to vectorize if they have a
1476 // constant second vector operand.
1477 TargetTransformInfo::OperandValueKind Op1VK =
1478 TargetTransformInfo::OK_AnyValue;
1479 TargetTransformInfo::OperandValueKind Op2VK =
1480 TargetTransformInfo::OK_UniformConstantValue;
1481 TargetTransformInfo::OperandValueProperties Op1VP =
1482 TargetTransformInfo::OP_None;
1483 TargetTransformInfo::OperandValueProperties Op2VP =
1484 TargetTransformInfo::OP_None;
1486 // If all operands are exactly the same ConstantInt then set the
1487 // operand kind to OK_UniformConstantValue.
1488 // If instead not all operands are constants, then set the operand kind
1489 // to OK_AnyValue. If all operands are constants but not the same,
1490 // then set the operand kind to OK_NonUniformConstantValue.
1491 ConstantInt *CInt = nullptr;
1492 for (unsigned i = 0; i < VL.size(); ++i) {
1493 const Instruction *I = cast<Instruction>(VL[i]);
1494 if (!isa<ConstantInt>(I->getOperand(1))) {
1495 Op2VK = TargetTransformInfo::OK_AnyValue;
1499 CInt = cast<ConstantInt>(I->getOperand(1));
1502 if (Op2VK == TargetTransformInfo::OK_UniformConstantValue &&
1503 CInt != cast<ConstantInt>(I->getOperand(1)))
1504 Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
1506 // FIXME: Currently cost of model modification for division by
1507 // power of 2 is handled only for X86. Add support for other targets.
1508 if (Op2VK == TargetTransformInfo::OK_UniformConstantValue && CInt &&
1509 CInt->getValue().isPowerOf2())
1510 Op2VP = TargetTransformInfo::OP_PowerOf2;
1512 ScalarCost = VecTy->getNumElements() *
1513 TTI->getArithmeticInstrCost(Opcode, ScalarTy, Op1VK, Op2VK,
1515 VecCost = TTI->getArithmeticInstrCost(Opcode, VecTy, Op1VK, Op2VK,
1518 return VecCost - ScalarCost;
1520 case Instruction::GetElementPtr: {
1521 TargetTransformInfo::OperandValueKind Op1VK =
1522 TargetTransformInfo::OK_AnyValue;
1523 TargetTransformInfo::OperandValueKind Op2VK =
1524 TargetTransformInfo::OK_UniformConstantValue;
1527 VecTy->getNumElements() *
1528 TTI->getArithmeticInstrCost(Instruction::Add, ScalarTy, Op1VK, Op2VK);
1530 TTI->getArithmeticInstrCost(Instruction::Add, VecTy, Op1VK, Op2VK);
1532 return VecCost - ScalarCost;
1534 case Instruction::Load: {
1535 // Cost of wide load - cost of scalar loads.
1536 int ScalarLdCost = VecTy->getNumElements() *
1537 TTI->getMemoryOpCost(Instruction::Load, ScalarTy, 1, 0);
1538 int VecLdCost = TTI->getMemoryOpCost(Instruction::Load, VecTy, 1, 0);
1539 return VecLdCost - ScalarLdCost;
1541 case Instruction::Store: {
1542 // We know that we can merge the stores. Calculate the cost.
1543 int ScalarStCost = VecTy->getNumElements() *
1544 TTI->getMemoryOpCost(Instruction::Store, ScalarTy, 1, 0);
1545 int VecStCost = TTI->getMemoryOpCost(Instruction::Store, VecTy, 1, 0);
1546 return VecStCost - ScalarStCost;
1548 case Instruction::Call: {
1549 CallInst *CI = cast<CallInst>(VL0);
1550 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
1552 // Calculate the cost of the scalar and vector calls.
1553 SmallVector<Type*, 4> ScalarTys, VecTys;
1554 for (unsigned op = 0, opc = CI->getNumArgOperands(); op!= opc; ++op) {
1555 ScalarTys.push_back(CI->getArgOperand(op)->getType());
1556 VecTys.push_back(VectorType::get(CI->getArgOperand(op)->getType(),
1557 VecTy->getNumElements()));
1560 int ScalarCallCost = VecTy->getNumElements() *
1561 TTI->getIntrinsicInstrCost(ID, ScalarTy, ScalarTys);
1563 int VecCallCost = TTI->getIntrinsicInstrCost(ID, VecTy, VecTys);
1565 DEBUG(dbgs() << "SLP: Call cost "<< VecCallCost - ScalarCallCost
1566 << " (" << VecCallCost << "-" << ScalarCallCost << ")"
1567 << " for " << *CI << "\n");
1569 return VecCallCost - ScalarCallCost;
1571 case Instruction::ShuffleVector: {
1572 TargetTransformInfo::OperandValueKind Op1VK =
1573 TargetTransformInfo::OK_AnyValue;
1574 TargetTransformInfo::OperandValueKind Op2VK =
1575 TargetTransformInfo::OK_AnyValue;
1578 for (unsigned i = 0; i < VL.size(); ++i) {
1579 Instruction *I = cast<Instruction>(VL[i]);
1583 TTI->getArithmeticInstrCost(I->getOpcode(), ScalarTy, Op1VK, Op2VK);
1585 // VecCost is equal to sum of the cost of creating 2 vectors
1586 // and the cost of creating shuffle.
1587 Instruction *I0 = cast<Instruction>(VL[0]);
1589 TTI->getArithmeticInstrCost(I0->getOpcode(), VecTy, Op1VK, Op2VK);
1590 Instruction *I1 = cast<Instruction>(VL[1]);
1592 TTI->getArithmeticInstrCost(I1->getOpcode(), VecTy, Op1VK, Op2VK);
1594 TTI->getShuffleCost(TargetTransformInfo::SK_Alternate, VecTy, 0);
1595 return VecCost - ScalarCost;
1598 llvm_unreachable("Unknown instruction");
1602 bool BoUpSLP::isFullyVectorizableTinyTree() {
1603 DEBUG(dbgs() << "SLP: Check whether the tree with height " <<
1604 VectorizableTree.size() << " is fully vectorizable .\n");
1606 // We only handle trees of height 2.
1607 if (VectorizableTree.size() != 2)
1610 // Handle splat stores.
1611 if (!VectorizableTree[0].NeedToGather && isSplat(VectorizableTree[1].Scalars))
1614 // Gathering cost would be too much for tiny trees.
1615 if (VectorizableTree[0].NeedToGather || VectorizableTree[1].NeedToGather)
1621 int BoUpSLP::getSpillCost() {
1622 // Walk from the bottom of the tree to the top, tracking which values are
1623 // live. When we see a call instruction that is not part of our tree,
1624 // query TTI to see if there is a cost to keeping values live over it
1625 // (for example, if spills and fills are required).
1626 unsigned BundleWidth = VectorizableTree.front().Scalars.size();
1629 SmallPtrSet<Instruction*, 4> LiveValues;
1630 Instruction *PrevInst = nullptr;
1632 for (unsigned N = 0; N < VectorizableTree.size(); ++N) {
1633 Instruction *Inst = dyn_cast<Instruction>(VectorizableTree[N].Scalars[0]);
1643 dbgs() << "SLP: #LV: " << LiveValues.size();
1644 for (auto *X : LiveValues)
1645 dbgs() << " " << X->getName();
1646 dbgs() << ", Looking at ";
1650 // Update LiveValues.
1651 LiveValues.erase(PrevInst);
1652 for (auto &J : PrevInst->operands()) {
1653 if (isa<Instruction>(&*J) && ScalarToTreeEntry.count(&*J))
1654 LiveValues.insert(cast<Instruction>(&*J));
1657 // Now find the sequence of instructions between PrevInst and Inst.
1658 BasicBlock::reverse_iterator InstIt(Inst), PrevInstIt(PrevInst);
1660 while (InstIt != PrevInstIt) {
1661 if (PrevInstIt == PrevInst->getParent()->rend()) {
1662 PrevInstIt = Inst->getParent()->rbegin();
1666 if (isa<CallInst>(&*PrevInstIt) && &*PrevInstIt != PrevInst) {
1667 SmallVector<Type*, 4> V;
1668 for (auto *II : LiveValues)
1669 V.push_back(VectorType::get(II->getType(), BundleWidth));
1670 Cost += TTI->getCostOfKeepingLiveOverCall(V);
1679 DEBUG(dbgs() << "SLP: SpillCost=" << Cost << "\n");
1683 int BoUpSLP::getTreeCost() {
1685 DEBUG(dbgs() << "SLP: Calculating cost for tree of size " <<
1686 VectorizableTree.size() << ".\n");
1688 // We only vectorize tiny trees if it is fully vectorizable.
1689 if (VectorizableTree.size() < 3 && !isFullyVectorizableTinyTree()) {
1690 if (!VectorizableTree.size()) {
1691 assert(!ExternalUses.size() && "We should not have any external users");
1696 unsigned BundleWidth = VectorizableTree[0].Scalars.size();
1698 for (unsigned i = 0, e = VectorizableTree.size(); i != e; ++i) {
1699 int C = getEntryCost(&VectorizableTree[i]);
1700 DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle that starts with "
1701 << *VectorizableTree[i].Scalars[0] << " .\n");
1705 SmallSet<Value *, 16> ExtractCostCalculated;
1706 int ExtractCost = 0;
1707 for (UserList::iterator I = ExternalUses.begin(), E = ExternalUses.end();
1709 // We only add extract cost once for the same scalar.
1710 if (!ExtractCostCalculated.insert(I->Scalar))
1713 VectorType *VecTy = VectorType::get(I->Scalar->getType(), BundleWidth);
1714 ExtractCost += TTI->getVectorInstrCost(Instruction::ExtractElement, VecTy,
1718 Cost += getSpillCost();
1720 DEBUG(dbgs() << "SLP: Total Cost " << Cost + ExtractCost<< ".\n");
1721 return Cost + ExtractCost;
1724 int BoUpSLP::getGatherCost(Type *Ty) {
1726 for (unsigned i = 0, e = cast<VectorType>(Ty)->getNumElements(); i < e; ++i)
1727 Cost += TTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
1731 int BoUpSLP::getGatherCost(ArrayRef<Value *> VL) {
1732 // Find the type of the operands in VL.
1733 Type *ScalarTy = VL[0]->getType();
1734 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1735 ScalarTy = SI->getValueOperand()->getType();
1736 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1737 // Find the cost of inserting/extracting values from the vector.
1738 return getGatherCost(VecTy);
1741 Value *BoUpSLP::getPointerOperand(Value *I) {
1742 if (LoadInst *LI = dyn_cast<LoadInst>(I))
1743 return LI->getPointerOperand();
1744 if (StoreInst *SI = dyn_cast<StoreInst>(I))
1745 return SI->getPointerOperand();
1749 unsigned BoUpSLP::getAddressSpaceOperand(Value *I) {
1750 if (LoadInst *L = dyn_cast<LoadInst>(I))
1751 return L->getPointerAddressSpace();
1752 if (StoreInst *S = dyn_cast<StoreInst>(I))
1753 return S->getPointerAddressSpace();
1757 bool BoUpSLP::isConsecutiveAccess(Value *A, Value *B) {
1758 Value *PtrA = getPointerOperand(A);
1759 Value *PtrB = getPointerOperand(B);
1760 unsigned ASA = getAddressSpaceOperand(A);
1761 unsigned ASB = getAddressSpaceOperand(B);
1763 // Check that the address spaces match and that the pointers are valid.
1764 if (!PtrA || !PtrB || (ASA != ASB))
1767 // Make sure that A and B are different pointers of the same type.
1768 if (PtrA == PtrB || PtrA->getType() != PtrB->getType())
1771 unsigned PtrBitWidth = DL->getPointerSizeInBits(ASA);
1772 Type *Ty = cast<PointerType>(PtrA->getType())->getElementType();
1773 APInt Size(PtrBitWidth, DL->getTypeStoreSize(Ty));
1775 APInt OffsetA(PtrBitWidth, 0), OffsetB(PtrBitWidth, 0);
1776 PtrA = PtrA->stripAndAccumulateInBoundsConstantOffsets(*DL, OffsetA);
1777 PtrB = PtrB->stripAndAccumulateInBoundsConstantOffsets(*DL, OffsetB);
1779 APInt OffsetDelta = OffsetB - OffsetA;
1781 // Check if they are based on the same pointer. That makes the offsets
1784 return OffsetDelta == Size;
1786 // Compute the necessary base pointer delta to have the necessary final delta
1787 // equal to the size.
1788 APInt BaseDelta = Size - OffsetDelta;
1790 // Otherwise compute the distance with SCEV between the base pointers.
1791 const SCEV *PtrSCEVA = SE->getSCEV(PtrA);
1792 const SCEV *PtrSCEVB = SE->getSCEV(PtrB);
1793 const SCEV *C = SE->getConstant(BaseDelta);
1794 const SCEV *X = SE->getAddExpr(PtrSCEVA, C);
1795 return X == PtrSCEVB;
1798 void BoUpSLP::setInsertPointAfterBundle(ArrayRef<Value *> VL) {
1799 Instruction *VL0 = cast<Instruction>(VL[0]);
1800 BasicBlock::iterator NextInst = VL0;
1802 Builder.SetInsertPoint(VL0->getParent(), NextInst);
1803 Builder.SetCurrentDebugLocation(VL0->getDebugLoc());
1806 Value *BoUpSLP::Gather(ArrayRef<Value *> VL, VectorType *Ty) {
1807 Value *Vec = UndefValue::get(Ty);
1808 // Generate the 'InsertElement' instruction.
1809 for (unsigned i = 0; i < Ty->getNumElements(); ++i) {
1810 Vec = Builder.CreateInsertElement(Vec, VL[i], Builder.getInt32(i));
1811 if (Instruction *Insrt = dyn_cast<Instruction>(Vec)) {
1812 GatherSeq.insert(Insrt);
1813 CSEBlocks.insert(Insrt->getParent());
1815 // Add to our 'need-to-extract' list.
1816 if (ScalarToTreeEntry.count(VL[i])) {
1817 int Idx = ScalarToTreeEntry[VL[i]];
1818 TreeEntry *E = &VectorizableTree[Idx];
1819 // Find which lane we need to extract.
1821 for (unsigned Lane = 0, LE = VL.size(); Lane != LE; ++Lane) {
1822 // Is this the lane of the scalar that we are looking for ?
1823 if (E->Scalars[Lane] == VL[i]) {
1828 assert(FoundLane >= 0 && "Could not find the correct lane");
1829 ExternalUses.push_back(ExternalUser(VL[i], Insrt, FoundLane));
1837 Value *BoUpSLP::alreadyVectorized(ArrayRef<Value *> VL) const {
1838 SmallDenseMap<Value*, int>::const_iterator Entry
1839 = ScalarToTreeEntry.find(VL[0]);
1840 if (Entry != ScalarToTreeEntry.end()) {
1841 int Idx = Entry->second;
1842 const TreeEntry *En = &VectorizableTree[Idx];
1843 if (En->isSame(VL) && En->VectorizedValue)
1844 return En->VectorizedValue;
1849 Value *BoUpSLP::vectorizeTree(ArrayRef<Value *> VL) {
1850 if (ScalarToTreeEntry.count(VL[0])) {
1851 int Idx = ScalarToTreeEntry[VL[0]];
1852 TreeEntry *E = &VectorizableTree[Idx];
1854 return vectorizeTree(E);
1857 Type *ScalarTy = VL[0]->getType();
1858 if (StoreInst *SI = dyn_cast<StoreInst>(VL[0]))
1859 ScalarTy = SI->getValueOperand()->getType();
1860 VectorType *VecTy = VectorType::get(ScalarTy, VL.size());
1862 return Gather(VL, VecTy);
1865 Value *BoUpSLP::vectorizeTree(TreeEntry *E) {
1866 IRBuilder<>::InsertPointGuard Guard(Builder);
1868 if (E->VectorizedValue) {
1869 DEBUG(dbgs() << "SLP: Diamond merged for " << *E->Scalars[0] << ".\n");
1870 return E->VectorizedValue;
1873 Instruction *VL0 = cast<Instruction>(E->Scalars[0]);
1874 Type *ScalarTy = VL0->getType();
1875 if (StoreInst *SI = dyn_cast<StoreInst>(VL0))
1876 ScalarTy = SI->getValueOperand()->getType();
1877 VectorType *VecTy = VectorType::get(ScalarTy, E->Scalars.size());
1879 if (E->NeedToGather) {
1880 setInsertPointAfterBundle(E->Scalars);
1881 return Gather(E->Scalars, VecTy);
1884 unsigned Opcode = getSameOpcode(E->Scalars);
1887 case Instruction::PHI: {
1888 PHINode *PH = dyn_cast<PHINode>(VL0);
1889 Builder.SetInsertPoint(PH->getParent()->getFirstNonPHI());
1890 Builder.SetCurrentDebugLocation(PH->getDebugLoc());
1891 PHINode *NewPhi = Builder.CreatePHI(VecTy, PH->getNumIncomingValues());
1892 E->VectorizedValue = NewPhi;
1894 // PHINodes may have multiple entries from the same block. We want to
1895 // visit every block once.
1896 SmallSet<BasicBlock*, 4> VisitedBBs;
1898 for (unsigned i = 0, e = PH->getNumIncomingValues(); i < e; ++i) {
1900 BasicBlock *IBB = PH->getIncomingBlock(i);
1902 if (!VisitedBBs.insert(IBB)) {
1903 NewPhi->addIncoming(NewPhi->getIncomingValueForBlock(IBB), IBB);
1907 // Prepare the operand vector.
1908 for (unsigned j = 0; j < E->Scalars.size(); ++j)
1909 Operands.push_back(cast<PHINode>(E->Scalars[j])->
1910 getIncomingValueForBlock(IBB));
1912 Builder.SetInsertPoint(IBB->getTerminator());
1913 Builder.SetCurrentDebugLocation(PH->getDebugLoc());
1914 Value *Vec = vectorizeTree(Operands);
1915 NewPhi->addIncoming(Vec, IBB);
1918 assert(NewPhi->getNumIncomingValues() == PH->getNumIncomingValues() &&
1919 "Invalid number of incoming values");
1923 case Instruction::ExtractElement: {
1924 if (CanReuseExtract(E->Scalars)) {
1925 Value *V = VL0->getOperand(0);
1926 E->VectorizedValue = V;
1929 return Gather(E->Scalars, VecTy);
1931 case Instruction::ZExt:
1932 case Instruction::SExt:
1933 case Instruction::FPToUI:
1934 case Instruction::FPToSI:
1935 case Instruction::FPExt:
1936 case Instruction::PtrToInt:
1937 case Instruction::IntToPtr:
1938 case Instruction::SIToFP:
1939 case Instruction::UIToFP:
1940 case Instruction::Trunc:
1941 case Instruction::FPTrunc:
1942 case Instruction::BitCast: {
1944 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
1945 INVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1947 setInsertPointAfterBundle(E->Scalars);
1949 Value *InVec = vectorizeTree(INVL);
1951 if (Value *V = alreadyVectorized(E->Scalars))
1954 CastInst *CI = dyn_cast<CastInst>(VL0);
1955 Value *V = Builder.CreateCast(CI->getOpcode(), InVec, VecTy);
1956 E->VectorizedValue = V;
1957 ++NumVectorInstructions;
1960 case Instruction::FCmp:
1961 case Instruction::ICmp: {
1962 ValueList LHSV, RHSV;
1963 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1964 LHSV.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1965 RHSV.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1968 setInsertPointAfterBundle(E->Scalars);
1970 Value *L = vectorizeTree(LHSV);
1971 Value *R = vectorizeTree(RHSV);
1973 if (Value *V = alreadyVectorized(E->Scalars))
1976 CmpInst::Predicate P0 = dyn_cast<CmpInst>(VL0)->getPredicate();
1978 if (Opcode == Instruction::FCmp)
1979 V = Builder.CreateFCmp(P0, L, R);
1981 V = Builder.CreateICmp(P0, L, R);
1983 E->VectorizedValue = V;
1984 ++NumVectorInstructions;
1987 case Instruction::Select: {
1988 ValueList TrueVec, FalseVec, CondVec;
1989 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
1990 CondVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
1991 TrueVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
1992 FalseVec.push_back(cast<Instruction>(E->Scalars[i])->getOperand(2));
1995 setInsertPointAfterBundle(E->Scalars);
1997 Value *Cond = vectorizeTree(CondVec);
1998 Value *True = vectorizeTree(TrueVec);
1999 Value *False = vectorizeTree(FalseVec);
2001 if (Value *V = alreadyVectorized(E->Scalars))
2004 Value *V = Builder.CreateSelect(Cond, True, False);
2005 E->VectorizedValue = V;
2006 ++NumVectorInstructions;
2009 case Instruction::Add:
2010 case Instruction::FAdd:
2011 case Instruction::Sub:
2012 case Instruction::FSub:
2013 case Instruction::Mul:
2014 case Instruction::FMul:
2015 case Instruction::UDiv:
2016 case Instruction::SDiv:
2017 case Instruction::FDiv:
2018 case Instruction::URem:
2019 case Instruction::SRem:
2020 case Instruction::FRem:
2021 case Instruction::Shl:
2022 case Instruction::LShr:
2023 case Instruction::AShr:
2024 case Instruction::And:
2025 case Instruction::Or:
2026 case Instruction::Xor: {
2027 ValueList LHSVL, RHSVL;
2028 if (isa<BinaryOperator>(VL0) && VL0->isCommutative())
2029 reorderInputsAccordingToOpcode(E->Scalars, LHSVL, RHSVL);
2031 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
2032 LHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
2033 RHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
2036 setInsertPointAfterBundle(E->Scalars);
2038 Value *LHS = vectorizeTree(LHSVL);
2039 Value *RHS = vectorizeTree(RHSVL);
2041 if (LHS == RHS && isa<Instruction>(LHS)) {
2042 assert((VL0->getOperand(0) == VL0->getOperand(1)) && "Invalid order");
2045 if (Value *V = alreadyVectorized(E->Scalars))
2048 BinaryOperator *BinOp = cast<BinaryOperator>(VL0);
2049 Value *V = Builder.CreateBinOp(BinOp->getOpcode(), LHS, RHS);
2050 E->VectorizedValue = V;
2051 propagateIRFlags(E->VectorizedValue, E->Scalars);
2052 ++NumVectorInstructions;
2054 if (Instruction *I = dyn_cast<Instruction>(V))
2055 return propagateMetadata(I, E->Scalars);
2059 case Instruction::Load: {
2060 // Loads are inserted at the head of the tree because we don't want to
2061 // sink them all the way down past store instructions.
2062 setInsertPointAfterBundle(E->Scalars);
2064 LoadInst *LI = cast<LoadInst>(VL0);
2065 Type *ScalarLoadTy = LI->getType();
2066 unsigned AS = LI->getPointerAddressSpace();
2068 Value *VecPtr = Builder.CreateBitCast(LI->getPointerOperand(),
2069 VecTy->getPointerTo(AS));
2071 // The pointer operand uses an in-tree scalar so we add the new BitCast to
2072 // ExternalUses list to make sure that an extract will be generated in the
2074 if (ScalarToTreeEntry.count(LI->getPointerOperand()))
2075 ExternalUses.push_back(
2076 ExternalUser(LI->getPointerOperand(), cast<User>(VecPtr), 0));
2078 unsigned Alignment = LI->getAlignment();
2079 LI = Builder.CreateLoad(VecPtr);
2081 Alignment = DL->getABITypeAlignment(ScalarLoadTy);
2082 LI->setAlignment(Alignment);
2083 E->VectorizedValue = LI;
2084 ++NumVectorInstructions;
2085 return propagateMetadata(LI, E->Scalars);
2087 case Instruction::Store: {
2088 StoreInst *SI = cast<StoreInst>(VL0);
2089 unsigned Alignment = SI->getAlignment();
2090 unsigned AS = SI->getPointerAddressSpace();
2093 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
2094 ValueOp.push_back(cast<StoreInst>(E->Scalars[i])->getValueOperand());
2096 setInsertPointAfterBundle(E->Scalars);
2098 Value *VecValue = vectorizeTree(ValueOp);
2099 Value *VecPtr = Builder.CreateBitCast(SI->getPointerOperand(),
2100 VecTy->getPointerTo(AS));
2101 StoreInst *S = Builder.CreateStore(VecValue, VecPtr);
2103 // The pointer operand uses an in-tree scalar so we add the new BitCast to
2104 // ExternalUses list to make sure that an extract will be generated in the
2106 if (ScalarToTreeEntry.count(SI->getPointerOperand()))
2107 ExternalUses.push_back(
2108 ExternalUser(SI->getPointerOperand(), cast<User>(VecPtr), 0));
2111 Alignment = DL->getABITypeAlignment(SI->getValueOperand()->getType());
2112 S->setAlignment(Alignment);
2113 E->VectorizedValue = S;
2114 ++NumVectorInstructions;
2115 return propagateMetadata(S, E->Scalars);
2117 case Instruction::GetElementPtr: {
2118 setInsertPointAfterBundle(E->Scalars);
2121 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
2122 Op0VL.push_back(cast<GetElementPtrInst>(E->Scalars[i])->getOperand(0));
2124 Value *Op0 = vectorizeTree(Op0VL);
2126 std::vector<Value *> OpVecs;
2127 for (int j = 1, e = cast<GetElementPtrInst>(VL0)->getNumOperands(); j < e;
2130 for (int i = 0, e = E->Scalars.size(); i < e; ++i)
2131 OpVL.push_back(cast<GetElementPtrInst>(E->Scalars[i])->getOperand(j));
2133 Value *OpVec = vectorizeTree(OpVL);
2134 OpVecs.push_back(OpVec);
2137 Value *V = Builder.CreateGEP(Op0, OpVecs);
2138 E->VectorizedValue = V;
2139 ++NumVectorInstructions;
2141 if (Instruction *I = dyn_cast<Instruction>(V))
2142 return propagateMetadata(I, E->Scalars);
2146 case Instruction::Call: {
2147 CallInst *CI = cast<CallInst>(VL0);
2148 setInsertPointAfterBundle(E->Scalars);
2150 Intrinsic::ID IID = Intrinsic::not_intrinsic;
2151 Value *ScalarArg = nullptr;
2152 if (CI && (FI = CI->getCalledFunction())) {
2153 IID = (Intrinsic::ID) FI->getIntrinsicID();
2155 std::vector<Value *> OpVecs;
2156 for (int j = 0, e = CI->getNumArgOperands(); j < e; ++j) {
2158 // ctlz,cttz and powi are special intrinsics whose second argument is
2159 // a scalar. This argument should not be vectorized.
2160 if (hasVectorInstrinsicScalarOpd(IID, 1) && j == 1) {
2161 CallInst *CEI = cast<CallInst>(E->Scalars[0]);
2162 ScalarArg = CEI->getArgOperand(j);
2163 OpVecs.push_back(CEI->getArgOperand(j));
2166 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
2167 CallInst *CEI = cast<CallInst>(E->Scalars[i]);
2168 OpVL.push_back(CEI->getArgOperand(j));
2171 Value *OpVec = vectorizeTree(OpVL);
2172 DEBUG(dbgs() << "SLP: OpVec[" << j << "]: " << *OpVec << "\n");
2173 OpVecs.push_back(OpVec);
2176 Module *M = F->getParent();
2177 Intrinsic::ID ID = getIntrinsicIDForCall(CI, TLI);
2178 Type *Tys[] = { VectorType::get(CI->getType(), E->Scalars.size()) };
2179 Function *CF = Intrinsic::getDeclaration(M, ID, Tys);
2180 Value *V = Builder.CreateCall(CF, OpVecs);
2182 // The scalar argument uses an in-tree scalar so we add the new vectorized
2183 // call to ExternalUses list to make sure that an extract will be
2184 // generated in the future.
2185 if (ScalarArg && ScalarToTreeEntry.count(ScalarArg))
2186 ExternalUses.push_back(ExternalUser(ScalarArg, cast<User>(V), 0));
2188 E->VectorizedValue = V;
2189 ++NumVectorInstructions;
2192 case Instruction::ShuffleVector: {
2193 ValueList LHSVL, RHSVL;
2194 for (int i = 0, e = E->Scalars.size(); i < e; ++i) {
2195 LHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(0));
2196 RHSVL.push_back(cast<Instruction>(E->Scalars[i])->getOperand(1));
2198 setInsertPointAfterBundle(E->Scalars);
2200 Value *LHS = vectorizeTree(LHSVL);
2201 Value *RHS = vectorizeTree(RHSVL);
2203 if (Value *V = alreadyVectorized(E->Scalars))
2206 // Create a vector of LHS op1 RHS
2207 BinaryOperator *BinOp0 = cast<BinaryOperator>(VL0);
2208 Value *V0 = Builder.CreateBinOp(BinOp0->getOpcode(), LHS, RHS);
2210 // Create a vector of LHS op2 RHS
2211 Instruction *VL1 = cast<Instruction>(E->Scalars[1]);
2212 BinaryOperator *BinOp1 = cast<BinaryOperator>(VL1);
2213 Value *V1 = Builder.CreateBinOp(BinOp1->getOpcode(), LHS, RHS);
2215 // Create shuffle to take alternate operations from the vector.
2216 // Also, gather up odd and even scalar ops to propagate IR flags to
2217 // each vector operation.
2218 ValueList OddScalars, EvenScalars;
2219 unsigned e = E->Scalars.size();
2220 SmallVector<Constant *, 8> Mask(e);
2221 for (unsigned i = 0; i < e; ++i) {
2223 Mask[i] = Builder.getInt32(e + i);
2224 OddScalars.push_back(E->Scalars[i]);
2226 Mask[i] = Builder.getInt32(i);
2227 EvenScalars.push_back(E->Scalars[i]);
2231 Value *ShuffleMask = ConstantVector::get(Mask);
2232 propagateIRFlags(V0, EvenScalars);
2233 propagateIRFlags(V1, OddScalars);
2235 Value *V = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
2236 E->VectorizedValue = V;
2237 ++NumVectorInstructions;
2238 if (Instruction *I = dyn_cast<Instruction>(V))
2239 return propagateMetadata(I, E->Scalars);
2244 llvm_unreachable("unknown inst");
2249 Value *BoUpSLP::vectorizeTree() {
2251 // All blocks must be scheduled before any instructions are inserted.
2252 for (auto &BSIter : BlocksSchedules) {
2253 scheduleBlock(BSIter.second.get());
2256 Builder.SetInsertPoint(F->getEntryBlock().begin());
2257 vectorizeTree(&VectorizableTree[0]);
2259 DEBUG(dbgs() << "SLP: Extracting " << ExternalUses.size() << " values .\n");
2261 // Extract all of the elements with the external uses.
2262 for (UserList::iterator it = ExternalUses.begin(), e = ExternalUses.end();
2264 Value *Scalar = it->Scalar;
2265 llvm::User *User = it->User;
2267 // Skip users that we already RAUW. This happens when one instruction
2268 // has multiple uses of the same value.
2269 if (std::find(Scalar->user_begin(), Scalar->user_end(), User) ==
2272 assert(ScalarToTreeEntry.count(Scalar) && "Invalid scalar");
2274 int Idx = ScalarToTreeEntry[Scalar];
2275 TreeEntry *E = &VectorizableTree[Idx];
2276 assert(!E->NeedToGather && "Extracting from a gather list");
2278 Value *Vec = E->VectorizedValue;
2279 assert(Vec && "Can't find vectorizable value");
2281 Value *Lane = Builder.getInt32(it->Lane);
2282 // Generate extracts for out-of-tree users.
2283 // Find the insertion point for the extractelement lane.
2284 if (isa<Instruction>(Vec)){
2285 if (PHINode *PH = dyn_cast<PHINode>(User)) {
2286 for (int i = 0, e = PH->getNumIncomingValues(); i != e; ++i) {
2287 if (PH->getIncomingValue(i) == Scalar) {
2288 Builder.SetInsertPoint(PH->getIncomingBlock(i)->getTerminator());
2289 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
2290 CSEBlocks.insert(PH->getIncomingBlock(i));
2291 PH->setOperand(i, Ex);
2295 Builder.SetInsertPoint(cast<Instruction>(User));
2296 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
2297 CSEBlocks.insert(cast<Instruction>(User)->getParent());
2298 User->replaceUsesOfWith(Scalar, Ex);
2301 Builder.SetInsertPoint(F->getEntryBlock().begin());
2302 Value *Ex = Builder.CreateExtractElement(Vec, Lane);
2303 CSEBlocks.insert(&F->getEntryBlock());
2304 User->replaceUsesOfWith(Scalar, Ex);
2307 DEBUG(dbgs() << "SLP: Replaced:" << *User << ".\n");
2310 // For each vectorized value:
2311 for (int EIdx = 0, EE = VectorizableTree.size(); EIdx < EE; ++EIdx) {
2312 TreeEntry *Entry = &VectorizableTree[EIdx];
2315 for (int Lane = 0, LE = Entry->Scalars.size(); Lane != LE; ++Lane) {
2316 Value *Scalar = Entry->Scalars[Lane];
2317 // No need to handle users of gathered values.
2318 if (Entry->NeedToGather)
2321 assert(Entry->VectorizedValue && "Can't find vectorizable value");
2323 Type *Ty = Scalar->getType();
2324 if (!Ty->isVoidTy()) {
2326 for (User *U : Scalar->users()) {
2327 DEBUG(dbgs() << "SLP: \tvalidating user:" << *U << ".\n");
2329 assert((ScalarToTreeEntry.count(U) ||
2330 // It is legal to replace users in the ignorelist by undef.
2331 (std::find(UserIgnoreList.begin(), UserIgnoreList.end(), U) !=
2332 UserIgnoreList.end())) &&
2333 "Replacing out-of-tree value with undef");
2336 Value *Undef = UndefValue::get(Ty);
2337 Scalar->replaceAllUsesWith(Undef);
2339 DEBUG(dbgs() << "SLP: \tErasing scalar:" << *Scalar << ".\n");
2340 cast<Instruction>(Scalar)->eraseFromParent();
2344 Builder.ClearInsertionPoint();
2346 return VectorizableTree[0].VectorizedValue;
2349 void BoUpSLP::optimizeGatherSequence() {
2350 DEBUG(dbgs() << "SLP: Optimizing " << GatherSeq.size()
2351 << " gather sequences instructions.\n");
2352 // LICM InsertElementInst sequences.
2353 for (SetVector<Instruction *>::iterator it = GatherSeq.begin(),
2354 e = GatherSeq.end(); it != e; ++it) {
2355 InsertElementInst *Insert = dyn_cast<InsertElementInst>(*it);
2360 // Check if this block is inside a loop.
2361 Loop *L = LI->getLoopFor(Insert->getParent());
2365 // Check if it has a preheader.
2366 BasicBlock *PreHeader = L->getLoopPreheader();
2370 // If the vector or the element that we insert into it are
2371 // instructions that are defined in this basic block then we can't
2372 // hoist this instruction.
2373 Instruction *CurrVec = dyn_cast<Instruction>(Insert->getOperand(0));
2374 Instruction *NewElem = dyn_cast<Instruction>(Insert->getOperand(1));
2375 if (CurrVec && L->contains(CurrVec))
2377 if (NewElem && L->contains(NewElem))
2380 // We can hoist this instruction. Move it to the pre-header.
2381 Insert->moveBefore(PreHeader->getTerminator());
2384 // Make a list of all reachable blocks in our CSE queue.
2385 SmallVector<const DomTreeNode *, 8> CSEWorkList;
2386 CSEWorkList.reserve(CSEBlocks.size());
2387 for (BasicBlock *BB : CSEBlocks)
2388 if (DomTreeNode *N = DT->getNode(BB)) {
2389 assert(DT->isReachableFromEntry(N));
2390 CSEWorkList.push_back(N);
2393 // Sort blocks by domination. This ensures we visit a block after all blocks
2394 // dominating it are visited.
2395 std::stable_sort(CSEWorkList.begin(), CSEWorkList.end(),
2396 [this](const DomTreeNode *A, const DomTreeNode *B) {
2397 return DT->properlyDominates(A, B);
2400 // Perform O(N^2) search over the gather sequences and merge identical
2401 // instructions. TODO: We can further optimize this scan if we split the
2402 // instructions into different buckets based on the insert lane.
2403 SmallVector<Instruction *, 16> Visited;
2404 for (auto I = CSEWorkList.begin(), E = CSEWorkList.end(); I != E; ++I) {
2405 assert((I == CSEWorkList.begin() || !DT->dominates(*I, *std::prev(I))) &&
2406 "Worklist not sorted properly!");
2407 BasicBlock *BB = (*I)->getBlock();
2408 // For all instructions in blocks containing gather sequences:
2409 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e;) {
2410 Instruction *In = it++;
2411 if (!isa<InsertElementInst>(In) && !isa<ExtractElementInst>(In))
2414 // Check if we can replace this instruction with any of the
2415 // visited instructions.
2416 for (SmallVectorImpl<Instruction *>::iterator v = Visited.begin(),
2419 if (In->isIdenticalTo(*v) &&
2420 DT->dominates((*v)->getParent(), In->getParent())) {
2421 In->replaceAllUsesWith(*v);
2422 In->eraseFromParent();
2428 assert(std::find(Visited.begin(), Visited.end(), In) == Visited.end());
2429 Visited.push_back(In);
2437 // Groups the instructions to a bundle (which is then a single scheduling entity)
2438 // and schedules instructions until the bundle gets ready.
2439 bool BoUpSLP::BlockScheduling::tryScheduleBundle(ArrayRef<Value *> VL,
2440 AliasAnalysis *AA) {
2441 if (isa<PHINode>(VL[0]))
2444 // Initialize the instruction bundle.
2445 Instruction *OldScheduleEnd = ScheduleEnd;
2446 ScheduleData *PrevInBundle = nullptr;
2447 ScheduleData *Bundle = nullptr;
2448 bool ReSchedule = false;
2449 DEBUG(dbgs() << "SLP: bundle: " << *VL[0] << "\n");
2450 for (Value *V : VL) {
2451 extendSchedulingRegion(V);
2452 ScheduleData *BundleMember = getScheduleData(V);
2453 assert(BundleMember &&
2454 "no ScheduleData for bundle member (maybe not in same basic block)");
2455 if (BundleMember->IsScheduled) {
2456 // A bundle member was scheduled as single instruction before and now
2457 // needs to be scheduled as part of the bundle. We just get rid of the
2458 // existing schedule.
2459 DEBUG(dbgs() << "SLP: reset schedule because " << *BundleMember
2460 << " was already scheduled\n");
2463 assert(BundleMember->isSchedulingEntity() &&
2464 "bundle member already part of other bundle");
2466 PrevInBundle->NextInBundle = BundleMember;
2468 Bundle = BundleMember;
2470 BundleMember->UnscheduledDepsInBundle = 0;
2471 Bundle->UnscheduledDepsInBundle += BundleMember->UnscheduledDeps;
2473 // Group the instructions to a bundle.
2474 BundleMember->FirstInBundle = Bundle;
2475 PrevInBundle = BundleMember;
2477 if (ScheduleEnd != OldScheduleEnd) {
2478 // The scheduling region got new instructions at the lower end (or it is a
2479 // new region for the first bundle). This makes it necessary to
2480 // recalculate all dependencies.
2481 // It is seldom that this needs to be done a second time after adding the
2482 // initial bundle to the region.
2483 for (auto *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
2484 ScheduleData *SD = getScheduleData(I);
2485 SD->clearDependencies();
2491 initialFillReadyList(ReadyInsts);
2494 DEBUG(dbgs() << "SLP: try schedule bundle " << *Bundle << " in block "
2495 << BB->getName() << "\n");
2497 calculateDependencies(Bundle, true, AA);
2499 // Now try to schedule the new bundle. As soon as the bundle is "ready" it
2500 // means that there are no cyclic dependencies and we can schedule it.
2501 // Note that's important that we don't "schedule" the bundle yet (see
2502 // cancelScheduling).
2503 while (!Bundle->isReady() && !ReadyInsts.empty()) {
2505 ScheduleData *pickedSD = ReadyInsts.back();
2506 ReadyInsts.pop_back();
2508 if (pickedSD->isSchedulingEntity() && pickedSD->isReady()) {
2509 schedule(pickedSD, ReadyInsts);
2512 return Bundle->isReady();
2515 void BoUpSLP::BlockScheduling::cancelScheduling(ArrayRef<Value *> VL) {
2516 if (isa<PHINode>(VL[0]))
2519 ScheduleData *Bundle = getScheduleData(VL[0]);
2520 DEBUG(dbgs() << "SLP: cancel scheduling of " << *Bundle << "\n");
2521 assert(!Bundle->IsScheduled &&
2522 "Can't cancel bundle which is already scheduled");
2523 assert(Bundle->isSchedulingEntity() && Bundle->isPartOfBundle() &&
2524 "tried to unbundle something which is not a bundle");
2526 // Un-bundle: make single instructions out of the bundle.
2527 ScheduleData *BundleMember = Bundle;
2528 while (BundleMember) {
2529 assert(BundleMember->FirstInBundle == Bundle && "corrupt bundle links");
2530 BundleMember->FirstInBundle = BundleMember;
2531 ScheduleData *Next = BundleMember->NextInBundle;
2532 BundleMember->NextInBundle = nullptr;
2533 BundleMember->UnscheduledDepsInBundle = BundleMember->UnscheduledDeps;
2534 if (BundleMember->UnscheduledDepsInBundle == 0) {
2535 ReadyInsts.insert(BundleMember);
2537 BundleMember = Next;
2541 void BoUpSLP::BlockScheduling::extendSchedulingRegion(Value *V) {
2542 if (getScheduleData(V))
2544 Instruction *I = dyn_cast<Instruction>(V);
2545 assert(I && "bundle member must be an instruction");
2546 assert(!isa<PHINode>(I) && "phi nodes don't need to be scheduled");
2547 if (!ScheduleStart) {
2548 // It's the first instruction in the new region.
2549 initScheduleData(I, I->getNextNode(), nullptr, nullptr);
2551 ScheduleEnd = I->getNextNode();
2552 assert(ScheduleEnd && "tried to vectorize a TerminatorInst?");
2553 DEBUG(dbgs() << "SLP: initialize schedule region to " << *I << "\n");
2556 // Search up and down at the same time, because we don't know if the new
2557 // instruction is above or below the existing scheduling region.
2558 BasicBlock::reverse_iterator UpIter(ScheduleStart);
2559 BasicBlock::reverse_iterator UpperEnd = BB->rend();
2560 BasicBlock::iterator DownIter(ScheduleEnd);
2561 BasicBlock::iterator LowerEnd = BB->end();
2563 if (UpIter != UpperEnd) {
2564 if (&*UpIter == I) {
2565 initScheduleData(I, ScheduleStart, nullptr, FirstLoadStoreInRegion);
2567 DEBUG(dbgs() << "SLP: extend schedule region start to " << *I << "\n");
2572 if (DownIter != LowerEnd) {
2573 if (&*DownIter == I) {
2574 initScheduleData(ScheduleEnd, I->getNextNode(), LastLoadStoreInRegion,
2576 ScheduleEnd = I->getNextNode();
2577 assert(ScheduleEnd && "tried to vectorize a TerminatorInst?");
2578 DEBUG(dbgs() << "SLP: extend schedule region end to " << *I << "\n");
2583 assert((UpIter != UpperEnd || DownIter != LowerEnd) &&
2584 "instruction not found in block");
2588 void BoUpSLP::BlockScheduling::initScheduleData(Instruction *FromI,
2590 ScheduleData *PrevLoadStore,
2591 ScheduleData *NextLoadStore) {
2592 ScheduleData *CurrentLoadStore = PrevLoadStore;
2593 for (Instruction *I = FromI; I != ToI; I = I->getNextNode()) {
2594 ScheduleData *SD = ScheduleDataMap[I];
2596 // Allocate a new ScheduleData for the instruction.
2597 if (ChunkPos >= ChunkSize) {
2598 ScheduleDataChunks.push_back(
2599 llvm::make_unique<ScheduleData[]>(ChunkSize));
2602 SD = &(ScheduleDataChunks.back()[ChunkPos++]);
2603 ScheduleDataMap[I] = SD;
2606 assert(!isInSchedulingRegion(SD) &&
2607 "new ScheduleData already in scheduling region");
2608 SD->init(SchedulingRegionID);
2610 if (I->mayReadOrWriteMemory()) {
2611 // Update the linked list of memory accessing instructions.
2612 if (CurrentLoadStore) {
2613 CurrentLoadStore->NextLoadStore = SD;
2615 FirstLoadStoreInRegion = SD;
2617 CurrentLoadStore = SD;
2620 if (NextLoadStore) {
2621 if (CurrentLoadStore)
2622 CurrentLoadStore->NextLoadStore = NextLoadStore;
2624 LastLoadStoreInRegion = CurrentLoadStore;
2628 /// \returns the AA location that is being access by the instruction.
2629 static AliasAnalysis::Location getLocation(Instruction *I, AliasAnalysis *AA) {
2630 if (StoreInst *SI = dyn_cast<StoreInst>(I))
2631 return AA->getLocation(SI);
2632 if (LoadInst *LI = dyn_cast<LoadInst>(I))
2633 return AA->getLocation(LI);
2634 return AliasAnalysis::Location();
2637 void BoUpSLP::BlockScheduling::calculateDependencies(ScheduleData *SD,
2638 bool InsertInReadyList,
2639 AliasAnalysis *AA) {
2640 assert(SD->isSchedulingEntity());
2642 SmallVector<ScheduleData *, 10> WorkList;
2643 WorkList.push_back(SD);
2645 while (!WorkList.empty()) {
2646 ScheduleData *SD = WorkList.back();
2647 WorkList.pop_back();
2649 ScheduleData *BundleMember = SD;
2650 while (BundleMember) {
2651 assert(isInSchedulingRegion(BundleMember));
2652 if (!BundleMember->hasValidDependencies()) {
2654 DEBUG(dbgs() << "SLP: update deps of " << *BundleMember << "\n");
2655 BundleMember->Dependencies = 0;
2656 BundleMember->resetUnscheduledDeps();
2658 // Handle def-use chain dependencies.
2659 for (User *U : BundleMember->Inst->users()) {
2660 if (isa<Instruction>(U)) {
2661 ScheduleData *UseSD = getScheduleData(U);
2662 if (UseSD && isInSchedulingRegion(UseSD->FirstInBundle)) {
2663 BundleMember->Dependencies++;
2664 ScheduleData *DestBundle = UseSD->FirstInBundle;
2665 if (!DestBundle->IsScheduled) {
2666 BundleMember->incrementUnscheduledDeps(1);
2668 if (!DestBundle->hasValidDependencies()) {
2669 WorkList.push_back(DestBundle);
2673 // I'm not sure if this can ever happen. But we need to be safe.
2674 // This lets the instruction/bundle never be scheduled and eventally
2675 // disable vectorization.
2676 BundleMember->Dependencies++;
2677 BundleMember->incrementUnscheduledDeps(1);
2681 // Handle the memory dependencies.
2682 ScheduleData *DepDest = BundleMember->NextLoadStore;
2684 AliasAnalysis::Location SrcLoc = getLocation(BundleMember->Inst, AA);
2685 bool SrcMayWrite = BundleMember->Inst->mayWriteToMemory();
2688 assert(isInSchedulingRegion(DepDest));
2689 if (SrcMayWrite || DepDest->Inst->mayWriteToMemory()) {
2690 AliasAnalysis::Location DstLoc = getLocation(DepDest->Inst, AA);
2691 if (!SrcLoc.Ptr || !DstLoc.Ptr || AA->alias(SrcLoc, DstLoc)) {
2692 DepDest->MemoryDependencies.push_back(BundleMember);
2693 BundleMember->Dependencies++;
2694 ScheduleData *DestBundle = DepDest->FirstInBundle;
2695 if (!DestBundle->IsScheduled) {
2696 BundleMember->incrementUnscheduledDeps(1);
2698 if (!DestBundle->hasValidDependencies()) {
2699 WorkList.push_back(DestBundle);
2703 DepDest = DepDest->NextLoadStore;
2707 BundleMember = BundleMember->NextInBundle;
2709 if (InsertInReadyList && SD->isReady()) {
2710 ReadyInsts.push_back(SD);
2711 DEBUG(dbgs() << "SLP: gets ready on update: " << *SD->Inst << "\n");
2716 void BoUpSLP::BlockScheduling::resetSchedule() {
2717 assert(ScheduleStart &&
2718 "tried to reset schedule on block which has not been scheduled");
2719 for (Instruction *I = ScheduleStart; I != ScheduleEnd; I = I->getNextNode()) {
2720 ScheduleData *SD = getScheduleData(I);
2721 assert(isInSchedulingRegion(SD));
2722 SD->IsScheduled = false;
2723 SD->resetUnscheduledDeps();
2728 void BoUpSLP::scheduleBlock(BlockScheduling *BS) {
2730 if (!BS->ScheduleStart)
2733 DEBUG(dbgs() << "SLP: schedule block " << BS->BB->getName() << "\n");
2735 BS->resetSchedule();
2737 // For the real scheduling we use a more sophisticated ready-list: it is
2738 // sorted by the original instruction location. This lets the final schedule
2739 // be as close as possible to the original instruction order.
2740 struct ScheduleDataCompare {
2741 bool operator()(ScheduleData *SD1, ScheduleData *SD2) {
2742 return SD2->SchedulingPriority < SD1->SchedulingPriority;
2745 std::set<ScheduleData *, ScheduleDataCompare> ReadyInsts;
2747 // Ensure that all depencency data is updated and fill the ready-list with
2748 // initial instructions.
2750 int NumToSchedule = 0;
2751 for (auto *I = BS->ScheduleStart; I != BS->ScheduleEnd;
2752 I = I->getNextNode()) {
2753 ScheduleData *SD = BS->getScheduleData(I);
2755 SD->isPartOfBundle() == (ScalarToTreeEntry.count(SD->Inst) != 0) &&
2756 "scheduler and vectorizer have different opinion on what is a bundle");
2757 SD->FirstInBundle->SchedulingPriority = Idx++;
2758 if (SD->isSchedulingEntity()) {
2759 BS->calculateDependencies(SD, false, AA);
2763 BS->initialFillReadyList(ReadyInsts);
2765 Instruction *LastScheduledInst = BS->ScheduleEnd;
2767 // Do the "real" scheduling.
2768 while (!ReadyInsts.empty()) {
2769 ScheduleData *picked = *ReadyInsts.begin();
2770 ReadyInsts.erase(ReadyInsts.begin());
2772 // Move the scheduled instruction(s) to their dedicated places, if not
2774 ScheduleData *BundleMember = picked;
2775 while (BundleMember) {
2776 Instruction *pickedInst = BundleMember->Inst;
2777 if (LastScheduledInst->getNextNode() != pickedInst) {
2778 BS->BB->getInstList().remove(pickedInst);
2779 BS->BB->getInstList().insert(LastScheduledInst, pickedInst);
2781 LastScheduledInst = pickedInst;
2782 BundleMember = BundleMember->NextInBundle;
2785 BS->schedule(picked, ReadyInsts);
2788 assert(NumToSchedule == 0 && "could not schedule all instructions");
2790 // Avoid duplicate scheduling of the block.
2791 BS->ScheduleStart = nullptr;
2794 /// The SLPVectorizer Pass.
2795 struct SLPVectorizer : public FunctionPass {
2796 typedef SmallVector<StoreInst *, 8> StoreList;
2797 typedef MapVector<Value *, StoreList> StoreListMap;
2799 /// Pass identification, replacement for typeid
2802 explicit SLPVectorizer() : FunctionPass(ID) {
2803 initializeSLPVectorizerPass(*PassRegistry::getPassRegistry());
2806 ScalarEvolution *SE;
2807 const DataLayout *DL;
2808 TargetTransformInfo *TTI;
2809 TargetLibraryInfo *TLI;
2814 bool runOnFunction(Function &F) override {
2815 if (skipOptnoneFunction(F))
2818 SE = &getAnalysis<ScalarEvolution>();
2819 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
2820 DL = DLP ? &DLP->getDataLayout() : nullptr;
2821 TTI = &getAnalysis<TargetTransformInfo>();
2822 TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
2823 AA = &getAnalysis<AliasAnalysis>();
2824 LI = &getAnalysis<LoopInfo>();
2825 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2828 bool Changed = false;
2830 // If the target claims to have no vector registers don't attempt
2832 if (!TTI->getNumberOfRegisters(true))
2835 // Must have DataLayout. We can't require it because some tests run w/o
2840 // Don't vectorize when the attribute NoImplicitFloat is used.
2841 if (F.hasFnAttribute(Attribute::NoImplicitFloat))
2844 DEBUG(dbgs() << "SLP: Analyzing blocks in " << F.getName() << ".\n");
2846 // Use the bottom up slp vectorizer to construct chains that start with
2847 // store instructions.
2848 BoUpSLP R(&F, SE, DL, TTI, TLI, AA, LI, DT);
2850 // Scan the blocks in the function in post order.
2851 for (po_iterator<BasicBlock*> it = po_begin(&F.getEntryBlock()),
2852 e = po_end(&F.getEntryBlock()); it != e; ++it) {
2853 BasicBlock *BB = *it;
2854 // Vectorize trees that end at stores.
2855 if (unsigned count = collectStores(BB, R)) {
2857 DEBUG(dbgs() << "SLP: Found " << count << " stores to vectorize.\n");
2858 Changed |= vectorizeStoreChains(R);
2861 // Vectorize trees that end at reductions.
2862 Changed |= vectorizeChainsInBlock(BB, R);
2866 R.optimizeGatherSequence();
2867 DEBUG(dbgs() << "SLP: vectorized \"" << F.getName() << "\"\n");
2868 DEBUG(verifyFunction(F));
2873 void getAnalysisUsage(AnalysisUsage &AU) const override {
2874 FunctionPass::getAnalysisUsage(AU);
2875 AU.addRequired<ScalarEvolution>();
2876 AU.addRequired<AliasAnalysis>();
2877 AU.addRequired<TargetTransformInfo>();
2878 AU.addRequired<LoopInfo>();
2879 AU.addRequired<DominatorTreeWrapperPass>();
2880 AU.addPreserved<LoopInfo>();
2881 AU.addPreserved<DominatorTreeWrapperPass>();
2882 AU.setPreservesCFG();
2887 /// \brief Collect memory references and sort them according to their base
2888 /// object. We sort the stores to their base objects to reduce the cost of the
2889 /// quadratic search on the stores. TODO: We can further reduce this cost
2890 /// if we flush the chain creation every time we run into a memory barrier.
2891 unsigned collectStores(BasicBlock *BB, BoUpSLP &R);
2893 /// \brief Try to vectorize a chain that starts at two arithmetic instrs.
2894 bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R);
2896 /// \brief Try to vectorize a list of operands.
2897 /// \@param BuildVector A list of users to ignore for the purpose of
2898 /// scheduling and that don't need extracting.
2899 /// \returns true if a value was vectorized.
2900 bool tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
2901 ArrayRef<Value *> BuildVector = None,
2902 bool allowReorder = false);
2904 /// \brief Try to vectorize a chain that may start at the operands of \V;
2905 bool tryToVectorize(BinaryOperator *V, BoUpSLP &R);
2907 /// \brief Vectorize the stores that were collected in StoreRefs.
2908 bool vectorizeStoreChains(BoUpSLP &R);
2910 /// \brief Scan the basic block and look for patterns that are likely to start
2911 /// a vectorization chain.
2912 bool vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R);
2914 bool vectorizeStoreChain(ArrayRef<Value *> Chain, int CostThreshold,
2917 bool vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold,
2920 StoreListMap StoreRefs;
2923 /// \brief Check that the Values in the slice in VL array are still existent in
2924 /// the WeakVH array.
2925 /// Vectorization of part of the VL array may cause later values in the VL array
2926 /// to become invalid. We track when this has happened in the WeakVH array.
2927 static bool hasValueBeenRAUWed(ArrayRef<Value *> &VL,
2928 SmallVectorImpl<WeakVH> &VH,
2929 unsigned SliceBegin,
2930 unsigned SliceSize) {
2931 for (unsigned i = SliceBegin; i < SliceBegin + SliceSize; ++i)
2938 bool SLPVectorizer::vectorizeStoreChain(ArrayRef<Value *> Chain,
2939 int CostThreshold, BoUpSLP &R) {
2940 unsigned ChainLen = Chain.size();
2941 DEBUG(dbgs() << "SLP: Analyzing a store chain of length " << ChainLen
2943 Type *StoreTy = cast<StoreInst>(Chain[0])->getValueOperand()->getType();
2944 unsigned Sz = DL->getTypeSizeInBits(StoreTy);
2945 unsigned VF = MinVecRegSize / Sz;
2947 if (!isPowerOf2_32(Sz) || VF < 2)
2950 // Keep track of values that were deleted by vectorizing in the loop below.
2951 SmallVector<WeakVH, 8> TrackValues(Chain.begin(), Chain.end());
2953 bool Changed = false;
2954 // Look for profitable vectorizable trees at all offsets, starting at zero.
2955 for (unsigned i = 0, e = ChainLen; i < e; ++i) {
2959 // Check that a previous iteration of this loop did not delete the Value.
2960 if (hasValueBeenRAUWed(Chain, TrackValues, i, VF))
2963 DEBUG(dbgs() << "SLP: Analyzing " << VF << " stores at offset " << i
2965 ArrayRef<Value *> Operands = Chain.slice(i, VF);
2967 R.buildTree(Operands);
2969 int Cost = R.getTreeCost();
2971 DEBUG(dbgs() << "SLP: Found cost=" << Cost << " for VF=" << VF << "\n");
2972 if (Cost < CostThreshold) {
2973 DEBUG(dbgs() << "SLP: Decided to vectorize cost=" << Cost << "\n");
2976 // Move to the next bundle.
2985 bool SLPVectorizer::vectorizeStores(ArrayRef<StoreInst *> Stores,
2986 int costThreshold, BoUpSLP &R) {
2987 SetVector<Value *> Heads, Tails;
2988 SmallDenseMap<Value *, Value *> ConsecutiveChain;
2990 // We may run into multiple chains that merge into a single chain. We mark the
2991 // stores that we vectorized so that we don't visit the same store twice.
2992 BoUpSLP::ValueSet VectorizedStores;
2993 bool Changed = false;
2995 // Do a quadratic search on all of the given stores and find
2996 // all of the pairs of stores that follow each other.
2997 for (unsigned i = 0, e = Stores.size(); i < e; ++i) {
2998 for (unsigned j = 0; j < e; ++j) {
3002 if (R.isConsecutiveAccess(Stores[i], Stores[j])) {
3003 Tails.insert(Stores[j]);
3004 Heads.insert(Stores[i]);
3005 ConsecutiveChain[Stores[i]] = Stores[j];
3010 // For stores that start but don't end a link in the chain:
3011 for (SetVector<Value *>::iterator it = Heads.begin(), e = Heads.end();
3013 if (Tails.count(*it))
3016 // We found a store instr that starts a chain. Now follow the chain and try
3018 BoUpSLP::ValueList Operands;
3020 // Collect the chain into a list.
3021 while (Tails.count(I) || Heads.count(I)) {
3022 if (VectorizedStores.count(I))
3024 Operands.push_back(I);
3025 // Move to the next value in the chain.
3026 I = ConsecutiveChain[I];
3029 bool Vectorized = vectorizeStoreChain(Operands, costThreshold, R);
3031 // Mark the vectorized stores so that we don't vectorize them again.
3033 VectorizedStores.insert(Operands.begin(), Operands.end());
3034 Changed |= Vectorized;
3041 unsigned SLPVectorizer::collectStores(BasicBlock *BB, BoUpSLP &R) {
3044 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; ++it) {
3045 StoreInst *SI = dyn_cast<StoreInst>(it);
3049 // Don't touch volatile stores.
3050 if (!SI->isSimple())
3053 // Check that the pointer points to scalars.
3054 Type *Ty = SI->getValueOperand()->getType();
3055 if (Ty->isAggregateType() || Ty->isVectorTy())
3058 // Find the base pointer.
3059 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), DL);
3061 // Save the store locations.
3062 StoreRefs[Ptr].push_back(SI);
3068 bool SLPVectorizer::tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) {
3071 Value *VL[] = { A, B };
3072 return tryToVectorizeList(VL, R, None, true);
3075 bool SLPVectorizer::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
3076 ArrayRef<Value *> BuildVector,
3077 bool allowReorder) {
3081 DEBUG(dbgs() << "SLP: Vectorizing a list of length = " << VL.size() << ".\n");
3083 // Check that all of the parts are scalar instructions of the same type.
3084 Instruction *I0 = dyn_cast<Instruction>(VL[0]);
3088 unsigned Opcode0 = I0->getOpcode();
3090 Type *Ty0 = I0->getType();
3091 unsigned Sz = DL->getTypeSizeInBits(Ty0);
3092 unsigned VF = MinVecRegSize / Sz;
3094 for (int i = 0, e = VL.size(); i < e; ++i) {
3095 Type *Ty = VL[i]->getType();
3096 if (Ty->isAggregateType() || Ty->isVectorTy())
3098 Instruction *Inst = dyn_cast<Instruction>(VL[i]);
3099 if (!Inst || Inst->getOpcode() != Opcode0)
3103 bool Changed = false;
3105 // Keep track of values that were deleted by vectorizing in the loop below.
3106 SmallVector<WeakVH, 8> TrackValues(VL.begin(), VL.end());
3108 for (unsigned i = 0, e = VL.size(); i < e; ++i) {
3109 unsigned OpsWidth = 0;
3116 if (!isPowerOf2_32(OpsWidth) || OpsWidth < 2)
3119 // Check that a previous iteration of this loop did not delete the Value.
3120 if (hasValueBeenRAUWed(VL, TrackValues, i, OpsWidth))
3123 DEBUG(dbgs() << "SLP: Analyzing " << OpsWidth << " operations "
3125 ArrayRef<Value *> Ops = VL.slice(i, OpsWidth);
3127 ArrayRef<Value *> BuildVectorSlice;
3128 if (!BuildVector.empty())
3129 BuildVectorSlice = BuildVector.slice(i, OpsWidth);
3131 R.buildTree(Ops, BuildVectorSlice);
3132 // TODO: check if we can allow reordering also for other cases than
3133 // tryToVectorizePair()
3134 if (allowReorder && R.shouldReorder()) {
3135 assert(Ops.size() == 2);
3136 assert(BuildVectorSlice.empty());
3137 Value *ReorderedOps[] = { Ops[1], Ops[0] };
3138 R.buildTree(ReorderedOps, None);
3140 int Cost = R.getTreeCost();
3142 if (Cost < -SLPCostThreshold) {
3143 DEBUG(dbgs() << "SLP: Vectorizing list at cost:" << Cost << ".\n");
3144 Value *VectorizedRoot = R.vectorizeTree();
3146 // Reconstruct the build vector by extracting the vectorized root. This
3147 // way we handle the case where some elements of the vector are undefined.
3148 // (return (inserelt <4 xi32> (insertelt undef (opd0) 0) (opd1) 2))
3149 if (!BuildVectorSlice.empty()) {
3150 // The insert point is the last build vector instruction. The vectorized
3151 // root will precede it. This guarantees that we get an instruction. The
3152 // vectorized tree could have been constant folded.
3153 Instruction *InsertAfter = cast<Instruction>(BuildVectorSlice.back());
3154 unsigned VecIdx = 0;
3155 for (auto &V : BuildVectorSlice) {
3156 IRBuilder<true, NoFolder> Builder(
3157 ++BasicBlock::iterator(InsertAfter));
3158 InsertElementInst *IE = cast<InsertElementInst>(V);
3159 Instruction *Extract = cast<Instruction>(Builder.CreateExtractElement(
3160 VectorizedRoot, Builder.getInt32(VecIdx++)));
3161 IE->setOperand(1, Extract);
3162 IE->removeFromParent();
3163 IE->insertAfter(Extract);
3167 // Move to the next bundle.
3176 bool SLPVectorizer::tryToVectorize(BinaryOperator *V, BoUpSLP &R) {
3180 // Try to vectorize V.
3181 if (tryToVectorizePair(V->getOperand(0), V->getOperand(1), R))
3184 BinaryOperator *A = dyn_cast<BinaryOperator>(V->getOperand(0));
3185 BinaryOperator *B = dyn_cast<BinaryOperator>(V->getOperand(1));
3187 if (B && B->hasOneUse()) {
3188 BinaryOperator *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
3189 BinaryOperator *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
3190 if (tryToVectorizePair(A, B0, R)) {
3193 if (tryToVectorizePair(A, B1, R)) {
3199 if (A && A->hasOneUse()) {
3200 BinaryOperator *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
3201 BinaryOperator *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
3202 if (tryToVectorizePair(A0, B, R)) {
3205 if (tryToVectorizePair(A1, B, R)) {
3212 /// \brief Generate a shuffle mask to be used in a reduction tree.
3214 /// \param VecLen The length of the vector to be reduced.
3215 /// \param NumEltsToRdx The number of elements that should be reduced in the
3217 /// \param IsPairwise Whether the reduction is a pairwise or splitting
3218 /// reduction. A pairwise reduction will generate a mask of
3219 /// <0,2,...> or <1,3,..> while a splitting reduction will generate
3220 /// <2,3, undef,undef> for a vector of 4 and NumElts = 2.
3221 /// \param IsLeft True will generate a mask of even elements, odd otherwise.
3222 static Value *createRdxShuffleMask(unsigned VecLen, unsigned NumEltsToRdx,
3223 bool IsPairwise, bool IsLeft,
3224 IRBuilder<> &Builder) {
3225 assert((IsPairwise || !IsLeft) && "Don't support a <0,1,undef,...> mask");
3227 SmallVector<Constant *, 32> ShuffleMask(
3228 VecLen, UndefValue::get(Builder.getInt32Ty()));
3231 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right).
3232 for (unsigned i = 0; i != NumEltsToRdx; ++i)
3233 ShuffleMask[i] = Builder.getInt32(2 * i + !IsLeft);
3235 // Move the upper half of the vector to the lower half.
3236 for (unsigned i = 0; i != NumEltsToRdx; ++i)
3237 ShuffleMask[i] = Builder.getInt32(NumEltsToRdx + i);
3239 return ConstantVector::get(ShuffleMask);
3243 /// Model horizontal reductions.
3245 /// A horizontal reduction is a tree of reduction operations (currently add and
3246 /// fadd) that has operations that can be put into a vector as its leaf.
3247 /// For example, this tree:
3254 /// This tree has "mul" as its reduced values and "+" as its reduction
3255 /// operations. A reduction might be feeding into a store or a binary operation
3270 class HorizontalReduction {
3271 SmallVector<Value *, 16> ReductionOps;
3272 SmallVector<Value *, 32> ReducedVals;
3274 BinaryOperator *ReductionRoot;
3275 PHINode *ReductionPHI;
3277 /// The opcode of the reduction.
3278 unsigned ReductionOpcode;
3279 /// The opcode of the values we perform a reduction on.
3280 unsigned ReducedValueOpcode;
3281 /// The width of one full horizontal reduction operation.
3282 unsigned ReduxWidth;
3283 /// Should we model this reduction as a pairwise reduction tree or a tree that
3284 /// splits the vector in halves and adds those halves.
3285 bool IsPairwiseReduction;
3288 HorizontalReduction()
3289 : ReductionRoot(nullptr), ReductionPHI(nullptr), ReductionOpcode(0),
3290 ReducedValueOpcode(0), ReduxWidth(0), IsPairwiseReduction(false) {}
3292 /// \brief Try to find a reduction tree.
3293 bool matchAssociativeReduction(PHINode *Phi, BinaryOperator *B,
3294 const DataLayout *DL) {
3296 std::find(Phi->op_begin(), Phi->op_end(), B) != Phi->op_end()) &&
3297 "Thi phi needs to use the binary operator");
3299 // We could have a initial reductions that is not an add.
3300 // r *= v1 + v2 + v3 + v4
3301 // In such a case start looking for a tree rooted in the first '+'.
3303 if (B->getOperand(0) == Phi) {
3305 B = dyn_cast<BinaryOperator>(B->getOperand(1));
3306 } else if (B->getOperand(1) == Phi) {
3308 B = dyn_cast<BinaryOperator>(B->getOperand(0));
3315 Type *Ty = B->getType();
3316 if (Ty->isVectorTy())
3319 ReductionOpcode = B->getOpcode();
3320 ReducedValueOpcode = 0;
3321 ReduxWidth = MinVecRegSize / DL->getTypeSizeInBits(Ty);
3328 // We currently only support adds.
3329 if (ReductionOpcode != Instruction::Add &&
3330 ReductionOpcode != Instruction::FAdd)
3333 // Post order traverse the reduction tree starting at B. We only handle true
3334 // trees containing only binary operators.
3335 SmallVector<std::pair<BinaryOperator *, unsigned>, 32> Stack;
3336 Stack.push_back(std::make_pair(B, 0));
3337 while (!Stack.empty()) {
3338 BinaryOperator *TreeN = Stack.back().first;
3339 unsigned EdgeToVist = Stack.back().second++;
3340 bool IsReducedValue = TreeN->getOpcode() != ReductionOpcode;
3342 // Only handle trees in the current basic block.
3343 if (TreeN->getParent() != B->getParent())
3346 // Each tree node needs to have one user except for the ultimate
3348 if (!TreeN->hasOneUse() && TreeN != B)
3352 if (EdgeToVist == 2 || IsReducedValue) {
3353 if (IsReducedValue) {
3354 // Make sure that the opcodes of the operations that we are going to
3356 if (!ReducedValueOpcode)
3357 ReducedValueOpcode = TreeN->getOpcode();
3358 else if (ReducedValueOpcode != TreeN->getOpcode())
3360 ReducedVals.push_back(TreeN);
3362 // We need to be able to reassociate the adds.
3363 if (!TreeN->isAssociative())
3365 ReductionOps.push_back(TreeN);
3372 // Visit left or right.
3373 Value *NextV = TreeN->getOperand(EdgeToVist);
3374 BinaryOperator *Next = dyn_cast<BinaryOperator>(NextV);
3376 Stack.push_back(std::make_pair(Next, 0));
3377 else if (NextV != Phi)
3383 /// \brief Attempt to vectorize the tree found by
3384 /// matchAssociativeReduction.
3385 bool tryToReduce(BoUpSLP &V, TargetTransformInfo *TTI) {
3386 if (ReducedVals.empty())
3389 unsigned NumReducedVals = ReducedVals.size();
3390 if (NumReducedVals < ReduxWidth)
3393 Value *VectorizedTree = nullptr;
3394 IRBuilder<> Builder(ReductionRoot);
3395 FastMathFlags Unsafe;
3396 Unsafe.setUnsafeAlgebra();
3397 Builder.SetFastMathFlags(Unsafe);
3400 for (; i < NumReducedVals - ReduxWidth + 1; i += ReduxWidth) {
3401 V.buildTree(makeArrayRef(&ReducedVals[i], ReduxWidth), ReductionOps);
3404 int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]);
3405 if (Cost >= -SLPCostThreshold)
3408 DEBUG(dbgs() << "SLP: Vectorizing horizontal reduction at cost:" << Cost
3411 // Vectorize a tree.
3412 DebugLoc Loc = cast<Instruction>(ReducedVals[i])->getDebugLoc();
3413 Value *VectorizedRoot = V.vectorizeTree();
3415 // Emit a reduction.
3416 Value *ReducedSubTree = emitReduction(VectorizedRoot, Builder);
3417 if (VectorizedTree) {
3418 Builder.SetCurrentDebugLocation(Loc);
3419 VectorizedTree = createBinOp(Builder, ReductionOpcode, VectorizedTree,
3420 ReducedSubTree, "bin.rdx");
3422 VectorizedTree = ReducedSubTree;
3425 if (VectorizedTree) {
3426 // Finish the reduction.
3427 for (; i < NumReducedVals; ++i) {
3428 Builder.SetCurrentDebugLocation(
3429 cast<Instruction>(ReducedVals[i])->getDebugLoc());
3430 VectorizedTree = createBinOp(Builder, ReductionOpcode, VectorizedTree,
3435 assert(ReductionRoot && "Need a reduction operation");
3436 ReductionRoot->setOperand(0, VectorizedTree);
3437 ReductionRoot->setOperand(1, ReductionPHI);
3439 ReductionRoot->replaceAllUsesWith(VectorizedTree);
3441 return VectorizedTree != nullptr;
3446 /// \brief Calcuate the cost of a reduction.
3447 int getReductionCost(TargetTransformInfo *TTI, Value *FirstReducedVal) {
3448 Type *ScalarTy = FirstReducedVal->getType();
3449 Type *VecTy = VectorType::get(ScalarTy, ReduxWidth);
3451 int PairwiseRdxCost = TTI->getReductionCost(ReductionOpcode, VecTy, true);
3452 int SplittingRdxCost = TTI->getReductionCost(ReductionOpcode, VecTy, false);
3454 IsPairwiseReduction = PairwiseRdxCost < SplittingRdxCost;
3455 int VecReduxCost = IsPairwiseReduction ? PairwiseRdxCost : SplittingRdxCost;
3457 int ScalarReduxCost =
3458 ReduxWidth * TTI->getArithmeticInstrCost(ReductionOpcode, VecTy);
3460 DEBUG(dbgs() << "SLP: Adding cost " << VecReduxCost - ScalarReduxCost
3461 << " for reduction that starts with " << *FirstReducedVal
3463 << (IsPairwiseReduction ? "pairwise" : "splitting")
3464 << " reduction)\n");
3466 return VecReduxCost - ScalarReduxCost;
3469 static Value *createBinOp(IRBuilder<> &Builder, unsigned Opcode, Value *L,
3470 Value *R, const Twine &Name = "") {
3471 if (Opcode == Instruction::FAdd)
3472 return Builder.CreateFAdd(L, R, Name);
3473 return Builder.CreateBinOp((Instruction::BinaryOps)Opcode, L, R, Name);
3476 /// \brief Emit a horizontal reduction of the vectorized value.
3477 Value *emitReduction(Value *VectorizedValue, IRBuilder<> &Builder) {
3478 assert(VectorizedValue && "Need to have a vectorized tree node");
3479 Instruction *ValToReduce = dyn_cast<Instruction>(VectorizedValue);
3480 assert(isPowerOf2_32(ReduxWidth) &&
3481 "We only handle power-of-two reductions for now");
3483 Value *TmpVec = ValToReduce;
3484 for (unsigned i = ReduxWidth / 2; i != 0; i >>= 1) {
3485 if (IsPairwiseReduction) {
3487 createRdxShuffleMask(ReduxWidth, i, true, true, Builder);
3489 createRdxShuffleMask(ReduxWidth, i, true, false, Builder);
3491 Value *LeftShuf = Builder.CreateShuffleVector(
3492 TmpVec, UndefValue::get(TmpVec->getType()), LeftMask, "rdx.shuf.l");
3493 Value *RightShuf = Builder.CreateShuffleVector(
3494 TmpVec, UndefValue::get(TmpVec->getType()), (RightMask),
3496 TmpVec = createBinOp(Builder, ReductionOpcode, LeftShuf, RightShuf,
3500 createRdxShuffleMask(ReduxWidth, i, false, false, Builder);
3501 Value *Shuf = Builder.CreateShuffleVector(
3502 TmpVec, UndefValue::get(TmpVec->getType()), UpperHalf, "rdx.shuf");
3503 TmpVec = createBinOp(Builder, ReductionOpcode, TmpVec, Shuf, "bin.rdx");
3507 // The result is in the first element of the vector.
3508 return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
3512 /// \brief Recognize construction of vectors like
3513 /// %ra = insertelement <4 x float> undef, float %s0, i32 0
3514 /// %rb = insertelement <4 x float> %ra, float %s1, i32 1
3515 /// %rc = insertelement <4 x float> %rb, float %s2, i32 2
3516 /// %rd = insertelement <4 x float> %rc, float %s3, i32 3
3518 /// Returns true if it matches
3520 static bool findBuildVector(InsertElementInst *FirstInsertElem,
3521 SmallVectorImpl<Value *> &BuildVector,
3522 SmallVectorImpl<Value *> &BuildVectorOpds) {
3523 if (!isa<UndefValue>(FirstInsertElem->getOperand(0)))
3526 InsertElementInst *IE = FirstInsertElem;
3528 BuildVector.push_back(IE);
3529 BuildVectorOpds.push_back(IE->getOperand(1));
3531 if (IE->use_empty())
3534 InsertElementInst *NextUse = dyn_cast<InsertElementInst>(IE->user_back());
3538 // If this isn't the final use, make sure the next insertelement is the only
3539 // use. It's OK if the final constructed vector is used multiple times
3540 if (!IE->hasOneUse())
3549 static bool PhiTypeSorterFunc(Value *V, Value *V2) {
3550 return V->getType() < V2->getType();
3553 bool SLPVectorizer::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
3554 bool Changed = false;
3555 SmallVector<Value *, 4> Incoming;
3556 SmallSet<Value *, 16> VisitedInstrs;
3558 bool HaveVectorizedPhiNodes = true;
3559 while (HaveVectorizedPhiNodes) {
3560 HaveVectorizedPhiNodes = false;
3562 // Collect the incoming values from the PHIs.
3564 for (BasicBlock::iterator instr = BB->begin(), ie = BB->end(); instr != ie;
3566 PHINode *P = dyn_cast<PHINode>(instr);
3570 if (!VisitedInstrs.count(P))
3571 Incoming.push_back(P);
3575 std::stable_sort(Incoming.begin(), Incoming.end(), PhiTypeSorterFunc);
3577 // Try to vectorize elements base on their type.
3578 for (SmallVector<Value *, 4>::iterator IncIt = Incoming.begin(),
3582 // Look for the next elements with the same type.
3583 SmallVector<Value *, 4>::iterator SameTypeIt = IncIt;
3584 while (SameTypeIt != E &&
3585 (*SameTypeIt)->getType() == (*IncIt)->getType()) {
3586 VisitedInstrs.insert(*SameTypeIt);
3590 // Try to vectorize them.
3591 unsigned NumElts = (SameTypeIt - IncIt);
3592 DEBUG(errs() << "SLP: Trying to vectorize starting at PHIs (" << NumElts << ")\n");
3593 if (NumElts > 1 && tryToVectorizeList(makeArrayRef(IncIt, NumElts), R)) {
3594 // Success start over because instructions might have been changed.
3595 HaveVectorizedPhiNodes = true;
3600 // Start over at the next instruction of a different type (or the end).
3605 VisitedInstrs.clear();
3607 for (BasicBlock::iterator it = BB->begin(), e = BB->end(); it != e; it++) {
3608 // We may go through BB multiple times so skip the one we have checked.
3609 if (!VisitedInstrs.insert(it))
3612 if (isa<DbgInfoIntrinsic>(it))
3615 // Try to vectorize reductions that use PHINodes.
3616 if (PHINode *P = dyn_cast<PHINode>(it)) {
3617 // Check that the PHI is a reduction PHI.
3618 if (P->getNumIncomingValues() != 2)
3621 (P->getIncomingBlock(0) == BB
3622 ? (P->getIncomingValue(0))
3623 : (P->getIncomingBlock(1) == BB ? P->getIncomingValue(1)
3625 // Check if this is a Binary Operator.
3626 BinaryOperator *BI = dyn_cast_or_null<BinaryOperator>(Rdx);
3630 // Try to match and vectorize a horizontal reduction.
3631 HorizontalReduction HorRdx;
3632 if (ShouldVectorizeHor &&
3633 HorRdx.matchAssociativeReduction(P, BI, DL) &&
3634 HorRdx.tryToReduce(R, TTI)) {
3641 Value *Inst = BI->getOperand(0);
3643 Inst = BI->getOperand(1);
3645 if (tryToVectorize(dyn_cast<BinaryOperator>(Inst), R)) {
3646 // We would like to start over since some instructions are deleted
3647 // and the iterator may become invalid value.
3657 // Try to vectorize horizontal reductions feeding into a store.
3658 if (ShouldStartVectorizeHorAtStore)
3659 if (StoreInst *SI = dyn_cast<StoreInst>(it))
3660 if (BinaryOperator *BinOp =
3661 dyn_cast<BinaryOperator>(SI->getValueOperand())) {
3662 HorizontalReduction HorRdx;
3663 if (((HorRdx.matchAssociativeReduction(nullptr, BinOp, DL) &&
3664 HorRdx.tryToReduce(R, TTI)) ||
3665 tryToVectorize(BinOp, R))) {
3673 // Try to vectorize trees that start at compare instructions.
3674 if (CmpInst *CI = dyn_cast<CmpInst>(it)) {
3675 if (tryToVectorizePair(CI->getOperand(0), CI->getOperand(1), R)) {
3677 // We would like to start over since some instructions are deleted
3678 // and the iterator may become invalid value.
3684 for (int i = 0; i < 2; ++i) {
3685 if (BinaryOperator *BI = dyn_cast<BinaryOperator>(CI->getOperand(i))) {
3686 if (tryToVectorizePair(BI->getOperand(0), BI->getOperand(1), R)) {
3688 // We would like to start over since some instructions are deleted
3689 // and the iterator may become invalid value.
3698 // Try to vectorize trees that start at insertelement instructions.
3699 if (InsertElementInst *FirstInsertElem = dyn_cast<InsertElementInst>(it)) {
3700 SmallVector<Value *, 16> BuildVector;
3701 SmallVector<Value *, 16> BuildVectorOpds;
3702 if (!findBuildVector(FirstInsertElem, BuildVector, BuildVectorOpds))
3705 // Vectorize starting with the build vector operands ignoring the
3706 // BuildVector instructions for the purpose of scheduling and user
3708 if (tryToVectorizeList(BuildVectorOpds, R, BuildVector)) {
3721 bool SLPVectorizer::vectorizeStoreChains(BoUpSLP &R) {
3722 bool Changed = false;
3723 // Attempt to sort and vectorize each of the store-groups.
3724 for (StoreListMap::iterator it = StoreRefs.begin(), e = StoreRefs.end();
3726 if (it->second.size() < 2)
3729 DEBUG(dbgs() << "SLP: Analyzing a store chain of length "
3730 << it->second.size() << ".\n");
3732 // Process the stores in chunks of 16.
3733 for (unsigned CI = 0, CE = it->second.size(); CI < CE; CI+=16) {
3734 unsigned Len = std::min<unsigned>(CE - CI, 16);
3735 Changed |= vectorizeStores(makeArrayRef(&it->second[CI], Len),
3736 -SLPCostThreshold, R);
3742 } // end anonymous namespace
3744 char SLPVectorizer::ID = 0;
3745 static const char lv_name[] = "SLP Vectorizer";
3746 INITIALIZE_PASS_BEGIN(SLPVectorizer, SV_NAME, lv_name, false, false)
3747 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
3748 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
3749 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
3750 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
3751 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
3754 Pass *createSLPVectorizerPass() { return new SLPVectorizer(); }