LLVM 23.0.0git
SystemZMachineScheduler.h
Go to the documentation of this file.
1//==- SystemZMachineScheduler.h - SystemZ Scheduler Interface ----*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// -------------------------- Pre RA scheduling ----------------------------- //
10//
11// SystemZPreRASchedStrategy reduces register pressure by scheduling a (live)
12// definition low if it does not cause another register to become live (all
13// uses live). In most regions it then reduces the scheduled latency but only
14// if the SU that is higher (by Height) than the scheduled latency is as well
15// lower (by Depth) than the remaining latency. It also helps copy coalescing
16// and comparison elimination.
17//
18// -------------------------- Post RA scheduling ---------------------------- //
19//
20// SystemZPostRASchedStrategy is a scheduling strategy which is plugged into
21// the MachineScheduler. It has a sorted Available set of SUs and a pickNode()
22// implementation that looks to optimize decoder grouping and balance the
23// usage of processor resources. Scheduler states are saved for the end
24// region of each MBB, so that a successor block can learn from it.
25//
26//----------------------------------------------------------------------------//
27
28#ifndef LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
29#define LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
30
34#include <set>
35
36namespace llvm {
37
38/// A MachineSchedStrategy implementation for SystemZ pre RA scheduling.
40 Register Cmp0SrcReg;
41 // Return true if MI defines the Cmp0SrcReg that is used by a scheduled
42 // compare with 0. If CCDef is true MI must also have an implicit def of CC.
43 bool definesCmp0Src(const MachineInstr *MI, bool CCDef = true) const;
44
45 // SUs that have a Height of at least this value will not be scheduled
46 // "low" to reduce liveness.
47 unsigned LivenessHeightCutOff;
48
49 // Return true if the instruction defines a register while all use operands
50 // are already live.
51 bool closesLiveRange(const SUnit *SU, ScheduleDAGMILive *DAG) const;
52
53protected:
54 bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand,
55 SchedBoundary *Zone) const override;
56
57public:
60
63 unsigned NumRegionInstrs) override;
64 void initialize(ScheduleDAGMI *dag) override;
65 void schedNode(SUnit *SU, bool IsTopNode) override;
66};
67
68/// A MachineSchedStrategy implementation for SystemZ post RA scheduling.
70
71 const MachineLoopInfo *MLI;
72 const SystemZInstrInfo *TII;
73
74 // A SchedModel is needed before any DAG is built while advancing past
75 // non-scheduled instructions, so it would not always be possible to call
76 // DAG->getSchedClass(SU).
77 TargetSchedModel SchedModel;
78
79 /// A candidate during instruction evaluation.
80 struct Candidate {
81 SUnit *SU = nullptr;
82
83 /// The decoding cost.
84 int GroupingCost = 0;
85
86 /// The processor resources cost.
87 int ResourcesCost = 0;
88
89 Candidate() = default;
90 Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec);
91
92 // Compare two candidates.
93 bool operator<(const Candidate &other);
94
95 // Check if this node is free of cost ("as good as any").
96 bool noCost() const {
97 return (GroupingCost <= 0 && !ResourcesCost);
98 }
99
100#ifndef NDEBUG
101 void dumpCosts() {
102 if (GroupingCost != 0)
103 dbgs() << " Grouping cost:" << GroupingCost;
104 if (ResourcesCost != 0)
105 dbgs() << " Resource cost:" << ResourcesCost;
106 }
107#endif
108 };
109
110 // A sorter for the Available set that makes sure that SUs are considered
111 // in the best order.
112 struct SUSorter {
113 bool operator() (SUnit *lhs, SUnit *rhs) const {
114 if (lhs->isScheduleHigh && !rhs->isScheduleHigh)
115 return true;
116 if (!lhs->isScheduleHigh && rhs->isScheduleHigh)
117 return false;
118
119 if (lhs->getHeight() > rhs->getHeight())
120 return true;
121 else if (lhs->getHeight() < rhs->getHeight())
122 return false;
123
124 return (lhs->NodeNum < rhs->NodeNum);
125 }
126 };
127 // A set of SUs with a sorter and dump method.
128 struct SUSet : std::set<SUnit*, SUSorter> {
129 #ifndef NDEBUG
130 void dump(SystemZHazardRecognizer &HazardRec) const;
131 #endif
132 };
133
134 /// The set of available SUs to schedule next.
135 SUSet Available;
136
137 /// Current MBB
139
140 /// Maintain hazard recognizers for all blocks, so that the scheduler state
141 /// can be maintained past BB boundaries when appropariate.
142 typedef std::map<MachineBasicBlock*, SystemZHazardRecognizer*> MBB2HazRec;
143 MBB2HazRec SchedStates;
144
145 /// Pointer to the HazardRecognizer that tracks the scheduler state for
146 /// the current region.
147 SystemZHazardRecognizer *HazardRec;
148
149 /// Update the scheduler state by emitting (non-scheduled) instructions
150 /// up to, but not including, NextBegin.
151 void advanceTo(MachineBasicBlock::iterator NextBegin);
152
153public:
156
157 /// Called for a region before scheduling.
160 unsigned NumRegionInstrs) override;
161
162 /// PostRA scheduling does not track pressure.
163 bool shouldTrackPressure() const override { return false; }
164
165 // Process scheduling regions top-down so that scheduler states can be
166 // transferrred over scheduling boundaries.
167 bool doMBBSchedRegionsTopDown() const override { return true; }
168
169 void initialize(ScheduleDAGMI *dag) override;
170
171 /// Tell the strategy that MBB is about to be processed.
172 void enterMBB(MachineBasicBlock *NextMBB) override;
173
174 /// Tell the strategy that current MBB is done.
175 void leaveMBB() override;
176
177 /// Pick the next node to schedule, or return NULL.
178 SUnit *pickNode(bool &IsTopNode) override;
179
180 /// ScheduleDAGMI has scheduled an instruction - tell HazardRec
181 /// about it.
182 void schedNode(SUnit *SU, bool IsTopNode) override;
183
184 /// SU has had all predecessor dependencies resolved. Put it into
185 /// Available.
186 void releaseTopNode(SUnit *SU) override;
187
188 /// Currently only scheduling top-down, so this method is empty.
189 void releaseBottomNode(SUnit *SU) override {};
190};
191
192} // end namespace llvm
193
194#endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
IRTranslator LLVM IR MI
ScheduleDAGMILive * DAG
GenericScheduler(const MachineSchedContext *C)
MachineInstrBundleIterator< MachineInstr > iterator
Representation of each machine instruction.
MachineSchedStrategy - Interface to the scheduling algorithm used by ScheduleDAGMI.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Scheduling unit. This is a node in the scheduling DAG.
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...
bool isScheduleHigh
True if preferable to schedule high.
Each Scheduling boundary is associated with ready queues.
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
SystemZHazardRecognizer maintains the state for one MBB during scheduling.
bool shouldTrackPressure() const override
PostRA scheduling does not track pressure.
SUnit * pickNode(bool &IsTopNode) override
Pick the next node to schedule, or return NULL.
void leaveMBB() override
Tell the strategy that current MBB is done.
void releaseBottomNode(SUnit *SU) override
Currently only scheduling top-down, so this method is empty.
void initPolicy(MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned NumRegionInstrs) override
Called for a region before scheduling.
void schedNode(SUnit *SU, bool IsTopNode) override
ScheduleDAGMI has scheduled an instruction - tell HazardRec about it.
void initialize(ScheduleDAGMI *dag) override
Initialize the strategy after building the DAG for a new region.
void enterMBB(MachineBasicBlock *NextMBB) override
Tell the strategy that MBB is about to be processed.
SystemZPostRASchedStrategy(const MachineSchedContext *C)
void releaseTopNode(SUnit *SU) override
SU has had all predecessor dependencies resolved.
void schedNode(SUnit *SU, bool IsTopNode) override
Update the scheduler's state after scheduling a node.
void initPolicy(MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned NumRegionInstrs) override
Initialize the per-region scheduling policy.
SystemZPreRASchedStrategy(const MachineSchedContext *C)
void initialize(ScheduleDAGMI *dag) override
Initialize the strategy after building the DAG for a new region.
bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand, SchedBoundary *Zone) const override
Apply a set of heuristics to a new candidate.
Provide an instruction scheduling machine model to CodeGen passes.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
Store the state used by GenericScheduler heuristics, required for the lifetime of one invocation of p...
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...