LLVM 22.0.0git
VPlan.cpp
Go to the documentation of this file.
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"
21#include "VPlanCFG.h"
22#include "VPlanDominatorTree.h"
23#include "VPlanHelpers.h"
24#include "VPlanPatternMatch.h"
25#include "VPlanTransforms.h"
26#include "VPlanUtils.h"
28#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/Twine.h"
34#include "llvm/IR/BasicBlock.h"
35#include "llvm/IR/CFG.h"
36#include "llvm/IR/IRBuilder.h"
37#include "llvm/IR/Instruction.h"
39#include "llvm/IR/Type.h"
40#include "llvm/IR/Value.h"
43#include "llvm/Support/Debug.h"
49#include <cassert>
50#include <string>
51
52using namespace llvm;
53using namespace llvm::VPlanPatternMatch;
54
55namespace llvm {
57}
58
59/// @{
60/// Metadata attribute names
61const char LLVMLoopVectorizeFollowupAll[] = "llvm.loop.vectorize.followup_all";
63 "llvm.loop.vectorize.followup_vectorized";
65 "llvm.loop.vectorize.followup_epilogue";
66/// @}
67
69
71 "vplan-print-in-dot-format", cl::Hidden,
72 cl::desc("Use dot format instead of plain text when dumping VPlans"));
73
74#define DEBUG_TYPE "loop-vectorize"
75
76#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
78 const VPBasicBlock *Parent = R.getParent();
79 VPSlotTracker SlotTracker(Parent ? Parent->getPlan() : nullptr);
80 R.print(OS, "", SlotTracker);
81 return OS;
82}
83#endif
84
86 const ElementCount &VF) const {
87 switch (LaneKind) {
89 // Lane = RuntimeVF - VF.getKnownMinValue() + Lane
90 return Builder.CreateSub(getRuntimeVF(Builder, Builder.getInt32Ty(), VF),
91 Builder.getInt32(VF.getKnownMinValue() - Lane));
93 return Builder.getInt32(Lane);
94 }
95 llvm_unreachable("Unknown lane kind");
96}
97
98VPValue::VPValue(const unsigned char SC, Value *UV, VPDef *Def)
99 : SubclassID(SC), UnderlyingVal(UV), Def(Def) {
100 if (Def)
101 Def->addDefinedValue(this);
102}
103
105 assert(Users.empty() && "trying to delete a VPValue with remaining users");
106 if (Def)
107 Def->removeDefinedValue(this);
108}
109
110#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
113 R->print(OS, "", SlotTracker);
114 else
116}
117
118void VPValue::dump() const {
119 const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this->Def);
121 (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
123 dbgs() << "\n";
124}
125
126void VPDef::dump() const {
127 const VPRecipeBase *Instr = dyn_cast_or_null<VPRecipeBase>(this);
129 (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr);
130 print(dbgs(), "", SlotTracker);
131 dbgs() << "\n";
132}
133#endif
134
138
142
143// Get the top-most entry block of \p Start. This is the entry block of the
144// containing VPlan. This function is templated to support both const and non-const blocks
145template <typename T> static T *getPlanEntry(T *Start) {
146 T *Next = Start;
147 T *Current = Start;
148 while ((Next = Next->getParent()))
149 Current = Next;
150
151 SmallSetVector<T *, 8> WorkList;
152 WorkList.insert(Current);
153
154 for (unsigned i = 0; i < WorkList.size(); i++) {
155 T *Current = WorkList[i];
156 if (!Current->hasPredecessors())
157 return Current;
158 auto &Predecessors = Current->getPredecessors();
159 WorkList.insert_range(Predecessors);
160 }
161
162 llvm_unreachable("VPlan without any entry node without predecessors");
163}
164
165VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; }
166
167const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; }
168
169/// \return the VPBasicBlock that is the entry of Block, possibly indirectly.
176
183
184void VPBlockBase::setPlan(VPlan *ParentPlan) {
185 assert(ParentPlan->getEntry() == this && "Can only set plan on its entry.");
186 Plan = ParentPlan;
187}
188
189/// \return the VPBasicBlock that is the exit of Block, possibly indirectly.
191 const VPBlockBase *Block = this;
193 Block = Region->getExiting();
195}
196
203
205 if (!Successors.empty() || !Parent)
206 return this;
207 assert(Parent->getExiting() == this &&
208 "Block w/o successors not the exiting block of its parent.");
209 return Parent->getEnclosingBlockWithSuccessors();
210}
211
213 if (!Predecessors.empty() || !Parent)
214 return this;
215 assert(Parent->getEntry() == this &&
216 "Block w/o predecessors not the entry of its parent.");
217 return Parent->getEnclosingBlockWithPredecessors();
218}
219
221 const VPDominatorTree &VPDT) {
222 auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
223 if (!VPBB)
224 return false;
225
226 // If VPBB is in a region R, VPBB is a loop header if R is a loop region with
227 // VPBB as its entry, i.e., free of predecessors.
228 if (auto *R = VPBB->getParent())
229 return !R->isReplicator() && !VPBB->hasPredecessors();
230
231 // A header dominates its second predecessor (the latch), with the other
232 // predecessor being the preheader
233 return VPB->getPredecessors().size() == 2 &&
234 VPDT.dominates(VPB, VPB->getPredecessors()[1]);
235}
236
238 const VPDominatorTree &VPDT) {
239 // A latch has a header as its second successor, with its other successor
240 // leaving the loop. A preheader OTOH has a header as its first (and only)
241 // successor.
242 return VPB->getNumSuccessors() == 2 &&
243 VPBlockUtils::isHeader(VPB->getSuccessors()[1], VPDT);
244}
245
247 iterator It = begin();
248 while (It != end() && It->isPhi())
249 It++;
250 return It;
251}
252
260
262 if (Def->isLiveIn())
263 return Def->getLiveInIRValue();
264
265 if (hasScalarValue(Def, Lane))
266 return Data.VPV2Scalars[Def][Lane.mapToCacheIndex(VF)];
267
268 if (!Lane.isFirstLane() && vputils::isSingleScalar(Def) &&
270 return Data.VPV2Scalars[Def][0];
271 }
272
273 // Look through BuildVector to avoid redundant extracts.
274 // TODO: Remove once replicate regions are unrolled explicitly.
275 if (Lane.getKind() == VPLane::Kind::First && match(Def, m_BuildVector())) {
276 auto *BuildVector = cast<VPInstruction>(Def);
277 return get(BuildVector->getOperand(Lane.getKnownLane()), true);
278 }
279
281 auto *VecPart = Data.VPV2Vector[Def];
282 if (!VecPart->getType()->isVectorTy()) {
283 assert(Lane.isFirstLane() && "cannot get lane > 0 for scalar");
284 return VecPart;
285 }
286 // TODO: Cache created scalar values.
287 Value *LaneV = Lane.getAsRuntimeExpr(Builder, VF);
288 auto *Extract = Builder.CreateExtractElement(VecPart, LaneV);
289 // set(Def, Extract, Instance);
290 return Extract;
291}
292
293Value *VPTransformState::get(const VPValue *Def, bool NeedsScalar) {
294 if (NeedsScalar) {
295 assert((VF.isScalar() || Def->isLiveIn() || hasVectorValue(Def) ||
297 (hasScalarValue(Def, VPLane(0)) &&
298 Data.VPV2Scalars[Def].size() == 1)) &&
299 "Trying to access a single scalar per part but has multiple scalars "
300 "per part.");
301 return get(Def, VPLane(0));
302 }
303
304 // If Values have been set for this Def return the one relevant for \p Part.
305 if (hasVectorValue(Def))
306 return Data.VPV2Vector[Def];
307
308 auto GetBroadcastInstrs = [this](Value *V) {
309 if (VF.isScalar())
310 return V;
311 // Broadcast the scalar into all locations in the vector.
312 Value *Shuf = Builder.CreateVectorSplat(VF, V, "broadcast");
313 return Shuf;
314 };
315
316 if (!hasScalarValue(Def, {0})) {
317 assert(Def->isLiveIn() && "expected a live-in");
318 Value *IRV = Def->getLiveInIRValue();
319 Value *B = GetBroadcastInstrs(IRV);
320 set(Def, B);
321 return B;
322 }
323
324 Value *ScalarValue = get(Def, VPLane(0));
325 // If we aren't vectorizing, we can just copy the scalar map values over
326 // to the vector map.
327 if (VF.isScalar()) {
328 set(Def, ScalarValue);
329 return ScalarValue;
330 }
331
332 bool IsSingleScalar = vputils::isSingleScalar(Def);
333
334 VPLane LastLane(IsSingleScalar ? 0 : VF.getFixedValue() - 1);
335 // Check if there is a scalar value for the selected lane.
336 if (!hasScalarValue(Def, LastLane)) {
337 // At the moment, VPWidenIntOrFpInductionRecipes, VPScalarIVStepsRecipes and
338 // VPExpandSCEVRecipes can also be a single scalar.
340 VPExpandSCEVRecipe>(Def->getDefiningRecipe())) &&
341 "unexpected recipe found to be invariant");
342 IsSingleScalar = true;
343 LastLane = 0;
344 }
345
346 auto *LastInst = cast<Instruction>(get(Def, LastLane));
347 // Set the insert point after the last scalarized instruction or after the
348 // last PHI, if LastInst is a PHI. This ensures the insertelement sequence
349 // will directly follow the scalar definitions.
350 auto OldIP = Builder.saveIP();
351 auto NewIP = isa<PHINode>(LastInst)
352 ? LastInst->getParent()->getFirstNonPHIIt()
353 : std::next(BasicBlock::iterator(LastInst));
354 Builder.SetInsertPoint(&*NewIP);
355
356 // However, if we are vectorizing, we need to construct the vector values.
357 // If the value is known to be uniform after vectorization, we can just
358 // broadcast the scalar value corresponding to lane zero. Otherwise, we
359 // construct the vector values using insertelement instructions. Since the
360 // resulting vectors are stored in State, we will only generate the
361 // insertelements once.
362 Value *VectorValue = nullptr;
363 if (IsSingleScalar) {
364 VectorValue = GetBroadcastInstrs(ScalarValue);
365 set(Def, VectorValue);
366 } else {
367 assert(!VF.isScalable() && "VF is assumed to be non scalable.");
369 "Explicit BuildVector recipes must have"
370 "handled packing for non-VPInstructions.");
371 // Initialize packing with insertelements to start from poison.
372 VectorValue = PoisonValue::get(toVectorizedTy(LastInst->getType(), VF));
373 for (unsigned Lane = 0; Lane < VF.getFixedValue(); ++Lane)
374 VectorValue = packScalarIntoVectorizedValue(Def, VectorValue, Lane);
375 set(Def, VectorValue);
376 }
377 Builder.restoreIP(OldIP);
378 return VectorValue;
379}
380
382 const DILocation *DIL = DL;
383 // When a FSDiscriminator is enabled, we don't need to add the multiply
384 // factors to the discriminators.
385 if (DIL &&
386 Builder.GetInsertBlock()
387 ->getParent()
388 ->shouldEmitDebugInfoForProfiling() &&
390 // FIXME: For scalable vectors, assume vscale=1.
391 unsigned UF = Plan->getUF();
392 auto NewDIL =
393 DIL->cloneByMultiplyingDuplicationFactor(UF * VF.getKnownMinValue());
394 if (NewDIL)
395 Builder.SetCurrentDebugLocation(*NewDIL);
396 else
397 LLVM_DEBUG(dbgs() << "Failed to create new discriminator: "
398 << DIL->getFilename() << " Line: " << DIL->getLine());
399 } else
400 Builder.SetCurrentDebugLocation(DL);
401}
402
404 Value *WideValue,
405 const VPLane &Lane) {
406 Value *ScalarInst = get(Def, Lane);
407 Value *LaneExpr = Lane.getAsRuntimeExpr(Builder, VF);
408 if (auto *StructTy = dyn_cast<StructType>(WideValue->getType())) {
409 // We must handle each element of a vectorized struct type.
410 for (unsigned I = 0, E = StructTy->getNumElements(); I != E; I++) {
411 Value *ScalarValue = Builder.CreateExtractValue(ScalarInst, I);
412 Value *VectorValue = Builder.CreateExtractValue(WideValue, I);
413 VectorValue =
414 Builder.CreateInsertElement(VectorValue, ScalarValue, LaneExpr);
415 WideValue = Builder.CreateInsertValue(WideValue, VectorValue, I);
416 }
417 } else {
418 WideValue = Builder.CreateInsertElement(WideValue, ScalarInst, LaneExpr);
419 }
420 return WideValue;
421}
422
423BasicBlock *VPBasicBlock::createEmptyBasicBlock(VPTransformState &State) {
424 auto &CFG = State.CFG;
425 // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks.
426 // Pred stands for Predessor. Prev stands for Previous - last visited/created.
427 BasicBlock *PrevBB = CFG.PrevBB;
428 BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(),
429 PrevBB->getParent(), CFG.ExitBB);
430 LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n');
431
432 return NewBB;
433}
434
436 auto &CFG = State.CFG;
437 BasicBlock *NewBB = CFG.VPBB2IRBB[this];
438
439 // Register NewBB in its loop. In innermost loops its the same for all
440 // BB's.
441 Loop *ParentLoop = State.CurrentParentLoop;
442 // If this block has a sole successor that is an exit block or is an exit
443 // block itself then it needs adding to the same parent loop as the exit
444 // block.
445 VPBlockBase *SuccOrExitVPB = getSingleSuccessor();
446 SuccOrExitVPB = SuccOrExitVPB ? SuccOrExitVPB : this;
447 if (State.Plan->isExitBlock(SuccOrExitVPB)) {
448 ParentLoop = State.LI->getLoopFor(
449 cast<VPIRBasicBlock>(SuccOrExitVPB)->getIRBasicBlock());
450 }
451
452 if (ParentLoop && !State.LI->getLoopFor(NewBB))
453 ParentLoop->addBasicBlockToLoop(NewBB, *State.LI);
454
456 if (VPBlockUtils::isHeader(this, State.VPDT)) {
457 // There's no block for the latch yet, connect to the preheader only.
458 Preds = {getPredecessors()[0]};
459 } else {
460 Preds = to_vector(getPredecessors());
461 }
462
463 // Hook up the new basic block to its predecessors.
464 for (VPBlockBase *PredVPBlock : Preds) {
465 VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock();
466 auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors();
467 assert(CFG.VPBB2IRBB.contains(PredVPBB) &&
468 "Predecessor basic-block not found building successor.");
469 BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB];
470 auto *PredBBTerminator = PredBB->getTerminator();
471 LLVM_DEBUG(dbgs() << "LV: draw edge from " << PredBB->getName() << '\n');
472
473 auto *TermBr = dyn_cast<BranchInst>(PredBBTerminator);
474 if (isa<UnreachableInst>(PredBBTerminator)) {
475 assert(PredVPSuccessors.size() == 1 &&
476 "Predecessor ending w/o branch must have single successor.");
477 DebugLoc DL = PredBBTerminator->getDebugLoc();
478 PredBBTerminator->eraseFromParent();
479 auto *Br = BranchInst::Create(NewBB, PredBB);
480 Br->setDebugLoc(DL);
481 } else if (TermBr && !TermBr->isConditional()) {
482 TermBr->setSuccessor(0, NewBB);
483 } else {
484 // Set each forward successor here when it is created, excluding
485 // backedges. A backward successor is set when the branch is created.
486 // Branches to VPIRBasicBlocks must have the same successors in VPlan as
487 // in the original IR, except when the predecessor is the entry block.
488 // This enables including SCEV and memory runtime check blocks in VPlan.
489 // TODO: Remove exception by modeling the terminator of entry block using
490 // BranchOnCond.
491 unsigned idx = PredVPSuccessors.front() == this ? 0 : 1;
492 assert((TermBr && (!TermBr->getSuccessor(idx) ||
493 (isa<VPIRBasicBlock>(this) &&
494 (TermBr->getSuccessor(idx) == NewBB ||
495 PredVPBlock == getPlan()->getEntry())))) &&
496 "Trying to reset an existing successor block.");
497 TermBr->setSuccessor(idx, NewBB);
498 }
499 CFG.DTU.applyUpdates({{DominatorTree::Insert, PredBB, NewBB}});
500 }
501}
502
505 "VPIRBasicBlock can have at most two successors at the moment!");
506 // Move completely disconnected blocks to their final position.
507 if (IRBB->hasNPredecessors(0) && succ_begin(IRBB) == succ_end(IRBB))
508 IRBB->moveAfter(State->CFG.PrevBB);
509 State->Builder.SetInsertPoint(IRBB->getTerminator());
510 State->CFG.PrevBB = IRBB;
511 State->CFG.VPBB2IRBB[this] = IRBB;
512 executeRecipes(State, IRBB);
513 // Create a branch instruction to terminate IRBB if one was not created yet
514 // and is needed.
515 if (getSingleSuccessor() && isa<UnreachableInst>(IRBB->getTerminator())) {
516 auto *Br = State->Builder.CreateBr(IRBB);
517 Br->setOperand(0, nullptr);
518 IRBB->getTerminator()->eraseFromParent();
519 } else {
520 assert(
521 (getNumSuccessors() == 0 || isa<BranchInst>(IRBB->getTerminator())) &&
522 "other blocks must be terminated by a branch");
523 }
524
525 connectToPredecessors(*State);
526}
527
528VPIRBasicBlock *VPIRBasicBlock::clone() {
529 auto *NewBlock = getPlan()->createEmptyVPIRBasicBlock(IRBB);
530 for (VPRecipeBase &R : Recipes)
531 NewBlock->appendRecipe(R.clone());
532 return NewBlock;
533}
534
536 bool Replica = bool(State->Lane);
537 BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible.
538
539 if (VPBlockUtils::isHeader(this, State->VPDT)) {
540 // Create and register the new vector loop.
541 Loop *PrevParentLoop = State->CurrentParentLoop;
542 State->CurrentParentLoop = State->LI->AllocateLoop();
543
544 // Insert the new loop into the loop nest and register the new basic blocks
545 // before calling any utilities such as SCEV that require valid LoopInfo.
546 if (PrevParentLoop)
547 PrevParentLoop->addChildLoop(State->CurrentParentLoop);
548 else
549 State->LI->addTopLevelLoop(State->CurrentParentLoop);
550 }
551
552 auto IsReplicateRegion = [](VPBlockBase *BB) {
554 assert((!R || R->isReplicator()) &&
555 "only replicate region blocks should remain");
556 return R;
557 };
558 // 1. Create an IR basic block.
559 if ((Replica && this == getParent()->getEntry()) ||
560 IsReplicateRegion(getSingleHierarchicalPredecessor())) {
561 // Reuse the previous basic block if the current VPBB is either
562 // * the entry to a replicate region, or
563 // * the exit of a replicate region.
564 State->CFG.VPBB2IRBB[this] = NewBB;
565 } else {
566 NewBB = createEmptyBasicBlock(*State);
567
568 State->Builder.SetInsertPoint(NewBB);
569 // Temporarily terminate with unreachable until CFG is rewired.
570 UnreachableInst *Terminator = State->Builder.CreateUnreachable();
571 State->Builder.SetInsertPoint(Terminator);
572
573 State->CFG.PrevBB = NewBB;
574 State->CFG.VPBB2IRBB[this] = NewBB;
575 connectToPredecessors(*State);
576 }
577
578 // 2. Fill the IR basic block with IR instructions.
579 executeRecipes(State, NewBB);
580
581 // If this block is a latch, update CurrentParentLoop.
582 if (VPBlockUtils::isLatch(this, State->VPDT))
583 State->CurrentParentLoop = State->CurrentParentLoop->getParentLoop();
584}
585
586VPBasicBlock *VPBasicBlock::clone() {
587 auto *NewBlock = getPlan()->createVPBasicBlock(getName());
588 for (VPRecipeBase &R : *this)
589 NewBlock->appendRecipe(R.clone());
590 return NewBlock;
591}
592
594 LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB: " << getName()
595 << " in BB: " << BB->getName() << '\n');
596
597 State->CFG.PrevVPBB = this;
598
599 for (VPRecipeBase &Recipe : Recipes) {
600 State->setDebugLocFrom(Recipe.getDebugLoc());
601 Recipe.execute(*State);
602 }
603
604 LLVM_DEBUG(dbgs() << "LV: filled BB: " << *BB);
605}
606
607VPBasicBlock *VPBasicBlock::splitAt(iterator SplitAt) {
608 assert((SplitAt == end() || SplitAt->getParent() == this) &&
609 "can only split at a position in the same block");
610
611 // Create new empty block after the block to split.
612 auto *SplitBlock = getPlan()->createVPBasicBlock(getName() + ".split");
614
615 // Finally, move the recipes starting at SplitAt to new block.
616 for (VPRecipeBase &ToMove :
617 make_early_inc_range(make_range(SplitAt, this->end())))
618 ToMove.moveBefore(*SplitBlock, SplitBlock->end());
619
620 return SplitBlock;
621}
622
623/// Return the enclosing loop region for region \p P. The templated version is
624/// used to support both const and non-const block arguments.
625template <typename T> static T *getEnclosingLoopRegionForRegion(T *P) {
626 if (P && P->isReplicator()) {
627 P = P->getParent();
628 // Multiple loop regions can be nested, but replicate regions can only be
629 // nested inside a loop region or must be outside any other region.
630 assert((!P || !P->isReplicator()) && "unexpected nested replicate regions");
631 }
632 return P;
633}
634
638
642
643static bool hasConditionalTerminator(const VPBasicBlock *VPBB) {
644 if (VPBB->empty()) {
645 assert(
646 VPBB->getNumSuccessors() < 2 &&
647 "block with multiple successors doesn't have a recipe as terminator");
648 return false;
649 }
650
651 const VPRecipeBase *R = &VPBB->back();
652 bool IsSwitch = isa<VPInstruction>(R) &&
653 cast<VPInstruction>(R)->getOpcode() == Instruction::Switch;
657 (void)IsCondBranch;
658 (void)IsSwitch;
659 if (VPBB->getNumSuccessors() == 2 ||
660 (VPBB->isExiting() && !VPBB->getParent()->isReplicator())) {
661 assert((IsCondBranch || IsSwitch) &&
662 "block with multiple successors not terminated by "
663 "conditional branch nor switch recipe");
664
665 return true;
666 }
667
668 if (VPBB->getNumSuccessors() > 2) {
669 assert(IsSwitch && "block with more than 2 successors not terminated by "
670 "a switch recipe");
671 return true;
672 }
673
674 assert(
675 !IsCondBranch &&
676 "block with 0 or 1 successors terminated by conditional branch recipe");
677 return false;
678}
679
681 if (hasConditionalTerminator(this))
682 return &back();
683 return nullptr;
684}
685
687 if (hasConditionalTerminator(this))
688 return &back();
689 return nullptr;
690}
691
693 return getParent() && getParent()->getExitingBasicBlock() == this;
694}
695
696#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
701
702void VPBlockBase::printSuccessors(raw_ostream &O, const Twine &Indent) const {
703 if (getSuccessors().empty()) {
704 O << Indent << "No successors\n";
705 } else {
706 O << Indent << "Successor(s): ";
707 ListSeparator LS;
708 for (auto *Succ : getSuccessors())
709 O << LS << Succ->getName();
710 O << '\n';
711 }
712}
713
714void VPBasicBlock::print(raw_ostream &O, const Twine &Indent,
715 VPSlotTracker &SlotTracker) const {
716 O << Indent << getName() << ":\n";
717
718 auto RecipeIndent = Indent + " ";
719 for (const VPRecipeBase &Recipe : *this) {
720 Recipe.print(O, RecipeIndent, SlotTracker);
721 O << '\n';
722 }
723
724 printSuccessors(O, Indent);
725}
726#endif
727
728static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry);
729
730// Clone the CFG for all nodes reachable from \p Entry, this includes cloning
731// the blocks and their recipes. Operands of cloned recipes will NOT be updated.
732// Remapping of operands must be done separately. Returns a pair with the new
733// entry and exiting blocks of the cloned region. If \p Entry isn't part of a
734// region, return nullptr for the exiting block.
735static std::pair<VPBlockBase *, VPBlockBase *> cloneFrom(VPBlockBase *Entry) {
737 VPBlockBase *Exiting = nullptr;
738 bool InRegion = Entry->getParent();
739 // First, clone blocks reachable from Entry.
740 for (VPBlockBase *BB : vp_depth_first_shallow(Entry)) {
741 VPBlockBase *NewBB = BB->clone();
742 Old2NewVPBlocks[BB] = NewBB;
743 if (InRegion && BB->getNumSuccessors() == 0) {
744 assert(!Exiting && "Multiple exiting blocks?");
745 Exiting = BB;
746 }
747 }
748 assert((!InRegion || Exiting) && "regions must have a single exiting block");
749
750 // Second, update the predecessors & successors of the cloned blocks.
751 for (VPBlockBase *BB : vp_depth_first_shallow(Entry)) {
752 VPBlockBase *NewBB = Old2NewVPBlocks[BB];
754 for (VPBlockBase *Pred : BB->getPredecessors()) {
755 NewPreds.push_back(Old2NewVPBlocks[Pred]);
756 }
757 NewBB->setPredecessors(NewPreds);
759 for (VPBlockBase *Succ : BB->successors()) {
760 NewSuccs.push_back(Old2NewVPBlocks[Succ]);
761 }
762 NewBB->setSuccessors(NewSuccs);
763 }
764
765#if !defined(NDEBUG)
766 // Verify that the order of predecessors and successors matches in the cloned
767 // version.
768 for (const auto &[OldBB, NewBB] :
770 vp_depth_first_shallow(Old2NewVPBlocks[Entry]))) {
771 for (const auto &[OldPred, NewPred] :
772 zip(OldBB->getPredecessors(), NewBB->getPredecessors()))
773 assert(NewPred == Old2NewVPBlocks[OldPred] && "Different predecessors");
774
775 for (const auto &[OldSucc, NewSucc] :
776 zip(OldBB->successors(), NewBB->successors()))
777 assert(NewSucc == Old2NewVPBlocks[OldSucc] && "Different successors");
778 }
779#endif
780
781 return std::make_pair(Old2NewVPBlocks[Entry],
782 Exiting ? Old2NewVPBlocks[Exiting] : nullptr);
783}
784
785VPRegionBlock *VPRegionBlock::clone() {
786 const auto &[NewEntry, NewExiting] = cloneFrom(getEntry());
787 auto *NewRegion = getPlan()->createVPRegionBlock(NewEntry, NewExiting,
788 getName(), isReplicator());
789 for (VPBlockBase *Block : vp_depth_first_shallow(NewEntry))
790 Block->setParent(NewRegion);
791 return NewRegion;
792}
793
796 "Loop regions should have been lowered to plain CFG");
797 assert(!State->Lane && "Replicating a Region with non-null instance.");
798 assert(!State->VF.isScalable() && "VF is assumed to be non scalable.");
799
801 Entry);
802 State->Lane = VPLane(0);
803 for (unsigned Lane = 0, VF = State->VF.getFixedValue(); Lane < VF; ++Lane) {
804 State->Lane = VPLane(Lane, VPLane::Kind::First);
805 // Visit the VPBlocks connected to \p this, starting from it.
806 for (VPBlockBase *Block : RPOT) {
807 LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n');
808 Block->execute(State);
809 }
810 }
811
812 // Exit replicating mode.
813 State->Lane.reset();
814}
815
818 for (VPRecipeBase &R : Recipes)
819 Cost += R.cost(VF, Ctx);
820 return Cost;
821}
822
823const VPBasicBlock *VPBasicBlock::getCFGPredecessor(unsigned Idx) const {
824 const VPBlockBase *Pred = nullptr;
825 if (hasPredecessors()) {
826 Pred = getPredecessors()[Idx];
827 } else {
828 auto *Region = getParent();
829 assert(Region && !Region->isReplicator() && Region->getEntry() == this &&
830 "must be in the entry block of a non-replicate region");
831 assert(Idx < 2 && Region->getNumPredecessors() == 1 &&
832 "loop region has a single predecessor (preheader), its entry block "
833 "has 2 incoming blocks");
834
835 // Idx == 0 selects the predecessor of the region, Idx == 1 selects the
836 // region itself whose exiting block feeds the phi across the backedge.
837 Pred = Idx == 0 ? Region->getSinglePredecessor() : Region;
838 }
839 return Pred->getExitingBasicBlock();
840}
841
843 if (!isReplicator()) {
846 Cost += Block->cost(VF, Ctx);
847 InstructionCost BackedgeCost =
848 ForceTargetInstructionCost.getNumOccurrences()
849 ? InstructionCost(ForceTargetInstructionCost.getNumOccurrences())
850 : Ctx.TTI.getCFInstrCost(Instruction::Br, Ctx.CostKind);
851 LLVM_DEBUG(dbgs() << "Cost of " << BackedgeCost << " for VF " << VF
852 << ": vector loop backedge\n");
853 Cost += BackedgeCost;
854 return Cost;
855 }
856
857 // Compute the cost of a replicate region. Replicating isn't supported for
858 // scalable vectors, return an invalid cost for them.
859 // TODO: Discard scalable VPlans with replicate recipes earlier after
860 // construction.
861 if (VF.isScalable())
863
864 // First compute the cost of the conditionally executed recipes, followed by
865 // account for the branching cost, except if the mask is a header mask or
866 // uniform condition.
867 using namespace llvm::VPlanPatternMatch;
869 InstructionCost ThenCost = Then->cost(VF, Ctx);
870
871 // For the scalar case, we may not always execute the original predicated
872 // block, Thus, scale the block's cost by the probability of executing it.
873 if (VF.isScalar())
874 return ThenCost / getPredBlockCostDivisor(Ctx.CostKind);
875
876 return ThenCost;
877}
878
879#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
881 VPSlotTracker &SlotTracker) const {
882 O << Indent << (isReplicator() ? "<xVFxUF> " : "<x1> ") << getName() << ": {";
883 auto NewIndent = Indent + " ";
884 for (auto *BlockBase : vp_depth_first_shallow(Entry)) {
885 O << '\n';
886 BlockBase->print(O, NewIndent, SlotTracker);
887 }
888 O << Indent << "}\n";
889
890 printSuccessors(O, Indent);
891}
892#endif
893
895 auto *Header = cast<VPBasicBlock>(getEntry());
896 if (auto *CanIV = dyn_cast<VPCanonicalIVPHIRecipe>(&Header->front())) {
897 assert(this == getPlan()->getVectorLoopRegion() &&
898 "Canonical IV must be in the entry of the top-level loop region");
899 auto *ScalarR = VPBuilder(CanIV).createScalarPhi(
900 {CanIV->getStartValue(), CanIV->getBackedgeValue()},
901 CanIV->getDebugLoc(), "index");
902 CanIV->replaceAllUsesWith(ScalarR);
903 CanIV->eraseFromParent();
904 }
905
906 VPBlockBase *Preheader = getSinglePredecessor();
907 auto *ExitingLatch = cast<VPBasicBlock>(getExiting());
909 VPBlockUtils::disconnectBlocks(Preheader, this);
910 VPBlockUtils::disconnectBlocks(this, Middle);
911
912 for (VPBlockBase *VPB : vp_depth_first_shallow(Entry))
913 VPB->setParent(getParent());
914
915 VPBlockUtils::connectBlocks(Preheader, Header);
916 VPBlockUtils::connectBlocks(ExitingLatch, Middle);
917 VPBlockUtils::connectBlocks(ExitingLatch, Header);
918}
919
920VPlan::VPlan(Loop *L) {
921 setEntry(createVPIRBasicBlock(L->getLoopPreheader()));
922 ScalarHeader = createVPIRBasicBlock(L->getHeader());
923
924 SmallVector<BasicBlock *> IRExitBlocks;
925 L->getUniqueExitBlocks(IRExitBlocks);
926 for (BasicBlock *EB : IRExitBlocks)
927 ExitBlocks.push_back(createVPIRBasicBlock(EB));
928}
929
931 VPValue DummyValue;
932
933 for (auto *VPB : CreatedBlocks) {
934 if (auto *VPBB = dyn_cast<VPBasicBlock>(VPB)) {
935 // Replace all operands of recipes and all VPValues defined in VPBB with
936 // DummyValue so the block can be deleted.
937 for (VPRecipeBase &R : *VPBB) {
938 for (auto *Def : R.definedValues())
939 Def->replaceAllUsesWith(&DummyValue);
940
941 for (unsigned I = 0, E = R.getNumOperands(); I != E; I++)
942 R.setOperand(I, &DummyValue);
943 }
944 }
945 delete VPB;
946 }
947 for (VPValue *VPV : getLiveIns())
948 delete VPV;
949 if (BackedgeTakenCount)
950 delete BackedgeTakenCount;
951}
952
954 auto Iter = find_if(getExitBlocks(), [IRBB](const VPIRBasicBlock *VPIRBB) {
955 return VPIRBB->getIRBasicBlock() == IRBB;
956 });
957 assert(Iter != getExitBlocks().end() && "no exit block found");
958 return *Iter;
959}
960
962 return is_contained(ExitBlocks, VPBB);
963}
964
965/// Generate the code inside the preheader and body of the vectorized loop.
966/// Assumes a single pre-header basic-block was created for this. Introduce
967/// additional basic-blocks as needed, and fill them all.
969 // Initialize CFG state.
970 State->CFG.PrevVPBB = nullptr;
971 State->CFG.ExitBB = State->CFG.PrevBB->getSingleSuccessor();
972
973 // Update VPDominatorTree since VPBasicBlock may be removed after State was
974 // constructed.
975 State->VPDT.recalculate(*this);
976
977 // Disconnect VectorPreHeader from ExitBB in both the CFG and DT.
978 BasicBlock *VectorPreHeader = State->CFG.PrevBB;
979 cast<BranchInst>(VectorPreHeader->getTerminator())->setSuccessor(0, nullptr);
980 State->CFG.DTU.applyUpdates(
981 {{DominatorTree::Delete, VectorPreHeader, State->CFG.ExitBB}});
982
983 LLVM_DEBUG(dbgs() << "Executing best plan with VF=" << State->VF
984 << ", UF=" << getUF() << '\n');
985 setName("Final VPlan");
986 LLVM_DEBUG(dump());
987
988 // Disconnect scalar preheader and scalar header, as the dominator tree edge
989 // will be updated as part of VPlan execution. This allows keeping the DTU
990 // logic generic during VPlan execution.
991 BasicBlock *ScalarPh = State->CFG.ExitBB;
992 State->CFG.DTU.applyUpdates(
993 {{DominatorTree::Delete, ScalarPh, ScalarPh->getSingleSuccessor()}});
994
996 Entry);
997 // Generate code for the VPlan, in parts of the vector skeleton, loop body and
998 // successor blocks including the middle, exit and scalar preheader blocks.
999 for (VPBlockBase *Block : RPOT)
1000 Block->execute(State);
1001
1002 State->CFG.DTU.flush();
1003
1004 VPBasicBlock *Header = vputils::getFirstLoopHeader(*this, State->VPDT);
1005 if (!Header)
1006 return;
1007
1008 auto *LatchVPBB = cast<VPBasicBlock>(Header->getPredecessors()[1]);
1009 BasicBlock *VectorLatchBB = State->CFG.VPBB2IRBB[LatchVPBB];
1010
1011 // Fix the latch value of canonical, reduction and first-order recurrences
1012 // phis in the vector loop.
1013 for (VPRecipeBase &R : Header->phis()) {
1014 // Skip phi-like recipes that generate their backedege values themselves.
1015 if (isa<VPWidenPHIRecipe>(&R))
1016 continue;
1017
1018 auto *PhiR = cast<VPSingleDefRecipe>(&R);
1019 // VPInstructions currently model scalar Phis only.
1020 bool NeedsScalar = isa<VPInstruction>(PhiR) ||
1022 cast<VPReductionPHIRecipe>(PhiR)->isInLoop());
1023
1024 Value *Phi = State->get(PhiR, NeedsScalar);
1025 // VPHeaderPHIRecipe supports getBackedgeValue() but VPInstruction does
1026 // not.
1027 Value *Val = State->get(PhiR->getOperand(1), NeedsScalar);
1028 cast<PHINode>(Phi)->addIncoming(Val, VectorLatchBB);
1029 }
1030}
1031
1033 // For now only return the cost of the vector loop region, ignoring any other
1034 // blocks, like the preheader or middle blocks, expect for checking them for
1035 // recipes with invalid costs.
1037
1038 // If the cost of the loop region is invalid or any recipe in the skeleton
1039 // outside loop regions are invalid return an invalid cost.
1042 [&VF, &Ctx](VPBasicBlock *VPBB) {
1043 return !VPBB->cost(VF, Ctx).isValid();
1044 }))
1046
1047 return Cost;
1048}
1049
1051 // TODO: Cache if possible.
1053 if (auto *R = dyn_cast<VPRegionBlock>(B))
1054 return R->isReplicator() ? nullptr : R;
1055 return nullptr;
1056}
1057
1060 if (auto *R = dyn_cast<VPRegionBlock>(B))
1061 return R->isReplicator() ? nullptr : R;
1062 return nullptr;
1063}
1064
1065#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1068
1069 if (VF.getNumUsers() > 0) {
1070 O << "\nLive-in ";
1071 VF.printAsOperand(O, SlotTracker);
1072 O << " = VF";
1073 }
1074
1075 if (VFxUF.getNumUsers() > 0) {
1076 O << "\nLive-in ";
1077 VFxUF.printAsOperand(O, SlotTracker);
1078 O << " = VF * UF";
1079 }
1080
1081 if (VectorTripCount.getNumUsers() > 0) {
1082 O << "\nLive-in ";
1083 VectorTripCount.printAsOperand(O, SlotTracker);
1084 O << " = vector-trip-count";
1085 }
1086
1087 if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) {
1088 O << "\nLive-in ";
1089 BackedgeTakenCount->printAsOperand(O, SlotTracker);
1090 O << " = backedge-taken count";
1091 }
1092
1093 O << "\n";
1094 if (TripCount) {
1095 if (TripCount->isLiveIn())
1096 O << "Live-in ";
1097 TripCount->printAsOperand(O, SlotTracker);
1098 O << " = original trip-count";
1099 O << "\n";
1100 }
1101}
1102
1106
1107 O << "VPlan '" << getName() << "' {";
1108
1109 printLiveIns(O);
1110
1112 RPOT(getEntry());
1113 for (const VPBlockBase *Block : RPOT) {
1114 O << '\n';
1115 Block->print(O, "", SlotTracker);
1116 }
1117
1118 O << "}\n";
1119}
1120
1121std::string VPlan::getName() const {
1122 std::string Out;
1123 raw_string_ostream RSO(Out);
1124 RSO << Name << " for ";
1125 if (!VFs.empty()) {
1126 RSO << "VF={" << VFs[0];
1127 for (ElementCount VF : drop_begin(VFs))
1128 RSO << "," << VF;
1129 RSO << "},";
1130 }
1131
1132 if (UFs.empty()) {
1133 RSO << "UF>=1";
1134 } else {
1135 RSO << "UF={" << UFs[0];
1136 for (unsigned UF : drop_begin(UFs))
1137 RSO << "," << UF;
1138 RSO << "}";
1139 }
1140
1141 return Out;
1142}
1143
1146 VPlanPrinter Printer(O, *this);
1147 Printer.dump();
1148}
1149
1151void VPlan::dump() const { print(dbgs()); }
1152#endif
1153
1154static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry,
1155 DenseMap<VPValue *, VPValue *> &Old2NewVPValues) {
1156 // Update the operands of all cloned recipes starting at NewEntry. This
1157 // traverses all reachable blocks. This is done in two steps, to handle cycles
1158 // in PHI recipes.
1160 OldDeepRPOT(Entry);
1162 NewDeepRPOT(NewEntry);
1163 // First, collect all mappings from old to new VPValues defined by cloned
1164 // recipes.
1165 for (const auto &[OldBB, NewBB] :
1168 assert(OldBB->getRecipeList().size() == NewBB->getRecipeList().size() &&
1169 "blocks must have the same number of recipes");
1170 for (const auto &[OldR, NewR] : zip(*OldBB, *NewBB)) {
1171 assert(OldR.getNumOperands() == NewR.getNumOperands() &&
1172 "recipes must have the same number of operands");
1173 assert(OldR.getNumDefinedValues() == NewR.getNumDefinedValues() &&
1174 "recipes must define the same number of operands");
1175 for (const auto &[OldV, NewV] :
1176 zip(OldR.definedValues(), NewR.definedValues()))
1177 Old2NewVPValues[OldV] = NewV;
1178 }
1179 }
1180
1181 // Update all operands to use cloned VPValues.
1182 for (VPBasicBlock *NewBB :
1184 for (VPRecipeBase &NewR : *NewBB)
1185 for (unsigned I = 0, E = NewR.getNumOperands(); I != E; ++I) {
1186 VPValue *NewOp = Old2NewVPValues.lookup(NewR.getOperand(I));
1187 NewR.setOperand(I, NewOp);
1188 }
1189 }
1190}
1191
1193 unsigned NumBlocksBeforeCloning = CreatedBlocks.size();
1194 // Clone blocks.
1195 const auto &[NewEntry, __] = cloneFrom(Entry);
1196
1197 BasicBlock *ScalarHeaderIRBB = getScalarHeader()->getIRBasicBlock();
1198 VPIRBasicBlock *NewScalarHeader = nullptr;
1199 if (getScalarHeader()->hasPredecessors()) {
1200 NewScalarHeader = cast<VPIRBasicBlock>(*find_if(
1201 vp_depth_first_shallow(NewEntry), [ScalarHeaderIRBB](VPBlockBase *VPB) {
1202 auto *VPIRBB = dyn_cast<VPIRBasicBlock>(VPB);
1203 return VPIRBB && VPIRBB->getIRBasicBlock() == ScalarHeaderIRBB;
1204 }));
1205 } else {
1206 NewScalarHeader = createVPIRBasicBlock(ScalarHeaderIRBB);
1207 }
1208 // Create VPlan, clone live-ins and remap operands in the cloned blocks.
1209 auto *NewPlan = new VPlan(cast<VPBasicBlock>(NewEntry), NewScalarHeader);
1210 DenseMap<VPValue *, VPValue *> Old2NewVPValues;
1211 for (VPValue *OldLiveIn : getLiveIns()) {
1212 Old2NewVPValues[OldLiveIn] =
1213 NewPlan->getOrAddLiveIn(OldLiveIn->getLiveInIRValue());
1214 }
1215 Old2NewVPValues[&VectorTripCount] = &NewPlan->VectorTripCount;
1216 Old2NewVPValues[&VF] = &NewPlan->VF;
1217 Old2NewVPValues[&VFxUF] = &NewPlan->VFxUF;
1218 if (BackedgeTakenCount) {
1219 NewPlan->BackedgeTakenCount = new VPValue();
1220 Old2NewVPValues[BackedgeTakenCount] = NewPlan->BackedgeTakenCount;
1221 }
1222 if (TripCount && TripCount->isLiveIn())
1223 Old2NewVPValues[TripCount] =
1224 NewPlan->getOrAddLiveIn(TripCount->getLiveInIRValue());
1225 // else NewTripCount will be created and inserted into Old2NewVPValues when
1226 // TripCount is cloned. In any case NewPlan->TripCount is updated below.
1227
1228 remapOperands(Entry, NewEntry, Old2NewVPValues);
1229
1230 // Initialize remaining fields of cloned VPlan.
1231 NewPlan->VFs = VFs;
1232 NewPlan->UFs = UFs;
1233 // TODO: Adjust names.
1234 NewPlan->Name = Name;
1235 if (TripCount) {
1236 assert(Old2NewVPValues.contains(TripCount) &&
1237 "TripCount must have been added to Old2NewVPValues");
1238 NewPlan->TripCount = Old2NewVPValues[TripCount];
1239 }
1240
1241 // Transfer all cloned blocks (the second half of all current blocks) from
1242 // current to new VPlan.
1243 unsigned NumBlocksAfterCloning = CreatedBlocks.size();
1244 for (unsigned I :
1245 seq<unsigned>(NumBlocksBeforeCloning, NumBlocksAfterCloning))
1246 NewPlan->CreatedBlocks.push_back(this->CreatedBlocks[I]);
1247 CreatedBlocks.truncate(NumBlocksBeforeCloning);
1248
1249 // Update ExitBlocks of the new plan.
1250 for (VPBlockBase *VPB : NewPlan->CreatedBlocks) {
1251 if (VPB->getNumSuccessors() == 0 && isa<VPIRBasicBlock>(VPB) &&
1252 VPB != NewScalarHeader)
1253 NewPlan->ExitBlocks.push_back(cast<VPIRBasicBlock>(VPB));
1254 }
1255
1256 return NewPlan;
1257}
1258
1260 auto *VPIRBB = new VPIRBasicBlock(IRBB);
1261 CreatedBlocks.push_back(VPIRBB);
1262 return VPIRBB;
1263}
1264
1266 auto *VPIRBB = createEmptyVPIRBasicBlock(IRBB);
1267 for (Instruction &I :
1268 make_range(IRBB->begin(), IRBB->getTerminator()->getIterator()))
1269 VPIRBB->appendRecipe(VPIRInstruction::create(I));
1270 return VPIRBB;
1271}
1272
1273#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1274
1275Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
1276 return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") +
1277 Twine(getOrCreateBID(Block));
1278}
1279
1280Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) {
1281 const std::string &Name = Block->getName();
1282 if (!Name.empty())
1283 return Name;
1284 return "VPB" + Twine(getOrCreateBID(Block));
1285}
1286
1288 Depth = 1;
1289 bumpIndent(0);
1290 OS << "digraph VPlan {\n";
1291 OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan";
1292 if (!Plan.getName().empty())
1293 OS << "\\n" << DOT::EscapeString(Plan.getName());
1294
1295 {
1296 // Print live-ins.
1297 std::string Str;
1298 raw_string_ostream SS(Str);
1299 Plan.printLiveIns(SS);
1301 StringRef(Str).rtrim('\n').split(Lines, "\n");
1302 for (auto Line : Lines)
1303 OS << DOT::EscapeString(Line.str()) << "\\n";
1304 }
1305
1306 OS << "\"]\n";
1307 OS << "node [shape=rect, fontname=Courier, fontsize=30]\n";
1308 OS << "edge [fontname=Courier, fontsize=30]\n";
1309 OS << "compound=true\n";
1310
1311 for (const VPBlockBase *Block : vp_depth_first_shallow(Plan.getEntry()))
1312 dumpBlock(Block);
1313
1314 OS << "}\n";
1315}
1316
1317void VPlanPrinter::dumpBlock(const VPBlockBase *Block) {
1319 dumpBasicBlock(BasicBlock);
1321 dumpRegion(Region);
1322 else
1323 llvm_unreachable("Unsupported kind of VPBlock.");
1324}
1325
1326void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To,
1327 bool Hidden, const Twine &Label) {
1328 // Due to "dot" we print an edge between two regions as an edge between the
1329 // exiting basic block and the entry basic of the respective regions.
1330 const VPBlockBase *Tail = From->getExitingBasicBlock();
1331 const VPBlockBase *Head = To->getEntryBasicBlock();
1332 OS << Indent << getUID(Tail) << " -> " << getUID(Head);
1333 OS << " [ label=\"" << Label << '\"';
1334 if (Tail != From)
1335 OS << " ltail=" << getUID(From);
1336 if (Head != To)
1337 OS << " lhead=" << getUID(To);
1338 if (Hidden)
1339 OS << "; splines=none";
1340 OS << "]\n";
1341}
1342
1343void VPlanPrinter::dumpEdges(const VPBlockBase *Block) {
1344 auto &Successors = Block->getSuccessors();
1345 if (Successors.size() == 1)
1346 drawEdge(Block, Successors.front(), false, "");
1347 else if (Successors.size() == 2) {
1348 drawEdge(Block, Successors.front(), false, "T");
1349 drawEdge(Block, Successors.back(), false, "F");
1350 } else {
1351 unsigned SuccessorNumber = 0;
1352 for (auto *Successor : Successors)
1353 drawEdge(Block, Successor, false, Twine(SuccessorNumber++));
1354 }
1355}
1356
1357void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) {
1358 // Implement dot-formatted dump by performing plain-text dump into the
1359 // temporary storage followed by some post-processing.
1360 OS << Indent << getUID(BasicBlock) << " [label =\n";
1361 bumpIndent(1);
1362 std::string Str;
1363 raw_string_ostream SS(Str);
1364 // Use no indentation as we need to wrap the lines into quotes ourselves.
1365 BasicBlock->print(SS, "", SlotTracker);
1366
1367 // We need to process each line of the output separately, so split
1368 // single-string plain-text dump.
1370 StringRef(Str).rtrim('\n').split(Lines, "\n");
1371
1372 auto EmitLine = [&](StringRef Line, StringRef Suffix) {
1373 OS << Indent << '"' << DOT::EscapeString(Line.str()) << "\\l\"" << Suffix;
1374 };
1375
1376 // Don't need the "+" after the last line.
1377 for (auto Line : make_range(Lines.begin(), Lines.end() - 1))
1378 EmitLine(Line, " +\n");
1379 EmitLine(Lines.back(), "\n");
1380
1381 bumpIndent(-1);
1382 OS << Indent << "]\n";
1383
1384 dumpEdges(BasicBlock);
1385}
1386
1387void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) {
1388 OS << Indent << "subgraph " << getUID(Region) << " {\n";
1389 bumpIndent(1);
1390 OS << Indent << "fontname=Courier\n"
1391 << Indent << "label=\""
1392 << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ")
1393 << DOT::EscapeString(Region->getName()) << "\"\n";
1394 // Dump the blocks of the region.
1395 assert(Region->getEntry() && "Region contains no inner blocks.");
1396 for (const VPBlockBase *Block : vp_depth_first_shallow(Region->getEntry()))
1397 dumpBlock(Block);
1398 bumpIndent(-1);
1399 OS << Indent << "}\n";
1400 dumpEdges(Region);
1401}
1402
1403#endif
1404
1405/// Returns true if there is a vector loop region and \p VPV is defined in a
1406/// loop region.
1407static bool isDefinedInsideLoopRegions(const VPValue *VPV) {
1408 const VPRecipeBase *DefR = VPV->getDefiningRecipe();
1409 return DefR && (!DefR->getParent()->getPlan()->getVectorLoopRegion() ||
1411}
1412
1417 replaceUsesWithIf(New, [](VPUser &, unsigned) { return true; });
1418}
1419
1421 VPValue *New,
1422 llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace) {
1423 // Note that this early exit is required for correctness; the implementation
1424 // below relies on the number of users for this VPValue to decrease, which
1425 // isn't the case if this == New.
1426 if (this == New)
1427 return;
1428
1429 for (unsigned J = 0; J < getNumUsers();) {
1430 VPUser *User = Users[J];
1431 bool RemovedUser = false;
1432 for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
1433 if (User->getOperand(I) != this || !ShouldReplace(*User, I))
1434 continue;
1435
1436 RemovedUser = true;
1437 User->setOperand(I, New);
1438 }
1439 // If a user got removed after updating the current user, the next user to
1440 // update will be moved to the current position, so we only need to
1441 // increment the index if the number of users did not change.
1442 if (!RemovedUser)
1443 J++;
1444 }
1445}
1446
1448 for (unsigned Idx = 0; Idx != getNumOperands(); ++Idx) {
1449 if (getOperand(Idx) == From)
1450 setOperand(Idx, To);
1451 }
1452}
1453
1454#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1456 OS << Tracker.getOrCreateName(this);
1457}
1458
1461 Op->printAsOperand(O, SlotTracker);
1462 });
1463}
1464#endif
1465
1466void VPSlotTracker::assignName(const VPValue *V) {
1467 assert(!VPValue2Name.contains(V) && "VPValue already has a name!");
1468 auto *UV = V->getUnderlyingValue();
1469 auto *VPI = dyn_cast_or_null<VPInstruction>(V->getDefiningRecipe());
1470 if (!UV && !(VPI && !VPI->getName().empty())) {
1471 VPValue2Name[V] = (Twine("vp<%") + Twine(NextSlot) + ">").str();
1472 NextSlot++;
1473 return;
1474 }
1475
1476 // Use the name of the underlying Value, wrapped in "ir<>", and versioned by
1477 // appending ".Number" to the name if there are multiple uses.
1478 std::string Name;
1479 if (UV)
1480 Name = getName(UV);
1481 else
1482 Name = VPI->getName();
1483
1484 assert(!Name.empty() && "Name cannot be empty.");
1485 StringRef Prefix = UV ? "ir<" : "vp<%";
1486 std::string BaseName = (Twine(Prefix) + Name + Twine(">")).str();
1487
1488 // First assign the base name for V.
1489 const auto &[A, _] = VPValue2Name.try_emplace(V, BaseName);
1490 // Integer or FP constants with different types will result in he same string
1491 // due to stripping types.
1492 if (V->isLiveIn() && isa<ConstantInt, ConstantFP>(UV))
1493 return;
1494
1495 // If it is already used by C > 0 other VPValues, increase the version counter
1496 // C and use it for V.
1497 const auto &[C, UseInserted] = BaseName2Version.try_emplace(BaseName, 0);
1498 if (!UseInserted) {
1499 C->second++;
1500 A->second = (BaseName + Twine(".") + Twine(C->second)).str();
1501 }
1502}
1503
1504void VPSlotTracker::assignNames(const VPlan &Plan) {
1505 if (Plan.VF.getNumUsers() > 0)
1506 assignName(&Plan.VF);
1507 if (Plan.VFxUF.getNumUsers() > 0)
1508 assignName(&Plan.VFxUF);
1509 assignName(&Plan.VectorTripCount);
1510 if (Plan.BackedgeTakenCount)
1511 assignName(Plan.BackedgeTakenCount);
1512 for (VPValue *LI : Plan.getLiveIns())
1513 assignName(LI);
1514
1515 ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<const VPBlockBase *>>
1516 RPOT(VPBlockDeepTraversalWrapper<const VPBlockBase *>(Plan.getEntry()));
1517 for (const VPBasicBlock *VPBB :
1519 assignNames(VPBB);
1520}
1521
1522void VPSlotTracker::assignNames(const VPBasicBlock *VPBB) {
1523 for (const VPRecipeBase &Recipe : *VPBB)
1524 for (VPValue *Def : Recipe.definedValues())
1525 assignName(Def);
1526}
1527
1528std::string VPSlotTracker::getName(const Value *V) {
1529 std::string Name;
1530 raw_string_ostream S(Name);
1531 if (V->hasName() || !isa<Instruction>(V)) {
1532 V->printAsOperand(S, false);
1533 return Name;
1534 }
1535
1536 if (!MST) {
1537 // Lazily create the ModuleSlotTracker when we first hit an unnamed
1538 // instruction.
1539 auto *I = cast<Instruction>(V);
1540 // This check is required to support unit tests with incomplete IR.
1541 if (I->getParent()) {
1542 MST = std::make_unique<ModuleSlotTracker>(I->getModule());
1543 MST->incorporateFunction(*I->getFunction());
1544 } else {
1545 MST = std::make_unique<ModuleSlotTracker>(nullptr);
1546 }
1547 }
1548 V->printAsOperand(S, false, *MST);
1549 return Name;
1550}
1551
1552std::string VPSlotTracker::getOrCreateName(const VPValue *V) const {
1553 std::string Name = VPValue2Name.lookup(V);
1554 if (!Name.empty())
1555 return Name;
1556
1557 // If no name was assigned, no VPlan was provided when creating the slot
1558 // tracker or it is not reachable from the provided VPlan. This can happen,
1559 // e.g. when trying to print a recipe that has not been inserted into a VPlan
1560 // in a debugger.
1561 // TODO: Update VPSlotTracker constructor to assign names to recipes &
1562 // VPValues not associated with a VPlan, instead of constructing names ad-hoc
1563 // here.
1564 const VPRecipeBase *DefR = V->getDefiningRecipe();
1565 (void)DefR;
1566 assert((!DefR || !DefR->getParent() || !DefR->getParent()->getPlan()) &&
1567 "VPValue defined by a recipe in a VPlan?");
1568
1569 // Use the underlying value's name, if there is one.
1570 if (auto *UV = V->getUnderlyingValue()) {
1571 std::string Name;
1572 raw_string_ostream S(Name);
1573 UV->printAsOperand(S, false);
1574 return (Twine("ir<") + Name + ">").str();
1575 }
1576
1577 return "<badref>";
1578}
1579
1581 const std::function<bool(ElementCount)> &Predicate, VFRange &Range) {
1582 assert(!Range.isEmpty() && "Trying to test an empty VF range.");
1583 bool PredicateAtRangeStart = Predicate(Range.Start);
1584
1585 for (ElementCount TmpVF : VFRange(Range.Start * 2, Range.End))
1586 if (Predicate(TmpVF) != PredicateAtRangeStart) {
1587 Range.End = TmpVF;
1588 break;
1589 }
1590
1591 return PredicateAtRangeStart;
1592}
1593
1594/// Build VPlans for the full range of feasible VF's = {\p MinVF, 2 * \p MinVF,
1595/// 4 * \p MinVF, ..., \p MaxVF} by repeatedly building a VPlan for a sub-range
1596/// of VF's starting at a given VF and extending it as much as possible. Each
1597/// vectorization decision can potentially shorten this sub-range during
1598/// buildVPlan().
1600 ElementCount MaxVF) {
1601 auto MaxVFTimes2 = MaxVF * 2;
1602 for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {
1603 VFRange SubRange = {VF, MaxVFTimes2};
1604 if (auto Plan = tryToBuildVPlan(SubRange)) {
1606 // Update the name of the latch of the top-level vector loop region region
1607 // after optimizations which includes block folding.
1608 Plan->getVectorLoopRegion()->getExiting()->setName("vector.latch");
1609 VPlans.push_back(std::move(Plan));
1610 }
1611 VF = SubRange.End;
1612 }
1613}
1614
1616 assert(count_if(VPlans,
1617 [VF](const VPlanPtr &Plan) { return Plan->hasVF(VF); }) ==
1618 1 &&
1619 "Multiple VPlans for VF.");
1620
1621 for (const VPlanPtr &Plan : VPlans) {
1622 if (Plan->hasVF(VF))
1623 return *Plan.get();
1624 }
1625 llvm_unreachable("No plan found!");
1626}
1627
1630 // Reserve first location for self reference to the LoopID metadata node.
1631 MDs.push_back(nullptr);
1632 bool IsUnrollMetadata = false;
1633 MDNode *LoopID = L->getLoopID();
1634 if (LoopID) {
1635 // First find existing loop unrolling disable metadata.
1636 for (unsigned I = 1, IE = LoopID->getNumOperands(); I < IE; ++I) {
1637 auto *MD = dyn_cast<MDNode>(LoopID->getOperand(I));
1638 if (MD) {
1639 const auto *S = dyn_cast<MDString>(MD->getOperand(0));
1640 if (!S)
1641 continue;
1642 if (S->getString().starts_with("llvm.loop.unroll.runtime.disable"))
1643 continue;
1644 IsUnrollMetadata =
1645 S->getString().starts_with("llvm.loop.unroll.disable");
1646 }
1647 MDs.push_back(LoopID->getOperand(I));
1648 }
1649 }
1650
1651 if (!IsUnrollMetadata) {
1652 // Add runtime unroll disable metadata.
1653 LLVMContext &Context = L->getHeader()->getContext();
1654 SmallVector<Metadata *, 1> DisableOperands;
1655 DisableOperands.push_back(
1656 MDString::get(Context, "llvm.loop.unroll.runtime.disable"));
1657 MDNode *DisableNode = MDNode::get(Context, DisableOperands);
1658 MDs.push_back(DisableNode);
1659 MDNode *NewLoopID = MDNode::get(Context, MDs);
1660 // Set operand 0 to refer to the loop id itself.
1661 NewLoopID->replaceOperandWith(0, NewLoopID);
1662 L->setLoopID(NewLoopID);
1663 }
1664}
1665
1667 Loop *VectorLoop, VPBasicBlock *HeaderVPBB, bool VectorizingEpilogue,
1668 unsigned EstimatedVFxUF, bool DisableRuntimeUnroll) {
1669 MDNode *LID = OrigLoop->getLoopID();
1670 // Update the metadata of the scalar loop. Skip the update when vectorizing
1671 // the epilogue loop, to ensure it is only updated once.
1672 if (!VectorizingEpilogue) {
1673 std::optional<MDNode *> RemainderLoopID = makeFollowupLoopID(
1675 if (RemainderLoopID) {
1676 OrigLoop->setLoopID(*RemainderLoopID);
1677 } else {
1678 if (DisableRuntimeUnroll)
1680
1681 LoopVectorizeHints Hints(OrigLoop, true, *ORE);
1682 Hints.setAlreadyVectorized();
1683 }
1684 }
1685
1686 if (!VectorLoop)
1687 return;
1688
1689 if (std::optional<MDNode *> VectorizedLoopID =
1692 VectorLoop->setLoopID(*VectorizedLoopID);
1693 } else {
1694 // Keep all loop hints from the original loop on the vector loop (we'll
1695 // replace the vectorizer-specific hints below).
1696 if (LID)
1697 VectorLoop->setLoopID(LID);
1698
1699 if (!VectorizingEpilogue) {
1700 LoopVectorizeHints Hints(VectorLoop, true, *ORE);
1701 Hints.setAlreadyVectorized();
1702 }
1703
1704 // Check if it's EVL-vectorized and mark the corresponding metadata.
1705 bool IsEVLVectorized =
1706 llvm::any_of(*HeaderVPBB, [](const VPRecipeBase &Recipe) {
1707 // Looking for the ExplictVectorLength VPInstruction.
1708 if (const auto *VI = dyn_cast<VPInstruction>(&Recipe))
1709 return VI->getOpcode() == VPInstruction::ExplicitVectorLength;
1710 return false;
1711 });
1712 if (IsEVLVectorized) {
1713 LLVMContext &Context = VectorLoop->getHeader()->getContext();
1714 MDNode *LoopID = VectorLoop->getLoopID();
1715 auto *IsEVLVectorizedMD = MDNode::get(
1716 Context,
1717 {MDString::get(Context, "llvm.loop.isvectorized.tailfoldingstyle"),
1718 MDString::get(Context, "evl")});
1719 MDNode *NewLoopID = makePostTransformationMetadata(Context, LoopID, {},
1720 {IsEVLVectorizedMD});
1721 VectorLoop->setLoopID(NewLoopID);
1722 }
1723 }
1725 TTI.getUnrollingPreferences(VectorLoop, *PSE.getSE(), UP, ORE);
1726 if (!UP.UnrollVectorizedLoop || VectorizingEpilogue)
1728
1729 // Set/update profile weights for the vector and remainder loops as original
1730 // loop iterations are now distributed among them. Note that original loop
1731 // becomes the scalar remainder loop after vectorization.
1732 //
1733 // For cases like foldTailByMasking() and requiresScalarEpiloque() we may
1734 // end up getting slightly roughened result but that should be OK since
1735 // profile is not inherently precise anyway. Note also possible bypass of
1736 // vector code caused by legality checks is ignored, assigning all the weight
1737 // to the vector loop, optimistically.
1738 //
1739 // For scalable vectorization we can't know at compile time how many
1740 // iterations of the loop are handled in one vector iteration, so instead
1741 // use the value of vscale used for tuning.
1742 setProfileInfoAfterUnrolling(OrigLoop, VectorLoop, OrigLoop, EstimatedVFxUF);
1743}
1744
1745#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1747 if (VPlans.empty()) {
1748 O << "LV: No VPlans built.\n";
1749 return;
1750 }
1751 for (const auto &Plan : VPlans)
1753 Plan->printDOT(O);
1754 else
1755 Plan->print(O);
1756}
1757#endif
1758
1761 if (!V->isLiveIn())
1762 return {};
1763
1764 return TTI::getOperandInfo(V->getLiveInIRValue());
1765}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
dxil pretty DXIL Metadata Pretty Printer
Flatten the CFG
#define _
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This file defines the LoopVectorizationLegality class.
This file provides a LoopVectorizationPlanner class.
#define I(x, y, z)
Definition MD5.cpp:58
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static StringRef getName(Value *V)
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file implements dominator tree analysis for a single level of a VPlan's H-CFG.
This file contains the declarations of different VPlan-related auxiliary helpers.
This file provides utility VPlan to VPlan transformations.
static void addRuntimeUnrollDisableMetaData(Loop *L)
Definition VPlan.cpp:1628
static T * getPlanEntry(T *Start)
Definition VPlan.cpp:145
static T * getEnclosingLoopRegionForRegion(T *P)
Return the enclosing loop region for region P.
Definition VPlan.cpp:625
const char LLVMLoopVectorizeFollowupAll[]
Definition VPlan.cpp:61
static bool isDefinedInsideLoopRegions(const VPValue *VPV)
Returns true if there is a vector loop region and VPV is defined in a loop region.
Definition VPlan.cpp:1407
static bool hasConditionalTerminator(const VPBasicBlock *VPBB)
Definition VPlan.cpp:643
const char LLVMLoopVectorizeFollowupVectorized[]
Definition VPlan.cpp:62
static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry, DenseMap< VPValue *, VPValue * > &Old2NewVPValues)
Definition VPlan.cpp:1154
const char LLVMLoopVectorizeFollowupEpilogue[]
Definition VPlan.cpp:64
static std::pair< VPBlockBase *, VPBlockBase * > cloneFrom(VPBlockBase *Entry)
Definition VPlan.cpp:735
static cl::opt< bool > PrintVPlansInDotFormat("vplan-print-in-dot-format", cl::Hidden, cl::desc("Use dot format instead of plain text when dumping VPlans"))
This file contains the declarations of the Vectorization Plan base classes:
static bool IsCondBranch(unsigned BrOpc)
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
size_t size() const
Definition BasicBlock.h:480
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
std::optional< const DILocation * > cloneByMultiplyingDuplicationFactor(unsigned DF) const
Returns a new DILocation with duplication factor DF * current duplication factor encoded in the discr...
A debug info location.
Definition DebugLoc.h:124
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:187
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:156
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
static InstructionCost getInvalid(CostType Val=0)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A helper class to return the specified delimiter string after the first invocation of operator String...
BlockT * getHeader() const
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
VPlan & getPlanFor(ElementCount VF) const
Return the VPlan for VF.
Definition VPlan.cpp:1615
void buildVPlans(ElementCount MinVF, ElementCount MaxVF)
Build VPlans for power-of-2 VF's between MinVF and MaxVF inclusive, according to the information gath...
Definition VPlan.cpp:1599
void updateLoopMetadataAndProfileInfo(Loop *VectorLoop, VPBasicBlock *HeaderVPBB, bool VectorizingEpilogue, unsigned EstimatedVFxUF, bool DisableRuntimeUnroll)
Update loop metadata and profile info for both the scalar remainder loop and VectorLoop,...
Definition VPlan.cpp:1666
static bool getDecisionAndClampRange(const std::function< bool(ElementCount)> &Predicate, VFRange &Range)
Test a Predicate on a Range of VF's.
Definition VPlan.cpp:1580
void printPlans(raw_ostream &O)
Definition VPlan.cpp:1746
Utility class for getting and setting loop vectorizer hints in the form of loop metadata.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition LoopInfo.cpp:538
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition LoopInfo.cpp:514
Metadata node.
Definition Metadata.h:1077
LLVM_ABI void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1445
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1565
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1451
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:607
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
BlockT * getEntry() const
Get the entry BasicBlock of the Region.
Definition RegionInfo.h:320
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:104
void insert_range(Range &&R)
Definition SetVector.h:193
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:168
This class provides computation of slot numbers for LLVM Assembly writing.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:356
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map.
Definition StringMap.h:370
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:710
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Definition StringRef.h:812
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
static LLVM_ABI OperandValueInfo getOperandInfo(const Value *V)
Collect properties of V used in cost analysis, e.g. OP_PowerOf2.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
This function has undefined behavior.
void setOperand(unsigned i, Value *Val)
Definition User.h:237
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:3754
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition VPlan.h:3829
RecipeListTy::iterator iterator
Instruction iterators...
Definition VPlan.h:3781
void execute(VPTransformState *State) override
The method which generates the output IR instructions that correspond to this VPBasicBlock,...
Definition VPlan.cpp:535
iterator end()
Definition VPlan.h:3791
iterator begin()
Recipe iterator methods.
Definition VPlan.h:3789
VPBasicBlock * clone() override
Clone the current block and it's recipes, without updating the operands of the cloned recipes.
Definition VPlan.cpp:586
InstructionCost cost(ElementCount VF, VPCostContext &Ctx) override
Return the cost of this VPBasicBlock.
Definition VPlan.cpp:816
const VPBasicBlock * getCFGPredecessor(unsigned Idx) const
Returns the predecessor block at index Idx with the predecessors as per the corresponding plain CFG.
Definition VPlan.cpp:823
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:246
void connectToPredecessors(VPTransformState &State)
Connect the VPBBs predecessors' in the VPlan CFG to the IR basic block generated for this VPBB.
Definition VPlan.cpp:435
VPRegionBlock * getEnclosingLoopRegion()
Definition VPlan.cpp:635
VPBasicBlock * splitAt(iterator SplitAt)
Split current block at SplitAt by inserting a new block between the current block and its successors ...
Definition VPlan.cpp:607
RecipeListTy Recipes
The VPRecipes held in the order of output instructions to generate.
Definition VPlan.h:3769
void executeRecipes(VPTransformState *State, BasicBlock *BB)
Execute the recipes in the IR basic block BB.
Definition VPlan.cpp:593
void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print this VPBsicBlock to O, prefixing all lines with Indent.
Definition VPlan.cpp:714
bool isExiting() const
Returns true if the block is exiting it's parent region.
Definition VPlan.cpp:692
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
Definition VPlan.cpp:680
const VPRecipeBase & back() const
Definition VPlan.h:3803
bool empty() const
Definition VPlan.h:3800
size_t size() const
Definition VPlan.h:3799
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:81
void setSuccessors(ArrayRef< VPBlockBase * > NewSuccs)
Set each VPBasicBlock in NewSuccss as successor of this VPBlockBase.
Definition VPlan.h:300
VPRegionBlock * getParent()
Definition VPlan.h:173
const VPBasicBlock * getExitingBasicBlock() const
Definition VPlan.cpp:190
void setName(const Twine &newName)
Definition VPlan.h:166
size_t getNumSuccessors() const
Definition VPlan.h:219
iterator_range< VPBlockBase ** > successors()
Definition VPlan.h:201
virtual void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const =0
Print plain-text dump of this VPBlockBase to O, prefixing all lines with Indent.
bool hasPredecessors() const
Returns true if this block has any predecessors.
Definition VPlan.h:223
void printSuccessors(raw_ostream &O, const Twine &Indent) const
Print the successors of this block to O, prefixing all lines with Indent.
Definition VPlan.cpp:702
size_t getNumPredecessors() const
Definition VPlan.h:220
void setPredecessors(ArrayRef< VPBlockBase * > NewPreds)
Set each VPBasicBlock in NewPreds as predecessor of this VPBlockBase.
Definition VPlan.h:291
VPBlockBase * getEnclosingBlockWithPredecessors()
Definition VPlan.cpp:212
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:204
VPlan * getPlan()
Definition VPlan.cpp:165
void setPlan(VPlan *ParentPlan)
Sets the pointer of the plan containing the block.
Definition VPlan.cpp:184
const std::string & getName() const
Definition VPlan.h:164
VPBlockBase * getSinglePredecessor() const
Definition VPlan.h:215
const VPBlocksTy & getHierarchicalSuccessors()
Definition VPlan.h:242
VPBlockBase(const unsigned char SC, const std::string &N)
Definition VPlan.h:150
VPBlockBase * getEnclosingBlockWithSuccessors()
An Enclosing Block of a block B is any block containing B, including B itself.
Definition VPlan.cpp:204
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:170
VPBlockBase * getSingleHierarchicalPredecessor()
Definition VPlan.h:264
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:209
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:198
static auto blocksOnly(const T &Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:217
static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr)
Insert disconnected VPBlockBase NewBlock after BlockPtr.
Definition VPlanUtils.h:120
static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop latch, using isHeader().
Definition VPlan.cpp:237
static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop header, based on regions or VPDT in their absence.
Definition VPlan.cpp:220
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:176
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To)
Disconnect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:195
VPlan-based builder utility analogous to IRBuilder.
VPPhi * createScalarPhi(ArrayRef< VPValue * > IncomingValues, DebugLoc DL, const Twine &Name="")
void dump() const
Dump the VPDef to stderr (for debugging).
Definition VPlan.cpp:126
virtual void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const =0
Each concrete VPDef prints itself.
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
Recipe to expand a SCEV expression.
Definition VPlan.h:3373
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:3907
void execute(VPTransformState *State) override
The method which generates the output IR instructions that correspond to this VPBasicBlock,...
Definition VPlan.cpp:503
BasicBlock * getIRBasicBlock() const
Definition VPlan.h:3931
VPIRBasicBlock * clone() override
Clone the current block and it's recipes, without updating the operands of the cloned recipes.
Definition VPlan.cpp:528
static LLVM_ABI_FOR_TEST VPIRInstruction * create(Instruction &I)
Create a new VPIRPhi for \I , if it is a PHINode, otherwise create a VPIRInstruction.
In what follows, the term "input IR" refers to code that is fed into the vectorizer whereas the term ...
Value * getAsRuntimeExpr(IRBuilderBase &Builder, const ElementCount &VF) const
Returns an expression describing the lane index that can be used at runtime.
Definition VPlan.cpp:85
static VPLane getFirstLane()
@ ScalableLast
For ScalableLast, Lane is the offset from the start of the last N-element subvector in a scalable vec...
@ First
For First, Lane is the index into the first N elements of a fixed-vector <N x <ElTy>> or a scalable v...
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:394
VPBasicBlock * getParent()
Definition VPlan.h:415
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:3942
VPRegionBlock * clone() override
Clone all blocks in the single-entry single-exit region of the block and their recipes without updati...
Definition VPlan.cpp:785
const VPBlockBase * getEntry() const
Definition VPlan.h:3978
void dissolveToCFGLoop()
Remove the current region from its VPlan, connecting its predecessor to its entry,...
Definition VPlan.cpp:894
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4010
InstructionCost cost(ElementCount VF, VPCostContext &Ctx) override
Return the cost of the block.
Definition VPlan.cpp:842
void print(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print this VPRegionBlock to O (recursively), prefixing all lines with Indent.
Definition VPlan.cpp:880
void execute(VPTransformState *State) override
The method which generates the output IR instructions that correspond to this VPRegionBlock,...
Definition VPlan.cpp:794
const VPBlockBase * getExiting() const
Definition VPlan.h:3990
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
Definition VPlan.h:3644
This class can be used to assign names to VPValues.
std::string getOrCreateName(const VPValue *V) const
Returns the name assigned to V, if there is one, otherwise try to construct one from the underlying v...
Definition VPlan.cpp:1552
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:197
void replaceUsesOfWith(VPValue *From, VPValue *To)
Replaces all uses of From in the VPUser with To.
Definition VPlan.cpp:1447
void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const
Print the operands to O.
Definition VPlan.cpp:1459
operand_range operands()
Definition VPlanValue.h:265
void setOperand(unsigned I, VPValue *New)
Definition VPlanValue.h:241
unsigned getNumOperands() const
Definition VPlanValue.h:235
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:236
bool isDefinedOutsideLoopRegions() const
Returns true if the VPValue is defined outside any loop.
Definition VPlan.cpp:1413
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:135
void printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const
Definition VPlan.cpp:1455
friend class VPDef
Definition VPlanValue.h:49
Value * UnderlyingVal
Definition VPlanValue.h:61
void dump() const
Dump the value to stderr (for debugging).
Definition VPlan.cpp:118
VPValue(const unsigned char SC, Value *UV=nullptr, VPDef *Def=nullptr)
Definition VPlan.cpp:98
virtual ~VPValue()
Definition VPlan.cpp:104
void print(raw_ostream &OS, VPSlotTracker &Tracker) const
Definition VPlan.cpp:111
void replaceAllUsesWith(VPValue *New)
Definition VPlan.cpp:1416
unsigned getNumUsers() const
Definition VPlanValue.h:113
void replaceUsesWithIf(VPValue *New, llvm::function_ref< bool(VPUser &U, unsigned Idx)> ShouldReplace)
Go through the uses list for this VPValue and make each use point to New if the callback ShouldReplac...
Definition VPlan.cpp:1420
VPDef * Def
Pointer to the VPDef that defines this VPValue.
Definition VPlanValue.h:65
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2107
LLVM_DUMP_METHOD void dump()
Definition VPlan.cpp:1287
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4045
void printDOT(raw_ostream &O) const
Print this VPlan in DOT format to O.
Definition VPlan.cpp:1145
friend class VPSlotTracker
Definition VPlan.h:4047
std::string getName() const
Return a string with the name of the plan and the applicable VFs and UFs.
Definition VPlan.cpp:1121
VPBasicBlock * getEntry()
Definition VPlan.h:4144
VPRegionBlock * createVPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exiting, const std::string &Name="", bool IsReplicator=false)
Create a new VPRegionBlock with Entry, Exiting and Name.
Definition VPlan.h:4385
void setName(const Twine &newName)
Definition VPlan.h:4292
VPIRBasicBlock * getExitBlock(BasicBlock *IRBB) const
Return the VPIRBasicBlock corresponding to IRBB.
Definition VPlan.cpp:953
LLVM_ABI_FOR_TEST ~VPlan()
Definition VPlan.cpp:930
bool isExitBlock(VPBlockBase *VPBB)
Returns true if VPBB is an exit block.
Definition VPlan.cpp:961
friend class VPlanPrinter
Definition VPlan.h:4046
unsigned getUF() const
Definition VPlan.h:4274
VPIRBasicBlock * createEmptyVPIRBasicBlock(BasicBlock *IRBB)
Create a VPIRBasicBlock wrapping IRBB, but do not create VPIRInstructions wrapping the instructions i...
Definition VPlan.cpp:1259
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:4196
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1050
InstructionCost cost(ElementCount VF, VPCostContext &Ctx)
Return the cost of this plan.
Definition VPlan.cpp:1032
void setEntry(VPBasicBlock *VPBB)
Definition VPlan.h:4133
VPBasicBlock * createVPBasicBlock(const Twine &Name, VPRecipeBase *Recipe=nullptr)
Create a new VPBasicBlock with Name and containing Recipe if present.
Definition VPlan.h:4375
LLVM_ABI_FOR_TEST VPIRBasicBlock * createVPIRBasicBlock(BasicBlock *IRBB)
Create a VPIRBasicBlock from IRBB containing VPIRInstructions for all instructions in IRBB,...
Definition VPlan.cpp:1265
LLVM_DUMP_METHOD void dump() const
Dump the plan to stderr (for debugging).
Definition VPlan.cpp:1151
void execute(VPTransformState *State)
Generate the IR code for this VPlan.
Definition VPlan.cpp:968
ArrayRef< VPValue * > getLiveIns() const
Return the list of live-in VPValues available in the VPlan.
Definition VPlan.h:4326
void print(raw_ostream &O) const
Print this VPlan to O.
Definition VPlan.cpp:1104
VPIRBasicBlock * getScalarHeader() const
Return the VPIRBasicBlock wrapping the header of the scalar loop.
Definition VPlan.h:4192
void printLiveIns(raw_ostream &O) const
Print the live-ins of this VPlan to O.
Definition VPlan.cpp:1066
VPlan * duplicate()
Clone the current VPlan, update all VPValues of the new VPlan and cloned recipes to refer to the clon...
Definition VPlan.cpp:1192
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:216
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition ilist_node.h:134
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI std::string EscapeString(const std::string &Label)
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
bool match(Val *V, const Pattern &P)
VPInstruction_match< VPInstruction::BranchOnCount, Op0_t, Op1_t > m_BranchOnCount(const Op0_t &Op0, const Op1_t &Op1)
class_match< VPValue > m_VPValue()
Match an arbitrary VPValue and ignore it.
VPInstruction_match< VPInstruction::BuildVector > m_BuildVector()
BuildVector is matches only its opcode, w/o matching its operands as the number of operands is not fi...
VPInstruction_match< VPInstruction::BranchOnCond, Op0_t > m_BranchOnCond(const Op0_t &Op0)
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
Definition VPlanUtils.h:44
VPBasicBlock * getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT)
Returns the header block of the first, top-level loop, or null if none exist.
bool onlyFirstLaneUsed(const VPValue *Def)
Returns true if only the first lane of Def is used.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:330
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:843
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
Value * getRuntimeVF(IRBuilderBase &B, Type *Ty, ElementCount VF)
Return the runtime value for VF.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI std::optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
void interleaveComma(const Container &c, StreamT &os, UnaryFunctor each_fn)
Definition STLExtras.h:2233
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:646
auto cast_or_null(const Y &Val)
Definition Casting.h:720
iterator_range< df_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_depth_first_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order.
Definition VPlanCFG.h:216
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
LLVM_ABI cl::opt< bool > EnableFSDiscriminator
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1734
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Type * toVectorizedTy(Type *Ty, ElementCount EC)
A helper for converting to vectorized types.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
cl::opt< unsigned > ForceTargetInstructionCost
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
LLVM_ABI void setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop, Loop *RemainderLoop, uint64_t UF)
Set weights for UnrolledLoop and RemainderLoop based on weights for OrigLoop and the following distri...
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:1963
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1760
LLVM_ABI llvm::MDNode * makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID, llvm::ArrayRef< llvm::StringRef > RemovePrefixes, llvm::ArrayRef< llvm::MDNode * > AddAttrs)
Create a new LoopID after the loop has been transformed.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1899
cl::opt< bool > EnableVPlanNativePath
Definition VPlan.cpp:56
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
unsigned getPredBlockCostDivisor(TargetTransformInfo::TargetCostKind CostKind)
A helper function that returns how much we should divide the cost of a predicated block by.
std::unique_ptr< VPlan > VPlanPtr
Definition VPlan.h:77
Parameters that control the generic loop unrolling transformation.
bool UnrollVectorizedLoop
Don't disable runtime unroll for the loops which were vectorized.
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
ElementCount End
Struct to hold various analysis needed for cost computations.
TargetTransformInfo::OperandValueInfo getOperandInfo(VPValue *V) const
Returns the OperandInfo for V, if it is a live-in.
Definition VPlan.cpp:1760
VPTransformState holds information passed down when "executing" a VPlan, needed for generating the ou...
LoopInfo * LI
Hold a pointer to LoopInfo to register new basic blocks in the loop.
VPTypeAnalysis TypeAnalysis
VPlan-based type analysis.
struct llvm::VPTransformState::DataState Data
struct llvm::VPTransformState::CFGState CFG
Value * get(const VPValue *Def, bool IsScalar=false)
Get the generated vector Value for a given VPValue Def if IsScalar is false, otherwise return the gen...
Definition VPlan.cpp:293
VPTransformState(const TargetTransformInfo *TTI, ElementCount VF, LoopInfo *LI, DominatorTree *DT, AssumptionCache *AC, IRBuilderBase &Builder, VPlan *Plan, Loop *CurrentParentLoop, Type *CanonicalIVTy)
Definition VPlan.cpp:253
std::optional< VPLane > Lane
Hold the index to generate specific scalar instructions.
IRBuilderBase & Builder
Hold a reference to the IRBuilder used to generate output IR code.
bool hasScalarValue(const VPValue *Def, VPLane Lane)
const TargetTransformInfo * TTI
Target Transform Info.
VPlan * Plan
Pointer to the VPlan code is generated for.
void set(const VPValue *Def, Value *V, bool IsScalar=false)
Set the generated vector Value for a given VPValue, if IsScalar is false.
bool hasVectorValue(const VPValue *Def)
VPDominatorTree VPDT
VPlan-based dominator tree.
ElementCount VF
The chosen Vectorization Factor of the loop being vectorized.
Value * packScalarIntoVectorizedValue(const VPValue *Def, Value *WideValue, const VPLane &Lane)
Insert the scalar value of Def at Lane into Lane of WideValue and return the resulting value.
Definition VPlan.cpp:403
AssumptionCache * AC
Hold a pointer to AssumptionCache to register new assumptions after replicating assume calls.
void setDebugLocFrom(DebugLoc DL)
Set the debug location in the builder using the debug location DL.
Definition VPlan.cpp:381
Loop * CurrentParentLoop
The parent loop object for the current scope, or nullptr.
static void optimize(VPlan &Plan)
Apply VPlan-to-VPlan optimizations to Plan, including induction recipe optimizations,...