blob: 2a2ef27a29f586c705028b2041ba3e88833fa0f9 [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattneraf50d002002-04-09 05:45:58 +00009//
10// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
11// generic support routines for instruction scheduling.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +000014
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000015#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000016#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000019#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000021#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000022#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000023#include <algorithm>
Vikram S. Advec5b46322001-09-30 23:43:34 +000024
Chris Lattner70e60cb2002-05-22 17:08:27 +000025SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000026
Vikram S. Advebed4eff2003-09-16 05:55:15 +000027static cl::opt<bool> EnableFillingDelaySlots("sched-fill-delay-slots",
28 cl::desc("Fill branch delay slots during local scheduling"));
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::opt<SchedDebugLevel_t, true>
31SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
32 cl::desc("enable instruction scheduling debugging information"),
33 cl::values(
34 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000035 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
36 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
37 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
38 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000039
40
Vikram S. Advec5b46322001-09-30 23:43:34 +000041//************************* Internal Data Types *****************************/
42
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000043class InstrSchedule;
44class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000045
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000046
47//----------------------------------------------------------------------
48// class InstrGroup:
49//
50// Represents a group of instructions scheduled to be issued
51// in a single cycle.
52//----------------------------------------------------------------------
53
Chris Lattnere3561c22003-08-15 05:20:06 +000054class InstrGroup {
55 InstrGroup(const InstrGroup&); // DO NOT IMPLEMENT
56 void operator=(const InstrGroup&); // DO NOT IMPLEMENT
57
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000058public:
59 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
60 assert(slotNum < group.size());
61 return group[slotNum];
62 }
63
64private:
65 friend class InstrSchedule;
66
67 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
68 assert(slotNum < group.size());
69 group[slotNum] = node;
70 }
71
72 /*ctor*/ InstrGroup(unsigned int nslots)
73 : group(nslots, NULL) {}
74
75 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
76
77private:
Misha Brukmanc2312df2003-05-22 21:24:35 +000078 std::vector<const SchedGraphNode*> group;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000079};
80
81
82//----------------------------------------------------------------------
83// class ScheduleIterator:
84//
85// Iterates over the machine instructions in the for a single basic block.
86// The schedule is represented by an InstrSchedule object.
87//----------------------------------------------------------------------
88
89template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000090class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000091private:
92 unsigned cycleNum;
93 unsigned slotNum;
94 const InstrSchedule& S;
95public:
96 typedef ScheduleIterator<_NodeType> _Self;
97
98 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
99 unsigned _cycleNum,
100 unsigned _slotNum)
101 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
102 skipToNextInstr();
103 }
104
105 /*ctor*/ inline ScheduleIterator(const _Self& x)
106 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
107
108 inline bool operator==(const _Self& x) const {
109 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
110 }
111
112 inline bool operator!=(const _Self& x) const { return !operator==(x); }
113
114 inline _NodeType* operator*() const {
115 assert(cycleNum < S.groups.size());
116 return (*S.groups[cycleNum])[slotNum];
117 }
118 inline _NodeType* operator->() const { return operator*(); }
119
120 _Self& operator++(); // Preincrement
121 inline _Self operator++(int) { // Postincrement
122 _Self tmp(*this); ++*this; return tmp;
123 }
124
125 static _Self begin(const InstrSchedule& _schedule);
126 static _Self end( const InstrSchedule& _schedule);
127
128private:
129 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
130 void skipToNextInstr();
131};
132
133
134//----------------------------------------------------------------------
135// class InstrSchedule:
136//
137// Represents the schedule of machine instructions for a single basic block.
138//----------------------------------------------------------------------
139
Chris Lattnere3561c22003-08-15 05:20:06 +0000140class InstrSchedule {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000141 const unsigned int nslots;
142 unsigned int numInstr;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000143 std::vector<InstrGroup*> groups; // indexed by cycle number
144 std::vector<cycles_t> startTime; // indexed by node id
Chris Lattnere3561c22003-08-15 05:20:06 +0000145
146 InstrSchedule(InstrSchedule&); // DO NOT IMPLEMENT
147 void operator=(InstrSchedule&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000148
149public: // iterators
150 typedef ScheduleIterator<SchedGraphNode> iterator;
151 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
152
153 iterator begin();
154 const_iterator begin() const;
155 iterator end();
156 const_iterator end() const;
157
158public: // constructors and destructor
159 /*ctor*/ InstrSchedule (unsigned int _nslots,
160 unsigned int _numNodes);
161 /*dtor*/ ~InstrSchedule ();
162
163public: // accessor functions to query chosen schedule
164 const SchedGraphNode* getInstr (unsigned int slotNum,
165 cycles_t c) const {
166 const InstrGroup* igroup = this->getIGroup(c);
167 return (igroup == NULL)? NULL : (*igroup)[slotNum];
168 }
169
170 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000171 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000172 groups.resize(c+1);
173 if (groups[c] == NULL)
174 groups[c] = new InstrGroup(nslots);
175 return groups[c];
176 }
177
178 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000179 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000180 return groups[c];
181 }
182
183 inline cycles_t getStartTime (unsigned int nodeId) const {
184 assert(nodeId < startTime.size());
185 return startTime[nodeId];
186 }
187
188 unsigned int getNumInstructions() const {
189 return numInstr;
190 }
191
192 inline void scheduleInstr (const SchedGraphNode* node,
193 unsigned int slotNum,
194 cycles_t cycle) {
195 InstrGroup* igroup = this->getIGroup(cycle);
196 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
197 igroup->addInstr(node, slotNum);
198 assert(node->getNodeId() < startTime.size());
199 startTime[node->getNodeId()] = cycle;
200 ++numInstr;
201 }
202
203private:
204 friend class iterator;
205 friend class const_iterator;
206 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
207};
208
209
210/*ctor*/
211InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
212 : nslots(_nslots),
213 numInstr(0),
214 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
215 startTime(_numNodes, (cycles_t) -1) // set all to -1
216{
217}
218
219
220/*dtor*/
221InstrSchedule::~InstrSchedule()
222{
223 for (unsigned c=0, NC=groups.size(); c < NC; c++)
224 if (groups[c] != NULL)
225 delete groups[c]; // delete InstrGroup objects
226}
227
228
229template<class _NodeType>
230inline
231void
232ScheduleIterator<_NodeType>::skipToNextInstr()
233{
234 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
235 ++cycleNum; // skip cycles with no instructions
236
237 while (cycleNum < S.groups.size() &&
238 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000239 {
240 ++slotNum;
241 if (slotNum == S.nslots) {
242 ++cycleNum;
243 slotNum = 0;
244 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
245 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000246 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000247 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000248}
249
250template<class _NodeType>
251inline
252ScheduleIterator<_NodeType>&
253ScheduleIterator<_NodeType>::operator++() // Preincrement
254{
255 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000256 if (slotNum == S.nslots) {
257 ++cycleNum;
258 slotNum = 0;
259 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000260 skipToNextInstr();
261 return *this;
262}
263
264template<class _NodeType>
265ScheduleIterator<_NodeType>
266ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
267{
268 return _Self(_schedule, 0, 0);
269}
270
271template<class _NodeType>
272ScheduleIterator<_NodeType>
273ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
274{
275 return _Self(_schedule, _schedule.groups.size(), 0);
276}
277
278InstrSchedule::iterator
279InstrSchedule::begin()
280{
281 return iterator::begin(*this);
282}
283
284InstrSchedule::const_iterator
285InstrSchedule::begin() const
286{
287 return const_iterator::begin(*this);
288}
289
290InstrSchedule::iterator
291InstrSchedule::end()
292{
293 return iterator::end(*this);
294}
295
296InstrSchedule::const_iterator
297InstrSchedule::end() const
298{
299 return const_iterator::end( *this);
300}
301
302
303//----------------------------------------------------------------------
304// class DelaySlotInfo:
305//
306// Record information about delay slots for a single branch instruction.
307// Delay slots are simply indexed by slot number 1 ... numDelaySlots
308//----------------------------------------------------------------------
309
Chris Lattnere3561c22003-08-15 05:20:06 +0000310class DelaySlotInfo {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000311 const SchedGraphNode* brNode;
Chris Lattnere3561c22003-08-15 05:20:06 +0000312 unsigned ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000313 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000314 cycles_t delayedNodeCycle;
Chris Lattnere3561c22003-08-15 05:20:06 +0000315 unsigned delayedNodeSlotNum;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000316
Chris Lattnere3561c22003-08-15 05:20:06 +0000317 DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT
318 void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000319public:
320 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
321 unsigned _ndelays)
322 : brNode(_brNode), ndelays(_ndelays),
323 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
324
325 inline unsigned getNumDelays () {
326 return ndelays;
327 }
328
Misha Brukmanc2312df2003-05-22 21:24:35 +0000329 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000330 return delayNodeVec;
331 }
332
333 inline void addDelayNode (const SchedGraphNode* node) {
334 delayNodeVec.push_back(node);
335 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
336 }
337
338 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
339 delayedNodeCycle = cycle;
340 delayedNodeSlotNum = slotNum;
341 }
342
Vikram S. Advec5b46322001-09-30 23:43:34 +0000343 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000344};
345
346
347//----------------------------------------------------------------------
348// class SchedulingManager:
349//
350// Represents the schedule of machine instructions for a single basic block.
351//----------------------------------------------------------------------
352
Chris Lattnere3561c22003-08-15 05:20:06 +0000353class SchedulingManager {
354 SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT
355 void operator=(const SchedulingManager &); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000356public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000357 const unsigned nslots;
358 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000359 SchedPriorities& schedPrio;
360 InstrSchedule isched;
361
362private:
Chris Lattnere3561c22003-08-15 05:20:06 +0000363 unsigned totalInstrCount;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000364 cycles_t curTime;
365 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000366 // indexed by slot#
367 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
368 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
369 std::vector<int> numInClass; // indexed by sched class
370 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000371 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000372 // indexed by branch node ptr
373
374public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000375 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
376 SchedPriorities& schedPrio);
377 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000378 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000379 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
380 E = delaySlotInfoForBranches.end(); I != E; ++I)
381 delete I->second;
382 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000383
384 //----------------------------------------------------------------------
385 // Simplify access to the machine instruction info
386 //----------------------------------------------------------------------
387
Chris Lattner3501fea2003-01-14 22:00:31 +0000388 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000389 return schedInfo.getInstrInfo();
390 }
391
392 //----------------------------------------------------------------------
393 // Interface for checking and updating the current time
394 //----------------------------------------------------------------------
395
396 inline cycles_t getTime () const {
397 return curTime;
398 }
399
400 inline cycles_t getEarliestIssueTime() const {
401 return nextEarliestIssueTime;
402 }
403
404 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
405 assert(opCode < (int) nextEarliestStartTime.size());
406 return nextEarliestStartTime[opCode];
407 }
408
409 // Update current time to specified cycle
410 inline void updateTime (cycles_t c) {
411 curTime = c;
412 schedPrio.updateTime(c);
413 }
414
415 //----------------------------------------------------------------------
416 // Functions to manage the choices for the current cycle including:
417 // -- a vector of choices by priority (choiceVec)
418 // -- vectors of the choices for each instruction slot (choicesForSlot[])
419 // -- number of choices in each sched class, used to check issue conflicts
420 // between choices for a single cycle
421 //----------------------------------------------------------------------
422
423 inline unsigned int getNumChoices () const {
424 return choiceVec.size();
425 }
426
427 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000428 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000429 return numInClass[sc];
430 }
431
432 inline const SchedGraphNode* getChoice(unsigned int i) const {
433 // assert(i < choiceVec.size()); don't check here.
434 return choiceVec[i];
435 }
436
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000437 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000438 assert(slotNum < nslots);
439 return choicesForSlot[slotNum];
440 }
441
442 inline void addChoice (const SchedGraphNode* node) {
443 // Append the instruction to the vector of choices for current cycle.
444 // Increment numInClass[c] for the sched class to which the instr belongs.
445 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000446 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000447 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000448 numInClass[sc]++;
449 }
450
451 inline void addChoiceToSlot (unsigned int slotNum,
452 const SchedGraphNode* node) {
453 // Add the instruction to the choice set for the specified slot
454 assert(slotNum < nslots);
455 choicesForSlot[slotNum].insert(node);
456 }
457
458 inline void resetChoices () {
459 choiceVec.clear();
460 for (unsigned int s=0; s < nslots; s++)
461 choicesForSlot[s].clear();
462 for (unsigned int c=0; c < numInClass.size(); c++)
463 numInClass[c] = 0;
464 }
465
466 //----------------------------------------------------------------------
467 // Code to query and manage the partial instruction schedule so far
468 //----------------------------------------------------------------------
469
470 inline unsigned int getNumScheduled () const {
471 return isched.getNumInstructions();
472 }
473
474 inline unsigned int getNumUnscheduled() const {
475 return totalInstrCount - isched.getNumInstructions();
476 }
477
478 inline bool isScheduled (const SchedGraphNode* node) const {
479 return (isched.getStartTime(node->getNodeId()) >= 0);
480 }
481
482 inline void scheduleInstr (const SchedGraphNode* node,
483 unsigned int slotNum,
484 cycles_t cycle)
485 {
486 assert(! isScheduled(node) && "Instruction already scheduled?");
487
488 // add the instruction to the schedule
489 isched.scheduleInstr(node, slotNum, cycle);
490
491 // update the earliest start times of all nodes that conflict with `node'
492 // and the next-earliest time anything can issue if `node' causes bubbles
493 updateEarliestStartTimes(node, cycle);
494
495 // remove the instruction from the choice sets for all slots
496 for (unsigned s=0; s < nslots; s++)
497 choicesForSlot[s].erase(node);
498
499 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000500 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000501 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000502 numInClass[sc]--;
503 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000504
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000505 //----------------------------------------------------------------------
506 // Create and retrieve delay slot info for delayed instructions
507 //----------------------------------------------------------------------
508
509 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
510 bool createIfMissing=false)
511 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000512 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000513 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000514 if (I != delaySlotInfoForBranches.end())
515 return I->second;
516
517 if (!createIfMissing) return 0;
518
519 DelaySlotInfo *dinfo =
520 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
521 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000522 }
523
524private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000525 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
526 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000527};
528
529
530/*ctor*/
531SchedulingManager::SchedulingManager(const TargetMachine& target,
532 const SchedGraph* graph,
533 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000534 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
535 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000536 schedPrio(_schedPrio),
537 isched(nslots, graph->getNumNodes()),
538 totalInstrCount(graph->getNumNodes() - 2),
539 nextEarliestIssueTime(0),
540 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000541 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000542 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
543 (cycles_t) 0) // set all to 0
544{
545 updateTime(0);
546
547 // Note that an upper bound on #choices for each slot is = nslots since
548 // we use this vector to hold a feasible set of instructions, and more
549 // would be infeasible. Reserve that much memory since it is probably small.
550 for (unsigned int i=0; i < nslots; i++)
551 choicesForSlot[i].resize(nslots);
552}
553
554
555void
556SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
557 cycles_t schedTime)
558{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000559 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000560 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000561 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000562 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000563 }
564
Vikram S. Adve1632e882002-10-13 00:40:37 +0000565 const std::vector<MachineOpCode>&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000566 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000567
Vikram S. Adve1632e882002-10-13 00:40:37 +0000568 for (unsigned i=0; i < conflictVec.size(); i++)
569 {
570 MachineOpCode toOp = conflictVec[i];
571 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp);
572 assert(toOp < (int) nextEarliestStartTime.size());
573 if (nextEarliestStartTime[toOp] < est)
574 nextEarliestStartTime[toOp] = est;
575 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000576}
577
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000578//************************* Internal Functions *****************************/
579
580
581static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000582AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000583{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000584 // find the slot to start from, in the current cycle
585 unsigned int startSlot = 0;
586 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000587
Vikram S. Advec5b46322001-09-30 23:43:34 +0000588 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000589
Vikram S. Advec5b46322001-09-30 23:43:34 +0000590 // If only one instruction can be issued, do so.
591 if (maxIssue == 1)
592 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000593 if (S.getChoicesForSlot(s).size() > 0) {
594 // found the one instruction
595 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
596 return;
597 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000598
599 // Otherwise, choose from the choices for each slot
600 //
601 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
602 assert(igroup != NULL && "Group creation failed?");
603
604 // Find a slot that has only a single choice, and take it.
605 // If all slots have 0 or multiple choices, pick the first slot with
606 // choices and use its last instruction (just to avoid shifting the vector).
607 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000608 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
609 int chosenSlot = -1;
610 for (unsigned s=startSlot; s < S.nslots; s++)
611 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
612 chosenSlot = (int) s;
613 break;
614 }
615
616 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000617 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000618 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
619 chosenSlot = (int) s;
620 break;
621 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000622
Misha Brukman6b77ec42003-05-22 21:49:18 +0000623 if (chosenSlot != -1) {
624 // Insert the chosen instr in the chosen slot and
625 // erase it from all slots.
626 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
627 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000628 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000629 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000630
631 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000632}
633
634
635//
636// For now, just assume we are scheduling within a single basic block.
637// Get the machine instruction vector for the basic block and clear it,
638// then append instructions in scheduled order.
639// Also, re-insert the dummy PHI instructions that were at the beginning
640// of the basic block, since they are not part of the schedule.
641//
642static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000643RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000644{
Chris Lattner3501fea2003-01-14 22:00:31 +0000645 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000646
647#ifndef NDEBUG
648 // Lets make sure we didn't lose any instructions, except possibly
649 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
650 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000651 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000652 if (! mii.isNop((*I)->getOpCode()) &&
653 ! mii.isDummyPhiInstr((*I)->getOpCode()))
654 ++numInstr;
655 assert(S.isched.getNumInstructions() >= numInstr &&
656 "Lost some non-NOP instructions during scheduling!");
657#endif
658
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000659 if (S.isched.getNumInstructions() == 0)
660 return; // empty basic block!
661
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000662 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000663 MachineBasicBlock::iterator I = MBB.begin();
664 for ( ; I != MBB.end(); ++I)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000665 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
666 break;
667
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000668 // Erase all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000669 // pre-allocate create space for the ones we will put back in.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000670 MBB.erase(I, MBB.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000671
672 InstrSchedule::const_iterator NIend = S.isched.end();
673 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000674 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000675}
676
677
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000678
679static void
680MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
681{
682 // Check if any successors are now ready that were not already marked
683 // ready before, and that have not yet been scheduled.
684 //
685 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
686 if (! (*SI)->isDummyNode()
687 && ! S.isScheduled(*SI)
688 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000689 {
690 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000691
Misha Brukman6b77ec42003-05-22 21:49:18 +0000692 bool succIsReady = true;
693 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
694 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
695 succIsReady = false;
696 break;
697 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000698
Misha Brukman6b77ec42003-05-22 21:49:18 +0000699 if (succIsReady) // add the successor to the ready list
700 S.schedPrio.insertReady(*SI);
701 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000702}
703
704
705// Choose up to `nslots' FEASIBLE instructions and assign each
706// instruction to all possible slots that do not violate feasibility.
707// FEASIBLE means it should be guaranteed that the set
708// of chosen instructions can be issued in a single group.
709//
710// Return value:
711// maxIssue : total number of feasible instructions
712// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
713//
714static unsigned
715FindSlotChoices(SchedulingManager& S,
716 DelaySlotInfo*& getDelaySlotInfo)
717{
718 // initialize result vectors to empty
719 S.resetChoices();
720
721 // find the slot to start from, in the current cycle
722 unsigned int startSlot = 0;
723 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
724 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000725 if ((*igroup)[s] != NULL) {
726 startSlot = s+1;
727 break;
728 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000729
730 // Make sure we pick at most one instruction that would break the group.
731 // Also, if we do pick one, remember which it was.
732 unsigned int indexForBreakingNode = S.nslots;
733 unsigned int indexForDelayedInstr = S.nslots;
734 DelaySlotInfo* delaySlotInfo = NULL;
735
736 getDelaySlotInfo = NULL;
737
738 // Choose instructions in order of priority.
739 // Add choices to the choice vector in the SchedulingManager class as
740 // we choose them so that subsequent choices will be correctly tested
741 // for feasibility, w.r.t. higher priority choices for the same cycle.
742 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000743 while (S.getNumChoices() < S.nslots - startSlot) {
744 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
745 if (nextNode == NULL)
746 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000747
Misha Brukman6b77ec42003-05-22 21:49:18 +0000748 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0) {
749 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
750 if (delaySlotInfo != NULL) {
751 if (indexForBreakingNode < S.nslots)
752 // cannot issue a delayed instr in the same cycle as one
753 // that breaks the issue group or as another delayed instr
754 nextNode = NULL;
755 else
756 indexForDelayedInstr = S.getNumChoices();
757 }
758 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode())) {
759 if (indexForBreakingNode < S.nslots)
760 // have a breaking instruction already so throw this one away
761 nextNode = NULL;
762 else
763 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000764 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000765
766 if (nextNode != NULL) {
767 S.addChoice(nextNode);
768
769 if (S.schedInfo.isSingleIssue(nextNode->getOpCode())) {
770 assert(S.getNumChoices() == 1 &&
771 "Prioritizer returned invalid instr for this cycle!");
772 break;
773 }
774 }
775
776 if (indexForDelayedInstr < S.nslots)
777 break; // leave the rest for delay slots
778 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000779
780 assert(S.getNumChoices() <= S.nslots);
781 assert(! (indexForDelayedInstr < S.nslots &&
782 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
783
784 // Assign each chosen instruction to all possible slots for that instr.
785 // But if only one instruction was chosen, put it only in the first
786 // feasible slot; no more analysis will be needed.
787 //
788 if (indexForDelayedInstr >= S.nslots &&
789 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000790 { // No instructions that break the issue group or that have delay slots.
791 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000792
Misha Brukman6b77ec42003-05-22 21:49:18 +0000793 if (S.getNumChoices() == 1) {
794 MachineOpCode opCode = S.getChoice(0)->getOpCode();
795 unsigned int s;
796 for (s=startSlot; s < S.nslots; s++)
797 if (S.schedInfo.instrCanUseSlot(opCode, s))
798 break;
799 assert(s < S.nslots && "No feasible slot for this opCode?");
800 S.addChoiceToSlot(s, S.getChoice(0));
801 } else {
802 for (unsigned i=0; i < S.getNumChoices(); i++) {
803 MachineOpCode opCode = S.getChoice(i)->getOpCode();
804 for (unsigned int s=startSlot; s < S.nslots; s++)
805 if (S.schedInfo.instrCanUseSlot(opCode, s))
806 S.addChoiceToSlot(s, S.getChoice(i));
807 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000808 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000809 } else if (indexForDelayedInstr < S.nslots) {
810 // There is an instruction that needs delay slots.
811 // Try to assign that instruction to a higher slot than any other
812 // instructions in the group, so that its delay slots can go
813 // right after it.
814 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000815
Misha Brukman6b77ec42003-05-22 21:49:18 +0000816 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
817 "Instruction with delay slots should be last choice!");
818 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000819
Misha Brukman6b77ec42003-05-22 21:49:18 +0000820 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
821 MachineOpCode delayOpCode = delayedNode->getOpCode();
822 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000823
Misha Brukman6b77ec42003-05-22 21:49:18 +0000824 unsigned delayedNodeSlot = S.nslots;
825 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000826
Misha Brukman6b77ec42003-05-22 21:49:18 +0000827 // Find the last possible slot for the delayed instruction that leaves
828 // at least `d' slots vacant after it (d = #delay slots)
829 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
830 if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) {
831 delayedNodeSlot = s;
832 break;
833 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000834
Misha Brukman6b77ec42003-05-22 21:49:18 +0000835 highestSlotUsed = -1;
836 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
837 // Try to assign every other instruction to a lower numbered
838 // slot than delayedNodeSlot.
839 MachineOpCode opCode =S.getChoice(i)->getOpCode();
840 bool noSlotFound = true;
841 unsigned int s;
842 for (s=startSlot; s < delayedNodeSlot; s++)
843 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
844 S.addChoiceToSlot(s, S.getChoice(i));
845 noSlotFound = false;
846 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000847
Misha Brukman6b77ec42003-05-22 21:49:18 +0000848 // No slot before `delayedNodeSlot' was found for this opCode
849 // Use a later slot, and allow some delay slots to fall in
850 // the next cycle.
851 if (noSlotFound)
852 for ( ; s < S.nslots; s++)
853 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
854 S.addChoiceToSlot(s, S.getChoice(i));
855 break;
856 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000857
Misha Brukman6b77ec42003-05-22 21:49:18 +0000858 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000859
Misha Brukman6b77ec42003-05-22 21:49:18 +0000860 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000861 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000862
Misha Brukman6b77ec42003-05-22 21:49:18 +0000863 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
864
865 // We will put the delayed node in the first slot after the
866 // highest slot used. But we just mark that for now, and
867 // schedule it separately because we want to schedule the delay
868 // slots for the node at the same time.
869 cycles_t dcycle = S.getTime();
870 unsigned int dslot = highestSlotUsed + 1;
871 if (dslot == S.nslots) {
872 dslot = 0;
873 ++dcycle;
874 }
875 delaySlotInfo->recordChosenSlot(dcycle, dslot);
876 getDelaySlotInfo = delaySlotInfo;
877 } else {
878 // There is an instruction that breaks the issue group.
879 // For such an instruction, assign to the last possible slot in
880 // the current group, and then don't assign any other instructions
881 // to later slots.
882 assert(indexForBreakingNode < S.nslots);
883 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
884 unsigned breakingSlot = INT_MAX;
885 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000886
Misha Brukman6b77ec42003-05-22 21:49:18 +0000887 // Find the last possible slot for this instruction.
888 for (int s = S.nslots-1; s >= (int) startSlot; s--)
889 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s)) {
890 breakingSlot = s;
891 break;
892 }
893 assert(breakingSlot < S.nslots &&
894 "No feasible slot for `breakingNode'?");
895
896 // Higher priority instructions than the one that breaks the group:
897 // These can be assigned to all slots, but will be assigned only
898 // to earlier slots if possible.
899 for (unsigned i=0;
900 i < S.getNumChoices() && i < indexForBreakingNode; i++)
901 {
902 MachineOpCode opCode =S.getChoice(i)->getOpCode();
903
904 // If a higher priority instruction cannot be assigned to
905 // any earlier slots, don't schedule the breaking instruction.
906 //
907 bool foundLowerSlot = false;
908 nslotsToUse = S.nslots; // May be modified in the loop
909 for (unsigned int s=startSlot; s < nslotsToUse; s++)
910 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
911 if (breakingSlot < S.nslots && s < breakingSlot) {
912 foundLowerSlot = true;
913 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
914 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000915
Misha Brukman6b77ec42003-05-22 21:49:18 +0000916 S.addChoiceToSlot(s, S.getChoice(i));
917 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000918
Misha Brukman6b77ec42003-05-22 21:49:18 +0000919 if (!foundLowerSlot)
920 breakingSlot = INT_MAX; // disable breaking instr
921 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000922
Misha Brukman6b77ec42003-05-22 21:49:18 +0000923 // Assign the breaking instruction (if any) to a single slot
924 // Otherwise, just ignore the instruction. It will simply be
925 // scheduled in a later cycle.
926 if (breakingSlot < S.nslots) {
927 S.addChoiceToSlot(breakingSlot, breakingNode);
928 nslotsToUse = breakingSlot;
929 } else
930 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000931
Misha Brukman6b77ec42003-05-22 21:49:18 +0000932 // For lower priority instructions than the one that breaks the
933 // group, only assign them to slots lower than the breaking slot.
934 // Otherwise, just ignore the instruction.
935 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
936 MachineOpCode opCode = S.getChoice(i)->getOpCode();
937 for (unsigned int s=startSlot; s < nslotsToUse; s++)
938 if (S.schedInfo.instrCanUseSlot(opCode, s))
939 S.addChoiceToSlot(s, S.getChoice(i));
940 }
941 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000942
943 return S.getNumChoices();
944}
945
946
Vikram S. Advec5b46322001-09-30 23:43:34 +0000947static unsigned
948ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000949{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000950 assert(S.schedPrio.getNumReady() > 0
951 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000952
Vikram S. Advec5b46322001-09-30 23:43:34 +0000953 cycles_t firstCycle = S.getTime();
954 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000955
Vikram S. Advec5b46322001-09-30 23:43:34 +0000956 // Choose up to `nslots' feasible instructions and their possible slots.
957 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000958
Misha Brukman6b77ec42003-05-22 21:49:18 +0000959 while (numIssued == 0) {
960 S.updateTime(S.getTime()+1);
961 numIssued = FindSlotChoices(S, getDelaySlotInfo);
962 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000963
Vikram S. Advec5b46322001-09-30 23:43:34 +0000964 AssignInstructionsToSlots(S, numIssued);
965
966 if (getDelaySlotInfo != NULL)
967 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
968
969 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000970 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
971 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
972 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
973 const InstrGroup* igroup = S.isched.getIGroup(c);
974 for (unsigned int s=0; s < S.nslots; s++) {
975 std::cerr << " ";
976 if ((*igroup)[s] != NULL)
977 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
978 else
979 std::cerr << "<none>\n";
980 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000981 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000982 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000983
984 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000985}
986
987
Vikram S. Advec5b46322001-09-30 23:43:34 +0000988static void
989ForwardListSchedule(SchedulingManager& S)
990{
991 unsigned N;
992 const SchedGraphNode* node;
993
994 S.schedPrio.initialize();
995
Misha Brukman6b77ec42003-05-22 21:49:18 +0000996 while ((N = S.schedPrio.getNumReady()) > 0) {
997 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +0000998
Misha Brukman6b77ec42003-05-22 21:49:18 +0000999 // Choose one group of instructions for a cycle, plus any delay slot
1000 // instructions (which may overflow into successive cycles).
1001 // This will advance S.getTime() to the last cycle in which
1002 // instructions are actually issued.
1003 //
1004 unsigned numIssued = ChooseOneGroup(S);
1005 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +00001006
Misha Brukman6b77ec42003-05-22 21:49:18 +00001007 // Notify the priority manager of scheduled instructions and mark
1008 // any successors that may now be ready
1009 //
1010 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
1011 const InstrGroup* igroup = S.isched.getIGroup(c);
1012 for (unsigned int s=0; s < S.nslots; s++)
1013 if ((node = (*igroup)[s]) != NULL) {
1014 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1015 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001016 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001017 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001018
1019 // Move to the next the next earliest cycle for which
1020 // an instruction can be issued, or the next earliest in which
1021 // one will be ready, or to the next cycle, whichever is latest.
1022 //
1023 S.updateTime(std::max(S.getTime() + 1,
1024 std::max(S.getEarliestIssueTime(),
1025 S.schedPrio.getEarliestReadyTime())));
1026 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001027}
1028
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001029
1030//---------------------------------------------------------------------
1031// Code for filling delay slots for delayed terminator instructions
1032// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1033// instructions (e.g., CALL) are not handled here because they almost
1034// always can be filled with instructions from the call sequence code
1035// before a call. That's preferable because we incur many tradeoffs here
1036// when we cannot find single-cycle instructions that can be reordered.
1037//----------------------------------------------------------------------
1038
Vikram S. Advec5b46322001-09-30 23:43:34 +00001039static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001040NodeCanFillDelaySlot(const SchedulingManager& S,
1041 const SchedGraphNode* node,
1042 const SchedGraphNode* brNode,
1043 bool nodeIsPredecessor)
1044{
1045 assert(! node->isDummyNode());
1046
1047 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001048 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001049 return false;
1050
1051 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001052 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001053 return false;
1054
1055 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001056 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001057
1058 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1059 EI != node->endInEdges(); ++EI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001060 if (! ((SchedGraphNode*)(*EI)->getSrc())->isDummyNode()
1061 && mii.isLoad(((SchedGraphNode*)(*EI)->getSrc())->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001062 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1063 return false;
1064
1065 // for now, don't put an instruction that does not have operand
1066 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001067 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001068 return false;
1069
Misha Brukman6eba07a2003-09-17 21:34:23 +00001070 // Finally, if the instruction precedes the branch, we make sure the
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001071 // instruction can be reordered relative to the branch. We simply check
1072 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1073 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001074 if (nodeIsPredecessor) {
1075 bool onlyCDEdgeToBranch = true;
1076 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1077 OEI != node->endOutEdges(); ++OEI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001078 if (! ((SchedGraphNode*)(*OEI)->getSink())->isDummyNode()
Misha Brukman6b77ec42003-05-22 21:49:18 +00001079 && ((*OEI)->getSink() != brNode
1080 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1081 {
1082 onlyCDEdgeToBranch = false;
1083 break;
1084 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001085
Misha Brukman6b77ec42003-05-22 21:49:18 +00001086 if (!onlyCDEdgeToBranch)
1087 return false;
1088 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001089
1090 return true;
1091}
1092
1093
Vikram S. Advec5b46322001-09-30 23:43:34 +00001094static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001095MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001096 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001097 SchedGraphNode* node,
1098 const SchedGraphNode* brNode,
1099 bool nodeIsPredecessor)
1100{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001101 if (nodeIsPredecessor) {
Misha Brukman6eba07a2003-09-17 21:34:23 +00001102 // If node is in the same basic block (i.e., precedes brNode),
Misha Brukman6b77ec42003-05-22 21:49:18 +00001103 // remove it and all its incident edges from the graph. Make sure we
1104 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1105 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1106 } else {
1107 // If the node was from a target block, add the node to the graph
1108 // and add a CD edge from brNode to node.
1109 assert(0 && "NOT IMPLEMENTED YET");
1110 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001111
1112 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1113 dinfo->addDelayNode(node);
1114}
1115
1116
Vikram S. Advec5b46322001-09-30 23:43:34 +00001117void
1118FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1119 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001120 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001121{
Chris Lattner3501fea2003-01-14 22:00:31 +00001122 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001123 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001124 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001125
1126 if (ndelays == 0)
1127 return;
1128
1129 sdelayNodeVec.reserve(ndelays);
1130
1131 // Use a separate vector to hold the feasible multi-cycle nodes.
1132 // These will be used if not enough single-cycle nodes are found.
1133 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001134 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001135
1136 for (sg_pred_iterator P = pred_begin(brNode);
1137 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1138 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001139 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001140 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001141 {
1142 if (mii.maxLatency((*P)->getOpCode()) > 1)
1143 mdelayNodeVec.push_back(*P);
1144 else
1145 sdelayNodeVec.push_back(*P);
1146 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001147
1148 // If not enough single-cycle instructions were found, select the
1149 // lowest-latency multi-cycle instructions and use them.
1150 // Note that this is the most efficient code when only 1 (or even 2)
1151 // values need to be selected.
1152 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001153 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1154 unsigned lmin =
1155 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
1156 unsigned minIndex = 0;
1157 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001158 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001159 unsigned li =
1160 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
1161 if (lmin >= li)
1162 {
1163 lmin = li;
1164 minIndex = i;
1165 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001166 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001167 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1168 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1169 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1170 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001171}
1172
1173
1174// Remove the NOPs currently in delay slots from the graph.
1175// Mark instructions specified in sdelayNodeVec to replace them.
1176// If not enough useful instructions were found, mark the NOPs to be used
1177// for filling delay slots, otherwise, otherwise just discard them.
1178//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001179static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1180 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001181 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001182 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001183 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001184{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001185 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001186 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001187 const MachineInstr* brInstr = node->getMachineInstr();
1188 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001189 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1190
1191 // Remove the NOPs currently in delay slots from the graph.
1192 // If not enough useful instructions were found, use the NOPs to
1193 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001194 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001195 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001196 MachineBasicBlock& MBB = node->getMachineBasicBlock();
1197 assert(MBB[firstDelaySlotIdx - 1] == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001198 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001199
1200 // First find all useful instructions already in the delay slots
1201 // and USE THEM. We'll throw away the unused alternatives below
1202 //
1203 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001204 if (! mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001205 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001206 graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001207
1208 // Then find the NOPs and keep only as many as are needed.
1209 // Put the rest in nopNodeVec to be deleted.
1210 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001211 if (mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001212 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001213 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001214 else {
1215 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001216
Misha Brukman6b77ec42003-05-22 21:49:18 +00001217 //remove the MI from the Machine Code For Instruction
Chris Lattner9cdaa632003-07-26 23:23:41 +00001218 const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Misha Brukman6b77ec42003-05-22 21:49:18 +00001219 MachineCodeForInstruction& llvmMvec =
Chris Lattner9cdaa632003-07-26 23:23:41 +00001220 MachineCodeForInstruction::get((const Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001221
Misha Brukman6b77ec42003-05-22 21:49:18 +00001222 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1223 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1224 if (*mciI==MBB[i])
1225 llvmMvec.erase(mciI);
1226 }
1227 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001228
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001229 assert(sdelayNodeVec.size() >= ndelays);
1230
1231 // If some delay slots were already filled, throw away that many new choices
1232 if (sdelayNodeVec.size() > ndelays)
1233 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001234
1235 // Mark the nodes chosen for delay slots. This removes them from the graph.
1236 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1237 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1238
1239 // And remove the unused NOPs from the graph.
1240 for (unsigned i=0; i < nopNodeVec.size(); i++)
1241 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1242}
1243
1244
1245// For all delayed instructions, choose instructions to put in the delay
1246// slots and pull those out of the graph. Mark them for the delay slots
1247// in the DelaySlotInfo object for that graph node. If no useful work
1248// is found for a delay slot, use the NOP that is currently in that slot.
1249//
1250// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001251// EXCEPT CALLS AND RETURNS.
1252// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001253// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001254// suboptimal. Also, it complicates generating the calling sequence code in
1255// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001256//
1257static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001258ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001259 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001260{
Chris Lattner3501fea2003-01-14 22:00:31 +00001261 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001262
1263 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001264 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001265 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001266 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001267
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001268 if (EnableFillingDelaySlots &&
1269 termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001270 {
1271 // To find instructions that need delay slots without searching the full
1272 // machine code, we assume that the only delayed instructions are CALLs
1273 // or instructions generated for the terminator inst.
1274 // Find the first branch instr in the sequence of machine instrs for term
1275 //
1276 unsigned first = 0;
1277 while (first < termMvec.size() &&
1278 ! mii.isBranch(termMvec[first]->getOpCode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001279 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001280 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001281 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001282 assert(first < termMvec.size() &&
1283 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1284
1285 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1286
1287 // Compute a vector of the nodes chosen for delay slots and then
1288 // mark delay slots to replace NOPs with these useful instructions.
1289 //
1290 if (brInstr != NULL) {
1291 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1292 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1293 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1294 }
1295 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001296
1297 // Also mark delay slots for other delayed instructions to hold NOPs.
1298 // Simply passing in an empty delayNodeVec will have this effect.
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001299 // If brInstr is not handled above (EnableFillingDelaySlots == false),
1300 // brInstr will be NULL so this will handle the branch instrs. as well.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001301 //
1302 delayNodeVec.clear();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001303 for (unsigned i=0; i < MBB.size(); ++i)
1304 if (MBB[i] != brInstr &&
1305 mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001306 {
1307 SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]);
1308 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1309 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001310}
1311
1312
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001313//
1314// Schedule the delayed branch and its delay slots
1315//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001316unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001317DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1318{
1319 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1320 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1321 && "Slot for branch should be empty");
1322
1323 unsigned int nextSlot = delayedNodeSlotNum;
1324 cycles_t nextTime = delayedNodeCycle;
1325
1326 S.scheduleInstr(brNode, nextSlot, nextTime);
1327
Misha Brukman6b77ec42003-05-22 21:49:18 +00001328 for (unsigned d=0; d < ndelays; d++) {
1329 ++nextSlot;
1330 if (nextSlot == S.nslots) {
1331 nextSlot = 0;
1332 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001333 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001334
1335 // Find the first feasible instruction for this delay slot
1336 // Note that we only check for issue restrictions here.
1337 // We do *not* check for flow dependences but rely on pipeline
1338 // interlocks to resolve them. Machines without interlocks
1339 // will require this code to be modified.
1340 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1341 const SchedGraphNode* dnode = delayNodeVec[i];
1342 if ( ! S.isScheduled(dnode)
1343 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1344 && instrIsFeasible(S, dnode->getOpCode()))
1345 {
1346 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
1347 && "Instructions without interlocks not yet supported "
1348 "when filling branch delay slots");
1349 S.scheduleInstr(dnode, nextSlot, nextTime);
1350 break;
1351 }
1352 }
1353 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001354
1355 // Update current time if delay slots overflowed into later cycles.
1356 // Do this here because we know exactly which cycle is the last cycle
1357 // that contains delay slots. The next loop doesn't compute that.
1358 if (nextTime > S.getTime())
1359 S.updateTime(nextTime);
1360
1361 // Now put any remaining instructions in the unfilled delay slots.
1362 // This could lead to suboptimal performance but needed for correctness.
1363 nextSlot = delayedNodeSlotNum;
1364 nextTime = delayedNodeCycle;
1365 for (unsigned i=0; i < delayNodeVec.size(); i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001366 if (! S.isScheduled(delayNodeVec[i])) {
1367 do { // find the next empty slot
1368 ++nextSlot;
1369 if (nextSlot == S.nslots) {
1370 nextSlot = 0;
1371 nextTime++;
1372 }
1373 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001374
Misha Brukman6b77ec42003-05-22 21:49:18 +00001375 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1376 break;
1377 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001378
1379 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001380}
1381
Vikram S. Advec5b46322001-09-30 23:43:34 +00001382
1383// Check if the instruction would conflict with instructions already
1384// chosen for the current cycle
1385//
1386static inline bool
1387ConflictsWithChoices(const SchedulingManager& S,
1388 MachineOpCode opCode)
1389{
1390 // Check if the instruction must issue by itself, and some feasible
1391 // choices have already been made for this cycle
1392 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1393 return true;
1394
1395 // For each class that opCode belongs to, check if there are too many
1396 // instructions of that class.
1397 //
1398 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1399 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1400}
1401
1402
1403//************************* External Functions *****************************/
1404
1405
1406//---------------------------------------------------------------------------
1407// Function: ViolatesMinimumGap
1408//
1409// Purpose:
1410// Check minimum gap requirements relative to instructions scheduled in
1411// previous cycles.
1412// Note that we do not need to consider `nextEarliestIssueTime' here because
1413// that is also captured in the earliest start times for each opcode.
1414//---------------------------------------------------------------------------
1415
1416static inline bool
1417ViolatesMinimumGap(const SchedulingManager& S,
1418 MachineOpCode opCode,
1419 const cycles_t inCycle)
1420{
1421 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1422}
1423
1424
1425//---------------------------------------------------------------------------
1426// Function: instrIsFeasible
1427//
1428// Purpose:
1429// Check if any issue restrictions would prevent the instruction from
1430// being issued in the current cycle
1431//---------------------------------------------------------------------------
1432
1433bool
1434instrIsFeasible(const SchedulingManager& S,
1435 MachineOpCode opCode)
1436{
1437 // skip the instruction if it cannot be issued due to issue restrictions
1438 // caused by previously issued instructions
1439 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1440 return false;
1441
1442 // skip the instruction if it cannot be issued due to issue restrictions
1443 // caused by previously chosen instructions for the current cycle
1444 if (ConflictsWithChoices(S, opCode))
1445 return false;
1446
1447 return true;
1448}
1449
1450//---------------------------------------------------------------------------
1451// Function: ScheduleInstructionsWithSSA
1452//
1453// Purpose:
1454// Entry point for instruction scheduling on SSA form.
1455// Schedules the machine instructions generated by instruction selection.
1456// Assumes that register allocation has not been done, i.e., operands
1457// are still in SSA form.
1458//---------------------------------------------------------------------------
1459
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001460namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001461 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001462 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001463 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001464 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001465
1466 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001467
Chris Lattnerf57b8452002-04-27 06:56:12 +00001468 // getAnalysisUsage - We use LiveVarInfo...
1469 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001470 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001471 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001472 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001473
Chris Lattner7e708292002-06-25 16:13:24 +00001474 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001475 };
1476} // end anonymous namespace
1477
Vikram S. Adve802cec42002-03-24 03:44:55 +00001478
Chris Lattner7e708292002-06-25 16:13:24 +00001479bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001480{
Chris Lattner7e708292002-06-25 16:13:24 +00001481 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001482
Misha Brukman6b77ec42003-05-22 21:49:18 +00001483 if (SchedDebugLevel >= Sched_PrintSchedGraphs) {
Misha Brukmanc2312df2003-05-22 21:24:35 +00001484 std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001485 graphSet.dump();
1486 }
1487
1488 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1489 GI != GE; ++GI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001490 {
1491 SchedGraph* graph = (*GI);
1492 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001493
Misha Brukman6b77ec42003-05-22 21:49:18 +00001494 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1495 std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001496
Misha Brukman6b77ec42003-05-22 21:49:18 +00001497 // expensive!
1498 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
1499 SchedulingManager S(target, graph, schedPrio);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001500
Misha Brukman6b77ec42003-05-22 21:49:18 +00001501 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
1502 ForwardListSchedule(S); // computes schedule in S
1503 RecordSchedule(MBB, S); // records schedule in BB
1504 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001505
Misha Brukman6b77ec42003-05-22 21:49:18 +00001506 if (SchedDebugLevel >= Sched_PrintMachineCode) {
1507 std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
1508 MachineFunction::get(&F).dump();
1509 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001510
1511 return false;
1512}
1513
1514
Brian Gaekebf3c4cf2003-08-14 06:09:32 +00001515FunctionPass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001516 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001517}