blob: 13b629c1b838416220d2e3f04fc249aeebc2df56 [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattneraf50d002002-04-09 05:45:58 +00009//
10// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
11// generic support routines for instruction scheduling.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +000014
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000015#include "SchedPriorities.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000016#include "llvm/BasicBlock.h"
Chris Lattner85015a02004-08-16 21:55:02 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/Target/TargetMachine.h"
20#include "../../Target/SparcV9/MachineCodeForInstruction.h"
21#include "../../Target/SparcV9/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000022#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000023#include <algorithm>
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Vikram S. Advec5b46322001-09-30 23:43:34 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Chris Lattner70e60cb2002-05-22 17:08:27 +000028SchedDebugLevel_t SchedDebugLevel;
Vikram S. Advec5b46322001-09-30 23:43:34 +000029
Vikram S. Advebed4eff2003-09-16 05:55:15 +000030static cl::opt<bool> EnableFillingDelaySlots("sched-fill-delay-slots",
31 cl::desc("Fill branch delay slots during local scheduling"));
32
Chris Lattner5ff62e92002-07-22 02:10:13 +000033static cl::opt<SchedDebugLevel_t, true>
34SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel),
35 cl::desc("enable instruction scheduling debugging information"),
36 cl::values(
37 clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000038 clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"),
39 clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"),
40 clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"),
Chris Lattner23f9ef02004-07-16 00:04:54 +000041 clEnumValEnd));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000042
43
Vikram S. Advec5b46322001-09-30 23:43:34 +000044//************************* Internal Data Types *****************************/
45
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000046class InstrSchedule;
47class SchedulingManager;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000048
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000049
50//----------------------------------------------------------------------
51// class InstrGroup:
52//
53// Represents a group of instructions scheduled to be issued
54// in a single cycle.
55//----------------------------------------------------------------------
56
Chris Lattnere3561c22003-08-15 05:20:06 +000057class InstrGroup {
58 InstrGroup(const InstrGroup&); // DO NOT IMPLEMENT
59 void operator=(const InstrGroup&); // DO NOT IMPLEMENT
60
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000061public:
62 inline const SchedGraphNode* operator[](unsigned int slotNum) const {
63 assert(slotNum < group.size());
64 return group[slotNum];
65 }
66
67private:
68 friend class InstrSchedule;
69
70 inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) {
71 assert(slotNum < group.size());
72 group[slotNum] = node;
73 }
74
75 /*ctor*/ InstrGroup(unsigned int nslots)
76 : group(nslots, NULL) {}
77
78 /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT
79
80private:
Misha Brukmanc2312df2003-05-22 21:24:35 +000081 std::vector<const SchedGraphNode*> group;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000082};
83
84
85//----------------------------------------------------------------------
86// class ScheduleIterator:
87//
88// Iterates over the machine instructions in the for a single basic block.
89// The schedule is represented by an InstrSchedule object.
90//----------------------------------------------------------------------
91
92template<class _NodeType>
Chris Lattnerd8bbc062002-07-25 18:04:48 +000093class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000094private:
95 unsigned cycleNum;
96 unsigned slotNum;
97 const InstrSchedule& S;
98public:
99 typedef ScheduleIterator<_NodeType> _Self;
100
101 /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule,
102 unsigned _cycleNum,
103 unsigned _slotNum)
104 : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) {
105 skipToNextInstr();
106 }
107
108 /*ctor*/ inline ScheduleIterator(const _Self& x)
109 : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {}
110
111 inline bool operator==(const _Self& x) const {
112 return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S);
113 }
114
115 inline bool operator!=(const _Self& x) const { return !operator==(x); }
116
Chris Lattner414d9d22003-11-05 06:25:06 +0000117 inline _NodeType* operator*() const;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000118 inline _NodeType* operator->() const { return operator*(); }
119
120 _Self& operator++(); // Preincrement
121 inline _Self operator++(int) { // Postincrement
122 _Self tmp(*this); ++*this; return tmp;
123 }
124
125 static _Self begin(const InstrSchedule& _schedule);
126 static _Self end( const InstrSchedule& _schedule);
127
128private:
129 inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT
130 void skipToNextInstr();
131};
132
133
134//----------------------------------------------------------------------
135// class InstrSchedule:
136//
137// Represents the schedule of machine instructions for a single basic block.
138//----------------------------------------------------------------------
139
Chris Lattnere3561c22003-08-15 05:20:06 +0000140class InstrSchedule {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000141 const unsigned int nslots;
142 unsigned int numInstr;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000143 std::vector<InstrGroup*> groups; // indexed by cycle number
144 std::vector<cycles_t> startTime; // indexed by node id
Chris Lattnere3561c22003-08-15 05:20:06 +0000145
146 InstrSchedule(InstrSchedule&); // DO NOT IMPLEMENT
147 void operator=(InstrSchedule&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000148
149public: // iterators
150 typedef ScheduleIterator<SchedGraphNode> iterator;
151 typedef ScheduleIterator<const SchedGraphNode> const_iterator;
152
Brian Gaekef738db02004-02-09 18:42:46 +0000153 iterator begin() { return iterator::begin(*this); }
154 const_iterator begin() const { return const_iterator::begin(*this); }
155 iterator end() { return iterator::end(*this); }
156 const_iterator end() const { return const_iterator::end(*this); }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000157
158public: // constructors and destructor
159 /*ctor*/ InstrSchedule (unsigned int _nslots,
160 unsigned int _numNodes);
161 /*dtor*/ ~InstrSchedule ();
162
163public: // accessor functions to query chosen schedule
164 const SchedGraphNode* getInstr (unsigned int slotNum,
165 cycles_t c) const {
166 const InstrGroup* igroup = this->getIGroup(c);
167 return (igroup == NULL)? NULL : (*igroup)[slotNum];
168 }
169
170 inline InstrGroup* getIGroup (cycles_t c) {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000171 if ((unsigned)c >= groups.size())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000172 groups.resize(c+1);
173 if (groups[c] == NULL)
174 groups[c] = new InstrGroup(nslots);
175 return groups[c];
176 }
177
178 inline const InstrGroup* getIGroup (cycles_t c) const {
Chris Lattnerdfb8b952002-02-24 23:01:50 +0000179 assert((unsigned)c < groups.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000180 return groups[c];
181 }
182
183 inline cycles_t getStartTime (unsigned int nodeId) const {
184 assert(nodeId < startTime.size());
185 return startTime[nodeId];
186 }
187
188 unsigned int getNumInstructions() const {
189 return numInstr;
190 }
191
192 inline void scheduleInstr (const SchedGraphNode* node,
193 unsigned int slotNum,
194 cycles_t cycle) {
195 InstrGroup* igroup = this->getIGroup(cycle);
Brian Gaeke365f54c2004-07-29 21:31:20 +0000196 if (!((*igroup)[slotNum] == NULL)) {
197 std::cerr << "Slot already filled?\n";
198 abort();
199 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000200 igroup->addInstr(node, slotNum);
201 assert(node->getNodeId() < startTime.size());
202 startTime[node->getNodeId()] = cycle;
203 ++numInstr;
204 }
205
206private:
Chris Lattner414d9d22003-11-05 06:25:06 +0000207 friend class ScheduleIterator<SchedGraphNode>;
208 friend class ScheduleIterator<const SchedGraphNode>;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000209 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
210};
211
Chris Lattner414d9d22003-11-05 06:25:06 +0000212template<class NodeType>
213inline NodeType *ScheduleIterator<NodeType>::operator*() const {
214 assert(cycleNum < S.groups.size());
215 return (*S.groups[cycleNum])[slotNum];
216}
217
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000218
219/*ctor*/
220InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
221 : nslots(_nslots),
222 numInstr(0),
223 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
224 startTime(_numNodes, (cycles_t) -1) // set all to -1
225{
226}
227
228
229/*dtor*/
230InstrSchedule::~InstrSchedule()
231{
232 for (unsigned c=0, NC=groups.size(); c < NC; c++)
233 if (groups[c] != NULL)
234 delete groups[c]; // delete InstrGroup objects
235}
236
237
238template<class _NodeType>
239inline
240void
241ScheduleIterator<_NodeType>::skipToNextInstr()
242{
243 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
244 ++cycleNum; // skip cycles with no instructions
245
246 while (cycleNum < S.groups.size() &&
247 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000248 {
249 ++slotNum;
250 if (slotNum == S.nslots) {
251 ++cycleNum;
252 slotNum = 0;
253 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
254 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000255 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000256 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000257}
258
259template<class _NodeType>
260inline
261ScheduleIterator<_NodeType>&
262ScheduleIterator<_NodeType>::operator++() // Preincrement
263{
264 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000265 if (slotNum == S.nslots) {
266 ++cycleNum;
267 slotNum = 0;
268 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000269 skipToNextInstr();
270 return *this;
271}
272
273template<class _NodeType>
274ScheduleIterator<_NodeType>
275ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
276{
277 return _Self(_schedule, 0, 0);
278}
279
280template<class _NodeType>
281ScheduleIterator<_NodeType>
282ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
283{
284 return _Self(_schedule, _schedule.groups.size(), 0);
285}
286
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000287
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
Chris Lattnere3561c22003-08-15 05:20:06 +0000295class DelaySlotInfo {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000296 const SchedGraphNode* brNode;
Chris Lattnere3561c22003-08-15 05:20:06 +0000297 unsigned ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000298 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000299 cycles_t delayedNodeCycle;
Chris Lattnere3561c22003-08-15 05:20:06 +0000300 unsigned delayedNodeSlotNum;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000301
Chris Lattnere3561c22003-08-15 05:20:06 +0000302 DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT
303 void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000304public:
305 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
306 unsigned _ndelays)
307 : brNode(_brNode), ndelays(_ndelays),
308 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
309
310 inline unsigned getNumDelays () {
311 return ndelays;
312 }
313
Misha Brukmanc2312df2003-05-22 21:24:35 +0000314 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000315 return delayNodeVec;
316 }
317
318 inline void addDelayNode (const SchedGraphNode* node) {
319 delayNodeVec.push_back(node);
320 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
321 }
322
323 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
324 delayedNodeCycle = cycle;
325 delayedNodeSlotNum = slotNum;
326 }
327
Vikram S. Advec5b46322001-09-30 23:43:34 +0000328 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000329};
330
331
332//----------------------------------------------------------------------
333// class SchedulingManager:
334//
335// Represents the schedule of machine instructions for a single basic block.
336//----------------------------------------------------------------------
337
Chris Lattnere3561c22003-08-15 05:20:06 +0000338class SchedulingManager {
339 SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT
340 void operator=(const SchedulingManager &); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000341public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000342 const unsigned nslots;
343 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000344 SchedPriorities& schedPrio;
345 InstrSchedule isched;
346
347private:
Chris Lattnere3561c22003-08-15 05:20:06 +0000348 unsigned totalInstrCount;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000349 cycles_t curTime;
350 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000351 // indexed by slot#
352 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
353 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
354 std::vector<int> numInClass; // indexed by sched class
355 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000356 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000357 // indexed by branch node ptr
358
359public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000360 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
361 SchedPriorities& schedPrio);
362 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000363 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000364 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
365 E = delaySlotInfoForBranches.end(); I != E; ++I)
366 delete I->second;
367 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000368
369 //----------------------------------------------------------------------
370 // Simplify access to the machine instruction info
371 //----------------------------------------------------------------------
372
Chris Lattner3501fea2003-01-14 22:00:31 +0000373 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000374 return schedInfo.getInstrInfo();
375 }
376
377 //----------------------------------------------------------------------
378 // Interface for checking and updating the current time
379 //----------------------------------------------------------------------
380
381 inline cycles_t getTime () const {
382 return curTime;
383 }
384
385 inline cycles_t getEarliestIssueTime() const {
386 return nextEarliestIssueTime;
387 }
388
389 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
390 assert(opCode < (int) nextEarliestStartTime.size());
391 return nextEarliestStartTime[opCode];
392 }
393
394 // Update current time to specified cycle
395 inline void updateTime (cycles_t c) {
396 curTime = c;
397 schedPrio.updateTime(c);
398 }
399
400 //----------------------------------------------------------------------
401 // Functions to manage the choices for the current cycle including:
402 // -- a vector of choices by priority (choiceVec)
403 // -- vectors of the choices for each instruction slot (choicesForSlot[])
404 // -- number of choices in each sched class, used to check issue conflicts
405 // between choices for a single cycle
406 //----------------------------------------------------------------------
407
408 inline unsigned int getNumChoices () const {
409 return choiceVec.size();
410 }
411
412 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000413 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000414 return numInClass[sc];
415 }
416
417 inline const SchedGraphNode* getChoice(unsigned int i) const {
418 // assert(i < choiceVec.size()); don't check here.
419 return choiceVec[i];
420 }
421
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000422 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000423 assert(slotNum < nslots);
424 return choicesForSlot[slotNum];
425 }
426
427 inline void addChoice (const SchedGraphNode* node) {
428 // Append the instruction to the vector of choices for current cycle.
429 // Increment numInClass[c] for the sched class to which the instr belongs.
430 choiceVec.push_back(node);
Brian Gaeke918cdd42004-02-12 01:34:05 +0000431 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpcode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000432 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000433 numInClass[sc]++;
434 }
435
436 inline void addChoiceToSlot (unsigned int slotNum,
437 const SchedGraphNode* node) {
438 // Add the instruction to the choice set for the specified slot
439 assert(slotNum < nslots);
440 choicesForSlot[slotNum].insert(node);
441 }
442
443 inline void resetChoices () {
444 choiceVec.clear();
445 for (unsigned int s=0; s < nslots; s++)
446 choicesForSlot[s].clear();
447 for (unsigned int c=0; c < numInClass.size(); c++)
448 numInClass[c] = 0;
449 }
450
451 //----------------------------------------------------------------------
452 // Code to query and manage the partial instruction schedule so far
453 //----------------------------------------------------------------------
454
455 inline unsigned int getNumScheduled () const {
456 return isched.getNumInstructions();
457 }
458
459 inline unsigned int getNumUnscheduled() const {
460 return totalInstrCount - isched.getNumInstructions();
461 }
462
463 inline bool isScheduled (const SchedGraphNode* node) const {
464 return (isched.getStartTime(node->getNodeId()) >= 0);
465 }
466
467 inline void scheduleInstr (const SchedGraphNode* node,
468 unsigned int slotNum,
469 cycles_t cycle)
470 {
471 assert(! isScheduled(node) && "Instruction already scheduled?");
472
473 // add the instruction to the schedule
474 isched.scheduleInstr(node, slotNum, cycle);
475
476 // update the earliest start times of all nodes that conflict with `node'
477 // and the next-earliest time anything can issue if `node' causes bubbles
478 updateEarliestStartTimes(node, cycle);
479
480 // remove the instruction from the choice sets for all slots
481 for (unsigned s=0; s < nslots; s++)
482 choicesForSlot[s].erase(node);
483
484 // and decrement the instr count for the sched class to which it belongs
Brian Gaeke918cdd42004-02-12 01:34:05 +0000485 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpcode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000486 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000487 numInClass[sc]--;
488 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000489
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000490 //----------------------------------------------------------------------
491 // Create and retrieve delay slot info for delayed instructions
492 //----------------------------------------------------------------------
493
494 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
495 bool createIfMissing=false)
496 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000497 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000498 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000499 if (I != delaySlotInfoForBranches.end())
500 return I->second;
501
502 if (!createIfMissing) return 0;
503
504 DelaySlotInfo *dinfo =
Brian Gaeke918cdd42004-02-12 01:34:05 +0000505 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpcode()));
Chris Lattneraf50d002002-04-09 05:45:58 +0000506 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000507 }
508
509private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000510 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
511 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000512};
513
514
515/*ctor*/
516SchedulingManager::SchedulingManager(const TargetMachine& target,
517 const SchedGraph* graph,
518 SchedPriorities& _schedPrio)
Chris Lattner98107ff2004-06-02 06:06:20 +0000519 : nslots(target.getSchedInfo()->getMaxNumIssueTotal()),
520 schedInfo(*target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000521 schedPrio(_schedPrio),
522 isched(nslots, graph->getNumNodes()),
523 totalInstrCount(graph->getNumNodes() - 2),
524 nextEarliestIssueTime(0),
525 choicesForSlot(nslots),
Chris Lattner98107ff2004-06-02 06:06:20 +0000526 numInClass(target.getSchedInfo()->getNumSchedClasses(), 0), // set all to 0
527 nextEarliestStartTime(target.getInstrInfo()->getNumOpcodes(),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000528 (cycles_t) 0) // set all to 0
529{
530 updateTime(0);
531
532 // Note that an upper bound on #choices for each slot is = nslots since
533 // we use this vector to hold a feasible set of instructions, and more
534 // would be infeasible. Reserve that much memory since it is probably small.
535 for (unsigned int i=0; i < nslots; i++)
536 choicesForSlot[i].resize(nslots);
537}
538
539
540void
541SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
542 cycles_t schedTime)
543{
Brian Gaeke918cdd42004-02-12 01:34:05 +0000544 if (schedInfo.numBubblesAfter(node->getOpcode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000545 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000546 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Brian Gaeke918cdd42004-02-12 01:34:05 +0000547 curTime + 1 + schedInfo.numBubblesAfter(node->getOpcode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000548 }
549
Vikram S. Adve1632e882002-10-13 00:40:37 +0000550 const std::vector<MachineOpCode>&
Brian Gaeke918cdd42004-02-12 01:34:05 +0000551 conflictVec = schedInfo.getConflictList(node->getOpcode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000552
Vikram S. Adve1632e882002-10-13 00:40:37 +0000553 for (unsigned i=0; i < conflictVec.size(); i++)
554 {
555 MachineOpCode toOp = conflictVec[i];
Brian Gaeke918cdd42004-02-12 01:34:05 +0000556 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpcode(),toOp);
Vikram S. Adve1632e882002-10-13 00:40:37 +0000557 assert(toOp < (int) nextEarliestStartTime.size());
558 if (nextEarliestStartTime[toOp] < est)
559 nextEarliestStartTime[toOp] = est;
560 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000561}
562
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000563//************************* Internal Functions *****************************/
564
565
566static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000567AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000568{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000569 // find the slot to start from, in the current cycle
570 unsigned int startSlot = 0;
571 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000572
Vikram S. Advec5b46322001-09-30 23:43:34 +0000573 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000574
Vikram S. Advec5b46322001-09-30 23:43:34 +0000575 // If only one instruction can be issued, do so.
576 if (maxIssue == 1)
577 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000578 if (S.getChoicesForSlot(s).size() > 0) {
579 // found the one instruction
580 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
581 return;
582 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000583
584 // Otherwise, choose from the choices for each slot
585 //
586 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
587 assert(igroup != NULL && "Group creation failed?");
588
589 // Find a slot that has only a single choice, and take it.
590 // If all slots have 0 or multiple choices, pick the first slot with
591 // choices and use its last instruction (just to avoid shifting the vector).
592 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000593 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
594 int chosenSlot = -1;
595 for (unsigned s=startSlot; s < S.nslots; s++)
596 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
597 chosenSlot = (int) s;
598 break;
599 }
600
601 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000602 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000603 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
604 chosenSlot = (int) s;
605 break;
606 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000607
Misha Brukman6b77ec42003-05-22 21:49:18 +0000608 if (chosenSlot != -1) {
609 // Insert the chosen instr in the chosen slot and
610 // erase it from all slots.
611 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
612 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000613 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000614 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000615
616 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000617}
618
619
620//
621// For now, just assume we are scheduling within a single basic block.
622// Get the machine instruction vector for the basic block and clear it,
623// then append instructions in scheduled order.
624// Also, re-insert the dummy PHI instructions that were at the beginning
625// of the basic block, since they are not part of the schedule.
626//
627static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000628RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000629{
Chris Lattner3501fea2003-01-14 22:00:31 +0000630 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000631
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000632 // Lets make sure we didn't lose any instructions, except possibly
633 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
634 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000635 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000636 if (! mii.isNop(I->getOpcode()) &&
637 ! mii.isDummyPhiInstr(I->getOpcode()))
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000638 ++numInstr;
639 assert(S.isched.getNumInstructions() >= numInstr &&
640 "Lost some non-NOP instructions during scheduling!");
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000641
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000642 if (S.isched.getNumInstructions() == 0)
643 return; // empty basic block!
644
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000645 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000646 MachineBasicBlock::iterator I = MBB.begin();
647 for ( ; I != MBB.end(); ++I)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000648 if (! mii.isDummyPhiInstr(I->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000649 break;
650
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000651 // Remove all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000652 // pre-allocate create space for the ones we will put back in.
Chris Lattnerb4186e02004-03-31 21:59:59 +0000653 while (I != MBB.end())
654 MBB.remove(I++);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000655
656 InstrSchedule::const_iterator NIend = S.isched.end();
657 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000658 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000659}
660
661
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000662
663static void
664MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
665{
666 // Check if any successors are now ready that were not already marked
667 // ready before, and that have not yet been scheduled.
668 //
669 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
670 if (! (*SI)->isDummyNode()
671 && ! S.isScheduled(*SI)
672 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000673 {
674 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000675
Misha Brukman6b77ec42003-05-22 21:49:18 +0000676 bool succIsReady = true;
677 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
678 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
679 succIsReady = false;
680 break;
681 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000682
Misha Brukman6b77ec42003-05-22 21:49:18 +0000683 if (succIsReady) // add the successor to the ready list
684 S.schedPrio.insertReady(*SI);
685 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000686}
687
688
689// Choose up to `nslots' FEASIBLE instructions and assign each
690// instruction to all possible slots that do not violate feasibility.
691// FEASIBLE means it should be guaranteed that the set
692// of chosen instructions can be issued in a single group.
693//
694// Return value:
695// maxIssue : total number of feasible instructions
696// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
697//
698static unsigned
699FindSlotChoices(SchedulingManager& S,
700 DelaySlotInfo*& getDelaySlotInfo)
701{
702 // initialize result vectors to empty
703 S.resetChoices();
704
705 // find the slot to start from, in the current cycle
706 unsigned int startSlot = 0;
707 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
708 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000709 if ((*igroup)[s] != NULL) {
710 startSlot = s+1;
711 break;
712 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000713
714 // Make sure we pick at most one instruction that would break the group.
715 // Also, if we do pick one, remember which it was.
716 unsigned int indexForBreakingNode = S.nslots;
717 unsigned int indexForDelayedInstr = S.nslots;
718 DelaySlotInfo* delaySlotInfo = NULL;
719
720 getDelaySlotInfo = NULL;
721
722 // Choose instructions in order of priority.
723 // Add choices to the choice vector in the SchedulingManager class as
724 // we choose them so that subsequent choices will be correctly tested
725 // for feasibility, w.r.t. higher priority choices for the same cycle.
726 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000727 while (S.getNumChoices() < S.nslots - startSlot) {
728 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
729 if (nextNode == NULL)
730 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000731
Brian Gaeke918cdd42004-02-12 01:34:05 +0000732 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpcode()) > 0) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000733 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
734 if (delaySlotInfo != NULL) {
735 if (indexForBreakingNode < S.nslots)
736 // cannot issue a delayed instr in the same cycle as one
737 // that breaks the issue group or as another delayed instr
738 nextNode = NULL;
739 else
740 indexForDelayedInstr = S.getNumChoices();
741 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000742 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000743 if (indexForBreakingNode < S.nslots)
744 // have a breaking instruction already so throw this one away
745 nextNode = NULL;
746 else
747 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000748 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000749
750 if (nextNode != NULL) {
751 S.addChoice(nextNode);
752
Brian Gaeke918cdd42004-02-12 01:34:05 +0000753 if (S.schedInfo.isSingleIssue(nextNode->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000754 assert(S.getNumChoices() == 1 &&
755 "Prioritizer returned invalid instr for this cycle!");
756 break;
757 }
758 }
759
760 if (indexForDelayedInstr < S.nslots)
761 break; // leave the rest for delay slots
762 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000763
764 assert(S.getNumChoices() <= S.nslots);
765 assert(! (indexForDelayedInstr < S.nslots &&
766 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
767
768 // Assign each chosen instruction to all possible slots for that instr.
769 // But if only one instruction was chosen, put it only in the first
770 // feasible slot; no more analysis will be needed.
771 //
772 if (indexForDelayedInstr >= S.nslots &&
773 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000774 { // No instructions that break the issue group or that have delay slots.
775 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000776
Misha Brukman6b77ec42003-05-22 21:49:18 +0000777 if (S.getNumChoices() == 1) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000778 MachineOpCode opCode = S.getChoice(0)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000779 unsigned int s;
780 for (s=startSlot; s < S.nslots; s++)
781 if (S.schedInfo.instrCanUseSlot(opCode, s))
782 break;
783 assert(s < S.nslots && "No feasible slot for this opCode?");
784 S.addChoiceToSlot(s, S.getChoice(0));
785 } else {
786 for (unsigned i=0; i < S.getNumChoices(); i++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000787 MachineOpCode opCode = S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000788 for (unsigned int s=startSlot; s < S.nslots; s++)
789 if (S.schedInfo.instrCanUseSlot(opCode, s))
790 S.addChoiceToSlot(s, S.getChoice(i));
791 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000792 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000793 } else if (indexForDelayedInstr < S.nslots) {
794 // There is an instruction that needs delay slots.
795 // Try to assign that instruction to a higher slot than any other
796 // instructions in the group, so that its delay slots can go
797 // right after it.
798 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000799
Misha Brukman6b77ec42003-05-22 21:49:18 +0000800 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
801 "Instruction with delay slots should be last choice!");
802 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000803
Misha Brukman6b77ec42003-05-22 21:49:18 +0000804 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Brian Gaeke918cdd42004-02-12 01:34:05 +0000805 MachineOpCode delayOpCode = delayedNode->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000806 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000807
Misha Brukman6b77ec42003-05-22 21:49:18 +0000808 unsigned delayedNodeSlot = S.nslots;
809 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000810
Misha Brukman6b77ec42003-05-22 21:49:18 +0000811 // Find the last possible slot for the delayed instruction that leaves
812 // at least `d' slots vacant after it (d = #delay slots)
813 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
814 if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) {
815 delayedNodeSlot = s;
816 break;
817 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000818
Misha Brukman6b77ec42003-05-22 21:49:18 +0000819 highestSlotUsed = -1;
820 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
821 // Try to assign every other instruction to a lower numbered
822 // slot than delayedNodeSlot.
Brian Gaeke918cdd42004-02-12 01:34:05 +0000823 MachineOpCode opCode =S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000824 bool noSlotFound = true;
825 unsigned int s;
826 for (s=startSlot; s < delayedNodeSlot; s++)
827 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
828 S.addChoiceToSlot(s, S.getChoice(i));
829 noSlotFound = false;
830 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000831
Misha Brukman6b77ec42003-05-22 21:49:18 +0000832 // No slot before `delayedNodeSlot' was found for this opCode
833 // Use a later slot, and allow some delay slots to fall in
834 // the next cycle.
835 if (noSlotFound)
836 for ( ; s < S.nslots; s++)
837 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
838 S.addChoiceToSlot(s, S.getChoice(i));
839 break;
840 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000841
Misha Brukman6b77ec42003-05-22 21:49:18 +0000842 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000843
Misha Brukman6b77ec42003-05-22 21:49:18 +0000844 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000845 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000846
Misha Brukman6b77ec42003-05-22 21:49:18 +0000847 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
848
849 // We will put the delayed node in the first slot after the
850 // highest slot used. But we just mark that for now, and
851 // schedule it separately because we want to schedule the delay
852 // slots for the node at the same time.
853 cycles_t dcycle = S.getTime();
854 unsigned int dslot = highestSlotUsed + 1;
855 if (dslot == S.nslots) {
856 dslot = 0;
857 ++dcycle;
858 }
859 delaySlotInfo->recordChosenSlot(dcycle, dslot);
860 getDelaySlotInfo = delaySlotInfo;
861 } else {
862 // There is an instruction that breaks the issue group.
863 // For such an instruction, assign to the last possible slot in
864 // the current group, and then don't assign any other instructions
865 // to later slots.
866 assert(indexForBreakingNode < S.nslots);
867 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
868 unsigned breakingSlot = INT_MAX;
869 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000870
Misha Brukman6b77ec42003-05-22 21:49:18 +0000871 // Find the last possible slot for this instruction.
872 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000873 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpcode(), s)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000874 breakingSlot = s;
875 break;
876 }
877 assert(breakingSlot < S.nslots &&
878 "No feasible slot for `breakingNode'?");
879
880 // Higher priority instructions than the one that breaks the group:
881 // These can be assigned to all slots, but will be assigned only
882 // to earlier slots if possible.
883 for (unsigned i=0;
884 i < S.getNumChoices() && i < indexForBreakingNode; i++)
885 {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000886 MachineOpCode opCode =S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000887
888 // If a higher priority instruction cannot be assigned to
889 // any earlier slots, don't schedule the breaking instruction.
890 //
891 bool foundLowerSlot = false;
892 nslotsToUse = S.nslots; // May be modified in the loop
893 for (unsigned int s=startSlot; s < nslotsToUse; s++)
894 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
895 if (breakingSlot < S.nslots && s < breakingSlot) {
896 foundLowerSlot = true;
897 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
898 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000899
Misha Brukman6b77ec42003-05-22 21:49:18 +0000900 S.addChoiceToSlot(s, S.getChoice(i));
901 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000902
Misha Brukman6b77ec42003-05-22 21:49:18 +0000903 if (!foundLowerSlot)
904 breakingSlot = INT_MAX; // disable breaking instr
905 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000906
Misha Brukman6b77ec42003-05-22 21:49:18 +0000907 // Assign the breaking instruction (if any) to a single slot
908 // Otherwise, just ignore the instruction. It will simply be
909 // scheduled in a later cycle.
910 if (breakingSlot < S.nslots) {
911 S.addChoiceToSlot(breakingSlot, breakingNode);
912 nslotsToUse = breakingSlot;
913 } else
914 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000915
Misha Brukman6b77ec42003-05-22 21:49:18 +0000916 // For lower priority instructions than the one that breaks the
917 // group, only assign them to slots lower than the breaking slot.
918 // Otherwise, just ignore the instruction.
919 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000920 MachineOpCode opCode = S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000921 for (unsigned int s=startSlot; s < nslotsToUse; s++)
922 if (S.schedInfo.instrCanUseSlot(opCode, s))
923 S.addChoiceToSlot(s, S.getChoice(i));
924 }
925 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000926
927 return S.getNumChoices();
928}
929
930
Vikram S. Advec5b46322001-09-30 23:43:34 +0000931static unsigned
932ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000933{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000934 assert(S.schedPrio.getNumReady() > 0
935 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000936
Vikram S. Advec5b46322001-09-30 23:43:34 +0000937 cycles_t firstCycle = S.getTime();
938 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000939
Vikram S. Advec5b46322001-09-30 23:43:34 +0000940 // Choose up to `nslots' feasible instructions and their possible slots.
941 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000942
Misha Brukman6b77ec42003-05-22 21:49:18 +0000943 while (numIssued == 0) {
944 S.updateTime(S.getTime()+1);
945 numIssued = FindSlotChoices(S, getDelaySlotInfo);
946 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000947
Vikram S. Advec5b46322001-09-30 23:43:34 +0000948 AssignInstructionsToSlots(S, numIssued);
949
950 if (getDelaySlotInfo != NULL)
951 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
952
953 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000954 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
955 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
956 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
957 const InstrGroup* igroup = S.isched.getIGroup(c);
958 for (unsigned int s=0; s < S.nslots; s++) {
959 std::cerr << " ";
960 if ((*igroup)[s] != NULL)
961 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
962 else
963 std::cerr << "<none>\n";
964 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000965 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000966 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000967
968 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000969}
970
971
Vikram S. Advec5b46322001-09-30 23:43:34 +0000972static void
973ForwardListSchedule(SchedulingManager& S)
974{
975 unsigned N;
976 const SchedGraphNode* node;
977
978 S.schedPrio.initialize();
979
Misha Brukman6b77ec42003-05-22 21:49:18 +0000980 while ((N = S.schedPrio.getNumReady()) > 0) {
981 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +0000982
Misha Brukman6b77ec42003-05-22 21:49:18 +0000983 // Choose one group of instructions for a cycle, plus any delay slot
984 // instructions (which may overflow into successive cycles).
985 // This will advance S.getTime() to the last cycle in which
986 // instructions are actually issued.
987 //
988 unsigned numIssued = ChooseOneGroup(S);
989 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +0000990
Misha Brukman6b77ec42003-05-22 21:49:18 +0000991 // Notify the priority manager of scheduled instructions and mark
992 // any successors that may now be ready
993 //
994 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
995 const InstrGroup* igroup = S.isched.getIGroup(c);
996 for (unsigned int s=0; s < S.nslots; s++)
997 if ((node = (*igroup)[s]) != NULL) {
998 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
999 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001000 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001001 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001002
1003 // Move to the next the next earliest cycle for which
1004 // an instruction can be issued, or the next earliest in which
1005 // one will be ready, or to the next cycle, whichever is latest.
1006 //
1007 S.updateTime(std::max(S.getTime() + 1,
1008 std::max(S.getEarliestIssueTime(),
1009 S.schedPrio.getEarliestReadyTime())));
1010 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001011}
1012
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001013
1014//---------------------------------------------------------------------
1015// Code for filling delay slots for delayed terminator instructions
1016// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1017// instructions (e.g., CALL) are not handled here because they almost
1018// always can be filled with instructions from the call sequence code
1019// before a call. That's preferable because we incur many tradeoffs here
1020// when we cannot find single-cycle instructions that can be reordered.
1021//----------------------------------------------------------------------
1022
Vikram S. Advec5b46322001-09-30 23:43:34 +00001023static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001024NodeCanFillDelaySlot(const SchedulingManager& S,
1025 const SchedGraphNode* node,
1026 const SchedGraphNode* brNode,
1027 bool nodeIsPredecessor)
1028{
1029 assert(! node->isDummyNode());
1030
1031 // don't put a branch in the delay slot of another branch
Brian Gaeke918cdd42004-02-12 01:34:05 +00001032 if (S.getInstrInfo().isBranch(node->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001033 return false;
1034
1035 // don't put a single-issue instruction in the delay slot of a branch
Brian Gaeke918cdd42004-02-12 01:34:05 +00001036 if (S.schedInfo.isSingleIssue(node->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001037 return false;
1038
1039 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001040 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001041
1042 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1043 EI != node->endInEdges(); ++EI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001044 if (! ((SchedGraphNode*)(*EI)->getSrc())->isDummyNode()
Brian Gaeke918cdd42004-02-12 01:34:05 +00001045 && mii.isLoad(((SchedGraphNode*)(*EI)->getSrc())->getOpcode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001046 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1047 return false;
1048
Misha Brukman6eba07a2003-09-17 21:34:23 +00001049 // Finally, if the instruction precedes the branch, we make sure the
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001050 // instruction can be reordered relative to the branch. We simply check
1051 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1052 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001053 if (nodeIsPredecessor) {
1054 bool onlyCDEdgeToBranch = true;
1055 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1056 OEI != node->endOutEdges(); ++OEI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001057 if (! ((SchedGraphNode*)(*OEI)->getSink())->isDummyNode()
Misha Brukman6b77ec42003-05-22 21:49:18 +00001058 && ((*OEI)->getSink() != brNode
1059 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1060 {
1061 onlyCDEdgeToBranch = false;
1062 break;
1063 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001064
Misha Brukman6b77ec42003-05-22 21:49:18 +00001065 if (!onlyCDEdgeToBranch)
1066 return false;
1067 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001068
1069 return true;
1070}
1071
1072
Vikram S. Advec5b46322001-09-30 23:43:34 +00001073static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001074MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001075 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001076 SchedGraphNode* node,
1077 const SchedGraphNode* brNode,
1078 bool nodeIsPredecessor)
1079{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001080 if (nodeIsPredecessor) {
Misha Brukman6eba07a2003-09-17 21:34:23 +00001081 // If node is in the same basic block (i.e., precedes brNode),
Misha Brukman6b77ec42003-05-22 21:49:18 +00001082 // remove it and all its incident edges from the graph. Make sure we
1083 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1084 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1085 } else {
1086 // If the node was from a target block, add the node to the graph
1087 // and add a CD edge from brNode to node.
1088 assert(0 && "NOT IMPLEMENTED YET");
1089 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001090
1091 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1092 dinfo->addDelayNode(node);
1093}
1094
1095
Vikram S. Advec5b46322001-09-30 23:43:34 +00001096void
1097FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1098 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001099 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001100{
Chris Lattner3501fea2003-01-14 22:00:31 +00001101 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001102 unsigned ndelays =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001103 mii.getNumDelaySlots(brNode->getOpcode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001104
1105 if (ndelays == 0)
1106 return;
1107
1108 sdelayNodeVec.reserve(ndelays);
1109
1110 // Use a separate vector to hold the feasible multi-cycle nodes.
1111 // These will be used if not enough single-cycle nodes are found.
1112 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001113 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001114
1115 for (sg_pred_iterator P = pred_begin(brNode);
1116 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1117 if (! (*P)->isDummyNode() &&
Brian Gaeke918cdd42004-02-12 01:34:05 +00001118 ! mii.isNop((*P)->getOpcode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001119 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001120 {
Brian Gaeke918cdd42004-02-12 01:34:05 +00001121 if (mii.maxLatency((*P)->getOpcode()) > 1)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001122 mdelayNodeVec.push_back(*P);
1123 else
1124 sdelayNodeVec.push_back(*P);
1125 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001126
1127 // If not enough single-cycle instructions were found, select the
1128 // lowest-latency multi-cycle instructions and use them.
1129 // Note that this is the most efficient code when only 1 (or even 2)
1130 // values need to be selected.
1131 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001132 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1133 unsigned lmin =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001134 mii.maxLatency(mdelayNodeVec[0]->getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +00001135 unsigned minIndex = 0;
1136 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001137 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001138 unsigned li =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001139 mii.maxLatency(mdelayNodeVec[i]->getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +00001140 if (lmin >= li)
1141 {
1142 lmin = li;
1143 minIndex = i;
1144 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001145 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001146 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1147 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1148 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1149 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001150}
1151
1152
1153// Remove the NOPs currently in delay slots from the graph.
1154// Mark instructions specified in sdelayNodeVec to replace them.
1155// If not enough useful instructions were found, mark the NOPs to be used
1156// for filling delay slots, otherwise, otherwise just discard them.
1157//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001158static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1159 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001160 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001161 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001162 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001163{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001164 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001165 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001166 const MachineInstr* brInstr = node->getMachineInstr();
Brian Gaeke918cdd42004-02-12 01:34:05 +00001167 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpcode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001168 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1169
1170 // Remove the NOPs currently in delay slots from the graph.
1171 // If not enough useful instructions were found, use the NOPs to
1172 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001173 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001174 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001175 MachineBasicBlock& MBB = node->getMachineBasicBlock();
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001176 MachineBasicBlock::iterator MBBI = MBB.begin();
1177 std::advance(MBBI, firstDelaySlotIdx - 1);
Brian Gaeke365f54c2004-07-29 21:31:20 +00001178 if (!(&*MBBI++ == brInstr)) {
1179 std::cerr << "Incorrect instr. index in basic block for brInstr";
1180 abort();
1181 }
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001182
1183 // First find all useful instructions already in the delay slots
1184 // and USE THEM. We'll throw away the unused alternatives below
1185 //
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001186 MachineBasicBlock::iterator Tmp = MBBI;
1187 for (unsigned i = 0; i != ndelays; ++i, ++MBBI)
1188 if (!mii.isNop(MBBI->getOpcode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001189 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001190 graph->getGraphNodeForInstr(MBBI));
1191 MBBI = Tmp;
1192
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001193 // Then find the NOPs and keep only as many as are needed.
1194 // Put the rest in nopNodeVec to be deleted.
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001195 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx+ndelays; ++i, ++MBBI)
1196 if (mii.isNop(MBBI->getOpcode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001197 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001198 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBBI));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001199 else {
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001200 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBBI));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001201
Misha Brukman6b77ec42003-05-22 21:49:18 +00001202 //remove the MI from the Machine Code For Instruction
Chris Lattner9cdaa632003-07-26 23:23:41 +00001203 const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Misha Brukman6b77ec42003-05-22 21:49:18 +00001204 MachineCodeForInstruction& llvmMvec =
Chris Lattner9cdaa632003-07-26 23:23:41 +00001205 MachineCodeForInstruction::get((const Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001206
Misha Brukman6b77ec42003-05-22 21:49:18 +00001207 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1208 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001209 if (*mciI == MBBI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001210 llvmMvec.erase(mciI);
1211 }
1212 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001213
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001214 assert(sdelayNodeVec.size() >= ndelays);
1215
1216 // If some delay slots were already filled, throw away that many new choices
1217 if (sdelayNodeVec.size() > ndelays)
1218 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001219
1220 // Mark the nodes chosen for delay slots. This removes them from the graph.
1221 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1222 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1223
1224 // And remove the unused NOPs from the graph.
1225 for (unsigned i=0; i < nopNodeVec.size(); i++)
1226 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1227}
1228
1229
1230// For all delayed instructions, choose instructions to put in the delay
1231// slots and pull those out of the graph. Mark them for the delay slots
1232// in the DelaySlotInfo object for that graph node. If no useful work
1233// is found for a delay slot, use the NOP that is currently in that slot.
1234//
1235// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001236// EXCEPT CALLS AND RETURNS.
1237// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001238// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001239// suboptimal. Also, it complicates generating the calling sequence code in
1240// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001241//
1242static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001243ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001244 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001245{
Chris Lattner3501fea2003-01-14 22:00:31 +00001246 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001247
1248 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001249 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001250 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001251 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001252
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001253 if (EnableFillingDelaySlots &&
1254 termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001255 {
1256 // To find instructions that need delay slots without searching the full
1257 // machine code, we assume that the only delayed instructions are CALLs
1258 // or instructions generated for the terminator inst.
1259 // Find the first branch instr in the sequence of machine instrs for term
1260 //
1261 unsigned first = 0;
1262 while (first < termMvec.size() &&
Brian Gaeke918cdd42004-02-12 01:34:05 +00001263 ! mii.isBranch(termMvec[first]->getOpcode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001264 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001265 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001266 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001267 assert(first < termMvec.size() &&
1268 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1269
1270 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1271
1272 // Compute a vector of the nodes chosen for delay slots and then
1273 // mark delay slots to replace NOPs with these useful instructions.
1274 //
1275 if (brInstr != NULL) {
1276 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1277 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1278 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1279 }
1280 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001281
1282 // Also mark delay slots for other delayed instructions to hold NOPs.
1283 // Simply passing in an empty delayNodeVec will have this effect.
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001284 // If brInstr is not handled above (EnableFillingDelaySlots == false),
1285 // brInstr will be NULL so this will handle the branch instrs. as well.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001286 //
1287 delayNodeVec.clear();
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001288 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
1289 if (I != brInstr && mii.getNumDelaySlots(I->getOpcode()) > 0) {
1290 SchedGraphNode* node = graph->getGraphNodeForInstr(I);
Misha Brukman6b77ec42003-05-22 21:49:18 +00001291 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1292 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001293}
1294
1295
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001296//
1297// Schedule the delayed branch and its delay slots
1298//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001299unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001300DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1301{
1302 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1303 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1304 && "Slot for branch should be empty");
1305
1306 unsigned int nextSlot = delayedNodeSlotNum;
1307 cycles_t nextTime = delayedNodeCycle;
1308
1309 S.scheduleInstr(brNode, nextSlot, nextTime);
1310
Misha Brukman6b77ec42003-05-22 21:49:18 +00001311 for (unsigned d=0; d < ndelays; d++) {
1312 ++nextSlot;
1313 if (nextSlot == S.nslots) {
1314 nextSlot = 0;
1315 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001316 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001317
1318 // Find the first feasible instruction for this delay slot
1319 // Note that we only check for issue restrictions here.
1320 // We do *not* check for flow dependences but rely on pipeline
1321 // interlocks to resolve them. Machines without interlocks
1322 // will require this code to be modified.
1323 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1324 const SchedGraphNode* dnode = delayNodeVec[i];
1325 if ( ! S.isScheduled(dnode)
Brian Gaeke918cdd42004-02-12 01:34:05 +00001326 && S.schedInfo.instrCanUseSlot(dnode->getOpcode(), nextSlot)
Brian Gaekeb2f30a32004-07-28 19:24:48 +00001327 && instrIsFeasible(S, dnode->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001328 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
Brian Gaekebf3c4cf2003-08-14 06:09:32 +00001494FunctionPass *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}
Brian Gaeked0fde302003-11-11 22:41:34 +00001497
1498} // End llvm namespace
1499