blob: 60343442e327ca5937bb242a3b9e9c20d3a67fc7 [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/CommandLine.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000031#include "llvm/Target/TargetInstrInfo.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000032
33using namespace llvm;
34
Sergei Larin4d8986a2012-09-04 14:49:56 +000035namespace llvm {
Sergei Larin4d8986a2012-09-04 14:49:56 +000036//===----------------------------------------------------------------------===//
Sergei Larinef4cc112012-09-10 17:31:34 +000037// ConvergingVLIWScheduler - Implementation of the standard
38// MachineSchedStrategy.
Sergei Larin4d8986a2012-09-04 14:49:56 +000039//===----------------------------------------------------------------------===//
40
Sergei Larinef4cc112012-09-10 17:31:34 +000041class VLIWResourceModel {
42 /// ResourcesModel - Represents VLIW state.
43 /// Not limited to VLIW targets per say, but assumes
44 /// definition of DFA by a target.
45 DFAPacketizer *ResourcesModel;
46
Andrew Trickdd79f0f2012-10-10 05:43:09 +000047 const TargetSchedModel *SchedModel;
Sergei Larinef4cc112012-09-10 17:31:34 +000048
49 /// Local packet/bundle model. Purely
50 /// internal to the MI schedulre at the time.
51 std::vector<SUnit*> Packet;
52
53 /// Total packets created.
54 unsigned TotalPackets;
55
56public:
Eric Christopherf8b8e4a2015-02-02 22:11:40 +000057 VLIWResourceModel(const TargetSubtargetInfo &STI, const TargetSchedModel *SM)
58 : SchedModel(SM), TotalPackets(0) {
59 ResourcesModel = STI.getInstrInfo()->CreateTargetScheduleState(STI);
Sergei Larinef4cc112012-09-10 17:31:34 +000060
61 // This hard requirement could be relaxed,
62 // but for now do not let it proceed.
63 assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
64
Andrew Trickdd79f0f2012-10-10 05:43:09 +000065 Packet.resize(SchedModel->getIssueWidth());
Sergei Larinef4cc112012-09-10 17:31:34 +000066 Packet.clear();
67 ResourcesModel->clearResources();
68 }
69
70 ~VLIWResourceModel() {
71 delete ResourcesModel;
72 }
73
74 void resetPacketState() {
75 Packet.clear();
76 }
77
78 void resetDFA() {
79 ResourcesModel->clearResources();
80 }
81
82 void reset() {
83 Packet.clear();
84 ResourcesModel->clearResources();
85 }
86
87 bool isResourceAvailable(SUnit *SU);
88 bool reserveResources(SUnit *SU);
89 unsigned getTotalPackets() const { return TotalPackets; }
90};
91
Andrew Trick7a8e1002012-09-11 00:39:15 +000092/// Extend the standard ScheduleDAGMI to provide more context and override the
93/// top-level schedule() driver.
Andrew Trickd7f890e2013-12-28 21:56:47 +000094class VLIWMachineScheduler : public ScheduleDAGMILive {
Sergei Larinef4cc112012-09-10 17:31:34 +000095public:
David Blaikie422b93d2014-04-21 20:32:32 +000096 VLIWMachineScheduler(MachineSchedContext *C,
97 std::unique_ptr<MachineSchedStrategy> S)
98 : ScheduleDAGMILive(C, std::move(S)) {}
Sergei Larinef4cc112012-09-10 17:31:34 +000099
100 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
101 /// time to do some work.
Craig Topperfd38cbe2014-08-30 16:48:34 +0000102 void schedule() override;
Alp Tokercf218752014-06-30 18:57:16 +0000103 /// Perform platform-specific DAG postprocessing.
Sergei Larin2db64a72012-09-14 15:07:59 +0000104 void postprocessDAG();
Sergei Larinef4cc112012-09-10 17:31:34 +0000105};
106
107/// ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics
108/// to balance the schedule.
Sergei Larin4d8986a2012-09-04 14:49:56 +0000109class ConvergingVLIWScheduler : public MachineSchedStrategy {
110
Sergei Larinef4cc112012-09-10 17:31:34 +0000111 /// Store the state used by ConvergingVLIWScheduler heuristics, required
112 /// for the lifetime of one invocation of pickNode().
Sergei Larin4d8986a2012-09-04 14:49:56 +0000113 struct SchedCandidate {
114 // The best SUnit candidate.
115 SUnit *SU;
116
117 // Register pressure values for the best candidate.
118 RegPressureDelta RPDelta;
119
120 // Best scheduling cost.
121 int SCost;
122
Craig Toppere73658d2014-04-28 04:05:08 +0000123 SchedCandidate(): SU(nullptr), SCost(0) {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000124 };
125 /// Represent the type of SchedCandidate found within a single queue.
126 enum CandResult {
127 NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure,
128 BestCost};
129
130 /// Each Scheduling boundary is associated with ready queues. It tracks the
131 /// current cycle in whichever direction at has moved, and maintains the state
132 /// of "hazards" and other interlocks at the current cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000133 struct VLIWSchedBoundary {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000134 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000135 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000136
137 ReadyQueue Available;
138 ReadyQueue Pending;
139 bool CheckPending;
140
141 ScheduleHazardRecognizer *HazardRec;
Sergei Larinef4cc112012-09-10 17:31:34 +0000142 VLIWResourceModel *ResourceModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000143
144 unsigned CurrCycle;
145 unsigned IssueCount;
146
147 /// MinReadyCycle - Cycle of the soonest available instruction.
148 unsigned MinReadyCycle;
149
150 // Remember the greatest min operand latency.
151 unsigned MaxMinLatency;
152
153 /// Pending queues extend the ready queues with the same ID and the
154 /// PendingFlag set.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000155 VLIWSchedBoundary(unsigned ID, const Twine &Name):
Craig Toppere73658d2014-04-28 04:05:08 +0000156 DAG(nullptr), SchedModel(nullptr), Available(ID, Name+".A"),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000157 Pending(ID << ConvergingVLIWScheduler::LogMaxQID, Name+".P"),
Craig Toppere73658d2014-04-28 04:05:08 +0000158 CheckPending(false), HazardRec(nullptr), ResourceModel(nullptr),
Sergei Larinef4cc112012-09-10 17:31:34 +0000159 CurrCycle(0), IssueCount(0),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000160 MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
161
Andrew Trickd7f890e2013-12-28 21:56:47 +0000162 ~VLIWSchedBoundary() {
Sergei Larinef4cc112012-09-10 17:31:34 +0000163 delete ResourceModel;
164 delete HazardRec;
165 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000166
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000167 void init(VLIWMachineScheduler *dag, const TargetSchedModel *smodel) {
168 DAG = dag;
169 SchedModel = smodel;
170 }
171
Sergei Larin4d8986a2012-09-04 14:49:56 +0000172 bool isTop() const {
173 return Available.getID() == ConvergingVLIWScheduler::TopQID;
174 }
175
176 bool checkHazard(SUnit *SU);
177
178 void releaseNode(SUnit *SU, unsigned ReadyCycle);
179
180 void bumpCycle();
181
182 void bumpNode(SUnit *SU);
183
184 void releasePending();
185
186 void removeReady(SUnit *SU);
187
188 SUnit *pickOnlyChoice();
189 };
190
191 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000192 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000193
194 // State of the top and bottom scheduled instruction boundaries.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000195 VLIWSchedBoundary Top;
196 VLIWSchedBoundary Bot;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000197
198public:
199 /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
200 enum {
201 TopQID = 1,
202 BotQID = 2,
203 LogMaxQID = 2
204 };
205
Craig Toppere73658d2014-04-28 04:05:08 +0000206 ConvergingVLIWScheduler()
207 : DAG(nullptr), SchedModel(nullptr), Top(TopQID, "TopQ"),
208 Bot(BotQID, "BotQ") {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000209
Craig Topperfd38cbe2014-08-30 16:48:34 +0000210 void initialize(ScheduleDAGMI *dag) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000211
Craig Topperfd38cbe2014-08-30 16:48:34 +0000212 SUnit *pickNode(bool &IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000213
Craig Topperfd38cbe2014-08-30 16:48:34 +0000214 void schedNode(SUnit *SU, bool IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000215
Craig Topperfd38cbe2014-08-30 16:48:34 +0000216 void releaseTopNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000217
Craig Topperfd38cbe2014-08-30 16:48:34 +0000218 void releaseBottomNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000219
Sergei Larin2db64a72012-09-14 15:07:59 +0000220 unsigned ReportPackets() {
221 return Top.ResourceModel->getTotalPackets() +
222 Bot.ResourceModel->getTotalPackets();
223 }
224
Sergei Larin4d8986a2012-09-04 14:49:56 +0000225protected:
226 SUnit *pickNodeBidrectional(bool &IsTopNode);
227
228 int SchedulingCost(ReadyQueue &Q,
229 SUnit *SU, SchedCandidate &Candidate,
230 RegPressureDelta &Delta, bool verbose);
231
232 CandResult pickNodeFromQueue(ReadyQueue &Q,
233 const RegPressureTracker &RPTracker,
234 SchedCandidate &Candidate);
235#ifndef NDEBUG
236 void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
Andrew Trick1a831342013-08-30 03:49:48 +0000237 PressureChange P = PressureChange());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000238#endif
239};
240
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000241} // namespace
Sergei Larin4d8986a2012-09-04 14:49:56 +0000242
243
244#endif