1 //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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
17 //===----------------------------------------------------------------------===//
20 #include "VPlanDominatorTree.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/PostOrderIterator.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/CFG.h"
28 #include "llvm/IR/InstrTypes.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/IR/Value.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/GenericDomTreeConstruction.h"
37 #include "llvm/Support/GraphWriter.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
46 extern cl::opt
<bool> EnableVPlanNativePath
;
48 #define DEBUG_TYPE "vplan"
50 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, const VPValue
&V
) {
51 if (const VPInstruction
*Instr
= dyn_cast
<VPInstruction
>(&V
))
58 /// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
59 const VPBasicBlock
*VPBlockBase::getEntryBasicBlock() const {
60 const VPBlockBase
*Block
= this;
61 while (const VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
62 Block
= Region
->getEntry();
63 return cast
<VPBasicBlock
>(Block
);
66 VPBasicBlock
*VPBlockBase::getEntryBasicBlock() {
67 VPBlockBase
*Block
= this;
68 while (VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
69 Block
= Region
->getEntry();
70 return cast
<VPBasicBlock
>(Block
);
73 /// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
74 const VPBasicBlock
*VPBlockBase::getExitBasicBlock() const {
75 const VPBlockBase
*Block
= this;
76 while (const VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
77 Block
= Region
->getExit();
78 return cast
<VPBasicBlock
>(Block
);
81 VPBasicBlock
*VPBlockBase::getExitBasicBlock() {
82 VPBlockBase
*Block
= this;
83 while (VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
84 Block
= Region
->getExit();
85 return cast
<VPBasicBlock
>(Block
);
88 VPBlockBase
*VPBlockBase::getEnclosingBlockWithSuccessors() {
89 if (!Successors
.empty() || !Parent
)
91 assert(Parent
->getExit() == this &&
92 "Block w/o successors not the exit of its parent.");
93 return Parent
->getEnclosingBlockWithSuccessors();
96 VPBlockBase
*VPBlockBase::getEnclosingBlockWithPredecessors() {
97 if (!Predecessors
.empty() || !Parent
)
99 assert(Parent
->getEntry() == this &&
100 "Block w/o predecessors not the entry of its parent.");
101 return Parent
->getEnclosingBlockWithPredecessors();
104 void VPBlockBase::deleteCFG(VPBlockBase
*Entry
) {
105 SmallVector
<VPBlockBase
*, 8> Blocks
;
106 for (VPBlockBase
*Block
: depth_first(Entry
))
107 Blocks
.push_back(Block
);
109 for (VPBlockBase
*Block
: Blocks
)
114 VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState
&CFG
) {
115 // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
116 // Pred stands for Predessor. Prev stands for Previous - last visited/created.
117 BasicBlock
*PrevBB
= CFG
.PrevBB
;
118 BasicBlock
*NewBB
= BasicBlock::Create(PrevBB
->getContext(), getName(),
119 PrevBB
->getParent(), CFG
.LastBB
);
120 LLVM_DEBUG(dbgs() << "LV: created " << NewBB
->getName() << '\n');
122 // Hook up the new basic block to its predecessors.
123 for (VPBlockBase
*PredVPBlock
: getHierarchicalPredecessors()) {
124 VPBasicBlock
*PredVPBB
= PredVPBlock
->getExitBasicBlock();
125 auto &PredVPSuccessors
= PredVPBB
->getSuccessors();
126 BasicBlock
*PredBB
= CFG
.VPBB2IRBB
[PredVPBB
];
128 // In outer loop vectorization scenario, the predecessor BBlock may not yet
129 // be visited(backedge). Mark the VPBasicBlock for fixup at the end of
130 // vectorization. We do not encounter this case in inner loop vectorization
131 // as we start out by building a loop skeleton with the vector loop header
132 // and latch blocks. As a result, we never enter this function for the
133 // header block in the non VPlan-native path.
135 assert(EnableVPlanNativePath
&&
136 "Unexpected null predecessor in non VPlan-native path");
137 CFG
.VPBBsToFix
.push_back(PredVPBB
);
141 assert(PredBB
&& "Predecessor basic-block not found building successor.");
142 auto *PredBBTerminator
= PredBB
->getTerminator();
143 LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB
->getName() << '\n');
144 if (isa
<UnreachableInst
>(PredBBTerminator
)) {
145 assert(PredVPSuccessors
.size() == 1 &&
146 "Predecessor ending w/o branch must have single successor.");
147 PredBBTerminator
->eraseFromParent();
148 BranchInst::Create(NewBB
, PredBB
);
150 assert(PredVPSuccessors
.size() == 2 &&
151 "Predecessor ending with branch must have two successors.");
152 unsigned idx
= PredVPSuccessors
.front() == this ? 0 : 1;
153 assert(!PredBBTerminator
->getSuccessor(idx
) &&
154 "Trying to reset an existing successor block.");
155 PredBBTerminator
->setSuccessor(idx
, NewBB
);
161 void VPBasicBlock::execute(VPTransformState
*State
) {
162 bool Replica
= State
->Instance
&&
163 !(State
->Instance
->Part
== 0 && State
->Instance
->Lane
== 0);
164 VPBasicBlock
*PrevVPBB
= State
->CFG
.PrevVPBB
;
165 VPBlockBase
*SingleHPred
= nullptr;
166 BasicBlock
*NewBB
= State
->CFG
.PrevBB
; // Reuse it if possible.
168 // 1. Create an IR basic block, or reuse the last one if possible.
169 // The last IR basic block is reused, as an optimization, in three cases:
170 // A. the first VPBB reuses the loop header BB - when PrevVPBB is null;
171 // B. when the current VPBB has a single (hierarchical) predecessor which
172 // is PrevVPBB and the latter has a single (hierarchical) successor; and
173 // C. when the current VPBB is an entry of a region replica - where PrevVPBB
174 // is the exit of this region from a previous instance, or the predecessor
176 if (PrevVPBB
&& /* A */
177 !((SingleHPred
= getSingleHierarchicalPredecessor()) &&
178 SingleHPred
->getExitBasicBlock() == PrevVPBB
&&
179 PrevVPBB
->getSingleHierarchicalSuccessor()) && /* B */
180 !(Replica
&& getPredecessors().empty())) { /* C */
181 NewBB
= createEmptyBasicBlock(State
->CFG
);
182 State
->Builder
.SetInsertPoint(NewBB
);
183 // Temporarily terminate with unreachable until CFG is rewired.
184 UnreachableInst
*Terminator
= State
->Builder
.CreateUnreachable();
185 State
->Builder
.SetInsertPoint(Terminator
);
186 // Register NewBB in its loop. In innermost loops its the same for all BB's.
187 Loop
*L
= State
->LI
->getLoopFor(State
->CFG
.LastBB
);
188 L
->addBasicBlockToLoop(NewBB
, *State
->LI
);
189 State
->CFG
.PrevBB
= NewBB
;
192 // 2. Fill the IR basic block with IR instructions.
193 LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName()
194 << " in BB:" << NewBB
->getName() << '\n');
196 State
->CFG
.VPBB2IRBB
[this] = NewBB
;
197 State
->CFG
.PrevVPBB
= this;
199 for (VPRecipeBase
&Recipe
: Recipes
)
200 Recipe
.execute(*State
);
203 if (EnableVPlanNativePath
&& (CBV
= getCondBit())) {
204 Value
*IRCBV
= CBV
->getUnderlyingValue();
205 assert(IRCBV
&& "Unexpected null underlying value for condition bit");
207 // Condition bit value in a VPBasicBlock is used as the branch selector. In
208 // the VPlan-native path case, since all branches are uniform we generate a
209 // branch instruction using the condition value from vector lane 0 and dummy
210 // successors. The successors are fixed later when the successor blocks are
212 Value
*NewCond
= State
->Callback
.getOrCreateVectorValues(IRCBV
, 0);
213 NewCond
= State
->Builder
.CreateExtractElement(NewCond
,
214 State
->Builder
.getInt32(0));
216 // Replace the temporary unreachable terminator with the new conditional
218 auto *CurrentTerminator
= NewBB
->getTerminator();
219 assert(isa
<UnreachableInst
>(CurrentTerminator
) &&
220 "Expected to replace unreachable terminator with conditional "
222 auto *CondBr
= BranchInst::Create(NewBB
, nullptr, NewCond
);
223 CondBr
->setSuccessor(0, nullptr);
224 ReplaceInstWithInst(CurrentTerminator
, CondBr
);
227 LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB
);
230 void VPRegionBlock::execute(VPTransformState
*State
) {
231 ReversePostOrderTraversal
<VPBlockBase
*> RPOT(Entry
);
233 if (!isReplicator()) {
234 // Visit the VPBlocks connected to "this", starting from it.
235 for (VPBlockBase
*Block
: RPOT
) {
236 if (EnableVPlanNativePath
) {
237 // The inner loop vectorization path does not represent loop preheader
238 // and exit blocks as part of the VPlan. In the VPlan-native path, skip
239 // vectorizing loop preheader block. In future, we may replace this
240 // check with the check for loop preheader.
241 if (Block
->getNumPredecessors() == 0)
244 // Skip vectorizing loop exit block. In future, we may replace this
245 // check with the check for loop exit.
246 if (Block
->getNumSuccessors() == 0)
250 LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block
->getName() << '\n');
251 Block
->execute(State
);
256 assert(!State
->Instance
&& "Replicating a Region with non-null instance.");
258 // Enter replicating mode.
259 State
->Instance
= {0, 0};
261 for (unsigned Part
= 0, UF
= State
->UF
; Part
< UF
; ++Part
) {
262 State
->Instance
->Part
= Part
;
263 for (unsigned Lane
= 0, VF
= State
->VF
; Lane
< VF
; ++Lane
) {
264 State
->Instance
->Lane
= Lane
;
265 // Visit the VPBlocks connected to \p this, starting from it.
266 for (VPBlockBase
*Block
: RPOT
) {
267 LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block
->getName() << '\n');
268 Block
->execute(State
);
273 // Exit replicating mode.
274 State
->Instance
.reset();
277 void VPRecipeBase::insertBefore(VPRecipeBase
*InsertPos
) {
278 Parent
= InsertPos
->getParent();
279 Parent
->getRecipeList().insert(InsertPos
->getIterator(), this);
282 iplist
<VPRecipeBase
>::iterator
VPRecipeBase::eraseFromParent() {
283 return getParent()->getRecipeList().erase(getIterator());
286 void VPRecipeBase::moveAfter(VPRecipeBase
*InsertPos
) {
287 InsertPos
->getParent()->getRecipeList().splice(
288 std::next(InsertPos
->getIterator()), getParent()->getRecipeList(),
292 void VPInstruction::generateInstruction(VPTransformState
&State
,
294 IRBuilder
<> &Builder
= State
.Builder
;
296 if (Instruction::isBinaryOp(getOpcode())) {
297 Value
*A
= State
.get(getOperand(0), Part
);
298 Value
*B
= State
.get(getOperand(1), Part
);
299 Value
*V
= Builder
.CreateBinOp((Instruction::BinaryOps
)getOpcode(), A
, B
);
300 State
.set(this, V
, Part
);
304 switch (getOpcode()) {
305 case VPInstruction::Not
: {
306 Value
*A
= State
.get(getOperand(0), Part
);
307 Value
*V
= Builder
.CreateNot(A
);
308 State
.set(this, V
, Part
);
311 case VPInstruction::ICmpULE
: {
312 Value
*IV
= State
.get(getOperand(0), Part
);
313 Value
*TC
= State
.get(getOperand(1), Part
);
314 Value
*V
= Builder
.CreateICmpULE(IV
, TC
);
315 State
.set(this, V
, Part
);
318 case Instruction::Select
: {
319 Value
*Cond
= State
.get(getOperand(0), Part
);
320 Value
*Op1
= State
.get(getOperand(1), Part
);
321 Value
*Op2
= State
.get(getOperand(2), Part
);
322 Value
*V
= Builder
.CreateSelect(Cond
, Op1
, Op2
);
323 State
.set(this, V
, Part
);
327 llvm_unreachable("Unsupported opcode for instruction");
331 void VPInstruction::execute(VPTransformState
&State
) {
332 assert(!State
.Instance
&& "VPInstruction executing an Instance");
333 for (unsigned Part
= 0; Part
< State
.UF
; ++Part
)
334 generateInstruction(State
, Part
);
337 void VPInstruction::print(raw_ostream
&O
, const Twine
&Indent
) const {
338 O
<< " +\n" << Indent
<< "\"EMIT ";
343 void VPInstruction::print(raw_ostream
&O
) const {
347 switch (getOpcode()) {
348 case VPInstruction::Not
:
351 case VPInstruction::ICmpULE
:
354 case VPInstruction::SLPLoad
:
355 O
<< "combined load";
357 case VPInstruction::SLPStore
:
358 O
<< "combined store";
361 O
<< Instruction::getOpcodeName(getOpcode());
364 for (const VPValue
*Operand
: operands()) {
366 Operand
->printAsOperand(O
);
370 /// Generate the code inside the body of the vectorized loop. Assumes a single
371 /// LoopVectorBody basic-block was created for this. Introduce additional
372 /// basic-blocks as needed, and fill them all.
373 void VPlan::execute(VPTransformState
*State
) {
374 // -1. Check if the backedge taken count is needed, and if so build it.
375 if (BackedgeTakenCount
&& BackedgeTakenCount
->getNumUsers()) {
376 Value
*TC
= State
->TripCount
;
377 IRBuilder
<> Builder(State
->CFG
.PrevBB
->getTerminator());
378 auto *TCMO
= Builder
.CreateSub(TC
, ConstantInt::get(TC
->getType(), 1),
379 "trip.count.minus.1");
380 Value2VPValue
[TCMO
] = BackedgeTakenCount
;
383 // 0. Set the reverse mapping from VPValues to Values for code generation.
384 for (auto &Entry
: Value2VPValue
)
385 State
->VPValue2Value
[Entry
.second
] = Entry
.first
;
387 BasicBlock
*VectorPreHeaderBB
= State
->CFG
.PrevBB
;
388 BasicBlock
*VectorHeaderBB
= VectorPreHeaderBB
->getSingleSuccessor();
389 assert(VectorHeaderBB
&& "Loop preheader does not have a single successor.");
391 // 1. Make room to generate basic-blocks inside loop body if needed.
392 BasicBlock
*VectorLatchBB
= VectorHeaderBB
->splitBasicBlock(
393 VectorHeaderBB
->getFirstInsertionPt(), "vector.body.latch");
394 Loop
*L
= State
->LI
->getLoopFor(VectorHeaderBB
);
395 L
->addBasicBlockToLoop(VectorLatchBB
, *State
->LI
);
396 // Remove the edge between Header and Latch to allow other connections.
397 // Temporarily terminate with unreachable until CFG is rewired.
398 // Note: this asserts the generated code's assumption that
399 // getFirstInsertionPt() can be dereferenced into an Instruction.
400 VectorHeaderBB
->getTerminator()->eraseFromParent();
401 State
->Builder
.SetInsertPoint(VectorHeaderBB
);
402 UnreachableInst
*Terminator
= State
->Builder
.CreateUnreachable();
403 State
->Builder
.SetInsertPoint(Terminator
);
405 // 2. Generate code in loop body.
406 State
->CFG
.PrevVPBB
= nullptr;
407 State
->CFG
.PrevBB
= VectorHeaderBB
;
408 State
->CFG
.LastBB
= VectorLatchBB
;
410 for (VPBlockBase
*Block
: depth_first(Entry
))
411 Block
->execute(State
);
413 // Setup branch terminator successors for VPBBs in VPBBsToFix based on
414 // VPBB's successors.
415 for (auto VPBB
: State
->CFG
.VPBBsToFix
) {
416 assert(EnableVPlanNativePath
&&
417 "Unexpected VPBBsToFix in non VPlan-native path");
418 BasicBlock
*BB
= State
->CFG
.VPBB2IRBB
[VPBB
];
419 assert(BB
&& "Unexpected null basic block for VPBB");
422 auto *BBTerminator
= BB
->getTerminator();
424 for (VPBlockBase
*SuccVPBlock
: VPBB
->getHierarchicalSuccessors()) {
425 VPBasicBlock
*SuccVPBB
= SuccVPBlock
->getEntryBasicBlock();
426 BBTerminator
->setSuccessor(Idx
, State
->CFG
.VPBB2IRBB
[SuccVPBB
]);
431 // 3. Merge the temporary latch created with the last basic-block filled.
432 BasicBlock
*LastBB
= State
->CFG
.PrevBB
;
433 // Connect LastBB to VectorLatchBB to facilitate their merge.
434 assert((EnableVPlanNativePath
||
435 isa
<UnreachableInst
>(LastBB
->getTerminator())) &&
436 "Expected InnerLoop VPlan CFG to terminate with unreachable");
437 assert((!EnableVPlanNativePath
|| isa
<BranchInst
>(LastBB
->getTerminator())) &&
438 "Expected VPlan CFG to terminate with branch in NativePath");
439 LastBB
->getTerminator()->eraseFromParent();
440 BranchInst::Create(VectorLatchBB
, LastBB
);
442 // Merge LastBB with Latch.
443 bool Merged
= MergeBlockIntoPredecessor(VectorLatchBB
, nullptr, State
->LI
);
445 assert(Merged
&& "Could not merge last basic block with latch.");
446 VectorLatchBB
= LastBB
;
448 // We do not attempt to preserve DT for outer loop vectorization currently.
449 if (!EnableVPlanNativePath
)
450 updateDominatorTree(State
->DT
, VectorPreHeaderBB
, VectorLatchBB
);
453 void VPlan::updateDominatorTree(DominatorTree
*DT
, BasicBlock
*LoopPreHeaderBB
,
454 BasicBlock
*LoopLatchBB
) {
455 BasicBlock
*LoopHeaderBB
= LoopPreHeaderBB
->getSingleSuccessor();
456 assert(LoopHeaderBB
&& "Loop preheader does not have a single successor.");
457 DT
->addNewBlock(LoopHeaderBB
, LoopPreHeaderBB
);
458 // The vector body may be more than a single basic-block by this point.
459 // Update the dominator tree information inside the vector body by propagating
460 // it from header to latch, expecting only triangular control-flow, if any.
461 BasicBlock
*PostDomSucc
= nullptr;
462 for (auto *BB
= LoopHeaderBB
; BB
!= LoopLatchBB
; BB
= PostDomSucc
) {
463 // Get the list of successors of this block.
464 std::vector
<BasicBlock
*> Succs(succ_begin(BB
), succ_end(BB
));
465 assert(Succs
.size() <= 2 &&
466 "Basic block in vector loop has more than 2 successors.");
467 PostDomSucc
= Succs
[0];
468 if (Succs
.size() == 1) {
469 assert(PostDomSucc
->getSinglePredecessor() &&
470 "PostDom successor has more than one predecessor.");
471 DT
->addNewBlock(PostDomSucc
, BB
);
474 BasicBlock
*InterimSucc
= Succs
[1];
475 if (PostDomSucc
->getSingleSuccessor() == InterimSucc
) {
476 PostDomSucc
= Succs
[1];
477 InterimSucc
= Succs
[0];
479 assert(InterimSucc
->getSingleSuccessor() == PostDomSucc
&&
480 "One successor of a basic block does not lead to the other.");
481 assert(InterimSucc
->getSinglePredecessor() &&
482 "Interim successor has more than one predecessor.");
483 assert(PostDomSucc
->hasNPredecessors(2) &&
484 "PostDom successor has more than two predecessors.");
485 DT
->addNewBlock(InterimSucc
, BB
);
486 DT
->addNewBlock(PostDomSucc
, BB
);
490 const Twine
VPlanPrinter::getUID(const VPBlockBase
*Block
) {
491 return (isa
<VPRegionBlock
>(Block
) ? "cluster_N" : "N") +
492 Twine(getOrCreateBID(Block
));
495 const Twine
VPlanPrinter::getOrCreateName(const VPBlockBase
*Block
) {
496 const std::string
&Name
= Block
->getName();
499 return "VPB" + Twine(getOrCreateBID(Block
));
502 void VPlanPrinter::dump() {
505 OS
<< "digraph VPlan {\n";
506 OS
<< "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
507 if (!Plan
.getName().empty())
508 OS
<< "\\n" << DOT::EscapeString(Plan
.getName());
509 if (!Plan
.Value2VPValue
.empty() || Plan
.BackedgeTakenCount
) {
511 if (Plan
.BackedgeTakenCount
)
513 << *Plan
.getOrCreateBackedgeTakenCount() << " := BackedgeTakenCount";
514 for (auto Entry
: Plan
.Value2VPValue
) {
515 OS
<< "\\n" << *Entry
.second
;
516 OS
<< DOT::EscapeString(" := ");
517 Entry
.first
->printAsOperand(OS
, false);
521 OS
<< "node [shape=rect, fontname=Courier, fontsize=30]\n";
522 OS
<< "edge [fontname=Courier, fontsize=30]\n";
523 OS
<< "compound=true\n";
525 for (VPBlockBase
*Block
: depth_first(Plan
.getEntry()))
531 void VPlanPrinter::dumpBlock(const VPBlockBase
*Block
) {
532 if (const VPBasicBlock
*BasicBlock
= dyn_cast
<VPBasicBlock
>(Block
))
533 dumpBasicBlock(BasicBlock
);
534 else if (const VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
537 llvm_unreachable("Unsupported kind of VPBlock.");
540 void VPlanPrinter::drawEdge(const VPBlockBase
*From
, const VPBlockBase
*To
,
541 bool Hidden
, const Twine
&Label
) {
542 // Due to "dot" we print an edge between two regions as an edge between the
543 // exit basic block and the entry basic of the respective regions.
544 const VPBlockBase
*Tail
= From
->getExitBasicBlock();
545 const VPBlockBase
*Head
= To
->getEntryBasicBlock();
546 OS
<< Indent
<< getUID(Tail
) << " -> " << getUID(Head
);
547 OS
<< " [ label=\"" << Label
<< '\"';
549 OS
<< " ltail=" << getUID(From
);
551 OS
<< " lhead=" << getUID(To
);
553 OS
<< "; splines=none";
557 void VPlanPrinter::dumpEdges(const VPBlockBase
*Block
) {
558 auto &Successors
= Block
->getSuccessors();
559 if (Successors
.size() == 1)
560 drawEdge(Block
, Successors
.front(), false, "");
561 else if (Successors
.size() == 2) {
562 drawEdge(Block
, Successors
.front(), false, "T");
563 drawEdge(Block
, Successors
.back(), false, "F");
565 unsigned SuccessorNumber
= 0;
566 for (auto *Successor
: Successors
)
567 drawEdge(Block
, Successor
, false, Twine(SuccessorNumber
++));
571 void VPlanPrinter::dumpBasicBlock(const VPBasicBlock
*BasicBlock
) {
572 OS
<< Indent
<< getUID(BasicBlock
) << " [label =\n";
574 OS
<< Indent
<< "\"" << DOT::EscapeString(BasicBlock
->getName()) << ":\\n\"";
577 // Dump the block predicate.
578 const VPValue
*Pred
= BasicBlock
->getPredicate();
580 OS
<< " +\n" << Indent
<< " \"BlockPredicate: ";
581 if (const VPInstruction
*PredI
= dyn_cast
<VPInstruction
>(Pred
)) {
582 PredI
->printAsOperand(OS
);
583 OS
<< " (" << DOT::EscapeString(PredI
->getParent()->getName())
586 Pred
->printAsOperand(OS
);
589 for (const VPRecipeBase
&Recipe
: *BasicBlock
)
590 Recipe
.print(OS
, Indent
);
592 // Dump the condition bit.
593 const VPValue
*CBV
= BasicBlock
->getCondBit();
595 OS
<< " +\n" << Indent
<< " \"CondBit: ";
596 if (const VPInstruction
*CBI
= dyn_cast
<VPInstruction
>(CBV
)) {
597 CBI
->printAsOperand(OS
);
598 OS
<< " (" << DOT::EscapeString(CBI
->getParent()->getName()) << ")\\l\"";
600 CBV
->printAsOperand(OS
);
606 OS
<< "\n" << Indent
<< "]\n";
607 dumpEdges(BasicBlock
);
610 void VPlanPrinter::dumpRegion(const VPRegionBlock
*Region
) {
611 OS
<< Indent
<< "subgraph " << getUID(Region
) << " {\n";
613 OS
<< Indent
<< "fontname=Courier\n"
614 << Indent
<< "label=\""
615 << DOT::EscapeString(Region
->isReplicator() ? "<xVFxUF> " : "<x1> ")
616 << DOT::EscapeString(Region
->getName()) << "\"\n";
617 // Dump the blocks of the region.
618 assert(Region
->getEntry() && "Region contains no inner blocks.");
619 for (const VPBlockBase
*Block
: depth_first(Region
->getEntry()))
622 OS
<< Indent
<< "}\n";
626 void VPlanPrinter::printAsIngredient(raw_ostream
&O
, Value
*V
) {
627 std::string IngredientString
;
628 raw_string_ostream
RSO(IngredientString
);
629 if (auto *Inst
= dyn_cast
<Instruction
>(V
)) {
630 if (!Inst
->getType()->isVoidTy()) {
631 Inst
->printAsOperand(RSO
, false);
634 RSO
<< Inst
->getOpcodeName() << " ";
635 unsigned E
= Inst
->getNumOperands();
637 Inst
->getOperand(0)->printAsOperand(RSO
, false);
638 for (unsigned I
= 1; I
< E
; ++I
)
639 Inst
->getOperand(I
)->printAsOperand(RSO
<< ", ", false);
642 V
->printAsOperand(RSO
, false);
644 O
<< DOT::EscapeString(IngredientString
);
647 void VPWidenRecipe::print(raw_ostream
&O
, const Twine
&Indent
) const {
648 O
<< " +\n" << Indent
<< "\"WIDEN\\l\"";
649 for (auto &Instr
: make_range(Begin
, End
))
650 O
<< " +\n" << Indent
<< "\" " << VPlanIngredient(&Instr
) << "\\l\"";
653 void VPWidenIntOrFpInductionRecipe::print(raw_ostream
&O
,
654 const Twine
&Indent
) const {
655 O
<< " +\n" << Indent
<< "\"WIDEN-INDUCTION";
658 O
<< " +\n" << Indent
<< "\" " << VPlanIngredient(IV
) << "\\l\"";
659 O
<< " +\n" << Indent
<< "\" " << VPlanIngredient(Trunc
) << "\\l\"";
661 O
<< " " << VPlanIngredient(IV
) << "\\l\"";
664 void VPWidenPHIRecipe::print(raw_ostream
&O
, const Twine
&Indent
) const {
665 O
<< " +\n" << Indent
<< "\"WIDEN-PHI " << VPlanIngredient(Phi
) << "\\l\"";
668 void VPBlendRecipe::print(raw_ostream
&O
, const Twine
&Indent
) const {
669 O
<< " +\n" << Indent
<< "\"BLEND ";
670 Phi
->printAsOperand(O
, false);
673 // Not a User of any mask: not really blending, this is a
674 // single-predecessor phi.
676 Phi
->getIncomingValue(0)->printAsOperand(O
, false);
678 for (unsigned I
= 0, E
= User
->getNumOperands(); I
< E
; ++I
) {
680 Phi
->getIncomingValue(I
)->printAsOperand(O
, false);
682 User
->getOperand(I
)->printAsOperand(O
);
688 void VPReplicateRecipe::print(raw_ostream
&O
, const Twine
&Indent
) const {
690 << Indent
<< "\"" << (IsUniform
? "CLONE " : "REPLICATE ")
691 << VPlanIngredient(Ingredient
);
697 void VPPredInstPHIRecipe::print(raw_ostream
&O
, const Twine
&Indent
) const {
699 << Indent
<< "\"PHI-PREDICATED-INSTRUCTION " << VPlanIngredient(PredInst
)
703 void VPWidenMemoryInstructionRecipe::print(raw_ostream
&O
,
704 const Twine
&Indent
) const {
705 O
<< " +\n" << Indent
<< "\"WIDEN " << VPlanIngredient(&Instr
);
708 User
->getOperand(0)->printAsOperand(O
);
713 template void DomTreeBuilder::Calculate
<VPDominatorTree
>(VPDominatorTree
&DT
);
715 void VPValue::replaceAllUsesWith(VPValue
*New
) {
716 for (VPUser
*User
: users())
717 for (unsigned I
= 0, E
= User
->getNumOperands(); I
< E
; ++I
)
718 if (User
->getOperand(I
) == this)
719 User
->setOperand(I
, New
);
722 void VPInterleavedAccessInfo::visitRegion(VPRegionBlock
*Region
,
724 InterleavedAccessInfo
&IAI
) {
725 ReversePostOrderTraversal
<VPBlockBase
*> RPOT(Region
->getEntry());
726 for (VPBlockBase
*Base
: RPOT
) {
727 visitBlock(Base
, Old2New
, IAI
);
731 void VPInterleavedAccessInfo::visitBlock(VPBlockBase
*Block
, Old2NewTy
&Old2New
,
732 InterleavedAccessInfo
&IAI
) {
733 if (VPBasicBlock
*VPBB
= dyn_cast
<VPBasicBlock
>(Block
)) {
734 for (VPRecipeBase
&VPI
: *VPBB
) {
735 assert(isa
<VPInstruction
>(&VPI
) && "Can only handle VPInstructions");
736 auto *VPInst
= cast
<VPInstruction
>(&VPI
);
737 auto *Inst
= cast
<Instruction
>(VPInst
->getUnderlyingValue());
738 auto *IG
= IAI
.getInterleaveGroup(Inst
);
742 auto NewIGIter
= Old2New
.find(IG
);
743 if (NewIGIter
== Old2New
.end())
744 Old2New
[IG
] = new InterleaveGroup
<VPInstruction
>(
745 IG
->getFactor(), IG
->isReverse(), Align(IG
->getAlignment()));
747 if (Inst
== IG
->getInsertPos())
748 Old2New
[IG
]->setInsertPos(VPInst
);
750 InterleaveGroupMap
[VPInst
] = Old2New
[IG
];
751 InterleaveGroupMap
[VPInst
]->insertMember(
752 VPInst
, IG
->getIndex(Inst
),
753 Align(IG
->isReverse() ? (-1) * int(IG
->getFactor())
756 } else if (VPRegionBlock
*Region
= dyn_cast
<VPRegionBlock
>(Block
))
757 visitRegion(Region
, Old2New
, IAI
);
759 llvm_unreachable("Unsupported kind of VPBlock.");
762 VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan
&Plan
,
763 InterleavedAccessInfo
&IAI
) {
765 visitRegion(cast
<VPRegionBlock
>(Plan
.getEntry()), Old2New
, IAI
);