xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlan.cpp (revision ea7f3035a01786e7d69bb1ce1e038cefb6657a2e)
1 //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This is the LLVM vectorization plan. It represents a candidate for
12 /// vectorization, allowing to plan and optimize how to vectorize a given loop
13 /// before generating LLVM-IR.
14 /// The vectorizer uses vectorization plans to estimate the costs of potential
15 /// candidates and if profitable to execute the desired plan, generating vector
16 /// LLVM-IR code.
17 ///
18 //===----------------------------------------------------------------------===//
19 
20 #include "VPlan.h"
21 #include "VPlanDominatorTree.h"
22 #include "llvm/ADT/DepthFirstIterator.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Analysis/LoopInfo.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/CFG.h"
29 #include "llvm/IR/InstrTypes.h"
30 #include "llvm/IR/Instruction.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/IR/Value.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GenericDomTreeConstruction.h"
38 #include "llvm/Support/GraphWriter.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include <cassert>
42 #include <iterator>
43 #include <string>
44 #include <vector>
45 
46 using namespace llvm;
47 extern cl::opt<bool> EnableVPlanNativePath;
48 
49 #define DEBUG_TYPE "vplan"
50 
51 raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
52   if (const VPInstruction *Instr = dyn_cast<VPInstruction>(&V))
53     Instr->print(OS);
54   else
55     V.printAsOperand(OS);
56   return OS;
57 }
58 
59 /// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
60 const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const {
61   const VPBlockBase *Block = this;
62   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
63     Block = Region->getEntry();
64   return cast<VPBasicBlock>(Block);
65 }
66 
67 VPBasicBlock *VPBlockBase::getEntryBasicBlock() {
68   VPBlockBase *Block = this;
69   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
70     Block = Region->getEntry();
71   return cast<VPBasicBlock>(Block);
72 }
73 
74 /// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
75 const VPBasicBlock *VPBlockBase::getExitBasicBlock() const {
76   const VPBlockBase *Block = this;
77   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
78     Block = Region->getExit();
79   return cast<VPBasicBlock>(Block);
80 }
81 
82 VPBasicBlock *VPBlockBase::getExitBasicBlock() {
83   VPBlockBase *Block = this;
84   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
85     Block = Region->getExit();
86   return cast<VPBasicBlock>(Block);
87 }
88 
89 VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() {
90   if (!Successors.empty() || !Parent)
91     return this;
92   assert(Parent->getExit() == this &&
93          "Block w/o successors not the exit of its parent.");
94   return Parent->getEnclosingBlockWithSuccessors();
95 }
96 
97 VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
98   if (!Predecessors.empty() || !Parent)
99     return this;
100   assert(Parent->getEntry() == this &&
101          "Block w/o predecessors not the entry of its parent.");
102   return Parent->getEnclosingBlockWithPredecessors();
103 }
104 
105 void VPBlockBase::deleteCFG(VPBlockBase *Entry) {
106   SmallVector<VPBlockBase *, 8> Blocks;
107   for (VPBlockBase *Block : depth_first(Entry))
108     Blocks.push_back(Block);
109 
110   for (VPBlockBase *Block : Blocks)
111     delete Block;
112 }
113 
114 BasicBlock *
115 VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
116   // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
117   // Pred stands for Predessor. Prev stands for Previous - last visited/created.
118   BasicBlock *PrevBB = CFG.PrevBB;
119   BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(),
120                                          PrevBB->getParent(), CFG.LastBB);
121   LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n');
122 
123   // Hook up the new basic block to its predecessors.
124   for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) {
125     VPBasicBlock *PredVPBB = PredVPBlock->getExitBasicBlock();
126     auto &PredVPSuccessors = PredVPBB->getSuccessors();
127     BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
128 
129     // In outer loop vectorization scenario, the predecessor BBlock may not yet
130     // be visited(backedge). Mark the VPBasicBlock for fixup at the end of
131     // vectorization. We do not encounter this case in inner loop vectorization
132     // as we start out by building a loop skeleton with the vector loop header
133     // and latch blocks. As a result, we never enter this function for the
134     // header block in the non VPlan-native path.
135     if (!PredBB) {
136       assert(EnableVPlanNativePath &&
137              "Unexpected null predecessor in non VPlan-native path");
138       CFG.VPBBsToFix.push_back(PredVPBB);
139       continue;
140     }
141 
142     assert(PredBB && "Predecessor basic-block not found building successor.");
143     auto *PredBBTerminator = PredBB->getTerminator();
144     LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
145     if (isa<UnreachableInst>(PredBBTerminator)) {
146       assert(PredVPSuccessors.size() == 1 &&
147              "Predecessor ending w/o branch must have single successor.");
148       PredBBTerminator->eraseFromParent();
149       BranchInst::Create(NewBB, PredBB);
150     } else {
151       assert(PredVPSuccessors.size() == 2 &&
152              "Predecessor ending with branch must have two successors.");
153       unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
154       assert(!PredBBTerminator->getSuccessor(idx) &&
155              "Trying to reset an existing successor block.");
156       PredBBTerminator->setSuccessor(idx, NewBB);
157     }
158   }
159   return NewBB;
160 }
161 
162 void VPBasicBlock::execute(VPTransformState *State) {
163   bool Replica = State->Instance &&
164                  !(State->Instance->Part == 0 && State->Instance->Lane == 0);
165   VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB;
166   VPBlockBase *SingleHPred = nullptr;
167   BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible.
168 
169   // 1. Create an IR basic block, or reuse the last one if possible.
170   // The last IR basic block is reused, as an optimization, in three cases:
171   // A. the first VPBB reuses the loop header BB - when PrevVPBB is null;
172   // B. when the current VPBB has a single (hierarchical) predecessor which
173   //    is PrevVPBB and the latter has a single (hierarchical) successor; and
174   // C. when the current VPBB is an entry of a region replica - where PrevVPBB
175   //    is the exit of this region from a previous instance, or the predecessor
176   //    of this region.
177   if (PrevVPBB && /* A */
178       !((SingleHPred = getSingleHierarchicalPredecessor()) &&
179         SingleHPred->getExitBasicBlock() == PrevVPBB &&
180         PrevVPBB->getSingleHierarchicalSuccessor()) && /* B */
181       !(Replica && getPredecessors().empty())) {       /* C */
182     NewBB = createEmptyBasicBlock(State->CFG);
183     State->Builder.SetInsertPoint(NewBB);
184     // Temporarily terminate with unreachable until CFG is rewired.
185     UnreachableInst *Terminator = State->Builder.CreateUnreachable();
186     State->Builder.SetInsertPoint(Terminator);
187     // Register NewBB in its loop. In innermost loops its the same for all BB's.
188     Loop *L = State->LI->getLoopFor(State->CFG.LastBB);
189     L->addBasicBlockToLoop(NewBB, *State->LI);
190     State->CFG.PrevBB = NewBB;
191   }
192 
193   // 2. Fill the IR basic block with IR instructions.
194   LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName()
195                     << " in BB:" << NewBB->getName() << '\n');
196 
197   State->CFG.VPBB2IRBB[this] = NewBB;
198   State->CFG.PrevVPBB = this;
199 
200   for (VPRecipeBase &Recipe : Recipes)
201     Recipe.execute(*State);
202 
203   VPValue *CBV;
204   if (EnableVPlanNativePath && (CBV = getCondBit())) {
205     Value *IRCBV = CBV->getUnderlyingValue();
206     assert(IRCBV && "Unexpected null underlying value for condition bit");
207 
208     // Delete the condition bit at this point - it should be no longer needed.
209     delete CBV;
210     setCondBit(nullptr);
211 
212     // Condition bit value in a VPBasicBlock is used as the branch selector. In
213     // the VPlan-native path case, since all branches are uniform we generate a
214     // branch instruction using the condition value from vector lane 0 and dummy
215     // successors. The successors are fixed later when the successor blocks are
216     // visited.
217     Value *NewCond = State->Callback.getOrCreateVectorValues(IRCBV, 0);
218     NewCond = State->Builder.CreateExtractElement(NewCond,
219                                                   State->Builder.getInt32(0));
220 
221     // Replace the temporary unreachable terminator with the new conditional
222     // branch.
223     auto *CurrentTerminator = NewBB->getTerminator();
224     assert(isa<UnreachableInst>(CurrentTerminator) &&
225            "Expected to replace unreachable terminator with conditional "
226            "branch.");
227     auto *CondBr = BranchInst::Create(NewBB, nullptr, NewCond);
228     CondBr->setSuccessor(0, nullptr);
229     ReplaceInstWithInst(CurrentTerminator, CondBr);
230   }
231 
232   LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB);
233 }
234 
235 void VPRegionBlock::execute(VPTransformState *State) {
236   ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry);
237 
238   if (!isReplicator()) {
239     // Visit the VPBlocks connected to "this", starting from it.
240     for (VPBlockBase *Block : RPOT) {
241       if (EnableVPlanNativePath) {
242         // The inner loop vectorization path does not represent loop preheader
243         // and exit blocks as part of the VPlan. In the VPlan-native path, skip
244         // vectorizing loop preheader block. In future, we may replace this
245         // check with the check for loop preheader.
246         if (Block->getNumPredecessors() == 0)
247           continue;
248 
249         // Skip vectorizing loop exit block. In future, we may replace this
250         // check with the check for loop exit.
251         if (Block->getNumSuccessors() == 0)
252           continue;
253       }
254 
255       LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
256       Block->execute(State);
257     }
258     return;
259   }
260 
261   assert(!State->Instance && "Replicating a Region with non-null instance.");
262 
263   // Enter replicating mode.
264   State->Instance = {0, 0};
265 
266   for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) {
267     State->Instance->Part = Part;
268     for (unsigned Lane = 0, VF = State->VF; Lane < VF; ++Lane) {
269       State->Instance->Lane = Lane;
270       // Visit the VPBlocks connected to \p this, starting from it.
271       for (VPBlockBase *Block : RPOT) {
272         LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
273         Block->execute(State);
274       }
275     }
276   }
277 
278   // Exit replicating mode.
279   State->Instance.reset();
280 }
281 
282 void VPRecipeBase::insertBefore(VPRecipeBase *InsertPos) {
283   Parent = InsertPos->getParent();
284   Parent->getRecipeList().insert(InsertPos->getIterator(), this);
285 }
286 
287 iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
288   return getParent()->getRecipeList().erase(getIterator());
289 }
290 
291 void VPInstruction::generateInstruction(VPTransformState &State,
292                                         unsigned Part) {
293   IRBuilder<> &Builder = State.Builder;
294 
295   if (Instruction::isBinaryOp(getOpcode())) {
296     Value *A = State.get(getOperand(0), Part);
297     Value *B = State.get(getOperand(1), Part);
298     Value *V = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(), A, B);
299     State.set(this, V, Part);
300     return;
301   }
302 
303   switch (getOpcode()) {
304   case VPInstruction::Not: {
305     Value *A = State.get(getOperand(0), Part);
306     Value *V = Builder.CreateNot(A);
307     State.set(this, V, Part);
308     break;
309   }
310   default:
311     llvm_unreachable("Unsupported opcode for instruction");
312   }
313 }
314 
315 void VPInstruction::execute(VPTransformState &State) {
316   assert(!State.Instance && "VPInstruction executing an Instance");
317   for (unsigned Part = 0; Part < State.UF; ++Part)
318     generateInstruction(State, Part);
319 }
320 
321 void VPInstruction::print(raw_ostream &O, const Twine &Indent) const {
322   O << " +\n" << Indent << "\"EMIT ";
323   print(O);
324   O << "\\l\"";
325 }
326 
327 void VPInstruction::print(raw_ostream &O) const {
328   printAsOperand(O);
329   O << " = ";
330 
331   switch (getOpcode()) {
332   case VPInstruction::Not:
333     O << "not";
334     break;
335   default:
336     O << Instruction::getOpcodeName(getOpcode());
337   }
338 
339   for (const VPValue *Operand : operands()) {
340     O << " ";
341     Operand->printAsOperand(O);
342   }
343 }
344 
345 /// Generate the code inside the body of the vectorized loop. Assumes a single
346 /// LoopVectorBody basic-block was created for this. Introduce additional
347 /// basic-blocks as needed, and fill them all.
348 void VPlan::execute(VPTransformState *State) {
349   // 0. Set the reverse mapping from VPValues to Values for code generation.
350   for (auto &Entry : Value2VPValue)
351     State->VPValue2Value[Entry.second] = Entry.first;
352 
353   BasicBlock *VectorPreHeaderBB = State->CFG.PrevBB;
354   BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor();
355   assert(VectorHeaderBB && "Loop preheader does not have a single successor.");
356   BasicBlock *VectorLatchBB = VectorHeaderBB;
357 
358   // 1. Make room to generate basic-blocks inside loop body if needed.
359   VectorLatchBB = VectorHeaderBB->splitBasicBlock(
360       VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch");
361   Loop *L = State->LI->getLoopFor(VectorHeaderBB);
362   L->addBasicBlockToLoop(VectorLatchBB, *State->LI);
363   // Remove the edge between Header and Latch to allow other connections.
364   // Temporarily terminate with unreachable until CFG is rewired.
365   // Note: this asserts the generated code's assumption that
366   // getFirstInsertionPt() can be dereferenced into an Instruction.
367   VectorHeaderBB->getTerminator()->eraseFromParent();
368   State->Builder.SetInsertPoint(VectorHeaderBB);
369   UnreachableInst *Terminator = State->Builder.CreateUnreachable();
370   State->Builder.SetInsertPoint(Terminator);
371 
372   // 2. Generate code in loop body.
373   State->CFG.PrevVPBB = nullptr;
374   State->CFG.PrevBB = VectorHeaderBB;
375   State->CFG.LastBB = VectorLatchBB;
376 
377   for (VPBlockBase *Block : depth_first(Entry))
378     Block->execute(State);
379 
380   // Setup branch terminator successors for VPBBs in VPBBsToFix based on
381   // VPBB's successors.
382   for (auto VPBB : State->CFG.VPBBsToFix) {
383     assert(EnableVPlanNativePath &&
384            "Unexpected VPBBsToFix in non VPlan-native path");
385     BasicBlock *BB = State->CFG.VPBB2IRBB[VPBB];
386     assert(BB && "Unexpected null basic block for VPBB");
387 
388     unsigned Idx = 0;
389     auto *BBTerminator = BB->getTerminator();
390 
391     for (VPBlockBase *SuccVPBlock : VPBB->getHierarchicalSuccessors()) {
392       VPBasicBlock *SuccVPBB = SuccVPBlock->getEntryBasicBlock();
393       BBTerminator->setSuccessor(Idx, State->CFG.VPBB2IRBB[SuccVPBB]);
394       ++Idx;
395     }
396   }
397 
398   // 3. Merge the temporary latch created with the last basic-block filled.
399   BasicBlock *LastBB = State->CFG.PrevBB;
400   // Connect LastBB to VectorLatchBB to facilitate their merge.
401   assert((EnableVPlanNativePath ||
402           isa<UnreachableInst>(LastBB->getTerminator())) &&
403          "Expected InnerLoop VPlan CFG to terminate with unreachable");
404   assert((!EnableVPlanNativePath || isa<BranchInst>(LastBB->getTerminator())) &&
405          "Expected VPlan CFG to terminate with branch in NativePath");
406   LastBB->getTerminator()->eraseFromParent();
407   BranchInst::Create(VectorLatchBB, LastBB);
408 
409   // Merge LastBB with Latch.
410   bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI);
411   (void)Merged;
412   assert(Merged && "Could not merge last basic block with latch.");
413   VectorLatchBB = LastBB;
414 
415   // We do not attempt to preserve DT for outer loop vectorization currently.
416   if (!EnableVPlanNativePath)
417     updateDominatorTree(State->DT, VectorPreHeaderBB, VectorLatchBB);
418 }
419 
420 void VPlan::updateDominatorTree(DominatorTree *DT, BasicBlock *LoopPreHeaderBB,
421                                 BasicBlock *LoopLatchBB) {
422   BasicBlock *LoopHeaderBB = LoopPreHeaderBB->getSingleSuccessor();
423   assert(LoopHeaderBB && "Loop preheader does not have a single successor.");
424   DT->addNewBlock(LoopHeaderBB, LoopPreHeaderBB);
425   // The vector body may be more than a single basic-block by this point.
426   // Update the dominator tree information inside the vector body by propagating
427   // it from header to latch, expecting only triangular control-flow, if any.
428   BasicBlock *PostDomSucc = nullptr;
429   for (auto *BB = LoopHeaderBB; BB != LoopLatchBB; BB = PostDomSucc) {
430     // Get the list of successors of this block.
431     std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB));
432     assert(Succs.size() <= 2 &&
433            "Basic block in vector loop has more than 2 successors.");
434     PostDomSucc = Succs[0];
435     if (Succs.size() == 1) {
436       assert(PostDomSucc->getSinglePredecessor() &&
437              "PostDom successor has more than one predecessor.");
438       DT->addNewBlock(PostDomSucc, BB);
439       continue;
440     }
441     BasicBlock *InterimSucc = Succs[1];
442     if (PostDomSucc->getSingleSuccessor() == InterimSucc) {
443       PostDomSucc = Succs[1];
444       InterimSucc = Succs[0];
445     }
446     assert(InterimSucc->getSingleSuccessor() == PostDomSucc &&
447            "One successor of a basic block does not lead to the other.");
448     assert(InterimSucc->getSinglePredecessor() &&
449            "Interim successor has more than one predecessor.");
450     assert(pred_size(PostDomSucc) == 2 &&
451            "PostDom successor has more than two predecessors.");
452     DT->addNewBlock(InterimSucc, BB);
453     DT->addNewBlock(PostDomSucc, BB);
454   }
455 }
456 
457 const Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
458   return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") +
459          Twine(getOrCreateBID(Block));
460 }
461 
462 const Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) {
463   const std::string &Name = Block->getName();
464   if (!Name.empty())
465     return Name;
466   return "VPB" + Twine(getOrCreateBID(Block));
467 }
468 
469 void VPlanPrinter::dump() {
470   Depth = 1;
471   bumpIndent(0);
472   OS << "digraph VPlan {\n";
473   OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
474   if (!Plan.getName().empty())
475     OS << "\\n" << DOT::EscapeString(Plan.getName());
476   if (!Plan.Value2VPValue.empty()) {
477     OS << ", where:";
478     for (auto Entry : Plan.Value2VPValue) {
479       OS << "\\n" << *Entry.second;
480       OS << DOT::EscapeString(" := ");
481       Entry.first->printAsOperand(OS, false);
482     }
483   }
484   OS << "\"]\n";
485   OS << "node [shape=rect, fontname=Courier, fontsize=30]\n";
486   OS << "edge [fontname=Courier, fontsize=30]\n";
487   OS << "compound=true\n";
488 
489   for (VPBlockBase *Block : depth_first(Plan.getEntry()))
490     dumpBlock(Block);
491 
492   OS << "}\n";
493 }
494 
495 void VPlanPrinter::dumpBlock(const VPBlockBase *Block) {
496   if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block))
497     dumpBasicBlock(BasicBlock);
498   else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
499     dumpRegion(Region);
500   else
501     llvm_unreachable("Unsupported kind of VPBlock.");
502 }
503 
504 void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To,
505                             bool Hidden, const Twine &Label) {
506   // Due to "dot" we print an edge between two regions as an edge between the
507   // exit basic block and the entry basic of the respective regions.
508   const VPBlockBase *Tail = From->getExitBasicBlock();
509   const VPBlockBase *Head = To->getEntryBasicBlock();
510   OS << Indent << getUID(Tail) << " -> " << getUID(Head);
511   OS << " [ label=\"" << Label << '\"';
512   if (Tail != From)
513     OS << " ltail=" << getUID(From);
514   if (Head != To)
515     OS << " lhead=" << getUID(To);
516   if (Hidden)
517     OS << "; splines=none";
518   OS << "]\n";
519 }
520 
521 void VPlanPrinter::dumpEdges(const VPBlockBase *Block) {
522   auto &Successors = Block->getSuccessors();
523   if (Successors.size() == 1)
524     drawEdge(Block, Successors.front(), false, "");
525   else if (Successors.size() == 2) {
526     drawEdge(Block, Successors.front(), false, "T");
527     drawEdge(Block, Successors.back(), false, "F");
528   } else {
529     unsigned SuccessorNumber = 0;
530     for (auto *Successor : Successors)
531       drawEdge(Block, Successor, false, Twine(SuccessorNumber++));
532   }
533 }
534 
535 void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) {
536   OS << Indent << getUID(BasicBlock) << " [label =\n";
537   bumpIndent(1);
538   OS << Indent << "\"" << DOT::EscapeString(BasicBlock->getName()) << ":\\n\"";
539   bumpIndent(1);
540   for (const VPRecipeBase &Recipe : *BasicBlock)
541     Recipe.print(OS, Indent);
542 
543   // Dump the condition bit.
544   const VPValue *CBV = BasicBlock->getCondBit();
545   if (CBV) {
546     OS << " +\n" << Indent << " \"CondBit: ";
547     if (const VPInstruction *CBI = dyn_cast<VPInstruction>(CBV)) {
548       CBI->printAsOperand(OS);
549       OS << " (" << DOT::EscapeString(CBI->getParent()->getName()) << ")\\l\"";
550     } else
551       CBV->printAsOperand(OS);
552   }
553 
554   bumpIndent(-2);
555   OS << "\n" << Indent << "]\n";
556   dumpEdges(BasicBlock);
557 }
558 
559 void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
560   OS << Indent << "subgraph " << getUID(Region) << " {\n";
561   bumpIndent(1);
562   OS << Indent << "fontname=Courier\n"
563      << Indent << "label=\""
564      << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ")
565      << DOT::EscapeString(Region->getName()) << "\"\n";
566   // Dump the blocks of the region.
567   assert(Region->getEntry() && "Region contains no inner blocks.");
568   for (const VPBlockBase *Block : depth_first(Region->getEntry()))
569     dumpBlock(Block);
570   bumpIndent(-1);
571   OS << Indent << "}\n";
572   dumpEdges(Region);
573 }
574 
575 void VPlanPrinter::printAsIngredient(raw_ostream &O, Value *V) {
576   std::string IngredientString;
577   raw_string_ostream RSO(IngredientString);
578   if (auto *Inst = dyn_cast<Instruction>(V)) {
579     if (!Inst->getType()->isVoidTy()) {
580       Inst->printAsOperand(RSO, false);
581       RSO << " = ";
582     }
583     RSO << Inst->getOpcodeName() << " ";
584     unsigned E = Inst->getNumOperands();
585     if (E > 0) {
586       Inst->getOperand(0)->printAsOperand(RSO, false);
587       for (unsigned I = 1; I < E; ++I)
588         Inst->getOperand(I)->printAsOperand(RSO << ", ", false);
589     }
590   } else // !Inst
591     V->printAsOperand(RSO, false);
592   RSO.flush();
593   O << DOT::EscapeString(IngredientString);
594 }
595 
596 void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent) const {
597   O << " +\n" << Indent << "\"WIDEN\\l\"";
598   for (auto &Instr : make_range(Begin, End))
599     O << " +\n" << Indent << "\"  " << VPlanIngredient(&Instr) << "\\l\"";
600 }
601 
602 void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O,
603                                           const Twine &Indent) const {
604   O << " +\n" << Indent << "\"WIDEN-INDUCTION";
605   if (Trunc) {
606     O << "\\l\"";
607     O << " +\n" << Indent << "\"  " << VPlanIngredient(IV) << "\\l\"";
608     O << " +\n" << Indent << "\"  " << VPlanIngredient(Trunc) << "\\l\"";
609   } else
610     O << " " << VPlanIngredient(IV) << "\\l\"";
611 }
612 
613 void VPWidenPHIRecipe::print(raw_ostream &O, const Twine &Indent) const {
614   O << " +\n" << Indent << "\"WIDEN-PHI " << VPlanIngredient(Phi) << "\\l\"";
615 }
616 
617 void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent) const {
618   O << " +\n" << Indent << "\"BLEND ";
619   Phi->printAsOperand(O, false);
620   O << " =";
621   if (!User) {
622     // Not a User of any mask: not really blending, this is a
623     // single-predecessor phi.
624     O << " ";
625     Phi->getIncomingValue(0)->printAsOperand(O, false);
626   } else {
627     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
628       O << " ";
629       Phi->getIncomingValue(I)->printAsOperand(O, false);
630       O << "/";
631       User->getOperand(I)->printAsOperand(O);
632     }
633   }
634   O << "\\l\"";
635 }
636 
637 void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent) const {
638   O << " +\n"
639     << Indent << "\"" << (IsUniform ? "CLONE " : "REPLICATE ")
640     << VPlanIngredient(Ingredient);
641   if (AlsoPack)
642     O << " (S->V)";
643   O << "\\l\"";
644 }
645 
646 void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent) const {
647   O << " +\n"
648     << Indent << "\"PHI-PREDICATED-INSTRUCTION " << VPlanIngredient(PredInst)
649     << "\\l\"";
650 }
651 
652 void VPWidenMemoryInstructionRecipe::print(raw_ostream &O,
653                                            const Twine &Indent) const {
654   O << " +\n" << Indent << "\"WIDEN " << VPlanIngredient(&Instr);
655   if (User) {
656     O << ", ";
657     User->getOperand(0)->printAsOperand(O);
658   }
659   O << "\\l\"";
660 }
661 
662 template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
663