blob: a50439de7f66fe5d236c1d038ffe8d480b01c6e6 [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattneraf50d002002-04-09 05:45:58 +00009//
10// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
11// generic support routines for instruction scheduling.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +000014
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000015#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000016#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000019#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000021#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000022#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000023#include <algorithm>
Vikram S. Advec5b46322001-09-30 23:43:34 +000024
Chris Lattner70e60cb2002-05-22 17:08:27 +000025SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000026
Vikram S. Advebed4eff2003-09-16 05:55:15 +000027static cl::opt<bool> EnableFillingDelaySlots("sched-fill-delay-slots",
28 cl::desc("Fill branch delay slots during local scheduling"));
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030static cl::opt<SchedDebugLevel_t, true>
31SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
32 cl::desc("enable instruction scheduling debugging information"),
33 cl::values(
34 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000035 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
36 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
37 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
38 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000039
40
Vikram S. Advec5b46322001-09-30 23:43:34 +000041//************************* Internal Data Types *****************************/
42
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000043class InstrSchedule;
44class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000045
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000046
47//----------------------------------------------------------------------
48// class InstrGroup:
49//
50// Represents a group of instructions scheduled to be issued
51// in a single cycle.
52//----------------------------------------------------------------------
53
Chris Lattnere3561c22003-08-15 05:20:06 +000054class InstrGroup {
55 InstrGroup(const InstrGroup&); // DO NOT IMPLEMENT
56 void operator=(const InstrGroup&); // DO NOT IMPLEMENT
57
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000058public:
59 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
60 assert(slotNum < group.size());
61 return group[slotNum];
62 }
63
64private:
65 friend class InstrSchedule;
66
67 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
68 assert(slotNum < group.size());
69 group[slotNum] = node;
70 }
71
72 /*ctor*/ InstrGroup(unsigned int nslots)
73 : group(nslots, NULL) {}
74
75 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
76
77private:
Misha Brukmanc2312df2003-05-22 21:24:35 +000078 std::vector<const SchedGraphNode*> group;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000079};
80
81
82//----------------------------------------------------------------------
83// class ScheduleIterator:
84//
85// Iterates over the machine instructions in the for a single basic block.
86// The schedule is represented by an InstrSchedule object.
87//----------------------------------------------------------------------
88
89template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000090class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000091private:
92 unsigned cycleNum;
93 unsigned slotNum;
94 const InstrSchedule& S;
95public:
96 typedef ScheduleIterator<_NodeType> _Self;
97
98 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
99 unsigned _cycleNum,
100 unsigned _slotNum)
101 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
102 skipToNextInstr();
103 }
104
105 /*ctor*/ inline ScheduleIterator(const _Self& x)
106 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
107
108 inline bool operator==(const _Self& x) const {
109 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
110 }
111
112 inline bool operator!=(const _Self& x) const { return !operator==(x); }
113
Chris Lattner414d9d22003-11-05 06:25:06 +0000114 inline _NodeType* operator*() const;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000115 inline _NodeType* operator->() const { return operator*(); }
116
117 _Self& operator++(); // Preincrement
118 inline _Self operator++(int) { // Postincrement
119 _Self tmp(*this); ++*this; return tmp;
120 }
121
122 static _Self begin(const InstrSchedule& _schedule);
123 static _Self end( const InstrSchedule& _schedule);
124
125private:
126 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
127 void skipToNextInstr();
128};
129
130
131//----------------------------------------------------------------------
132// class InstrSchedule:
133//
134// Represents the schedule of machine instructions for a single basic block.
135//----------------------------------------------------------------------
136
Chris Lattnere3561c22003-08-15 05:20:06 +0000137class InstrSchedule {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000138 const unsigned int nslots;
139 unsigned int numInstr;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000140 std::vector<InstrGroup*> groups; // indexed by cycle number
141 std::vector<cycles_t> startTime; // indexed by node id
Chris Lattnere3561c22003-08-15 05:20:06 +0000142
143 InstrSchedule(InstrSchedule&); // DO NOT IMPLEMENT
144 void operator=(InstrSchedule&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000145
146public: // iterators
147 typedef ScheduleIterator<SchedGraphNode> iterator;
148 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
149
150 iterator begin();
151 const_iterator begin() const;
152 iterator end();
153 const_iterator end() const;
154
155public: // constructors and destructor
156 /*ctor*/ InstrSchedule (unsigned int _nslots,
157 unsigned int _numNodes);
158 /*dtor*/ ~InstrSchedule ();
159
160public: // accessor functions to query chosen schedule
161 const SchedGraphNode* getInstr (unsigned int slotNum,
162 cycles_t c) const {
163 const InstrGroup* igroup = this->getIGroup(c);
164 return (igroup == NULL)? NULL : (*igroup)[slotNum];
165 }
166
167 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000168 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000169 groups.resize(c+1);
170 if (groups[c] == NULL)
171 groups[c] = new InstrGroup(nslots);
172 return groups[c];
173 }
174
175 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000176 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000177 return groups[c];
178 }
179
180 inline cycles_t getStartTime (unsigned int nodeId) const {
181 assert(nodeId < startTime.size());
182 return startTime[nodeId];
183 }
184
185 unsigned int getNumInstructions() const {
186 return numInstr;
187 }
188
189 inline void scheduleInstr (const SchedGraphNode* node,
190 unsigned int slotNum,
191 cycles_t cycle) {
192 InstrGroup* igroup = this->getIGroup(cycle);
193 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
194 igroup->addInstr(node, slotNum);
195 assert(node->getNodeId() < startTime.size());
196 startTime[node->getNodeId()] = cycle;
197 ++numInstr;
198 }
199
200private:
Chris Lattner414d9d22003-11-05 06:25:06 +0000201 friend class ScheduleIterator<SchedGraphNode>;
202 friend class ScheduleIterator<const SchedGraphNode>;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000203 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
204};
205
Chris Lattner414d9d22003-11-05 06:25:06 +0000206template<class NodeType>
207inline NodeType *ScheduleIterator<NodeType>::operator*() const {
208 assert(cycleNum < S.groups.size());
209 return (*S.groups[cycleNum])[slotNum];
210}
211
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000212
213/*ctor*/
214InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
215 : nslots(_nslots),
216 numInstr(0),
217 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
218 startTime(_numNodes, (cycles_t) -1) // set all to -1
219{
220}
221
222
223/*dtor*/
224InstrSchedule::~InstrSchedule()
225{
226 for (unsigned c=0, NC=groups.size(); c < NC; c++)
227 if (groups[c] != NULL)
228 delete groups[c]; // delete InstrGroup objects
229}
230
231
232template<class _NodeType>
233inline
234void
235ScheduleIterator<_NodeType>::skipToNextInstr()
236{
237 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
238 ++cycleNum; // skip cycles with no instructions
239
240 while (cycleNum < S.groups.size() &&
241 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000242 {
243 ++slotNum;
244 if (slotNum == S.nslots) {
245 ++cycleNum;
246 slotNum = 0;
247 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
248 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000249 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000250 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000251}
252
253template<class _NodeType>
254inline
255ScheduleIterator<_NodeType>&
256ScheduleIterator<_NodeType>::operator++() // Preincrement
257{
258 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000259 if (slotNum == S.nslots) {
260 ++cycleNum;
261 slotNum = 0;
262 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000263 skipToNextInstr();
264 return *this;
265}
266
267template<class _NodeType>
268ScheduleIterator<_NodeType>
269ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
270{
271 return _Self(_schedule, 0, 0);
272}
273
274template<class _NodeType>
275ScheduleIterator<_NodeType>
276ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
277{
278 return _Self(_schedule, _schedule.groups.size(), 0);
279}
280
281InstrSchedule::iterator
282InstrSchedule::begin()
283{
284 return iterator::begin(*this);
285}
286
287InstrSchedule::const_iterator
288InstrSchedule::begin() const
289{
290 return const_iterator::begin(*this);
291}
292
293InstrSchedule::iterator
294InstrSchedule::end()
295{
296 return iterator::end(*this);
297}
298
299InstrSchedule::const_iterator
300InstrSchedule::end() const
301{
302 return const_iterator::end( *this);
303}
304
305
306//----------------------------------------------------------------------
307// class DelaySlotInfo:
308//
309// Record information about delay slots for a single branch instruction.
310// Delay slots are simply indexed by slot number 1 ... numDelaySlots
311//----------------------------------------------------------------------
312
Chris Lattnere3561c22003-08-15 05:20:06 +0000313class DelaySlotInfo {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000314 const SchedGraphNode* brNode;
Chris Lattnere3561c22003-08-15 05:20:06 +0000315 unsigned ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000316 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000317 cycles_t delayedNodeCycle;
Chris Lattnere3561c22003-08-15 05:20:06 +0000318 unsigned delayedNodeSlotNum;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000319
Chris Lattnere3561c22003-08-15 05:20:06 +0000320 DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT
321 void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000322public:
323 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
324 unsigned _ndelays)
325 : brNode(_brNode), ndelays(_ndelays),
326 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
327
328 inline unsigned getNumDelays () {
329 return ndelays;
330 }
331
Misha Brukmanc2312df2003-05-22 21:24:35 +0000332 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000333 return delayNodeVec;
334 }
335
336 inline void addDelayNode (const SchedGraphNode* node) {
337 delayNodeVec.push_back(node);
338 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
339 }
340
341 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
342 delayedNodeCycle = cycle;
343 delayedNodeSlotNum = slotNum;
344 }
345
Vikram S. Advec5b46322001-09-30 23:43:34 +0000346 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000347};
348
349
350//----------------------------------------------------------------------
351// class SchedulingManager:
352//
353// Represents the schedule of machine instructions for a single basic block.
354//----------------------------------------------------------------------
355
Chris Lattnere3561c22003-08-15 05:20:06 +0000356class SchedulingManager {
357 SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT
358 void operator=(const SchedulingManager &); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000359public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000360 const unsigned nslots;
361 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000362 SchedPriorities& schedPrio;
363 InstrSchedule isched;
364
365private:
Chris Lattnere3561c22003-08-15 05:20:06 +0000366 unsigned totalInstrCount;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000367 cycles_t curTime;
368 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000369 // indexed by slot#
370 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
371 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
372 std::vector<int> numInClass; // indexed by sched class
373 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000374 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000375 // indexed by branch node ptr
376
377public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000378 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
379 SchedPriorities& schedPrio);
380 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000381 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000382 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
383 E = delaySlotInfoForBranches.end(); I != E; ++I)
384 delete I->second;
385 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000386
387 //----------------------------------------------------------------------
388 // Simplify access to the machine instruction info
389 //----------------------------------------------------------------------
390
Chris Lattner3501fea2003-01-14 22:00:31 +0000391 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000392 return schedInfo.getInstrInfo();
393 }
394
395 //----------------------------------------------------------------------
396 // Interface for checking and updating the current time
397 //----------------------------------------------------------------------
398
399 inline cycles_t getTime () const {
400 return curTime;
401 }
402
403 inline cycles_t getEarliestIssueTime() const {
404 return nextEarliestIssueTime;
405 }
406
407 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
408 assert(opCode < (int) nextEarliestStartTime.size());
409 return nextEarliestStartTime[opCode];
410 }
411
412 // Update current time to specified cycle
413 inline void updateTime (cycles_t c) {
414 curTime = c;
415 schedPrio.updateTime(c);
416 }
417
418 //----------------------------------------------------------------------
419 // Functions to manage the choices for the current cycle including:
420 // -- a vector of choices by priority (choiceVec)
421 // -- vectors of the choices for each instruction slot (choicesForSlot[])
422 // -- number of choices in each sched class, used to check issue conflicts
423 // between choices for a single cycle
424 //----------------------------------------------------------------------
425
426 inline unsigned int getNumChoices () const {
427 return choiceVec.size();
428 }
429
430 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000431 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000432 return numInClass[sc];
433 }
434
435 inline const SchedGraphNode* getChoice(unsigned int i) const {
436 // assert(i < choiceVec.size()); don't check here.
437 return choiceVec[i];
438 }
439
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000440 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000441 assert(slotNum < nslots);
442 return choicesForSlot[slotNum];
443 }
444
445 inline void addChoice (const SchedGraphNode* node) {
446 // Append the instruction to the vector of choices for current cycle.
447 // Increment numInClass[c] for the sched class to which the instr belongs.
448 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000449 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000450 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000451 numInClass[sc]++;
452 }
453
454 inline void addChoiceToSlot (unsigned int slotNum,
455 const SchedGraphNode* node) {
456 // Add the instruction to the choice set for the specified slot
457 assert(slotNum < nslots);
458 choicesForSlot[slotNum].insert(node);
459 }
460
461 inline void resetChoices () {
462 choiceVec.clear();
463 for (unsigned int s=0; s < nslots; s++)
464 choicesForSlot[s].clear();
465 for (unsigned int c=0; c < numInClass.size(); c++)
466 numInClass[c] = 0;
467 }
468
469 //----------------------------------------------------------------------
470 // Code to query and manage the partial instruction schedule so far
471 //----------------------------------------------------------------------
472
473 inline unsigned int getNumScheduled () const {
474 return isched.getNumInstructions();
475 }
476
477 inline unsigned int getNumUnscheduled() const {
478 return totalInstrCount - isched.getNumInstructions();
479 }
480
481 inline bool isScheduled (const SchedGraphNode* node) const {
482 return (isched.getStartTime(node->getNodeId()) >= 0);
483 }
484
485 inline void scheduleInstr (const SchedGraphNode* node,
486 unsigned int slotNum,
487 cycles_t cycle)
488 {
489 assert(! isScheduled(node) && "Instruction already scheduled?");
490
491 // add the instruction to the schedule
492 isched.scheduleInstr(node, slotNum, cycle);
493
494 // update the earliest start times of all nodes that conflict with `node'
495 // and the next-earliest time anything can issue if `node' causes bubbles
496 updateEarliestStartTimes(node, cycle);
497
498 // remove the instruction from the choice sets for all slots
499 for (unsigned s=0; s < nslots; s++)
500 choicesForSlot[s].erase(node);
501
502 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000503 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000504 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000505 numInClass[sc]--;
506 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000507
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000508 //----------------------------------------------------------------------
509 // Create and retrieve delay slot info for delayed instructions
510 //----------------------------------------------------------------------
511
512 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
513 bool createIfMissing=false)
514 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000515 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000516 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000517 if (I != delaySlotInfoForBranches.end())
518 return I->second;
519
520 if (!createIfMissing) return 0;
521
522 DelaySlotInfo *dinfo =
523 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
524 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000525 }
526
527private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000528 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
529 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000530};
531
532
533/*ctor*/
534SchedulingManager::SchedulingManager(const TargetMachine& target,
535 const SchedGraph* graph,
536 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000537 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
538 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000539 schedPrio(_schedPrio),
540 isched(nslots, graph->getNumNodes()),
541 totalInstrCount(graph->getNumNodes() - 2),
542 nextEarliestIssueTime(0),
543 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000544 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000545 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
546 (cycles_t) 0) // set all to 0
547{
548 updateTime(0);
549
550 // Note that an upper bound on #choices for each slot is = nslots since
551 // we use this vector to hold a feasible set of instructions, and more
552 // would be infeasible. Reserve that much memory since it is probably small.
553 for (unsigned int i=0; i < nslots; i++)
554 choicesForSlot[i].resize(nslots);
555}
556
557
558void
559SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
560 cycles_t schedTime)
561{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000562 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000563 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000564 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000565 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000566 }
567
Vikram S. Adve1632e882002-10-13 00:40:37 +0000568 const std::vector<MachineOpCode>&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000569 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000570
Vikram S. Adve1632e882002-10-13 00:40:37 +0000571 for (unsigned i=0; i < conflictVec.size(); i++)
572 {
573 MachineOpCode toOp = conflictVec[i];
574 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp);
575 assert(toOp < (int) nextEarliestStartTime.size());
576 if (nextEarliestStartTime[toOp] < est)
577 nextEarliestStartTime[toOp] = est;
578 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000579}
580
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000581//************************* Internal Functions *****************************/
582
583
584static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000585AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000586{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000587 // find the slot to start from, in the current cycle
588 unsigned int startSlot = 0;
589 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000590
Vikram S. Advec5b46322001-09-30 23:43:34 +0000591 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000592
Vikram S. Advec5b46322001-09-30 23:43:34 +0000593 // If only one instruction can be issued, do so.
594 if (maxIssue == 1)
595 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000596 if (S.getChoicesForSlot(s).size() > 0) {
597 // found the one instruction
598 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
599 return;
600 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000601
602 // Otherwise, choose from the choices for each slot
603 //
604 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
605 assert(igroup != NULL && "Group creation failed?");
606
607 // Find a slot that has only a single choice, and take it.
608 // If all slots have 0 or multiple choices, pick the first slot with
609 // choices and use its last instruction (just to avoid shifting the vector).
610 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000611 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
612 int chosenSlot = -1;
613 for (unsigned s=startSlot; s < S.nslots; s++)
614 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
615 chosenSlot = (int) s;
616 break;
617 }
618
619 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000620 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000621 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
622 chosenSlot = (int) s;
623 break;
624 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000625
Misha Brukman6b77ec42003-05-22 21:49:18 +0000626 if (chosenSlot != -1) {
627 // Insert the chosen instr in the chosen slot and
628 // erase it from all slots.
629 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
630 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000631 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000632 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000633
634 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000635}
636
637
638//
639// For now, just assume we are scheduling within a single basic block.
640// Get the machine instruction vector for the basic block and clear it,
641// then append instructions in scheduled order.
642// Also, re-insert the dummy PHI instructions that were at the beginning
643// of the basic block, since they are not part of the schedule.
644//
645static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000646RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000647{
Chris Lattner3501fea2003-01-14 22:00:31 +0000648 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000649
650#ifndef NDEBUG
651 // Lets make sure we didn't lose any instructions, except possibly
652 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
653 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000654 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000655 if (! mii.isNop((*I)->getOpCode()) &&
656 ! mii.isDummyPhiInstr((*I)->getOpCode()))
657 ++numInstr;
658 assert(S.isched.getNumInstructions() >= numInstr &&
659 "Lost some non-NOP instructions during scheduling!");
660#endif
661
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000662 if (S.isched.getNumInstructions() == 0)
663 return; // empty basic block!
664
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000665 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000666 MachineBasicBlock::iterator I = MBB.begin();
667 for ( ; I != MBB.end(); ++I)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000668 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
669 break;
670
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000671 // Erase all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000672 // pre-allocate create space for the ones we will put back in.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000673 MBB.erase(I, MBB.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000674
675 InstrSchedule::const_iterator NIend = S.isched.end();
676 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000677 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000678}
679
680
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000681
682static void
683MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
684{
685 // Check if any successors are now ready that were not already marked
686 // ready before, and that have not yet been scheduled.
687 //
688 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
689 if (! (*SI)->isDummyNode()
690 && ! S.isScheduled(*SI)
691 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000692 {
693 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000694
Misha Brukman6b77ec42003-05-22 21:49:18 +0000695 bool succIsReady = true;
696 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
697 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
698 succIsReady = false;
699 break;
700 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000701
Misha Brukman6b77ec42003-05-22 21:49:18 +0000702 if (succIsReady) // add the successor to the ready list
703 S.schedPrio.insertReady(*SI);
704 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000705}
706
707
708// Choose up to `nslots' FEASIBLE instructions and assign each
709// instruction to all possible slots that do not violate feasibility.
710// FEASIBLE means it should be guaranteed that the set
711// of chosen instructions can be issued in a single group.
712//
713// Return value:
714// maxIssue : total number of feasible instructions
715// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
716//
717static unsigned
718FindSlotChoices(SchedulingManager& S,
719 DelaySlotInfo*& getDelaySlotInfo)
720{
721 // initialize result vectors to empty
722 S.resetChoices();
723
724 // find the slot to start from, in the current cycle
725 unsigned int startSlot = 0;
726 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
727 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000728 if ((*igroup)[s] != NULL) {
729 startSlot = s+1;
730 break;
731 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000732
733 // Make sure we pick at most one instruction that would break the group.
734 // Also, if we do pick one, remember which it was.
735 unsigned int indexForBreakingNode = S.nslots;
736 unsigned int indexForDelayedInstr = S.nslots;
737 DelaySlotInfo* delaySlotInfo = NULL;
738
739 getDelaySlotInfo = NULL;
740
741 // Choose instructions in order of priority.
742 // Add choices to the choice vector in the SchedulingManager class as
743 // we choose them so that subsequent choices will be correctly tested
744 // for feasibility, w.r.t. higher priority choices for the same cycle.
745 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000746 while (S.getNumChoices() < S.nslots - startSlot) {
747 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
748 if (nextNode == NULL)
749 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000750
Misha Brukman6b77ec42003-05-22 21:49:18 +0000751 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0) {
752 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
753 if (delaySlotInfo != NULL) {
754 if (indexForBreakingNode < S.nslots)
755 // cannot issue a delayed instr in the same cycle as one
756 // that breaks the issue group or as another delayed instr
757 nextNode = NULL;
758 else
759 indexForDelayedInstr = S.getNumChoices();
760 }
761 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode())) {
762 if (indexForBreakingNode < S.nslots)
763 // have a breaking instruction already so throw this one away
764 nextNode = NULL;
765 else
766 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000767 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000768
769 if (nextNode != NULL) {
770 S.addChoice(nextNode);
771
772 if (S.schedInfo.isSingleIssue(nextNode->getOpCode())) {
773 assert(S.getNumChoices() == 1 &&
774 "Prioritizer returned invalid instr for this cycle!");
775 break;
776 }
777 }
778
779 if (indexForDelayedInstr < S.nslots)
780 break; // leave the rest for delay slots
781 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000782
783 assert(S.getNumChoices() <= S.nslots);
784 assert(! (indexForDelayedInstr < S.nslots &&
785 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
786
787 // Assign each chosen instruction to all possible slots for that instr.
788 // But if only one instruction was chosen, put it only in the first
789 // feasible slot; no more analysis will be needed.
790 //
791 if (indexForDelayedInstr >= S.nslots &&
792 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000793 { // No instructions that break the issue group or that have delay slots.
794 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000795
Misha Brukman6b77ec42003-05-22 21:49:18 +0000796 if (S.getNumChoices() == 1) {
797 MachineOpCode opCode = S.getChoice(0)->getOpCode();
798 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 } else {
805 for (unsigned i=0; i < S.getNumChoices(); i++) {
806 MachineOpCode opCode = S.getChoice(i)->getOpCode();
807 for (unsigned int s=startSlot; s < S.nslots; s++)
808 if (S.schedInfo.instrCanUseSlot(opCode, s))
809 S.addChoiceToSlot(s, S.getChoice(i));
810 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000811 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000812 } else if (indexForDelayedInstr < S.nslots) {
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 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000818
Misha Brukman6b77ec42003-05-22 21:49:18 +0000819 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
820 "Instruction with delay slots should be last choice!");
821 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000822
Misha Brukman6b77ec42003-05-22 21:49:18 +0000823 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
824 MachineOpCode delayOpCode = delayedNode->getOpCode();
825 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000826
Misha Brukman6b77ec42003-05-22 21:49:18 +0000827 unsigned delayedNodeSlot = S.nslots;
828 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000829
Misha Brukman6b77ec42003-05-22 21:49:18 +0000830 // 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 delayedNodeSlot = s;
835 break;
836 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000837
Misha Brukman6b77ec42003-05-22 21:49:18 +0000838 highestSlotUsed = -1;
839 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
840 // Try to assign every other instruction to a lower numbered
841 // slot than delayedNodeSlot.
842 MachineOpCode opCode =S.getChoice(i)->getOpCode();
843 bool noSlotFound = true;
844 unsigned int s;
845 for (s=startSlot; s < delayedNodeSlot; s++)
846 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
847 S.addChoiceToSlot(s, S.getChoice(i));
848 noSlotFound = false;
849 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000850
Misha Brukman6b77ec42003-05-22 21:49:18 +0000851 // No slot before `delayedNodeSlot' was found for this opCode
852 // Use a later slot, and allow some delay slots to fall in
853 // the next cycle.
854 if (noSlotFound)
855 for ( ; s < S.nslots; s++)
856 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
857 S.addChoiceToSlot(s, S.getChoice(i));
858 break;
859 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000860
Misha Brukman6b77ec42003-05-22 21:49:18 +0000861 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000862
Misha Brukman6b77ec42003-05-22 21:49:18 +0000863 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000864 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000865
Misha Brukman6b77ec42003-05-22 21:49:18 +0000866 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
867
868 // We will put the delayed node in the first slot after the
869 // highest slot used. But we just mark that for now, and
870 // schedule it separately because we want to schedule the delay
871 // slots for the node at the same time.
872 cycles_t dcycle = S.getTime();
873 unsigned int dslot = highestSlotUsed + 1;
874 if (dslot == S.nslots) {
875 dslot = 0;
876 ++dcycle;
877 }
878 delaySlotInfo->recordChosenSlot(dcycle, dslot);
879 getDelaySlotInfo = delaySlotInfo;
880 } else {
881 // There is an instruction that breaks the issue group.
882 // For such an instruction, assign to the last possible slot in
883 // the current group, and then don't assign any other instructions
884 // to later slots.
885 assert(indexForBreakingNode < S.nslots);
886 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
887 unsigned breakingSlot = INT_MAX;
888 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000889
Misha Brukman6b77ec42003-05-22 21:49:18 +0000890 // Find the last possible slot for this instruction.
891 for (int s = S.nslots-1; s >= (int) startSlot; s--)
892 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s)) {
893 breakingSlot = s;
894 break;
895 }
896 assert(breakingSlot < S.nslots &&
897 "No feasible slot for `breakingNode'?");
898
899 // Higher priority instructions than the one that breaks the group:
900 // These can be assigned to all slots, but will be assigned only
901 // to earlier slots if possible.
902 for (unsigned i=0;
903 i < S.getNumChoices() && i < indexForBreakingNode; i++)
904 {
905 MachineOpCode opCode =S.getChoice(i)->getOpCode();
906
907 // If a higher priority instruction cannot be assigned to
908 // any earlier slots, don't schedule the breaking instruction.
909 //
910 bool foundLowerSlot = false;
911 nslotsToUse = S.nslots; // May be modified in the loop
912 for (unsigned int s=startSlot; s < nslotsToUse; s++)
913 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
914 if (breakingSlot < S.nslots && s < breakingSlot) {
915 foundLowerSlot = true;
916 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
917 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000918
Misha Brukman6b77ec42003-05-22 21:49:18 +0000919 S.addChoiceToSlot(s, S.getChoice(i));
920 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000921
Misha Brukman6b77ec42003-05-22 21:49:18 +0000922 if (!foundLowerSlot)
923 breakingSlot = INT_MAX; // disable breaking instr
924 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000925
Misha Brukman6b77ec42003-05-22 21:49:18 +0000926 // Assign the breaking instruction (if any) to a single slot
927 // Otherwise, just ignore the instruction. It will simply be
928 // scheduled in a later cycle.
929 if (breakingSlot < S.nslots) {
930 S.addChoiceToSlot(breakingSlot, breakingNode);
931 nslotsToUse = breakingSlot;
932 } else
933 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000934
Misha Brukman6b77ec42003-05-22 21:49:18 +0000935 // For lower priority instructions than the one that breaks the
936 // group, only assign them to slots lower than the breaking slot.
937 // Otherwise, just ignore the instruction.
938 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
939 MachineOpCode opCode = S.getChoice(i)->getOpCode();
940 for (unsigned int s=startSlot; s < nslotsToUse; s++)
941 if (S.schedInfo.instrCanUseSlot(opCode, s))
942 S.addChoiceToSlot(s, S.getChoice(i));
943 }
944 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000945
946 return S.getNumChoices();
947}
948
949
Vikram S. Advec5b46322001-09-30 23:43:34 +0000950static unsigned
951ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000952{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000953 assert(S.schedPrio.getNumReady() > 0
954 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000955
Vikram S. Advec5b46322001-09-30 23:43:34 +0000956 cycles_t firstCycle = S.getTime();
957 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000958
Vikram S. Advec5b46322001-09-30 23:43:34 +0000959 // Choose up to `nslots' feasible instructions and their possible slots.
960 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000961
Misha Brukman6b77ec42003-05-22 21:49:18 +0000962 while (numIssued == 0) {
963 S.updateTime(S.getTime()+1);
964 numIssued = FindSlotChoices(S, getDelaySlotInfo);
965 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000966
Vikram S. Advec5b46322001-09-30 23:43:34 +0000967 AssignInstructionsToSlots(S, numIssued);
968
969 if (getDelaySlotInfo != NULL)
970 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
971
972 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000973 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
974 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
975 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
976 const InstrGroup* igroup = S.isched.getIGroup(c);
977 for (unsigned int s=0; s < S.nslots; s++) {
978 std::cerr << " ";
979 if ((*igroup)[s] != NULL)
980 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
981 else
982 std::cerr << "<none>\n";
983 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000984 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000985 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000986
987 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000988}
989
990
Vikram S. Advec5b46322001-09-30 23:43:34 +0000991static void
992ForwardListSchedule(SchedulingManager& S)
993{
994 unsigned N;
995 const SchedGraphNode* node;
996
997 S.schedPrio.initialize();
998
Misha Brukman6b77ec42003-05-22 21:49:18 +0000999 while ((N = S.schedPrio.getNumReady()) > 0) {
1000 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001001
Misha Brukman6b77ec42003-05-22 21:49:18 +00001002 // Choose one group of instructions for a cycle, plus any delay slot
1003 // instructions (which may overflow into successive cycles).
1004 // This will advance S.getTime() to the last cycle in which
1005 // instructions are actually issued.
1006 //
1007 unsigned numIssued = ChooseOneGroup(S);
1008 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +00001009
Misha Brukman6b77ec42003-05-22 21:49:18 +00001010 // Notify the priority manager of scheduled instructions and mark
1011 // any successors that may now be ready
1012 //
1013 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
1014 const InstrGroup* igroup = S.isched.getIGroup(c);
1015 for (unsigned int s=0; s < S.nslots; s++)
1016 if ((node = (*igroup)[s]) != NULL) {
1017 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1018 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001019 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001020 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001021
1022 // Move to the next the next earliest cycle for which
1023 // an instruction can be issued, or the next earliest in which
1024 // one will be ready, or to the next cycle, whichever is latest.
1025 //
1026 S.updateTime(std::max(S.getTime() + 1,
1027 std::max(S.getEarliestIssueTime(),
1028 S.schedPrio.getEarliestReadyTime())));
1029 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001030}
1031
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001032
1033//---------------------------------------------------------------------
1034// Code for filling delay slots for delayed terminator instructions
1035// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1036// instructions (e.g., CALL) are not handled here because they almost
1037// always can be filled with instructions from the call sequence code
1038// before a call. That's preferable because we incur many tradeoffs here
1039// when we cannot find single-cycle instructions that can be reordered.
1040//----------------------------------------------------------------------
1041
Vikram S. Advec5b46322001-09-30 23:43:34 +00001042static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001043NodeCanFillDelaySlot(const SchedulingManager& S,
1044 const SchedGraphNode* node,
1045 const SchedGraphNode* brNode,
1046 bool nodeIsPredecessor)
1047{
1048 assert(! node->isDummyNode());
1049
1050 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001051 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001052 return false;
1053
1054 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001055 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001056 return false;
1057
1058 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001059 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001060
1061 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1062 EI != node->endInEdges(); ++EI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001063 if (! ((SchedGraphNode*)(*EI)->getSrc())->isDummyNode()
1064 && mii.isLoad(((SchedGraphNode*)(*EI)->getSrc())->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001065 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1066 return false;
1067
1068 // for now, don't put an instruction that does not have operand
1069 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001070 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001071 return false;
1072
Misha Brukman6eba07a2003-09-17 21:34:23 +00001073 // Finally, if the instruction precedes the branch, we make sure the
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001074 // instruction can be reordered relative to the branch. We simply check
1075 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1076 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001077 if (nodeIsPredecessor) {
1078 bool onlyCDEdgeToBranch = true;
1079 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1080 OEI != node->endOutEdges(); ++OEI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001081 if (! ((SchedGraphNode*)(*OEI)->getSink())->isDummyNode()
Misha Brukman6b77ec42003-05-22 21:49:18 +00001082 && ((*OEI)->getSink() != brNode
1083 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1084 {
1085 onlyCDEdgeToBranch = false;
1086 break;
1087 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001088
Misha Brukman6b77ec42003-05-22 21:49:18 +00001089 if (!onlyCDEdgeToBranch)
1090 return false;
1091 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001092
1093 return true;
1094}
1095
1096
Vikram S. Advec5b46322001-09-30 23:43:34 +00001097static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001098MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001099 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001100 SchedGraphNode* node,
1101 const SchedGraphNode* brNode,
1102 bool nodeIsPredecessor)
1103{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001104 if (nodeIsPredecessor) {
Misha Brukman6eba07a2003-09-17 21:34:23 +00001105 // If node is in the same basic block (i.e., precedes brNode),
Misha Brukman6b77ec42003-05-22 21:49:18 +00001106 // remove it and all its incident edges from the graph. Make sure we
1107 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1108 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1109 } else {
1110 // If the node was from a target block, add the node to the graph
1111 // and add a CD edge from brNode to node.
1112 assert(0 && "NOT IMPLEMENTED YET");
1113 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001114
1115 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1116 dinfo->addDelayNode(node);
1117}
1118
1119
Vikram S. Advec5b46322001-09-30 23:43:34 +00001120void
1121FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1122 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001123 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001124{
Chris Lattner3501fea2003-01-14 22:00:31 +00001125 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001126 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001127 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001128
1129 if (ndelays == 0)
1130 return;
1131
1132 sdelayNodeVec.reserve(ndelays);
1133
1134 // Use a separate vector to hold the feasible multi-cycle nodes.
1135 // These will be used if not enough single-cycle nodes are found.
1136 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001137 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001138
1139 for (sg_pred_iterator P = pred_begin(brNode);
1140 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1141 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001142 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001143 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001144 {
1145 if (mii.maxLatency((*P)->getOpCode()) > 1)
1146 mdelayNodeVec.push_back(*P);
1147 else
1148 sdelayNodeVec.push_back(*P);
1149 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001150
1151 // If not enough single-cycle instructions were found, select the
1152 // lowest-latency multi-cycle instructions and use them.
1153 // Note that this is the most efficient code when only 1 (or even 2)
1154 // values need to be selected.
1155 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001156 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1157 unsigned lmin =
1158 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
1159 unsigned minIndex = 0;
1160 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001161 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001162 unsigned li =
1163 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
1164 if (lmin >= li)
1165 {
1166 lmin = li;
1167 minIndex = i;
1168 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001169 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001170 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1171 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1172 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1173 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001174}
1175
1176
1177// Remove the NOPs currently in delay slots from the graph.
1178// Mark instructions specified in sdelayNodeVec to replace them.
1179// If not enough useful instructions were found, mark the NOPs to be used
1180// for filling delay slots, otherwise, otherwise just discard them.
1181//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001182static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1183 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001184 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001185 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001186 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001187{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001188 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001189 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001190 const MachineInstr* brInstr = node->getMachineInstr();
1191 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001192 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1193
1194 // Remove the NOPs currently in delay slots from the graph.
1195 // If not enough useful instructions were found, use the NOPs to
1196 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001197 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001198 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001199 MachineBasicBlock& MBB = node->getMachineBasicBlock();
1200 assert(MBB[firstDelaySlotIdx - 1] == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001201 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001202
1203 // First find all useful instructions already in the delay slots
1204 // and USE THEM. We'll throw away the unused alternatives below
1205 //
1206 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001207 if (! mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001208 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001209 graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001210
1211 // Then find the NOPs and keep only as many as are needed.
1212 // Put the rest in nopNodeVec to be deleted.
1213 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001214 if (mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001215 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001216 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001217 else {
1218 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001219
Misha Brukman6b77ec42003-05-22 21:49:18 +00001220 //remove the MI from the Machine Code For Instruction
Chris Lattner9cdaa632003-07-26 23:23:41 +00001221 const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Misha Brukman6b77ec42003-05-22 21:49:18 +00001222 MachineCodeForInstruction& llvmMvec =
Chris Lattner9cdaa632003-07-26 23:23:41 +00001223 MachineCodeForInstruction::get((const Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001224
Misha Brukman6b77ec42003-05-22 21:49:18 +00001225 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1226 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1227 if (*mciI==MBB[i])
1228 llvmMvec.erase(mciI);
1229 }
1230 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001231
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001232 assert(sdelayNodeVec.size() >= ndelays);
1233
1234 // If some delay slots were already filled, throw away that many new choices
1235 if (sdelayNodeVec.size() > ndelays)
1236 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001237
1238 // Mark the nodes chosen for delay slots. This removes them from the graph.
1239 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1240 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1241
1242 // And remove the unused NOPs from the graph.
1243 for (unsigned i=0; i < nopNodeVec.size(); i++)
1244 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1245}
1246
1247
1248// For all delayed instructions, choose instructions to put in the delay
1249// slots and pull those out of the graph. Mark them for the delay slots
1250// in the DelaySlotInfo object for that graph node. If no useful work
1251// is found for a delay slot, use the NOP that is currently in that slot.
1252//
1253// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001254// EXCEPT CALLS AND RETURNS.
1255// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001256// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001257// suboptimal. Also, it complicates generating the calling sequence code in
1258// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001259//
1260static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001261ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001262 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001263{
Chris Lattner3501fea2003-01-14 22:00:31 +00001264 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001265
1266 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001267 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001268 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001269 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001270
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001271 if (EnableFillingDelaySlots &&
1272 termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001273 {
1274 // To find instructions that need delay slots without searching the full
1275 // machine code, we assume that the only delayed instructions are CALLs
1276 // or instructions generated for the terminator inst.
1277 // Find the first branch instr in the sequence of machine instrs for term
1278 //
1279 unsigned first = 0;
1280 while (first < termMvec.size() &&
1281 ! mii.isBranch(termMvec[first]->getOpCode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001282 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001283 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001284 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001285 assert(first < termMvec.size() &&
1286 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1287
1288 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1289
1290 // Compute a vector of the nodes chosen for delay slots and then
1291 // mark delay slots to replace NOPs with these useful instructions.
1292 //
1293 if (brInstr != NULL) {
1294 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1295 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1296 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1297 }
1298 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001299
1300 // Also mark delay slots for other delayed instructions to hold NOPs.
1301 // Simply passing in an empty delayNodeVec will have this effect.
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001302 // If brInstr is not handled above (EnableFillingDelaySlots == false),
1303 // brInstr will be NULL so this will handle the branch instrs. as well.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001304 //
1305 delayNodeVec.clear();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001306 for (unsigned i=0; i < MBB.size(); ++i)
1307 if (MBB[i] != brInstr &&
1308 mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001309 {
1310 SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]);
1311 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1312 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001313}
1314
1315
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001316//
1317// Schedule the delayed branch and its delay slots
1318//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001319unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001320DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1321{
1322 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1323 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1324 && "Slot for branch should be empty");
1325
1326 unsigned int nextSlot = delayedNodeSlotNum;
1327 cycles_t nextTime = delayedNodeCycle;
1328
1329 S.scheduleInstr(brNode, nextSlot, nextTime);
1330
Misha Brukman6b77ec42003-05-22 21:49:18 +00001331 for (unsigned d=0; d < ndelays; d++) {
1332 ++nextSlot;
1333 if (nextSlot == S.nslots) {
1334 nextSlot = 0;
1335 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001336 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001337
1338 // Find the first feasible instruction for this delay slot
1339 // Note that we only check for issue restrictions here.
1340 // We do *not* check for flow dependences but rely on pipeline
1341 // interlocks to resolve them. Machines without interlocks
1342 // will require this code to be modified.
1343 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1344 const SchedGraphNode* dnode = delayNodeVec[i];
1345 if ( ! S.isScheduled(dnode)
1346 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1347 && instrIsFeasible(S, dnode->getOpCode()))
1348 {
1349 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
1350 && "Instructions without interlocks not yet supported "
1351 "when filling branch delay slots");
1352 S.scheduleInstr(dnode, nextSlot, nextTime);
1353 break;
1354 }
1355 }
1356 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001357
1358 // Update current time if delay slots overflowed into later cycles.
1359 // Do this here because we know exactly which cycle is the last cycle
1360 // that contains delay slots. The next loop doesn't compute that.
1361 if (nextTime > S.getTime())
1362 S.updateTime(nextTime);
1363
1364 // Now put any remaining instructions in the unfilled delay slots.
1365 // This could lead to suboptimal performance but needed for correctness.
1366 nextSlot = delayedNodeSlotNum;
1367 nextTime = delayedNodeCycle;
1368 for (unsigned i=0; i < delayNodeVec.size(); i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001369 if (! S.isScheduled(delayNodeVec[i])) {
1370 do { // find the next empty slot
1371 ++nextSlot;
1372 if (nextSlot == S.nslots) {
1373 nextSlot = 0;
1374 nextTime++;
1375 }
1376 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001377
Misha Brukman6b77ec42003-05-22 21:49:18 +00001378 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1379 break;
1380 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001381
1382 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001383}
1384
Vikram S. Advec5b46322001-09-30 23:43:34 +00001385
1386// Check if the instruction would conflict with instructions already
1387// chosen for the current cycle
1388//
1389static inline bool
1390ConflictsWithChoices(const SchedulingManager& S,
1391 MachineOpCode opCode)
1392{
1393 // Check if the instruction must issue by itself, and some feasible
1394 // choices have already been made for this cycle
1395 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1396 return true;
1397
1398 // For each class that opCode belongs to, check if there are too many
1399 // instructions of that class.
1400 //
1401 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1402 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1403}
1404
1405
1406//************************* External Functions *****************************/
1407
1408
1409//---------------------------------------------------------------------------
1410// Function: ViolatesMinimumGap
1411//
1412// Purpose:
1413// Check minimum gap requirements relative to instructions scheduled in
1414// previous cycles.
1415// Note that we do not need to consider `nextEarliestIssueTime' here because
1416// that is also captured in the earliest start times for each opcode.
1417//---------------------------------------------------------------------------
1418
1419static inline bool
1420ViolatesMinimumGap(const SchedulingManager& S,
1421 MachineOpCode opCode,
1422 const cycles_t inCycle)
1423{
1424 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1425}
1426
1427
1428//---------------------------------------------------------------------------
1429// Function: instrIsFeasible
1430//
1431// Purpose:
1432// Check if any issue restrictions would prevent the instruction from
1433// being issued in the current cycle
1434//---------------------------------------------------------------------------
1435
1436bool
1437instrIsFeasible(const SchedulingManager& S,
1438 MachineOpCode opCode)
1439{
1440 // skip the instruction if it cannot be issued due to issue restrictions
1441 // caused by previously issued instructions
1442 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1443 return false;
1444
1445 // skip the instruction if it cannot be issued due to issue restrictions
1446 // caused by previously chosen instructions for the current cycle
1447 if (ConflictsWithChoices(S, opCode))
1448 return false;
1449
1450 return true;
1451}
1452
1453//---------------------------------------------------------------------------
1454// Function: ScheduleInstructionsWithSSA
1455//
1456// Purpose:
1457// Entry point for instruction scheduling on SSA form.
1458// Schedules the machine instructions generated by instruction selection.
1459// Assumes that register allocation has not been done, i.e., operands
1460// are still in SSA form.
1461//---------------------------------------------------------------------------
1462
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001463namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001464 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001465 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001466 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001467 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001468
1469 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001470
Chris Lattnerf57b8452002-04-27 06:56:12 +00001471 // getAnalysisUsage - We use LiveVarInfo...
1472 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001473 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001474 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001475 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001476
Chris Lattner7e708292002-06-25 16:13:24 +00001477 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001478 };
1479} // end anonymous namespace
1480
Vikram S. Adve802cec42002-03-24 03:44:55 +00001481
Chris Lattner7e708292002-06-25 16:13:24 +00001482bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001483{
Chris Lattner7e708292002-06-25 16:13:24 +00001484 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001485
Misha Brukman6b77ec42003-05-22 21:49:18 +00001486 if (SchedDebugLevel >= Sched_PrintSchedGraphs) {
Misha Brukmanc2312df2003-05-22 21:24:35 +00001487 std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001488 graphSet.dump();
1489 }
1490
1491 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1492 GI != GE; ++GI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001493 {
1494 SchedGraph* graph = (*GI);
1495 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001496
Misha Brukman6b77ec42003-05-22 21:49:18 +00001497 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1498 std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001499
Misha Brukman6b77ec42003-05-22 21:49:18 +00001500 // expensive!
1501 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
1502 SchedulingManager S(target, graph, schedPrio);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001503
Misha Brukman6b77ec42003-05-22 21:49:18 +00001504 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
1505 ForwardListSchedule(S); // computes schedule in S
1506 RecordSchedule(MBB, S); // records schedule in BB
1507 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001508
Misha Brukman6b77ec42003-05-22 21:49:18 +00001509 if (SchedDebugLevel >= Sched_PrintMachineCode) {
1510 std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
1511 MachineFunction::get(&F).dump();
1512 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001513
1514 return false;
1515}
1516
1517
Brian Gaekebf3c4cf2003-08-14 06:09:32 +00001518FunctionPass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001519 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001520}