blob: ae19a0635e5e668b0764e34f5bc27fc8b44e17c8 [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
2//
3// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
4// generic support routines for instruction scheduling.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +00007
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00009#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000010#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000011#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000012#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000014#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000015#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000016#include <algorithm>
Vikram S. Advec5b46322001-09-30 23:43:34 +000017
Chris Lattner70e60cb2002-05-22 17:08:27 +000018SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000019
Chris Lattner5ff62e92002-07-22 02:10:13 +000020static cl::opt<SchedDebugLevel_t, true>
21SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
22 cl::desc("enable instruction scheduling debugging information"),
23 cl::values(
24 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000025 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
26 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
27 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
28 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000029
30
Vikram S. Advec5b46322001-09-30 23:43:34 +000031//************************* Internal Data Types *****************************/
32
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000033class InstrSchedule;
34class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000035
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000036
37//----------------------------------------------------------------------
38// class InstrGroup:
39//
40// Represents a group of instructions scheduled to be issued
41// in a single cycle.
42//----------------------------------------------------------------------
43
Chris Lattnere3561c22003-08-15 05:20:06 +000044class InstrGroup {
45 InstrGroup(const InstrGroup&); // DO NOT IMPLEMENT
46 void operator=(const InstrGroup&); // DO NOT IMPLEMENT
47
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000048public:
49 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
50 assert(slotNum < group.size());
51 return group[slotNum];
52 }
53
54private:
55 friend class InstrSchedule;
56
57 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
58 assert(slotNum < group.size());
59 group[slotNum] = node;
60 }
61
62 /*ctor*/ InstrGroup(unsigned int nslots)
63 : group(nslots, NULL) {}
64
65 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
66
67private:
Misha Brukmanc2312df2003-05-22 21:24:35 +000068 std::vector<const SchedGraphNode*> group;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000069};
70
71
72//----------------------------------------------------------------------
73// class ScheduleIterator:
74//
75// Iterates over the machine instructions in the for a single basic block.
76// The schedule is represented by an InstrSchedule object.
77//----------------------------------------------------------------------
78
79template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000080class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000081private:
82 unsigned cycleNum;
83 unsigned slotNum;
84 const InstrSchedule& S;
85public:
86 typedef ScheduleIterator<_NodeType> _Self;
87
88 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
89 unsigned _cycleNum,
90 unsigned _slotNum)
91 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
92 skipToNextInstr();
93 }
94
95 /*ctor*/ inline ScheduleIterator(const _Self& x)
96 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
97
98 inline bool operator==(const _Self& x) const {
99 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
100 }
101
102 inline bool operator!=(const _Self& x) const { return !operator==(x); }
103
104 inline _NodeType* operator*() const {
105 assert(cycleNum < S.groups.size());
106 return (*S.groups[cycleNum])[slotNum];
107 }
108 inline _NodeType* operator->() const { return operator*(); }
109
110 _Self& operator++(); // Preincrement
111 inline _Self operator++(int) { // Postincrement
112 _Self tmp(*this); ++*this; return tmp;
113 }
114
115 static _Self begin(const InstrSchedule& _schedule);
116 static _Self end( const InstrSchedule& _schedule);
117
118private:
119 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
120 void skipToNextInstr();
121};
122
123
124//----------------------------------------------------------------------
125// class InstrSchedule:
126//
127// Represents the schedule of machine instructions for a single basic block.
128//----------------------------------------------------------------------
129
Chris Lattnere3561c22003-08-15 05:20:06 +0000130class InstrSchedule {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000131 const unsigned int nslots;
132 unsigned int numInstr;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000133 std::vector<InstrGroup*> groups; // indexed by cycle number
134 std::vector<cycles_t> startTime; // indexed by node id
Chris Lattnere3561c22003-08-15 05:20:06 +0000135
136 InstrSchedule(InstrSchedule&); // DO NOT IMPLEMENT
137 void operator=(InstrSchedule&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000138
139public: // iterators
140 typedef ScheduleIterator<SchedGraphNode> iterator;
141 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
142
143 iterator begin();
144 const_iterator begin() const;
145 iterator end();
146 const_iterator end() const;
147
148public: // constructors and destructor
149 /*ctor*/ InstrSchedule (unsigned int _nslots,
150 unsigned int _numNodes);
151 /*dtor*/ ~InstrSchedule ();
152
153public: // accessor functions to query chosen schedule
154 const SchedGraphNode* getInstr (unsigned int slotNum,
155 cycles_t c) const {
156 const InstrGroup* igroup = this->getIGroup(c);
157 return (igroup == NULL)? NULL : (*igroup)[slotNum];
158 }
159
160 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000161 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000162 groups.resize(c+1);
163 if (groups[c] == NULL)
164 groups[c] = new InstrGroup(nslots);
165 return groups[c];
166 }
167
168 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000169 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000170 return groups[c];
171 }
172
173 inline cycles_t getStartTime (unsigned int nodeId) const {
174 assert(nodeId < startTime.size());
175 return startTime[nodeId];
176 }
177
178 unsigned int getNumInstructions() const {
179 return numInstr;
180 }
181
182 inline void scheduleInstr (const SchedGraphNode* node,
183 unsigned int slotNum,
184 cycles_t cycle) {
185 InstrGroup* igroup = this->getIGroup(cycle);
186 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
187 igroup->addInstr(node, slotNum);
188 assert(node->getNodeId() < startTime.size());
189 startTime[node->getNodeId()] = cycle;
190 ++numInstr;
191 }
192
193private:
194 friend class iterator;
195 friend class const_iterator;
196 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
197};
198
199
200/*ctor*/
201InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
202 : nslots(_nslots),
203 numInstr(0),
204 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
205 startTime(_numNodes, (cycles_t) -1) // set all to -1
206{
207}
208
209
210/*dtor*/
211InstrSchedule::~InstrSchedule()
212{
213 for (unsigned c=0, NC=groups.size(); c < NC; c++)
214 if (groups[c] != NULL)
215 delete groups[c]; // delete InstrGroup objects
216}
217
218
219template<class _NodeType>
220inline
221void
222ScheduleIterator<_NodeType>::skipToNextInstr()
223{
224 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
225 ++cycleNum; // skip cycles with no instructions
226
227 while (cycleNum < S.groups.size() &&
228 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000229 {
230 ++slotNum;
231 if (slotNum == S.nslots) {
232 ++cycleNum;
233 slotNum = 0;
234 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
235 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000236 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000237 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000238}
239
240template<class _NodeType>
241inline
242ScheduleIterator<_NodeType>&
243ScheduleIterator<_NodeType>::operator++() // Preincrement
244{
245 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000246 if (slotNum == S.nslots) {
247 ++cycleNum;
248 slotNum = 0;
249 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000250 skipToNextInstr();
251 return *this;
252}
253
254template<class _NodeType>
255ScheduleIterator<_NodeType>
256ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
257{
258 return _Self(_schedule, 0, 0);
259}
260
261template<class _NodeType>
262ScheduleIterator<_NodeType>
263ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
264{
265 return _Self(_schedule, _schedule.groups.size(), 0);
266}
267
268InstrSchedule::iterator
269InstrSchedule::begin()
270{
271 return iterator::begin(*this);
272}
273
274InstrSchedule::const_iterator
275InstrSchedule::begin() const
276{
277 return const_iterator::begin(*this);
278}
279
280InstrSchedule::iterator
281InstrSchedule::end()
282{
283 return iterator::end(*this);
284}
285
286InstrSchedule::const_iterator
287InstrSchedule::end() const
288{
289 return const_iterator::end( *this);
290}
291
292
293//----------------------------------------------------------------------
294// class DelaySlotInfo:
295//
296// Record information about delay slots for a single branch instruction.
297// Delay slots are simply indexed by slot number 1 ... numDelaySlots
298//----------------------------------------------------------------------
299
Chris Lattnere3561c22003-08-15 05:20:06 +0000300class DelaySlotInfo {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000301 const SchedGraphNode* brNode;
Chris Lattnere3561c22003-08-15 05:20:06 +0000302 unsigned ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000303 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000304 cycles_t delayedNodeCycle;
Chris Lattnere3561c22003-08-15 05:20:06 +0000305 unsigned delayedNodeSlotNum;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000306
Chris Lattnere3561c22003-08-15 05:20:06 +0000307 DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT
308 void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000309public:
310 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
311 unsigned _ndelays)
312 : brNode(_brNode), ndelays(_ndelays),
313 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
314
315 inline unsigned getNumDelays () {
316 return ndelays;
317 }
318
Misha Brukmanc2312df2003-05-22 21:24:35 +0000319 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000320 return delayNodeVec;
321 }
322
323 inline void addDelayNode (const SchedGraphNode* node) {
324 delayNodeVec.push_back(node);
325 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
326 }
327
328 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
329 delayedNodeCycle = cycle;
330 delayedNodeSlotNum = slotNum;
331 }
332
Vikram S. Advec5b46322001-09-30 23:43:34 +0000333 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000334};
335
336
337//----------------------------------------------------------------------
338// class SchedulingManager:
339//
340// Represents the schedule of machine instructions for a single basic block.
341//----------------------------------------------------------------------
342
Chris Lattnere3561c22003-08-15 05:20:06 +0000343class SchedulingManager {
344 SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT
345 void operator=(const SchedulingManager &); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000346public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000347 const unsigned nslots;
348 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000349 SchedPriorities& schedPrio;
350 InstrSchedule isched;
351
352private:
Chris Lattnere3561c22003-08-15 05:20:06 +0000353 unsigned totalInstrCount;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000354 cycles_t curTime;
355 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000356 // indexed by slot#
357 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
358 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
359 std::vector<int> numInClass; // indexed by sched class
360 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000361 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000362 // indexed by branch node ptr
363
364public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000365 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
366 SchedPriorities& schedPrio);
367 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000368 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000369 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
370 E = delaySlotInfoForBranches.end(); I != E; ++I)
371 delete I->second;
372 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000373
374 //----------------------------------------------------------------------
375 // Simplify access to the machine instruction info
376 //----------------------------------------------------------------------
377
Chris Lattner3501fea2003-01-14 22:00:31 +0000378 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000379 return schedInfo.getInstrInfo();
380 }
381
382 //----------------------------------------------------------------------
383 // Interface for checking and updating the current time
384 //----------------------------------------------------------------------
385
386 inline cycles_t getTime () const {
387 return curTime;
388 }
389
390 inline cycles_t getEarliestIssueTime() const {
391 return nextEarliestIssueTime;
392 }
393
394 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
395 assert(opCode < (int) nextEarliestStartTime.size());
396 return nextEarliestStartTime[opCode];
397 }
398
399 // Update current time to specified cycle
400 inline void updateTime (cycles_t c) {
401 curTime = c;
402 schedPrio.updateTime(c);
403 }
404
405 //----------------------------------------------------------------------
406 // Functions to manage the choices for the current cycle including:
407 // -- a vector of choices by priority (choiceVec)
408 // -- vectors of the choices for each instruction slot (choicesForSlot[])
409 // -- number of choices in each sched class, used to check issue conflicts
410 // between choices for a single cycle
411 //----------------------------------------------------------------------
412
413 inline unsigned int getNumChoices () const {
414 return choiceVec.size();
415 }
416
417 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000418 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000419 return numInClass[sc];
420 }
421
422 inline const SchedGraphNode* getChoice(unsigned int i) const {
423 // assert(i < choiceVec.size()); don't check here.
424 return choiceVec[i];
425 }
426
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000427 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000428 assert(slotNum < nslots);
429 return choicesForSlot[slotNum];
430 }
431
432 inline void addChoice (const SchedGraphNode* node) {
433 // Append the instruction to the vector of choices for current cycle.
434 // Increment numInClass[c] for the sched class to which the instr belongs.
435 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000436 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000437 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000438 numInClass[sc]++;
439 }
440
441 inline void addChoiceToSlot (unsigned int slotNum,
442 const SchedGraphNode* node) {
443 // Add the instruction to the choice set for the specified slot
444 assert(slotNum < nslots);
445 choicesForSlot[slotNum].insert(node);
446 }
447
448 inline void resetChoices () {
449 choiceVec.clear();
450 for (unsigned int s=0; s < nslots; s++)
451 choicesForSlot[s].clear();
452 for (unsigned int c=0; c < numInClass.size(); c++)
453 numInClass[c] = 0;
454 }
455
456 //----------------------------------------------------------------------
457 // Code to query and manage the partial instruction schedule so far
458 //----------------------------------------------------------------------
459
460 inline unsigned int getNumScheduled () const {
461 return isched.getNumInstructions();
462 }
463
464 inline unsigned int getNumUnscheduled() const {
465 return totalInstrCount - isched.getNumInstructions();
466 }
467
468 inline bool isScheduled (const SchedGraphNode* node) const {
469 return (isched.getStartTime(node->getNodeId()) >= 0);
470 }
471
472 inline void scheduleInstr (const SchedGraphNode* node,
473 unsigned int slotNum,
474 cycles_t cycle)
475 {
476 assert(! isScheduled(node) && "Instruction already scheduled?");
477
478 // add the instruction to the schedule
479 isched.scheduleInstr(node, slotNum, cycle);
480
481 // update the earliest start times of all nodes that conflict with `node'
482 // and the next-earliest time anything can issue if `node' causes bubbles
483 updateEarliestStartTimes(node, cycle);
484
485 // remove the instruction from the choice sets for all slots
486 for (unsigned s=0; s < nslots; s++)
487 choicesForSlot[s].erase(node);
488
489 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000490 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000491 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000492 numInClass[sc]--;
493 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000494
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000495 //----------------------------------------------------------------------
496 // Create and retrieve delay slot info for delayed instructions
497 //----------------------------------------------------------------------
498
499 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
500 bool createIfMissing=false)
501 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000502 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000503 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000504 if (I != delaySlotInfoForBranches.end())
505 return I->second;
506
507 if (!createIfMissing) return 0;
508
509 DelaySlotInfo *dinfo =
510 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
511 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000512 }
513
514private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000515 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
516 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000517};
518
519
520/*ctor*/
521SchedulingManager::SchedulingManager(const TargetMachine& target,
522 const SchedGraph* graph,
523 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000524 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
525 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000526 schedPrio(_schedPrio),
527 isched(nslots, graph->getNumNodes()),
528 totalInstrCount(graph->getNumNodes() - 2),
529 nextEarliestIssueTime(0),
530 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000531 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000532 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
533 (cycles_t) 0) // set all to 0
534{
535 updateTime(0);
536
537 // Note that an upper bound on #choices for each slot is = nslots since
538 // we use this vector to hold a feasible set of instructions, and more
539 // would be infeasible. Reserve that much memory since it is probably small.
540 for (unsigned int i=0; i < nslots; i++)
541 choicesForSlot[i].resize(nslots);
542}
543
544
545void
546SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
547 cycles_t schedTime)
548{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000549 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000550 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000551 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000552 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000553 }
554
Vikram S. Adve1632e882002-10-13 00:40:37 +0000555 const std::vector<MachineOpCode>&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000556 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000557
Vikram S. Adve1632e882002-10-13 00:40:37 +0000558 for (unsigned i=0; i < conflictVec.size(); i++)
559 {
560 MachineOpCode toOp = conflictVec[i];
561 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp);
562 assert(toOp < (int) nextEarliestStartTime.size());
563 if (nextEarliestStartTime[toOp] < est)
564 nextEarliestStartTime[toOp] = est;
565 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000566}
567
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000568//************************* Internal Functions *****************************/
569
570
571static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000572AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000573{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000574 // find the slot to start from, in the current cycle
575 unsigned int startSlot = 0;
576 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000577
Vikram S. Advec5b46322001-09-30 23:43:34 +0000578 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000579
Vikram S. Advec5b46322001-09-30 23:43:34 +0000580 // If only one instruction can be issued, do so.
581 if (maxIssue == 1)
582 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000583 if (S.getChoicesForSlot(s).size() > 0) {
584 // found the one instruction
585 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
586 return;
587 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000588
589 // Otherwise, choose from the choices for each slot
590 //
591 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
592 assert(igroup != NULL && "Group creation failed?");
593
594 // Find a slot that has only a single choice, and take it.
595 // If all slots have 0 or multiple choices, pick the first slot with
596 // choices and use its last instruction (just to avoid shifting the vector).
597 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000598 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
599 int chosenSlot = -1;
600 for (unsigned s=startSlot; s < S.nslots; s++)
601 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
602 chosenSlot = (int) s;
603 break;
604 }
605
606 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000607 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000608 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
609 chosenSlot = (int) s;
610 break;
611 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000612
Misha Brukman6b77ec42003-05-22 21:49:18 +0000613 if (chosenSlot != -1) {
614 // Insert the chosen instr in the chosen slot and
615 // erase it from all slots.
616 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
617 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000618 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000619 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000620
621 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000622}
623
624
625//
626// For now, just assume we are scheduling within a single basic block.
627// Get the machine instruction vector for the basic block and clear it,
628// then append instructions in scheduled order.
629// Also, re-insert the dummy PHI instructions that were at the beginning
630// of the basic block, since they are not part of the schedule.
631//
632static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000633RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000634{
Chris Lattner3501fea2003-01-14 22:00:31 +0000635 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000636
637#ifndef NDEBUG
638 // Lets make sure we didn't lose any instructions, except possibly
639 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
640 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000641 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000642 if (! mii.isNop((*I)->getOpCode()) &&
643 ! mii.isDummyPhiInstr((*I)->getOpCode()))
644 ++numInstr;
645 assert(S.isched.getNumInstructions() >= numInstr &&
646 "Lost some non-NOP instructions during scheduling!");
647#endif
648
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000649 if (S.isched.getNumInstructions() == 0)
650 return; // empty basic block!
651
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000652 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000653 MachineBasicBlock::iterator I = MBB.begin();
654 for ( ; I != MBB.end(); ++I)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000655 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
656 break;
657
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000658 // Erase all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000659 // pre-allocate create space for the ones we will put back in.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000660 MBB.erase(I, MBB.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000661
662 InstrSchedule::const_iterator NIend = S.isched.end();
663 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000664 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000665}
666
667
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000668
669static void
670MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
671{
672 // Check if any successors are now ready that were not already marked
673 // ready before, and that have not yet been scheduled.
674 //
675 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
676 if (! (*SI)->isDummyNode()
677 && ! S.isScheduled(*SI)
678 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000679 {
680 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000681
Misha Brukman6b77ec42003-05-22 21:49:18 +0000682 bool succIsReady = true;
683 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
684 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
685 succIsReady = false;
686 break;
687 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000688
Misha Brukman6b77ec42003-05-22 21:49:18 +0000689 if (succIsReady) // add the successor to the ready list
690 S.schedPrio.insertReady(*SI);
691 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000692}
693
694
695// Choose up to `nslots' FEASIBLE instructions and assign each
696// instruction to all possible slots that do not violate feasibility.
697// FEASIBLE means it should be guaranteed that the set
698// of chosen instructions can be issued in a single group.
699//
700// Return value:
701// maxIssue : total number of feasible instructions
702// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
703//
704static unsigned
705FindSlotChoices(SchedulingManager& S,
706 DelaySlotInfo*& getDelaySlotInfo)
707{
708 // initialize result vectors to empty
709 S.resetChoices();
710
711 // find the slot to start from, in the current cycle
712 unsigned int startSlot = 0;
713 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
714 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000715 if ((*igroup)[s] != NULL) {
716 startSlot = s+1;
717 break;
718 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000719
720 // Make sure we pick at most one instruction that would break the group.
721 // Also, if we do pick one, remember which it was.
722 unsigned int indexForBreakingNode = S.nslots;
723 unsigned int indexForDelayedInstr = S.nslots;
724 DelaySlotInfo* delaySlotInfo = NULL;
725
726 getDelaySlotInfo = NULL;
727
728 // Choose instructions in order of priority.
729 // Add choices to the choice vector in the SchedulingManager class as
730 // we choose them so that subsequent choices will be correctly tested
731 // for feasibility, w.r.t. higher priority choices for the same cycle.
732 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000733 while (S.getNumChoices() < S.nslots - startSlot) {
734 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
735 if (nextNode == NULL)
736 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000737
Misha Brukman6b77ec42003-05-22 21:49:18 +0000738 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0) {
739 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
740 if (delaySlotInfo != NULL) {
741 if (indexForBreakingNode < S.nslots)
742 // cannot issue a delayed instr in the same cycle as one
743 // that breaks the issue group or as another delayed instr
744 nextNode = NULL;
745 else
746 indexForDelayedInstr = S.getNumChoices();
747 }
748 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode())) {
749 if (indexForBreakingNode < S.nslots)
750 // have a breaking instruction already so throw this one away
751 nextNode = NULL;
752 else
753 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000754 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000755
756 if (nextNode != NULL) {
757 S.addChoice(nextNode);
758
759 if (S.schedInfo.isSingleIssue(nextNode->getOpCode())) {
760 assert(S.getNumChoices() == 1 &&
761 "Prioritizer returned invalid instr for this cycle!");
762 break;
763 }
764 }
765
766 if (indexForDelayedInstr < S.nslots)
767 break; // leave the rest for delay slots
768 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000769
770 assert(S.getNumChoices() <= S.nslots);
771 assert(! (indexForDelayedInstr < S.nslots &&
772 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
773
774 // Assign each chosen instruction to all possible slots for that instr.
775 // But if only one instruction was chosen, put it only in the first
776 // feasible slot; no more analysis will be needed.
777 //
778 if (indexForDelayedInstr >= S.nslots &&
779 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000780 { // No instructions that break the issue group or that have delay slots.
781 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000782
Misha Brukman6b77ec42003-05-22 21:49:18 +0000783 if (S.getNumChoices() == 1) {
784 MachineOpCode opCode = S.getChoice(0)->getOpCode();
785 unsigned int s;
786 for (s=startSlot; s < S.nslots; s++)
787 if (S.schedInfo.instrCanUseSlot(opCode, s))
788 break;
789 assert(s < S.nslots && "No feasible slot for this opCode?");
790 S.addChoiceToSlot(s, S.getChoice(0));
791 } else {
792 for (unsigned i=0; i < S.getNumChoices(); i++) {
793 MachineOpCode opCode = S.getChoice(i)->getOpCode();
794 for (unsigned int s=startSlot; s < S.nslots; s++)
795 if (S.schedInfo.instrCanUseSlot(opCode, s))
796 S.addChoiceToSlot(s, S.getChoice(i));
797 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000798 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000799 } else if (indexForDelayedInstr < S.nslots) {
800 // There is an instruction that needs delay slots.
801 // Try to assign that instruction to a higher slot than any other
802 // instructions in the group, so that its delay slots can go
803 // right after it.
804 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000805
Misha Brukman6b77ec42003-05-22 21:49:18 +0000806 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
807 "Instruction with delay slots should be last choice!");
808 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000809
Misha Brukman6b77ec42003-05-22 21:49:18 +0000810 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
811 MachineOpCode delayOpCode = delayedNode->getOpCode();
812 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000813
Misha Brukman6b77ec42003-05-22 21:49:18 +0000814 unsigned delayedNodeSlot = S.nslots;
815 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000816
Misha Brukman6b77ec42003-05-22 21:49:18 +0000817 // Find the last possible slot for the delayed instruction that leaves
818 // at least `d' slots vacant after it (d = #delay slots)
819 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
820 if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) {
821 delayedNodeSlot = s;
822 break;
823 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000824
Misha Brukman6b77ec42003-05-22 21:49:18 +0000825 highestSlotUsed = -1;
826 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
827 // Try to assign every other instruction to a lower numbered
828 // slot than delayedNodeSlot.
829 MachineOpCode opCode =S.getChoice(i)->getOpCode();
830 bool noSlotFound = true;
831 unsigned int s;
832 for (s=startSlot; s < delayedNodeSlot; s++)
833 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
834 S.addChoiceToSlot(s, S.getChoice(i));
835 noSlotFound = false;
836 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000837
Misha Brukman6b77ec42003-05-22 21:49:18 +0000838 // No slot before `delayedNodeSlot' was found for this opCode
839 // Use a later slot, and allow some delay slots to fall in
840 // the next cycle.
841 if (noSlotFound)
842 for ( ; s < S.nslots; s++)
843 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
844 S.addChoiceToSlot(s, S.getChoice(i));
845 break;
846 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000847
Misha Brukman6b77ec42003-05-22 21:49:18 +0000848 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000849
Misha Brukman6b77ec42003-05-22 21:49:18 +0000850 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000851 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000852
Misha Brukman6b77ec42003-05-22 21:49:18 +0000853 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
854
855 // We will put the delayed node in the first slot after the
856 // highest slot used. But we just mark that for now, and
857 // schedule it separately because we want to schedule the delay
858 // slots for the node at the same time.
859 cycles_t dcycle = S.getTime();
860 unsigned int dslot = highestSlotUsed + 1;
861 if (dslot == S.nslots) {
862 dslot = 0;
863 ++dcycle;
864 }
865 delaySlotInfo->recordChosenSlot(dcycle, dslot);
866 getDelaySlotInfo = delaySlotInfo;
867 } else {
868 // There is an instruction that breaks the issue group.
869 // For such an instruction, assign to the last possible slot in
870 // the current group, and then don't assign any other instructions
871 // to later slots.
872 assert(indexForBreakingNode < S.nslots);
873 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
874 unsigned breakingSlot = INT_MAX;
875 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000876
Misha Brukman6b77ec42003-05-22 21:49:18 +0000877 // Find the last possible slot for this instruction.
878 for (int s = S.nslots-1; s >= (int) startSlot; s--)
879 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s)) {
880 breakingSlot = s;
881 break;
882 }
883 assert(breakingSlot < S.nslots &&
884 "No feasible slot for `breakingNode'?");
885
886 // Higher priority instructions than the one that breaks the group:
887 // These can be assigned to all slots, but will be assigned only
888 // to earlier slots if possible.
889 for (unsigned i=0;
890 i < S.getNumChoices() && i < indexForBreakingNode; i++)
891 {
892 MachineOpCode opCode =S.getChoice(i)->getOpCode();
893
894 // If a higher priority instruction cannot be assigned to
895 // any earlier slots, don't schedule the breaking instruction.
896 //
897 bool foundLowerSlot = false;
898 nslotsToUse = S.nslots; // May be modified in the loop
899 for (unsigned int s=startSlot; s < nslotsToUse; s++)
900 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
901 if (breakingSlot < S.nslots && s < breakingSlot) {
902 foundLowerSlot = true;
903 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
904 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000905
Misha Brukman6b77ec42003-05-22 21:49:18 +0000906 S.addChoiceToSlot(s, S.getChoice(i));
907 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000908
Misha Brukman6b77ec42003-05-22 21:49:18 +0000909 if (!foundLowerSlot)
910 breakingSlot = INT_MAX; // disable breaking instr
911 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000912
Misha Brukman6b77ec42003-05-22 21:49:18 +0000913 // Assign the breaking instruction (if any) to a single slot
914 // Otherwise, just ignore the instruction. It will simply be
915 // scheduled in a later cycle.
916 if (breakingSlot < S.nslots) {
917 S.addChoiceToSlot(breakingSlot, breakingNode);
918 nslotsToUse = breakingSlot;
919 } else
920 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000921
Misha Brukman6b77ec42003-05-22 21:49:18 +0000922 // For lower priority instructions than the one that breaks the
923 // group, only assign them to slots lower than the breaking slot.
924 // Otherwise, just ignore the instruction.
925 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
926 MachineOpCode opCode = S.getChoice(i)->getOpCode();
927 for (unsigned int s=startSlot; s < nslotsToUse; s++)
928 if (S.schedInfo.instrCanUseSlot(opCode, s))
929 S.addChoiceToSlot(s, S.getChoice(i));
930 }
931 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000932
933 return S.getNumChoices();
934}
935
936
Vikram S. Advec5b46322001-09-30 23:43:34 +0000937static unsigned
938ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000939{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000940 assert(S.schedPrio.getNumReady() > 0
941 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000942
Vikram S. Advec5b46322001-09-30 23:43:34 +0000943 cycles_t firstCycle = S.getTime();
944 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000945
Vikram S. Advec5b46322001-09-30 23:43:34 +0000946 // Choose up to `nslots' feasible instructions and their possible slots.
947 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000948
Misha Brukman6b77ec42003-05-22 21:49:18 +0000949 while (numIssued == 0) {
950 S.updateTime(S.getTime()+1);
951 numIssued = FindSlotChoices(S, getDelaySlotInfo);
952 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000953
Vikram S. Advec5b46322001-09-30 23:43:34 +0000954 AssignInstructionsToSlots(S, numIssued);
955
956 if (getDelaySlotInfo != NULL)
957 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
958
959 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000960 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
961 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
962 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
963 const InstrGroup* igroup = S.isched.getIGroup(c);
964 for (unsigned int s=0; s < S.nslots; s++) {
965 std::cerr << " ";
966 if ((*igroup)[s] != NULL)
967 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
968 else
969 std::cerr << "<none>\n";
970 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000971 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000972 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000973
974 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000975}
976
977
Vikram S. Advec5b46322001-09-30 23:43:34 +0000978static void
979ForwardListSchedule(SchedulingManager& S)
980{
981 unsigned N;
982 const SchedGraphNode* node;
983
984 S.schedPrio.initialize();
985
Misha Brukman6b77ec42003-05-22 21:49:18 +0000986 while ((N = S.schedPrio.getNumReady()) > 0) {
987 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +0000988
Misha Brukman6b77ec42003-05-22 21:49:18 +0000989 // Choose one group of instructions for a cycle, plus any delay slot
990 // instructions (which may overflow into successive cycles).
991 // This will advance S.getTime() to the last cycle in which
992 // instructions are actually issued.
993 //
994 unsigned numIssued = ChooseOneGroup(S);
995 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +0000996
Misha Brukman6b77ec42003-05-22 21:49:18 +0000997 // Notify the priority manager of scheduled instructions and mark
998 // any successors that may now be ready
999 //
1000 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
1001 const InstrGroup* igroup = S.isched.getIGroup(c);
1002 for (unsigned int s=0; s < S.nslots; s++)
1003 if ((node = (*igroup)[s]) != NULL) {
1004 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1005 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001006 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001007 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001008
1009 // Move to the next the next earliest cycle for which
1010 // an instruction can be issued, or the next earliest in which
1011 // one will be ready, or to the next cycle, whichever is latest.
1012 //
1013 S.updateTime(std::max(S.getTime() + 1,
1014 std::max(S.getEarliestIssueTime(),
1015 S.schedPrio.getEarliestReadyTime())));
1016 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001017}
1018
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001019
1020//---------------------------------------------------------------------
1021// Code for filling delay slots for delayed terminator instructions
1022// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1023// instructions (e.g., CALL) are not handled here because they almost
1024// always can be filled with instructions from the call sequence code
1025// before a call. That's preferable because we incur many tradeoffs here
1026// when we cannot find single-cycle instructions that can be reordered.
1027//----------------------------------------------------------------------
1028
Vikram S. Advec5b46322001-09-30 23:43:34 +00001029static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001030NodeCanFillDelaySlot(const SchedulingManager& S,
1031 const SchedGraphNode* node,
1032 const SchedGraphNode* brNode,
1033 bool nodeIsPredecessor)
1034{
1035 assert(! node->isDummyNode());
1036
1037 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001038 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001039 return false;
1040
1041 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001042 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001043 return false;
1044
1045 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001046 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001047
1048 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1049 EI != node->endInEdges(); ++EI)
1050 if (! (*EI)->getSrc()->isDummyNode()
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001051 && mii.isLoad((*EI)->getSrc()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001052 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1053 return false;
1054
1055 // for now, don't put an instruction that does not have operand
1056 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001057 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001058 return false;
1059
1060 // Finally, if the instruction preceeds the branch, we make sure the
1061 // instruction can be reordered relative to the branch. We simply check
1062 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1063 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001064 if (nodeIsPredecessor) {
1065 bool onlyCDEdgeToBranch = true;
1066 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1067 OEI != node->endOutEdges(); ++OEI)
1068 if (! (*OEI)->getSink()->isDummyNode()
1069 && ((*OEI)->getSink() != brNode
1070 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1071 {
1072 onlyCDEdgeToBranch = false;
1073 break;
1074 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001075
Misha Brukman6b77ec42003-05-22 21:49:18 +00001076 if (!onlyCDEdgeToBranch)
1077 return false;
1078 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001079
1080 return true;
1081}
1082
1083
Vikram S. Advec5b46322001-09-30 23:43:34 +00001084static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001085MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001086 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001087 SchedGraphNode* node,
1088 const SchedGraphNode* brNode,
1089 bool nodeIsPredecessor)
1090{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001091 if (nodeIsPredecessor) {
1092 // If node is in the same basic block (i.e., preceeds brNode),
1093 // remove it and all its incident edges from the graph. Make sure we
1094 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1095 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1096 } else {
1097 // If the node was from a target block, add the node to the graph
1098 // and add a CD edge from brNode to node.
1099 assert(0 && "NOT IMPLEMENTED YET");
1100 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001101
1102 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1103 dinfo->addDelayNode(node);
1104}
1105
1106
Vikram S. Advec5b46322001-09-30 23:43:34 +00001107void
1108FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1109 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001110 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001111{
Chris Lattner3501fea2003-01-14 22:00:31 +00001112 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001113 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001114 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001115
1116 if (ndelays == 0)
1117 return;
1118
1119 sdelayNodeVec.reserve(ndelays);
1120
1121 // Use a separate vector to hold the feasible multi-cycle nodes.
1122 // These will be used if not enough single-cycle nodes are found.
1123 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001124 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001125
1126 for (sg_pred_iterator P = pred_begin(brNode);
1127 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1128 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001129 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001130 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001131 {
1132 if (mii.maxLatency((*P)->getOpCode()) > 1)
1133 mdelayNodeVec.push_back(*P);
1134 else
1135 sdelayNodeVec.push_back(*P);
1136 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001137
1138 // If not enough single-cycle instructions were found, select the
1139 // lowest-latency multi-cycle instructions and use them.
1140 // Note that this is the most efficient code when only 1 (or even 2)
1141 // values need to be selected.
1142 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001143 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1144 unsigned lmin =
1145 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
1146 unsigned minIndex = 0;
1147 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001148 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001149 unsigned li =
1150 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
1151 if (lmin >= li)
1152 {
1153 lmin = li;
1154 minIndex = i;
1155 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001156 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001157 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1158 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1159 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1160 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001161}
1162
1163
1164// Remove the NOPs currently in delay slots from the graph.
1165// Mark instructions specified in sdelayNodeVec to replace them.
1166// If not enough useful instructions were found, mark the NOPs to be used
1167// for filling delay slots, otherwise, otherwise just discard them.
1168//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001169static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1170 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001171 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001172 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001173 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001174{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001175 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001176 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001177 const MachineInstr* brInstr = node->getMachineInstr();
1178 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001179 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1180
1181 // Remove the NOPs currently in delay slots from the graph.
1182 // If not enough useful instructions were found, use the NOPs to
1183 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001184 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001185 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001186 MachineBasicBlock& MBB = node->getMachineBasicBlock();
1187 assert(MBB[firstDelaySlotIdx - 1] == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001188 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001189
1190 // First find all useful instructions already in the delay slots
1191 // and USE THEM. We'll throw away the unused alternatives below
1192 //
1193 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001194 if (! mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001195 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001196 graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001197
1198 // Then find the NOPs and keep only as many as are needed.
1199 // Put the rest in nopNodeVec to be deleted.
1200 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001201 if (mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001202 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001203 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001204 else {
1205 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001206
Misha Brukman6b77ec42003-05-22 21:49:18 +00001207 //remove the MI from the Machine Code For Instruction
Chris Lattner9cdaa632003-07-26 23:23:41 +00001208 const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Misha Brukman6b77ec42003-05-22 21:49:18 +00001209 MachineCodeForInstruction& llvmMvec =
Chris Lattner9cdaa632003-07-26 23:23:41 +00001210 MachineCodeForInstruction::get((const Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001211
Misha Brukman6b77ec42003-05-22 21:49:18 +00001212 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1213 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1214 if (*mciI==MBB[i])
1215 llvmMvec.erase(mciI);
1216 }
1217 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001218
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001219 assert(sdelayNodeVec.size() >= ndelays);
1220
1221 // If some delay slots were already filled, throw away that many new choices
1222 if (sdelayNodeVec.size() > ndelays)
1223 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001224
1225 // Mark the nodes chosen for delay slots. This removes them from the graph.
1226 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1227 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1228
1229 // And remove the unused NOPs from the graph.
1230 for (unsigned i=0; i < nopNodeVec.size(); i++)
1231 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1232}
1233
1234
1235// For all delayed instructions, choose instructions to put in the delay
1236// slots and pull those out of the graph. Mark them for the delay slots
1237// in the DelaySlotInfo object for that graph node. If no useful work
1238// is found for a delay slot, use the NOP that is currently in that slot.
1239//
1240// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001241// EXCEPT CALLS AND RETURNS.
1242// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001243// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001244// suboptimal. Also, it complicates generating the calling sequence code in
1245// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001246//
1247static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001248ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001249 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001250{
Chris Lattner3501fea2003-01-14 22:00:31 +00001251 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001252
1253 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001254 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001255 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001256 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001257
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001258 if (termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001259 {
1260 // To find instructions that need delay slots without searching the full
1261 // machine code, we assume that the only delayed instructions are CALLs
1262 // or instructions generated for the terminator inst.
1263 // Find the first branch instr in the sequence of machine instrs for term
1264 //
1265 unsigned first = 0;
1266 while (first < termMvec.size() &&
1267 ! mii.isBranch(termMvec[first]->getOpCode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001268 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001269 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001270 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001271 assert(first < termMvec.size() &&
1272 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1273
1274 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1275
1276 // Compute a vector of the nodes chosen for delay slots and then
1277 // mark delay slots to replace NOPs with these useful instructions.
1278 //
1279 if (brInstr != NULL) {
1280 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1281 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1282 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1283 }
1284 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001285
1286 // Also mark delay slots for other delayed instructions to hold NOPs.
1287 // Simply passing in an empty delayNodeVec will have this effect.
1288 //
1289 delayNodeVec.clear();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001290 for (unsigned i=0; i < MBB.size(); ++i)
1291 if (MBB[i] != brInstr &&
1292 mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001293 {
1294 SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]);
1295 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1296 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001297}
1298
1299
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001300//
1301// Schedule the delayed branch and its delay slots
1302//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001303unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001304DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1305{
1306 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1307 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1308 && "Slot for branch should be empty");
1309
1310 unsigned int nextSlot = delayedNodeSlotNum;
1311 cycles_t nextTime = delayedNodeCycle;
1312
1313 S.scheduleInstr(brNode, nextSlot, nextTime);
1314
Misha Brukman6b77ec42003-05-22 21:49:18 +00001315 for (unsigned d=0; d < ndelays; d++) {
1316 ++nextSlot;
1317 if (nextSlot == S.nslots) {
1318 nextSlot = 0;
1319 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001320 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001321
1322 // Find the first feasible instruction for this delay slot
1323 // Note that we only check for issue restrictions here.
1324 // We do *not* check for flow dependences but rely on pipeline
1325 // interlocks to resolve them. Machines without interlocks
1326 // will require this code to be modified.
1327 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1328 const SchedGraphNode* dnode = delayNodeVec[i];
1329 if ( ! S.isScheduled(dnode)
1330 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1331 && instrIsFeasible(S, dnode->getOpCode()))
1332 {
1333 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
1334 && "Instructions without interlocks not yet supported "
1335 "when filling branch delay slots");
1336 S.scheduleInstr(dnode, nextSlot, nextTime);
1337 break;
1338 }
1339 }
1340 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001341
1342 // Update current time if delay slots overflowed into later cycles.
1343 // Do this here because we know exactly which cycle is the last cycle
1344 // that contains delay slots. The next loop doesn't compute that.
1345 if (nextTime > S.getTime())
1346 S.updateTime(nextTime);
1347
1348 // Now put any remaining instructions in the unfilled delay slots.
1349 // This could lead to suboptimal performance but needed for correctness.
1350 nextSlot = delayedNodeSlotNum;
1351 nextTime = delayedNodeCycle;
1352 for (unsigned i=0; i < delayNodeVec.size(); i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001353 if (! S.isScheduled(delayNodeVec[i])) {
1354 do { // find the next empty slot
1355 ++nextSlot;
1356 if (nextSlot == S.nslots) {
1357 nextSlot = 0;
1358 nextTime++;
1359 }
1360 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001361
Misha Brukman6b77ec42003-05-22 21:49:18 +00001362 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1363 break;
1364 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001365
1366 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001367}
1368
Vikram S. Advec5b46322001-09-30 23:43:34 +00001369
1370// Check if the instruction would conflict with instructions already
1371// chosen for the current cycle
1372//
1373static inline bool
1374ConflictsWithChoices(const SchedulingManager& S,
1375 MachineOpCode opCode)
1376{
1377 // Check if the instruction must issue by itself, and some feasible
1378 // choices have already been made for this cycle
1379 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1380 return true;
1381
1382 // For each class that opCode belongs to, check if there are too many
1383 // instructions of that class.
1384 //
1385 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1386 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1387}
1388
1389
1390//************************* External Functions *****************************/
1391
1392
1393//---------------------------------------------------------------------------
1394// Function: ViolatesMinimumGap
1395//
1396// Purpose:
1397// Check minimum gap requirements relative to instructions scheduled in
1398// previous cycles.
1399// Note that we do not need to consider `nextEarliestIssueTime' here because
1400// that is also captured in the earliest start times for each opcode.
1401//---------------------------------------------------------------------------
1402
1403static inline bool
1404ViolatesMinimumGap(const SchedulingManager& S,
1405 MachineOpCode opCode,
1406 const cycles_t inCycle)
1407{
1408 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1409}
1410
1411
1412//---------------------------------------------------------------------------
1413// Function: instrIsFeasible
1414//
1415// Purpose:
1416// Check if any issue restrictions would prevent the instruction from
1417// being issued in the current cycle
1418//---------------------------------------------------------------------------
1419
1420bool
1421instrIsFeasible(const SchedulingManager& S,
1422 MachineOpCode opCode)
1423{
1424 // skip the instruction if it cannot be issued due to issue restrictions
1425 // caused by previously issued instructions
1426 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1427 return false;
1428
1429 // skip the instruction if it cannot be issued due to issue restrictions
1430 // caused by previously chosen instructions for the current cycle
1431 if (ConflictsWithChoices(S, opCode))
1432 return false;
1433
1434 return true;
1435}
1436
1437//---------------------------------------------------------------------------
1438// Function: ScheduleInstructionsWithSSA
1439//
1440// Purpose:
1441// Entry point for instruction scheduling on SSA form.
1442// Schedules the machine instructions generated by instruction selection.
1443// Assumes that register allocation has not been done, i.e., operands
1444// are still in SSA form.
1445//---------------------------------------------------------------------------
1446
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001447namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001448 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001449 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001450 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001451 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001452
1453 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001454
Chris Lattnerf57b8452002-04-27 06:56:12 +00001455 // getAnalysisUsage - We use LiveVarInfo...
1456 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001457 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001458 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001459 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001460
Chris Lattner7e708292002-06-25 16:13:24 +00001461 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001462 };
1463} // end anonymous namespace
1464
Vikram S. Adve802cec42002-03-24 03:44:55 +00001465
Chris Lattner7e708292002-06-25 16:13:24 +00001466bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001467{
Chris Lattner7e708292002-06-25 16:13:24 +00001468 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001469
Misha Brukman6b77ec42003-05-22 21:49:18 +00001470 if (SchedDebugLevel >= Sched_PrintSchedGraphs) {
Misha Brukmanc2312df2003-05-22 21:24:35 +00001471 std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001472 graphSet.dump();
1473 }
1474
1475 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1476 GI != GE; ++GI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001477 {
1478 SchedGraph* graph = (*GI);
1479 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001480
Misha Brukman6b77ec42003-05-22 21:49:18 +00001481 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1482 std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001483
Misha Brukman6b77ec42003-05-22 21:49:18 +00001484 // expensive!
1485 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
1486 SchedulingManager S(target, graph, schedPrio);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001487
Misha Brukman6b77ec42003-05-22 21:49:18 +00001488 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
1489 ForwardListSchedule(S); // computes schedule in S
1490 RecordSchedule(MBB, S); // records schedule in BB
1491 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001492
Misha Brukman6b77ec42003-05-22 21:49:18 +00001493 if (SchedDebugLevel >= Sched_PrintMachineCode) {
1494 std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
1495 MachineFunction::get(&F).dump();
1496 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001497
1498 return false;
1499}
1500
1501
Brian Gaekebf3c4cf2003-08-14 06:09:32 +00001502FunctionPass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001503 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001504}