blob: 018653cca6055a1507faa03f7963f29edc34e1e7 [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
14#ifndef HEXAGONASMPRINTER_H
15#define HEXAGONASMPRINTER_H
16
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:
Andrew Trickdd79f0f2012-10-10 05:43:09 +000057VLIWResourceModel(const TargetMachine &TM, const TargetSchedModel *SM) :
58 SchedModel(SM), TotalPackets(0) {
Eric Christopherd9134482014-08-04 21:25:23 +000059 ResourcesModel =
60 TM.getSubtargetImpl()->getInstrInfo()->CreateTargetScheduleState(&TM,
61 nullptr);
Sergei Larinef4cc112012-09-10 17:31:34 +000062
63 // This hard requirement could be relaxed,
64 // but for now do not let it proceed.
65 assert(ResourcesModel && "Unimplemented CreateTargetScheduleState.");
66
Andrew Trickdd79f0f2012-10-10 05:43:09 +000067 Packet.resize(SchedModel->getIssueWidth());
Sergei Larinef4cc112012-09-10 17:31:34 +000068 Packet.clear();
69 ResourcesModel->clearResources();
70 }
71
72 ~VLIWResourceModel() {
73 delete ResourcesModel;
74 }
75
76 void resetPacketState() {
77 Packet.clear();
78 }
79
80 void resetDFA() {
81 ResourcesModel->clearResources();
82 }
83
84 void reset() {
85 Packet.clear();
86 ResourcesModel->clearResources();
87 }
88
89 bool isResourceAvailable(SUnit *SU);
90 bool reserveResources(SUnit *SU);
91 unsigned getTotalPackets() const { return TotalPackets; }
92};
93
Andrew Trick7a8e1002012-09-11 00:39:15 +000094/// Extend the standard ScheduleDAGMI to provide more context and override the
95/// top-level schedule() driver.
Andrew Trickd7f890e2013-12-28 21:56:47 +000096class VLIWMachineScheduler : public ScheduleDAGMILive {
Sergei Larinef4cc112012-09-10 17:31:34 +000097public:
David Blaikie422b93d2014-04-21 20:32:32 +000098 VLIWMachineScheduler(MachineSchedContext *C,
99 std::unique_ptr<MachineSchedStrategy> S)
100 : ScheduleDAGMILive(C, std::move(S)) {}
Sergei Larinef4cc112012-09-10 17:31:34 +0000101
102 /// Schedule - This is called back from ScheduleDAGInstrs::Run() when it's
103 /// time to do some work.
Craig Topper73156022014-03-02 09:09:27 +0000104 virtual void schedule() override;
Alp Tokercf218752014-06-30 18:57:16 +0000105 /// Perform platform-specific DAG postprocessing.
Sergei Larin2db64a72012-09-14 15:07:59 +0000106 void postprocessDAG();
Sergei Larinef4cc112012-09-10 17:31:34 +0000107};
108
109/// ConvergingVLIWScheduler shrinks the unscheduled zone using heuristics
110/// to balance the schedule.
Sergei Larin4d8986a2012-09-04 14:49:56 +0000111class ConvergingVLIWScheduler : public MachineSchedStrategy {
112
Sergei Larinef4cc112012-09-10 17:31:34 +0000113 /// Store the state used by ConvergingVLIWScheduler heuristics, required
114 /// for the lifetime of one invocation of pickNode().
Sergei Larin4d8986a2012-09-04 14:49:56 +0000115 struct SchedCandidate {
116 // The best SUnit candidate.
117 SUnit *SU;
118
119 // Register pressure values for the best candidate.
120 RegPressureDelta RPDelta;
121
122 // Best scheduling cost.
123 int SCost;
124
Craig Toppere73658d2014-04-28 04:05:08 +0000125 SchedCandidate(): SU(nullptr), SCost(0) {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000126 };
127 /// Represent the type of SchedCandidate found within a single queue.
128 enum CandResult {
129 NoCand, NodeOrder, SingleExcess, SingleCritical, SingleMax, MultiPressure,
130 BestCost};
131
132 /// Each Scheduling boundary is associated with ready queues. It tracks the
133 /// current cycle in whichever direction at has moved, and maintains the state
134 /// of "hazards" and other interlocks at the current cycle.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000135 struct VLIWSchedBoundary {
Sergei Larin4d8986a2012-09-04 14:49:56 +0000136 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000137 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000138
139 ReadyQueue Available;
140 ReadyQueue Pending;
141 bool CheckPending;
142
143 ScheduleHazardRecognizer *HazardRec;
Sergei Larinef4cc112012-09-10 17:31:34 +0000144 VLIWResourceModel *ResourceModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000145
146 unsigned CurrCycle;
147 unsigned IssueCount;
148
149 /// MinReadyCycle - Cycle of the soonest available instruction.
150 unsigned MinReadyCycle;
151
152 // Remember the greatest min operand latency.
153 unsigned MaxMinLatency;
154
155 /// Pending queues extend the ready queues with the same ID and the
156 /// PendingFlag set.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000157 VLIWSchedBoundary(unsigned ID, const Twine &Name):
Craig Toppere73658d2014-04-28 04:05:08 +0000158 DAG(nullptr), SchedModel(nullptr), Available(ID, Name+".A"),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000159 Pending(ID << ConvergingVLIWScheduler::LogMaxQID, Name+".P"),
Craig Toppere73658d2014-04-28 04:05:08 +0000160 CheckPending(false), HazardRec(nullptr), ResourceModel(nullptr),
Sergei Larinef4cc112012-09-10 17:31:34 +0000161 CurrCycle(0), IssueCount(0),
Sergei Larin4d8986a2012-09-04 14:49:56 +0000162 MinReadyCycle(UINT_MAX), MaxMinLatency(0) {}
163
Andrew Trickd7f890e2013-12-28 21:56:47 +0000164 ~VLIWSchedBoundary() {
Sergei Larinef4cc112012-09-10 17:31:34 +0000165 delete ResourceModel;
166 delete HazardRec;
167 }
Sergei Larin4d8986a2012-09-04 14:49:56 +0000168
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000169 void init(VLIWMachineScheduler *dag, const TargetSchedModel *smodel) {
170 DAG = dag;
171 SchedModel = smodel;
172 }
173
Sergei Larin4d8986a2012-09-04 14:49:56 +0000174 bool isTop() const {
175 return Available.getID() == ConvergingVLIWScheduler::TopQID;
176 }
177
178 bool checkHazard(SUnit *SU);
179
180 void releaseNode(SUnit *SU, unsigned ReadyCycle);
181
182 void bumpCycle();
183
184 void bumpNode(SUnit *SU);
185
186 void releasePending();
187
188 void removeReady(SUnit *SU);
189
190 SUnit *pickOnlyChoice();
191 };
192
193 VLIWMachineScheduler *DAG;
Andrew Trickdd79f0f2012-10-10 05:43:09 +0000194 const TargetSchedModel *SchedModel;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000195
196 // State of the top and bottom scheduled instruction boundaries.
Andrew Trickd7f890e2013-12-28 21:56:47 +0000197 VLIWSchedBoundary Top;
198 VLIWSchedBoundary Bot;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000199
200public:
201 /// SUnit::NodeQueueId: 0 (none), 1 (top), 2 (bot), 3 (both)
202 enum {
203 TopQID = 1,
204 BotQID = 2,
205 LogMaxQID = 2
206 };
207
Craig Toppere73658d2014-04-28 04:05:08 +0000208 ConvergingVLIWScheduler()
209 : DAG(nullptr), SchedModel(nullptr), Top(TopQID, "TopQ"),
210 Bot(BotQID, "BotQ") {}
Sergei Larin4d8986a2012-09-04 14:49:56 +0000211
Craig Topper73156022014-03-02 09:09:27 +0000212 virtual void initialize(ScheduleDAGMI *dag) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000213
Craig Topper73156022014-03-02 09:09:27 +0000214 virtual SUnit *pickNode(bool &IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000215
Craig Topper73156022014-03-02 09:09:27 +0000216 virtual void schedNode(SUnit *SU, bool IsTopNode) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000217
Craig Topper73156022014-03-02 09:09:27 +0000218 virtual void releaseTopNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000219
Craig Topper73156022014-03-02 09:09:27 +0000220 virtual void releaseBottomNode(SUnit *SU) override;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000221
Sergei Larin2db64a72012-09-14 15:07:59 +0000222 unsigned ReportPackets() {
223 return Top.ResourceModel->getTotalPackets() +
224 Bot.ResourceModel->getTotalPackets();
225 }
226
Sergei Larin4d8986a2012-09-04 14:49:56 +0000227protected:
228 SUnit *pickNodeBidrectional(bool &IsTopNode);
229
230 int SchedulingCost(ReadyQueue &Q,
231 SUnit *SU, SchedCandidate &Candidate,
232 RegPressureDelta &Delta, bool verbose);
233
234 CandResult pickNodeFromQueue(ReadyQueue &Q,
235 const RegPressureTracker &RPTracker,
236 SchedCandidate &Candidate);
237#ifndef NDEBUG
238 void traceCandidate(const char *Label, const ReadyQueue &Q, SUnit *SU,
Andrew Trick1a831342013-08-30 03:49:48 +0000239 PressureChange P = PressureChange());
Sergei Larin4d8986a2012-09-04 14:49:56 +0000240#endif
241};
242
Sergei Larin4d8986a2012-09-04 14:49:56 +0000243} // namespace
244
245
246#endif