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