blob: 5de987b3b5409ff88ceadcd1cedd93dd1f524efb [file] [log] [blame]
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001// $Id$
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00002//***************************************************************************
3// File:
4// InstrScheduling.cpp
5//
6// Purpose:
7//
8// History:
9// 7/23/01 - Vikram Adve - Created
Vikram S. Advef0ba2802001-09-18 12:51:38 +000010//**************************************************************************/
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000011
Vikram S. Advec5b46322001-09-30 23:43:34 +000012
13//************************* User Include Files *****************************/
14
Chris Lattner1ff63a12001-09-07 21:19:42 +000015#include "llvm/CodeGen/InstrScheduling.h"
Chris Lattner46cbff62001-09-14 16:56:32 +000016#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000017#include "llvm/Analysis/LiveVar/BBLiveVar.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Instruction.h"
Vikram S. Advec5b46322001-09-30 23:43:34 +000021
22
23//************************ System Include Files *****************************/
24
Chris Lattner1ff63a12001-09-07 21:19:42 +000025#include <hash_set>
26#include <algorithm>
27#include <iterator>
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000028
Vikram S. Advec5b46322001-09-30 23:43:34 +000029
30//************************* External Data Types *****************************/
31
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000032cl::Enum<enum SchedDebugLevel_t> SchedDebugLevel("dsched", cl::NoFlags,
33 "enable instruction scheduling debugging information",
34 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
35 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"), 0);
38
39
Vikram S. Advec5b46322001-09-30 23:43:34 +000040//************************* Internal Data Types *****************************/
41
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000042class InstrSchedule;
43class SchedulingManager;
44class DelaySlotInfo;
45
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
54class InstrGroup: public NonCopyable {
55public:
56 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
57 assert(slotNum < group.size());
58 return group[slotNum];
59 }
60
61private:
62 friend class InstrSchedule;
63
64 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
65 assert(slotNum < group.size());
66 group[slotNum] = node;
67 }
68
69 /*ctor*/ InstrGroup(unsigned int nslots)
70 : group(nslots, NULL) {}
71
72 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
73
74private:
75 vector<const SchedGraphNode*> group;
76};
77
78
79//----------------------------------------------------------------------
80// class ScheduleIterator:
81//
82// Iterates over the machine instructions in the for a single basic block.
83// The schedule is represented by an InstrSchedule object.
84//----------------------------------------------------------------------
85
86template<class _NodeType>
87class ScheduleIterator: public std::forward_iterator<_NodeType, ptrdiff_t> {
88private:
89 unsigned cycleNum;
90 unsigned slotNum;
91 const InstrSchedule& S;
92public:
93 typedef ScheduleIterator<_NodeType> _Self;
94
95 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
96 unsigned _cycleNum,
97 unsigned _slotNum)
98 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
99 skipToNextInstr();
100 }
101
102 /*ctor*/ inline ScheduleIterator(const _Self& x)
103 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
104
105 inline bool operator==(const _Self& x) const {
106 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
107 }
108
109 inline bool operator!=(const _Self& x) const { return !operator==(x); }
110
111 inline _NodeType* operator*() const {
112 assert(cycleNum < S.groups.size());
113 return (*S.groups[cycleNum])[slotNum];
114 }
115 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
137class InstrSchedule: public NonCopyable {
138private:
139 const unsigned int nslots;
140 unsigned int numInstr;
141 vector<InstrGroup*> groups; // indexed by cycle number
142 vector<cycles_t> startTime; // indexed by node id
143
144public: // iterators
145 typedef ScheduleIterator<SchedGraphNode> iterator;
146 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
147
148 iterator begin();
149 const_iterator begin() const;
150 iterator end();
151 const_iterator end() const;
152
153public: // constructors and destructor
154 /*ctor*/ InstrSchedule (unsigned int _nslots,
155 unsigned int _numNodes);
156 /*dtor*/ ~InstrSchedule ();
157
158public: // accessor functions to query chosen schedule
159 const SchedGraphNode* getInstr (unsigned int slotNum,
160 cycles_t c) const {
161 const InstrGroup* igroup = this->getIGroup(c);
162 return (igroup == NULL)? NULL : (*igroup)[slotNum];
163 }
164
165 inline InstrGroup* getIGroup (cycles_t c) {
166 if (c >= groups.size())
167 groups.resize(c+1);
168 if (groups[c] == NULL)
169 groups[c] = new InstrGroup(nslots);
170 return groups[c];
171 }
172
173 inline const InstrGroup* getIGroup (cycles_t c) const {
174 assert(c < groups.size());
175 return groups[c];
176 }
177
178 inline cycles_t getStartTime (unsigned int nodeId) const {
179 assert(nodeId < startTime.size());
180 return startTime[nodeId];
181 }
182
183 unsigned int getNumInstructions() const {
184 return numInstr;
185 }
186
187 inline void scheduleInstr (const SchedGraphNode* node,
188 unsigned int slotNum,
189 cycles_t cycle) {
190 InstrGroup* igroup = this->getIGroup(cycle);
191 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
192 igroup->addInstr(node, slotNum);
193 assert(node->getNodeId() < startTime.size());
194 startTime[node->getNodeId()] = cycle;
195 ++numInstr;
196 }
197
198private:
199 friend class iterator;
200 friend class const_iterator;
201 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
202};
203
204
205/*ctor*/
206InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
207 : nslots(_nslots),
208 numInstr(0),
209 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
210 startTime(_numNodes, (cycles_t) -1) // set all to -1
211{
212}
213
214
215/*dtor*/
216InstrSchedule::~InstrSchedule()
217{
218 for (unsigned c=0, NC=groups.size(); c < NC; c++)
219 if (groups[c] != NULL)
220 delete groups[c]; // delete InstrGroup objects
221}
222
223
224template<class _NodeType>
225inline
226void
227ScheduleIterator<_NodeType>::skipToNextInstr()
228{
229 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
230 ++cycleNum; // skip cycles with no instructions
231
232 while (cycleNum < S.groups.size() &&
233 (*S.groups[cycleNum])[slotNum] == NULL)
234 {
235 ++slotNum;
236 if (slotNum == S.nslots)
237 {
238 ++cycleNum;
239 slotNum = 0;
240 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
241 ++cycleNum; // skip cycles with no instructions
242 }
243 }
244}
245
246template<class _NodeType>
247inline
248ScheduleIterator<_NodeType>&
249ScheduleIterator<_NodeType>::operator++() // Preincrement
250{
251 ++slotNum;
252 if (slotNum == S.nslots)
253 {
254 ++cycleNum;
255 slotNum = 0;
256 }
257 skipToNextInstr();
258 return *this;
259}
260
261template<class _NodeType>
262ScheduleIterator<_NodeType>
263ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
264{
265 return _Self(_schedule, 0, 0);
266}
267
268template<class _NodeType>
269ScheduleIterator<_NodeType>
270ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
271{
272 return _Self(_schedule, _schedule.groups.size(), 0);
273}
274
275InstrSchedule::iterator
276InstrSchedule::begin()
277{
278 return iterator::begin(*this);
279}
280
281InstrSchedule::const_iterator
282InstrSchedule::begin() const
283{
284 return const_iterator::begin(*this);
285}
286
287InstrSchedule::iterator
288InstrSchedule::end()
289{
290 return iterator::end(*this);
291}
292
293InstrSchedule::const_iterator
294InstrSchedule::end() const
295{
296 return const_iterator::end( *this);
297}
298
299
300//----------------------------------------------------------------------
301// class DelaySlotInfo:
302//
303// Record information about delay slots for a single branch instruction.
304// Delay slots are simply indexed by slot number 1 ... numDelaySlots
305//----------------------------------------------------------------------
306
307class DelaySlotInfo: public NonCopyable {
308private:
309 const SchedGraphNode* brNode;
310 unsigned int ndelays;
311 vector<const SchedGraphNode*> delayNodeVec;
312 cycles_t delayedNodeCycle;
313 unsigned int delayedNodeSlotNum;
314
315public:
316 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
317 unsigned _ndelays)
318 : brNode(_brNode), ndelays(_ndelays),
319 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
320
321 inline unsigned getNumDelays () {
322 return ndelays;
323 }
324
325 inline const vector<const SchedGraphNode*>& getDelayNodeVec() {
326 return delayNodeVec;
327 }
328
329 inline void addDelayNode (const SchedGraphNode* node) {
330 delayNodeVec.push_back(node);
331 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
332 }
333
334 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
335 delayedNodeCycle = cycle;
336 delayedNodeSlotNum = slotNum;
337 }
338
Vikram S. Advec5b46322001-09-30 23:43:34 +0000339 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000340};
341
342
343//----------------------------------------------------------------------
344// class SchedulingManager:
345//
346// Represents the schedule of machine instructions for a single basic block.
347//----------------------------------------------------------------------
348
349class SchedulingManager: public NonCopyable {
350public: // publicly accessible data members
351 const unsigned int nslots;
352 const MachineSchedInfo& schedInfo;
353 SchedPriorities& schedPrio;
354 InstrSchedule isched;
355
356private:
357 unsigned int totalInstrCount;
358 cycles_t curTime;
359 cycles_t nextEarliestIssueTime; // next cycle we can issue
360 vector<hash_set<const SchedGraphNode*> > choicesForSlot; // indexed by slot#
361 vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
362 vector<int> numInClass; // indexed by sched class
363 vector<cycles_t> nextEarliestStartTime; // indexed by opCode
364 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
365 // indexed by branch node ptr
366
367public:
368 /*ctor*/ SchedulingManager (const TargetMachine& _target,
369 const SchedGraph* graph,
370 SchedPriorities& schedPrio);
371 /*dtor*/ ~SchedulingManager () {}
372
373 //----------------------------------------------------------------------
374 // Simplify access to the machine instruction info
375 //----------------------------------------------------------------------
376
377 inline const MachineInstrInfo& getInstrInfo () const {
378 return schedInfo.getInstrInfo();
379 }
380
381 //----------------------------------------------------------------------
382 // Interface for checking and updating the current time
383 //----------------------------------------------------------------------
384
385 inline cycles_t getTime () const {
386 return curTime;
387 }
388
389 inline cycles_t getEarliestIssueTime() const {
390 return nextEarliestIssueTime;
391 }
392
393 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
394 assert(opCode < (int) nextEarliestStartTime.size());
395 return nextEarliestStartTime[opCode];
396 }
397
398 // Update current time to specified cycle
399 inline void updateTime (cycles_t c) {
400 curTime = c;
401 schedPrio.updateTime(c);
402 }
403
404 //----------------------------------------------------------------------
405 // Functions to manage the choices for the current cycle including:
406 // -- a vector of choices by priority (choiceVec)
407 // -- vectors of the choices for each instruction slot (choicesForSlot[])
408 // -- number of choices in each sched class, used to check issue conflicts
409 // between choices for a single cycle
410 //----------------------------------------------------------------------
411
412 inline unsigned int getNumChoices () const {
413 return choiceVec.size();
414 }
415
416 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
417 assert(sc < (int) numInClass.size() && "Invalid op code or sched class!");
418 return numInClass[sc];
419 }
420
421 inline const SchedGraphNode* getChoice(unsigned int i) const {
422 // assert(i < choiceVec.size()); don't check here.
423 return choiceVec[i];
424 }
425
426 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
427 assert(slotNum < nslots);
428 return choicesForSlot[slotNum];
429 }
430
431 inline void addChoice (const SchedGraphNode* node) {
432 // Append the instruction to the vector of choices for current cycle.
433 // Increment numInClass[c] for the sched class to which the instr belongs.
434 choiceVec.push_back(node);
Chris Lattner1ff63a12001-09-07 21:19:42 +0000435 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getMachineInstr()->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000436 assert(sc < (int) numInClass.size());
437 numInClass[sc]++;
438 }
439
440 inline void addChoiceToSlot (unsigned int slotNum,
441 const SchedGraphNode* node) {
442 // Add the instruction to the choice set for the specified slot
443 assert(slotNum < nslots);
444 choicesForSlot[slotNum].insert(node);
445 }
446
447 inline void resetChoices () {
448 choiceVec.clear();
449 for (unsigned int s=0; s < nslots; s++)
450 choicesForSlot[s].clear();
451 for (unsigned int c=0; c < numInClass.size(); c++)
452 numInClass[c] = 0;
453 }
454
455 //----------------------------------------------------------------------
456 // Code to query and manage the partial instruction schedule so far
457 //----------------------------------------------------------------------
458
459 inline unsigned int getNumScheduled () const {
460 return isched.getNumInstructions();
461 }
462
463 inline unsigned int getNumUnscheduled() const {
464 return totalInstrCount - isched.getNumInstructions();
465 }
466
467 inline bool isScheduled (const SchedGraphNode* node) const {
468 return (isched.getStartTime(node->getNodeId()) >= 0);
469 }
470
471 inline void scheduleInstr (const SchedGraphNode* node,
472 unsigned int slotNum,
473 cycles_t cycle)
474 {
475 assert(! isScheduled(node) && "Instruction already scheduled?");
476
477 // add the instruction to the schedule
478 isched.scheduleInstr(node, slotNum, cycle);
479
480 // update the earliest start times of all nodes that conflict with `node'
481 // and the next-earliest time anything can issue if `node' causes bubbles
482 updateEarliestStartTimes(node, cycle);
483
484 // remove the instruction from the choice sets for all slots
485 for (unsigned s=0; s < nslots; s++)
486 choicesForSlot[s].erase(node);
487
488 // and decrement the instr count for the sched class to which it belongs
Chris Lattner1ff63a12001-09-07 21:19:42 +0000489 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getMachineInstr()->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000490 assert(sc < (int) numInClass.size());
491 numInClass[sc]--;
492 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000493
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000494 //----------------------------------------------------------------------
495 // Create and retrieve delay slot info for delayed instructions
496 //----------------------------------------------------------------------
497
498 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
499 bool createIfMissing=false)
500 {
501 DelaySlotInfo* dinfo;
502 hash_map<const SchedGraphNode*, DelaySlotInfo* >::const_iterator
503 I = delaySlotInfoForBranches.find(bn);
504 if (I == delaySlotInfoForBranches.end())
505 {
506 if (createIfMissing)
507 {
508 dinfo = new DelaySlotInfo(bn,
Vikram S. Advec5b46322001-09-30 23:43:34 +0000509 getInstrInfo().getNumDelaySlots(bn->getMachineInstr()->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000510 delaySlotInfoForBranches[bn] = dinfo;
511 }
512 else
513 dinfo = NULL;
514 }
515 else
516 dinfo = (*I).second;
517
518 return dinfo;
519 }
520
521private:
522 /*ctor*/ SchedulingManager (); // Disable: DO NOT IMPLEMENT.
523 void updateEarliestStartTimes(const SchedGraphNode* node,
524 cycles_t schedTime);
525};
526
527
528/*ctor*/
529SchedulingManager::SchedulingManager(const TargetMachine& target,
530 const SchedGraph* graph,
531 SchedPriorities& _schedPrio)
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000532 : nslots(target.getSchedInfo().getMaxNumIssueTotal()),
533 schedInfo(target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000534 schedPrio(_schedPrio),
535 isched(nslots, graph->getNumNodes()),
536 totalInstrCount(graph->getNumNodes() - 2),
537 nextEarliestIssueTime(0),
538 choicesForSlot(nslots),
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000539 numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000540 nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(),
541 (cycles_t) 0) // set all to 0
542{
543 updateTime(0);
544
545 // Note that an upper bound on #choices for each slot is = nslots since
546 // we use this vector to hold a feasible set of instructions, and more
547 // would be infeasible. Reserve that much memory since it is probably small.
548 for (unsigned int i=0; i < nslots; i++)
549 choicesForSlot[i].resize(nslots);
550}
551
552
553void
554SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
555 cycles_t schedTime)
556{
Chris Lattner1ff63a12001-09-07 21:19:42 +0000557 if (schedInfo.numBubblesAfter(node->getMachineInstr()->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000558 { // Update next earliest time before which *nothing* can issue.
559 nextEarliestIssueTime = max(nextEarliestIssueTime,
Chris Lattner1ff63a12001-09-07 21:19:42 +0000560 curTime + 1 + schedInfo.numBubblesAfter(node->getMachineInstr()->getOpCode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000561 }
562
563 const vector<MachineOpCode>*
Chris Lattner1ff63a12001-09-07 21:19:42 +0000564 conflictVec = schedInfo.getConflictList(node->getMachineInstr()->getOpCode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000565
566 if (conflictVec != NULL)
567 for (unsigned i=0; i < conflictVec->size(); i++)
568 {
569 MachineOpCode toOp = (*conflictVec)[i];
Chris Lattner1ff63a12001-09-07 21:19:42 +0000570 cycles_t est = schedTime + schedInfo.getMinIssueGap(node->getMachineInstr()->getOpCode(),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000571 toOp);
572 assert(toOp < (int) nextEarliestStartTime.size());
573 if (nextEarliestStartTime[toOp] < est)
574 nextEarliestStartTime[toOp] = est;
575 }
576}
577
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000578//************************* Internal Functions *****************************/
579
580
581static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000582AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000583{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000584 // find the slot to start from, in the current cycle
585 unsigned int startSlot = 0;
586 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000587
Vikram S. Advec5b46322001-09-30 23:43:34 +0000588 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000589
Vikram S. Advec5b46322001-09-30 23:43:34 +0000590 // If only one instruction can be issued, do so.
591 if (maxIssue == 1)
592 for (unsigned s=startSlot; s < S.nslots; s++)
593 if (S.getChoicesForSlot(s).size() > 0)
594 {// found the one instruction
595 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
596 return;
597 }
598
599 // Otherwise, choose from the choices for each slot
600 //
601 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
602 assert(igroup != NULL && "Group creation failed?");
603
604 // Find a slot that has only a single choice, and take it.
605 // If all slots have 0 or multiple choices, pick the first slot with
606 // choices and use its last instruction (just to avoid shifting the vector).
607 unsigned numIssued;
608 for (numIssued = 0; numIssued < maxIssue; numIssued++)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000609 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000610 int chosenSlot = -1, chosenNodeIndex = -1;
611 for (unsigned s=startSlot; s < S.nslots; s++)
612 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000613 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000614 chosenSlot = (int) s;
615 break;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000616 }
617
Vikram S. Advec5b46322001-09-30 23:43:34 +0000618 if (chosenSlot == -1)
619 for (unsigned s=startSlot; s < S.nslots; s++)
620 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0)
621 {
622 chosenSlot = (int) s;
623 break;
624 }
625
626 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);
631 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +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
646RecordSchedule(const BasicBlock* bb, const SchedulingManager& S)
647{
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000648 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
649 const MachineInstrInfo& mii = S.schedInfo.getInstrInfo();
650
651#ifndef NDEBUG
652 // Lets make sure we didn't lose any instructions, except possibly
653 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
654 unsigned numInstr = 0;
655 for (MachineCodeForBasicBlock::iterator I=mvec.begin(); I != mvec.end(); ++I)
656 if (! mii.isNop((*I)->getOpCode()) &&
657 ! mii.isDummyPhiInstr((*I)->getOpCode()))
658 ++numInstr;
659 assert(S.isched.getNumInstructions() >= numInstr &&
660 "Lost some non-NOP instructions during scheduling!");
661#endif
662
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000663 if (S.isched.getNumInstructions() == 0)
664 return; // empty basic block!
665
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000666 // First find the dummy instructions at the start of the basic block
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000667 MachineCodeForBasicBlock::iterator I = mvec.begin();
668 for ( ; I != mvec.end(); ++I)
669 if (! mii.isDummyPhiInstr((*I)->getOpCode()))
670 break;
671
672 // Erase all except the dummy PHI instructions from mvec, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000673 // pre-allocate create space for the ones we will put back in.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000674 mvec.erase(I, mvec.end());
675 mvec.reserve(mvec.size() + S.isched.getNumInstructions());
676
677 InstrSchedule::const_iterator NIend = S.isched.end();
678 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattner2e530932001-09-09 19:41:52 +0000679 mvec.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000680}
681
682
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000683
684static void
685MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
686{
687 // Check if any successors are now ready that were not already marked
688 // ready before, and that have not yet been scheduled.
689 //
690 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
691 if (! (*SI)->isDummyNode()
692 && ! S.isScheduled(*SI)
693 && ! S.schedPrio.nodeIsReady(*SI))
694 {// successor not scheduled and not marked ready; check *its* preds.
695
696 bool succIsReady = true;
697 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
698 if (! (*P)->isDummyNode()
699 && ! S.isScheduled(*P))
700 {
701 succIsReady = false;
702 break;
703 }
704
705 if (succIsReady) // add the successor to the ready list
706 S.schedPrio.insertReady(*SI);
707 }
708}
709
710
711// Choose up to `nslots' FEASIBLE instructions and assign each
712// instruction to all possible slots that do not violate feasibility.
713// FEASIBLE means it should be guaranteed that the set
714// of chosen instructions can be issued in a single group.
715//
716// Return value:
717// maxIssue : total number of feasible instructions
718// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
719//
720static unsigned
721FindSlotChoices(SchedulingManager& S,
722 DelaySlotInfo*& getDelaySlotInfo)
723{
724 // initialize result vectors to empty
725 S.resetChoices();
726
727 // find the slot to start from, in the current cycle
728 unsigned int startSlot = 0;
729 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
730 for (int s = S.nslots - 1; s >= 0; s--)
731 if ((*igroup)[s] != NULL)
732 {
733 startSlot = s+1;
734 break;
735 }
736
737 // Make sure we pick at most one instruction that would break the group.
738 // Also, if we do pick one, remember which it was.
739 unsigned int indexForBreakingNode = S.nslots;
740 unsigned int indexForDelayedInstr = S.nslots;
741 DelaySlotInfo* delaySlotInfo = NULL;
742
743 getDelaySlotInfo = NULL;
744
745 // Choose instructions in order of priority.
746 // Add choices to the choice vector in the SchedulingManager class as
747 // we choose them so that subsequent choices will be correctly tested
748 // for feasibility, w.r.t. higher priority choices for the same cycle.
749 //
750 while (S.getNumChoices() < S.nslots - startSlot)
751 {
752 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
753 if (nextNode == NULL)
754 break; // no more instructions for this cycle
755
Chris Lattner1ff63a12001-09-07 21:19:42 +0000756 if (S.getInstrInfo().getNumDelaySlots(nextNode->getMachineInstr()->getOpCode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000757 {
758 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
759 if (delaySlotInfo != NULL)
760 {
761 if (indexForBreakingNode < S.nslots)
762 // cannot issue a delayed instr in the same cycle as one
763 // that breaks the issue group or as another delayed instr
764 nextNode = NULL;
765 else
766 indexForDelayedInstr = S.getNumChoices();
767 }
768 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000769 else if (S.schedInfo.breaksIssueGroup(nextNode->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000770 {
771 if (indexForBreakingNode < S.nslots)
772 // have a breaking instruction already so throw this one away
773 nextNode = NULL;
774 else
775 indexForBreakingNode = S.getNumChoices();
776 }
777
778 if (nextNode != NULL)
779 S.addChoice(nextNode);
780
Chris Lattner1ff63a12001-09-07 21:19:42 +0000781 if (S.schedInfo.isSingleIssue(nextNode->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000782 {
783 assert(S.getNumChoices() == 1 &&
784 "Prioritizer returned invalid instr for this cycle!");
785 break;
786 }
787
788 if (indexForDelayedInstr < S.nslots)
789 break; // leave the rest for delay slots
790 }
791
792 assert(S.getNumChoices() <= S.nslots);
793 assert(! (indexForDelayedInstr < S.nslots &&
794 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
795
796 // Assign each chosen instruction to all possible slots for that instr.
797 // But if only one instruction was chosen, put it only in the first
798 // feasible slot; no more analysis will be needed.
799 //
800 if (indexForDelayedInstr >= S.nslots &&
801 indexForBreakingNode >= S.nslots)
802 { // No instructions that break the issue group or that have delay slots.
803 // This is the common case, so handle it separately for efficiency.
804
805 if (S.getNumChoices() == 1)
806 {
Chris Lattner1ff63a12001-09-07 21:19:42 +0000807 MachineOpCode opCode = S.getChoice(0)->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000808 unsigned int s;
809 for (s=startSlot; s < S.nslots; s++)
810 if (S.schedInfo.instrCanUseSlot(opCode, s))
811 break;
812 assert(s < S.nslots && "No feasible slot for this opCode?");
813 S.addChoiceToSlot(s, S.getChoice(0));
814 }
815 else
816 {
817 for (unsigned i=0; i < S.getNumChoices(); i++)
818 {
Chris Lattner1ff63a12001-09-07 21:19:42 +0000819 MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000820 for (unsigned int s=startSlot; s < S.nslots; s++)
821 if (S.schedInfo.instrCanUseSlot(opCode, s))
822 S.addChoiceToSlot(s, S.getChoice(i));
823 }
824 }
825 }
826 else if (indexForDelayedInstr < S.nslots)
827 {
828 // There is an instruction that needs delay slots.
829 // Try to assign that instruction to a higher slot than any other
830 // instructions in the group, so that its delay slots can go
831 // right after it.
832 //
833
834 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
835 "Instruction with delay slots should be last choice!");
836 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
837
838 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Chris Lattner1ff63a12001-09-07 21:19:42 +0000839 MachineOpCode delayOpCode = delayedNode->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000840 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
841
842 unsigned delayedNodeSlot = S.nslots;
843 int highestSlotUsed;
844
845 // Find the last possible slot for the delayed instruction that leaves
846 // at least `d' slots vacant after it (d = #delay slots)
847 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
848 if (S.schedInfo.instrCanUseSlot(delayOpCode, s))
849 {
850 delayedNodeSlot = s;
851 break;
852 }
853
854 highestSlotUsed = -1;
855 for (unsigned i=0; i < S.getNumChoices() - 1; i++)
856 {
857 // Try to assign every other instruction to a lower numbered
858 // slot than delayedNodeSlot.
Vikram S. Advec5b46322001-09-30 23:43:34 +0000859 MachineOpCode opCode =S.getChoice(i)->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000860 bool noSlotFound = true;
861 unsigned int s;
862 for (s=startSlot; s < delayedNodeSlot; s++)
863 if (S.schedInfo.instrCanUseSlot(opCode, s))
864 {
865 S.addChoiceToSlot(s, S.getChoice(i));
866 noSlotFound = false;
867 }
868
869 // No slot before `delayedNodeSlot' was found for this opCode
870 // Use a later slot, and allow some delay slots to fall in
871 // the next cycle.
872 if (noSlotFound)
873 for ( ; s < S.nslots; s++)
874 if (S.schedInfo.instrCanUseSlot(opCode, s))
875 {
876 S.addChoiceToSlot(s, S.getChoice(i));
877 break;
878 }
879
880 assert(s < S.nslots && "No feasible slot for instruction?");
881
882 highestSlotUsed = max(highestSlotUsed, (int) s);
883 }
884
885 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
886
887 // We will put the delayed node in the first slot after the
888 // highest slot used. But we just mark that for now, and
889 // schedule it separately because we want to schedule the delay
890 // slots for the node at the same time.
891 cycles_t dcycle = S.getTime();
892 unsigned int dslot = highestSlotUsed + 1;
893 if (dslot == S.nslots)
894 {
895 dslot = 0;
896 ++dcycle;
897 }
898 delaySlotInfo->recordChosenSlot(dcycle, dslot);
899 getDelaySlotInfo = delaySlotInfo;
900 }
901 else
902 { // There is an instruction that breaks the issue group.
903 // For such an instruction, assign to the last possible slot in
904 // the current group, and then don't assign any other instructions
905 // to later slots.
906 assert(indexForBreakingNode < S.nslots);
907 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
908 unsigned breakingSlot = INT_MAX;
909 unsigned int nslotsToUse = S.nslots;
910
911 // Find the last possible slot for this instruction.
912 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Chris Lattner1ff63a12001-09-07 21:19:42 +0000913 if (S.schedInfo.instrCanUseSlot(breakingNode->getMachineInstr()->getOpCode(), s))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000914 {
915 breakingSlot = s;
916 break;
917 }
918 assert(breakingSlot < S.nslots &&
919 "No feasible slot for `breakingNode'?");
920
921 // Higher priority instructions than the one that breaks the group:
922 // These can be assigned to all slots, but will be assigned only
923 // to earlier slots if possible.
924 for (unsigned i=0;
925 i < S.getNumChoices() && i < indexForBreakingNode; i++)
926 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000927 MachineOpCode opCode =S.getChoice(i)->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000928
929 // If a higher priority instruction cannot be assigned to
930 // any earlier slots, don't schedule the breaking instruction.
931 //
932 bool foundLowerSlot = false;
933 nslotsToUse = S.nslots; // May be modified in the loop
934 for (unsigned int s=startSlot; s < nslotsToUse; s++)
935 if (S.schedInfo.instrCanUseSlot(opCode, s))
936 {
937 if (breakingSlot < S.nslots && s < breakingSlot)
938 {
939 foundLowerSlot = true;
940 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
941 }
942
943 S.addChoiceToSlot(s, S.getChoice(i));
944 }
945
946 if (!foundLowerSlot)
947 breakingSlot = INT_MAX; // disable breaking instr
948 }
949
950 // Assign the breaking instruction (if any) to a single slot
951 // Otherwise, just ignore the instruction. It will simply be
952 // scheduled in a later cycle.
953 if (breakingSlot < S.nslots)
954 {
955 S.addChoiceToSlot(breakingSlot, breakingNode);
956 nslotsToUse = breakingSlot;
957 }
958 else
959 nslotsToUse = S.nslots;
960
961 // For lower priority instructions than the one that breaks the
962 // group, only assign them to slots lower than the breaking slot.
963 // Otherwise, just ignore the instruction.
964 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++)
965 {
966 bool foundLowerSlot = false;
Chris Lattner1ff63a12001-09-07 21:19:42 +0000967 MachineOpCode opCode = S.getChoice(i)->getMachineInstr()->getOpCode();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000968 for (unsigned int s=startSlot; s < nslotsToUse; s++)
969 if (S.schedInfo.instrCanUseSlot(opCode, s))
970 S.addChoiceToSlot(s, S.getChoice(i));
971 }
972 } // endif (no delay slots and no breaking slots)
973
974 return S.getNumChoices();
975}
976
977
Vikram S. Advec5b46322001-09-30 23:43:34 +0000978static unsigned
979ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000980{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000981 assert(S.schedPrio.getNumReady() > 0
982 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000983
Vikram S. Advec5b46322001-09-30 23:43:34 +0000984 cycles_t firstCycle = S.getTime();
985 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000986
Vikram S. Advec5b46322001-09-30 23:43:34 +0000987 // Choose up to `nslots' feasible instructions and their possible slots.
988 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000989
Vikram S. Advec5b46322001-09-30 23:43:34 +0000990 while (numIssued == 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000991 {
Vikram S. Advec5b46322001-09-30 23:43:34 +0000992 S.updateTime(S.getTime()+1);
993 numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000994 }
995
Vikram S. Advec5b46322001-09-30 23:43:34 +0000996 AssignInstructionsToSlots(S, numIssued);
997
998 if (getDelaySlotInfo != NULL)
999 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
1000
1001 // Print trace of scheduled instructions before newly ready ones
1002 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1003 {
1004 for (cycles_t c = firstCycle; c <= S.getTime(); c++)
1005 {
1006 cout << " Cycle " << c << " : Scheduled instructions:\n";
1007 const InstrGroup* igroup = S.isched.getIGroup(c);
1008 for (unsigned int s=0; s < S.nslots; s++)
1009 {
1010 cout << " ";
1011 if ((*igroup)[s] != NULL)
1012 cout << * ((*igroup)[s])->getMachineInstr() << endl;
1013 else
1014 cout << "<none>" << endl;
1015 }
1016 }
1017 }
1018
1019 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001020}
1021
1022
Vikram S. Advec5b46322001-09-30 23:43:34 +00001023static void
1024ForwardListSchedule(SchedulingManager& S)
1025{
1026 unsigned N;
1027 const SchedGraphNode* node;
1028
1029 S.schedPrio.initialize();
1030
1031 while ((N = S.schedPrio.getNumReady()) > 0)
1032 {
1033 cycles_t nextCycle = S.getTime();
1034
1035 // Choose one group of instructions for a cycle, plus any delay slot
1036 // instructions (which may overflow into successive cycles).
1037 // This will advance S.getTime() to the last cycle in which
1038 // instructions are actually issued.
1039 //
1040 unsigned numIssued = ChooseOneGroup(S);
1041 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
1042
1043 // Notify the priority manager of scheduled instructions and mark
1044 // any successors that may now be ready
1045 //
1046 for (cycles_t c = nextCycle; c <= S.getTime(); c++)
1047 {
1048 const InstrGroup* igroup = S.isched.getIGroup(c);
1049 for (unsigned int s=0; s < S.nslots; s++)
1050 if ((node = (*igroup)[s]) != NULL)
1051 {
1052 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
1053 MarkSuccessorsReady(S, node);
1054 }
1055 }
1056
1057 // Move to the next the next earliest cycle for which
1058 // an instruction can be issued, or the next earliest in which
1059 // one will be ready, or to the next cycle, whichever is latest.
1060 //
1061 S.updateTime(max(S.getTime() + 1,
1062 max(S.getEarliestIssueTime(),
1063 S.schedPrio.getEarliestReadyTime())));
1064 }
1065}
1066
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001067
1068//---------------------------------------------------------------------
1069// Code for filling delay slots for delayed terminator instructions
1070// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1071// instructions (e.g., CALL) are not handled here because they almost
1072// always can be filled with instructions from the call sequence code
1073// before a call. That's preferable because we incur many tradeoffs here
1074// when we cannot find single-cycle instructions that can be reordered.
1075//----------------------------------------------------------------------
1076
Vikram S. Advec5b46322001-09-30 23:43:34 +00001077static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001078NodeCanFillDelaySlot(const SchedulingManager& S,
1079 const SchedGraphNode* node,
1080 const SchedGraphNode* brNode,
1081 bool nodeIsPredecessor)
1082{
1083 assert(! node->isDummyNode());
1084
1085 // don't put a branch in the delay slot of another branch
Chris Lattner1ff63a12001-09-07 21:19:42 +00001086 if (S.getInstrInfo().isBranch(node->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001087 return false;
1088
1089 // don't put a single-issue instruction in the delay slot of a branch
Chris Lattner1ff63a12001-09-07 21:19:42 +00001090 if (S.schedInfo.isSingleIssue(node->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001091 return false;
1092
1093 // don't put a load-use dependence in the delay slot of a branch
1094 const MachineInstrInfo& mii = S.getInstrInfo();
1095
1096 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1097 EI != node->endInEdges(); ++EI)
1098 if (! (*EI)->getSrc()->isDummyNode()
Chris Lattner1ff63a12001-09-07 21:19:42 +00001099 && mii.isLoad((*EI)->getSrc()->getMachineInstr()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001100 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1101 return false;
1102
1103 // for now, don't put an instruction that does not have operand
1104 // interlocks in the delay slot of a branch
Chris Lattner1ff63a12001-09-07 21:19:42 +00001105 if (! S.getInstrInfo().hasOperandInterlock(node->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001106 return false;
1107
1108 // Finally, if the instruction preceeds the branch, we make sure the
1109 // instruction can be reordered relative to the branch. We simply check
1110 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1111 //
1112 if (nodeIsPredecessor)
1113 {
1114 bool onlyCDEdgeToBranch = true;
1115 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1116 OEI != node->endOutEdges(); ++OEI)
1117 if (! (*OEI)->getSink()->isDummyNode()
1118 && ((*OEI)->getSink() != brNode
1119 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1120 {
1121 onlyCDEdgeToBranch = false;
1122 break;
1123 }
1124
1125 if (!onlyCDEdgeToBranch)
1126 return false;
1127 }
1128
1129 return true;
1130}
1131
1132
Vikram S. Advec5b46322001-09-30 23:43:34 +00001133static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001134MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001135 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001136 SchedGraphNode* node,
1137 const SchedGraphNode* brNode,
1138 bool nodeIsPredecessor)
1139{
1140 if (nodeIsPredecessor)
1141 { // If node is in the same basic block (i.e., preceeds brNode),
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001142 // remove it and all its incident edges from the graph. Make sure we
1143 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1144 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001145 }
1146 else
1147 { // If the node was from a target block, add the node to the graph
1148 // and add a CD edge from brNode to node.
1149 assert(0 && "NOT IMPLEMENTED YET");
1150 }
1151
1152 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1153 dinfo->addDelayNode(node);
1154}
1155
1156
Vikram S. Advec5b46322001-09-30 23:43:34 +00001157void
1158FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1159 SchedGraphNode* brNode,
1160 vector<SchedGraphNode*>& sdelayNodeVec)
1161{
1162 const MachineInstrInfo& mii = S.getInstrInfo();
1163 unsigned ndelays =
1164 mii.getNumDelaySlots(brNode->getMachineInstr()->getOpCode());
1165
1166 if (ndelays == 0)
1167 return;
1168
1169 sdelayNodeVec.reserve(ndelays);
1170
1171 // Use a separate vector to hold the feasible multi-cycle nodes.
1172 // These will be used if not enough single-cycle nodes are found.
1173 //
1174 vector<SchedGraphNode*> mdelayNodeVec;
1175
1176 for (sg_pred_iterator P = pred_begin(brNode);
1177 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1178 if (! (*P)->isDummyNode() &&
1179 ! mii.isNop((*P)->getMachineInstr()->getOpCode()) &&
1180 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
1181 {
1182 if (mii.maxLatency((*P)->getMachineInstr()->getOpCode()) > 1)
1183 mdelayNodeVec.push_back(*P);
1184 else
1185 sdelayNodeVec.push_back(*P);
1186 }
1187
1188 // If not enough single-cycle instructions were found, select the
1189 // lowest-latency multi-cycle instructions and use them.
1190 // Note that this is the most efficient code when only 1 (or even 2)
1191 // values need to be selected.
1192 //
1193 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0)
1194 {
1195 unsigned lmin =
1196 mii.maxLatency(mdelayNodeVec[0]->getMachineInstr()->getOpCode());
1197 unsigned minIndex = 0;
1198 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
1199 {
1200 unsigned li =
1201 mii.maxLatency(mdelayNodeVec[i]->getMachineInstr()->getOpCode());
1202 if (lmin >= li)
1203 {
1204 lmin = li;
1205 minIndex = i;
1206 }
1207 }
1208 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1209 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1210 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1211 }
1212}
1213
1214
1215// Remove the NOPs currently in delay slots from the graph.
1216// Mark instructions specified in sdelayNodeVec to replace them.
1217// If not enough useful instructions were found, mark the NOPs to be used
1218// for filling delay slots, otherwise, otherwise just discard them.
1219//
1220void
1221ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1222 SchedGraphNode* node,
1223 vector<SchedGraphNode*> sdelayNodeVec,
1224 SchedGraph* graph)
1225{
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001226 vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Vikram S. Advec5b46322001-09-30 23:43:34 +00001227 const MachineInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001228 const MachineInstr* brInstr = node->getMachineInstr();
1229 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001230 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1231
1232 // Remove the NOPs currently in delay slots from the graph.
1233 // If not enough useful instructions were found, use the NOPs to
1234 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001235 //
1236 MachineCodeForVMInstr& termMvec = node->getInstr()->getMachineInstrVec();
1237 unsigned int firstDelaySlotIdx;
1238 for (unsigned i=0; i < termMvec.size(); ++i)
1239 if (termMvec[i] == brInstr)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001240 {
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001241 firstDelaySlotIdx = i+1;
1242 break;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001243 }
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001244 assert(firstDelaySlotIdx <= termMvec.size()-1 &&
1245 "This sucks! Where's that delay slot instruction?");
1246
1247 // First find all useful instructions already in the delay slots
1248 // and USE THEM. We'll throw away the unused alternatives below
1249 //
1250 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
1251 if (! mii.isNop(termMvec[i]->getOpCode()))
1252 sdelayNodeVec.insert(sdelayNodeVec.begin(),
1253 graph->getGraphNodeForInstr(termMvec[i]));
1254
1255 // Then find the NOPs and keep only as many as are needed.
1256 // Put the rest in nopNodeVec to be deleted.
1257 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i)
1258 if (mii.isNop(termMvec[i]->getOpCode()))
1259 if (sdelayNodeVec.size() < ndelays)
1260 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(termMvec[i]));
1261 else
1262 nopNodeVec.push_back(graph->getGraphNodeForInstr(termMvec[i]));
1263
1264 assert(sdelayNodeVec.size() >= ndelays);
1265
1266 // If some delay slots were already filled, throw away that many new choices
1267 if (sdelayNodeVec.size() > ndelays)
1268 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001269
1270 // Mark the nodes chosen for delay slots. This removes them from the graph.
1271 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1272 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1273
1274 // And remove the unused NOPs from the graph.
1275 for (unsigned i=0; i < nopNodeVec.size(); i++)
1276 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1277}
1278
1279
1280// For all delayed instructions, choose instructions to put in the delay
1281// slots and pull those out of the graph. Mark them for the delay slots
1282// in the DelaySlotInfo object for that graph node. If no useful work
1283// is found for a delay slot, use the NOP that is currently in that slot.
1284//
1285// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001286// EXCEPT CALLS AND RETURNS.
1287// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001288// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001289// suboptimal. Also, it complicates generating the calling sequence code in
1290// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001291//
1292static void
1293ChooseInstructionsForDelaySlots(SchedulingManager& S,
1294 const BasicBlock* bb,
1295 SchedGraph* graph)
1296{
1297 const MachineInstrInfo& mii = S.getInstrInfo();
1298 const TerminatorInst* termInstr = bb->getTerminator();
1299 MachineCodeForVMInstr& termMvec = termInstr->getMachineInstrVec();
1300 vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001301 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001302
1303 assert(termInstr->getOpcode() != Instruction::Call
1304 && "Call used as terminator?");
1305
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001306 if (termInstr->getOpcode() != Instruction::Ret)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001307 {
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001308 // To find instructions that need delay slots without searching the full
1309 // machine code, we assume that the only delayed instructions are CALLs
1310 // or instructions generated for the terminator inst.
1311 // Find the first branch instr in the sequence of machine instrs for term
1312 //
1313 unsigned first = 0;
1314 while (first < termMvec.size() &&
1315 ! mii.isBranch(termMvec[first]->getOpCode()))
1316 {
1317 ++first;
1318 }
1319 assert(first < termMvec.size() &&
1320 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1321
1322 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1323
1324 // Compute a vector of the nodes chosen for delay slots and then
1325 // mark delay slots to replace NOPs with these useful instructions.
1326 //
1327 if (brInstr != NULL)
1328 {
1329 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1330 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1331 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1332 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001333 }
1334
1335 // Also mark delay slots for other delayed instructions to hold NOPs.
1336 // Simply passing in an empty delayNodeVec will have this effect.
1337 //
1338 delayNodeVec.clear();
1339 const MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
1340 for (unsigned i=0; i < bbMvec.size(); i++)
1341 if (bbMvec[i] != brInstr &&
1342 mii.getNumDelaySlots(bbMvec[i]->getOpCode()) > 0)
1343 {
1344 SchedGraphNode* node = graph->getGraphNodeForInstr(bbMvec[i]);
1345 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1346 }
1347}
1348
1349
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001350//
1351// Schedule the delayed branch and its delay slots
1352//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001353unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001354DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1355{
1356 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1357 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1358 && "Slot for branch should be empty");
1359
1360 unsigned int nextSlot = delayedNodeSlotNum;
1361 cycles_t nextTime = delayedNodeCycle;
1362
1363 S.scheduleInstr(brNode, nextSlot, nextTime);
1364
1365 for (unsigned d=0; d < ndelays; d++)
1366 {
1367 ++nextSlot;
1368 if (nextSlot == S.nslots)
1369 {
1370 nextSlot = 0;
1371 nextTime++;
1372 }
1373
1374 // Find the first feasible instruction for this delay slot
1375 // Note that we only check for issue restrictions here.
1376 // We do *not* check for flow dependences but rely on pipeline
1377 // interlocks to resolve them. Machines without interlocks
1378 // will require this code to be modified.
1379 for (unsigned i=0; i < delayNodeVec.size(); i++)
1380 {
1381 const SchedGraphNode* dnode = delayNodeVec[i];
1382 if ( ! S.isScheduled(dnode)
Chris Lattner1ff63a12001-09-07 21:19:42 +00001383 && S.schedInfo.instrCanUseSlot(dnode->getMachineInstr()->getOpCode(), nextSlot)
1384 && instrIsFeasible(S, dnode->getMachineInstr()->getOpCode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001385 {
Chris Lattner1ff63a12001-09-07 21:19:42 +00001386 assert(S.getInstrInfo().hasOperandInterlock(dnode->getMachineInstr()->getOpCode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001387 && "Instructions without interlocks not yet supported "
1388 "when filling branch delay slots");
1389 S.scheduleInstr(dnode, nextSlot, nextTime);
1390 break;
1391 }
1392 }
1393 }
1394
1395 // Update current time if delay slots overflowed into later cycles.
1396 // Do this here because we know exactly which cycle is the last cycle
1397 // that contains delay slots. The next loop doesn't compute that.
1398 if (nextTime > S.getTime())
1399 S.updateTime(nextTime);
1400
1401 // Now put any remaining instructions in the unfilled delay slots.
1402 // This could lead to suboptimal performance but needed for correctness.
1403 nextSlot = delayedNodeSlotNum;
1404 nextTime = delayedNodeCycle;
1405 for (unsigned i=0; i < delayNodeVec.size(); i++)
1406 if (! S.isScheduled(delayNodeVec[i]))
1407 {
1408 do { // find the next empty slot
1409 ++nextSlot;
1410 if (nextSlot == S.nslots)
1411 {
1412 nextSlot = 0;
1413 nextTime++;
1414 }
1415 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
1416
1417 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1418 break;
1419 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001420
1421 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001422}
1423
Vikram S. Advec5b46322001-09-30 23:43:34 +00001424
1425// Check if the instruction would conflict with instructions already
1426// chosen for the current cycle
1427//
1428static inline bool
1429ConflictsWithChoices(const SchedulingManager& S,
1430 MachineOpCode opCode)
1431{
1432 // Check if the instruction must issue by itself, and some feasible
1433 // choices have already been made for this cycle
1434 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1435 return true;
1436
1437 // For each class that opCode belongs to, check if there are too many
1438 // instructions of that class.
1439 //
1440 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1441 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1442}
1443
1444
1445//************************* External Functions *****************************/
1446
1447
1448//---------------------------------------------------------------------------
1449// Function: ViolatesMinimumGap
1450//
1451// Purpose:
1452// Check minimum gap requirements relative to instructions scheduled in
1453// previous cycles.
1454// Note that we do not need to consider `nextEarliestIssueTime' here because
1455// that is also captured in the earliest start times for each opcode.
1456//---------------------------------------------------------------------------
1457
1458static inline bool
1459ViolatesMinimumGap(const SchedulingManager& S,
1460 MachineOpCode opCode,
1461 const cycles_t inCycle)
1462{
1463 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1464}
1465
1466
1467//---------------------------------------------------------------------------
1468// Function: instrIsFeasible
1469//
1470// Purpose:
1471// Check if any issue restrictions would prevent the instruction from
1472// being issued in the current cycle
1473//---------------------------------------------------------------------------
1474
1475bool
1476instrIsFeasible(const SchedulingManager& S,
1477 MachineOpCode opCode)
1478{
1479 // skip the instruction if it cannot be issued due to issue restrictions
1480 // caused by previously issued instructions
1481 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1482 return false;
1483
1484 // skip the instruction if it cannot be issued due to issue restrictions
1485 // caused by previously chosen instructions for the current cycle
1486 if (ConflictsWithChoices(S, opCode))
1487 return false;
1488
1489 return true;
1490}
1491
1492//---------------------------------------------------------------------------
1493// Function: ScheduleInstructionsWithSSA
1494//
1495// Purpose:
1496// Entry point for instruction scheduling on SSA form.
1497// Schedules the machine instructions generated by instruction selection.
1498// Assumes that register allocation has not been done, i.e., operands
1499// are still in SSA form.
1500//---------------------------------------------------------------------------
1501
1502bool
1503ScheduleInstructionsWithSSA(Method* method,
1504 const TargetMachine &target)
1505{
1506 SchedGraphSet graphSet(method, target);
1507
1508 if (SchedDebugLevel >= Sched_PrintSchedGraphs)
1509 {
1510 cout << endl << "*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING"
1511 << endl;
1512 graphSet.dump();
1513 }
1514
1515 for (SchedGraphSet::const_iterator GI=graphSet.begin();
1516 GI != graphSet.end(); ++GI)
1517 {
1518 SchedGraph* graph = (*GI).second;
1519 const vector<const BasicBlock*>& bbvec = graph->getBasicBlocks();
1520 assert(bbvec.size() == 1 && "Cannot schedule multiple basic blocks");
1521 const BasicBlock* bb = bbvec[0];
1522
1523 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1524 cout << endl << "*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
1525
1526 SchedPriorities schedPrio(method, graph); // expensive!
1527 SchedulingManager S(target, graph, schedPrio);
1528
1529 ChooseInstructionsForDelaySlots(S, bb, graph); // modifies graph
1530
1531 ForwardListSchedule(S); // computes schedule in S
1532
1533 RecordSchedule((*GI).first, S); // records schedule in BB
1534 }
1535
1536 if (SchedDebugLevel >= Sched_PrintMachineCode)
1537 {
1538 cout << endl
1539 << "*** Machine instructions after INSTRUCTION SCHEDULING" << endl;
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001540 method->getMachineCode().dump();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001541 }
1542
1543 return false; // no reason to fail yet
1544}
1545
1546