52#define DEBUG_TYPE "pipeliner"
56 "Number of loops that we attempt to use window scheduling");
58 "Number of times that we run list schedule in the window scheduling");
60 "Number of loops that we successfully use window scheduling");
62 "Window scheduling abort due to the failure of the II analysis");
65 WindowSearchNum(
"window-search-num",
66 cl::desc(
"The number of searches per loop in the window "
67 "algorithm. 0 means no search number limit."),
71 "window-search-ratio",
72 cl::desc(
"The ratio of searches per loop in the window algorithm. 100 "
73 "means search all positions in the loop, while 0 means not "
74 "performing any search."),
80 "The coefficient used when initializing II in the window algorithm."),
84 "window-region-limit",
86 "The lower limit of the scheduling region in the window algorithm."),
91 cl::desc(
"The lower limit of the difference between best II and base II in "
92 "the window algorithm. If the difference is smaller than "
93 "this lower limit, window scheduling will not be performed."),
101 cl::desc(
"The upper limit of II in the window algorithm."),
106 Subtarget(&MF->getSubtarget()),
TII(Subtarget->getInstrInfo()),
107 TRI(Subtarget->getRegisterInfo()),
MRI(&MF->getRegInfo()) {
108 TripleDAG = std::unique_ptr<ScheduleDAGInstrs>(
114 LLVM_DEBUG(
dbgs() <<
"The WindowScheduler failed to initialize!\n");
120 ++NumTryWindowSchedule;
126 for (
unsigned Idx : SearchIndexes) {
128 ++NumTryWindowSearch;
133 SchedDAG->startBlock(
MBB);
135 SchedDAG->schedule();
140 LLVM_DEBUG(
dbgs() <<
"Can't find a valid II. Keep searching...\n");
158 <<
" and Best II is " <<
BestII <<
".\n");
167 return OnlyBuildGraph
202 if (PrevUses.
count(Phi.getOperand(0).getReg()))
204 PrevDefs.
insert(Phi.getOperand(0).getReg());
205 for (
unsigned I = 1, E = Phi.getNumOperands();
I != E;
I += 2) {
206 if (PrevDefs.
count(Phi.getOperand(
I).getReg()))
208 PrevUses.
insert(Phi.getOperand(
I).getReg());
213 for (
auto &
MI : *
MBB) {
214 if (
MI.isMetaInstruction() ||
MI.isTerminator())
217 if (IsLoopCarried(
MI)) {
218 LLVM_DEBUG(
dbgs() <<
"Loop carried phis are not supported yet!\n");
227 dbgs() <<
"Boundary MI is not allowed in window scheduling!\n");
230 if (PLI->shouldIgnoreForPipelining(&
MI)) {
231 LLVM_DEBUG(
dbgs() <<
"Special MI defined by target is not allowed in "
232 "window scheduling!\n");
235 for (
auto &Def :
MI.all_defs())
236 if (Def.isReg() && Def.getReg().isPhysical()) {
238 "window scheduling!\n");
243 LLVM_DEBUG(
dbgs() <<
"There are too few MIs in the window region!\n");
283 MI.eraseFromParent();
292 const unsigned DuplicateNum = 3;
295 assert(
OriMIs.size() > 0 &&
"The Original MIs were not backed up!");
301 if (
MI->isMetaInstruction() ||
MI->isTerminator())
305 DefPairs[
MI->getOperand(0).getReg()] = AntiReg;
314 for (
size_t Cnt = 1; Cnt < DuplicateNum; ++Cnt) {
316 if (
MI->isPHI() ||
MI->isMetaInstruction() ||
317 (
MI->isTerminator() && Cnt < DuplicateNum - 1))
322 for (
auto MO : NewMI->all_defs())
323 if (MO.isReg() && MO.getReg().isVirtual()) {
326 NewMI->substituteRegister(MO.getReg(), NewDef, 0, *
TRI);
327 NewDefs[MO.getReg()] = NewDef;
330 for (
auto DefRegPair : DefPairs)
331 if (NewMI->readsRegister(DefRegPair.first,
TRI)) {
332 Register NewUse = DefRegPair.second;
360 if (
auto It = DefPairs.find(NewUse); It != DefPairs.end())
362 NewMI->substituteRegister(DefRegPair.first, NewUse, 0, *
TRI);
365 for (
auto &NewDef : NewDefs)
366 DefPairs[NewDef.first] = NewDef.second;
378 for (
auto &Phi :
MBB->
phis()) {
379 for (
auto DefRegPair : DefPairs)
380 if (Phi.readsRegister(DefRegPair.first,
TRI))
381 Phi.substituteRegister(DefRegPair.first, DefRegPair.second, 0, *
TRI);
388 for (
size_t I = 0;
I <
TriMIs.size(); ++
I) {
391 std::advance(OldPos,
I);
392 auto CurPos =
MI->getIterator();
393 if (CurPos != OldPos) {
401 unsigned SearchRatio) {
406 assert(SearchRatio <= 100 &&
"SearchRatio should be equal or less than 100!");
408 unsigned Step = SearchNum > 0 && SearchNum <= MaxIdx ? MaxIdx / SearchNum : 1;
410 for (
unsigned Idx = 0;
Idx < MaxIdx;
Idx += Step)
412 return SearchIndexes;
417 unsigned MaxDepth = 1;
418 for (
auto &SU : DAG.
SUnits)
419 MaxDepth = std::max(SU.getDepth() + SU.Latency, MaxDepth);
420 return MaxDepth * WindowIICoeff;
435 int ExpectCycle = CurCycle;
437 for (
auto &Pred : SU->Preds) {
440 auto *PredMI = Pred.getSUnit()->
getInstr();
442 ExpectCycle = std::max(ExpectCycle, PredCycle + (
int)Pred.getLatency());
448 while (!RM.canReserveResources(*SU, CurCycle) || CurCycle < ExpectCycle) {
453 RM.reserveResources(*SU, CurCycle);
494 int MaxStallCycle = 0;
495 int CurrentII = MaxCycle + 1;
500 for (
auto &Succ : SU->Succs) {
501 if (Succ.isWeak() || Succ.getSUnit() == &
TripleDAG->ExitSU)
504 if (DefCycle + (
int)Succ.getLatency() <= CurrentII)
509 auto *SuccMI = Succ.getSUnit()->getInstr();
511 if (DefCycle < UseCycle)
514 int StallCycle = DefCycle + (int)Succ.getLatency() - CurrentII - UseCycle;
515 MaxStallCycle = std::max(MaxStallCycle, StallCycle);
518 LLVM_DEBUG(
dbgs() <<
"MaxStallCycle is " << MaxStallCycle <<
".\n");
519 return MaxStallCycle;
531 return MaxCycle + StallCycle + 1;
536 for (
auto &Phi :
MBB->
phis()) {
537 int LateCycle = INT_MAX;
539 for (
auto &Succ : SU->Succs) {
545 auto *SuccMI = Succ.getSUnit()->getInstr();
548 LateCycle = std::min(LateCycle,
Cycle);
554 if (AntiMI->getParent() ==
MBB) {
557 LateCycle = std::min(LateCycle, AntiCycle);
561 if (LateCycle == INT_MAX)
562 LateCycle = (int)(
II - 1);
563 LLVM_DEBUG(
dbgs() <<
"\tCycle range [0, " << LateCycle <<
"] " << Phi);
586 if (It == CycleToMIs.
end())
588 for (
auto *
MI : It->second)
589 IssueOrder[
MI] = Id++;
614 assert(IssueOrder.count(Pair.first) &&
"Cannot find original MI!");
615 SchedResult.push_back(std::make_tuple(Pair.first, Pair.second,
617 IssueOrder[Pair.first]));
624 [](
const std::tuple<MachineInstr *, int, int, int> &
A,
625 const std::tuple<MachineInstr *, int, int, int> &
B) {
626 return std::get<3>(
A) < std::get<3>(
B);
631 std::vector<MachineInstr *> OrderedInsts;
633 auto *
MI = std::get<0>(
Info);
634 OrderedInsts.push_back(
MI);
635 Cycles[
MI] = std::get<1>(
Info);
636 Stages[
MI] = std::get<2>(
Info);
652 if (!MO.isReg() || MO.getReg() == 0)
664 std::advance(RegionBegin,
Offset);
665 auto RegionEnd = RegionBegin;
666 std::advance(RegionEnd, Num);
684 "Cannot find OriMI in OriMIs!");
692 if (
MI->isMetaInstruction())
698 return Id >= (size_t)
Offset ? 1 : 0;
702 assert(Phi->isPHI() &&
"Expecting PHI!");
704 for (
auto MO : Phi->uses()) {
706 AntiReg = MO.getReg();
707 else if (MO.isMBB() && MO.getMBB() ==
MBB)
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
const HexagonInstrInfo * TII
unsigned const TargetRegisterInfo * TRI
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
cl::opt< unsigned > WindowIILimit("window-ii-limit", cl::desc("The upper limit of II in the window algorithm."), cl::Hidden, cl::init(1000))
iterator find(const_arg_type_t< KeyT > Val)
A possibly irreducible generalization of a Loop.
void repairIntervalsInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, ArrayRef< Register > OrigRegs)
Update live intervals for instructions in a range of iterators.
void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block.
SlotIndexes * getSlotIndexes() const
Represents a single loop in the control flow graph.
iterator_range< iterator > phis()
Returns a range that iterates over the phis in the basic block.
void push_back(MachineInstr *MI)
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
The ModuloScheduleExpander takes a ModuloSchedule and expands it in-place, rewriting the old loop and...
void cleanup()
Performs final cleanup after expansion.
void expand()
Performs the actual expansion.
Represents a schedule for a single-block loop.
Wrapper class representing virtual and physical registers.
@ Data
Regular data dependence (aka true-dependence).
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
A ScheduleDAG for scheduling lists of MachineInstr.
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...
std::vector< SUnit > SUnits
The scheduling units.
void removeMachineInstrFromMaps(MachineInstr &MI, bool AllowBundled=false)
Removes machine instruction (bundle) MI from the mapping.
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.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual std::unique_ptr< PipelinerLoopInfo > analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const
Analyze loop L, which must be a single-basic-block loop, and if the conditions can be understood enou...
bool isZeroCost(unsigned Opcode) const
Return true for pseudo instructions that don't consume any machine resources in their current form.
virtual bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const
Test if the given instruction should be considered a scheduling boundary.
virtual ScheduleDAGInstrs * createMachineScheduler(MachineSchedContext *C) const
Create an instance of ScheduleDAGInstrs to be run within the standard MachineScheduler pass for this ...
virtual bool enableWindowScheduler() const
True if the subtarget should run WindowScheduler.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
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.
MachineSchedContext * Context
virtual void restoreTripleMBB()
Restore the order of MIs in TripleMBB after each list scheduling.
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
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.
DenseMap< MachineInstr *, MachineInstr * > TriToOri
TriToOri keeps the mappings between the MI clones in TripleMBB and their original MI.
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
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.
virtual void schedulePhi(int Offset, unsigned &II)
Phis are scheduled separately after each list scheduling.
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.
WindowScheduler(MachineSchedContext *C, MachineLoop &ML)
SmallVector< MachineInstr * > TriMIs
TriMIs keeps the MIs of TripleMBB, which is used to restore TripleMBB.
A range adaptor for a pair of iterators.
@ C
The default llvm calling convention, compatible with C.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find 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.
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...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...