blob: 9368a4337433d9352323d94e74fe012b67879bcc [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 Parzyszekf05dc4d2016-07-18 15:47:25 +000021#include <iomanip>
22#include <sstream>
23
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +000024static cl::opt<bool> IgnoreBBRegPressure("ignore-bb-reg-pressure",
25 cl::Hidden, cl::ZeroOrMore, cl::init(false));
26
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000027static cl::opt<bool> SchedPredsCloser("sched-preds-closer",
28 cl::Hidden, cl::ZeroOrMore, cl::init(true));
29
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +000030static cl::opt<unsigned> SchedDebugVerboseLevel("misched-verbose-level",
31 cl::Hidden, cl::ZeroOrMore, cl::init(1));
32
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +000033static cl::opt<bool> TopUseShorterTie("top-use-shorter-tie",
34 cl::Hidden, cl::ZeroOrMore, cl::init(false));
35
36static cl::opt<bool> BotUseShorterTie("bot-use-shorter-tie",
37 cl::Hidden, cl::ZeroOrMore, cl::init(false));
38
39static cl::opt<bool> DisableTCTie("disable-tc-tie",
40 cl::Hidden, cl::ZeroOrMore, cl::init(false));
41
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000042static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization",
43 cl::Hidden, cl::ZeroOrMore, cl::init(true));
44
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +000045// Check if the scheduler should penalize instructions that are available to
46// early due to a zero-latency dependence.
47static cl::opt<bool> CheckEarlyAvail("check-early-avail", cl::Hidden,
48 cl::ZeroOrMore, cl::init(true));
49
Sergei Larin4d8986a2012-09-04 14:49:56 +000050using namespace llvm;
51
Chandler Carruth84e68b22014-04-22 02:41:26 +000052#define DEBUG_TYPE "misched"
53
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000054class HexagonCallMutation : public ScheduleDAGMutation {
55public:
56 void apply(ScheduleDAGInstrs *DAG) override;
57private:
58 bool shouldTFRICallBind(const HexagonInstrInfo &HII,
59 const SUnit &Inst1, const SUnit &Inst2) const;
60};
61
62// Check if a call and subsequent A2_tfrpi instructions should maintain
63// scheduling affinity. We are looking for the TFRI to be consumed in
64// the next instruction. This should help reduce the instances of
65// double register pairs being allocated and scheduled before a call
66// when not used until after the call. This situation is exacerbated
67// by the fact that we allocate the pair from the callee saves list,
68// leading to excess spills and restores.
69bool HexagonCallMutation::shouldTFRICallBind(const HexagonInstrInfo &HII,
70 const SUnit &Inst1, const SUnit &Inst2) const {
71 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi)
72 return false;
73
74 // TypeXTYPE are 64 bit operations.
75 if (HII.getType(Inst2.getInstr()) == HexagonII::TypeXTYPE)
76 return true;
77 return false;
78}
79
80void HexagonCallMutation::apply(ScheduleDAGInstrs *DAG) {
Craig Topper062a2ba2014-04-25 05:30:21 +000081 SUnit* LastSequentialCall = nullptr;
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000082 unsigned VRegHoldingRet = 0;
83 unsigned RetRegister;
84 SUnit* LastUseOfRet = nullptr;
85 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo();
86 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
87
Sergei Larin2db64a72012-09-14 15:07:59 +000088 // Currently we only catch the situation when compare gets scheduled
89 // before preceding call.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000090 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) {
Sergei Larin2db64a72012-09-14 15:07:59 +000091 // Remember the call.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000092 if (DAG->SUnits[su].getInstr()->isCall())
93 LastSequentialCall = &DAG->SUnits[su];
Sergei Larin2db64a72012-09-14 15:07:59 +000094 // Look for a compare that defines a predicate.
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +000095 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall)
96 DAG->SUnits[su].addPred(SDep(LastSequentialCall, SDep::Barrier));
97 // Look for call and tfri* instructions.
98 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 &&
99 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1]))
100 DAG->SUnits[su].addPred(SDep(&DAG->SUnits[su-1], SDep::Barrier));
101 // Prevent redundant register copies between two calls, which are caused by
102 // both the return value and the argument for the next call being in %R0.
103 // Example:
104 // 1: <call1>
105 // 2: %VregX = COPY %R0
106 // 3: <use of %VregX>
107 // 4: %R0 = ...
108 // 5: <call2>
109 // The scheduler would often swap 3 and 4, so an additional register is
110 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent
111 // this. The same applies for %D0 and %V0/%W0, which are also handled.
112 else if (SchedRetvalOptimization) {
113 const MachineInstr *MI = DAG->SUnits[su].getInstr();
114 if (MI->isCopy() && (MI->readsRegister(Hexagon::R0, &TRI) ||
115 MI->readsRegister(Hexagon::V0, &TRI))) {
116 // %vregX = COPY %R0
117 VRegHoldingRet = MI->getOperand(0).getReg();
118 RetRegister = MI->getOperand(1).getReg();
119 LastUseOfRet = nullptr;
120 } else if (VRegHoldingRet && MI->readsVirtualRegister(VRegHoldingRet))
121 // <use of %vregX>
122 LastUseOfRet = &DAG->SUnits[su];
123 else if (LastUseOfRet && MI->definesRegister(RetRegister, &TRI))
124 // %R0 = ...
125 DAG->SUnits[su].addPred(SDep(LastUseOfRet, SDep::Barrier));
126 }
Sergei Larin2db64a72012-09-14 15:07:59 +0000127 }
128}
129
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +0000130
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000131/// Save the last formed packet
132void VLIWResourceModel::savePacket() {
133 OldPacket = Packet;
134}
135
Sergei Larin4d8986a2012-09-04 14:49:56 +0000136/// Check if scheduling of this SU is possible
137/// in the current packet.
138/// It is _not_ precise (statefull), it is more like
139/// another heuristic. Many corner cases are figured
140/// empirically.
141bool VLIWResourceModel::isResourceAvailable(SUnit *SU) {
142 if (!SU || !SU->getInstr())
143 return false;
144
145 // First see if the pipeline could receive this instruction
146 // in the current cycle.
147 switch (SU->getInstr()->getOpcode()) {
148 default:
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000149 if (!ResourcesModel->canReserveResources(*SU->getInstr()))
Sergei Larin4d8986a2012-09-04 14:49:56 +0000150 return false;
151 case TargetOpcode::EXTRACT_SUBREG:
152 case TargetOpcode::INSERT_SUBREG:
153 case TargetOpcode::SUBREG_TO_REG:
154 case TargetOpcode::REG_SEQUENCE:
155 case TargetOpcode::IMPLICIT_DEF:
156 case TargetOpcode::COPY:
157 case TargetOpcode::INLINEASM:
158 break;
159 }
160
Krzysztof Parzyszek786333f2016-07-18 16:05:27 +0000161 MachineFunction &MF = *SU->getInstr()->getParent()->getParent();
162 auto &QII = *MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
163
Sergei Larin4d8986a2012-09-04 14:49:56 +0000164 // Now see if there are no other dependencies to instructions already
165 // in the packet.
166 for (unsigned i = 0, e = Packet.size(); i != e; ++i) {
167 if (Packet[i]->Succs.size() == 0)
168 continue;
Krzysztof Parzyszek786333f2016-07-18 16:05:27 +0000169
170 // Enable .cur formation.
171 if (QII.mayBeCurLoad(Packet[i]->getInstr()))
172 continue;
173
Sergei Larin4d8986a2012-09-04 14:49:56 +0000174 for (SUnit::const_succ_iterator I = Packet[i]->Succs.begin(),
175 E = Packet[i]->Succs.end(); I != E; ++I) {
176 // Since we do not add pseudos to packets, might as well
177 // ignore order dependencies.
178 if (I->isCtrl())
179 continue;
180
181 if (I->getSUnit() == SU)
182 return false;
183 }
184 }
185 return true;
186}
187
188/// Keep track of available resources.
Sergei Larinef4cc112012-09-10 17:31:34 +0000189bool VLIWResourceModel::reserveResources(SUnit *SU) {
190 bool startNewCycle = false;
Sergei Larin2db64a72012-09-14 15:07:59 +0000191 // Artificially reset state.
192 if (!SU) {
193 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000194 savePacket();
Sergei Larin2db64a72012-09-14 15:07:59 +0000195 Packet.clear();
196 TotalPackets++;
197 return false;
198 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000199 // If this SU does not fit in the packet
200 // start a new one.
201 if (!isResourceAvailable(SU)) {
202 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000203 savePacket();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000204 Packet.clear();
205 TotalPackets++;
Sergei Larinef4cc112012-09-10 17:31:34 +0000206 startNewCycle = true;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000207 }
208
209 switch (SU->getInstr()->getOpcode()) {
210 default:
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000211 ResourcesModel->reserveResources(*SU->getInstr());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000212 break;
213 case TargetOpcode::EXTRACT_SUBREG:
214 case TargetOpcode::INSERT_SUBREG:
215 case TargetOpcode::SUBREG_TO_REG:
216 case TargetOpcode::REG_SEQUENCE:
217 case TargetOpcode::IMPLICIT_DEF:
218 case TargetOpcode::KILL:
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000219 case TargetOpcode::CFI_INSTRUCTION:
Sergei Larin4d8986a2012-09-04 14:49:56 +0000220 case TargetOpcode::EH_LABEL:
221 case TargetOpcode::COPY:
222 case TargetOpcode::INLINEASM:
223 break;
224 }
225 Packet.push_back(SU);
226
227#ifndef NDEBUG
228 DEBUG(dbgs() << "Packet[" << TotalPackets << "]:\n");
229 for (unsigned i = 0, e = Packet.size(); i != e; ++i) {
230 DEBUG(dbgs() << "\t[" << i << "] SU(");
Sergei Larinef4cc112012-09-10 17:31:34 +0000231 DEBUG(dbgs() << Packet[i]->NodeNum << ")\t");
232 DEBUG(Packet[i]->getInstr()->dump());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000233 }
234#endif
235
236 // If packet is now full, reset the state so in the next cycle
237 // we start fresh.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000238 if (Packet.size() >= SchedModel->getIssueWidth()) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000239 ResourcesModel->clearResources();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000240 savePacket();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000241 Packet.clear();
242 TotalPackets++;
Sergei Larinef4cc112012-09-10 17:31:34 +0000243 startNewCycle = true;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000244 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000245
246 return startNewCycle;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000247}
248
Sergei Larin4d8986a2012-09-04 14:49:56 +0000249/// schedule - Called back from MachineScheduler::runOnMachineFunction
250/// after setting up the current scheduling region. [RegionBegin, RegionEnd)
251/// only includes instructions that have DAG nodes, not scheduling boundaries.
252void VLIWMachineScheduler::schedule() {
253 DEBUG(dbgs()
254 << "********** MI Converging Scheduling VLIW BB#" << BB->getNumber()
255 << " " << BB->getName()
256 << " in_func " << BB->getParent()->getFunction()->getName()
Alexey Samsonov8968e6d2014-08-20 19:36:05 +0000257 << " at loop depth " << MLI->getLoopDepth(BB)
Sergei Larin4d8986a2012-09-04 14:49:56 +0000258 << " \n");
259
Andrew Trick7a8e1002012-09-11 00:39:15 +0000260 buildDAGWithRegPressure();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000261
Andrew Tricke2c3f5c2013-01-25 06:33:57 +0000262 SmallVector<SUnit*, 8> TopRoots, BotRoots;
263 findRootsAndBiasEdges(TopRoots, BotRoots);
264
265 // Initialize the strategy before modifying the DAG.
266 SchedImpl->initialize(this);
267
Sergei Larinef4cc112012-09-10 17:31:34 +0000268 // To view Height/Depth correctly, they should be accessed at least once.
Andrew Trick63474622013-03-02 01:43:08 +0000269 //
270 // FIXME: SUnit::dumpAll always recompute depth and height now. The max
271 // depth/height could be computed directly from the roots and leaves.
Sergei Larinef4cc112012-09-10 17:31:34 +0000272 DEBUG(unsigned maxH = 0;
273 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
274 if (SUnits[su].getHeight() > maxH)
275 maxH = SUnits[su].getHeight();
276 dbgs() << "Max Height " << maxH << "\n";);
277 DEBUG(unsigned maxD = 0;
278 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
279 if (SUnits[su].getDepth() > maxD)
280 maxD = SUnits[su].getDepth();
281 dbgs() << "Max Depth " << maxD << "\n";);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000282 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
283 SUnits[su].dumpAll(this));
284
Andrew Tricke2c3f5c2013-01-25 06:33:57 +0000285 initQueues(TopRoots, BotRoots);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000286
Sergei Larin4d8986a2012-09-04 14:49:56 +0000287 bool IsTopNode = false;
James Y Knighte72b0db2015-09-18 18:52:20 +0000288 while (true) {
289 DEBUG(dbgs() << "** VLIWMachineScheduler::schedule picking next node\n");
290 SUnit *SU = SchedImpl->pickNode(IsTopNode);
291 if (!SU) break;
292
Sergei Larin4d8986a2012-09-04 14:49:56 +0000293 if (!checkSchedLimit())
294 break;
295
Andrew Trick7a8e1002012-09-11 00:39:15 +0000296 scheduleMI(SU, IsTopNode);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000297
Andrew Trick7a8e1002012-09-11 00:39:15 +0000298 updateQueues(SU, IsTopNode);
Andrew Trickd7f890e2013-12-28 21:56:47 +0000299
300 // Notify the scheduling strategy after updating the DAG.
301 SchedImpl->schedNode(SU, IsTopNode);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000302 }
303 assert(CurrentTop == CurrentBottom && "Nonempty unscheduled zone.");
304
Sergei Larin4d8986a2012-09-04 14:49:56 +0000305 placeDebugValues();
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000306
307 DEBUG({
308 unsigned BBNum = begin()->getParent()->getNumber();
309 dbgs() << "*** Final schedule for BB#" << BBNum << " ***\n";
310 dumpSchedule();
311 dbgs() << '\n';
312 });
Sergei Larin4d8986a2012-09-04 14:49:56 +0000313}
314
Andrew Trick7a8e1002012-09-11 00:39:15 +0000315void ConvergingVLIWScheduler::initialize(ScheduleDAGMI *dag) {
316 DAG = static_cast<VLIWMachineScheduler*>(dag);
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000317 SchedModel = DAG->getSchedModel();
Andrew Trick553e0fe2013-02-13 19:22:27 +0000318
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000319 Top.init(DAG, SchedModel);
320 Bot.init(DAG, SchedModel);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000321
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000322 // Initialize the HazardRecognizers. If itineraries don't exist, are empty, or
323 // are disabled, then these HazardRecs will be disabled.
324 const InstrItineraryData *Itin = DAG->getSchedModel()->getInstrItineraries();
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000325 const TargetSubtargetInfo &STI = DAG->MF.getSubtarget();
326 const TargetInstrInfo *TII = STI.getInstrInfo();
Andrew Trick553e0fe2013-02-13 19:22:27 +0000327 delete Top.HazardRec;
328 delete Bot.HazardRec;
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000329 Top.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG);
330 Bot.HazardRec = TII->CreateTargetMIHazardRecognizer(Itin, DAG);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000331
Chandler Carruthc18e39c2013-07-27 10:48:45 +0000332 delete Top.ResourceModel;
333 delete Bot.ResourceModel;
Eric Christopherf8b8e4a2015-02-02 22:11:40 +0000334 Top.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel());
335 Bot.ResourceModel = new VLIWResourceModel(STI, DAG->getSchedModel());
Sergei Larinef4cc112012-09-10 17:31:34 +0000336
Andrew Trick7a8e1002012-09-11 00:39:15 +0000337 assert((!llvm::ForceTopDown || !llvm::ForceBottomUp) &&
Sergei Larin4d8986a2012-09-04 14:49:56 +0000338 "-misched-topdown incompatible with -misched-bottomup");
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000339
340 DAG->addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Krzysztof Parzyszek36b0f932016-07-15 19:09:37 +0000341 DAG->addMutation(make_unique<HexagonCallMutation>());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000342}
343
344void ConvergingVLIWScheduler::releaseTopNode(SUnit *SU) {
345 if (SU->isScheduled)
346 return;
347
348 for (SUnit::succ_iterator I = SU->Preds.begin(), E = SU->Preds.end();
349 I != E; ++I) {
350 unsigned PredReadyCycle = I->getSUnit()->TopReadyCycle;
Andrew Trickde2109e2013-06-15 04:49:57 +0000351 unsigned MinLatency = I->getLatency();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000352#ifndef NDEBUG
353 Top.MaxMinLatency = std::max(MinLatency, Top.MaxMinLatency);
354#endif
355 if (SU->TopReadyCycle < PredReadyCycle + MinLatency)
356 SU->TopReadyCycle = PredReadyCycle + MinLatency;
357 }
358 Top.releaseNode(SU, SU->TopReadyCycle);
359}
360
361void ConvergingVLIWScheduler::releaseBottomNode(SUnit *SU) {
362 if (SU->isScheduled)
363 return;
364
365 assert(SU->getInstr() && "Scheduled SUnit must have instr");
366
367 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
368 I != E; ++I) {
369 unsigned SuccReadyCycle = I->getSUnit()->BotReadyCycle;
Andrew Trickde2109e2013-06-15 04:49:57 +0000370 unsigned MinLatency = I->getLatency();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000371#ifndef NDEBUG
372 Bot.MaxMinLatency = std::max(MinLatency, Bot.MaxMinLatency);
373#endif
374 if (SU->BotReadyCycle < SuccReadyCycle + MinLatency)
375 SU->BotReadyCycle = SuccReadyCycle + MinLatency;
376 }
377 Bot.releaseNode(SU, SU->BotReadyCycle);
378}
379
380/// Does this SU have a hazard within the current instruction group.
381///
382/// The scheduler supports two modes of hazard recognition. The first is the
383/// ScheduleHazardRecognizer API. It is a fully general hazard recognizer that
384/// supports highly complicated in-order reservation tables
385/// (ScoreboardHazardRecognizer) and arbitrary target-specific logic.
386///
387/// The second is a streamlined mechanism that checks for hazards based on
388/// simple counters that the scheduler itself maintains. It explicitly checks
389/// for instruction dispatch limitations, including the number of micro-ops that
390/// can dispatch per cycle.
391///
392/// TODO: Also check whether the SU must start a new group.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000393bool ConvergingVLIWScheduler::VLIWSchedBoundary::checkHazard(SUnit *SU) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000394 if (HazardRec->isEnabled())
395 return HazardRec->getHazardType(SU) != ScheduleHazardRecognizer::NoHazard;
396
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000397 unsigned uops = SchedModel->getNumMicroOps(SU->getInstr());
398 if (IssueCount + uops > SchedModel->getIssueWidth())
Sergei Larin4d8986a2012-09-04 14:49:56 +0000399 return true;
400
401 return false;
402}
403
Andrew Trickd7f890e2013-12-28 21:56:47 +0000404void ConvergingVLIWScheduler::VLIWSchedBoundary::releaseNode(SUnit *SU,
Sergei Larin4d8986a2012-09-04 14:49:56 +0000405 unsigned ReadyCycle) {
406 if (ReadyCycle < MinReadyCycle)
407 MinReadyCycle = ReadyCycle;
408
409 // Check for interlocks first. For the purpose of other heuristics, an
410 // instruction that cannot issue appears as if it's not in the ReadyQueue.
411 if (ReadyCycle > CurrCycle || checkHazard(SU))
412
413 Pending.push(SU);
414 else
415 Available.push(SU);
416}
417
418/// Move the boundary of scheduled code by one cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000419void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpCycle() {
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000420 unsigned Width = SchedModel->getIssueWidth();
Sergei Larin4d8986a2012-09-04 14:49:56 +0000421 IssueCount = (IssueCount <= Width) ? 0 : IssueCount - Width;
422
423 assert(MinReadyCycle < UINT_MAX && "MinReadyCycle uninitialized");
424 unsigned NextCycle = std::max(CurrCycle + 1, MinReadyCycle);
425
426 if (!HazardRec->isEnabled()) {
427 // Bypass HazardRec virtual calls.
428 CurrCycle = NextCycle;
Sergei Larinef4cc112012-09-10 17:31:34 +0000429 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000430 // Bypass getHazardType calls in case of long latency.
431 for (; CurrCycle != NextCycle; ++CurrCycle) {
432 if (isTop())
433 HazardRec->AdvanceCycle();
434 else
435 HazardRec->RecedeCycle();
436 }
437 }
438 CheckPending = true;
439
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000440 DEBUG(dbgs() << "*** Next cycle " << Available.getName() << " cycle "
441 << CurrCycle << '\n');
Sergei Larin4d8986a2012-09-04 14:49:56 +0000442}
443
444/// Move the boundary of scheduled code by one SUnit.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000445void ConvergingVLIWScheduler::VLIWSchedBoundary::bumpNode(SUnit *SU) {
Sergei Larinef4cc112012-09-10 17:31:34 +0000446 bool startNewCycle = false;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000447
448 // Update the reservation table.
449 if (HazardRec->isEnabled()) {
450 if (!isTop() && SU->isCall) {
451 // Calls are scheduled with their preceding instructions. For bottom-up
452 // scheduling, clear the pipeline state before emitting.
453 HazardRec->Reset();
454 }
455 HazardRec->EmitInstruction(SU);
456 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000457
458 // Update DFA model.
459 startNewCycle = ResourceModel->reserveResources(SU);
460
Sergei Larin4d8986a2012-09-04 14:49:56 +0000461 // Check the instruction group dispatch limit.
462 // TODO: Check if this SU must end a dispatch group.
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000463 IssueCount += SchedModel->getNumMicroOps(SU->getInstr());
Sergei Larinef4cc112012-09-10 17:31:34 +0000464 if (startNewCycle) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000465 DEBUG(dbgs() << "*** Max instrs at cycle " << CurrCycle << '\n');
466 bumpCycle();
467 }
Sergei Larinef4cc112012-09-10 17:31:34 +0000468 else
469 DEBUG(dbgs() << "*** IssueCount " << IssueCount
470 << " at cycle " << CurrCycle << '\n');
Sergei Larin4d8986a2012-09-04 14:49:56 +0000471}
472
473/// Release pending ready nodes in to the available queue. This makes them
474/// visible to heuristics.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000475void ConvergingVLIWScheduler::VLIWSchedBoundary::releasePending() {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000476 // If the available queue is empty, it is safe to reset MinReadyCycle.
477 if (Available.empty())
478 MinReadyCycle = UINT_MAX;
479
480 // Check to see if any of the pending instructions are ready to issue. If
481 // so, add them to the available queue.
482 for (unsigned i = 0, e = Pending.size(); i != e; ++i) {
483 SUnit *SU = *(Pending.begin()+i);
484 unsigned ReadyCycle = isTop() ? SU->TopReadyCycle : SU->BotReadyCycle;
485
486 if (ReadyCycle < MinReadyCycle)
487 MinReadyCycle = ReadyCycle;
488
489 if (ReadyCycle > CurrCycle)
490 continue;
491
492 if (checkHazard(SU))
493 continue;
494
495 Available.push(SU);
496 Pending.remove(Pending.begin()+i);
497 --i; --e;
498 }
499 CheckPending = false;
500}
501
502/// Remove SU from the ready set for this boundary.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000503void ConvergingVLIWScheduler::VLIWSchedBoundary::removeReady(SUnit *SU) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000504 if (Available.isInQueue(SU))
505 Available.remove(Available.find(SU));
506 else {
507 assert(Pending.isInQueue(SU) && "bad ready count");
508 Pending.remove(Pending.find(SU));
509 }
510}
511
512/// If this queue only has one ready candidate, return it. As a side effect,
513/// advance the cycle until at least one node is ready. If multiple instructions
514/// are ready, return NULL.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000515SUnit *ConvergingVLIWScheduler::VLIWSchedBoundary::pickOnlyChoice() {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000516 if (CheckPending)
517 releasePending();
518
519 for (unsigned i = 0; Available.empty(); ++i) {
520 assert(i <= (HazardRec->getMaxLookAhead() + MaxMinLatency) &&
521 "permanent hazard"); (void)i;
Craig Topper062a2ba2014-04-25 05:30:21 +0000522 ResourceModel->reserveResources(nullptr);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000523 bumpCycle();
524 releasePending();
525 }
526 if (Available.size() == 1)
527 return *Available.begin();
Craig Topper062a2ba2014-04-25 05:30:21 +0000528 return nullptr;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000529}
530
531#ifndef NDEBUG
Sergei Larinef4cc112012-09-10 17:31:34 +0000532void ConvergingVLIWScheduler::traceCandidate(const char *Label,
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000533 const ReadyQueue &Q, SUnit *SU, int Cost, PressureChange P) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000534 dbgs() << Label << " " << Q.getName() << " ";
535 if (P.isValid())
Andrew Trick1a831342013-08-30 03:49:48 +0000536 dbgs() << DAG->TRI->getRegPressureSetName(P.getPSet()) << ":"
537 << P.getUnitInc() << " ";
Sergei Larin4d8986a2012-09-04 14:49:56 +0000538 else
539 dbgs() << " ";
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000540 dbgs() << "cost(" << Cost << ")\t";
Sergei Larin4d8986a2012-09-04 14:49:56 +0000541 SU->dump(DAG);
542}
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000543
544// Very detailed queue dump, to be used with higher verbosity levels.
545void ConvergingVLIWScheduler::readyQueueVerboseDump(
546 const RegPressureTracker &RPTracker, SchedCandidate &Candidate,
547 ReadyQueue &Q) {
548 RegPressureTracker &TempTracker = const_cast<RegPressureTracker &>(RPTracker);
549
550 dbgs() << ">>> " << Q.getName() << "\n";
551 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
552 RegPressureDelta RPDelta;
553 TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta,
554 DAG->getRegionCriticalPSets(),
555 DAG->getRegPressure().MaxSetPressure);
556 std::stringstream dbgstr;
557 dbgstr << "SU(" << std::setw(3) << (*I)->NodeNum << ")";
558 dbgs() << dbgstr.str();
559 SchedulingCost(Q, *I, Candidate, RPDelta, true);
560 dbgs() << "\t";
561 (*I)->getInstr()->dump();
562 }
563 dbgs() << "\n";
564}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000565#endif
566
Sergei Larinef4cc112012-09-10 17:31:34 +0000567/// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor
568/// of SU, return it, otherwise return null.
569static SUnit *getSingleUnscheduledPred(SUnit *SU) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000570 SUnit *OnlyAvailablePred = nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000571 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
572 I != E; ++I) {
573 SUnit &Pred = *I->getSUnit();
574 if (!Pred.isScheduled) {
575 // We found an available, but not scheduled, predecessor. If it's the
576 // only one we have found, keep track of it... otherwise give up.
577 if (OnlyAvailablePred && OnlyAvailablePred != &Pred)
Craig Topper062a2ba2014-04-25 05:30:21 +0000578 return nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000579 OnlyAvailablePred = &Pred;
580 }
581 }
582 return OnlyAvailablePred;
583}
584
585/// getSingleUnscheduledSucc - If there is exactly one unscheduled successor
586/// of SU, return it, otherwise return null.
587static SUnit *getSingleUnscheduledSucc(SUnit *SU) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000588 SUnit *OnlyAvailableSucc = nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000589 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
590 I != E; ++I) {
591 SUnit &Succ = *I->getSUnit();
592 if (!Succ.isScheduled) {
593 // We found an available, but not scheduled, successor. If it's the
594 // only one we have found, keep track of it... otherwise give up.
595 if (OnlyAvailableSucc && OnlyAvailableSucc != &Succ)
Craig Topper062a2ba2014-04-25 05:30:21 +0000596 return nullptr;
Sergei Larinef4cc112012-09-10 17:31:34 +0000597 OnlyAvailableSucc = &Succ;
598 }
599 }
600 return OnlyAvailableSucc;
601}
602
Sergei Larin4d8986a2012-09-04 14:49:56 +0000603// Constants used to denote relative importance of
604// heuristic components for cost computation.
605static const unsigned PriorityOne = 200;
Eli Friedman8f06d552013-09-11 00:41:02 +0000606static const unsigned PriorityTwo = 50;
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000607static const unsigned PriorityThree = 75;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000608static const unsigned ScaleTwo = 10;
609static const unsigned FactorOne = 2;
610
611/// Single point to compute overall scheduling cost.
612/// TODO: More heuristics will be used soon.
613int ConvergingVLIWScheduler::SchedulingCost(ReadyQueue &Q, SUnit *SU,
614 SchedCandidate &Candidate,
615 RegPressureDelta &Delta,
616 bool verbose) {
617 // Initial trivial priority.
618 int ResCount = 1;
619
620 // Do not waste time on a node that is already scheduled.
621 if (!SU || SU->isScheduled)
622 return ResCount;
623
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000624 MachineInstr *Instr = SU->getInstr();
625
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000626 DEBUG(if (verbose) dbgs() << ((Q.getID() == TopQID) ? "(top|" : "(bot|"));
Sergei Larin4d8986a2012-09-04 14:49:56 +0000627 // Forced priority is high.
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000628 if (SU->isScheduleHigh) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000629 ResCount += PriorityOne;
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000630 DEBUG(dbgs() << "H|");
631 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000632
633 // Critical path first.
Sergei Larinef4cc112012-09-10 17:31:34 +0000634 if (Q.getID() == TopQID) {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000635 ResCount += (SU->getHeight() * ScaleTwo);
Sergei Larinef4cc112012-09-10 17:31:34 +0000636
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000637 DEBUG(if (verbose) {
638 std::stringstream dbgstr;
639 dbgstr << "h" << std::setw(3) << SU->getHeight() << "|";
640 dbgs() << dbgstr.str();
641 });
642
Sergei Larinef4cc112012-09-10 17:31:34 +0000643 // If resources are available for it, multiply the
644 // chance of scheduling.
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000645 if (Top.ResourceModel->isResourceAvailable(SU)) {
Sergei Larinef4cc112012-09-10 17:31:34 +0000646 ResCount <<= FactorOne;
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000647 ResCount += PriorityThree;
648 DEBUG(if (verbose) dbgs() << "A|");
649 } else
650 DEBUG(if (verbose) dbgs() << " |");
Sergei Larinef4cc112012-09-10 17:31:34 +0000651 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000652 ResCount += (SU->getDepth() * ScaleTwo);
653
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000654 DEBUG(if (verbose) {
655 std::stringstream dbgstr;
656 dbgstr << "d" << std::setw(3) << SU->getDepth() << "|";
657 dbgs() << dbgstr.str();
658 });
659
Sergei Larinef4cc112012-09-10 17:31:34 +0000660 // If resources are available for it, multiply the
661 // chance of scheduling.
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000662 if (Bot.ResourceModel->isResourceAvailable(SU)) {
Sergei Larinef4cc112012-09-10 17:31:34 +0000663 ResCount <<= FactorOne;
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000664 ResCount += PriorityThree;
665 DEBUG(if (verbose) dbgs() << "A|");
666 } else
667 DEBUG(if (verbose) dbgs() << " |");
Sergei Larinef4cc112012-09-10 17:31:34 +0000668 }
669
670 unsigned NumNodesBlocking = 0;
671 if (Q.getID() == TopQID) {
672 // How many SUs does it block from scheduling?
673 // Look at all of the successors of this node.
674 // Count the number of nodes that
675 // this node is the sole unscheduled node for.
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000676 for (const SDep &SI : SU->Succs)
677 if (getSingleUnscheduledPred(SI.getSUnit()) == SU)
Sergei Larinef4cc112012-09-10 17:31:34 +0000678 ++NumNodesBlocking;
679 } else {
680 // How many unscheduled predecessors block this node?
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000681 for (const SDep &PI : SU->Preds)
682 if (getSingleUnscheduledSucc(PI.getSUnit()) == SU)
Sergei Larinef4cc112012-09-10 17:31:34 +0000683 ++NumNodesBlocking;
684 }
685 ResCount += (NumNodesBlocking * ScaleTwo);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000686
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000687 DEBUG(if (verbose) {
688 std::stringstream dbgstr;
689 dbgstr << "blk " << std::setw(2) << NumNodesBlocking << ")|";
690 dbgs() << dbgstr.str();
691 });
692
Sergei Larin4d8986a2012-09-04 14:49:56 +0000693 // Factor in reg pressure as a heuristic.
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000694 if (!IgnoreBBRegPressure) {
695 // Decrease priority by the amount that register pressure exceeds the limit.
696 ResCount -= (Delta.Excess.getUnitInc()*PriorityOne);
697 // Decrease priority if register pressure exceeds the limit.
698 ResCount -= (Delta.CriticalMax.getUnitInc()*PriorityOne);
699 // Decrease priority slightly if register pressure would increase over the
700 // current maximum.
701 ResCount -= (Delta.CurrentMax.getUnitInc()*PriorityTwo);
702 DEBUG(if (verbose) {
703 dbgs() << "RP " << Delta.Excess.getUnitInc() << "/"
704 << Delta.CriticalMax.getUnitInc() <<"/"
705 << Delta.CurrentMax.getUnitInc() << ")|";
706 });
707 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000708
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000709 // Give a little extra priority to a .cur instruction if there is a resource
710 // available for it.
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000711 auto &QST = DAG->MF.getSubtarget<HexagonSubtarget>();
712 auto &QII = *QST.getInstrInfo();
713
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000714 // Give a little extra priority to a .cur instruction if there is a resource
715 // available for it.
716 if (SU->isInstr() && QII.mayBeCurLoad(SU->getInstr())) {
717 if (Q.getID() == TopQID && Top.ResourceModel->isResourceAvailable(SU)) {
718 ResCount += PriorityTwo;
719 DEBUG(if (verbose) dbgs() << "C|");
720 } else if (Q.getID() == BotQID &&
721 Bot.ResourceModel->isResourceAvailable(SU)) {
722 ResCount += PriorityTwo;
723 DEBUG(if (verbose) dbgs() << "C|");
724 }
725 }
726
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000727 // Give preference to a zero latency instruction if the dependent
728 // instruction is in the current packet.
729 if (Q.getID() == TopQID) {
730 for (const SDep &PI : SU->Preds) {
731 if (!PI.getSUnit()->getInstr()->isPseudo() && PI.isAssignedRegDep() &&
732 PI.getLatency() == 0 &&
733 Top.ResourceModel->isInPacket(PI.getSUnit())) {
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000734 ResCount += PriorityThree;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000735 DEBUG(if (verbose) dbgs() << "Z|");
736 }
737 }
738 } else {
739 for (const SDep &SI : SU->Succs) {
740 if (!SI.getSUnit()->getInstr()->isPseudo() && SI.isAssignedRegDep() &&
741 SI.getLatency() == 0 &&
742 Bot.ResourceModel->isInPacket(SI.getSUnit())) {
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000743 ResCount += PriorityThree;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000744 DEBUG(if (verbose) dbgs() << "Z|");
745 }
746 }
747 }
748
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000749 // Give less preference to an instruction that will cause a stall with
750 // an instruction in the previous packet.
751 if (QII.isV60VectorInstruction(Instr)) {
752 // Check for stalls in the previous packet.
753 if (Q.getID() == TopQID) {
754 for (auto J : Top.ResourceModel->OldPacket)
755 if (QII.producesStall(J->getInstr(), Instr))
756 ResCount -= PriorityOne;
757 } else {
758 for (auto J : Bot.ResourceModel->OldPacket)
759 if (QII.producesStall(Instr, J->getInstr()))
760 ResCount -= PriorityOne;
761 }
762 }
763
Krzysztof Parzyszek3467e9d2016-07-18 14:52:13 +0000764 // If the instruction has a non-zero latency dependence with an instruction in
765 // the current packet, then it should not be scheduled yet. The case occurs
766 // when the dependent instruction is scheduled in a new packet, so the
767 // scheduler updates the current cycle and pending instructions become
768 // available.
769 if (CheckEarlyAvail) {
770 if (Q.getID() == TopQID) {
771 for (const auto &PI : SU->Preds) {
772 if (PI.getLatency() > 0 &&
773 Top.ResourceModel->isInPacket(PI.getSUnit())) {
774 ResCount -= PriorityOne;
775 DEBUG(if (verbose) dbgs() << "D|");
776 }
777 }
778 } else {
779 for (const auto &SI : SU->Succs) {
780 if (SI.getLatency() > 0 &&
781 Bot.ResourceModel->isInPacket(SI.getSUnit())) {
782 ResCount -= PriorityOne;
783 DEBUG(if (verbose) dbgs() << "D|");
784 }
785 }
786 }
787 }
788
789 // Give less preference to an instruction that will cause a stall with
790 // an instruction in the previous packet.
791 if (QII.isV60VectorInstruction(Instr)) {
792 // Check for stalls in the previous packet.
793 if (Q.getID() == TopQID) {
794 for (auto J : Top.ResourceModel->OldPacket)
795 if (QII.producesStall(J->getInstr(), Instr))
796 ResCount -= PriorityOne;
797 } else {
798 for (auto J : Bot.ResourceModel->OldPacket)
799 if (QII.producesStall(Instr, J->getInstr()))
800 ResCount -= PriorityOne;
801 }
802 }
803
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000804 DEBUG(if (verbose) {
805 std::stringstream dbgstr;
806 dbgstr << "Total " << std::setw(4) << ResCount << ")";
807 dbgs() << dbgstr.str();
808 });
Sergei Larin4d8986a2012-09-04 14:49:56 +0000809
810 return ResCount;
811}
812
813/// Pick the best candidate from the top queue.
814///
815/// TODO: getMaxPressureDelta results can be mostly cached for each SUnit during
816/// DAG building. To adjust for the current scheduling location we need to
817/// maintain the number of vreg uses remaining to be top-scheduled.
818ConvergingVLIWScheduler::CandResult ConvergingVLIWScheduler::
819pickNodeFromQueue(ReadyQueue &Q, const RegPressureTracker &RPTracker,
820 SchedCandidate &Candidate) {
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000821 DEBUG(if (SchedDebugVerboseLevel > 1)
822 readyQueueVerboseDump(RPTracker, Candidate, Q);
823 else Q.dump(););
Sergei Larin4d8986a2012-09-04 14:49:56 +0000824
825 // getMaxPressureDelta temporarily modifies the tracker.
826 RegPressureTracker &TempTracker = const_cast<RegPressureTracker&>(RPTracker);
827
828 // BestSU remains NULL if no top candidates beat the best existing candidate.
829 CandResult FoundCandidate = NoCand;
830 for (ReadyQueue::iterator I = Q.begin(), E = Q.end(); I != E; ++I) {
831 RegPressureDelta RPDelta;
832 TempTracker.getMaxPressureDelta((*I)->getInstr(), RPDelta,
833 DAG->getRegionCriticalPSets(),
834 DAG->getRegPressure().MaxSetPressure);
835
836 int CurrentCost = SchedulingCost(Q, *I, Candidate, RPDelta, false);
837
838 // Initialize the candidate if needed.
839 if (!Candidate.SU) {
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000840 DEBUG(traceCandidate("DCAND", Q, *I, CurrentCost));
Sergei Larin4d8986a2012-09-04 14:49:56 +0000841 Candidate.SU = *I;
842 Candidate.RPDelta = RPDelta;
843 Candidate.SCost = CurrentCost;
844 FoundCandidate = NodeOrder;
845 continue;
846 }
847
Sergei Larin4d8986a2012-09-04 14:49:56 +0000848 // Best cost.
849 if (CurrentCost > Candidate.SCost) {
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000850 DEBUG(traceCandidate("CCAND", Q, *I, CurrentCost));
Sergei Larin4d8986a2012-09-04 14:49:56 +0000851 Candidate.SU = *I;
852 Candidate.RPDelta = RPDelta;
853 Candidate.SCost = CurrentCost;
854 FoundCandidate = BestCost;
855 continue;
856 }
857
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000858 // Tie breaker using Timing Class.
859 if (!DisableTCTie) {
860 auto &QST = DAG->MF.getSubtarget<HexagonSubtarget>();
861 auto &QII = *QST.getInstrInfo();
862
863 const MachineInstr *MI = (*I)->getInstr();
864 const MachineInstr *CandI = Candidate.SU->getInstr();
865 const InstrItineraryData *InstrItins = QST.getInstrItineraryData();
866
867 unsigned InstrLatency = QII.getInstrTimingClassLatency(InstrItins, MI);
868 unsigned CandLatency = QII.getInstrTimingClassLatency(InstrItins, CandI);
869 DEBUG(dbgs() << "TC Tie Breaker Cand: "
870 << CandLatency << " Instr:" << InstrLatency << "\n"
871 << *MI << *CandI << "\n");
872 if (Q.getID() == TopQID && CurrentCost == Candidate.SCost) {
873 if (InstrLatency < CandLatency && TopUseShorterTie) {
874 Candidate.SU = *I;
875 Candidate.RPDelta = RPDelta;
876 Candidate.SCost = CurrentCost;
877 FoundCandidate = BestCost;
878 DEBUG(dbgs() << "Used top shorter tie breaker\n");
879 continue;
880 } else if (InstrLatency > CandLatency && !TopUseShorterTie) {
881 Candidate.SU = *I;
882 Candidate.RPDelta = RPDelta;
883 Candidate.SCost = CurrentCost;
884 FoundCandidate = BestCost;
885 DEBUG(dbgs() << "Used top longer tie breaker\n");
886 continue;
887 }
888 } else if (Q.getID() == BotQID && CurrentCost == Candidate.SCost) {
889 if (InstrLatency < CandLatency && BotUseShorterTie) {
890 Candidate.SU = *I;
891 Candidate.RPDelta = RPDelta;
892 Candidate.SCost = CurrentCost;
893 FoundCandidate = BestCost;
894 DEBUG(dbgs() << "Used Bot shorter tie breaker\n");
895 continue;
896 } else if (InstrLatency > CandLatency && !BotUseShorterTie) {
897 Candidate.SU = *I;
898 Candidate.RPDelta = RPDelta;
899 Candidate.SCost = CurrentCost;
900 FoundCandidate = BestCost;
901 DEBUG(dbgs() << "Used Bot longer tie breaker\n");
902 continue;
903 }
904 }
905 }
906
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000907 if (CurrentCost == Candidate.SCost) {
908 if ((Q.getID() == TopQID &&
909 (*I)->Succs.size() > Candidate.SU->Succs.size()) ||
910 (Q.getID() == BotQID &&
911 (*I)->Preds.size() < Candidate.SU->Preds.size())) {
912 DEBUG(traceCandidate("SPCAND", Q, *I, CurrentCost));
913 Candidate.SU = *I;
914 Candidate.RPDelta = RPDelta;
915 Candidate.SCost = CurrentCost;
916 FoundCandidate = BestCost;
917 continue;
918 }
919 }
920
Sergei Larin4d8986a2012-09-04 14:49:56 +0000921 // Fall through to original instruction order.
922 // Only consider node order if Candidate was chosen from this Q.
923 if (FoundCandidate == NoCand)
924 continue;
925 }
926 return FoundCandidate;
927}
928
929/// Pick the best candidate node from either the top or bottom queue.
930SUnit *ConvergingVLIWScheduler::pickNodeBidrectional(bool &IsTopNode) {
931 // Schedule as far as possible in the direction of no choice. This is most
932 // efficient, but also provides the best heuristics for CriticalPSets.
933 if (SUnit *SU = Bot.pickOnlyChoice()) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000934 DEBUG(dbgs() << "Picked only Bottom\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000935 IsTopNode = false;
936 return SU;
937 }
938 if (SUnit *SU = Top.pickOnlyChoice()) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000939 DEBUG(dbgs() << "Picked only Top\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000940 IsTopNode = true;
941 return SU;
942 }
943 SchedCandidate BotCand;
944 // Prefer bottom scheduling when heuristics are silent.
945 CandResult BotResult = pickNodeFromQueue(Bot.Available,
946 DAG->getBotRPTracker(), BotCand);
947 assert(BotResult != NoCand && "failed to find the first candidate");
948
949 // If either Q has a single candidate that provides the least increase in
950 // Excess pressure, we can immediately schedule from that Q.
951 //
952 // RegionCriticalPSets summarizes the pressure within the scheduled region and
953 // affects picking from either Q. If scheduling in one direction must
954 // increase pressure for one of the excess PSets, then schedule in that
955 // direction first to provide more freedom in the other direction.
956 if (BotResult == SingleExcess || BotResult == SingleCritical) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000957 DEBUG(dbgs() << "Prefered Bottom Node\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000958 IsTopNode = false;
959 return BotCand.SU;
960 }
961 // Check if the top Q has a better candidate.
962 SchedCandidate TopCand;
963 CandResult TopResult = pickNodeFromQueue(Top.Available,
964 DAG->getTopRPTracker(), TopCand);
965 assert(TopResult != NoCand && "failed to find the first candidate");
966
967 if (TopResult == SingleExcess || TopResult == SingleCritical) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000968 DEBUG(dbgs() << "Prefered Top Node\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000969 IsTopNode = true;
970 return TopCand.SU;
971 }
972 // If either Q has a single candidate that minimizes pressure above the
973 // original region's pressure pick it.
974 if (BotResult == SingleMax) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000975 DEBUG(dbgs() << "Prefered Bottom Node SingleMax\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000976 IsTopNode = false;
977 return BotCand.SU;
978 }
979 if (TopResult == SingleMax) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000980 DEBUG(dbgs() << "Prefered Top Node SingleMax\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000981 IsTopNode = true;
982 return TopCand.SU;
983 }
984 if (TopCand.SCost > BotCand.SCost) {
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000985 DEBUG(dbgs() << "Prefered Top Node Cost\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000986 IsTopNode = true;
987 return TopCand.SU;
988 }
989 // Otherwise prefer the bottom candidate in node order.
Krzysztof Parzyszek393b3792016-07-18 15:17:10 +0000990 DEBUG(dbgs() << "Prefered Bottom in Node order\n");
Sergei Larin4d8986a2012-09-04 14:49:56 +0000991 IsTopNode = false;
992 return BotCand.SU;
993}
994
995/// Pick the best node to balance the schedule. Implements MachineSchedStrategy.
996SUnit *ConvergingVLIWScheduler::pickNode(bool &IsTopNode) {
997 if (DAG->top() == DAG->bottom()) {
998 assert(Top.Available.empty() && Top.Pending.empty() &&
999 Bot.Available.empty() && Bot.Pending.empty() && "ReadyQ garbage");
Craig Topper062a2ba2014-04-25 05:30:21 +00001000 return nullptr;
Sergei Larin4d8986a2012-09-04 14:49:56 +00001001 }
1002 SUnit *SU;
Andrew Trick7a8e1002012-09-11 00:39:15 +00001003 if (llvm::ForceTopDown) {
Sergei Larin4d8986a2012-09-04 14:49:56 +00001004 SU = Top.pickOnlyChoice();
1005 if (!SU) {
1006 SchedCandidate TopCand;
1007 CandResult TopResult =
1008 pickNodeFromQueue(Top.Available, DAG->getTopRPTracker(), TopCand);
1009 assert(TopResult != NoCand && "failed to find the first candidate");
1010 (void)TopResult;
1011 SU = TopCand.SU;
1012 }
1013 IsTopNode = true;
Andrew Trick7a8e1002012-09-11 00:39:15 +00001014 } else if (llvm::ForceBottomUp) {
Sergei Larin4d8986a2012-09-04 14:49:56 +00001015 SU = Bot.pickOnlyChoice();
1016 if (!SU) {
1017 SchedCandidate BotCand;
1018 CandResult BotResult =
1019 pickNodeFromQueue(Bot.Available, DAG->getBotRPTracker(), BotCand);
1020 assert(BotResult != NoCand && "failed to find the first candidate");
1021 (void)BotResult;
1022 SU = BotCand.SU;
1023 }
1024 IsTopNode = false;
1025 } else {
1026 SU = pickNodeBidrectional(IsTopNode);
1027 }
1028 if (SU->isTopReady())
1029 Top.removeReady(SU);
1030 if (SU->isBottomReady())
1031 Bot.removeReady(SU);
1032
1033 DEBUG(dbgs() << "*** " << (IsTopNode ? "Top" : "Bottom")
1034 << " Scheduling Instruction in cycle "
1035 << (IsTopNode ? Top.CurrCycle : Bot.CurrCycle) << '\n';
1036 SU->dump(DAG));
1037 return SU;
1038}
1039
1040/// Update the scheduler's state after scheduling a node. This is the same node
Sergei Larinef4cc112012-09-10 17:31:34 +00001041/// that was just returned by pickNode(). However, VLIWMachineScheduler needs
1042/// to update it's state based on the current cycle before MachineSchedStrategy
1043/// does.
Sergei Larin4d8986a2012-09-04 14:49:56 +00001044void ConvergingVLIWScheduler::schedNode(SUnit *SU, bool IsTopNode) {
1045 if (IsTopNode) {
1046 SU->TopReadyCycle = Top.CurrCycle;
1047 Top.bumpNode(SU);
Sergei Larinef4cc112012-09-10 17:31:34 +00001048 } else {
Sergei Larin4d8986a2012-09-04 14:49:56 +00001049 SU->BotReadyCycle = Bot.CurrCycle;
1050 Bot.bumpNode(SU);
1051 }
1052}