blob: 97360c3dd087593bae044dc6e540d756babb83ab [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"
Vikram S. Adve0baf1c02002-07-08 22:59:23 +000011#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000012#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner483e14e2002-04-27 07:27:19 +000013#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h" // FIXME: Remove when modularized better
Chris Lattner3462cae2002-02-03 07:28:30 +000014#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000015#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000016#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000017#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000018using std::cerr;
19using std::vector;
Vikram S. Advec5b46322001-09-30 23:43:34 +000020
Chris Lattner70e60cb2002-05-22 17:08:27 +000021SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000022
Chris Lattner5ff62e92002-07-22 02:10:13 +000023static cl::opt<SchedDebugLevel_t, true>
24SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
25 cl::desc("enable instruction scheduling debugging information"),
26 cl::values(
27 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
28 clEnumValN(Sched_Disable, "off", "disable instruction scheduling"),
29 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
30 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
31 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
32 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000033
34
Vikram S. Advec5b46322001-09-30 23:43:34 +000035//************************* Internal Data Types *****************************/
36
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000037class InstrSchedule;
38class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000039
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000040
41//----------------------------------------------------------------------
42// class InstrGroup:
43//
44// Represents a group of instructions scheduled to be issued
45// in a single cycle.
46//----------------------------------------------------------------------
47
48class InstrGroup: public NonCopyable {
49public:
50 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
51 assert(slotNum < group.size());
52 return group[slotNum];
53 }
54
55private:
56 friend class InstrSchedule;
57
58 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
59 assert(slotNum < group.size());
60 group[slotNum] = node;
61 }
62
63 /*ctor*/ InstrGroup(unsigned int nslots)
64 : group(nslots, NULL) {}
65
66 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
67
68private:
69 vector<const SchedGraphNode*> group;
70};
71
72
73//----------------------------------------------------------------------
74// class ScheduleIterator:
75//
76// Iterates over the machine instructions in the for a single basic block.
77// The schedule is represented by an InstrSchedule object.
78//----------------------------------------------------------------------
79
80template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000081class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000082private:
83 unsigned cycleNum;
84 unsigned slotNum;
85 const InstrSchedule& S;
86public:
87 typedef ScheduleIterator<_NodeType> _Self;
88
89 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
90 unsigned _cycleNum,
91 unsigned _slotNum)
92 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
93 skipToNextInstr();
94 }
95
96 /*ctor*/ inline ScheduleIterator(const _Self& x)
97 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
98
99 inline bool operator==(const _Self& x) const {
100 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
101 }
102
103 inline bool operator!=(const _Self& x) const { return !operator==(x); }
104
105 inline _NodeType* operator*() const {
106 assert(cycleNum < S.groups.size());
107 return (*S.groups[cycleNum])[slotNum];
108 }
109 inline _NodeType* operator->() const { return operator*(); }
110
111 _Self& operator++(); // Preincrement
112 inline _Self operator++(int) { // Postincrement
113 _Self tmp(*this); ++*this; return tmp;
114 }
115
116 static _Self begin(const InstrSchedule& _schedule);
117 static _Self end( const InstrSchedule& _schedule);
118
119private:
120 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
121 void skipToNextInstr();
122};
123
124
125//----------------------------------------------------------------------
126// class InstrSchedule:
127//
128// Represents the schedule of machine instructions for a single basic block.
129//----------------------------------------------------------------------
130
131class InstrSchedule: public NonCopyable {
132private:
133 const unsigned int nslots;
134 unsigned int numInstr;
135 vector<InstrGroup*> groups; // indexed by cycle number
136 vector<cycles_t> startTime; // indexed by node id
137
138public: // iterators
139 typedef ScheduleIterator<SchedGraphNode> iterator;
140 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
141
142 iterator begin();
143 const_iterator begin() const;
144 iterator end();
145 const_iterator end() const;
146
147public: // constructors and destructor
148 /*ctor*/ InstrSchedule (unsigned int _nslots,
149 unsigned int _numNodes);
150 /*dtor*/ ~InstrSchedule ();
151
152public: // accessor functions to query chosen schedule
153 const SchedGraphNode* getInstr (unsigned int slotNum,
154 cycles_t c) const {
155 const InstrGroup* igroup = this->getIGroup(c);
156 return (igroup == NULL)? NULL : (*igroup)[slotNum];
157 }
158
159 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000160 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000161 groups.resize(c+1);
162 if (groups[c] == NULL)
163 groups[c] = new InstrGroup(nslots);
164 return groups[c];
165 }
166
167 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000168 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000169 return groups[c];
170 }
171
172 inline cycles_t getStartTime (unsigned int nodeId) const {
173 assert(nodeId < startTime.size());
174 return startTime[nodeId];
175 }
176
177 unsigned int getNumInstructions() const {
178 return numInstr;
179 }
180
181 inline void scheduleInstr (const SchedGraphNode* node,
182 unsigned int slotNum,
183 cycles_t cycle) {
184 InstrGroup* igroup = this->getIGroup(cycle);
185 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
186 igroup->addInstr(node, slotNum);
187 assert(node->getNodeId() < startTime.size());
188 startTime[node->getNodeId()] = cycle;
189 ++numInstr;
190 }
191
192private:
193 friend class iterator;
194 friend class const_iterator;
195 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
196};
197
198
199/*ctor*/
200InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
201 : nslots(_nslots),
202 numInstr(0),
203 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
204 startTime(_numNodes, (cycles_t) -1) // set all to -1
205{
206}
207
208
209/*dtor*/
210InstrSchedule::~InstrSchedule()
211{
212 for (unsigned c=0, NC=groups.size(); c < NC; c++)
213 if (groups[c] != NULL)
214 delete groups[c]; // delete InstrGroup objects
215}
216
217
218template<class _NodeType>
219inline
220void
221ScheduleIterator<_NodeType>::skipToNextInstr()
222{
223 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
224 ++cycleNum; // skip cycles with no instructions
225
226 while (cycleNum < S.groups.size() &&
227 (*S.groups[cycleNum])[slotNum] == NULL)
228 {
229 ++slotNum;
230 if (slotNum == S.nslots)
231 {
232 ++cycleNum;
233 slotNum = 0;
234 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
235 ++cycleNum; // skip cycles with no instructions
236 }
237 }
238}
239
240template<class _NodeType>
241inline
242ScheduleIterator<_NodeType>&
243ScheduleIterator<_NodeType>::operator++() // Preincrement
244{
245 ++slotNum;
246 if (slotNum == S.nslots)
247 {
248 ++cycleNum;
249 slotNum = 0;
250 }
251 skipToNextInstr();
252 return *this;
253}
254
255template<class _NodeType>
256ScheduleIterator<_NodeType>
257ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
258{
259 return _Self(_schedule, 0, 0);
260}
261
262template<class _NodeType>
263ScheduleIterator<_NodeType>
264ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
265{
266 return _Self(_schedule, _schedule.groups.size(), 0);
267}
268
269InstrSchedule::iterator
270InstrSchedule::begin()
271{
272 return iterator::begin(*this);
273}
274
275InstrSchedule::const_iterator
276InstrSchedule::begin() const
277{
278 return const_iterator::begin(*this);
279}
280
281InstrSchedule::iterator
282InstrSchedule::end()
283{
284 return iterator::end(*this);
285}
286
287InstrSchedule::const_iterator
288InstrSchedule::end() const
289{
290 return const_iterator::end( *this);
291}
292
293
294//----------------------------------------------------------------------
295// class DelaySlotInfo:
296//
297// Record information about delay slots for a single branch instruction.
298// Delay slots are simply indexed by slot number 1 ... numDelaySlots
299//----------------------------------------------------------------------
300
301class DelaySlotInfo: public NonCopyable {
302private:
303 const SchedGraphNode* brNode;
304 unsigned int ndelays;
305 vector<const SchedGraphNode*> delayNodeVec;
306 cycles_t delayedNodeCycle;
307 unsigned int delayedNodeSlotNum;
308
309public:
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
319 inline const vector<const SchedGraphNode*>& getDelayNodeVec() {
320 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
343class SchedulingManager: public NonCopyable {
344public: // publicly accessible data members
345 const unsigned int nslots;
346 const MachineSchedInfo& schedInfo;
347 SchedPriorities& schedPrio;
348 InstrSchedule isched;
349
350private:
351 unsigned int totalInstrCount;
352 cycles_t curTime;
353 cycles_t nextEarliestIssueTime; // next cycle we can issue
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000354 vector<hash_set<const SchedGraphNode*> > choicesForSlot; // indexed by slot#
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000355 vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
356 vector<int> numInClass; // indexed by sched class
357 vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000358 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000359 // indexed by branch node ptr
360
361public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000362 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
363 SchedPriorities& schedPrio);
364 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000365 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000366 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
367 E = delaySlotInfoForBranches.end(); I != E; ++I)
368 delete I->second;
369 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000370
371 //----------------------------------------------------------------------
372 // Simplify access to the machine instruction info
373 //----------------------------------------------------------------------
374
375 inline const MachineInstrInfo& getInstrInfo () const {
376 return schedInfo.getInstrInfo();
377 }
378
379 //----------------------------------------------------------------------
380 // Interface for checking and updating the current time
381 //----------------------------------------------------------------------
382
383 inline cycles_t getTime () const {
384 return curTime;
385 }
386
387 inline cycles_t getEarliestIssueTime() const {
388 return nextEarliestIssueTime;
389 }
390
391 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
392 assert(opCode < (int) nextEarliestStartTime.size());
393 return nextEarliestStartTime[opCode];
394 }
395
396 // Update current time to specified cycle
397 inline void updateTime (cycles_t c) {
398 curTime = c;
399 schedPrio.updateTime(c);
400 }
401
402 //----------------------------------------------------------------------
403 // Functions to manage the choices for the current cycle including:
404 // -- a vector of choices by priority (choiceVec)
405 // -- vectors of the choices for each instruction slot (choicesForSlot[])
406 // -- number of choices in each sched class, used to check issue conflicts
407 // between choices for a single cycle
408 //----------------------------------------------------------------------
409
410 inline unsigned int getNumChoices () const {
411 return choiceVec.size();
412 }
413
414 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
415 assert(sc < (int) numInClass.size() && "Invalid op code or sched class!");
416 return numInClass[sc];
417 }
418
419 inline const SchedGraphNode* getChoice(unsigned int i) const {
420 // assert(i < choiceVec.size()); don't check here.
421 return choiceVec[i];
422 }
423
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000424 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000425 assert(slotNum < nslots);
426 return choicesForSlot[slotNum];
427 }
428
429 inline void addChoice (const SchedGraphNode* node) {
430 // Append the instruction to the vector of choices for current cycle.
431 // Increment numInClass[c] for the sched class to which the instr belongs.
432 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000433 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000434 assert(sc < (int) numInClass.size());
435 numInClass[sc]++;
436 }
437
438 inline void addChoiceToSlot (unsigned int slotNum,
439 const SchedGraphNode* node) {
440 // Add the instruction to the choice set for the specified slot
441 assert(slotNum < nslots);
442 choicesForSlot[slotNum].insert(node);
443 }
444
445 inline void resetChoices () {
446 choiceVec.clear();
447 for (unsigned int s=0; s < nslots; s++)
448 choicesForSlot[s].clear();
449 for (unsigned int c=0; c < numInClass.size(); c++)
450 numInClass[c] = 0;
451 }
452
453 //----------------------------------------------------------------------
454 // Code to query and manage the partial instruction schedule so far
455 //----------------------------------------------------------------------
456
457 inline unsigned int getNumScheduled () const {
458 return isched.getNumInstructions();
459 }
460
461 inline unsigned int getNumUnscheduled() const {
462 return totalInstrCount - isched.getNumInstructions();
463 }
464
465 inline bool isScheduled (const SchedGraphNode* node) const {
466 return (isched.getStartTime(node->getNodeId()) >= 0);
467 }
468
469 inline void scheduleInstr (const SchedGraphNode* node,
470 unsigned int slotNum,
471 cycles_t cycle)
472 {
473 assert(! isScheduled(node) && "Instruction already scheduled?");
474
475 // add the instruction to the schedule
476 isched.scheduleInstr(node, slotNum, cycle);
477
478 // update the earliest start times of all nodes that conflict with `node'
479 // and the next-earliest time anything can issue if `node' causes bubbles
480 updateEarliestStartTimes(node, cycle);
481
482 // remove the instruction from the choice sets for all slots
483 for (unsigned s=0; s < nslots; s++)
484 choicesForSlot[s].erase(node);
485
486 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000487 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000488 assert(sc < (int) numInClass.size());
489 numInClass[sc]--;
490 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000491
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000492 //----------------------------------------------------------------------
493 // Create and retrieve delay slot info for delayed instructions
494 //----------------------------------------------------------------------
495
496 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
497 bool createIfMissing=false)
498 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000499 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000500 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000501 if (I != delaySlotInfoForBranches.end())
502 return I->second;
503
504 if (!createIfMissing) return 0;
505
506 DelaySlotInfo *dinfo =
507 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
508 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000509 }
510
511private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000512 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
513 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000514};
515
516
517/*ctor*/
518SchedulingManager::SchedulingManager(const TargetMachine& target,
519 const SchedGraph* graph,
520 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000521 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
522 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000523 schedPrio(_schedPrio),
524 isched(nslots, graph->getNumNodes()),
525 totalInstrCount(graph->getNumNodes() - 2),
526 nextEarliestIssueTime(0),
527 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000528 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000529 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
530 (cycles_t) 0) // set all to 0
531{
532 updateTime(0);
533
534 // Note that an upper bound on #choices for each slot is = nslots since
535 // we use this vector to hold a feasible set of instructions, and more
536 // would be infeasible. Reserve that much memory since it is probably small.
537 for (unsigned int i=0; i < nslots; i++)
538 choicesForSlot[i].resize(nslots);
539}
540
541
542void
543SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
544 cycles_t schedTime)
545{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000546 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000547 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000548 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000549 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000550 }
551
552 const vector<MachineOpCode>*
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000553 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000554
555 if (conflictVec != NULL)
556 for (unsigned i=0; i < conflictVec->size(); i++)
557 {
558 MachineOpCode toOp = (*conflictVec)[i];
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000559 cycles_t est = schedTime + schedInfo.getMinIssueGap(node->getOpCode(),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000560 toOp);
561 assert(toOp < (int) nextEarliestStartTime.size());
562 if (nextEarliestStartTime[toOp] < est)
563 nextEarliestStartTime[toOp] = est;
564 }
565}
566
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000567//************************* Internal Functions *****************************/
568
569
570static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000571AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000572{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000573 // find the slot to start from, in the current cycle
574 unsigned int startSlot = 0;
575 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000576
Vikram S. Advec5b46322001-09-30 23:43:34 +0000577 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000578
Vikram S. Advec5b46322001-09-30 23:43:34 +0000579 // If only one instruction can be issued, do so.
580 if (maxIssue == 1)
581 for (unsigned s=startSlot; s < S.nslots; s++)
582 if (S.getChoicesForSlot(s).size() > 0)
583 {// found the one instruction
584 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
585 return;
586 }
587
588 // Otherwise, choose from the choices for each slot
589 //
590 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
591 assert(igroup != NULL && "Group creation failed?");
592
593 // Find a slot that has only a single choice, and take it.
594 // If all slots have 0 or multiple choices, pick the first slot with
595 // choices and use its last instruction (just to avoid shifting the vector).
596 unsigned numIssued;
597 for (numIssued = 0; numIssued < maxIssue; numIssued++)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000598 {
Chris Lattner697954c2002-01-20 22:54:45 +0000599 int chosenSlot = -1;
Vikram S. Advec5b46322001-09-30 23:43:34 +0000600 for (unsigned s=startSlot; s < S.nslots; s++)
601 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000602 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000603 chosenSlot = (int) s;
604 break;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000605 }
606
Vikram S. Advec5b46322001-09-30 23:43:34 +0000607 if (chosenSlot == -1)
608 for (unsigned s=startSlot; s < S.nslots; s++)
609 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0)
610 {
611 chosenSlot = (int) s;
612 break;
613 }
614
615 if (chosenSlot != -1)
616 { // Insert the chosen instr in the chosen slot and
617 // erase it from all slots.
618 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
619 S.scheduleInstr(node, chosenSlot, curTime);
620 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000621 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000622
623 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000624}
625
626
627//
628// For now, just assume we are scheduling within a single basic block.
629// Get the machine instruction vector for the basic block and clear it,
630// then append instructions in scheduled order.
631// Also, re-insert the dummy PHI instructions that were at the beginning
632// of the basic block, since they are not part of the schedule.
633//
634static void
635RecordSchedule(const BasicBlock* bb, const SchedulingManager& S)
636{
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000637 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(bb);
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000638 const MachineInstrInfo& mii = S.schedInfo.getInstrInfo();
639
640#ifndef NDEBUG
641 // Lets make sure we didn't lose any instructions, except possibly
642 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
643 unsigned numInstr = 0;
644 for (MachineCodeForBasicBlock::iterator I=mvec.begin(); I != mvec.end(); ++I)
645 if (! mii.isNop((*I)->getOpCode()) &&
646 ! mii.isDummyPhiInstr((*I)->getOpCode()))
647 ++numInstr;
648 assert(S.isched.getNumInstructions() >= numInstr &&
649 "Lost some non-NOP instructions during scheduling!");
650#endif
651
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000652 if (S.isched.getNumInstructions() == 0)
653 return; // empty basic block!
654
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000655 // First find the dummy instructions at the start of the basic block
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000656 MachineCodeForBasicBlock::iterator I = mvec.begin();
657 for ( ; I != mvec.end(); ++I)
658 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
659 break;
660
661 // Erase all except the dummy PHI instructions from mvec, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000662 // pre-allocate create space for the ones we will put back in.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000663 mvec.erase(I, mvec.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000664
665 InstrSchedule::const_iterator NIend = S.isched.end();
666 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattner2e530932001-09-09 19:41:52 +0000667 mvec.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000668}
669
670
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000671
672static void
673MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
674{
675 // Check if any successors are now ready that were not already marked
676 // ready before, and that have not yet been scheduled.
677 //
678 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
679 if (! (*SI)->isDummyNode()
680 && ! S.isScheduled(*SI)
681 && ! S.schedPrio.nodeIsReady(*SI))
682 {// successor not scheduled and not marked ready; check *its* preds.
683
684 bool succIsReady = true;
685 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
686 if (! (*P)->isDummyNode()
687 && ! S.isScheduled(*P))
688 {
689 succIsReady = false;
690 break;
691 }
692
693 if (succIsReady) // add the successor to the ready list
694 S.schedPrio.insertReady(*SI);
695 }
696}
697
698
699// Choose up to `nslots' FEASIBLE instructions and assign each
700// instruction to all possible slots that do not violate feasibility.
701// FEASIBLE means it should be guaranteed that the set
702// of chosen instructions can be issued in a single group.
703//
704// Return value:
705// maxIssue : total number of feasible instructions
706// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
707//
708static unsigned
709FindSlotChoices(SchedulingManager& S,
710 DelaySlotInfo*& getDelaySlotInfo)
711{
712 // initialize result vectors to empty
713 S.resetChoices();
714
715 // find the slot to start from, in the current cycle
716 unsigned int startSlot = 0;
717 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
718 for (int s = S.nslots - 1; s >= 0; s--)
719 if ((*igroup)[s] != NULL)
720 {
721 startSlot = s+1;
722 break;
723 }
724
725 // Make sure we pick at most one instruction that would break the group.
726 // Also, if we do pick one, remember which it was.
727 unsigned int indexForBreakingNode = S.nslots;
728 unsigned int indexForDelayedInstr = S.nslots;
729 DelaySlotInfo* delaySlotInfo = NULL;
730
731 getDelaySlotInfo = NULL;
732
733 // Choose instructions in order of priority.
734 // Add choices to the choice vector in the SchedulingManager class as
735 // we choose them so that subsequent choices will be correctly tested
736 // for feasibility, w.r.t. higher priority choices for the same cycle.
737 //
738 while (S.getNumChoices() < S.nslots - startSlot)
739 {
740 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
741 if (nextNode == NULL)
742 break; // no more instructions for this cycle
743
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000744 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000745 {
746 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
747 if (delaySlotInfo != NULL)
748 {
749 if (indexForBreakingNode < S.nslots)
750 // cannot issue a delayed instr in the same cycle as one
751 // that breaks the issue group or as another delayed instr
752 nextNode = NULL;
753 else
754 indexForDelayedInstr = S.getNumChoices();
755 }
756 }
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000757 else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000758 {
759 if (indexForBreakingNode < S.nslots)
760 // have a breaking instruction already so throw this one away
761 nextNode = NULL;
762 else
763 indexForBreakingNode = S.getNumChoices();
764 }
765
766 if (nextNode != NULL)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000767 {
768 S.addChoice(nextNode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000769
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000770 if (S.schedInfo.isSingleIssue(nextNode->getOpCode()))
771 {
772 assert(S.getNumChoices() == 1 &&
773 "Prioritizer returned invalid instr for this cycle!");
774 break;
775 }
776 }
777
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000778 if (indexForDelayedInstr < S.nslots)
779 break; // leave the rest for delay slots
780 }
781
782 assert(S.getNumChoices() <= S.nslots);
783 assert(! (indexForDelayedInstr < S.nslots &&
784 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
785
786 // Assign each chosen instruction to all possible slots for that instr.
787 // But if only one instruction was chosen, put it only in the first
788 // feasible slot; no more analysis will be needed.
789 //
790 if (indexForDelayedInstr >= S.nslots &&
791 indexForBreakingNode >= S.nslots)
792 { // No instructions that break the issue group or that have delay slots.
793 // This is the common case, so handle it separately for efficiency.
794
795 if (S.getNumChoices() == 1)
796 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000797 MachineOpCode opCode = S.getChoice(0)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000798 unsigned int s;
799 for (s=startSlot; s < S.nslots; s++)
800 if (S.schedInfo.instrCanUseSlot(opCode, s))
801 break;
802 assert(s < S.nslots && "No feasible slot for this opCode?");
803 S.addChoiceToSlot(s, S.getChoice(0));
804 }
805 else
806 {
807 for (unsigned i=0; i < S.getNumChoices(); i++)
808 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000809 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000810 for (unsigned int s=startSlot; s < S.nslots; s++)
811 if (S.schedInfo.instrCanUseSlot(opCode, s))
812 S.addChoiceToSlot(s, S.getChoice(i));
813 }
814 }
815 }
816 else if (indexForDelayedInstr < S.nslots)
817 {
818 // There is an instruction that needs delay slots.
819 // Try to assign that instruction to a higher slot than any other
820 // instructions in the group, so that its delay slots can go
821 // right after it.
822 //
823
824 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
825 "Instruction with delay slots should be last choice!");
826 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
827
828 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000829 MachineOpCode delayOpCode = delayedNode->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000830 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
831
832 unsigned delayedNodeSlot = S.nslots;
833 int highestSlotUsed;
834
835 // Find the last possible slot for the delayed instruction that leaves
836 // at least `d' slots vacant after it (d = #delay slots)
837 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
838 if (S.schedInfo.instrCanUseSlot(delayOpCode, s))
839 {
840 delayedNodeSlot = s;
841 break;
842 }
843
844 highestSlotUsed = -1;
845 for (unsigned i=0; i < S.getNumChoices() - 1; i++)
846 {
847 // Try to assign every other instruction to a lower numbered
848 // slot than delayedNodeSlot.
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000849 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000850 bool noSlotFound = true;
851 unsigned int s;
852 for (s=startSlot; s < delayedNodeSlot; s++)
853 if (S.schedInfo.instrCanUseSlot(opCode, s))
854 {
855 S.addChoiceToSlot(s, S.getChoice(i));
856 noSlotFound = false;
857 }
858
859 // No slot before `delayedNodeSlot' was found for this opCode
860 // Use a later slot, and allow some delay slots to fall in
861 // the next cycle.
862 if (noSlotFound)
863 for ( ; s < S.nslots; s++)
864 if (S.schedInfo.instrCanUseSlot(opCode, s))
865 {
866 S.addChoiceToSlot(s, S.getChoice(i));
867 break;
868 }
869
870 assert(s < S.nslots && "No feasible slot for instruction?");
871
Chris Lattner697954c2002-01-20 22:54:45 +0000872 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000873 }
874
875 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
876
877 // We will put the delayed node in the first slot after the
878 // highest slot used. But we just mark that for now, and
879 // schedule it separately because we want to schedule the delay
880 // slots for the node at the same time.
881 cycles_t dcycle = S.getTime();
882 unsigned int dslot = highestSlotUsed + 1;
883 if (dslot == S.nslots)
884 {
885 dslot = 0;
886 ++dcycle;
887 }
888 delaySlotInfo->recordChosenSlot(dcycle, dslot);
889 getDelaySlotInfo = delaySlotInfo;
890 }
891 else
892 { // There is an instruction that breaks the issue group.
893 // For such an instruction, assign to the last possible slot in
894 // the current group, and then don't assign any other instructions
895 // to later slots.
896 assert(indexForBreakingNode < S.nslots);
897 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
898 unsigned breakingSlot = INT_MAX;
899 unsigned int nslotsToUse = S.nslots;
900
901 // Find the last possible slot for this instruction.
902 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000903 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000904 {
905 breakingSlot = s;
906 break;
907 }
908 assert(breakingSlot < S.nslots &&
909 "No feasible slot for `breakingNode'?");
910
911 // Higher priority instructions than the one that breaks the group:
912 // These can be assigned to all slots, but will be assigned only
913 // to earlier slots if possible.
914 for (unsigned i=0;
915 i < S.getNumChoices() && i < indexForBreakingNode; i++)
916 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000917 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000918
919 // If a higher priority instruction cannot be assigned to
920 // any earlier slots, don't schedule the breaking instruction.
921 //
922 bool foundLowerSlot = false;
923 nslotsToUse = S.nslots; // May be modified in the loop
924 for (unsigned int s=startSlot; s < nslotsToUse; s++)
925 if (S.schedInfo.instrCanUseSlot(opCode, s))
926 {
927 if (breakingSlot < S.nslots && s < breakingSlot)
928 {
929 foundLowerSlot = true;
930 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
931 }
932
933 S.addChoiceToSlot(s, S.getChoice(i));
934 }
935
936 if (!foundLowerSlot)
937 breakingSlot = INT_MAX; // disable breaking instr
938 }
939
940 // Assign the breaking instruction (if any) to a single slot
941 // Otherwise, just ignore the instruction. It will simply be
942 // scheduled in a later cycle.
943 if (breakingSlot < S.nslots)
944 {
945 S.addChoiceToSlot(breakingSlot, breakingNode);
946 nslotsToUse = breakingSlot;
947 }
948 else
949 nslotsToUse = S.nslots;
950
951 // For lower priority instructions than the one that breaks the
952 // group, only assign them to slots lower than the breaking slot.
953 // Otherwise, just ignore the instruction.
954 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++)
955 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000956 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000957 for (unsigned int s=startSlot; s < nslotsToUse; s++)
958 if (S.schedInfo.instrCanUseSlot(opCode, s))
959 S.addChoiceToSlot(s, S.getChoice(i));
960 }
961 } // endif (no delay slots and no breaking slots)
962
963 return S.getNumChoices();
964}
965
966
Vikram S. Advec5b46322001-09-30 23:43:34 +0000967static unsigned
968ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000969{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000970 assert(S.schedPrio.getNumReady() > 0
971 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000972
Vikram S. Advec5b46322001-09-30 23:43:34 +0000973 cycles_t firstCycle = S.getTime();
974 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000975
Vikram S. Advec5b46322001-09-30 23:43:34 +0000976 // Choose up to `nslots' feasible instructions and their possible slots.
977 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000978
Vikram S. Advec5b46322001-09-30 23:43:34 +0000979 while (numIssued == 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000980 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000981 S.updateTime(S.getTime()+1);
982 numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000983 }
984
Vikram S. Advec5b46322001-09-30 23:43:34 +0000985 AssignInstructionsToSlots(S, numIssued);
986
987 if (getDelaySlotInfo != NULL)
988 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
989
990 // Print trace of scheduled instructions before newly ready ones
991 if (SchedDebugLevel >= Sched_PrintSchedTrace)
992 {
993 for (cycles_t c = firstCycle; c <= S.getTime(); c++)
994 {
Chris Lattner697954c2002-01-20 22:54:45 +0000995 cerr << " Cycle " << (long)c << " : Scheduled instructions:\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000996 const InstrGroup* igroup = S.isched.getIGroup(c);
997 for (unsigned int s=0; s < S.nslots; s++)
998 {
Chris Lattner697954c2002-01-20 22:54:45 +0000999 cerr << " ";
Vikram S. Advec5b46322001-09-30 23:43:34 +00001000 if ((*igroup)[s] != NULL)
Chris Lattner697954c2002-01-20 22:54:45 +00001001 cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +00001002 else
Chris Lattner697954c2002-01-20 22:54:45 +00001003 cerr << "<none>\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +00001004 }
1005 }
1006 }
1007
1008 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001009}
1010
1011
Vikram S. Advec5b46322001-09-30 23:43:34 +00001012static void
1013ForwardListSchedule(SchedulingManager& S)
1014{
1015 unsigned N;
1016 const SchedGraphNode* node;
1017
1018 S.schedPrio.initialize();
1019
1020 while ((N = S.schedPrio.getNumReady()) > 0)
1021 {
1022 cycles_t nextCycle = S.getTime();
1023
1024 // Choose one group of instructions for a cycle, plus any delay slot
1025 // instructions (which may overflow into successive cycles).
1026 // This will advance S.getTime() to the last cycle in which
1027 // instructions are actually issued.
1028 //
1029 unsigned numIssued = ChooseOneGroup(S);
1030 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
1031
1032 // Notify the priority manager of scheduled instructions and mark
1033 // any successors that may now be ready
1034 //
1035 for (cycles_t c = nextCycle; c <= S.getTime(); c++)
1036 {
1037 const InstrGroup* igroup = S.isched.getIGroup(c);
1038 for (unsigned int s=0; s < S.nslots; s++)
1039 if ((node = (*igroup)[s]) != NULL)
1040 {
1041 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1042 MarkSuccessorsReady(S, node);
1043 }
1044 }
1045
1046 // Move to the next the next earliest cycle for which
1047 // an instruction can be issued, or the next earliest in which
1048 // one will be ready, or to the next cycle, whichever is latest.
1049 //
Chris Lattner697954c2002-01-20 22:54:45 +00001050 S.updateTime(std::max(S.getTime() + 1,
1051 std::max(S.getEarliestIssueTime(),
1052 S.schedPrio.getEarliestReadyTime())));
Vikram S. Advec5b46322001-09-30 23:43:34 +00001053 }
1054}
1055
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001056
1057//---------------------------------------------------------------------
1058// Code for filling delay slots for delayed terminator instructions
1059// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1060// instructions (e.g., CALL) are not handled here because they almost
1061// always can be filled with instructions from the call sequence code
1062// before a call. That's preferable because we incur many tradeoffs here
1063// when we cannot find single-cycle instructions that can be reordered.
1064//----------------------------------------------------------------------
1065
Vikram S. Advec5b46322001-09-30 23:43:34 +00001066static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001067NodeCanFillDelaySlot(const SchedulingManager& S,
1068 const SchedGraphNode* node,
1069 const SchedGraphNode* brNode,
1070 bool nodeIsPredecessor)
1071{
1072 assert(! node->isDummyNode());
1073
1074 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001075 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001076 return false;
1077
1078 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001079 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001080 return false;
1081
1082 // don't put a load-use dependence in the delay slot of a branch
1083 const MachineInstrInfo& mii = S.getInstrInfo();
1084
1085 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1086 EI != node->endInEdges(); ++EI)
1087 if (! (*EI)->getSrc()->isDummyNode()
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001088 && mii.isLoad((*EI)->getSrc()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001089 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1090 return false;
1091
1092 // for now, don't put an instruction that does not have operand
1093 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001094 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001095 return false;
1096
1097 // Finally, if the instruction preceeds the branch, we make sure the
1098 // instruction can be reordered relative to the branch. We simply check
1099 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1100 //
1101 if (nodeIsPredecessor)
1102 {
1103 bool onlyCDEdgeToBranch = true;
1104 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1105 OEI != node->endOutEdges(); ++OEI)
1106 if (! (*OEI)->getSink()->isDummyNode()
1107 && ((*OEI)->getSink() != brNode
1108 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1109 {
1110 onlyCDEdgeToBranch = false;
1111 break;
1112 }
1113
1114 if (!onlyCDEdgeToBranch)
1115 return false;
1116 }
1117
1118 return true;
1119}
1120
1121
Vikram S. Advec5b46322001-09-30 23:43:34 +00001122static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001123MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001124 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001125 SchedGraphNode* node,
1126 const SchedGraphNode* brNode,
1127 bool nodeIsPredecessor)
1128{
1129 if (nodeIsPredecessor)
1130 { // If node is in the same basic block (i.e., preceeds brNode),
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001131 // remove it and all its incident edges from the graph. Make sure we
1132 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1133 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001134 }
1135 else
1136 { // If the node was from a target block, add the node to the graph
1137 // and add a CD edge from brNode to node.
1138 assert(0 && "NOT IMPLEMENTED YET");
1139 }
1140
1141 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1142 dinfo->addDelayNode(node);
1143}
1144
1145
Vikram S. Advec5b46322001-09-30 23:43:34 +00001146void
1147FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1148 SchedGraphNode* brNode,
1149 vector<SchedGraphNode*>& sdelayNodeVec)
1150{
1151 const MachineInstrInfo& mii = S.getInstrInfo();
1152 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001153 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001154
1155 if (ndelays == 0)
1156 return;
1157
1158 sdelayNodeVec.reserve(ndelays);
1159
1160 // Use a separate vector to hold the feasible multi-cycle nodes.
1161 // These will be used if not enough single-cycle nodes are found.
1162 //
1163 vector<SchedGraphNode*> mdelayNodeVec;
1164
1165 for (sg_pred_iterator P = pred_begin(brNode);
1166 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1167 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001168 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001169 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
1170 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001171 if (mii.maxLatency((*P)->getOpCode()) > 1)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001172 mdelayNodeVec.push_back(*P);
1173 else
1174 sdelayNodeVec.push_back(*P);
1175 }
1176
1177 // If not enough single-cycle instructions were found, select the
1178 // lowest-latency multi-cycle instructions and use them.
1179 // Note that this is the most efficient code when only 1 (or even 2)
1180 // values need to be selected.
1181 //
1182 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
1183 {
1184 unsigned lmin =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001185 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001186 unsigned minIndex = 0;
1187 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
1188 {
1189 unsigned li =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001190 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001191 if (lmin >= li)
1192 {
1193 lmin = li;
1194 minIndex = i;
1195 }
1196 }
1197 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1198 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1199 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1200 }
1201}
1202
1203
1204// Remove the NOPs currently in delay slots from the graph.
1205// Mark instructions specified in sdelayNodeVec to replace them.
1206// If not enough useful instructions were found, mark the NOPs to be used
1207// for filling delay slots, otherwise, otherwise just discard them.
1208//
1209void
1210ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1211 SchedGraphNode* node,
1212 vector<SchedGraphNode*> sdelayNodeVec,
1213 SchedGraph* graph)
1214{
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001215 vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Vikram S. Advec5b46322001-09-30 23:43:34 +00001216 const MachineInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001217 const MachineInstr* brInstr = node->getMachineInstr();
1218 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001219 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1220
1221 // Remove the NOPs currently in delay slots from the graph.
1222 // If not enough useful instructions were found, use the NOPs to
1223 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001224 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001225 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Vikram S. Adve0baf1c02002-07-08 22:59:23 +00001226 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(node->getBB());
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001227 assert(bbMvec[firstDelaySlotIdx - 1] == brInstr &&
1228 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001229
1230 // First find all useful instructions already in the delay slots
1231 // and USE THEM. We'll throw away the unused alternatives below
1232 //
1233 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001234 if (! mii.isNop(bbMvec[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001235 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001236 graph->getGraphNodeForInstr(bbMvec[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001237
1238 // Then find the NOPs and keep only as many as are needed.
1239 // Put the rest in nopNodeVec to be deleted.
1240 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001241 if (mii.isNop(bbMvec[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001242 if (sdelayNodeVec.size() < ndelays)
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001243 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001244 else
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001245 {
1246 nopNodeVec.push_back(graph->getGraphNodeForInstr(bbMvec[i]));
1247
1248 //remove the MI from the Machine Code For Instruction
1249 MachineCodeForInstruction& llvmMvec =
1250 MachineCodeForInstruction::get((Instruction *)
1251 (node->getBB()->getTerminator()));
1252 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1253 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1254 if(*mciI==bbMvec[i])
1255 llvmMvec.erase(mciI);
1256 }
1257 }
1258
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001259 assert(sdelayNodeVec.size() >= ndelays);
1260
1261 // If some delay slots were already filled, throw away that many new choices
1262 if (sdelayNodeVec.size() > ndelays)
1263 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001264
1265 // Mark the nodes chosen for delay slots. This removes them from the graph.
1266 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1267 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1268
1269 // And remove the unused NOPs from the graph.
1270 for (unsigned i=0; i < nopNodeVec.size(); i++)
1271 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1272}
1273
1274
1275// For all delayed instructions, choose instructions to put in the delay
1276// slots and pull those out of the graph. Mark them for the delay slots
1277// in the DelaySlotInfo object for that graph node. If no useful work
1278// is found for a delay slot, use the NOP that is currently in that slot.
1279//
1280// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001281// EXCEPT CALLS AND RETURNS.
1282// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001283// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001284// suboptimal. Also, it complicates generating the calling sequence code in
1285// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001286//
1287static void
1288ChooseInstructionsForDelaySlots(SchedulingManager& S,
Chris Lattner3462cae2002-02-03 07:28:30 +00001289 const BasicBlock *bb,
1290 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001291{
1292 const MachineInstrInfo& mii = S.getInstrInfo();
Chris Lattner455889a2002-02-12 22:39:50 +00001293 const Instruction *termInstr = (Instruction*)bb->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001294 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001295 vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001296 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001297
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001298 if (termInstr->getOpcode() != Instruction::Ret)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001299 {
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001300 // To find instructions that need delay slots without searching the full
1301 // machine code, we assume that the only delayed instructions are CALLs
1302 // or instructions generated for the terminator inst.
1303 // Find the first branch instr in the sequence of machine instrs for term
1304 //
1305 unsigned first = 0;
1306 while (first < termMvec.size() &&
1307 ! mii.isBranch(termMvec[first]->getOpCode()))
1308 {
1309 ++first;
1310 }
1311 assert(first < termMvec.size() &&
1312 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1313
1314 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1315
1316 // Compute a vector of the nodes chosen for delay slots and then
1317 // mark delay slots to replace NOPs with these useful instructions.
1318 //
1319 if (brInstr != NULL)
1320 {
1321 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1322 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1323 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1324 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001325 }
1326
1327 // Also mark delay slots for other delayed instructions to hold NOPs.
1328 // Simply passing in an empty delayNodeVec will have this effect.
1329 //
1330 delayNodeVec.clear();
Vikram S. Adve0baf1c02002-07-08 22:59:23 +00001331 const MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(bb);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001332 for (unsigned i=0; i < bbMvec.size(); i++)
1333 if (bbMvec[i] != brInstr &&
1334 mii.getNumDelaySlots(bbMvec[i]->getOpCode()) > 0)
1335 {
1336 SchedGraphNode* node = graph->getGraphNodeForInstr(bbMvec[i]);
1337 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1338 }
1339}
1340
1341
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001342//
1343// Schedule the delayed branch and its delay slots
1344//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001345unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001346DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1347{
1348 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1349 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1350 && "Slot for branch should be empty");
1351
1352 unsigned int nextSlot = delayedNodeSlotNum;
1353 cycles_t nextTime = delayedNodeCycle;
1354
1355 S.scheduleInstr(brNode, nextSlot, nextTime);
1356
1357 for (unsigned d=0; d < ndelays; d++)
1358 {
1359 ++nextSlot;
1360 if (nextSlot == S.nslots)
1361 {
1362 nextSlot = 0;
1363 nextTime++;
1364 }
1365
1366 // Find the first feasible instruction for this delay slot
1367 // Note that we only check for issue restrictions here.
1368 // We do *not* check for flow dependences but rely on pipeline
1369 // interlocks to resolve them. Machines without interlocks
1370 // will require this code to be modified.
1371 for (unsigned i=0; i < delayNodeVec.size(); i++)
1372 {
1373 const SchedGraphNode* dnode = delayNodeVec[i];
1374 if ( ! S.isScheduled(dnode)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001375 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1376 && instrIsFeasible(S, dnode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001377 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001378 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001379 && "Instructions without interlocks not yet supported "
1380 "when filling branch delay slots");
1381 S.scheduleInstr(dnode, nextSlot, nextTime);
1382 break;
1383 }
1384 }
1385 }
1386
1387 // Update current time if delay slots overflowed into later cycles.
1388 // Do this here because we know exactly which cycle is the last cycle
1389 // that contains delay slots. The next loop doesn't compute that.
1390 if (nextTime > S.getTime())
1391 S.updateTime(nextTime);
1392
1393 // Now put any remaining instructions in the unfilled delay slots.
1394 // This could lead to suboptimal performance but needed for correctness.
1395 nextSlot = delayedNodeSlotNum;
1396 nextTime = delayedNodeCycle;
1397 for (unsigned i=0; i < delayNodeVec.size(); i++)
1398 if (! S.isScheduled(delayNodeVec[i]))
1399 {
1400 do { // find the next empty slot
1401 ++nextSlot;
1402 if (nextSlot == S.nslots)
1403 {
1404 nextSlot = 0;
1405 nextTime++;
1406 }
1407 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
1408
1409 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1410 break;
1411 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001412
1413 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001414}
1415
Vikram S. Advec5b46322001-09-30 23:43:34 +00001416
1417// Check if the instruction would conflict with instructions already
1418// chosen for the current cycle
1419//
1420static inline bool
1421ConflictsWithChoices(const SchedulingManager& S,
1422 MachineOpCode opCode)
1423{
1424 // Check if the instruction must issue by itself, and some feasible
1425 // choices have already been made for this cycle
1426 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1427 return true;
1428
1429 // For each class that opCode belongs to, check if there are too many
1430 // instructions of that class.
1431 //
1432 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1433 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1434}
1435
1436
1437//************************* External Functions *****************************/
1438
1439
1440//---------------------------------------------------------------------------
1441// Function: ViolatesMinimumGap
1442//
1443// Purpose:
1444// Check minimum gap requirements relative to instructions scheduled in
1445// previous cycles.
1446// Note that we do not need to consider `nextEarliestIssueTime' here because
1447// that is also captured in the earliest start times for each opcode.
1448//---------------------------------------------------------------------------
1449
1450static inline bool
1451ViolatesMinimumGap(const SchedulingManager& S,
1452 MachineOpCode opCode,
1453 const cycles_t inCycle)
1454{
1455 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1456}
1457
1458
1459//---------------------------------------------------------------------------
1460// Function: instrIsFeasible
1461//
1462// Purpose:
1463// Check if any issue restrictions would prevent the instruction from
1464// being issued in the current cycle
1465//---------------------------------------------------------------------------
1466
1467bool
1468instrIsFeasible(const SchedulingManager& S,
1469 MachineOpCode opCode)
1470{
1471 // skip the instruction if it cannot be issued due to issue restrictions
1472 // caused by previously issued instructions
1473 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1474 return false;
1475
1476 // skip the instruction if it cannot be issued due to issue restrictions
1477 // caused by previously chosen instructions for the current cycle
1478 if (ConflictsWithChoices(S, opCode))
1479 return false;
1480
1481 return true;
1482}
1483
1484//---------------------------------------------------------------------------
1485// Function: ScheduleInstructionsWithSSA
1486//
1487// Purpose:
1488// Entry point for instruction scheduling on SSA form.
1489// Schedules the machine instructions generated by instruction selection.
1490// Assumes that register allocation has not been done, i.e., operands
1491// are still in SSA form.
1492//---------------------------------------------------------------------------
1493
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001494namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001495 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001496 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001497 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001498 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001499
1500 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001501
Chris Lattnerf57b8452002-04-27 06:56:12 +00001502 // getAnalysisUsage - We use LiveVarInfo...
1503 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001504 AU.addRequired<FunctionLiveVarInfo>();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001505 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001506
Chris Lattner7e708292002-06-25 16:13:24 +00001507 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001508 };
1509} // end anonymous namespace
1510
Vikram S. Adve802cec42002-03-24 03:44:55 +00001511
Chris Lattner7e708292002-06-25 16:13:24 +00001512bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001513{
1514 if (SchedDebugLevel == Sched_Disable)
1515 return false;
1516
Chris Lattner7e708292002-06-25 16:13:24 +00001517 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001518
1519 if (SchedDebugLevel >= Sched_PrintSchedGraphs)
1520 {
1521 cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
1522 graphSet.dump();
1523 }
1524
1525 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1526 GI != GE; ++GI)
1527 {
1528 SchedGraph* graph = (*GI);
1529 const vector<const BasicBlock*> &bbvec = graph->getBasicBlocks();
1530 assert(bbvec.size() == 1 && "Cannot schedule multiple basic blocks");
1531 const BasicBlock* bb = bbvec[0];
1532
1533 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1534 cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
1535
1536 // expensive!
Chris Lattner7e708292002-06-25 16:13:24 +00001537 SchedPriorities schedPrio(&F, graph,getAnalysis<FunctionLiveVarInfo>());
Vikram S. Adve802cec42002-03-24 03:44:55 +00001538 SchedulingManager S(target, graph, schedPrio);
1539
1540 ChooseInstructionsForDelaySlots(S, bb, graph); // modifies graph
1541
1542 ForwardListSchedule(S); // computes schedule in S
1543
1544 RecordSchedule(bb, S); // records schedule in BB
1545 }
1546
1547 if (SchedDebugLevel >= Sched_PrintMachineCode)
1548 {
1549 cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
Chris Lattner7e708292002-06-25 16:13:24 +00001550 MachineCodeForMethod::get(&F).dump();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001551 }
1552
1553 return false;
1554}
1555
1556
Chris Lattnerf57b8452002-04-27 06:56:12 +00001557Pass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001558 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001559}