blob: d1f001350976421c923ff8619b6dbde11e827390 [file] [log] [blame]
Sergei Larin4d8986a2012-09-04 14:49:56 +00001//===- HexagonMachineScheduler.cpp - MI Scheduler for Hexagon -------------===//
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// MachineScheduler schedules machine instructions after phi elimination. It
11// preserves LiveIntervals so it can be invoked before register allocation.
12//
13//===----------------------------------------------------------------------===//
14
Sergei Larin4d8986a2012-09-04 14:49:56 +000015#include "HexagonMachineScheduler.h"
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +000016#include "HexagonSubtarget.h"
Jakub Staszakdf17ddd2013-03-10 13:11:23 +000017#include "llvm/CodeGen/MachineLoopInfo.h"
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +000018#include "llvm/CodeGen/ScheduleDAGMutation.h"
Jakub Staszakdf17ddd2013-03-10 13:11:23 +000019#include "llvm/IR/Function.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000020
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000021static cl::opt<bool> SchedPredsCloser("sched-preds-closer",
22 cl::Hidden, cl::ZeroOrMore, cl::init(true));
23
24static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization",
25 cl::Hidden, cl::ZeroOrMore, cl::init(true));
26
Sergei Larin4d8986a2012-09-04 14:49:56 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "misched"
30
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000031class HexagonCallMutation : public ScheduleDAGMutation {
32public:
33 void apply(ScheduleDAGInstrs *DAG) override;
34private:
35 bool shouldTFRICallBind(const HexagonInstrInfo &HII,
36 const SUnit &Inst1, const SUnit &Inst2) const;
37};
38
39// Check if a call and subsequent A2_tfrpi instructions should maintain
40// scheduling affinity. We are looking for the TFRI to be consumed in
41// the next instruction. This should help reduce the instances of
42// double register pairs being allocated and scheduled before a call
43// when not used until after the call. This situation is exacerbated
44// by the fact that we allocate the pair from the callee saves list,
45// leading to excess spills and restores.
46bool HexagonCallMutation::shouldTFRICallBind(const HexagonInstrInfo &HII,
47 const SUnit &Inst1, const SUnit &Inst2) const {
48 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi)
49 return false;
50
51 // TypeXTYPE are 64 bit operations.
52 if (HII.getType(Inst2.getInstr()) == HexagonII::TypeXTYPE)
53 return true;
54 return false;
55}
56
57void HexagonCallMutation::apply(ScheduleDAGInstrs *DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +000058 SUnit* LastSequentialCall = nullptr;
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000059 unsigned VRegHoldingRet = 0;
60 unsigned RetRegister;
61 SUnit* LastUseOfRet = nullptr;
62 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo();
63 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
64
Sergei Larin2db64a72012-09-14 15:07:59 +000065 // Currently we only catch the situation when compare gets scheduled
66 // before preceding call.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000067 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) {
Sergei Larin2db64a72012-09-14 15:07:59 +000068 // Remember the call.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000069 if (DAG->SUnits[su].getInstr()->isCall())
70 LastSequentialCall = &DAG->SUnits[su];
Sergei Larin2db64a72012-09-14 15:07:59 +000071 // Look for a compare that defines a predicate.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000072 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall)
73 DAG->SUnits[su].addPred(SDep(LastSequentialCall, SDep::Barrier));
74 // Look for call and tfri* instructions.
75 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 &&
76 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1]))
77 DAG->SUnits[su].addPred(SDep(&DAG->SUnits[su-1], SDep::Barrier));
78 // Prevent redundant register copies between two calls, which are caused by
79 // both the return value and the argument for the next call being in %R0.
80 // Example:
81 // 1: <call1>
82 // 2: %VregX = COPY %R0
83 // 3: <use of %VregX>
84 // 4: %R0 = ...
85 // 5: <call2>
86 // The scheduler would often swap 3 and 4, so an additional register is
87 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent
88 // this. The same applies for %D0 and %V0/%W0, which are also handled.
89 else if (SchedRetvalOptimization) {
90 const MachineInstr *MI = DAG->SUnits[su].getInstr();
91 if (MI->isCopy() && (MI->readsRegister(Hexagon::R0, &TRI) ||
92 MI->readsRegister(Hexagon::V0, &TRI))) {
93 // %vregX = COPY %R0
94 VRegHoldingRet = MI->getOperand(0).getReg();
95 RetRegister = MI->getOperand(1).getReg();
96 LastUseOfRet = nullptr;
97 } else if (VRegHoldingRet && MI->readsVirtualRegister(VRegHoldingRet))
98 // <use of %vregX>
99 LastUseOfRet = &DAG->SUnits[su];
100 else if (LastUseOfRet && MI->definesRegister(RetRegister, &TRI))
101 // %R0 = ...
102 DAG->SUnits[su].addPred(SDep(LastUseOfRet, SDep::Barrier));
103 }
Sergei Larin2db64a72012-09-14 15:07:59 +0000104 }
105}
106
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +0000107
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000108/// Save the last formed packet
109void VLIWResourceModel::savePacket() {
110 OldPacket = Packet;
111}
112
Sergei Larin4d8986a2012-09-04 14:49:56 +0000113/// Check if scheduling of this SU is possible
114/// in the current packet.
115/// It is _not_ precise (statefull), it is more like
116/// another heuristic. Many corner cases are figured
117/// empirically.
118bool VLIWResourceModel::isResourceAvailable(SUnit *SU) {
119 if (!SU || !SU->getInstr())
120 return false;
121
122 // First see if the pipeline could receive this instruction
123 // in the current cycle.
124 switch (SU->getInstr()->getOpcode()) {
125 default:
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000126 if (!ResourcesModel->canReserveResources(*SU->getInstr()))
Sergei Larin4d8986a2012-09-04 14:49:56 +0000127 return false;
128 case TargetOpcode::EXTRACT_SUBREG:
129 case TargetOpcode::INSERT_SUBREG:
130 case TargetOpcode::SUBREG_TO_REG:
131 case TargetOpcode::REG_SEQUENCE:
132 case TargetOpcode::IMPLICIT_DEF:
133 case TargetOpcode::COPY:
134 case TargetOpcode::INLINEASM:
135 break;
136 }
137
138 // Now see if there are no other dependencies to instructions already
139 // in the packet.
140 for (unsigned i = 0, e = Packet.size(); i != e; ++i) {
141 if (Packet[i]->Succs.size() == 0)
142 continue;
143 for (SUnit::const_succ_iterator I = Packet[i]->Succs.begin(),
144 E = Packet[i]->Succs.end(); I != E; ++I) {
145 // Since we do not add pseudos to packets, might as well
146 // ignore order dependencies.
147 if (I->isCtrl())
148 continue;
149
150 if (I->getSUnit() == SU)
151 return false;
152 }
153 }
154 return true;
155}
156
157/// Keep track of available resources.
Sergei Larinef4cc112012-09-10 17:31:34 +0000158bool VLIWResourceModel::reserveResources(SUnit *SU) {
159 bool startNewCycle = false;
Sergei Larin2db64a72012-09-14 15:07:59 +0000160 // Artificially reset state.
161 if (!SU) {
162 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000163 savePacket();
Sergei Larin2db64a72012-09-14 15:07:59 +0000164 Packet.clear();
165 TotalPackets++;
166 return false;
167 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000168 // If this SU does not fit in the packet
169 // start a new one.
170 if (!isResourceAvailable(SU)) {
171 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000172 savePacket();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000173 Packet.clear();
174 TotalPackets++;
Sergei Larinef4cc112012-09-10 17:31:34 +0000175 startNewCycle = true;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000176 }
177
178 switch (SU->getInstr()->getOpcode()) {
179 default:
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000180 ResourcesModel->reserveResources(*SU->getInstr());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000181 break;
182 case TargetOpcode::EXTRACT_SUBREG:
183 case TargetOpcode::INSERT_SUBREG:
184 case TargetOpcode::SUBREG_TO_REG:
185 case TargetOpcode::REG_SEQUENCE:
186 case TargetOpcode::IMPLICIT_DEF:
187 case TargetOpcode::KILL:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000188 case TargetOpcode::CFI_INSTRUCTION:
Sergei Larin4d8986a2012-09-04 14:49:56 +0000189 case TargetOpcode::EH_LABEL:
190 case TargetOpcode::COPY:
191 case TargetOpcode::INLINEASM:
192 break;
193 }
194 Packet.push_back(SU);
195
196#ifndef NDEBUG
197 DEBUG(dbgs() << "Packet[" << TotalPackets << "]:\n");
198 for (unsigned i = 0, e = Packet.size(); i != e; ++i) {
199 DEBUG(dbgs() << "\t[" << i << "] SU(");
Sergei Larinef4cc112012-09-10 17:31:34 +0000200 DEBUG(dbgs() << Packet[i]->NodeNum << ")\t");
201 DEBUG(Packet[i]->getInstr()->dump());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000202 }
203#endif
204
205 // If packet is now full, reset the state so in the next cycle
206 // we start fresh.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000207 if (Packet.size() >= SchedModel->getIssueWidth()) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000208 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000209 savePacket();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000210 Packet.clear();
211 TotalPackets++;
Sergei Larinef4cc112012-09-10 17:31:34 +0000212 startNewCycle = true;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000213 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000214
215 return startNewCycle;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000216}
217
Sergei Larin4d8986a2012-09-04 14:49:56 +0000218/// schedule - Called back from MachineScheduler::runOnMachineFunction
219/// after setting up the current scheduling region. [RegionBegin, RegionEnd)
220/// only includes instructions that have DAG nodes, not scheduling boundaries.
221void VLIWMachineScheduler::schedule() {
222 DEBUG(dbgs()
223 << "********** MI Converging Scheduling VLIW BB#" << BB->getNumber()
224 << " " << BB->getName()
225 << " in_func " << BB->getParent()->getFunction()->getName()
Alexey Samsonov8968e6d2014-08-20 19:36:05 +0000226 << " at loop depth " << MLI->getLoopDepth(BB)
Sergei Larin4d8986a2012-09-04 14:49:56 +0000227 << " \n");
228
Andrew Trick7a8e1002012-09-11 00:39:15 +0000229 buildDAGWithRegPressure();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000230
Andrew Tricke2c3f5c2013-01-25 06:33:57 +0000231 SmallVector<SUnit*, 8> TopRoots, BotRoots;
232 findRootsAndBiasEdges(TopRoots, BotRoots);
233
234 // Initialize the strategy before modifying the DAG.
235 SchedImpl->initialize(this);
236
Sergei Larinef4cc112012-09-10 17:31:34 +0000237 // To view Height/Depth correctly, they should be accessed at least once.
Andrew Trick63474622013-03-02 01:43:08 +0000238 //
239 // FIXME: SUnit::dumpAll always recompute depth and height now. The max
240 // depth/height could be computed directly from the roots and leaves.
Sergei Larinef4cc112012-09-10 17:31:34 +0000241 DEBUG(unsigned maxH = 0;
242 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
243 if (SUnits[su].getHeight() > maxH)
244 maxH = SUnits[su].getHeight();
245 dbgs() << "Max Height " << maxH << "\n";);
246 DEBUG(unsigned maxD = 0;
247 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
248 if (SUnits[su].getDepth() > maxD)
249 maxD = SUnits[su].getDepth();
250 dbgs() << "Max Depth " << maxD << "\n";);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000251 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
252 SUnits[su].dumpAll(this));
253
Andrew Tricke2c3f5c2013-01-25 06:33:57 +0000254 initQueues(TopRoots, BotRoots);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000255
Sergei Larin4d8986a2012-09-04 14:49:56 +0000256 bool IsTopNode = false;
James Y Knighte72b0db2015-09-18 18:52:20 +0000257 while (true) {
258 DEBUG(dbgs() << "** VLIWMachineScheduler::schedule picking next node\n");
259 SUnit *SU = SchedImpl->pickNode(IsTopNode);
260 if (!SU) break;
261
Sergei Larin4d8986a2012-09-04 14:49:56 +0000262 if (!checkSchedLimit())
263 break;
264
Andrew Trick7a8e1002012-09-11 00:39:15 +0000265 scheduleMI(SU, IsTopNode);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000266
Andrew Trick7a8e1002012-09-11 00:39:15 +0000267 updateQueues(SU, IsTopNode);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000268
269 // Notify the scheduling strategy after updating the DAG.
270 SchedImpl->schedNode(SU, IsTopNode);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000271 }
272 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
273
Sergei Larin4d8986a2012-09-04 14:49:56 +0000274 placeDebugValues();
275}
276
Andrew Trick7a8e1002012-09-11 00:39:15 +0000277void ConvergingVLIWScheduler::initialize(ScheduleDAGMI *dag) {
278 DAG = static_cast<VLIWMachineScheduler*>(dag);
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000279 SchedModel = DAG->getSchedModel();
Andrew Trick553e0fe2013-02-13 19:22:27 +0000280
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000281 Top.init(DAG, SchedModel);
282 Bot.init(DAG, SchedModel);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000283
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000284 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or
285 // are disabled, then these HazardRecs will be disabled.
286 const InstrItineraryData *Itin = DAG->getSchedModel()->getInstrItineraries();
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000287 const TargetSubtargetInfo &STI = DAG->MF.getSubtarget();
288 const TargetInstrInfo *TII = STI.getInstrInfo();
Andrew Trick553e0fe2013-02-13 19:22:27 +0000289 delete Top.HazardRec;
290 delete Bot.HazardRec;
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000291 Top.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG);
292 Bot.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000293
Chandler Carruthc18e39c2013-07-27 10:48:45 +0000294 delete Top.ResourceModel;
295 delete Bot.ResourceModel;
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000296 Top.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel());
297 Bot.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel());
Sergei Larinef4cc112012-09-10 17:31:34 +0000298
Andrew Trick7a8e1002012-09-11 00:39:15 +0000299 assert((!llvm::ForceTopDown || !llvm::ForceBottomUp) &&
Sergei Larin4d8986a2012-09-04 14:49:56 +0000300 "-misched-topdown incompatible with -misched-bottomup");
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000301
302 DAG->addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +0000303 DAG->addMutation(make_unique<HexagonCallMutation>());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000304}
305
306void ConvergingVLIWScheduler::releaseTopNode(SUnit *SU) {
307 if (SU->isScheduled)
308 return;
309
310 for (SUnit::succ_iterator I = SU->Preds.begin(), E = SU->Preds.end();
311 I != E; ++I) {
312 unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle;
Andrew Trickde2109e2013-06-15 04:49:57 +0000313 unsigned MinLatency = I->getLatency();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000314#ifndef NDEBUG
315 Top.MaxMinLatency = std::max(MinLatency, Top.MaxMinLatency);
316#endif
317 if (SU->TopReadyCycle < PredReadyCycle + MinLatency)
318 SU->TopReadyCycle = PredReadyCycle + MinLatency;
319 }
320 Top.releaseNode(SU, SU->TopReadyCycle);
321}
322
323void ConvergingVLIWScheduler::releaseBottomNode(SUnit *SU) {
324 if (SU->isScheduled)
325 return;
326
327 assert(SU->getInstr() && "Scheduled SUnit must have instr");
328
329 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
330 I != E; ++I) {
331 unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle;
Andrew Trickde2109e2013-06-15 04:49:57 +0000332 unsigned MinLatency = I->getLatency();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000333#ifndef NDEBUG
334 Bot.MaxMinLatency = std::max(MinLatency, Bot.MaxMinLatency);
335#endif
336 if (SU->BotReadyCycle < SuccReadyCycle + MinLatency)
337 SU->BotReadyCycle = SuccReadyCycle + MinLatency;
338 }
339 Bot.releaseNode(SU, SU->BotReadyCycle);
340}
341
342/// Does this SU have a hazard within the current instruction group.
343///
344/// The scheduler supports two modes of hazard recognition. The first is the
345/// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that
346/// supports highly complicated in-order reservation tables
347/// (ScoreboardHazardRecognizer) and arbitrary target-specific logic.
348///
349/// The second is a streamlined mechanism that checks for hazards based on
350/// simple counters that the scheduler itself maintains. It explicitly checks
351/// for instruction dispatch limitations, including the number of micro-ops that
352/// can dispatch per cycle.
353///
354/// TODO: Also check whether the SU must start a new group.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000355bool ConvergingVLIWScheduler::VLIWSchedBoundary::checkHazard(SUnit *SU) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000356 if (HazardRec->isEnabled())
357 return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard;
358
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000359 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr());
360 if (IssueCount + uops > SchedModel->getIssueWidth())
Sergei Larin4d8986a2012-09-04 14:49:56 +0000361 return true;
362
363 return false;
364}
365
Andrew Trickd7f890e2013-12-28 21:56:47 +0000366void ConvergingVLIWScheduler::VLIWSchedBoundary::releaseNode(SUnit *SU,
Sergei Larin4d8986a2012-09-04 14:49:56 +0000367 unsigned ReadyCycle) {
368 if (ReadyCycle < MinReadyCycle)
369 MinReadyCycle = ReadyCycle;
370
371 // Check for interlocks first. For the purpose of other heuristics, an
372 // instruction that cannot issue appears as if it's not in the ReadyQueue.
373 if (ReadyCycle > CurrCycle || checkHazard(SU))
374
375 Pending.push(SU);
376 else
377 Available.push(SU);
378}
379
380/// Move the boundary of scheduled code by one cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000381void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpCycle() {
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000382 unsigned Width = SchedModel->getIssueWidth();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000383 IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width;
384
385 assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized");
386 unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle);
387
388 if (!HazardRec->isEnabled()) {
389 // Bypass HazardRec virtual calls.
390 CurrCycle = NextCycle;
Sergei Larinef4cc112012-09-10 17:31:34 +0000391 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000392 // Bypass getHazardType calls in case of long latency.
393 for (; CurrCycle != NextCycle; ++CurrCycle) {
394 if (isTop())
395 HazardRec->AdvanceCycle();
396 else
397 HazardRec->RecedeCycle();
398 }
399 }
400 CheckPending = true;
401
402 DEBUG(dbgs() << "*** " << Available.getName() << " cycle "
403 << CurrCycle << '\n');
404}
405
406/// Move the boundary of scheduled code by one SUnit.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000407void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpNode(SUnit *SU) {
Sergei Larinef4cc112012-09-10 17:31:34 +0000408 bool startNewCycle = false;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000409
410 // Update the reservation table.
411 if (HazardRec->isEnabled()) {
412 if (!isTop() && SU->isCall) {
413 // Calls are scheduled with their preceding instructions. For bottom-up
414 // scheduling, clear the pipeline state before emitting.
415 HazardRec->Reset();
416 }
417 HazardRec->EmitInstruction(SU);
418 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000419
420 // Update DFA model.
421 startNewCycle = ResourceModel->reserveResources(SU);
422
Sergei Larin4d8986a2012-09-04 14:49:56 +0000423 // Check the instruction group dispatch limit.
424 // TODO: Check if this SU must end a dispatch group.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000425 IssueCount += SchedModel->getNumMicroOps(SU->getInstr());
Sergei Larinef4cc112012-09-10 17:31:34 +0000426 if (startNewCycle) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000427 DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n');
428 bumpCycle();
429 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000430 else
431 DEBUG(dbgs() << "*** IssueCount " << IssueCount
432 << " at cycle " << CurrCycle << '\n');
Sergei Larin4d8986a2012-09-04 14:49:56 +0000433}
434
435/// Release pending ready nodes in to the available queue. This makes them
436/// visible to heuristics.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000437void ConvergingVLIWScheduler::VLIWSchedBoundary::releasePending() {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000438 // If the available queue is empty, it is safe to reset MinReadyCycle.
439 if (Available.empty())
440 MinReadyCycle = UINT_MAX;
441
442 // Check to see if any of the pending instructions are ready to issue. If
443 // so, add them to the available queue.
444 for (unsigned i = 0, e = Pending.size(); i != e; ++i) {
445 SUnit *SU = *(Pending.begin()+i);
446 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle;
447
448 if (ReadyCycle < MinReadyCycle)
449 MinReadyCycle = ReadyCycle;
450
451 if (ReadyCycle > CurrCycle)
452 continue;
453
454 if (checkHazard(SU))
455 continue;
456
457 Available.push(SU);
458 Pending.remove(Pending.begin()+i);
459 --i; --e;
460 }
461 CheckPending = false;
462}
463
464/// Remove SU from the ready set for this boundary.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000465void ConvergingVLIWScheduler::VLIWSchedBoundary::removeReady(SUnit *SU) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000466 if (Available.isInQueue(SU))
467 Available.remove(Available.find(SU));
468 else {
469 assert(Pending.isInQueue(SU) && "bad ready count");
470 Pending.remove(Pending.find(SU));
471 }
472}
473
474/// If this queue only has one ready candidate, return it. As a side effect,
475/// advance the cycle until at least one node is ready. If multiple instructions
476/// are ready, return NULL.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000477SUnit *ConvergingVLIWScheduler::VLIWSchedBoundary::pickOnlyChoice() {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000478 if (CheckPending)
479 releasePending();
480
481 for (unsigned i = 0; Available.empty(); ++i) {
482 assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) &&
483 "permanent hazard"); (void)i;
Craig Topper062a2ba2014-04-25 05:30:21 +0000484 ResourceModel->reserveResources(nullptr);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000485 bumpCycle();
486 releasePending();
487 }
488 if (Available.size() == 1)
489 return *Available.begin();
Craig Topper062a2ba2014-04-25 05:30:21 +0000490 return nullptr;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000491}
492
493#ifndef NDEBUG
Sergei Larinef4cc112012-09-10 17:31:34 +0000494void ConvergingVLIWScheduler::traceCandidate(const char *Label,
495 const ReadyQueue &Q,
Andrew Trick1a831342013-08-30 03:49:48 +0000496 SUnit *SU, PressureChange P) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000497 dbgs() << Label << " " << Q.getName() << " ";
498 if (P.isValid())
Andrew Trick1a831342013-08-30 03:49:48 +0000499 dbgs() << DAG->TRI->getRegPressureSetName(P.getPSet()) << ":"
500 << P.getUnitInc() << " ";
Sergei Larin4d8986a2012-09-04 14:49:56 +0000501 else
502 dbgs() << " ";
503 SU->dump(DAG);
504}
505#endif
506
Sergei Larinef4cc112012-09-10 17:31:34 +0000507/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
508/// of SU, return it, otherwise return null.
509static SUnit *getSingleUnscheduledPred(SUnit *SU) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000510 SUnit *OnlyAvailablePred = nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000511 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
512 I != E; ++I) {
513 SUnit &Pred = *I->getSUnit();
514 if (!Pred.isScheduled) {
515 // We found an available, but not scheduled, predecessor. If it's the
516 // only one we have found, keep track of it... otherwise give up.
517 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Craig Topper062a2ba2014-04-25 05:30:21 +0000518 return nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000519 OnlyAvailablePred = &Pred;
520 }
521 }
522 return OnlyAvailablePred;
523}
524
525/// getSingleUnscheduledSucc - If there is exactly one unscheduled successor
526/// of SU, return it, otherwise return null.
527static SUnit *getSingleUnscheduledSucc(SUnit *SU) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000528 SUnit *OnlyAvailableSucc = nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000529 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
530 I != E; ++I) {
531 SUnit &Succ = *I->getSUnit();
532 if (!Succ.isScheduled) {
533 // We found an available, but not scheduled, successor. If it's the
534 // only one we have found, keep track of it... otherwise give up.
535 if (OnlyAvailableSucc && OnlyAvailableSucc != &Succ)
Craig Topper062a2ba2014-04-25 05:30:21 +0000536 return nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000537 OnlyAvailableSucc = &Succ;
538 }
539 }
540 return OnlyAvailableSucc;
541}
542
Sergei Larin4d8986a2012-09-04 14:49:56 +0000543// Constants used to denote relative importance of
544// heuristic components for cost computation.
545static const unsigned PriorityOne = 200;
Eli Friedman8f06d552013-09-11 00:41:02 +0000546static const unsigned PriorityTwo = 50;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000547static const unsigned ScaleTwo = 10;
548static const unsigned FactorOne = 2;
549
550/// Single point to compute overall scheduling cost.
551/// TODO: More heuristics will be used soon.
552int ConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU,
553 SchedCandidate &Candidate,
554 RegPressureDelta &Delta,
555 bool verbose) {
556 // Initial trivial priority.
557 int ResCount = 1;
558
559 // Do not waste time on a node that is already scheduled.
560 if (!SU || SU->isScheduled)
561 return ResCount;
562
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000563 MachineInstr *Instr = SU->getInstr();
564
Sergei Larin4d8986a2012-09-04 14:49:56 +0000565 // Forced priority is high.
566 if (SU->isScheduleHigh)
567 ResCount += PriorityOne;
568
569 // Critical path first.
Sergei Larinef4cc112012-09-10 17:31:34 +0000570 if (Q.getID() == TopQID) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000571 ResCount += (SU->getHeight() * ScaleTwo);
Sergei Larinef4cc112012-09-10 17:31:34 +0000572
573 // If resources are available for it, multiply the
574 // chance of scheduling.
575 if (Top.ResourceModel->isResourceAvailable(SU))
576 ResCount <<= FactorOne;
577 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000578 ResCount += (SU->getDepth() * ScaleTwo);
579
Sergei Larinef4cc112012-09-10 17:31:34 +0000580 // If resources are available for it, multiply the
581 // chance of scheduling.
582 if (Bot.ResourceModel->isResourceAvailable(SU))
583 ResCount <<= FactorOne;
584 }
585
586 unsigned NumNodesBlocking = 0;
587 if (Q.getID() == TopQID) {
588 // How many SUs does it block from scheduling?
589 // Look at all of the successors of this node.
590 // Count the number of nodes that
591 // this node is the sole unscheduled node for.
592 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
593 I != E; ++I)
594 if (getSingleUnscheduledPred(I->getSUnit()) == SU)
595 ++NumNodesBlocking;
596 } else {
597 // How many unscheduled predecessors block this node?
598 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
599 I != E; ++I)
600 if (getSingleUnscheduledSucc(I->getSUnit()) == SU)
601 ++NumNodesBlocking;
602 }
603 ResCount += (NumNodesBlocking * ScaleTwo);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000604
605 // Factor in reg pressure as a heuristic.
Eli Friedman8f06d552013-09-11 00:41:02 +0000606 ResCount -= (Delta.Excess.getUnitInc()*PriorityTwo);
607 ResCount -= (Delta.CriticalMax.getUnitInc()*PriorityTwo);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000608
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000609 auto &QST = DAG->MF.getSubtarget<HexagonSubtarget>();
610 auto &QII = *QST.getInstrInfo();
611
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000612 // Give preference to a zero latency instruction if the dependent
613 // instruction is in the current packet.
614 if (Q.getID() == TopQID) {
615 for (const SDep &PI : SU->Preds) {
616 if (!PI.getSUnit()->getInstr()->isPseudo() && PI.isAssignedRegDep() &&
617 PI.getLatency() == 0 &&
618 Top.ResourceModel->isInPacket(PI.getSUnit())) {
619 ResCount += PriorityTwo;
620 DEBUG(if (verbose) dbgs() << "Z|");
621 }
622 }
623 } else {
624 for (const SDep &SI : SU->Succs) {
625 if (!SI.getSUnit()->getInstr()->isPseudo() && SI.isAssignedRegDep() &&
626 SI.getLatency() == 0 &&
627 Bot.ResourceModel->isInPacket(SI.getSUnit())) {
628 ResCount += PriorityTwo;
629 DEBUG(if (verbose) dbgs() << "Z|");
630 }
631 }
632 }
633
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000634 // Give less preference to an instruction that will cause a stall with
635 // an instruction in the previous packet.
636 if (QII.isV60VectorInstruction(Instr)) {
637 // Check for stalls in the previous packet.
638 if (Q.getID() == TopQID) {
639 for (auto J : Top.ResourceModel->OldPacket)
640 if (QII.producesStall(J->getInstr(), Instr))
641 ResCount -= PriorityOne;
642 } else {
643 for (auto J : Bot.ResourceModel->OldPacket)
644 if (QII.producesStall(Instr, J->getInstr()))
645 ResCount -= PriorityOne;
646 }
647 }
648
Sergei Larin4d8986a2012-09-04 14:49:56 +0000649 DEBUG(if (verbose) dbgs() << " Total(" << ResCount << ")");
650
651 return ResCount;
652}
653
654/// Pick the best candidate from the top queue.
655///
656/// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during
657/// DAG building. To adjust for the current scheduling location we need to
658/// maintain the number of vreg uses remaining to be top-scheduled.
659ConvergingVLIWScheduler::CandResult ConvergingVLIWScheduler::
660pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker,
661 SchedCandidate &Candidate) {
662 DEBUG(Q.dump());
663
664 // getMaxPressureDelta temporarily modifies the tracker.
665 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
666
667 // BestSU remains NULL if no top candidates beat the best existing candidate.
668 CandResult FoundCandidate = NoCand;
669 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
670 RegPressureDelta RPDelta;
671 TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta,
672 DAG->getRegionCriticalPSets(),
673 DAG->getRegPressure().MaxSetPressure);
674
675 int CurrentCost = SchedulingCost(Q, *I, Candidate, RPDelta, false);
676
677 // Initialize the candidate if needed.
678 if (!Candidate.SU) {
679 Candidate.SU = *I;
680 Candidate.RPDelta = RPDelta;
681 Candidate.SCost = CurrentCost;
682 FoundCandidate = NodeOrder;
683 continue;
684 }
685
Sergei Larin4d8986a2012-09-04 14:49:56 +0000686 // Best cost.
687 if (CurrentCost > Candidate.SCost) {
688 DEBUG(traceCandidate("CCAND", Q, *I));
689 Candidate.SU = *I;
690 Candidate.RPDelta = RPDelta;
691 Candidate.SCost = CurrentCost;
692 FoundCandidate = BestCost;
693 continue;
694 }
695
696 // Fall through to original instruction order.
697 // Only consider node order if Candidate was chosen from this Q.
698 if (FoundCandidate == NoCand)
699 continue;
700 }
701 return FoundCandidate;
702}
703
704/// Pick the best candidate node from either the top or bottom queue.
705SUnit *ConvergingVLIWScheduler::pickNodeBidrectional(bool &IsTopNode) {
706 // Schedule as far as possible in the direction of no choice. This is most
707 // efficient, but also provides the best heuristics for CriticalPSets.
708 if (SUnit *SU = Bot.pickOnlyChoice()) {
709 IsTopNode = false;
710 return SU;
711 }
712 if (SUnit *SU = Top.pickOnlyChoice()) {
713 IsTopNode = true;
714 return SU;
715 }
716 SchedCandidate BotCand;
717 // Prefer bottom scheduling when heuristics are silent.
718 CandResult BotResult = pickNodeFromQueue(Bot.Available,
719 DAG->getBotRPTracker(), BotCand);
720 assert(BotResult != NoCand && "failed to find the first candidate");
721
722 // If either Q has a single candidate that provides the least increase in
723 // Excess pressure, we can immediately schedule from that Q.
724 //
725 // RegionCriticalPSets summarizes the pressure within the scheduled region and
726 // affects picking from either Q. If scheduling in one direction must
727 // increase pressure for one of the excess PSets, then schedule in that
728 // direction first to provide more freedom in the other direction.
729 if (BotResult == SingleExcess || BotResult == SingleCritical) {
730 IsTopNode = false;
731 return BotCand.SU;
732 }
733 // Check if the top Q has a better candidate.
734 SchedCandidate TopCand;
735 CandResult TopResult = pickNodeFromQueue(Top.Available,
736 DAG->getTopRPTracker(), TopCand);
737 assert(TopResult != NoCand && "failed to find the first candidate");
738
739 if (TopResult == SingleExcess || TopResult == SingleCritical) {
740 IsTopNode = true;
741 return TopCand.SU;
742 }
743 // If either Q has a single candidate that minimizes pressure above the
744 // original region's pressure pick it.
745 if (BotResult == SingleMax) {
746 IsTopNode = false;
747 return BotCand.SU;
748 }
749 if (TopResult == SingleMax) {
750 IsTopNode = true;
751 return TopCand.SU;
752 }
753 if (TopCand.SCost > BotCand.SCost) {
754 IsTopNode = true;
755 return TopCand.SU;
756 }
757 // Otherwise prefer the bottom candidate in node order.
758 IsTopNode = false;
759 return BotCand.SU;
760}
761
762/// Pick the best node to balance the schedule. Implements MachineSchedStrategy.
763SUnit *ConvergingVLIWScheduler::pickNode(bool &IsTopNode) {
764 if (DAG->top() == DAG->bottom()) {
765 assert(Top.Available.empty() && Top.Pending.empty() &&
766 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
Craig Topper062a2ba2014-04-25 05:30:21 +0000767 return nullptr;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000768 }
769 SUnit *SU;
Andrew Trick7a8e1002012-09-11 00:39:15 +0000770 if (llvm::ForceTopDown) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000771 SU = Top.pickOnlyChoice();
772 if (!SU) {
773 SchedCandidate TopCand;
774 CandResult TopResult =
775 pickNodeFromQueue(Top.Available, DAG->getTopRPTracker(), TopCand);
776 assert(TopResult != NoCand && "failed to find the first candidate");
777 (void)TopResult;
778 SU = TopCand.SU;
779 }
780 IsTopNode = true;
Andrew Trick7a8e1002012-09-11 00:39:15 +0000781 } else if (llvm::ForceBottomUp) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000782 SU = Bot.pickOnlyChoice();
783 if (!SU) {
784 SchedCandidate BotCand;
785 CandResult BotResult =
786 pickNodeFromQueue(Bot.Available, DAG->getBotRPTracker(), BotCand);
787 assert(BotResult != NoCand && "failed to find the first candidate");
788 (void)BotResult;
789 SU = BotCand.SU;
790 }
791 IsTopNode = false;
792 } else {
793 SU = pickNodeBidrectional(IsTopNode);
794 }
795 if (SU->isTopReady())
796 Top.removeReady(SU);
797 if (SU->isBottomReady())
798 Bot.removeReady(SU);
799
800 DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom")
801 << " Scheduling Instruction in cycle "
802 << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << '\n';
803 SU->dump(DAG));
804 return SU;
805}
806
807/// Update the scheduler's state after scheduling a node. This is the same node
Sergei Larinef4cc112012-09-10 17:31:34 +0000808/// that was just returned by pickNode(). However, VLIWMachineScheduler needs
809/// to update it's state based on the current cycle before MachineSchedStrategy
810/// does.
Sergei Larin4d8986a2012-09-04 14:49:56 +0000811void ConvergingVLIWScheduler::schedNode(SUnit *SU, bool IsTopNode) {
812 if (IsTopNode) {
813 SU->TopReadyCycle = Top.CurrCycle;
814 Top.bumpNode(SU);
Sergei Larinef4cc112012-09-10 17:31:34 +0000815 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000816 SU->BotReadyCycle = Bot.CurrCycle;
817 Bot.bumpNode(SU);
818 }
819}