xref: /llvm-project/llvm/lib/Transforms/Vectorize/VPlan.cpp (revision 31a94bd783ae61f418bd730109992dfb30b70e87)
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 "VPlanCFG.h"
21 #include "VPlanDominatorTree.h"
22 #include "VPlanPatternMatch.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Analysis/DomTreeUpdater.h"
29 #include "llvm/Analysis/LoopInfo.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Type.h"
36 #include "llvm/IR/Value.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/GenericDomTreeConstruction.h"
41 #include "llvm/Support/GraphWriter.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
44 #include "llvm/Transforms/Utils/LoopVersioning.h"
45 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
46 #include <cassert>
47 #include <string>
48 #include <vector>
49 
50 using namespace llvm;
51 using namespace llvm::VPlanPatternMatch;
52 
53 namespace llvm {
54 extern cl::opt<bool> EnableVPlanNativePath;
55 }
56 
57 #define DEBUG_TYPE "vplan"
58 
59 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60 raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) {
61   const VPInstruction *Instr = dyn_cast<VPInstruction>(&V);
62   VPSlotTracker SlotTracker(
63       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
64   V.print(OS, SlotTracker);
65   return OS;
66 }
67 #endif
68 
69 Value *VPLane::getAsRuntimeExpr(IRBuilderBase &Builder,
70                                 const ElementCount &VF) const {
71   switch (LaneKind) {
72   case VPLane::Kind::ScalableLast:
73     // Lane = RuntimeVF - VF.getKnownMinValue() + Lane
74     return Builder.CreateSub(getRuntimeVF(Builder, Builder.getInt32Ty(), VF),
75                              Builder.getInt32(VF.getKnownMinValue() - Lane));
76   case VPLane::Kind::First:
77     return Builder.getInt32(Lane);
78   }
79   llvm_unreachable("Unknown lane kind");
80 }
81 
82 VPValue::VPValue(const unsigned char SC, Value *UV, VPDef *Def)
83     : SubclassID(SC), UnderlyingVal(UV), Def(Def) {
84   if (Def)
85     Def->addDefinedValue(this);
86 }
87 
88 VPValue::~VPValue() {
89   assert(Users.empty() && "trying to delete a VPValue with remaining users");
90   if (Def)
91     Def->removeDefinedValue(this);
92 }
93 
94 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
95 void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const {
96   if (const VPRecipeBase *R = dyn_cast_or_null<VPRecipeBase>(Def))
97     R->print(OS, "", SlotTracker);
98   else
99     printAsOperand(OS, SlotTracker);
100 }
101 
102 void VPValue::dump() const {
103   const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this->Def);
104   VPSlotTracker SlotTracker(
105       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
106   print(dbgs(), SlotTracker);
107   dbgs() << "\n";
108 }
109 
110 void VPDef::dump() const {
111   const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this);
112   VPSlotTracker SlotTracker(
113       (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
114   print(dbgs(), "", SlotTracker);
115   dbgs() << "\n";
116 }
117 #endif
118 
119 VPRecipeBase *VPValue::getDefiningRecipe() {
120   return cast_or_null<VPRecipeBase>(Def);
121 }
122 
123 const VPRecipeBase *VPValue::getDefiningRecipe() const {
124   return cast_or_null<VPRecipeBase>(Def);
125 }
126 
127 // Get the top-most entry block of \p Start. This is the entry block of the
128 // containing VPlan. This function is templated to support both const and non-const blocks
129 template <typename T> static T *getPlanEntry(T *Start) {
130   T *Next = Start;
131   T *Current = Start;
132   while ((Next = Next->getParent()))
133     Current = Next;
134 
135   SmallSetVector<T *, 8> WorkList;
136   WorkList.insert(Current);
137 
138   for (unsigned i = 0; i < WorkList.size(); i++) {
139     T *Current = WorkList[i];
140     if (Current->getNumPredecessors() == 0)
141       return Current;
142     auto &Predecessors = Current->getPredecessors();
143     WorkList.insert(Predecessors.begin(), Predecessors.end());
144   }
145 
146   llvm_unreachable("VPlan without any entry node without predecessors");
147 }
148 
149 VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; }
150 
151 const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; }
152 
153 /// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
154 const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const {
155   const VPBlockBase *Block = this;
156   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
157     Block = Region->getEntry();
158   return cast<VPBasicBlock>(Block);
159 }
160 
161 VPBasicBlock *VPBlockBase::getEntryBasicBlock() {
162   VPBlockBase *Block = this;
163   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
164     Block = Region->getEntry();
165   return cast<VPBasicBlock>(Block);
166 }
167 
168 void VPBlockBase::setPlan(VPlan *ParentPlan) {
169   assert(
170       (ParentPlan->getEntry() == this || ParentPlan->getPreheader() == this) &&
171       "Can only set plan on its entry or preheader block.");
172   Plan = ParentPlan;
173 }
174 
175 /// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
176 const VPBasicBlock *VPBlockBase::getExitingBasicBlock() const {
177   const VPBlockBase *Block = this;
178   while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
179     Block = Region->getExiting();
180   return cast<VPBasicBlock>(Block);
181 }
182 
183 VPBasicBlock *VPBlockBase::getExitingBasicBlock() {
184   VPBlockBase *Block = this;
185   while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
186     Block = Region->getExiting();
187   return cast<VPBasicBlock>(Block);
188 }
189 
190 VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() {
191   if (!Successors.empty() || !Parent)
192     return this;
193   assert(Parent->getExiting() == this &&
194          "Block w/o successors not the exiting block of its parent.");
195   return Parent->getEnclosingBlockWithSuccessors();
196 }
197 
198 VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() {
199   if (!Predecessors.empty() || !Parent)
200     return this;
201   assert(Parent->getEntry() == this &&
202          "Block w/o predecessors not the entry of its parent.");
203   return Parent->getEnclosingBlockWithPredecessors();
204 }
205 
206 void VPBlockBase::deleteCFG(VPBlockBase *Entry) {
207   for (VPBlockBase *Block : to_vector(vp_depth_first_shallow(Entry)))
208     delete Block;
209 }
210 
211 VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() {
212   iterator It = begin();
213   while (It != end() && It->isPhi())
214     It++;
215   return It;
216 }
217 
218 VPTransformState::VPTransformState(ElementCount VF, unsigned UF, LoopInfo *LI,
219                                    DominatorTree *DT, IRBuilderBase &Builder,
220                                    InnerLoopVectorizer *ILV, VPlan *Plan,
221                                    LLVMContext &Ctx)
222     : VF(VF), UF(UF), CFG(DT), LI(LI), Builder(Builder), ILV(ILV), Plan(Plan),
223       LVer(nullptr),
224       TypeAnalysis(Plan->getCanonicalIV()->getScalarType(), Ctx) {}
225 
226 Value *VPTransformState::get(VPValue *Def, const VPIteration &Instance) {
227   if (Def->isLiveIn())
228     return Def->getLiveInIRValue();
229 
230   if (hasScalarValue(Def, Instance)) {
231     return Data
232         .PerPartScalars[Def][Instance.Part][Instance.Lane.mapToCacheIndex(VF)];
233   }
234   if (!Instance.Lane.isFirstLane() &&
235       vputils::isUniformAfterVectorization(Def) &&
236       hasScalarValue(Def, {Instance.Part, VPLane::getFirstLane()})) {
237     return Data.PerPartScalars[Def][Instance.Part][0];
238   }
239 
240   assert(hasVectorValue(Def, Instance.Part));
241   auto *VecPart = Data.PerPartOutput[Def][Instance.Part];
242   if (!VecPart->getType()->isVectorTy()) {
243     assert(Instance.Lane.isFirstLane() && "cannot get lane > 0 for scalar");
244     return VecPart;
245   }
246   // TODO: Cache created scalar values.
247   Value *Lane = Instance.Lane.getAsRuntimeExpr(Builder, VF);
248   auto *Extract = Builder.CreateExtractElement(VecPart, Lane);
249   // set(Def, Extract, Instance);
250   return Extract;
251 }
252 
253 Value *VPTransformState::get(VPValue *Def, unsigned Part, bool NeedsScalar) {
254   if (NeedsScalar) {
255     assert((VF.isScalar() || Def->isLiveIn() || hasVectorValue(Def, Part) ||
256             (hasScalarValue(Def, VPIteration(Part, 0)) &&
257              Data.PerPartScalars[Def][Part].size() == 1)) &&
258            "Trying to access a single scalar per part but has multiple scalars "
259            "per part.");
260     return get(Def, VPIteration(Part, 0));
261   }
262 
263   // If Values have been set for this Def return the one relevant for \p Part.
264   if (hasVectorValue(Def, Part))
265     return Data.PerPartOutput[Def][Part];
266 
267   auto GetBroadcastInstrs = [this, Def](Value *V) {
268     bool SafeToHoist = Def->isDefinedOutsideVectorRegions();
269     if (VF.isScalar())
270       return V;
271     // Place the code for broadcasting invariant variables in the new preheader.
272     IRBuilder<>::InsertPointGuard Guard(Builder);
273     if (SafeToHoist) {
274       BasicBlock *LoopVectorPreHeader = CFG.VPBB2IRBB[cast<VPBasicBlock>(
275           Plan->getVectorLoopRegion()->getSinglePredecessor())];
276       if (LoopVectorPreHeader)
277         Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
278     }
279 
280     // Place the code for broadcasting invariant variables in the new preheader.
281     // Broadcast the scalar into all locations in the vector.
282     Value *Shuf = Builder.CreateVectorSplat(VF, V, "broadcast");
283 
284     return Shuf;
285   };
286 
287   if (!hasScalarValue(Def, {Part, 0})) {
288     assert(Def->isLiveIn() && "expected a live-in");
289     if (Part != 0)
290       return get(Def, 0);
291     Value *IRV = Def->getLiveInIRValue();
292     Value *B = GetBroadcastInstrs(IRV);
293     set(Def, B, Part);
294     return B;
295   }
296 
297   Value *ScalarValue = get(Def, {Part, 0});
298   // If we aren't vectorizing, we can just copy the scalar map values over
299   // to the vector map.
300   if (VF.isScalar()) {
301     set(Def, ScalarValue, Part);
302     return ScalarValue;
303   }
304 
305   bool IsUniform = vputils::isUniformAfterVectorization(Def);
306 
307   unsigned LastLane = IsUniform ? 0 : VF.getKnownMinValue() - 1;
308   // Check if there is a scalar value for the selected lane.
309   if (!hasScalarValue(Def, {Part, LastLane})) {
310     // At the moment, VPWidenIntOrFpInductionRecipes, VPScalarIVStepsRecipes and
311     // VPExpandSCEVRecipes can also be uniform.
312     assert((isa<VPWidenIntOrFpInductionRecipe>(Def->getDefiningRecipe()) ||
313             isa<VPScalarIVStepsRecipe>(Def->getDefiningRecipe()) ||
314             isa<VPExpandSCEVRecipe>(Def->getDefiningRecipe())) &&
315            "unexpected recipe found to be invariant");
316     IsUniform = true;
317     LastLane = 0;
318   }
319 
320   auto *LastInst = cast<Instruction>(get(Def, {Part, LastLane}));
321   // Set the insert point after the last scalarized instruction or after the
322   // last PHI, if LastInst is a PHI. This ensures the insertelement sequence
323   // will directly follow the scalar definitions.
324   auto OldIP = Builder.saveIP();
325   auto NewIP =
326       isa<PHINode>(LastInst)
327           ? BasicBlock::iterator(LastInst->getParent()->getFirstNonPHI())
328           : std::next(BasicBlock::iterator(LastInst));
329   Builder.SetInsertPoint(&*NewIP);
330 
331   // However, if we are vectorizing, we need to construct the vector values.
332   // If the value is known to be uniform after vectorization, we can just
333   // broadcast the scalar value corresponding to lane zero for each unroll
334   // iteration. Otherwise, we construct the vector values using
335   // insertelement instructions. Since the resulting vectors are stored in
336   // State, we will only generate the insertelements once.
337   Value *VectorValue = nullptr;
338   if (IsUniform) {
339     VectorValue = GetBroadcastInstrs(ScalarValue);
340     set(Def, VectorValue, Part);
341   } else {
342     // Initialize packing with insertelements to start from undef.
343     assert(!VF.isScalable() && "VF is assumed to be non scalable.");
344     Value *Undef = PoisonValue::get(VectorType::get(LastInst->getType(), VF));
345     set(Def, Undef, Part);
346     for (unsigned Lane = 0; Lane < VF.getKnownMinValue(); ++Lane)
347       packScalarIntoVectorValue(Def, {Part, Lane});
348     VectorValue = get(Def, Part);
349   }
350   Builder.restoreIP(OldIP);
351   return VectorValue;
352 }
353 
354 BasicBlock *VPTransformState::CFGState::getPreheaderBBFor(VPRecipeBase *R) {
355   VPRegionBlock *LoopRegion = R->getParent()->getEnclosingLoopRegion();
356   return VPBB2IRBB[LoopRegion->getPreheaderVPBB()];
357 }
358 
359 void VPTransformState::addNewMetadata(Instruction *To,
360                                       const Instruction *Orig) {
361   // If the loop was versioned with memchecks, add the corresponding no-alias
362   // metadata.
363   if (LVer && (isa<LoadInst>(Orig) || isa<StoreInst>(Orig)))
364     LVer->annotateInstWithNoAlias(To, Orig);
365 }
366 
367 void VPTransformState::addMetadata(Value *To, Instruction *From) {
368   // No source instruction to transfer metadata from?
369   if (!From)
370     return;
371 
372   if (Instruction *ToI = dyn_cast<Instruction>(To)) {
373     propagateMetadata(ToI, From);
374     addNewMetadata(ToI, From);
375   }
376 }
377 
378 void VPTransformState::setDebugLocFrom(DebugLoc DL) {
379   const DILocation *DIL = DL;
380   // When a FSDiscriminator is enabled, we don't need to add the multiply
381   // factors to the discriminators.
382   if (DIL &&
383       Builder.GetInsertBlock()
384           ->getParent()
385           ->shouldEmitDebugInfoForProfiling() &&
386       !EnableFSDiscriminator) {
387     // FIXME: For scalable vectors, assume vscale=1.
388     auto NewDIL =
389         DIL->cloneByMultiplyingDuplicationFactor(UF * VF.getKnownMinValue());
390     if (NewDIL)
391       Builder.SetCurrentDebugLocation(*NewDIL);
392     else
393       LLVM_DEBUG(dbgs() << "Failed to create new discriminator: "
394                         << DIL->getFilename() << " Line: " << DIL->getLine());
395   } else
396     Builder.SetCurrentDebugLocation(DIL);
397 }
398 
399 void VPTransformState::packScalarIntoVectorValue(VPValue *Def,
400                                                  const VPIteration &Instance) {
401   Value *ScalarInst = get(Def, Instance);
402   Value *VectorValue = get(Def, Instance.Part);
403   VectorValue = Builder.CreateInsertElement(
404       VectorValue, ScalarInst, Instance.Lane.getAsRuntimeExpr(Builder, VF));
405   set(Def, VectorValue, Instance.Part);
406 }
407 
408 BasicBlock *
409 VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) {
410   // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
411   // Pred stands for Predessor. Prev stands for Previous - last visited/created.
412   BasicBlock *PrevBB = CFG.PrevBB;
413   BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(),
414                                          PrevBB->getParent(), CFG.ExitBB);
415   LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n');
416 
417   // Hook up the new basic block to its predecessors.
418   for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) {
419     VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock();
420     auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors();
421     BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
422 
423     assert(PredBB && "Predecessor basic-block not found building successor.");
424     auto *PredBBTerminator = PredBB->getTerminator();
425     LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
426 
427     auto *TermBr = dyn_cast<BranchInst>(PredBBTerminator);
428     if (isa<UnreachableInst>(PredBBTerminator)) {
429       assert(PredVPSuccessors.size() == 1 &&
430              "Predecessor ending w/o branch must have single successor.");
431       DebugLoc DL = PredBBTerminator->getDebugLoc();
432       PredBBTerminator->eraseFromParent();
433       auto *Br = BranchInst::Create(NewBB, PredBB);
434       Br->setDebugLoc(DL);
435     } else if (TermBr && !TermBr->isConditional()) {
436       TermBr->setSuccessor(0, NewBB);
437     } else {
438       // Set each forward successor here when it is created, excluding
439       // backedges. A backward successor is set when the branch is created.
440       unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
441       assert(!TermBr->getSuccessor(idx) &&
442              "Trying to reset an existing successor block.");
443       TermBr->setSuccessor(idx, NewBB);
444     }
445     CFG.DTU.applyUpdates({{DominatorTree::Insert, PredBB, NewBB}});
446   }
447   return NewBB;
448 }
449 
450 void VPIRBasicBlock::execute(VPTransformState *State) {
451   assert(getHierarchicalSuccessors().empty() &&
452          "VPIRBasicBlock cannot have successors at the moment");
453 
454   State->Builder.SetInsertPoint(getIRBasicBlock()->getTerminator());
455   executeRecipes(State, getIRBasicBlock());
456 
457   for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) {
458     VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock();
459     BasicBlock *PredBB = State->CFG.VPBB2IRBB[PredVPBB];
460     assert(PredBB && "Predecessor basic-block not found building successor.");
461     LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n');
462 
463     auto *PredBBTerminator = PredBB->getTerminator();
464     auto *TermBr = cast<BranchInst>(PredBBTerminator);
465     // Set each forward successor here when it is created, excluding
466     // backedges. A backward successor is set when the branch is created.
467     const auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors();
468     unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
469     assert(!TermBr->getSuccessor(idx) &&
470            "Trying to reset an existing successor block.");
471     TermBr->setSuccessor(idx, IRBB);
472     State->CFG.DTU.applyUpdates({{DominatorTree::Insert, PredBB, IRBB}});
473   }
474 }
475 
476 void VPBasicBlock::execute(VPTransformState *State) {
477   bool Replica = State->Instance && !State->Instance->isFirstIteration();
478   VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB;
479   VPBlockBase *SingleHPred = nullptr;
480   BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible.
481 
482   auto IsLoopRegion = [](VPBlockBase *BB) {
483     auto *R = dyn_cast<VPRegionBlock>(BB);
484     return R && !R->isReplicator();
485   };
486 
487   // 1. Create an IR basic block.
488   if (PrevVPBB && /* A */
489       !((SingleHPred = getSingleHierarchicalPredecessor()) &&
490         SingleHPred->getExitingBasicBlock() == PrevVPBB &&
491         PrevVPBB->getSingleHierarchicalSuccessor() &&
492         (SingleHPred->getParent() == getEnclosingLoopRegion() &&
493          !IsLoopRegion(SingleHPred))) &&         /* B */
494       !(Replica && getPredecessors().empty())) { /* C */
495     // The last IR basic block is reused, as an optimization, in three cases:
496     // A. the first VPBB reuses the loop pre-header BB - when PrevVPBB is null;
497     // B. when the current VPBB has a single (hierarchical) predecessor which
498     //    is PrevVPBB and the latter has a single (hierarchical) successor which
499     //    both are in the same non-replicator region; and
500     // C. when the current VPBB is an entry of a region replica - where PrevVPBB
501     //    is the exiting VPBB of this region from a previous instance, or the
502     //    predecessor of this region.
503 
504     NewBB = createEmptyBasicBlock(State->CFG);
505     State->Builder.SetInsertPoint(NewBB);
506     // Temporarily terminate with unreachable until CFG is rewired.
507     UnreachableInst *Terminator = State->Builder.CreateUnreachable();
508     // Register NewBB in its loop. In innermost loops its the same for all
509     // BB's.
510     if (State->CurrentVectorLoop)
511       State->CurrentVectorLoop->addBasicBlockToLoop(NewBB, *State->LI);
512     State->Builder.SetInsertPoint(Terminator);
513     State->CFG.PrevBB = NewBB;
514   }
515 
516   // 2. Fill the IR basic block with IR instructions.
517   executeRecipes(State, NewBB);
518 }
519 
520 void VPBasicBlock::dropAllReferences(VPValue *NewValue) {
521   for (VPRecipeBase &R : Recipes) {
522     for (auto *Def : R.definedValues())
523       Def->replaceAllUsesWith(NewValue);
524 
525     for (unsigned I = 0, E = R.getNumOperands(); I != E; I++)
526       R.setOperand(I, NewValue);
527   }
528 }
529 
530 void VPBasicBlock::executeRecipes(VPTransformState *State, BasicBlock *BB) {
531   LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName()
532                     << " in BB:" << BB->getName() << '\n');
533 
534   State->CFG.VPBB2IRBB[this] = BB;
535   State->CFG.PrevVPBB = this;
536 
537   for (VPRecipeBase &Recipe : Recipes)
538     Recipe.execute(*State);
539 
540   LLVM_DEBUG(dbgs() << "LV: filled BB:" << *BB);
541 }
542 
543 VPBasicBlock *VPBasicBlock::splitAt(iterator SplitAt) {
544   assert((SplitAt == end() || SplitAt->getParent() == this) &&
545          "can only split at a position in the same block");
546 
547   SmallVector<VPBlockBase *, 2> Succs(successors());
548   // First, disconnect the current block from its successors.
549   for (VPBlockBase *Succ : Succs)
550     VPBlockUtils::disconnectBlocks(this, Succ);
551 
552   // Create new empty block after the block to split.
553   auto *SplitBlock = new VPBasicBlock(getName() + ".split");
554   VPBlockUtils::insertBlockAfter(SplitBlock, this);
555 
556   // Add successors for block to split to new block.
557   for (VPBlockBase *Succ : Succs)
558     VPBlockUtils::connectBlocks(SplitBlock, Succ);
559 
560   // Finally, move the recipes starting at SplitAt to new block.
561   for (VPRecipeBase &ToMove :
562        make_early_inc_range(make_range(SplitAt, this->end())))
563     ToMove.moveBefore(*SplitBlock, SplitBlock->end());
564 
565   return SplitBlock;
566 }
567 
568 VPRegionBlock *VPBasicBlock::getEnclosingLoopRegion() {
569   VPRegionBlock *P = getParent();
570   if (P && P->isReplicator()) {
571     P = P->getParent();
572     assert(!cast<VPRegionBlock>(P)->isReplicator() &&
573            "unexpected nested replicate regions");
574   }
575   return P;
576 }
577 
578 static bool hasConditionalTerminator(const VPBasicBlock *VPBB) {
579   if (VPBB->empty()) {
580     assert(
581         VPBB->getNumSuccessors() < 2 &&
582         "block with multiple successors doesn't have a recipe as terminator");
583     return false;
584   }
585 
586   const VPRecipeBase *R = &VPBB->back();
587   bool IsCondBranch = isa<VPBranchOnMaskRecipe>(R) ||
588                       match(R, m_BranchOnCond(m_VPValue())) ||
589                       match(R, m_BranchOnCount(m_VPValue(), m_VPValue()));
590   (void)IsCondBranch;
591 
592   if (VPBB->getNumSuccessors() >= 2 ||
593       (VPBB->isExiting() && !VPBB->getParent()->isReplicator())) {
594     assert(IsCondBranch && "block with multiple successors not terminated by "
595                            "conditional branch recipe");
596 
597     return true;
598   }
599 
600   assert(
601       !IsCondBranch &&
602       "block with 0 or 1 successors terminated by conditional branch recipe");
603   return false;
604 }
605 
606 VPRecipeBase *VPBasicBlock::getTerminator() {
607   if (hasConditionalTerminator(this))
608     return &back();
609   return nullptr;
610 }
611 
612 const VPRecipeBase *VPBasicBlock::getTerminator() const {
613   if (hasConditionalTerminator(this))
614     return &back();
615   return nullptr;
616 }
617 
618 bool VPBasicBlock::isExiting() const {
619   return getParent() && getParent()->getExitingBasicBlock() == this;
620 }
621 
622 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
623 void VPBlockBase::printSuccessors(raw_ostream &O, const Twine &Indent) const {
624   if (getSuccessors().empty()) {
625     O << Indent << "No successors\n";
626   } else {
627     O << Indent << "Successor(s): ";
628     ListSeparator LS;
629     for (auto *Succ : getSuccessors())
630       O << LS << Succ->getName();
631     O << '\n';
632   }
633 }
634 
635 void VPBasicBlock::print(raw_ostream &O, const Twine &Indent,
636                          VPSlotTracker &SlotTracker) const {
637   O << Indent << getName() << ":\n";
638 
639   auto RecipeIndent = Indent + "  ";
640   for (const VPRecipeBase &Recipe : *this) {
641     Recipe.print(O, RecipeIndent, SlotTracker);
642     O << '\n';
643   }
644 
645   printSuccessors(O, Indent);
646 }
647 #endif
648 
649 static std::pair<VPBlockBase *, VPBlockBase *> cloneSESE(VPBlockBase *Entry);
650 
651 // Clone the CFG for all nodes in the single-entry-single-exit region reachable
652 // from \p Entry, this includes cloning the blocks and their recipes. Operands
653 // of cloned recipes will NOT be updated. Remapping of operands must be done
654 // separately. Returns a pair with the the new entry and exiting blocks of the
655 // cloned region.
656 static std::pair<VPBlockBase *, VPBlockBase *> cloneSESE(VPBlockBase *Entry) {
657   DenseMap<VPBlockBase *, VPBlockBase *> Old2NewVPBlocks;
658   ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>> RPOT(
659       Entry);
660   for (VPBlockBase *BB : RPOT) {
661     VPBlockBase *NewBB = BB->clone();
662     for (VPBlockBase *Pred : BB->getPredecessors())
663       VPBlockUtils::connectBlocks(Old2NewVPBlocks[Pred], NewBB);
664 
665     Old2NewVPBlocks[BB] = NewBB;
666   }
667 
668 #if !defined(NDEBUG)
669   // Verify that the order of predecessors and successors matches in the cloned
670   // version.
671   ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
672       NewRPOT(Old2NewVPBlocks[Entry]);
673   for (const auto &[OldBB, NewBB] : zip(RPOT, NewRPOT)) {
674     for (const auto &[OldPred, NewPred] :
675          zip(OldBB->getPredecessors(), NewBB->getPredecessors()))
676       assert(NewPred == Old2NewVPBlocks[OldPred] && "Different predecessors");
677 
678     for (const auto &[OldSucc, NewSucc] :
679          zip(OldBB->successors(), NewBB->successors()))
680       assert(NewSucc == Old2NewVPBlocks[OldSucc] && "Different successors");
681   }
682 #endif
683 
684   return std::make_pair(Old2NewVPBlocks[Entry],
685                         Old2NewVPBlocks[*reverse(RPOT).begin()]);
686 }
687 
688 VPRegionBlock *VPRegionBlock::clone() {
689   const auto &[NewEntry, NewExiting] = cloneSESE(getEntry());
690   auto *NewRegion =
691       new VPRegionBlock(NewEntry, NewExiting, getName(), isReplicator());
692   for (VPBlockBase *Block : vp_depth_first_shallow(NewEntry))
693     Block->setParent(NewRegion);
694   return NewRegion;
695 }
696 
697 void VPRegionBlock::dropAllReferences(VPValue *NewValue) {
698   for (VPBlockBase *Block : vp_depth_first_shallow(Entry))
699     // Drop all references in VPBasicBlocks and replace all uses with
700     // DummyValue.
701     Block->dropAllReferences(NewValue);
702 }
703 
704 void VPRegionBlock::execute(VPTransformState *State) {
705   ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
706       RPOT(Entry);
707 
708   if (!isReplicator()) {
709     // Create and register the new vector loop.
710     Loop *PrevLoop = State->CurrentVectorLoop;
711     State->CurrentVectorLoop = State->LI->AllocateLoop();
712     BasicBlock *VectorPH = State->CFG.VPBB2IRBB[getPreheaderVPBB()];
713     Loop *ParentLoop = State->LI->getLoopFor(VectorPH);
714 
715     // Insert the new loop into the loop nest and register the new basic blocks
716     // before calling any utilities such as SCEV that require valid LoopInfo.
717     if (ParentLoop)
718       ParentLoop->addChildLoop(State->CurrentVectorLoop);
719     else
720       State->LI->addTopLevelLoop(State->CurrentVectorLoop);
721 
722     // Visit the VPBlocks connected to "this", starting from it.
723     for (VPBlockBase *Block : RPOT) {
724       LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
725       Block->execute(State);
726     }
727 
728     State->CurrentVectorLoop = PrevLoop;
729     return;
730   }
731 
732   assert(!State->Instance && "Replicating a Region with non-null instance.");
733 
734   // Enter replicating mode.
735   State->Instance = VPIteration(0, 0);
736 
737   for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) {
738     State->Instance->Part = Part;
739     assert(!State->VF.isScalable() && "VF is assumed to be non scalable.");
740     for (unsigned Lane = 0, VF = State->VF.getKnownMinValue(); Lane < VF;
741          ++Lane) {
742       State->Instance->Lane = VPLane(Lane, VPLane::Kind::First);
743       // Visit the VPBlocks connected to \p this, starting from it.
744       for (VPBlockBase *Block : RPOT) {
745         LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
746         Block->execute(State);
747       }
748     }
749   }
750 
751   // Exit replicating mode.
752   State->Instance.reset();
753 }
754 
755 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
756 void VPRegionBlock::print(raw_ostream &O, const Twine &Indent,
757                           VPSlotTracker &SlotTracker) const {
758   O << Indent << (isReplicator() ? "<xVFxUF> " : "<x1> ") << getName() << ": {";
759   auto NewIndent = Indent + "  ";
760   for (auto *BlockBase : vp_depth_first_shallow(Entry)) {
761     O << '\n';
762     BlockBase->print(O, NewIndent, SlotTracker);
763   }
764   O << Indent << "}\n";
765 
766   printSuccessors(O, Indent);
767 }
768 #endif
769 
770 VPlan::~VPlan() {
771   for (auto &KV : LiveOuts)
772     delete KV.second;
773   LiveOuts.clear();
774 
775   if (Entry) {
776     VPValue DummyValue;
777     for (VPBlockBase *Block : vp_depth_first_shallow(Entry))
778       Block->dropAllReferences(&DummyValue);
779 
780     VPBlockBase::deleteCFG(Entry);
781 
782     Preheader->dropAllReferences(&DummyValue);
783     delete Preheader;
784   }
785   for (VPValue *VPV : VPLiveInsToFree)
786     delete VPV;
787   if (BackedgeTakenCount)
788     delete BackedgeTakenCount;
789 }
790 
791 VPlanPtr VPlan::createInitialVPlan(const SCEV *TripCount, ScalarEvolution &SE,
792                                    BasicBlock *PH) {
793   VPIRBasicBlock *Entry = new VPIRBasicBlock(PH);
794   VPBasicBlock *VecPreheader = new VPBasicBlock("vector.ph");
795   auto Plan = std::make_unique<VPlan>(Entry, VecPreheader);
796   Plan->TripCount =
797       vputils::getOrCreateVPValueForSCEVExpr(*Plan, TripCount, SE);
798   // Create empty VPRegionBlock, to be filled during processing later.
799   auto *TopRegion = new VPRegionBlock("vector loop", false /*isReplicator*/);
800   VPBlockUtils::insertBlockAfter(TopRegion, VecPreheader);
801   VPBasicBlock *MiddleVPBB = new VPBasicBlock("middle.block");
802   VPBlockUtils::insertBlockAfter(MiddleVPBB, TopRegion);
803   return Plan;
804 }
805 
806 void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV,
807                              Value *CanonicalIVStartValue,
808                              VPTransformState &State) {
809   // Check if the backedge taken count is needed, and if so build it.
810   if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) {
811     IRBuilder<> Builder(State.CFG.PrevBB->getTerminator());
812     auto *TCMO = Builder.CreateSub(TripCountV,
813                                    ConstantInt::get(TripCountV->getType(), 1),
814                                    "trip.count.minus.1");
815     BackedgeTakenCount->setUnderlyingValue(TCMO);
816   }
817 
818   VectorTripCount.setUnderlyingValue(VectorTripCountV);
819 
820   IRBuilder<> Builder(State.CFG.PrevBB->getTerminator());
821   // FIXME: Model VF * UF computation completely in VPlan.
822   VFxUF.setUnderlyingValue(
823       createStepForVF(Builder, TripCountV->getType(), State.VF, State.UF));
824 
825   // When vectorizing the epilogue loop, the canonical induction start value
826   // needs to be changed from zero to the value after the main vector loop.
827   // FIXME: Improve modeling for canonical IV start values in the epilogue loop.
828   if (CanonicalIVStartValue) {
829     VPValue *VPV = getOrAddLiveIn(CanonicalIVStartValue);
830     auto *IV = getCanonicalIV();
831     assert(all_of(IV->users(),
832                   [](const VPUser *U) {
833                     return isa<VPScalarIVStepsRecipe>(U) ||
834                            isa<VPScalarCastRecipe>(U) ||
835                            isa<VPDerivedIVRecipe>(U) ||
836                            cast<VPInstruction>(U)->getOpcode() ==
837                                Instruction::Add;
838                   }) &&
839            "the canonical IV should only be used by its increment or "
840            "ScalarIVSteps when resetting the start value");
841     IV->setOperand(0, VPV);
842   }
843 }
844 
845 /// Replace \p VPBB with a VPIRBasicBlock wrapping \p IRBB. All recipes from \p
846 /// VPBB are moved to the newly created VPIRBasicBlock.
847 static void replaceVPBBWithIRVPBB(VPBasicBlock *VPBB, BasicBlock *IRBB) {
848   assert(VPBB->getNumSuccessors() == 0 && "VPBB must be a leave node");
849   VPIRBasicBlock *IRMiddleVPBB = new VPIRBasicBlock(IRBB);
850   for (auto &R : make_early_inc_range(*VPBB))
851     R.moveBefore(*IRMiddleVPBB, IRMiddleVPBB->end());
852   VPBlockBase *PredVPBB = VPBB->getSinglePredecessor();
853   VPBlockUtils::disconnectBlocks(PredVPBB, VPBB);
854   VPBlockUtils::connectBlocks(PredVPBB, IRMiddleVPBB);
855   delete VPBB;
856 }
857 
858 /// Generate the code inside the preheader and body of the vectorized loop.
859 /// Assumes a single pre-header basic-block was created for this. Introduce
860 /// additional basic-blocks as needed, and fill them all.
861 void VPlan::execute(VPTransformState *State) {
862   // Initialize CFG state.
863   State->CFG.PrevVPBB = nullptr;
864   State->CFG.ExitBB = State->CFG.PrevBB->getSingleSuccessor();
865   BasicBlock *VectorPreHeader = State->CFG.PrevBB;
866   State->Builder.SetInsertPoint(VectorPreHeader->getTerminator());
867   replaceVPBBWithIRVPBB(
868       cast<VPBasicBlock>(getVectorLoopRegion()->getSingleSuccessor()),
869       State->CFG.ExitBB);
870 
871   // Disconnect VectorPreHeader from ExitBB in both the CFG and DT.
872   cast<BranchInst>(VectorPreHeader->getTerminator())->setSuccessor(0, nullptr);
873   State->CFG.DTU.applyUpdates(
874       {{DominatorTree::Delete, VectorPreHeader, State->CFG.ExitBB}});
875 
876   // Generate code in the loop pre-header and body.
877   for (VPBlockBase *Block : vp_depth_first_shallow(Entry))
878     Block->execute(State);
879 
880   VPBasicBlock *LatchVPBB = getVectorLoopRegion()->getExitingBasicBlock();
881   BasicBlock *VectorLatchBB = State->CFG.VPBB2IRBB[LatchVPBB];
882 
883   // Fix the latch value of canonical, reduction and first-order recurrences
884   // phis in the vector loop.
885   VPBasicBlock *Header = getVectorLoopRegion()->getEntryBasicBlock();
886   for (VPRecipeBase &R : Header->phis()) {
887     // Skip phi-like recipes that generate their backedege values themselves.
888     if (isa<VPWidenPHIRecipe>(&R))
889       continue;
890 
891     if (isa<VPWidenPointerInductionRecipe>(&R) ||
892         isa<VPWidenIntOrFpInductionRecipe>(&R)) {
893       PHINode *Phi = nullptr;
894       if (isa<VPWidenIntOrFpInductionRecipe>(&R)) {
895         Phi = cast<PHINode>(State->get(R.getVPSingleValue(), 0));
896       } else {
897         auto *WidenPhi = cast<VPWidenPointerInductionRecipe>(&R);
898         assert(!WidenPhi->onlyScalarsGenerated(State->VF.isScalable()) &&
899                "recipe generating only scalars should have been replaced");
900         auto *GEP = cast<GetElementPtrInst>(State->get(WidenPhi, 0));
901         Phi = cast<PHINode>(GEP->getPointerOperand());
902       }
903 
904       Phi->setIncomingBlock(1, VectorLatchBB);
905 
906       // Move the last step to the end of the latch block. This ensures
907       // consistent placement of all induction updates.
908       Instruction *Inc = cast<Instruction>(Phi->getIncomingValue(1));
909       Inc->moveBefore(VectorLatchBB->getTerminator()->getPrevNode());
910       continue;
911     }
912 
913     auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
914     // For  canonical IV, first-order recurrences and in-order reduction phis,
915     // only a single part is generated, which provides the last part from the
916     // previous iteration. For non-ordered reductions all UF parts are
917     // generated.
918     bool SinglePartNeeded =
919         isa<VPCanonicalIVPHIRecipe>(PhiR) ||
920         isa<VPFirstOrderRecurrencePHIRecipe, VPEVLBasedIVPHIRecipe>(PhiR) ||
921         (isa<VPReductionPHIRecipe>(PhiR) &&
922          cast<VPReductionPHIRecipe>(PhiR)->isOrdered());
923     bool NeedsScalar =
924         isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(PhiR) ||
925         (isa<VPReductionPHIRecipe>(PhiR) &&
926          cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
927     unsigned LastPartForNewPhi = SinglePartNeeded ? 1 : State->UF;
928 
929     for (unsigned Part = 0; Part < LastPartForNewPhi; ++Part) {
930       Value *Phi = State->get(PhiR, Part, NeedsScalar);
931       Value *Val =
932           State->get(PhiR->getBackedgeValue(),
933                      SinglePartNeeded ? State->UF - 1 : Part, NeedsScalar);
934       cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
935     }
936   }
937 
938   State->CFG.DTU.flush();
939   assert(State->CFG.DTU.getDomTree().verify(
940              DominatorTree::VerificationLevel::Fast) &&
941          "DT not preserved correctly");
942 }
943 
944 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
945 void VPlan::printLiveIns(raw_ostream &O) const {
946   VPSlotTracker SlotTracker(this);
947 
948   if (VFxUF.getNumUsers() > 0) {
949     O << "\nLive-in ";
950     VFxUF.printAsOperand(O, SlotTracker);
951     O << " = VF * UF";
952   }
953 
954   if (VectorTripCount.getNumUsers() > 0) {
955     O << "\nLive-in ";
956     VectorTripCount.printAsOperand(O, SlotTracker);
957     O << " = vector-trip-count";
958   }
959 
960   if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) {
961     O << "\nLive-in ";
962     BackedgeTakenCount->printAsOperand(O, SlotTracker);
963     O << " = backedge-taken count";
964   }
965 
966   O << "\n";
967   if (TripCount->isLiveIn())
968     O << "Live-in ";
969   TripCount->printAsOperand(O, SlotTracker);
970   O << " = original trip-count";
971   O << "\n";
972 }
973 
974 LLVM_DUMP_METHOD
975 void VPlan::print(raw_ostream &O) const {
976   VPSlotTracker SlotTracker(this);
977 
978   O << "VPlan '" << getName() << "' {";
979 
980   printLiveIns(O);
981 
982   if (!getPreheader()->empty()) {
983     O << "\n";
984     getPreheader()->print(O, "", SlotTracker);
985   }
986 
987   for (const VPBlockBase *Block : vp_depth_first_shallow(getEntry())) {
988     O << '\n';
989     Block->print(O, "", SlotTracker);
990   }
991 
992   if (!LiveOuts.empty())
993     O << "\n";
994   for (const auto &KV : LiveOuts) {
995     KV.second->print(O, SlotTracker);
996   }
997 
998   O << "}\n";
999 }
1000 
1001 std::string VPlan::getName() const {
1002   std::string Out;
1003   raw_string_ostream RSO(Out);
1004   RSO << Name << " for ";
1005   if (!VFs.empty()) {
1006     RSO << "VF={" << VFs[0];
1007     for (ElementCount VF : drop_begin(VFs))
1008       RSO << "," << VF;
1009     RSO << "},";
1010   }
1011 
1012   if (UFs.empty()) {
1013     RSO << "UF>=1";
1014   } else {
1015     RSO << "UF={" << UFs[0];
1016     for (unsigned UF : drop_begin(UFs))
1017       RSO << "," << UF;
1018     RSO << "}";
1019   }
1020 
1021   return Out;
1022 }
1023 
1024 LLVM_DUMP_METHOD
1025 void VPlan::printDOT(raw_ostream &O) const {
1026   VPlanPrinter Printer(O, *this);
1027   Printer.dump();
1028 }
1029 
1030 LLVM_DUMP_METHOD
1031 void VPlan::dump() const { print(dbgs()); }
1032 #endif
1033 
1034 void VPlan::addLiveOut(PHINode *PN, VPValue *V) {
1035   assert(LiveOuts.count(PN) == 0 && "an exit value for PN already exists");
1036   LiveOuts.insert({PN, new VPLiveOut(PN, V)});
1037 }
1038 
1039 static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry,
1040                           DenseMap<VPValue *, VPValue *> &Old2NewVPValues) {
1041   // Update the operands of all cloned recipes starting at NewEntry. This
1042   // traverses all reachable blocks. This is done in two steps, to handle cycles
1043   // in PHI recipes.
1044   ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>>
1045       OldDeepRPOT(Entry);
1046   ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>>
1047       NewDeepRPOT(NewEntry);
1048   // First, collect all mappings from old to new VPValues defined by cloned
1049   // recipes.
1050   for (const auto &[OldBB, NewBB] :
1051        zip(VPBlockUtils::blocksOnly<VPBasicBlock>(OldDeepRPOT),
1052            VPBlockUtils::blocksOnly<VPBasicBlock>(NewDeepRPOT))) {
1053     assert(OldBB->getRecipeList().size() == NewBB->getRecipeList().size() &&
1054            "blocks must have the same number of recipes");
1055     for (const auto &[OldR, NewR] : zip(*OldBB, *NewBB)) {
1056       assert(OldR.getNumOperands() == NewR.getNumOperands() &&
1057              "recipes must have the same number of operands");
1058       assert(OldR.getNumDefinedValues() == NewR.getNumDefinedValues() &&
1059              "recipes must define the same number of operands");
1060       for (const auto &[OldV, NewV] :
1061            zip(OldR.definedValues(), NewR.definedValues()))
1062         Old2NewVPValues[OldV] = NewV;
1063     }
1064   }
1065 
1066   // Update all operands to use cloned VPValues.
1067   for (VPBasicBlock *NewBB :
1068        VPBlockUtils::blocksOnly<VPBasicBlock>(NewDeepRPOT)) {
1069     for (VPRecipeBase &NewR : *NewBB)
1070       for (unsigned I = 0, E = NewR.getNumOperands(); I != E; ++I) {
1071         VPValue *NewOp = Old2NewVPValues.lookup(NewR.getOperand(I));
1072         NewR.setOperand(I, NewOp);
1073       }
1074   }
1075 }
1076 
1077 VPlan *VPlan::duplicate() {
1078   // Clone blocks.
1079   VPBasicBlock *NewPreheader = Preheader->clone();
1080   const auto &[NewEntry, __] = cloneSESE(Entry);
1081 
1082   // Create VPlan, clone live-ins and remap operands in the cloned blocks.
1083   auto *NewPlan = new VPlan(NewPreheader, cast<VPBasicBlock>(NewEntry));
1084   DenseMap<VPValue *, VPValue *> Old2NewVPValues;
1085   for (VPValue *OldLiveIn : VPLiveInsToFree) {
1086     Old2NewVPValues[OldLiveIn] =
1087         NewPlan->getOrAddLiveIn(OldLiveIn->getLiveInIRValue());
1088   }
1089   Old2NewVPValues[&VectorTripCount] = &NewPlan->VectorTripCount;
1090   Old2NewVPValues[&VFxUF] = &NewPlan->VFxUF;
1091   if (BackedgeTakenCount) {
1092     NewPlan->BackedgeTakenCount = new VPValue();
1093     Old2NewVPValues[BackedgeTakenCount] = NewPlan->BackedgeTakenCount;
1094   }
1095   assert(TripCount && "trip count must be set");
1096   if (TripCount->isLiveIn())
1097     Old2NewVPValues[TripCount] =
1098         NewPlan->getOrAddLiveIn(TripCount->getLiveInIRValue());
1099   // else NewTripCount will be created and inserted into Old2NewVPValues when
1100   // TripCount is cloned. In any case NewPlan->TripCount is updated below.
1101 
1102   remapOperands(Preheader, NewPreheader, Old2NewVPValues);
1103   remapOperands(Entry, NewEntry, Old2NewVPValues);
1104 
1105   // Clone live-outs.
1106   for (const auto &[_, LO] : LiveOuts)
1107     NewPlan->addLiveOut(LO->getPhi(), Old2NewVPValues[LO->getOperand(0)]);
1108 
1109   // Initialize remaining fields of cloned VPlan.
1110   NewPlan->VFs = VFs;
1111   NewPlan->UFs = UFs;
1112   // TODO: Adjust names.
1113   NewPlan->Name = Name;
1114   assert(Old2NewVPValues.contains(TripCount) &&
1115          "TripCount must have been added to Old2NewVPValues");
1116   NewPlan->TripCount = Old2NewVPValues[TripCount];
1117   return NewPlan;
1118 }
1119 
1120 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1121 
1122 Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
1123   return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") +
1124          Twine(getOrCreateBID(Block));
1125 }
1126 
1127 Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) {
1128   const std::string &Name = Block->getName();
1129   if (!Name.empty())
1130     return Name;
1131   return "VPB" + Twine(getOrCreateBID(Block));
1132 }
1133 
1134 void VPlanPrinter::dump() {
1135   Depth = 1;
1136   bumpIndent(0);
1137   OS << "digraph VPlan {\n";
1138   OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
1139   if (!Plan.getName().empty())
1140     OS << "\\n" << DOT::EscapeString(Plan.getName());
1141 
1142   {
1143     // Print live-ins.
1144   std::string Str;
1145   raw_string_ostream SS(Str);
1146   Plan.printLiveIns(SS);
1147   SmallVector<StringRef, 0> Lines;
1148   StringRef(Str).rtrim('\n').split(Lines, "\n");
1149   for (auto Line : Lines)
1150     OS << DOT::EscapeString(Line.str()) << "\\n";
1151   }
1152 
1153   OS << "\"]\n";
1154   OS << "node [shape=rect, fontname=Courier, fontsize=30]\n";
1155   OS << "edge [fontname=Courier, fontsize=30]\n";
1156   OS << "compound=true\n";
1157 
1158   dumpBlock(Plan.getPreheader());
1159 
1160   for (const VPBlockBase *Block : vp_depth_first_shallow(Plan.getEntry()))
1161     dumpBlock(Block);
1162 
1163   OS << "}\n";
1164 }
1165 
1166 void VPlanPrinter::dumpBlock(const VPBlockBase *Block) {
1167   if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block))
1168     dumpBasicBlock(BasicBlock);
1169   else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1170     dumpRegion(Region);
1171   else
1172     llvm_unreachable("Unsupported kind of VPBlock.");
1173 }
1174 
1175 void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To,
1176                             bool Hidden, const Twine &Label) {
1177   // Due to "dot" we print an edge between two regions as an edge between the
1178   // exiting basic block and the entry basic of the respective regions.
1179   const VPBlockBase *Tail = From->getExitingBasicBlock();
1180   const VPBlockBase *Head = To->getEntryBasicBlock();
1181   OS << Indent << getUID(Tail) << " -> " << getUID(Head);
1182   OS << " [ label=\"" << Label << '\"';
1183   if (Tail != From)
1184     OS << " ltail=" << getUID(From);
1185   if (Head != To)
1186     OS << " lhead=" << getUID(To);
1187   if (Hidden)
1188     OS << "; splines=none";
1189   OS << "]\n";
1190 }
1191 
1192 void VPlanPrinter::dumpEdges(const VPBlockBase *Block) {
1193   auto &Successors = Block->getSuccessors();
1194   if (Successors.size() == 1)
1195     drawEdge(Block, Successors.front(), false, "");
1196   else if (Successors.size() == 2) {
1197     drawEdge(Block, Successors.front(), false, "T");
1198     drawEdge(Block, Successors.back(), false, "F");
1199   } else {
1200     unsigned SuccessorNumber = 0;
1201     for (auto *Successor : Successors)
1202       drawEdge(Block, Successor, false, Twine(SuccessorNumber++));
1203   }
1204 }
1205 
1206 void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) {
1207   // Implement dot-formatted dump by performing plain-text dump into the
1208   // temporary storage followed by some post-processing.
1209   OS << Indent << getUID(BasicBlock) << " [label =\n";
1210   bumpIndent(1);
1211   std::string Str;
1212   raw_string_ostream SS(Str);
1213   // Use no indentation as we need to wrap the lines into quotes ourselves.
1214   BasicBlock->print(SS, "", SlotTracker);
1215 
1216   // We need to process each line of the output separately, so split
1217   // single-string plain-text dump.
1218   SmallVector<StringRef, 0> Lines;
1219   StringRef(Str).rtrim('\n').split(Lines, "\n");
1220 
1221   auto EmitLine = [&](StringRef Line, StringRef Suffix) {
1222     OS << Indent << '"' << DOT::EscapeString(Line.str()) << "\\l\"" << Suffix;
1223   };
1224 
1225   // Don't need the "+" after the last line.
1226   for (auto Line : make_range(Lines.begin(), Lines.end() - 1))
1227     EmitLine(Line, " +\n");
1228   EmitLine(Lines.back(), "\n");
1229 
1230   bumpIndent(-1);
1231   OS << Indent << "]\n";
1232 
1233   dumpEdges(BasicBlock);
1234 }
1235 
1236 void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
1237   OS << Indent << "subgraph " << getUID(Region) << " {\n";
1238   bumpIndent(1);
1239   OS << Indent << "fontname=Courier\n"
1240      << Indent << "label=\""
1241      << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ")
1242      << DOT::EscapeString(Region->getName()) << "\"\n";
1243   // Dump the blocks of the region.
1244   assert(Region->getEntry() && "Region contains no inner blocks.");
1245   for (const VPBlockBase *Block : vp_depth_first_shallow(Region->getEntry()))
1246     dumpBlock(Block);
1247   bumpIndent(-1);
1248   OS << Indent << "}\n";
1249   dumpEdges(Region);
1250 }
1251 
1252 void VPlanIngredient::print(raw_ostream &O) const {
1253   if (auto *Inst = dyn_cast<Instruction>(V)) {
1254     if (!Inst->getType()->isVoidTy()) {
1255       Inst->printAsOperand(O, false);
1256       O << " = ";
1257     }
1258     O << Inst->getOpcodeName() << " ";
1259     unsigned E = Inst->getNumOperands();
1260     if (E > 0) {
1261       Inst->getOperand(0)->printAsOperand(O, false);
1262       for (unsigned I = 1; I < E; ++I)
1263         Inst->getOperand(I)->printAsOperand(O << ", ", false);
1264     }
1265   } else // !Inst
1266     V->printAsOperand(O, false);
1267 }
1268 
1269 #endif
1270 
1271 template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
1272 
1273 void VPValue::replaceAllUsesWith(VPValue *New) {
1274   replaceUsesWithIf(New, [](VPUser &, unsigned) { return true; });
1275 }
1276 
1277 void VPValue::replaceUsesWithIf(
1278     VPValue *New,
1279     llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace) {
1280   // Note that this early exit is required for correctness; the implementation
1281   // below relies on the number of users for this VPValue to decrease, which
1282   // isn't the case if this == New.
1283   if (this == New)
1284     return;
1285 
1286   for (unsigned J = 0; J < getNumUsers();) {
1287     VPUser *User = Users[J];
1288     bool RemovedUser = false;
1289     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
1290       if (User->getOperand(I) != this || !ShouldReplace(*User, I))
1291         continue;
1292 
1293       RemovedUser = true;
1294       User->setOperand(I, New);
1295     }
1296     // If a user got removed after updating the current user, the next user to
1297     // update will be moved to the current position, so we only need to
1298     // increment the index if the number of users did not change.
1299     if (!RemovedUser)
1300       J++;
1301   }
1302 }
1303 
1304 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1305 void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {
1306   OS << Tracker.getOrCreateName(this);
1307 }
1308 
1309 void VPUser::printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const {
1310   interleaveComma(operands(), O, [&O, &SlotTracker](VPValue *Op) {
1311     Op->printAsOperand(O, SlotTracker);
1312   });
1313 }
1314 #endif
1315 
1316 void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region,
1317                                           Old2NewTy &Old2New,
1318                                           InterleavedAccessInfo &IAI) {
1319   ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
1320       RPOT(Region->getEntry());
1321   for (VPBlockBase *Base : RPOT) {
1322     visitBlock(Base, Old2New, IAI);
1323   }
1324 }
1325 
1326 void VPInterleavedAccessInfo::visitBlock(VPBlockBase *Block, Old2NewTy &Old2New,
1327                                          InterleavedAccessInfo &IAI) {
1328   if (VPBasicBlock *VPBB = dyn_cast<VPBasicBlock>(Block)) {
1329     for (VPRecipeBase &VPI : *VPBB) {
1330       if (isa<VPWidenPHIRecipe>(&VPI))
1331         continue;
1332       assert(isa<VPInstruction>(&VPI) && "Can only handle VPInstructions");
1333       auto *VPInst = cast<VPInstruction>(&VPI);
1334 
1335       auto *Inst = dyn_cast_or_null<Instruction>(VPInst->getUnderlyingValue());
1336       if (!Inst)
1337         continue;
1338       auto *IG = IAI.getInterleaveGroup(Inst);
1339       if (!IG)
1340         continue;
1341 
1342       auto NewIGIter = Old2New.find(IG);
1343       if (NewIGIter == Old2New.end())
1344         Old2New[IG] = new InterleaveGroup<VPInstruction>(
1345             IG->getFactor(), IG->isReverse(), IG->getAlign());
1346 
1347       if (Inst == IG->getInsertPos())
1348         Old2New[IG]->setInsertPos(VPInst);
1349 
1350       InterleaveGroupMap[VPInst] = Old2New[IG];
1351       InterleaveGroupMap[VPInst]->insertMember(
1352           VPInst, IG->getIndex(Inst),
1353           Align(IG->isReverse() ? (-1) * int(IG->getFactor())
1354                                 : IG->getFactor()));
1355     }
1356   } else if (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block))
1357     visitRegion(Region, Old2New, IAI);
1358   else
1359     llvm_unreachable("Unsupported kind of VPBlock.");
1360 }
1361 
1362 VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan,
1363                                                  InterleavedAccessInfo &IAI) {
1364   Old2NewTy Old2New;
1365   visitRegion(Plan.getVectorLoopRegion(), Old2New, IAI);
1366 }
1367 
1368 void VPSlotTracker::assignName(const VPValue *V) {
1369   assert(!VPValue2Name.contains(V) && "VPValue already has a name!");
1370   auto *UV = V->getUnderlyingValue();
1371   if (!UV) {
1372     VPValue2Name[V] = (Twine("vp<%") + Twine(NextSlot) + ">").str();
1373     NextSlot++;
1374     return;
1375   }
1376 
1377   // Use the name of the underlying Value, wrapped in "ir<>", and versioned by
1378   // appending ".Number" to the name if there are multiple uses.
1379   std::string Name;
1380   raw_string_ostream S(Name);
1381   UV->printAsOperand(S, false);
1382   assert(!Name.empty() && "Name cannot be empty.");
1383   std::string BaseName = (Twine("ir<") + Name + Twine(">")).str();
1384 
1385   // First assign the base name for V.
1386   const auto &[A, _] = VPValue2Name.insert({V, BaseName});
1387   // Integer or FP constants with different types will result in he same string
1388   // due to stripping types.
1389   if (V->isLiveIn() && isa<ConstantInt, ConstantFP>(UV))
1390     return;
1391 
1392   // If it is already used by C > 0 other VPValues, increase the version counter
1393   // C and use it for V.
1394   const auto &[C, UseInserted] = BaseName2Version.insert({BaseName, 0});
1395   if (!UseInserted) {
1396     C->second++;
1397     A->second = (BaseName + Twine(".") + Twine(C->second)).str();
1398   }
1399 }
1400 
1401 void VPSlotTracker::assignNames(const VPlan &Plan) {
1402   if (Plan.VFxUF.getNumUsers() > 0)
1403     assignName(&Plan.VFxUF);
1404   assignName(&Plan.VectorTripCount);
1405   if (Plan.BackedgeTakenCount)
1406     assignName(Plan.BackedgeTakenCount);
1407   for (VPValue *LI : Plan.VPLiveInsToFree)
1408     assignName(LI);
1409   assignNames(Plan.getPreheader());
1410 
1411   ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<const VPBlockBase *>>
1412       RPOT(VPBlockDeepTraversalWrapper<const VPBlockBase *>(Plan.getEntry()));
1413   for (const VPBasicBlock *VPBB :
1414        VPBlockUtils::blocksOnly<const VPBasicBlock>(RPOT))
1415     assignNames(VPBB);
1416 }
1417 
1418 void VPSlotTracker::assignNames(const VPBasicBlock *VPBB) {
1419   for (const VPRecipeBase &Recipe : *VPBB)
1420     for (VPValue *Def : Recipe.definedValues())
1421       assignName(Def);
1422 }
1423 
1424 std::string VPSlotTracker::getOrCreateName(const VPValue *V) const {
1425   std::string Name = VPValue2Name.lookup(V);
1426   if (!Name.empty())
1427     return Name;
1428 
1429   // If no name was assigned, no VPlan was provided when creating the slot
1430   // tracker or it is not reachable from the provided VPlan. This can happen,
1431   // e.g. when trying to print a recipe that has not been inserted into a VPlan
1432   // in a debugger.
1433   // TODO: Update VPSlotTracker constructor to assign names to recipes &
1434   // VPValues not associated with a VPlan, instead of constructing names ad-hoc
1435   // here.
1436   const VPRecipeBase *DefR = V->getDefiningRecipe();
1437   (void)DefR;
1438   assert((!DefR || !DefR->getParent() || !DefR->getParent()->getPlan()) &&
1439          "VPValue defined by a recipe in a VPlan?");
1440 
1441   // Use the underlying value's name, if there is one.
1442   if (auto *UV = V->getUnderlyingValue()) {
1443     std::string Name;
1444     raw_string_ostream S(Name);
1445     UV->printAsOperand(S, false);
1446     return (Twine("ir<") + Name + ">").str();
1447   }
1448 
1449   return "<badref>";
1450 }
1451 
1452 bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
1453   return all_of(Def->users(),
1454                 [Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
1455 }
1456 
1457 bool vputils::onlyFirstPartUsed(const VPValue *Def) {
1458   return all_of(Def->users(),
1459                 [Def](const VPUser *U) { return U->onlyFirstPartUsed(Def); });
1460 }
1461 
1462 VPValue *vputils::getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr,
1463                                                 ScalarEvolution &SE) {
1464   if (auto *Expanded = Plan.getSCEVExpansion(Expr))
1465     return Expanded;
1466   VPValue *Expanded = nullptr;
1467   if (auto *E = dyn_cast<SCEVConstant>(Expr))
1468     Expanded = Plan.getOrAddLiveIn(E->getValue());
1469   else if (auto *E = dyn_cast<SCEVUnknown>(Expr))
1470     Expanded = Plan.getOrAddLiveIn(E->getValue());
1471   else {
1472     Expanded = new VPExpandSCEVRecipe(Expr, SE);
1473     Plan.getPreheader()->appendRecipe(Expanded->getDefiningRecipe());
1474   }
1475   Plan.addSCEVExpansion(Expr, Expanded);
1476   return Expanded;
1477 }
1478 
1479 bool vputils::isHeaderMask(VPValue *V, VPlan &Plan) {
1480   if (isa<VPActiveLaneMaskPHIRecipe>(V))
1481     return true;
1482 
1483   auto IsWideCanonicalIV = [](VPValue *A) {
1484     return isa<VPWidenCanonicalIVRecipe>(A) ||
1485            (isa<VPWidenIntOrFpInductionRecipe>(A) &&
1486             cast<VPWidenIntOrFpInductionRecipe>(A)->isCanonical());
1487   };
1488 
1489   VPValue *A, *B;
1490   if (match(V, m_ActiveLaneMask(m_VPValue(A), m_VPValue(B))))
1491     return B == Plan.getTripCount() &&
1492            (match(A, m_ScalarIVSteps(m_CanonicalIV(), m_SpecificInt(1))) ||
1493             IsWideCanonicalIV(A));
1494 
1495   return match(V, m_Binary<Instruction::ICmp>(m_VPValue(A), m_VPValue(B))) &&
1496          IsWideCanonicalIV(A) && B == Plan.getOrCreateBackedgeTakenCount();
1497 }
1498