blob: 5554facca3f6413e4fc44953a6498ab51cb03e1c [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 Lattner483e14e2002-04-27 07:27:19 +000012#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h" // FIXME: Remove when modularized better
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>
Chris Lattner697954c2002-01-20 22:54:45 +000017using std::cerr;
18using std::vector;
Vikram S. Advec5b46322001-09-30 23:43:34 +000019
Chris Lattner70e60cb2002-05-22 17:08:27 +000020SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000021
Chris Lattner5ff62e92002-07-22 02:10:13 +000022static cl::opt<SchedDebugLevel_t, true>
23SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
24 cl::desc("enable instruction scheduling debugging information"),
25 cl::values(
26 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000027 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
28 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
29 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
30 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000031
32
Vikram S. Advec5b46322001-09-30 23:43:34 +000033//************************* Internal Data Types *****************************/
34
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000035class InstrSchedule;
36class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000037
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000038
39//----------------------------------------------------------------------
40// class InstrGroup:
41//
42// Represents a group of instructions scheduled to be issued
43// in a single cycle.
44//----------------------------------------------------------------------
45
46class InstrGroup: public NonCopyable {
47public:
48 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
49 assert(slotNum < group.size());
50 return group[slotNum];
51 }
52
53private:
54 friend class InstrSchedule;
55
56 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
57 assert(slotNum < group.size());
58 group[slotNum] = node;
59 }
60
61 /*ctor*/ InstrGroup(unsigned int nslots)
62 : group(nslots, NULL) {}
63
64 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
65
66private:
67 vector<const SchedGraphNode*> group;
68};
69
70
71//----------------------------------------------------------------------
72// class ScheduleIterator:
73//
74// Iterates over the machine instructions in the for a single basic block.
75// The schedule is represented by an InstrSchedule object.
76//----------------------------------------------------------------------
77
78template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000079class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000080private:
81 unsigned cycleNum;
82 unsigned slotNum;
83 const InstrSchedule& S;
84public:
85 typedef ScheduleIterator<_NodeType> _Self;
86
87 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
88 unsigned _cycleNum,
89 unsigned _slotNum)
90 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
91 skipToNextInstr();
92 }
93
94 /*ctor*/ inline ScheduleIterator(const _Self& x)
95 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
96
97 inline bool operator==(const _Self& x) const {
98 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
99 }
100
101 inline bool operator!=(const _Self& x) const { return !operator==(x); }
102
103 inline _NodeType* operator*() const {
104 assert(cycleNum < S.groups.size());
105 return (*S.groups[cycleNum])[slotNum];
106 }
107 inline _NodeType* operator->() const { return operator*(); }
108
109 _Self& operator++(); // Preincrement
110 inline _Self operator++(int) { // Postincrement
111 _Self tmp(*this); ++*this; return tmp;
112 }
113
114 static _Self begin(const InstrSchedule& _schedule);
115 static _Self end( const InstrSchedule& _schedule);
116
117private:
118 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
119 void skipToNextInstr();
120};
121
122
123//----------------------------------------------------------------------
124// class InstrSchedule:
125//
126// Represents the schedule of machine instructions for a single basic block.
127//----------------------------------------------------------------------
128
129class InstrSchedule: public NonCopyable {
130private:
131 const unsigned int nslots;
132 unsigned int numInstr;
133 vector<InstrGroup*> groups; // indexed by cycle number
134 vector<cycles_t> startTime; // indexed by node id
135
136public: // iterators
137 typedef ScheduleIterator<SchedGraphNode> iterator;
138 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
139
140 iterator begin();
141 const_iterator begin() const;
142 iterator end();
143 const_iterator end() const;
144
145public: // constructors and destructor
146 /*ctor*/ InstrSchedule (unsigned int _nslots,
147 unsigned int _numNodes);
148 /*dtor*/ ~InstrSchedule ();
149
150public: // accessor functions to query chosen schedule
151 const SchedGraphNode* getInstr (unsigned int slotNum,
152 cycles_t c) const {
153 const InstrGroup* igroup = this->getIGroup(c);
154 return (igroup == NULL)? NULL : (*igroup)[slotNum];
155 }
156
157 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000158 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000159 groups.resize(c+1);
160 if (groups[c] == NULL)
161 groups[c] = new InstrGroup(nslots);
162 return groups[c];
163 }
164
165 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000166 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000167 return groups[c];
168 }
169
170 inline cycles_t getStartTime (unsigned int nodeId) const {
171 assert(nodeId < startTime.size());
172 return startTime[nodeId];
173 }
174
175 unsigned int getNumInstructions() const {
176 return numInstr;
177 }
178
179 inline void scheduleInstr (const SchedGraphNode* node,
180 unsigned int slotNum,
181 cycles_t cycle) {
182 InstrGroup* igroup = this->getIGroup(cycle);
183 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
184 igroup->addInstr(node, slotNum);
185 assert(node->getNodeId() < startTime.size());
186 startTime[node->getNodeId()] = cycle;
187 ++numInstr;
188 }
189
190private:
191 friend class iterator;
192 friend class const_iterator;
193 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
194};
195
196
197/*ctor*/
198InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
199 : nslots(_nslots),
200 numInstr(0),
201 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
202 startTime(_numNodes, (cycles_t) -1) // set all to -1
203{
204}
205
206
207/*dtor*/
208InstrSchedule::~InstrSchedule()
209{
210 for (unsigned c=0, NC=groups.size(); c < NC; c++)
211 if (groups[c] != NULL)
212 delete groups[c]; // delete InstrGroup objects
213}
214
215
216template<class _NodeType>
217inline
218void
219ScheduleIterator<_NodeType>::skipToNextInstr()
220{
221 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
222 ++cycleNum; // skip cycles with no instructions
223
224 while (cycleNum < S.groups.size() &&
225 (*S.groups[cycleNum])[slotNum] == NULL)
226 {
227 ++slotNum;
228 if (slotNum == S.nslots)
229 {
230 ++cycleNum;
231 slotNum = 0;
232 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
233 ++cycleNum; // skip cycles with no instructions
234 }
235 }
236}
237
238template<class _NodeType>
239inline
240ScheduleIterator<_NodeType>&
241ScheduleIterator<_NodeType>::operator++() // Preincrement
242{
243 ++slotNum;
244 if (slotNum == S.nslots)
245 {
246 ++cycleNum;
247 slotNum = 0;
248 }
249 skipToNextInstr();
250 return *this;
251}
252
253template<class _NodeType>
254ScheduleIterator<_NodeType>
255ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
256{
257 return _Self(_schedule, 0, 0);
258}
259
260template<class _NodeType>
261ScheduleIterator<_NodeType>
262ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
263{
264 return _Self(_schedule, _schedule.groups.size(), 0);
265}
266
267InstrSchedule::iterator
268InstrSchedule::begin()
269{
270 return iterator::begin(*this);
271}
272
273InstrSchedule::const_iterator
274InstrSchedule::begin() const
275{
276 return const_iterator::begin(*this);
277}
278
279InstrSchedule::iterator
280InstrSchedule::end()
281{
282 return iterator::end(*this);
283}
284
285InstrSchedule::const_iterator
286InstrSchedule::end() const
287{
288 return const_iterator::end( *this);
289}
290
291
292//----------------------------------------------------------------------
293// class DelaySlotInfo:
294//
295// Record information about delay slots for a single branch instruction.
296// Delay slots are simply indexed by slot number 1 ... numDelaySlots
297//----------------------------------------------------------------------
298
299class DelaySlotInfo: public NonCopyable {
300private:
301 const SchedGraphNode* brNode;
302 unsigned int ndelays;
303 vector<const SchedGraphNode*> delayNodeVec;
304 cycles_t delayedNodeCycle;
305 unsigned int delayedNodeSlotNum;
306
307public:
308 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
309 unsigned _ndelays)
310 : brNode(_brNode), ndelays(_ndelays),
311 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
312
313 inline unsigned getNumDelays () {
314 return ndelays;
315 }
316
317 inline const vector<const SchedGraphNode*>& getDelayNodeVec() {
318 return delayNodeVec;
319 }
320
321 inline void addDelayNode (const SchedGraphNode* node) {
322 delayNodeVec.push_back(node);
323 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
324 }
325
326 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
327 delayedNodeCycle = cycle;
328 delayedNodeSlotNum = slotNum;
329 }
330
Vikram S. Advec5b46322001-09-30 23:43:34 +0000331 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000332};
333
334
335//----------------------------------------------------------------------
336// class SchedulingManager:
337//
338// Represents the schedule of machine instructions for a single basic block.
339//----------------------------------------------------------------------
340
341class SchedulingManager: public NonCopyable {
342public: // publicly accessible data members
343 const unsigned int nslots;
344 const MachineSchedInfo& schedInfo;
345 SchedPriorities& schedPrio;
346 InstrSchedule isched;
347
348private:
349 unsigned int totalInstrCount;
350 cycles_t curTime;
351 cycles_t nextEarliestIssueTime; // next cycle we can issue
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000352 vector<hash_set<const SchedGraphNode*> > choicesForSlot; // indexed by slot#
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000353 vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
354 vector<int> numInClass; // indexed by sched class
355 vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000356 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000357 // indexed by branch node ptr
358
359public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000360 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
361 SchedPriorities& schedPrio);
362 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000363 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000364 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
365 E = delaySlotInfoForBranches.end(); I != E; ++I)
366 delete I->second;
367 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000368
369 //----------------------------------------------------------------------
370 // Simplify access to the machine instruction info
371 //----------------------------------------------------------------------
372
373 inline const MachineInstrInfo& getInstrInfo () const {
374 return schedInfo.getInstrInfo();
375 }
376
377 //----------------------------------------------------------------------
378 // Interface for checking and updating the current time
379 //----------------------------------------------------------------------
380
381 inline cycles_t getTime () const {
382 return curTime;
383 }
384
385 inline cycles_t getEarliestIssueTime() const {
386 return nextEarliestIssueTime;
387 }
388
389 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
390 assert(opCode < (int) nextEarliestStartTime.size());
391 return nextEarliestStartTime[opCode];
392 }
393
394 // Update current time to specified cycle
395 inline void updateTime (cycles_t c) {
396 curTime = c;
397 schedPrio.updateTime(c);
398 }
399
400 //----------------------------------------------------------------------
401 // Functions to manage the choices for the current cycle including:
402 // -- a vector of choices by priority (choiceVec)
403 // -- vectors of the choices for each instruction slot (choicesForSlot[])
404 // -- number of choices in each sched class, used to check issue conflicts
405 // between choices for a single cycle
406 //----------------------------------------------------------------------
407
408 inline unsigned int getNumChoices () const {
409 return choiceVec.size();
410 }
411
412 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000413 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000414 return numInClass[sc];
415 }
416
417 inline const SchedGraphNode* getChoice(unsigned int i) const {
418 // assert(i < choiceVec.size()); don't check here.
419 return choiceVec[i];
420 }
421
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000422 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000423 assert(slotNum < nslots);
424 return choicesForSlot[slotNum];
425 }
426
427 inline void addChoice (const SchedGraphNode* node) {
428 // Append the instruction to the vector of choices for current cycle.
429 // Increment numInClass[c] for the sched class to which the instr belongs.
430 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000431 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000432 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000433 numInClass[sc]++;
434 }
435
436 inline void addChoiceToSlot (unsigned int slotNum,
437 const SchedGraphNode* node) {
438 // Add the instruction to the choice set for the specified slot
439 assert(slotNum < nslots);
440 choicesForSlot[slotNum].insert(node);
441 }
442
443 inline void resetChoices () {
444 choiceVec.clear();
445 for (unsigned int s=0; s < nslots; s++)
446 choicesForSlot[s].clear();
447 for (unsigned int c=0; c < numInClass.size(); c++)
448 numInClass[c] = 0;
449 }
450
451 //----------------------------------------------------------------------
452 // Code to query and manage the partial instruction schedule so far
453 //----------------------------------------------------------------------
454
455 inline unsigned int getNumScheduled () const {
456 return isched.getNumInstructions();
457 }
458
459 inline unsigned int getNumUnscheduled() const {
460 return totalInstrCount - isched.getNumInstructions();
461 }
462
463 inline bool isScheduled (const SchedGraphNode* node) const {
464 return (isched.getStartTime(node->getNodeId()) >= 0);
465 }
466
467 inline void scheduleInstr (const SchedGraphNode* node,
468 unsigned int slotNum,
469 cycles_t cycle)
470 {
471 assert(! isScheduled(node) && "Instruction already scheduled?");
472
473 // add the instruction to the schedule
474 isched.scheduleInstr(node, slotNum, cycle);
475
476 // update the earliest start times of all nodes that conflict with `node'
477 // and the next-earliest time anything can issue if `node' causes bubbles
478 updateEarliestStartTimes(node, cycle);
479
480 // remove the instruction from the choice sets for all slots
481 for (unsigned s=0; s < nslots; s++)
482 choicesForSlot[s].erase(node);
483
484 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000485 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000486 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000487 numInClass[sc]--;
488 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000489
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000490 //----------------------------------------------------------------------
491 // Create and retrieve delay slot info for delayed instructions
492 //----------------------------------------------------------------------
493
494 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
495 bool createIfMissing=false)
496 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000497 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000498 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000499 if (I != delaySlotInfoForBranches.end())
500 return I->second;
501
502 if (!createIfMissing) return 0;
503
504 DelaySlotInfo *dinfo =
505 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
506 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000507 }
508
509private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000510 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
511 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000512};
513
514
515/*ctor*/
516SchedulingManager::SchedulingManager(const TargetMachine& target,
517 const SchedGraph* graph,
518 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000519 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
520 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000521 schedPrio(_schedPrio),
522 isched(nslots, graph->getNumNodes()),
523 totalInstrCount(graph->getNumNodes() - 2),
524 nextEarliestIssueTime(0),
525 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000526 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000527 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
528 (cycles_t) 0) // set all to 0
529{
530 updateTime(0);
531
532 // Note that an upper bound on #choices for each slot is = nslots since
533 // we use this vector to hold a feasible set of instructions, and more
534 // would be infeasible. Reserve that much memory since it is probably small.
535 for (unsigned int i=0; i < nslots; i++)
536 choicesForSlot[i].resize(nslots);
537}
538
539
540void
541SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
542 cycles_t schedTime)
543{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000544 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000545 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000546 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000547 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000548 }
549
Vikram S. Adve1632e882002-10-13 00:40:37 +0000550 const std::vector<MachineOpCode>&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000551 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000552
Vikram S. Adve1632e882002-10-13 00:40:37 +0000553 for (unsigned i=0; i < conflictVec.size(); i++)
554 {
555 MachineOpCode toOp = conflictVec[i];
556 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp);
557 assert(toOp < (int) nextEarliestStartTime.size());
558 if (nextEarliestStartTime[toOp] < est)
559 nextEarliestStartTime[toOp] = est;
560 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000561}
562
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000563//************************* Internal Functions *****************************/
564
565
566static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000567AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000568{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000569 // find the slot to start from, in the current cycle
570 unsigned int startSlot = 0;
571 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000572
Vikram S. Advec5b46322001-09-30 23:43:34 +0000573 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000574
Vikram S. Advec5b46322001-09-30 23:43:34 +0000575 // If only one instruction can be issued, do so.
576 if (maxIssue == 1)
577 for (unsigned s=startSlot; s < S.nslots; s++)
578 if (S.getChoicesForSlot(s).size() > 0)
579 {// found the one instruction
580 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
581 return;
582 }
583
584 // Otherwise, choose from the choices for each slot
585 //
586 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
587 assert(igroup != NULL && "Group creation failed?");
588
589 // Find a slot that has only a single choice, and take it.
590 // If all slots have 0 or multiple choices, pick the first slot with
591 // choices and use its last instruction (just to avoid shifting the vector).
592 unsigned numIssued;
593 for (numIssued = 0; numIssued < maxIssue; numIssued++)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000594 {
Chris Lattner697954c2002-01-20 22:54:45 +0000595 int chosenSlot = -1;
Vikram S. Advec5b46322001-09-30 23:43:34 +0000596 for (unsigned s=startSlot; s < S.nslots; s++)
597 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000598 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000599 chosenSlot = (int) s;
600 break;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000601 }
602
Vikram S. Advec5b46322001-09-30 23:43:34 +0000603 if (chosenSlot == -1)
604 for (unsigned s=startSlot; s < S.nslots; s++)
605 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0)
606 {
607 chosenSlot = (int) s;
608 break;
609 }
610
611 if (chosenSlot != -1)
612 { // Insert the chosen instr in the chosen slot and
613 // erase it from all slots.
614 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
615 S.scheduleInstr(node, chosenSlot, curTime);
616 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000617 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000618
619 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000620}
621
622
623//
624// For now, just assume we are scheduling within a single basic block.
625// Get the machine instruction vector for the basic block and clear it,
626// then append instructions in scheduled order.
627// Also, re-insert the dummy PHI instructions that were at the beginning
628// of the basic block, since they are not part of the schedule.
629//
630static void
631RecordSchedule(const BasicBlock* bb, const SchedulingManager& S)
632{
Chris Lattner55291ea2002-10-28 01:41:47 +0000633 MachineBasicBlock& mvec = MachineBasicBlock::get(bb);
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000634 const MachineInstrInfo& mii = S.schedInfo.getInstrInfo();
635
636#ifndef NDEBUG
637 // Lets make sure we didn't lose any instructions, except possibly
638 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
639 unsigned numInstr = 0;
Chris Lattner55291ea2002-10-28 01:41:47 +0000640 for (MachineBasicBlock::iterator I=mvec.begin(); I != mvec.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000641 if (! mii.isNop((*I)->getOpCode()) &&
642 ! mii.isDummyPhiInstr((*I)->getOpCode()))
643 ++numInstr;
644 assert(S.isched.getNumInstructions() >= numInstr &&
645 "Lost some non-NOP instructions during scheduling!");
646#endif
647
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000648 if (S.isched.getNumInstructions() == 0)
649 return; // empty basic block!
650
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000651 // First find the dummy instructions at the start of the basic block
Chris Lattner55291ea2002-10-28 01:41:47 +0000652 MachineBasicBlock::iterator I = mvec.begin();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000653 for ( ; I != mvec.end(); ++I)
654 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
655 break;
656
657 // Erase all except the dummy PHI instructions from mvec, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000658 // pre-allocate create space for the ones we will put back in.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000659 mvec.erase(I, mvec.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000660
661 InstrSchedule::const_iterator NIend = S.isched.end();
662 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattner2e530932001-09-09 19:41:52 +0000663 mvec.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000664}
665
666
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000667
668static void
669MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
670{
671 // Check if any successors are now ready that were not already marked
672 // ready before, and that have not yet been scheduled.
673 //
674 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
675 if (! (*SI)->isDummyNode()
676 && ! S.isScheduled(*SI)
677 && ! S.schedPrio.nodeIsReady(*SI))
678 {// successor not scheduled and not marked ready; check *its* preds.
679
680 bool succIsReady = true;
681 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
682 if (! (*P)->isDummyNode()
683 && ! S.isScheduled(*P))
684 {
685 succIsReady = false;
686 break;
687 }
688
689 if (succIsReady) // add the successor to the ready list
690 S.schedPrio.insertReady(*SI);
691 }
692}
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--)
715 if ((*igroup)[s] != NULL)
716 {
717 startSlot = s+1;
718 break;
719 }
720
721 // Make sure we pick at most one instruction that would break the group.
722 // Also, if we do pick one, remember which it was.
723 unsigned int indexForBreakingNode = S.nslots;
724 unsigned int indexForDelayedInstr = S.nslots;
725 DelaySlotInfo* delaySlotInfo = NULL;
726
727 getDelaySlotInfo = NULL;
728
729 // Choose instructions in order of priority.
730 // Add choices to the choice vector in the SchedulingManager class as
731 // we choose them so that subsequent choices will be correctly tested
732 // for feasibility, w.r.t. higher priority choices for the same cycle.
733 //
734 while (S.getNumChoices() < S.nslots - startSlot)
735 {
736 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
737 if (nextNode == NULL)
738 break; // no more instructions for this cycle
739
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000740 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000741 {
742 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
743 if (delaySlotInfo != NULL)
744 {
745 if (indexForBreakingNode < S.nslots)
746 // cannot issue a delayed instr in the same cycle as one
747 // that breaks the issue group or as another delayed instr
748 nextNode = NULL;
749 else
750 indexForDelayedInstr = S.getNumChoices();
751 }
752 }
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000753 else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000754 {
755 if (indexForBreakingNode < S.nslots)
756 // have a breaking instruction already so throw this one away
757 nextNode = NULL;
758 else
759 indexForBreakingNode = S.getNumChoices();
760 }
761
762 if (nextNode != NULL)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000763 {
764 S.addChoice(nextNode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000765
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000766 if (S.schedInfo.isSingleIssue(nextNode->getOpCode()))
767 {
768 assert(S.getNumChoices() == 1 &&
769 "Prioritizer returned invalid instr for this cycle!");
770 break;
771 }
772 }
773
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000774 if (indexForDelayedInstr < S.nslots)
775 break; // leave the rest for delay slots
776 }
777
778 assert(S.getNumChoices() <= S.nslots);
779 assert(! (indexForDelayedInstr < S.nslots &&
780 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
781
782 // Assign each chosen instruction to all possible slots for that instr.
783 // But if only one instruction was chosen, put it only in the first
784 // feasible slot; no more analysis will be needed.
785 //
786 if (indexForDelayedInstr >= S.nslots &&
787 indexForBreakingNode >= S.nslots)
788 { // No instructions that break the issue group or that have delay slots.
789 // This is the common case, so handle it separately for efficiency.
790
791 if (S.getNumChoices() == 1)
792 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000793 MachineOpCode opCode = S.getChoice(0)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000794 unsigned int s;
795 for (s=startSlot; s < S.nslots; s++)
796 if (S.schedInfo.instrCanUseSlot(opCode, s))
797 break;
798 assert(s < S.nslots && "No feasible slot for this opCode?");
799 S.addChoiceToSlot(s, S.getChoice(0));
800 }
801 else
802 {
803 for (unsigned i=0; i < S.getNumChoices(); i++)
804 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000805 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000806 for (unsigned int s=startSlot; s < S.nslots; s++)
807 if (S.schedInfo.instrCanUseSlot(opCode, s))
808 S.addChoiceToSlot(s, S.getChoice(i));
809 }
810 }
811 }
812 else if (indexForDelayedInstr < S.nslots)
813 {
814 // There is an instruction that needs delay slots.
815 // Try to assign that instruction to a higher slot than any other
816 // instructions in the group, so that its delay slots can go
817 // right after it.
818 //
819
820 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
821 "Instruction with delay slots should be last choice!");
822 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
823
824 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000825 MachineOpCode delayOpCode = delayedNode->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000826 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
827
828 unsigned delayedNodeSlot = S.nslots;
829 int highestSlotUsed;
830
831 // Find the last possible slot for the delayed instruction that leaves
832 // at least `d' slots vacant after it (d = #delay slots)
833 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
834 if (S.schedInfo.instrCanUseSlot(delayOpCode, s))
835 {
836 delayedNodeSlot = s;
837 break;
838 }
839
840 highestSlotUsed = -1;
841 for (unsigned i=0; i < S.getNumChoices() - 1; i++)
842 {
843 // Try to assign every other instruction to a lower numbered
844 // slot than delayedNodeSlot.
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000845 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000846 bool noSlotFound = true;
847 unsigned int s;
848 for (s=startSlot; s < delayedNodeSlot; s++)
849 if (S.schedInfo.instrCanUseSlot(opCode, s))
850 {
851 S.addChoiceToSlot(s, S.getChoice(i));
852 noSlotFound = false;
853 }
854
855 // No slot before `delayedNodeSlot' was found for this opCode
856 // Use a later slot, and allow some delay slots to fall in
857 // the next cycle.
858 if (noSlotFound)
859 for ( ; s < S.nslots; s++)
860 if (S.schedInfo.instrCanUseSlot(opCode, s))
861 {
862 S.addChoiceToSlot(s, S.getChoice(i));
863 break;
864 }
865
866 assert(s < S.nslots && "No feasible slot for instruction?");
867
Chris Lattner697954c2002-01-20 22:54:45 +0000868 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000869 }
870
871 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
872
873 // We will put the delayed node in the first slot after the
874 // highest slot used. But we just mark that for now, and
875 // schedule it separately because we want to schedule the delay
876 // slots for the node at the same time.
877 cycles_t dcycle = S.getTime();
878 unsigned int dslot = highestSlotUsed + 1;
879 if (dslot == S.nslots)
880 {
881 dslot = 0;
882 ++dcycle;
883 }
884 delaySlotInfo->recordChosenSlot(dcycle, dslot);
885 getDelaySlotInfo = delaySlotInfo;
886 }
887 else
888 { // There is an instruction that breaks the issue group.
889 // For such an instruction, assign to the last possible slot in
890 // the current group, and then don't assign any other instructions
891 // to later slots.
892 assert(indexForBreakingNode < S.nslots);
893 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
894 unsigned breakingSlot = INT_MAX;
895 unsigned int nslotsToUse = S.nslots;
896
897 // Find the last possible slot for this instruction.
898 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000899 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000900 {
901 breakingSlot = s;
902 break;
903 }
904 assert(breakingSlot < S.nslots &&
905 "No feasible slot for `breakingNode'?");
906
907 // Higher priority instructions than the one that breaks the group:
908 // These can be assigned to all slots, but will be assigned only
909 // to earlier slots if possible.
910 for (unsigned i=0;
911 i < S.getNumChoices() && i < indexForBreakingNode; i++)
912 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000913 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000914
915 // If a higher priority instruction cannot be assigned to
916 // any earlier slots, don't schedule the breaking instruction.
917 //
918 bool foundLowerSlot = false;
919 nslotsToUse = S.nslots; // May be modified in the loop
920 for (unsigned int s=startSlot; s < nslotsToUse; s++)
921 if (S.schedInfo.instrCanUseSlot(opCode, s))
922 {
923 if (breakingSlot < S.nslots && s < breakingSlot)
924 {
925 foundLowerSlot = true;
926 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
927 }
928
929 S.addChoiceToSlot(s, S.getChoice(i));
930 }
931
932 if (!foundLowerSlot)
933 breakingSlot = INT_MAX; // disable breaking instr
934 }
935
936 // Assign the breaking instruction (if any) to a single slot
937 // Otherwise, just ignore the instruction. It will simply be
938 // scheduled in a later cycle.
939 if (breakingSlot < S.nslots)
940 {
941 S.addChoiceToSlot(breakingSlot, breakingNode);
942 nslotsToUse = breakingSlot;
943 }
944 else
945 nslotsToUse = S.nslots;
946
947 // For lower priority instructions than the one that breaks the
948 // group, only assign them to slots lower than the breaking slot.
949 // Otherwise, just ignore the instruction.
950 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++)
951 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000952 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000953 for (unsigned int s=startSlot; s < nslotsToUse; s++)
954 if (S.schedInfo.instrCanUseSlot(opCode, s))
955 S.addChoiceToSlot(s, S.getChoice(i));
956 }
957 } // endif (no delay slots and no breaking slots)
958
959 return S.getNumChoices();
960}
961
962
Vikram S. Advec5b46322001-09-30 23:43:34 +0000963static unsigned
964ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000965{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000966 assert(S.schedPrio.getNumReady() > 0
967 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000968
Vikram S. Advec5b46322001-09-30 23:43:34 +0000969 cycles_t firstCycle = S.getTime();
970 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000971
Vikram S. Advec5b46322001-09-30 23:43:34 +0000972 // Choose up to `nslots' feasible instructions and their possible slots.
973 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000974
Vikram S. Advec5b46322001-09-30 23:43:34 +0000975 while (numIssued == 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000976 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000977 S.updateTime(S.getTime()+1);
978 numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000979 }
980
Vikram S. Advec5b46322001-09-30 23:43:34 +0000981 AssignInstructionsToSlots(S, numIssued);
982
983 if (getDelaySlotInfo != NULL)
984 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
985
986 // Print trace of scheduled instructions before newly ready ones
987 if (SchedDebugLevel >= Sched_PrintSchedTrace)
988 {
989 for (cycles_t c = firstCycle; c <= S.getTime(); c++)
990 {
Chris Lattner697954c2002-01-20 22:54:45 +0000991 cerr << " Cycle " << (long)c << " : Scheduled instructions:\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000992 const InstrGroup* igroup = S.isched.getIGroup(c);
993 for (unsigned int s=0; s < S.nslots; s++)
994 {
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << " ";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000996 if ((*igroup)[s] != NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000997 cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000998 else
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << "<none>\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +00001000 }
1001 }
1002 }
1003
1004 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001005}
1006
1007
Vikram S. Advec5b46322001-09-30 23:43:34 +00001008static void
1009ForwardListSchedule(SchedulingManager& S)
1010{
1011 unsigned N;
1012 const SchedGraphNode* node;
1013
1014 S.schedPrio.initialize();
1015
1016 while ((N = S.schedPrio.getNumReady()) > 0)
1017 {
1018 cycles_t nextCycle = S.getTime();
1019
1020 // Choose one group of instructions for a cycle, plus any delay slot
1021 // instructions (which may overflow into successive cycles).
1022 // This will advance S.getTime() to the last cycle in which
1023 // instructions are actually issued.
1024 //
1025 unsigned numIssued = ChooseOneGroup(S);
1026 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
1027
1028 // Notify the priority manager of scheduled instructions and mark
1029 // any successors that may now be ready
1030 //
1031 for (cycles_t c = nextCycle; c <= S.getTime(); c++)
1032 {
1033 const InstrGroup* igroup = S.isched.getIGroup(c);
1034 for (unsigned int s=0; s < S.nslots; s++)
1035 if ((node = (*igroup)[s]) != NULL)
1036 {
1037 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1038 MarkSuccessorsReady(S, node);
1039 }
1040 }
1041
1042 // Move to the next the next earliest cycle for which
1043 // an instruction can be issued, or the next earliest in which
1044 // one will be ready, or to the next cycle, whichever is latest.
1045 //
Chris Lattner697954c2002-01-20 22:54:45 +00001046 S.updateTime(std::max(S.getTime() + 1,
1047 std::max(S.getEarliestIssueTime(),
1048 S.schedPrio.getEarliestReadyTime())));
Vikram S. Advec5b46322001-09-30 23:43:34 +00001049 }
1050}
1051
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001052
1053//---------------------------------------------------------------------
1054// Code for filling delay slots for delayed terminator instructions
1055// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1056// instructions (e.g., CALL) are not handled here because they almost
1057// always can be filled with instructions from the call sequence code
1058// before a call. That's preferable because we incur many tradeoffs here
1059// when we cannot find single-cycle instructions that can be reordered.
1060//----------------------------------------------------------------------
1061
Vikram S. Advec5b46322001-09-30 23:43:34 +00001062static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001063NodeCanFillDelaySlot(const SchedulingManager& S,
1064 const SchedGraphNode* node,
1065 const SchedGraphNode* brNode,
1066 bool nodeIsPredecessor)
1067{
1068 assert(! node->isDummyNode());
1069
1070 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001071 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001072 return false;
1073
1074 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001075 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001076 return false;
1077
1078 // don't put a load-use dependence in the delay slot of a branch
1079 const MachineInstrInfo& mii = S.getInstrInfo();
1080
1081 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1082 EI != node->endInEdges(); ++EI)
1083 if (! (*EI)->getSrc()->isDummyNode()
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001084 && mii.isLoad((*EI)->getSrc()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001085 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1086 return false;
1087
1088 // for now, don't put an instruction that does not have operand
1089 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001090 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001091 return false;
1092
1093 // Finally, if the instruction preceeds the branch, we make sure the
1094 // instruction can be reordered relative to the branch. We simply check
1095 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1096 //
1097 if (nodeIsPredecessor)
1098 {
1099 bool onlyCDEdgeToBranch = true;
1100 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1101 OEI != node->endOutEdges(); ++OEI)
1102 if (! (*OEI)->getSink()->isDummyNode()
1103 && ((*OEI)->getSink() != brNode
1104 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1105 {
1106 onlyCDEdgeToBranch = false;
1107 break;
1108 }
1109
1110 if (!onlyCDEdgeToBranch)
1111 return false;
1112 }
1113
1114 return true;
1115}
1116
1117
Vikram S. Advec5b46322001-09-30 23:43:34 +00001118static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001119MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001120 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001121 SchedGraphNode* node,
1122 const SchedGraphNode* brNode,
1123 bool nodeIsPredecessor)
1124{
1125 if (nodeIsPredecessor)
1126 { // If node is in the same basic block (i.e., preceeds brNode),
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001127 // remove it and all its incident edges from the graph. Make sure we
1128 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1129 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001130 }
1131 else
1132 { // If the node was from a target block, add the node to the graph
1133 // and add a CD edge from brNode to node.
1134 assert(0 && "NOT IMPLEMENTED YET");
1135 }
1136
1137 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1138 dinfo->addDelayNode(node);
1139}
1140
1141
Vikram S. Advec5b46322001-09-30 23:43:34 +00001142void
1143FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1144 SchedGraphNode* brNode,
1145 vector<SchedGraphNode*>& sdelayNodeVec)
1146{
1147 const MachineInstrInfo& mii = S.getInstrInfo();
1148 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001149 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001150
1151 if (ndelays == 0)
1152 return;
1153
1154 sdelayNodeVec.reserve(ndelays);
1155
1156 // Use a separate vector to hold the feasible multi-cycle nodes.
1157 // These will be used if not enough single-cycle nodes are found.
1158 //
1159 vector<SchedGraphNode*> mdelayNodeVec;
1160
1161 for (sg_pred_iterator P = pred_begin(brNode);
1162 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1163 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001164 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001165 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
1166 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001167 if (mii.maxLatency((*P)->getOpCode()) > 1)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001168 mdelayNodeVec.push_back(*P);
1169 else
1170 sdelayNodeVec.push_back(*P);
1171 }
1172
1173 // If not enough single-cycle instructions were found, select the
1174 // lowest-latency multi-cycle instructions and use them.
1175 // Note that this is the most efficient code when only 1 (or even 2)
1176 // values need to be selected.
1177 //
1178 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
1179 {
1180 unsigned lmin =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001181 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001182 unsigned minIndex = 0;
1183 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
1184 {
1185 unsigned li =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001186 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001187 if (lmin >= li)
1188 {
1189 lmin = li;
1190 minIndex = i;
1191 }
1192 }
1193 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1194 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1195 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1196 }
1197}
1198
1199
1200// Remove the NOPs currently in delay slots from the graph.
1201// Mark instructions specified in sdelayNodeVec to replace them.
1202// If not enough useful instructions were found, mark the NOPs to be used
1203// for filling delay slots, otherwise, otherwise just discard them.
1204//
1205void
1206ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1207 SchedGraphNode* node,
1208 vector<SchedGraphNode*> sdelayNodeVec,
1209 SchedGraph* graph)
1210{
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001211 vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Vikram S. Advec5b46322001-09-30 23:43:34 +00001212 const MachineInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001213 const MachineInstr* brInstr = node->getMachineInstr();
1214 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001215 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1216
1217 // Remove the NOPs currently in delay slots from the graph.
1218 // If not enough useful instructions were found, use the NOPs to
1219 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001220 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001221 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattner55291ea2002-10-28 01:41:47 +00001222 MachineBasicBlock& bbMvec = MachineBasicBlock::get(node->getBB());
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001223 assert(bbMvec[firstDelaySlotIdx - 1] == brInstr &&
1224 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001225
1226 // First find all useful instructions already in the delay slots
1227 // and USE THEM. We'll throw away the unused alternatives below
1228 //
1229 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001230 if (! mii.isNop(bbMvec[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001231 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001232 graph->getGraphNodeForInstr(bbMvec[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001233
1234 // Then find the NOPs and keep only as many as are needed.
1235 // Put the rest in nopNodeVec to be deleted.
1236 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001237 if (mii.isNop(bbMvec[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001238 if (sdelayNodeVec.size() < ndelays)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001239 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001240 else
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001241 {
1242 nopNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
1243
1244 //remove the MI from the Machine Code For Instruction
1245 MachineCodeForInstruction& llvmMvec =
1246 MachineCodeForInstruction::get((Instruction *)
1247 (node->getBB()->getTerminator()));
1248 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1249 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1250 if(*mciI==bbMvec[i])
1251 llvmMvec.erase(mciI);
1252 }
1253 }
1254
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001255 assert(sdelayNodeVec.size() >= ndelays);
1256
1257 // If some delay slots were already filled, throw away that many new choices
1258 if (sdelayNodeVec.size() > ndelays)
1259 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001260
1261 // Mark the nodes chosen for delay slots. This removes them from the graph.
1262 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1263 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1264
1265 // And remove the unused NOPs from the graph.
1266 for (unsigned i=0; i < nopNodeVec.size(); i++)
1267 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1268}
1269
1270
1271// For all delayed instructions, choose instructions to put in the delay
1272// slots and pull those out of the graph. Mark them for the delay slots
1273// in the DelaySlotInfo object for that graph node. If no useful work
1274// is found for a delay slot, use the NOP that is currently in that slot.
1275//
1276// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001277// EXCEPT CALLS AND RETURNS.
1278// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001279// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001280// suboptimal. Also, it complicates generating the calling sequence code in
1281// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001282//
1283static void
1284ChooseInstructionsForDelaySlots(SchedulingManager& S,
Chris Lattner3462cae2002-02-03 07:28:30 +00001285 const BasicBlock *bb,
1286 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001287{
1288 const MachineInstrInfo& mii = S.getInstrInfo();
Chris Lattner455889a2002-02-12 22:39:50 +00001289 const Instruction *termInstr = (Instruction*)bb->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001290 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001291 vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001292 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001293
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001294 if (termInstr->getOpcode() != Instruction::Ret)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001295 {
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001296 // To find instructions that need delay slots without searching the full
1297 // machine code, we assume that the only delayed instructions are CALLs
1298 // or instructions generated for the terminator inst.
1299 // Find the first branch instr in the sequence of machine instrs for term
1300 //
1301 unsigned first = 0;
1302 while (first < termMvec.size() &&
1303 ! mii.isBranch(termMvec[first]->getOpCode()))
1304 {
1305 ++first;
1306 }
1307 assert(first < termMvec.size() &&
1308 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1309
1310 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1311
1312 // Compute a vector of the nodes chosen for delay slots and then
1313 // mark delay slots to replace NOPs with these useful instructions.
1314 //
1315 if (brInstr != NULL)
1316 {
1317 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1318 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1319 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1320 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001321 }
1322
1323 // Also mark delay slots for other delayed instructions to hold NOPs.
1324 // Simply passing in an empty delayNodeVec will have this effect.
1325 //
1326 delayNodeVec.clear();
Chris Lattner55291ea2002-10-28 01:41:47 +00001327 const MachineBasicBlock& bbMvec = MachineBasicBlock::get(bb);
1328 for (unsigned i=0; i < bbMvec.size(); ++i)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001329 if (bbMvec[i] != brInstr &&
1330 mii.getNumDelaySlots(bbMvec[i]->getOpCode()) > 0)
1331 {
1332 SchedGraphNode* node = graph->getGraphNodeForInstr(bbMvec[i]);
1333 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1334 }
1335}
1336
1337
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001338//
1339// Schedule the delayed branch and its delay slots
1340//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001341unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001342DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1343{
1344 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1345 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1346 && "Slot for branch should be empty");
1347
1348 unsigned int nextSlot = delayedNodeSlotNum;
1349 cycles_t nextTime = delayedNodeCycle;
1350
1351 S.scheduleInstr(brNode, nextSlot, nextTime);
1352
1353 for (unsigned d=0; d < ndelays; d++)
1354 {
1355 ++nextSlot;
1356 if (nextSlot == S.nslots)
1357 {
1358 nextSlot = 0;
1359 nextTime++;
1360 }
1361
1362 // Find the first feasible instruction for this delay slot
1363 // Note that we only check for issue restrictions here.
1364 // We do *not* check for flow dependences but rely on pipeline
1365 // interlocks to resolve them. Machines without interlocks
1366 // will require this code to be modified.
1367 for (unsigned i=0; i < delayNodeVec.size(); i++)
1368 {
1369 const SchedGraphNode* dnode = delayNodeVec[i];
1370 if ( ! S.isScheduled(dnode)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001371 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1372 && instrIsFeasible(S, dnode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001373 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001374 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001375 && "Instructions without interlocks not yet supported "
1376 "when filling branch delay slots");
1377 S.scheduleInstr(dnode, nextSlot, nextTime);
1378 break;
1379 }
1380 }
1381 }
1382
1383 // Update current time if delay slots overflowed into later cycles.
1384 // Do this here because we know exactly which cycle is the last cycle
1385 // that contains delay slots. The next loop doesn't compute that.
1386 if (nextTime > S.getTime())
1387 S.updateTime(nextTime);
1388
1389 // Now put any remaining instructions in the unfilled delay slots.
1390 // This could lead to suboptimal performance but needed for correctness.
1391 nextSlot = delayedNodeSlotNum;
1392 nextTime = delayedNodeCycle;
1393 for (unsigned i=0; i < delayNodeVec.size(); i++)
1394 if (! S.isScheduled(delayNodeVec[i]))
1395 {
1396 do { // find the next empty slot
1397 ++nextSlot;
1398 if (nextSlot == S.nslots)
1399 {
1400 nextSlot = 0;
1401 nextTime++;
1402 }
1403 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
1404
1405 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1406 break;
1407 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001408
1409 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001410}
1411
Vikram S. Advec5b46322001-09-30 23:43:34 +00001412
1413// Check if the instruction would conflict with instructions already
1414// chosen for the current cycle
1415//
1416static inline bool
1417ConflictsWithChoices(const SchedulingManager& S,
1418 MachineOpCode opCode)
1419{
1420 // Check if the instruction must issue by itself, and some feasible
1421 // choices have already been made for this cycle
1422 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1423 return true;
1424
1425 // For each class that opCode belongs to, check if there are too many
1426 // instructions of that class.
1427 //
1428 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1429 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1430}
1431
1432
1433//************************* External Functions *****************************/
1434
1435
1436//---------------------------------------------------------------------------
1437// Function: ViolatesMinimumGap
1438//
1439// Purpose:
1440// Check minimum gap requirements relative to instructions scheduled in
1441// previous cycles.
1442// Note that we do not need to consider `nextEarliestIssueTime' here because
1443// that is also captured in the earliest start times for each opcode.
1444//---------------------------------------------------------------------------
1445
1446static inline bool
1447ViolatesMinimumGap(const SchedulingManager& S,
1448 MachineOpCode opCode,
1449 const cycles_t inCycle)
1450{
1451 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1452}
1453
1454
1455//---------------------------------------------------------------------------
1456// Function: instrIsFeasible
1457//
1458// Purpose:
1459// Check if any issue restrictions would prevent the instruction from
1460// being issued in the current cycle
1461//---------------------------------------------------------------------------
1462
1463bool
1464instrIsFeasible(const SchedulingManager& S,
1465 MachineOpCode opCode)
1466{
1467 // skip the instruction if it cannot be issued due to issue restrictions
1468 // caused by previously issued instructions
1469 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1470 return false;
1471
1472 // skip the instruction if it cannot be issued due to issue restrictions
1473 // caused by previously chosen instructions for the current cycle
1474 if (ConflictsWithChoices(S, opCode))
1475 return false;
1476
1477 return true;
1478}
1479
1480//---------------------------------------------------------------------------
1481// Function: ScheduleInstructionsWithSSA
1482//
1483// Purpose:
1484// Entry point for instruction scheduling on SSA form.
1485// Schedules the machine instructions generated by instruction selection.
1486// Assumes that register allocation has not been done, i.e., operands
1487// are still in SSA form.
1488//---------------------------------------------------------------------------
1489
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001490namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001491 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001492 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001493 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001494 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001495
1496 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001497
Chris Lattnerf57b8452002-04-27 06:56:12 +00001498 // getAnalysisUsage - We use LiveVarInfo...
1499 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001500 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001501 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001502 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001503
Chris Lattner7e708292002-06-25 16:13:24 +00001504 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001505 };
1506} // end anonymous namespace
1507
Vikram S. Adve802cec42002-03-24 03:44:55 +00001508
Chris Lattner7e708292002-06-25 16:13:24 +00001509bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001510{
Chris Lattner7e708292002-06-25 16:13:24 +00001511 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001512
1513 if (SchedDebugLevel >= Sched_PrintSchedGraphs)
1514 {
1515 cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
1516 graphSet.dump();
1517 }
1518
1519 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1520 GI != GE; ++GI)
1521 {
1522 SchedGraph* graph = (*GI);
1523 const vector<const BasicBlock*> &bbvec = graph->getBasicBlocks();
1524 assert(bbvec.size() == 1 && "Cannot schedule multiple basic blocks");
1525 const BasicBlock* bb = bbvec[0];
1526
1527 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1528 cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
1529
1530 // expensive!
Chris Lattner7e708292002-06-25 16:13:24 +00001531 SchedPriorities schedPrio(&F, graph,getAnalysis<FunctionLiveVarInfo>());
Vikram S. Adve802cec42002-03-24 03:44:55 +00001532 SchedulingManager S(target, graph, schedPrio);
1533
1534 ChooseInstructionsForDelaySlots(S, bb, graph); // modifies graph
1535
1536 ForwardListSchedule(S); // computes schedule in S
1537
1538 RecordSchedule(bb, S); // records schedule in BB
1539 }
1540
1541 if (SchedDebugLevel >= Sched_PrintMachineCode)
1542 {
1543 cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
Misha Brukmanfce11432002-10-28 00:28:31 +00001544 MachineFunction::get(&F).dump();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001545 }
1546
1547 return false;
1548}
1549
1550
Chris Lattnerf57b8452002-04-27 06:56:12 +00001551Pass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001552 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001553}