diff options
Diffstat (limited to 'llvm/lib/Transforms/Vectorize/VPlan.cpp')
-rw-r--r-- | llvm/lib/Transforms/Vectorize/VPlan.cpp | 116 |
1 files changed, 73 insertions, 43 deletions
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp index d554f438c804..e81b88fd8099 100644 --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/PostOrderIterator.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/IR/BasicBlock.h" @@ -46,7 +47,10 @@ #include <vector> using namespace llvm; + +namespace llvm { extern cl::opt<bool> EnableVPlanNativePath; +} #define DEBUG_TYPE "vplan" @@ -160,8 +164,9 @@ VPBasicBlock *VPBlockBase::getEntryBasicBlock() { } void VPBlockBase::setPlan(VPlan *ParentPlan) { - assert(ParentPlan->getEntry() == this && - "Can only set plan on its entry block."); + assert( + (ParentPlan->getEntry() == this || ParentPlan->getPreheader() == this) && + "Can only set plan on its entry or preheader block."); Plan = ParentPlan; } @@ -209,7 +214,7 @@ VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi() { } Value *VPTransformState::get(VPValue *Def, const VPIteration &Instance) { - if (!Def->hasDefiningRecipe()) + if (Def->isLiveIn()) return Def->getLiveInIRValue(); if (hasScalarValue(Def, Instance)) { @@ -243,11 +248,19 @@ void VPTransformState::addNewMetadata(Instruction *To, } void VPTransformState::addMetadata(Instruction *To, Instruction *From) { + // No source instruction to transfer metadata from? + if (!From) + return; + propagateMetadata(To, From); addNewMetadata(To, From); } void VPTransformState::addMetadata(ArrayRef<Value *> To, Instruction *From) { + // No source instruction to transfer metadata from? + if (!From) + return; + for (Value *V : To) { if (Instruction *I = dyn_cast<Instruction>(V)) addMetadata(I, From); @@ -265,7 +278,7 @@ void VPTransformState::setDebugLocFromInst(const Value *V) { // When a FSDiscriminator is enabled, we don't need to add the multiply // factors to the discriminators. if (DIL && Inst->getFunction()->shouldEmitDebugInfoForProfiling() && - !isa<DbgInfoIntrinsic>(Inst) && !EnableFSDiscriminator) { + !Inst->isDebugOrPseudoInst() && !EnableFSDiscriminator) { // FIXME: For scalable vectors, assume vscale=1. auto NewDIL = DIL->cloneByMultiplyingDuplicationFactor(UF * VF.getKnownMinValue()); @@ -577,7 +590,9 @@ void VPRegionBlock::print(raw_ostream &O, const Twine &Indent, #endif VPlan::~VPlan() { - clearLiveOuts(); + for (auto &KV : LiveOuts) + delete KV.second; + LiveOuts.clear(); if (Entry) { VPValue DummyValue; @@ -585,15 +600,23 @@ VPlan::~VPlan() { Block->dropAllReferences(&DummyValue); VPBlockBase::deleteCFG(Entry); + + Preheader->dropAllReferences(&DummyValue); + delete Preheader; } - for (VPValue *VPV : VPValuesToFree) + for (VPValue *VPV : VPLiveInsToFree) delete VPV; - if (TripCount) - delete TripCount; if (BackedgeTakenCount) delete BackedgeTakenCount; - for (auto &P : VPExternalDefs) - delete P.second; +} + +VPlanPtr VPlan::createInitialVPlan(const SCEV *TripCount, ScalarEvolution &SE) { + VPBasicBlock *Preheader = new VPBasicBlock("ph"); + VPBasicBlock *VecPreheader = new VPBasicBlock("vector.ph"); + auto Plan = std::make_unique<VPlan>(Preheader, VecPreheader); + Plan->TripCount = + vputils::getOrCreateVPValueForSCEVExpr(*Plan, TripCount, SE); + return Plan; } VPActiveLaneMaskPHIRecipe *VPlan::getActiveLaneMaskPhi() { @@ -609,13 +632,6 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV, Value *CanonicalIVStartValue, VPTransformState &State, bool IsEpilogueVectorization) { - - // Check if the trip count is needed, and if so build it. - if (TripCount && TripCount->getNumUsers()) { - for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part) - State.set(TripCount, TripCountV, Part); - } - // Check if the backedge taken count is needed, and if so build it. if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) { IRBuilder<> Builder(State.CFG.PrevBB->getTerminator()); @@ -636,7 +652,7 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV, // needs to be changed from zero to the value after the main vector loop. // FIXME: Improve modeling for canonical IV start values in the epilogue loop. if (CanonicalIVStartValue) { - VPValue *VPV = getOrAddExternalDef(CanonicalIVStartValue); + VPValue *VPV = getVPValueOrAddLiveIn(CanonicalIVStartValue); auto *IV = getCanonicalIV(); assert(all_of(IV->users(), [](const VPUser *U) { @@ -650,8 +666,7 @@ void VPlan::prepareToExecute(Value *TripCountV, Value *VectorTripCountV, VPInstruction::CanonicalIVIncrementNUW; }) && "the canonical IV should only be used by its increments or " - "ScalarIVSteps when " - "resetting the start value"); + "ScalarIVSteps when resetting the start value"); IV->setOperand(0, VPV); } } @@ -748,13 +763,25 @@ void VPlan::print(raw_ostream &O) const { if (VectorTripCount.getNumUsers() > 0) { O << "\nLive-in "; VectorTripCount.printAsOperand(O, SlotTracker); - O << " = vector-trip-count\n"; + O << " = vector-trip-count"; } if (BackedgeTakenCount && BackedgeTakenCount->getNumUsers()) { O << "\nLive-in "; BackedgeTakenCount->printAsOperand(O, SlotTracker); - O << " = backedge-taken count\n"; + O << " = backedge-taken count"; + } + + O << "\n"; + if (TripCount->isLiveIn()) + O << "Live-in "; + TripCount->printAsOperand(O, SlotTracker); + O << " = original trip-count"; + O << "\n"; + + if (!getPreheader()->empty()) { + O << "\n"; + getPreheader()->print(O, "", SlotTracker); } for (const VPBlockBase *Block : vp_depth_first_shallow(getEntry())) { @@ -765,11 +792,7 @@ void VPlan::print(raw_ostream &O) const { if (!LiveOuts.empty()) O << "\n"; for (const auto &KV : LiveOuts) { - O << "Live-out "; - KV.second->getPhi()->printAsOperand(O); - O << " = "; - KV.second->getOperand(0)->printAsOperand(O, SlotTracker); - O << "\n"; + KV.second->print(O, SlotTracker); } O << "}\n"; @@ -882,6 +905,8 @@ void VPlanPrinter::dump() { OS << "edge [fontname=Courier, fontsize=30]\n"; OS << "compound=true\n"; + dumpBlock(Plan.getPreheader()); + for (const VPBlockBase *Block : vp_depth_first_shallow(Plan.getEntry())) dumpBlock(Block); @@ -1086,26 +1111,27 @@ VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan, } void VPSlotTracker::assignSlot(const VPValue *V) { - assert(Slots.find(V) == Slots.end() && "VPValue already has a slot!"); + assert(!Slots.contains(V) && "VPValue already has a slot!"); Slots[V] = NextSlot++; } void VPSlotTracker::assignSlots(const VPlan &Plan) { - - for (const auto &P : Plan.VPExternalDefs) - assignSlot(P.second); - assignSlot(&Plan.VectorTripCount); if (Plan.BackedgeTakenCount) assignSlot(Plan.BackedgeTakenCount); + assignSlots(Plan.getPreheader()); ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<const VPBlockBase *>> RPOT(VPBlockDeepTraversalWrapper<const VPBlockBase *>(Plan.getEntry())); for (const VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<const VPBasicBlock>(RPOT)) - for (const VPRecipeBase &Recipe : *VPBB) - for (VPValue *Def : Recipe.definedValues()) - assignSlot(Def); + assignSlots(VPBB); +} + +void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) { + for (const VPRecipeBase &Recipe : *VPBB) + for (VPValue *Def : Recipe.definedValues()) + assignSlot(Def); } bool vputils::onlyFirstLaneUsed(VPValue *Def) { @@ -1115,13 +1141,17 @@ bool vputils::onlyFirstLaneUsed(VPValue *Def) { VPValue *vputils::getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr, ScalarEvolution &SE) { + if (auto *Expanded = Plan.getSCEVExpansion(Expr)) + return Expanded; + VPValue *Expanded = nullptr; if (auto *E = dyn_cast<SCEVConstant>(Expr)) - return Plan.getOrAddExternalDef(E->getValue()); - if (auto *E = dyn_cast<SCEVUnknown>(Expr)) - return Plan.getOrAddExternalDef(E->getValue()); - - VPBasicBlock *Preheader = Plan.getEntry()->getEntryBasicBlock(); - VPExpandSCEVRecipe *Step = new VPExpandSCEVRecipe(Expr, SE); - Preheader->appendRecipe(Step); - return Step; + Expanded = Plan.getVPValueOrAddLiveIn(E->getValue()); + else if (auto *E = dyn_cast<SCEVUnknown>(Expr)) + Expanded = Plan.getVPValueOrAddLiveIn(E->getValue()); + else { + Expanded = new VPExpandSCEVRecipe(Expr, SE); + Plan.getPreheader()->appendRecipe(Expanded->getDefiningRecipe()); + } + Plan.addSCEVExpansion(Expr, Expanded); + return Expanded; } |