blob: fcbf4c4b5fe4fadd77596f956197d754f6db307d [file] [log] [blame]
Jonas Paulsson8010b632016-10-20 08:27:16 +00001//-- SystemZMachineScheduler.cpp - SystemZ Scheduler Interface -*- C++ -*---==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// -------------------------- Post RA scheduling ---------------------------- //
11// SystemZPostRASchedStrategy is a scheduling strategy which is plugged into
12// the MachineScheduler. It has a sorted Available set of SUs and a pickNode()
13// implementation that looks to optimize decoder grouping and balance the
Jonas Paulsson57a705d2017-08-17 08:33:44 +000014// usage of processor resources. Scheduler states are saved for the end
15// region of each MBB, so that a successor block can learn from it.
Jonas Paulsson8010b632016-10-20 08:27:16 +000016//===----------------------------------------------------------------------===//
17
18#include "SystemZMachineScheduler.h"
19
20using namespace llvm;
21
Evandro Menezes0cd23f562017-07-11 22:08:28 +000022#define DEBUG_TYPE "machine-scheduler"
Jonas Paulsson8010b632016-10-20 08:27:16 +000023
24#ifndef NDEBUG
25// Print the set of SUs
26void SystemZPostRASchedStrategy::SUSet::
Rafael Espindola6da25f42017-06-21 23:02:57 +000027dump(SystemZHazardRecognizer &HazardRec) const {
Jonas Paulsson8010b632016-10-20 08:27:16 +000028 dbgs() << "{";
29 for (auto &SU : *this) {
30 HazardRec.dumpSU(SU, dbgs());
31 if (SU != *rbegin())
32 dbgs() << ", ";
33 }
34 dbgs() << "}\n";
35}
36#endif
37
Jonas Paulsson57a705d2017-08-17 08:33:44 +000038// Try to find a single predecessor that would be interesting for the
39// scheduler in the top-most region of MBB.
40static MachineBasicBlock *getSingleSchedPred(MachineBasicBlock *MBB,
41 const MachineLoop *Loop) {
42 MachineBasicBlock *PredMBB = nullptr;
43 if (MBB->pred_size() == 1)
44 PredMBB = *MBB->pred_begin();
45
46 // The loop header has two predecessors, return the latch, but not for a
47 // single block loop.
48 if (MBB->pred_size() == 2 && Loop != nullptr && Loop->getHeader() == MBB) {
49 for (auto I = MBB->pred_begin(); I != MBB->pred_end(); ++I)
50 if (Loop->contains(*I))
51 PredMBB = (*I == MBB ? nullptr : *I);
52 }
53
54 assert ((PredMBB == nullptr || !Loop || Loop->contains(PredMBB))
55 && "Loop MBB should not consider predecessor outside of loop.");
56
57 return PredMBB;
58}
59
60void SystemZPostRASchedStrategy::
61advanceTo(MachineBasicBlock::iterator NextBegin) {
62 MachineBasicBlock::iterator LastEmittedMI = HazardRec->getLastEmittedMI();
63 MachineBasicBlock::iterator I =
64 ((LastEmittedMI != nullptr && LastEmittedMI->getParent() == MBB) ?
65 std::next(LastEmittedMI) : MBB->begin());
66
67 for (; I != NextBegin; ++I) {
Shiva Chen801bf7e2018-05-09 02:42:00 +000068 if (I->isPosition() || I->isDebugInstr())
Jonas Paulsson57a705d2017-08-17 08:33:44 +000069 continue;
70 HazardRec->emitInstruction(&*I);
71 }
72}
73
Jonas Paulsson61fbcf52018-03-07 08:39:00 +000074void SystemZPostRASchedStrategy::initialize(ScheduleDAGMI *dag) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000075 LLVM_DEBUG(HazardRec->dumpState(););
Jonas Paulsson61fbcf52018-03-07 08:39:00 +000076}
77
Jonas Paulsson57a705d2017-08-17 08:33:44 +000078void SystemZPostRASchedStrategy::enterMBB(MachineBasicBlock *NextMBB) {
79 assert ((SchedStates.find(NextMBB) == SchedStates.end()) &&
80 "Entering MBB twice?");
Nicola Zaghend34e60c2018-05-14 12:53:11 +000081 LLVM_DEBUG(dbgs() << "** Entering " << printMBBReference(*NextMBB));
Jonas Paulsson57a705d2017-08-17 08:33:44 +000082
83 MBB = NextMBB;
Jonas Paulsson61fbcf52018-03-07 08:39:00 +000084
Jonas Paulsson57a705d2017-08-17 08:33:44 +000085 /// Create a HazardRec for MBB, save it in SchedStates and set HazardRec to
86 /// point to it.
87 HazardRec = SchedStates[MBB] = new SystemZHazardRecognizer(TII, &SchedModel);
Nicola Zaghend34e60c2018-05-14 12:53:11 +000088 LLVM_DEBUG(const MachineLoop *Loop = MLI->getLoopFor(MBB);
89 if (Loop && Loop->getHeader() == MBB) dbgs() << " (Loop header)";
90 dbgs() << ":\n";);
Jonas Paulsson57a705d2017-08-17 08:33:44 +000091
92 // Try to take over the state from a single predecessor, if it has been
93 // scheduled. If this is not possible, we are done.
94 MachineBasicBlock *SinglePredMBB =
95 getSingleSchedPred(MBB, MLI->getLoopFor(MBB));
96 if (SinglePredMBB == nullptr ||
97 SchedStates.find(SinglePredMBB) == SchedStates.end())
98 return;
99
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000100 LLVM_DEBUG(dbgs() << "** Continued scheduling from "
101 << printMBBReference(*SinglePredMBB) << "\n";);
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000102
103 HazardRec->copyState(SchedStates[SinglePredMBB]);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000104 LLVM_DEBUG(HazardRec->dumpState(););
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000105
106 // Emit incoming terminator(s). Be optimistic and assume that branch
107 // prediction will generally do "the right thing".
108 for (MachineBasicBlock::iterator I = SinglePredMBB->getFirstTerminator();
109 I != SinglePredMBB->end(); I++) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000110 LLVM_DEBUG(dbgs() << "** Emitting incoming branch: "; I->dump(););
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000111 bool TakenBranch = (I->isBranch() &&
112 (TII->getBranchInfo(*I).Target->isReg() || // Relative branch
113 TII->getBranchInfo(*I).Target->getMBB() == MBB));
114 HazardRec->emitInstruction(&*I, TakenBranch);
115 if (TakenBranch)
116 break;
117 }
118}
119
120void SystemZPostRASchedStrategy::leaveMBB() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000121 LLVM_DEBUG(dbgs() << "** Leaving " << printMBBReference(*MBB) << "\n";);
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000122
123 // Advance to first terminator. The successor block will handle terminators
124 // dependent on CFG layout (T/NT branch etc).
125 advanceTo(MBB->getFirstTerminator());
126}
127
Jonas Paulsson8010b632016-10-20 08:27:16 +0000128SystemZPostRASchedStrategy::
129SystemZPostRASchedStrategy(const MachineSchedContext *C)
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000130 : MLI(C->MLI),
131 TII(static_cast<const SystemZInstrInfo *>
132 (C->MF->getSubtarget().getInstrInfo())),
133 MBB(nullptr), HazardRec(nullptr) {
134 const TargetSubtargetInfo *ST = &C->MF->getSubtarget();
Sanjay Patel0d7df362018-04-08 19:56:04 +0000135 SchedModel.init(ST);
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000136}
Jonas Paulsson8010b632016-10-20 08:27:16 +0000137
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000138SystemZPostRASchedStrategy::~SystemZPostRASchedStrategy() {
139 // Delete hazard recognizers kept around for each MBB.
140 for (auto I : SchedStates) {
141 SystemZHazardRecognizer *hazrec = I.second;
142 delete hazrec;
143 }
144}
145
146void SystemZPostRASchedStrategy::initPolicy(MachineBasicBlock::iterator Begin,
147 MachineBasicBlock::iterator End,
148 unsigned NumRegionInstrs) {
149 // Don't emit the terminators.
150 if (Begin->isTerminator())
151 return;
152
153 // Emit any instructions before start of region.
154 advanceTo(Begin);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000155}
156
157// Pick the next node to schedule.
158SUnit *SystemZPostRASchedStrategy::pickNode(bool &IsTopNode) {
159 // Only scheduling top-down.
160 IsTopNode = true;
161
162 if (Available.empty())
163 return nullptr;
164
165 // If only one choice, return it.
166 if (Available.size() == 1) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000167 LLVM_DEBUG(dbgs() << "** Only one: ";
168 HazardRec->dumpSU(*Available.begin(), dbgs()); dbgs() << "\n";);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000169 return *Available.begin();
170 }
171
172 // All nodes that are possible to schedule are stored by in the
173 // Available set.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000174 LLVM_DEBUG(dbgs() << "** Available: "; Available.dump(*HazardRec););
Jonas Paulsson8010b632016-10-20 08:27:16 +0000175
176 Candidate Best;
177 for (auto *SU : Available) {
178
179 // SU is the next candidate to be compared against current Best.
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000180 Candidate c(SU, *HazardRec);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000181
182 // Remeber which SU is the best candidate.
183 if (Best.SU == nullptr || c < Best) {
184 Best = c;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000185 LLVM_DEBUG(dbgs() << "** Best so far: ";);
Jonas Paulsson61fbcf52018-03-07 08:39:00 +0000186 } else
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000187 LLVM_DEBUG(dbgs() << "** Tried : ";);
188 LLVM_DEBUG(HazardRec->dumpSU(c.SU, dbgs()); c.dumpCosts();
189 dbgs() << " Height:" << c.SU->getHeight(); dbgs() << "\n";);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000190
191 // Once we know we have seen all SUs that affect grouping or use unbuffered
192 // resources, we can stop iterating if Best looks good.
193 if (!SU->isScheduleHigh && Best.noCost())
194 break;
195 }
196
197 assert (Best.SU != nullptr);
198 return Best.SU;
199}
200
201SystemZPostRASchedStrategy::Candidate::
202Candidate(SUnit *SU_, SystemZHazardRecognizer &HazardRec) : Candidate() {
203 SU = SU_;
204
205 // Check the grouping cost. For a node that must begin / end a
206 // group, it is positive if it would do so prematurely, or negative
207 // if it would fit naturally into the schedule.
208 GroupingCost = HazardRec.groupingCost(SU);
209
Jonas Paulsson61fbcf52018-03-07 08:39:00 +0000210 // Check the resources cost for this SU.
Jonas Paulsson8010b632016-10-20 08:27:16 +0000211 ResourcesCost = HazardRec.resourcesCost(SU);
212}
213
214bool SystemZPostRASchedStrategy::Candidate::
215operator<(const Candidate &other) {
216
217 // Check decoder grouping.
218 if (GroupingCost < other.GroupingCost)
219 return true;
220 if (GroupingCost > other.GroupingCost)
221 return false;
222
223 // Compare the use of resources.
224 if (ResourcesCost < other.ResourcesCost)
225 return true;
226 if (ResourcesCost > other.ResourcesCost)
227 return false;
228
229 // Higher SU is otherwise generally better.
230 if (SU->getHeight() > other.SU->getHeight())
231 return true;
232 if (SU->getHeight() < other.SU->getHeight())
233 return false;
234
235 // If all same, fall back to original order.
236 if (SU->NodeNum < other.SU->NodeNum)
237 return true;
238
239 return false;
240}
241
242void SystemZPostRASchedStrategy::schedNode(SUnit *SU, bool IsTopNode) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000243 LLVM_DEBUG(dbgs() << "** Scheduling SU(" << SU->NodeNum << ") ";
244 if (Available.size() == 1) dbgs() << "(only one) ";
245 Candidate c(SU, *HazardRec); c.dumpCosts(); dbgs() << "\n";);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000246
247 // Remove SU from Available set and update HazardRec.
248 Available.erase(SU);
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000249 HazardRec->EmitInstruction(SU);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000250}
251
252void SystemZPostRASchedStrategy::releaseTopNode(SUnit *SU) {
253 // Set isScheduleHigh flag on all SUs that we want to consider first in
254 // pickNode().
Jonas Paulsson57a705d2017-08-17 08:33:44 +0000255 const MCSchedClassDesc *SC = HazardRec->getSchedClass(SU);
Jonas Paulsson8010b632016-10-20 08:27:16 +0000256 bool AffectsGrouping = (SC->isValid() && (SC->BeginGroup || SC->EndGroup));
257 SU->isScheduleHigh = (AffectsGrouping || SU->isUnbuffered);
258
259 // Put all released SUs in the Available set.
260 Available.insert(SU);
261}