LLVM 24.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 const SystemZOptions &CLOpts;
74
75 // A SchedModel is needed before any DAG is built while advancing past
76 // non-scheduled instructions, so it would not always be possible to call
77 // DAG->getSchedClass(SU).
78 TargetSchedModel SchedModel;
79
80 /// A candidate during instruction evaluation.
81 struct Candidate {
82 SUnit *SU = nullptr;
83
84 /// The decoding cost.
85 int GroupingCost = 0;
86
87 /// The processor resources cost.
88 int ResourcesCost = 0;
89
90 Candidate() = default;
91 Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec);
92
93 // Compare two candidates.
94 bool operator<(const Candidate &other);
95
96 // Check if this node is free of cost ("as good as any").
97 bool noCost() const {
98 return (GroupingCost <= 0 && !ResourcesCost);
99 }
100
101#ifndef NDEBUG
102 void dumpCosts() {
103 if (GroupingCost != 0)
104 dbgs() << " Grouping cost:" << GroupingCost;
105 if (ResourcesCost != 0)
106 dbgs() << " Resource cost:" << ResourcesCost;
107 }
108#endif
109 };
110
111 // A sorter for the Available set that makes sure that SUs are considered
112 // in the best order.
113 struct SUSorter {
114 bool operator() (SUnit *lhs, SUnit *rhs) const {
115 if (lhs->isScheduleHigh && !rhs->isScheduleHigh)
116 return true;
117 if (!lhs->isScheduleHigh && rhs->isScheduleHigh)
118 return false;
119
120 if (lhs->getHeight() > rhs->getHeight())
121 return true;
122 else if (lhs->getHeight() < rhs->getHeight())
123 return false;
124
125 return (lhs->NodeNum < rhs->NodeNum);
126 }
127 };
128 // A set of SUs with a sorter and dump method.
129 struct SUSet : std::set<SUnit*, SUSorter> {
130 #ifndef NDEBUG
131 void dump(SystemZHazardRecognizer &HazardRec) const;
132 #endif
133 };
134
135 /// The set of available SUs to schedule next.
136 SUSet Available;
137
138 /// Current MBB
140
141 /// Maintain hazard recognizers for all blocks, so that the scheduler state
142 /// can be maintained past BB boundaries when appropariate.
143 typedef std::map<MachineBasicBlock*, SystemZHazardRecognizer*> MBB2HazRec;
144 MBB2HazRec SchedStates;
145
146 /// Pointer to the HazardRecognizer that tracks the scheduler state for
147 /// the current region.
148 SystemZHazardRecognizer *HazardRec;
149
150 /// Update the scheduler state by emitting (non-scheduled) instructions
151 /// up to, but not including, NextBegin.
152 void advanceTo(MachineBasicBlock::iterator NextBegin);
153
154public:
157
158 /// Called for a region before scheduling.
161 unsigned NumRegionInstrs) override;
162
163 /// PostRA scheduling does not track pressure.
164 bool shouldTrackPressure() const override { return false; }
165
166 // Process scheduling regions top-down so that scheduler states can be
167 // transferrred over scheduling boundaries.
168 bool doMBBSchedRegionsTopDown() const override { return true; }
169
170 void initialize(ScheduleDAGMI *dag) override;
171
172 /// Tell the strategy that MBB is about to be processed.
173 void enterMBB(MachineBasicBlock *NextMBB) override;
174
175 /// Tell the strategy that current MBB is done.
176 void leaveMBB() override;
177
178 /// Pick the next node to schedule, or return NULL.
179 SUnit *pickNode(bool &IsTopNode) override;
180
181 /// ScheduleDAGMI has scheduled an instruction - tell HazardRec
182 /// about it.
183 void schedNode(SUnit *SU, bool IsTopNode) override;
184
185 /// SU has had all predecessor dependencies resolved. Put it into
186 /// Available.
187 void releaseTopNode(SUnit *SU) override;
188
189 /// Currently only scheduling top-down, so this method is empty.
190 void releaseBottomNode(SUnit *SU) override {};
191};
192
193} // end namespace llvm
194
195#endif // LLVM_LIB_TARGET_SYSTEMZ_SYSTEMZMACHINESCHEDULER_H
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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.
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
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
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...