blob: dc10028c0424446791b0be529dbaa08361fbf116 [file] [log] [blame]
Sergei Larin4d8986a2012-09-04 14:49:56 +00001//===-- HexagonMachineScheduler.h - Custom Hexagon MI scheduler. ----===//
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// Custom Hexagon MI scheduler.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TARGET_HEXAGON_HEXAGONMACHINESCHEDULER_H
15#define LLVM_LIB_TARGET_HEXAGON_HEXAGONMACHINESCHEDULER_H
Sergei Larin4d8986a2012-09-04 14:49:56 +000016
Chandler Carruth802d7552012-12-04 07:12:27 +000017#include "llvm/ADT/PriorityQueue.h"
18#include "llvm/Analysis/AliasAnalysis.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000019#include "llvm/CodeGen/LiveIntervalAnalysis.h"
20#include "llvm/CodeGen/MachineScheduler.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/RegisterClassInfo.h"
23#include "llvm/CodeGen/RegisterPressure.h"
24#include "llvm/CodeGen/ResourcePriorityQueue.h"
25#include "llvm/CodeGen/ScheduleDAGInstrs.h"
26#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000030#include "llvm/Target/TargetInstrInfo.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000031
32using namespace llvm;
33
Sergei Larin4d8986a2012-09-04 14:49:56 +000034namespace llvm {
Sergei Larin4d8986a2012-09-04 14:49:56 +000035//===----------------------------------------------------------------------===//
Sergei Larinef4cc112012-09-10 17:31:34 +000036// ConvergingVLIWScheduler - Implementation of the standard
37// MachineSchedStrategy.
Sergei Larin4d8986a2012-09-04 14:49:56 +000038//===----------------------------------------------------------------------===//
39
Sergei Larinef4cc112012-09-10 17:31:34 +000040class VLIWResourceModel {
41 /// ResourcesModel - Represents VLIW state.
42 /// Not limited to VLIW targets per say, but assumes
43 /// definition of DFA by a target.
44 DFAPacketizer *ResourcesModel;
45
Andrew Trickdd79f0f2012-10-10 05:43:09 +000046 const TargetSchedModel *SchedModel;
Sergei Larinef4cc112012-09-10 17:31:34 +000047
48 /// Local packet/bundle model. Purely
49 /// internal to the MI schedulre at the time.
50 std::vector<SUnit*> Packet;
51
52 /// Total packets created.
53 unsigned TotalPackets;
54
55public:
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +000056 /// Save the last formed packet.
57 std::vector<SUnit*> OldPacket;
58
59public:
Eric Christopherf8b8e4a2015-02-02 22:11:40 +000060 VLIWResourceModel(const TargetSubtargetInfo &STI, const TargetSchedModel *SM)
61 : SchedModel(SM), TotalPackets(0) {
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +000062 ResourcesModel = STI.getInstrInfo()->CreateTargetScheduleState(STI);
Sergei Larinef4cc112012-09-10 17:31:34 +000063
64 // This hard requirement could be relaxed,
65 // but for now do not let it proceed.
66 assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
67
Andrew Trickdd79f0f2012-10-10 05:43:09 +000068 Packet.resize(SchedModel->getIssueWidth());
Sergei Larinef4cc112012-09-10 17:31:34 +000069 Packet.clear();
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +000070 OldPacket.resize(SchedModel->getIssueWidth());
71 OldPacket.clear();
Sergei Larinef4cc112012-09-10 17:31:34 +000072 ResourcesModel->clearResources();
73 }
74
75 ~VLIWResourceModel() {
76 delete ResourcesModel;
77 }
78
79 void resetPacketState() {
80 Packet.clear();
81 }
82
83 void resetDFA() {
84 ResourcesModel->clearResources();
85 }
86
87 void reset() {
88 Packet.clear();
89 ResourcesModel->clearResources();
90 }
91
92 bool isResourceAvailable(SUnit *SU);
93 bool reserveResources(SUnit *SU);
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +000094 void savePacket();
Sergei Larinef4cc112012-09-10 17:31:34 +000095 unsigned getTotalPackets() const { return TotalPackets; }
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +000096
David Majnemer0d955d02016-08-11 22:21:41 +000097 bool isInPacket(SUnit *SU) const { return is_contained(Packet, SU); }
Sergei Larinef4cc112012-09-10 17:31:34 +000098};
99
Andrew Trick7a8e1002012-09-11 00:39:15 +0000100/// Extend the standard ScheduleDAGMI to provide more context and override the
101/// top-level schedule() driver.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000102class VLIWMachineScheduler : public ScheduleDAGMILive {
Sergei Larinef4cc112012-09-10 17:31:34 +0000103public:
David Blaikie422b93d2014-04-21 20:32:32 +0000104 VLIWMachineScheduler(MachineSchedContext *C,
105 std::unique_ptr<MachineSchedStrategy> S)
106 : ScheduleDAGMILive(C, std::move(S)) {}
Sergei Larinef4cc112012-09-10 17:31:34 +0000107
108 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
109 /// time to do some work.
Craig Topperfd38cbe2014-08-30 16:48:34 +0000110 void schedule() override;
Sergei Larinef4cc112012-09-10 17:31:34 +0000111};
112
113/// ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics
114/// to balance the schedule.
Sergei Larin4d8986a2012-09-04 14:49:56 +0000115class ConvergingVLIWScheduler : public MachineSchedStrategy {
116
Sergei Larinef4cc112012-09-10 17:31:34 +0000117 /// Store the state used by ConvergingVLIWScheduler heuristics, required
118 /// for the lifetime of one invocation of pickNode().
Sergei Larin4d8986a2012-09-04 14:49:56 +0000119 struct SchedCandidate {
120 // The best SUnit candidate.
121 SUnit *SU;
122
123 // Register pressure values for the best candidate.
124 RegPressureDelta RPDelta;
125
126 // Best scheduling cost.
127 int SCost;
128
Craig Toppere73658d2014-04-28 04:05:08 +0000129 SchedCandidate(): SU(nullptr), SCost(0) {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000130 };
131 /// Represent the type of SchedCandidate found within a single queue.
132 enum CandResult {
133 NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure,
134 BestCost};
135
136 /// Each Scheduling boundary is associated with ready queues. It tracks the
137 /// current cycle in whichever direction at has moved, and maintains the state
138 /// of "hazards" and other interlocks at the current cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000139 struct VLIWSchedBoundary {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000140 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000141 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000142
143 ReadyQueue Available;
144 ReadyQueue Pending;
145 bool CheckPending;
146
147 ScheduleHazardRecognizer *HazardRec;
Sergei Larinef4cc112012-09-10 17:31:34 +0000148 VLIWResourceModel *ResourceModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000149
150 unsigned CurrCycle;
151 unsigned IssueCount;
152
153 /// MinReadyCycle - Cycle of the soonest available instruction.
154 unsigned MinReadyCycle;
155
156 // Remember the greatest min operand latency.
157 unsigned MaxMinLatency;
158
159 /// Pending queues extend the ready queues with the same ID and the
160 /// PendingFlag set.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000161 VLIWSchedBoundary(unsigned ID, const Twine &Name):
Craig Toppere73658d2014-04-28 04:05:08 +0000162 DAG(nullptr), SchedModel(nullptr), Available(ID, Name+".A"),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000163 Pending(ID << ConvergingVLIWScheduler::LogMaxQID, Name+".P"),
Craig Toppere73658d2014-04-28 04:05:08 +0000164 CheckPending(false), HazardRec(nullptr), ResourceModel(nullptr),
Sergei Larinef4cc112012-09-10 17:31:34 +0000165 CurrCycle(0), IssueCount(0),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000166 MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
167
Andrew Trickd7f890e2013-12-28 21:56:47 +0000168 ~VLIWSchedBoundary() {
Sergei Larinef4cc112012-09-10 17:31:34 +0000169 delete ResourceModel;
170 delete HazardRec;
171 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000172
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000173 void init(VLIWMachineScheduler *dag, const TargetSchedModel *smodel) {
174 DAG = dag;
175 SchedModel = smodel;
Krzysztof Parzyszek6c715e12016-07-15 20:16:03 +0000176 IssueCount = 0;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000177 }
178
Sergei Larin4d8986a2012-09-04 14:49:56 +0000179 bool isTop() const {
180 return Available.getID() == ConvergingVLIWScheduler::TopQID;
181 }
182
183 bool checkHazard(SUnit *SU);
184
185 void releaseNode(SUnit *SU, unsigned ReadyCycle);
186
187 void bumpCycle();
188
189 void bumpNode(SUnit *SU);
190
191 void releasePending();
192
193 void removeReady(SUnit *SU);
194
195 SUnit *pickOnlyChoice();
196 };
197
198 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000199 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000200
201 // State of the top and bottom scheduled instruction boundaries.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000202 VLIWSchedBoundary Top;
203 VLIWSchedBoundary Bot;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000204
205public:
206 /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
207 enum {
208 TopQID = 1,
209 BotQID = 2,
210 LogMaxQID = 2
211 };
212
Craig Toppere73658d2014-04-28 04:05:08 +0000213 ConvergingVLIWScheduler()
214 : DAG(nullptr), SchedModel(nullptr), Top(TopQID, "TopQ"),
215 Bot(BotQID, "BotQ") {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000216
Craig Topperfd38cbe2014-08-30 16:48:34 +0000217 void initialize(ScheduleDAGMI *dag) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000218
Craig Topperfd38cbe2014-08-30 16:48:34 +0000219 SUnit *pickNode(bool &IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000220
Craig Topperfd38cbe2014-08-30 16:48:34 +0000221 void schedNode(SUnit *SU, bool IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000222
Craig Topperfd38cbe2014-08-30 16:48:34 +0000223 void releaseTopNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000224
Craig Topperfd38cbe2014-08-30 16:48:34 +0000225 void releaseBottomNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000226
Sergei Larin2db64a72012-09-14 15:07:59 +0000227 unsigned ReportPackets() {
228 return Top.ResourceModel->getTotalPackets() +
229 Bot.ResourceModel->getTotalPackets();
230 }
231
Sergei Larin4d8986a2012-09-04 14:49:56 +0000232protected:
233 SUnit *pickNodeBidrectional(bool &IsTopNode);
234
235 int SchedulingCost(ReadyQueue &Q,
236 SUnit *SU, SchedCandidate &Candidate,
237 RegPressureDelta &Delta, bool verbose);
238
239 CandResult pickNodeFromQueue(ReadyQueue &Q,
240 const RegPressureTracker &RPTracker,
241 SchedCandidate &Candidate);
242#ifndef NDEBUG
243 void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
Krzysztof Parzyszekf05dc4d2016-07-18 15:47:25 +0000244 int Cost, PressureChange P = PressureChange());
245
246 void readyQueueVerboseDump(const RegPressureTracker &RPTracker,
247 SchedCandidate &Candidate, ReadyQueue &Q);
Sergei Larin4d8986a2012-09-04 14:49:56 +0000248#endif
249};
250
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000251} // namespace
Sergei Larin4d8986a2012-09-04 14:49:56 +0000252
253
254#endif