56#define DEBUG_TYPE "pipeliner"
60 "Number of loops that we attempt to use window scheduling");
62 "Number of times that we run list schedule in the window scheduling");
64 "Number of loops that we successfully use window scheduling");
66 "Window scheduling abort due to the failure of the II analysis");
75 AAMDNodes &AATags,
bool &IsAllIdentified) {
76 if (!
MI->hasOneMemOperand())
96 const Value *SrcVal =
nullptr, *DstVal =
nullptr;
97 int64_t SrcOffset = 0, DstOffset = 0;
100 bool SrcAllIdentified =
false, DstAllIdentified =
false;
101 bool SrcKnown =
getMemInfo(SrcMI, SrcVal, SrcOffset, SrcObjs, SrcAATags,
103 bool DstKnown =
getMemInfo(DstMI, DstVal, DstOffset, DstObjs, DstAATags,
108 if (SrcKnown && DstKnown && SrcAllIdentified && DstAllIdentified &&
124 if (!SrcKnown || !DstKnown)
126 if (SrcVal == DstVal && SrcOffset <= DstOffset)
136 for (
const Value *SrcObj : SrcObjs)
137 for (
const Value *DstObj : DstObjs)
153 const DenseSet<std::pair<MachineInstr *, MachineInstr *>> &LoopCarriedMemDeps;
154 const DenseMap<MachineInstr *, TriCloneInfo> &TriToOri;
158 WindowMemDepMutation(
159 const DenseSet<std::pair<MachineInstr *, MachineInstr *>> &Deps,
160 const DenseMap<MachineInstr *, TriCloneInfo> &TriToOri,
bool &EdgeFailed)
161 : LoopCarriedMemDeps(Deps), TriToOri(TriToOri), EdgeFailed(EdgeFailed) {}
163 void apply(ScheduleDAGInstrs *DAG)
override {
164 if (LoopCarriedMemDeps.empty())
166 for (SUnit &PredSU : DAG->
SUnits) {
167 MachineInstr *PredMI = PredSU.
getInstr();
170 auto PredIt = TriToOri.find(PredMI);
171 if (PredIt == TriToOri.end())
173 for (SUnit &SuccSU : DAG->
SUnits) {
174 MachineInstr *SuccMI = SuccSU.
getInstr();
177 auto SuccIt = TriToOri.find(SuccMI);
178 if (SuccIt == TriToOri.end())
182 if (PredIt->second.Copy >= SuccIt->second.Copy)
184 if (!LoopCarriedMemDeps.contains(
185 {PredIt->second.Ori, SuccIt->second.Ori}))
205 TripleDAG = std::unique_ptr<ScheduleDAGInstrs>(
211 LLVM_DEBUG(
dbgs() <<
"The WindowScheduler failed to initialize!\n");
217 ++NumTryWindowSchedule;
232 assert(SchedDAG &&
"Expected a scheduler for window scheduling");
233 auto *SchedDAGMI =
static_cast<ScheduleDAGMI *
>(SchedDAG.get());
234 SchedDAGMI->
addMutation(std::make_unique<WindowMemDepMutation>(
238 for (
unsigned Idx : SearchIndexes) {
240 ++NumTryWindowSearch;
246 SchedDAG->startBlock(
MBB);
248 SchedDAG->schedule();
252 LLVM_DEBUG(
dbgs() <<
"Can't enforce a loop-carried memory dependence. "
253 "Keep searching...\n");
257 if (
II ==
Opts.window_ii_limit) {
259 LLVM_DEBUG(
dbgs() <<
"Can't find a valid II. Keep searching...\n");
277 <<
" and Best II is " <<
BestII <<
".\n");
286 return OnlyBuildGraph
294 if (!
Subtarget->enableWindowScheduler()) {
321 if (PrevUses.
count(Phi.getOperand(0).getReg()))
323 PrevDefs.
insert(Phi.getOperand(0).getReg());
324 for (
unsigned I = 1, E = Phi.getNumOperands();
I != E;
I += 2) {
325 if (PrevDefs.
count(Phi.getOperand(
I).getReg()))
327 PrevUses.
insert(Phi.getOperand(
I).getReg());
331 auto PLI =
TII->analyzeLoopForPipelining(
MBB);
332 for (
auto &
MI : *
MBB) {
333 if (
MI.isDebugOrPseudoInstr() ||
MI.isTerminator())
336 if (IsLoopCarried(
MI)) {
337 LLVM_DEBUG(
dbgs() <<
"Loop carried phis are not supported yet!\n");
344 if (
TII->isSchedulingBoundary(
MI,
MBB, *
MF)) {
346 dbgs() <<
"Boundary MI is not allowed in window scheduling!\n");
349 if (PLI->shouldIgnoreForPipelining(&
MI)) {
350 LLVM_DEBUG(
dbgs() <<
"Special MI defined by target is not allowed in "
351 "window scheduling!\n");
354 for (
auto &Def :
MI.all_defs())
355 if (Def.isReg() && Def.getReg().isPhysical()) {
357 "window scheduling!\n");
362 LLVM_DEBUG(
dbgs() <<
"There are too few MIs in the window region!\n");
378 MBB,
MBB->begin(),
MBB->getFirstTerminator(),
379 std::distance(
MBB->begin(),
MBB->getFirstTerminator()));
390 if (
MI.mayLoadOrStore() && !
MI.isDebugInstr())
407 if (!SrcMI->mayStore() && !DstMI->mayStore())
424 for (
auto &
MI :
MBB->instrs())
428 Context->LIS->getSlotIndexes()->removeMachineInstrFromMaps(
MI,
true);
436 Context->LIS->getSlotIndexes()->removeMachineInstrFromMaps(
MI,
true);
437 MI.eraseFromParent();
445 const unsigned DuplicateNum = 3;
448 assert(
OriMIs.size() > 0 &&
"The Original MIs were not backed up!");
454 if (
MI->isDebugOrPseudoInstr() ||
MI->isTerminator())
458 DefPairs[
MI->getOperand(0).getReg()] = AntiReg;
459 auto *NewMI =
MF->CloneMachineInstr(
MI);
460 MBB->push_back(NewMI);
467 for (
size_t Cnt = 1; Cnt < DuplicateNum; ++Cnt) {
469 if (
MI->isPHI() ||
MI->isDebugOrPseudoInstr() ||
470 (
MI->isTerminator() && Cnt < DuplicateNum - 1))
472 auto *NewMI =
MF->CloneMachineInstr(
MI);
475 for (
auto MO : NewMI->all_defs())
476 if (MO.isReg() && MO.getReg().isVirtual()) {
478 MRI->createVirtualRegister(
MRI->getRegClass(MO.getReg()));
479 NewMI->substituteRegister(MO.getReg(), NewDef, 0, *
TRI);
480 NewDefs[MO.getReg()] = NewDef;
483 for (
auto DefRegPair : DefPairs)
484 if (NewMI->readsRegister(DefRegPair.first,
TRI)) {
485 Register NewUse = DefRegPair.second;
513 if (
auto It = DefPairs.find(NewUse); It != DefPairs.end())
515 NewMI->substituteRegister(DefRegPair.first, NewUse, 0, *
TRI);
518 for (
auto &NewDef : NewDefs)
519 DefPairs[NewDef.first] = NewDef.second;
520 MBB->push_back(NewMI);
522 TriToOri[NewMI] = {
MI,
static_cast<unsigned>(Cnt)};
531 for (
auto &Phi :
MBB->phis()) {
532 for (
auto DefRegPair : DefPairs)
533 if (Phi.readsRegister(DefRegPair.first,
TRI))
534 Phi.substituteRegister(DefRegPair.first, DefRegPair.second, 0, *
TRI);
541 for (
size_t I = 0;
I <
TriMIs.size(); ++
I) {
543 auto OldPos =
MBB->begin();
544 std::advance(OldPos,
I);
545 auto CurPos =
MI->getIterator();
546 if (CurPos != OldPos) {
547 MBB->splice(OldPos,
MBB, CurPos);
554 unsigned SearchRatio) {
559 assert(SearchRatio <= 100 &&
"SearchRatio should be equal or less than 100!");
561 unsigned Step = SearchNum > 0 && SearchNum <= MaxIdx ? MaxIdx / SearchNum : 1;
563 for (
unsigned Idx = 0; Idx < MaxIdx; Idx += Step)
565 return SearchIndexes;
570 unsigned MaxDepth = 1;
571 for (
auto &SU : DAG.
SUnits)
572 MaxDepth = std::max(SU.getDepth() + SU.Latency, MaxDepth);
573 return MaxDepth *
Opts.window_ii_coeff;
588 int ExpectCycle = CurCycle;
590 for (
auto &Pred : SU->Preds) {
593 auto *PredMI = Pred.getSUnit()->getInstr();
595 ExpectCycle = std::max(ExpectCycle, PredCycle + (
int)Pred.getLatency());
598 if (!
TII->isZeroCost(
MI.getOpcode())) {
601 while (!RM.canReserveResources(*SU, CurCycle) || CurCycle < ExpectCycle) {
603 if (CurCycle == (
int)
Opts.window_ii_limit)
606 RM.reserveResources(*SU, CurCycle);
647 int MaxStallCycle = 0;
648 int CurrentII = MaxCycle + 1;
653 for (
auto &Succ : SU->Succs) {
654 if (Succ.isWeak() || Succ.getSUnit() == &
TripleDAG->ExitSU)
657 if (DefCycle + (
int)Succ.getLatency() <= CurrentII)
662 auto *SuccMI = Succ.getSUnit()->getInstr();
664 if (DefCycle < UseCycle)
665 return Opts.window_ii_limit;
667 int StallCycle = DefCycle + (int)Succ.getLatency() - CurrentII - UseCycle;
668 MaxStallCycle = std::max(MaxStallCycle, StallCycle);
671 LLVM_DEBUG(
dbgs() <<
"MaxStallCycle is " << MaxStallCycle <<
".\n");
672 return MaxStallCycle;
678 if (MaxCycle == (
int)
Opts.window_ii_limit)
681 if (StallCycle == (
int)
Opts.window_ii_limit)
684 return MaxCycle + StallCycle + 1;
689 for (
auto &Phi :
MBB->phis()) {
690 int LateCycle = INT_MAX;
692 for (
auto &Succ : SU->Succs) {
698 auto *SuccMI = Succ.getSUnit()->getInstr();
701 LateCycle = std::min(LateCycle, Cycle);
705 auto *AntiMI =
MRI->getVRegDef(AntiReg);
707 if (AntiMI->getParent() ==
MBB) {
710 LateCycle = std::min(LateCycle, AntiCycle);
714 if (LateCycle == INT_MAX)
715 LateCycle = (int)(
II - 1);
716 LLVM_DEBUG(
dbgs() <<
"\tCycle range [0, " << LateCycle <<
"] " << Phi);
729 for (
auto &Phi :
MBB->phis())
737 for (
int Cycle = 0; Cycle < (int)
II; ++Cycle) {
738 auto It = CycleToMIs.
find(Cycle);
739 if (It == CycleToMIs.
end())
741 for (
auto *
MI : It->second)
742 IssueOrder[
MI] = Id++;
767 assert(IssueOrder.count(Pair.first) &&
"Cannot find original MI!");
768 SchedResult.push_back(std::make_tuple(Pair.first, Pair.second,
770 IssueOrder[Pair.first]));
777 [](
const std::tuple<MachineInstr *, int, int, int> &
A,
778 const std::tuple<MachineInstr *, int, int, int> &
B) {
779 return std::get<3>(
A) < std::get<3>(
B);
784 std::vector<MachineInstr *> OrderedInsts;
786 auto *
MI = std::get<0>(Info);
787 OrderedInsts.push_back(
MI);
788 Cycles[
MI] = std::get<1>(Info);
789 Stages[
MI] = std::get<2>(Info);
805 if (!MO.isReg() || MO.getReg() == 0)
811 Context->LIS->repairIntervalsInRange(
MBB,
MBB->begin(),
MBB->end(), UsedRegs);
816 auto RegionBegin =
MBB->begin();
817 std::advance(RegionBegin,
Offset);
818 auto RegionEnd = RegionBegin;
819 std::advance(RegionEnd, Num);
844 if (
MI->isDebugOrPseudoInstr())
850 return Id >= (size_t)
Offset ? 1 : 0;
854 assert(Phi->isPHI() &&
"Expecting PHI!");
856 for (
auto MO : Phi->uses()) {
858 AntiReg = MO.getReg();
859 else if (MO.isMBB() && MO.getMBB() ==
MBB)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool hasLoopCarriedMemDep(const SUnitWithMemInfo &Src, const SUnitWithMemInfo &Dst, BatchAAResults &BAA, const TargetInstrInfo *TII)
Returns true if there is a loop-carried order dependency from Src to Dst.
This file provides utility analysis objects describing memory locations.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
MachineRegisterInfo & MRI
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Target-Independent Code Generator Pass Configuration Options pass.
static bool getMemInfo(const MachineInstr *MI, const Value *&MemOpValue, int64_t &MemOpOffset, SmallVectorImpl< const Value * > &UnderlyingObjs, AAMDNodes &AATags, bool &IsAllIdentified)
Collect the underlying objects and alias metadata for the single memory operand of MI.
static bool hasLoopCarriedMemDep(const MachineInstr *SrcMI, const MachineInstr *DstMI, BatchAAResults &BAA, const MachineBasicBlock *LoopBB, const MachineRegisterInfo &MRI, const TargetInstrInfo *TII)
Return true if there is a loop-carried order dependency from memory access SrcMI to DstMI in loop Loo...
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void enableCrossIterationMode()
Assume that values may come from different cycle iterations.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
iterator find(const_arg_type_t< KeyT > Val)
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Representation of each machine instruction.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
A description of a memory reference used in the backend.
AAMDNodes getAAInfo() const
Return the AA tags for the memory reference.
const Value * getValue() const
Return the base address of the memory access.
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
The ModuloScheduleExpander takes a ModuloSchedule and expands it in-place, rewriting the old loop and...
LLVM_ABI void cleanup()
Performs final cleanup after expansion.
LLVM_ABI void expand()
Performs the actual expansion.
DenseMap< MachineInstr *, std::pair< Register, int64_t > > InstrChangesTy
Represents a schedule for a single-block loop.
Wrapper class representing virtual and physical registers.
@ Data
Regular data dependence (aka true-dependence).
@ Barrier
An unknown scheduling barrier.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
A ScheduleDAG for scheduling lists of MachineInstr.
bool addEdge(SUnit *SuccSU, const SDep &PredDep)
Add a DAG edge to the given SU with the given predecessor dependence data.
SUnit * getSUnit(MachineInstr *MI) const
Returns an existing SUnit for this MI, or nullptr.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
void addMutation(std::unique_ptr< ScheduleDAGMutation > Mutation)
Add a postprocessing step to the DAG builder.
Mutate the DAG as a postpass after normal DAG building.
std::vector< SUnit > SUnits
The scheduling units.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
LLVM Value Representation.
MachineInstr * getOriMI(MachineInstr *NewMI)
Get the original MI from which the new MI is cloned.
virtual ScheduleDAGInstrs * createMachineScheduler(bool OnlyBuildGraph=false)
Two types of ScheduleDAGs are needed, one for creating dependency graphs only, and the other for list...
unsigned SchedPhiNum
SchedPhiNum records the number of phi in the original MBB, and the scheduling starts with MI after ph...
virtual void preProcess()
Add some related processing before running window scheduling.
void collectLoopCarriedMemDeps()
Detect loop-carried memory dependences in the original MBB that must be preserved across loop iterati...
MachineSchedContext * Context
virtual void restoreTripleMBB()
Restore the order of MIs in TripleMBB after each list scheduling.
WindowScheduler(MachineSchedContext *C, MachineLoop &ML, MachineDominatorTree *MDT=nullptr)
virtual void postProcess()
Add some related processing after running window scheduling.
DenseMap< MachineInstr *, int > OriToCycle
OriToCycle keeps the mappings between the original MI and its issue cycle.
virtual int calculateMaxCycle(ScheduleDAGInstrs &DAG, unsigned Offset)
Calculate MIs execution cycle after list scheduling.
MachineRegisterInfo * MRI
MachineDominatorTree * MDT
unsigned BestII
BestII and BestOffset record the characteristics of the best scheduling result and are used together ...
void backupMBB()
Back up the MIs in the original MBB and remove them from MBB.
int getEstimatedII(ScheduleDAGInstrs &DAG)
Estimate a II value at which all MIs will be scheduled successfully.
virtual void expand()
Using the scheduling infrastructure to expand the results of window scheduling.
DenseMap< MachineInstr *, int > getIssueOrder(unsigned Offset, unsigned II)
Get the final issue order of all scheduled MIs including phis.
Register getAntiRegister(MachineInstr *Phi)
Gets the register in phi which is generated from the current MBB.
unsigned getOriStage(MachineInstr *OriMI, unsigned Offset)
Get the scheduling stage, where the stage of the new MI is identical to the original MI.
const TargetRegisterInfo * TRI
virtual void updateLiveIntervals()
Update the live intervals for all registers used within MBB.
const TargetSubtargetInfo * Subtarget
bool MemDepEdgeFailed
MemDepEdgeFailed is set when enforcing a loop-carried memory dependence as an artificial DAG edge wou...
std::unique_ptr< ScheduleDAGInstrs > TripleDAG
To innovatively identify the dependencies between MIs across two trips, we construct a DAG for a new ...
unsigned BaseII
BaseII is the II obtained when the window offset is SchedPhiNum.
DenseSet< std::pair< MachineInstr *, MachineInstr * > > LoopCarriedMemDeps
LoopCarriedMemDeps keeps ordered pairs (Pred, Succ) of original memory MIs such that the iteration-N ...
virtual void schedulePhi(int Offset, unsigned &II)
Phis are scheduled separately after each list scheduling.
DenseMap< MachineInstr *, TriCloneInfo > TriToOri
TriToOri keeps the mappings between the MI clones in TripleMBB and the information about their origin...
virtual unsigned analyseII(ScheduleDAGInstrs &DAG, unsigned Offset)
Analyzes the II value after each list scheduling.
int getOriCycle(MachineInstr *NewMI)
Get the issue cycle of the new MI based on the cycle of the original MI.
unsigned SchedInstrNum
SchedInstrNum records the MIs involved in scheduling in the original MBB, excluding debug instruction...
const TargetInstrInfo * TII
virtual void generateTripleMBB()
Make three copies of the original MBB to generate a new TripleMBB.
iterator_range< MachineBasicBlock::iterator > getScheduleRange(unsigned Offset, unsigned Num)
Gets the iterator range of MIs in the scheduling window.
virtual bool isScheduleValid()
Check whether the final result of window scheduling is valid.
virtual int calculateStallCycle(unsigned Offset, int MaxCycle)
Calculate the stall cycle between two trips after list scheduling.
virtual void updateScheduleResult(unsigned Offset, unsigned II)
Update the scheduling result after each list scheduling.
SmallVector< MachineInstr * > OriMIs
OriMIs keeps the MIs removed from the original MBB.
virtual bool initialize()
Initializes the algorithm and determines if it can be executed.
SmallVector< std::tuple< MachineInstr *, int, int, int >, 256 > SchedResult
SchedResult keeps the result of each list scheduling, and the format of the tuple is <MI pointer,...
virtual SmallVector< unsigned > getSearchIndexes(unsigned SearchNum, unsigned SearchRatio)
Give the folding position in the window algorithm, where different heuristics can be used.
void restoreMBB()
Erase the MIs in current MBB and restore the original MIs.
SmallVector< MachineInstr * > TriMIs
TriMIs keeps the MIs of TripleMBB, which is used to restore TripleMBB.
const CodeGenOptions & Opts
A range adaptor for a pair of iterators.
void apply(Opt *O, const Mod &M, const Mods &... Ms)
This is an optimization pass for GlobalISel generic memory operations.
ScheduleDAGMILive * createSchedLive(MachineSchedContext *C)
Create the standard converging machine scheduler.
void stable_sort(R &&Range)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI bool mayOverlapInLaterIter(const MachineInstr *BaseMI, const MachineInstr *OtherMI, const MachineBasicBlock *LoopBB, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
Return true if BaseMI and OtherMI may access overlapping memory in a later iteration of the loop Loop...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...