LLVM 22.0.0git
VPlan.h
Go to the documentation of this file.
1//===- VPlan.h - Represent A Vectorizer Plan --------------------*- C++ -*-===//
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 file contains the declarations of the Vectorization Plan base classes:
11/// 1. VPBasicBlock and VPRegionBlock that inherit from a common pure virtual
12/// VPBlockBase, together implementing a Hierarchical CFG;
13/// 2. Pure virtual VPRecipeBase serving as the base class for recipes contained
14/// within VPBasicBlocks;
15/// 3. Pure virtual VPSingleDefRecipe serving as a base class for recipes that
16/// also inherit from VPValue.
17/// 4. VPInstruction, a concrete Recipe and VPUser modeling a single planned
18/// instruction;
19/// 5. The VPlan class holding a candidate for vectorization;
20/// These are documented in docs/VectorizationPlan.rst.
21//
22//===----------------------------------------------------------------------===//
23
24#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
25#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
26
27#include "VPlanValue.h"
28#include "llvm/ADT/MapVector.h"
31#include "llvm/ADT/Twine.h"
32#include "llvm/ADT/ilist.h"
33#include "llvm/ADT/ilist_node.h"
37#include "llvm/IR/DebugLoc.h"
38#include "llvm/IR/FMF.h"
39#include "llvm/IR/Operator.h"
42#include <cassert>
43#include <cstddef>
44#include <functional>
45#include <string>
46#include <utility>
47#include <variant>
48
49namespace llvm {
50
51class BasicBlock;
52class DominatorTree;
54class IRBuilderBase;
55struct VPTransformState;
56class raw_ostream;
58class SCEV;
59class Type;
60class VPBasicBlock;
61class VPBuilder;
62class VPDominatorTree;
63class VPRegionBlock;
64class VPlan;
65class VPLane;
67class VPlanSlp;
68class Value;
70
71struct VPCostContext;
72
73namespace Intrinsic {
74typedef unsigned ID;
75}
76
77using VPlanPtr = std::unique_ptr<VPlan>;
78
79/// VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
80/// A VPBlockBase can be either a VPBasicBlock or a VPRegionBlock.
82 friend class VPBlockUtils;
83
84 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
85
86 /// An optional name for the block.
87 std::string Name;
88
89 /// The immediate VPRegionBlock which this VPBlockBase belongs to, or null if
90 /// it is a topmost VPBlockBase.
91 VPRegionBlock *Parent = nullptr;
92
93 /// List of predecessor blocks.
95
96 /// List of successor blocks.
98
99 /// VPlan containing the block. Can only be set on the entry block of the
100 /// plan.
101 VPlan *Plan = nullptr;
102
103 /// Add \p Successor as the last successor to this block.
104 void appendSuccessor(VPBlockBase *Successor) {
105 assert(Successor && "Cannot add nullptr successor!");
106 Successors.push_back(Successor);
107 }
108
109 /// Add \p Predecessor as the last predecessor to this block.
110 void appendPredecessor(VPBlockBase *Predecessor) {
111 assert(Predecessor && "Cannot add nullptr predecessor!");
112 Predecessors.push_back(Predecessor);
113 }
114
115 /// Remove \p Predecessor from the predecessors of this block.
116 void removePredecessor(VPBlockBase *Predecessor) {
117 auto Pos = find(Predecessors, Predecessor);
118 assert(Pos && "Predecessor does not exist");
119 Predecessors.erase(Pos);
120 }
121
122 /// Remove \p Successor from the successors of this block.
123 void removeSuccessor(VPBlockBase *Successor) {
124 auto Pos = find(Successors, Successor);
125 assert(Pos && "Successor does not exist");
126 Successors.erase(Pos);
127 }
128
129 /// This function replaces one predecessor with another, useful when
130 /// trying to replace an old block in the CFG with a new one.
131 void replacePredecessor(VPBlockBase *Old, VPBlockBase *New) {
132 auto I = find(Predecessors, Old);
133 assert(I != Predecessors.end());
134 assert(Old->getParent() == New->getParent() &&
135 "replaced predecessor must have the same parent");
136 *I = New;
137 }
138
139 /// This function replaces one successor with another, useful when
140 /// trying to replace an old block in the CFG with a new one.
141 void replaceSuccessor(VPBlockBase *Old, VPBlockBase *New) {
142 auto I = find(Successors, Old);
143 assert(I != Successors.end());
144 assert(Old->getParent() == New->getParent() &&
145 "replaced successor must have the same parent");
146 *I = New;
147 }
148
149protected:
150 VPBlockBase(const unsigned char SC, const std::string &N)
151 : SubclassID(SC), Name(N) {}
152
153public:
154 /// An enumeration for keeping track of the concrete subclass of VPBlockBase
155 /// that are actually instantiated. Values of this enumeration are kept in the
156 /// SubclassID field of the VPBlockBase objects. They are used for concrete
157 /// type identification.
158 using VPBlockTy = enum { VPRegionBlockSC, VPBasicBlockSC, VPIRBasicBlockSC };
159
161
162 virtual ~VPBlockBase() = default;
163
164 const std::string &getName() const { return Name; }
165
166 void setName(const Twine &newName) { Name = newName.str(); }
167
168 /// \return an ID for the concrete type of this object.
169 /// This is used to implement the classof checks. This should not be used
170 /// for any other purpose, as the values may change as LLVM evolves.
171 unsigned getVPBlockID() const { return SubclassID; }
172
173 VPRegionBlock *getParent() { return Parent; }
174 const VPRegionBlock *getParent() const { return Parent; }
175
176 /// \return A pointer to the plan containing the current block.
177 VPlan *getPlan();
178 const VPlan *getPlan() const;
179
180 /// Sets the pointer of the plan containing the block. The block must be the
181 /// entry block into the VPlan.
182 void setPlan(VPlan *ParentPlan);
183
184 void setParent(VPRegionBlock *P) { Parent = P; }
185
186 /// \return the VPBasicBlock that is the entry of this VPBlockBase,
187 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
188 /// VPBlockBase is a VPBasicBlock, it is returned.
189 const VPBasicBlock *getEntryBasicBlock() const;
190 VPBasicBlock *getEntryBasicBlock();
191
192 /// \return the VPBasicBlock that is the exiting this VPBlockBase,
193 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
194 /// VPBlockBase is a VPBasicBlock, it is returned.
195 const VPBasicBlock *getExitingBasicBlock() const;
196 VPBasicBlock *getExitingBasicBlock();
197
198 const VPBlocksTy &getSuccessors() const { return Successors; }
199 VPBlocksTy &getSuccessors() { return Successors; }
200
203
204 const VPBlocksTy &getPredecessors() const { return Predecessors; }
205 VPBlocksTy &getPredecessors() { return Predecessors; }
206
207 /// \return the successor of this VPBlockBase if it has a single successor.
208 /// Otherwise return a null pointer.
210 return (Successors.size() == 1 ? *Successors.begin() : nullptr);
211 }
212
213 /// \return the predecessor of this VPBlockBase if it has a single
214 /// predecessor. Otherwise return a null pointer.
216 return (Predecessors.size() == 1 ? *Predecessors.begin() : nullptr);
217 }
218
219 size_t getNumSuccessors() const { return Successors.size(); }
220 size_t getNumPredecessors() const { return Predecessors.size(); }
221
222 /// Returns true if this block has any predecessors.
223 bool hasPredecessors() const { return !Predecessors.empty(); }
224
225 /// An Enclosing Block of a block B is any block containing B, including B
226 /// itself. \return the closest enclosing block starting from "this", which
227 /// has successors. \return the root enclosing block if all enclosing blocks
228 /// have no successors.
229 VPBlockBase *getEnclosingBlockWithSuccessors();
230
231 /// \return the closest enclosing block starting from "this", which has
232 /// predecessors. \return the root enclosing block if all enclosing blocks
233 /// have no predecessors.
234 VPBlockBase *getEnclosingBlockWithPredecessors();
235
236 /// \return the successors either attached directly to this VPBlockBase or, if
237 /// this VPBlockBase is the exit block of a VPRegionBlock and has no
238 /// successors of its own, search recursively for the first enclosing
239 /// VPRegionBlock that has successors and return them. If no such
240 /// VPRegionBlock exists, return the (empty) successors of the topmost
241 /// VPBlockBase reached.
243 return getEnclosingBlockWithSuccessors()->getSuccessors();
244 }
245
246 /// \return the hierarchical successor of this VPBlockBase if it has a single
247 /// hierarchical successor. Otherwise return a null pointer.
249 return getEnclosingBlockWithSuccessors()->getSingleSuccessor();
250 }
251
252 /// \return the predecessors either attached directly to this VPBlockBase or,
253 /// if this VPBlockBase is the entry block of a VPRegionBlock and has no
254 /// predecessors of its own, search recursively for the first enclosing
255 /// VPRegionBlock that has predecessors and return them. If no such
256 /// VPRegionBlock exists, return the (empty) predecessors of the topmost
257 /// VPBlockBase reached.
259 return getEnclosingBlockWithPredecessors()->getPredecessors();
260 }
261
262 /// \return the hierarchical predecessor of this VPBlockBase if it has a
263 /// single hierarchical predecessor. Otherwise return a null pointer.
267
268 /// Set a given VPBlockBase \p Successor as the single successor of this
269 /// VPBlockBase. This VPBlockBase is not added as predecessor of \p Successor.
270 /// This VPBlockBase must have no successors.
272 assert(Successors.empty() && "Setting one successor when others exist.");
273 assert(Successor->getParent() == getParent() &&
274 "connected blocks must have the same parent");
275 appendSuccessor(Successor);
276 }
277
278 /// Set two given VPBlockBases \p IfTrue and \p IfFalse to be the two
279 /// successors of this VPBlockBase. This VPBlockBase is not added as
280 /// predecessor of \p IfTrue or \p IfFalse. This VPBlockBase must have no
281 /// successors.
282 void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse) {
283 assert(Successors.empty() && "Setting two successors when others exist.");
284 appendSuccessor(IfTrue);
285 appendSuccessor(IfFalse);
286 }
287
288 /// Set each VPBasicBlock in \p NewPreds as predecessor of this VPBlockBase.
289 /// This VPBlockBase must have no predecessors. This VPBlockBase is not added
290 /// as successor of any VPBasicBlock in \p NewPreds.
292 assert(Predecessors.empty() && "Block predecessors already set.");
293 for (auto *Pred : NewPreds)
294 appendPredecessor(Pred);
295 }
296
297 /// Set each VPBasicBlock in \p NewSuccss as successor of this VPBlockBase.
298 /// This VPBlockBase must have no successors. This VPBlockBase is not added
299 /// as predecessor of any VPBasicBlock in \p NewSuccs.
301 assert(Successors.empty() && "Block successors already set.");
302 for (auto *Succ : NewSuccs)
303 appendSuccessor(Succ);
304 }
305
306 /// Remove all the predecessor of this block.
307 void clearPredecessors() { Predecessors.clear(); }
308
309 /// Remove all the successors of this block.
310 void clearSuccessors() { Successors.clear(); }
311
312 /// Swap predecessors of the block. The block must have exactly 2
313 /// predecessors.
315 assert(Predecessors.size() == 2 && "must have 2 predecessors to swap");
316 std::swap(Predecessors[0], Predecessors[1]);
317 }
318
319 /// Swap successors of the block. The block must have exactly 2 successors.
320 // TODO: This should be part of introducing conditional branch recipes rather
321 // than being independent.
323 assert(Successors.size() == 2 && "must have 2 successors to swap");
324 std::swap(Successors[0], Successors[1]);
325 }
326
327 /// Returns the index for \p Pred in the blocks predecessors list.
328 unsigned getIndexForPredecessor(const VPBlockBase *Pred) const {
329 assert(count(Predecessors, Pred) == 1 &&
330 "must have Pred exactly once in Predecessors");
331 return std::distance(Predecessors.begin(), find(Predecessors, Pred));
332 }
333
334 /// Returns the index for \p Succ in the blocks successor list.
335 unsigned getIndexForSuccessor(const VPBlockBase *Succ) const {
336 assert(count(Successors, Succ) == 1 &&
337 "must have Succ exactly once in Successors");
338 return std::distance(Successors.begin(), find(Successors, Succ));
339 }
340
341 /// The method which generates the output IR that correspond to this
342 /// VPBlockBase, thereby "executing" the VPlan.
343 virtual void execute(VPTransformState *State) = 0;
344
345 /// Return the cost of the block.
347
348#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
349 void printAsOperand(raw_ostream &OS, bool PrintType = false) const {
350 OS << getName();
351 }
352
353 /// Print plain-text dump of this VPBlockBase to \p O, prefixing all lines
354 /// with \p Indent. \p SlotTracker is used to print unnamed VPValue's using
355 /// consequtive numbers.
356 ///
357 /// Note that the numbering is applied to the whole VPlan, so printing
358 /// individual blocks is consistent with the whole VPlan printing.
359 virtual void print(raw_ostream &O, const Twine &Indent,
360 VPSlotTracker &SlotTracker) const = 0;
361
362 /// Print plain-text dump of this VPlan to \p O.
363 void print(raw_ostream &O) const;
364
365 /// Print the successors of this block to \p O, prefixing all lines with \p
366 /// Indent.
367 void printSuccessors(raw_ostream &O, const Twine &Indent) const;
368
369 /// Dump this VPBlockBase to dbgs().
370 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
371#endif
372
373 /// Clone the current block and it's recipes without updating the operands of
374 /// the cloned recipes, including all blocks in the single-entry single-exit
375 /// region for VPRegionBlocks.
376 virtual VPBlockBase *clone() = 0;
377};
378
379/// VPRecipeBase is a base class modeling a sequence of one or more output IR
380/// instructions. VPRecipeBase owns the VPValues it defines through VPDef
381/// and is responsible for deleting its defined values. Single-value
382/// recipes must inherit from VPSingleDef instead of inheriting from both
383/// VPRecipeBase and VPValue separately.
385 : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock>,
386 public VPDef,
387 public VPUser {
388 friend VPBasicBlock;
389 friend class VPBlockUtils;
390
391 /// Each VPRecipe belongs to a single VPBasicBlock.
392 VPBasicBlock *Parent = nullptr;
393
394 /// The debug location for the recipe.
395 DebugLoc DL;
396
397public:
398 VPRecipeBase(const unsigned char SC, ArrayRef<VPValue *> Operands,
400 : VPDef(SC), VPUser(Operands), DL(DL) {}
401
402 ~VPRecipeBase() override = default;
403
404 /// Clone the current recipe.
405 virtual VPRecipeBase *clone() = 0;
406
407 /// \return the VPBasicBlock which this VPRecipe belongs to.
408 VPBasicBlock *getParent() { return Parent; }
409 const VPBasicBlock *getParent() const { return Parent; }
410
411 /// \return the VPRegionBlock which the recipe belongs to.
412 VPRegionBlock *getRegion();
413 const VPRegionBlock *getRegion() const;
414
415 /// The method which generates the output IR instructions that correspond to
416 /// this VPRecipe, thereby "executing" the VPlan.
417 virtual void execute(VPTransformState &State) = 0;
418
419 /// Return the cost of this recipe, taking into account if the cost
420 /// computation should be skipped and the ForceTargetInstructionCost flag.
421 /// Also takes care of printing the cost for debugging.
423
424 /// Insert an unlinked recipe into a basic block immediately before
425 /// the specified recipe.
426 void insertBefore(VPRecipeBase *InsertPos);
427 /// Insert an unlinked recipe into \p BB immediately before the insertion
428 /// point \p IP;
429 void insertBefore(VPBasicBlock &BB, iplist<VPRecipeBase>::iterator IP);
430
431 /// Insert an unlinked Recipe into a basic block immediately after
432 /// the specified Recipe.
433 void insertAfter(VPRecipeBase *InsertPos);
434
435 /// Unlink this recipe from its current VPBasicBlock and insert it into
436 /// the VPBasicBlock that MovePos lives in, right after MovePos.
437 void moveAfter(VPRecipeBase *MovePos);
438
439 /// Unlink this recipe and insert into BB before I.
440 ///
441 /// \pre I is a valid iterator into BB.
442 void moveBefore(VPBasicBlock &BB, iplist<VPRecipeBase>::iterator I);
443
444 /// This method unlinks 'this' from the containing basic block, but does not
445 /// delete it.
446 void removeFromParent();
447
448 /// This method unlinks 'this' from the containing basic block and deletes it.
449 ///
450 /// \returns an iterator pointing to the element after the erased one
452
453 /// Method to support type inquiry through isa, cast, and dyn_cast.
454 static inline bool classof(const VPDef *D) {
455 // All VPDefs are also VPRecipeBases.
456 return true;
457 }
458
459 static inline bool classof(const VPUser *U) { return true; }
460
461 /// Returns true if the recipe may have side-effects.
462 bool mayHaveSideEffects() const;
463
464 /// Returns true for PHI-like recipes.
465 bool isPhi() const;
466
467 /// Returns true if the recipe may read from memory.
468 bool mayReadFromMemory() const;
469
470 /// Returns true if the recipe may write to memory.
471 bool mayWriteToMemory() const;
472
473 /// Returns true if the recipe may read from or write to memory.
474 bool mayReadOrWriteMemory() const {
476 }
477
478 /// Returns the debug location of the recipe.
479 DebugLoc getDebugLoc() const { return DL; }
480
481 /// Return true if the recipe is a scalar cast.
482 bool isScalarCast() const;
483
484 /// Set the recipe's debug location to \p NewDL.
485 void setDebugLoc(DebugLoc NewDL) { DL = NewDL; }
486
487#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
488 /// Print the recipe, delegating to printRecipe().
489 void print(raw_ostream &O, const Twine &Indent,
490 VPSlotTracker &SlotTracker) const override final;
491#endif
492
493protected:
494 /// Compute the cost of this recipe either using a recipe's specialized
495 /// implementation or using the legacy cost model and the underlying
496 /// instructions.
497 virtual InstructionCost computeCost(ElementCount VF,
498 VPCostContext &Ctx) const;
499
500#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
501 /// Each concrete VPRecipe prints itself, without printing common information,
502 /// like debug info or metadata.
503 virtual void printRecipe(raw_ostream &O, const Twine &Indent,
504 VPSlotTracker &SlotTracker) const = 0;
505#endif
506};
507
508// Helper macro to define common classof implementations for recipes.
509#define VP_CLASSOF_IMPL(VPDefID) \
510 static inline bool classof(const VPDef *D) { \
511 return D->getVPDefID() == VPDefID; \
512 } \
513 static inline bool classof(const VPValue *V) { \
514 auto *R = V->getDefiningRecipe(); \
515 return R && R->getVPDefID() == VPDefID; \
516 } \
517 static inline bool classof(const VPUser *U) { \
518 auto *R = dyn_cast<VPRecipeBase>(U); \
519 return R && R->getVPDefID() == VPDefID; \
520 } \
521 static inline bool classof(const VPRecipeBase *R) { \
522 return R->getVPDefID() == VPDefID; \
523 } \
524 static inline bool classof(const VPSingleDefRecipe *R) { \
525 return R->getVPDefID() == VPDefID; \
526 }
527
528/// VPSingleDef is a base class for recipes for modeling a sequence of one or
529/// more output IR that define a single result VPValue.
530/// Note that VPRecipeBase must be inherited from before VPValue.
531class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
532public:
533 VPSingleDefRecipe(const unsigned char SC, ArrayRef<VPValue *> Operands,
535 : VPRecipeBase(SC, Operands, DL), VPValue(this) {}
536
537 VPSingleDefRecipe(const unsigned char SC, ArrayRef<VPValue *> Operands,
539 : VPRecipeBase(SC, Operands, DL), VPValue(this, UV) {}
540
541 static inline bool classof(const VPRecipeBase *R) {
542 switch (R->getVPDefID()) {
543 case VPRecipeBase::VPDerivedIVSC:
544 case VPRecipeBase::VPEVLBasedIVPHISC:
545 case VPRecipeBase::VPExpandSCEVSC:
546 case VPRecipeBase::VPExpressionSC:
547 case VPRecipeBase::VPInstructionSC:
548 case VPRecipeBase::VPReductionEVLSC:
549 case VPRecipeBase::VPReductionSC:
550 case VPRecipeBase::VPReplicateSC:
551 case VPRecipeBase::VPScalarIVStepsSC:
552 case VPRecipeBase::VPVectorPointerSC:
553 case VPRecipeBase::VPVectorEndPointerSC:
554 case VPRecipeBase::VPWidenCallSC:
555 case VPRecipeBase::VPWidenCanonicalIVSC:
556 case VPRecipeBase::VPWidenCastSC:
557 case VPRecipeBase::VPWidenGEPSC:
558 case VPRecipeBase::VPWidenIntrinsicSC:
559 case VPRecipeBase::VPWidenSC:
560 case VPRecipeBase::VPWidenSelectSC:
561 case VPRecipeBase::VPBlendSC:
562 case VPRecipeBase::VPPredInstPHISC:
563 case VPRecipeBase::VPCanonicalIVPHISC:
564 case VPRecipeBase::VPActiveLaneMaskPHISC:
565 case VPRecipeBase::VPFirstOrderRecurrencePHISC:
566 case VPRecipeBase::VPWidenPHISC:
567 case VPRecipeBase::VPWidenIntOrFpInductionSC:
568 case VPRecipeBase::VPWidenPointerInductionSC:
569 case VPRecipeBase::VPReductionPHISC:
570 return true;
571 case VPRecipeBase::VPBranchOnMaskSC:
572 case VPRecipeBase::VPInterleaveEVLSC:
573 case VPRecipeBase::VPInterleaveSC:
574 case VPRecipeBase::VPIRInstructionSC:
575 case VPRecipeBase::VPWidenLoadEVLSC:
576 case VPRecipeBase::VPWidenLoadSC:
577 case VPRecipeBase::VPWidenStoreEVLSC:
578 case VPRecipeBase::VPWidenStoreSC:
579 case VPRecipeBase::VPHistogramSC:
580 // TODO: Widened stores don't define a value, but widened loads do. Split
581 // the recipes to be able to make widened loads VPSingleDefRecipes.
582 return false;
583 }
584 llvm_unreachable("Unhandled VPDefID");
585 }
586
587 static inline bool classof(const VPUser *U) {
588 auto *R = dyn_cast<VPRecipeBase>(U);
589 return R && classof(R);
590 }
591
592 VPSingleDefRecipe *clone() override = 0;
593
594 /// Returns the underlying instruction.
601
602#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
603 /// Print this VPSingleDefRecipe to dbgs() (for debugging).
605#endif
606};
607
608/// Class to record and manage LLVM IR flags.
610 enum class OperationType : unsigned char {
611 Cmp,
612 FCmp,
613 OverflowingBinOp,
614 Trunc,
615 DisjointOp,
616 PossiblyExactOp,
617 GEPOp,
618 FPMathOp,
619 NonNegOp,
620 Other
621 };
622
623public:
624 struct WrapFlagsTy {
625 char HasNUW : 1;
626 char HasNSW : 1;
627
629 };
630
632 char HasNUW : 1;
633 char HasNSW : 1;
634
636 };
637
642
644 char NonNeg : 1;
645 NonNegFlagsTy(bool IsNonNeg) : NonNeg(IsNonNeg) {}
646 };
647
648private:
649 struct ExactFlagsTy {
650 char IsExact : 1;
651 };
652 struct FastMathFlagsTy {
653 char AllowReassoc : 1;
654 char NoNaNs : 1;
655 char NoInfs : 1;
656 char NoSignedZeros : 1;
657 char AllowReciprocal : 1;
658 char AllowContract : 1;
659 char ApproxFunc : 1;
660
661 LLVM_ABI_FOR_TEST FastMathFlagsTy(const FastMathFlags &FMF);
662 };
663 /// Holds both the predicate and fast-math flags for floating-point
664 /// comparisons.
665 struct FCmpFlagsTy {
667 FastMathFlagsTy FMFs;
668 };
669
670 OperationType OpType;
671
672 union {
677 ExactFlagsTy ExactFlags;
680 FastMathFlagsTy FMFs;
681 FCmpFlagsTy FCmpFlags;
682 unsigned AllFlags;
683 };
684
685public:
686 VPIRFlags() : OpType(OperationType::Other), AllFlags(0) {}
687
689 if (auto *FCmp = dyn_cast<FCmpInst>(&I)) {
690 OpType = OperationType::FCmp;
691 FCmpFlags.Pred = FCmp->getPredicate();
692 FCmpFlags.FMFs = FCmp->getFastMathFlags();
693 } else if (auto *Op = dyn_cast<CmpInst>(&I)) {
694 OpType = OperationType::Cmp;
695 CmpPredicate = Op->getPredicate();
696 } else if (auto *Op = dyn_cast<PossiblyDisjointInst>(&I)) {
697 OpType = OperationType::DisjointOp;
698 DisjointFlags.IsDisjoint = Op->isDisjoint();
699 } else if (auto *Op = dyn_cast<OverflowingBinaryOperator>(&I)) {
700 OpType = OperationType::OverflowingBinOp;
701 WrapFlags = {Op->hasNoUnsignedWrap(), Op->hasNoSignedWrap()};
702 } else if (auto *Op = dyn_cast<TruncInst>(&I)) {
703 OpType = OperationType::Trunc;
704 TruncFlags = {Op->hasNoUnsignedWrap(), Op->hasNoSignedWrap()};
705 } else if (auto *Op = dyn_cast<PossiblyExactOperator>(&I)) {
706 OpType = OperationType::PossiblyExactOp;
707 ExactFlags.IsExact = Op->isExact();
708 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
709 OpType = OperationType::GEPOp;
710 GEPFlags = GEP->getNoWrapFlags();
711 } else if (auto *PNNI = dyn_cast<PossiblyNonNegInst>(&I)) {
712 OpType = OperationType::NonNegOp;
713 NonNegFlags.NonNeg = PNNI->hasNonNeg();
714 } else if (auto *Op = dyn_cast<FPMathOperator>(&I)) {
715 OpType = OperationType::FPMathOp;
716 FMFs = Op->getFastMathFlags();
717 } else {
718 OpType = OperationType::Other;
719 AllFlags = 0;
720 }
721 }
722
724 : OpType(OperationType::Cmp), CmpPredicate(Pred) {}
725
727 : OpType(OperationType::FCmp) {
728 FCmpFlags.Pred = Pred;
729 FCmpFlags.FMFs = FMFs;
730 }
731
733 : OpType(OperationType::OverflowingBinOp), WrapFlags(WrapFlags) {}
734
736 : OpType(OperationType::Trunc), TruncFlags(TruncFlags) {}
737
738 VPIRFlags(FastMathFlags FMFs) : OpType(OperationType::FPMathOp), FMFs(FMFs) {}
739
741 : OpType(OperationType::DisjointOp), DisjointFlags(DisjointFlags) {}
742
744 : OpType(OperationType::NonNegOp), NonNegFlags(NonNegFlags) {}
745
747 : OpType(OperationType::GEPOp), GEPFlags(GEPFlags) {}
748
750 OpType = Other.OpType;
751 AllFlags = Other.AllFlags;
752 }
753
754 /// Only keep flags also present in \p Other. \p Other must have the same
755 /// OpType as the current object.
756 void intersectFlags(const VPIRFlags &Other);
757
758 /// Drop all poison-generating flags.
760 // NOTE: This needs to be kept in-sync with
761 // Instruction::dropPoisonGeneratingFlags.
762 switch (OpType) {
763 case OperationType::OverflowingBinOp:
764 WrapFlags.HasNUW = false;
765 WrapFlags.HasNSW = false;
766 break;
767 case OperationType::Trunc:
768 TruncFlags.HasNUW = false;
769 TruncFlags.HasNSW = false;
770 break;
771 case OperationType::DisjointOp:
772 DisjointFlags.IsDisjoint = false;
773 break;
774 case OperationType::PossiblyExactOp:
775 ExactFlags.IsExact = false;
776 break;
777 case OperationType::GEPOp:
779 break;
780 case OperationType::FPMathOp:
781 case OperationType::FCmp:
782 getFMFsRef().NoNaNs = false;
783 getFMFsRef().NoInfs = false;
784 break;
785 case OperationType::NonNegOp:
786 NonNegFlags.NonNeg = false;
787 break;
788 case OperationType::Cmp:
789 case OperationType::Other:
790 break;
791 }
792 }
793
794 /// Apply the IR flags to \p I.
795 void applyFlags(Instruction &I) const {
796 switch (OpType) {
797 case OperationType::OverflowingBinOp:
798 I.setHasNoUnsignedWrap(WrapFlags.HasNUW);
799 I.setHasNoSignedWrap(WrapFlags.HasNSW);
800 break;
801 case OperationType::Trunc:
802 I.setHasNoUnsignedWrap(TruncFlags.HasNUW);
803 I.setHasNoSignedWrap(TruncFlags.HasNSW);
804 break;
805 case OperationType::DisjointOp:
806 cast<PossiblyDisjointInst>(&I)->setIsDisjoint(DisjointFlags.IsDisjoint);
807 break;
808 case OperationType::PossiblyExactOp:
809 I.setIsExact(ExactFlags.IsExact);
810 break;
811 case OperationType::GEPOp:
812 cast<GetElementPtrInst>(&I)->setNoWrapFlags(GEPFlags);
813 break;
814 case OperationType::FPMathOp:
815 case OperationType::FCmp: {
816 const FastMathFlagsTy &F = getFMFsRef();
817 I.setHasAllowReassoc(F.AllowReassoc);
818 I.setHasNoNaNs(F.NoNaNs);
819 I.setHasNoInfs(F.NoInfs);
820 I.setHasNoSignedZeros(F.NoSignedZeros);
821 I.setHasAllowReciprocal(F.AllowReciprocal);
822 I.setHasAllowContract(F.AllowContract);
823 I.setHasApproxFunc(F.ApproxFunc);
824 break;
825 }
826 case OperationType::NonNegOp:
827 I.setNonNeg(NonNegFlags.NonNeg);
828 break;
829 case OperationType::Cmp:
830 case OperationType::Other:
831 break;
832 }
833 }
834
836 assert((OpType == OperationType::Cmp || OpType == OperationType::FCmp) &&
837 "recipe doesn't have a compare predicate");
838 return OpType == OperationType::FCmp ? FCmpFlags.Pred : CmpPredicate;
839 }
840
842 assert((OpType == OperationType::Cmp || OpType == OperationType::FCmp) &&
843 "recipe doesn't have a compare predicate");
844 if (OpType == OperationType::FCmp)
845 FCmpFlags.Pred = Pred;
846 else
847 CmpPredicate = Pred;
848 }
849
851
852 /// Returns true if the recipe has a comparison predicate.
853 bool hasPredicate() const {
854 return OpType == OperationType::Cmp || OpType == OperationType::FCmp;
855 }
856
857 /// Returns true if the recipe has fast-math flags.
858 bool hasFastMathFlags() const {
859 return OpType == OperationType::FPMathOp || OpType == OperationType::FCmp;
860 }
861
863
864 /// Returns true if the recipe has non-negative flag.
865 bool hasNonNegFlag() const { return OpType == OperationType::NonNegOp; }
866
867 bool isNonNeg() const {
868 assert(OpType == OperationType::NonNegOp &&
869 "recipe doesn't have a NNEG flag");
870 return NonNegFlags.NonNeg;
871 }
872
873 bool hasNoUnsignedWrap() const {
874 switch (OpType) {
875 case OperationType::OverflowingBinOp:
876 return WrapFlags.HasNUW;
877 case OperationType::Trunc:
878 return TruncFlags.HasNUW;
879 default:
880 llvm_unreachable("recipe doesn't have a NUW flag");
881 }
882 }
883
884 bool hasNoSignedWrap() const {
885 switch (OpType) {
886 case OperationType::OverflowingBinOp:
887 return WrapFlags.HasNSW;
888 case OperationType::Trunc:
889 return TruncFlags.HasNSW;
890 default:
891 llvm_unreachable("recipe doesn't have a NSW flag");
892 }
893 }
894
895 bool isDisjoint() const {
896 assert(OpType == OperationType::DisjointOp &&
897 "recipe cannot have a disjoing flag");
898 return DisjointFlags.IsDisjoint;
899 }
900
901private:
902 /// Get a reference to the fast-math flags for FPMathOp or FCmp.
903 FastMathFlagsTy &getFMFsRef() {
904 return OpType == OperationType::FCmp ? FCmpFlags.FMFs : FMFs;
905 }
906 const FastMathFlagsTy &getFMFsRef() const {
907 return OpType == OperationType::FCmp ? FCmpFlags.FMFs : FMFs;
908 }
909
910public:
911#if !defined(NDEBUG)
912 /// Returns true if the set flags are valid for \p Opcode.
913 LLVM_ABI_FOR_TEST bool flagsValidForOpcode(unsigned Opcode) const;
914#endif
915
916#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
917 void printFlags(raw_ostream &O) const;
918#endif
919};
920
921/// A pure-virtual common base class for recipes defining a single VPValue and
922/// using IR flags.
924 VPRecipeWithIRFlags(const unsigned char SC, ArrayRef<VPValue *> Operands,
925 const VPIRFlags &Flags,
927 : VPSingleDefRecipe(SC, Operands, DL), VPIRFlags(Flags) {}
928
929 static inline bool classof(const VPRecipeBase *R) {
930 return R->getVPDefID() == VPRecipeBase::VPInstructionSC ||
931 R->getVPDefID() == VPRecipeBase::VPWidenSC ||
932 R->getVPDefID() == VPRecipeBase::VPWidenGEPSC ||
933 R->getVPDefID() == VPRecipeBase::VPWidenCallSC ||
934 R->getVPDefID() == VPRecipeBase::VPWidenCastSC ||
935 R->getVPDefID() == VPRecipeBase::VPWidenIntrinsicSC ||
936 R->getVPDefID() == VPRecipeBase::VPWidenSelectSC ||
937 R->getVPDefID() == VPRecipeBase::VPReductionSC ||
938 R->getVPDefID() == VPRecipeBase::VPReductionEVLSC ||
939 R->getVPDefID() == VPRecipeBase::VPReplicateSC ||
940 R->getVPDefID() == VPRecipeBase::VPVectorEndPointerSC ||
941 R->getVPDefID() == VPRecipeBase::VPVectorPointerSC;
942 }
943
944 static inline bool classof(const VPUser *U) {
945 auto *R = dyn_cast<VPRecipeBase>(U);
946 return R && classof(R);
947 }
948
949 static inline bool classof(const VPValue *V) {
950 auto *R = dyn_cast_or_null<VPRecipeBase>(V->getDefiningRecipe());
951 return R && classof(R);
952 }
953
954 VPRecipeWithIRFlags *clone() override = 0;
955
956 static inline bool classof(const VPSingleDefRecipe *U) {
957 auto *R = dyn_cast<VPRecipeBase>(U);
958 return R && classof(R);
959 }
960
961 void execute(VPTransformState &State) override = 0;
962
963 /// Compute the cost for this recipe for \p VF, using \p Opcode and \p Ctx.
965 VPCostContext &Ctx) const;
966};
967
968/// Helper to access the operand that contains the unroll part for this recipe
969/// after unrolling.
970template <unsigned PartOpIdx> class LLVM_ABI_FOR_TEST VPUnrollPartAccessor {
971protected:
972 /// Return the VPValue operand containing the unroll part or null if there is
973 /// no such operand.
974 VPValue *getUnrollPartOperand(const VPUser &U) const;
975
976 /// Return the unroll part.
977 unsigned getUnrollPart(const VPUser &U) const;
978};
979
980/// Helper to manage IR metadata for recipes. It filters out metadata that
981/// cannot be propagated.
984
985public:
986 VPIRMetadata() = default;
987
988 /// Adds metatadata that can be preserved from the original instruction
989 /// \p I.
991
992 /// Copy constructor for cloning.
993 VPIRMetadata(const VPIRMetadata &Other) = default;
994
996
997 /// Add all metadata to \p I.
998 void applyMetadata(Instruction &I) const;
999
1000 /// Set metadata with kind \p Kind to \p Node. If metadata with \p Kind
1001 /// already exists, it will be replaced. Otherwise, it will be added.
1002 void setMetadata(unsigned Kind, MDNode *Node) {
1003 auto It =
1004 llvm::find_if(Metadata, [Kind](const std::pair<unsigned, MDNode *> &P) {
1005 return P.first == Kind;
1006 });
1007 if (It != Metadata.end())
1008 It->second = Node;
1009 else
1010 Metadata.emplace_back(Kind, Node);
1011 }
1012
1013 /// Intersect this VPIRMetadata object with \p MD, keeping only metadata
1014 /// nodes that are common to both.
1015 void intersect(const VPIRMetadata &MD);
1016
1017 /// Get metadata of kind \p Kind. Returns nullptr if not found.
1018 MDNode *getMetadata(unsigned Kind) const {
1019 auto It =
1020 find_if(Metadata, [Kind](const auto &P) { return P.first == Kind; });
1021 return It != Metadata.end() ? It->second : nullptr;
1022 }
1023
1024#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1025 /// Print metadata with node IDs.
1026 void print(raw_ostream &O, VPSlotTracker &SlotTracker) const;
1027#endif
1028};
1029
1030/// This is a concrete Recipe that models a single VPlan-level instruction.
1031/// While as any Recipe it may generate a sequence of IR instructions when
1032/// executed, these instructions would always form a single-def expression as
1033/// the VPInstruction is also a single def-use vertex.
1035 public VPIRMetadata,
1036 public VPUnrollPartAccessor<1> {
1037 friend class VPlanSlp;
1038
1039public:
1040 /// VPlan opcodes, extending LLVM IR with idiomatics instructions.
1041 enum {
1043 Instruction::OtherOpsEnd + 1, // Combines the incoming and previous
1044 // values of a first-order recurrence.
1048 // Creates a mask where each lane is active (true) whilst the current
1049 // counter (first operand + index) is less than the second operand. i.e.
1050 // mask[i] = icmpt ult (op0 + i), op1
1051 // The size of the mask returned is VF * Multiplier (UF, third op).
1055 // Increment the canonical IV separately for each unrolled part.
1060 /// Given operands of (the same) struct type, creates a struct of fixed-
1061 /// width vectors each containing a struct field of all operands. The
1062 /// number of operands matches the element count of every vector.
1064 /// Creates a fixed-width vector containing all operands. The number of
1065 /// operands matches the vector element count.
1067 /// Extracts all lanes from its (non-scalable) vector operand. This is an
1068 /// abstract VPInstruction whose single defined VPValue represents VF
1069 /// scalars extracted from a vector, to be replaced by VF ExtractElement
1070 /// VPInstructions.
1072 /// Compute the final result of a AnyOf reduction with select(cmp(),x,y),
1073 /// where one of (x,y) is loop invariant, and both x and y are integer type.
1077 // Extracts the last part of its operand. Removed during unrolling.
1079 // Extracts the last lane of its vector operand, per part.
1081 // Extracts the second-to-last lane from its operand or the second-to-last
1082 // part if it is scalar. In the latter case, the recipe will be removed
1083 // during unrolling.
1085 LogicalAnd, // Non-poison propagating logical And.
1086 // Add an offset in bytes (second operand) to a base pointer (first
1087 // operand). Only generates scalar values (either for the first lane only or
1088 // for all lanes, depending on its uses).
1090 // Add a vector offset in bytes (second operand) to a scalar base pointer
1091 // (first operand).
1093 // Returns a scalar boolean value, which is true if any lane of its
1094 // (boolean) vector operands is true. It produces the reduced value across
1095 // all unrolled iterations. Unrolling will add all copies of its original
1096 // operand as additional operands. AnyOf is poison-safe as all operands
1097 // will be frozen.
1099 // Calculates the first active lane index of the vector predicate operands.
1100 // It produces the lane index across all unrolled iterations. Unrolling will
1101 // add all copies of its original operand as additional operands.
1102 // Implemented with @llvm.experimental.cttz.elts, but returns the expected
1103 // result even with operands that are all zeroes.
1105 // Calculates the last active lane index of the vector predicate operands.
1106 // The predicates must be prefix-masks (all 1s before all 0s). Used when
1107 // tail-folding to extract the correct live-out value from the last active
1108 // iteration. It produces the lane index across all unrolled iterations.
1109 // Unrolling will add all copies of its original operand as additional
1110 // operands.
1112
1113 // The opcodes below are used for VPInstructionWithType.
1114 //
1115 /// Scale the first operand (vector step) by the second operand
1116 /// (scalar-step). Casts both operands to the result type if needed.
1118 /// Start vector for reductions with 3 operands: the original start value,
1119 /// the identity value for the reduction and an integer indicating the
1120 /// scaling factor.
1122 // Creates a step vector starting from 0 to VF with a step of 1.
1124 /// Extracts a single lane (first operand) from a set of vector operands.
1125 /// The lane specifies an index into a vector formed by combining all vector
1126 /// operands (all operands after the first one).
1128 /// Explicit user for the resume phi of the canonical induction in the main
1129 /// VPlan, used by the epilogue vector loop.
1131 /// Returns the value for vscale.
1134 };
1135
1136 /// Returns true if this VPInstruction generates scalar values for all lanes.
1137 /// Most VPInstructions generate a single value per part, either vector or
1138 /// scalar. VPReplicateRecipe takes care of generating multiple (scalar)
1139 /// values per all lanes, stemming from an original ingredient. This method
1140 /// identifies the (rare) cases of VPInstructions that do so as well, w/o an
1141 /// underlying ingredient.
1142 bool doesGeneratePerAllLanes() const;
1143
1144private:
1145 typedef unsigned char OpcodeTy;
1146 OpcodeTy Opcode;
1147
1148 /// An optional name that can be used for the generated IR instruction.
1149 std::string Name;
1150
1151 /// Returns true if we can generate a scalar for the first lane only if
1152 /// needed.
1153 bool canGenerateScalarForFirstLane() const;
1154
1155 /// Utility methods serving execute(): generates a single vector instance of
1156 /// the modeled instruction. \returns the generated value. . In some cases an
1157 /// existing value is returned rather than a generated one.
1158 Value *generate(VPTransformState &State);
1159
1160#if !defined(NDEBUG)
1161 /// Return the number of operands determined by the opcode of the
1162 /// VPInstruction. Returns -1u if the number of operands cannot be determined
1163 /// directly by the opcode.
1164 static unsigned getNumOperandsForOpcode(unsigned Opcode);
1165#endif
1166
1167public:
1168 VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands,
1169 const VPIRFlags &Flags = {}, const VPIRMetadata &MD = {},
1170 DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "");
1171
1172 VP_CLASSOF_IMPL(VPDef::VPInstructionSC)
1173
1174 VPInstruction *clone() override {
1175 auto *New = new VPInstruction(Opcode, operands(), *this, *this,
1176 getDebugLoc(), Name);
1177 if (getUnderlyingValue())
1178 New->setUnderlyingValue(getUnderlyingInstr());
1179 return New;
1180 }
1181
1182 unsigned getOpcode() const { return Opcode; }
1183
1184 /// Generate the instruction.
1185 /// TODO: We currently execute only per-part unless a specific instance is
1186 /// provided.
1187 void execute(VPTransformState &State) override;
1188
1189 /// Return the cost of this VPInstruction.
1190 InstructionCost computeCost(ElementCount VF,
1191 VPCostContext &Ctx) const override;
1192
1193#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1194 /// Print the VPInstruction to dbgs() (for debugging).
1195 LLVM_DUMP_METHOD void dump() const;
1196#endif
1197
1198 bool hasResult() const {
1199 // CallInst may or may not have a result, depending on the called function.
1200 // Conservatively return calls have results for now.
1201 switch (getOpcode()) {
1202 case Instruction::Ret:
1203 case Instruction::Br:
1204 case Instruction::Store:
1205 case Instruction::Switch:
1206 case Instruction::IndirectBr:
1207 case Instruction::Resume:
1208 case Instruction::CatchRet:
1209 case Instruction::Unreachable:
1210 case Instruction::Fence:
1211 case Instruction::AtomicRMW:
1214 return false;
1215 default:
1216 return true;
1217 }
1218 }
1219
1220 /// Returns true if the underlying opcode may read from or write to memory.
1221 bool opcodeMayReadOrWriteFromMemory() const;
1222
1223 /// Returns true if the recipe only uses the first lane of operand \p Op.
1224 bool usesFirstLaneOnly(const VPValue *Op) const override;
1225
1226 /// Returns true if the recipe only uses the first part of operand \p Op.
1227 bool usesFirstPartOnly(const VPValue *Op) const override;
1228
1229 /// Returns true if this VPInstruction produces a scalar value from a vector,
1230 /// e.g. by performing a reduction or extracting a lane.
1231 bool isVectorToScalar() const;
1232
1233 /// Returns true if this VPInstruction's operands are single scalars and the
1234 /// result is also a single scalar.
1235 bool isSingleScalar() const;
1236
1237 /// Returns the symbolic name assigned to the VPInstruction.
1238 StringRef getName() const { return Name; }
1239
1240 /// Set the symbolic name for the VPInstruction.
1241 void setName(StringRef NewName) { Name = NewName.str(); }
1242
1243protected:
1244#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1245 /// Print the VPInstruction to \p O.
1246 void printRecipe(raw_ostream &O, const Twine &Indent,
1247 VPSlotTracker &SlotTracker) const override;
1248#endif
1249};
1250
1251/// A specialization of VPInstruction augmenting it with a dedicated result
1252/// type, to be used when the opcode and operands of the VPInstruction don't
1253/// directly determine the result type. Note that there is no separate VPDef ID
1254/// for VPInstructionWithType; it shares the same ID as VPInstruction and is
1255/// distinguished purely by the opcode.
1257 /// Scalar result type produced by the recipe.
1258 Type *ResultTy;
1259
1260public:
1262 Type *ResultTy, const VPIRFlags &Flags = {},
1263 const VPIRMetadata &Metadata = {},
1265 const Twine &Name = "")
1266 : VPInstruction(Opcode, Operands, Flags, Metadata, DL, Name),
1267 ResultTy(ResultTy) {}
1268
1269 static inline bool classof(const VPRecipeBase *R) {
1270 // VPInstructionWithType are VPInstructions with specific opcodes requiring
1271 // type information.
1272 if (R->isScalarCast())
1273 return true;
1274 auto *VPI = dyn_cast<VPInstruction>(R);
1275 if (!VPI)
1276 return false;
1277 switch (VPI->getOpcode()) {
1281 return true;
1282 default:
1283 return false;
1284 }
1285 }
1286
1287 static inline bool classof(const VPUser *R) {
1289 }
1290
1291 VPInstruction *clone() override {
1292 auto *New =
1294 *this, *this, getDebugLoc(), getName());
1295 New->setUnderlyingValue(getUnderlyingValue());
1296 return New;
1297 }
1298
1299 void execute(VPTransformState &State) override;
1300
1301 /// Return the cost of this VPInstruction.
1303 VPCostContext &Ctx) const override {
1304 // TODO: Compute accurate cost after retiring the legacy cost model.
1305 return 0;
1306 }
1307
1308 Type *getResultType() const { return ResultTy; }
1309
1310protected:
1311#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1312 /// Print the recipe.
1313 void printRecipe(raw_ostream &O, const Twine &Indent,
1314 VPSlotTracker &SlotTracker) const override;
1315#endif
1316};
1317
1318/// Helper type to provide functions to access incoming values and blocks for
1319/// phi-like recipes.
1321protected:
1322 /// Return a VPRecipeBase* to the current object.
1323 virtual const VPRecipeBase *getAsRecipe() const = 0;
1324
1325public:
1326 virtual ~VPPhiAccessors() = default;
1327
1328 /// Returns the incoming VPValue with index \p Idx.
1329 VPValue *getIncomingValue(unsigned Idx) const {
1330 return getAsRecipe()->getOperand(Idx);
1331 }
1332
1333 /// Returns the incoming block with index \p Idx.
1334 const VPBasicBlock *getIncomingBlock(unsigned Idx) const;
1335
1336 /// Returns the number of incoming values, also number of incoming blocks.
1337 virtual unsigned getNumIncoming() const {
1338 return getAsRecipe()->getNumOperands();
1339 }
1340
1341 /// Returns an interator range over the incoming values.
1343 return make_range(getAsRecipe()->op_begin(),
1344 getAsRecipe()->op_begin() + getNumIncoming());
1345 }
1346
1348 detail::index_iterator, std::function<const VPBasicBlock *(size_t)>>>;
1349
1350 /// Returns an iterator range over the incoming blocks.
1352 std::function<const VPBasicBlock *(size_t)> GetBlock = [this](size_t Idx) {
1353 return getIncomingBlock(Idx);
1354 };
1355 return map_range(index_range(0, getNumIncoming()), GetBlock);
1356 }
1357
1358 /// Returns an iterator range over pairs of incoming values and corresponding
1359 /// incoming blocks.
1365
1366 /// Removes the incoming value for \p IncomingBlock, which must be a
1367 /// predecessor.
1368 void removeIncomingValueFor(VPBlockBase *IncomingBlock) const;
1369
1370#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1371 /// Print the recipe.
1373#endif
1374};
1375
1377 VPPhi(ArrayRef<VPValue *> Operands, DebugLoc DL, const Twine &Name = "")
1378 : VPInstruction(Instruction::PHI, Operands, {}, {}, DL, Name) {}
1379
1380 static inline bool classof(const VPUser *U) {
1381 auto *VPI = dyn_cast<VPInstruction>(U);
1382 return VPI && VPI->getOpcode() == Instruction::PHI;
1383 }
1384
1385 static inline bool classof(const VPValue *V) {
1386 auto *VPI = dyn_cast<VPInstruction>(V);
1387 return VPI && VPI->getOpcode() == Instruction::PHI;
1388 }
1389
1390 static inline bool classof(const VPSingleDefRecipe *SDR) {
1391 auto *VPI = dyn_cast<VPInstruction>(SDR);
1392 return VPI && VPI->getOpcode() == Instruction::PHI;
1393 }
1394
1395 VPPhi *clone() override {
1396 auto *PhiR = new VPPhi(operands(), getDebugLoc(), getName());
1397 PhiR->setUnderlyingValue(getUnderlyingValue());
1398 return PhiR;
1399 }
1400
1401 void execute(VPTransformState &State) override;
1402
1403protected:
1404#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1405 /// Print the recipe.
1406 void printRecipe(raw_ostream &O, const Twine &Indent,
1407 VPSlotTracker &SlotTracker) const override;
1408#endif
1409
1410 const VPRecipeBase *getAsRecipe() const override { return this; }
1411};
1412
1413/// A recipe to wrap on original IR instruction not to be modified during
1414/// execution, except for PHIs. PHIs are modeled via the VPIRPhi subclass.
1415/// Expect PHIs, VPIRInstructions cannot have any operands.
1417 Instruction &I;
1418
1419protected:
1420 /// VPIRInstruction::create() should be used to create VPIRInstructions, as
1421 /// subclasses may need to be created, e.g. VPIRPhi.
1423 : VPRecipeBase(VPDef::VPIRInstructionSC, ArrayRef<VPValue *>()), I(I) {}
1424
1425public:
1426 ~VPIRInstruction() override = default;
1427
1428 /// Create a new VPIRPhi for \p \I, if it is a PHINode, otherwise create a
1429 /// VPIRInstruction.
1431
1432 VP_CLASSOF_IMPL(VPDef::VPIRInstructionSC)
1433
1435 auto *R = create(I);
1436 for (auto *Op : operands())
1437 R->addOperand(Op);
1438 return R;
1439 }
1440
1441 void execute(VPTransformState &State) override;
1442
1443 /// Return the cost of this VPIRInstruction.
1445 computeCost(ElementCount VF, VPCostContext &Ctx) const override;
1446
1447 Instruction &getInstruction() const { return I; }
1448
1449 bool usesScalars(const VPValue *Op) const override {
1451 "Op must be an operand of the recipe");
1452 return true;
1453 }
1454
1455 bool usesFirstPartOnly(const VPValue *Op) const override {
1457 "Op must be an operand of the recipe");
1458 return true;
1459 }
1460
1461 bool usesFirstLaneOnly(const VPValue *Op) const override {
1463 "Op must be an operand of the recipe");
1464 return true;
1465 }
1466
1467 /// Update the recipe's first operand to the last lane of the last part of the
1468 /// operand using \p Builder. Must only be used for VPIRInstructions with at
1469 /// least one operand wrapping a PHINode.
1471
1472protected:
1473#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1474 /// Print the recipe.
1475 void printRecipe(raw_ostream &O, const Twine &Indent,
1476 VPSlotTracker &SlotTracker) const override;
1477#endif
1478};
1479
1480/// An overlay for VPIRInstructions wrapping PHI nodes enabling convenient use
1481/// cast/dyn_cast/isa and execute() implementation. A single VPValue operand is
1482/// allowed, and it is used to add a new incoming value for the single
1483/// predecessor VPBB.
1485 public VPPhiAccessors {
1487
1488 static inline bool classof(const VPRecipeBase *U) {
1489 auto *R = dyn_cast<VPIRInstruction>(U);
1490 return R && isa<PHINode>(R->getInstruction());
1491 }
1492
1494
1495 void execute(VPTransformState &State) override;
1496
1497protected:
1498#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1499 /// Print the recipe.
1500 void printRecipe(raw_ostream &O, const Twine &Indent,
1501 VPSlotTracker &SlotTracker) const override;
1502#endif
1503
1504 const VPRecipeBase *getAsRecipe() const override { return this; }
1505};
1506
1507/// VPWidenRecipe is a recipe for producing a widened instruction using the
1508/// opcode and operands of the recipe. This recipe covers most of the
1509/// traditional vectorization cases where each recipe transforms into a
1510/// vectorized version of itself.
1512 public VPIRMetadata {
1513 unsigned Opcode;
1514
1515public:
1517 const VPIRFlags &Flags = {}, const VPIRMetadata &Metadata = {},
1518 DebugLoc DL = {})
1519 : VPRecipeWithIRFlags(VPDef::VPWidenSC, Operands, Flags, DL),
1520 VPIRMetadata(Metadata), Opcode(I.getOpcode()) {
1521 setUnderlyingValue(&I);
1522 }
1523
1524 ~VPWidenRecipe() override = default;
1525
1526 VPWidenRecipe *clone() override {
1527 return new VPWidenRecipe(*getUnderlyingInstr(), operands(), *this, *this,
1528 getDebugLoc());
1529 }
1530
1531 VP_CLASSOF_IMPL(VPDef::VPWidenSC)
1532
1533 /// Produce a widened instruction using the opcode and operands of the recipe,
1534 /// processing State.VF elements.
1535 void execute(VPTransformState &State) override;
1536
1537 /// Return the cost of this VPWidenRecipe.
1538 InstructionCost computeCost(ElementCount VF,
1539 VPCostContext &Ctx) const override;
1540
1541 unsigned getOpcode() const { return Opcode; }
1542
1543protected:
1544#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1545 /// Print the recipe.
1546 void printRecipe(raw_ostream &O, const Twine &Indent,
1547 VPSlotTracker &SlotTracker) const override;
1548#endif
1549};
1550
1551/// VPWidenCastRecipe is a recipe to create vector cast instructions.
1553 /// Cast instruction opcode.
1554 Instruction::CastOps Opcode;
1555
1556 /// Result type for the cast.
1557 Type *ResultTy;
1558
1559public:
1561 CastInst *CI = nullptr, const VPIRFlags &Flags = {},
1562 const VPIRMetadata &Metadata = {},
1564 : VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, Flags, DL),
1565 VPIRMetadata(Metadata), Opcode(Opcode), ResultTy(ResultTy) {
1566 assert(flagsValidForOpcode(Opcode) &&
1567 "Set flags not supported for the provided opcode");
1569 }
1570
1571 ~VPWidenCastRecipe() override = default;
1572
1574 return new VPWidenCastRecipe(Opcode, getOperand(0), ResultTy,
1576 *this, *this, getDebugLoc());
1577 }
1578
1579 VP_CLASSOF_IMPL(VPDef::VPWidenCastSC)
1580
1581 /// Produce widened copies of the cast.
1582 LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override;
1583
1584 /// Return the cost of this VPWidenCastRecipe.
1586 computeCost(ElementCount VF, VPCostContext &Ctx) const override;
1587
1588 Instruction::CastOps getOpcode() const { return Opcode; }
1589
1590 /// Returns the result type of the cast.
1591 Type *getResultType() const { return ResultTy; }
1592
1593protected:
1594#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1595 /// Print the recipe.
1596 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
1597 VPSlotTracker &SlotTracker) const override;
1598#endif
1599};
1600
1601/// A recipe for widening vector intrinsics.
1603 /// ID of the vector intrinsic to widen.
1604 Intrinsic::ID VectorIntrinsicID;
1605
1606 /// Scalar return type of the intrinsic.
1607 Type *ResultTy;
1608
1609 /// True if the intrinsic may read from memory.
1610 bool MayReadFromMemory;
1611
1612 /// True if the intrinsic may read write to memory.
1613 bool MayWriteToMemory;
1614
1615 /// True if the intrinsic may have side-effects.
1616 bool MayHaveSideEffects;
1617
1618public:
1620 ArrayRef<VPValue *> CallArguments, Type *Ty,
1621 const VPIRFlags &Flags = {},
1622 const VPIRMetadata &MD = {},
1624 : VPRecipeWithIRFlags(VPDef::VPWidenIntrinsicSC, CallArguments, Flags,
1625 DL),
1626 VPIRMetadata(MD), VectorIntrinsicID(VectorIntrinsicID), ResultTy(Ty),
1627 MayReadFromMemory(CI.mayReadFromMemory()),
1628 MayWriteToMemory(CI.mayWriteToMemory()),
1629 MayHaveSideEffects(CI.mayHaveSideEffects()) {
1630 setUnderlyingValue(&CI);
1631 }
1632
1634 ArrayRef<VPValue *> CallArguments, Type *Ty,
1635 const VPIRFlags &Flags = {},
1636 const VPIRMetadata &Metadata = {},
1638 : VPRecipeWithIRFlags(VPDef::VPWidenIntrinsicSC, CallArguments, Flags,
1639 DL),
1640 VPIRMetadata(Metadata), VectorIntrinsicID(VectorIntrinsicID),
1641 ResultTy(Ty) {
1642 LLVMContext &Ctx = Ty->getContext();
1643 AttributeSet Attrs = Intrinsic::getFnAttributes(Ctx, VectorIntrinsicID);
1644 MemoryEffects ME = Attrs.getMemoryEffects();
1645 MayReadFromMemory = !ME.onlyWritesMemory();
1646 MayWriteToMemory = !ME.onlyReadsMemory();
1647 MayHaveSideEffects = MayWriteToMemory ||
1648 !Attrs.hasAttribute(Attribute::NoUnwind) ||
1649 !Attrs.hasAttribute(Attribute::WillReturn);
1650 }
1651
1652 ~VPWidenIntrinsicRecipe() override = default;
1653
1655 if (Value *CI = getUnderlyingValue())
1656 return new VPWidenIntrinsicRecipe(*cast<CallInst>(CI), VectorIntrinsicID,
1657 operands(), ResultTy, *this, *this,
1658 getDebugLoc());
1659 return new VPWidenIntrinsicRecipe(VectorIntrinsicID, operands(), ResultTy,
1660 *this, *this, getDebugLoc());
1661 }
1662
1663 VP_CLASSOF_IMPL(VPDef::VPWidenIntrinsicSC)
1664
1665 /// Produce a widened version of the vector intrinsic.
1666 LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override;
1667
1668 /// Return the cost of this vector intrinsic.
1670 computeCost(ElementCount VF, VPCostContext &Ctx) const override;
1671
1672 /// Return the ID of the intrinsic.
1673 Intrinsic::ID getVectorIntrinsicID() const { return VectorIntrinsicID; }
1674
1675 /// Return the scalar return type of the intrinsic.
1676 Type *getResultType() const { return ResultTy; }
1677
1678 /// Return to name of the intrinsic as string.
1680
1681 /// Returns true if the intrinsic may read from memory.
1682 bool mayReadFromMemory() const { return MayReadFromMemory; }
1683
1684 /// Returns true if the intrinsic may write to memory.
1685 bool mayWriteToMemory() const { return MayWriteToMemory; }
1686
1687 /// Returns true if the intrinsic may have side-effects.
1688 bool mayHaveSideEffects() const { return MayHaveSideEffects; }
1689
1690 LLVM_ABI_FOR_TEST bool usesFirstLaneOnly(const VPValue *Op) const override;
1691
1692protected:
1693#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1694 /// Print the recipe.
1695 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
1696 VPSlotTracker &SlotTracker) const override;
1697#endif
1698};
1699
1700/// A recipe for widening Call instructions using library calls.
1702 public VPIRMetadata {
1703 /// Variant stores a pointer to the chosen function. There is a 1:1 mapping
1704 /// between a given VF and the chosen vectorized variant, so there will be a
1705 /// different VPlan for each VF with a valid variant.
1706 Function *Variant;
1707
1708public:
1710 ArrayRef<VPValue *> CallArguments,
1711 const VPIRFlags &Flags = {},
1712 const VPIRMetadata &Metadata = {}, DebugLoc DL = {})
1713 : VPRecipeWithIRFlags(VPDef::VPWidenCallSC, CallArguments, Flags, DL),
1714 VPIRMetadata(Metadata), Variant(Variant) {
1715 setUnderlyingValue(UV);
1716 assert(
1717 isa<Function>(getOperand(getNumOperands() - 1)->getLiveInIRValue()) &&
1718 "last operand must be the called function");
1719 }
1720
1721 ~VPWidenCallRecipe() override = default;
1722
1724 return new VPWidenCallRecipe(getUnderlyingValue(), Variant, operands(),
1725 *this, *this, getDebugLoc());
1726 }
1727
1728 VP_CLASSOF_IMPL(VPDef::VPWidenCallSC)
1729
1730 /// Produce a widened version of the call instruction.
1731 void execute(VPTransformState &State) override;
1732
1733 /// Return the cost of this VPWidenCallRecipe.
1734 InstructionCost computeCost(ElementCount VF,
1735 VPCostContext &Ctx) const override;
1736
1740
1743
1744protected:
1745#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1746 /// Print the recipe.
1747 void printRecipe(raw_ostream &O, const Twine &Indent,
1748 VPSlotTracker &SlotTracker) const override;
1749#endif
1750};
1751
1752/// A recipe representing a sequence of load -> update -> store as part of
1753/// a histogram operation. This means there may be aliasing between vector
1754/// lanes, which is handled by the llvm.experimental.vector.histogram family
1755/// of intrinsics. The only update operations currently supported are
1756/// 'add' and 'sub' where the other term is loop-invariant.
1758 /// Opcode of the update operation, currently either add or sub.
1759 unsigned Opcode;
1760
1761public:
1762 VPHistogramRecipe(unsigned Opcode, ArrayRef<VPValue *> Operands,
1764 : VPRecipeBase(VPDef::VPHistogramSC, Operands, DL), Opcode(Opcode) {}
1765
1766 ~VPHistogramRecipe() override = default;
1767
1769 return new VPHistogramRecipe(Opcode, operands(), getDebugLoc());
1770 }
1771
1772 VP_CLASSOF_IMPL(VPDef::VPHistogramSC);
1773
1774 /// Produce a vectorized histogram operation.
1775 void execute(VPTransformState &State) override;
1776
1777 /// Return the cost of this VPHistogramRecipe.
1779 VPCostContext &Ctx) const override;
1780
1781 unsigned getOpcode() const { return Opcode; }
1782
1783 /// Return the mask operand if one was provided, or a null pointer if all
1784 /// lanes should be executed unconditionally.
1785 VPValue *getMask() const {
1786 return getNumOperands() == 3 ? getOperand(2) : nullptr;
1787 }
1788
1789protected:
1790#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1791 /// Print the recipe
1792 void printRecipe(raw_ostream &O, const Twine &Indent,
1793 VPSlotTracker &SlotTracker) const override;
1794#endif
1795};
1796
1797/// A recipe for widening select instructions. Supports both wide vector and
1798/// single-scalar conditions, matching the behavior of LLVM IR's select
1799/// instruction.
1801 public VPIRMetadata {
1803 const VPIRFlags &Flags = {}, const VPIRMetadata &MD = {},
1804 DebugLoc DL = {})
1805 : VPRecipeWithIRFlags(VPDef::VPWidenSelectSC, Operands, Flags, DL),
1806 VPIRMetadata(MD) {
1807 setUnderlyingValue(SI);
1808 }
1809
1810 ~VPWidenSelectRecipe() override = default;
1811
1814 operands(), *this, *this, getDebugLoc());
1815 }
1816
1817 VP_CLASSOF_IMPL(VPDef::VPWidenSelectSC)
1818
1819 /// Produce a widened version of the select instruction.
1820 void execute(VPTransformState &State) override;
1821
1822 /// Return the cost of this VPWidenSelectRecipe.
1823 InstructionCost computeCost(ElementCount VF,
1824 VPCostContext &Ctx) const override;
1825
1826 unsigned getOpcode() const { return Instruction::Select; }
1827
1828 VPValue *getCond() const {
1829 return getOperand(0);
1830 }
1831
1832 /// Returns true if the recipe only uses the first lane of operand \p Op.
1833 bool usesFirstLaneOnly(const VPValue *Op) const override {
1835 "Op must be an operand of the recipe");
1836 return Op == getCond() && Op->isDefinedOutsideLoopRegions();
1837 }
1838
1839protected:
1840#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1841 /// Print the recipe.
1842 void printRecipe(raw_ostream &O, const Twine &Indent,
1843 VPSlotTracker &SlotTracker) const override;
1844#endif
1845};
1846
1847/// A recipe for handling GEP instructions.
1849 Type *SourceElementTy;
1850
1851 bool isPointerLoopInvariant() const {
1852 return getOperand(0)->isDefinedOutsideLoopRegions();
1853 }
1854
1855 bool isIndexLoopInvariant(unsigned I) const {
1856 return getOperand(I + 1)->isDefinedOutsideLoopRegions();
1857 }
1858
1859public:
1861 const VPIRFlags &Flags = {},
1863 : VPRecipeWithIRFlags(VPDef::VPWidenGEPSC, Operands, Flags, DL),
1864 SourceElementTy(GEP->getSourceElementType()) {
1865 setUnderlyingValue(GEP);
1867 (void)Metadata;
1869 assert(Metadata.empty() && "unexpected metadata on GEP");
1870 }
1871
1872 ~VPWidenGEPRecipe() override = default;
1873
1876 operands(), *this, getDebugLoc());
1877 }
1878
1879 VP_CLASSOF_IMPL(VPDef::VPWidenGEPSC)
1880
1881 /// This recipe generates a GEP instruction.
1882 unsigned getOpcode() const { return Instruction::GetElementPtr; }
1883
1884 /// Generate the gep nodes.
1885 void execute(VPTransformState &State) override;
1886
1887 Type *getSourceElementType() const { return SourceElementTy; }
1888
1889 /// Return the cost of this VPWidenGEPRecipe.
1891 VPCostContext &Ctx) const override {
1892 // TODO: Compute accurate cost after retiring the legacy cost model.
1893 return 0;
1894 }
1895
1896 /// Returns true if the recipe only uses the first lane of operand \p Op.
1897 bool usesFirstLaneOnly(const VPValue *Op) const override;
1898
1899protected:
1900#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1901 /// Print the recipe.
1902 void printRecipe(raw_ostream &O, const Twine &Indent,
1903 VPSlotTracker &SlotTracker) const override;
1904#endif
1905};
1906
1907/// A recipe to compute a pointer to the last element of each part of a widened
1908/// memory access for widened memory accesses of IndexedTy. Used for
1909/// VPWidenMemoryRecipes or VPInterleaveRecipes that are reversed.
1911 public VPUnrollPartAccessor<2> {
1912 Type *IndexedTy;
1913
1914 /// The constant stride of the pointer computed by this recipe, expressed in
1915 /// units of IndexedTy.
1916 int64_t Stride;
1917
1918public:
1920 int64_t Stride, GEPNoWrapFlags GEPFlags, DebugLoc DL)
1921 : VPRecipeWithIRFlags(VPDef::VPVectorEndPointerSC,
1922 ArrayRef<VPValue *>({Ptr, VF}), GEPFlags, DL),
1923 IndexedTy(IndexedTy), Stride(Stride) {
1924 assert(Stride < 0 && "Stride must be negative");
1925 }
1926
1927 VP_CLASSOF_IMPL(VPDef::VPVectorEndPointerSC)
1928
1930 const VPValue *getVFValue() const { return getOperand(1); }
1931
1932 void execute(VPTransformState &State) override;
1933
1934 bool usesFirstLaneOnly(const VPValue *Op) const override {
1936 "Op must be an operand of the recipe");
1937 return true;
1938 }
1939
1940 /// Return the cost of this VPVectorPointerRecipe.
1942 VPCostContext &Ctx) const override {
1943 // TODO: Compute accurate cost after retiring the legacy cost model.
1944 return 0;
1945 }
1946
1947 /// Returns true if the recipe only uses the first part of operand \p Op.
1948 bool usesFirstPartOnly(const VPValue *Op) const override {
1950 "Op must be an operand of the recipe");
1951 assert(getNumOperands() <= 2 && "must have at most two operands");
1952 return true;
1953 }
1954
1956 return new VPVectorEndPointerRecipe(getOperand(0), getVFValue(), IndexedTy,
1957 Stride, getGEPNoWrapFlags(),
1958 getDebugLoc());
1959 }
1960
1961protected:
1962#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1963 /// Print the recipe.
1964 void printRecipe(raw_ostream &O, const Twine &Indent,
1965 VPSlotTracker &SlotTracker) const override;
1966#endif
1967};
1968
1969/// A recipe to compute the pointers for widened memory accesses of \p
1970/// SourceElementTy. Unrolling adds an extra offset operand for unrolled parts >
1971/// 0 and it produces `GEP Ptr, Offset`. The offset for unrolled part 0 is 0.
1973 Type *SourceElementTy;
1974
1975public:
1976 VPVectorPointerRecipe(VPValue *Ptr, Type *SourceElementTy,
1978 : VPRecipeWithIRFlags(VPDef::VPVectorPointerSC, Ptr, GEPFlags, DL),
1979 SourceElementTy(SourceElementTy) {}
1980
1981 VP_CLASSOF_IMPL(VPDef::VPVectorPointerSC)
1982
1984 return getNumOperands() == 2 ? getOperand(1) : nullptr;
1985 }
1986
1987 void execute(VPTransformState &State) override;
1988
1989 Type *getSourceElementType() const { return SourceElementTy; }
1990
1991 bool usesFirstLaneOnly(const VPValue *Op) const override {
1993 "Op must be an operand of the recipe");
1994 return true;
1995 }
1996
1997 /// Returns true if the recipe only uses the first part of operand \p Op.
1998 bool usesFirstPartOnly(const VPValue *Op) const override {
2000 "Op must be an operand of the recipe");
2001 assert(getNumOperands() <= 2 && "must have at most two operands");
2002 return true;
2003 }
2004
2006 auto *Clone = new VPVectorPointerRecipe(getOperand(0), SourceElementTy,
2008 if (auto *Off = getOffset())
2009 Clone->addOperand(Off);
2010 return Clone;
2011 }
2012
2013 /// Return the cost of this VPHeaderPHIRecipe.
2015 VPCostContext &Ctx) const override {
2016 // TODO: Compute accurate cost after retiring the legacy cost model.
2017 return 0;
2018 }
2019
2020protected:
2021#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2022 /// Print the recipe.
2023 void printRecipe(raw_ostream &O, const Twine &Indent,
2024 VPSlotTracker &SlotTracker) const override;
2025#endif
2026};
2027
2028/// A pure virtual base class for all recipes modeling header phis, including
2029/// phis for first order recurrences, pointer inductions and reductions. The
2030/// start value is the first operand of the recipe and the incoming value from
2031/// the backedge is the second operand.
2032///
2033/// Inductions are modeled using the following sub-classes:
2034/// * VPCanonicalIVPHIRecipe: Canonical scalar induction of the vector loop,
2035/// starting at a specified value (zero for the main vector loop, the resume
2036/// value for the epilogue vector loop) and stepping by 1. The induction
2037/// controls exiting of the vector loop by comparing against the vector trip
2038/// count. Produces a single scalar PHI for the induction value per
2039/// iteration.
2040/// * VPWidenIntOrFpInductionRecipe: Generates vector values for integer and
2041/// floating point inductions with arbitrary start and step values. Produces
2042/// a vector PHI per-part.
2043/// * VPDerivedIVRecipe: Converts the canonical IV value to the corresponding
2044/// value of an IV with different start and step values. Produces a single
2045/// scalar value per iteration
2046/// * VPScalarIVStepsRecipe: Generates scalar values per-lane based on a
2047/// canonical or derived induction.
2048/// * VPWidenPointerInductionRecipe: Generate vector and scalar values for a
2049/// pointer induction. Produces either a vector PHI per-part or scalar values
2050/// per-lane based on the canonical induction.
2052 public VPPhiAccessors {
2053protected:
2054 VPHeaderPHIRecipe(unsigned char VPDefID, Instruction *UnderlyingInstr,
2055 VPValue *Start, DebugLoc DL = DebugLoc::getUnknown())
2056 : VPSingleDefRecipe(VPDefID, ArrayRef<VPValue *>({Start}),
2057 UnderlyingInstr, DL) {}
2058
2059 const VPRecipeBase *getAsRecipe() const override { return this; }
2060
2061public:
2062 ~VPHeaderPHIRecipe() override = default;
2063
2064 /// Method to support type inquiry through isa, cast, and dyn_cast.
2065 static inline bool classof(const VPRecipeBase *R) {
2066 return R->getVPDefID() >= VPDef::VPFirstHeaderPHISC &&
2067 R->getVPDefID() <= VPDef::VPLastHeaderPHISC;
2068 }
2069 static inline bool classof(const VPValue *V) {
2070 return isa<VPHeaderPHIRecipe>(V->getDefiningRecipe());
2071 }
2072 static inline bool classof(const VPSingleDefRecipe *R) {
2073 return isa<VPHeaderPHIRecipe>(static_cast<const VPRecipeBase *>(R));
2074 }
2075
2076 /// Generate the phi nodes.
2077 void execute(VPTransformState &State) override = 0;
2078
2079 /// Return the cost of this header phi recipe.
2081 VPCostContext &Ctx) const override;
2082
2083 /// Returns the start value of the phi, if one is set.
2085 return getNumOperands() == 0 ? nullptr : getOperand(0);
2086 }
2088 return getNumOperands() == 0 ? nullptr : getOperand(0);
2089 }
2090
2091 /// Update the start value of the recipe.
2093
2094 /// Returns the incoming value from the loop backedge.
2096 return getOperand(1);
2097 }
2098
2099 /// Update the incoming value from the loop backedge.
2101
2102 /// Returns the backedge value as a recipe. The backedge value is guaranteed
2103 /// to be a recipe.
2105 return *getBackedgeValue()->getDefiningRecipe();
2106 }
2107
2108protected:
2109#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2110 /// Print the recipe.
2111 void printRecipe(raw_ostream &O, const Twine &Indent,
2112 VPSlotTracker &SlotTracker) const override = 0;
2113#endif
2114};
2115
2116/// Base class for widened induction (VPWidenIntOrFpInductionRecipe and
2117/// VPWidenPointerInductionRecipe), providing shared functionality, including
2118/// retrieving the step value, induction descriptor and original phi node.
2120 const InductionDescriptor &IndDesc;
2121
2122public:
2123 VPWidenInductionRecipe(unsigned char Kind, PHINode *IV, VPValue *Start,
2124 VPValue *Step, const InductionDescriptor &IndDesc,
2125 DebugLoc DL)
2126 : VPHeaderPHIRecipe(Kind, IV, Start, DL), IndDesc(IndDesc) {
2127 addOperand(Step);
2128 }
2129
2130 static inline bool classof(const VPRecipeBase *R) {
2131 return R->getVPDefID() == VPDef::VPWidenIntOrFpInductionSC ||
2132 R->getVPDefID() == VPDef::VPWidenPointerInductionSC;
2133 }
2134
2135 static inline bool classof(const VPValue *V) {
2136 auto *R = V->getDefiningRecipe();
2137 return R && classof(R);
2138 }
2139
2140 static inline bool classof(const VPSingleDefRecipe *R) {
2141 return classof(static_cast<const VPRecipeBase *>(R));
2142 }
2143
2144 void execute(VPTransformState &State) override = 0;
2145
2146 /// Returns the step value of the induction.
2148 const VPValue *getStepValue() const { return getOperand(1); }
2149
2150 /// Update the step value of the recipe.
2151 void setStepValue(VPValue *V) { setOperand(1, V); }
2152
2154 const VPValue *getVFValue() const { return getOperand(2); }
2155
2156 /// Returns the number of incoming values, also number of incoming blocks.
2157 /// Note that at the moment, VPWidenPointerInductionRecipe only has a single
2158 /// incoming value, its start value.
2159 unsigned getNumIncoming() const override { return 1; }
2160
2162
2163 /// Returns the induction descriptor for the recipe.
2164 const InductionDescriptor &getInductionDescriptor() const { return IndDesc; }
2165
2167 // TODO: All operands of base recipe must exist and be at same index in
2168 // derived recipe.
2170 "VPWidenIntOrFpInductionRecipe generates its own backedge value");
2171 }
2172
2174 // TODO: All operands of base recipe must exist and be at same index in
2175 // derived recipe.
2177 "VPWidenIntOrFpInductionRecipe generates its own backedge value");
2178 }
2179
2180 /// Returns true if the recipe only uses the first lane of operand \p Op.
2181 bool usesFirstLaneOnly(const VPValue *Op) const override {
2183 "Op must be an operand of the recipe");
2184 // The recipe creates its own wide start value, so it only requests the
2185 // first lane of the operand.
2186 // TODO: Remove once creating the start value is modeled separately.
2187 return Op == getStartValue() || Op == getStepValue();
2188 }
2189};
2190
2191/// A recipe for handling phi nodes of integer and floating-point inductions,
2192/// producing their vector values. This is an abstract recipe and must be
2193/// converted to concrete recipes before executing.
2195 public VPIRFlags {
2196 TruncInst *Trunc;
2197
2198 // If this recipe is unrolled it will have 2 additional operands.
2199 bool isUnrolled() const { return getNumOperands() == 5; }
2200
2201public:
2203 VPValue *VF, const InductionDescriptor &IndDesc,
2204 const VPIRFlags &Flags, DebugLoc DL)
2205 : VPWidenInductionRecipe(VPDef::VPWidenIntOrFpInductionSC, IV, Start,
2206 Step, IndDesc, DL),
2207 VPIRFlags(Flags), Trunc(nullptr) {
2208 addOperand(VF);
2209 }
2210
2212 VPValue *VF, const InductionDescriptor &IndDesc,
2213 TruncInst *Trunc, const VPIRFlags &Flags,
2214 DebugLoc DL)
2215 : VPWidenInductionRecipe(VPDef::VPWidenIntOrFpInductionSC, IV, Start,
2216 Step, IndDesc, DL),
2217 VPIRFlags(Flags), Trunc(Trunc) {
2218 addOperand(VF);
2220 (void)Metadata;
2221 if (Trunc)
2223 assert(Metadata.empty() && "unexpected metadata on Trunc");
2224 }
2225
2227
2233
2234 VP_CLASSOF_IMPL(VPDef::VPWidenIntOrFpInductionSC)
2235
2236 void execute(VPTransformState &State) override {
2237 llvm_unreachable("cannot execute this recipe, should be expanded via "
2238 "expandVPWidenIntOrFpInductionRecipe");
2239 }
2240
2242 // If the recipe has been unrolled return the VPValue for the induction
2243 // increment.
2244 return isUnrolled() ? getOperand(getNumOperands() - 2) : nullptr;
2245 }
2246
2247 /// Returns the number of incoming values, also number of incoming blocks.
2248 /// Note that at the moment, VPWidenIntOrFpInductionRecipes only have a single
2249 /// incoming value, its start value.
2250 unsigned getNumIncoming() const override { return 1; }
2251
2252 /// Returns the first defined value as TruncInst, if it is one or nullptr
2253 /// otherwise.
2254 TruncInst *getTruncInst() { return Trunc; }
2255 const TruncInst *getTruncInst() const { return Trunc; }
2256
2257 /// Returns true if the induction is canonical, i.e. starting at 0 and
2258 /// incremented by UF * VF (= the original IV is incremented by 1) and has the
2259 /// same type as the canonical induction.
2260 bool isCanonical() const;
2261
2262 /// Returns the scalar type of the induction.
2264 return Trunc ? Trunc->getType()
2266 }
2267
2268 /// Returns the VPValue representing the value of this induction at
2269 /// the last unrolled part, if it exists. Returns itself if unrolling did not
2270 /// take place.
2272 return isUnrolled() ? getOperand(getNumOperands() - 1) : this;
2273 }
2274
2275protected:
2276#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2277 /// Print the recipe.
2278 void printRecipe(raw_ostream &O, const Twine &Indent,
2279 VPSlotTracker &SlotTracker) const override;
2280#endif
2281};
2282
2284public:
2285 /// Create a new VPWidenPointerInductionRecipe for \p Phi with start value \p
2286 /// Start and the number of elements unrolled \p NumUnrolledElems, typically
2287 /// VF*UF.
2289 VPValue *NumUnrolledElems,
2290 const InductionDescriptor &IndDesc, DebugLoc DL)
2291 : VPWidenInductionRecipe(VPDef::VPWidenPointerInductionSC, Phi, Start,
2292 Step, IndDesc, DL) {
2293 addOperand(NumUnrolledElems);
2294 }
2295
2297
2303
2304 VP_CLASSOF_IMPL(VPDef::VPWidenPointerInductionSC)
2305
2306 /// Generate vector values for the pointer induction.
2307 void execute(VPTransformState &State) override {
2308 llvm_unreachable("cannot execute this recipe, should be expanded via "
2309 "expandVPWidenPointerInduction");
2310 };
2311
2312 /// Returns true if only scalar values will be generated.
2313 bool onlyScalarsGenerated(bool IsScalable);
2314
2315protected:
2316#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2317 /// Print the recipe.
2318 void printRecipe(raw_ostream &O, const Twine &Indent,
2319 VPSlotTracker &SlotTracker) const override;
2320#endif
2321};
2322
2323/// A recipe for widened phis. Incoming values are operands of the recipe and
2324/// their operand index corresponds to the incoming predecessor block. If the
2325/// recipe is placed in an entry block to a (non-replicate) region, it must have
2326/// exactly 2 incoming values, the first from the predecessor of the region and
2327/// the second from the exiting block of the region.
2329 public VPPhiAccessors {
2330 /// Name to use for the generated IR instruction for the widened phi.
2331 std::string Name;
2332
2333public:
2334 /// Create a new VPWidenPHIRecipe for \p Phi with start value \p Start and
2335 /// debug location \p DL.
2336 VPWidenPHIRecipe(PHINode *Phi, VPValue *Start = nullptr,
2337 DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "")
2338 : VPSingleDefRecipe(VPDef::VPWidenPHISC, {}, Phi, DL), Name(Name.str()) {
2339 if (Start)
2340 addOperand(Start);
2341 }
2342
2345 getOperand(0), getDebugLoc(), Name);
2347 C->addOperand(Op);
2348 return C;
2349 }
2350
2351 ~VPWidenPHIRecipe() override = default;
2352
2353 VP_CLASSOF_IMPL(VPDef::VPWidenPHISC)
2354
2355 /// Generate the phi/select nodes.
2356 void execute(VPTransformState &State) override;
2357
2358protected:
2359#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2360 /// Print the recipe.
2361 void printRecipe(raw_ostream &O, const Twine &Indent,
2362 VPSlotTracker &SlotTracker) const override;
2363#endif
2364
2365 const VPRecipeBase *getAsRecipe() const override { return this; }
2366};
2367
2368/// A recipe for handling first-order recurrence phis. The start value is the
2369/// first operand of the recipe and the incoming value from the backedge is the
2370/// second operand.
2373 VPValue &BackedgeValue)
2374 : VPHeaderPHIRecipe(VPDef::VPFirstOrderRecurrencePHISC, Phi, &Start) {
2375 addOperand(&BackedgeValue);
2376 }
2377
2378 VP_CLASSOF_IMPL(VPDef::VPFirstOrderRecurrencePHISC)
2379
2384
2385 void execute(VPTransformState &State) override;
2386
2387 /// Return the cost of this first-order recurrence phi recipe.
2389 VPCostContext &Ctx) const override;
2390
2391 /// Returns true if the recipe only uses the first lane of operand \p Op.
2392 bool usesFirstLaneOnly(const VPValue *Op) const override {
2394 "Op must be an operand of the recipe");
2395 return Op == getStartValue();
2396 }
2397
2398protected:
2399#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2400 /// Print the recipe.
2401 void printRecipe(raw_ostream &O, const Twine &Indent,
2402 VPSlotTracker &SlotTracker) const override;
2403#endif
2404};
2405
2406/// Possible variants of a reduction.
2407
2408/// This reduction is ordered and in-loop.
2409struct RdxOrdered {};
2410/// This reduction is in-loop.
2411struct RdxInLoop {};
2412/// This reduction is unordered with the partial result scaled down by some
2413/// factor.
2416};
2417using ReductionStyle = std::variant<RdxOrdered, RdxInLoop, RdxUnordered>;
2418
2419inline ReductionStyle getReductionStyle(bool InLoop, bool Ordered,
2420 unsigned ScaleFactor) {
2421 assert((!Ordered || InLoop) && "Ordered implies in-loop");
2422 if (Ordered)
2423 return RdxOrdered{};
2424 if (InLoop)
2425 return RdxInLoop{};
2426 return RdxUnordered{/*VFScaleFactor=*/ScaleFactor};
2427}
2428
2429/// A recipe for handling reduction phis. The start value is the first operand
2430/// of the recipe and the incoming value from the backedge is the second
2431/// operand.
2433 public VPUnrollPartAccessor<2> {
2434 /// The recurrence kind of the reduction.
2435 const RecurKind Kind;
2436
2437 ReductionStyle Style;
2438
2439 /// The phi is part of a multi-use reduction (e.g., used in FindLastIV
2440 /// patterns for argmin/argmax).
2441 /// TODO: Also support cases where the phi itself has a single use, but its
2442 /// compare has multiple uses.
2443 bool HasUsesOutsideReductionChain;
2444
2445public:
2446 /// Create a new VPReductionPHIRecipe for the reduction \p Phi.
2448 VPValue &BackedgeValue, ReductionStyle Style,
2449 bool HasUsesOutsideReductionChain = false)
2450 : VPHeaderPHIRecipe(VPDef::VPReductionPHISC, Phi, &Start), Kind(Kind),
2451 Style(Style),
2452 HasUsesOutsideReductionChain(HasUsesOutsideReductionChain) {
2453 addOperand(&BackedgeValue);
2454 }
2455
2456 ~VPReductionPHIRecipe() override = default;
2457
2459 return new VPReductionPHIRecipe(
2461 *getOperand(0), *getBackedgeValue(), Style,
2462 HasUsesOutsideReductionChain);
2463 }
2464
2465 VP_CLASSOF_IMPL(VPDef::VPReductionPHISC)
2466
2467 /// Generate the phi/select nodes.
2468 void execute(VPTransformState &State) override;
2469
2470 /// Get the factor that the VF of this recipe's output should be scaled by, or
2471 /// 1 if it isn't scaled.
2472 unsigned getVFScaleFactor() const {
2473 auto *Partial = std::get_if<RdxUnordered>(&Style);
2474 return Partial ? Partial->VFScaleFactor : 1;
2475 }
2476
2477 /// Returns the number of incoming values, also number of incoming blocks.
2478 /// Note that at the moment, VPWidenPointerInductionRecipe only has a single
2479 /// incoming value, its start value.
2480 unsigned getNumIncoming() const override { return 2; }
2481
2482 /// Returns the recurrence kind of the reduction.
2483 RecurKind getRecurrenceKind() const { return Kind; }
2484
2485 /// Returns true, if the phi is part of an ordered reduction.
2486 bool isOrdered() const { return std::holds_alternative<RdxOrdered>(Style); }
2487
2488 /// Returns true if the phi is part of an in-loop reduction.
2489 bool isInLoop() const {
2490 return std::holds_alternative<RdxInLoop>(Style) ||
2491 std::holds_alternative<RdxOrdered>(Style);
2492 }
2493
2494 /// Returns true if the reduction outputs a vector with a scaled down VF.
2495 bool isPartialReduction() const { return getVFScaleFactor() > 1; }
2496
2497 /// Returns true, if the phi is part of a multi-use reduction.
2499 return HasUsesOutsideReductionChain;
2500 }
2501
2502 /// Returns true if the recipe only uses the first lane of operand \p Op.
2503 bool usesFirstLaneOnly(const VPValue *Op) const override {
2505 "Op must be an operand of the recipe");
2506 return isOrdered() || isInLoop();
2507 }
2508
2509protected:
2510#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2511 /// Print the recipe.
2512 void printRecipe(raw_ostream &O, const Twine &Indent,
2513 VPSlotTracker &SlotTracker) const override;
2514#endif
2515};
2516
2517/// A recipe for vectorizing a phi-node as a sequence of mask-based select
2518/// instructions.
2520public:
2521 /// The blend operation is a User of the incoming values and of their
2522 /// respective masks, ordered [I0, M0, I1, M1, I2, M2, ...]. Note that M0 can
2523 /// be omitted (implied by passing an odd number of operands) in which case
2524 /// all other incoming values are merged into it.
2526 : VPSingleDefRecipe(VPDef::VPBlendSC, Operands, Phi, DL) {
2527 assert(Operands.size() >= 2 && "Expected at least two operands!");
2528 }
2529
2534
2535 VP_CLASSOF_IMPL(VPDef::VPBlendSC)
2536
2537 /// A normalized blend is one that has an odd number of operands, whereby the
2538 /// first operand does not have an associated mask.
2539 bool isNormalized() const { return getNumOperands() % 2; }
2540
2541 /// Return the number of incoming values, taking into account when normalized
2542 /// the first incoming value will have no mask.
2543 unsigned getNumIncomingValues() const {
2544 return (getNumOperands() + isNormalized()) / 2;
2545 }
2546
2547 /// Return incoming value number \p Idx.
2548 VPValue *getIncomingValue(unsigned Idx) const {
2549 return Idx == 0 ? getOperand(0) : getOperand(Idx * 2 - isNormalized());
2550 }
2551
2552 /// Return mask number \p Idx.
2553 VPValue *getMask(unsigned Idx) const {
2554 assert((Idx > 0 || !isNormalized()) && "First index has no mask!");
2555 return Idx == 0 ? getOperand(1) : getOperand(Idx * 2 + !isNormalized());
2556 }
2557
2558 /// Set mask number \p Idx to \p V.
2559 void setMask(unsigned Idx, VPValue *V) {
2560 assert((Idx > 0 || !isNormalized()) && "First index has no mask!");
2561 Idx == 0 ? setOperand(1, V) : setOperand(Idx * 2 + !isNormalized(), V);
2562 }
2563
2564 void execute(VPTransformState &State) override {
2565 llvm_unreachable("VPBlendRecipe should be expanded by simplifyBlends");
2566 }
2567
2568 /// Return the cost of this VPWidenMemoryRecipe.
2569 InstructionCost computeCost(ElementCount VF,
2570 VPCostContext &Ctx) const override;
2571
2572 /// Returns true if the recipe only uses the first lane of operand \p Op.
2573 bool usesFirstLaneOnly(const VPValue *Op) const override {
2575 "Op must be an operand of the recipe");
2576 // Recursing through Blend recipes only, must terminate at header phi's the
2577 // latest.
2578 return all_of(users(),
2579 [this](VPUser *U) { return U->usesFirstLaneOnly(this); });
2580 }
2581
2582protected:
2583#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2584 /// Print the recipe.
2585 void printRecipe(raw_ostream &O, const Twine &Indent,
2586 VPSlotTracker &SlotTracker) const override;
2587#endif
2588};
2589
2590/// A common base class for interleaved memory operations.
2591/// An Interleaved memory operation is a memory access method that combines
2592/// multiple strided loads/stores into a single wide load/store with shuffles.
2593/// The first operand is the start address. The optional operands are, in order,
2594/// the stored values and the mask.
2596 public VPIRMetadata {
2598
2599 /// Indicates if the interleave group is in a conditional block and requires a
2600 /// mask.
2601 bool HasMask = false;
2602
2603 /// Indicates if gaps between members of the group need to be masked out or if
2604 /// unusued gaps can be loaded speculatively.
2605 bool NeedsMaskForGaps = false;
2606
2607protected:
2608 VPInterleaveBase(const unsigned char SC,
2610 ArrayRef<VPValue *> Operands,
2611 ArrayRef<VPValue *> StoredValues, VPValue *Mask,
2612 bool NeedsMaskForGaps, const VPIRMetadata &MD, DebugLoc DL)
2613 : VPRecipeBase(SC, Operands, DL), VPIRMetadata(MD), IG(IG),
2614 NeedsMaskForGaps(NeedsMaskForGaps) {
2615 // TODO: extend the masked interleaved-group support to reversed access.
2616 assert((!Mask || !IG->isReverse()) &&
2617 "Reversed masked interleave-group not supported.");
2618 for (unsigned I = 0; I < IG->getFactor(); ++I)
2619 if (Instruction *Inst = IG->getMember(I)) {
2620 if (Inst->getType()->isVoidTy())
2621 continue;
2622 new VPValue(Inst, this);
2623 }
2624
2625 for (auto *SV : StoredValues)
2626 addOperand(SV);
2627 if (Mask) {
2628 HasMask = true;
2629 addOperand(Mask);
2630 }
2631 }
2632
2633public:
2634 VPInterleaveBase *clone() override = 0;
2635
2636 static inline bool classof(const VPRecipeBase *R) {
2637 return R->getVPDefID() == VPRecipeBase::VPInterleaveSC ||
2638 R->getVPDefID() == VPRecipeBase::VPInterleaveEVLSC;
2639 }
2640
2641 static inline bool classof(const VPUser *U) {
2642 auto *R = dyn_cast<VPRecipeBase>(U);
2643 return R && classof(R);
2644 }
2645
2646 /// Return the address accessed by this recipe.
2647 VPValue *getAddr() const {
2648 return getOperand(0); // Address is the 1st, mandatory operand.
2649 }
2650
2651 /// Return the mask used by this recipe. Note that a full mask is represented
2652 /// by a nullptr.
2653 VPValue *getMask() const {
2654 // Mask is optional and the last operand.
2655 return HasMask ? getOperand(getNumOperands() - 1) : nullptr;
2656 }
2657
2658 /// Return true if the access needs a mask because of the gaps.
2659 bool needsMaskForGaps() const { return NeedsMaskForGaps; }
2660
2662
2663 Instruction *getInsertPos() const { return IG->getInsertPos(); }
2664
2665 void execute(VPTransformState &State) override {
2666 llvm_unreachable("VPInterleaveBase should not be instantiated.");
2667 }
2668
2669 /// Return the cost of this recipe.
2670 InstructionCost computeCost(ElementCount VF,
2671 VPCostContext &Ctx) const override;
2672
2673 /// Returns true if the recipe only uses the first lane of operand \p Op.
2674 bool usesFirstLaneOnly(const VPValue *Op) const override = 0;
2675
2676 /// Returns the number of stored operands of this interleave group. Returns 0
2677 /// for load interleave groups.
2678 virtual unsigned getNumStoreOperands() const = 0;
2679
2680 /// Return the VPValues stored by this interleave group. If it is a load
2681 /// interleave group, return an empty ArrayRef.
2683 return ArrayRef<VPValue *>(op_end() -
2684 (getNumStoreOperands() + (HasMask ? 1 : 0)),
2686 }
2687};
2688
2689/// VPInterleaveRecipe is a recipe for transforming an interleave group of load
2690/// or stores into one wide load/store and shuffles. The first operand of a
2691/// VPInterleave recipe is the address, followed by the stored values, followed
2692/// by an optional mask.
2694public:
2696 ArrayRef<VPValue *> StoredValues, VPValue *Mask,
2697 bool NeedsMaskForGaps, const VPIRMetadata &MD, DebugLoc DL)
2698 : VPInterleaveBase(VPDef::VPInterleaveSC, IG, Addr, StoredValues, Mask,
2699 NeedsMaskForGaps, MD, DL) {}
2700
2701 ~VPInterleaveRecipe() override = default;
2702
2706 needsMaskForGaps(), *this, getDebugLoc());
2707 }
2708
2709 VP_CLASSOF_IMPL(VPDef::VPInterleaveSC)
2710
2711 /// Generate the wide load or store, and shuffles.
2712 void execute(VPTransformState &State) override;
2713
2714 bool usesFirstLaneOnly(const VPValue *Op) const override {
2716 "Op must be an operand of the recipe");
2717 return Op == getAddr() && !llvm::is_contained(getStoredValues(), Op);
2718 }
2719
2720 unsigned getNumStoreOperands() const override {
2721 return getNumOperands() - (getMask() ? 2 : 1);
2722 }
2723
2724protected:
2725#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2726 /// Print the recipe.
2727 void printRecipe(raw_ostream &O, const Twine &Indent,
2728 VPSlotTracker &SlotTracker) const override;
2729#endif
2730};
2731
2732/// A recipe for interleaved memory operations with vector-predication
2733/// intrinsics. The first operand is the address, the second operand is the
2734/// explicit vector length. Stored values and mask are optional operands.
2736public:
2738 : VPInterleaveBase(VPDef::VPInterleaveEVLSC, R.getInterleaveGroup(),
2739 ArrayRef<VPValue *>({R.getAddr(), &EVL}),
2740 R.getStoredValues(), Mask, R.needsMaskForGaps(), R,
2741 R.getDebugLoc()) {
2742 assert(!getInterleaveGroup()->isReverse() &&
2743 "Reversed interleave-group with tail folding is not supported.");
2744 assert(!needsMaskForGaps() && "Interleaved access with gap mask is not "
2745 "supported for scalable vector.");
2746 }
2747
2748 ~VPInterleaveEVLRecipe() override = default;
2749
2751 llvm_unreachable("cloning not implemented yet");
2752 }
2753
2754 VP_CLASSOF_IMPL(VPDef::VPInterleaveEVLSC)
2755
2756 /// The VPValue of the explicit vector length.
2757 VPValue *getEVL() const { return getOperand(1); }
2758
2759 /// Generate the wide load or store, and shuffles.
2760 void execute(VPTransformState &State) override;
2761
2762 /// The recipe only uses the first lane of the address, and EVL operand.
2763 bool usesFirstLaneOnly(const VPValue *Op) const override {
2765 "Op must be an operand of the recipe");
2766 return (Op == getAddr() && !llvm::is_contained(getStoredValues(), Op)) ||
2767 Op == getEVL();
2768 }
2769
2770 unsigned getNumStoreOperands() const override {
2771 return getNumOperands() - (getMask() ? 3 : 2);
2772 }
2773
2774protected:
2775#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2776 /// Print the recipe.
2777 void printRecipe(raw_ostream &O, const Twine &Indent,
2778 VPSlotTracker &SlotTracker) const override;
2779#endif
2780};
2781
2782/// A recipe to represent inloop, ordered or partial reduction operations. It
2783/// performs a reduction on a vector operand into a scalar (vector in the case
2784/// of a partial reduction) value, and adds the result to a chain. The Operands
2785/// are {ChainOp, VecOp, [Condition]}.
2787
2788 /// The recurrence kind for the reduction in question.
2789 RecurKind RdxKind;
2790 /// Whether the reduction is conditional.
2791 bool IsConditional = false;
2792 ReductionStyle Style;
2793
2794protected:
2795 VPReductionRecipe(const unsigned char SC, RecurKind RdxKind,
2797 ArrayRef<VPValue *> Operands, VPValue *CondOp,
2798 ReductionStyle Style, DebugLoc DL)
2799 : VPRecipeWithIRFlags(SC, Operands, FMFs, DL), RdxKind(RdxKind),
2800 Style(Style) {
2801 if (CondOp) {
2802 IsConditional = true;
2803 addOperand(CondOp);
2804 }
2806 }
2807
2808public:
2810 VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp,
2812 : VPReductionRecipe(VPDef::VPReductionSC, RdxKind, FMFs, I,
2813 ArrayRef<VPValue *>({ChainOp, VecOp}), CondOp, Style,
2814 DL) {}
2815
2817 VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp,
2819 : VPReductionRecipe(VPDef::VPReductionSC, RdxKind, FMFs, nullptr,
2820 ArrayRef<VPValue *>({ChainOp, VecOp}), CondOp, Style,
2821 DL) {}
2822
2823 ~VPReductionRecipe() override = default;
2824
2826 return new VPReductionRecipe(RdxKind, getFastMathFlags(),
2828 getCondOp(), Style, getDebugLoc());
2829 }
2830
2831 static inline bool classof(const VPRecipeBase *R) {
2832 return R->getVPDefID() == VPRecipeBase::VPReductionSC ||
2833 R->getVPDefID() == VPRecipeBase::VPReductionEVLSC;
2834 }
2835
2836 static inline bool classof(const VPUser *U) {
2837 auto *R = dyn_cast<VPRecipeBase>(U);
2838 return R && classof(R);
2839 }
2840
2841 static inline bool classof(const VPValue *VPV) {
2842 const VPRecipeBase *R = VPV->getDefiningRecipe();
2843 return R && classof(R);
2844 }
2845
2846 static inline bool classof(const VPSingleDefRecipe *R) {
2847 return classof(static_cast<const VPRecipeBase *>(R));
2848 }
2849
2850 /// Generate the reduction in the loop.
2851 void execute(VPTransformState &State) override;
2852
2853 /// Return the cost of VPReductionRecipe.
2854 InstructionCost computeCost(ElementCount VF,
2855 VPCostContext &Ctx) const override;
2856
2857 /// Return the recurrence kind for the in-loop reduction.
2858 RecurKind getRecurrenceKind() const { return RdxKind; }
2859 /// Return true if the in-loop reduction is ordered.
2860 bool isOrdered() const { return std::holds_alternative<RdxOrdered>(Style); };
2861 /// Return true if the in-loop reduction is conditional.
2862 bool isConditional() const { return IsConditional; };
2863 /// Returns true if the reduction outputs a vector with a scaled down VF.
2864 bool isPartialReduction() const { return getVFScaleFactor() > 1; }
2865 /// Returns true if the reduction is in-loop.
2866 bool isInLoop() const {
2867 return std::holds_alternative<RdxInLoop>(Style) ||
2868 std::holds_alternative<RdxOrdered>(Style);
2869 }
2870 /// The VPValue of the scalar Chain being accumulated.
2871 VPValue *getChainOp() const { return getOperand(0); }
2872 /// The VPValue of the vector value to be reduced.
2873 VPValue *getVecOp() const { return getOperand(1); }
2874 /// The VPValue of the condition for the block.
2876 return isConditional() ? getOperand(getNumOperands() - 1) : nullptr;
2877 }
2878 /// Get the factor that the VF of this recipe's output should be scaled by, or
2879 /// 1 if it isn't scaled.
2880 unsigned getVFScaleFactor() const {
2881 auto *Partial = std::get_if<RdxUnordered>(&Style);
2882 return Partial ? Partial->VFScaleFactor : 1;
2883 }
2884
2885protected:
2886#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2887 /// Print the recipe.
2888 void printRecipe(raw_ostream &O, const Twine &Indent,
2889 VPSlotTracker &SlotTracker) const override;
2890#endif
2891};
2892
2893/// A recipe to represent inloop reduction operations with vector-predication
2894/// intrinsics, performing a reduction on a vector operand with the explicit
2895/// vector length (EVL) into a scalar value, and adding the result to a chain.
2896/// The Operands are {ChainOp, VecOp, EVL, [Condition]}.
2898public:
2902 VPDef::VPReductionEVLSC, R.getRecurrenceKind(),
2903 R.getFastMathFlags(),
2905 ArrayRef<VPValue *>({R.getChainOp(), R.getVecOp(), &EVL}), CondOp,
2906 getReductionStyle(/*InLoop=*/true, R.isOrdered(), 1), DL) {}
2907
2908 ~VPReductionEVLRecipe() override = default;
2909
2911 llvm_unreachable("cloning not implemented yet");
2912 }
2913
2914 VP_CLASSOF_IMPL(VPDef::VPReductionEVLSC)
2915
2916 /// Generate the reduction in the loop
2917 void execute(VPTransformState &State) override;
2918
2919 /// The VPValue of the explicit vector length.
2920 VPValue *getEVL() const { return getOperand(2); }
2921
2922 /// Returns true if the recipe only uses the first lane of operand \p Op.
2923 bool usesFirstLaneOnly(const VPValue *Op) const override {
2925 "Op must be an operand of the recipe");
2926 return Op == getEVL();
2927 }
2928
2929protected:
2930#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2931 /// Print the recipe.
2932 void printRecipe(raw_ostream &O, const Twine &Indent,
2933 VPSlotTracker &SlotTracker) const override;
2934#endif
2935};
2936
2937/// VPReplicateRecipe replicates a given instruction producing multiple scalar
2938/// copies of the original scalar type, one per lane, instead of producing a
2939/// single copy of widened type for all lanes. If the instruction is known to be
2940/// a single scalar, only one copy, per lane zero, will be generated.
2942 public VPIRMetadata {
2943 /// Indicator if only a single replica per lane is needed.
2944 bool IsSingleScalar;
2945
2946 /// Indicator if the replicas are also predicated.
2947 bool IsPredicated;
2948
2949public:
2951 bool IsSingleScalar, VPValue *Mask = nullptr,
2952 const VPIRFlags &Flags = {}, VPIRMetadata Metadata = {},
2953 DebugLoc DL = DebugLoc::getUnknown())
2954 : VPRecipeWithIRFlags(VPDef::VPReplicateSC, Operands, Flags, DL),
2955 VPIRMetadata(Metadata), IsSingleScalar(IsSingleScalar),
2956 IsPredicated(Mask) {
2957 setUnderlyingValue(I);
2958 if (Mask)
2959 addOperand(Mask);
2960 }
2961
2962 ~VPReplicateRecipe() override = default;
2963
2965 auto *Copy = new VPReplicateRecipe(
2966 getUnderlyingInstr(), operands(), IsSingleScalar,
2967 isPredicated() ? getMask() : nullptr, *this, *this, getDebugLoc());
2968 Copy->transferFlags(*this);
2969 return Copy;
2970 }
2971
2972 VP_CLASSOF_IMPL(VPDef::VPReplicateSC)
2973
2974 /// Generate replicas of the desired Ingredient. Replicas will be generated
2975 /// for all parts and lanes unless a specific part and lane are specified in
2976 /// the \p State.
2977 void execute(VPTransformState &State) override;
2978
2979 /// Return the cost of this VPReplicateRecipe.
2980 InstructionCost computeCost(ElementCount VF,
2981 VPCostContext &Ctx) const override;
2982
2983 bool isSingleScalar() const { return IsSingleScalar; }
2984
2985 bool isPredicated() const { return IsPredicated; }
2986
2987 /// Returns true if the recipe only uses the first lane of operand \p Op.
2988 bool usesFirstLaneOnly(const VPValue *Op) const override {
2990 "Op must be an operand of the recipe");
2991 return isSingleScalar();
2992 }
2993
2994 /// Returns true if the recipe uses scalars of operand \p Op.
2995 bool usesScalars(const VPValue *Op) const override {
2997 "Op must be an operand of the recipe");
2998 return true;
2999 }
3000
3001 /// Returns true if the recipe is used by a widened recipe via an intervening
3002 /// VPPredInstPHIRecipe. In this case, the scalar values should also be packed
3003 /// in a vector.
3004 bool shouldPack() const;
3005
3006 /// Return the mask of a predicated VPReplicateRecipe.
3008 assert(isPredicated() && "Trying to get the mask of a unpredicated recipe");
3009 return getOperand(getNumOperands() - 1);
3010 }
3011
3012 unsigned getOpcode() const { return getUnderlyingInstr()->getOpcode(); }
3013
3014protected:
3015#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3016 /// Print the recipe.
3017 void printRecipe(raw_ostream &O, const Twine &Indent,
3018 VPSlotTracker &SlotTracker) const override;
3019#endif
3020};
3021
3022/// A recipe for generating conditional branches on the bits of a mask.
3024public:
3026 : VPRecipeBase(VPDef::VPBranchOnMaskSC, {BlockInMask}, DL) {}
3027
3030 }
3031
3032 VP_CLASSOF_IMPL(VPDef::VPBranchOnMaskSC)
3033
3034 /// Generate the extraction of the appropriate bit from the block mask and the
3035 /// conditional branch.
3036 void execute(VPTransformState &State) override;
3037
3038 /// Return the cost of this VPBranchOnMaskRecipe.
3039 InstructionCost computeCost(ElementCount VF,
3040 VPCostContext &Ctx) const override;
3041
3042#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3043 /// Print the recipe.
3044 void printRecipe(raw_ostream &O, const Twine &Indent,
3045 VPSlotTracker &SlotTracker) const override {
3046 O << Indent << "BRANCH-ON-MASK ";
3048 }
3049#endif
3050
3051 /// Returns true if the recipe uses scalars of operand \p Op.
3052 bool usesScalars(const VPValue *Op) const override {
3054 "Op must be an operand of the recipe");
3055 return true;
3056 }
3057};
3058
3059/// A recipe to combine multiple recipes into a single 'expression' recipe,
3060/// which should be considered a single entity for cost-modeling and transforms.
3061/// The recipe needs to be 'decomposed', i.e. replaced by its individual
3062/// expression recipes, before execute. The individual expression recipes are
3063/// completely disconnected from the def-use graph of other recipes not part of
3064/// the expression. Def-use edges between pairs of expression recipes remain
3065/// intact, whereas every edge between an expression recipe and a recipe outside
3066/// the expression is elevated to connect the non-expression recipe with the
3067/// VPExpressionRecipe itself.
3068class VPExpressionRecipe : public VPSingleDefRecipe {
3069 /// Recipes included in this VPExpressionRecipe. This could contain
3070 /// duplicates.
3071 SmallVector<VPSingleDefRecipe *> ExpressionRecipes;
3072
3073 /// Temporary VPValues used for external operands of the expression, i.e.
3074 /// operands not defined by recipes in the expression.
3075 SmallVector<VPValue *> LiveInPlaceholders;
3076
3077 enum class ExpressionTypes {
3078 /// Represents an inloop extended reduction operation, performing a
3079 /// reduction on an extended vector operand into a scalar value, and adding
3080 /// the result to a chain.
3081 ExtendedReduction,
3082 /// Represent an inloop multiply-accumulate reduction, multiplying the
3083 /// extended vector operands, performing a reduction.add on the result, and
3084 /// adding the scalar result to a chain.
3085 ExtMulAccReduction,
3086 /// Represent an inloop multiply-accumulate reduction, multiplying the
3087 /// vector operands, performing a reduction.add on the result, and adding
3088 /// the scalar result to a chain.
3089 MulAccReduction,
3090 /// Represent an inloop multiply-accumulate reduction, multiplying the
3091 /// extended vector operands, negating the multiplication, performing a
3092 /// reduction.add on the result, and adding the scalar result to a chain.
3093 ExtNegatedMulAccReduction,
3094 };
3095
3096 /// Type of the expression.
3097 ExpressionTypes ExpressionType;
3098
3099 /// Construct a new VPExpressionRecipe by internalizing recipes in \p
3100 /// ExpressionRecipes. External operands (i.e. not defined by another recipe
3101 /// in the expression) are replaced by temporary VPValues and the original
3102 /// operands are transferred to the VPExpressionRecipe itself. Clone recipes
3103 /// as needed (excluding last) to ensure they are only used by other recipes
3104 /// in the expression.
3105 VPExpressionRecipe(ExpressionTypes ExpressionType,
3106 ArrayRef<VPSingleDefRecipe *> ExpressionRecipes);
3107
3108public:
3110 : VPExpressionRecipe(ExpressionTypes::ExtendedReduction, {Ext, Red}) {}
3112 : VPExpressionRecipe(ExpressionTypes::MulAccReduction, {Mul, Red}) {}
3115 : VPExpressionRecipe(ExpressionTypes::ExtMulAccReduction,
3116 {Ext0, Ext1, Mul, Red}) {}
3119 VPReductionRecipe *Red)
3120 : VPExpressionRecipe(ExpressionTypes::ExtNegatedMulAccReduction,
3121 {Ext0, Ext1, Mul, Sub, Red}) {
3122 assert(Mul->getOpcode() == Instruction::Mul && "Expected a mul");
3123 assert(Red->getRecurrenceKind() == RecurKind::Add &&
3124 "Expected an add reduction");
3125 assert(getNumOperands() >= 3 && "Expected at least three operands");
3126 [[maybe_unused]] auto *SubConst = dyn_cast<ConstantInt>(getOperand(2)->getLiveInIRValue());
3127 assert(SubConst && SubConst->getValue() == 0 &&
3128 Sub->getOpcode() == Instruction::Sub && "Expected a negating sub");
3129 }
3130
3132 SmallPtrSet<VPSingleDefRecipe *, 4> ExpressionRecipesSeen;
3133 for (auto *R : reverse(ExpressionRecipes)) {
3134 if (ExpressionRecipesSeen.insert(R).second)
3135 delete R;
3136 }
3137 for (VPValue *T : LiveInPlaceholders)
3138 delete T;
3139 }
3140
3141 VP_CLASSOF_IMPL(VPDef::VPExpressionSC)
3142
3143 VPExpressionRecipe *clone() override {
3144 assert(!ExpressionRecipes.empty() && "empty expressions should be removed");
3145 SmallVector<VPSingleDefRecipe *> NewExpressiondRecipes;
3146 for (auto *R : ExpressionRecipes)
3147 NewExpressiondRecipes.push_back(R->clone());
3148 for (auto *New : NewExpressiondRecipes) {
3149 for (const auto &[Idx, Old] : enumerate(ExpressionRecipes))
3150 New->replaceUsesOfWith(Old, NewExpressiondRecipes[Idx]);
3151 // Update placeholder operands in the cloned recipe to use the external
3152 // operands, to be internalized when the cloned expression is constructed.
3153 for (const auto &[Placeholder, OutsideOp] :
3154 zip(LiveInPlaceholders, operands()))
3155 New->replaceUsesOfWith(Placeholder, OutsideOp);
3156 }
3157 return new VPExpressionRecipe(ExpressionType, NewExpressiondRecipes);
3158 }
3159
3160 /// Return the VPValue to use to infer the result type of the recipe.
3162 unsigned OpIdx =
3163 cast<VPReductionRecipe>(ExpressionRecipes.back())->isConditional() ? 2
3164 : 1;
3165 return getOperand(getNumOperands() - OpIdx);
3166 }
3167
3168 /// Insert the recipes of the expression back into the VPlan, directly before
3169 /// the current recipe. Leaves the expression recipe empty, which must be
3170 /// removed before codegen.
3171 void decompose();
3172
3173 unsigned getVFScaleFactor() const {
3174 auto *PR = dyn_cast<VPReductionRecipe>(ExpressionRecipes.back());
3175 return PR ? PR->getVFScaleFactor() : 1;
3176 }
3177
3178 /// Method for generating code, must not be called as this recipe is abstract.
3179 void execute(VPTransformState &State) override {
3180 llvm_unreachable("recipe must be removed before execute");
3181 }
3182
3184 VPCostContext &Ctx) const override;
3185
3186 /// Returns true if this expression contains recipes that may read from or
3187 /// write to memory.
3188 bool mayReadOrWriteMemory() const;
3189
3190 /// Returns true if this expression contains recipes that may have side
3191 /// effects.
3192 bool mayHaveSideEffects() const;
3193
3194 /// Returns true if the result of this VPExpressionRecipe is a single-scalar.
3195 bool isSingleScalar() const;
3196
3197protected:
3198#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3199 /// Print the recipe.
3200 void printRecipe(raw_ostream &O, const Twine &Indent,
3201 VPSlotTracker &SlotTracker) const override;
3202#endif
3203};
3204
3205/// VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when
3206/// control converges back from a Branch-on-Mask. The phi nodes are needed in
3207/// order to merge values that are set under such a branch and feed their uses.
3208/// The phi nodes can be scalar or vector depending on the users of the value.
3209/// This recipe works in concert with VPBranchOnMaskRecipe.
3211public:
3212 /// Construct a VPPredInstPHIRecipe given \p PredInst whose value needs a phi
3213 /// nodes after merging back from a Branch-on-Mask.
3215 : VPSingleDefRecipe(VPDef::VPPredInstPHISC, PredV, DL) {}
3216 ~VPPredInstPHIRecipe() override = default;
3217
3219 return new VPPredInstPHIRecipe(getOperand(0), getDebugLoc());
3220 }
3221
3222 VP_CLASSOF_IMPL(VPDef::VPPredInstPHISC)
3223
3224 /// Generates phi nodes for live-outs (from a replicate region) as needed to
3225 /// retain SSA form.
3226 void execute(VPTransformState &State) override;
3227
3228 /// Return the cost of this VPPredInstPHIRecipe.
3230 VPCostContext &Ctx) const override {
3231 // TODO: Compute accurate cost after retiring the legacy cost model.
3232 return 0;
3233 }
3234
3235 /// Returns true if the recipe uses scalars of operand \p Op.
3236 bool usesScalars(const VPValue *Op) const override {
3238 "Op must be an operand of the recipe");
3239 return true;
3240 }
3241
3242protected:
3243#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3244 /// Print the recipe.
3245 void printRecipe(raw_ostream &O, const Twine &Indent,
3246 VPSlotTracker &SlotTracker) const override;
3247#endif
3248};
3249
3250/// A common base class for widening memory operations. An optional mask can be
3251/// provided as the last operand.
3253 public VPIRMetadata {
3254protected:
3256
3257 /// Alignment information for this memory access.
3259
3260 /// Whether the accessed addresses are consecutive.
3262
3263 /// Whether the consecutive accessed addresses are in reverse order.
3265
3266 /// Whether the memory access is masked.
3267 bool IsMasked = false;
3268
3269 void setMask(VPValue *Mask) {
3270 assert(!IsMasked && "cannot re-set mask");
3271 if (!Mask)
3272 return;
3273 addOperand(Mask);
3274 IsMasked = true;
3275 }
3276
3277 VPWidenMemoryRecipe(const char unsigned SC, Instruction &I,
3278 std::initializer_list<VPValue *> Operands,
3279 bool Consecutive, bool Reverse,
3280 const VPIRMetadata &Metadata, DebugLoc DL)
3281 : VPRecipeBase(SC, Operands, DL), VPIRMetadata(Metadata), Ingredient(I),
3283 Reverse(Reverse) {
3284 assert((Consecutive || !Reverse) && "Reverse implies consecutive");
3286 "Reversed acccess without VPVectorEndPointerRecipe address?");
3287 }
3288
3289public:
3291 llvm_unreachable("cloning not supported");
3292 }
3293
3294 static inline bool classof(const VPRecipeBase *R) {
3295 return R->getVPDefID() == VPRecipeBase::VPWidenLoadSC ||
3296 R->getVPDefID() == VPRecipeBase::VPWidenStoreSC ||
3297 R->getVPDefID() == VPRecipeBase::VPWidenLoadEVLSC ||
3298 R->getVPDefID() == VPRecipeBase::VPWidenStoreEVLSC;
3299 }
3300
3301 static inline bool classof(const VPUser *U) {
3302 auto *R = dyn_cast<VPRecipeBase>(U);
3303 return R && classof(R);
3304 }
3305
3306 /// Return whether the loaded-from / stored-to addresses are consecutive.
3307 bool isConsecutive() const { return Consecutive; }
3308
3309 /// Return whether the consecutive loaded/stored addresses are in reverse
3310 /// order.
3311 bool isReverse() const { return Reverse; }
3312
3313 /// Return the address accessed by this recipe.
3314 VPValue *getAddr() const { return getOperand(0); }
3315
3316 /// Returns true if the recipe is masked.
3317 bool isMasked() const { return IsMasked; }
3318
3319 /// Return the mask used by this recipe. Note that a full mask is represented
3320 /// by a nullptr.
3321 VPValue *getMask() const {
3322 // Mask is optional and therefore the last operand.
3323 return isMasked() ? getOperand(getNumOperands() - 1) : nullptr;
3324 }
3325
3326 /// Returns the alignment of the memory access.
3327 Align getAlign() const { return Alignment; }
3328
3329 /// Generate the wide load/store.
3330 void execute(VPTransformState &State) override {
3331 llvm_unreachable("VPWidenMemoryRecipe should not be instantiated.");
3332 }
3333
3334 /// Return the cost of this VPWidenMemoryRecipe.
3335 InstructionCost computeCost(ElementCount VF,
3336 VPCostContext &Ctx) const override;
3337
3339};
3340
3341/// A recipe for widening load operations, using the address to load from and an
3342/// optional mask.
3344 public VPValue {
3346 bool Consecutive, bool Reverse,
3347 const VPIRMetadata &Metadata, DebugLoc DL)
3348 : VPWidenMemoryRecipe(VPDef::VPWidenLoadSC, Load, {Addr}, Consecutive,
3349 Reverse, Metadata, DL),
3350 VPValue(this, &Load) {
3351 setMask(Mask);
3352 }
3353
3356 getMask(), Consecutive, Reverse, *this,
3357 getDebugLoc());
3358 }
3359
3360 VP_CLASSOF_IMPL(VPDef::VPWidenLoadSC);
3361
3362 /// Generate a wide load or gather.
3363 void execute(VPTransformState &State) override;
3364
3365 /// Returns true if the recipe only uses the first lane of operand \p Op.
3366 bool usesFirstLaneOnly(const VPValue *Op) const override {
3368 "Op must be an operand of the recipe");
3369 // Widened, consecutive loads operations only demand the first lane of
3370 // their address.
3371 return Op == getAddr() && isConsecutive();
3372 }
3373
3374protected:
3375#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3376 /// Print the recipe.
3377 void printRecipe(raw_ostream &O, const Twine &Indent,
3378 VPSlotTracker &SlotTracker) const override;
3379#endif
3380};
3381
3382/// A recipe for widening load operations with vector-predication intrinsics,
3383/// using the address to load from, the explicit vector length and an optional
3384/// mask.
3385struct VPWidenLoadEVLRecipe final : public VPWidenMemoryRecipe, public VPValue {
3387 VPValue *Mask)
3388 : VPWidenMemoryRecipe(VPDef::VPWidenLoadEVLSC, L.getIngredient(),
3389 {Addr, &EVL}, L.isConsecutive(), L.isReverse(), L,
3390 L.getDebugLoc()),
3391 VPValue(this, &getIngredient()) {
3392 setMask(Mask);
3393 }
3394
3395 VP_CLASSOF_IMPL(VPDef::VPWidenLoadEVLSC)
3396
3397 /// Return the EVL operand.
3398 VPValue *getEVL() const { return getOperand(1); }
3399
3400 /// Generate the wide load or gather.
3401 LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override;
3402
3403 /// Return the cost of this VPWidenLoadEVLRecipe.
3405 computeCost(ElementCount VF, VPCostContext &Ctx) const override;
3406
3407 /// Returns true if the recipe only uses the first lane of operand \p Op.
3408 bool usesFirstLaneOnly(const VPValue *Op) const override {
3410 "Op must be an operand of the recipe");
3411 // Widened loads only demand the first lane of EVL and consecutive loads
3412 // only demand the first lane of their address.
3413 return Op == getEVL() || (Op == getAddr() && isConsecutive());
3414 }
3415
3416protected:
3417#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3418 /// Print the recipe.
3419 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
3420 VPSlotTracker &SlotTracker) const override;
3421#endif
3422};
3423
3424/// A recipe for widening store operations, using the stored value, the address
3425/// to store to and an optional mask.
3427 VPWidenStoreRecipe(StoreInst &Store, VPValue *Addr, VPValue *StoredVal,
3428 VPValue *Mask, bool Consecutive, bool Reverse,
3429 const VPIRMetadata &Metadata, DebugLoc DL)
3430 : VPWidenMemoryRecipe(VPDef::VPWidenStoreSC, Store, {Addr, StoredVal},
3431 Consecutive, Reverse, Metadata, DL) {
3432 setMask(Mask);
3433 }
3434
3440
3441 VP_CLASSOF_IMPL(VPDef::VPWidenStoreSC);
3442
3443 /// Return the value stored by this recipe.
3444 VPValue *getStoredValue() const { return getOperand(1); }
3445
3446 /// Generate a wide store or scatter.
3447 void execute(VPTransformState &State) override;
3448
3449 /// Returns true if the recipe only uses the first lane of operand \p Op.
3450 bool usesFirstLaneOnly(const VPValue *Op) const override {
3452 "Op must be an operand of the recipe");
3453 // Widened, consecutive stores only demand the first lane of their address,
3454 // unless the same operand is also stored.
3455 return Op == getAddr() && isConsecutive() && Op != getStoredValue();
3456 }
3457
3458protected:
3459#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3460 /// Print the recipe.
3461 void printRecipe(raw_ostream &O, const Twine &Indent,
3462 VPSlotTracker &SlotTracker) const override;
3463#endif
3464};
3465
3466/// A recipe for widening store operations with vector-predication intrinsics,
3467/// using the value to store, the address to store to, the explicit vector
3468/// length and an optional mask.
3471 VPValue *Mask)
3472 : VPWidenMemoryRecipe(VPDef::VPWidenStoreEVLSC, S.getIngredient(),
3473 {Addr, S.getStoredValue(), &EVL}, S.isConsecutive(),
3474 S.isReverse(), S, S.getDebugLoc()) {
3475 setMask(Mask);
3476 }
3477
3478 VP_CLASSOF_IMPL(VPDef::VPWidenStoreEVLSC)
3479
3480 /// Return the address accessed by this recipe.
3481 VPValue *getStoredValue() const { return getOperand(1); }
3482
3483 /// Return the EVL operand.
3484 VPValue *getEVL() const { return getOperand(2); }
3485
3486 /// Generate the wide store or scatter.
3487 LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override;
3488
3489 /// Return the cost of this VPWidenStoreEVLRecipe.
3491 computeCost(ElementCount VF, VPCostContext &Ctx) const override;
3492
3493 /// Returns true if the recipe only uses the first lane of operand \p Op.
3494 bool usesFirstLaneOnly(const VPValue *Op) const override {
3496 "Op must be an operand of the recipe");
3497 if (Op == getEVL()) {
3498 assert(getStoredValue() != Op && "unexpected store of EVL");
3499 return true;
3500 }
3501 // Widened, consecutive memory operations only demand the first lane of
3502 // their address, unless the same operand is also stored. That latter can
3503 // happen with opaque pointers.
3504 return Op == getAddr() && isConsecutive() && Op != getStoredValue();
3505 }
3506
3507protected:
3508#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3509 /// Print the recipe.
3510 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
3511 VPSlotTracker &SlotTracker) const override;
3512#endif
3513};
3514
3515/// Recipe to expand a SCEV expression.
3517 const SCEV *Expr;
3518
3519public:
3521 : VPSingleDefRecipe(VPDef::VPExpandSCEVSC, {}), Expr(Expr) {}
3522
3523 ~VPExpandSCEVRecipe() override = default;
3524
3525 VPExpandSCEVRecipe *clone() override { return new VPExpandSCEVRecipe(Expr); }
3526
3527 VP_CLASSOF_IMPL(VPDef::VPExpandSCEVSC)
3528
3529 void execute(VPTransformState &State) override {
3530 llvm_unreachable("SCEV expressions must be expanded before final execute");
3531 }
3532
3533 /// Return the cost of this VPExpandSCEVRecipe.
3535 VPCostContext &Ctx) const override {
3536 // TODO: Compute accurate cost after retiring the legacy cost model.
3537 return 0;
3538 }
3539
3540 const SCEV *getSCEV() const { return Expr; }
3541
3542protected:
3543#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3544 /// Print the recipe.
3545 void printRecipe(raw_ostream &O, const Twine &Indent,
3546 VPSlotTracker &SlotTracker) const override;
3547#endif
3548};
3549
3550/// Canonical scalar induction phi of the vector loop. Starting at the specified
3551/// start value (either 0 or the resume value when vectorizing the epilogue
3552/// loop). VPWidenCanonicalIVRecipe represents the vector version of the
3553/// canonical induction variable.
3555public:
3557 : VPHeaderPHIRecipe(VPDef::VPCanonicalIVPHISC, nullptr, StartV, DL) {}
3558
3559 ~VPCanonicalIVPHIRecipe() override = default;
3560
3562 auto *R = new VPCanonicalIVPHIRecipe(getOperand(0), getDebugLoc());
3563 R->addOperand(getBackedgeValue());
3564 return R;
3565 }
3566
3567 VP_CLASSOF_IMPL(VPDef::VPCanonicalIVPHISC)
3568
3569 void execute(VPTransformState &State) override {
3570 llvm_unreachable("cannot execute this recipe, should be replaced by a "
3571 "scalar phi recipe");
3572 }
3573
3574 /// Returns the scalar type of the induction.
3576 return getStartValue()->getLiveInIRValue()->getType();
3577 }
3578
3579 /// Returns true if the recipe only uses the first lane of operand \p Op.
3580 bool usesFirstLaneOnly(const VPValue *Op) const override {
3582 "Op must be an operand of the recipe");
3583 return true;
3584 }
3585
3586 /// Returns true if the recipe only uses the first part of operand \p Op.
3587 bool usesFirstPartOnly(const VPValue *Op) const override {
3589 "Op must be an operand of the recipe");
3590 return true;
3591 }
3592
3593 /// Return the cost of this VPCanonicalIVPHIRecipe.
3595 VPCostContext &Ctx) const override {
3596 // For now, match the behavior of the legacy cost model.
3597 return 0;
3598 }
3599
3600protected:
3601#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3602 /// Print the recipe.
3603 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
3604 VPSlotTracker &SlotTracker) const override;
3605#endif
3606};
3607
3608/// A recipe for generating the active lane mask for the vector loop that is
3609/// used to predicate the vector operations.
3611public:
3613 : VPHeaderPHIRecipe(VPDef::VPActiveLaneMaskPHISC, nullptr, StartMask,
3614 DL) {}
3615
3616 ~VPActiveLaneMaskPHIRecipe() override = default;
3617
3620 if (getNumOperands() == 2)
3621 R->addOperand(getOperand(1));
3622 return R;
3623 }
3624
3625 VP_CLASSOF_IMPL(VPDef::VPActiveLaneMaskPHISC)
3626
3627 /// Generate the active lane mask phi of the vector loop.
3628 void execute(VPTransformState &State) override;
3629
3630protected:
3631#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3632 /// Print the recipe.
3633 void printRecipe(raw_ostream &O, const Twine &Indent,
3634 VPSlotTracker &SlotTracker) const override;
3635#endif
3636};
3637
3638/// A recipe for generating the phi node for the current index of elements,
3639/// adjusted in accordance with EVL value. It starts at the start value of the
3640/// canonical induction and gets incremented by EVL in each iteration of the
3641/// vector loop.
3643public:
3645 : VPHeaderPHIRecipe(VPDef::VPEVLBasedIVPHISC, nullptr, StartIV, DL) {}
3646
3647 ~VPEVLBasedIVPHIRecipe() override = default;
3648
3650 llvm_unreachable("cloning not implemented yet");
3651 }
3652
3653 VP_CLASSOF_IMPL(VPDef::VPEVLBasedIVPHISC)
3654
3655 void execute(VPTransformState &State) override {
3656 llvm_unreachable("cannot execute this recipe, should be replaced by a "
3657 "scalar phi recipe");
3658 }
3659
3660 /// Return the cost of this VPEVLBasedIVPHIRecipe.
3662 VPCostContext &Ctx) const override {
3663 // For now, match the behavior of the legacy cost model.
3664 return 0;
3665 }
3666
3667 /// Returns true if the recipe only uses the first lane of operand \p Op.
3668 bool usesFirstLaneOnly(const VPValue *Op) const override {
3670 "Op must be an operand of the recipe");
3671 return true;
3672 }
3673
3674protected:
3675#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3676 /// Print the recipe.
3677 LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent,
3678 VPSlotTracker &SlotTracker) const override;
3679#endif
3680};
3681
3682/// A Recipe for widening the canonical induction variable of the vector loop.
3684 public VPUnrollPartAccessor<1> {
3685public:
3687 : VPSingleDefRecipe(VPDef::VPWidenCanonicalIVSC, {CanonicalIV}) {}
3688
3689 ~VPWidenCanonicalIVRecipe() override = default;
3690
3695
3696 VP_CLASSOF_IMPL(VPDef::VPWidenCanonicalIVSC)
3697
3698 /// Generate a canonical vector induction variable of the vector loop, with
3699 /// start = {<Part*VF, Part*VF+1, ..., Part*VF+VF-1> for 0 <= Part < UF}, and
3700 /// step = <VF*UF, VF*UF, ..., VF*UF>.
3701 void execute(VPTransformState &State) override;
3702
3703 /// Return the cost of this VPWidenCanonicalIVPHIRecipe.
3705 VPCostContext &Ctx) const override {
3706 // TODO: Compute accurate cost after retiring the legacy cost model.
3707 return 0;
3708 }
3709
3710protected:
3711#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3712 /// Print the recipe.
3713 void printRecipe(raw_ostream &O, const Twine &Indent,
3714 VPSlotTracker &SlotTracker) const override;
3715#endif
3716};
3717
3718/// A recipe for converting the input value \p IV value to the corresponding
3719/// value of an IV with different start and step values, using Start + IV *
3720/// Step.
3722 /// Kind of the induction.
3724 /// If not nullptr, the floating point induction binary operator. Must be set
3725 /// for floating point inductions.
3726 const FPMathOperator *FPBinOp;
3727
3728 /// Name to use for the generated IR instruction for the derived IV.
3729 std::string Name;
3730
3731public:
3733 VPCanonicalIVPHIRecipe *CanonicalIV, VPValue *Step,
3734 const Twine &Name = "")
3736 IndDesc.getKind(),
3737 dyn_cast_or_null<FPMathOperator>(IndDesc.getInductionBinOp()),
3738 Start, CanonicalIV, Step, Name) {}
3739
3741 const FPMathOperator *FPBinOp, VPValue *Start, VPValue *IV,
3742 VPValue *Step, const Twine &Name = "")
3743 : VPSingleDefRecipe(VPDef::VPDerivedIVSC, {Start, IV, Step}), Kind(Kind),
3744 FPBinOp(FPBinOp), Name(Name.str()) {}
3745
3746 ~VPDerivedIVRecipe() override = default;
3747
3749 return new VPDerivedIVRecipe(Kind, FPBinOp, getStartValue(), getOperand(1),
3750 getStepValue());
3751 }
3752
3753 VP_CLASSOF_IMPL(VPDef::VPDerivedIVSC)
3754
3755 /// Generate the transformed value of the induction at offset StartValue (1.
3756 /// operand) + IV (2. operand) * StepValue (3, operand).
3757 void execute(VPTransformState &State) override;
3758
3759 /// Return the cost of this VPDerivedIVRecipe.
3761 VPCostContext &Ctx) const override {
3762 // TODO: Compute accurate cost after retiring the legacy cost model.
3763 return 0;
3764 }
3765
3767 return getStartValue()->getLiveInIRValue()->getType();
3768 }
3769
3770 VPValue *getStartValue() const { return getOperand(0); }
3771 VPValue *getStepValue() const { return getOperand(2); }
3772
3773 /// Returns true if the recipe only uses the first lane of operand \p Op.
3774 bool usesFirstLaneOnly(const VPValue *Op) const override {
3776 "Op must be an operand of the recipe");
3777 return true;
3778 }
3779
3780protected:
3781#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3782 /// Print the recipe.
3783 void printRecipe(raw_ostream &O, const Twine &Indent,
3784 VPSlotTracker &SlotTracker) const override;
3785#endif
3786};
3787
3788/// A recipe for handling phi nodes of integer and floating-point inductions,
3789/// producing their scalar values.
3791 public VPUnrollPartAccessor<3> {
3792 Instruction::BinaryOps InductionOpcode;
3793
3794public:
3797 DebugLoc DL)
3798 : VPRecipeWithIRFlags(VPDef::VPScalarIVStepsSC,
3799 ArrayRef<VPValue *>({IV, Step, VF}), FMFs, DL),
3800 InductionOpcode(Opcode) {}
3801
3803 VPValue *Step, VPValue *VF,
3806 IV, Step, VF, IndDesc.getInductionOpcode(),
3807 dyn_cast_or_null<FPMathOperator>(IndDesc.getInductionBinOp())
3808 ? IndDesc.getInductionBinOp()->getFastMathFlags()
3809 : FastMathFlags(),
3810 DL) {}
3811
3812 ~VPScalarIVStepsRecipe() override = default;
3813
3815 return new VPScalarIVStepsRecipe(
3816 getOperand(0), getOperand(1), getOperand(2), InductionOpcode,
3818 getDebugLoc());
3819 }
3820
3821 /// Return true if this VPScalarIVStepsRecipe corresponds to part 0. Note that
3822 /// this is only accurate after the VPlan has been unrolled.
3823 bool isPart0() const { return getUnrollPart(*this) == 0; }
3824
3825 VP_CLASSOF_IMPL(VPDef::VPScalarIVStepsSC)
3826
3827 /// Generate the scalarized versions of the phi node as needed by their users.
3828 void execute(VPTransformState &State) override;
3829
3830 /// Return the cost of this VPScalarIVStepsRecipe.
3832 VPCostContext &Ctx) const override {
3833 // TODO: Compute accurate cost after retiring the legacy cost model.
3834 return 0;
3835 }
3836
3837 VPValue *getStepValue() const { return getOperand(1); }
3838
3839 /// Returns true if the recipe only uses the first lane of operand \p Op.
3840 bool usesFirstLaneOnly(const VPValue *Op) const override {
3842 "Op must be an operand of the recipe");
3843 return true;
3844 }
3845
3846protected:
3847#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3848 /// Print the recipe.
3849 void printRecipe(raw_ostream &O, const Twine &Indent,
3850 VPSlotTracker &SlotTracker) const override;
3851#endif
3852};
3853
3854/// Casting from VPRecipeBase -> VPPhiAccessors is supported for all recipe
3855/// types implementing VPPhiAccessors. Used by isa<> & co.
3857 static inline bool isPossible(const VPRecipeBase *f) {
3858 // TODO: include VPPredInstPHIRecipe too, once it implements VPPhiAccessors.
3860 }
3861};
3862/// Support casting from VPRecipeBase -> VPPhiAccessors, by down-casting to the
3863/// recipe types implementing VPPhiAccessors. Used by cast<>, dyn_cast<> & co.
3864template <typename SrcTy>
3865struct CastInfoVPPhiAccessors : public CastIsPossible<VPPhiAccessors, SrcTy> {
3866
3868
3869 /// doCast is used by cast<>.
3870 static inline VPPhiAccessors *doCast(SrcTy R) {
3871 return const_cast<VPPhiAccessors *>([R]() -> const VPPhiAccessors * {
3872 switch (R->getVPDefID()) {
3873 case VPDef::VPInstructionSC:
3874 return cast<VPPhi>(R);
3875 case VPDef::VPIRInstructionSC:
3876 return cast<VPIRPhi>(R);
3877 case VPDef::VPWidenPHISC:
3878 return cast<VPWidenPHIRecipe>(R);
3879 default:
3880 return cast<VPHeaderPHIRecipe>(R);
3881 }
3882 }());
3883 }
3884
3885 /// doCastIfPossible is used by dyn_cast<>.
3886 static inline VPPhiAccessors *doCastIfPossible(SrcTy f) {
3887 if (!Self::isPossible(f))
3888 return nullptr;
3889 return doCast(f);
3890 }
3891};
3892template <>
3895template <>
3898
3899/// Casting from (const) VPRecipeBase -> (const) VPIRMetadata is supported for
3900/// all recipe types implementing VPIRMetadata. Used by isa<> & co.
3901namespace detail {
3902template <typename DstTy, typename RecipeBasePtrTy>
3903static inline auto castToVPIRMetadata(RecipeBasePtrTy R) -> DstTy {
3904 switch (R->getVPDefID()) {
3905 case VPDef::VPInstructionSC:
3906 return cast<VPInstruction>(R);
3907 case VPDef::VPWidenSC:
3908 return cast<VPWidenRecipe>(R);
3909 case VPDef::VPWidenCastSC:
3910 return cast<VPWidenCastRecipe>(R);
3911 case VPDef::VPWidenIntrinsicSC:
3913 case VPDef::VPWidenCallSC:
3914 return cast<VPWidenCallRecipe>(R);
3915 case VPDef::VPWidenSelectSC:
3916 return cast<VPWidenSelectRecipe>(R);
3917 case VPDef::VPReplicateSC:
3918 return cast<VPReplicateRecipe>(R);
3919 case VPDef::VPInterleaveSC:
3920 case VPDef::VPInterleaveEVLSC:
3921 return cast<VPInterleaveBase>(R);
3922 case VPDef::VPWidenLoadSC:
3923 case VPDef::VPWidenLoadEVLSC:
3924 case VPDef::VPWidenStoreSC:
3925 case VPDef::VPWidenStoreEVLSC:
3926 return cast<VPWidenMemoryRecipe>(R);
3927 default:
3928 llvm_unreachable("invalid recipe for VPIRMetadata cast");
3929 }
3930}
3931} // namespace detail
3932
3933/// Support casting from VPRecipeBase -> VPIRMetadata, by down-casting to the
3934/// recipe types implementing VPIRMetadata. Used by cast<>, dyn_cast<> & co.
3935template <typename DstTy, typename SrcTy>
3936struct CastInfoVPIRMetadata : public CastIsPossible<DstTy, SrcTy> {
3937 static inline bool isPossible(SrcTy R) {
3938 // NOTE: Each recipe inheriting from VPIRMetadata must be listed here and
3939 // also handled in castToVPIRMetadata.
3945 }
3946
3947 using RetTy = DstTy *;
3948
3949 /// doCast is used by cast<>.
3950 static inline RetTy doCast(SrcTy R) {
3952 }
3953
3954 /// doCastIfPossible is used by dyn_cast<>.
3955 static inline RetTy doCastIfPossible(SrcTy R) {
3956 if (!isPossible(R))
3957 return nullptr;
3958 return doCast(R);
3959 }
3960};
3961template <>
3964template <>
3967
3968/// VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph. It
3969/// holds a sequence of zero or more VPRecipe's each representing a sequence of
3970/// output IR instructions. All PHI-like recipes must come before any non-PHI recipes.
3971class LLVM_ABI_FOR_TEST VPBasicBlock : public VPBlockBase {
3972 friend class VPlan;
3973
3974 /// Use VPlan::createVPBasicBlock to create VPBasicBlocks.
3975 VPBasicBlock(const Twine &Name = "", VPRecipeBase *Recipe = nullptr)
3976 : VPBlockBase(VPBasicBlockSC, Name.str()) {
3977 if (Recipe)
3978 appendRecipe(Recipe);
3979 }
3980
3981public:
3983
3984protected:
3985 /// The VPRecipes held in the order of output instructions to generate.
3987
3988 VPBasicBlock(const unsigned char BlockSC, const Twine &Name = "")
3989 : VPBlockBase(BlockSC, Name.str()) {}
3990
3991public:
3992 ~VPBasicBlock() override {
3993 while (!Recipes.empty())
3994 Recipes.pop_back();
3995 }
3996
3997 /// Instruction iterators...
4002
4003 //===--------------------------------------------------------------------===//
4004 /// Recipe iterator methods
4005 ///
4006 inline iterator begin() { return Recipes.begin(); }
4007 inline const_iterator begin() const { return Recipes.begin(); }
4008 inline iterator end() { return Recipes.end(); }
4009 inline const_iterator end() const { return Recipes.end(); }
4010
4011 inline reverse_iterator rbegin() { return Recipes.rbegin(); }
4012 inline const_reverse_iterator rbegin() const { return Recipes.rbegin(); }
4013 inline reverse_iterator rend() { return Recipes.rend(); }
4014 inline const_reverse_iterator rend() const { return Recipes.rend(); }
4015
4016 inline size_t size() const { return Recipes.size(); }
4017 inline bool empty() const { return Recipes.empty(); }
4018 inline const VPRecipeBase &front() const { return Recipes.front(); }
4019 inline VPRecipeBase &front() { return Recipes.front(); }
4020 inline const VPRecipeBase &back() const { return Recipes.back(); }
4021 inline VPRecipeBase &back() { return Recipes.back(); }
4022
4023 /// Returns a reference to the list of recipes.
4025
4026 /// Returns a pointer to a member of the recipe list.
4027 static RecipeListTy VPBasicBlock::*getSublistAccess(VPRecipeBase *) {
4028 return &VPBasicBlock::Recipes;
4029 }
4030
4031 /// Method to support type inquiry through isa, cast, and dyn_cast.
4032 static inline bool classof(const VPBlockBase *V) {
4033 return V->getVPBlockID() == VPBlockBase::VPBasicBlockSC ||
4034 V->getVPBlockID() == VPBlockBase::VPIRBasicBlockSC;
4035 }
4036
4037 void insert(VPRecipeBase *Recipe, iterator InsertPt) {
4038 assert(Recipe && "No recipe to append.");
4039 assert(!Recipe->Parent && "Recipe already in VPlan");
4040 Recipe->Parent = this;
4041 Recipes.insert(InsertPt, Recipe);
4042 }
4043
4044 /// Augment the existing recipes of a VPBasicBlock with an additional
4045 /// \p Recipe as the last recipe.
4046 void appendRecipe(VPRecipeBase *Recipe) { insert(Recipe, end()); }
4047
4048 /// The method which generates the output IR instructions that correspond to
4049 /// this VPBasicBlock, thereby "executing" the VPlan.
4050 void execute(VPTransformState *State) override;
4051
4052 /// Return the cost of this VPBasicBlock.
4053 InstructionCost cost(ElementCount VF, VPCostContext &Ctx) override;
4054
4055 /// Return the position of the first non-phi node recipe in the block.
4056 iterator getFirstNonPhi();
4057
4058 /// Returns an iterator range over the PHI-like recipes in the block.
4062
4063 /// Split current block at \p SplitAt by inserting a new block between the
4064 /// current block and its successors and moving all recipes starting at
4065 /// SplitAt to the new block. Returns the new block.
4066 VPBasicBlock *splitAt(iterator SplitAt);
4067
4068 VPRegionBlock *getEnclosingLoopRegion();
4069 const VPRegionBlock *getEnclosingLoopRegion() const;
4070
4071#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4072 /// Print this VPBsicBlock to \p O, prefixing all lines with \p Indent. \p
4073 /// SlotTracker is used to print unnamed VPValue's using consequtive numbers.
4074 ///
4075 /// Note that the numbering is applied to the whole VPlan, so printing
4076 /// individual blocks is consistent with the whole VPlan printing.
4077 void print(raw_ostream &O, const Twine &Indent,
4078 VPSlotTracker &SlotTracker) const override;
4079 using VPBlockBase::print; // Get the print(raw_stream &O) version.
4080#endif
4081
4082 /// If the block has multiple successors, return the branch recipe terminating
4083 /// the block. If there are no or only a single successor, return nullptr;
4084 VPRecipeBase *getTerminator();
4085 const VPRecipeBase *getTerminator() const;
4086
4087 /// Returns true if the block is exiting it's parent region.
4088 bool isExiting() const;
4089
4090 /// Clone the current block and it's recipes, without updating the operands of
4091 /// the cloned recipes.
4092 VPBasicBlock *clone() override;
4093
4094 /// Returns the predecessor block at index \p Idx with the predecessors as per
4095 /// the corresponding plain CFG. If the block is an entry block to a region,
4096 /// the first predecessor is the single predecessor of a region, and the
4097 /// second predecessor is the exiting block of the region.
4098 const VPBasicBlock *getCFGPredecessor(unsigned Idx) const;
4099
4100protected:
4101 /// Execute the recipes in the IR basic block \p BB.
4102 void executeRecipes(VPTransformState *State, BasicBlock *BB);
4103
4104 /// Connect the VPBBs predecessors' in the VPlan CFG to the IR basic block
4105 /// generated for this VPBB.
4106 void connectToPredecessors(VPTransformState &State);
4107
4108private:
4109 /// Create an IR BasicBlock to hold the output instructions generated by this
4110 /// VPBasicBlock, and return it. Update the CFGState accordingly.
4111 BasicBlock *createEmptyBasicBlock(VPTransformState &State);
4112};
4113
4114inline const VPBasicBlock *
4116 return getAsRecipe()->getParent()->getCFGPredecessor(Idx);
4117}
4118
4119/// A special type of VPBasicBlock that wraps an existing IR basic block.
4120/// Recipes of the block get added before the first non-phi instruction in the
4121/// wrapped block.
4122/// Note: At the moment, VPIRBasicBlock can only be used to wrap VPlan's
4123/// preheader block.
4124class VPIRBasicBlock : public VPBasicBlock {
4125 friend class VPlan;
4126
4127 BasicBlock *IRBB;
4128
4129 /// Use VPlan::createVPIRBasicBlock to create VPIRBasicBlocks.
4130 VPIRBasicBlock(BasicBlock *IRBB)
4131 : VPBasicBlock(VPIRBasicBlockSC,
4132 (Twine("ir-bb<") + IRBB->getName() + Twine(">")).str()),
4133 IRBB(IRBB) {}
4134
4135public:
4136 ~VPIRBasicBlock() override = default;
4137
4138 static inline bool classof(const VPBlockBase *V) {
4139 return V->getVPBlockID() == VPBlockBase::VPIRBasicBlockSC;
4140 }
4141
4142 /// The method which generates the output IR instructions that correspond to
4143 /// this VPBasicBlock, thereby "executing" the VPlan.
4144 void execute(VPTransformState *State) override;
4145
4146 VPIRBasicBlock *clone() override;
4147
4148 BasicBlock *getIRBasicBlock() const { return IRBB; }
4149};
4150
4151/// VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks
4152/// which form a Single-Entry-Single-Exiting subgraph of the output IR CFG.
4153/// A VPRegionBlock may indicate that its contents are to be replicated several
4154/// times. This is designed to support predicated scalarization, in which a
4155/// scalar if-then code structure needs to be generated VF * UF times. Having
4156/// this replication indicator helps to keep a single model for multiple
4157/// candidate VF's. The actual replication takes place only once the desired VF
4158/// and UF have been determined.
4159class LLVM_ABI_FOR_TEST VPRegionBlock : public VPBlockBase {
4160 friend class VPlan;
4161
4162 /// Hold the Single Entry of the SESE region modelled by the VPRegionBlock.
4163 VPBlockBase *Entry;
4164
4165 /// Hold the Single Exiting block of the SESE region modelled by the
4166 /// VPRegionBlock.
4167 VPBlockBase *Exiting;
4168
4169 /// An indicator whether this region is to generate multiple replicated
4170 /// instances of output IR corresponding to its VPBlockBases.
4171 bool IsReplicator;
4172
4173 /// Use VPlan::createVPRegionBlock to create VPRegionBlocks.
4174 VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exiting,
4175 const std::string &Name = "", bool IsReplicator = false)
4176 : VPBlockBase(VPRegionBlockSC, Name), Entry(Entry), Exiting(Exiting),
4177 IsReplicator(IsReplicator) {
4178 assert(Entry->getPredecessors().empty() && "Entry block has predecessors.");
4179 assert(Exiting->getSuccessors().empty() && "Exit block has successors.");
4180 Entry->setParent(this);
4181 Exiting->setParent(this);
4182 }
4183 VPRegionBlock(const std::string &Name = "", bool IsReplicator = false)
4184 : VPBlockBase(VPRegionBlockSC, Name), Entry(nullptr), Exiting(nullptr),
4185 IsReplicator(IsReplicator) {}
4186
4187public:
4188 ~VPRegionBlock() override = default;
4189
4190 /// Method to support type inquiry through isa, cast, and dyn_cast.
4191 static inline bool classof(const VPBlockBase *V) {
4192 return V->getVPBlockID() == VPBlockBase::VPRegionBlockSC;
4193 }
4194
4195 const VPBlockBase *getEntry() const { return Entry; }
4196 VPBlockBase *getEntry() { return Entry; }
4197
4198 /// Set \p EntryBlock as the entry VPBlockBase of this VPRegionBlock. \p
4199 /// EntryBlock must have no predecessors.
4200 void setEntry(VPBlockBase *EntryBlock) {
4201 assert(EntryBlock->getPredecessors().empty() &&
4202 "Entry block cannot have predecessors.");
4203 Entry = EntryBlock;
4204 EntryBlock->setParent(this);
4205 }
4206
4207 const VPBlockBase *getExiting() const { return Exiting; }
4208 VPBlockBase *getExiting() { return Exiting; }
4209
4210 /// Set \p ExitingBlock as the exiting VPBlockBase of this VPRegionBlock. \p
4211 /// ExitingBlock must have no successors.
4212 void setExiting(VPBlockBase *ExitingBlock) {
4213 assert(ExitingBlock->getSuccessors().empty() &&
4214 "Exit block cannot have successors.");
4215 Exiting = ExitingBlock;
4216 ExitingBlock->setParent(this);
4217 }
4218
4219 /// Returns the pre-header VPBasicBlock of the loop region.
4221 assert(!isReplicator() && "should only get pre-header of loop regions");
4222 return getSinglePredecessor()->getExitingBasicBlock();
4223 }
4224
4225 /// An indicator whether this region is to generate multiple replicated
4226 /// instances of output IR corresponding to its VPBlockBases.
4227 bool isReplicator() const { return IsReplicator; }
4228
4229 /// The method which generates the output IR instructions that correspond to
4230 /// this VPRegionBlock, thereby "executing" the VPlan.
4231 void execute(VPTransformState *State) override;
4232
4233 // Return the cost of this region.
4234 InstructionCost cost(ElementCount VF, VPCostContext &Ctx) override;
4235
4236#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4237 /// Print this VPRegionBlock to \p O (recursively), prefixing all lines with
4238 /// \p Indent. \p SlotTracker is used to print unnamed VPValue's using
4239 /// consequtive numbers.
4240 ///
4241 /// Note that the numbering is applied to the whole VPlan, so printing
4242 /// individual regions is consistent with the whole VPlan printing.
4243 void print(raw_ostream &O, const Twine &Indent,
4244 VPSlotTracker &SlotTracker) const override;
4245 using VPBlockBase::print; // Get the print(raw_stream &O) version.
4246#endif
4247
4248 /// Clone all blocks in the single-entry single-exit region of the block and
4249 /// their recipes without updating the operands of the cloned recipes.
4250 VPRegionBlock *clone() override;
4251
4252 /// Remove the current region from its VPlan, connecting its predecessor to
4253 /// its entry, and its exiting block to its successor.
4254 void dissolveToCFGLoop();
4255
4256 /// Returns the canonical induction recipe of the region.
4258 VPBasicBlock *EntryVPBB = getEntryBasicBlock();
4259 if (EntryVPBB->empty()) {
4260 // VPlan native path. TODO: Unify both code paths.
4261 EntryVPBB = cast<VPBasicBlock>(EntryVPBB->getSingleSuccessor());
4262 }
4263 return cast<VPCanonicalIVPHIRecipe>(&*EntryVPBB->begin());
4264 }
4266 return const_cast<VPRegionBlock *>(this)->getCanonicalIV();
4267 }
4268
4269 /// Return the type of the canonical IV for loop regions.
4270 Type *getCanonicalIVType() { return getCanonicalIV()->getScalarType(); }
4271 const Type *getCanonicalIVType() const {
4272 return getCanonicalIV()->getScalarType();
4273 }
4274};
4275
4277 return getParent()->getParent();
4278}
4279
4281 return getParent()->getParent();
4282}
4283
4284/// VPlan models a candidate for vectorization, encoding various decisions take
4285/// to produce efficient output IR, including which branches, basic-blocks and
4286/// output IR instructions to generate, and their cost. VPlan holds a
4287/// Hierarchical-CFG of VPBasicBlocks and VPRegionBlocks rooted at an Entry
4288/// VPBasicBlock.
4289class VPlan {
4290 friend class VPlanPrinter;
4291 friend class VPSlotTracker;
4292
4293 /// VPBasicBlock corresponding to the original preheader. Used to place
4294 /// VPExpandSCEV recipes for expressions used during skeleton creation and the
4295 /// rest of VPlan execution.
4296 /// When this VPlan is used for the epilogue vector loop, the entry will be
4297 /// replaced by a new entry block created during skeleton creation.
4298 VPBasicBlock *Entry;
4299
4300 /// VPIRBasicBlock wrapping the header of the original scalar loop.
4301 VPIRBasicBlock *ScalarHeader;
4302
4303 /// Immutable list of VPIRBasicBlocks wrapping the exit blocks of the original
4304 /// scalar loop. Note that some exit blocks may be unreachable at the moment,
4305 /// e.g. if the scalar epilogue always executes.
4307
4308 /// Holds the VFs applicable to this VPlan.
4310
4311 /// Holds the UFs applicable to this VPlan. If empty, the VPlan is valid for
4312 /// any UF.
4314
4315 /// Holds the name of the VPlan, for printing.
4316 std::string Name;
4317
4318 /// Represents the trip count of the original loop, for folding
4319 /// the tail.
4320 VPValue *TripCount = nullptr;
4321
4322 /// Represents the backedge taken count of the original loop, for folding
4323 /// the tail. It equals TripCount - 1.
4324 VPValue *BackedgeTakenCount = nullptr;
4325
4326 /// Represents the vector trip count.
4327 VPValue VectorTripCount;
4328
4329 /// Represents the vectorization factor of the loop.
4330 VPValue VF;
4331
4332 /// Represents the loop-invariant VF * UF of the vector loop region.
4333 VPValue VFxUF;
4334
4335 /// Contains all the external definitions created for this VPlan, as a mapping
4336 /// from IR Values to VPValues.
4338
4339 /// Blocks allocated and owned by the VPlan. They will be deleted once the
4340 /// VPlan is destroyed.
4341 SmallVector<VPBlockBase *> CreatedBlocks;
4342
4343 /// Construct a VPlan with \p Entry to the plan and with \p ScalarHeader
4344 /// wrapping the original header of the scalar loop.
4345 VPlan(VPBasicBlock *Entry, VPIRBasicBlock *ScalarHeader)
4346 : Entry(Entry), ScalarHeader(ScalarHeader) {
4347 Entry->setPlan(this);
4348 assert(ScalarHeader->getNumSuccessors() == 0 &&
4349 "scalar header must be a leaf node");
4350 }
4351
4352public:
4353 /// Construct a VPlan for \p L. This will create VPIRBasicBlocks wrapping the
4354 /// original preheader and scalar header of \p L, to be used as entry and
4355 /// scalar header blocks of the new VPlan.
4356 VPlan(Loop *L);
4357
4358 /// Construct a VPlan with a new VPBasicBlock as entry, a VPIRBasicBlock
4359 /// wrapping \p ScalarHeaderBB and a trip count of \p TC.
4360 VPlan(BasicBlock *ScalarHeaderBB) {
4361 setEntry(createVPBasicBlock("preheader"));
4362 ScalarHeader = createVPIRBasicBlock(ScalarHeaderBB);
4363 }
4364
4366
4368 Entry = VPBB;
4369 VPBB->setPlan(this);
4370 }
4371
4372 /// Generate the IR code for this VPlan.
4373 void execute(VPTransformState *State);
4374
4375 /// Return the cost of this plan.
4377
4378 VPBasicBlock *getEntry() { return Entry; }
4379 const VPBasicBlock *getEntry() const { return Entry; }
4380
4381 /// Returns the preheader of the vector loop region, if one exists, or null
4382 /// otherwise.
4384 VPRegionBlock *VectorRegion = getVectorLoopRegion();
4385 return VectorRegion
4386 ? cast<VPBasicBlock>(VectorRegion->getSinglePredecessor())
4387 : nullptr;
4388 }
4389
4390 /// Returns the VPRegionBlock of the vector loop.
4393
4394 /// Returns the 'middle' block of the plan, that is the block that selects
4395 /// whether to execute the scalar tail loop or the exit block from the loop
4396 /// latch. If there is an early exit from the vector loop, the middle block
4397 /// conceptully has the early exit block as third successor, split accross 2
4398 /// VPBBs. In that case, the second VPBB selects whether to execute the scalar
4399 /// tail loop or the exit bock. If the scalar tail loop or exit block are
4400 /// known to always execute, the middle block may branch directly to that
4401 /// block. This function cannot be called once the vector loop region has been
4402 /// removed.
4404 VPRegionBlock *LoopRegion = getVectorLoopRegion();
4405 assert(
4406 LoopRegion &&
4407 "cannot call the function after vector loop region has been removed");
4408 auto *RegionSucc = cast<VPBasicBlock>(LoopRegion->getSingleSuccessor());
4409 if (RegionSucc->getSingleSuccessor() ||
4410 is_contained(RegionSucc->getSuccessors(), getScalarPreheader()))
4411 return RegionSucc;
4412 // There is an early exit. The successor of RegionSucc is the middle block.
4413 return cast<VPBasicBlock>(RegionSucc->getSuccessors()[1]);
4414 }
4415
4417 return const_cast<VPlan *>(this)->getMiddleBlock();
4418 }
4419
4420 /// Return the VPBasicBlock for the preheader of the scalar loop.
4422 return cast<VPBasicBlock>(getScalarHeader()->getSinglePredecessor());
4423 }
4424
4425 /// Return the VPIRBasicBlock wrapping the header of the scalar loop.
4426 VPIRBasicBlock *getScalarHeader() const { return ScalarHeader; }
4427
4428 /// Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of
4429 /// the original scalar loop.
4430 ArrayRef<VPIRBasicBlock *> getExitBlocks() const { return ExitBlocks; }
4431
4432 /// Return the VPIRBasicBlock corresponding to \p IRBB. \p IRBB must be an
4433 /// exit block.
4435
4436 /// Returns true if \p VPBB is an exit block.
4437 bool isExitBlock(VPBlockBase *VPBB);
4438
4439 /// The trip count of the original loop.
4441 assert(TripCount && "trip count needs to be set before accessing it");
4442 return TripCount;
4443 }
4444
4445 /// Set the trip count assuming it is currently null; if it is not - use
4446 /// resetTripCount().
4447 void setTripCount(VPValue *NewTripCount) {
4448 assert(!TripCount && NewTripCount && "TripCount should not be set yet.");
4449 TripCount = NewTripCount;
4450 }
4451
4452 /// Resets the trip count for the VPlan. The caller must make sure all uses of
4453 /// the original trip count have been replaced.
4454 void resetTripCount(VPValue *NewTripCount) {
4455 assert(TripCount && NewTripCount && TripCount->getNumUsers() == 0 &&
4456 "TripCount must be set when resetting");
4457 TripCount = NewTripCount;
4458 }
4459
4460 /// The backedge taken count of the original loop.
4462 if (!BackedgeTakenCount)
4463 BackedgeTakenCount = new VPValue();
4464 return BackedgeTakenCount;
4465 }
4466 VPValue *getBackedgeTakenCount() const { return BackedgeTakenCount; }
4467
4468 /// The vector trip count.
4469 VPValue &getVectorTripCount() { return VectorTripCount; }
4470
4471 /// Returns the VF of the vector loop region.
4472 VPValue &getVF() { return VF; };
4473 const VPValue &getVF() const { return VF; };
4474
4475 /// Returns VF * UF of the vector loop region.
4476 VPValue &getVFxUF() { return VFxUF; }
4477
4480 }
4481
4482 void addVF(ElementCount VF) { VFs.insert(VF); }
4483
4485 assert(hasVF(VF) && "Cannot set VF not already in plan");
4486 VFs.clear();
4487 VFs.insert(VF);
4488 }
4489
4490 bool hasVF(ElementCount VF) const { return VFs.count(VF); }
4491 bool hasScalableVF() const {
4492 return any_of(VFs, [](ElementCount VF) { return VF.isScalable(); });
4493 }
4494
4495 /// Returns an iterator range over all VFs of the plan.
4498 return VFs;
4499 }
4500
4501 bool hasScalarVFOnly() const {
4502 bool HasScalarVFOnly = VFs.size() == 1 && VFs[0].isScalar();
4503 assert(HasScalarVFOnly == hasVF(ElementCount::getFixed(1)) &&
4504 "Plan with scalar VF should only have a single VF");
4505 return HasScalarVFOnly;
4506 }
4507
4508 bool hasUF(unsigned UF) const { return UFs.empty() || UFs.contains(UF); }
4509
4510 unsigned getUF() const {
4511 assert(UFs.size() == 1 && "Expected a single UF");
4512 return UFs[0];
4513 }
4514
4515 void setUF(unsigned UF) {
4516 assert(hasUF(UF) && "Cannot set the UF not already in plan");
4517 UFs.clear();
4518 UFs.insert(UF);
4519 }
4520
4521 /// Returns true if the VPlan already has been unrolled, i.e. it has a single
4522 /// concrete UF.
4523 bool isUnrolled() const { return UFs.size() == 1; }
4524
4525 /// Return a string with the name of the plan and the applicable VFs and UFs.
4526 std::string getName() const;
4527
4528 void setName(const Twine &newName) { Name = newName.str(); }
4529
4530 /// Gets the live-in VPValue for \p V or adds a new live-in (if none exists
4531 /// yet) for \p V.
4533 assert(V && "Trying to get or add the VPValue of a null Value");
4534 auto [It, Inserted] = LiveIns.try_emplace(V);
4535 if (Inserted) {
4536 VPValue *VPV = new VPValue(V);
4537 assert(VPV->isLiveIn() && "VPV must be a live-in.");
4538 It->second = VPV;
4539 }
4540
4541 assert(It->second->isLiveIn() && "Only live-ins should be in mapping");
4542 return It->second;
4543 }
4544
4545 /// Return a VPValue wrapping i1 true.
4546 VPValue *getTrue() { return getConstantInt(1, 1); }
4547
4548 /// Return a VPValue wrapping i1 false.
4549 VPValue *getFalse() { return getConstantInt(1, 0); }
4550
4551 /// Return a VPValue wrapping a ConstantInt with the given type and value.
4552 VPValue *getConstantInt(Type *Ty, uint64_t Val, bool IsSigned = false) {
4553 return getOrAddLiveIn(ConstantInt::get(Ty, Val, IsSigned));
4554 }
4555
4556 /// Return a VPValue wrapping a ConstantInt with the given bitwidth and value.
4558 bool IsSigned = false) {
4559 return getConstantInt(APInt(BitWidth, Val, IsSigned));
4560 }
4561
4562 /// Return a VPValue wrapping a ConstantInt with the given APInt value.
4564 return getOrAddLiveIn(ConstantInt::get(getContext(), Val));
4565 }
4566
4567 /// Return the live-in VPValue for \p V, if there is one or nullptr otherwise.
4568 VPValue *getLiveIn(Value *V) const { return LiveIns.lookup(V); }
4569
4570 /// Return the list of live-in VPValues available in the VPlan.
4571 auto getLiveIns() const { return LiveIns.values(); }
4572
4573#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4574 /// Print the live-ins of this VPlan to \p O.
4575 void printLiveIns(raw_ostream &O) const;
4576
4577 /// Print this VPlan to \p O.
4578 LLVM_ABI_FOR_TEST void print(raw_ostream &O) const;
4579
4580 /// Print this VPlan in DOT format to \p O.
4581 LLVM_ABI_FOR_TEST void printDOT(raw_ostream &O) const;
4582
4583 /// Dump the plan to stderr (for debugging).
4584 LLVM_DUMP_METHOD void dump() const;
4585#endif
4586
4587 /// Clone the current VPlan, update all VPValues of the new VPlan and cloned
4588 /// recipes to refer to the clones, and return it.
4590
4591 /// Create a new VPBasicBlock with \p Name and containing \p Recipe if
4592 /// present. The returned block is owned by the VPlan and deleted once the
4593 /// VPlan is destroyed.
4595 VPRecipeBase *Recipe = nullptr) {
4596 auto *VPB = new VPBasicBlock(Name, Recipe);
4597 CreatedBlocks.push_back(VPB);
4598 return VPB;
4599 }
4600
4601 /// Create a new loop region with \p Name and entry and exiting blocks set
4602 /// to \p Entry and \p Exiting respectively, if set. The returned block is
4603 /// owned by the VPlan and deleted once the VPlan is destroyed.
4604 VPRegionBlock *createLoopRegion(const std::string &Name = "",
4605 VPBlockBase *Entry = nullptr,
4606 VPBlockBase *Exiting = nullptr) {
4607 auto *VPB = Entry ? new VPRegionBlock(Entry, Exiting, Name)
4608 : new VPRegionBlock(Name);
4609 CreatedBlocks.push_back(VPB);
4610 return VPB;
4611 }
4612
4613 /// Create a new replicate region with \p Entry, \p Exiting and \p Name. The
4614 /// returned block is owned by the VPlan and deleted once the VPlan is
4615 /// destroyed.
4617 const std::string &Name = "") {
4618 auto *VPB = new VPRegionBlock(Entry, Exiting, Name, true);
4619 CreatedBlocks.push_back(VPB);
4620 return VPB;
4621 }
4622
4623 /// Create a VPIRBasicBlock wrapping \p IRBB, but do not create
4624 /// VPIRInstructions wrapping the instructions in t\p IRBB. The returned
4625 /// block is owned by the VPlan and deleted once the VPlan is destroyed.
4627
4628 /// Create a VPIRBasicBlock from \p IRBB containing VPIRInstructions for all
4629 /// instructions in \p IRBB, except its terminator which is managed by the
4630 /// successors of the block in VPlan. The returned block is owned by the VPlan
4631 /// and deleted once the VPlan is destroyed.
4633
4634 /// Returns true if the VPlan is based on a loop with an early exit. That is
4635 /// the case if the VPlan has either more than one exit block or a single exit
4636 /// block with multiple predecessors (one for the exit via the latch and one
4637 /// via the other early exit).
4638 bool hasEarlyExit() const {
4639 return count_if(ExitBlocks,
4640 [](VPIRBasicBlock *EB) { return EB->hasPredecessors(); }) >
4641 1 ||
4642 (ExitBlocks.size() == 1 && ExitBlocks[0]->getNumPredecessors() > 1);
4643 }
4644
4645 /// Returns true if the scalar tail may execute after the vector loop. Note
4646 /// that this relies on unneeded branches to the scalar tail loop being
4647 /// removed.
4648 bool hasScalarTail() const {
4649 return !(!getScalarPreheader()->hasPredecessors() ||
4651 }
4652};
4653
4654#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4655inline raw_ostream &operator<<(raw_ostream &OS, const VPlan &Plan) {
4656 Plan.print(OS);
4657 return OS;
4658}
4659#endif
4660
4661} // end namespace llvm
4662
4663#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
#define LLVM_ABI_FOR_TEST
Definition Compiler.h:218
dxil translate DXIL Translate Metadata
Hexagon Common GEP
iv users
Definition IVUsers.cpp:48
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static std::pair< Value *, APInt > getMask(Value *WideMask, unsigned Factor, ElementCount LeafValueEC)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first DebugLoc that has line number information, given a range of instructions.
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
#define T
MachineInstr unsigned OpIdx
#define P(N)
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
static StringRef getName(Value *V)
static bool mayHaveSideEffects(MachineInstr &MI)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static const BasicSubtargetSubTypeKV * find(StringRef S, ArrayRef< BasicSubtargetSubTypeKV > A)
Find KV in array using binary search.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
This file contains the declarations of the entities induced by Vectorization Plans,...
#define VP_CLASSOF_IMPL(VPDefID)
Definition VPlan.h:509
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
A debug info location.
Definition DebugLoc.h:123
static DebugLoc getUnknown()
Definition DebugLoc.h:161
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags none()
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
A struct for saving information about induction variables.
InductionKind
This enum represents the kinds of inductions that we support.
InnerLoopVectorizer vectorizes loops which contain only one basic block to a specified vectorization ...
The group of interleaved loads/stores sharing the same stride and close to each other.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
LoopVectorizationCostModel - estimates the expected speedups due to vectorization.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1078
Root of the metadata hierarchy.
Definition Metadata.h:64
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
This class represents an analyzed expression in the program.
This class represents the LLVM 'select' instruction.
This class provides computation of slot numbers for LLVM Assembly writing.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
This class represents a truncation of integer types.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
void execute(VPTransformState &State) override
Generate the active lane mask phi of the vector loop.
VPActiveLaneMaskPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3618
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPActiveLaneMaskPHIRecipe(VPValue *StartMask, DebugLoc DL)
Definition VPlan.h:3612
~VPActiveLaneMaskPHIRecipe() override=default
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:3971
RecipeListTy::const_iterator const_iterator
Definition VPlan.h:3999
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition VPlan.h:4046
RecipeListTy::const_reverse_iterator const_reverse_iterator
Definition VPlan.h:4001
RecipeListTy::iterator iterator
Instruction iterators...
Definition VPlan.h:3998
RecipeListTy & getRecipeList()
Returns a reference to the list of recipes.
Definition VPlan.h:4024
iplist< VPRecipeBase > RecipeListTy
Definition VPlan.h:3982
VPBasicBlock(const unsigned char BlockSC, const Twine &Name="")
Definition VPlan.h:3988
iterator end()
Definition VPlan.h:4008
iterator begin()
Recipe iterator methods.
Definition VPlan.h:4006
RecipeListTy::reverse_iterator reverse_iterator
Definition VPlan.h:4000
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4059
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:770
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:216
~VPBasicBlock() override
Definition VPlan.h:3992
const_reverse_iterator rbegin() const
Definition VPlan.h:4012
reverse_iterator rend()
Definition VPlan.h:4013
RecipeListTy Recipes
The VPRecipes held in the order of output instructions to generate.
Definition VPlan.h:3986
VPRecipeBase & back()
Definition VPlan.h:4021
const VPRecipeBase & front() const
Definition VPlan.h:4018
const_iterator begin() const
Definition VPlan.h:4007
VPRecipeBase & front()
Definition VPlan.h:4019
const VPRecipeBase & back() const
Definition VPlan.h:4020
void insert(VPRecipeBase *Recipe, iterator InsertPt)
Definition VPlan.h:4037
bool empty() const
Definition VPlan.h:4017
const_iterator end() const
Definition VPlan.h:4009
static bool classof(const VPBlockBase *V)
Method to support type inquiry through isa, cast, and dyn_cast.
Definition VPlan.h:4032
static RecipeListTy VPBasicBlock::* getSublistAccess(VPRecipeBase *)
Returns a pointer to a member of the recipe list.
Definition VPlan.h:4027
reverse_iterator rbegin()
Definition VPlan.h:4011
friend class VPlan
Definition VPlan.h:3972
size_t size() const
Definition VPlan.h:4016
const_reverse_iterator rend() const
Definition VPlan.h:4014
VPValue * getIncomingValue(unsigned Idx) const
Return incoming value number Idx.
Definition VPlan.h:2548
VPValue * getMask(unsigned Idx) const
Return mask number Idx.
Definition VPlan.h:2553
unsigned getNumIncomingValues() const
Return the number of incoming values, taking into account when normalized the first incoming value wi...
Definition VPlan.h:2543
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
Definition VPlan.h:2564
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2573
VPBlendRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2530
VPBlendRecipe(PHINode *Phi, ArrayRef< VPValue * > Operands, DebugLoc DL)
The blend operation is a User of the incoming values and of their respective masks,...
Definition VPlan.h:2525
void setMask(unsigned Idx, VPValue *V)
Set mask number Idx to V.
Definition VPlan.h:2559
bool isNormalized() const
A normalized blend is one that has an odd number of operands, whereby the first operand does not have...
Definition VPlan.h:2539
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
VPBlocksTy & getPredecessors()
Definition VPlan.h:205
iterator_range< VPBlockBase ** > predecessors()
Definition VPlan.h:202
LLVM_DUMP_METHOD void dump() const
Dump this VPBlockBase to dbgs().
Definition VPlan.h:370
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 swapSuccessors()
Swap successors of the block. The block must have exactly 2 successors.
Definition VPlan.h:322
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:645
SmallVectorImpl< VPBlockBase * > VPBlocksTy
Definition VPlan.h:160
virtual ~VPBlockBase()=default
const VPBlocksTy & getHierarchicalPredecessors()
Definition VPlan.h:258
unsigned getIndexForSuccessor(const VPBlockBase *Succ) const
Returns the index for Succ in the blocks successor list.
Definition VPlan.h:335
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:208
unsigned getIndexForPredecessor(const VPBlockBase *Pred) const
Returns the index for Pred in the blocks predecessors list.
Definition VPlan.h:328
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:204
virtual VPBlockBase * clone()=0
Clone the current block and it's recipes without updating the operands of the cloned recipes,...
enum { VPRegionBlockSC, VPBasicBlockSC, VPIRBasicBlockSC } VPBlockTy
An enumeration for keeping track of the concrete subclass of VPBlockBase that are actually instantiat...
Definition VPlan.h:158
virtual InstructionCost cost(ElementCount VF, VPCostContext &Ctx)=0
Return the cost of the block.
void setPlan(VPlan *ParentPlan)
Sets the pointer of the plan containing the block.
Definition VPlan.cpp:180
const VPRegionBlock * getParent() const
Definition VPlan.h:174
const std::string & getName() const
Definition VPlan.h:164
void clearSuccessors()
Remove all the successors of this block.
Definition VPlan.h:310
VPBlockBase * getSingleHierarchicalSuccessor()
Definition VPlan.h:248
void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse)
Set two given VPBlockBases IfTrue and IfFalse to be the two successors of this VPBlockBase.
Definition VPlan.h:282
VPBlockBase * getSinglePredecessor() const
Definition VPlan.h:215
virtual void execute(VPTransformState *State)=0
The method which generates the output IR that correspond to this VPBlockBase, thereby "executing" the...
const VPBlocksTy & getHierarchicalSuccessors()
Definition VPlan.h:242
void clearPredecessors()
Remove all the predecessor of this block.
Definition VPlan.h:307
friend class VPBlockUtils
Definition VPlan.h:82
unsigned getVPBlockID() const
Definition VPlan.h:171
void printAsOperand(raw_ostream &OS, bool PrintType=false) const
Definition VPlan.h:349
void swapPredecessors()
Swap predecessors of the block.
Definition VPlan.h:314
VPBlockBase(const unsigned char SC, const std::string &N)
Definition VPlan.h:150
VPBlocksTy & getSuccessors()
Definition VPlan.h:199
VPBlockBase * getEnclosingBlockWithSuccessors()
An Enclosing Block of a block B is any block containing B, including B itself.
Definition VPlan.cpp:200
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:166
void setOneSuccessor(VPBlockBase *Successor)
Set a given VPBlockBase Successor as the single successor of this VPBlockBase.
Definition VPlan.h:271
void setParent(VPRegionBlock *P)
Definition VPlan.h:184
VPBlockBase * getSingleHierarchicalPredecessor()
Definition VPlan.h:264
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:209
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:198
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
Definition VPlan.h:3044
VPBranchOnMaskRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3028
bool usesScalars(const VPValue *Op) const override
Returns true if the recipe uses scalars of operand Op.
Definition VPlan.h:3052
VPBranchOnMaskRecipe(VPValue *BlockInMask, DebugLoc DL)
Definition VPlan.h:3025
VPlan-based builder utility analogous to IRBuilder.
Canonical scalar induction phi of the vector loop.
Definition VPlan.h:3554
~VPCanonicalIVPHIRecipe() override=default
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3580
VPCanonicalIVPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3561
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
bool usesFirstPartOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first part of operand Op.
Definition VPlan.h:3587
VPCanonicalIVPHIRecipe(VPValue *StartV, DebugLoc DL)
Definition VPlan.h:3556
Type * getScalarType() const
Returns the scalar type of the induction.
Definition VPlan.h:3575
void execute(VPTransformState &State) override
Generate the phi nodes.
Definition VPlan.h:3569
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPCanonicalIVPHIRecipe.
Definition VPlan.h:3594
This class augments a recipe with a set of VPValues defined by the recipe.
Definition VPlanValue.h:305
friend class VPValue
Definition VPlanValue.h:306
VPDef(const unsigned char SC)
Definition VPlanValue.h:384
void execute(VPTransformState &State) override
Generate the transformed value of the induction at offset StartValue (1.
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPDerivedIVRecipe.
Definition VPlan.h:3760
VPValue * getStepValue() const
Definition VPlan.h:3771
Type * getScalarType() const
Definition VPlan.h:3766
VPDerivedIVRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3748
VPDerivedIVRecipe(InductionDescriptor::InductionKind Kind, const FPMathOperator *FPBinOp, VPValue *Start, VPValue *IV, VPValue *Step, const Twine &Name="")
Definition VPlan.h:3740
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
~VPDerivedIVRecipe() override=default
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3774
VPValue * getStartValue() const
Definition VPlan.h:3770
VPDerivedIVRecipe(const InductionDescriptor &IndDesc, VPValue *Start, VPCanonicalIVPHIRecipe *CanonicalIV, VPValue *Step, const Twine &Name="")
Definition VPlan.h:3732
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3668
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPEVLBasedIVPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3649
~VPEVLBasedIVPHIRecipe() override=default
void execute(VPTransformState &State) override
Generate the phi nodes.
Definition VPlan.h:3655
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPEVLBasedIVPHIRecipe.
Definition VPlan.h:3661
VPEVLBasedIVPHIRecipe(VPValue *StartIV, DebugLoc DL)
Definition VPlan.h:3644
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
Definition VPlan.h:3529
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPExpandSCEVRecipe.
Definition VPlan.h:3534
VPExpandSCEVRecipe(const SCEV *Expr)
Definition VPlan.h:3520
const SCEV * getSCEV() const
Definition VPlan.h:3540
VPExpandSCEVRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3525
~VPExpandSCEVRecipe() override=default
void execute(VPTransformState &State) override
Method for generating code, must not be called as this recipe is abstract.
Definition VPlan.h:3179
VPValue * getOperandOfResultType() const
Return the VPValue to use to infer the result type of the recipe.
Definition VPlan.h:3161
VPExpressionRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3143
void decompose()
Insert the recipes of the expression back into the VPlan, directly before the current recipe.
~VPExpressionRecipe() override
Definition VPlan.h:3131
bool isSingleScalar() const
Returns true if the result of this VPExpressionRecipe is a single-scalar.
VPExpressionRecipe(VPWidenCastRecipe *Ext0, VPWidenCastRecipe *Ext1, VPWidenRecipe *Mul, VPWidenRecipe *Sub, VPReductionRecipe *Red)
Definition VPlan.h:3117
VPExpressionRecipe(VPWidenCastRecipe *Ext, VPReductionRecipe *Red)
Definition VPlan.h:3109
bool mayHaveSideEffects() const
Returns true if this expression contains recipes that may have side effects.
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Compute the cost of this recipe either using a recipe's specialized implementation or using the legac...
bool mayReadOrWriteMemory() const
Returns true if this expression contains recipes that may read from or write to memory.
VPExpressionRecipe(VPWidenCastRecipe *Ext0, VPWidenCastRecipe *Ext1, VPWidenRecipe *Mul, VPReductionRecipe *Red)
Definition VPlan.h:3113
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
unsigned getVFScaleFactor() const
Definition VPlan.h:3173
VPExpressionRecipe(VPWidenRecipe *Mul, VPReductionRecipe *Red)
Definition VPlan.h:3111
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this header phi recipe.
const VPRecipeBase * getAsRecipe() const override
Return a VPRecipeBase* to the current object.
Definition VPlan.h:2059
static bool classof(const VPSingleDefRecipe *R)
Definition VPlan.h:2072
static bool classof(const VPValue *V)
Definition VPlan.h:2069
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override=0
Print the recipe.
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2095
void setBackedgeValue(VPValue *V)
Update the incoming value from the loop backedge.
Definition VPlan.h:2100
VPValue * getStartValue()
Returns the start value of the phi, if one is set.
Definition VPlan.h:2084
void setStartValue(VPValue *V)
Update the start value of the recipe.
Definition VPlan.h:2092
static bool classof(const VPRecipeBase *R)
Method to support type inquiry through isa, cast, and dyn_cast.
Definition VPlan.h:2065
VPValue * getStartValue() const
Definition VPlan.h:2087
void execute(VPTransformState &State) override=0
Generate the phi nodes.
virtual VPRecipeBase & getBackedgeRecipe()
Returns the backedge value as a recipe.
Definition VPlan.h:2104
VPHeaderPHIRecipe(unsigned char VPDefID, Instruction *UnderlyingInstr, VPValue *Start, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:2054
~VPHeaderPHIRecipe() override=default
void execute(VPTransformState &State) override
Produce a vectorized histogram operation.
VP_CLASSOF_IMPL(VPDef::VPHistogramSC)
VPHistogramRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1768
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPHistogramRecipe.
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPValue * getMask() const
Return the mask operand if one was provided, or a null pointer if all lanes should be executed uncond...
Definition VPlan.h:1785
unsigned getOpcode() const
Definition VPlan.h:1781
VPHistogramRecipe(unsigned Opcode, ArrayRef< VPValue * > Operands, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:1762
~VPHistogramRecipe() override=default
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:4124
void execute(VPTransformState *State) override
The method which generates the output IR instructions that correspond to this VPBasicBlock,...
Definition VPlan.cpp:446
BasicBlock * getIRBasicBlock() const
Definition VPlan.h:4148
static bool classof(const VPBlockBase *V)
Definition VPlan.h:4138
~VPIRBasicBlock() override=default
friend class VPlan
Definition VPlan.h:4125
VPIRBasicBlock * clone() override
Clone the current block and it's recipes, without updating the operands of the cloned recipes.
Definition VPlan.cpp:471
Class to record and manage LLVM IR flags.
Definition VPlan.h:609
FastMathFlagsTy FMFs
Definition VPlan.h:680
LLVM_ABI_FOR_TEST bool flagsValidForOpcode(unsigned Opcode) const
Returns true if the set flags are valid for Opcode.
VPIRFlags(DisjointFlagsTy DisjointFlags)
Definition VPlan.h:740
VPIRFlags(WrapFlagsTy WrapFlags)
Definition VPlan.h:732
WrapFlagsTy WrapFlags
Definition VPlan.h:674
CmpInst::Predicate CmpPredicate
Definition VPlan.h:673
void printFlags(raw_ostream &O) const
VPIRFlags(CmpInst::Predicate Pred, FastMathFlags FMFs)
Definition VPlan.h:726
GEPNoWrapFlags GEPFlags
Definition VPlan.h:678
bool hasFastMathFlags() const
Returns true if the recipe has fast-math flags.
Definition VPlan.h:858
LLVM_ABI_FOR_TEST FastMathFlags getFastMathFlags() const
TruncFlagsTy TruncFlags
Definition VPlan.h:675
CmpInst::Predicate getPredicate() const
Definition VPlan.h:835
bool hasNonNegFlag() const
Returns true if the recipe has non-negative flag.
Definition VPlan.h:865
void transferFlags(VPIRFlags &Other)
Definition VPlan.h:749
ExactFlagsTy ExactFlags
Definition VPlan.h:677
bool hasNoSignedWrap() const
Definition VPlan.h:884
void intersectFlags(const VPIRFlags &Other)
Only keep flags also present in Other.
bool isDisjoint() const
Definition VPlan.h:895
VPIRFlags(TruncFlagsTy TruncFlags)
Definition VPlan.h:735
VPIRFlags(FastMathFlags FMFs)
Definition VPlan.h:738
VPIRFlags(NonNegFlagsTy NonNegFlags)
Definition VPlan.h:743
VPIRFlags(CmpInst::Predicate Pred)
Definition VPlan.h:723
bool isNonNeg() const
Definition VPlan.h:867
GEPNoWrapFlags getGEPNoWrapFlags() const
Definition VPlan.h:850
bool hasPredicate() const
Returns true if the recipe has a comparison predicate.
Definition VPlan.h:853
DisjointFlagsTy DisjointFlags
Definition VPlan.h:676
unsigned AllFlags
Definition VPlan.h:682
void setPredicate(CmpInst::Predicate Pred)
Definition VPlan.h:841
bool hasNoUnsignedWrap() const
Definition VPlan.h:873
FCmpFlagsTy FCmpFlags
Definition VPlan.h:681
NonNegFlagsTy NonNegFlags
Definition VPlan.h:679
void dropPoisonGeneratingFlags()
Drop all poison-generating flags.
Definition VPlan.h:759
void applyFlags(Instruction &I) const
Apply the IR flags to I.
Definition VPlan.h:795
VPIRFlags(GEPNoWrapFlags GEPFlags)
Definition VPlan.h:746
VPIRFlags(Instruction &I)
Definition VPlan.h:688
Instruction & getInstruction() const
Definition VPlan.h:1447
bool usesFirstPartOnly(const VPValue *Op) const override
Returns true if the VPUser only uses the first part of operand Op.
Definition VPlan.h:1455
void extractLastLaneOfLastPartOfFirstOperand(VPBuilder &Builder)
Update the recipe's first operand to the last lane of the last part of the operand using Builder.
~VPIRInstruction() override=default
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
VPIRInstruction * clone() override
Clone the current recipe.
Definition VPlan.h:1434
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the VPUser only uses the first lane of operand Op.
Definition VPlan.h:1461
static LLVM_ABI_FOR_TEST VPIRInstruction * create(Instruction &I)
Create a new VPIRPhi for \I , if it is a PHINode, otherwise create a VPIRInstruction.
LLVM_ABI_FOR_TEST InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPIRInstruction.
bool usesScalars(const VPValue *Op) const override
Returns true if the VPUser uses scalars of operand Op.
Definition VPlan.h:1449
VPIRInstruction(Instruction &I)
VPIRInstruction::create() should be used to create VPIRInstructions, as subclasses may need to be cre...
Definition VPlan.h:1422
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
Helper to manage IR metadata for recipes.
Definition VPlan.h:982
VPIRMetadata & operator=(const VPIRMetadata &Other)=default
MDNode * getMetadata(unsigned Kind) const
Get metadata of kind Kind. Returns nullptr if not found.
Definition VPlan.h:1018
VPIRMetadata(Instruction &I)
Adds metatadata that can be preserved from the original instruction I.
Definition VPlan.h:990
VPIRMetadata(const VPIRMetadata &Other)=default
Copy constructor for cloning.
void intersect(const VPIRMetadata &MD)
Intersect this VPIRMetadata object with MD, keeping only metadata nodes that are common to both.
VPIRMetadata()=default
void print(raw_ostream &O, VPSlotTracker &SlotTracker) const
Print metadata with node IDs.
void applyMetadata(Instruction &I) const
Add all metadata to I.
void setMetadata(unsigned Kind, MDNode *Node)
Set metadata with kind Kind to Node.
Definition VPlan.h:1002
VPInstructionWithType(unsigned Opcode, ArrayRef< VPValue * > Operands, Type *ResultTy, const VPIRFlags &Flags={}, const VPIRMetadata &Metadata={}, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Definition VPlan.h:1261
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPInstruction.
Definition VPlan.h:1302
static bool classof(const VPUser *R)
Definition VPlan.h:1287
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:1269
Type * getResultType() const
Definition VPlan.h:1308
VPInstruction * clone() override
Clone the current recipe.
Definition VPlan.h:1291
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
void execute(VPTransformState &State) override
Generate the instruction.
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1036
VPInstruction * clone() override
Clone the current recipe.
Definition VPlan.h:1174
@ ExtractLane
Extracts a single lane (first operand) from a set of vector operands.
Definition VPlan.h:1127
@ ComputeAnyOfResult
Compute the final result of a AnyOf reduction with select(cmp(),x,y), where one of (x,...
Definition VPlan.h:1074
@ WideIVStep
Scale the first operand (vector step) by the second operand (scalar-step).
Definition VPlan.h:1117
@ ResumeForEpilogue
Explicit user for the resume phi of the canonical induction in the main VPlan, used by the epilogue v...
Definition VPlan.h:1130
@ Unpack
Extracts all lanes from its (non-scalable) vector operand.
Definition VPlan.h:1071
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1121
@ BuildVector
Creates a fixed-width vector containing all operands.
Definition VPlan.h:1066
@ BuildStructVector
Given operands of (the same) struct type, creates a struct of fixed- width vectors each containing a ...
Definition VPlan.h:1063
@ VScale
Returns the value for vscale.
Definition VPlan.h:1132
@ CanonicalIVIncrementForPart
Definition VPlan.h:1056
bool hasResult() const
Definition VPlan.h:1198
StringRef getName() const
Returns the symbolic name assigned to the VPInstruction.
Definition VPlan.h:1238
unsigned getOpcode() const
Definition VPlan.h:1182
VPInstruction(unsigned Opcode, ArrayRef< VPValue * > Operands, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
void setName(StringRef NewName)
Set the symbolic name for the VPInstruction.
Definition VPlan.h:1241
friend class VPlanSlp
Definition VPlan.h:1037
virtual unsigned getNumStoreOperands() const =0
Returns the number of stored operands of this interleave group.
bool usesFirstLaneOnly(const VPValue *Op) const override=0
Returns true if the recipe only uses the first lane of operand Op.
bool needsMaskForGaps() const
Return true if the access needs a mask because of the gaps.
Definition VPlan.h:2659
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
Definition VPlan.h:2665
static bool classof(const VPUser *U)
Definition VPlan.h:2641
VPInterleaveBase(const unsigned char SC, const InterleaveGroup< Instruction > *IG, ArrayRef< VPValue * > Operands, ArrayRef< VPValue * > StoredValues, VPValue *Mask, bool NeedsMaskForGaps, const VPIRMetadata &MD, DebugLoc DL)
Definition VPlan.h:2608
Instruction * getInsertPos() const
Definition VPlan.h:2663
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:2636
const InterleaveGroup< Instruction > * getInterleaveGroup() const
Definition VPlan.h:2661
VPValue * getMask() const
Return the mask used by this recipe.
Definition VPlan.h:2653
ArrayRef< VPValue * > getStoredValues() const
Return the VPValues stored by this interleave group.
Definition VPlan.h:2682
VPInterleaveBase * clone() override=0
Clone the current recipe.
VPValue * getAddr() const
Return the address accessed by this recipe.
Definition VPlan.h:2647
A recipe for interleaved memory operations with vector-predication intrinsics.
Definition VPlan.h:2735
bool usesFirstLaneOnly(const VPValue *Op) const override
The recipe only uses the first lane of the address, and EVL operand.
Definition VPlan.h:2763
VPValue * getEVL() const
The VPValue of the explicit vector length.
Definition VPlan.h:2757
~VPInterleaveEVLRecipe() override=default
unsigned getNumStoreOperands() const override
Returns the number of stored operands of this interleave group.
Definition VPlan.h:2770
VPInterleaveEVLRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2750
VPInterleaveEVLRecipe(VPInterleaveRecipe &R, VPValue &EVL, VPValue *Mask)
Definition VPlan.h:2737
VPInterleaveRecipe is a recipe for transforming an interleave group of load or stores into one wide l...
Definition VPlan.h:2693
unsigned getNumStoreOperands() const override
Returns the number of stored operands of this interleave group.
Definition VPlan.h:2720
~VPInterleaveRecipe() override=default
VPInterleaveRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2703
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2714
VPInterleaveRecipe(const InterleaveGroup< Instruction > *IG, VPValue *Addr, ArrayRef< VPValue * > StoredValues, VPValue *Mask, bool NeedsMaskForGaps, const VPIRMetadata &MD, DebugLoc DL)
Definition VPlan.h:2695
In what follows, the term "input IR" refers to code that is fed into the vectorizer whereas the term ...
Helper type to provide functions to access incoming values and blocks for phi-like recipes.
Definition VPlan.h:1320
virtual const VPRecipeBase * getAsRecipe() const =0
Return a VPRecipeBase* to the current object.
VPUser::const_operand_range incoming_values() const
Returns an interator range over the incoming values.
Definition VPlan.h:1342
virtual unsigned getNumIncoming() const
Returns the number of incoming values, also number of incoming blocks.
Definition VPlan.h:1337
void removeIncomingValueFor(VPBlockBase *IncomingBlock) const
Removes the incoming value for IncomingBlock, which must be a predecessor.
const VPBasicBlock * getIncomingBlock(unsigned Idx) const
Returns the incoming block with index Idx.
Definition VPlan.h:4115
detail::zippy< llvm::detail::zip_first, VPUser::const_operand_range, const_incoming_blocks_range > incoming_values_and_blocks() const
Returns an iterator range over pairs of incoming values and corresponding incoming blocks.
Definition VPlan.h:1362
VPValue * getIncomingValue(unsigned Idx) const
Returns the incoming VPValue with index Idx.
Definition VPlan.h:1329
virtual ~VPPhiAccessors()=default
void printPhiOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const
Print the recipe.
iterator_range< mapped_iterator< detail::index_iterator, std::function< const VPBasicBlock *(size_t)> > > const_incoming_blocks_range
Definition VPlan.h:1347
const_incoming_blocks_range incoming_blocks() const
Returns an iterator range over the incoming blocks.
Definition VPlan.h:1351
~VPPredInstPHIRecipe() override=default
bool usesScalars(const VPValue *Op) const override
Returns true if the recipe uses scalars of operand Op.
Definition VPlan.h:3236
VPPredInstPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3218
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPPredInstPHIRecipe.
Definition VPlan.h:3229
VPPredInstPHIRecipe(VPValue *PredV, DebugLoc DL)
Construct a VPPredInstPHIRecipe given PredInst whose value needs a phi nodes after merging back from ...
Definition VPlan.h:3214
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:387
bool mayReadFromMemory() const
Returns true if the recipe may read from memory.
bool mayReadOrWriteMemory() const
Returns true if the recipe may read from or write to memory.
Definition VPlan.h:474
virtual void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const =0
Each concrete VPRecipe prints itself, without printing common information, like debug info or metadat...
VPRegionBlock * getRegion()
Definition VPlan.h:4276
void setDebugLoc(DebugLoc NewDL)
Set the recipe's debug location to NewDL.
Definition VPlan.h:485
bool mayWriteToMemory() const
Returns true if the recipe may write to memory.
~VPRecipeBase() override=default
VPBasicBlock * getParent()
Definition VPlan.h:408
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:479
virtual void execute(VPTransformState &State)=0
The method which generates the output IR instructions that correspond to this VPRecipe,...
void moveBefore(VPBasicBlock &BB, iplist< VPRecipeBase >::iterator I)
Unlink this recipe and insert into BB before I.
void insertBefore(VPRecipeBase *InsertPos)
Insert an unlinked recipe into a basic block immediately before the specified recipe.
void insertAfter(VPRecipeBase *InsertPos)
Insert an unlinked Recipe into a basic block immediately after the specified Recipe.
static bool classof(const VPDef *D)
Method to support type inquiry through isa, cast, and dyn_cast.
Definition VPlan.h:454
iplist< VPRecipeBase >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
virtual VPRecipeBase * clone()=0
Clone the current recipe.
friend class VPBlockUtils
Definition VPlan.h:389
const VPBasicBlock * getParent() const
Definition VPlan.h:409
InstructionCost cost(ElementCount VF, VPCostContext &Ctx)
Return the cost of this recipe, taking into account if the cost computation should be skipped and the...
static bool classof(const VPUser *U)
Definition VPlan.h:459
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
void moveAfter(VPRecipeBase *MovePos)
Unlink this recipe from its current VPBasicBlock and insert it into the VPBasicBlock that MovePos liv...
VPRecipeBase(const unsigned char SC, ArrayRef< VPValue * > Operands, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:398
VPValue * getEVL() const
The VPValue of the explicit vector length.
Definition VPlan.h:2920
VPReductionEVLRecipe(VPReductionRecipe &R, VPValue &EVL, VPValue *CondOp, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:2899
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2923
VPReductionEVLRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2910
~VPReductionEVLRecipe() override=default
bool isOrdered() const
Returns true, if the phi is part of an ordered reduction.
Definition VPlan.h:2486
VPReductionPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2458
unsigned getVFScaleFactor() const
Get the factor that the VF of this recipe's output should be scaled by, or 1 if it isn't scaled.
Definition VPlan.h:2472
~VPReductionPHIRecipe() override=default
bool hasUsesOutsideReductionChain() const
Returns true, if the phi is part of a multi-use reduction.
Definition VPlan.h:2498
unsigned getNumIncoming() const override
Returns the number of incoming values, also number of incoming blocks.
Definition VPlan.h:2480
bool isInLoop() const
Returns true if the phi is part of an in-loop reduction.
Definition VPlan.h:2489
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2503
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPReductionPHIRecipe(PHINode *Phi, RecurKind Kind, VPValue &Start, VPValue &BackedgeValue, ReductionStyle Style, bool HasUsesOutsideReductionChain=false)
Create a new VPReductionPHIRecipe for the reduction Phi.
Definition VPlan.h:2447
void execute(VPTransformState &State) override
Generate the phi/select nodes.
bool isPartialReduction() const
Returns true if the reduction outputs a vector with a scaled down VF.
Definition VPlan.h:2495
RecurKind getRecurrenceKind() const
Returns the recurrence kind of the reduction.
Definition VPlan.h:2483
A recipe to represent inloop, ordered or partial reduction operations.
Definition VPlan.h:2786
VPReductionRecipe(const unsigned char SC, RecurKind RdxKind, FastMathFlags FMFs, Instruction *I, ArrayRef< VPValue * > Operands, VPValue *CondOp, ReductionStyle Style, DebugLoc DL)
Definition VPlan.h:2795
bool isConditional() const
Return true if the in-loop reduction is conditional.
Definition VPlan.h:2862
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:2831
static bool classof(const VPSingleDefRecipe *R)
Definition VPlan.h:2846
VPValue * getVecOp() const
The VPValue of the vector value to be reduced.
Definition VPlan.h:2873
VPValue * getCondOp() const
The VPValue of the condition for the block.
Definition VPlan.h:2875
RecurKind getRecurrenceKind() const
Return the recurrence kind for the in-loop reduction.
Definition VPlan.h:2858
VPReductionRecipe(RecurKind RdxKind, FastMathFlags FMFs, Instruction *I, VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp, ReductionStyle Style, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:2809
bool isOrdered() const
Return true if the in-loop reduction is ordered.
Definition VPlan.h:2860
VPReductionRecipe(const RecurKind RdxKind, FastMathFlags FMFs, VPValue *ChainOp, VPValue *VecOp, VPValue *CondOp, ReductionStyle Style, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:2816
bool isPartialReduction() const
Returns true if the reduction outputs a vector with a scaled down VF.
Definition VPlan.h:2864
~VPReductionRecipe() override=default
VPValue * getChainOp() const
The VPValue of the scalar Chain being accumulated.
Definition VPlan.h:2871
bool isInLoop() const
Returns true if the reduction is in-loop.
Definition VPlan.h:2866
VPReductionRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2825
static bool classof(const VPUser *U)
Definition VPlan.h:2836
static bool classof(const VPValue *VPV)
Definition VPlan.h:2841
unsigned getVFScaleFactor() const
Get the factor that the VF of this recipe's output should be scaled by, or 1 if it isn't scaled.
Definition VPlan.h:2880
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4159
const VPBlockBase * getEntry() const
Definition VPlan.h:4195
Type * getCanonicalIVType()
Return the type of the canonical IV for loop regions.
Definition VPlan.h:4270
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4227
~VPRegionBlock() override=default
void setExiting(VPBlockBase *ExitingBlock)
Set ExitingBlock as the exiting VPBlockBase of this VPRegionBlock.
Definition VPlan.h:4212
VPBlockBase * getExiting()
Definition VPlan.h:4208
VPCanonicalIVPHIRecipe * getCanonicalIV()
Returns the canonical induction recipe of the region.
Definition VPlan.h:4257
void setEntry(VPBlockBase *EntryBlock)
Set EntryBlock as the entry VPBlockBase of this VPRegionBlock.
Definition VPlan.h:4200
const Type * getCanonicalIVType() const
Definition VPlan.h:4271
const VPBlockBase * getExiting() const
Definition VPlan.h:4207
VPBlockBase * getEntry()
Definition VPlan.h:4196
const VPCanonicalIVPHIRecipe * getCanonicalIV() const
Definition VPlan.h:4265
VPBasicBlock * getPreheaderVPBB()
Returns the pre-header VPBasicBlock of the loop region.
Definition VPlan.h:4220
friend class VPlan
Definition VPlan.h:4160
static bool classof(const VPBlockBase *V)
Method to support type inquiry through isa, cast, and dyn_cast.
Definition VPlan.h:4191
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:2942
bool isSingleScalar() const
Definition VPlan.h:2983
VPReplicateRecipe(Instruction *I, ArrayRef< VPValue * > Operands, bool IsSingleScalar, VPValue *Mask=nullptr, const VPIRFlags &Flags={}, VPIRMetadata Metadata={}, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:2950
~VPReplicateRecipe() override=default
bool usesScalars(const VPValue *Op) const override
Returns true if the recipe uses scalars of operand Op.
Definition VPlan.h:2995
bool isPredicated() const
Definition VPlan.h:2985
VPReplicateRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2964
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2988
unsigned getOpcode() const
Definition VPlan.h:3012
VPValue * getMask()
Return the mask of a predicated VPReplicateRecipe.
Definition VPlan.h:3007
VPValue * getStepValue() const
Definition VPlan.h:3837
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPScalarIVStepsRecipe.
Definition VPlan.h:3831
VPScalarIVStepsRecipe(const InductionDescriptor &IndDesc, VPValue *IV, VPValue *Step, VPValue *VF, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:3802
bool isPart0() const
Return true if this VPScalarIVStepsRecipe corresponds to part 0.
Definition VPlan.h:3823
VPScalarIVStepsRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3814
VPScalarIVStepsRecipe(VPValue *IV, VPValue *Step, VPValue *VF, Instruction::BinaryOps Opcode, FastMathFlags FMFs, DebugLoc DL)
Definition VPlan.h:3795
~VPScalarIVStepsRecipe() override=default
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3840
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:531
VPSingleDefRecipe(const unsigned char SC, ArrayRef< VPValue * > Operands, Value *UV, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:537
Instruction * getUnderlyingInstr()
Returns the underlying instruction.
Definition VPlan.h:595
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:541
const Instruction * getUnderlyingInstr() const
Definition VPlan.h:598
static bool classof(const VPUser *U)
Definition VPlan.h:587
LLVM_ABI_FOR_TEST LLVM_DUMP_METHOD void dump() const
Print this VPSingleDefRecipe to dbgs() (for debugging).
VPSingleDefRecipe * clone() override=0
Clone the current recipe.
VPSingleDefRecipe(const unsigned char SC, ArrayRef< VPValue * > Operands, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:533
This class can be used to assign names to VPValues.
Helper to access the operand that contains the unroll part for this recipe after unrolling.
Definition VPlan.h:970
VPValue * getUnrollPartOperand(const VPUser &U) const
Return the VPValue operand containing the unroll part or null if there is no such operand.
unsigned getUnrollPart(const VPUser &U) const
Return the unroll part.
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:202
void printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const
Print the operands to O.
Definition VPlan.cpp:1420
operand_range operands()
Definition VPlanValue.h:270
void setOperand(unsigned I, VPValue *New)
Definition VPlanValue.h:246
unsigned getNumOperands() const
Definition VPlanValue.h:240
operand_iterator op_end()
Definition VPlanValue.h:268
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:241
VPUser(ArrayRef< VPValue * > Operands)
Definition VPlanValue.h:221
iterator_range< const_operand_iterator > const_operand_range
Definition VPlanValue.h:264
iterator_range< operand_iterator > operand_range
Definition VPlanValue.h:263
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:46
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:131
friend class VPExpressionRecipe
Definition VPlanValue.h:51
Value * getLiveInIRValue() const
Returns the underlying IR value, if this VPValue is defined outside the scope of VPlan.
Definition VPlanValue.h:181
friend class VPDef
Definition VPlanValue.h:47
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
Definition VPlanValue.h:83
VPValue(const unsigned char SC, Value *UV=nullptr, VPDef *Def=nullptr)
Definition VPlan.cpp:94
void setUnderlyingValue(Value *Val)
Definition VPlanValue.h:191
unsigned getNumUsers() const
Definition VPlanValue.h:111
bool isLiveIn() const
Returns true if this VPValue is a live-in, i.e. defined outside the VPlan.
Definition VPlanValue.h:176
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the VPUser only uses the first lane of operand Op.
Definition VPlan.h:1934
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPVectorEndPointerRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1955
const VPValue * getVFValue() const
Definition VPlan.h:1930
bool usesFirstPartOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first part of operand Op.
Definition VPlan.h:1948
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPVectorPointerRecipe.
Definition VPlan.h:1941
VPVectorEndPointerRecipe(VPValue *Ptr, VPValue *VF, Type *IndexedTy, int64_t Stride, GEPNoWrapFlags GEPFlags, DebugLoc DL)
Definition VPlan.h:1919
Type * getSourceElementType() const
Definition VPlan.h:1989
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the VPUser only uses the first lane of operand Op.
Definition VPlan.h:1991
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
void execute(VPTransformState &State) override
The method which generates the output IR instructions that correspond to this VPRecipe,...
bool usesFirstPartOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first part of operand Op.
Definition VPlan.h:1998
VPVectorPointerRecipe(VPValue *Ptr, Type *SourceElementTy, GEPNoWrapFlags GEPFlags, DebugLoc DL)
Definition VPlan.h:1976
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPHeaderPHIRecipe.
Definition VPlan.h:2014
VPVectorPointerRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2005
A recipe for widening Call instructions using library calls.
Definition VPlan.h:1702
VPWidenCallRecipe(Value *UV, Function *Variant, ArrayRef< VPValue * > CallArguments, const VPIRFlags &Flags={}, const VPIRMetadata &Metadata={}, DebugLoc DL={})
Definition VPlan.h:1709
const_operand_range args() const
Definition VPlan.h:1742
VPWidenCallRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1723
operand_range args()
Definition VPlan.h:1741
Function * getCalledScalarFunction() const
Definition VPlan.h:1737
~VPWidenCallRecipe() override=default
void execute(VPTransformState &State) override
Generate a canonical vector induction variable of the vector loop, with start = {<Part*VF,...
~VPWidenCanonicalIVRecipe() override=default
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenCanonicalIVPHIRecipe.
Definition VPlan.h:3704
VPWidenCanonicalIVRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3691
VPWidenCanonicalIVRecipe(VPCanonicalIVPHIRecipe *CanonicalIV)
Definition VPlan.h:3686
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1552
Instruction::CastOps getOpcode() const
Definition VPlan.h:1588
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
Type * getResultType() const
Returns the result type of the cast.
Definition VPlan.h:1591
LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override
Produce widened copies of the cast.
~VPWidenCastRecipe() override=default
LLVM_ABI_FOR_TEST InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenCastRecipe.
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy, CastInst *CI=nullptr, const VPIRFlags &Flags={}, const VPIRMetadata &Metadata={}, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:1560
VPWidenCastRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1573
unsigned getOpcode() const
This recipe generates a GEP instruction.
Definition VPlan.h:1882
Type * getSourceElementType() const
Definition VPlan.h:1887
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenGEPRecipe.
Definition VPlan.h:1890
VPWidenGEPRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1874
~VPWidenGEPRecipe() override=default
VPWidenGEPRecipe(GetElementPtrInst *GEP, ArrayRef< VPValue * > Operands, const VPIRFlags &Flags={}, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:1860
void execute(VPTransformState &State) override=0
Generate the phi nodes.
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2181
static bool classof(const VPValue *V)
Definition VPlan.h:2135
void setStepValue(VPValue *V)
Update the step value of the recipe.
Definition VPlan.h:2151
VPValue * getBackedgeValue() override
Returns the incoming value from the loop backedge.
Definition VPlan.h:2166
unsigned getNumIncoming() const override
Returns the number of incoming values, also number of incoming blocks.
Definition VPlan.h:2159
PHINode * getPHINode() const
Definition VPlan.h:2161
VPWidenInductionRecipe(unsigned char Kind, PHINode *IV, VPValue *Start, VPValue *Step, const InductionDescriptor &IndDesc, DebugLoc DL)
Definition VPlan.h:2123
VPValue * getStepValue()
Returns the step value of the induction.
Definition VPlan.h:2147
const InductionDescriptor & getInductionDescriptor() const
Returns the induction descriptor for the recipe.
Definition VPlan.h:2164
VPRecipeBase & getBackedgeRecipe() override
Returns the backedge value as a recipe.
Definition VPlan.h:2173
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:2130
const VPValue * getVFValue() const
Definition VPlan.h:2154
static bool classof(const VPSingleDefRecipe *R)
Definition VPlan.h:2140
const VPValue * getStepValue() const
Definition VPlan.h:2148
const TruncInst * getTruncInst() const
Definition VPlan.h:2255
void execute(VPTransformState &State) override
Generate the phi nodes.
Definition VPlan.h:2236
~VPWidenIntOrFpInductionRecipe() override=default
VPWidenIntOrFpInductionRecipe(PHINode *IV, VPValue *Start, VPValue *Step, VPValue *VF, const InductionDescriptor &IndDesc, TruncInst *Trunc, const VPIRFlags &Flags, DebugLoc DL)
Definition VPlan.h:2211
VPWidenIntOrFpInductionRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2228
TruncInst * getTruncInst()
Returns the first defined value as TruncInst, if it is one or nullptr otherwise.
Definition VPlan.h:2254
VPWidenIntOrFpInductionRecipe(PHINode *IV, VPValue *Start, VPValue *Step, VPValue *VF, const InductionDescriptor &IndDesc, const VPIRFlags &Flags, DebugLoc DL)
Definition VPlan.h:2202
VPValue * getLastUnrolledPartOperand()
Returns the VPValue representing the value of this induction at the last unrolled part,...
Definition VPlan.h:2271
unsigned getNumIncoming() const override
Returns the number of incoming values, also number of incoming blocks.
Definition VPlan.h:2250
Type * getScalarType() const
Returns the scalar type of the induction.
Definition VPlan.h:2263
bool isCanonical() const
Returns true if the induction is canonical, i.e.
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
A recipe for widening vector intrinsics.
Definition VPlan.h:1602
VPWidenIntrinsicRecipe(Intrinsic::ID VectorIntrinsicID, ArrayRef< VPValue * > CallArguments, Type *Ty, const VPIRFlags &Flags={}, const VPIRMetadata &Metadata={}, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:1633
Intrinsic::ID getVectorIntrinsicID() const
Return the ID of the intrinsic.
Definition VPlan.h:1673
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
bool mayReadFromMemory() const
Returns true if the intrinsic may read from memory.
Definition VPlan.h:1682
StringRef getIntrinsicName() const
Return to name of the intrinsic as string.
VPWidenIntrinsicRecipe(CallInst &CI, Intrinsic::ID VectorIntrinsicID, ArrayRef< VPValue * > CallArguments, Type *Ty, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:1619
bool mayHaveSideEffects() const
Returns true if the intrinsic may have side-effects.
Definition VPlan.h:1688
VPWidenIntrinsicRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1654
bool mayWriteToMemory() const
Returns true if the intrinsic may write to memory.
Definition VPlan.h:1685
~VPWidenIntrinsicRecipe() override=default
LLVM_ABI_FOR_TEST bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the VPUser only uses the first lane of operand Op.
Type * getResultType() const
Return the scalar return type of the intrinsic.
Definition VPlan.h:1676
LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override
Produce a widened version of the vector intrinsic.
LLVM_ABI_FOR_TEST InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this vector intrinsic.
bool IsMasked
Whether the memory access is masked.
Definition VPlan.h:3267
bool Reverse
Whether the consecutive accessed addresses are in reverse order.
Definition VPlan.h:3264
bool isConsecutive() const
Return whether the loaded-from / stored-to addresses are consecutive.
Definition VPlan.h:3307
static bool classof(const VPUser *U)
Definition VPlan.h:3301
void execute(VPTransformState &State) override
Generate the wide load/store.
Definition VPlan.h:3330
Instruction & Ingredient
Definition VPlan.h:3255
VPWidenMemoryRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3290
Instruction & getIngredient() const
Definition VPlan.h:3338
bool Consecutive
Whether the accessed addresses are consecutive.
Definition VPlan.h:3261
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:3294
VPValue * getMask() const
Return the mask used by this recipe.
Definition VPlan.h:3321
Align Alignment
Alignment information for this memory access.
Definition VPlan.h:3258
bool isMasked() const
Returns true if the recipe is masked.
Definition VPlan.h:3317
VPWidenMemoryRecipe(const char unsigned SC, Instruction &I, std::initializer_list< VPValue * > Operands, bool Consecutive, bool Reverse, const VPIRMetadata &Metadata, DebugLoc DL)
Definition VPlan.h:3277
void setMask(VPValue *Mask)
Definition VPlan.h:3269
Align getAlign() const
Returns the alignment of the memory access.
Definition VPlan.h:3327
VPValue * getAddr() const
Return the address accessed by this recipe.
Definition VPlan.h:3314
bool isReverse() const
Return whether the consecutive loaded/stored addresses are in reverse order.
Definition VPlan.h:3311
const VPRecipeBase * getAsRecipe() const override
Return a VPRecipeBase* to the current object.
Definition VPlan.h:2365
VPWidenPHIRecipe(PHINode *Phi, VPValue *Start=nullptr, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Create a new VPWidenPHIRecipe for Phi with start value Start and debug location DL.
Definition VPlan.h:2336
VPWidenPHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2343
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
~VPWidenPHIRecipe() override=default
void execute(VPTransformState &State) override
Generate the phi/select nodes.
VPWidenPointerInductionRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2298
~VPWidenPointerInductionRecipe() override=default
bool onlyScalarsGenerated(bool IsScalable)
Returns true if only scalar values will be generated.
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
void execute(VPTransformState &State) override
Generate vector values for the pointer induction.
Definition VPlan.h:2307
VPWidenPointerInductionRecipe(PHINode *Phi, VPValue *Start, VPValue *Step, VPValue *NumUnrolledElems, const InductionDescriptor &IndDesc, DebugLoc DL)
Create a new VPWidenPointerInductionRecipe for Phi with start value Start and the number of elements ...
Definition VPlan.h:2288
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1512
VPWidenRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1526
VPWidenRecipe(Instruction &I, ArrayRef< VPValue * > Operands, const VPIRFlags &Flags={}, const VPIRMetadata &Metadata={}, DebugLoc DL={})
Definition VPlan.h:1516
~VPWidenRecipe() override=default
unsigned getOpcode() const
Definition VPlan.h:1541
Class that maps (parts of) an existing VPlan to trees of combined VPInstructions.
Definition VPlanSLP.h:74
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4289
LLVM_ABI_FOR_TEST void printDOT(raw_ostream &O) const
Print this VPlan in DOT format to O.
Definition VPlan.cpp:1106
friend class VPSlotTracker
Definition VPlan.h:4291
std::string getName() const
Return a string with the name of the plan and the applicable VFs and UFs.
Definition VPlan.cpp:1082
bool hasVF(ElementCount VF) const
Definition VPlan.h:4490
LLVMContext & getContext() const
Definition VPlan.h:4478
VPBasicBlock * getEntry()
Definition VPlan.h:4378
VPValue & getVectorTripCount()
The vector trip count.
Definition VPlan.h:4469
void setName(const Twine &newName)
Definition VPlan.h:4528
bool hasScalableVF() const
Definition VPlan.h:4491
VPValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:4476
VPValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:4472
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4440
VPValue * getTrue()
Return a VPValue wrapping i1 true.
Definition VPlan.h:4546
VPValue * getOrCreateBackedgeTakenCount()
The backedge taken count of the original loop.
Definition VPlan.h:4461
iterator_range< SmallSetVector< ElementCount, 2 >::iterator > vectorFactors() const
Returns an iterator range over all VFs of the plan.
Definition VPlan.h:4497
VPIRBasicBlock * getExitBlock(BasicBlock *IRBB) const
Return the VPIRBasicBlock corresponding to IRBB.
Definition VPlan.cpp:890
LLVM_ABI_FOR_TEST ~VPlan()
Definition VPlan.cpp:868
const VPValue & getVF() const
Definition VPlan.h:4473
bool isExitBlock(VPBlockBase *VPBB)
Returns true if VPBB is an exit block.
Definition VPlan.cpp:898
const VPBasicBlock * getEntry() const
Definition VPlan.h:4379
friend class VPlanPrinter
Definition VPlan.h:4290
VPValue * getConstantInt(const APInt &Val)
Return a VPValue wrapping a ConstantInt with the given APInt value.
Definition VPlan.h:4563
unsigned getUF() const
Definition VPlan.h:4510
VPRegionBlock * createReplicateRegion(VPBlockBase *Entry, VPBlockBase *Exiting, const std::string &Name="")
Create a new replicate region with Entry, Exiting and Name.
Definition VPlan.h:4616
VPIRBasicBlock * createEmptyVPIRBasicBlock(BasicBlock *IRBB)
Create a VPIRBasicBlock wrapping IRBB, but do not create VPIRInstructions wrapping the instructions i...
Definition VPlan.cpp:1220
auto getLiveIns() const
Return the list of live-in VPValues available in the VPlan.
Definition VPlan.h:4571
bool hasUF(unsigned UF) const
Definition VPlan.h:4508
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:4430
VPValue * getConstantInt(Type *Ty, uint64_t Val, bool IsSigned=false)
Return a VPValue wrapping a ConstantInt with the given type and value.
Definition VPlan.h:4552
VPValue * getBackedgeTakenCount() const
Definition VPlan.h:4466
void setVF(ElementCount VF)
Definition VPlan.h:4484
bool isUnrolled() const
Returns true if the VPlan already has been unrolled, i.e.
Definition VPlan.h:4523
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1011
bool hasEarlyExit() const
Returns true if the VPlan is based on a loop with an early exit.
Definition VPlan.h:4638
InstructionCost cost(ElementCount VF, VPCostContext &Ctx)
Return the cost of this plan.
Definition VPlan.cpp:993
const VPBasicBlock * getMiddleBlock() const
Definition VPlan.h:4416
void setTripCount(VPValue *NewTripCount)
Set the trip count assuming it is currently null; if it is not - use resetTripCount().
Definition VPlan.h:4447
void resetTripCount(VPValue *NewTripCount)
Resets the trip count for the VPlan.
Definition VPlan.h:4454
VPBasicBlock * getMiddleBlock()
Returns the 'middle' block of the plan, that is the block that selects whether to execute the scalar ...
Definition VPlan.h:4403
void setEntry(VPBasicBlock *VPBB)
Definition VPlan.h:4367
VPBasicBlock * createVPBasicBlock(const Twine &Name, VPRecipeBase *Recipe=nullptr)
Create a new VPBasicBlock with Name and containing Recipe if present.
Definition VPlan.h:4594
LLVM_ABI_FOR_TEST VPIRBasicBlock * createVPIRBasicBlock(BasicBlock *IRBB)
Create a VPIRBasicBlock from IRBB containing VPIRInstructions for all instructions in IRBB,...
Definition VPlan.cpp:1226
VPValue * getFalse()
Return a VPValue wrapping i1 false.
Definition VPlan.h:4549
VPValue * getOrAddLiveIn(Value *V)
Gets the live-in VPValue for V or adds a new live-in (if none exists yet) for V.
Definition VPlan.h:4532
VPRegionBlock * createLoopRegion(const std::string &Name="", VPBlockBase *Entry=nullptr, VPBlockBase *Exiting=nullptr)
Create a new loop region with Name and entry and exiting blocks set to Entry and Exiting respectively...
Definition VPlan.h:4604
LLVM_DUMP_METHOD void dump() const
Dump the plan to stderr (for debugging).
Definition VPlan.cpp:1112
bool hasScalarVFOnly() const
Definition VPlan.h:4501
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:4421
void execute(VPTransformState *State)
Generate the IR code for this VPlan.
Definition VPlan.cpp:905
LLVM_ABI_FOR_TEST void print(raw_ostream &O) const
Print this VPlan to O.
Definition VPlan.cpp:1065
void addVF(ElementCount VF)
Definition VPlan.h:4482
VPIRBasicBlock * getScalarHeader() const
Return the VPIRBasicBlock wrapping the header of the scalar loop.
Definition VPlan.h:4426
VPValue * getLiveIn(Value *V) const
Return the live-in VPValue for V, if there is one or nullptr otherwise.
Definition VPlan.h:4568
VPValue * getConstantInt(unsigned BitWidth, uint64_t Val, bool IsSigned=false)
Return a VPValue wrapping a ConstantInt with the given bitwidth and value.
Definition VPlan.h:4557
void printLiveIns(raw_ostream &O) const
Print the live-ins of this VPlan to O.
Definition VPlan.cpp:1027
VPBasicBlock * getVectorPreheader()
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4383
void setUF(unsigned UF)
Definition VPlan.h:4515
bool hasScalarTail() const
Returns true if the scalar tail may execute after the vector loop.
Definition VPlan.h:4648
LLVM_ABI_FOR_TEST VPlan * duplicate()
Clone the current VPlan, update all VPValues of the new VPlan and cloned recipes to refer to the clon...
Definition VPlan.cpp:1153
VPlan(BasicBlock *ScalarHeaderBB)
Construct a VPlan with a new VPBasicBlock as entry, a VPIRBasicBlock wrapping ScalarHeaderBB and a tr...
Definition VPlan.h:4360
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
Increasing range of size_t indices.
Definition STLExtras.h:2447
typename base_list_type::const_reverse_iterator const_reverse_iterator
Definition ilist.h:124
typename base_list_type::reverse_iterator reverse_iterator
Definition ilist.h:123
typename base_list_type::const_iterator const_iterator
Definition ilist.h:122
An intrusive list with ownership and callbacks specified/controlled by ilist_traits,...
Definition ilist.h:328
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This file defines classes to implement an intrusive doubly linked list class (i.e.
This file defines the ilist_node class template, which is a convenient base class for creating classe...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
LLVM_ABI AttributeSet getFnAttributes(LLVMContext &C, ID id)
Return the function attributes for an intrinsic.
std::variant< std::monostate, Loc::Single, Loc::Multi, Loc::MMI, Loc::EntryValue > Variant
Alias for the std::variant specialization base class of DbgVariable.
Definition DwarfDebug.h:189
static auto castToVPIRMetadata(RecipeBasePtrTy R) -> DstTy
Definition VPlan.h:3903
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:316
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
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:829
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1763
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:839
ReductionStyle getReductionStyle(bool InLoop, bool Ordered, unsigned ScaleFactor)
Definition VPlan.h:2419
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2494
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void getMetadataToPropagate(Instruction *Inst, SmallVectorImpl< std::pair< unsigned, MDNode * > > &Metadata)
Add metadata from Inst to Metadata, if it can be preserved after vectorization.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto cast_or_null(const Y &Val)
Definition Casting.h:714
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:301
auto map_range(ContainerTy &&C, FuncTy F)
Definition STLExtras.h:364
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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:1744
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
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:547
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
Definition STLExtras.h:323
@ Other
Any other memory.
Definition ModRef.h:68
RecurKind
These are the kinds of recurrences that we support.
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:1966
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
constexpr unsigned BitWidth
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:1973
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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:1770
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1909
std::variant< RdxOrdered, RdxInLoop, RdxUnordered > ReductionStyle
Definition VPlan.h:2417
std::unique_ptr< VPlan > VPlanPtr
Definition VPlan.h:77
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Support casting from VPRecipeBase -> VPIRMetadata, by down-casting to the recipe types implementing V...
Definition VPlan.h:3936
static RetTy doCast(SrcTy R)
doCast is used by cast<>.
Definition VPlan.h:3950
static RetTy doCastIfPossible(SrcTy R)
doCastIfPossible is used by dyn_cast<>.
Definition VPlan.h:3955
static bool isPossible(SrcTy R)
Definition VPlan.h:3937
Support casting from VPRecipeBase -> VPPhiAccessors, by down-casting to the recipe types implementing...
Definition VPlan.h:3865
static VPPhiAccessors * doCastIfPossible(SrcTy f)
doCastIfPossible is used by dyn_cast<>.
Definition VPlan.h:3886
CastInfo< VPPhiAccessors, SrcTy > Self
Definition VPlan.h:3867
static VPPhiAccessors * doCast(SrcTy R)
doCast is used by cast<>.
Definition VPlan.h:3870
This struct provides a method for customizing the way a cast is performed.
Definition Casting.h:476
static bool isPossible(const VPRecipeBase *f)
Definition VPlan.h:3857
This struct provides a way to check if a given cast is possible.
Definition Casting.h:253
static bool isPossible(const SrcTy &f)
Definition Casting.h:254
This reduction is in-loop.
Definition VPlan.h:2411
Possible variants of a reduction.
Definition VPlan.h:2409
This reduction is unordered with the partial result scaled down by some factor.
Definition VPlan.h:2414
unsigned VFScaleFactor
Definition VPlan.h:2415
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:276
Struct to hold various analysis needed for cost computations.
void execute(VPTransformState &State) override
Generate the phi nodes.
VPFirstOrderRecurrencePHIRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:2380
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this first-order recurrence phi recipe.
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:2392
void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
VPFirstOrderRecurrencePHIRecipe(PHINode *Phi, VPValue &Start, VPValue &BackedgeValue)
Definition VPlan.h:2372
DisjointFlagsTy(bool IsDisjoint)
Definition VPlan.h:640
NonNegFlagsTy(bool IsNonNeg)
Definition VPlan.h:645
TruncFlagsTy(bool HasNUW, bool HasNSW)
Definition VPlan.h:635
WrapFlagsTy(bool HasNUW, bool HasNSW)
Definition VPlan.h:628
PHINode & getIRPhi()
Definition VPlan.h:1493
VPIRPhi(PHINode &PN)
Definition VPlan.h:1486
static bool classof(const VPRecipeBase *U)
Definition VPlan.h:1488
const VPRecipeBase * getAsRecipe() const override
Return a VPRecipeBase* to the current object.
Definition VPlan.h:1504
static bool classof(const VPUser *U)
Definition VPlan.h:1380
VPPhi * clone() override
Clone the current recipe.
Definition VPlan.h:1395
const VPRecipeBase * getAsRecipe() const override
Return a VPRecipeBase* to the current object.
Definition VPlan.h:1410
VPPhi(ArrayRef< VPValue * > Operands, DebugLoc DL, const Twine &Name="")
Definition VPlan.h:1377
static bool classof(const VPSingleDefRecipe *SDR)
Definition VPlan.h:1390
static bool classof(const VPValue *V)
Definition VPlan.h:1385
A pure-virtual common base class for recipes defining a single VPValue and using IR flags.
Definition VPlan.h:923
static bool classof(const VPRecipeBase *R)
Definition VPlan.h:929
InstructionCost getCostForRecipeWithOpcode(unsigned Opcode, ElementCount VF, VPCostContext &Ctx) const
Compute the cost for this recipe for VF, using Opcode and Ctx.
VPRecipeWithIRFlags(const unsigned char SC, ArrayRef< VPValue * > Operands, const VPIRFlags &Flags, DebugLoc DL=DebugLoc::getUnknown())
Definition VPlan.h:924
static bool classof(const VPValue *V)
Definition VPlan.h:949
static bool classof(const VPSingleDefRecipe *U)
Definition VPlan.h:956
void execute(VPTransformState &State) override=0
The method which generates the output IR instructions that correspond to this VPRecipe,...
VPRecipeWithIRFlags * clone() override=0
Clone the current recipe.
static bool classof(const VPUser *U)
Definition VPlan.h:944
VPTransformState holds information passed down when "executing" a VPlan, needed for generating the ou...
A recipe for widening load operations with vector-predication intrinsics, using the address to load f...
Definition VPlan.h:3385
LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override
Generate the wide load or gather.
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
LLVM_ABI_FOR_TEST InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenLoadEVLRecipe.
VPValue * getEVL() const
Return the EVL operand.
Definition VPlan.h:3398
VPWidenLoadEVLRecipe(VPWidenLoadRecipe &L, VPValue *Addr, VPValue &EVL, VPValue *Mask)
Definition VPlan.h:3386
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3408
A recipe for widening load operations, using the address to load from and an optional mask.
Definition VPlan.h:3344
VP_CLASSOF_IMPL(VPDef::VPWidenLoadSC)
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3366
void execute(VPTransformState &State) override
Generate a wide load or gather.
VPWidenLoadRecipe(LoadInst &Load, VPValue *Addr, VPValue *Mask, bool Consecutive, bool Reverse, const VPIRMetadata &Metadata, DebugLoc DL)
Definition VPlan.h:3345
VPWidenLoadRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3354
A recipe for widening select instructions.
Definition VPlan.h:1801
VPWidenSelectRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:1812
VPWidenSelectRecipe(SelectInst *SI, ArrayRef< VPValue * > Operands, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL={})
Definition VPlan.h:1802
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:1833
VPValue * getCond() const
Definition VPlan.h:1828
unsigned getOpcode() const
Definition VPlan.h:1826
~VPWidenSelectRecipe() override=default
A recipe for widening store operations with vector-predication intrinsics, using the value to store,...
Definition VPlan.h:3469
VPValue * getStoredValue() const
Return the address accessed by this recipe.
Definition VPlan.h:3481
LLVM_ABI_FOR_TEST void execute(VPTransformState &State) override
Generate the wide store or scatter.
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3494
VPWidenStoreEVLRecipe(VPWidenStoreRecipe &S, VPValue *Addr, VPValue &EVL, VPValue *Mask)
Definition VPlan.h:3470
LLVM_ABI_FOR_TEST void printRecipe(raw_ostream &O, const Twine &Indent, VPSlotTracker &SlotTracker) const override
Print the recipe.
LLVM_ABI_FOR_TEST InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const override
Return the cost of this VPWidenStoreEVLRecipe.
VPValue * getEVL() const
Return the EVL operand.
Definition VPlan.h:3484
A recipe for widening store operations, using the stored value, the address to store to and an option...
Definition VPlan.h:3426
VP_CLASSOF_IMPL(VPDef::VPWidenStoreSC)
VPValue * getStoredValue() const
Return the value stored by this recipe.
Definition VPlan.h:3444
VPWidenStoreRecipe * clone() override
Clone the current recipe.
Definition VPlan.h:3435
bool usesFirstLaneOnly(const VPValue *Op) const override
Returns true if the recipe only uses the first lane of operand Op.
Definition VPlan.h:3450
VPWidenStoreRecipe(StoreInst &Store, VPValue *Addr, VPValue *StoredVal, VPValue *Mask, bool Consecutive, bool Reverse, const VPIRMetadata &Metadata, DebugLoc DL)
Definition VPlan.h:3427