blob: 4dae2534fde9036be83f1241fe4e47690f4049ee [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
2//
3// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
4// generic support routines for instruction scheduling.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +00007
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00009#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000010#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000011#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000012#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000014#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000015#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000016#include <algorithm>
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
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000343 const unsigned nslots;
344 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000345 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
Chris Lattner3501fea2003-01-14 22:00:31 +0000373 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000374 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
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000631RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000632{
Chris Lattner3501fea2003-01-14 22:00:31 +0000633 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000634
635#ifndef NDEBUG
636 // Lets make sure we didn't lose any instructions, except possibly
637 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
638 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000639 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000640 if (! mii.isNop((*I)->getOpCode()) &&
641 ! mii.isDummyPhiInstr((*I)->getOpCode()))
642 ++numInstr;
643 assert(S.isched.getNumInstructions() >= numInstr &&
644 "Lost some non-NOP instructions during scheduling!");
645#endif
646
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000647 if (S.isched.getNumInstructions() == 0)
648 return; // empty basic block!
649
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000650 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000651 MachineBasicBlock::iterator I = MBB.begin();
652 for ( ; I != MBB.end(); ++I)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000653 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
654 break;
655
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000656 // Erase all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000657 // pre-allocate create space for the ones we will put back in.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000658 MBB.erase(I, MBB.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000659
660 InstrSchedule::const_iterator NIend = S.isched.end();
661 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000662 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000663}
664
665
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000666
667static void
668MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
669{
670 // Check if any successors are now ready that were not already marked
671 // ready before, and that have not yet been scheduled.
672 //
673 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
674 if (! (*SI)->isDummyNode()
675 && ! S.isScheduled(*SI)
676 && ! S.schedPrio.nodeIsReady(*SI))
677 {// successor not scheduled and not marked ready; check *its* preds.
678
679 bool succIsReady = true;
680 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
681 if (! (*P)->isDummyNode()
682 && ! S.isScheduled(*P))
683 {
684 succIsReady = false;
685 break;
686 }
687
688 if (succIsReady) // add the successor to the ready list
689 S.schedPrio.insertReady(*SI);
690 }
691}
692
693
694// Choose up to `nslots' FEASIBLE instructions and assign each
695// instruction to all possible slots that do not violate feasibility.
696// FEASIBLE means it should be guaranteed that the set
697// of chosen instructions can be issued in a single group.
698//
699// Return value:
700// maxIssue : total number of feasible instructions
701// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
702//
703static unsigned
704FindSlotChoices(SchedulingManager& S,
705 DelaySlotInfo*& getDelaySlotInfo)
706{
707 // initialize result vectors to empty
708 S.resetChoices();
709
710 // find the slot to start from, in the current cycle
711 unsigned int startSlot = 0;
712 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
713 for (int s = S.nslots - 1; s >= 0; s--)
714 if ((*igroup)[s] != NULL)
715 {
716 startSlot = s+1;
717 break;
718 }
719
720 // Make sure we pick at most one instruction that would break the group.
721 // Also, if we do pick one, remember which it was.
722 unsigned int indexForBreakingNode = S.nslots;
723 unsigned int indexForDelayedInstr = S.nslots;
724 DelaySlotInfo* delaySlotInfo = NULL;
725
726 getDelaySlotInfo = NULL;
727
728 // Choose instructions in order of priority.
729 // Add choices to the choice vector in the SchedulingManager class as
730 // we choose them so that subsequent choices will be correctly tested
731 // for feasibility, w.r.t. higher priority choices for the same cycle.
732 //
733 while (S.getNumChoices() < S.nslots - startSlot)
734 {
735 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
736 if (nextNode == NULL)
737 break; // no more instructions for this cycle
738
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000739 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000740 {
741 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
742 if (delaySlotInfo != NULL)
743 {
744 if (indexForBreakingNode < S.nslots)
745 // cannot issue a delayed instr in the same cycle as one
746 // that breaks the issue group or as another delayed instr
747 nextNode = NULL;
748 else
749 indexForDelayedInstr = S.getNumChoices();
750 }
751 }
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000752 else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000753 {
754 if (indexForBreakingNode < S.nslots)
755 // have a breaking instruction already so throw this one away
756 nextNode = NULL;
757 else
758 indexForBreakingNode = S.getNumChoices();
759 }
760
761 if (nextNode != NULL)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000762 {
763 S.addChoice(nextNode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000764
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000765 if (S.schedInfo.isSingleIssue(nextNode->getOpCode()))
766 {
767 assert(S.getNumChoices() == 1 &&
768 "Prioritizer returned invalid instr for this cycle!");
769 break;
770 }
771 }
772
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000773 if (indexForDelayedInstr < S.nslots)
774 break; // leave the rest for delay slots
775 }
776
777 assert(S.getNumChoices() <= S.nslots);
778 assert(! (indexForDelayedInstr < S.nslots &&
779 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
780
781 // Assign each chosen instruction to all possible slots for that instr.
782 // But if only one instruction was chosen, put it only in the first
783 // feasible slot; no more analysis will be needed.
784 //
785 if (indexForDelayedInstr >= S.nslots &&
786 indexForBreakingNode >= S.nslots)
787 { // No instructions that break the issue group or that have delay slots.
788 // This is the common case, so handle it separately for efficiency.
789
790 if (S.getNumChoices() == 1)
791 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000792 MachineOpCode opCode = S.getChoice(0)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000793 unsigned int s;
794 for (s=startSlot; s < S.nslots; s++)
795 if (S.schedInfo.instrCanUseSlot(opCode, s))
796 break;
797 assert(s < S.nslots && "No feasible slot for this opCode?");
798 S.addChoiceToSlot(s, S.getChoice(0));
799 }
800 else
801 {
802 for (unsigned i=0; i < S.getNumChoices(); i++)
803 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000804 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000805 for (unsigned int s=startSlot; s < S.nslots; s++)
806 if (S.schedInfo.instrCanUseSlot(opCode, s))
807 S.addChoiceToSlot(s, S.getChoice(i));
808 }
809 }
810 }
811 else if (indexForDelayedInstr < S.nslots)
812 {
813 // There is an instruction that needs delay slots.
814 // Try to assign that instruction to a higher slot than any other
815 // instructions in the group, so that its delay slots can go
816 // right after it.
817 //
818
819 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
820 "Instruction with delay slots should be last choice!");
821 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
822
823 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000824 MachineOpCode delayOpCode = delayedNode->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000825 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
826
827 unsigned delayedNodeSlot = S.nslots;
828 int highestSlotUsed;
829
830 // Find the last possible slot for the delayed instruction that leaves
831 // at least `d' slots vacant after it (d = #delay slots)
832 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
833 if (S.schedInfo.instrCanUseSlot(delayOpCode, s))
834 {
835 delayedNodeSlot = s;
836 break;
837 }
838
839 highestSlotUsed = -1;
840 for (unsigned i=0; i < S.getNumChoices() - 1; i++)
841 {
842 // Try to assign every other instruction to a lower numbered
843 // slot than delayedNodeSlot.
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000844 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000845 bool noSlotFound = true;
846 unsigned int s;
847 for (s=startSlot; s < delayedNodeSlot; s++)
848 if (S.schedInfo.instrCanUseSlot(opCode, s))
849 {
850 S.addChoiceToSlot(s, S.getChoice(i));
851 noSlotFound = false;
852 }
853
854 // No slot before `delayedNodeSlot' was found for this opCode
855 // Use a later slot, and allow some delay slots to fall in
856 // the next cycle.
857 if (noSlotFound)
858 for ( ; s < S.nslots; s++)
859 if (S.schedInfo.instrCanUseSlot(opCode, s))
860 {
861 S.addChoiceToSlot(s, S.getChoice(i));
862 break;
863 }
864
865 assert(s < S.nslots && "No feasible slot for instruction?");
866
Chris Lattner697954c2002-01-20 22:54:45 +0000867 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000868 }
869
870 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
871
872 // We will put the delayed node in the first slot after the
873 // highest slot used. But we just mark that for now, and
874 // schedule it separately because we want to schedule the delay
875 // slots for the node at the same time.
876 cycles_t dcycle = S.getTime();
877 unsigned int dslot = highestSlotUsed + 1;
878 if (dslot == S.nslots)
879 {
880 dslot = 0;
881 ++dcycle;
882 }
883 delaySlotInfo->recordChosenSlot(dcycle, dslot);
884 getDelaySlotInfo = delaySlotInfo;
885 }
886 else
887 { // There is an instruction that breaks the issue group.
888 // For such an instruction, assign to the last possible slot in
889 // the current group, and then don't assign any other instructions
890 // to later slots.
891 assert(indexForBreakingNode < S.nslots);
892 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
893 unsigned breakingSlot = INT_MAX;
894 unsigned int nslotsToUse = S.nslots;
895
896 // Find the last possible slot for this instruction.
897 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000898 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000899 {
900 breakingSlot = s;
901 break;
902 }
903 assert(breakingSlot < S.nslots &&
904 "No feasible slot for `breakingNode'?");
905
906 // Higher priority instructions than the one that breaks the group:
907 // These can be assigned to all slots, but will be assigned only
908 // to earlier slots if possible.
909 for (unsigned i=0;
910 i < S.getNumChoices() && i < indexForBreakingNode; i++)
911 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000912 MachineOpCode opCode =S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000913
914 // If a higher priority instruction cannot be assigned to
915 // any earlier slots, don't schedule the breaking instruction.
916 //
917 bool foundLowerSlot = false;
918 nslotsToUse = S.nslots; // May be modified in the loop
919 for (unsigned int s=startSlot; s < nslotsToUse; s++)
920 if (S.schedInfo.instrCanUseSlot(opCode, s))
921 {
922 if (breakingSlot < S.nslots && s < breakingSlot)
923 {
924 foundLowerSlot = true;
925 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
926 }
927
928 S.addChoiceToSlot(s, S.getChoice(i));
929 }
930
931 if (!foundLowerSlot)
932 breakingSlot = INT_MAX; // disable breaking instr
933 }
934
935 // Assign the breaking instruction (if any) to a single slot
936 // Otherwise, just ignore the instruction. It will simply be
937 // scheduled in a later cycle.
938 if (breakingSlot < S.nslots)
939 {
940 S.addChoiceToSlot(breakingSlot, breakingNode);
941 nslotsToUse = breakingSlot;
942 }
943 else
944 nslotsToUse = S.nslots;
945
946 // For lower priority instructions than the one that breaks the
947 // group, only assign them to slots lower than the breaking slot.
948 // Otherwise, just ignore the instruction.
949 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++)
950 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000951 MachineOpCode opCode = S.getChoice(i)->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000952 for (unsigned int s=startSlot; s < nslotsToUse; s++)
953 if (S.schedInfo.instrCanUseSlot(opCode, s))
954 S.addChoiceToSlot(s, S.getChoice(i));
955 }
956 } // endif (no delay slots and no breaking slots)
957
958 return S.getNumChoices();
959}
960
961
Vikram S. Advec5b46322001-09-30 23:43:34 +0000962static unsigned
963ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000964{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000965 assert(S.schedPrio.getNumReady() > 0
966 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000967
Vikram S. Advec5b46322001-09-30 23:43:34 +0000968 cycles_t firstCycle = S.getTime();
969 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000970
Vikram S. Advec5b46322001-09-30 23:43:34 +0000971 // Choose up to `nslots' feasible instructions and their possible slots.
972 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000973
Vikram S. Advec5b46322001-09-30 23:43:34 +0000974 while (numIssued == 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000975 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000976 S.updateTime(S.getTime()+1);
977 numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000978 }
979
Vikram S. Advec5b46322001-09-30 23:43:34 +0000980 AssignInstructionsToSlots(S, numIssued);
981
982 if (getDelaySlotInfo != NULL)
983 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
984
985 // Print trace of scheduled instructions before newly ready ones
986 if (SchedDebugLevel >= Sched_PrintSchedTrace)
987 {
988 for (cycles_t c = firstCycle; c <= S.getTime(); c++)
989 {
Chris Lattner697954c2002-01-20 22:54:45 +0000990 cerr << " Cycle " << (long)c << " : Scheduled instructions:\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000991 const InstrGroup* igroup = S.isched.getIGroup(c);
992 for (unsigned int s=0; s < S.nslots; s++)
993 {
Chris Lattner697954c2002-01-20 22:54:45 +0000994 cerr << " ";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000995 if ((*igroup)[s] != NULL)
Chris Lattner697954c2002-01-20 22:54:45 +0000996 cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000997 else
Chris Lattner697954c2002-01-20 22:54:45 +0000998 cerr << "<none>\n";
Vikram S. Advec5b46322001-09-30 23:43:34 +0000999 }
1000 }
1001 }
1002
1003 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001004}
1005
1006
Vikram S. Advec5b46322001-09-30 23:43:34 +00001007static void
1008ForwardListSchedule(SchedulingManager& S)
1009{
1010 unsigned N;
1011 const SchedGraphNode* node;
1012
1013 S.schedPrio.initialize();
1014
1015 while ((N = S.schedPrio.getNumReady()) > 0)
1016 {
1017 cycles_t nextCycle = S.getTime();
1018
1019 // Choose one group of instructions for a cycle, plus any delay slot
1020 // instructions (which may overflow into successive cycles).
1021 // This will advance S.getTime() to the last cycle in which
1022 // instructions are actually issued.
1023 //
1024 unsigned numIssued = ChooseOneGroup(S);
1025 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
1026
1027 // Notify the priority manager of scheduled instructions and mark
1028 // any successors that may now be ready
1029 //
1030 for (cycles_t c = nextCycle; c <= S.getTime(); c++)
1031 {
1032 const InstrGroup* igroup = S.isched.getIGroup(c);
1033 for (unsigned int s=0; s < S.nslots; s++)
1034 if ((node = (*igroup)[s]) != NULL)
1035 {
1036 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1037 MarkSuccessorsReady(S, node);
1038 }
1039 }
1040
1041 // Move to the next the next earliest cycle for which
1042 // an instruction can be issued, or the next earliest in which
1043 // one will be ready, or to the next cycle, whichever is latest.
1044 //
Chris Lattner697954c2002-01-20 22:54:45 +00001045 S.updateTime(std::max(S.getTime() + 1,
1046 std::max(S.getEarliestIssueTime(),
1047 S.schedPrio.getEarliestReadyTime())));
Vikram S. Advec5b46322001-09-30 23:43:34 +00001048 }
1049}
1050
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001051
1052//---------------------------------------------------------------------
1053// Code for filling delay slots for delayed terminator instructions
1054// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1055// instructions (e.g., CALL) are not handled here because they almost
1056// always can be filled with instructions from the call sequence code
1057// before a call. That's preferable because we incur many tradeoffs here
1058// when we cannot find single-cycle instructions that can be reordered.
1059//----------------------------------------------------------------------
1060
Vikram S. Advec5b46322001-09-30 23:43:34 +00001061static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001062NodeCanFillDelaySlot(const SchedulingManager& S,
1063 const SchedGraphNode* node,
1064 const SchedGraphNode* brNode,
1065 bool nodeIsPredecessor)
1066{
1067 assert(! node->isDummyNode());
1068
1069 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001070 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001071 return false;
1072
1073 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001074 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001075 return false;
1076
1077 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001078 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001079
1080 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1081 EI != node->endInEdges(); ++EI)
1082 if (! (*EI)->getSrc()->isDummyNode()
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001083 && mii.isLoad((*EI)->getSrc()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001084 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1085 return false;
1086
1087 // for now, don't put an instruction that does not have operand
1088 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001089 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001090 return false;
1091
1092 // Finally, if the instruction preceeds the branch, we make sure the
1093 // instruction can be reordered relative to the branch. We simply check
1094 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1095 //
1096 if (nodeIsPredecessor)
1097 {
1098 bool onlyCDEdgeToBranch = true;
1099 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1100 OEI != node->endOutEdges(); ++OEI)
1101 if (! (*OEI)->getSink()->isDummyNode()
1102 && ((*OEI)->getSink() != brNode
1103 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1104 {
1105 onlyCDEdgeToBranch = false;
1106 break;
1107 }
1108
1109 if (!onlyCDEdgeToBranch)
1110 return false;
1111 }
1112
1113 return true;
1114}
1115
1116
Vikram S. Advec5b46322001-09-30 23:43:34 +00001117static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001118MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001119 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001120 SchedGraphNode* node,
1121 const SchedGraphNode* brNode,
1122 bool nodeIsPredecessor)
1123{
1124 if (nodeIsPredecessor)
1125 { // If node is in the same basic block (i.e., preceeds brNode),
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001126 // remove it and all its incident edges from the graph. Make sure we
1127 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1128 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001129 }
1130 else
1131 { // If the node was from a target block, add the node to the graph
1132 // and add a CD edge from brNode to node.
1133 assert(0 && "NOT IMPLEMENTED YET");
1134 }
1135
1136 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1137 dinfo->addDelayNode(node);
1138}
1139
1140
Vikram S. Advec5b46322001-09-30 23:43:34 +00001141void
1142FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1143 SchedGraphNode* brNode,
1144 vector<SchedGraphNode*>& sdelayNodeVec)
1145{
Chris Lattner3501fea2003-01-14 22:00:31 +00001146 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001147 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001148 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001149
1150 if (ndelays == 0)
1151 return;
1152
1153 sdelayNodeVec.reserve(ndelays);
1154
1155 // Use a separate vector to hold the feasible multi-cycle nodes.
1156 // These will be used if not enough single-cycle nodes are found.
1157 //
1158 vector<SchedGraphNode*> mdelayNodeVec;
1159
1160 for (sg_pred_iterator P = pred_begin(brNode);
1161 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1162 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001163 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001164 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
1165 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001166 if (mii.maxLatency((*P)->getOpCode()) > 1)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001167 mdelayNodeVec.push_back(*P);
1168 else
1169 sdelayNodeVec.push_back(*P);
1170 }
1171
1172 // If not enough single-cycle instructions were found, select the
1173 // lowest-latency multi-cycle instructions and use them.
1174 // Note that this is the most efficient code when only 1 (or even 2)
1175 // values need to be selected.
1176 //
1177 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
1178 {
1179 unsigned lmin =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001180 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001181 unsigned minIndex = 0;
1182 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
1183 {
1184 unsigned li =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001185 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001186 if (lmin >= li)
1187 {
1188 lmin = li;
1189 minIndex = i;
1190 }
1191 }
1192 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1193 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1194 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1195 }
1196}
1197
1198
1199// Remove the NOPs currently in delay slots from the graph.
1200// Mark instructions specified in sdelayNodeVec to replace them.
1201// If not enough useful instructions were found, mark the NOPs to be used
1202// for filling delay slots, otherwise, otherwise just discard them.
1203//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001204static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1205 SchedGraphNode* node,
1206 vector<SchedGraphNode*> sdelayNodeVec,
1207 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001208{
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001209 vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001210 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001211 const MachineInstr* brInstr = node->getMachineInstr();
1212 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001213 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1214
1215 // Remove the NOPs currently in delay slots from the graph.
1216 // If not enough useful instructions were found, use the NOPs to
1217 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001218 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001219 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001220 MachineBasicBlock& MBB = node->getMachineBasicBlock();
1221 assert(MBB[firstDelaySlotIdx - 1] == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001222 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001223
1224 // First find all useful instructions already in the delay slots
1225 // and USE THEM. We'll throw away the unused alternatives below
1226 //
1227 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001228 if (! mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001229 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001230 graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001231
1232 // Then find the NOPs and keep only as many as are needed.
1233 // Put the rest in nopNodeVec to be deleted.
1234 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001235 if (mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001236 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001237 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001238 else
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001239 {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001240 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001241
1242 //remove the MI from the Machine Code For Instruction
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001243 TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001244 MachineCodeForInstruction& llvmMvec =
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001245 MachineCodeForInstruction::get((Instruction *)TI);
1246
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001247 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1248 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001249 if (*mciI==MBB[i])
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001250 llvmMvec.erase(mciI);
1251 }
1252 }
1253
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001254 assert(sdelayNodeVec.size() >= ndelays);
1255
1256 // If some delay slots were already filled, throw away that many new choices
1257 if (sdelayNodeVec.size() > ndelays)
1258 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001259
1260 // Mark the nodes chosen for delay slots. This removes them from the graph.
1261 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1262 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1263
1264 // And remove the unused NOPs from the graph.
1265 for (unsigned i=0; i < nopNodeVec.size(); i++)
1266 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1267}
1268
1269
1270// For all delayed instructions, choose instructions to put in the delay
1271// slots and pull those out of the graph. Mark them for the delay slots
1272// in the DelaySlotInfo object for that graph node. If no useful work
1273// is found for a delay slot, use the NOP that is currently in that slot.
1274//
1275// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001276// EXCEPT CALLS AND RETURNS.
1277// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001278// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001279// suboptimal. Also, it complicates generating the calling sequence code in
1280// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001281//
1282static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001283ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001284 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001285{
Chris Lattner3501fea2003-01-14 22:00:31 +00001286 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001287
1288 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001289 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001290 vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001291 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001292
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001293 if (termInstr->getOpcode() != Instruction::Ret)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001294 {
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001295 // To find instructions that need delay slots without searching the full
1296 // machine code, we assume that the only delayed instructions are CALLs
1297 // or instructions generated for the terminator inst.
1298 // Find the first branch instr in the sequence of machine instrs for term
1299 //
1300 unsigned first = 0;
1301 while (first < termMvec.size() &&
1302 ! mii.isBranch(termMvec[first]->getOpCode()))
1303 {
1304 ++first;
1305 }
1306 assert(first < termMvec.size() &&
1307 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1308
1309 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1310
1311 // Compute a vector of the nodes chosen for delay slots and then
1312 // mark delay slots to replace NOPs with these useful instructions.
1313 //
1314 if (brInstr != NULL)
1315 {
1316 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1317 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1318 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1319 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001320 }
1321
1322 // Also mark delay slots for other delayed instructions to hold NOPs.
1323 // Simply passing in an empty delayNodeVec will have this effect.
1324 //
1325 delayNodeVec.clear();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001326 for (unsigned i=0; i < MBB.size(); ++i)
1327 if (MBB[i] != brInstr &&
1328 mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001329 {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001330 SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001331 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1332 }
1333}
1334
1335
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001336//
1337// Schedule the delayed branch and its delay slots
1338//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001339unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001340DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1341{
1342 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1343 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1344 && "Slot for branch should be empty");
1345
1346 unsigned int nextSlot = delayedNodeSlotNum;
1347 cycles_t nextTime = delayedNodeCycle;
1348
1349 S.scheduleInstr(brNode, nextSlot, nextTime);
1350
1351 for (unsigned d=0; d < ndelays; d++)
1352 {
1353 ++nextSlot;
1354 if (nextSlot == S.nslots)
1355 {
1356 nextSlot = 0;
1357 nextTime++;
1358 }
1359
1360 // Find the first feasible instruction for this delay slot
1361 // Note that we only check for issue restrictions here.
1362 // We do *not* check for flow dependences but rely on pipeline
1363 // interlocks to resolve them. Machines without interlocks
1364 // will require this code to be modified.
1365 for (unsigned i=0; i < delayNodeVec.size(); i++)
1366 {
1367 const SchedGraphNode* dnode = delayNodeVec[i];
1368 if ( ! S.isScheduled(dnode)
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001369 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1370 && instrIsFeasible(S, dnode->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001371 {
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001372 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001373 && "Instructions without interlocks not yet supported "
1374 "when filling branch delay slots");
1375 S.scheduleInstr(dnode, nextSlot, nextTime);
1376 break;
1377 }
1378 }
1379 }
1380
1381 // Update current time if delay slots overflowed into later cycles.
1382 // Do this here because we know exactly which cycle is the last cycle
1383 // that contains delay slots. The next loop doesn't compute that.
1384 if (nextTime > S.getTime())
1385 S.updateTime(nextTime);
1386
1387 // Now put any remaining instructions in the unfilled delay slots.
1388 // This could lead to suboptimal performance but needed for correctness.
1389 nextSlot = delayedNodeSlotNum;
1390 nextTime = delayedNodeCycle;
1391 for (unsigned i=0; i < delayNodeVec.size(); i++)
1392 if (! S.isScheduled(delayNodeVec[i]))
1393 {
1394 do { // find the next empty slot
1395 ++nextSlot;
1396 if (nextSlot == S.nslots)
1397 {
1398 nextSlot = 0;
1399 nextTime++;
1400 }
1401 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
1402
1403 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1404 break;
1405 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001406
1407 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001408}
1409
Vikram S. Advec5b46322001-09-30 23:43:34 +00001410
1411// Check if the instruction would conflict with instructions already
1412// chosen for the current cycle
1413//
1414static inline bool
1415ConflictsWithChoices(const SchedulingManager& S,
1416 MachineOpCode opCode)
1417{
1418 // Check if the instruction must issue by itself, and some feasible
1419 // choices have already been made for this cycle
1420 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1421 return true;
1422
1423 // For each class that opCode belongs to, check if there are too many
1424 // instructions of that class.
1425 //
1426 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1427 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1428}
1429
1430
1431//************************* External Functions *****************************/
1432
1433
1434//---------------------------------------------------------------------------
1435// Function: ViolatesMinimumGap
1436//
1437// Purpose:
1438// Check minimum gap requirements relative to instructions scheduled in
1439// previous cycles.
1440// Note that we do not need to consider `nextEarliestIssueTime' here because
1441// that is also captured in the earliest start times for each opcode.
1442//---------------------------------------------------------------------------
1443
1444static inline bool
1445ViolatesMinimumGap(const SchedulingManager& S,
1446 MachineOpCode opCode,
1447 const cycles_t inCycle)
1448{
1449 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1450}
1451
1452
1453//---------------------------------------------------------------------------
1454// Function: instrIsFeasible
1455//
1456// Purpose:
1457// Check if any issue restrictions would prevent the instruction from
1458// being issued in the current cycle
1459//---------------------------------------------------------------------------
1460
1461bool
1462instrIsFeasible(const SchedulingManager& S,
1463 MachineOpCode opCode)
1464{
1465 // skip the instruction if it cannot be issued due to issue restrictions
1466 // caused by previously issued instructions
1467 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1468 return false;
1469
1470 // skip the instruction if it cannot be issued due to issue restrictions
1471 // caused by previously chosen instructions for the current cycle
1472 if (ConflictsWithChoices(S, opCode))
1473 return false;
1474
1475 return true;
1476}
1477
1478//---------------------------------------------------------------------------
1479// Function: ScheduleInstructionsWithSSA
1480//
1481// Purpose:
1482// Entry point for instruction scheduling on SSA form.
1483// Schedules the machine instructions generated by instruction selection.
1484// Assumes that register allocation has not been done, i.e., operands
1485// are still in SSA form.
1486//---------------------------------------------------------------------------
1487
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001488namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001489 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001490 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001491 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001492 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001493
1494 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001495
Chris Lattnerf57b8452002-04-27 06:56:12 +00001496 // getAnalysisUsage - We use LiveVarInfo...
1497 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001498 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001499 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001500 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001501
Chris Lattner7e708292002-06-25 16:13:24 +00001502 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001503 };
1504} // end anonymous namespace
1505
Vikram S. Adve802cec42002-03-24 03:44:55 +00001506
Chris Lattner7e708292002-06-25 16:13:24 +00001507bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001508{
Chris Lattner7e708292002-06-25 16:13:24 +00001509 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001510
1511 if (SchedDebugLevel >= Sched_PrintSchedGraphs)
1512 {
1513 cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
1514 graphSet.dump();
1515 }
1516
1517 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1518 GI != GE; ++GI)
1519 {
1520 SchedGraph* graph = (*GI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001521 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001522
1523 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1524 cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
1525
1526 // expensive!
Chris Lattner92ba2aa2003-01-14 23:05:08 +00001527 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
Vikram S. Adve802cec42002-03-24 03:44:55 +00001528 SchedulingManager S(target, graph, schedPrio);
1529
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001530 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
Vikram S. Adve802cec42002-03-24 03:44:55 +00001531 ForwardListSchedule(S); // computes schedule in S
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001532 RecordSchedule(MBB, S); // records schedule in BB
Vikram S. Adve802cec42002-03-24 03:44:55 +00001533 }
1534
1535 if (SchedDebugLevel >= Sched_PrintMachineCode)
1536 {
1537 cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
Misha Brukmanfce11432002-10-28 00:28:31 +00001538 MachineFunction::get(&F).dump();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001539 }
1540
1541 return false;
1542}
1543
1544
Chris Lattnerf57b8452002-04-27 06:56:12 +00001545Pass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001546 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001547}