1 //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This is the LLVM vectorization plan. It represents a candidate for 11 /// vectorization, allowing to plan and optimize how to vectorize a given loop 12 /// before generating LLVM-IR. 13 /// The vectorizer uses vectorization plans to estimate the costs of potential 14 /// candidates and if profitable to execute the desired plan, generating vector 15 /// LLVM-IR code. 16 /// 17 //===----------------------------------------------------------------------===// 18 19 #include "VPlan.h" 20 #include "VPlanDominatorTree.h" 21 #include "llvm/ADT/DepthFirstIterator.h" 22 #include "llvm/ADT/PostOrderIterator.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/Twine.h" 25 #include "llvm/Analysis/IVDescriptors.h" 26 #include "llvm/Analysis/LoopInfo.h" 27 #include "llvm/IR/BasicBlock.h" 28 #include "llvm/IR/CFG.h" 29 #include "llvm/IR/InstrTypes.h" 30 #include "llvm/IR/Instruction.h" 31 #include "llvm/IR/Instructions.h" 32 #include "llvm/IR/Type.h" 33 #include "llvm/IR/Value.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/Debug.h" 37 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/GenericDomTreeConstruction.h" 39 #include "llvm/Support/GraphWriter.h" 40 #include "llvm/Support/raw_ostream.h" 41 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 42 #include <cassert> 43 #include <iterator> 44 #include <string> 45 #include <vector> 46 47 using namespace llvm; 48 extern cl::opt<bool> EnableVPlanNativePath; 49 50 #define DEBUG_TYPE "vplan" 51 52 raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) { 53 const VPInstruction *Instr = dyn_cast<VPInstruction>(&V); 54 VPSlotTracker SlotTracker( 55 (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr); 56 V.print(OS, SlotTracker); 57 return OS; 58 } 59 60 VPValue::VPValue(const unsigned char SC, Value *UV, VPDef *Def) 61 : SubclassID(SC), UnderlyingVal(UV), Def(Def) { 62 if (Def) 63 Def->addDefinedValue(this); 64 } 65 66 VPValue::~VPValue() { 67 assert(Users.empty() && "trying to delete a VPValue with remaining users"); 68 if (Def) 69 Def->removeDefinedValue(this); 70 } 71 72 void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const { 73 if (const VPInstruction *Instr = dyn_cast<VPInstruction>(this)) 74 Instr->print(OS, SlotTracker); 75 else 76 printAsOperand(OS, SlotTracker); 77 } 78 79 void VPValue::dump() const { 80 const VPInstruction *Instr = dyn_cast<VPInstruction>(this); 81 VPSlotTracker SlotTracker( 82 (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr); 83 print(dbgs(), SlotTracker); 84 dbgs() << "\n"; 85 } 86 87 void VPRecipeBase::dump() const { 88 VPSlotTracker SlotTracker(nullptr); 89 print(dbgs(), "", SlotTracker); 90 dbgs() << "\n"; 91 } 92 93 VPUser *VPRecipeBase::toVPUser() { 94 if (auto *U = dyn_cast<VPInstruction>(this)) 95 return U; 96 if (auto *U = dyn_cast<VPWidenRecipe>(this)) 97 return U; 98 if (auto *U = dyn_cast<VPWidenCallRecipe>(this)) 99 return U; 100 if (auto *U = dyn_cast<VPWidenSelectRecipe>(this)) 101 return U; 102 if (auto *U = dyn_cast<VPWidenGEPRecipe>(this)) 103 return U; 104 if (auto *U = dyn_cast<VPBlendRecipe>(this)) 105 return U; 106 if (auto *U = dyn_cast<VPInterleaveRecipe>(this)) 107 return U; 108 if (auto *U = dyn_cast<VPReplicateRecipe>(this)) 109 return U; 110 if (auto *U = dyn_cast<VPBranchOnMaskRecipe>(this)) 111 return U; 112 if (auto *U = dyn_cast<VPWidenMemoryInstructionRecipe>(this)) 113 return U; 114 if (auto *U = dyn_cast<VPReductionRecipe>(this)) 115 return U; 116 return nullptr; 117 } 118 119 VPValue *VPRecipeBase::toVPValue() { 120 if (auto *V = dyn_cast<VPInstruction>(this)) 121 return V; 122 if (auto *V = dyn_cast<VPReductionRecipe>(this)) 123 return V; 124 if (auto *V = dyn_cast<VPWidenMemoryInstructionRecipe>(this)) 125 return V; 126 if (auto *V = dyn_cast<VPWidenCallRecipe>(this)) 127 return V; 128 if (auto *V = dyn_cast<VPWidenSelectRecipe>(this)) 129 return V; 130 if (auto *V = dyn_cast<VPWidenGEPRecipe>(this)) 131 return V; 132 if (auto *V = dyn_cast<VPWidenRecipe>(this)) 133 return V; 134 if (auto *V = dyn_cast<VPReplicateRecipe>(this)) 135 return V; 136 return nullptr; 137 } 138 139 const VPValue *VPRecipeBase::toVPValue() const { 140 if (auto *V = dyn_cast<VPInstruction>(this)) 141 return V; 142 if (auto *V = dyn_cast<VPReductionRecipe>(this)) 143 return V; 144 if (auto *V = dyn_cast<VPWidenMemoryInstructionRecipe>(this)) 145 return V; 146 if (auto *V = dyn_cast<VPWidenCallRecipe>(this)) 147 return V; 148 if (auto *V = dyn_cast<VPWidenSelectRecipe>(this)) 149 return V; 150 if (auto *V = dyn_cast<VPWidenGEPRecipe>(this)) 151 return V; 152 if (auto *V = dyn_cast<VPWidenRecipe>(this)) 153 return V; 154 if (auto *V = dyn_cast<VPReplicateRecipe>(this)) 155 return V; 156 return nullptr; 157 } 158 159 // Get the top-most entry block of \p Start. This is the entry block of the 160 // containing VPlan. This function is templated to support both const and non-const blocks 161 template <typename T> static T *getPlanEntry(T *Start) { 162 T *Next = Start; 163 T *Current = Start; 164 while ((Next = Next->getParent())) 165 Current = Next; 166 167 SmallSetVector<T *, 8> WorkList; 168 WorkList.insert(Current); 169 170 for (unsigned i = 0; i < WorkList.size(); i++) { 171 T *Current = WorkList[i]; 172 if (Current->getNumPredecessors() == 0) 173 return Current; 174 auto &Predecessors = Current->getPredecessors(); 175 WorkList.insert(Predecessors.begin(), Predecessors.end()); 176 } 177 178 llvm_unreachable("VPlan without any entry node without predecessors"); 179 } 180 181 VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; } 182 183 const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; } 184 185 /// \return the VPBasicBlock that is the entry of Block, possibly indirectly. 186 const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const { 187 const VPBlockBase *Block = this; 188 while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 189 Block = Region->getEntry(); 190 return cast<VPBasicBlock>(Block); 191 } 192 193 VPBasicBlock *VPBlockBase::getEntryBasicBlock() { 194 VPBlockBase *Block = this; 195 while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 196 Block = Region->getEntry(); 197 return cast<VPBasicBlock>(Block); 198 } 199 200 void VPBlockBase::setPlan(VPlan *ParentPlan) { 201 assert(ParentPlan->getEntry() == this && 202 "Can only set plan on its entry block."); 203 Plan = ParentPlan; 204 } 205 206 /// \return the VPBasicBlock that is the exit of Block, possibly indirectly. 207 const VPBasicBlock *VPBlockBase::getExitBasicBlock() const { 208 const VPBlockBase *Block = this; 209 while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 210 Block = Region->getExit(); 211 return cast<VPBasicBlock>(Block); 212 } 213 214 VPBasicBlock *VPBlockBase::getExitBasicBlock() { 215 VPBlockBase *Block = this; 216 while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 217 Block = Region->getExit(); 218 return cast<VPBasicBlock>(Block); 219 } 220 221 VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() { 222 if (!Successors.empty() || !Parent) 223 return this; 224 assert(Parent->getExit() == this && 225 "Block w/o successors not the exit of its parent."); 226 return Parent->getEnclosingBlockWithSuccessors(); 227 } 228 229 VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() { 230 if (!Predecessors.empty() || !Parent) 231 return this; 232 assert(Parent->getEntry() == this && 233 "Block w/o predecessors not the entry of its parent."); 234 return Parent->getEnclosingBlockWithPredecessors(); 235 } 236 237 void VPBlockBase::deleteCFG(VPBlockBase *Entry) { 238 SmallVector<VPBlockBase *, 8> Blocks; 239 240 for (VPBlockBase *Block : depth_first(Entry)) 241 Blocks.push_back(Block); 242 243 for (VPBlockBase *Block : Blocks) 244 delete Block; 245 } 246 247 VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() { 248 iterator It = begin(); 249 while (It != end() && (isa<VPWidenPHIRecipe>(&*It) || 250 isa<VPWidenIntOrFpInductionRecipe>(&*It) || 251 isa<VPPredInstPHIRecipe>(&*It) || 252 isa<VPWidenCanonicalIVRecipe>(&*It))) 253 It++; 254 return It; 255 } 256 257 BasicBlock * 258 VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) { 259 // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks. 260 // Pred stands for Predessor. Prev stands for Previous - last visited/created. 261 BasicBlock *PrevBB = CFG.PrevBB; 262 BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(), 263 PrevBB->getParent(), CFG.LastBB); 264 LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n'); 265 266 // Hook up the new basic block to its predecessors. 267 for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) { 268 VPBasicBlock *PredVPBB = PredVPBlock->getExitBasicBlock(); 269 auto &PredVPSuccessors = PredVPBB->getSuccessors(); 270 BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB]; 271 272 // In outer loop vectorization scenario, the predecessor BBlock may not yet 273 // be visited(backedge). Mark the VPBasicBlock for fixup at the end of 274 // vectorization. We do not encounter this case in inner loop vectorization 275 // as we start out by building a loop skeleton with the vector loop header 276 // and latch blocks. As a result, we never enter this function for the 277 // header block in the non VPlan-native path. 278 if (!PredBB) { 279 assert(EnableVPlanNativePath && 280 "Unexpected null predecessor in non VPlan-native path"); 281 CFG.VPBBsToFix.push_back(PredVPBB); 282 continue; 283 } 284 285 assert(PredBB && "Predecessor basic-block not found building successor."); 286 auto *PredBBTerminator = PredBB->getTerminator(); 287 LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n'); 288 if (isa<UnreachableInst>(PredBBTerminator)) { 289 assert(PredVPSuccessors.size() == 1 && 290 "Predecessor ending w/o branch must have single successor."); 291 PredBBTerminator->eraseFromParent(); 292 BranchInst::Create(NewBB, PredBB); 293 } else { 294 assert(PredVPSuccessors.size() == 2 && 295 "Predecessor ending with branch must have two successors."); 296 unsigned idx = PredVPSuccessors.front() == this ? 0 : 1; 297 assert(!PredBBTerminator->getSuccessor(idx) && 298 "Trying to reset an existing successor block."); 299 PredBBTerminator->setSuccessor(idx, NewBB); 300 } 301 } 302 return NewBB; 303 } 304 305 void VPBasicBlock::execute(VPTransformState *State) { 306 bool Replica = State->Instance && 307 !(State->Instance->Part == 0 && State->Instance->Lane == 0); 308 VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB; 309 VPBlockBase *SingleHPred = nullptr; 310 BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible. 311 312 // 1. Create an IR basic block, or reuse the last one if possible. 313 // The last IR basic block is reused, as an optimization, in three cases: 314 // A. the first VPBB reuses the loop header BB - when PrevVPBB is null; 315 // B. when the current VPBB has a single (hierarchical) predecessor which 316 // is PrevVPBB and the latter has a single (hierarchical) successor; and 317 // C. when the current VPBB is an entry of a region replica - where PrevVPBB 318 // is the exit of this region from a previous instance, or the predecessor 319 // of this region. 320 if (PrevVPBB && /* A */ 321 !((SingleHPred = getSingleHierarchicalPredecessor()) && 322 SingleHPred->getExitBasicBlock() == PrevVPBB && 323 PrevVPBB->getSingleHierarchicalSuccessor()) && /* B */ 324 !(Replica && getPredecessors().empty())) { /* C */ 325 NewBB = createEmptyBasicBlock(State->CFG); 326 State->Builder.SetInsertPoint(NewBB); 327 // Temporarily terminate with unreachable until CFG is rewired. 328 UnreachableInst *Terminator = State->Builder.CreateUnreachable(); 329 State->Builder.SetInsertPoint(Terminator); 330 // Register NewBB in its loop. In innermost loops its the same for all BB's. 331 Loop *L = State->LI->getLoopFor(State->CFG.LastBB); 332 L->addBasicBlockToLoop(NewBB, *State->LI); 333 State->CFG.PrevBB = NewBB; 334 } 335 336 // 2. Fill the IR basic block with IR instructions. 337 LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName() 338 << " in BB:" << NewBB->getName() << '\n'); 339 340 State->CFG.VPBB2IRBB[this] = NewBB; 341 State->CFG.PrevVPBB = this; 342 343 for (VPRecipeBase &Recipe : Recipes) 344 Recipe.execute(*State); 345 346 VPValue *CBV; 347 if (EnableVPlanNativePath && (CBV = getCondBit())) { 348 Value *IRCBV = CBV->getUnderlyingValue(); 349 assert(IRCBV && "Unexpected null underlying value for condition bit"); 350 351 // Condition bit value in a VPBasicBlock is used as the branch selector. In 352 // the VPlan-native path case, since all branches are uniform we generate a 353 // branch instruction using the condition value from vector lane 0 and dummy 354 // successors. The successors are fixed later when the successor blocks are 355 // visited. 356 Value *NewCond = State->Callback.getOrCreateVectorValues(IRCBV, 0); 357 NewCond = State->Builder.CreateExtractElement(NewCond, 358 State->Builder.getInt32(0)); 359 360 // Replace the temporary unreachable terminator with the new conditional 361 // branch. 362 auto *CurrentTerminator = NewBB->getTerminator(); 363 assert(isa<UnreachableInst>(CurrentTerminator) && 364 "Expected to replace unreachable terminator with conditional " 365 "branch."); 366 auto *CondBr = BranchInst::Create(NewBB, nullptr, NewCond); 367 CondBr->setSuccessor(0, nullptr); 368 ReplaceInstWithInst(CurrentTerminator, CondBr); 369 } 370 371 LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB); 372 } 373 374 void VPBasicBlock::dropAllReferences(VPValue *NewValue) { 375 for (VPRecipeBase &R : Recipes) { 376 if (auto *VPV = R.toVPValue()) 377 VPV->replaceAllUsesWith(NewValue); 378 379 if (auto *User = R.toVPUser()) 380 for (unsigned I = 0, E = User->getNumOperands(); I != E; I++) 381 User->setOperand(I, NewValue); 382 } 383 } 384 385 void VPRegionBlock::dropAllReferences(VPValue *NewValue) { 386 for (VPBlockBase *Block : depth_first(Entry)) 387 // Drop all references in VPBasicBlocks and replace all uses with 388 // DummyValue. 389 Block->dropAllReferences(NewValue); 390 } 391 392 void VPRegionBlock::execute(VPTransformState *State) { 393 ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry); 394 395 if (!isReplicator()) { 396 // Visit the VPBlocks connected to "this", starting from it. 397 for (VPBlockBase *Block : RPOT) { 398 if (EnableVPlanNativePath) { 399 // The inner loop vectorization path does not represent loop preheader 400 // and exit blocks as part of the VPlan. In the VPlan-native path, skip 401 // vectorizing loop preheader block. In future, we may replace this 402 // check with the check for loop preheader. 403 if (Block->getNumPredecessors() == 0) 404 continue; 405 406 // Skip vectorizing loop exit block. In future, we may replace this 407 // check with the check for loop exit. 408 if (Block->getNumSuccessors() == 0) 409 continue; 410 } 411 412 LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n'); 413 Block->execute(State); 414 } 415 return; 416 } 417 418 assert(!State->Instance && "Replicating a Region with non-null instance."); 419 420 // Enter replicating mode. 421 State->Instance = {0, 0}; 422 423 for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) { 424 State->Instance->Part = Part; 425 assert(!State->VF.isScalable() && "VF is assumed to be non scalable."); 426 for (unsigned Lane = 0, VF = State->VF.getKnownMinValue(); Lane < VF; 427 ++Lane) { 428 State->Instance->Lane = Lane; 429 // Visit the VPBlocks connected to \p this, starting from it. 430 for (VPBlockBase *Block : RPOT) { 431 LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n'); 432 Block->execute(State); 433 } 434 } 435 } 436 437 // Exit replicating mode. 438 State->Instance.reset(); 439 } 440 441 void VPRecipeBase::insertBefore(VPRecipeBase *InsertPos) { 442 assert(!Parent && "Recipe already in some VPBasicBlock"); 443 assert(InsertPos->getParent() && 444 "Insertion position not in any VPBasicBlock"); 445 Parent = InsertPos->getParent(); 446 Parent->getRecipeList().insert(InsertPos->getIterator(), this); 447 } 448 449 void VPRecipeBase::insertAfter(VPRecipeBase *InsertPos) { 450 assert(!Parent && "Recipe already in some VPBasicBlock"); 451 assert(InsertPos->getParent() && 452 "Insertion position not in any VPBasicBlock"); 453 Parent = InsertPos->getParent(); 454 Parent->getRecipeList().insertAfter(InsertPos->getIterator(), this); 455 } 456 457 void VPRecipeBase::removeFromParent() { 458 assert(getParent() && "Recipe not in any VPBasicBlock"); 459 getParent()->getRecipeList().remove(getIterator()); 460 Parent = nullptr; 461 } 462 463 iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() { 464 assert(getParent() && "Recipe not in any VPBasicBlock"); 465 return getParent()->getRecipeList().erase(getIterator()); 466 } 467 468 void VPRecipeBase::moveAfter(VPRecipeBase *InsertPos) { 469 removeFromParent(); 470 insertAfter(InsertPos); 471 } 472 473 void VPInstruction::generateInstruction(VPTransformState &State, 474 unsigned Part) { 475 IRBuilder<> &Builder = State.Builder; 476 477 if (Instruction::isBinaryOp(getOpcode())) { 478 Value *A = State.get(getOperand(0), Part); 479 Value *B = State.get(getOperand(1), Part); 480 Value *V = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(), A, B); 481 State.set(this, V, Part); 482 return; 483 } 484 485 switch (getOpcode()) { 486 case VPInstruction::Not: { 487 Value *A = State.get(getOperand(0), Part); 488 Value *V = Builder.CreateNot(A); 489 State.set(this, V, Part); 490 break; 491 } 492 case VPInstruction::ICmpULE: { 493 Value *IV = State.get(getOperand(0), Part); 494 Value *TC = State.get(getOperand(1), Part); 495 Value *V = Builder.CreateICmpULE(IV, TC); 496 State.set(this, V, Part); 497 break; 498 } 499 case Instruction::Select: { 500 Value *Cond = State.get(getOperand(0), Part); 501 Value *Op1 = State.get(getOperand(1), Part); 502 Value *Op2 = State.get(getOperand(2), Part); 503 Value *V = Builder.CreateSelect(Cond, Op1, Op2); 504 State.set(this, V, Part); 505 break; 506 } 507 case VPInstruction::ActiveLaneMask: { 508 // Get first lane of vector induction variable. 509 Value *VIVElem0 = State.get(getOperand(0), {Part, 0}); 510 // Get the original loop tripcount. 511 Value *ScalarTC = State.TripCount; 512 513 auto *Int1Ty = Type::getInt1Ty(Builder.getContext()); 514 auto *PredTy = FixedVectorType::get(Int1Ty, State.VF.getKnownMinValue()); 515 Instruction *Call = Builder.CreateIntrinsic( 516 Intrinsic::get_active_lane_mask, {PredTy, ScalarTC->getType()}, 517 {VIVElem0, ScalarTC}, nullptr, "active.lane.mask"); 518 State.set(this, Call, Part); 519 break; 520 } 521 default: 522 llvm_unreachable("Unsupported opcode for instruction"); 523 } 524 } 525 526 void VPInstruction::execute(VPTransformState &State) { 527 assert(!State.Instance && "VPInstruction executing an Instance"); 528 for (unsigned Part = 0; Part < State.UF; ++Part) 529 generateInstruction(State, Part); 530 } 531 532 void VPInstruction::print(raw_ostream &O, const Twine &Indent, 533 VPSlotTracker &SlotTracker) const { 534 O << "\"EMIT "; 535 print(O, SlotTracker); 536 } 537 538 void VPInstruction::print(raw_ostream &O) const { 539 VPSlotTracker SlotTracker(getParent()->getPlan()); 540 print(O, SlotTracker); 541 } 542 543 void VPInstruction::print(raw_ostream &O, VPSlotTracker &SlotTracker) const { 544 if (hasResult()) { 545 printAsOperand(O, SlotTracker); 546 O << " = "; 547 } 548 549 switch (getOpcode()) { 550 case VPInstruction::Not: 551 O << "not"; 552 break; 553 case VPInstruction::ICmpULE: 554 O << "icmp ule"; 555 break; 556 case VPInstruction::SLPLoad: 557 O << "combined load"; 558 break; 559 case VPInstruction::SLPStore: 560 O << "combined store"; 561 break; 562 case VPInstruction::ActiveLaneMask: 563 O << "active lane mask"; 564 break; 565 566 default: 567 O << Instruction::getOpcodeName(getOpcode()); 568 } 569 570 for (const VPValue *Operand : operands()) { 571 O << " "; 572 Operand->printAsOperand(O, SlotTracker); 573 } 574 } 575 576 /// Generate the code inside the body of the vectorized loop. Assumes a single 577 /// LoopVectorBody basic-block was created for this. Introduce additional 578 /// basic-blocks as needed, and fill them all. 579 void VPlan::execute(VPTransformState *State) { 580 // -1. Check if the backedge taken count is needed, and if so build it. 581 if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) { 582 Value *TC = State->TripCount; 583 IRBuilder<> Builder(State->CFG.PrevBB->getTerminator()); 584 auto *TCMO = Builder.CreateSub(TC, ConstantInt::get(TC->getType(), 1), 585 "trip.count.minus.1"); 586 auto VF = State->VF; 587 Value *VTCMO = 588 VF.isScalar() ? TCMO : Builder.CreateVectorSplat(VF, TCMO, "broadcast"); 589 for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) 590 State->set(BackedgeTakenCount, VTCMO, Part); 591 } 592 593 // 0. Set the reverse mapping from VPValues to Values for code generation. 594 for (auto &Entry : Value2VPValue) 595 State->VPValue2Value[Entry.second] = Entry.first; 596 597 BasicBlock *VectorPreHeaderBB = State->CFG.PrevBB; 598 BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor(); 599 assert(VectorHeaderBB && "Loop preheader does not have a single successor."); 600 601 // 1. Make room to generate basic-blocks inside loop body if needed. 602 BasicBlock *VectorLatchBB = VectorHeaderBB->splitBasicBlock( 603 VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch"); 604 Loop *L = State->LI->getLoopFor(VectorHeaderBB); 605 L->addBasicBlockToLoop(VectorLatchBB, *State->LI); 606 // Remove the edge between Header and Latch to allow other connections. 607 // Temporarily terminate with unreachable until CFG is rewired. 608 // Note: this asserts the generated code's assumption that 609 // getFirstInsertionPt() can be dereferenced into an Instruction. 610 VectorHeaderBB->getTerminator()->eraseFromParent(); 611 State->Builder.SetInsertPoint(VectorHeaderBB); 612 UnreachableInst *Terminator = State->Builder.CreateUnreachable(); 613 State->Builder.SetInsertPoint(Terminator); 614 615 // 2. Generate code in loop body. 616 State->CFG.PrevVPBB = nullptr; 617 State->CFG.PrevBB = VectorHeaderBB; 618 State->CFG.LastBB = VectorLatchBB; 619 620 for (VPBlockBase *Block : depth_first(Entry)) 621 Block->execute(State); 622 623 // Setup branch terminator successors for VPBBs in VPBBsToFix based on 624 // VPBB's successors. 625 for (auto VPBB : State->CFG.VPBBsToFix) { 626 assert(EnableVPlanNativePath && 627 "Unexpected VPBBsToFix in non VPlan-native path"); 628 BasicBlock *BB = State->CFG.VPBB2IRBB[VPBB]; 629 assert(BB && "Unexpected null basic block for VPBB"); 630 631 unsigned Idx = 0; 632 auto *BBTerminator = BB->getTerminator(); 633 634 for (VPBlockBase *SuccVPBlock : VPBB->getHierarchicalSuccessors()) { 635 VPBasicBlock *SuccVPBB = SuccVPBlock->getEntryBasicBlock(); 636 BBTerminator->setSuccessor(Idx, State->CFG.VPBB2IRBB[SuccVPBB]); 637 ++Idx; 638 } 639 } 640 641 // 3. Merge the temporary latch created with the last basic-block filled. 642 BasicBlock *LastBB = State->CFG.PrevBB; 643 // Connect LastBB to VectorLatchBB to facilitate their merge. 644 assert((EnableVPlanNativePath || 645 isa<UnreachableInst>(LastBB->getTerminator())) && 646 "Expected InnerLoop VPlan CFG to terminate with unreachable"); 647 assert((!EnableVPlanNativePath || isa<BranchInst>(LastBB->getTerminator())) && 648 "Expected VPlan CFG to terminate with branch in NativePath"); 649 LastBB->getTerminator()->eraseFromParent(); 650 BranchInst::Create(VectorLatchBB, LastBB); 651 652 // Merge LastBB with Latch. 653 bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI); 654 (void)Merged; 655 assert(Merged && "Could not merge last basic block with latch."); 656 VectorLatchBB = LastBB; 657 658 // We do not attempt to preserve DT for outer loop vectorization currently. 659 if (!EnableVPlanNativePath) 660 updateDominatorTree(State->DT, VectorPreHeaderBB, VectorLatchBB, 661 L->getExitBlock()); 662 } 663 664 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 665 LLVM_DUMP_METHOD 666 void VPlan::dump() const { dbgs() << *this << '\n'; } 667 #endif 668 669 void VPlan::updateDominatorTree(DominatorTree *DT, BasicBlock *LoopPreHeaderBB, 670 BasicBlock *LoopLatchBB, 671 BasicBlock *LoopExitBB) { 672 BasicBlock *LoopHeaderBB = LoopPreHeaderBB->getSingleSuccessor(); 673 assert(LoopHeaderBB && "Loop preheader does not have a single successor."); 674 // The vector body may be more than a single basic-block by this point. 675 // Update the dominator tree information inside the vector body by propagating 676 // it from header to latch, expecting only triangular control-flow, if any. 677 BasicBlock *PostDomSucc = nullptr; 678 for (auto *BB = LoopHeaderBB; BB != LoopLatchBB; BB = PostDomSucc) { 679 // Get the list of successors of this block. 680 std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB)); 681 assert(Succs.size() <= 2 && 682 "Basic block in vector loop has more than 2 successors."); 683 PostDomSucc = Succs[0]; 684 if (Succs.size() == 1) { 685 assert(PostDomSucc->getSinglePredecessor() && 686 "PostDom successor has more than one predecessor."); 687 DT->addNewBlock(PostDomSucc, BB); 688 continue; 689 } 690 BasicBlock *InterimSucc = Succs[1]; 691 if (PostDomSucc->getSingleSuccessor() == InterimSucc) { 692 PostDomSucc = Succs[1]; 693 InterimSucc = Succs[0]; 694 } 695 assert(InterimSucc->getSingleSuccessor() == PostDomSucc && 696 "One successor of a basic block does not lead to the other."); 697 assert(InterimSucc->getSinglePredecessor() && 698 "Interim successor has more than one predecessor."); 699 assert(PostDomSucc->hasNPredecessors(2) && 700 "PostDom successor has more than two predecessors."); 701 DT->addNewBlock(InterimSucc, BB); 702 DT->addNewBlock(PostDomSucc, BB); 703 } 704 // Latch block is a new dominator for the loop exit. 705 DT->changeImmediateDominator(LoopExitBB, LoopLatchBB); 706 assert(DT->verify(DominatorTree::VerificationLevel::Fast)); 707 } 708 709 const Twine VPlanPrinter::getUID(const VPBlockBase *Block) { 710 return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") + 711 Twine(getOrCreateBID(Block)); 712 } 713 714 const Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) { 715 const std::string &Name = Block->getName(); 716 if (!Name.empty()) 717 return Name; 718 return "VPB" + Twine(getOrCreateBID(Block)); 719 } 720 721 void VPlanPrinter::dump() { 722 Depth = 1; 723 bumpIndent(0); 724 OS << "digraph VPlan {\n"; 725 OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan"; 726 if (!Plan.getName().empty()) 727 OS << "\\n" << DOT::EscapeString(Plan.getName()); 728 if (Plan.BackedgeTakenCount) { 729 OS << ", where:\\n"; 730 Plan.BackedgeTakenCount->print(OS, SlotTracker); 731 OS << " := BackedgeTakenCount"; 732 } 733 OS << "\"]\n"; 734 OS << "node [shape=rect, fontname=Courier, fontsize=30]\n"; 735 OS << "edge [fontname=Courier, fontsize=30]\n"; 736 OS << "compound=true\n"; 737 738 for (const VPBlockBase *Block : depth_first(Plan.getEntry())) 739 dumpBlock(Block); 740 741 OS << "}\n"; 742 } 743 744 void VPlanPrinter::dumpBlock(const VPBlockBase *Block) { 745 if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block)) 746 dumpBasicBlock(BasicBlock); 747 else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 748 dumpRegion(Region); 749 else 750 llvm_unreachable("Unsupported kind of VPBlock."); 751 } 752 753 void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To, 754 bool Hidden, const Twine &Label) { 755 // Due to "dot" we print an edge between two regions as an edge between the 756 // exit basic block and the entry basic of the respective regions. 757 const VPBlockBase *Tail = From->getExitBasicBlock(); 758 const VPBlockBase *Head = To->getEntryBasicBlock(); 759 OS << Indent << getUID(Tail) << " -> " << getUID(Head); 760 OS << " [ label=\"" << Label << '\"'; 761 if (Tail != From) 762 OS << " ltail=" << getUID(From); 763 if (Head != To) 764 OS << " lhead=" << getUID(To); 765 if (Hidden) 766 OS << "; splines=none"; 767 OS << "]\n"; 768 } 769 770 void VPlanPrinter::dumpEdges(const VPBlockBase *Block) { 771 auto &Successors = Block->getSuccessors(); 772 if (Successors.size() == 1) 773 drawEdge(Block, Successors.front(), false, ""); 774 else if (Successors.size() == 2) { 775 drawEdge(Block, Successors.front(), false, "T"); 776 drawEdge(Block, Successors.back(), false, "F"); 777 } else { 778 unsigned SuccessorNumber = 0; 779 for (auto *Successor : Successors) 780 drawEdge(Block, Successor, false, Twine(SuccessorNumber++)); 781 } 782 } 783 784 void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) { 785 OS << Indent << getUID(BasicBlock) << " [label =\n"; 786 bumpIndent(1); 787 OS << Indent << "\"" << DOT::EscapeString(BasicBlock->getName()) << ":\\n\""; 788 bumpIndent(1); 789 790 // Dump the block predicate. 791 const VPValue *Pred = BasicBlock->getPredicate(); 792 if (Pred) { 793 OS << " +\n" << Indent << " \"BlockPredicate: "; 794 if (const VPInstruction *PredI = dyn_cast<VPInstruction>(Pred)) { 795 PredI->printAsOperand(OS, SlotTracker); 796 OS << " (" << DOT::EscapeString(PredI->getParent()->getName()) 797 << ")\\l\""; 798 } else 799 Pred->printAsOperand(OS, SlotTracker); 800 } 801 802 for (const VPRecipeBase &Recipe : *BasicBlock) { 803 OS << " +\n" << Indent; 804 Recipe.print(OS, Indent, SlotTracker); 805 OS << "\\l\""; 806 } 807 808 // Dump the condition bit. 809 const VPValue *CBV = BasicBlock->getCondBit(); 810 if (CBV) { 811 OS << " +\n" << Indent << " \"CondBit: "; 812 if (const VPInstruction *CBI = dyn_cast<VPInstruction>(CBV)) { 813 CBI->printAsOperand(OS, SlotTracker); 814 OS << " (" << DOT::EscapeString(CBI->getParent()->getName()) << ")\\l\""; 815 } else { 816 CBV->printAsOperand(OS, SlotTracker); 817 OS << "\""; 818 } 819 } 820 821 bumpIndent(-2); 822 OS << "\n" << Indent << "]\n"; 823 dumpEdges(BasicBlock); 824 } 825 826 void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) { 827 OS << Indent << "subgraph " << getUID(Region) << " {\n"; 828 bumpIndent(1); 829 OS << Indent << "fontname=Courier\n" 830 << Indent << "label=\"" 831 << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ") 832 << DOT::EscapeString(Region->getName()) << "\"\n"; 833 // Dump the blocks of the region. 834 assert(Region->getEntry() && "Region contains no inner blocks."); 835 for (const VPBlockBase *Block : depth_first(Region->getEntry())) 836 dumpBlock(Block); 837 bumpIndent(-1); 838 OS << Indent << "}\n"; 839 dumpEdges(Region); 840 } 841 842 void VPlanPrinter::printAsIngredient(raw_ostream &O, const Value *V) { 843 std::string IngredientString; 844 raw_string_ostream RSO(IngredientString); 845 if (auto *Inst = dyn_cast<Instruction>(V)) { 846 if (!Inst->getType()->isVoidTy()) { 847 Inst->printAsOperand(RSO, false); 848 RSO << " = "; 849 } 850 RSO << Inst->getOpcodeName() << " "; 851 unsigned E = Inst->getNumOperands(); 852 if (E > 0) { 853 Inst->getOperand(0)->printAsOperand(RSO, false); 854 for (unsigned I = 1; I < E; ++I) 855 Inst->getOperand(I)->printAsOperand(RSO << ", ", false); 856 } 857 } else // !Inst 858 V->printAsOperand(RSO, false); 859 RSO.flush(); 860 O << DOT::EscapeString(IngredientString); 861 } 862 863 void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent, 864 VPSlotTracker &SlotTracker) const { 865 O << "\"WIDEN-CALL "; 866 867 auto *CI = cast<CallInst>(getUnderlyingInstr()); 868 if (CI->getType()->isVoidTy()) 869 O << "void "; 870 else { 871 printAsOperand(O, SlotTracker); 872 O << " = "; 873 } 874 875 O << "call @" << CI->getCalledFunction()->getName() << "("; 876 printOperands(O, SlotTracker); 877 O << ")"; 878 } 879 880 void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent, 881 VPSlotTracker &SlotTracker) const { 882 O << "\"WIDEN-SELECT "; 883 printAsOperand(O, SlotTracker); 884 O << " = select "; 885 getOperand(0)->printAsOperand(O, SlotTracker); 886 O << ", "; 887 getOperand(1)->printAsOperand(O, SlotTracker); 888 O << ", "; 889 getOperand(2)->printAsOperand(O, SlotTracker); 890 O << (InvariantCond ? " (condition is loop invariant)" : ""); 891 } 892 893 void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent, 894 VPSlotTracker &SlotTracker) const { 895 O << "\"WIDEN "; 896 printAsOperand(O, SlotTracker); 897 O << " = " << getUnderlyingInstr()->getOpcodeName() << " "; 898 printOperands(O, SlotTracker); 899 } 900 901 void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O, const Twine &Indent, 902 VPSlotTracker &SlotTracker) const { 903 O << "\"WIDEN-INDUCTION"; 904 if (Trunc) { 905 O << "\\l\""; 906 O << " +\n" << Indent << "\" " << VPlanIngredient(IV) << "\\l\""; 907 O << " +\n" << Indent << "\" " << VPlanIngredient(Trunc); 908 } else 909 O << " " << VPlanIngredient(IV); 910 } 911 912 void VPWidenGEPRecipe::print(raw_ostream &O, const Twine &Indent, 913 VPSlotTracker &SlotTracker) const { 914 O << "\"WIDEN-GEP "; 915 O << (IsPtrLoopInvariant ? "Inv" : "Var"); 916 size_t IndicesNumber = IsIndexLoopInvariant.size(); 917 for (size_t I = 0; I < IndicesNumber; ++I) 918 O << "[" << (IsIndexLoopInvariant[I] ? "Inv" : "Var") << "]"; 919 920 O << " "; 921 printAsOperand(O, SlotTracker); 922 O << " = getelementptr "; 923 printOperands(O, SlotTracker); 924 } 925 926 void VPWidenPHIRecipe::print(raw_ostream &O, const Twine &Indent, 927 VPSlotTracker &SlotTracker) const { 928 O << "\"WIDEN-PHI " << VPlanIngredient(Phi); 929 } 930 931 void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent, 932 VPSlotTracker &SlotTracker) const { 933 O << "\"BLEND "; 934 Phi->printAsOperand(O, false); 935 O << " ="; 936 if (getNumIncomingValues() == 1) { 937 // Not a User of any mask: not really blending, this is a 938 // single-predecessor phi. 939 O << " "; 940 getIncomingValue(0)->printAsOperand(O, SlotTracker); 941 } else { 942 for (unsigned I = 0, E = getNumIncomingValues(); I < E; ++I) { 943 O << " "; 944 getIncomingValue(I)->printAsOperand(O, SlotTracker); 945 O << "/"; 946 getMask(I)->printAsOperand(O, SlotTracker); 947 } 948 } 949 } 950 951 void VPReductionRecipe::print(raw_ostream &O, const Twine &Indent, 952 VPSlotTracker &SlotTracker) const { 953 O << "\"REDUCE "; 954 printAsOperand(O, SlotTracker); 955 O << " = "; 956 getChainOp()->printAsOperand(O, SlotTracker); 957 O << " + reduce." << Instruction::getOpcodeName(RdxDesc->getRecurrenceBinOp()) 958 << " ("; 959 getVecOp()->printAsOperand(O, SlotTracker); 960 if (getCondOp()) { 961 O << ", "; 962 getCondOp()->printAsOperand(O, SlotTracker); 963 } 964 O << ")"; 965 } 966 967 void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent, 968 VPSlotTracker &SlotTracker) const { 969 O << "\"" << (IsUniform ? "CLONE " : "REPLICATE ") 970 << VPlanIngredient(getUnderlyingInstr()); 971 if (AlsoPack) 972 O << " (S->V)"; 973 } 974 975 void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent, 976 VPSlotTracker &SlotTracker) const { 977 O << "\"PHI-PREDICATED-INSTRUCTION " << VPlanIngredient(PredInst); 978 } 979 980 void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent, 981 VPSlotTracker &SlotTracker) const { 982 O << "\"WIDEN "; 983 984 if (!isStore()) { 985 printAsOperand(O, SlotTracker); 986 O << " = "; 987 } 988 O << Instruction::getOpcodeName(getUnderlyingInstr()->getOpcode()) << " "; 989 990 printOperands(O, SlotTracker); 991 } 992 993 void VPWidenCanonicalIVRecipe::execute(VPTransformState &State) { 994 Value *CanonicalIV = State.CanonicalIV; 995 Type *STy = CanonicalIV->getType(); 996 IRBuilder<> Builder(State.CFG.PrevBB->getTerminator()); 997 ElementCount VF = State.VF; 998 assert(!VF.isScalable() && "the code following assumes non scalables ECs"); 999 Value *VStart = VF.isScalar() 1000 ? CanonicalIV 1001 : Builder.CreateVectorSplat(VF.getKnownMinValue(), 1002 CanonicalIV, "broadcast"); 1003 for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) { 1004 SmallVector<Constant *, 8> Indices; 1005 for (unsigned Lane = 0; Lane < VF.getKnownMinValue(); ++Lane) 1006 Indices.push_back( 1007 ConstantInt::get(STy, Part * VF.getKnownMinValue() + Lane)); 1008 // If VF == 1, there is only one iteration in the loop above, thus the 1009 // element pushed back into Indices is ConstantInt::get(STy, Part) 1010 Constant *VStep = 1011 VF.isScalar() ? Indices.back() : ConstantVector::get(Indices); 1012 // Add the consecutive indices to the vector value. 1013 Value *CanonicalVectorIV = Builder.CreateAdd(VStart, VStep, "vec.iv"); 1014 State.set(getVPValue(), CanonicalVectorIV, Part); 1015 } 1016 } 1017 1018 void VPWidenCanonicalIVRecipe::print(raw_ostream &O, const Twine &Indent, 1019 VPSlotTracker &SlotTracker) const { 1020 O << "\"EMIT "; 1021 getVPValue()->printAsOperand(O, SlotTracker); 1022 O << " = WIDEN-CANONICAL-INDUCTION"; 1023 } 1024 1025 template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT); 1026 1027 void VPValue::replaceAllUsesWith(VPValue *New) { 1028 for (unsigned J = 0; J < getNumUsers();) { 1029 VPUser *User = Users[J]; 1030 unsigned NumUsers = getNumUsers(); 1031 for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) 1032 if (User->getOperand(I) == this) 1033 User->setOperand(I, New); 1034 // If a user got removed after updating the current user, the next user to 1035 // update will be moved to the current position, so we only need to 1036 // increment the index if the number of users did not change. 1037 if (NumUsers == getNumUsers()) 1038 J++; 1039 } 1040 } 1041 1042 void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const { 1043 if (const Value *UV = getUnderlyingValue()) { 1044 OS << "ir<"; 1045 UV->printAsOperand(OS, false); 1046 OS << ">"; 1047 return; 1048 } 1049 1050 unsigned Slot = Tracker.getSlot(this); 1051 if (Slot == unsigned(-1)) 1052 OS << "<badref>"; 1053 else 1054 OS << "vp<%" << Tracker.getSlot(this) << ">"; 1055 } 1056 1057 void VPUser::printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const { 1058 bool First = true; 1059 for (VPValue *Op : operands()) { 1060 if (!First) 1061 O << ", "; 1062 Op->printAsOperand(O, SlotTracker); 1063 First = false; 1064 } 1065 } 1066 1067 void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region, 1068 Old2NewTy &Old2New, 1069 InterleavedAccessInfo &IAI) { 1070 ReversePostOrderTraversal<VPBlockBase *> RPOT(Region->getEntry()); 1071 for (VPBlockBase *Base : RPOT) { 1072 visitBlock(Base, Old2New, IAI); 1073 } 1074 } 1075 1076 void VPInterleavedAccessInfo::visitBlock(VPBlockBase *Block, Old2NewTy &Old2New, 1077 InterleavedAccessInfo &IAI) { 1078 if (VPBasicBlock *VPBB = dyn_cast<VPBasicBlock>(Block)) { 1079 for (VPRecipeBase &VPI : *VPBB) { 1080 assert(isa<VPInstruction>(&VPI) && "Can only handle VPInstructions"); 1081 auto *VPInst = cast<VPInstruction>(&VPI); 1082 auto *Inst = cast<Instruction>(VPInst->getUnderlyingValue()); 1083 auto *IG = IAI.getInterleaveGroup(Inst); 1084 if (!IG) 1085 continue; 1086 1087 auto NewIGIter = Old2New.find(IG); 1088 if (NewIGIter == Old2New.end()) 1089 Old2New[IG] = new InterleaveGroup<VPInstruction>( 1090 IG->getFactor(), IG->isReverse(), IG->getAlign()); 1091 1092 if (Inst == IG->getInsertPos()) 1093 Old2New[IG]->setInsertPos(VPInst); 1094 1095 InterleaveGroupMap[VPInst] = Old2New[IG]; 1096 InterleaveGroupMap[VPInst]->insertMember( 1097 VPInst, IG->getIndex(Inst), 1098 Align(IG->isReverse() ? (-1) * int(IG->getFactor()) 1099 : IG->getFactor())); 1100 } 1101 } else if (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 1102 visitRegion(Region, Old2New, IAI); 1103 else 1104 llvm_unreachable("Unsupported kind of VPBlock."); 1105 } 1106 1107 VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan, 1108 InterleavedAccessInfo &IAI) { 1109 Old2NewTy Old2New; 1110 visitRegion(cast<VPRegionBlock>(Plan.getEntry()), Old2New, IAI); 1111 } 1112 1113 void VPSlotTracker::assignSlot(const VPValue *V) { 1114 assert(Slots.find(V) == Slots.end() && "VPValue already has a slot!"); 1115 const Value *UV = V->getUnderlyingValue(); 1116 if (UV) 1117 return; 1118 const auto *VPI = dyn_cast<VPInstruction>(V); 1119 if (VPI && !VPI->hasResult()) 1120 return; 1121 1122 Slots[V] = NextSlot++; 1123 } 1124 1125 void VPSlotTracker::assignSlots(const VPBlockBase *VPBB) { 1126 if (auto *Region = dyn_cast<VPRegionBlock>(VPBB)) 1127 assignSlots(Region); 1128 else 1129 assignSlots(cast<VPBasicBlock>(VPBB)); 1130 } 1131 1132 void VPSlotTracker::assignSlots(const VPRegionBlock *Region) { 1133 ReversePostOrderTraversal<const VPBlockBase *> RPOT(Region->getEntry()); 1134 for (const VPBlockBase *Block : RPOT) 1135 assignSlots(Block); 1136 } 1137 1138 void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) { 1139 for (const VPRecipeBase &Recipe : *VPBB) { 1140 if (const auto *VPI = dyn_cast<VPInstruction>(&Recipe)) 1141 assignSlot(VPI); 1142 else if (const auto *VPIV = dyn_cast<VPWidenCanonicalIVRecipe>(&Recipe)) 1143 assignSlot(VPIV->getVPValue()); 1144 } 1145 } 1146 1147 void VPSlotTracker::assignSlots(const VPlan &Plan) { 1148 1149 for (const VPValue *V : Plan.VPExternalDefs) 1150 assignSlot(V); 1151 1152 for (auto &E : Plan.Value2VPValue) 1153 if (!isa<VPInstruction>(E.second)) 1154 assignSlot(E.second); 1155 1156 for (const VPValue *V : Plan.VPCBVs) 1157 assignSlot(V); 1158 1159 if (Plan.BackedgeTakenCount) 1160 assignSlot(Plan.BackedgeTakenCount); 1161 1162 ReversePostOrderTraversal<const VPBlockBase *> RPOT(Plan.getEntry()); 1163 for (const VPBlockBase *Block : RPOT) 1164 assignSlots(Block); 1165 } 1166