blob: 1fe8d9d0b2cfcdfcfc1c80866612d8e77f032024 [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:
Eric Christopherf8b8e4a2015-02-02 22:11:40 +000056 VLIWResourceModel(const TargetSubtargetInfo &STI, const TargetSchedModel *SM)
57 : SchedModel(SM), TotalPackets(0) {
58 ResourcesModel = STI.getInstrInfo()->CreateTargetScheduleState(STI);
Sergei Larinef4cc112012-09-10 17:31:34 +000059
60 // This hard requirement could be relaxed,
61 // but for now do not let it proceed.
62 assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
63
Andrew Trickdd79f0f2012-10-10 05:43:09 +000064 Packet.resize(SchedModel->getIssueWidth());
Sergei Larinef4cc112012-09-10 17:31:34 +000065 Packet.clear();
66 ResourcesModel->clearResources();
67 }
68
69 ~VLIWResourceModel() {
70 delete ResourcesModel;
71 }
72
73 void resetPacketState() {
74 Packet.clear();
75 }
76
77 void resetDFA() {
78 ResourcesModel->clearResources();
79 }
80
81 void reset() {
82 Packet.clear();
83 ResourcesModel->clearResources();
84 }
85
86 bool isResourceAvailable(SUnit *SU);
87 bool reserveResources(SUnit *SU);
88 unsigned getTotalPackets() const { return TotalPackets; }
89};
90
Andrew Trick7a8e1002012-09-11 00:39:15 +000091/// Extend the standard ScheduleDAGMI to provide more context and override the
92/// top-level schedule() driver.
Andrew Trickd7f890e2013-12-28 21:56:47 +000093class VLIWMachineScheduler : public ScheduleDAGMILive {
Sergei Larinef4cc112012-09-10 17:31:34 +000094public:
David Blaikie422b93d2014-04-21 20:32:32 +000095 VLIWMachineScheduler(MachineSchedContext *C,
96 std::unique_ptr<MachineSchedStrategy> S)
97 : ScheduleDAGMILive(C, std::move(S)) {}
Sergei Larinef4cc112012-09-10 17:31:34 +000098
99 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
100 /// time to do some work.
Craig Topperfd38cbe2014-08-30 16:48:34 +0000101 void schedule() override;
Alp Tokercf218752014-06-30 18:57:16 +0000102 /// Perform platform-specific DAG postprocessing.
Sergei Larin2db64a72012-09-14 15:07:59 +0000103 void postprocessDAG();
Sergei Larinef4cc112012-09-10 17:31:34 +0000104};
105
106/// ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics
107/// to balance the schedule.
Sergei Larin4d8986a2012-09-04 14:49:56 +0000108class ConvergingVLIWScheduler : public MachineSchedStrategy {
109
Sergei Larinef4cc112012-09-10 17:31:34 +0000110 /// Store the state used by ConvergingVLIWScheduler heuristics, required
111 /// for the lifetime of one invocation of pickNode().
Sergei Larin4d8986a2012-09-04 14:49:56 +0000112 struct SchedCandidate {
113 // The best SUnit candidate.
114 SUnit *SU;
115
116 // Register pressure values for the best candidate.
117 RegPressureDelta RPDelta;
118
119 // Best scheduling cost.
120 int SCost;
121
Craig Toppere73658d2014-04-28 04:05:08 +0000122 SchedCandidate(): SU(nullptr), SCost(0) {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000123 };
124 /// Represent the type of SchedCandidate found within a single queue.
125 enum CandResult {
126 NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure,
127 BestCost};
128
129 /// Each Scheduling boundary is associated with ready queues. It tracks the
130 /// current cycle in whichever direction at has moved, and maintains the state
131 /// of "hazards" and other interlocks at the current cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000132 struct VLIWSchedBoundary {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000133 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000134 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000135
136 ReadyQueue Available;
137 ReadyQueue Pending;
138 bool CheckPending;
139
140 ScheduleHazardRecognizer *HazardRec;
Sergei Larinef4cc112012-09-10 17:31:34 +0000141 VLIWResourceModel *ResourceModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000142
143 unsigned CurrCycle;
144 unsigned IssueCount;
145
146 /// MinReadyCycle - Cycle of the soonest available instruction.
147 unsigned MinReadyCycle;
148
149 // Remember the greatest min operand latency.
150 unsigned MaxMinLatency;
151
152 /// Pending queues extend the ready queues with the same ID and the
153 /// PendingFlag set.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000154 VLIWSchedBoundary(unsigned ID, const Twine &Name):
Craig Toppere73658d2014-04-28 04:05:08 +0000155 DAG(nullptr), SchedModel(nullptr), Available(ID, Name+".A"),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000156 Pending(ID << ConvergingVLIWScheduler::LogMaxQID, Name+".P"),
Craig Toppere73658d2014-04-28 04:05:08 +0000157 CheckPending(false), HazardRec(nullptr), ResourceModel(nullptr),
Sergei Larinef4cc112012-09-10 17:31:34 +0000158 CurrCycle(0), IssueCount(0),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000159 MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
160
Andrew Trickd7f890e2013-12-28 21:56:47 +0000161 ~VLIWSchedBoundary() {
Sergei Larinef4cc112012-09-10 17:31:34 +0000162 delete ResourceModel;
163 delete HazardRec;
164 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000165
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000166 void init(VLIWMachineScheduler *dag, const TargetSchedModel *smodel) {
167 DAG = dag;
168 SchedModel = smodel;
169 }
170
Sergei Larin4d8986a2012-09-04 14:49:56 +0000171 bool isTop() const {
172 return Available.getID() == ConvergingVLIWScheduler::TopQID;
173 }
174
175 bool checkHazard(SUnit *SU);
176
177 void releaseNode(SUnit *SU, unsigned ReadyCycle);
178
179 void bumpCycle();
180
181 void bumpNode(SUnit *SU);
182
183 void releasePending();
184
185 void removeReady(SUnit *SU);
186
187 SUnit *pickOnlyChoice();
188 };
189
190 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000191 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000192
193 // State of the top and bottom scheduled instruction boundaries.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000194 VLIWSchedBoundary Top;
195 VLIWSchedBoundary Bot;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000196
197public:
198 /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
199 enum {
200 TopQID = 1,
201 BotQID = 2,
202 LogMaxQID = 2
203 };
204
Craig Toppere73658d2014-04-28 04:05:08 +0000205 ConvergingVLIWScheduler()
206 : DAG(nullptr), SchedModel(nullptr), Top(TopQID, "TopQ"),
207 Bot(BotQID, "BotQ") {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000208
Craig Topperfd38cbe2014-08-30 16:48:34 +0000209 void initialize(ScheduleDAGMI *dag) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000210
Craig Topperfd38cbe2014-08-30 16:48:34 +0000211 SUnit *pickNode(bool &IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000212
Craig Topperfd38cbe2014-08-30 16:48:34 +0000213 void schedNode(SUnit *SU, bool IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000214
Craig Topperfd38cbe2014-08-30 16:48:34 +0000215 void releaseTopNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000216
Craig Topperfd38cbe2014-08-30 16:48:34 +0000217 void releaseBottomNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000218
Sergei Larin2db64a72012-09-14 15:07:59 +0000219 unsigned ReportPackets() {
220 return Top.ResourceModel->getTotalPackets() +
221 Bot.ResourceModel->getTotalPackets();
222 }
223
Sergei Larin4d8986a2012-09-04 14:49:56 +0000224protected:
225 SUnit *pickNodeBidrectional(bool &IsTopNode);
226
227 int SchedulingCost(ReadyQueue &Q,
228 SUnit *SU, SchedCandidate &Candidate,
229 RegPressureDelta &Delta, bool verbose);
230
231 CandResult pickNodeFromQueue(ReadyQueue &Q,
232 const RegPressureTracker &RPTracker,
233 SchedCandidate &Candidate);
234#ifndef NDEBUG
235 void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
Andrew Trick1a831342013-08-30 03:49:48 +0000236 PressureChange P = PressureChange());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000237#endif
238};
239
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000240} // namespace
Sergei Larin4d8986a2012-09-04 14:49:56 +0000241
242
243#endif