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