blob: 6cc271a7d30af88b5c720b1141c4e56e6eb7ce7c [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
2//
3// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
4// generic support routines for instruction scheduling.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +00007
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00009#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000010#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000011#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000012#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000013#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000014#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000015#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000016#include <algorithm>
Vikram S. Advec5b46322001-09-30 23:43:34 +000017
Chris Lattner70e60cb2002-05-22 17:08:27 +000018SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000019
Chris Lattner5ff62e92002-07-22 02:10:13 +000020static cl::opt<SchedDebugLevel_t, true>
21SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
22 cl::desc("enable instruction scheduling debugging information"),
23 cl::values(
24 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000025 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
26 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
27 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
28 0));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000029
30
Vikram S. Advec5b46322001-09-30 23:43:34 +000031//************************* Internal Data Types *****************************/
32
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000033class InstrSchedule;
34class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000035
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000036
37//----------------------------------------------------------------------
38// class InstrGroup:
39//
40// Represents a group of instructions scheduled to be issued
41// in a single cycle.
42//----------------------------------------------------------------------
43
44class InstrGroup: public NonCopyable {
45public:
46 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
47 assert(slotNum < group.size());
48 return group[slotNum];
49 }
50
51private:
52 friend class InstrSchedule;
53
54 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
55 assert(slotNum < group.size());
56 group[slotNum] = node;
57 }
58
59 /*ctor*/ InstrGroup(unsigned int nslots)
60 : group(nslots, NULL) {}
61
62 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
63
64private:
Misha Brukmanc2312df2003-05-22 21:24:35 +000065 std::vector<const SchedGraphNode*> group;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000066};
67
68
69//----------------------------------------------------------------------
70// class ScheduleIterator:
71//
72// Iterates over the machine instructions in the for a single basic block.
73// The schedule is represented by an InstrSchedule object.
74//----------------------------------------------------------------------
75
76template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000077class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000078private:
79 unsigned cycleNum;
80 unsigned slotNum;
81 const InstrSchedule& S;
82public:
83 typedef ScheduleIterator<_NodeType> _Self;
84
85 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
86 unsigned _cycleNum,
87 unsigned _slotNum)
88 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
89 skipToNextInstr();
90 }
91
92 /*ctor*/ inline ScheduleIterator(const _Self& x)
93 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
94
95 inline bool operator==(const _Self& x) const {
96 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
97 }
98
99 inline bool operator!=(const _Self& x) const { return !operator==(x); }
100
101 inline _NodeType* operator*() const {
102 assert(cycleNum < S.groups.size());
103 return (*S.groups[cycleNum])[slotNum];
104 }
105 inline _NodeType* operator->() const { return operator*(); }
106
107 _Self& operator++(); // Preincrement
108 inline _Self operator++(int) { // Postincrement
109 _Self tmp(*this); ++*this; return tmp;
110 }
111
112 static _Self begin(const InstrSchedule& _schedule);
113 static _Self end( const InstrSchedule& _schedule);
114
115private:
116 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
117 void skipToNextInstr();
118};
119
120
121//----------------------------------------------------------------------
122// class InstrSchedule:
123//
124// Represents the schedule of machine instructions for a single basic block.
125//----------------------------------------------------------------------
126
127class InstrSchedule: public NonCopyable {
128private:
129 const unsigned int nslots;
130 unsigned int numInstr;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000131 std::vector<InstrGroup*> groups; // indexed by cycle number
132 std::vector<cycles_t> startTime; // indexed by node id
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000133
134public: // iterators
135 typedef ScheduleIterator<SchedGraphNode> iterator;
136 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
137
138 iterator begin();
139 const_iterator begin() const;
140 iterator end();
141 const_iterator end() const;
142
143public: // constructors and destructor
144 /*ctor*/ InstrSchedule (unsigned int _nslots,
145 unsigned int _numNodes);
146 /*dtor*/ ~InstrSchedule ();
147
148public: // accessor functions to query chosen schedule
149 const SchedGraphNode* getInstr (unsigned int slotNum,
150 cycles_t c) const {
151 const InstrGroup* igroup = this->getIGroup(c);
152 return (igroup == NULL)? NULL : (*igroup)[slotNum];
153 }
154
155 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000156 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000157 groups.resize(c+1);
158 if (groups[c] == NULL)
159 groups[c] = new InstrGroup(nslots);
160 return groups[c];
161 }
162
163 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000164 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000165 return groups[c];
166 }
167
168 inline cycles_t getStartTime (unsigned int nodeId) const {
169 assert(nodeId < startTime.size());
170 return startTime[nodeId];
171 }
172
173 unsigned int getNumInstructions() const {
174 return numInstr;
175 }
176
177 inline void scheduleInstr (const SchedGraphNode* node,
178 unsigned int slotNum,
179 cycles_t cycle) {
180 InstrGroup* igroup = this->getIGroup(cycle);
181 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
182 igroup->addInstr(node, slotNum);
183 assert(node->getNodeId() < startTime.size());
184 startTime[node->getNodeId()] = cycle;
185 ++numInstr;
186 }
187
188private:
189 friend class iterator;
190 friend class const_iterator;
191 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
192};
193
194
195/*ctor*/
196InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
197 : nslots(_nslots),
198 numInstr(0),
199 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
200 startTime(_numNodes, (cycles_t) -1) // set all to -1
201{
202}
203
204
205/*dtor*/
206InstrSchedule::~InstrSchedule()
207{
208 for (unsigned c=0, NC=groups.size(); c < NC; c++)
209 if (groups[c] != NULL)
210 delete groups[c]; // delete InstrGroup objects
211}
212
213
214template<class _NodeType>
215inline
216void
217ScheduleIterator<_NodeType>::skipToNextInstr()
218{
219 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
220 ++cycleNum; // skip cycles with no instructions
221
222 while (cycleNum < S.groups.size() &&
223 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000224 {
225 ++slotNum;
226 if (slotNum == S.nslots) {
227 ++cycleNum;
228 slotNum = 0;
229 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
230 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000231 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000232 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000233}
234
235template<class _NodeType>
236inline
237ScheduleIterator<_NodeType>&
238ScheduleIterator<_NodeType>::operator++() // Preincrement
239{
240 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000241 if (slotNum == S.nslots) {
242 ++cycleNum;
243 slotNum = 0;
244 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000245 skipToNextInstr();
246 return *this;
247}
248
249template<class _NodeType>
250ScheduleIterator<_NodeType>
251ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
252{
253 return _Self(_schedule, 0, 0);
254}
255
256template<class _NodeType>
257ScheduleIterator<_NodeType>
258ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
259{
260 return _Self(_schedule, _schedule.groups.size(), 0);
261}
262
263InstrSchedule::iterator
264InstrSchedule::begin()
265{
266 return iterator::begin(*this);
267}
268
269InstrSchedule::const_iterator
270InstrSchedule::begin() const
271{
272 return const_iterator::begin(*this);
273}
274
275InstrSchedule::iterator
276InstrSchedule::end()
277{
278 return iterator::end(*this);
279}
280
281InstrSchedule::const_iterator
282InstrSchedule::end() const
283{
284 return const_iterator::end( *this);
285}
286
287
288//----------------------------------------------------------------------
289// class DelaySlotInfo:
290//
291// Record information about delay slots for a single branch instruction.
292// Delay slots are simply indexed by slot number 1 ... numDelaySlots
293//----------------------------------------------------------------------
294
295class DelaySlotInfo: public NonCopyable {
296private:
297 const SchedGraphNode* brNode;
298 unsigned int ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000299 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000300 cycles_t delayedNodeCycle;
301 unsigned int delayedNodeSlotNum;
302
303public:
304 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
305 unsigned _ndelays)
306 : brNode(_brNode), ndelays(_ndelays),
307 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
308
309 inline unsigned getNumDelays () {
310 return ndelays;
311 }
312
Misha Brukmanc2312df2003-05-22 21:24:35 +0000313 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000314 return delayNodeVec;
315 }
316
317 inline void addDelayNode (const SchedGraphNode* node) {
318 delayNodeVec.push_back(node);
319 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
320 }
321
322 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
323 delayedNodeCycle = cycle;
324 delayedNodeSlotNum = slotNum;
325 }
326
Vikram S. Advec5b46322001-09-30 23:43:34 +0000327 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000328};
329
330
331//----------------------------------------------------------------------
332// class SchedulingManager:
333//
334// Represents the schedule of machine instructions for a single basic block.
335//----------------------------------------------------------------------
336
337class SchedulingManager: public NonCopyable {
338public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000339 const unsigned nslots;
340 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000341 SchedPriorities& schedPrio;
342 InstrSchedule isched;
343
344private:
345 unsigned int totalInstrCount;
346 cycles_t curTime;
347 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000348 // indexed by slot#
349 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
350 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
351 std::vector<int> numInClass; // indexed by sched class
352 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000353 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000354 // indexed by branch node ptr
355
356public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000357 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
358 SchedPriorities& schedPrio);
359 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000360 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000361 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
362 E = delaySlotInfoForBranches.end(); I != E; ++I)
363 delete I->second;
364 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000365
366 //----------------------------------------------------------------------
367 // Simplify access to the machine instruction info
368 //----------------------------------------------------------------------
369
Chris Lattner3501fea2003-01-14 22:00:31 +0000370 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000371 return schedInfo.getInstrInfo();
372 }
373
374 //----------------------------------------------------------------------
375 // Interface for checking and updating the current time
376 //----------------------------------------------------------------------
377
378 inline cycles_t getTime () const {
379 return curTime;
380 }
381
382 inline cycles_t getEarliestIssueTime() const {
383 return nextEarliestIssueTime;
384 }
385
386 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
387 assert(opCode < (int) nextEarliestStartTime.size());
388 return nextEarliestStartTime[opCode];
389 }
390
391 // Update current time to specified cycle
392 inline void updateTime (cycles_t c) {
393 curTime = c;
394 schedPrio.updateTime(c);
395 }
396
397 //----------------------------------------------------------------------
398 // Functions to manage the choices for the current cycle including:
399 // -- a vector of choices by priority (choiceVec)
400 // -- vectors of the choices for each instruction slot (choicesForSlot[])
401 // -- number of choices in each sched class, used to check issue conflicts
402 // between choices for a single cycle
403 //----------------------------------------------------------------------
404
405 inline unsigned int getNumChoices () const {
406 return choiceVec.size();
407 }
408
409 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000410 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000411 return numInClass[sc];
412 }
413
414 inline const SchedGraphNode* getChoice(unsigned int i) const {
415 // assert(i < choiceVec.size()); don't check here.
416 return choiceVec[i];
417 }
418
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000419 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000420 assert(slotNum < nslots);
421 return choicesForSlot[slotNum];
422 }
423
424 inline void addChoice (const SchedGraphNode* node) {
425 // Append the instruction to the vector of choices for current cycle.
426 // Increment numInClass[c] for the sched class to which the instr belongs.
427 choiceVec.push_back(node);
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000428 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000429 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000430 numInClass[sc]++;
431 }
432
433 inline void addChoiceToSlot (unsigned int slotNum,
434 const SchedGraphNode* node) {
435 // Add the instruction to the choice set for the specified slot
436 assert(slotNum < nslots);
437 choicesForSlot[slotNum].insert(node);
438 }
439
440 inline void resetChoices () {
441 choiceVec.clear();
442 for (unsigned int s=0; s < nslots; s++)
443 choicesForSlot[s].clear();
444 for (unsigned int c=0; c < numInClass.size(); c++)
445 numInClass[c] = 0;
446 }
447
448 //----------------------------------------------------------------------
449 // Code to query and manage the partial instruction schedule so far
450 //----------------------------------------------------------------------
451
452 inline unsigned int getNumScheduled () const {
453 return isched.getNumInstructions();
454 }
455
456 inline unsigned int getNumUnscheduled() const {
457 return totalInstrCount - isched.getNumInstructions();
458 }
459
460 inline bool isScheduled (const SchedGraphNode* node) const {
461 return (isched.getStartTime(node->getNodeId()) >= 0);
462 }
463
464 inline void scheduleInstr (const SchedGraphNode* node,
465 unsigned int slotNum,
466 cycles_t cycle)
467 {
468 assert(! isScheduled(node) && "Instruction already scheduled?");
469
470 // add the instruction to the schedule
471 isched.scheduleInstr(node, slotNum, cycle);
472
473 // update the earliest start times of all nodes that conflict with `node'
474 // and the next-earliest time anything can issue if `node' causes bubbles
475 updateEarliestStartTimes(node, cycle);
476
477 // remove the instruction from the choice sets for all slots
478 for (unsigned s=0; s < nslots; s++)
479 choicesForSlot[s].erase(node);
480
481 // and decrement the instr count for the sched class to which it belongs
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000482 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000483 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000484 numInClass[sc]--;
485 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000486
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000487 //----------------------------------------------------------------------
488 // Create and retrieve delay slot info for delayed instructions
489 //----------------------------------------------------------------------
490
491 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
492 bool createIfMissing=false)
493 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000494 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000495 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000496 if (I != delaySlotInfoForBranches.end())
497 return I->second;
498
499 if (!createIfMissing) return 0;
500
501 DelaySlotInfo *dinfo =
502 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode()));
503 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000504 }
505
506private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000507 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
508 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000509};
510
511
512/*ctor*/
513SchedulingManager::SchedulingManager(const TargetMachine& target,
514 const SchedGraph* graph,
515 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000516 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
517 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000518 schedPrio(_schedPrio),
519 isched(nslots, graph->getNumNodes()),
520 totalInstrCount(graph->getNumNodes() - 2),
521 nextEarliestIssueTime(0),
522 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000523 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000524 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
525 (cycles_t) 0) // set all to 0
526{
527 updateTime(0);
528
529 // Note that an upper bound on #choices for each slot is = nslots since
530 // we use this vector to hold a feasible set of instructions, and more
531 // would be infeasible. Reserve that much memory since it is probably small.
532 for (unsigned int i=0; i < nslots; i++)
533 choicesForSlot[i].resize(nslots);
534}
535
536
537void
538SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
539 cycles_t schedTime)
540{
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000541 if (schedInfo.numBubblesAfter(node->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000542 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000543 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000544 curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000545 }
546
Vikram S. Adve1632e882002-10-13 00:40:37 +0000547 const std::vector<MachineOpCode>&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +0000548 conflictVec = schedInfo.getConflictList(node->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000549
Vikram S. Adve1632e882002-10-13 00:40:37 +0000550 for (unsigned i=0; i < conflictVec.size(); i++)
551 {
552 MachineOpCode toOp = conflictVec[i];
553 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp);
554 assert(toOp < (int) nextEarliestStartTime.size());
555 if (nextEarliestStartTime[toOp] < est)
556 nextEarliestStartTime[toOp] = est;
557 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000558}
559
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000560//************************* Internal Functions *****************************/
561
562
563static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000564AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000565{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000566 // find the slot to start from, in the current cycle
567 unsigned int startSlot = 0;
568 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000569
Vikram S. Advec5b46322001-09-30 23:43:34 +0000570 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000571
Vikram S. Advec5b46322001-09-30 23:43:34 +0000572 // If only one instruction can be issued, do so.
573 if (maxIssue == 1)
574 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000575 if (S.getChoicesForSlot(s).size() > 0) {
576 // found the one instruction
577 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
578 return;
579 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000580
581 // Otherwise, choose from the choices for each slot
582 //
583 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
584 assert(igroup != NULL && "Group creation failed?");
585
586 // Find a slot that has only a single choice, and take it.
587 // If all slots have 0 or multiple choices, pick the first slot with
588 // choices and use its last instruction (just to avoid shifting the vector).
589 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000590 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
591 int chosenSlot = -1;
592 for (unsigned s=startSlot; s < S.nslots; s++)
593 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
594 chosenSlot = (int) s;
595 break;
596 }
597
598 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000599 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000600 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
601 chosenSlot = (int) s;
602 break;
603 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000604
Misha Brukman6b77ec42003-05-22 21:49:18 +0000605 if (chosenSlot != -1) {
606 // Insert the chosen instr in the chosen slot and
607 // erase it from all slots.
608 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
609 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000610 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000611 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000612
613 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000614}
615
616
617//
618// For now, just assume we are scheduling within a single basic block.
619// Get the machine instruction vector for the basic block and clear it,
620// then append instructions in scheduled order.
621// Also, re-insert the dummy PHI instructions that were at the beginning
622// of the basic block, since they are not part of the schedule.
623//
624static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000625RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000626{
Chris Lattner3501fea2003-01-14 22:00:31 +0000627 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000628
629#ifndef NDEBUG
630 // Lets make sure we didn't lose any instructions, except possibly
631 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
632 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000633 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000634 if (! mii.isNop((*I)->getOpCode()) &&
635 ! mii.isDummyPhiInstr((*I)->getOpCode()))
636 ++numInstr;
637 assert(S.isched.getNumInstructions() >= numInstr &&
638 "Lost some non-NOP instructions during scheduling!");
639#endif
640
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000641 if (S.isched.getNumInstructions() == 0)
642 return; // empty basic block!
643
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000644 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000645 MachineBasicBlock::iterator I = MBB.begin();
646 for ( ; I != MBB.end(); ++I)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000647 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
648 break;
649
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000650 // Erase all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000651 // pre-allocate create space for the ones we will put back in.
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000652 MBB.erase(I, MBB.end());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000653
654 InstrSchedule::const_iterator NIend = S.isched.end();
655 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000656 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000657}
658
659
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000660
661static void
662MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
663{
664 // Check if any successors are now ready that were not already marked
665 // ready before, and that have not yet been scheduled.
666 //
667 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
668 if (! (*SI)->isDummyNode()
669 && ! S.isScheduled(*SI)
670 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000671 {
672 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000673
Misha Brukman6b77ec42003-05-22 21:49:18 +0000674 bool succIsReady = true;
675 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
676 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
677 succIsReady = false;
678 break;
679 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000680
Misha Brukman6b77ec42003-05-22 21:49:18 +0000681 if (succIsReady) // add the successor to the ready list
682 S.schedPrio.insertReady(*SI);
683 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000684}
685
686
687// Choose up to `nslots' FEASIBLE instructions and assign each
688// instruction to all possible slots that do not violate feasibility.
689// FEASIBLE means it should be guaranteed that the set
690// of chosen instructions can be issued in a single group.
691//
692// Return value:
693// maxIssue : total number of feasible instructions
694// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
695//
696static unsigned
697FindSlotChoices(SchedulingManager& S,
698 DelaySlotInfo*& getDelaySlotInfo)
699{
700 // initialize result vectors to empty
701 S.resetChoices();
702
703 // find the slot to start from, in the current cycle
704 unsigned int startSlot = 0;
705 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
706 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000707 if ((*igroup)[s] != NULL) {
708 startSlot = s+1;
709 break;
710 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000711
712 // Make sure we pick at most one instruction that would break the group.
713 // Also, if we do pick one, remember which it was.
714 unsigned int indexForBreakingNode = S.nslots;
715 unsigned int indexForDelayedInstr = S.nslots;
716 DelaySlotInfo* delaySlotInfo = NULL;
717
718 getDelaySlotInfo = NULL;
719
720 // Choose instructions in order of priority.
721 // Add choices to the choice vector in the SchedulingManager class as
722 // we choose them so that subsequent choices will be correctly tested
723 // for feasibility, w.r.t. higher priority choices for the same cycle.
724 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000725 while (S.getNumChoices() < S.nslots - startSlot) {
726 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
727 if (nextNode == NULL)
728 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000729
Misha Brukman6b77ec42003-05-22 21:49:18 +0000730 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0) {
731 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
732 if (delaySlotInfo != NULL) {
733 if (indexForBreakingNode < S.nslots)
734 // cannot issue a delayed instr in the same cycle as one
735 // that breaks the issue group or as another delayed instr
736 nextNode = NULL;
737 else
738 indexForDelayedInstr = S.getNumChoices();
739 }
740 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode())) {
741 if (indexForBreakingNode < S.nslots)
742 // have a breaking instruction already so throw this one away
743 nextNode = NULL;
744 else
745 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000746 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000747
748 if (nextNode != NULL) {
749 S.addChoice(nextNode);
750
751 if (S.schedInfo.isSingleIssue(nextNode->getOpCode())) {
752 assert(S.getNumChoices() == 1 &&
753 "Prioritizer returned invalid instr for this cycle!");
754 break;
755 }
756 }
757
758 if (indexForDelayedInstr < S.nslots)
759 break; // leave the rest for delay slots
760 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000761
762 assert(S.getNumChoices() <= S.nslots);
763 assert(! (indexForDelayedInstr < S.nslots &&
764 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
765
766 // Assign each chosen instruction to all possible slots for that instr.
767 // But if only one instruction was chosen, put it only in the first
768 // feasible slot; no more analysis will be needed.
769 //
770 if (indexForDelayedInstr >= S.nslots &&
771 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000772 { // No instructions that break the issue group or that have delay slots.
773 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000774
Misha Brukman6b77ec42003-05-22 21:49:18 +0000775 if (S.getNumChoices() == 1) {
776 MachineOpCode opCode = S.getChoice(0)->getOpCode();
777 unsigned int s;
778 for (s=startSlot; s < S.nslots; s++)
779 if (S.schedInfo.instrCanUseSlot(opCode, s))
780 break;
781 assert(s < S.nslots && "No feasible slot for this opCode?");
782 S.addChoiceToSlot(s, S.getChoice(0));
783 } else {
784 for (unsigned i=0; i < S.getNumChoices(); i++) {
785 MachineOpCode opCode = S.getChoice(i)->getOpCode();
786 for (unsigned int s=startSlot; s < S.nslots; s++)
787 if (S.schedInfo.instrCanUseSlot(opCode, s))
788 S.addChoiceToSlot(s, S.getChoice(i));
789 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000790 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000791 } else if (indexForDelayedInstr < S.nslots) {
792 // There is an instruction that needs delay slots.
793 // Try to assign that instruction to a higher slot than any other
794 // instructions in the group, so that its delay slots can go
795 // right after it.
796 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000797
Misha Brukman6b77ec42003-05-22 21:49:18 +0000798 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
799 "Instruction with delay slots should be last choice!");
800 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000801
Misha Brukman6b77ec42003-05-22 21:49:18 +0000802 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
803 MachineOpCode delayOpCode = delayedNode->getOpCode();
804 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000805
Misha Brukman6b77ec42003-05-22 21:49:18 +0000806 unsigned delayedNodeSlot = S.nslots;
807 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000808
Misha Brukman6b77ec42003-05-22 21:49:18 +0000809 // Find the last possible slot for the delayed instruction that leaves
810 // at least `d' slots vacant after it (d = #delay slots)
811 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
812 if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) {
813 delayedNodeSlot = s;
814 break;
815 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000816
Misha Brukman6b77ec42003-05-22 21:49:18 +0000817 highestSlotUsed = -1;
818 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
819 // Try to assign every other instruction to a lower numbered
820 // slot than delayedNodeSlot.
821 MachineOpCode opCode =S.getChoice(i)->getOpCode();
822 bool noSlotFound = true;
823 unsigned int s;
824 for (s=startSlot; s < delayedNodeSlot; s++)
825 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
826 S.addChoiceToSlot(s, S.getChoice(i));
827 noSlotFound = false;
828 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000829
Misha Brukman6b77ec42003-05-22 21:49:18 +0000830 // No slot before `delayedNodeSlot' was found for this opCode
831 // Use a later slot, and allow some delay slots to fall in
832 // the next cycle.
833 if (noSlotFound)
834 for ( ; s < S.nslots; s++)
835 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
836 S.addChoiceToSlot(s, S.getChoice(i));
837 break;
838 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000839
Misha Brukman6b77ec42003-05-22 21:49:18 +0000840 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000841
Misha Brukman6b77ec42003-05-22 21:49:18 +0000842 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000843 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000844
Misha Brukman6b77ec42003-05-22 21:49:18 +0000845 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
846
847 // We will put the delayed node in the first slot after the
848 // highest slot used. But we just mark that for now, and
849 // schedule it separately because we want to schedule the delay
850 // slots for the node at the same time.
851 cycles_t dcycle = S.getTime();
852 unsigned int dslot = highestSlotUsed + 1;
853 if (dslot == S.nslots) {
854 dslot = 0;
855 ++dcycle;
856 }
857 delaySlotInfo->recordChosenSlot(dcycle, dslot);
858 getDelaySlotInfo = delaySlotInfo;
859 } else {
860 // There is an instruction that breaks the issue group.
861 // For such an instruction, assign to the last possible slot in
862 // the current group, and then don't assign any other instructions
863 // to later slots.
864 assert(indexForBreakingNode < S.nslots);
865 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
866 unsigned breakingSlot = INT_MAX;
867 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000868
Misha Brukman6b77ec42003-05-22 21:49:18 +0000869 // Find the last possible slot for this instruction.
870 for (int s = S.nslots-1; s >= (int) startSlot; s--)
871 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s)) {
872 breakingSlot = s;
873 break;
874 }
875 assert(breakingSlot < S.nslots &&
876 "No feasible slot for `breakingNode'?");
877
878 // Higher priority instructions than the one that breaks the group:
879 // These can be assigned to all slots, but will be assigned only
880 // to earlier slots if possible.
881 for (unsigned i=0;
882 i < S.getNumChoices() && i < indexForBreakingNode; i++)
883 {
884 MachineOpCode opCode =S.getChoice(i)->getOpCode();
885
886 // If a higher priority instruction cannot be assigned to
887 // any earlier slots, don't schedule the breaking instruction.
888 //
889 bool foundLowerSlot = false;
890 nslotsToUse = S.nslots; // May be modified in the loop
891 for (unsigned int s=startSlot; s < nslotsToUse; s++)
892 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
893 if (breakingSlot < S.nslots && s < breakingSlot) {
894 foundLowerSlot = true;
895 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
896 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000897
Misha Brukman6b77ec42003-05-22 21:49:18 +0000898 S.addChoiceToSlot(s, S.getChoice(i));
899 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000900
Misha Brukman6b77ec42003-05-22 21:49:18 +0000901 if (!foundLowerSlot)
902 breakingSlot = INT_MAX; // disable breaking instr
903 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000904
Misha Brukman6b77ec42003-05-22 21:49:18 +0000905 // Assign the breaking instruction (if any) to a single slot
906 // Otherwise, just ignore the instruction. It will simply be
907 // scheduled in a later cycle.
908 if (breakingSlot < S.nslots) {
909 S.addChoiceToSlot(breakingSlot, breakingNode);
910 nslotsToUse = breakingSlot;
911 } else
912 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000913
Misha Brukman6b77ec42003-05-22 21:49:18 +0000914 // For lower priority instructions than the one that breaks the
915 // group, only assign them to slots lower than the breaking slot.
916 // Otherwise, just ignore the instruction.
917 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
918 MachineOpCode opCode = S.getChoice(i)->getOpCode();
919 for (unsigned int s=startSlot; s < nslotsToUse; s++)
920 if (S.schedInfo.instrCanUseSlot(opCode, s))
921 S.addChoiceToSlot(s, S.getChoice(i));
922 }
923 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000924
925 return S.getNumChoices();
926}
927
928
Vikram S. Advec5b46322001-09-30 23:43:34 +0000929static unsigned
930ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000931{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000932 assert(S.schedPrio.getNumReady() > 0
933 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000934
Vikram S. Advec5b46322001-09-30 23:43:34 +0000935 cycles_t firstCycle = S.getTime();
936 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000937
Vikram S. Advec5b46322001-09-30 23:43:34 +0000938 // Choose up to `nslots' feasible instructions and their possible slots.
939 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000940
Misha Brukman6b77ec42003-05-22 21:49:18 +0000941 while (numIssued == 0) {
942 S.updateTime(S.getTime()+1);
943 numIssued = FindSlotChoices(S, getDelaySlotInfo);
944 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000945
Vikram S. Advec5b46322001-09-30 23:43:34 +0000946 AssignInstructionsToSlots(S, numIssued);
947
948 if (getDelaySlotInfo != NULL)
949 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
950
951 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000952 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
953 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
954 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
955 const InstrGroup* igroup = S.isched.getIGroup(c);
956 for (unsigned int s=0; s < S.nslots; s++) {
957 std::cerr << " ";
958 if ((*igroup)[s] != NULL)
959 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
960 else
961 std::cerr << "<none>\n";
962 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000963 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000964 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000965
966 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000967}
968
969
Vikram S. Advec5b46322001-09-30 23:43:34 +0000970static void
971ForwardListSchedule(SchedulingManager& S)
972{
973 unsigned N;
974 const SchedGraphNode* node;
975
976 S.schedPrio.initialize();
977
Misha Brukman6b77ec42003-05-22 21:49:18 +0000978 while ((N = S.schedPrio.getNumReady()) > 0) {
979 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +0000980
Misha Brukman6b77ec42003-05-22 21:49:18 +0000981 // Choose one group of instructions for a cycle, plus any delay slot
982 // instructions (which may overflow into successive cycles).
983 // This will advance S.getTime() to the last cycle in which
984 // instructions are actually issued.
985 //
986 unsigned numIssued = ChooseOneGroup(S);
987 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +0000988
Misha Brukman6b77ec42003-05-22 21:49:18 +0000989 // Notify the priority manager of scheduled instructions and mark
990 // any successors that may now be ready
991 //
992 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
993 const InstrGroup* igroup = S.isched.getIGroup(c);
994 for (unsigned int s=0; s < S.nslots; s++)
995 if ((node = (*igroup)[s]) != NULL) {
996 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
997 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +0000998 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000999 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001000
1001 // Move to the next the next earliest cycle for which
1002 // an instruction can be issued, or the next earliest in which
1003 // one will be ready, or to the next cycle, whichever is latest.
1004 //
1005 S.updateTime(std::max(S.getTime() + 1,
1006 std::max(S.getEarliestIssueTime(),
1007 S.schedPrio.getEarliestReadyTime())));
1008 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001009}
1010
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001011
1012//---------------------------------------------------------------------
1013// Code for filling delay slots for delayed terminator instructions
1014// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1015// instructions (e.g., CALL) are not handled here because they almost
1016// always can be filled with instructions from the call sequence code
1017// before a call. That's preferable because we incur many tradeoffs here
1018// when we cannot find single-cycle instructions that can be reordered.
1019//----------------------------------------------------------------------
1020
Vikram S. Advec5b46322001-09-30 23:43:34 +00001021static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001022NodeCanFillDelaySlot(const SchedulingManager& S,
1023 const SchedGraphNode* node,
1024 const SchedGraphNode* brNode,
1025 bool nodeIsPredecessor)
1026{
1027 assert(! node->isDummyNode());
1028
1029 // don't put a branch in the delay slot of another branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001030 if (S.getInstrInfo().isBranch(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001031 return false;
1032
1033 // don't put a single-issue instruction in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001034 if (S.schedInfo.isSingleIssue(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001035 return false;
1036
1037 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001038 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001039
1040 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1041 EI != node->endInEdges(); ++EI)
1042 if (! (*EI)->getSrc()->isDummyNode()
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001043 && mii.isLoad((*EI)->getSrc()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001044 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1045 return false;
1046
1047 // for now, don't put an instruction that does not have operand
1048 // interlocks in the delay slot of a branch
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001049 if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001050 return false;
1051
1052 // Finally, if the instruction preceeds the branch, we make sure the
1053 // instruction can be reordered relative to the branch. We simply check
1054 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1055 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001056 if (nodeIsPredecessor) {
1057 bool onlyCDEdgeToBranch = true;
1058 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1059 OEI != node->endOutEdges(); ++OEI)
1060 if (! (*OEI)->getSink()->isDummyNode()
1061 && ((*OEI)->getSink() != brNode
1062 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1063 {
1064 onlyCDEdgeToBranch = false;
1065 break;
1066 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001067
Misha Brukman6b77ec42003-05-22 21:49:18 +00001068 if (!onlyCDEdgeToBranch)
1069 return false;
1070 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001071
1072 return true;
1073}
1074
1075
Vikram S. Advec5b46322001-09-30 23:43:34 +00001076static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001077MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001078 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001079 SchedGraphNode* node,
1080 const SchedGraphNode* brNode,
1081 bool nodeIsPredecessor)
1082{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001083 if (nodeIsPredecessor) {
1084 // If node is in the same basic block (i.e., preceeds brNode),
1085 // remove it and all its incident edges from the graph. Make sure we
1086 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1087 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1088 } else {
1089 // If the node was from a target block, add the node to the graph
1090 // and add a CD edge from brNode to node.
1091 assert(0 && "NOT IMPLEMENTED YET");
1092 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001093
1094 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1095 dinfo->addDelayNode(node);
1096}
1097
1098
Vikram S. Advec5b46322001-09-30 23:43:34 +00001099void
1100FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1101 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001102 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001103{
Chris Lattner3501fea2003-01-14 22:00:31 +00001104 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001105 unsigned ndelays =
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001106 mii.getNumDelaySlots(brNode->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001107
1108 if (ndelays == 0)
1109 return;
1110
1111 sdelayNodeVec.reserve(ndelays);
1112
1113 // Use a separate vector to hold the feasible multi-cycle nodes.
1114 // These will be used if not enough single-cycle nodes are found.
1115 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001116 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001117
1118 for (sg_pred_iterator P = pred_begin(brNode);
1119 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1120 if (! (*P)->isDummyNode() &&
Vikram S. Advefb1a6c82001-11-09 02:14:20 +00001121 ! mii.isNop((*P)->getOpCode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001122 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001123 {
1124 if (mii.maxLatency((*P)->getOpCode()) > 1)
1125 mdelayNodeVec.push_back(*P);
1126 else
1127 sdelayNodeVec.push_back(*P);
1128 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001129
1130 // If not enough single-cycle instructions were found, select the
1131 // lowest-latency multi-cycle instructions and use them.
1132 // Note that this is the most efficient code when only 1 (or even 2)
1133 // values need to be selected.
1134 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001135 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1136 unsigned lmin =
1137 mii.maxLatency(mdelayNodeVec[0]->getOpCode());
1138 unsigned minIndex = 0;
1139 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001140 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001141 unsigned li =
1142 mii.maxLatency(mdelayNodeVec[i]->getOpCode());
1143 if (lmin >= li)
1144 {
1145 lmin = li;
1146 minIndex = i;
1147 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001148 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001149 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1150 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1151 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1152 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001153}
1154
1155
1156// Remove the NOPs currently in delay slots from the graph.
1157// Mark instructions specified in sdelayNodeVec to replace them.
1158// If not enough useful instructions were found, mark the NOPs to be used
1159// for filling delay slots, otherwise, otherwise just discard them.
1160//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001161static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1162 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001163 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001164 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001165 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001166{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001167 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001168 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001169 const MachineInstr* brInstr = node->getMachineInstr();
1170 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001171 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1172
1173 // Remove the NOPs currently in delay slots from the graph.
1174 // If not enough useful instructions were found, use the NOPs to
1175 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001176 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001177 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001178 MachineBasicBlock& MBB = node->getMachineBasicBlock();
1179 assert(MBB[firstDelaySlotIdx - 1] == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001180 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001181
1182 // First find all useful instructions already in the delay slots
1183 // and USE THEM. We'll throw away the unused alternatives below
1184 //
1185 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001186 if (! mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001187 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001188 graph->getGraphNodeForInstr(MBB[i]));
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001189
1190 // Then find the NOPs and keep only as many as are needed.
1191 // Put the rest in nopNodeVec to be deleted.
1192 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001193 if (mii.isNop(MBB[i]->getOpCode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001194 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001195 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001196 else {
1197 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i]));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001198
Misha Brukman6b77ec42003-05-22 21:49:18 +00001199 //remove the MI from the Machine Code For Instruction
1200 TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
1201 MachineCodeForInstruction& llvmMvec =
1202 MachineCodeForInstruction::get((Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001203
Misha Brukman6b77ec42003-05-22 21:49:18 +00001204 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1205 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
1206 if (*mciI==MBB[i])
1207 llvmMvec.erase(mciI);
1208 }
1209 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001210
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001211 assert(sdelayNodeVec.size() >= ndelays);
1212
1213 // If some delay slots were already filled, throw away that many new choices
1214 if (sdelayNodeVec.size() > ndelays)
1215 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001216
1217 // Mark the nodes chosen for delay slots. This removes them from the graph.
1218 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1219 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1220
1221 // And remove the unused NOPs from the graph.
1222 for (unsigned i=0; i < nopNodeVec.size(); i++)
1223 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1224}
1225
1226
1227// For all delayed instructions, choose instructions to put in the delay
1228// slots and pull those out of the graph. Mark them for the delay slots
1229// in the DelaySlotInfo object for that graph node. If no useful work
1230// is found for a delay slot, use the NOP that is currently in that slot.
1231//
1232// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001233// EXCEPT CALLS AND RETURNS.
1234// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001235// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001236// suboptimal. Also, it complicates generating the calling sequence code in
1237// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001238//
1239static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001240ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001241 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001242{
Chris Lattner3501fea2003-01-14 22:00:31 +00001243 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001244
1245 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001246 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001247 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001248 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001249
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001250 if (termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001251 {
1252 // To find instructions that need delay slots without searching the full
1253 // machine code, we assume that the only delayed instructions are CALLs
1254 // or instructions generated for the terminator inst.
1255 // Find the first branch instr in the sequence of machine instrs for term
1256 //
1257 unsigned first = 0;
1258 while (first < termMvec.size() &&
1259 ! mii.isBranch(termMvec[first]->getOpCode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001260 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001261 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001262 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001263 assert(first < termMvec.size() &&
1264 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1265
1266 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1267
1268 // Compute a vector of the nodes chosen for delay slots and then
1269 // mark delay slots to replace NOPs with these useful instructions.
1270 //
1271 if (brInstr != NULL) {
1272 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1273 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1274 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1275 }
1276 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001277
1278 // Also mark delay slots for other delayed instructions to hold NOPs.
1279 // Simply passing in an empty delayNodeVec will have this effect.
1280 //
1281 delayNodeVec.clear();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001282 for (unsigned i=0; i < MBB.size(); ++i)
1283 if (MBB[i] != brInstr &&
1284 mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001285 {
1286 SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]);
1287 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1288 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001289}
1290
1291
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001292//
1293// Schedule the delayed branch and its delay slots
1294//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001295unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001296DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1297{
1298 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1299 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1300 && "Slot for branch should be empty");
1301
1302 unsigned int nextSlot = delayedNodeSlotNum;
1303 cycles_t nextTime = delayedNodeCycle;
1304
1305 S.scheduleInstr(brNode, nextSlot, nextTime);
1306
Misha Brukman6b77ec42003-05-22 21:49:18 +00001307 for (unsigned d=0; d < ndelays; d++) {
1308 ++nextSlot;
1309 if (nextSlot == S.nslots) {
1310 nextSlot = 0;
1311 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001312 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001313
1314 // Find the first feasible instruction for this delay slot
1315 // Note that we only check for issue restrictions here.
1316 // We do *not* check for flow dependences but rely on pipeline
1317 // interlocks to resolve them. Machines without interlocks
1318 // will require this code to be modified.
1319 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1320 const SchedGraphNode* dnode = delayNodeVec[i];
1321 if ( ! S.isScheduled(dnode)
1322 && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot)
1323 && instrIsFeasible(S, dnode->getOpCode()))
1324 {
1325 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode())
1326 && "Instructions without interlocks not yet supported "
1327 "when filling branch delay slots");
1328 S.scheduleInstr(dnode, nextSlot, nextTime);
1329 break;
1330 }
1331 }
1332 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001333
1334 // Update current time if delay slots overflowed into later cycles.
1335 // Do this here because we know exactly which cycle is the last cycle
1336 // that contains delay slots. The next loop doesn't compute that.
1337 if (nextTime > S.getTime())
1338 S.updateTime(nextTime);
1339
1340 // Now put any remaining instructions in the unfilled delay slots.
1341 // This could lead to suboptimal performance but needed for correctness.
1342 nextSlot = delayedNodeSlotNum;
1343 nextTime = delayedNodeCycle;
1344 for (unsigned i=0; i < delayNodeVec.size(); i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001345 if (! S.isScheduled(delayNodeVec[i])) {
1346 do { // find the next empty slot
1347 ++nextSlot;
1348 if (nextSlot == S.nslots) {
1349 nextSlot = 0;
1350 nextTime++;
1351 }
1352 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001353
Misha Brukman6b77ec42003-05-22 21:49:18 +00001354 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1355 break;
1356 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001357
1358 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001359}
1360
Vikram S. Advec5b46322001-09-30 23:43:34 +00001361
1362// Check if the instruction would conflict with instructions already
1363// chosen for the current cycle
1364//
1365static inline bool
1366ConflictsWithChoices(const SchedulingManager& S,
1367 MachineOpCode opCode)
1368{
1369 // Check if the instruction must issue by itself, and some feasible
1370 // choices have already been made for this cycle
1371 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1372 return true;
1373
1374 // For each class that opCode belongs to, check if there are too many
1375 // instructions of that class.
1376 //
1377 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1378 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1379}
1380
1381
1382//************************* External Functions *****************************/
1383
1384
1385//---------------------------------------------------------------------------
1386// Function: ViolatesMinimumGap
1387//
1388// Purpose:
1389// Check minimum gap requirements relative to instructions scheduled in
1390// previous cycles.
1391// Note that we do not need to consider `nextEarliestIssueTime' here because
1392// that is also captured in the earliest start times for each opcode.
1393//---------------------------------------------------------------------------
1394
1395static inline bool
1396ViolatesMinimumGap(const SchedulingManager& S,
1397 MachineOpCode opCode,
1398 const cycles_t inCycle)
1399{
1400 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1401}
1402
1403
1404//---------------------------------------------------------------------------
1405// Function: instrIsFeasible
1406//
1407// Purpose:
1408// Check if any issue restrictions would prevent the instruction from
1409// being issued in the current cycle
1410//---------------------------------------------------------------------------
1411
1412bool
1413instrIsFeasible(const SchedulingManager& S,
1414 MachineOpCode opCode)
1415{
1416 // skip the instruction if it cannot be issued due to issue restrictions
1417 // caused by previously issued instructions
1418 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1419 return false;
1420
1421 // skip the instruction if it cannot be issued due to issue restrictions
1422 // caused by previously chosen instructions for the current cycle
1423 if (ConflictsWithChoices(S, opCode))
1424 return false;
1425
1426 return true;
1427}
1428
1429//---------------------------------------------------------------------------
1430// Function: ScheduleInstructionsWithSSA
1431//
1432// Purpose:
1433// Entry point for instruction scheduling on SSA form.
1434// Schedules the machine instructions generated by instruction selection.
1435// Assumes that register allocation has not been done, i.e., operands
1436// are still in SSA form.
1437//---------------------------------------------------------------------------
1438
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001439namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001440 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001441 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001442 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001443 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001444
1445 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001446
Chris Lattnerf57b8452002-04-27 06:56:12 +00001447 // getAnalysisUsage - We use LiveVarInfo...
1448 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001449 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001450 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001451 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001452
Chris Lattner7e708292002-06-25 16:13:24 +00001453 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001454 };
1455} // end anonymous namespace
1456
Vikram S. Adve802cec42002-03-24 03:44:55 +00001457
Chris Lattner7e708292002-06-25 16:13:24 +00001458bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001459{
Chris Lattner7e708292002-06-25 16:13:24 +00001460 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001461
Misha Brukman6b77ec42003-05-22 21:49:18 +00001462 if (SchedDebugLevel >= Sched_PrintSchedGraphs) {
Misha Brukmanc2312df2003-05-22 21:24:35 +00001463 std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001464 graphSet.dump();
1465 }
1466
1467 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1468 GI != GE; ++GI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001469 {
1470 SchedGraph* graph = (*GI);
1471 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001472
Misha Brukman6b77ec42003-05-22 21:49:18 +00001473 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1474 std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001475
Misha Brukman6b77ec42003-05-22 21:49:18 +00001476 // expensive!
1477 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
1478 SchedulingManager S(target, graph, schedPrio);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001479
Misha Brukman6b77ec42003-05-22 21:49:18 +00001480 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
1481 ForwardListSchedule(S); // computes schedule in S
1482 RecordSchedule(MBB, S); // records schedule in BB
1483 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001484
Misha Brukman6b77ec42003-05-22 21:49:18 +00001485 if (SchedDebugLevel >= Sched_PrintMachineCode) {
1486 std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
1487 MachineFunction::get(&F).dump();
1488 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001489
1490 return false;
1491}
1492
1493
Chris Lattnerf57b8452002-04-27 06:56:12 +00001494Pass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001495 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001496}