xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlan.cpp (revision 85fe5c93456776f313e25efaf49d3ae9e8703c86)
1 //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This is the LLVM vectorization plan. It represents a candidate for
11 /// vectorization, allowing to plan and optimize how to vectorize a given loop
12 /// before generating LLVM-IR.
13 /// The vectorizer uses vectorization plans to estimate the costs of potential
14 /// candidates and if profitable to execute the desired plan, generating vector
15 /// LLVM-IR code.
16 ///
17 //===----------------------------------------------------------------------===//
18 
19 #include "VPlan.h"
20 #include "VPlanDominatorTree.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/PostOrderIterator.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Analysis/IVDescriptors.h"
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/CFG.h"
30 #include "llvm/IR/InstrTypes.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/GenericDomTreeConstruction.h"
40 #include "llvm/Support/GraphWriter.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
43 #include <cassert>
44 #include <iterator>
45 #include <string>
46 #include <vector>
47 
48 using namespace llvm;
49 extern cl::opt<bool> EnableVPlanNativePath;
50 
51 #define DEBUG_TYPE "vplan"
52 
53 raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
54   const VPInstruction *Instr = dyn_cast<VPInstruction>(&V);
55   VPSlotTracker SlotTracker(
56       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
57   V.print(OS, SlotTracker);
58   return OS;
59 }
60 
61 VPValue::VPValue(const unsigned char SC, Value *UV, VPDef *Def)
62     : SubclassID(SC), UnderlyingVal(UV), Def(Def) {
63   if (Def)
64     Def->addDefinedValue(this);
65 }
66 
67 VPValue::~VPValue() {
68   assert(Users.empty() && "trying to delete a VPValue with remaining users");
69   if (Def)
70     Def->removeDefinedValue(this);
71 }
72 
73 void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const {
74   if (const VPRecipeBase *R = dyn_cast_or_null<VPRecipeBase>(Def))
75     R->print(OS, "", SlotTracker);
76   else
77     printAsOperand(OS, SlotTracker);
78 }
79 
80 void VPValue::dump() const {
81   const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this->Def);
82   VPSlotTracker SlotTracker(
83       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
84   print(dbgs(), SlotTracker);
85   dbgs() << "\n";
86 }
87 
88 void VPDef::dump() const {
89   const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this);
90   VPSlotTracker SlotTracker(
91       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
92   print(dbgs(), "", SlotTracker);
93   dbgs() << "\n";
94 }
95 
96 // Get the top-most entry block of \p Start. This is the entry block of the
97 // containing VPlan. This function is templated to support both const and non-const blocks
98 template <typename T> static T *getPlanEntry(T *Start) {
99   T *Next = Start;
100   T *Current = Start;
101   while ((Next = Next->getParent()))
102     Current = Next;
103 
104   SmallSetVector<T *, 8> WorkList;
105   WorkList.insert(Current);
106 
107   for (unsigned i = 0; i < WorkList.size(); i++) {
108     T *Current = WorkList[i];
109     if (Current->getNumPredecessors() == 0)
110       return Current;
111     auto &Predecessors = Current->getPredecessors();
112     WorkList.insert(Predecessors.begin(), Predecessors.end());
113   }
114 
115   llvm_unreachable("VPlan without any entry node without predecessors");
116 }
117 
118 VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; }
119 
120 const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; }
121 
122 /// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
123 const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const {
124   const VPBlockBase *Block = this;
125   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
126     Block = Region->getEntry();
127   return cast<VPBasicBlock>(Block);
128 }
129 
130 VPBasicBlock *VPBlockBase::getEntryBasicBlock() {
131   VPBlockBase *Block = this;
132   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
133     Block = Region->getEntry();
134   return cast<VPBasicBlock>(Block);
135 }
136 
137 void VPBlockBase::setPlan(VPlan *ParentPlan) {
138   assert(ParentPlan->getEntry() == this &&
139          "Can only set plan on its entry block.");
140   Plan = ParentPlan;
141 }
142 
143 /// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
144 const VPBasicBlock *VPBlockBase::getExitBasicBlock() const {
145   const VPBlockBase *Block = this;
146   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
147     Block = Region->getExit();
148   return cast<VPBasicBlock>(Block);
149 }
150 
151 VPBasicBlock *VPBlockBase::getExitBasicBlock() {
152   VPBlockBase *Block = this;
153   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
154     Block = Region->getExit();
155   return cast<VPBasicBlock>(Block);
156 }
157 
158 VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() {
159   if (!Successors.empty() || !Parent)
160     return this;
161   assert(Parent->getExit() == this &&
162          "Block w/o successors not the exit of its parent.");
163   return Parent->getEnclosingBlockWithSuccessors();
164 }
165 
166 VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
167   if (!Predecessors.empty() || !Parent)
168     return this;
169   assert(Parent->getEntry() == this &&
170          "Block w/o predecessors not the entry of its parent.");
171   return Parent->getEnclosingBlockWithPredecessors();
172 }
173 
174 void VPBlockBase::deleteCFG(VPBlockBase *Entry) {
175   SmallVector<VPBlockBase *, 8> Blocks(depth_first(Entry));
176 
177   for (VPBlockBase *Block : Blocks)
178     delete Block;
179 }
180 
181 VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() {
182   iterator It = begin();
183   while (It != end() && (isa<VPWidenPHIRecipe>(&*It) ||
184                          isa<VPWidenIntOrFpInductionRecipe>(&*It) ||
185                          isa<VPPredInstPHIRecipe>(&*It) ||
186                          isa<VPWidenCanonicalIVRecipe>(&*It)))
187     It++;
188   return It;
189 }
190 
191 Value *VPTransformState::get(VPValue *Def, const VPIteration &Instance) {
192   if (!Def->getDef())
193     return Def->getLiveInIRValue();
194 
195   if (hasScalarValue(Def, Instance))
196     return Data.PerPartScalars[Def][Instance.Part][Instance.Lane];
197 
198   if (hasVectorValue(Def, Instance.Part)) {
199     assert(Data.PerPartOutput.count(Def));
200     auto *VecPart = Data.PerPartOutput[Def][Instance.Part];
201     if (!VecPart->getType()->isVectorTy()) {
202       assert(Instance.Lane == 0 && "cannot get lane > 0 for scalar");
203       return VecPart;
204     }
205     // TODO: Cache created scalar values.
206     return Builder.CreateExtractElement(VecPart,
207                                         Builder.getInt32(Instance.Lane));
208   }
209   return Callback.getOrCreateScalarValue(VPValue2Value[Def], Instance);
210 }
211 
212 BasicBlock *
213 VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
214   // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
215   // Pred stands for Predessor. Prev stands for Previous - last visited/created.
216   BasicBlock *PrevBB = CFG.PrevBB;
217   BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(),
218                                          PrevBB->getParent(), CFG.LastBB);
219   LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n');
220 
221   // Hook up the new basic block to its predecessors.
222   for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) {
223     VPBasicBlock *PredVPBB = PredVPBlock->getExitBasicBlock();
224     auto &PredVPSuccessors = PredVPBB->getSuccessors();
225     BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
226 
227     // In outer loop vectorization scenario, the predecessor BBlock may not yet
228     // be visited(backedge). Mark the VPBasicBlock for fixup at the end of
229     // vectorization. We do not encounter this case in inner loop vectorization
230     // as we start out by building a loop skeleton with the vector loop header
231     // and latch blocks. As a result, we never enter this function for the
232     // header block in the non VPlan-native path.
233     if (!PredBB) {
234       assert(EnableVPlanNativePath &&
235              "Unexpected null predecessor in non VPlan-native path");
236       CFG.VPBBsToFix.push_back(PredVPBB);
237       continue;
238     }
239 
240     assert(PredBB && "Predecessor basic-block not found building successor.");
241     auto *PredBBTerminator = PredBB->getTerminator();
242     LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
243     if (isa<UnreachableInst>(PredBBTerminator)) {
244       assert(PredVPSuccessors.size() == 1 &&
245              "Predecessor ending w/o branch must have single successor.");
246       PredBBTerminator->eraseFromParent();
247       BranchInst::Create(NewBB, PredBB);
248     } else {
249       assert(PredVPSuccessors.size() == 2 &&
250              "Predecessor ending with branch must have two successors.");
251       unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
252       assert(!PredBBTerminator->getSuccessor(idx) &&
253              "Trying to reset an existing successor block.");
254       PredBBTerminator->setSuccessor(idx, NewBB);
255     }
256   }
257   return NewBB;
258 }
259 
260 void VPBasicBlock::execute(VPTransformState *State) {
261   bool Replica = State->Instance && !State->Instance->isFirstIteration();
262   VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB;
263   VPBlockBase *SingleHPred = nullptr;
264   BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible.
265 
266   // 1. Create an IR basic block, or reuse the last one if possible.
267   // The last IR basic block is reused, as an optimization, in three cases:
268   // A. the first VPBB reuses the loop header BB - when PrevVPBB is null;
269   // B. when the current VPBB has a single (hierarchical) predecessor which
270   //    is PrevVPBB and the latter has a single (hierarchical) successor; and
271   // C. when the current VPBB is an entry of a region replica - where PrevVPBB
272   //    is the exit of this region from a previous instance, or the predecessor
273   //    of this region.
274   if (PrevVPBB && /* A */
275       !((SingleHPred = getSingleHierarchicalPredecessor()) &&
276         SingleHPred->getExitBasicBlock() == PrevVPBB &&
277         PrevVPBB->getSingleHierarchicalSuccessor()) && /* B */
278       !(Replica && getPredecessors().empty())) {       /* C */
279     NewBB = createEmptyBasicBlock(State->CFG);
280     State->Builder.SetInsertPoint(NewBB);
281     // Temporarily terminate with unreachable until CFG is rewired.
282     UnreachableInst *Terminator = State->Builder.CreateUnreachable();
283     State->Builder.SetInsertPoint(Terminator);
284     // Register NewBB in its loop. In innermost loops its the same for all BB's.
285     Loop *L = State->LI->getLoopFor(State->CFG.LastBB);
286     L->addBasicBlockToLoop(NewBB, *State->LI);
287     State->CFG.PrevBB = NewBB;
288   }
289 
290   // 2. Fill the IR basic block with IR instructions.
291   LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName()
292                     << " in BB:" << NewBB->getName() << '\n');
293 
294   State->CFG.VPBB2IRBB[this] = NewBB;
295   State->CFG.PrevVPBB = this;
296 
297   for (VPRecipeBase &Recipe : Recipes)
298     Recipe.execute(*State);
299 
300   VPValue *CBV;
301   if (EnableVPlanNativePath && (CBV = getCondBit())) {
302     Value *IRCBV = CBV->getUnderlyingValue();
303     assert(IRCBV && "Unexpected null underlying value for condition bit");
304 
305     // Condition bit value in a VPBasicBlock is used as the branch selector. In
306     // the VPlan-native path case, since all branches are uniform we generate a
307     // branch instruction using the condition value from vector lane 0 and dummy
308     // successors. The successors are fixed later when the successor blocks are
309     // visited.
310     Value *NewCond = State->Callback.getOrCreateVectorValues(IRCBV, 0);
311     NewCond = State->Builder.CreateExtractElement(NewCond,
312                                                   State->Builder.getInt32(0));
313 
314     // Replace the temporary unreachable terminator with the new conditional
315     // branch.
316     auto *CurrentTerminator = NewBB->getTerminator();
317     assert(isa<UnreachableInst>(CurrentTerminator) &&
318            "Expected to replace unreachable terminator with conditional "
319            "branch.");
320     auto *CondBr = BranchInst::Create(NewBB, nullptr, NewCond);
321     CondBr->setSuccessor(0, nullptr);
322     ReplaceInstWithInst(CurrentTerminator, CondBr);
323   }
324 
325   LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB);
326 }
327 
328 void VPBasicBlock::dropAllReferences(VPValue *NewValue) {
329   for (VPRecipeBase &R : Recipes) {
330     for (auto *Def : R.definedValues())
331       Def->replaceAllUsesWith(NewValue);
332 
333     for (unsigned I = 0, E = R.getNumOperands(); I != E; I++)
334       R.setOperand(I, NewValue);
335   }
336 }
337 
338 void VPRegionBlock::dropAllReferences(VPValue *NewValue) {
339   for (VPBlockBase *Block : depth_first(Entry))
340     // Drop all references in VPBasicBlocks and replace all uses with
341     // DummyValue.
342     Block->dropAllReferences(NewValue);
343 }
344 
345 void VPRegionBlock::execute(VPTransformState *State) {
346   ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry);
347 
348   if (!isReplicator()) {
349     // Visit the VPBlocks connected to "this", starting from it.
350     for (VPBlockBase *Block : RPOT) {
351       if (EnableVPlanNativePath) {
352         // The inner loop vectorization path does not represent loop preheader
353         // and exit blocks as part of the VPlan. In the VPlan-native path, skip
354         // vectorizing loop preheader block. In future, we may replace this
355         // check with the check for loop preheader.
356         if (Block->getNumPredecessors() == 0)
357           continue;
358 
359         // Skip vectorizing loop exit block. In future, we may replace this
360         // check with the check for loop exit.
361         if (Block->getNumSuccessors() == 0)
362           continue;
363       }
364 
365       LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
366       Block->execute(State);
367     }
368     return;
369   }
370 
371   assert(!State->Instance && "Replicating a Region with non-null instance.");
372 
373   // Enter replicating mode.
374   State->Instance = VPIteration(0, 0);
375 
376   for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) {
377     State->Instance->Part = Part;
378     assert(!State->VF.isScalable() && "VF is assumed to be non scalable.");
379     for (unsigned Lane = 0, VF = State->VF.getKnownMinValue(); Lane < VF;
380          ++Lane) {
381       State->Instance->Lane = Lane;
382       // Visit the VPBlocks connected to \p this, starting from it.
383       for (VPBlockBase *Block : RPOT) {
384         LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
385         Block->execute(State);
386       }
387     }
388   }
389 
390   // Exit replicating mode.
391   State->Instance.reset();
392 }
393 
394 void VPRecipeBase::insertBefore(VPRecipeBase *InsertPos) {
395   assert(!Parent && "Recipe already in some VPBasicBlock");
396   assert(InsertPos->getParent() &&
397          "Insertion position not in any VPBasicBlock");
398   Parent = InsertPos->getParent();
399   Parent->getRecipeList().insert(InsertPos->getIterator(), this);
400 }
401 
402 void VPRecipeBase::insertAfter(VPRecipeBase *InsertPos) {
403   assert(!Parent && "Recipe already in some VPBasicBlock");
404   assert(InsertPos->getParent() &&
405          "Insertion position not in any VPBasicBlock");
406   Parent = InsertPos->getParent();
407   Parent->getRecipeList().insertAfter(InsertPos->getIterator(), this);
408 }
409 
410 void VPRecipeBase::removeFromParent() {
411   assert(getParent() && "Recipe not in any VPBasicBlock");
412   getParent()->getRecipeList().remove(getIterator());
413   Parent = nullptr;
414 }
415 
416 iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
417   assert(getParent() && "Recipe not in any VPBasicBlock");
418   return getParent()->getRecipeList().erase(getIterator());
419 }
420 
421 void VPRecipeBase::moveAfter(VPRecipeBase *InsertPos) {
422   removeFromParent();
423   insertAfter(InsertPos);
424 }
425 
426 void VPRecipeBase::moveBefore(VPBasicBlock &BB,
427                               iplist<VPRecipeBase>::iterator I) {
428   assert(I == BB.end() || I->getParent() == &BB);
429   removeFromParent();
430   Parent = &BB;
431   BB.getRecipeList().insert(I, this);
432 }
433 
434 void VPInstruction::generateInstruction(VPTransformState &State,
435                                         unsigned Part) {
436   IRBuilder<> &Builder = State.Builder;
437 
438   if (Instruction::isBinaryOp(getOpcode())) {
439     Value *A = State.get(getOperand(0), Part);
440     Value *B = State.get(getOperand(1), Part);
441     Value *V = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(), A, B);
442     State.set(this, V, Part);
443     return;
444   }
445 
446   switch (getOpcode()) {
447   case VPInstruction::Not: {
448     Value *A = State.get(getOperand(0), Part);
449     Value *V = Builder.CreateNot(A);
450     State.set(this, V, Part);
451     break;
452   }
453   case VPInstruction::ICmpULE: {
454     Value *IV = State.get(getOperand(0), Part);
455     Value *TC = State.get(getOperand(1), Part);
456     Value *V = Builder.CreateICmpULE(IV, TC);
457     State.set(this, V, Part);
458     break;
459   }
460   case Instruction::Select: {
461     Value *Cond = State.get(getOperand(0), Part);
462     Value *Op1 = State.get(getOperand(1), Part);
463     Value *Op2 = State.get(getOperand(2), Part);
464     Value *V = Builder.CreateSelect(Cond, Op1, Op2);
465     State.set(this, V, Part);
466     break;
467   }
468   case VPInstruction::ActiveLaneMask: {
469     // Get first lane of vector induction variable.
470     Value *VIVElem0 = State.get(getOperand(0), VPIteration(Part, 0));
471     // Get the original loop tripcount.
472     Value *ScalarTC = State.TripCount;
473 
474     auto *Int1Ty = Type::getInt1Ty(Builder.getContext());
475     auto *PredTy = FixedVectorType::get(Int1Ty, State.VF.getKnownMinValue());
476     Instruction *Call = Builder.CreateIntrinsic(
477         Intrinsic::get_active_lane_mask, {PredTy, ScalarTC->getType()},
478         {VIVElem0, ScalarTC}, nullptr, "active.lane.mask");
479     State.set(this, Call, Part);
480     break;
481   }
482   default:
483     llvm_unreachable("Unsupported opcode for instruction");
484   }
485 }
486 
487 void VPInstruction::execute(VPTransformState &State) {
488   assert(!State.Instance && "VPInstruction executing an Instance");
489   for (unsigned Part = 0; Part < State.UF; ++Part)
490     generateInstruction(State, Part);
491 }
492 
493 void VPInstruction::dump() const {
494   VPSlotTracker SlotTracker(getParent()->getPlan());
495   print(dbgs(), "", SlotTracker);
496 }
497 
498 void VPInstruction::print(raw_ostream &O, const Twine &Indent,
499                           VPSlotTracker &SlotTracker) const {
500   O << "EMIT ";
501 
502   if (hasResult()) {
503     printAsOperand(O, SlotTracker);
504     O << " = ";
505   }
506 
507   switch (getOpcode()) {
508   case VPInstruction::Not:
509     O << "not";
510     break;
511   case VPInstruction::ICmpULE:
512     O << "icmp ule";
513     break;
514   case VPInstruction::SLPLoad:
515     O << "combined load";
516     break;
517   case VPInstruction::SLPStore:
518     O << "combined store";
519     break;
520   case VPInstruction::ActiveLaneMask:
521     O << "active lane mask";
522     break;
523 
524   default:
525     O << Instruction::getOpcodeName(getOpcode());
526   }
527 
528   for (const VPValue *Operand : operands()) {
529     O << " ";
530     Operand->printAsOperand(O, SlotTracker);
531   }
532 }
533 
534 /// Generate the code inside the body of the vectorized loop. Assumes a single
535 /// LoopVectorBody basic-block was created for this. Introduce additional
536 /// basic-blocks as needed, and fill them all.
537 void VPlan::execute(VPTransformState *State) {
538   // -1. Check if the backedge taken count is needed, and if so build it.
539   if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) {
540     Value *TC = State->TripCount;
541     IRBuilder<> Builder(State->CFG.PrevBB->getTerminator());
542     auto *TCMO = Builder.CreateSub(TC, ConstantInt::get(TC->getType(), 1),
543                                    "trip.count.minus.1");
544     auto VF = State->VF;
545     Value *VTCMO =
546         VF.isScalar() ? TCMO : Builder.CreateVectorSplat(VF, TCMO, "broadcast");
547     for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part)
548       State->set(BackedgeTakenCount, VTCMO, Part);
549   }
550 
551   // 0. Set the reverse mapping from VPValues to Values for code generation.
552   for (auto &Entry : Value2VPValue)
553     State->VPValue2Value[Entry.second] = Entry.first;
554 
555   BasicBlock *VectorPreHeaderBB = State->CFG.PrevBB;
556   BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor();
557   assert(VectorHeaderBB && "Loop preheader does not have a single successor.");
558 
559   // 1. Make room to generate basic-blocks inside loop body if needed.
560   BasicBlock *VectorLatchBB = VectorHeaderBB->splitBasicBlock(
561       VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch");
562   Loop *L = State->LI->getLoopFor(VectorHeaderBB);
563   L->addBasicBlockToLoop(VectorLatchBB, *State->LI);
564   // Remove the edge between Header and Latch to allow other connections.
565   // Temporarily terminate with unreachable until CFG is rewired.
566   // Note: this asserts the generated code's assumption that
567   // getFirstInsertionPt() can be dereferenced into an Instruction.
568   VectorHeaderBB->getTerminator()->eraseFromParent();
569   State->Builder.SetInsertPoint(VectorHeaderBB);
570   UnreachableInst *Terminator = State->Builder.CreateUnreachable();
571   State->Builder.SetInsertPoint(Terminator);
572 
573   // 2. Generate code in loop body.
574   State->CFG.PrevVPBB = nullptr;
575   State->CFG.PrevBB = VectorHeaderBB;
576   State->CFG.LastBB = VectorLatchBB;
577 
578   for (VPBlockBase *Block : depth_first(Entry))
579     Block->execute(State);
580 
581   // Setup branch terminator successors for VPBBs in VPBBsToFix based on
582   // VPBB's successors.
583   for (auto VPBB : State->CFG.VPBBsToFix) {
584     assert(EnableVPlanNativePath &&
585            "Unexpected VPBBsToFix in non VPlan-native path");
586     BasicBlock *BB = State->CFG.VPBB2IRBB[VPBB];
587     assert(BB && "Unexpected null basic block for VPBB");
588 
589     unsigned Idx = 0;
590     auto *BBTerminator = BB->getTerminator();
591 
592     for (VPBlockBase *SuccVPBlock : VPBB->getHierarchicalSuccessors()) {
593       VPBasicBlock *SuccVPBB = SuccVPBlock->getEntryBasicBlock();
594       BBTerminator->setSuccessor(Idx, State->CFG.VPBB2IRBB[SuccVPBB]);
595       ++Idx;
596     }
597   }
598 
599   // 3. Merge the temporary latch created with the last basic-block filled.
600   BasicBlock *LastBB = State->CFG.PrevBB;
601   // Connect LastBB to VectorLatchBB to facilitate their merge.
602   assert((EnableVPlanNativePath ||
603           isa<UnreachableInst>(LastBB->getTerminator())) &&
604          "Expected InnerLoop VPlan CFG to terminate with unreachable");
605   assert((!EnableVPlanNativePath || isa<BranchInst>(LastBB->getTerminator())) &&
606          "Expected VPlan CFG to terminate with branch in NativePath");
607   LastBB->getTerminator()->eraseFromParent();
608   BranchInst::Create(VectorLatchBB, LastBB);
609 
610   // Merge LastBB with Latch.
611   bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI);
612   (void)Merged;
613   assert(Merged && "Could not merge last basic block with latch.");
614   VectorLatchBB = LastBB;
615 
616   // We do not attempt to preserve DT for outer loop vectorization currently.
617   if (!EnableVPlanNativePath)
618     updateDominatorTree(State->DT, VectorPreHeaderBB, VectorLatchBB,
619                         L->getExitBlock());
620 }
621 
622 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
623 LLVM_DUMP_METHOD
624 void VPlan::dump() const { dbgs() << *this << '\n'; }
625 #endif
626 
627 void VPlan::updateDominatorTree(DominatorTree *DT, BasicBlock *LoopPreHeaderBB,
628                                 BasicBlock *LoopLatchBB,
629                                 BasicBlock *LoopExitBB) {
630   BasicBlock *LoopHeaderBB = LoopPreHeaderBB->getSingleSuccessor();
631   assert(LoopHeaderBB && "Loop preheader does not have a single successor.");
632   // The vector body may be more than a single basic-block by this point.
633   // Update the dominator tree information inside the vector body by propagating
634   // it from header to latch, expecting only triangular control-flow, if any.
635   BasicBlock *PostDomSucc = nullptr;
636   for (auto *BB = LoopHeaderBB; BB != LoopLatchBB; BB = PostDomSucc) {
637     // Get the list of successors of this block.
638     std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
639     assert(Succs.size() <= 2 &&
640            "Basic block in vector loop has more than 2 successors.");
641     PostDomSucc = Succs[0];
642     if (Succs.size() == 1) {
643       assert(PostDomSucc->getSinglePredecessor() &&
644              "PostDom successor has more than one predecessor.");
645       DT->addNewBlock(PostDomSucc, BB);
646       continue;
647     }
648     BasicBlock *InterimSucc = Succs[1];
649     if (PostDomSucc->getSingleSuccessor() == InterimSucc) {
650       PostDomSucc = Succs[1];
651       InterimSucc = Succs[0];
652     }
653     assert(InterimSucc->getSingleSuccessor() == PostDomSucc &&
654            "One successor of a basic block does not lead to the other.");
655     assert(InterimSucc->getSinglePredecessor() &&
656            "Interim successor has more than one predecessor.");
657     assert(PostDomSucc->hasNPredecessors(2) &&
658            "PostDom successor has more than two predecessors.");
659     DT->addNewBlock(InterimSucc, BB);
660     DT->addNewBlock(PostDomSucc, BB);
661   }
662   // Latch block is a new dominator for the loop exit.
663   DT->changeImmediateDominator(LoopExitBB, LoopLatchBB);
664   assert(DT->verify(DominatorTree::VerificationLevel::Fast));
665 }
666 
667 const Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
668   return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") +
669          Twine(getOrCreateBID(Block));
670 }
671 
672 const Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) {
673   const std::string &Name = Block->getName();
674   if (!Name.empty())
675     return Name;
676   return "VPB" + Twine(getOrCreateBID(Block));
677 }
678 
679 void VPlanPrinter::dump() {
680   Depth = 1;
681   bumpIndent(0);
682   OS << "digraph VPlan {\n";
683   OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
684   if (!Plan.getName().empty())
685     OS << "\\n" << DOT::EscapeString(Plan.getName());
686   if (Plan.BackedgeTakenCount) {
687     OS << ", where:\\n";
688     Plan.BackedgeTakenCount->print(OS, SlotTracker);
689     OS << " := BackedgeTakenCount";
690   }
691   OS << "\"]\n";
692   OS << "node [shape=rect, fontname=Courier, fontsize=30]\n";
693   OS << "edge [fontname=Courier, fontsize=30]\n";
694   OS << "compound=true\n";
695 
696   for (const VPBlockBase *Block : depth_first(Plan.getEntry()))
697     dumpBlock(Block);
698 
699   OS << "}\n";
700 }
701 
702 void VPlanPrinter::dumpBlock(const VPBlockBase *Block) {
703   if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block))
704     dumpBasicBlock(BasicBlock);
705   else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
706     dumpRegion(Region);
707   else
708     llvm_unreachable("Unsupported kind of VPBlock.");
709 }
710 
711 void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To,
712                             bool Hidden, const Twine &Label) {
713   // Due to "dot" we print an edge between two regions as an edge between the
714   // exit basic block and the entry basic of the respective regions.
715   const VPBlockBase *Tail = From->getExitBasicBlock();
716   const VPBlockBase *Head = To->getEntryBasicBlock();
717   OS << Indent << getUID(Tail) << " -> " << getUID(Head);
718   OS << " [ label=\"" << Label << '\"';
719   if (Tail != From)
720     OS << " ltail=" << getUID(From);
721   if (Head != To)
722     OS << " lhead=" << getUID(To);
723   if (Hidden)
724     OS << "; splines=none";
725   OS << "]\n";
726 }
727 
728 void VPlanPrinter::dumpEdges(const VPBlockBase *Block) {
729   auto &Successors = Block->getSuccessors();
730   if (Successors.size() == 1)
731     drawEdge(Block, Successors.front(), false, "");
732   else if (Successors.size() == 2) {
733     drawEdge(Block, Successors.front(), false, "T");
734     drawEdge(Block, Successors.back(), false, "F");
735   } else {
736     unsigned SuccessorNumber = 0;
737     for (auto *Successor : Successors)
738       drawEdge(Block, Successor, false, Twine(SuccessorNumber++));
739   }
740 }
741 
742 void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) {
743   OS << Indent << getUID(BasicBlock) << " [label =\n";
744   bumpIndent(1);
745   OS << Indent << "\"" << DOT::EscapeString(BasicBlock->getName()) << ":\\n\"";
746   bumpIndent(1);
747 
748   // Dump the block predicate.
749   const VPValue *Pred = BasicBlock->getPredicate();
750   if (Pred) {
751     OS << " +\n" << Indent << " \"BlockPredicate: \"";
752     if (const VPInstruction *PredI = dyn_cast<VPInstruction>(Pred)) {
753       PredI->printAsOperand(OS, SlotTracker);
754       OS << " (" << DOT::EscapeString(PredI->getParent()->getName())
755          << ")\\l\"";
756     } else
757       Pred->printAsOperand(OS, SlotTracker);
758   }
759 
760   for (const VPRecipeBase &Recipe : *BasicBlock) {
761     OS << " +\n" << Indent << "\"";
762     Recipe.print(OS, Indent, SlotTracker);
763     OS << "\\l\"";
764   }
765 
766   // Dump the condition bit.
767   const VPValue *CBV = BasicBlock->getCondBit();
768   if (CBV) {
769     OS << " +\n" << Indent << " \"CondBit: ";
770     if (const VPInstruction *CBI = dyn_cast<VPInstruction>(CBV)) {
771       CBI->printAsOperand(OS, SlotTracker);
772       OS << " (" << DOT::EscapeString(CBI->getParent()->getName()) << ")\\l\"";
773     } else {
774       CBV->printAsOperand(OS, SlotTracker);
775       OS << "\"";
776     }
777   }
778 
779   bumpIndent(-2);
780   OS << "\n" << Indent << "]\n";
781   dumpEdges(BasicBlock);
782 }
783 
784 void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
785   OS << Indent << "subgraph " << getUID(Region) << " {\n";
786   bumpIndent(1);
787   OS << Indent << "fontname=Courier\n"
788      << Indent << "label=\""
789      << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ")
790      << DOT::EscapeString(Region->getName()) << "\"\n";
791   // Dump the blocks of the region.
792   assert(Region->getEntry() && "Region contains no inner blocks.");
793   for (const VPBlockBase *Block : depth_first(Region->getEntry()))
794     dumpBlock(Block);
795   bumpIndent(-1);
796   OS << Indent << "}\n";
797   dumpEdges(Region);
798 }
799 
800 void VPlanPrinter::printAsIngredient(raw_ostream &O, const Value *V) {
801   std::string IngredientString;
802   raw_string_ostream RSO(IngredientString);
803   if (auto *Inst = dyn_cast<Instruction>(V)) {
804     if (!Inst->getType()->isVoidTy()) {
805       Inst->printAsOperand(RSO, false);
806       RSO << " = ";
807     }
808     RSO << Inst->getOpcodeName() << " ";
809     unsigned E = Inst->getNumOperands();
810     if (E > 0) {
811       Inst->getOperand(0)->printAsOperand(RSO, false);
812       for (unsigned I = 1; I < E; ++I)
813         Inst->getOperand(I)->printAsOperand(RSO << ", ", false);
814     }
815   } else // !Inst
816     V->printAsOperand(RSO, false);
817   RSO.flush();
818   O << DOT::EscapeString(IngredientString);
819 }
820 
821 void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent,
822                               VPSlotTracker &SlotTracker) const {
823   O << "WIDEN-CALL ";
824 
825   auto *CI = cast<CallInst>(getUnderlyingInstr());
826   if (CI->getType()->isVoidTy())
827     O << "void ";
828   else {
829     printAsOperand(O, SlotTracker);
830     O << " = ";
831   }
832 
833   O << "call @" << CI->getCalledFunction()->getName() << "(";
834   printOperands(O, SlotTracker);
835   O << ")";
836 }
837 
838 void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent,
839                                 VPSlotTracker &SlotTracker) const {
840   O << "WIDEN-SELECT ";
841   printAsOperand(O, SlotTracker);
842   O << " = select ";
843   getOperand(0)->printAsOperand(O, SlotTracker);
844   O << ", ";
845   getOperand(1)->printAsOperand(O, SlotTracker);
846   O << ", ";
847   getOperand(2)->printAsOperand(O, SlotTracker);
848   O << (InvariantCond ? " (condition is loop invariant)" : "");
849 }
850 
851 void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
852                           VPSlotTracker &SlotTracker) const {
853   O << "WIDEN ";
854   printAsOperand(O, SlotTracker);
855   O << " = " << getUnderlyingInstr()->getOpcodeName() << " ";
856   printOperands(O, SlotTracker);
857 }
858 
859 void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O, const Twine &Indent,
860                                           VPSlotTracker &SlotTracker) const {
861   O << "WIDEN-INDUCTION";
862   if (getTruncInst()) {
863     O << "\\l\"";
864     O << " +\n" << Indent << "\"  " << VPlanIngredient(IV) << "\\l\"";
865     O << " +\n" << Indent << "\"  ";
866     getVPValue(0)->printAsOperand(O, SlotTracker);
867   } else
868     O << " " << VPlanIngredient(IV);
869 }
870 
871 void VPWidenGEPRecipe::print(raw_ostream &O, const Twine &Indent,
872                              VPSlotTracker &SlotTracker) const {
873   O << "WIDEN-GEP ";
874   O << (IsPtrLoopInvariant ? "Inv" : "Var");
875   size_t IndicesNumber = IsIndexLoopInvariant.size();
876   for (size_t I = 0; I < IndicesNumber; ++I)
877     O << "[" << (IsIndexLoopInvariant[I] ? "Inv" : "Var") << "]";
878 
879   O << " ";
880   printAsOperand(O, SlotTracker);
881   O << " = getelementptr ";
882   printOperands(O, SlotTracker);
883 }
884 
885 void VPWidenPHIRecipe::print(raw_ostream &O, const Twine &Indent,
886                              VPSlotTracker &SlotTracker) const {
887   O << "WIDEN-PHI " << VPlanIngredient(Phi);
888 }
889 
890 void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent,
891                           VPSlotTracker &SlotTracker) const {
892   O << "BLEND ";
893   Phi->printAsOperand(O, false);
894   O << " =";
895   if (getNumIncomingValues() == 1) {
896     // Not a User of any mask: not really blending, this is a
897     // single-predecessor phi.
898     O << " ";
899     getIncomingValue(0)->printAsOperand(O, SlotTracker);
900   } else {
901     for (unsigned I = 0, E = getNumIncomingValues(); I < E; ++I) {
902       O << " ";
903       getIncomingValue(I)->printAsOperand(O, SlotTracker);
904       O << "/";
905       getMask(I)->printAsOperand(O, SlotTracker);
906     }
907   }
908 }
909 
910 void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent,
911                               VPSlotTracker &SlotTracker) const {
912   O << "REDUCE ";
913   printAsOperand(O, SlotTracker);
914   O << " = ";
915   getChainOp()->printAsOperand(O, SlotTracker);
916   O << " + reduce." << Instruction::getOpcodeName(RdxDesc->getOpcode())
917     << " (";
918   getVecOp()->printAsOperand(O, SlotTracker);
919   if (getCondOp()) {
920     O << ", ";
921     getCondOp()->printAsOperand(O, SlotTracker);
922   }
923   O << ")";
924 }
925 
926 void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent,
927                               VPSlotTracker &SlotTracker) const {
928   O << (IsUniform ? "CLONE " : "REPLICATE ");
929 
930   if (!getUnderlyingInstr()->getType()->isVoidTy()) {
931     printAsOperand(O, SlotTracker);
932     O << " = ";
933   }
934   O << Instruction::getOpcodeName(getUnderlyingInstr()->getOpcode()) << " ";
935   printOperands(O, SlotTracker);
936 
937   if (AlsoPack)
938     O << " (S->V)";
939 }
940 
941 void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent,
942                                 VPSlotTracker &SlotTracker) const {
943   O << "PHI-PREDICATED-INSTRUCTION ";
944   printOperands(O, SlotTracker);
945 }
946 
947 void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent,
948                                            VPSlotTracker &SlotTracker) const {
949   O << "WIDEN ";
950 
951   if (!isStore()) {
952     getVPValue()->printAsOperand(O, SlotTracker);
953     O << " = ";
954   }
955   O << Instruction::getOpcodeName(Ingredient.getOpcode()) << " ";
956 
957   printOperands(O, SlotTracker);
958 }
959 
960 void VPWidenCanonicalIVRecipe::execute(VPTransformState &State) {
961   Value *CanonicalIV = State.CanonicalIV;
962   Type *STy = CanonicalIV->getType();
963   IRBuilder<> Builder(State.CFG.PrevBB->getTerminator());
964   ElementCount VF = State.VF;
965   assert(!VF.isScalable() && "the code following assumes non scalables ECs");
966   Value *VStart = VF.isScalar()
967                       ? CanonicalIV
968                       : Builder.CreateVectorSplat(VF.getKnownMinValue(),
969                                                   CanonicalIV, "broadcast");
970   for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) {
971     SmallVector<Constant *, 8> Indices;
972     for (unsigned Lane = 0; Lane < VF.getKnownMinValue(); ++Lane)
973       Indices.push_back(
974           ConstantInt::get(STy, Part * VF.getKnownMinValue() + Lane));
975     // If VF == 1, there is only one iteration in the loop above, thus the
976     // element pushed back into Indices is ConstantInt::get(STy, Part)
977     Constant *VStep =
978         VF.isScalar() ? Indices.back() : ConstantVector::get(Indices);
979     // Add the consecutive indices to the vector value.
980     Value *CanonicalVectorIV = Builder.CreateAdd(VStart, VStep, "vec.iv");
981     State.set(getVPValue(), CanonicalVectorIV, Part);
982   }
983 }
984 
985 void VPWidenCanonicalIVRecipe::print(raw_ostream &O, const Twine &Indent,
986                                      VPSlotTracker &SlotTracker) const {
987   O << "EMIT ";
988   getVPValue()->printAsOperand(O, SlotTracker);
989   O << " = WIDEN-CANONICAL-INDUCTION";
990 }
991 
992 template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
993 
994 void VPValue::replaceAllUsesWith(VPValue *New) {
995   for (unsigned J = 0; J < getNumUsers();) {
996     VPUser *User = Users[J];
997     unsigned NumUsers = getNumUsers();
998     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I)
999       if (User->getOperand(I) == this)
1000         User->setOperand(I, New);
1001     // If a user got removed after updating the current user, the next user to
1002     // update will be moved to the current position, so we only need to
1003     // increment the index if the number of users did not change.
1004     if (NumUsers == getNumUsers())
1005       J++;
1006   }
1007 }
1008 
1009 void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {
1010   if (const Value *UV = getUnderlyingValue()) {
1011     OS << "ir<";
1012     UV->printAsOperand(OS, false);
1013     OS << ">";
1014     return;
1015   }
1016 
1017   unsigned Slot = Tracker.getSlot(this);
1018   if (Slot == unsigned(-1))
1019     OS << "<badref>";
1020   else
1021     OS << "vp<%" << Tracker.getSlot(this) << ">";
1022 }
1023 
1024 void VPUser::printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const {
1025   interleaveComma(operands(), O, [&O, &SlotTracker](VPValue *Op) {
1026     Op->printAsOperand(O, SlotTracker);
1027   });
1028 }
1029 
1030 void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region,
1031                                           Old2NewTy &Old2New,
1032                                           InterleavedAccessInfo &IAI) {
1033   ReversePostOrderTraversal<VPBlockBase *> RPOT(Region->getEntry());
1034   for (VPBlockBase *Base : RPOT) {
1035     visitBlock(Base, Old2New, IAI);
1036   }
1037 }
1038 
1039 void VPInterleavedAccessInfo::visitBlock(VPBlockBase *Block, Old2NewTy &Old2New,
1040                                          InterleavedAccessInfo &IAI) {
1041   if (VPBasicBlock *VPBB = dyn_cast<VPBasicBlock>(Block)) {
1042     for (VPRecipeBase &VPI : *VPBB) {
1043       assert(isa<VPInstruction>(&VPI) && "Can only handle VPInstructions");
1044       auto *VPInst = cast<VPInstruction>(&VPI);
1045       auto *Inst = cast<Instruction>(VPInst->getUnderlyingValue());
1046       auto *IG = IAI.getInterleaveGroup(Inst);
1047       if (!IG)
1048         continue;
1049 
1050       auto NewIGIter = Old2New.find(IG);
1051       if (NewIGIter == Old2New.end())
1052         Old2New[IG] = new InterleaveGroup<VPInstruction>(
1053             IG->getFactor(), IG->isReverse(), IG->getAlign());
1054 
1055       if (Inst == IG->getInsertPos())
1056         Old2New[IG]->setInsertPos(VPInst);
1057 
1058       InterleaveGroupMap[VPInst] = Old2New[IG];
1059       InterleaveGroupMap[VPInst]->insertMember(
1060           VPInst, IG->getIndex(Inst),
1061           Align(IG->isReverse() ? (-1) * int(IG->getFactor())
1062                                 : IG->getFactor()));
1063     }
1064   } else if (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1065     visitRegion(Region, Old2New, IAI);
1066   else
1067     llvm_unreachable("Unsupported kind of VPBlock.");
1068 }
1069 
1070 VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan,
1071                                                  InterleavedAccessInfo &IAI) {
1072   Old2NewTy Old2New;
1073   visitRegion(cast<VPRegionBlock>(Plan.getEntry()), Old2New, IAI);
1074 }
1075 
1076 void VPSlotTracker::assignSlot(const VPValue *V) {
1077   assert(Slots.find(V) == Slots.end() && "VPValue already has a slot!");
1078   Slots[V] = NextSlot++;
1079 }
1080 
1081 void VPSlotTracker::assignSlots(const VPBlockBase *VPBB) {
1082   if (auto *Region = dyn_cast<VPRegionBlock>(VPBB))
1083     assignSlots(Region);
1084   else
1085     assignSlots(cast<VPBasicBlock>(VPBB));
1086 }
1087 
1088 void VPSlotTracker::assignSlots(const VPRegionBlock *Region) {
1089   ReversePostOrderTraversal<const VPBlockBase *> RPOT(Region->getEntry());
1090   for (const VPBlockBase *Block : RPOT)
1091     assignSlots(Block);
1092 }
1093 
1094 void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) {
1095   for (const VPRecipeBase &Recipe : *VPBB) {
1096     for (VPValue *Def : Recipe.definedValues())
1097       assignSlot(Def);
1098   }
1099 }
1100 
1101 void VPSlotTracker::assignSlots(const VPlan &Plan) {
1102 
1103   for (const VPValue *V : Plan.VPExternalDefs)
1104     assignSlot(V);
1105 
1106   if (Plan.BackedgeTakenCount)
1107     assignSlot(Plan.BackedgeTakenCount);
1108 
1109   ReversePostOrderTraversal<const VPBlockBase *> RPOT(Plan.getEntry());
1110   for (const VPBlockBase *Block : RPOT)
1111     assignSlots(Block);
1112 }
1113