35#include "llvm/Config/llvm-config.h"
44#define DEBUG_TYPE "post-RA-sched"
48STATISTIC(NumFixedAnti,
"Number of fixed anti-dependencies");
55 cl::desc(
"Enable scheduling after register allocation"),
59 cl::desc(
"Break post-RA scheduling anti-dependencies: "
60 "\"critical\", \"all\", or \"none\""),
66 cl::desc(
"Debug control MBBs that are scheduled"),
70 cl::desc(
"Debug control MBBs that are scheduled"),
97 MachineFunctionProperties::Property::NoVRegs);
103 bool enablePostRAScheduler(
108 char PostRAScheduler::ID = 0;
119 std::vector<SUnit*> PendingQueue;
134 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
140 unsigned EndIndex = 0;
143 SchedulePostRATDList(
149 ~SchedulePostRATDList()
override;
157 void setEndIndex(
unsigned EndIdx) { EndIndex = EndIdx; }
163 unsigned regioninstrs)
override;
185 void postProcessDAG();
187 void ReleaseSucc(
SUnit *SU,
SDep *SuccEdge);
188 void ReleaseSuccessors(
SUnit *SU);
189 void ScheduleNodeTopDown(
SUnit *SU,
unsigned CurCycle);
190 void ListScheduleTopDown();
192 void dumpSchedule()
const;
193 void emitNoop(
unsigned CurCycle);
200 "Post RA top-down list latency scheduler",
false,
false)
202SchedulePostRATDList::SchedulePostRATDList(
210 MF.getSubtarget().getInstrItineraryData();
212 MF.getSubtarget().getInstrInfo()->CreateTargetPostRAHazardRecognizer(
214 MF.getSubtarget().getPostRAMutations(Mutations);
216 assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
217 MRI.tracksLiveness()) &&
218 "Live-ins must be accurate for anti-dependency breaking");
219 AntiDepBreak = ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL)
221 : ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL)
226SchedulePostRATDList::~SchedulePostRATDList() {
235 unsigned regioninstrs) {
241void SchedulePostRATDList::exitRegion() {
243 dbgs() <<
"*** Final schedule ***\n";
250#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
253 for (
const SUnit *SU : Sequence) {
257 dbgs() <<
"**** NOOP ****\n";
262bool PostRAScheduler::enablePostRAScheduler(
267 Mode =
ST.getAntiDepBreakMode();
268 ST.getCriticalPathRCs(CriticalPathRCs);
274 return ST.enablePostRAScheduler() &&
275 OptLevel >=
ST.getOptLevelToEnablePostRAScheduler();
284 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
290 TargetSubtargetInfo::ANTIDEP_NONE;
296 AntiDepMode, CriticalPathRCs))
302 ? TargetSubtargetInfo::ANTIDEP_ALL
304 ? TargetSubtargetInfo::ANTIDEP_CRITICAL
305 : TargetSubtargetInfo::ANTIDEP_NONE);
310 SchedulePostRATDList
Scheduler(Fn, MLI, AA, RegClassInfo, AntiDepMode,
314 for (
auto &
MBB : Fn) {
318 static int bbcnt = 0;
321 dbgs() <<
"*** DEBUG scheduling " << Fn.getName() <<
":"
332 unsigned Count =
MBB.
size(), CurrentCount = Count;
340 Scheduler.enterRegion(&
MBB,
I, Current, CurrentCount - Count);
346 CurrentCount = Count;
351 Count -=
MI.getBundleSize();
353 assert(Count == 0 &&
"Instruction count mismatch!");
355 "Instruction count mismatch!");
382 AntiDepBreak->StartBlock(BB);
387void SchedulePostRATDList::schedule() {
393 AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
394 EndIndex, DbgValues);
406 NumFixedAnti += Broken;
415 AvailableQueue.initNodes(SUnits);
416 ListScheduleTopDown();
417 AvailableQueue.releaseState();
423void SchedulePostRATDList::Observe(
MachineInstr &
MI,
unsigned Count) {
425 AntiDepBreak->Observe(
MI, Count, EndIndex);
430void SchedulePostRATDList::finishBlock() {
432 AntiDepBreak->FinishBlock();
439void SchedulePostRATDList::postProcessDAG() {
440 for (
auto &M : Mutations)
450void SchedulePostRATDList::ReleaseSucc(
SUnit *SU,
SDep *SuccEdge) {
459 dbgs() <<
"*** Scheduling failed! ***\n";
461 dbgs() <<
" has been released too many times!\n";
481 PendingQueue.push_back(SuccSU);
485void SchedulePostRATDList::ReleaseSuccessors(
SUnit *SU) {
488 ReleaseSucc(SU, &*
I);
495void SchedulePostRATDList::ScheduleNodeTopDown(
SUnit *SU,
unsigned CurCycle) {
501 "Node scheduled above its depth!");
504 ReleaseSuccessors(SU);
506 AvailableQueue.scheduledNode(SU);
510void SchedulePostRATDList::emitNoop(
unsigned CurCycle) {
511 LLVM_DEBUG(
dbgs() <<
"*** Emitting noop in cycle " << CurCycle <<
'\n');
512 HazardRec->EmitNoop();
519void SchedulePostRATDList::ListScheduleTopDown() {
520 unsigned CurCycle = 0;
529 ReleaseSuccessors(&EntrySU);
535 AvailableQueue.push(&
SUnit);
542 bool CycleHasInsts =
false;
546 std::vector<SUnit*> NotReady;
548 while (!AvailableQueue.empty() || !PendingQueue.empty()) {
551 unsigned MinDepth = ~0
u;
552 for (
unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
553 if (PendingQueue[i]->getDepth() <= CurCycle) {
554 AvailableQueue.push(PendingQueue[i]);
555 PendingQueue[i]->isAvailable =
true;
556 PendingQueue[i] = PendingQueue.back();
557 PendingQueue.pop_back();
559 }
else if (PendingQueue[i]->getDepth() < MinDepth)
560 MinDepth = PendingQueue[i]->getDepth();
564 AvailableQueue.dump(
this));
566 SUnit *FoundSUnit =
nullptr, *NotPreferredSUnit =
nullptr;
567 bool HasNoopHazards =
false;
568 while (!AvailableQueue.empty()) {
569 SUnit *CurSUnit = AvailableQueue.pop();
572 HazardRec->getHazardType(CurSUnit, 0);
574 if (HazardRec->ShouldPreferAnother(CurSUnit)) {
575 if (!NotPreferredSUnit) {
580 NotPreferredSUnit = CurSUnit;
584 FoundSUnit = CurSUnit;
592 NotReady.push_back(CurSUnit);
598 if (NotPreferredSUnit) {
601 dbgs() <<
"*** Will schedule a non-preferred instruction...\n");
602 FoundSUnit = NotPreferredSUnit;
604 AvailableQueue.push(NotPreferredSUnit);
607 NotPreferredSUnit =
nullptr;
611 if (!NotReady.empty()) {
612 AvailableQueue.push_all(NotReady);
619 unsigned NumPreNoops = HazardRec->PreEmitNoops(FoundSUnit);
620 for (
unsigned i = 0; i != NumPreNoops; ++i)
624 ScheduleNodeTopDown(FoundSUnit, CurCycle);
625 HazardRec->EmitInstruction(FoundSUnit);
626 CycleHasInsts =
true;
627 if (HazardRec->atIssueLimit()) {
628 LLVM_DEBUG(
dbgs() <<
"*** Max instructions per cycle " << CurCycle
630 HazardRec->AdvanceCycle();
632 CycleHasInsts =
false;
637 HazardRec->AdvanceCycle();
638 }
else if (!HasNoopHazards) {
642 HazardRec->AdvanceCycle();
652 CycleHasInsts =
false;
657 unsigned ScheduledNodes = VerifyScheduledDAG(
false);
660 "The number of nodes scheduled doesn't match the expected number!");
665void SchedulePostRATDList::EmitSchedule() {
666 RegionBegin = RegionEnd;
670 BB->
splice(RegionEnd, BB, FirstDbgValue);
673 for (
unsigned i = 0, e =
Sequence.size(); i != e; i++) {
674 if (
SUnit *SU = Sequence[i])
683 RegionBegin = std::prev(RegionEnd);
687 for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
688 DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
689 std::pair<MachineInstr *, MachineInstr *>
P = *std::prev(DI);
695 FirstDbgValue =
nullptr;
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
const HexagonInstrInfo * TII
Machine Instruction Scheduler
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< int > DebugDiv("postra-sched-debugdiv", cl::desc("Debug control MBBs that are scheduled"), cl::init(0), cl::Hidden)
static cl::opt< bool > EnablePostRAScheduler("post-RA-scheduler", cl::desc("Enable scheduling after register allocation"), cl::init(false), cl::Hidden)
static cl::opt< std::string > EnableAntiDepBreaking("break-anti-dependencies", cl::desc("Break post-RA scheduling anti-dependencies: " "\"critical\", \"all\", or \"none\""), cl::init("none"), cl::Hidden)
static cl::opt< int > DebugMod("postra-sched-debugmod", cl::desc("Debug control MBBs that are scheduled"), cl::init(0), cl::Hidden)
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.
Class recording the (high level) value of a variable.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
This class works in conjunction with the post-RA scheduler to rename registers to break register anti...
virtual ~AntiDepBreaker()
void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
Insert a noop into the instruction stream at the specified point.
bool isSchedulingBoundary(const MachineInstr &MI, const MachineBasicBlock *MBB, const MachineFunction &MF) const override
Test if the given instruction should be considered a scheduling boundary.
Itinerary data supplied by a subtarget to be used by a target.
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 '...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
void runOnMachineFunction(const MachineFunction &MF)
runOnFunction - Prepare to answer questions about MF.
bool isWeak() const
Tests if this a weak dependence.
Scheduling unit. This is a node in the scheduling DAG.
unsigned getDepth() const
Returns the depth of this node, which is the length of the maximum path up to any node which has no p...
bool isScheduled
True once scheduled.
bool isAvailable
True once available.
SmallVector< SDep, 4 > Succs
All sunit successors.
SmallVectorImpl< SDep >::iterator succ_iterator
void setDepthToAtLeast(unsigned NewDepth)
If NewDepth is greater than this node's depth value, sets it to be the new depth value.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
A ScheduleDAG for scheduling lists of MachineInstr.
virtual void finishBlock()
Cleans up after scheduling in the given block.
virtual void startBlock(MachineBasicBlock *BB)
Prepares to perform scheduling in the given block.
virtual void exitRegion()
Called when the scheduler has finished scheduling the current region.
virtual void schedule()=0
Orders nodes according to selected style.
virtual void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs)
Initialize the DAG and common scheduler state for a new scheduling region.
void clearDAG()
Clears the DAG state (between regions).
HazardRecognizer - This determines whether or not an instruction can be issued this cycle,...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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.
Target-Independent Code Generator Pass Configuration Options.
CodeGenOpt::Level getOptLevel() const
TargetSubtargetInfo - Generic base class for all target subtargets.
enum { ANTIDEP_NONE, ANTIDEP_CRITICAL, ANTIDEP_ALL } AntiDepBreakMode
virtual const TargetInstrInfo * getInstrInfo() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Level
Code generation optimization level.
initializer< Ty > init(const Ty &Val)
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
AntiDepBreaker * createAggressiveAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI, TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
char & PostRASchedulerID
PostRAScheduler - This pass performs post register allocation scheduling.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
AntiDepBreaker * createCriticalAntiDepBreaker(MachineFunction &MFi, const RegisterClassInfo &RCI)
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.