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