11f58dda4SAyal Zaks //===- VPlan.cpp - Vectorizer Plan ----------------------------------------===// 21f58dda4SAyal Zaks // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61f58dda4SAyal Zaks // 71f58dda4SAyal Zaks //===----------------------------------------------------------------------===// 81f58dda4SAyal Zaks /// 91f58dda4SAyal Zaks /// \file 101f58dda4SAyal Zaks /// This is the LLVM vectorization plan. It represents a candidate for 111f58dda4SAyal Zaks /// vectorization, allowing to plan and optimize how to vectorize a given loop 121f58dda4SAyal Zaks /// before generating LLVM-IR. 131f58dda4SAyal Zaks /// The vectorizer uses vectorization plans to estimate the costs of potential 141f58dda4SAyal Zaks /// candidates and if profitable to execute the desired plan, generating vector 151f58dda4SAyal Zaks /// LLVM-IR code. 161f58dda4SAyal Zaks /// 171f58dda4SAyal Zaks //===----------------------------------------------------------------------===// 181f58dda4SAyal Zaks 191f58dda4SAyal Zaks #include "VPlan.h" 202a34ac86SDiego Caballero #include "VPlanDominatorTree.h" 216cadde7fSEugene Zelenko #include "llvm/ADT/DepthFirstIterator.h" 221f58dda4SAyal Zaks #include "llvm/ADT/PostOrderIterator.h" 236cadde7fSEugene Zelenko #include "llvm/ADT/SmallVector.h" 246cadde7fSEugene Zelenko #include "llvm/ADT/Twine.h" 251f58dda4SAyal Zaks #include "llvm/Analysis/LoopInfo.h" 261f58dda4SAyal Zaks #include "llvm/IR/BasicBlock.h" 276cadde7fSEugene Zelenko #include "llvm/IR/CFG.h" 286cadde7fSEugene Zelenko #include "llvm/IR/InstrTypes.h" 296cadde7fSEugene Zelenko #include "llvm/IR/Instruction.h" 306cadde7fSEugene Zelenko #include "llvm/IR/Instructions.h" 316cadde7fSEugene Zelenko #include "llvm/IR/Type.h" 326cadde7fSEugene Zelenko #include "llvm/IR/Value.h" 336cadde7fSEugene Zelenko #include "llvm/Support/Casting.h" 344c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h" 356cadde7fSEugene Zelenko #include "llvm/Support/Debug.h" 366cadde7fSEugene Zelenko #include "llvm/Support/ErrorHandling.h" 372a34ac86SDiego Caballero #include "llvm/Support/GenericDomTreeConstruction.h" 381f58dda4SAyal Zaks #include "llvm/Support/GraphWriter.h" 396cadde7fSEugene Zelenko #include "llvm/Support/raw_ostream.h" 401f58dda4SAyal Zaks #include "llvm/Transforms/Utils/BasicBlockUtils.h" 416cadde7fSEugene Zelenko #include <cassert> 426cadde7fSEugene Zelenko #include <iterator> 436cadde7fSEugene Zelenko #include <string> 446cadde7fSEugene Zelenko #include <vector> 451f58dda4SAyal Zaks 461f58dda4SAyal Zaks using namespace llvm; 47ea7f3035SHideki Saito extern cl::opt<bool> EnableVPlanNativePath; 481f58dda4SAyal Zaks 491f58dda4SAyal Zaks #define DEBUG_TYPE "vplan" 501f58dda4SAyal Zaks 518b9d1f3cSGil Rapaport raw_ostream &llvm::operator<<(raw_ostream &OS, const VPValue &V) { 5240e7bfc4SFlorian Hahn const VPInstruction *Instr = dyn_cast<VPInstruction>(&V); 5340e7bfc4SFlorian Hahn VPSlotTracker SlotTracker( 5440e7bfc4SFlorian Hahn (Instr && Instr->getParent()) ? Instr->getParent()->getPlan() : nullptr); 5540e7bfc4SFlorian Hahn V.print(OS, SlotTracker); 568b9d1f3cSGil Rapaport return OS; 578b9d1f3cSGil Rapaport } 588b9d1f3cSGil Rapaport 5940e7bfc4SFlorian Hahn void VPValue::print(raw_ostream &OS, VPSlotTracker &SlotTracker) const { 6040e7bfc4SFlorian Hahn if (const VPInstruction *Instr = dyn_cast<VPInstruction>(this)) 6140e7bfc4SFlorian Hahn Instr->print(OS, SlotTracker); 6240e7bfc4SFlorian Hahn else 6340e7bfc4SFlorian Hahn printAsOperand(OS, SlotTracker); 6440e7bfc4SFlorian Hahn } 6540e7bfc4SFlorian Hahn 6605afa555SFlorian Hahn // Get the top-most entry block of \p Start. This is the entry block of the 6705afa555SFlorian Hahn // containing VPlan. This function is templated to support both const and non-const blocks 6805afa555SFlorian Hahn template <typename T> static T *getPlanEntry(T *Start) { 6905afa555SFlorian Hahn T *Next = Start; 7005afa555SFlorian Hahn T *Current = Start; 7105afa555SFlorian Hahn while ((Next = Next->getParent())) 7205afa555SFlorian Hahn Current = Next; 7305afa555SFlorian Hahn 7405afa555SFlorian Hahn SmallSetVector<T *, 8> WorkList; 7505afa555SFlorian Hahn WorkList.insert(Current); 7605afa555SFlorian Hahn 7705afa555SFlorian Hahn for (unsigned i = 0; i < WorkList.size(); i++) { 7805afa555SFlorian Hahn T *Current = WorkList[i]; 7905afa555SFlorian Hahn if (Current->getNumPredecessors() == 0) 8005afa555SFlorian Hahn return Current; 8105afa555SFlorian Hahn auto &Predecessors = Current->getPredecessors(); 8205afa555SFlorian Hahn WorkList.insert(Predecessors.begin(), Predecessors.end()); 8305afa555SFlorian Hahn } 8405afa555SFlorian Hahn 8505afa555SFlorian Hahn llvm_unreachable("VPlan without any entry node without predecessors"); 8605afa555SFlorian Hahn } 8705afa555SFlorian Hahn 8805afa555SFlorian Hahn VPlan *VPBlockBase::getPlan() { return getPlanEntry(this)->Plan; } 8905afa555SFlorian Hahn 9005afa555SFlorian Hahn const VPlan *VPBlockBase::getPlan() const { return getPlanEntry(this)->Plan; } 9105afa555SFlorian Hahn 921f58dda4SAyal Zaks /// \return the VPBasicBlock that is the entry of Block, possibly indirectly. 931f58dda4SAyal Zaks const VPBasicBlock *VPBlockBase::getEntryBasicBlock() const { 941f58dda4SAyal Zaks const VPBlockBase *Block = this; 951f58dda4SAyal Zaks while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 961f58dda4SAyal Zaks Block = Region->getEntry(); 971f58dda4SAyal Zaks return cast<VPBasicBlock>(Block); 981f58dda4SAyal Zaks } 991f58dda4SAyal Zaks 1001f58dda4SAyal Zaks VPBasicBlock *VPBlockBase::getEntryBasicBlock() { 1011f58dda4SAyal Zaks VPBlockBase *Block = this; 1021f58dda4SAyal Zaks while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 1031f58dda4SAyal Zaks Block = Region->getEntry(); 1041f58dda4SAyal Zaks return cast<VPBasicBlock>(Block); 1051f58dda4SAyal Zaks } 1061f58dda4SAyal Zaks 10705afa555SFlorian Hahn void VPBlockBase::setPlan(VPlan *ParentPlan) { 10805afa555SFlorian Hahn assert(ParentPlan->getEntry() == this && 10905afa555SFlorian Hahn "Can only set plan on its entry block."); 11005afa555SFlorian Hahn Plan = ParentPlan; 11105afa555SFlorian Hahn } 11205afa555SFlorian Hahn 1131f58dda4SAyal Zaks /// \return the VPBasicBlock that is the exit of Block, possibly indirectly. 1141f58dda4SAyal Zaks const VPBasicBlock *VPBlockBase::getExitBasicBlock() const { 1151f58dda4SAyal Zaks const VPBlockBase *Block = this; 1161f58dda4SAyal Zaks while (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 1171f58dda4SAyal Zaks Block = Region->getExit(); 1181f58dda4SAyal Zaks return cast<VPBasicBlock>(Block); 1191f58dda4SAyal Zaks } 1201f58dda4SAyal Zaks 1211f58dda4SAyal Zaks VPBasicBlock *VPBlockBase::getExitBasicBlock() { 1221f58dda4SAyal Zaks VPBlockBase *Block = this; 1231f58dda4SAyal Zaks while (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 1241f58dda4SAyal Zaks Block = Region->getExit(); 1251f58dda4SAyal Zaks return cast<VPBasicBlock>(Block); 1261f58dda4SAyal Zaks } 1271f58dda4SAyal Zaks 1281f58dda4SAyal Zaks VPBlockBase *VPBlockBase::getEnclosingBlockWithSuccessors() { 1291f58dda4SAyal Zaks if (!Successors.empty() || !Parent) 1301f58dda4SAyal Zaks return this; 1311f58dda4SAyal Zaks assert(Parent->getExit() == this && 1321f58dda4SAyal Zaks "Block w/o successors not the exit of its parent."); 1331f58dda4SAyal Zaks return Parent->getEnclosingBlockWithSuccessors(); 1341f58dda4SAyal Zaks } 1351f58dda4SAyal Zaks 1361f58dda4SAyal Zaks VPBlockBase *VPBlockBase::getEnclosingBlockWithPredecessors() { 1371f58dda4SAyal Zaks if (!Predecessors.empty() || !Parent) 1381f58dda4SAyal Zaks return this; 1391f58dda4SAyal Zaks assert(Parent->getEntry() == this && 1401f58dda4SAyal Zaks "Block w/o predecessors not the entry of its parent."); 1411f58dda4SAyal Zaks return Parent->getEnclosingBlockWithPredecessors(); 1421f58dda4SAyal Zaks } 1431f58dda4SAyal Zaks 1441f58dda4SAyal Zaks void VPBlockBase::deleteCFG(VPBlockBase *Entry) { 1451f58dda4SAyal Zaks SmallVector<VPBlockBase *, 8> Blocks; 1461f58dda4SAyal Zaks for (VPBlockBase *Block : depth_first(Entry)) 1471f58dda4SAyal Zaks Blocks.push_back(Block); 1481f58dda4SAyal Zaks 1491f58dda4SAyal Zaks for (VPBlockBase *Block : Blocks) 1501f58dda4SAyal Zaks delete Block; 1511f58dda4SAyal Zaks } 1521f58dda4SAyal Zaks 1531f58dda4SAyal Zaks BasicBlock * 1541f58dda4SAyal Zaks VPBasicBlock::createEmptyBasicBlock(VPTransformState::CFGState &CFG) { 1551f58dda4SAyal Zaks // BB stands for IR BasicBlocks. VPBB stands for VPlan VPBasicBlocks. 1561f58dda4SAyal Zaks // Pred stands for Predessor. Prev stands for Previous - last visited/created. 1571f58dda4SAyal Zaks BasicBlock *PrevBB = CFG.PrevBB; 1581f58dda4SAyal Zaks BasicBlock *NewBB = BasicBlock::Create(PrevBB->getContext(), getName(), 1591f58dda4SAyal Zaks PrevBB->getParent(), CFG.LastBB); 160d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: created " << NewBB->getName() << '\n'); 1611f58dda4SAyal Zaks 1621f58dda4SAyal Zaks // Hook up the new basic block to its predecessors. 1631f58dda4SAyal Zaks for (VPBlockBase *PredVPBlock : getHierarchicalPredecessors()) { 1641f58dda4SAyal Zaks VPBasicBlock *PredVPBB = PredVPBlock->getExitBasicBlock(); 1651f58dda4SAyal Zaks auto &PredVPSuccessors = PredVPBB->getSuccessors(); 1661f58dda4SAyal Zaks BasicBlock *PredBB = CFG.VPBB2IRBB[PredVPBB]; 167ea7f3035SHideki Saito 168ea7f3035SHideki Saito // In outer loop vectorization scenario, the predecessor BBlock may not yet 169ea7f3035SHideki Saito // be visited(backedge). Mark the VPBasicBlock for fixup at the end of 170ea7f3035SHideki Saito // vectorization. We do not encounter this case in inner loop vectorization 171ea7f3035SHideki Saito // as we start out by building a loop skeleton with the vector loop header 172ea7f3035SHideki Saito // and latch blocks. As a result, we never enter this function for the 173ea7f3035SHideki Saito // header block in the non VPlan-native path. 174ea7f3035SHideki Saito if (!PredBB) { 175ea7f3035SHideki Saito assert(EnableVPlanNativePath && 176ea7f3035SHideki Saito "Unexpected null predecessor in non VPlan-native path"); 177ea7f3035SHideki Saito CFG.VPBBsToFix.push_back(PredVPBB); 178ea7f3035SHideki Saito continue; 179ea7f3035SHideki Saito } 180ea7f3035SHideki Saito 1811f58dda4SAyal Zaks assert(PredBB && "Predecessor basic-block not found building successor."); 1821f58dda4SAyal Zaks auto *PredBBTerminator = PredBB->getTerminator(); 183d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: draw edge from" << PredBB->getName() << '\n'); 1841f58dda4SAyal Zaks if (isa<UnreachableInst>(PredBBTerminator)) { 1851f58dda4SAyal Zaks assert(PredVPSuccessors.size() == 1 && 1861f58dda4SAyal Zaks "Predecessor ending w/o branch must have single successor."); 1871f58dda4SAyal Zaks PredBBTerminator->eraseFromParent(); 1881f58dda4SAyal Zaks BranchInst::Create(NewBB, PredBB); 1891f58dda4SAyal Zaks } else { 1901f58dda4SAyal Zaks assert(PredVPSuccessors.size() == 2 && 1911f58dda4SAyal Zaks "Predecessor ending with branch must have two successors."); 1921f58dda4SAyal Zaks unsigned idx = PredVPSuccessors.front() == this ? 0 : 1; 1931f58dda4SAyal Zaks assert(!PredBBTerminator->getSuccessor(idx) && 1941f58dda4SAyal Zaks "Trying to reset an existing successor block."); 1951f58dda4SAyal Zaks PredBBTerminator->setSuccessor(idx, NewBB); 1961f58dda4SAyal Zaks } 1971f58dda4SAyal Zaks } 1981f58dda4SAyal Zaks return NewBB; 1991f58dda4SAyal Zaks } 2001f58dda4SAyal Zaks 2011f58dda4SAyal Zaks void VPBasicBlock::execute(VPTransformState *State) { 2021f58dda4SAyal Zaks bool Replica = State->Instance && 2031f58dda4SAyal Zaks !(State->Instance->Part == 0 && State->Instance->Lane == 0); 2041f58dda4SAyal Zaks VPBasicBlock *PrevVPBB = State->CFG.PrevVPBB; 2051f58dda4SAyal Zaks VPBlockBase *SingleHPred = nullptr; 2061f58dda4SAyal Zaks BasicBlock *NewBB = State->CFG.PrevBB; // Reuse it if possible. 2071f58dda4SAyal Zaks 2081f58dda4SAyal Zaks // 1. Create an IR basic block, or reuse the last one if possible. 2091f58dda4SAyal Zaks // The last IR basic block is reused, as an optimization, in three cases: 2101f58dda4SAyal Zaks // A. the first VPBB reuses the loop header BB - when PrevVPBB is null; 2111f58dda4SAyal Zaks // B. when the current VPBB has a single (hierarchical) predecessor which 2121f58dda4SAyal Zaks // is PrevVPBB and the latter has a single (hierarchical) successor; and 2131f58dda4SAyal Zaks // C. when the current VPBB is an entry of a region replica - where PrevVPBB 2141f58dda4SAyal Zaks // is the exit of this region from a previous instance, or the predecessor 2151f58dda4SAyal Zaks // of this region. 2161f58dda4SAyal Zaks if (PrevVPBB && /* A */ 2171f58dda4SAyal Zaks !((SingleHPred = getSingleHierarchicalPredecessor()) && 2181f58dda4SAyal Zaks SingleHPred->getExitBasicBlock() == PrevVPBB && 2191f58dda4SAyal Zaks PrevVPBB->getSingleHierarchicalSuccessor()) && /* B */ 2201f58dda4SAyal Zaks !(Replica && getPredecessors().empty())) { /* C */ 2211f58dda4SAyal Zaks NewBB = createEmptyBasicBlock(State->CFG); 2221f58dda4SAyal Zaks State->Builder.SetInsertPoint(NewBB); 2231f58dda4SAyal Zaks // Temporarily terminate with unreachable until CFG is rewired. 2241f58dda4SAyal Zaks UnreachableInst *Terminator = State->Builder.CreateUnreachable(); 2251f58dda4SAyal Zaks State->Builder.SetInsertPoint(Terminator); 2261f58dda4SAyal Zaks // Register NewBB in its loop. In innermost loops its the same for all BB's. 2271f58dda4SAyal Zaks Loop *L = State->LI->getLoopFor(State->CFG.LastBB); 2281f58dda4SAyal Zaks L->addBasicBlockToLoop(NewBB, *State->LI); 2291f58dda4SAyal Zaks State->CFG.PrevBB = NewBB; 2301f58dda4SAyal Zaks } 2311f58dda4SAyal Zaks 2321f58dda4SAyal Zaks // 2. Fill the IR basic block with IR instructions. 233d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: vectorizing VPBB:" << getName() 2341f58dda4SAyal Zaks << " in BB:" << NewBB->getName() << '\n'); 2351f58dda4SAyal Zaks 2361f58dda4SAyal Zaks State->CFG.VPBB2IRBB[this] = NewBB; 2371f58dda4SAyal Zaks State->CFG.PrevVPBB = this; 2381f58dda4SAyal Zaks 2391f58dda4SAyal Zaks for (VPRecipeBase &Recipe : Recipes) 2401f58dda4SAyal Zaks Recipe.execute(*State); 2411f58dda4SAyal Zaks 242ea7f3035SHideki Saito VPValue *CBV; 243ea7f3035SHideki Saito if (EnableVPlanNativePath && (CBV = getCondBit())) { 244ea7f3035SHideki Saito Value *IRCBV = CBV->getUnderlyingValue(); 245ea7f3035SHideki Saito assert(IRCBV && "Unexpected null underlying value for condition bit"); 246ea7f3035SHideki Saito 247ea7f3035SHideki Saito // Condition bit value in a VPBasicBlock is used as the branch selector. In 248ea7f3035SHideki Saito // the VPlan-native path case, since all branches are uniform we generate a 249ea7f3035SHideki Saito // branch instruction using the condition value from vector lane 0 and dummy 250ea7f3035SHideki Saito // successors. The successors are fixed later when the successor blocks are 251ea7f3035SHideki Saito // visited. 252ea7f3035SHideki Saito Value *NewCond = State->Callback.getOrCreateVectorValues(IRCBV, 0); 253ea7f3035SHideki Saito NewCond = State->Builder.CreateExtractElement(NewCond, 254ea7f3035SHideki Saito State->Builder.getInt32(0)); 255ea7f3035SHideki Saito 256ea7f3035SHideki Saito // Replace the temporary unreachable terminator with the new conditional 257ea7f3035SHideki Saito // branch. 258ea7f3035SHideki Saito auto *CurrentTerminator = NewBB->getTerminator(); 259ea7f3035SHideki Saito assert(isa<UnreachableInst>(CurrentTerminator) && 260ea7f3035SHideki Saito "Expected to replace unreachable terminator with conditional " 261ea7f3035SHideki Saito "branch."); 262ea7f3035SHideki Saito auto *CondBr = BranchInst::Create(NewBB, nullptr, NewCond); 263ea7f3035SHideki Saito CondBr->setSuccessor(0, nullptr); 264ea7f3035SHideki Saito ReplaceInstWithInst(CurrentTerminator, CondBr); 265ea7f3035SHideki Saito } 266ea7f3035SHideki Saito 267d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: filled BB:" << *NewBB); 2681f58dda4SAyal Zaks } 2691f58dda4SAyal Zaks 2701f58dda4SAyal Zaks void VPRegionBlock::execute(VPTransformState *State) { 2711f58dda4SAyal Zaks ReversePostOrderTraversal<VPBlockBase *> RPOT(Entry); 2721f58dda4SAyal Zaks 2731f58dda4SAyal Zaks if (!isReplicator()) { 2741f58dda4SAyal Zaks // Visit the VPBlocks connected to "this", starting from it. 2751f58dda4SAyal Zaks for (VPBlockBase *Block : RPOT) { 276ea7f3035SHideki Saito if (EnableVPlanNativePath) { 277ea7f3035SHideki Saito // The inner loop vectorization path does not represent loop preheader 278ea7f3035SHideki Saito // and exit blocks as part of the VPlan. In the VPlan-native path, skip 279ea7f3035SHideki Saito // vectorizing loop preheader block. In future, we may replace this 280ea7f3035SHideki Saito // check with the check for loop preheader. 281ea7f3035SHideki Saito if (Block->getNumPredecessors() == 0) 282ea7f3035SHideki Saito continue; 283ea7f3035SHideki Saito 284ea7f3035SHideki Saito // Skip vectorizing loop exit block. In future, we may replace this 285ea7f3035SHideki Saito // check with the check for loop exit. 286ea7f3035SHideki Saito if (Block->getNumSuccessors() == 0) 287ea7f3035SHideki Saito continue; 288ea7f3035SHideki Saito } 289ea7f3035SHideki Saito 290d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n'); 2911f58dda4SAyal Zaks Block->execute(State); 2921f58dda4SAyal Zaks } 2931f58dda4SAyal Zaks return; 2941f58dda4SAyal Zaks } 2951f58dda4SAyal Zaks 2961f58dda4SAyal Zaks assert(!State->Instance && "Replicating a Region with non-null instance."); 2971f58dda4SAyal Zaks 2981f58dda4SAyal Zaks // Enter replicating mode. 2991f58dda4SAyal Zaks State->Instance = {0, 0}; 3001f58dda4SAyal Zaks 3011f58dda4SAyal Zaks for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) { 3021f58dda4SAyal Zaks State->Instance->Part = Part; 3031f58dda4SAyal Zaks for (unsigned Lane = 0, VF = State->VF; Lane < VF; ++Lane) { 3041f58dda4SAyal Zaks State->Instance->Lane = Lane; 3051f58dda4SAyal Zaks // Visit the VPBlocks connected to \p this, starting from it. 3061f58dda4SAyal Zaks for (VPBlockBase *Block : RPOT) { 307d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "LV: VPBlock in RPO " << Block->getName() << '\n'); 3081f58dda4SAyal Zaks Block->execute(State); 3091f58dda4SAyal Zaks } 3101f58dda4SAyal Zaks } 3111f58dda4SAyal Zaks } 3121f58dda4SAyal Zaks 3131f58dda4SAyal Zaks // Exit replicating mode. 3141f58dda4SAyal Zaks State->Instance.reset(); 3151f58dda4SAyal Zaks } 3161f58dda4SAyal Zaks 3177591e4e9SFlorian Hahn void VPRecipeBase::insertBefore(VPRecipeBase *InsertPos) { 3187f152543SGil Rapaport assert(!Parent && "Recipe already in some VPBasicBlock"); 3197f152543SGil Rapaport assert(InsertPos->getParent() && 3207f152543SGil Rapaport "Insertion position not in any VPBasicBlock"); 3213bcff366SFlorian Hahn Parent = InsertPos->getParent(); 3223bcff366SFlorian Hahn Parent->getRecipeList().insert(InsertPos->getIterator(), this); 3237591e4e9SFlorian Hahn } 3247591e4e9SFlorian Hahn 3257f152543SGil Rapaport void VPRecipeBase::insertAfter(VPRecipeBase *InsertPos) { 3267f152543SGil Rapaport assert(!Parent && "Recipe already in some VPBasicBlock"); 3277f152543SGil Rapaport assert(InsertPos->getParent() && 3287f152543SGil Rapaport "Insertion position not in any VPBasicBlock"); 3297f152543SGil Rapaport Parent = InsertPos->getParent(); 3307f152543SGil Rapaport Parent->getRecipeList().insertAfter(InsertPos->getIterator(), this); 3317f152543SGil Rapaport } 3327f152543SGil Rapaport 3337f152543SGil Rapaport void VPRecipeBase::removeFromParent() { 3347f152543SGil Rapaport assert(getParent() && "Recipe not in any VPBasicBlock"); 3357f152543SGil Rapaport getParent()->getRecipeList().remove(getIterator()); 3367f152543SGil Rapaport Parent = nullptr; 3377f152543SGil Rapaport } 3387f152543SGil Rapaport 33963cbcf98SFlorian Hahn iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() { 3407f152543SGil Rapaport assert(getParent() && "Recipe not in any VPBasicBlock"); 34163cbcf98SFlorian Hahn return getParent()->getRecipeList().erase(getIterator()); 34263cbcf98SFlorian Hahn } 34363cbcf98SFlorian Hahn 34439d4c9fdSFlorian Hahn void VPRecipeBase::moveAfter(VPRecipeBase *InsertPos) { 3457f152543SGil Rapaport removeFromParent(); 3467f152543SGil Rapaport insertAfter(InsertPos); 34739d4c9fdSFlorian Hahn } 34839d4c9fdSFlorian Hahn 3498b9d1f3cSGil Rapaport void VPInstruction::generateInstruction(VPTransformState &State, 3508b9d1f3cSGil Rapaport unsigned Part) { 3518b9d1f3cSGil Rapaport IRBuilder<> &Builder = State.Builder; 3528b9d1f3cSGil Rapaport 3538b9d1f3cSGil Rapaport if (Instruction::isBinaryOp(getOpcode())) { 3548b9d1f3cSGil Rapaport Value *A = State.get(getOperand(0), Part); 3558b9d1f3cSGil Rapaport Value *B = State.get(getOperand(1), Part); 3568b9d1f3cSGil Rapaport Value *V = Builder.CreateBinOp((Instruction::BinaryOps)getOpcode(), A, B); 3578b9d1f3cSGil Rapaport State.set(this, V, Part); 3588b9d1f3cSGil Rapaport return; 3598b9d1f3cSGil Rapaport } 3608b9d1f3cSGil Rapaport 3618b9d1f3cSGil Rapaport switch (getOpcode()) { 3628b9d1f3cSGil Rapaport case VPInstruction::Not: { 3638b9d1f3cSGil Rapaport Value *A = State.get(getOperand(0), Part); 3648b9d1f3cSGil Rapaport Value *V = Builder.CreateNot(A); 3658b9d1f3cSGil Rapaport State.set(this, V, Part); 3668b9d1f3cSGil Rapaport break; 3678b9d1f3cSGil Rapaport } 368b0b5312eSAyal Zaks case VPInstruction::ICmpULE: { 369b0b5312eSAyal Zaks Value *IV = State.get(getOperand(0), Part); 370b0b5312eSAyal Zaks Value *TC = State.get(getOperand(1), Part); 371b0b5312eSAyal Zaks Value *V = Builder.CreateICmpULE(IV, TC); 372b0b5312eSAyal Zaks State.set(this, V, Part); 373b0b5312eSAyal Zaks break; 374b0b5312eSAyal Zaks } 375d15df0edSAyal Zaks case Instruction::Select: { 376d15df0edSAyal Zaks Value *Cond = State.get(getOperand(0), Part); 377d15df0edSAyal Zaks Value *Op1 = State.get(getOperand(1), Part); 378d15df0edSAyal Zaks Value *Op2 = State.get(getOperand(2), Part); 379d15df0edSAyal Zaks Value *V = Builder.CreateSelect(Cond, Op1, Op2); 380d15df0edSAyal Zaks State.set(this, V, Part); 381d15df0edSAyal Zaks break; 382d15df0edSAyal Zaks } 3838b9d1f3cSGil Rapaport default: 3848b9d1f3cSGil Rapaport llvm_unreachable("Unsupported opcode for instruction"); 3858b9d1f3cSGil Rapaport } 3868b9d1f3cSGil Rapaport } 3878b9d1f3cSGil Rapaport 3888b9d1f3cSGil Rapaport void VPInstruction::execute(VPTransformState &State) { 3898b9d1f3cSGil Rapaport assert(!State.Instance && "VPInstruction executing an Instance"); 3908b9d1f3cSGil Rapaport for (unsigned Part = 0; Part < State.UF; ++Part) 3918b9d1f3cSGil Rapaport generateInstruction(State, Part); 3928b9d1f3cSGil Rapaport } 3938b9d1f3cSGil Rapaport 39440e7bfc4SFlorian Hahn void VPInstruction::print(raw_ostream &O, const Twine &Indent, 39540e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 396*4c8285c7SFlorian Hahn O << "\"EMIT "; 39740e7bfc4SFlorian Hahn print(O, SlotTracker); 3988b9d1f3cSGil Rapaport } 3998b9d1f3cSGil Rapaport 4008b9d1f3cSGil Rapaport void VPInstruction::print(raw_ostream &O) const { 40140e7bfc4SFlorian Hahn VPSlotTracker SlotTracker(getParent()->getPlan()); 40240e7bfc4SFlorian Hahn print(O, SlotTracker); 40340e7bfc4SFlorian Hahn } 40440e7bfc4SFlorian Hahn 40540e7bfc4SFlorian Hahn void VPInstruction::print(raw_ostream &O, VPSlotTracker &SlotTracker) const { 406e6a74803SFlorian Hahn if (hasResult()) { 40740e7bfc4SFlorian Hahn printAsOperand(O, SlotTracker); 4088b9d1f3cSGil Rapaport O << " = "; 409e6a74803SFlorian Hahn } 4108b9d1f3cSGil Rapaport 4118b9d1f3cSGil Rapaport switch (getOpcode()) { 4128b9d1f3cSGil Rapaport case VPInstruction::Not: 4138b9d1f3cSGil Rapaport O << "not"; 4148b9d1f3cSGil Rapaport break; 415b0b5312eSAyal Zaks case VPInstruction::ICmpULE: 416b0b5312eSAyal Zaks O << "icmp ule"; 417b0b5312eSAyal Zaks break; 41809e516c5SFlorian Hahn case VPInstruction::SLPLoad: 41909e516c5SFlorian Hahn O << "combined load"; 42009e516c5SFlorian Hahn break; 42109e516c5SFlorian Hahn case VPInstruction::SLPStore: 42209e516c5SFlorian Hahn O << "combined store"; 42309e516c5SFlorian Hahn break; 4248b9d1f3cSGil Rapaport default: 4258b9d1f3cSGil Rapaport O << Instruction::getOpcodeName(getOpcode()); 4268b9d1f3cSGil Rapaport } 4278b9d1f3cSGil Rapaport 4288b9d1f3cSGil Rapaport for (const VPValue *Operand : operands()) { 4298b9d1f3cSGil Rapaport O << " "; 43040e7bfc4SFlorian Hahn Operand->printAsOperand(O, SlotTracker); 4318b9d1f3cSGil Rapaport } 4328b9d1f3cSGil Rapaport } 4338b9d1f3cSGil Rapaport 4341f58dda4SAyal Zaks /// Generate the code inside the body of the vectorized loop. Assumes a single 4351f58dda4SAyal Zaks /// LoopVectorBody basic-block was created for this. Introduce additional 4361f58dda4SAyal Zaks /// basic-blocks as needed, and fill them all. 4371f58dda4SAyal Zaks void VPlan::execute(VPTransformState *State) { 438b0b5312eSAyal Zaks // -1. Check if the backedge taken count is needed, and if so build it. 439b0b5312eSAyal Zaks if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) { 440b0b5312eSAyal Zaks Value *TC = State->TripCount; 441b0b5312eSAyal Zaks IRBuilder<> Builder(State->CFG.PrevBB->getTerminator()); 442b0b5312eSAyal Zaks auto *TCMO = Builder.CreateSub(TC, ConstantInt::get(TC->getType(), 1), 443b0b5312eSAyal Zaks "trip.count.minus.1"); 444a3c964a2SAyal Zaks Value *VTCMO = Builder.CreateVectorSplat(State->VF, TCMO, "broadcast"); 445a3c964a2SAyal Zaks for (unsigned Part = 0, UF = State->UF; Part < UF; ++Part) 446a3c964a2SAyal Zaks State->set(BackedgeTakenCount, VTCMO, Part); 447b0b5312eSAyal Zaks } 448b0b5312eSAyal Zaks 4498b9d1f3cSGil Rapaport // 0. Set the reverse mapping from VPValues to Values for code generation. 4508b9d1f3cSGil Rapaport for (auto &Entry : Value2VPValue) 4518b9d1f3cSGil Rapaport State->VPValue2Value[Entry.second] = Entry.first; 4528b9d1f3cSGil Rapaport 4531f58dda4SAyal Zaks BasicBlock *VectorPreHeaderBB = State->CFG.PrevBB; 4541f58dda4SAyal Zaks BasicBlock *VectorHeaderBB = VectorPreHeaderBB->getSingleSuccessor(); 4551f58dda4SAyal Zaks assert(VectorHeaderBB && "Loop preheader does not have a single successor."); 4561f58dda4SAyal Zaks 4571f58dda4SAyal Zaks // 1. Make room to generate basic-blocks inside loop body if needed. 458cced3eccSSimon Pilgrim BasicBlock *VectorLatchBB = VectorHeaderBB->splitBasicBlock( 4591f58dda4SAyal Zaks VectorHeaderBB->getFirstInsertionPt(), "vector.body.latch"); 4601f58dda4SAyal Zaks Loop *L = State->LI->getLoopFor(VectorHeaderBB); 4611f58dda4SAyal Zaks L->addBasicBlockToLoop(VectorLatchBB, *State->LI); 4621f58dda4SAyal Zaks // Remove the edge between Header and Latch to allow other connections. 4631f58dda4SAyal Zaks // Temporarily terminate with unreachable until CFG is rewired. 4641f58dda4SAyal Zaks // Note: this asserts the generated code's assumption that 4651f58dda4SAyal Zaks // getFirstInsertionPt() can be dereferenced into an Instruction. 4661f58dda4SAyal Zaks VectorHeaderBB->getTerminator()->eraseFromParent(); 4671f58dda4SAyal Zaks State->Builder.SetInsertPoint(VectorHeaderBB); 4681f58dda4SAyal Zaks UnreachableInst *Terminator = State->Builder.CreateUnreachable(); 4691f58dda4SAyal Zaks State->Builder.SetInsertPoint(Terminator); 4701f58dda4SAyal Zaks 4711f58dda4SAyal Zaks // 2. Generate code in loop body. 4721f58dda4SAyal Zaks State->CFG.PrevVPBB = nullptr; 4731f58dda4SAyal Zaks State->CFG.PrevBB = VectorHeaderBB; 4741f58dda4SAyal Zaks State->CFG.LastBB = VectorLatchBB; 4751f58dda4SAyal Zaks 4761f58dda4SAyal Zaks for (VPBlockBase *Block : depth_first(Entry)) 4771f58dda4SAyal Zaks Block->execute(State); 4781f58dda4SAyal Zaks 479ea7f3035SHideki Saito // Setup branch terminator successors for VPBBs in VPBBsToFix based on 480ea7f3035SHideki Saito // VPBB's successors. 481ea7f3035SHideki Saito for (auto VPBB : State->CFG.VPBBsToFix) { 482ea7f3035SHideki Saito assert(EnableVPlanNativePath && 483ea7f3035SHideki Saito "Unexpected VPBBsToFix in non VPlan-native path"); 484ea7f3035SHideki Saito BasicBlock *BB = State->CFG.VPBB2IRBB[VPBB]; 485ea7f3035SHideki Saito assert(BB && "Unexpected null basic block for VPBB"); 486ea7f3035SHideki Saito 487ea7f3035SHideki Saito unsigned Idx = 0; 488ea7f3035SHideki Saito auto *BBTerminator = BB->getTerminator(); 489ea7f3035SHideki Saito 490ea7f3035SHideki Saito for (VPBlockBase *SuccVPBlock : VPBB->getHierarchicalSuccessors()) { 491ea7f3035SHideki Saito VPBasicBlock *SuccVPBB = SuccVPBlock->getEntryBasicBlock(); 492ea7f3035SHideki Saito BBTerminator->setSuccessor(Idx, State->CFG.VPBB2IRBB[SuccVPBB]); 493ea7f3035SHideki Saito ++Idx; 494ea7f3035SHideki Saito } 495ea7f3035SHideki Saito } 496ea7f3035SHideki Saito 4971f58dda4SAyal Zaks // 3. Merge the temporary latch created with the last basic-block filled. 4981f58dda4SAyal Zaks BasicBlock *LastBB = State->CFG.PrevBB; 4991f58dda4SAyal Zaks // Connect LastBB to VectorLatchBB to facilitate their merge. 500ea7f3035SHideki Saito assert((EnableVPlanNativePath || 501ea7f3035SHideki Saito isa<UnreachableInst>(LastBB->getTerminator())) && 502ea7f3035SHideki Saito "Expected InnerLoop VPlan CFG to terminate with unreachable"); 503ea7f3035SHideki Saito assert((!EnableVPlanNativePath || isa<BranchInst>(LastBB->getTerminator())) && 504ea7f3035SHideki Saito "Expected VPlan CFG to terminate with branch in NativePath"); 5051f58dda4SAyal Zaks LastBB->getTerminator()->eraseFromParent(); 5061f58dda4SAyal Zaks BranchInst::Create(VectorLatchBB, LastBB); 5071f58dda4SAyal Zaks 5081f58dda4SAyal Zaks // Merge LastBB with Latch. 5091f58dda4SAyal Zaks bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI); 5101f58dda4SAyal Zaks (void)Merged; 5111f58dda4SAyal Zaks assert(Merged && "Could not merge last basic block with latch."); 5121f58dda4SAyal Zaks VectorLatchBB = LastBB; 5131f58dda4SAyal Zaks 514ea7f3035SHideki Saito // We do not attempt to preserve DT for outer loop vectorization currently. 515ea7f3035SHideki Saito if (!EnableVPlanNativePath) 516948e7452SEvgeniy Brevnov updateDominatorTree(State->DT, VectorPreHeaderBB, VectorLatchBB, 517948e7452SEvgeniy Brevnov L->getExitBlock()); 5181f58dda4SAyal Zaks } 5191f58dda4SAyal Zaks 520e9c68422SFlorian Hahn #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 521e9c68422SFlorian Hahn LLVM_DUMP_METHOD 522e9c68422SFlorian Hahn void VPlan::dump() const { dbgs() << *this << '\n'; } 523e9c68422SFlorian Hahn #endif 524e9c68422SFlorian Hahn 5251f58dda4SAyal Zaks void VPlan::updateDominatorTree(DominatorTree *DT, BasicBlock *LoopPreHeaderBB, 526948e7452SEvgeniy Brevnov BasicBlock *LoopLatchBB, 527948e7452SEvgeniy Brevnov BasicBlock *LoopExitBB) { 5281f58dda4SAyal Zaks BasicBlock *LoopHeaderBB = LoopPreHeaderBB->getSingleSuccessor(); 5291f58dda4SAyal Zaks assert(LoopHeaderBB && "Loop preheader does not have a single successor."); 5301f58dda4SAyal Zaks // The vector body may be more than a single basic-block by this point. 5311f58dda4SAyal Zaks // Update the dominator tree information inside the vector body by propagating 5321f58dda4SAyal Zaks // it from header to latch, expecting only triangular control-flow, if any. 5331f58dda4SAyal Zaks BasicBlock *PostDomSucc = nullptr; 5341f58dda4SAyal Zaks for (auto *BB = LoopHeaderBB; BB != LoopLatchBB; BB = PostDomSucc) { 5351f58dda4SAyal Zaks // Get the list of successors of this block. 5361f58dda4SAyal Zaks std::vector<BasicBlock *> Succs(succ_begin(BB), succ_end(BB)); 5371f58dda4SAyal Zaks assert(Succs.size() <= 2 && 5381f58dda4SAyal Zaks "Basic block in vector loop has more than 2 successors."); 5391f58dda4SAyal Zaks PostDomSucc = Succs[0]; 5401f58dda4SAyal Zaks if (Succs.size() == 1) { 5411f58dda4SAyal Zaks assert(PostDomSucc->getSinglePredecessor() && 5421f58dda4SAyal Zaks "PostDom successor has more than one predecessor."); 5431f58dda4SAyal Zaks DT->addNewBlock(PostDomSucc, BB); 5441f58dda4SAyal Zaks continue; 5451f58dda4SAyal Zaks } 5461f58dda4SAyal Zaks BasicBlock *InterimSucc = Succs[1]; 5471f58dda4SAyal Zaks if (PostDomSucc->getSingleSuccessor() == InterimSucc) { 5481f58dda4SAyal Zaks PostDomSucc = Succs[1]; 5491f58dda4SAyal Zaks InterimSucc = Succs[0]; 5501f58dda4SAyal Zaks } 5511f58dda4SAyal Zaks assert(InterimSucc->getSingleSuccessor() == PostDomSucc && 5521f58dda4SAyal Zaks "One successor of a basic block does not lead to the other."); 5531f58dda4SAyal Zaks assert(InterimSucc->getSinglePredecessor() && 5541f58dda4SAyal Zaks "Interim successor has more than one predecessor."); 5554de31bbaSVedant Kumar assert(PostDomSucc->hasNPredecessors(2) && 5561f58dda4SAyal Zaks "PostDom successor has more than two predecessors."); 5571f58dda4SAyal Zaks DT->addNewBlock(InterimSucc, BB); 5581f58dda4SAyal Zaks DT->addNewBlock(PostDomSucc, BB); 5591f58dda4SAyal Zaks } 560948e7452SEvgeniy Brevnov // Latch block is a new dominator for the loop exit. 561948e7452SEvgeniy Brevnov DT->changeImmediateDominator(LoopExitBB, LoopLatchBB); 562948e7452SEvgeniy Brevnov assert(DT->verify(DominatorTree::VerificationLevel::Fast)); 5631f58dda4SAyal Zaks } 5641f58dda4SAyal Zaks 5651f58dda4SAyal Zaks const Twine VPlanPrinter::getUID(const VPBlockBase *Block) { 5661f58dda4SAyal Zaks return (isa<VPRegionBlock>(Block) ? "cluster_N" : "N") + 5671f58dda4SAyal Zaks Twine(getOrCreateBID(Block)); 5681f58dda4SAyal Zaks } 5691f58dda4SAyal Zaks 5701f58dda4SAyal Zaks const Twine VPlanPrinter::getOrCreateName(const VPBlockBase *Block) { 5711f58dda4SAyal Zaks const std::string &Name = Block->getName(); 5721f58dda4SAyal Zaks if (!Name.empty()) 5731f58dda4SAyal Zaks return Name; 5741f58dda4SAyal Zaks return "VPB" + Twine(getOrCreateBID(Block)); 5751f58dda4SAyal Zaks } 5761f58dda4SAyal Zaks 5771f58dda4SAyal Zaks void VPlanPrinter::dump() { 5781f58dda4SAyal Zaks Depth = 1; 5791f58dda4SAyal Zaks bumpIndent(0); 5801f58dda4SAyal Zaks OS << "digraph VPlan {\n"; 5811f58dda4SAyal Zaks OS << "graph [labelloc=t, fontsize=30; label=\"Vectorization Plan"; 5821f58dda4SAyal Zaks if (!Plan.getName().empty()) 5831f58dda4SAyal Zaks OS << "\\n" << DOT::EscapeString(Plan.getName()); 58440e7bfc4SFlorian Hahn if (Plan.BackedgeTakenCount) { 585fd2c15e6SFlorian Hahn OS << ", where:\\n"; 58640e7bfc4SFlorian Hahn Plan.BackedgeTakenCount->print(OS, SlotTracker); 58740e7bfc4SFlorian Hahn OS << " := BackedgeTakenCount"; 58840e7bfc4SFlorian Hahn } 5891f58dda4SAyal Zaks OS << "\"]\n"; 5901f58dda4SAyal Zaks OS << "node [shape=rect, fontname=Courier, fontsize=30]\n"; 5911f58dda4SAyal Zaks OS << "edge [fontname=Courier, fontsize=30]\n"; 5921f58dda4SAyal Zaks OS << "compound=true\n"; 5931f58dda4SAyal Zaks 594e9c68422SFlorian Hahn for (const VPBlockBase *Block : depth_first(Plan.getEntry())) 5951f58dda4SAyal Zaks dumpBlock(Block); 5961f58dda4SAyal Zaks 5971f58dda4SAyal Zaks OS << "}\n"; 5981f58dda4SAyal Zaks } 5991f58dda4SAyal Zaks 6001f58dda4SAyal Zaks void VPlanPrinter::dumpBlock(const VPBlockBase *Block) { 6011f58dda4SAyal Zaks if (const VPBasicBlock *BasicBlock = dyn_cast<VPBasicBlock>(Block)) 6021f58dda4SAyal Zaks dumpBasicBlock(BasicBlock); 6031f58dda4SAyal Zaks else if (const VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 6041f58dda4SAyal Zaks dumpRegion(Region); 6051f58dda4SAyal Zaks else 6061f58dda4SAyal Zaks llvm_unreachable("Unsupported kind of VPBlock."); 6071f58dda4SAyal Zaks } 6081f58dda4SAyal Zaks 6091f58dda4SAyal Zaks void VPlanPrinter::drawEdge(const VPBlockBase *From, const VPBlockBase *To, 6101f58dda4SAyal Zaks bool Hidden, const Twine &Label) { 6111f58dda4SAyal Zaks // Due to "dot" we print an edge between two regions as an edge between the 6121f58dda4SAyal Zaks // exit basic block and the entry basic of the respective regions. 6131f58dda4SAyal Zaks const VPBlockBase *Tail = From->getExitBasicBlock(); 6141f58dda4SAyal Zaks const VPBlockBase *Head = To->getEntryBasicBlock(); 6151f58dda4SAyal Zaks OS << Indent << getUID(Tail) << " -> " << getUID(Head); 6161f58dda4SAyal Zaks OS << " [ label=\"" << Label << '\"'; 6171f58dda4SAyal Zaks if (Tail != From) 6181f58dda4SAyal Zaks OS << " ltail=" << getUID(From); 6191f58dda4SAyal Zaks if (Head != To) 6201f58dda4SAyal Zaks OS << " lhead=" << getUID(To); 6211f58dda4SAyal Zaks if (Hidden) 6221f58dda4SAyal Zaks OS << "; splines=none"; 6231f58dda4SAyal Zaks OS << "]\n"; 6241f58dda4SAyal Zaks } 6251f58dda4SAyal Zaks 6261f58dda4SAyal Zaks void VPlanPrinter::dumpEdges(const VPBlockBase *Block) { 6271f58dda4SAyal Zaks auto &Successors = Block->getSuccessors(); 6281f58dda4SAyal Zaks if (Successors.size() == 1) 6291f58dda4SAyal Zaks drawEdge(Block, Successors.front(), false, ""); 6301f58dda4SAyal Zaks else if (Successors.size() == 2) { 6311f58dda4SAyal Zaks drawEdge(Block, Successors.front(), false, "T"); 6321f58dda4SAyal Zaks drawEdge(Block, Successors.back(), false, "F"); 6331f58dda4SAyal Zaks } else { 6341f58dda4SAyal Zaks unsigned SuccessorNumber = 0; 6351f58dda4SAyal Zaks for (auto *Successor : Successors) 6361f58dda4SAyal Zaks drawEdge(Block, Successor, false, Twine(SuccessorNumber++)); 6371f58dda4SAyal Zaks } 6381f58dda4SAyal Zaks } 6391f58dda4SAyal Zaks 6401f58dda4SAyal Zaks void VPlanPrinter::dumpBasicBlock(const VPBasicBlock *BasicBlock) { 6411f58dda4SAyal Zaks OS << Indent << getUID(BasicBlock) << " [label =\n"; 6421f58dda4SAyal Zaks bumpIndent(1); 6431f58dda4SAyal Zaks OS << Indent << "\"" << DOT::EscapeString(BasicBlock->getName()) << ":\\n\""; 6441f58dda4SAyal Zaks bumpIndent(1); 6454e4ecae0SHideki Saito 6464e4ecae0SHideki Saito // Dump the block predicate. 6474e4ecae0SHideki Saito const VPValue *Pred = BasicBlock->getPredicate(); 6484e4ecae0SHideki Saito if (Pred) { 6494e4ecae0SHideki Saito OS << " +\n" << Indent << " \"BlockPredicate: "; 6504e4ecae0SHideki Saito if (const VPInstruction *PredI = dyn_cast<VPInstruction>(Pred)) { 65140e7bfc4SFlorian Hahn PredI->printAsOperand(OS, SlotTracker); 6524e4ecae0SHideki Saito OS << " (" << DOT::EscapeString(PredI->getParent()->getName()) 6534e4ecae0SHideki Saito << ")\\l\""; 6544e4ecae0SHideki Saito } else 65540e7bfc4SFlorian Hahn Pred->printAsOperand(OS, SlotTracker); 6564e4ecae0SHideki Saito } 6574e4ecae0SHideki Saito 658*4c8285c7SFlorian Hahn for (const VPRecipeBase &Recipe : *BasicBlock) { 659*4c8285c7SFlorian Hahn OS << " +\n" << Indent; 66040e7bfc4SFlorian Hahn Recipe.print(OS, Indent, SlotTracker); 661*4c8285c7SFlorian Hahn OS << "\\l\""; 662*4c8285c7SFlorian Hahn } 663d0953014SDiego Caballero 664d0953014SDiego Caballero // Dump the condition bit. 665d0953014SDiego Caballero const VPValue *CBV = BasicBlock->getCondBit(); 666d0953014SDiego Caballero if (CBV) { 667d0953014SDiego Caballero OS << " +\n" << Indent << " \"CondBit: "; 668d0953014SDiego Caballero if (const VPInstruction *CBI = dyn_cast<VPInstruction>(CBV)) { 66940e7bfc4SFlorian Hahn CBI->printAsOperand(OS, SlotTracker); 670d0953014SDiego Caballero OS << " (" << DOT::EscapeString(CBI->getParent()->getName()) << ")\\l\""; 671d8e7ca4aSRenato Golin } else { 67240e7bfc4SFlorian Hahn CBV->printAsOperand(OS, SlotTracker); 673d8e7ca4aSRenato Golin OS << "\""; 674d8e7ca4aSRenato Golin } 675d0953014SDiego Caballero } 676d0953014SDiego Caballero 6771f58dda4SAyal Zaks bumpIndent(-2); 6781f58dda4SAyal Zaks OS << "\n" << Indent << "]\n"; 6791f58dda4SAyal Zaks dumpEdges(BasicBlock); 6801f58dda4SAyal Zaks } 6811f58dda4SAyal Zaks 6821f58dda4SAyal Zaks void VPlanPrinter::dumpRegion(const VPRegionBlock *Region) { 6831f58dda4SAyal Zaks OS << Indent << "subgraph " << getUID(Region) << " {\n"; 6841f58dda4SAyal Zaks bumpIndent(1); 6851f58dda4SAyal Zaks OS << Indent << "fontname=Courier\n" 6861f58dda4SAyal Zaks << Indent << "label=\"" 6871f58dda4SAyal Zaks << DOT::EscapeString(Region->isReplicator() ? "<xVFxUF> " : "<x1> ") 6881f58dda4SAyal Zaks << DOT::EscapeString(Region->getName()) << "\"\n"; 6891f58dda4SAyal Zaks // Dump the blocks of the region. 6901f58dda4SAyal Zaks assert(Region->getEntry() && "Region contains no inner blocks."); 6911f58dda4SAyal Zaks for (const VPBlockBase *Block : depth_first(Region->getEntry())) 6921f58dda4SAyal Zaks dumpBlock(Block); 6931f58dda4SAyal Zaks bumpIndent(-1); 6941f58dda4SAyal Zaks OS << Indent << "}\n"; 6951f58dda4SAyal Zaks dumpEdges(Region); 6961f58dda4SAyal Zaks } 6971f58dda4SAyal Zaks 6981f58dda4SAyal Zaks void VPlanPrinter::printAsIngredient(raw_ostream &O, Value *V) { 6991f58dda4SAyal Zaks std::string IngredientString; 7001f58dda4SAyal Zaks raw_string_ostream RSO(IngredientString); 7011f58dda4SAyal Zaks if (auto *Inst = dyn_cast<Instruction>(V)) { 7021f58dda4SAyal Zaks if (!Inst->getType()->isVoidTy()) { 7031f58dda4SAyal Zaks Inst->printAsOperand(RSO, false); 7041f58dda4SAyal Zaks RSO << " = "; 7051f58dda4SAyal Zaks } 7061f58dda4SAyal Zaks RSO << Inst->getOpcodeName() << " "; 7071f58dda4SAyal Zaks unsigned E = Inst->getNumOperands(); 7081f58dda4SAyal Zaks if (E > 0) { 7091f58dda4SAyal Zaks Inst->getOperand(0)->printAsOperand(RSO, false); 7101f58dda4SAyal Zaks for (unsigned I = 1; I < E; ++I) 7111f58dda4SAyal Zaks Inst->getOperand(I)->printAsOperand(RSO << ", ", false); 7121f58dda4SAyal Zaks } 7131f58dda4SAyal Zaks } else // !Inst 7141f58dda4SAyal Zaks V->printAsOperand(RSO, false); 7151f58dda4SAyal Zaks RSO.flush(); 7161f58dda4SAyal Zaks O << DOT::EscapeString(IngredientString); 7171f58dda4SAyal Zaks } 7187333aa9fSHal Finkel 71990be3c24SFlorian Hahn void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent, 72090be3c24SFlorian Hahn VPSlotTracker &SlotTracker) const { 721*4c8285c7SFlorian Hahn O << "\"WIDEN-CALL " << VPlanIngredient(&Ingredient); 72290be3c24SFlorian Hahn } 72390be3c24SFlorian Hahn 72418138e02SFlorian Hahn void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent, 72518138e02SFlorian Hahn VPSlotTracker &SlotTracker) const { 726*4c8285c7SFlorian Hahn O << "\"WIDEN-SELECT" << VPlanIngredient(&Ingredient) 727*4c8285c7SFlorian Hahn << (InvariantCond ? " (condition is loop invariant)" : ""); 72818138e02SFlorian Hahn } 72918138e02SFlorian Hahn 73040e7bfc4SFlorian Hahn void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent, 73140e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 732*4c8285c7SFlorian Hahn O << "\"WIDEN\\l\""; 733*4c8285c7SFlorian Hahn O << "\" " << VPlanIngredient(&Ingredient); 7347333aa9fSHal Finkel } 7357333aa9fSHal Finkel 73640e7bfc4SFlorian Hahn void VPWidenIntOrFpInductionRecipe::print(raw_ostream &O, const Twine &Indent, 73740e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 738*4c8285c7SFlorian Hahn O << "\"WIDEN-INDUCTION"; 7397333aa9fSHal Finkel if (Trunc) { 7407333aa9fSHal Finkel O << "\\l\""; 7417333aa9fSHal Finkel O << " +\n" << Indent << "\" " << VPlanIngredient(IV) << "\\l\""; 742*4c8285c7SFlorian Hahn O << " +\n" << Indent << "\" " << VPlanIngredient(Trunc); 7437333aa9fSHal Finkel } else 744*4c8285c7SFlorian Hahn O << " " << VPlanIngredient(IV); 7457333aa9fSHal Finkel } 7467333aa9fSHal Finkel 74740e7bfc4SFlorian Hahn void VPWidenGEPRecipe::print(raw_ostream &O, const Twine &Indent, 74840e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 749*4c8285c7SFlorian Hahn O << "\"WIDEN-GEP "; 75039ccc099SGil Rapaport O << (IsPtrLoopInvariant ? "Inv" : "Var"); 75139ccc099SGil Rapaport size_t IndicesNumber = IsIndexLoopInvariant.size(); 75239ccc099SGil Rapaport for (size_t I = 0; I < IndicesNumber; ++I) 75339ccc099SGil Rapaport O << "[" << (IsIndexLoopInvariant[I] ? "Inv" : "Var") << "]"; 75439ccc099SGil Rapaport O << "\\l\""; 755*4c8285c7SFlorian Hahn O << " +\n" << Indent << "\" " << VPlanIngredient(GEP); 75639ccc099SGil Rapaport } 75739ccc099SGil Rapaport 75840e7bfc4SFlorian Hahn void VPWidenPHIRecipe::print(raw_ostream &O, const Twine &Indent, 75940e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 760*4c8285c7SFlorian Hahn O << "\"WIDEN-PHI " << VPlanIngredient(Phi); 7617333aa9fSHal Finkel } 7627333aa9fSHal Finkel 76340e7bfc4SFlorian Hahn void VPBlendRecipe::print(raw_ostream &O, const Twine &Indent, 76440e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 765*4c8285c7SFlorian Hahn O << "\"BLEND "; 7667333aa9fSHal Finkel Phi->printAsOperand(O, false); 7677333aa9fSHal Finkel O << " ="; 768e2a18678SGil Rapaport if (getNumIncomingValues() == 1) { 7697333aa9fSHal Finkel // Not a User of any mask: not really blending, this is a 7707333aa9fSHal Finkel // single-predecessor phi. 7717333aa9fSHal Finkel O << " "; 772e2a18678SGil Rapaport getIncomingValue(0)->printAsOperand(O, SlotTracker); 7737333aa9fSHal Finkel } else { 774e2a18678SGil Rapaport for (unsigned I = 0, E = getNumIncomingValues(); I < E; ++I) { 7757333aa9fSHal Finkel O << " "; 776e2a18678SGil Rapaport getIncomingValue(I)->printAsOperand(O, SlotTracker); 7777333aa9fSHal Finkel O << "/"; 778e2a18678SGil Rapaport getMask(I)->printAsOperand(O, SlotTracker); 7797333aa9fSHal Finkel } 7807333aa9fSHal Finkel } 7817333aa9fSHal Finkel } 7827333aa9fSHal Finkel 78340e7bfc4SFlorian Hahn void VPReplicateRecipe::print(raw_ostream &O, const Twine &Indent, 78440e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 785*4c8285c7SFlorian Hahn O << "\"" << (IsUniform ? "CLONE " : "REPLICATE ") 7867333aa9fSHal Finkel << VPlanIngredient(Ingredient); 7877333aa9fSHal Finkel if (AlsoPack) 7887333aa9fSHal Finkel O << " (S->V)"; 7897333aa9fSHal Finkel } 7907333aa9fSHal Finkel 79140e7bfc4SFlorian Hahn void VPPredInstPHIRecipe::print(raw_ostream &O, const Twine &Indent, 79240e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 793*4c8285c7SFlorian Hahn O << "\"PHI-PREDICATED-INSTRUCTION " << VPlanIngredient(PredInst); 7947333aa9fSHal Finkel } 7957333aa9fSHal Finkel 79640e7bfc4SFlorian Hahn void VPWidenMemoryInstructionRecipe::print(raw_ostream &O, const Twine &Indent, 79740e7bfc4SFlorian Hahn VPSlotTracker &SlotTracker) const { 798*4c8285c7SFlorian Hahn O << "\"WIDEN " << VPlanIngredient(&Instr); 7998647a72cSGil Rapaport O << ", "; 80040e7bfc4SFlorian Hahn getAddr()->printAsOperand(O, SlotTracker); 801d62bf161SGil Rapaport VPValue *Mask = getMask(); 802d62bf161SGil Rapaport if (Mask) { 8037333aa9fSHal Finkel O << ", "; 80440e7bfc4SFlorian Hahn Mask->printAsOperand(O, SlotTracker); 8057333aa9fSHal Finkel } 8067333aa9fSHal Finkel } 8072a34ac86SDiego Caballero 80816784892SAyal Zaks void VPWidenCanonicalIVRecipe::execute(VPTransformState &State) { 80916784892SAyal Zaks Value *CanonicalIV = State.CanonicalIV; 81016784892SAyal Zaks Type *STy = CanonicalIV->getType(); 81116784892SAyal Zaks IRBuilder<> Builder(State.CFG.PrevBB->getTerminator()); 81216784892SAyal Zaks Value *VStart = Builder.CreateVectorSplat(State.VF, CanonicalIV, "broadcast"); 81316784892SAyal Zaks for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) { 81416784892SAyal Zaks SmallVector<Constant *, 8> Indices; 81516784892SAyal Zaks for (unsigned Lane = 0, VF = State.VF; Lane < VF; ++Lane) 81616784892SAyal Zaks Indices.push_back(ConstantInt::get(STy, Part * VF + Lane)); 81716784892SAyal Zaks Constant *VStep = ConstantVector::get(Indices); 81816784892SAyal Zaks // Add the consecutive indices to the vector value. 81916784892SAyal Zaks Value *CanonicalVectorIV = Builder.CreateAdd(VStart, VStep, "vec.iv"); 82016784892SAyal Zaks State.set(getVPValue(), CanonicalVectorIV, Part); 82116784892SAyal Zaks } 82216784892SAyal Zaks } 82316784892SAyal Zaks 82416784892SAyal Zaks void VPWidenCanonicalIVRecipe::print(raw_ostream &O, const Twine &Indent, 82516784892SAyal Zaks VPSlotTracker &SlotTracker) const { 826*4c8285c7SFlorian Hahn O << "\"EMIT "; 82716784892SAyal Zaks getVPValue()->printAsOperand(O, SlotTracker); 828*4c8285c7SFlorian Hahn O << " = WIDEN-CANONICAL-INDUCTION"; 82916784892SAyal Zaks } 83016784892SAyal Zaks 8312a34ac86SDiego Caballero template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT); 832a4dc7feeSFlorian Hahn 83309e516c5SFlorian Hahn void VPValue::replaceAllUsesWith(VPValue *New) { 83409e516c5SFlorian Hahn for (VPUser *User : users()) 83509e516c5SFlorian Hahn for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) 83609e516c5SFlorian Hahn if (User->getOperand(I) == this) 83709e516c5SFlorian Hahn User->setOperand(I, New); 83809e516c5SFlorian Hahn } 83909e516c5SFlorian Hahn 84040e7bfc4SFlorian Hahn void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const { 841e6a74803SFlorian Hahn if (const Value *UV = getUnderlyingValue()) { 842e6a74803SFlorian Hahn OS << "ir<"; 843e6a74803SFlorian Hahn UV->printAsOperand(OS, false); 844e6a74803SFlorian Hahn OS << ">"; 845e6a74803SFlorian Hahn return; 846e6a74803SFlorian Hahn } 847e6a74803SFlorian Hahn 84840e7bfc4SFlorian Hahn unsigned Slot = Tracker.getSlot(this); 84940e7bfc4SFlorian Hahn if (Slot == unsigned(-1)) 85040e7bfc4SFlorian Hahn OS << "<badref>"; 85140e7bfc4SFlorian Hahn else 852e6a74803SFlorian Hahn OS << "vp<%" << Tracker.getSlot(this) << ">"; 85340e7bfc4SFlorian Hahn } 85440e7bfc4SFlorian Hahn 855a4dc7feeSFlorian Hahn void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region, 856a4dc7feeSFlorian Hahn Old2NewTy &Old2New, 857a4dc7feeSFlorian Hahn InterleavedAccessInfo &IAI) { 858a4dc7feeSFlorian Hahn ReversePostOrderTraversal<VPBlockBase *> RPOT(Region->getEntry()); 859a4dc7feeSFlorian Hahn for (VPBlockBase *Base : RPOT) { 860a4dc7feeSFlorian Hahn visitBlock(Base, Old2New, IAI); 861a4dc7feeSFlorian Hahn } 862a4dc7feeSFlorian Hahn } 863a4dc7feeSFlorian Hahn 864a4dc7feeSFlorian Hahn void VPInterleavedAccessInfo::visitBlock(VPBlockBase *Block, Old2NewTy &Old2New, 865a4dc7feeSFlorian Hahn InterleavedAccessInfo &IAI) { 866a4dc7feeSFlorian Hahn if (VPBasicBlock *VPBB = dyn_cast<VPBasicBlock>(Block)) { 867a4dc7feeSFlorian Hahn for (VPRecipeBase &VPI : *VPBB) { 868a4dc7feeSFlorian Hahn assert(isa<VPInstruction>(&VPI) && "Can only handle VPInstructions"); 869a4dc7feeSFlorian Hahn auto *VPInst = cast<VPInstruction>(&VPI); 870a4dc7feeSFlorian Hahn auto *Inst = cast<Instruction>(VPInst->getUnderlyingValue()); 871a4dc7feeSFlorian Hahn auto *IG = IAI.getInterleaveGroup(Inst); 872a4dc7feeSFlorian Hahn if (!IG) 873a4dc7feeSFlorian Hahn continue; 874a4dc7feeSFlorian Hahn 875a4dc7feeSFlorian Hahn auto NewIGIter = Old2New.find(IG); 876a4dc7feeSFlorian Hahn if (NewIGIter == Old2New.end()) 877a4dc7feeSFlorian Hahn Old2New[IG] = new InterleaveGroup<VPInstruction>( 87832851f8dSGuillaume Chatelet IG->getFactor(), IG->isReverse(), IG->getAlign()); 879a4dc7feeSFlorian Hahn 880a4dc7feeSFlorian Hahn if (Inst == IG->getInsertPos()) 881a4dc7feeSFlorian Hahn Old2New[IG]->setInsertPos(VPInst); 882a4dc7feeSFlorian Hahn 883a4dc7feeSFlorian Hahn InterleaveGroupMap[VPInst] = Old2New[IG]; 884a4dc7feeSFlorian Hahn InterleaveGroupMap[VPInst]->insertMember( 885a4dc7feeSFlorian Hahn VPInst, IG->getIndex(Inst), 886837a1b84SGuillaume Chatelet Align(IG->isReverse() ? (-1) * int(IG->getFactor()) 887837a1b84SGuillaume Chatelet : IG->getFactor())); 888a4dc7feeSFlorian Hahn } 889a4dc7feeSFlorian Hahn } else if (VPRegionBlock *Region = dyn_cast<VPRegionBlock>(Block)) 890a4dc7feeSFlorian Hahn visitRegion(Region, Old2New, IAI); 891a4dc7feeSFlorian Hahn else 892a4dc7feeSFlorian Hahn llvm_unreachable("Unsupported kind of VPBlock."); 893a4dc7feeSFlorian Hahn } 894a4dc7feeSFlorian Hahn 895a4dc7feeSFlorian Hahn VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan, 896a4dc7feeSFlorian Hahn InterleavedAccessInfo &IAI) { 897a4dc7feeSFlorian Hahn Old2NewTy Old2New; 898a4dc7feeSFlorian Hahn visitRegion(cast<VPRegionBlock>(Plan.getEntry()), Old2New, IAI); 899a4dc7feeSFlorian Hahn } 90040e7bfc4SFlorian Hahn 90140e7bfc4SFlorian Hahn void VPSlotTracker::assignSlot(const VPValue *V) { 90240e7bfc4SFlorian Hahn assert(Slots.find(V) == Slots.end() && "VPValue already has a slot!"); 903e6a74803SFlorian Hahn const Value *UV = V->getUnderlyingValue(); 904e6a74803SFlorian Hahn if (UV) 905e6a74803SFlorian Hahn return; 906e6a74803SFlorian Hahn const auto *VPI = dyn_cast<VPInstruction>(V); 907e6a74803SFlorian Hahn if (VPI && !VPI->hasResult()) 908e6a74803SFlorian Hahn return; 909e6a74803SFlorian Hahn 91040e7bfc4SFlorian Hahn Slots[V] = NextSlot++; 91140e7bfc4SFlorian Hahn } 91240e7bfc4SFlorian Hahn 91340e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPBlockBase *VPBB) { 91440e7bfc4SFlorian Hahn if (auto *Region = dyn_cast<VPRegionBlock>(VPBB)) 91540e7bfc4SFlorian Hahn assignSlots(Region); 91640e7bfc4SFlorian Hahn else 91740e7bfc4SFlorian Hahn assignSlots(cast<VPBasicBlock>(VPBB)); 91840e7bfc4SFlorian Hahn } 91940e7bfc4SFlorian Hahn 92040e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPRegionBlock *Region) { 92140e7bfc4SFlorian Hahn ReversePostOrderTraversal<const VPBlockBase *> RPOT(Region->getEntry()); 92240e7bfc4SFlorian Hahn for (const VPBlockBase *Block : RPOT) 92340e7bfc4SFlorian Hahn assignSlots(Block); 92440e7bfc4SFlorian Hahn } 92540e7bfc4SFlorian Hahn 92640e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) { 92740e7bfc4SFlorian Hahn for (const VPRecipeBase &Recipe : *VPBB) { 92840e7bfc4SFlorian Hahn if (const auto *VPI = dyn_cast<VPInstruction>(&Recipe)) 92940e7bfc4SFlorian Hahn assignSlot(VPI); 93016784892SAyal Zaks else if (const auto *VPIV = dyn_cast<VPWidenCanonicalIVRecipe>(&Recipe)) 93116784892SAyal Zaks assignSlot(VPIV->getVPValue()); 93240e7bfc4SFlorian Hahn } 93340e7bfc4SFlorian Hahn } 93440e7bfc4SFlorian Hahn 93540e7bfc4SFlorian Hahn void VPSlotTracker::assignSlots(const VPlan &Plan) { 93640e7bfc4SFlorian Hahn 93740e7bfc4SFlorian Hahn for (const VPValue *V : Plan.VPExternalDefs) 93840e7bfc4SFlorian Hahn assignSlot(V); 93940e7bfc4SFlorian Hahn 94040e7bfc4SFlorian Hahn for (auto &E : Plan.Value2VPValue) 94140e7bfc4SFlorian Hahn if (!isa<VPInstruction>(E.second)) 94240e7bfc4SFlorian Hahn assignSlot(E.second); 94340e7bfc4SFlorian Hahn 94440e7bfc4SFlorian Hahn for (const VPValue *V : Plan.VPCBVs) 94540e7bfc4SFlorian Hahn assignSlot(V); 94640e7bfc4SFlorian Hahn 94740e7bfc4SFlorian Hahn if (Plan.BackedgeTakenCount) 94840e7bfc4SFlorian Hahn assignSlot(Plan.BackedgeTakenCount); 94940e7bfc4SFlorian Hahn 95040e7bfc4SFlorian Hahn ReversePostOrderTraversal<const VPBlockBase *> RPOT(Plan.getEntry()); 95140e7bfc4SFlorian Hahn for (const VPBlockBase *Block : RPOT) 95240e7bfc4SFlorian Hahn assignSlots(Block); 95340e7bfc4SFlorian Hahn } 954