11f58dda4SAyal Zaks //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===//
21f58dda4SAyal Zaks //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61f58dda4SAyal Zaks //
71f58dda4SAyal Zaks //===----------------------------------------------------------------------===//
81f58dda4SAyal Zaks ///
91f58dda4SAyal Zaks /// \file
101f58dda4SAyal Zaks /// This is the LLVM vectorization plan. It represents a candidate for
111f58dda4SAyal Zaks /// vectorization, allowing to plan and optimize how to vectorize a given loop
121f58dda4SAyal Zaks /// before generating LLVM-IR.
131f58dda4SAyal Zaks /// The vectorizer uses vectorization plans to estimate the costs of potential
141f58dda4SAyal Zaks /// candidates and if profitable to execute the desired plan, generating vector
151f58dda4SAyal Zaks /// LLVM-IR code.
161f58dda4SAyal Zaks ///
171f58dda4SAyal Zaks //===----------------------------------------------------------------------===//
181f58dda4SAyal Zaks 
191f58dda4SAyal Zaks #include "VPlan.h"
202a34ac86SDiego Caballero #include "VPlanDominatorTree.h"
216cadde7fSEugene Zelenko #include "llvm/ADT/DepthFirstIterator.h"
221f58dda4SAyal Zaks #include "llvm/ADT/PostOrderIterator.h"
236cadde7fSEugene Zelenko #include "llvm/ADT/SmallVector.h"
246cadde7fSEugene Zelenko #include "llvm/ADT/Twine.h"
251f58dda4SAyal Zaks #include "llvm/Analysis/LoopInfo.h"
261f58dda4SAyal Zaks #include "llvm/IR/BasicBlock.h"
276cadde7fSEugene Zelenko #include "llvm/IR/CFG.h"
286cadde7fSEugene Zelenko #include "llvm/IR/InstrTypes.h"
296cadde7fSEugene Zelenko #include "llvm/IR/Instruction.h"
306cadde7fSEugene Zelenko #include "llvm/IR/Instructions.h"
316cadde7fSEugene Zelenko #include "llvm/IR/Type.h"
326cadde7fSEugene Zelenko #include "llvm/IR/Value.h"
336cadde7fSEugene Zelenko #include "llvm/Support/Casting.h"
344c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h"
356cadde7fSEugene Zelenko #include "llvm/Support/Debug.h"
366cadde7fSEugene Zelenko #include "llvm/Support/ErrorHandling.h"
372a34ac86SDiego Caballero #include "llvm/Support/GenericDomTreeConstruction.h"
381f58dda4SAyal Zaks #include "llvm/Support/GraphWriter.h"
396cadde7fSEugene Zelenko #include "llvm/Support/raw_ostream.h"
401f58dda4SAyal Zaks #include "llvm/Transforms/Utils/BasicBlockUtils.h"
416cadde7fSEugene Zelenko #include <cassert>
426cadde7fSEugene Zelenko #include <iterator>
436cadde7fSEugene Zelenko #include <string>
446cadde7fSEugene Zelenko #include <vector>
451f58dda4SAyal Zaks 
461f58dda4SAyal Zaks using namespace llvm;
47ea7f3035SHideki Saito extern cl::opt<bool> EnableVPlanNativePath;
481f58dda4SAyal Zaks 
491f58dda4SAyal Zaks #define DEBUG_TYPE "vplan"
501f58dda4SAyal Zaks 
518b9d1f3cSGil Rapaport raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
5240e7bfc4SFlorian Hahn   const VPInstruction *Instr = dyn_cast<VPInstruction>(&V);
5340e7bfc4SFlorian Hahn   VPSlotTracker SlotTracker(
5440e7bfc4SFlorian Hahn       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
5540e7bfc4SFlorian Hahn   V.print(OS, SlotTracker);
568b9d1f3cSGil Rapaport   return OS;
578b9d1f3cSGil Rapaport }
588b9d1f3cSGil Rapaport 
5940e7bfc4SFlorian Hahn void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const {
6040e7bfc4SFlorian Hahn   if (const VPInstruction *Instr = dyn_cast<VPInstruction>(this))
6140e7bfc4SFlorian Hahn     Instr->print(OS, SlotTracker);
6240e7bfc4SFlorian Hahn   else
6340e7bfc4SFlorian Hahn     printAsOperand(OS, SlotTracker);
6440e7bfc4SFlorian Hahn }
6540e7bfc4SFlorian Hahn 
6605afa555SFlorian Hahn // Get the top-most entry block of \p Start. This is the entry block of the
6705afa555SFlorian Hahn // containing VPlan. This function is templated to support both const and non-const blocks
6805afa555SFlorian Hahn template <typename T> static T *getPlanEntry(T *Start) {
6905afa555SFlorian Hahn   T *Next = Start;
7005afa555SFlorian Hahn   T *Current = Start;
7105afa555SFlorian Hahn   while ((Next = Next->getParent()))
7205afa555SFlorian Hahn     Current = Next;
7305afa555SFlorian Hahn 
7405afa555SFlorian Hahn   SmallSetVector<T *, 8> WorkList;
7505afa555SFlorian Hahn   WorkList.insert(Current);
7605afa555SFlorian Hahn 
7705afa555SFlorian Hahn   for (unsigned i = 0; i < WorkList.size(); i++) {
7805afa555SFlorian Hahn     T *Current = WorkList[i];
7905afa555SFlorian Hahn     if (Current->getNumPredecessors() == 0)
8005afa555SFlorian Hahn       return Current;
8105afa555SFlorian Hahn     auto &Predecessors = Current->getPredecessors();
8205afa555SFlorian Hahn     WorkList.insert(Predecessors.begin(), Predecessors.end());
8305afa555SFlorian Hahn   }
8405afa555SFlorian Hahn 
8505afa555SFlorian Hahn   llvm_unreachable("VPlan without any entry node without predecessors");
8605afa555SFlorian Hahn }
8705afa555SFlorian Hahn 
8805afa555SFlorian Hahn VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; }
8905afa555SFlorian Hahn 
9005afa555SFlorian Hahn const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; }
9105afa555SFlorian Hahn 
921f58dda4SAyal Zaks /// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
931f58dda4SAyal Zaks const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const {
941f58dda4SAyal Zaks   const VPBlockBase *Block = this;
951f58dda4SAyal Zaks   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
961f58dda4SAyal Zaks     Block = Region->getEntry();
971f58dda4SAyal Zaks   return cast<VPBasicBlock>(Block);
981f58dda4SAyal Zaks }
991f58dda4SAyal Zaks 
1001f58dda4SAyal Zaks VPBasicBlock *VPBlockBase::getEntryBasicBlock() {
1011f58dda4SAyal Zaks   VPBlockBase *Block = this;
1021f58dda4SAyal Zaks   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1031f58dda4SAyal Zaks     Block = Region->getEntry();
1041f58dda4SAyal Zaks   return cast<VPBasicBlock>(Block);
1051f58dda4SAyal Zaks }
1061f58dda4SAyal Zaks 
10705afa555SFlorian Hahn void VPBlockBase::setPlan(VPlan *ParentPlan) {
10805afa555SFlorian Hahn   assert(ParentPlan->getEntry() == this &&
10905afa555SFlorian Hahn          "Can only set plan on its entry block.");
11005afa555SFlorian Hahn   Plan = ParentPlan;
11105afa555SFlorian Hahn }
11205afa555SFlorian Hahn 
1131f58dda4SAyal Zaks /// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
1141f58dda4SAyal Zaks const VPBasicBlock *VPBlockBase::getExitBasicBlock() const {
1151f58dda4SAyal Zaks   const VPBlockBase *Block = this;
1161f58dda4SAyal Zaks   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1171f58dda4SAyal Zaks     Block = Region->getExit();
1181f58dda4SAyal Zaks   return cast<VPBasicBlock>(Block);
1191f58dda4SAyal Zaks }
1201f58dda4SAyal Zaks 
1211f58dda4SAyal Zaks VPBasicBlock *VPBlockBase::getExitBasicBlock() {
1221f58dda4SAyal Zaks   VPBlockBase *Block = this;
1231f58dda4SAyal Zaks   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1241f58dda4SAyal Zaks     Block = Region->getExit();
1251f58dda4SAyal Zaks   return cast<VPBasicBlock>(Block);
1261f58dda4SAyal Zaks }
1271f58dda4SAyal Zaks 
1281f58dda4SAyal Zaks VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() {
1291f58dda4SAyal Zaks   if (!Successors.empty() || !Parent)
1301f58dda4SAyal Zaks     return this;
1311f58dda4SAyal Zaks   assert(Parent->getExit() == this &&
1321f58dda4SAyal Zaks          "Block w/o successors not the exit of its parent.");
1331f58dda4SAyal Zaks   return Parent->getEnclosingBlockWithSuccessors();
1341f58dda4SAyal Zaks }
1351f58dda4SAyal Zaks 
1361f58dda4SAyal Zaks VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
1371f58dda4SAyal Zaks   if (!Predecessors.empty() || !Parent)
1381f58dda4SAyal Zaks     return this;
1391f58dda4SAyal Zaks   assert(Parent->getEntry() == this &&
1401f58dda4SAyal Zaks          "Block w/o predecessors not the entry of its parent.");
1411f58dda4SAyal Zaks   return Parent->getEnclosingBlockWithPredecessors();
1421f58dda4SAyal Zaks }
1431f58dda4SAyal Zaks 
1441f58dda4SAyal Zaks void VPBlockBase::deleteCFG(VPBlockBase *Entry) {
1451f58dda4SAyal Zaks   SmallVector<VPBlockBase *, 8> Blocks;
1461f58dda4SAyal Zaks   for (VPBlockBase *Block : depth_first(Entry))
1471f58dda4SAyal Zaks     Blocks.push_back(Block);
1481f58dda4SAyal Zaks 
1491f58dda4SAyal Zaks   for (VPBlockBase *Block : Blocks)
1501f58dda4SAyal Zaks     delete Block;
1511f58dda4SAyal Zaks }
1521f58dda4SAyal Zaks 
1531f58dda4SAyal Zaks BasicBlock *
1541f58dda4SAyal Zaks VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
1551f58dda4SAyal Zaks   // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
1561f58dda4SAyal Zaks   // Pred stands for Predessor. Prev stands for Previous - last visited/created.
1571f58dda4SAyal Zaks   BasicBlock *PrevBB = CFG.PrevBB;
1581f58dda4SAyal Zaks   BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(),
1591f58dda4SAyal Zaks                                          PrevBB->getParent(), CFG.LastBB);
160d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n');
1611f58dda4SAyal Zaks 
1621f58dda4SAyal Zaks   // Hook up the new basic block to its predecessors.
1631f58dda4SAyal Zaks   for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) {
1641f58dda4SAyal Zaks     VPBasicBlock *PredVPBB = PredVPBlock->getExitBasicBlock();
1651f58dda4SAyal Zaks     auto &PredVPSuccessors = PredVPBB->getSuccessors();
1661f58dda4SAyal Zaks     BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
167ea7f3035SHideki Saito 
168ea7f3035SHideki Saito     // In outer loop vectorization scenario, the predecessor BBlock may not yet
169ea7f3035SHideki Saito     // be visited(backedge). Mark the VPBasicBlock for fixup at the end of
170ea7f3035SHideki Saito     // vectorization. We do not encounter this case in inner loop vectorization
171ea7f3035SHideki Saito     // as we start out by building a loop skeleton with the vector loop header
172ea7f3035SHideki Saito     // and latch blocks. As a result, we never enter this function for the
173ea7f3035SHideki Saito     // header block in the non VPlan-native path.
174ea7f3035SHideki Saito     if (!PredBB) {
175ea7f3035SHideki Saito       assert(EnableVPlanNativePath &&
176ea7f3035SHideki Saito              "Unexpected null predecessor in non VPlan-native path");
177ea7f3035SHideki Saito       CFG.VPBBsToFix.push_back(PredVPBB);
178ea7f3035SHideki Saito       continue;
179ea7f3035SHideki Saito     }
180ea7f3035SHideki Saito 
1811f58dda4SAyal Zaks     assert(PredBB && "Predecessor basic-block not found building successor.");
1821f58dda4SAyal Zaks     auto *PredBBTerminator = PredBB->getTerminator();
183d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
1841f58dda4SAyal Zaks     if (isa<UnreachableInst>(PredBBTerminator)) {
1851f58dda4SAyal Zaks       assert(PredVPSuccessors.size() == 1 &&
1861f58dda4SAyal Zaks              "Predecessor ending w/o branch must have single successor.");
1871f58dda4SAyal Zaks       PredBBTerminator->eraseFromParent();
1881f58dda4SAyal Zaks       BranchInst::Create(NewBB, PredBB);
1891f58dda4SAyal Zaks     } else {
1901f58dda4SAyal Zaks       assert(PredVPSuccessors.size() == 2 &&
1911f58dda4SAyal Zaks              "Predecessor ending with branch must have two successors.");
1921f58dda4SAyal Zaks       unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
1931f58dda4SAyal Zaks       assert(!PredBBTerminator->getSuccessor(idx) &&
1941f58dda4SAyal Zaks              "Trying to reset an existing successor block.");
1951f58dda4SAyal Zaks       PredBBTerminator->setSuccessor(idx, NewBB);
1961f58dda4SAyal Zaks     }
1971f58dda4SAyal Zaks   }
1981f58dda4SAyal Zaks   return NewBB;
1991f58dda4SAyal Zaks }
2001f58dda4SAyal Zaks 
2011f58dda4SAyal Zaks void VPBasicBlock::execute(VPTransformState *State) {
2021f58dda4SAyal Zaks   bool Replica = State->Instance &&
2031f58dda4SAyal Zaks                  !(State->Instance->Part == 0 && State->Instance->Lane == 0);
2041f58dda4SAyal Zaks   VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB;
2051f58dda4SAyal Zaks   VPBlockBase *SingleHPred = nullptr;
2061f58dda4SAyal Zaks   BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible.
2071f58dda4SAyal Zaks 
2081f58dda4SAyal Zaks   // 1. Create an IR basic block, or reuse the last one if possible.
2091f58dda4SAyal Zaks   // The last IR basic block is reused, as an optimization, in three cases:
2101f58dda4SAyal Zaks   // A. the first VPBB reuses the loop header BB - when PrevVPBB is null;
2111f58dda4SAyal Zaks   // B. when the current VPBB has a single (hierarchical) predecessor which
2121f58dda4SAyal Zaks   //    is PrevVPBB and the latter has a single (hierarchical) successor; and
2131f58dda4SAyal Zaks   // C. when the current VPBB is an entry of a region replica - where PrevVPBB
2141f58dda4SAyal Zaks   //    is the exit of this region from a previous instance, or the predecessor
2151f58dda4SAyal Zaks   //    of this region.
2161f58dda4SAyal Zaks   if (PrevVPBB && /* A */
2171f58dda4SAyal Zaks       !((SingleHPred = getSingleHierarchicalPredecessor()) &&
2181f58dda4SAyal Zaks         SingleHPred->getExitBasicBlock() == PrevVPBB &&
2191f58dda4SAyal Zaks         PrevVPBB->getSingleHierarchicalSuccessor()) && /* B */
2201f58dda4SAyal Zaks       !(Replica && getPredecessors().empty())) {       /* C */
2211f58dda4SAyal Zaks     NewBB = createEmptyBasicBlock(State->CFG);
2221f58dda4SAyal Zaks     State->Builder.SetInsertPoint(NewBB);
2231f58dda4SAyal Zaks     // Temporarily terminate with unreachable until CFG is rewired.
2241f58dda4SAyal Zaks     UnreachableInst *Terminator = State->Builder.CreateUnreachable();
2251f58dda4SAyal Zaks     State->Builder.SetInsertPoint(Terminator);
2261f58dda4SAyal Zaks     // Register NewBB in its loop. In innermost loops its the same for all BB's.
2271f58dda4SAyal Zaks     Loop *L = State->LI->getLoopFor(State->CFG.LastBB);
2281f58dda4SAyal Zaks     L->addBasicBlockToLoop(NewBB, *State->LI);
2291f58dda4SAyal Zaks     State->CFG.PrevBB = NewBB;
2301f58dda4SAyal Zaks   }
2311f58dda4SAyal Zaks 
2321f58dda4SAyal Zaks   // 2. Fill the IR basic block with IR instructions.
233d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName()
2341f58dda4SAyal Zaks                     << " in BB:" << NewBB->getName() << '\n');
2351f58dda4SAyal Zaks 
2361f58dda4SAyal Zaks   State->CFG.VPBB2IRBB[this] = NewBB;
2371f58dda4SAyal Zaks   State->CFG.PrevVPBB = this;
2381f58dda4SAyal Zaks 
2391f58dda4SAyal Zaks   for (VPRecipeBase &Recipe : Recipes)
2401f58dda4SAyal Zaks     Recipe.execute(*State);
2411f58dda4SAyal Zaks 
242ea7f3035SHideki Saito   VPValue *CBV;
243ea7f3035SHideki Saito   if (EnableVPlanNativePath && (CBV = getCondBit())) {
244ea7f3035SHideki Saito     Value *IRCBV = CBV->getUnderlyingValue();
245ea7f3035SHideki Saito     assert(IRCBV && "Unexpected null underlying value for condition bit");
246ea7f3035SHideki Saito 
247ea7f3035SHideki Saito     // Condition bit value in a VPBasicBlock is used as the branch selector. In
248ea7f3035SHideki Saito     // the VPlan-native path case, since all branches are uniform we generate a
249ea7f3035SHideki Saito     // branch instruction using the condition value from vector lane 0 and dummy
250ea7f3035SHideki Saito     // successors. The successors are fixed later when the successor blocks are
251ea7f3035SHideki Saito     // visited.
252ea7f3035SHideki Saito     Value *NewCond = State->Callback.getOrCreateVectorValues(IRCBV, 0);
253ea7f3035SHideki Saito     NewCond = State->Builder.CreateExtractElement(NewCond,
254ea7f3035SHideki Saito                                                   State->Builder.getInt32(0));
255ea7f3035SHideki Saito 
256ea7f3035SHideki Saito     // Replace the temporary unreachable terminator with the new conditional
257ea7f3035SHideki Saito     // branch.
258ea7f3035SHideki Saito     auto *CurrentTerminator = NewBB->getTerminator();
259ea7f3035SHideki Saito     assert(isa<UnreachableInst>(CurrentTerminator) &&
260ea7f3035SHideki Saito            "Expected to replace unreachable terminator with conditional "
261ea7f3035SHideki Saito            "branch.");
262ea7f3035SHideki Saito     auto *CondBr = BranchInst::Create(NewBB, nullptr, NewCond);
263ea7f3035SHideki Saito     CondBr->setSuccessor(0, nullptr);
264ea7f3035SHideki Saito     ReplaceInstWithInst(CurrentTerminator, CondBr);
265ea7f3035SHideki Saito   }
266ea7f3035SHideki Saito 
267d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB);
2681f58dda4SAyal Zaks }
2691f58dda4SAyal Zaks 
2701f58dda4SAyal Zaks void VPRegionBlock::execute(VPTransformState *State) {
2711f58dda4SAyal Zaks   ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry);
2721f58dda4SAyal Zaks 
2731f58dda4SAyal Zaks   if (!isReplicator()) {
2741f58dda4SAyal Zaks     // Visit the VPBlocks connected to "this", starting from it.
2751f58dda4SAyal Zaks     for (VPBlockBase *Block : RPOT) {
276ea7f3035SHideki Saito       if (EnableVPlanNativePath) {
277ea7f3035SHideki Saito         // The inner loop vectorization path does not represent loop preheader
278ea7f3035SHideki Saito         // and exit blocks as part of the VPlan. In the VPlan-native path, skip
279ea7f3035SHideki Saito         // vectorizing loop preheader block. In future, we may replace this
280ea7f3035SHideki Saito         // check with the check for loop preheader.
281ea7f3035SHideki Saito         if (Block->getNumPredecessors() == 0)
282ea7f3035SHideki Saito           continue;
283ea7f3035SHideki Saito 
284ea7f3035SHideki Saito         // Skip vectorizing loop exit block. In future, we may replace this
285ea7f3035SHideki Saito         // check with the check for loop exit.
286ea7f3035SHideki Saito         if (Block->getNumSuccessors() == 0)
287ea7f3035SHideki Saito           continue;
288ea7f3035SHideki Saito       }
289ea7f3035SHideki Saito 
290d34e60caSNicola Zaghen       LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
2911f58dda4SAyal Zaks       Block->execute(State);
2921f58dda4SAyal Zaks     }
2931f58dda4SAyal Zaks     return;
2941f58dda4SAyal Zaks   }
2951f58dda4SAyal Zaks 
2961f58dda4SAyal Zaks   assert(!State->Instance && "Replicating a Region with non-null instance.");
2971f58dda4SAyal Zaks 
2981f58dda4SAyal Zaks   // Enter replicating mode.
2991f58dda4SAyal Zaks   State->Instance = {0, 0};
3001f58dda4SAyal Zaks 
3011f58dda4SAyal Zaks   for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) {
3021f58dda4SAyal Zaks     State->Instance->Part = Part;
3031f58dda4SAyal Zaks     for (unsigned Lane = 0, VF = State->VF; Lane < VF; ++Lane) {
3041f58dda4SAyal Zaks       State->Instance->Lane = Lane;
3051f58dda4SAyal Zaks       // Visit the VPBlocks connected to \p this, starting from it.
3061f58dda4SAyal Zaks       for (VPBlockBase *Block : RPOT) {
307d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
3081f58dda4SAyal Zaks         Block->execute(State);
3091f58dda4SAyal Zaks       }
3101f58dda4SAyal Zaks     }
3111f58dda4SAyal Zaks   }
3121f58dda4SAyal Zaks 
3131f58dda4SAyal Zaks   // Exit replicating mode.
3141f58dda4SAyal Zaks   State->Instance.reset();
3151f58dda4SAyal Zaks }
3161f58dda4SAyal Zaks 
3177591e4e9SFlorian Hahn void VPRecipeBase::insertBefore(VPRecipeBase *InsertPos) {
3187f152543SGil Rapaport   assert(!Parent && "Recipe already in some VPBasicBlock");
3197f152543SGil Rapaport   assert(InsertPos->getParent() &&
3207f152543SGil Rapaport          "Insertion position not in any VPBasicBlock");
3213bcff366SFlorian Hahn   Parent = InsertPos->getParent();
3223bcff366SFlorian Hahn   Parent->getRecipeList().insert(InsertPos->getIterator(), this);
3237591e4e9SFlorian Hahn }
3247591e4e9SFlorian Hahn 
3257f152543SGil Rapaport void VPRecipeBase::insertAfter(VPRecipeBase *InsertPos) {
3267f152543SGil Rapaport   assert(!Parent && "Recipe already in some VPBasicBlock");
3277f152543SGil Rapaport   assert(InsertPos->getParent() &&
3287f152543SGil Rapaport          "Insertion position not in any VPBasicBlock");
3297f152543SGil Rapaport   Parent = InsertPos->getParent();
3307f152543SGil Rapaport   Parent->getRecipeList().insertAfter(InsertPos->getIterator(), this);
3317f152543SGil Rapaport }
3327f152543SGil Rapaport 
3337f152543SGil Rapaport void VPRecipeBase::removeFromParent() {
3347f152543SGil Rapaport   assert(getParent() && "Recipe not in any VPBasicBlock");
3357f152543SGil Rapaport   getParent()->getRecipeList().remove(getIterator());
3367f152543SGil Rapaport   Parent = nullptr;
3377f152543SGil Rapaport }
3387f152543SGil Rapaport 
33963cbcf98SFlorian Hahn iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
3407f152543SGil Rapaport   assert(getParent() && "Recipe not in any VPBasicBlock");
34163cbcf98SFlorian Hahn   return getParent()->getRecipeList().erase(getIterator());
34263cbcf98SFlorian Hahn }
34363cbcf98SFlorian Hahn 
34439d4c9fdSFlorian Hahn void VPRecipeBase::moveAfter(VPRecipeBase *InsertPos) {
3457f152543SGil Rapaport   removeFromParent();
3467f152543SGil Rapaport   insertAfter(InsertPos);
34739d4c9fdSFlorian Hahn }
34839d4c9fdSFlorian Hahn 
3498b9d1f3cSGil Rapaport void VPInstruction::generateInstruction(VPTransformState &State,
3508b9d1f3cSGil Rapaport                                         unsigned Part) {
3518b9d1f3cSGil Rapaport   IRBuilder<> &Builder = State.Builder;
3528b9d1f3cSGil Rapaport 
3538b9d1f3cSGil Rapaport   if (Instruction::isBinaryOp(getOpcode())) {
3548b9d1f3cSGil Rapaport     Value *A = State.get(getOperand(0), Part);
3558b9d1f3cSGil Rapaport     Value *B = State.get(getOperand(1), Part);
3568b9d1f3cSGil Rapaport     Value *V = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(), A, B);
3578b9d1f3cSGil Rapaport     State.set(this, V, Part);
3588b9d1f3cSGil Rapaport     return;
3598b9d1f3cSGil Rapaport   }
3608b9d1f3cSGil Rapaport 
3618b9d1f3cSGil Rapaport   switch (getOpcode()) {
3628b9d1f3cSGil Rapaport   case VPInstruction::Not: {
3638b9d1f3cSGil Rapaport     Value *A = State.get(getOperand(0), Part);
3648b9d1f3cSGil Rapaport     Value *V = Builder.CreateNot(A);
3658b9d1f3cSGil Rapaport     State.set(this, V, Part);
3668b9d1f3cSGil Rapaport     break;
3678b9d1f3cSGil Rapaport   }
368b0b5312eSAyal Zaks   case VPInstruction::ICmpULE: {
369b0b5312eSAyal Zaks     Value *IV = State.get(getOperand(0), Part);
370b0b5312eSAyal Zaks     Value *TC = State.get(getOperand(1), Part);
371b0b5312eSAyal Zaks     Value *V = Builder.CreateICmpULE(IV, TC);
372b0b5312eSAyal Zaks     State.set(this, V, Part);
373b0b5312eSAyal Zaks     break;
374b0b5312eSAyal Zaks   }
375d15df0edSAyal Zaks   case Instruction::Select: {
376d15df0edSAyal Zaks     Value *Cond = State.get(getOperand(0), Part);
377d15df0edSAyal Zaks     Value *Op1 = State.get(getOperand(1), Part);
378d15df0edSAyal Zaks     Value *Op2 = State.get(getOperand(2), Part);
379d15df0edSAyal Zaks     Value *V = Builder.CreateSelect(Cond, Op1, Op2);
380d15df0edSAyal Zaks     State.set(this, V, Part);
381d15df0edSAyal Zaks     break;
382d15df0edSAyal Zaks   }
3838b9d1f3cSGil Rapaport   default:
3848b9d1f3cSGil Rapaport     llvm_unreachable("Unsupported opcode for instruction");
3858b9d1f3cSGil Rapaport   }
3868b9d1f3cSGil Rapaport }
3878b9d1f3cSGil Rapaport 
3888b9d1f3cSGil Rapaport void VPInstruction::execute(VPTransformState &State) {
3898b9d1f3cSGil Rapaport   assert(!State.Instance && "VPInstruction executing an Instance");
3908b9d1f3cSGil Rapaport   for (unsigned Part = 0; Part < State.UF; ++Part)
3918b9d1f3cSGil Rapaport     generateInstruction(State, Part);
3928b9d1f3cSGil Rapaport }
3938b9d1f3cSGil Rapaport 
39440e7bfc4SFlorian Hahn void VPInstruction::print(raw_ostream &O, const Twine &Indent,
39540e7bfc4SFlorian Hahn                           VPSlotTracker &SlotTracker) const {
3964c8285c7SFlorian Hahn   O << "\"EMIT ";
39740e7bfc4SFlorian Hahn   print(O, SlotTracker);
3988b9d1f3cSGil Rapaport }
3998b9d1f3cSGil Rapaport 
4008b9d1f3cSGil Rapaport void VPInstruction::print(raw_ostream &O) const {
40140e7bfc4SFlorian Hahn   VPSlotTracker SlotTracker(getParent()->getPlan());
40240e7bfc4SFlorian Hahn   print(O, SlotTracker);
40340e7bfc4SFlorian Hahn }
40440e7bfc4SFlorian Hahn 
40540e7bfc4SFlorian Hahn void VPInstruction::print(raw_ostream &O, VPSlotTracker &SlotTracker) const {
406e6a74803SFlorian Hahn   if (hasResult()) {
40740e7bfc4SFlorian Hahn     printAsOperand(O, SlotTracker);
4088b9d1f3cSGil Rapaport     O << " = ";
409e6a74803SFlorian Hahn   }
4108b9d1f3cSGil Rapaport 
4118b9d1f3cSGil Rapaport   switch (getOpcode()) {
4128b9d1f3cSGil Rapaport   case VPInstruction::Not:
4138b9d1f3cSGil Rapaport     O << "not";
4148b9d1f3cSGil Rapaport     break;
415b0b5312eSAyal Zaks   case VPInstruction::ICmpULE:
416b0b5312eSAyal Zaks     O << "icmp ule";
417b0b5312eSAyal Zaks     break;
41809e516c5SFlorian Hahn   case VPInstruction::SLPLoad:
41909e516c5SFlorian Hahn     O << "combined load";
42009e516c5SFlorian Hahn     break;
42109e516c5SFlorian Hahn   case VPInstruction::SLPStore:
42209e516c5SFlorian Hahn     O << "combined store";
42309e516c5SFlorian Hahn     break;
4248b9d1f3cSGil Rapaport   default:
4258b9d1f3cSGil Rapaport     O << Instruction::getOpcodeName(getOpcode());
4268b9d1f3cSGil Rapaport   }
4278b9d1f3cSGil Rapaport 
4288b9d1f3cSGil Rapaport   for (const VPValue *Operand : operands()) {
4298b9d1f3cSGil Rapaport     O << " ";
43040e7bfc4SFlorian Hahn     Operand->printAsOperand(O, SlotTracker);
4318b9d1f3cSGil Rapaport   }
4328b9d1f3cSGil Rapaport }
4338b9d1f3cSGil Rapaport 
4341f58dda4SAyal Zaks /// Generate the code inside the body of the vectorized loop. Assumes a single
4351f58dda4SAyal Zaks /// LoopVectorBody basic-block was created for this. Introduce additional
4361f58dda4SAyal Zaks /// basic-blocks as needed, and fill them all.
4371f58dda4SAyal Zaks void VPlan::execute(VPTransformState *State) {
438b0b5312eSAyal Zaks   // -1. Check if the backedge taken count is needed, and if so build it.
439b0b5312eSAyal Zaks   if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) {
440b0b5312eSAyal Zaks     Value *TC = State->TripCount;
441b0b5312eSAyal Zaks     IRBuilder<> Builder(State->CFG.PrevBB->getTerminator());
442b0b5312eSAyal Zaks     auto *TCMO = Builder.CreateSub(TC, ConstantInt::get(TC->getType(), 1),
443b0b5312eSAyal Zaks                                    "trip.count.minus.1");
444*13bf6039SAnh Tuyen Tran     auto VF = State->VF;
445*13bf6039SAnh Tuyen Tran     Value *VTCMO =
446*13bf6039SAnh Tuyen Tran         VF == 1 ? TCMO : Builder.CreateVectorSplat(VF, TCMO, "broadcast");
447a3c964a2SAyal Zaks     for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part)
448a3c964a2SAyal Zaks       State->set(BackedgeTakenCount, VTCMO, Part);
449b0b5312eSAyal Zaks   }
450b0b5312eSAyal Zaks 
4518b9d1f3cSGil Rapaport   // 0. Set the reverse mapping from VPValues to Values for code generation.
4528b9d1f3cSGil Rapaport   for (auto &Entry : Value2VPValue)
4538b9d1f3cSGil Rapaport     State->VPValue2Value[Entry.second] = Entry.first;
4548b9d1f3cSGil Rapaport 
4551f58dda4SAyal Zaks   BasicBlock *VectorPreHeaderBB = State->CFG.PrevBB;
4561f58dda4SAyal Zaks   BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor();
4571f58dda4SAyal Zaks   assert(VectorHeaderBB && "Loop preheader does not have a single successor.");
4581f58dda4SAyal Zaks 
4591f58dda4SAyal Zaks   // 1. Make room to generate basic-blocks inside loop body if needed.
460cced3eccSSimon Pilgrim   BasicBlock *VectorLatchBB = VectorHeaderBB->splitBasicBlock(
4611f58dda4SAyal Zaks       VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch");
4621f58dda4SAyal Zaks   Loop *L = State->LI->getLoopFor(VectorHeaderBB);
4631f58dda4SAyal Zaks   L->addBasicBlockToLoop(VectorLatchBB, *State->LI);
4641f58dda4SAyal Zaks   // Remove the edge between Header and Latch to allow other connections.
4651f58dda4SAyal Zaks   // Temporarily terminate with unreachable until CFG is rewired.
4661f58dda4SAyal Zaks   // Note: this asserts the generated code's assumption that
4671f58dda4SAyal Zaks   // getFirstInsertionPt() can be dereferenced into an Instruction.
4681f58dda4SAyal Zaks   VectorHeaderBB->getTerminator()->eraseFromParent();
4691f58dda4SAyal Zaks   State->Builder.SetInsertPoint(VectorHeaderBB);
4701f58dda4SAyal Zaks   UnreachableInst *Terminator = State->Builder.CreateUnreachable();
4711f58dda4SAyal Zaks   State->Builder.SetInsertPoint(Terminator);
4721f58dda4SAyal Zaks 
4731f58dda4SAyal Zaks   // 2. Generate code in loop body.
4741f58dda4SAyal Zaks   State->CFG.PrevVPBB = nullptr;
4751f58dda4SAyal Zaks   State->CFG.PrevBB = VectorHeaderBB;
4761f58dda4SAyal Zaks   State->CFG.LastBB = VectorLatchBB;
4771f58dda4SAyal Zaks 
4781f58dda4SAyal Zaks   for (VPBlockBase *Block : depth_first(Entry))
4791f58dda4SAyal Zaks     Block->execute(State);
4801f58dda4SAyal Zaks 
481ea7f3035SHideki Saito   // Setup branch terminator successors for VPBBs in VPBBsToFix based on
482ea7f3035SHideki Saito   // VPBB's successors.
483ea7f3035SHideki Saito   for (auto VPBB : State->CFG.VPBBsToFix) {
484ea7f3035SHideki Saito     assert(EnableVPlanNativePath &&
485ea7f3035SHideki Saito            "Unexpected VPBBsToFix in non VPlan-native path");
486ea7f3035SHideki Saito     BasicBlock *BB = State->CFG.VPBB2IRBB[VPBB];
487ea7f3035SHideki Saito     assert(BB && "Unexpected null basic block for VPBB");
488ea7f3035SHideki Saito 
489ea7f3035SHideki Saito     unsigned Idx = 0;
490ea7f3035SHideki Saito     auto *BBTerminator = BB->getTerminator();
491ea7f3035SHideki Saito 
492ea7f3035SHideki Saito     for (VPBlockBase *SuccVPBlock : VPBB->getHierarchicalSuccessors()) {
493ea7f3035SHideki Saito       VPBasicBlock *SuccVPBB = SuccVPBlock->getEntryBasicBlock();
494ea7f3035SHideki Saito       BBTerminator->setSuccessor(Idx, State->CFG.VPBB2IRBB[SuccVPBB]);
495ea7f3035SHideki Saito       ++Idx;
496ea7f3035SHideki Saito     }
497ea7f3035SHideki Saito   }
498ea7f3035SHideki Saito 
4991f58dda4SAyal Zaks   // 3. Merge the temporary latch created with the last basic-block filled.
5001f58dda4SAyal Zaks   BasicBlock *LastBB = State->CFG.PrevBB;
5011f58dda4SAyal Zaks   // Connect LastBB to VectorLatchBB to facilitate their merge.
502ea7f3035SHideki Saito   assert((EnableVPlanNativePath ||
503ea7f3035SHideki Saito           isa<UnreachableInst>(LastBB->getTerminator())) &&
504ea7f3035SHideki Saito          "Expected InnerLoop VPlan CFG to terminate with unreachable");
505ea7f3035SHideki Saito   assert((!EnableVPlanNativePath || isa<BranchInst>(LastBB->getTerminator())) &&
506ea7f3035SHideki Saito          "Expected VPlan CFG to terminate with branch in NativePath");
5071f58dda4SAyal Zaks   LastBB->getTerminator()->eraseFromParent();
5081f58dda4SAyal Zaks   BranchInst::Create(VectorLatchBB, LastBB);
5091f58dda4SAyal Zaks 
5101f58dda4SAyal Zaks   // Merge LastBB with Latch.
5111f58dda4SAyal Zaks   bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI);
5121f58dda4SAyal Zaks   (void)Merged;
5131f58dda4SAyal Zaks   assert(Merged && "Could not merge last basic block with latch.");
5141f58dda4SAyal Zaks   VectorLatchBB = LastBB;
5151f58dda4SAyal Zaks 
516ea7f3035SHideki Saito   // We do not attempt to preserve DT for outer loop vectorization currently.
517ea7f3035SHideki Saito   if (!EnableVPlanNativePath)
518948e7452SEvgeniy Brevnov     updateDominatorTree(State->DT, VectorPreHeaderBB, VectorLatchBB,
519948e7452SEvgeniy Brevnov                         L->getExitBlock());
5201f58dda4SAyal Zaks }
5211f58dda4SAyal Zaks 
522e9c68422SFlorian Hahn #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
523e9c68422SFlorian Hahn LLVM_DUMP_METHOD
524e9c68422SFlorian Hahn void VPlan::dump() const { dbgs() << *this << '\n'; }
525e9c68422SFlorian Hahn #endif
526e9c68422SFlorian Hahn 
5271f58dda4SAyal Zaks void VPlan::updateDominatorTree(DominatorTree *DT, BasicBlock *LoopPreHeaderBB,
528948e7452SEvgeniy Brevnov                                 BasicBlock *LoopLatchBB,
529948e7452SEvgeniy Brevnov                                 BasicBlock *LoopExitBB) {
5301f58dda4SAyal Zaks   BasicBlock *LoopHeaderBB = LoopPreHeaderBB->getSingleSuccessor();
5311f58dda4SAyal Zaks   assert(LoopHeaderBB && "Loop preheader does not have a single successor.");
5321f58dda4SAyal Zaks   // The vector body may be more than a single basic-block by this point.
5331f58dda4SAyal Zaks   // Update the dominator tree information inside the vector body by propagating
5341f58dda4SAyal Zaks   // it from header to latch, expecting only triangular control-flow, if any.
5351f58dda4SAyal Zaks   BasicBlock *PostDomSucc = nullptr;
5361f58dda4SAyal Zaks   for (auto *BB = LoopHeaderBB; BB != LoopLatchBB; BB = PostDomSucc) {
5371f58dda4SAyal Zaks     // Get the list of successors of this block.
5381f58dda4SAyal Zaks     std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
5391f58dda4SAyal Zaks     assert(Succs.size() <= 2 &&
5401f58dda4SAyal Zaks            "Basic block in vector loop has more than 2 successors.");
5411f58dda4SAyal Zaks     PostDomSucc = Succs[0];
5421f58dda4SAyal Zaks     if (Succs.size() == 1) {
5431f58dda4SAyal Zaks       assert(PostDomSucc->getSinglePredecessor() &&
5441f58dda4SAyal Zaks              "PostDom successor has more than one predecessor.");
5451f58dda4SAyal Zaks       DT->addNewBlock(PostDomSucc, BB);
5461f58dda4SAyal Zaks       continue;
5471f58dda4SAyal Zaks     }
5481f58dda4SAyal Zaks     BasicBlock *InterimSucc = Succs[1];
5491f58dda4SAyal Zaks     if (PostDomSucc->getSingleSuccessor() == InterimSucc) {
5501f58dda4SAyal Zaks       PostDomSucc = Succs[1];
5511f58dda4SAyal Zaks       InterimSucc = Succs[0];
5521f58dda4SAyal Zaks     }
5531f58dda4SAyal Zaks     assert(InterimSucc->getSingleSuccessor() == PostDomSucc &&
5541f58dda4SAyal Zaks            "One successor of a basic block does not lead to the other.");
5551f58dda4SAyal Zaks     assert(InterimSucc->getSinglePredecessor() &&
5561f58dda4SAyal Zaks            "Interim successor has more than one predecessor.");
5574de31bbaSVedant Kumar     assert(PostDomSucc->hasNPredecessors(2) &&
5581f58dda4SAyal Zaks            "PostDom successor has more than two predecessors.");
5591f58dda4SAyal Zaks     DT->addNewBlock(InterimSucc, BB);
5601f58dda4SAyal Zaks     DT->addNewBlock(PostDomSucc, BB);
5611f58dda4SAyal Zaks   }
562948e7452SEvgeniy Brevnov   // Latch block is a new dominator for the loop exit.
563948e7452SEvgeniy Brevnov   DT->changeImmediateDominator(LoopExitBB, LoopLatchBB);
564948e7452SEvgeniy Brevnov   assert(DT->verify(DominatorTree::VerificationLevel::Fast));
5651f58dda4SAyal Zaks }
5661f58dda4SAyal Zaks 
5671f58dda4SAyal Zaks const Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
5681f58dda4SAyal Zaks   return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") +
5691f58dda4SAyal Zaks          Twine(getOrCreateBID(Block));
5701f58dda4SAyal Zaks }
5711f58dda4SAyal Zaks 
5721f58dda4SAyal Zaks const Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) {
5731f58dda4SAyal Zaks   const std::string &Name = Block->getName();
5741f58dda4SAyal Zaks   if (!Name.empty())
5751f58dda4SAyal Zaks     return Name;
5761f58dda4SAyal Zaks   return "VPB" + Twine(getOrCreateBID(Block));
5771f58dda4SAyal Zaks }
5781f58dda4SAyal Zaks 
5791f58dda4SAyal Zaks void VPlanPrinter::dump() {
5801f58dda4SAyal Zaks   Depth = 1;
5811f58dda4SAyal Zaks   bumpIndent(0);
5821f58dda4SAyal Zaks   OS << "digraph VPlan {\n";
5831f58dda4SAyal Zaks   OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
5841f58dda4SAyal Zaks   if (!Plan.getName().empty())
5851f58dda4SAyal Zaks     OS << "\\n" << DOT::EscapeString(Plan.getName());
58640e7bfc4SFlorian Hahn   if (Plan.BackedgeTakenCount) {
587fd2c15e6SFlorian Hahn     OS << ", where:\\n";
58840e7bfc4SFlorian Hahn     Plan.BackedgeTakenCount->print(OS, SlotTracker);
58940e7bfc4SFlorian Hahn     OS << " := BackedgeTakenCount";
59040e7bfc4SFlorian Hahn   }
5911f58dda4SAyal Zaks   OS << "\"]\n";
5921f58dda4SAyal Zaks   OS << "node [shape=rect, fontname=Courier, fontsize=30]\n";
5931f58dda4SAyal Zaks   OS << "edge [fontname=Courier, fontsize=30]\n";
5941f58dda4SAyal Zaks   OS << "compound=true\n";
5951f58dda4SAyal Zaks 
596e9c68422SFlorian Hahn   for (const VPBlockBase *Block : depth_first(Plan.getEntry()))
5971f58dda4SAyal Zaks     dumpBlock(Block);
5981f58dda4SAyal Zaks 
5991f58dda4SAyal Zaks   OS << "}\n";
6001f58dda4SAyal Zaks }
6011f58dda4SAyal Zaks 
6021f58dda4SAyal Zaks void VPlanPrinter::dumpBlock(const VPBlockBase *Block) {
6031f58dda4SAyal Zaks   if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block))
6041f58dda4SAyal Zaks     dumpBasicBlock(BasicBlock);
6051f58dda4SAyal Zaks   else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
6061f58dda4SAyal Zaks     dumpRegion(Region);
6071f58dda4SAyal Zaks   else
6081f58dda4SAyal Zaks     llvm_unreachable("Unsupported kind of VPBlock.");
6091f58dda4SAyal Zaks }
6101f58dda4SAyal Zaks 
6111f58dda4SAyal Zaks void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To,
6121f58dda4SAyal Zaks                             bool Hidden, const Twine &Label) {
6131f58dda4SAyal Zaks   // Due to "dot" we print an edge between two regions as an edge between the
6141f58dda4SAyal Zaks   // exit basic block and the entry basic of the respective regions.
6151f58dda4SAyal Zaks   const VPBlockBase *Tail = From->getExitBasicBlock();
6161f58dda4SAyal Zaks   const VPBlockBase *Head = To->getEntryBasicBlock();
6171f58dda4SAyal Zaks   OS << Indent << getUID(Tail) << " -> " << getUID(Head);
6181f58dda4SAyal Zaks   OS << " [ label=\"" << Label << '\"';
6191f58dda4SAyal Zaks   if (Tail != From)
6201f58dda4SAyal Zaks     OS << " ltail=" << getUID(From);
6211f58dda4SAyal Zaks   if (Head != To)
6221f58dda4SAyal Zaks     OS << " lhead=" << getUID(To);
6231f58dda4SAyal Zaks   if (Hidden)
6241f58dda4SAyal Zaks     OS << "; splines=none";
6251f58dda4SAyal Zaks   OS << "]\n";
6261f58dda4SAyal Zaks }
6271f58dda4SAyal Zaks 
6281f58dda4SAyal Zaks void VPlanPrinter::dumpEdges(const VPBlockBase *Block) {
6291f58dda4SAyal Zaks   auto &Successors = Block->getSuccessors();
6301f58dda4SAyal Zaks   if (Successors.size() == 1)
6311f58dda4SAyal Zaks     drawEdge(Block, Successors.front(), false, "");
6321f58dda4SAyal Zaks   else if (Successors.size() == 2) {
6331f58dda4SAyal Zaks     drawEdge(Block, Successors.front(), false, "T");
6341f58dda4SAyal Zaks     drawEdge(Block, Successors.back(), false, "F");
6351f58dda4SAyal Zaks   } else {
6361f58dda4SAyal Zaks     unsigned SuccessorNumber = 0;
6371f58dda4SAyal Zaks     for (auto *Successor : Successors)
6381f58dda4SAyal Zaks       drawEdge(Block, Successor, false, Twine(SuccessorNumber++));
6391f58dda4SAyal Zaks   }
6401f58dda4SAyal Zaks }
6411f58dda4SAyal Zaks 
6421f58dda4SAyal Zaks void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) {
6431f58dda4SAyal Zaks   OS << Indent << getUID(BasicBlock) << " [label =\n";
6441f58dda4SAyal Zaks   bumpIndent(1);
6451f58dda4SAyal Zaks   OS << Indent << "\"" << DOT::EscapeString(BasicBlock->getName()) << ":\\n\"";
6461f58dda4SAyal Zaks   bumpIndent(1);
6474e4ecae0SHideki Saito 
6484e4ecae0SHideki Saito   // Dump the block predicate.
6494e4ecae0SHideki Saito   const VPValue *Pred = BasicBlock->getPredicate();
6504e4ecae0SHideki Saito   if (Pred) {
6514e4ecae0SHideki Saito     OS << " +\n" << Indent << " \"BlockPredicate: ";
6524e4ecae0SHideki Saito     if (const VPInstruction *PredI = dyn_cast<VPInstruction>(Pred)) {
65340e7bfc4SFlorian Hahn       PredI->printAsOperand(OS, SlotTracker);
6544e4ecae0SHideki Saito       OS << " (" << DOT::EscapeString(PredI->getParent()->getName())
6554e4ecae0SHideki Saito          << ")\\l\"";
6564e4ecae0SHideki Saito     } else
65740e7bfc4SFlorian Hahn       Pred->printAsOperand(OS, SlotTracker);
6584e4ecae0SHideki Saito   }
6594e4ecae0SHideki Saito 
6604c8285c7SFlorian Hahn   for (const VPRecipeBase &Recipe : *BasicBlock) {
6614c8285c7SFlorian Hahn     OS << " +\n" << Indent;
66240e7bfc4SFlorian Hahn     Recipe.print(OS, Indent, SlotTracker);
6634c8285c7SFlorian Hahn     OS << "\\l\"";
6644c8285c7SFlorian Hahn   }
665d0953014SDiego Caballero 
666d0953014SDiego Caballero   // Dump the condition bit.
667d0953014SDiego Caballero   const VPValue *CBV = BasicBlock->getCondBit();
668d0953014SDiego Caballero   if (CBV) {
669d0953014SDiego Caballero     OS << " +\n" << Indent << " \"CondBit: ";
670d0953014SDiego Caballero     if (const VPInstruction *CBI = dyn_cast<VPInstruction>(CBV)) {
67140e7bfc4SFlorian Hahn       CBI->printAsOperand(OS, SlotTracker);
672d0953014SDiego Caballero       OS << " (" << DOT::EscapeString(CBI->getParent()->getName()) << ")\\l\"";
673d8e7ca4aSRenato Golin     } else {
67440e7bfc4SFlorian Hahn       CBV->printAsOperand(OS, SlotTracker);
675d8e7ca4aSRenato Golin       OS << "\"";
676d8e7ca4aSRenato Golin     }
677d0953014SDiego Caballero   }
678d0953014SDiego Caballero 
6791f58dda4SAyal Zaks   bumpIndent(-2);
6801f58dda4SAyal Zaks   OS << "\n" << Indent << "]\n";
6811f58dda4SAyal Zaks   dumpEdges(BasicBlock);
6821f58dda4SAyal Zaks }
6831f58dda4SAyal Zaks 
6841f58dda4SAyal Zaks void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
6851f58dda4SAyal Zaks   OS << Indent << "subgraph " << getUID(Region) << " {\n";
6861f58dda4SAyal Zaks   bumpIndent(1);
6871f58dda4SAyal Zaks   OS << Indent << "fontname=Courier\n"
6881f58dda4SAyal Zaks      << Indent << "label=\""
6891f58dda4SAyal Zaks      << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ")
6901f58dda4SAyal Zaks      << DOT::EscapeString(Region->getName()) << "\"\n";
6911f58dda4SAyal Zaks   // Dump the blocks of the region.
6921f58dda4SAyal Zaks   assert(Region->getEntry() && "Region contains no inner blocks.");
6931f58dda4SAyal Zaks   for (const VPBlockBase *Block : depth_first(Region->getEntry()))
6941f58dda4SAyal Zaks     dumpBlock(Block);
6951f58dda4SAyal Zaks   bumpIndent(-1);
6961f58dda4SAyal Zaks   OS << Indent << "}\n";
6971f58dda4SAyal Zaks   dumpEdges(Region);
6981f58dda4SAyal Zaks }
6991f58dda4SAyal Zaks 
7001f58dda4SAyal Zaks void VPlanPrinter::printAsIngredient(raw_ostream &O, Value *V) {
7011f58dda4SAyal Zaks   std::string IngredientString;
7021f58dda4SAyal Zaks   raw_string_ostream RSO(IngredientString);
7031f58dda4SAyal Zaks   if (auto *Inst = dyn_cast<Instruction>(V)) {
7041f58dda4SAyal Zaks     if (!Inst->getType()->isVoidTy()) {
7051f58dda4SAyal Zaks       Inst->printAsOperand(RSO, false);
7061f58dda4SAyal Zaks       RSO << " = ";
7071f58dda4SAyal Zaks     }
7081f58dda4SAyal Zaks     RSO << Inst->getOpcodeName() << " ";
7091f58dda4SAyal Zaks     unsigned E = Inst->getNumOperands();
7101f58dda4SAyal Zaks     if (E > 0) {
7111f58dda4SAyal Zaks       Inst->getOperand(0)->printAsOperand(RSO, false);
7121f58dda4SAyal Zaks       for (unsigned I = 1; I < E; ++I)
7131f58dda4SAyal Zaks         Inst->getOperand(I)->printAsOperand(RSO << ", ", false);
7141f58dda4SAyal Zaks     }
7151f58dda4SAyal Zaks   } else // !Inst
7161f58dda4SAyal Zaks     V->printAsOperand(RSO, false);
7171f58dda4SAyal Zaks   RSO.flush();
7181f58dda4SAyal Zaks   O << DOT::EscapeString(IngredientString);
7191f58dda4SAyal Zaks }
7207333aa9fSHal Finkel 
72190be3c24SFlorian Hahn void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent,
72290be3c24SFlorian Hahn                               VPSlotTracker &SlotTracker) const {
7234c8285c7SFlorian Hahn   O << "\"WIDEN-CALL " << VPlanIngredient(&Ingredient);
72490be3c24SFlorian Hahn }
72590be3c24SFlorian Hahn 
72618138e02SFlorian Hahn void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent,
72718138e02SFlorian Hahn                                 VPSlotTracker &SlotTracker) const {
7284c8285c7SFlorian Hahn   O << "\"WIDEN-SELECT" << VPlanIngredient(&Ingredient)
7294c8285c7SFlorian Hahn     << (InvariantCond ? " (condition is loop invariant)" : "");
73018138e02SFlorian Hahn }
73118138e02SFlorian Hahn 
73240e7bfc4SFlorian Hahn void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
73340e7bfc4SFlorian Hahn                           VPSlotTracker &SlotTracker) const {
7344c8285c7SFlorian Hahn   O << "\"WIDEN\\l\"";
7354c8285c7SFlorian Hahn   O << "\"  " << VPlanIngredient(&Ingredient);
7367333aa9fSHal Finkel }
7377333aa9fSHal Finkel 
73840e7bfc4SFlorian Hahn void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O, const Twine &Indent,
73940e7bfc4SFlorian Hahn                                           VPSlotTracker &SlotTracker) const {
7404c8285c7SFlorian Hahn   O << "\"WIDEN-INDUCTION";
7417333aa9fSHal Finkel   if (Trunc) {
7427333aa9fSHal Finkel     O << "\\l\"";
7437333aa9fSHal Finkel     O << " +\n" << Indent << "\"  " << VPlanIngredient(IV) << "\\l\"";
7444c8285c7SFlorian Hahn     O << " +\n" << Indent << "\"  " << VPlanIngredient(Trunc);
7457333aa9fSHal Finkel   } else
7464c8285c7SFlorian Hahn     O << " " << VPlanIngredient(IV);
7477333aa9fSHal Finkel }
7487333aa9fSHal Finkel 
74940e7bfc4SFlorian Hahn void VPWidenGEPRecipe::print(raw_ostream &O, const Twine &Indent,
75040e7bfc4SFlorian Hahn                              VPSlotTracker &SlotTracker) const {
7514c8285c7SFlorian Hahn   O << "\"WIDEN-GEP ";
75239ccc099SGil Rapaport   O << (IsPtrLoopInvariant ? "Inv" : "Var");
75339ccc099SGil Rapaport   size_t IndicesNumber = IsIndexLoopInvariant.size();
75439ccc099SGil Rapaport   for (size_t I = 0; I < IndicesNumber; ++I)
75539ccc099SGil Rapaport     O << "[" << (IsIndexLoopInvariant[I] ? "Inv" : "Var") << "]";
75639ccc099SGil Rapaport   O << "\\l\"";
7574c8285c7SFlorian Hahn   O << " +\n" << Indent << "\"  " << VPlanIngredient(GEP);
75839ccc099SGil Rapaport }
75939ccc099SGil Rapaport 
76040e7bfc4SFlorian Hahn void VPWidenPHIRecipe::print(raw_ostream &O, const Twine &Indent,
76140e7bfc4SFlorian Hahn                              VPSlotTracker &SlotTracker) const {
7624c8285c7SFlorian Hahn   O << "\"WIDEN-PHI " << VPlanIngredient(Phi);
7637333aa9fSHal Finkel }
7647333aa9fSHal Finkel 
76540e7bfc4SFlorian Hahn void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent,
76640e7bfc4SFlorian Hahn                           VPSlotTracker &SlotTracker) const {
7674c8285c7SFlorian Hahn   O << "\"BLEND ";
7687333aa9fSHal Finkel   Phi->printAsOperand(O, false);
7697333aa9fSHal Finkel   O << " =";
770e2a18678SGil Rapaport   if (getNumIncomingValues() == 1) {
7717333aa9fSHal Finkel     // Not a User of any mask: not really blending, this is a
7727333aa9fSHal Finkel     // single-predecessor phi.
7737333aa9fSHal Finkel     O << " ";
774e2a18678SGil Rapaport     getIncomingValue(0)->printAsOperand(O, SlotTracker);
7757333aa9fSHal Finkel   } else {
776e2a18678SGil Rapaport     for (unsigned I = 0, E = getNumIncomingValues(); I < E; ++I) {
7777333aa9fSHal Finkel       O << " ";
778e2a18678SGil Rapaport       getIncomingValue(I)->printAsOperand(O, SlotTracker);
7797333aa9fSHal Finkel       O << "/";
780e2a18678SGil Rapaport       getMask(I)->printAsOperand(O, SlotTracker);
7817333aa9fSHal Finkel     }
7827333aa9fSHal Finkel   }
7837333aa9fSHal Finkel }
7847333aa9fSHal Finkel 
78540e7bfc4SFlorian Hahn void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent,
78640e7bfc4SFlorian Hahn                               VPSlotTracker &SlotTracker) const {
7874c8285c7SFlorian Hahn   O << "\"" << (IsUniform ? "CLONE " : "REPLICATE ")
7887333aa9fSHal Finkel     << VPlanIngredient(Ingredient);
7897333aa9fSHal Finkel   if (AlsoPack)
7907333aa9fSHal Finkel     O << " (S->V)";
7917333aa9fSHal Finkel }
7927333aa9fSHal Finkel 
79340e7bfc4SFlorian Hahn void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent,
79440e7bfc4SFlorian Hahn                                 VPSlotTracker &SlotTracker) const {
7954c8285c7SFlorian Hahn   O << "\"PHI-PREDICATED-INSTRUCTION " << VPlanIngredient(PredInst);
7967333aa9fSHal Finkel }
7977333aa9fSHal Finkel 
79840e7bfc4SFlorian Hahn void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent,
79940e7bfc4SFlorian Hahn                                            VPSlotTracker &SlotTracker) const {
8004c8285c7SFlorian Hahn   O << "\"WIDEN " << VPlanIngredient(&Instr);
8018647a72cSGil Rapaport   O << ", ";
80240e7bfc4SFlorian Hahn   getAddr()->printAsOperand(O, SlotTracker);
803d62bf161SGil Rapaport   VPValue *Mask = getMask();
804d62bf161SGil Rapaport   if (Mask) {
8057333aa9fSHal Finkel     O << ", ";
80640e7bfc4SFlorian Hahn     Mask->printAsOperand(O, SlotTracker);
8077333aa9fSHal Finkel   }
8087333aa9fSHal Finkel }
8092a34ac86SDiego Caballero 
81016784892SAyal Zaks void VPWidenCanonicalIVRecipe::execute(VPTransformState &State) {
81116784892SAyal Zaks   Value *CanonicalIV = State.CanonicalIV;
81216784892SAyal Zaks   Type *STy = CanonicalIV->getType();
81316784892SAyal Zaks   IRBuilder<> Builder(State.CFG.PrevBB->getTerminator());
814*13bf6039SAnh Tuyen Tran   auto VF = State.VF;
815*13bf6039SAnh Tuyen Tran   Value *VStart = VF == 1
816*13bf6039SAnh Tuyen Tran                       ? CanonicalIV
817*13bf6039SAnh Tuyen Tran                       : Builder.CreateVectorSplat(VF, CanonicalIV, "broadcast");
81816784892SAyal Zaks   for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) {
81916784892SAyal Zaks     SmallVector<Constant *, 8> Indices;
820*13bf6039SAnh Tuyen Tran     for (unsigned Lane = 0; Lane < VF; ++Lane)
82116784892SAyal Zaks       Indices.push_back(ConstantInt::get(STy, Part * VF + Lane));
822*13bf6039SAnh Tuyen Tran     // If VF == 1, there is only one iteration in the loop above, thus the
823*13bf6039SAnh Tuyen Tran     // element pushed back into Indices is ConstantInt::get(STy, Part)
824*13bf6039SAnh Tuyen Tran     Constant *VStep = VF == 1 ? Indices.back() : ConstantVector::get(Indices);
82516784892SAyal Zaks     // Add the consecutive indices to the vector value.
82616784892SAyal Zaks     Value *CanonicalVectorIV = Builder.CreateAdd(VStart, VStep, "vec.iv");
82716784892SAyal Zaks     State.set(getVPValue(), CanonicalVectorIV, Part);
82816784892SAyal Zaks   }
82916784892SAyal Zaks }
83016784892SAyal Zaks 
83116784892SAyal Zaks void VPWidenCanonicalIVRecipe::print(raw_ostream &O, const Twine &Indent,
83216784892SAyal Zaks                                      VPSlotTracker &SlotTracker) const {
8334c8285c7SFlorian Hahn   O << "\"EMIT ";
83416784892SAyal Zaks   getVPValue()->printAsOperand(O, SlotTracker);
8354c8285c7SFlorian Hahn   O << " = WIDEN-CANONICAL-INDUCTION";
83616784892SAyal Zaks }
83716784892SAyal Zaks 
8382a34ac86SDiego Caballero template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
839a4dc7feeSFlorian Hahn 
84009e516c5SFlorian Hahn void VPValue::replaceAllUsesWith(VPValue *New) {
84109e516c5SFlorian Hahn   for (VPUser *User : users())
84209e516c5SFlorian Hahn     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I)
84309e516c5SFlorian Hahn       if (User->getOperand(I) == this)
84409e516c5SFlorian Hahn         User->setOperand(I, New);
84509e516c5SFlorian Hahn }
84609e516c5SFlorian Hahn 
84740e7bfc4SFlorian Hahn void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {
848e6a74803SFlorian Hahn   if (const Value *UV = getUnderlyingValue()) {
849e6a74803SFlorian Hahn     OS << "ir<";
850e6a74803SFlorian Hahn     UV->printAsOperand(OS, false);
851e6a74803SFlorian Hahn     OS << ">";
852e6a74803SFlorian Hahn     return;
853e6a74803SFlorian Hahn   }
854e6a74803SFlorian Hahn 
85540e7bfc4SFlorian Hahn   unsigned Slot = Tracker.getSlot(this);
85640e7bfc4SFlorian Hahn   if (Slot == unsigned(-1))
85740e7bfc4SFlorian Hahn     OS << "<badref>";
85840e7bfc4SFlorian Hahn   else
859e6a74803SFlorian Hahn     OS << "vp<%" << Tracker.getSlot(this) << ">";
86040e7bfc4SFlorian Hahn }
86140e7bfc4SFlorian Hahn 
862a4dc7feeSFlorian Hahn void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region,
863a4dc7feeSFlorian Hahn                                           Old2NewTy &Old2New,
864a4dc7feeSFlorian Hahn                                           InterleavedAccessInfo &IAI) {
865a4dc7feeSFlorian Hahn   ReversePostOrderTraversal<VPBlockBase *> RPOT(Region->getEntry());
866a4dc7feeSFlorian Hahn   for (VPBlockBase *Base : RPOT) {
867a4dc7feeSFlorian Hahn     visitBlock(Base, Old2New, IAI);
868a4dc7feeSFlorian Hahn   }
869a4dc7feeSFlorian Hahn }
870a4dc7feeSFlorian Hahn 
871a4dc7feeSFlorian Hahn void VPInterleavedAccessInfo::visitBlock(VPBlockBase *Block, Old2NewTy &Old2New,
872a4dc7feeSFlorian Hahn                                          InterleavedAccessInfo &IAI) {
873a4dc7feeSFlorian Hahn   if (VPBasicBlock *VPBB = dyn_cast<VPBasicBlock>(Block)) {
874a4dc7feeSFlorian Hahn     for (VPRecipeBase &VPI : *VPBB) {
875a4dc7feeSFlorian Hahn       assert(isa<VPInstruction>(&VPI) && "Can only handle VPInstructions");
876a4dc7feeSFlorian Hahn       auto *VPInst = cast<VPInstruction>(&VPI);
877a4dc7feeSFlorian Hahn       auto *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
878a4dc7feeSFlorian Hahn       auto *IG = IAI.getInterleaveGroup(Inst);
879a4dc7feeSFlorian Hahn       if (!IG)
880a4dc7feeSFlorian Hahn         continue;
881a4dc7feeSFlorian Hahn 
882a4dc7feeSFlorian Hahn       auto NewIGIter = Old2New.find(IG);
883a4dc7feeSFlorian Hahn       if (NewIGIter == Old2New.end())
884a4dc7feeSFlorian Hahn         Old2New[IG] = new InterleaveGroup<VPInstruction>(
88532851f8dSGuillaume Chatelet             IG->getFactor(), IG->isReverse(), IG->getAlign());
886a4dc7feeSFlorian Hahn 
887a4dc7feeSFlorian Hahn       if (Inst == IG->getInsertPos())
888a4dc7feeSFlorian Hahn         Old2New[IG]->setInsertPos(VPInst);
889a4dc7feeSFlorian Hahn 
890a4dc7feeSFlorian Hahn       InterleaveGroupMap[VPInst] = Old2New[IG];
891a4dc7feeSFlorian Hahn       InterleaveGroupMap[VPInst]->insertMember(
892a4dc7feeSFlorian Hahn           VPInst, IG->getIndex(Inst),
893837a1b84SGuillaume Chatelet           Align(IG->isReverse() ? (-1) * int(IG->getFactor())
894837a1b84SGuillaume Chatelet                                 : IG->getFactor()));
895a4dc7feeSFlorian Hahn     }
896a4dc7feeSFlorian Hahn   } else if (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
897a4dc7feeSFlorian Hahn     visitRegion(Region, Old2New, IAI);
898a4dc7feeSFlorian Hahn   else
899a4dc7feeSFlorian Hahn     llvm_unreachable("Unsupported kind of VPBlock.");
900a4dc7feeSFlorian Hahn }
901a4dc7feeSFlorian Hahn 
902a4dc7feeSFlorian Hahn VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan,
903a4dc7feeSFlorian Hahn                                                  InterleavedAccessInfo &IAI) {
904a4dc7feeSFlorian Hahn   Old2NewTy Old2New;
905a4dc7feeSFlorian Hahn   visitRegion(cast<VPRegionBlock>(Plan.getEntry()), Old2New, IAI);
906a4dc7feeSFlorian Hahn }
90740e7bfc4SFlorian Hahn 
90840e7bfc4SFlorian Hahn void VPSlotTracker::assignSlot(const VPValue *V) {
90940e7bfc4SFlorian Hahn   assert(Slots.find(V) == Slots.end() && "VPValue already has a slot!");
910e6a74803SFlorian Hahn   const Value *UV = V->getUnderlyingValue();
911e6a74803SFlorian Hahn   if (UV)
912e6a74803SFlorian Hahn     return;
913e6a74803SFlorian Hahn   const auto *VPI = dyn_cast<VPInstruction>(V);
914e6a74803SFlorian Hahn   if (VPI && !VPI->hasResult())
915e6a74803SFlorian Hahn     return;
916e6a74803SFlorian Hahn 
91740e7bfc4SFlorian Hahn   Slots[V] = NextSlot++;
91840e7bfc4SFlorian Hahn }
91940e7bfc4SFlorian Hahn 
92040e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPBlockBase *VPBB) {
92140e7bfc4SFlorian Hahn   if (auto *Region = dyn_cast<VPRegionBlock>(VPBB))
92240e7bfc4SFlorian Hahn     assignSlots(Region);
92340e7bfc4SFlorian Hahn   else
92440e7bfc4SFlorian Hahn     assignSlots(cast<VPBasicBlock>(VPBB));
92540e7bfc4SFlorian Hahn }
92640e7bfc4SFlorian Hahn 
92740e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPRegionBlock *Region) {
92840e7bfc4SFlorian Hahn   ReversePostOrderTraversal<const VPBlockBase *> RPOT(Region->getEntry());
92940e7bfc4SFlorian Hahn   for (const VPBlockBase *Block : RPOT)
93040e7bfc4SFlorian Hahn     assignSlots(Block);
93140e7bfc4SFlorian Hahn }
93240e7bfc4SFlorian Hahn 
93340e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) {
93440e7bfc4SFlorian Hahn   for (const VPRecipeBase &Recipe : *VPBB) {
93540e7bfc4SFlorian Hahn     if (const auto *VPI = dyn_cast<VPInstruction>(&Recipe))
93640e7bfc4SFlorian Hahn       assignSlot(VPI);
93716784892SAyal Zaks     else if (const auto *VPIV = dyn_cast<VPWidenCanonicalIVRecipe>(&Recipe))
93816784892SAyal Zaks       assignSlot(VPIV->getVPValue());
93940e7bfc4SFlorian Hahn   }
94040e7bfc4SFlorian Hahn }
94140e7bfc4SFlorian Hahn 
94240e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPlan &Plan) {
94340e7bfc4SFlorian Hahn 
94440e7bfc4SFlorian Hahn   for (const VPValue *V : Plan.VPExternalDefs)
94540e7bfc4SFlorian Hahn     assignSlot(V);
94640e7bfc4SFlorian Hahn 
94740e7bfc4SFlorian Hahn   for (auto &E : Plan.Value2VPValue)
94840e7bfc4SFlorian Hahn     if (!isa<VPInstruction>(E.second))
94940e7bfc4SFlorian Hahn       assignSlot(E.second);
95040e7bfc4SFlorian Hahn 
95140e7bfc4SFlorian Hahn   for (const VPValue *V : Plan.VPCBVs)
95240e7bfc4SFlorian Hahn     assignSlot(V);
95340e7bfc4SFlorian Hahn 
95440e7bfc4SFlorian Hahn   if (Plan.BackedgeTakenCount)
95540e7bfc4SFlorian Hahn     assignSlot(Plan.BackedgeTakenCount);
95640e7bfc4SFlorian Hahn 
95740e7bfc4SFlorian Hahn   ReversePostOrderTraversal<const VPBlockBase *> RPOT(Plan.getEntry());
95840e7bfc4SFlorian Hahn   for (const VPBlockBase *Block : RPOT)
95940e7bfc4SFlorian Hahn     assignSlots(Block);
96040e7bfc4SFlorian Hahn }
961