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