LLVM 24.0.0git
PPCMachineScheduler.cpp
Go to the documentation of this file.
1//===- PPCMachineScheduler.cpp - MI Scheduler for PowerPC -------------===//
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
11#include "PPCSubtarget.h"
12
13using namespace llvm;
14
17 CLOpts(C->MF->getSubtarget<PPCSubtarget>().getCLOpts()) {}
18
21 CLOpts(C->MF->getSubtarget<PPCSubtarget>().getCLOpts()) {}
22
24 return Cand.SU->getInstr()->getOpcode() == PPC::ADDI ||
25 Cand.SU->getInstr()->getOpcode() == PPC::ADDI8;
26}
27
28bool PPCPreRASchedStrategy::biasAddiLoadCandidate(SchedCandidate &Cand,
29 SchedCandidate &TryCand,
30 SchedBoundary &Zone) const {
31 if (!CLOpts.sched_addi_load)
32 return false;
33
34 SchedCandidate &FirstCand = Zone.isTop() ? TryCand : Cand;
35 SchedCandidate &SecondCand = Zone.isTop() ? Cand : TryCand;
36 if (isADDIInstr(FirstCand) && SecondCand.SU->getInstr()->mayLoad()) {
37 TryCand.Reason = Stall;
38 return true;
39 }
40 if (FirstCand.SU->getInstr()->mayLoad() && isADDIInstr(SecondCand)) {
41 TryCand.Reason = NoCand;
42 return true;
43 }
44
45 return false;
46}
47
49 SchedCandidate &TryCand,
50 SchedBoundary *Zone) const {
51 // From GenericScheduler::tryCandidate
52
53 // Initialize the candidate if needed.
54 if (!Cand.isValid()) {
55 TryCand.Reason = NodeOrder;
56 return true;
57 }
58
59 // Bias PhysReg Defs and copies to their uses and defined respectively.
60 if (tryGreater(biasPhysReg(TryCand.SU, TryCand.AtTop),
61 biasPhysReg(Cand.SU, Cand.AtTop), TryCand, Cand, PhysReg))
62 return TryCand.Reason != NoCand;
63
64 // Avoid exceeding the target's limit.
65 if (DAG->isTrackingPressure() &&
66 tryPressure(TryCand.RPDelta.Excess, Cand.RPDelta.Excess, TryCand, Cand,
67 RegExcess, TRI, DAG->MF))
68 return TryCand.Reason != NoCand;
69
70 // Avoid increasing the max critical pressure in the scheduled region.
71 if (DAG->isTrackingPressure() &&
73 TryCand, Cand, RegCritical, TRI, DAG->MF))
74 return TryCand.Reason != NoCand;
75
76 // We only compare a subset of features when comparing nodes between
77 // Top and Bottom boundary. Some properties are simply incomparable, in many
78 // other instances we should only override the other boundary if something
79 // is a clear good pick on one boundary. Skip heuristics that are more
80 // "tie-breaking" in nature.
81 bool SameBoundary = Zone != nullptr;
82 if (SameBoundary) {
83 // For loops that are acyclic path limited, aggressively schedule for
84 // latency. Within an single cycle, whenever CurrMOps > 0, allow normal
85 // heuristics to take precedence.
86 if (Rem.IsAcyclicLatencyLimited && !Zone->getCurrMOps() &&
87 tryLatency(TryCand, Cand, *Zone))
88 return TryCand.Reason != NoCand;
89
90 // Prioritize instructions that read unbuffered resources by stall cycles.
91 if (tryLess(Zone->getLatencyStallCycles(TryCand.SU),
92 Zone->getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
93 return TryCand.Reason != NoCand;
94 }
95
96 // Keep clustered nodes together to encourage downstream peephole
97 // optimizations which may reduce resource requirements.
98 //
99 // This is a best effort to set things up for a post-RA pass. Optimizations
100 // like generating loads of multiple registers should ideally be done within
101 // the scheduler pass by combining the loads during DAG postprocessing.
102 unsigned CandZoneCluster = Cand.AtTop ? TopClusterID : BotClusterID;
103 unsigned TryCandZoneCluster = TryCand.AtTop ? TopClusterID : BotClusterID;
104 bool CandIsClusterSucc =
105 isTheSameCluster(CandZoneCluster, Cand.SU->ParentClusterIdx);
106 bool TryCandIsClusterSucc =
107 isTheSameCluster(TryCandZoneCluster, TryCand.SU->ParentClusterIdx);
108
109 if (tryGreater(TryCandIsClusterSucc, CandIsClusterSucc, TryCand, Cand,
110 Cluster))
111 return TryCand.Reason != NoCand;
112
113 if (SameBoundary) {
114 // Weak edges are for clustering and other constraints.
115 if (tryLess(getWeakLeft(TryCand.SU, TryCand.AtTop),
116 getWeakLeft(Cand.SU, Cand.AtTop), TryCand, Cand, Weak))
117 return TryCand.Reason != NoCand;
118 }
119
120 // Avoid increasing the max pressure of the entire region.
121 if (DAG->isTrackingPressure() &&
122 tryPressure(TryCand.RPDelta.CurrentMax, Cand.RPDelta.CurrentMax, TryCand,
123 Cand, RegMax, TRI, DAG->MF))
124 return TryCand.Reason != NoCand;
125
126 if (SameBoundary) {
127 // Avoid critical resource consumption and balance the schedule.
130 TryCand, Cand, ResourceReduce))
131 return TryCand.Reason != NoCand;
133 Cand.ResDelta.DemandedResources, TryCand, Cand,
135 return TryCand.Reason != NoCand;
136
137 // Avoid serializing long latency dependence chains.
138 // For acyclic path limited loops, latency was already checked above.
139 if (!RegionPolicy.DisableLatencyHeuristic && TryCand.Policy.ReduceLatency &&
140 !Rem.IsAcyclicLatencyLimited && tryLatency(TryCand, Cand, *Zone))
141 return TryCand.Reason != NoCand;
142
143 // Fall through to original instruction order.
144 if ((Zone->isTop() && TryCand.SU->NodeNum < Cand.SU->NodeNum) ||
145 (!Zone->isTop() && TryCand.SU->NodeNum > Cand.SU->NodeNum)) {
146 TryCand.Reason = NodeOrder;
147 }
148 }
149
150 // GenericScheduler::tryCandidate end
151
152 // Add powerpc specific heuristic only when TryCand isn't selected or
153 // selected as node order.
154 if (TryCand.Reason != NodeOrder && TryCand.Reason != NoCand)
155 return true;
156
157 // There are some benefits to schedule the ADDI before the load to hide the
158 // latency, as RA may create a true dependency between the load and addi.
159 if (SameBoundary) {
160 if (biasAddiLoadCandidate(Cand, TryCand, *Zone))
161 return TryCand.Reason != NoCand;
162 }
163
164 return TryCand.Reason != NoCand;
165}
166
168 SchedCandidate &TryCand) const {
169 if (!CLOpts.postra_bias_addi)
170 return false;
171
172 if (isADDIInstr(TryCand) && !isADDIInstr(Cand)) {
173 TryCand.Reason = Stall;
174 return true;
175 }
176 return false;
177}
178
180 SchedCandidate &TryCand) {
181 // From PostGenericScheduler::tryCandidate
182
183 // Initialize the candidate if needed.
184 if (!Cand.isValid()) {
185 TryCand.Reason = NodeOrder;
186 return true;
187 }
188
189 // Prioritize instructions that read unbuffered resources by stall cycles.
190 if (tryLess(Top.getLatencyStallCycles(TryCand.SU),
191 Top.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
192 return TryCand.Reason != NoCand;
193
194 // Keep clustered nodes together.
195 unsigned CandZoneCluster = Cand.AtTop ? TopClusterID : BotClusterID;
196 unsigned TryCandZoneCluster = TryCand.AtTop ? TopClusterID : BotClusterID;
197 bool CandIsClusterSucc =
198 isTheSameCluster(CandZoneCluster, Cand.SU->ParentClusterIdx);
199 bool TryCandIsClusterSucc =
200 isTheSameCluster(TryCandZoneCluster, TryCand.SU->ParentClusterIdx);
201
202 if (tryGreater(TryCandIsClusterSucc, CandIsClusterSucc, TryCand, Cand,
203 Cluster))
204 return TryCand.Reason != NoCand;
205
206 // Avoid critical resource consumption and balance the schedule.
208 TryCand, Cand, ResourceReduce))
209 return TryCand.Reason != NoCand;
211 Cand.ResDelta.DemandedResources, TryCand, Cand,
213 return TryCand.Reason != NoCand;
214
215 // Avoid serializing long latency dependence chains.
216 if (Cand.Policy.ReduceLatency && tryLatency(TryCand, Cand, Top)) {
217 return TryCand.Reason != NoCand;
218 }
219
220 // Fall through to original instruction order.
221 if (TryCand.SU->NodeNum < Cand.SU->NodeNum)
222 TryCand.Reason = NodeOrder;
223
224 // PostGenericScheduler::tryCandidate end
225
226 // Add powerpc post ra specific heuristic only when TryCand isn't selected or
227 // selected as node order.
228 if (TryCand.Reason != NodeOrder && TryCand.Reason != NoCand)
229 return true;
230
231 // There are some benefits to schedule the ADDI as early as possible post ra
232 // to avoid stalled by vector instructions which take up all the hw units.
233 // And ADDI is usually used to post inc the loop indvar, which matters the
234 // performance.
235 if (biasAddiCandidate(Cand, TryCand))
236 return TryCand.Reason != NoCand;
237
238 return TryCand.Reason != NoCand;
239}
240
242 // Custom PPC PostRA specific behavior here.
244}
245
247 // Custom PPC PostRA specific behavior here.
249}
250
252 // Custom PPC PostRA specific initialization here.
254}
255
257 // Custom PPC PostRA specific scheduling here.
258 return PostGenericScheduler::pickNode(IsTopNode);
259}
260
MachineBasicBlock & MBB
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static bool isADDIInstr(const GenericScheduler::SchedCandidate &Cand)
MachineSchedPolicy RegionPolicy
const TargetSchedModel * SchedModel
const TargetRegisterInfo * TRI
ScheduleDAGMILive * DAG
GenericScheduler(const MachineSchedContext *C)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
virtual void leaveMBB()
Tell the strategy that current MBB is done.
virtual void enterMBB(MachineBasicBlock *MBB)
Tell the strategy that MBB is about to be processed.
PPCPostRASchedStrategy(const MachineSchedContext *C)
SUnit * pickNode(bool &IsTopNode) override
Pick the next node to schedule.
void enterMBB(MachineBasicBlock *MBB) override
Tell the strategy that MBB is about to be processed.
bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand) override
Apply a set of heuristics to a new candidate for PostRA scheduling.
bool biasAddiCandidate(SchedCandidate &Cand, SchedCandidate &TryCand) const
void leaveMBB() override
Tell the strategy that current MBB is done.
void initialize(ScheduleDAGMI *Dag) override
Initialize the strategy after building the DAG for a new region.
PPCPreRASchedStrategy(const MachineSchedContext *C)
bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand, SchedBoundary *Zone) const override
Apply a set of heuristics to a new candidate.
void initialize(ScheduleDAGMI *Dag) override
Initialize the strategy after building the DAG for a new region.
PostGenericScheduler(const MachineSchedContext *C)
SUnit * pickNode(bool &IsTopNode) override
Pick the next node to schedule.
Scheduling unit. This is a node in the scheduling DAG.
unsigned NodeNum
Entry # of node in the node vector.
unsigned ParentClusterIdx
The parent cluster id.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
Each Scheduling boundary is associated with ready queues.
LLVM_ABI unsigned getLatencyStallCycles(SUnit *SU)
Get the difference between the given SUnit's ready time and the current cycle.
unsigned getCurrMOps() const
Micro-ops issued in the current cycle.
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI int biasPhysReg(const SUnit *SU, bool isTop, bool BiasPRegsExtra=false)
Minimize physical register live ranges.
LLVM_ABI unsigned getWeakLeft(const SUnit *SU, bool isTop)
LLVM_ABI bool tryPressure(const PressureChange &TryP, const PressureChange &CandP, GenericSchedulerBase::SchedCandidate &TryCand, GenericSchedulerBase::SchedCandidate &Cand, GenericSchedulerBase::CandReason Reason, const TargetRegisterInfo *TRI, const MachineFunction &MF)
LLVM_ABI bool tryLatency(GenericSchedulerBase::SchedCandidate &TryCand, GenericSchedulerBase::SchedCandidate &Cand, SchedBoundary &Zone)
bool isTheSameCluster(unsigned A, unsigned B)
Return whether the input cluster ID's are the same and valid.
LLVM_ABI bool tryGreater(int TryVal, int CandVal, GenericSchedulerBase::SchedCandidate &TryCand, GenericSchedulerBase::SchedCandidate &Cand, GenericSchedulerBase::CandReason Reason)
LLVM_ABI bool tryLess(int TryVal, int CandVal, GenericSchedulerBase::SchedCandidate &TryCand, GenericSchedulerBase::SchedCandidate &Cand, GenericSchedulerBase::CandReason Reason)
Return true if this heuristic determines order.
Store the state used by GenericScheduler heuristics, required for the lifetime of one invocation of p...
LLVM_ABI void initResourceDelta(const ScheduleDAGMI *DAG, const TargetSchedModel *SchedModel)
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...