1 //===- LoopVectorizationPlanner.h - Planner for LoopVectorization ---------===// 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 provides a LoopVectorizationPlanner class. 11 /// InnerLoopVectorizer vectorizes loops which contain only one basic 12 /// LoopVectorizationPlanner - drives the vectorization process after having 13 /// passed Legality checks. 14 /// The planner builds and optimizes the Vectorization Plans which record the 15 /// decisions how to vectorize the given loop. In particular, represent the 16 /// control-flow of the vectorized version, the replication of instructions that 17 /// are to be scalarized, and interleave access groups. 18 /// 19 /// Also provides a VPlan-based builder utility analogous to IRBuilder. 20 /// It provides an instruction-level API for generating VPInstructions while 21 /// abstracting away the Recipe manipulation details. 22 //===----------------------------------------------------------------------===// 23 24 #ifndef LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONPLANNER_H 25 #define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONPLANNER_H 26 27 #include "VPlan.h" 28 #include "llvm/ADT/SmallSet.h" 29 #include "llvm/Support/InstructionCost.h" 30 31 namespace { 32 class GeneratedRTChecks; 33 } 34 35 namespace llvm { 36 37 class LoopInfo; 38 class DominatorTree; 39 class LoopVectorizationLegality; 40 class LoopVectorizationCostModel; 41 class PredicatedScalarEvolution; 42 class LoopVectorizeHints; 43 class LoopVersioning; 44 class OptimizationRemarkEmitter; 45 class TargetTransformInfo; 46 class TargetLibraryInfo; 47 class VPRecipeBuilder; 48 struct VFRange; 49 50 extern cl::opt<bool> EnableVPlanNativePath; 51 extern cl::opt<unsigned> ForceTargetInstructionCost; 52 53 /// VPlan-based builder utility analogous to IRBuilder. 54 class VPBuilder { 55 VPBasicBlock *BB = nullptr; 56 VPBasicBlock::iterator InsertPt = VPBasicBlock::iterator(); 57 58 /// Insert \p VPI in BB at InsertPt if BB is set. tryInsertInstruction(T * R)59 template <typename T> T *tryInsertInstruction(T *R) { 60 if (BB) 61 BB->insert(R, InsertPt); 62 return R; 63 } 64 65 VPInstruction *createInstruction(unsigned Opcode, 66 ArrayRef<VPValue *> Operands, DebugLoc DL, 67 const Twine &Name = "") { 68 return tryInsertInstruction(new VPInstruction(Opcode, Operands, DL, Name)); 69 } 70 71 public: 72 VPBuilder() = default; VPBuilder(VPBasicBlock * InsertBB)73 VPBuilder(VPBasicBlock *InsertBB) { setInsertPoint(InsertBB); } VPBuilder(VPRecipeBase * InsertPt)74 VPBuilder(VPRecipeBase *InsertPt) { setInsertPoint(InsertPt); } VPBuilder(VPBasicBlock * TheBB,VPBasicBlock::iterator IP)75 VPBuilder(VPBasicBlock *TheBB, VPBasicBlock::iterator IP) { 76 setInsertPoint(TheBB, IP); 77 } 78 79 /// Clear the insertion point: created instructions will not be inserted into 80 /// a block. clearInsertionPoint()81 void clearInsertionPoint() { 82 BB = nullptr; 83 InsertPt = VPBasicBlock::iterator(); 84 } 85 getInsertBlock()86 VPBasicBlock *getInsertBlock() const { return BB; } getInsertPoint()87 VPBasicBlock::iterator getInsertPoint() const { return InsertPt; } 88 89 /// Create a VPBuilder to insert after \p R. getToInsertAfter(VPRecipeBase * R)90 static VPBuilder getToInsertAfter(VPRecipeBase *R) { 91 VPBuilder B; 92 B.setInsertPoint(R->getParent(), std::next(R->getIterator())); 93 return B; 94 } 95 96 /// InsertPoint - A saved insertion point. 97 class VPInsertPoint { 98 VPBasicBlock *Block = nullptr; 99 VPBasicBlock::iterator Point; 100 101 public: 102 /// Creates a new insertion point which doesn't point to anything. 103 VPInsertPoint() = default; 104 105 /// Creates a new insertion point at the given location. VPInsertPoint(VPBasicBlock * InsertBlock,VPBasicBlock::iterator InsertPoint)106 VPInsertPoint(VPBasicBlock *InsertBlock, VPBasicBlock::iterator InsertPoint) 107 : Block(InsertBlock), Point(InsertPoint) {} 108 109 /// Returns true if this insert point is set. isSet()110 bool isSet() const { return Block != nullptr; } 111 getBlock()112 VPBasicBlock *getBlock() const { return Block; } getPoint()113 VPBasicBlock::iterator getPoint() const { return Point; } 114 }; 115 116 /// Sets the current insert point to a previously-saved location. restoreIP(VPInsertPoint IP)117 void restoreIP(VPInsertPoint IP) { 118 if (IP.isSet()) 119 setInsertPoint(IP.getBlock(), IP.getPoint()); 120 else 121 clearInsertionPoint(); 122 } 123 124 /// This specifies that created VPInstructions should be appended to the end 125 /// of the specified block. setInsertPoint(VPBasicBlock * TheBB)126 void setInsertPoint(VPBasicBlock *TheBB) { 127 assert(TheBB && "Attempting to set a null insert point"); 128 BB = TheBB; 129 InsertPt = BB->end(); 130 } 131 132 /// This specifies that created instructions should be inserted at the 133 /// specified point. setInsertPoint(VPBasicBlock * TheBB,VPBasicBlock::iterator IP)134 void setInsertPoint(VPBasicBlock *TheBB, VPBasicBlock::iterator IP) { 135 BB = TheBB; 136 InsertPt = IP; 137 } 138 139 /// This specifies that created instructions should be inserted at the 140 /// specified point. setInsertPoint(VPRecipeBase * IP)141 void setInsertPoint(VPRecipeBase *IP) { 142 BB = IP->getParent(); 143 InsertPt = IP->getIterator(); 144 } 145 146 /// Insert \p R at the current insertion point. insert(VPRecipeBase * R)147 void insert(VPRecipeBase *R) { BB->insert(R, InsertPt); } 148 149 /// Create an N-ary operation with \p Opcode, \p Operands and set \p Inst as 150 /// its underlying Instruction. 151 VPInstruction *createNaryOp(unsigned Opcode, ArrayRef<VPValue *> Operands, 152 Instruction *Inst = nullptr, 153 const Twine &Name = "") { 154 DebugLoc DL = DebugLoc::getUnknown(); 155 if (Inst) 156 DL = Inst->getDebugLoc(); 157 VPInstruction *NewVPInst = createInstruction(Opcode, Operands, DL, Name); 158 NewVPInst->setUnderlyingValue(Inst); 159 return NewVPInst; 160 } 161 VPInstruction *createNaryOp(unsigned Opcode, ArrayRef<VPValue *> Operands, 162 DebugLoc DL, const Twine &Name = "") { 163 return createInstruction(Opcode, Operands, DL, Name); 164 } 165 VPInstruction *createNaryOp(unsigned Opcode, ArrayRef<VPValue *> Operands, 166 const VPIRFlags &Flags, 167 DebugLoc DL = DebugLoc::getUnknown(), 168 const Twine &Name = "") { 169 return tryInsertInstruction( 170 new VPInstruction(Opcode, Operands, Flags, DL, Name)); 171 } 172 173 VPInstruction *createNaryOp(unsigned Opcode, 174 std::initializer_list<VPValue *> Operands, 175 Type *ResultTy, const VPIRFlags &Flags = {}, 176 DebugLoc DL = DebugLoc::getUnknown(), 177 const Twine &Name = "") { 178 return tryInsertInstruction( 179 new VPInstructionWithType(Opcode, Operands, ResultTy, Flags, DL, Name)); 180 } 181 182 VPInstruction *createOverflowingOp(unsigned Opcode, 183 std::initializer_list<VPValue *> Operands, 184 VPRecipeWithIRFlags::WrapFlagsTy WrapFlags, 185 DebugLoc DL = DebugLoc::getUnknown(), 186 const Twine &Name = "") { 187 return tryInsertInstruction( 188 new VPInstruction(Opcode, Operands, WrapFlags, DL, Name)); 189 } 190 191 VPInstruction *createNot(VPValue *Operand, 192 DebugLoc DL = DebugLoc::getUnknown(), 193 const Twine &Name = "") { 194 return createInstruction(VPInstruction::Not, {Operand}, DL, Name); 195 } 196 197 VPInstruction *createAnd(VPValue *LHS, VPValue *RHS, 198 DebugLoc DL = DebugLoc::getUnknown(), 199 const Twine &Name = "") { 200 return createInstruction(Instruction::BinaryOps::And, {LHS, RHS}, DL, Name); 201 } 202 203 VPInstruction *createOr(VPValue *LHS, VPValue *RHS, 204 DebugLoc DL = DebugLoc::getUnknown(), 205 const Twine &Name = "") { 206 207 return tryInsertInstruction(new VPInstruction( 208 Instruction::BinaryOps::Or, {LHS, RHS}, 209 VPRecipeWithIRFlags::DisjointFlagsTy(false), DL, Name)); 210 } 211 212 VPInstruction *createLogicalAnd(VPValue *LHS, VPValue *RHS, 213 DebugLoc DL = DebugLoc::getUnknown(), 214 const Twine &Name = "") { 215 return tryInsertInstruction( 216 new VPInstruction(VPInstruction::LogicalAnd, {LHS, RHS}, DL, Name)); 217 } 218 219 VPInstruction * 220 createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal, 221 DebugLoc DL = DebugLoc::getUnknown(), const Twine &Name = "", 222 std::optional<FastMathFlags> FMFs = std::nullopt) { 223 auto *Select = 224 FMFs ? new VPInstruction(Instruction::Select, {Cond, TrueVal, FalseVal}, 225 *FMFs, DL, Name) 226 : new VPInstruction(Instruction::Select, {Cond, TrueVal, FalseVal}, 227 DL, Name); 228 return tryInsertInstruction(Select); 229 } 230 231 /// Create a new ICmp VPInstruction with predicate \p Pred and operands \p A 232 /// and \p B. 233 VPInstruction *createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B, 234 DebugLoc DL = DebugLoc::getUnknown(), 235 const Twine &Name = "") { 236 assert(Pred >= CmpInst::FIRST_ICMP_PREDICATE && 237 Pred <= CmpInst::LAST_ICMP_PREDICATE && "invalid predicate"); 238 return tryInsertInstruction( 239 new VPInstruction(Instruction::ICmp, {A, B}, Pred, DL, Name)); 240 } 241 242 /// Create a new FCmp VPInstruction with predicate \p Pred and operands \p A 243 /// and \p B. 244 VPInstruction *createFCmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B, 245 DebugLoc DL = DebugLoc::getUnknown(), 246 const Twine &Name = "") { 247 assert(Pred >= CmpInst::FIRST_FCMP_PREDICATE && 248 Pred <= CmpInst::LAST_FCMP_PREDICATE && "invalid predicate"); 249 return tryInsertInstruction( 250 new VPInstruction(Instruction::FCmp, {A, B}, Pred, DL, Name)); 251 } 252 253 VPInstruction *createPtrAdd(VPValue *Ptr, VPValue *Offset, 254 DebugLoc DL = DebugLoc::getUnknown(), 255 const Twine &Name = "") { 256 return tryInsertInstruction( 257 new VPInstruction(VPInstruction::PtrAdd, {Ptr, Offset}, 258 GEPNoWrapFlags::none(), DL, Name)); 259 } 260 VPInstruction *createInBoundsPtrAdd(VPValue *Ptr, VPValue *Offset, 261 DebugLoc DL = DebugLoc::getUnknown(), 262 const Twine &Name = "") { 263 return tryInsertInstruction( 264 new VPInstruction(VPInstruction::PtrAdd, {Ptr, Offset}, 265 GEPNoWrapFlags::inBounds(), DL, Name)); 266 } 267 268 VPPhi *createScalarPhi(ArrayRef<VPValue *> IncomingValues, DebugLoc DL, 269 const Twine &Name = "") { 270 return tryInsertInstruction(new VPPhi(IncomingValues, DL, Name)); 271 } 272 273 /// Convert the input value \p Current to the corresponding value of an 274 /// induction with \p Start and \p Step values, using \p Start + \p Current * 275 /// \p Step. 276 VPDerivedIVRecipe *createDerivedIV(InductionDescriptor::InductionKind Kind, 277 FPMathOperator *FPBinOp, VPValue *Start, 278 VPValue *Current, VPValue *Step, 279 const Twine &Name = "") { 280 return tryInsertInstruction( 281 new VPDerivedIVRecipe(Kind, FPBinOp, Start, Current, Step, Name)); 282 } 283 createScalarCast(Instruction::CastOps Opcode,VPValue * Op,Type * ResultTy,DebugLoc DL)284 VPInstruction *createScalarCast(Instruction::CastOps Opcode, VPValue *Op, 285 Type *ResultTy, DebugLoc DL) { 286 return tryInsertInstruction( 287 new VPInstructionWithType(Opcode, Op, ResultTy, {}, DL)); 288 } 289 createScalarZExtOrTrunc(VPValue * Op,Type * ResultTy,Type * SrcTy,DebugLoc DL)290 VPValue *createScalarZExtOrTrunc(VPValue *Op, Type *ResultTy, Type *SrcTy, 291 DebugLoc DL) { 292 if (ResultTy == SrcTy) 293 return Op; 294 Instruction::CastOps CastOp = 295 ResultTy->getScalarSizeInBits() < SrcTy->getScalarSizeInBits() 296 ? Instruction::Trunc 297 : Instruction::ZExt; 298 return createScalarCast(CastOp, Op, ResultTy, DL); 299 } 300 createWidenCast(Instruction::CastOps Opcode,VPValue * Op,Type * ResultTy)301 VPWidenCastRecipe *createWidenCast(Instruction::CastOps Opcode, VPValue *Op, 302 Type *ResultTy) { 303 return tryInsertInstruction(new VPWidenCastRecipe(Opcode, Op, ResultTy)); 304 } 305 306 VPScalarIVStepsRecipe * createScalarIVSteps(Instruction::BinaryOps InductionOpcode,FPMathOperator * FPBinOp,VPValue * IV,VPValue * Step,VPValue * VF,DebugLoc DL)307 createScalarIVSteps(Instruction::BinaryOps InductionOpcode, 308 FPMathOperator *FPBinOp, VPValue *IV, VPValue *Step, 309 VPValue *VF, DebugLoc DL) { 310 return tryInsertInstruction(new VPScalarIVStepsRecipe( 311 IV, Step, VF, InductionOpcode, 312 FPBinOp ? FPBinOp->getFastMathFlags() : FastMathFlags(), DL)); 313 } 314 315 //===--------------------------------------------------------------------===// 316 // RAII helpers. 317 //===--------------------------------------------------------------------===// 318 319 /// RAII object that stores the current insertion point and restores it when 320 /// the object is destroyed. 321 class InsertPointGuard { 322 VPBuilder &Builder; 323 VPBasicBlock *Block; 324 VPBasicBlock::iterator Point; 325 326 public: InsertPointGuard(VPBuilder & B)327 InsertPointGuard(VPBuilder &B) 328 : Builder(B), Block(B.getInsertBlock()), Point(B.getInsertPoint()) {} 329 330 InsertPointGuard(const InsertPointGuard &) = delete; 331 InsertPointGuard &operator=(const InsertPointGuard &) = delete; 332 ~InsertPointGuard()333 ~InsertPointGuard() { Builder.restoreIP(VPInsertPoint(Block, Point)); } 334 }; 335 }; 336 337 /// TODO: The following VectorizationFactor was pulled out of 338 /// LoopVectorizationCostModel class. LV also deals with 339 /// VectorizerParams::VectorizationFactor. 340 /// We need to streamline them. 341 342 /// Information about vectorization costs. 343 struct VectorizationFactor { 344 /// Vector width with best cost. 345 ElementCount Width; 346 347 /// Cost of the loop with that width. 348 InstructionCost Cost; 349 350 /// Cost of the scalar loop. 351 InstructionCost ScalarCost; 352 353 /// The minimum trip count required to make vectorization profitable, e.g. due 354 /// to runtime checks. 355 ElementCount MinProfitableTripCount; 356 VectorizationFactorVectorizationFactor357 VectorizationFactor(ElementCount Width, InstructionCost Cost, 358 InstructionCost ScalarCost) 359 : Width(Width), Cost(Cost), ScalarCost(ScalarCost) {} 360 361 /// Width 1 means no vectorization, cost 0 means uncomputed cost. DisabledVectorizationFactor362 static VectorizationFactor Disabled() { 363 return {ElementCount::getFixed(1), 0, 0}; 364 } 365 366 bool operator==(const VectorizationFactor &rhs) const { 367 return Width == rhs.Width && Cost == rhs.Cost; 368 } 369 370 bool operator!=(const VectorizationFactor &rhs) const { 371 return !(*this == rhs); 372 } 373 }; 374 375 /// A class that represents two vectorization factors (initialized with 0 by 376 /// default). One for fixed-width vectorization and one for scalable 377 /// vectorization. This can be used by the vectorizer to choose from a range of 378 /// fixed and/or scalable VFs in order to find the most cost-effective VF to 379 /// vectorize with. 380 struct FixedScalableVFPair { 381 ElementCount FixedVF; 382 ElementCount ScalableVF; 383 FixedScalableVFPairFixedScalableVFPair384 FixedScalableVFPair() 385 : FixedVF(ElementCount::getFixed(0)), 386 ScalableVF(ElementCount::getScalable(0)) {} FixedScalableVFPairFixedScalableVFPair387 FixedScalableVFPair(const ElementCount &Max) : FixedScalableVFPair() { 388 *(Max.isScalable() ? &ScalableVF : &FixedVF) = Max; 389 } FixedScalableVFPairFixedScalableVFPair390 FixedScalableVFPair(const ElementCount &FixedVF, 391 const ElementCount &ScalableVF) 392 : FixedVF(FixedVF), ScalableVF(ScalableVF) { 393 assert(!FixedVF.isScalable() && ScalableVF.isScalable() && 394 "Invalid scalable properties"); 395 } 396 getNoneFixedScalableVFPair397 static FixedScalableVFPair getNone() { return FixedScalableVFPair(); } 398 399 /// \return true if either fixed- or scalable VF is non-zero. 400 explicit operator bool() const { return FixedVF || ScalableVF; } 401 402 /// \return true if either fixed- or scalable VF is a valid vector VF. hasVectorFixedScalableVFPair403 bool hasVector() const { return FixedVF.isVector() || ScalableVF.isVector(); } 404 }; 405 406 /// Planner drives the vectorization process after having passed 407 /// Legality checks. 408 class LoopVectorizationPlanner { 409 /// The loop that we evaluate. 410 Loop *OrigLoop; 411 412 /// Loop Info analysis. 413 LoopInfo *LI; 414 415 /// The dominator tree. 416 DominatorTree *DT; 417 418 /// Target Library Info. 419 const TargetLibraryInfo *TLI; 420 421 /// Target Transform Info. 422 const TargetTransformInfo &TTI; 423 424 /// The legality analysis. 425 LoopVectorizationLegality *Legal; 426 427 /// The profitability analysis. 428 LoopVectorizationCostModel &CM; 429 430 /// The interleaved access analysis. 431 InterleavedAccessInfo &IAI; 432 433 PredicatedScalarEvolution &PSE; 434 435 const LoopVectorizeHints &Hints; 436 437 OptimizationRemarkEmitter *ORE; 438 439 SmallVector<VPlanPtr, 4> VPlans; 440 441 /// Profitable vector factors. 442 SmallVector<VectorizationFactor, 8> ProfitableVFs; 443 444 /// A builder used to construct the current plan. 445 VPBuilder Builder; 446 447 /// Computes the cost of \p Plan for vectorization factor \p VF. 448 /// 449 /// The current implementation requires access to the 450 /// LoopVectorizationLegality to handle inductions and reductions, which is 451 /// why it is kept separate from the VPlan-only cost infrastructure. 452 /// 453 /// TODO: Move to VPlan::cost once the use of LoopVectorizationLegality has 454 /// been retired. 455 InstructionCost cost(VPlan &Plan, ElementCount VF) const; 456 457 /// Precompute costs for certain instructions using the legacy cost model. The 458 /// function is used to bring up the VPlan-based cost model to initially avoid 459 /// taking different decisions due to inaccuracies in the legacy cost model. 460 InstructionCost precomputeCosts(VPlan &Plan, ElementCount VF, 461 VPCostContext &CostCtx) const; 462 463 public: LoopVectorizationPlanner(Loop * L,LoopInfo * LI,DominatorTree * DT,const TargetLibraryInfo * TLI,const TargetTransformInfo & TTI,LoopVectorizationLegality * Legal,LoopVectorizationCostModel & CM,InterleavedAccessInfo & IAI,PredicatedScalarEvolution & PSE,const LoopVectorizeHints & Hints,OptimizationRemarkEmitter * ORE)464 LoopVectorizationPlanner( 465 Loop *L, LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI, 466 const TargetTransformInfo &TTI, LoopVectorizationLegality *Legal, 467 LoopVectorizationCostModel &CM, InterleavedAccessInfo &IAI, 468 PredicatedScalarEvolution &PSE, const LoopVectorizeHints &Hints, 469 OptimizationRemarkEmitter *ORE) 470 : OrigLoop(L), LI(LI), DT(DT), TLI(TLI), TTI(TTI), Legal(Legal), CM(CM), 471 IAI(IAI), PSE(PSE), Hints(Hints), ORE(ORE) {} 472 473 /// Build VPlans for the specified \p UserVF and \p UserIC if they are 474 /// non-zero or all applicable candidate VFs otherwise. If vectorization and 475 /// interleaving should be avoided up-front, no plans are generated. 476 void plan(ElementCount UserVF, unsigned UserIC); 477 478 /// Use the VPlan-native path to plan how to best vectorize, return the best 479 /// VF and its cost. 480 VectorizationFactor planInVPlanNativePath(ElementCount UserVF); 481 482 /// Return the VPlan for \p VF. At the moment, there is always a single VPlan 483 /// for each VF. 484 VPlan &getPlanFor(ElementCount VF) const; 485 486 /// Compute and return the most profitable vectorization factor. Also collect 487 /// all profitable VFs in ProfitableVFs. 488 VectorizationFactor computeBestVF(); 489 490 /// Generate the IR code for the vectorized loop captured in VPlan \p BestPlan 491 /// according to the best selected \p VF and \p UF. 492 /// 493 /// TODO: \p VectorizingEpilogue indicates if the executed VPlan is for the 494 /// epilogue vector loop. It should be removed once the re-use issue has been 495 /// fixed. 496 /// 497 /// Returns a mapping of SCEVs to their expanded IR values. 498 /// Note that this is a temporary workaround needed due to the current 499 /// epilogue handling. 500 DenseMap<const SCEV *, Value *> executePlan(ElementCount VF, unsigned UF, 501 VPlan &BestPlan, 502 InnerLoopVectorizer &LB, 503 DominatorTree *DT, 504 bool VectorizingEpilogue); 505 506 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 507 void printPlans(raw_ostream &O); 508 #endif 509 510 /// Look through the existing plans and return true if we have one with 511 /// vectorization factor \p VF. hasPlanWithVF(ElementCount VF)512 bool hasPlanWithVF(ElementCount VF) const { 513 return any_of(VPlans, 514 [&](const VPlanPtr &Plan) { return Plan->hasVF(VF); }); 515 } 516 517 /// Test a \p Predicate on a \p Range of VF's. Return the value of applying 518 /// \p Predicate on Range.Start, possibly decreasing Range.End such that the 519 /// returned value holds for the entire \p Range. 520 static bool 521 getDecisionAndClampRange(const std::function<bool(ElementCount)> &Predicate, 522 VFRange &Range); 523 524 /// \return The most profitable vectorization factor and the cost of that VF 525 /// for vectorizing the epilogue. Returns VectorizationFactor::Disabled if 526 /// epilogue vectorization is not supported for the loop. 527 VectorizationFactor 528 selectEpilogueVectorizationFactor(const ElementCount MaxVF, unsigned IC); 529 530 /// Emit remarks for recipes with invalid costs in the available VPlans. 531 void emitInvalidCostRemarks(OptimizationRemarkEmitter *ORE); 532 533 protected: 534 /// Build VPlans for power-of-2 VF's between \p MinVF and \p MaxVF inclusive, 535 /// according to the information gathered by Legal when it checked if it is 536 /// legal to vectorize the loop. 537 void buildVPlans(ElementCount MinVF, ElementCount MaxVF); 538 539 private: 540 /// Build a VPlan according to the information gathered by Legal. \return a 541 /// VPlan for vectorization factors \p Range.Start and up to \p Range.End 542 /// exclusive, possibly decreasing \p Range.End. If no VPlan can be built for 543 /// the input range, set the largest included VF to the maximum VF for which 544 /// no plan could be built. 545 VPlanPtr tryToBuildVPlan(VFRange &Range); 546 547 /// Build a VPlan using VPRecipes according to the information gather by 548 /// Legal. This method is only used for the legacy inner loop vectorizer. 549 /// \p Range's largest included VF is restricted to the maximum VF the 550 /// returned VPlan is valid for. If no VPlan can be built for the input range, 551 /// set the largest included VF to the maximum VF for which no plan could be 552 /// built. Each VPlan is built starting from a copy of \p InitialPlan, which 553 /// is a plain CFG VPlan wrapping the original scalar loop. 554 VPlanPtr tryToBuildVPlanWithVPRecipes(VPlanPtr InitialPlan, VFRange &Range, 555 LoopVersioning *LVer); 556 557 /// Build VPlans for power-of-2 VF's between \p MinVF and \p MaxVF inclusive, 558 /// according to the information gathered by Legal when it checked if it is 559 /// legal to vectorize the loop. This method creates VPlans using VPRecipes. 560 void buildVPlansWithVPRecipes(ElementCount MinVF, ElementCount MaxVF); 561 562 // Adjust the recipes for reductions. For in-loop reductions the chain of 563 // instructions leading from the loop exit instr to the phi need to be 564 // converted to reductions, with one operand being vector and the other being 565 // the scalar reduction chain. For other reductions, a select is introduced 566 // between the phi and users outside the vector region when folding the tail. 567 void adjustRecipesForReductions(VPlanPtr &Plan, 568 VPRecipeBuilder &RecipeBuilder, 569 ElementCount MinVF); 570 571 /// Attach the runtime checks of \p RTChecks to \p Plan. 572 void attachRuntimeChecks(VPlan &Plan, GeneratedRTChecks &RTChecks, 573 bool HasBranchWeights) const; 574 575 #ifndef NDEBUG 576 /// \return The most profitable vectorization factor for the available VPlans 577 /// and the cost of that VF. 578 /// This is now only used to verify the decisions by the new VPlan-based 579 /// cost-model and will be retired once the VPlan-based cost-model is 580 /// stabilized. 581 VectorizationFactor selectVectorizationFactor(); 582 #endif 583 584 /// Returns true if the per-lane cost of VectorizationFactor A is lower than 585 /// that of B. 586 bool isMoreProfitable(const VectorizationFactor &A, 587 const VectorizationFactor &B, bool HasTail) const; 588 589 /// Returns true if the per-lane cost of VectorizationFactor A is lower than 590 /// that of B in the context of vectorizing a loop with known \p MaxTripCount. 591 bool isMoreProfitable(const VectorizationFactor &A, 592 const VectorizationFactor &B, 593 const unsigned MaxTripCount, bool HasTail) const; 594 595 /// Determines if we have the infrastructure to vectorize the loop and its 596 /// epilogue, assuming the main loop is vectorized by \p VF. 597 bool isCandidateForEpilogueVectorization(const ElementCount VF) const; 598 }; 599 600 } // namespace llvm 601 602 #endif // LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONPLANNER_H 603