33#define DEBUG_TYPE "scheduler"
36 : Picker(this), InstrItins(IS->MF->getSubtarget().getInstrItineraryData()) {
41 Opts = &IS->CurDAG->getSelectionDAGOptions();
42 ResourcesModel.reset(TII->CreateTargetScheduleState(STI));
45 assert(ResourcesModel &&
"Unimplemented CreateTargetScheduleState.");
49 unsigned NumRC = TRI->getNumRegClasses();
50 RegLimit.resize(NumRC);
51 RegPressure.resize(NumRC);
57ResourcePriorityQueue::numberRCValPredInSU(
SUnit *SU,
unsigned RCId) {
58 unsigned NumberDeps = 0;
63 SUnit *PredSU = Pred.getSUnit();
82 for (
unsigned i = 0, e = ScegN->
getNumValues(); i != e; ++i) {
84 if (
TLI->isTypeLegal(VT)
85 && (
TLI->getRegClassFor(VT)->getID() == RCId)) {
94unsigned ResourcePriorityQueue::numberRCValSuccInSU(
SUnit *SU,
96 unsigned NumberDeps = 0;
97 for (
const SDep &Succ : SU->
Succs) {
102 const SDNode *ScegN = SuccSU->
getNode();
121 MVT VT =
Op.getNode()->getSimpleValueType(
Op.getResNo());
122 if (TLI->isTypeLegal(VT)
123 && (TLI->getRegClassFor(VT)->getID() == RCId)) {
133 unsigned NumberDeps = 0;
142 unsigned NumberDeps = 0;
155 NumNodesSolelyBlocking.assign(SUnits->size(), 0);
159 RegLimit[RC.getID()] = TRI->getRegPressureLimit(&RC, *MF);
161 ParallelLiveRanges = 0;
162 HorizontalVerticalBalance = 0;
163 ResourcesModel->clearResources();
166 for (
SUnit &SU : *SUnits) {
178 if (LHS->isScheduleHigh && !RHS->isScheduleHigh)
181 if (!LHS->isScheduleHigh && RHS->isScheduleHigh)
184 unsigned LHSNum = LHS->NodeNum;
185 unsigned RHSNum = RHS->NodeNum;
188 unsigned LHSLatency =
PQ->getLatency(LHSNum);
189 unsigned RHSLatency =
PQ->getLatency(RHSNum);
190 if (LHSLatency < RHSLatency)
return true;
191 if (LHSLatency > RHSLatency)
return false;
195 unsigned LHSBlocked =
PQ->getNumSolelyBlockNodes(LHSNum);
196 unsigned RHSBlocked =
PQ->getNumSolelyBlockNodes(RHSNum);
197 if (LHSBlocked < RHSBlocked)
return true;
198 if (LHSBlocked > RHSBlocked)
return false;
202 return LHSNum < RHSNum;
208SUnit *ResourcePriorityQueue::getSingleUnscheduledPred(
SUnit *SU) {
209 SUnit *OnlyAvailablePred =
nullptr;
211 SUnit &PredSU = *Pred.getSUnit();
215 if (OnlyAvailablePred && OnlyAvailablePred != &PredSU)
217 OnlyAvailablePred = &PredSU;
220 return OnlyAvailablePred;
226 unsigned NumNodesBlocking = 0;
228 if (getSingleUnscheduledPred(Succ.
getSUnit()) == SU)
231 NumNodesSolelyBlocking[SU->
NodeNum] = NumNodesBlocking;
251 if (!ResourcesModel->canReserveResources(&TII->get(
255 case TargetOpcode::EXTRACT_SUBREG:
256 case TargetOpcode::INSERT_SUBREG:
257 case TargetOpcode::SUBREG_TO_REG:
258 case TargetOpcode::REG_SEQUENCE:
259 case TargetOpcode::IMPLICIT_DEF:
265 for (
const SUnit *S : Packet)
266 for (
const SDep &Succ : S->Succs) {
284 ResourcesModel->clearResources();
291 ResourcesModel->reserveResources(&TII->get(
294 case TargetOpcode::EXTRACT_SUBREG:
295 case TargetOpcode::INSERT_SUBREG:
296 case TargetOpcode::SUBREG_TO_REG:
297 case TargetOpcode::REG_SEQUENCE:
298 case TargetOpcode::IMPLICIT_DEF:
301 Packet.push_back(SU);
305 ResourcesModel->clearResources();
311 if (Packet.size() >= InstrItins->SchedModel.IssueWidth) {
312 ResourcesModel->clearResources();
326 if (TLI->isTypeLegal(VT)
327 && TLI->getRegClassFor(VT)
328 && TLI->getRegClassFor(VT)->getID() == RCId)
329 RegBalance += numberRCValSuccInSU(SU, RCId);
334 MVT VT =
Op.getNode()->getSimpleValueType(
Op.getResNo());
338 if (TLI->isTypeLegal(VT) && TLI->getRegClassFor(VT)
339 && TLI->getRegClassFor(VT)->getID() == RCId)
340 RegBalance -= numberRCValPredInSU(SU, RCId);
365 RegLimit[RC.getID()]))
401 if (HorizontalVerticalBalance > Opts->dfa_sched_reg_pressure_threshold) {
432 if (
N->isMachineOpcode()) {
433 const MCInstrDesc &TID = TII->get(
N->getMachineOpcode());
438 switch (
N->getOpcode()) {
461 ResourcesModel->clearResources();
471 for (
unsigned i = 0, e = ScegN->
getNumValues(); i != e; ++i) {
474 if (TLI->isTypeLegal(VT)) {
477 RegPressure[RC->
getID()] += numberRCValSuccInSU(SU, RC->
getID());
483 MVT VT =
Op.getNode()->getSimpleValueType(
Op.getResNo());
485 if (TLI->isTypeLegal(VT)) {
488 if (RegPressure[RC->
getID()] >
489 (numberRCValPredInSU(SU, RC->
getID())))
490 RegPressure[RC->
getID()] -= numberRCValPredInSU(SU, RC->
getID());
491 else RegPressure[RC->
getID()] = 0;
496 if (Pred.isCtrl() || (Pred.getSUnit()->NumRegDefsLeft == 0))
498 --Pred.getSUnit()->NumRegDefsLeft;
508 unsigned NumberNonControlDeps = 0;
511 adjustPriorityOfUnscheduledPreds(Succ.
getSUnit());
513 NumberNonControlDeps++;
516 if (!NumberNonControlDeps) {
517 if (ParallelLiveRanges >= SU->
NumPreds)
520 ParallelLiveRanges = 0;
532 unsigned NodeNumDefs = 0;
534 if (
N->isMachineOpcode()) {
535 const MCInstrDesc &TID = TII->get(
N->getMachineOpcode());
537 if (
N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
541 NodeNumDefs = std::min(
N->getNumValues(), TID.
getNumDefs());
544 switch(
N->getOpcode()) {
564void ResourcePriorityQueue::adjustPriorityOfUnscheduledPreds(
SUnit *SU) {
567 SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU);
568 if (!OnlyAvailablePred || !OnlyAvailablePred->
isAvailable)
573 remove(OnlyAvailablePred);
577 push(OnlyAvailablePred);
587 std::vector<SUnit *>::iterator Best = Queue.begin();
588 if (!Opts->disable_dfa_sched) {
590 for (
auto I = std::next(Queue.begin()), E = Queue.end();
I != E; ++
I) {
600 for (
auto I = std::next(Queue.begin()), E = Queue.end();
I != E; ++
I)
601 if (Picker(*Best, *
I))
606 if (Best != std::prev(Queue.end()))
616 assert(!Queue.empty() &&
"Queue is empty!");
617 std::vector<SUnit *>::iterator
I =
find(Queue, SU);
618 if (
I != std::prev(Queue.end()))
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const unsigned PriorityTwo
static const unsigned FactorOne
static const unsigned PriorityThree
static const unsigned ScaleOne
static unsigned numberCtrlPredInSU(SUnit *SU)
static const unsigned PriorityOne
static unsigned numberCtrlDepsInSU(SUnit *SU)
static const unsigned PriorityFour
static const unsigned ScaleTwo
static const unsigned ScaleThree
This file describes how to lower LLVM code to machine code.
Describe properties that are true of each instruction in the target description file.
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
bool isCall() const
Return true if the instruction is a call.
unsigned getID() const
getID() - Return the register class ID number.
void push(SUnit *U) override
void scheduledNode(SUnit *SU) override
scheduledNode - Main resource tracking point.
ResourcePriorityQueue(SelectionDAGISel *IS)
void initNodes(std::vector< SUnit > &sunits) override
Initialize nodes.
bool empty() const override
SUnit * pop() override
Main access point - returns next instructions to be placed in scheduling sequence.
int rawRegPressureDelta(SUnit *SU, unsigned RCId)
int regPressureDelta(SUnit *SU, bool RawPressure=false)
Estimates change in reg pressure from this SU.
void remove(SUnit *SU) override
~ResourcePriorityQueue() override
void reserveResources(SUnit *SU)
Keep track of available resources.
int SUSchedulingCost(SUnit *SU)
Single cost function reflecting benefit of scheduling SU in the current cycle.
void initNumRegDefsLeft(SUnit *SU)
InitNumRegDefsLeft - Determine the # of regs defined by this node.
bool isResourceAvailable(SUnit *SU)
Check if scheduling of this SU is possible in the current packet.
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
MVT getSimpleValueType(unsigned ResNo) const
Return the type of a specified result as a simple type.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
unsigned getMachineOpcode() const
This may only be called if isMachineOpcode returns true.
const SDValue & getOperand(unsigned Num) const
SDNode * getGluedNode() const
If this node has a glue operand, return the node to which the glue operand points.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isCtrl() const
Shorthand for getKind() != SDep::Data.
Scheduling unit. This is a node in the scheduling DAG.
unsigned NodeQueueId
Queue id of node.
unsigned NodeNum
Entry # of node in the node vector.
unsigned getHeight() const
Returns the height of this node, which is the length of the maximum path down to any node which has n...
unsigned short NumRegDefsLeft
bool isScheduleHigh
True if preferable to schedule high.
bool isScheduled
True once scheduled.
bool isAvailable
True once available.
SmallVector< SDep, 4 > Succs
All sunit successors.
SDNode * getNode() const
Returns the representative SDNode for this SUnit.
SmallVector< SDep, 4 > Preds
All sunit predecessors.
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
@ INLINEASM
INLINEASM - Represents an inline asm block.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
MCRegisterClass TargetRegisterClass
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
LLVM_ABI bool operator()(const SUnit *LHS, const SUnit *RHS) const
This heuristic is used if DFA scheduling is not desired for some VLIW platform.
ResourcePriorityQueue * PQ