blob: b98de81820dc25dab329d1b81b19ec5b1888940a [file] [log] [blame]
Chris Lattneraf50d002002-04-09 05:45:58 +00001//===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattneraf50d002002-04-09 05:45:58 +00009//
10// This file implements the llvm/CodeGen/InstrScheduling.h interface, along with
11// generic support routines for instruction scheduling.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advec5b46322001-09-30 23:43:34 +000014
Chris Lattnerc6f3ae52002-04-29 17:42:12 +000015#include "SchedPriorities.h"
Vikram S. Adve0e1158f2001-08-28 23:07:19 +000016#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000018#include "llvm/CodeGen/MachineFunction.h"
Brian Gaeke367b91d2004-02-25 22:09:36 +000019#include "../../Target/SparcV9/LiveVar/FunctionLiveVarInfo.h"
Chris Lattner3462cae2002-02-03 07:28:30 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerf35f2fb2002-02-04 16:35:45 +000021#include "llvm/BasicBlock.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000022#include "Support/CommandLine.h"
Chris Lattner1ff63a12001-09-07 21:19:42 +000023#include <algorithm>
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"),
41 0));
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);
196 assert((*igroup)[slotNum] == NULL && "Slot already filled?");
197 igroup->addInstr(node, slotNum);
198 assert(node->getNodeId() < startTime.size());
199 startTime[node->getNodeId()] = cycle;
200 ++numInstr;
201 }
202
203private:
Chris Lattner414d9d22003-11-05 06:25:06 +0000204 friend class ScheduleIterator<SchedGraphNode>;
205 friend class ScheduleIterator<const SchedGraphNode>;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000206 /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT.
207};
208
Chris Lattner414d9d22003-11-05 06:25:06 +0000209template<class NodeType>
210inline NodeType *ScheduleIterator<NodeType>::operator*() const {
211 assert(cycleNum < S.groups.size());
212 return (*S.groups[cycleNum])[slotNum];
213}
214
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000215
216/*ctor*/
217InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes)
218 : nslots(_nslots),
219 numInstr(0),
220 groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles
221 startTime(_numNodes, (cycles_t) -1) // set all to -1
222{
223}
224
225
226/*dtor*/
227InstrSchedule::~InstrSchedule()
228{
229 for (unsigned c=0, NC=groups.size(); c < NC; c++)
230 if (groups[c] != NULL)
231 delete groups[c]; // delete InstrGroup objects
232}
233
234
235template<class _NodeType>
236inline
237void
238ScheduleIterator<_NodeType>::skipToNextInstr()
239{
240 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
241 ++cycleNum; // skip cycles with no instructions
242
243 while (cycleNum < S.groups.size() &&
244 (*S.groups[cycleNum])[slotNum] == NULL)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000245 {
246 ++slotNum;
247 if (slotNum == S.nslots) {
248 ++cycleNum;
249 slotNum = 0;
250 while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL)
251 ++cycleNum; // skip cycles with no instructions
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000252 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000253 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000254}
255
256template<class _NodeType>
257inline
258ScheduleIterator<_NodeType>&
259ScheduleIterator<_NodeType>::operator++() // Preincrement
260{
261 ++slotNum;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000262 if (slotNum == S.nslots) {
263 ++cycleNum;
264 slotNum = 0;
265 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000266 skipToNextInstr();
267 return *this;
268}
269
270template<class _NodeType>
271ScheduleIterator<_NodeType>
272ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule)
273{
274 return _Self(_schedule, 0, 0);
275}
276
277template<class _NodeType>
278ScheduleIterator<_NodeType>
279ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule)
280{
281 return _Self(_schedule, _schedule.groups.size(), 0);
282}
283
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000284
285//----------------------------------------------------------------------
286// class DelaySlotInfo:
287//
288// Record information about delay slots for a single branch instruction.
289// Delay slots are simply indexed by slot number 1 ... numDelaySlots
290//----------------------------------------------------------------------
291
Chris Lattnere3561c22003-08-15 05:20:06 +0000292class DelaySlotInfo {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000293 const SchedGraphNode* brNode;
Chris Lattnere3561c22003-08-15 05:20:06 +0000294 unsigned ndelays;
Misha Brukmanc2312df2003-05-22 21:24:35 +0000295 std::vector<const SchedGraphNode*> delayNodeVec;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000296 cycles_t delayedNodeCycle;
Chris Lattnere3561c22003-08-15 05:20:06 +0000297 unsigned delayedNodeSlotNum;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000298
Chris Lattnere3561c22003-08-15 05:20:06 +0000299 DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT
300 void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000301public:
302 /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode,
303 unsigned _ndelays)
304 : brNode(_brNode), ndelays(_ndelays),
305 delayedNodeCycle(0), delayedNodeSlotNum(0) {}
306
307 inline unsigned getNumDelays () {
308 return ndelays;
309 }
310
Misha Brukmanc2312df2003-05-22 21:24:35 +0000311 inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000312 return delayNodeVec;
313 }
314
315 inline void addDelayNode (const SchedGraphNode* node) {
316 delayNodeVec.push_back(node);
317 assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!");
318 }
319
320 inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) {
321 delayedNodeCycle = cycle;
322 delayedNodeSlotNum = slotNum;
323 }
324
Vikram S. Advec5b46322001-09-30 23:43:34 +0000325 unsigned scheduleDelayedNode (SchedulingManager& S);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000326};
327
328
329//----------------------------------------------------------------------
330// class SchedulingManager:
331//
332// Represents the schedule of machine instructions for a single basic block.
333//----------------------------------------------------------------------
334
Chris Lattnere3561c22003-08-15 05:20:06 +0000335class SchedulingManager {
336 SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT
337 void operator=(const SchedulingManager &); // DO NOT IMPLEMENT
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000338public: // publicly accessible data members
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000339 const unsigned nslots;
340 const TargetSchedInfo& schedInfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000341 SchedPriorities& schedPrio;
342 InstrSchedule isched;
343
344private:
Chris Lattnere3561c22003-08-15 05:20:06 +0000345 unsigned totalInstrCount;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000346 cycles_t curTime;
347 cycles_t nextEarliestIssueTime; // next cycle we can issue
Misha Brukmanc2312df2003-05-22 21:24:35 +0000348 // indexed by slot#
349 std::vector<hash_set<const SchedGraphNode*> > choicesForSlot;
350 std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr
351 std::vector<int> numInClass; // indexed by sched class
352 std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000353 hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000354 // indexed by branch node ptr
355
356public:
Chris Lattneraf50d002002-04-09 05:45:58 +0000357 SchedulingManager(const TargetMachine& _target, const SchedGraph* graph,
358 SchedPriorities& schedPrio);
359 ~SchedulingManager() {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000360 for (hash_map<const SchedGraphNode*,
Chris Lattneraf50d002002-04-09 05:45:58 +0000361 DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(),
362 E = delaySlotInfoForBranches.end(); I != E; ++I)
363 delete I->second;
364 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000365
366 //----------------------------------------------------------------------
367 // Simplify access to the machine instruction info
368 //----------------------------------------------------------------------
369
Chris Lattner3501fea2003-01-14 22:00:31 +0000370 inline const TargetInstrInfo& getInstrInfo () const {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000371 return schedInfo.getInstrInfo();
372 }
373
374 //----------------------------------------------------------------------
375 // Interface for checking and updating the current time
376 //----------------------------------------------------------------------
377
378 inline cycles_t getTime () const {
379 return curTime;
380 }
381
382 inline cycles_t getEarliestIssueTime() const {
383 return nextEarliestIssueTime;
384 }
385
386 inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const {
387 assert(opCode < (int) nextEarliestStartTime.size());
388 return nextEarliestStartTime[opCode];
389 }
390
391 // Update current time to specified cycle
392 inline void updateTime (cycles_t c) {
393 curTime = c;
394 schedPrio.updateTime(c);
395 }
396
397 //----------------------------------------------------------------------
398 // Functions to manage the choices for the current cycle including:
399 // -- a vector of choices by priority (choiceVec)
400 // -- vectors of the choices for each instruction slot (choicesForSlot[])
401 // -- number of choices in each sched class, used to check issue conflicts
402 // between choices for a single cycle
403 //----------------------------------------------------------------------
404
405 inline unsigned int getNumChoices () const {
406 return choiceVec.size();
407 }
408
409 inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const {
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000410 assert(sc < numInClass.size() && "Invalid op code or sched class!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000411 return numInClass[sc];
412 }
413
414 inline const SchedGraphNode* getChoice(unsigned int i) const {
415 // assert(i < choiceVec.size()); don't check here.
416 return choiceVec[i];
417 }
418
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000419 inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) {
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000420 assert(slotNum < nslots);
421 return choicesForSlot[slotNum];
422 }
423
424 inline void addChoice (const SchedGraphNode* node) {
425 // Append the instruction to the vector of choices for current cycle.
426 // Increment numInClass[c] for the sched class to which the instr belongs.
427 choiceVec.push_back(node);
Brian Gaeke918cdd42004-02-12 01:34:05 +0000428 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpcode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000429 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000430 numInClass[sc]++;
431 }
432
433 inline void addChoiceToSlot (unsigned int slotNum,
434 const SchedGraphNode* node) {
435 // Add the instruction to the choice set for the specified slot
436 assert(slotNum < nslots);
437 choicesForSlot[slotNum].insert(node);
438 }
439
440 inline void resetChoices () {
441 choiceVec.clear();
442 for (unsigned int s=0; s < nslots; s++)
443 choicesForSlot[s].clear();
444 for (unsigned int c=0; c < numInClass.size(); c++)
445 numInClass[c] = 0;
446 }
447
448 //----------------------------------------------------------------------
449 // Code to query and manage the partial instruction schedule so far
450 //----------------------------------------------------------------------
451
452 inline unsigned int getNumScheduled () const {
453 return isched.getNumInstructions();
454 }
455
456 inline unsigned int getNumUnscheduled() const {
457 return totalInstrCount - isched.getNumInstructions();
458 }
459
460 inline bool isScheduled (const SchedGraphNode* node) const {
461 return (isched.getStartTime(node->getNodeId()) >= 0);
462 }
463
464 inline void scheduleInstr (const SchedGraphNode* node,
465 unsigned int slotNum,
466 cycles_t cycle)
467 {
468 assert(! isScheduled(node) && "Instruction already scheduled?");
469
470 // add the instruction to the schedule
471 isched.scheduleInstr(node, slotNum, cycle);
472
473 // update the earliest start times of all nodes that conflict with `node'
474 // and the next-earliest time anything can issue if `node' causes bubbles
475 updateEarliestStartTimes(node, cycle);
476
477 // remove the instruction from the choice sets for all slots
478 for (unsigned s=0; s < nslots; s++)
479 choicesForSlot[s].erase(node);
480
481 // and decrement the instr count for the sched class to which it belongs
Brian Gaeke918cdd42004-02-12 01:34:05 +0000482 const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpcode());
Chris Lattnerc5ddc8b2002-10-28 04:53:02 +0000483 assert(sc < numInClass.size());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000484 numInClass[sc]--;
485 }
Chris Lattner1ff63a12001-09-07 21:19:42 +0000486
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000487 //----------------------------------------------------------------------
488 // Create and retrieve delay slot info for delayed instructions
489 //----------------------------------------------------------------------
490
491 inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn,
492 bool createIfMissing=false)
493 {
Chris Lattnerd8bbc062002-07-25 18:04:48 +0000494 hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000495 I = delaySlotInfoForBranches.find(bn);
Chris Lattneraf50d002002-04-09 05:45:58 +0000496 if (I != delaySlotInfoForBranches.end())
497 return I->second;
498
499 if (!createIfMissing) return 0;
500
501 DelaySlotInfo *dinfo =
Brian Gaeke918cdd42004-02-12 01:34:05 +0000502 new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpcode()));
Chris Lattneraf50d002002-04-09 05:45:58 +0000503 return delaySlotInfoForBranches[bn] = dinfo;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000504 }
505
506private:
Chris Lattneraf50d002002-04-09 05:45:58 +0000507 SchedulingManager(); // DISABLED: DO NOT IMPLEMENT
508 void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000509};
510
511
512/*ctor*/
513SchedulingManager::SchedulingManager(const TargetMachine& target,
514 const SchedGraph* graph,
515 SchedPriorities& _schedPrio)
Chris Lattner98107ff2004-06-02 06:06:20 +0000516 : nslots(target.getSchedInfo()->getMaxNumIssueTotal()),
517 schedInfo(*target.getSchedInfo()),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000518 schedPrio(_schedPrio),
519 isched(nslots, graph->getNumNodes()),
520 totalInstrCount(graph->getNumNodes() - 2),
521 nextEarliestIssueTime(0),
522 choicesForSlot(nslots),
Chris Lattner98107ff2004-06-02 06:06:20 +0000523 numInClass(target.getSchedInfo()->getNumSchedClasses(), 0), // set all to 0
524 nextEarliestStartTime(target.getInstrInfo()->getNumOpcodes(),
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000525 (cycles_t) 0) // set all to 0
526{
527 updateTime(0);
528
529 // Note that an upper bound on #choices for each slot is = nslots since
530 // we use this vector to hold a feasible set of instructions, and more
531 // would be infeasible. Reserve that much memory since it is probably small.
532 for (unsigned int i=0; i < nslots; i++)
533 choicesForSlot[i].resize(nslots);
534}
535
536
537void
538SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node,
539 cycles_t schedTime)
540{
Brian Gaeke918cdd42004-02-12 01:34:05 +0000541 if (schedInfo.numBubblesAfter(node->getOpcode()) > 0)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000542 { // Update next earliest time before which *nothing* can issue.
Chris Lattner697954c2002-01-20 22:54:45 +0000543 nextEarliestIssueTime = std::max(nextEarliestIssueTime,
Brian Gaeke918cdd42004-02-12 01:34:05 +0000544 curTime + 1 + schedInfo.numBubblesAfter(node->getOpcode()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000545 }
546
Vikram S. Adve1632e882002-10-13 00:40:37 +0000547 const std::vector<MachineOpCode>&
Brian Gaeke918cdd42004-02-12 01:34:05 +0000548 conflictVec = schedInfo.getConflictList(node->getOpcode());
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000549
Vikram S. Adve1632e882002-10-13 00:40:37 +0000550 for (unsigned i=0; i < conflictVec.size(); i++)
551 {
552 MachineOpCode toOp = conflictVec[i];
Brian Gaeke918cdd42004-02-12 01:34:05 +0000553 cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpcode(),toOp);
Vikram S. Adve1632e882002-10-13 00:40:37 +0000554 assert(toOp < (int) nextEarliestStartTime.size());
555 if (nextEarliestStartTime[toOp] < est)
556 nextEarliestStartTime[toOp] = est;
557 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000558}
559
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000560//************************* Internal Functions *****************************/
561
562
563static void
Vikram S. Advec5b46322001-09-30 23:43:34 +0000564AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000565{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000566 // find the slot to start from, in the current cycle
567 unsigned int startSlot = 0;
568 cycles_t curTime = S.getTime();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000569
Vikram S. Advec5b46322001-09-30 23:43:34 +0000570 assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000571
Vikram S. Advec5b46322001-09-30 23:43:34 +0000572 // If only one instruction can be issued, do so.
573 if (maxIssue == 1)
574 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000575 if (S.getChoicesForSlot(s).size() > 0) {
576 // found the one instruction
577 S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime);
578 return;
579 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000580
581 // Otherwise, choose from the choices for each slot
582 //
583 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
584 assert(igroup != NULL && "Group creation failed?");
585
586 // Find a slot that has only a single choice, and take it.
587 // If all slots have 0 or multiple choices, pick the first slot with
588 // choices and use its last instruction (just to avoid shifting the vector).
589 unsigned numIssued;
Misha Brukman6b77ec42003-05-22 21:49:18 +0000590 for (numIssued = 0; numIssued < maxIssue; numIssued++) {
591 int chosenSlot = -1;
592 for (unsigned s=startSlot; s < S.nslots; s++)
593 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) {
594 chosenSlot = (int) s;
595 break;
596 }
597
598 if (chosenSlot == -1)
Vikram S. Advec5b46322001-09-30 23:43:34 +0000599 for (unsigned s=startSlot; s < S.nslots; s++)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000600 if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) {
601 chosenSlot = (int) s;
602 break;
603 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000604
Misha Brukman6b77ec42003-05-22 21:49:18 +0000605 if (chosenSlot != -1) {
606 // Insert the chosen instr in the chosen slot and
607 // erase it from all slots.
608 const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin();
609 S.scheduleInstr(node, chosenSlot, curTime);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000610 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000611 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000612
613 assert(numIssued > 0 && "Should not happen when maxIssue > 0!");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000614}
615
616
617//
618// For now, just assume we are scheduling within a single basic block.
619// Get the machine instruction vector for the basic block and clear it,
620// then append instructions in scheduled order.
621// Also, re-insert the dummy PHI instructions that were at the beginning
622// of the basic block, since they are not part of the schedule.
623//
624static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000625RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000626{
Chris Lattner3501fea2003-01-14 22:00:31 +0000627 const TargetInstrInfo& mii = S.schedInfo.getInstrInfo();
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000628
629#ifndef NDEBUG
630 // Lets make sure we didn't lose any instructions, except possibly
631 // some NOPs from delay slots. Also, PHIs are not included in the schedule.
632 unsigned numInstr = 0;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000633 for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000634 if (! mii.isNop(I->getOpcode()) &&
635 ! mii.isDummyPhiInstr(I->getOpcode()))
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000636 ++numInstr;
637 assert(S.isched.getNumInstructions() >= numInstr &&
638 "Lost some non-NOP instructions during scheduling!");
639#endif
640
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000641 if (S.isched.getNumInstructions() == 0)
642 return; // empty basic block!
643
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000644 // First find the dummy instructions at the start of the basic block
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000645 MachineBasicBlock::iterator I = MBB.begin();
646 for ( ; I != MBB.end(); ++I)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000647 if (! mii.isDummyPhiInstr(I->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000648 break;
649
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000650 // Remove all except the dummy PHI instructions from MBB, and
Vikram S. Advef0ba2802001-09-18 12:51:38 +0000651 // pre-allocate create space for the ones we will put back in.
Chris Lattnerb4186e02004-03-31 21:59:59 +0000652 while (I != MBB.end())
653 MBB.remove(I++);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000654
655 InstrSchedule::const_iterator NIend = S.isched.end();
656 for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI)
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000657 MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr()));
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000658}
659
660
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000661
662static void
663MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node)
664{
665 // Check if any successors are now ready that were not already marked
666 // ready before, and that have not yet been scheduled.
667 //
668 for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI)
669 if (! (*SI)->isDummyNode()
670 && ! S.isScheduled(*SI)
671 && ! S.schedPrio.nodeIsReady(*SI))
Misha Brukman6b77ec42003-05-22 21:49:18 +0000672 {
673 // successor not scheduled and not marked ready; check *its* preds.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000674
Misha Brukman6b77ec42003-05-22 21:49:18 +0000675 bool succIsReady = true;
676 for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P)
677 if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) {
678 succIsReady = false;
679 break;
680 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000681
Misha Brukman6b77ec42003-05-22 21:49:18 +0000682 if (succIsReady) // add the successor to the ready list
683 S.schedPrio.insertReady(*SI);
684 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000685}
686
687
688// Choose up to `nslots' FEASIBLE instructions and assign each
689// instruction to all possible slots that do not violate feasibility.
690// FEASIBLE means it should be guaranteed that the set
691// of chosen instructions can be issued in a single group.
692//
693// Return value:
694// maxIssue : total number of feasible instructions
695// S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i
696//
697static unsigned
698FindSlotChoices(SchedulingManager& S,
699 DelaySlotInfo*& getDelaySlotInfo)
700{
701 // initialize result vectors to empty
702 S.resetChoices();
703
704 // find the slot to start from, in the current cycle
705 unsigned int startSlot = 0;
706 InstrGroup* igroup = S.isched.getIGroup(S.getTime());
707 for (int s = S.nslots - 1; s >= 0; s--)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000708 if ((*igroup)[s] != NULL) {
709 startSlot = s+1;
710 break;
711 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000712
713 // Make sure we pick at most one instruction that would break the group.
714 // Also, if we do pick one, remember which it was.
715 unsigned int indexForBreakingNode = S.nslots;
716 unsigned int indexForDelayedInstr = S.nslots;
717 DelaySlotInfo* delaySlotInfo = NULL;
718
719 getDelaySlotInfo = NULL;
720
721 // Choose instructions in order of priority.
722 // Add choices to the choice vector in the SchedulingManager class as
723 // we choose them so that subsequent choices will be correctly tested
724 // for feasibility, w.r.t. higher priority choices for the same cycle.
725 //
Misha Brukman6b77ec42003-05-22 21:49:18 +0000726 while (S.getNumChoices() < S.nslots - startSlot) {
727 const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime());
728 if (nextNode == NULL)
729 break; // no more instructions for this cycle
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000730
Brian Gaeke918cdd42004-02-12 01:34:05 +0000731 if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpcode()) > 0) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000732 delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode);
733 if (delaySlotInfo != NULL) {
734 if (indexForBreakingNode < S.nslots)
735 // cannot issue a delayed instr in the same cycle as one
736 // that breaks the issue group or as another delayed instr
737 nextNode = NULL;
738 else
739 indexForDelayedInstr = S.getNumChoices();
740 }
Brian Gaeke918cdd42004-02-12 01:34:05 +0000741 } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000742 if (indexForBreakingNode < S.nslots)
743 // have a breaking instruction already so throw this one away
744 nextNode = NULL;
745 else
746 indexForBreakingNode = S.getNumChoices();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000747 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000748
749 if (nextNode != NULL) {
750 S.addChoice(nextNode);
751
Brian Gaeke918cdd42004-02-12 01:34:05 +0000752 if (S.schedInfo.isSingleIssue(nextNode->getOpcode())) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000753 assert(S.getNumChoices() == 1 &&
754 "Prioritizer returned invalid instr for this cycle!");
755 break;
756 }
757 }
758
759 if (indexForDelayedInstr < S.nslots)
760 break; // leave the rest for delay slots
761 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000762
763 assert(S.getNumChoices() <= S.nslots);
764 assert(! (indexForDelayedInstr < S.nslots &&
765 indexForBreakingNode < S.nslots) && "Cannot have both in a cycle");
766
767 // Assign each chosen instruction to all possible slots for that instr.
768 // But if only one instruction was chosen, put it only in the first
769 // feasible slot; no more analysis will be needed.
770 //
771 if (indexForDelayedInstr >= S.nslots &&
772 indexForBreakingNode >= S.nslots)
Misha Brukman6b77ec42003-05-22 21:49:18 +0000773 { // No instructions that break the issue group or that have delay slots.
774 // This is the common case, so handle it separately for efficiency.
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000775
Misha Brukman6b77ec42003-05-22 21:49:18 +0000776 if (S.getNumChoices() == 1) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000777 MachineOpCode opCode = S.getChoice(0)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000778 unsigned int s;
779 for (s=startSlot; s < S.nslots; s++)
780 if (S.schedInfo.instrCanUseSlot(opCode, s))
781 break;
782 assert(s < S.nslots && "No feasible slot for this opCode?");
783 S.addChoiceToSlot(s, S.getChoice(0));
784 } else {
785 for (unsigned i=0; i < S.getNumChoices(); i++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000786 MachineOpCode opCode = S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000787 for (unsigned int s=startSlot; s < S.nslots; s++)
788 if (S.schedInfo.instrCanUseSlot(opCode, s))
789 S.addChoiceToSlot(s, S.getChoice(i));
790 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000791 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000792 } else if (indexForDelayedInstr < S.nslots) {
793 // There is an instruction that needs delay slots.
794 // Try to assign that instruction to a higher slot than any other
795 // instructions in the group, so that its delay slots can go
796 // right after it.
797 //
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000798
Misha Brukman6b77ec42003-05-22 21:49:18 +0000799 assert(indexForDelayedInstr == S.getNumChoices() - 1 &&
800 "Instruction with delay slots should be last choice!");
801 assert(delaySlotInfo != NULL && "No delay slot info for instr?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000802
Misha Brukman6b77ec42003-05-22 21:49:18 +0000803 const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr);
Brian Gaeke918cdd42004-02-12 01:34:05 +0000804 MachineOpCode delayOpCode = delayedNode->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000805 unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000806
Misha Brukman6b77ec42003-05-22 21:49:18 +0000807 unsigned delayedNodeSlot = S.nslots;
808 int highestSlotUsed;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000809
Misha Brukman6b77ec42003-05-22 21:49:18 +0000810 // Find the last possible slot for the delayed instruction that leaves
811 // at least `d' slots vacant after it (d = #delay slots)
812 for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--)
813 if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) {
814 delayedNodeSlot = s;
815 break;
816 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000817
Misha Brukman6b77ec42003-05-22 21:49:18 +0000818 highestSlotUsed = -1;
819 for (unsigned i=0; i < S.getNumChoices() - 1; i++) {
820 // Try to assign every other instruction to a lower numbered
821 // slot than delayedNodeSlot.
Brian Gaeke918cdd42004-02-12 01:34:05 +0000822 MachineOpCode opCode =S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000823 bool noSlotFound = true;
824 unsigned int s;
825 for (s=startSlot; s < delayedNodeSlot; s++)
826 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
827 S.addChoiceToSlot(s, S.getChoice(i));
828 noSlotFound = false;
829 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000830
Misha Brukman6b77ec42003-05-22 21:49:18 +0000831 // No slot before `delayedNodeSlot' was found for this opCode
832 // Use a later slot, and allow some delay slots to fall in
833 // the next cycle.
834 if (noSlotFound)
835 for ( ; s < S.nslots; s++)
836 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
837 S.addChoiceToSlot(s, S.getChoice(i));
838 break;
839 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000840
Misha Brukman6b77ec42003-05-22 21:49:18 +0000841 assert(s < S.nslots && "No feasible slot for instruction?");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000842
Misha Brukman6b77ec42003-05-22 21:49:18 +0000843 highestSlotUsed = std::max(highestSlotUsed, (int) s);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000844 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000845
Misha Brukman6b77ec42003-05-22 21:49:18 +0000846 assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?");
847
848 // We will put the delayed node in the first slot after the
849 // highest slot used. But we just mark that for now, and
850 // schedule it separately because we want to schedule the delay
851 // slots for the node at the same time.
852 cycles_t dcycle = S.getTime();
853 unsigned int dslot = highestSlotUsed + 1;
854 if (dslot == S.nslots) {
855 dslot = 0;
856 ++dcycle;
857 }
858 delaySlotInfo->recordChosenSlot(dcycle, dslot);
859 getDelaySlotInfo = delaySlotInfo;
860 } else {
861 // There is an instruction that breaks the issue group.
862 // For such an instruction, assign to the last possible slot in
863 // the current group, and then don't assign any other instructions
864 // to later slots.
865 assert(indexForBreakingNode < S.nslots);
866 const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode);
867 unsigned breakingSlot = INT_MAX;
868 unsigned int nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000869
Misha Brukman6b77ec42003-05-22 21:49:18 +0000870 // Find the last possible slot for this instruction.
871 for (int s = S.nslots-1; s >= (int) startSlot; s--)
Brian Gaeke918cdd42004-02-12 01:34:05 +0000872 if (S.schedInfo.instrCanUseSlot(breakingNode->getOpcode(), s)) {
Misha Brukman6b77ec42003-05-22 21:49:18 +0000873 breakingSlot = s;
874 break;
875 }
876 assert(breakingSlot < S.nslots &&
877 "No feasible slot for `breakingNode'?");
878
879 // Higher priority instructions than the one that breaks the group:
880 // These can be assigned to all slots, but will be assigned only
881 // to earlier slots if possible.
882 for (unsigned i=0;
883 i < S.getNumChoices() && i < indexForBreakingNode; i++)
884 {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000885 MachineOpCode opCode =S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000886
887 // If a higher priority instruction cannot be assigned to
888 // any earlier slots, don't schedule the breaking instruction.
889 //
890 bool foundLowerSlot = false;
891 nslotsToUse = S.nslots; // May be modified in the loop
892 for (unsigned int s=startSlot; s < nslotsToUse; s++)
893 if (S.schedInfo.instrCanUseSlot(opCode, s)) {
894 if (breakingSlot < S.nslots && s < breakingSlot) {
895 foundLowerSlot = true;
896 nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND!
897 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000898
Misha Brukman6b77ec42003-05-22 21:49:18 +0000899 S.addChoiceToSlot(s, S.getChoice(i));
900 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000901
Misha Brukman6b77ec42003-05-22 21:49:18 +0000902 if (!foundLowerSlot)
903 breakingSlot = INT_MAX; // disable breaking instr
904 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000905
Misha Brukman6b77ec42003-05-22 21:49:18 +0000906 // Assign the breaking instruction (if any) to a single slot
907 // Otherwise, just ignore the instruction. It will simply be
908 // scheduled in a later cycle.
909 if (breakingSlot < S.nslots) {
910 S.addChoiceToSlot(breakingSlot, breakingNode);
911 nslotsToUse = breakingSlot;
912 } else
913 nslotsToUse = S.nslots;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000914
Misha Brukman6b77ec42003-05-22 21:49:18 +0000915 // For lower priority instructions than the one that breaks the
916 // group, only assign them to slots lower than the breaking slot.
917 // Otherwise, just ignore the instruction.
918 for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) {
Brian Gaeke918cdd42004-02-12 01:34:05 +0000919 MachineOpCode opCode = S.getChoice(i)->getOpcode();
Misha Brukman6b77ec42003-05-22 21:49:18 +0000920 for (unsigned int s=startSlot; s < nslotsToUse; s++)
921 if (S.schedInfo.instrCanUseSlot(opCode, s))
922 S.addChoiceToSlot(s, S.getChoice(i));
923 }
924 } // endif (no delay slots and no breaking slots)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000925
926 return S.getNumChoices();
927}
928
929
Vikram S. Advec5b46322001-09-30 23:43:34 +0000930static unsigned
931ChooseOneGroup(SchedulingManager& S)
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000932{
Vikram S. Advec5b46322001-09-30 23:43:34 +0000933 assert(S.schedPrio.getNumReady() > 0
934 && "Don't get here without ready instructions.");
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000935
Vikram S. Advec5b46322001-09-30 23:43:34 +0000936 cycles_t firstCycle = S.getTime();
937 DelaySlotInfo* getDelaySlotInfo = NULL;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000938
Vikram S. Advec5b46322001-09-30 23:43:34 +0000939 // Choose up to `nslots' feasible instructions and their possible slots.
940 unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000941
Misha Brukman6b77ec42003-05-22 21:49:18 +0000942 while (numIssued == 0) {
943 S.updateTime(S.getTime()+1);
944 numIssued = FindSlotChoices(S, getDelaySlotInfo);
945 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000946
Vikram S. Advec5b46322001-09-30 23:43:34 +0000947 AssignInstructionsToSlots(S, numIssued);
948
949 if (getDelaySlotInfo != NULL)
950 numIssued += getDelaySlotInfo->scheduleDelayedNode(S);
951
952 // Print trace of scheduled instructions before newly ready ones
Misha Brukman6b77ec42003-05-22 21:49:18 +0000953 if (SchedDebugLevel >= Sched_PrintSchedTrace) {
954 for (cycles_t c = firstCycle; c <= S.getTime(); c++) {
955 std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n";
956 const InstrGroup* igroup = S.isched.getIGroup(c);
957 for (unsigned int s=0; s < S.nslots; s++) {
958 std::cerr << " ";
959 if ((*igroup)[s] != NULL)
960 std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n";
961 else
962 std::cerr << "<none>\n";
963 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000964 }
Misha Brukman6b77ec42003-05-22 21:49:18 +0000965 }
Vikram S. Advec5b46322001-09-30 23:43:34 +0000966
967 return numIssued;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +0000968}
969
970
Vikram S. Advec5b46322001-09-30 23:43:34 +0000971static void
972ForwardListSchedule(SchedulingManager& S)
973{
974 unsigned N;
975 const SchedGraphNode* node;
976
977 S.schedPrio.initialize();
978
Misha Brukman6b77ec42003-05-22 21:49:18 +0000979 while ((N = S.schedPrio.getNumReady()) > 0) {
980 cycles_t nextCycle = S.getTime();
Vikram S. Advec5b46322001-09-30 23:43:34 +0000981
Misha Brukman6b77ec42003-05-22 21:49:18 +0000982 // Choose one group of instructions for a cycle, plus any delay slot
983 // instructions (which may overflow into successive cycles).
984 // This will advance S.getTime() to the last cycle in which
985 // instructions are actually issued.
986 //
987 unsigned numIssued = ChooseOneGroup(S);
988 assert(numIssued > 0 && "Deadlock in list scheduling algorithm?");
Vikram S. Advec5b46322001-09-30 23:43:34 +0000989
Misha Brukman6b77ec42003-05-22 21:49:18 +0000990 // Notify the priority manager of scheduled instructions and mark
991 // any successors that may now be ready
992 //
993 for (cycles_t c = nextCycle; c <= S.getTime(); c++) {
994 const InstrGroup* igroup = S.isched.getIGroup(c);
995 for (unsigned int s=0; s < S.nslots; s++)
996 if ((node = (*igroup)[s]) != NULL) {
997 S.schedPrio.issuedReadyNodeAt(S.getTime(), node);
998 MarkSuccessorsReady(S, node);
Vikram S. Advec5b46322001-09-30 23:43:34 +0000999 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001000 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001001
1002 // Move to the next the next earliest cycle for which
1003 // an instruction can be issued, or the next earliest in which
1004 // one will be ready, or to the next cycle, whichever is latest.
1005 //
1006 S.updateTime(std::max(S.getTime() + 1,
1007 std::max(S.getEarliestIssueTime(),
1008 S.schedPrio.getEarliestReadyTime())));
1009 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001010}
1011
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001012
1013//---------------------------------------------------------------------
1014// Code for filling delay slots for delayed terminator instructions
1015// (e.g., BRANCH and RETURN). Delay slots for non-terminator
1016// instructions (e.g., CALL) are not handled here because they almost
1017// always can be filled with instructions from the call sequence code
1018// before a call. That's preferable because we incur many tradeoffs here
1019// when we cannot find single-cycle instructions that can be reordered.
1020//----------------------------------------------------------------------
1021
Vikram S. Advec5b46322001-09-30 23:43:34 +00001022static bool
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001023NodeCanFillDelaySlot(const SchedulingManager& S,
1024 const SchedGraphNode* node,
1025 const SchedGraphNode* brNode,
1026 bool nodeIsPredecessor)
1027{
1028 assert(! node->isDummyNode());
1029
1030 // don't put a branch in the delay slot of another branch
Brian Gaeke918cdd42004-02-12 01:34:05 +00001031 if (S.getInstrInfo().isBranch(node->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001032 return false;
1033
1034 // don't put a single-issue instruction in the delay slot of a branch
Brian Gaeke918cdd42004-02-12 01:34:05 +00001035 if (S.schedInfo.isSingleIssue(node->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001036 return false;
1037
1038 // don't put a load-use dependence in the delay slot of a branch
Chris Lattner3501fea2003-01-14 22:00:31 +00001039 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001040
1041 for (SchedGraphNode::const_iterator EI = node->beginInEdges();
1042 EI != node->endInEdges(); ++EI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001043 if (! ((SchedGraphNode*)(*EI)->getSrc())->isDummyNode()
Brian Gaeke918cdd42004-02-12 01:34:05 +00001044 && mii.isLoad(((SchedGraphNode*)(*EI)->getSrc())->getOpcode())
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001045 && (*EI)->getDepType() == SchedGraphEdge::CtrlDep)
1046 return false;
1047
1048 // for now, don't put an instruction that does not have operand
1049 // interlocks in the delay slot of a branch
Brian Gaeke918cdd42004-02-12 01:34:05 +00001050 if (! S.getInstrInfo().hasOperandInterlock(node->getOpcode()))
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001051 return false;
1052
Misha Brukman6eba07a2003-09-17 21:34:23 +00001053 // Finally, if the instruction precedes the branch, we make sure the
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001054 // instruction can be reordered relative to the branch. We simply check
1055 // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch.
1056 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001057 if (nodeIsPredecessor) {
1058 bool onlyCDEdgeToBranch = true;
1059 for (SchedGraphNode::const_iterator OEI = node->beginOutEdges();
1060 OEI != node->endOutEdges(); ++OEI)
Tanya Lattnerb6489f32003-08-25 22:42:20 +00001061 if (! ((SchedGraphNode*)(*OEI)->getSink())->isDummyNode()
Misha Brukman6b77ec42003-05-22 21:49:18 +00001062 && ((*OEI)->getSink() != brNode
1063 || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep))
1064 {
1065 onlyCDEdgeToBranch = false;
1066 break;
1067 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001068
Misha Brukman6b77ec42003-05-22 21:49:18 +00001069 if (!onlyCDEdgeToBranch)
1070 return false;
1071 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001072
1073 return true;
1074}
1075
1076
Vikram S. Advec5b46322001-09-30 23:43:34 +00001077static void
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001078MarkNodeForDelaySlot(SchedulingManager& S,
Vikram S. Advef0ba2802001-09-18 12:51:38 +00001079 SchedGraph* graph,
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001080 SchedGraphNode* node,
1081 const SchedGraphNode* brNode,
1082 bool nodeIsPredecessor)
1083{
Misha Brukman6b77ec42003-05-22 21:49:18 +00001084 if (nodeIsPredecessor) {
Misha Brukman6eba07a2003-09-17 21:34:23 +00001085 // If node is in the same basic block (i.e., precedes brNode),
Misha Brukman6b77ec42003-05-22 21:49:18 +00001086 // remove it and all its incident edges from the graph. Make sure we
1087 // add dummy edges for pred/succ nodes that become entry/exit nodes.
1088 graph->eraseIncidentEdges(node, /*addDummyEdges*/ true);
1089 } else {
1090 // If the node was from a target block, add the node to the graph
1091 // and add a CD edge from brNode to node.
1092 assert(0 && "NOT IMPLEMENTED YET");
1093 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001094
1095 DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true);
1096 dinfo->addDelayNode(node);
1097}
1098
1099
Vikram S. Advec5b46322001-09-30 23:43:34 +00001100void
1101FindUsefulInstructionsForDelaySlots(SchedulingManager& S,
1102 SchedGraphNode* brNode,
Misha Brukmanc2312df2003-05-22 21:24:35 +00001103 std::vector<SchedGraphNode*>& sdelayNodeVec)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001104{
Chris Lattner3501fea2003-01-14 22:00:31 +00001105 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001106 unsigned ndelays =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001107 mii.getNumDelaySlots(brNode->getOpcode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001108
1109 if (ndelays == 0)
1110 return;
1111
1112 sdelayNodeVec.reserve(ndelays);
1113
1114 // Use a separate vector to hold the feasible multi-cycle nodes.
1115 // These will be used if not enough single-cycle nodes are found.
1116 //
Misha Brukmanc2312df2003-05-22 21:24:35 +00001117 std::vector<SchedGraphNode*> mdelayNodeVec;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001118
1119 for (sg_pred_iterator P = pred_begin(brNode);
1120 P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P)
1121 if (! (*P)->isDummyNode() &&
Brian Gaeke918cdd42004-02-12 01:34:05 +00001122 ! mii.isNop((*P)->getOpcode()) &&
Vikram S. Advec5b46322001-09-30 23:43:34 +00001123 NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001124 {
Brian Gaeke918cdd42004-02-12 01:34:05 +00001125 if (mii.maxLatency((*P)->getOpcode()) > 1)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001126 mdelayNodeVec.push_back(*P);
1127 else
1128 sdelayNodeVec.push_back(*P);
1129 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001130
1131 // If not enough single-cycle instructions were found, select the
1132 // lowest-latency multi-cycle instructions and use them.
1133 // Note that this is the most efficient code when only 1 (or even 2)
1134 // values need to be selected.
1135 //
Misha Brukman6b77ec42003-05-22 21:49:18 +00001136 while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) {
1137 unsigned lmin =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001138 mii.maxLatency(mdelayNodeVec[0]->getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +00001139 unsigned minIndex = 0;
1140 for (unsigned i=1; i < mdelayNodeVec.size(); i++)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001141 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001142 unsigned li =
Brian Gaeke918cdd42004-02-12 01:34:05 +00001143 mii.maxLatency(mdelayNodeVec[i]->getOpcode());
Misha Brukman6b77ec42003-05-22 21:49:18 +00001144 if (lmin >= li)
1145 {
1146 lmin = li;
1147 minIndex = i;
1148 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001149 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001150 sdelayNodeVec.push_back(mdelayNodeVec[minIndex]);
1151 if (sdelayNodeVec.size() < ndelays) // avoid the last erase!
1152 mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex);
1153 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001154}
1155
1156
1157// Remove the NOPs currently in delay slots from the graph.
1158// Mark instructions specified in sdelayNodeVec to replace them.
1159// If not enough useful instructions were found, mark the NOPs to be used
1160// for filling delay slots, otherwise, otherwise just discard them.
1161//
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001162static void ReplaceNopsWithUsefulInstr(SchedulingManager& S,
1163 SchedGraphNode* node,
Misha Brukman6b77ec42003-05-22 21:49:18 +00001164 // FIXME: passing vector BY VALUE!!!
Misha Brukmanc2312df2003-05-22 21:24:35 +00001165 std::vector<SchedGraphNode*> sdelayNodeVec,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001166 SchedGraph* graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001167{
Misha Brukmanc2312df2003-05-22 21:24:35 +00001168 std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs
Chris Lattner3501fea2003-01-14 22:00:31 +00001169 const TargetInstrInfo& mii = S.getInstrInfo();
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001170 const MachineInstr* brInstr = node->getMachineInstr();
Brian Gaeke918cdd42004-02-12 01:34:05 +00001171 unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpcode());
Vikram S. Advec5b46322001-09-30 23:43:34 +00001172 assert(ndelays > 0 && "Unnecessary call to replace NOPs");
1173
1174 // Remove the NOPs currently in delay slots from the graph.
1175 // If not enough useful instructions were found, use the NOPs to
1176 // fill delay slots, otherwise, just discard them.
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001177 //
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001178 unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1;
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001179 MachineBasicBlock& MBB = node->getMachineBasicBlock();
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001180 MachineBasicBlock::iterator MBBI = MBB.begin();
1181 std::advance(MBBI, firstDelaySlotIdx - 1);
1182 assert(&*MBBI++ == brInstr &&
Vikram S. Adveaf00d482001-11-12 14:18:01 +00001183 "Incorrect instr. index in basic block for brInstr");
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001184
1185 // First find all useful instructions already in the delay slots
1186 // and USE THEM. We'll throw away the unused alternatives below
1187 //
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001188 MachineBasicBlock::iterator Tmp = MBBI;
1189 for (unsigned i = 0; i != ndelays; ++i, ++MBBI)
1190 if (!mii.isNop(MBBI->getOpcode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001191 sdelayNodeVec.insert(sdelayNodeVec.begin(),
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001192 graph->getGraphNodeForInstr(MBBI));
1193 MBBI = Tmp;
1194
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001195 // Then find the NOPs and keep only as many as are needed.
1196 // Put the rest in nopNodeVec to be deleted.
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001197 for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx+ndelays; ++i, ++MBBI)
1198 if (mii.isNop(MBBI->getOpcode()))
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001199 if (sdelayNodeVec.size() < ndelays)
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001200 sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBBI));
Misha Brukman6b77ec42003-05-22 21:49:18 +00001201 else {
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001202 nopNodeVec.push_back(graph->getGraphNodeForInstr(MBBI));
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001203
Misha Brukman6b77ec42003-05-22 21:49:18 +00001204 //remove the MI from the Machine Code For Instruction
Chris Lattner9cdaa632003-07-26 23:23:41 +00001205 const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator();
Misha Brukman6b77ec42003-05-22 21:49:18 +00001206 MachineCodeForInstruction& llvmMvec =
Chris Lattner9cdaa632003-07-26 23:23:41 +00001207 MachineCodeForInstruction::get((const Instruction *)TI);
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001208
Misha Brukman6b77ec42003-05-22 21:49:18 +00001209 for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(),
1210 mciE=llvmMvec.end(); mciI!=mciE; ++mciI){
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001211 if (*mciI == MBBI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001212 llvmMvec.erase(mciI);
1213 }
1214 }
Mehwish Nagdae95ce742002-07-25 17:31:05 +00001215
Vikram S. Advefb8c0532001-10-22 13:49:27 +00001216 assert(sdelayNodeVec.size() >= ndelays);
1217
1218 // If some delay slots were already filled, throw away that many new choices
1219 if (sdelayNodeVec.size() > ndelays)
1220 sdelayNodeVec.resize(ndelays);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001221
1222 // Mark the nodes chosen for delay slots. This removes them from the graph.
1223 for (unsigned i=0; i < sdelayNodeVec.size(); i++)
1224 MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true);
1225
1226 // And remove the unused NOPs from the graph.
1227 for (unsigned i=0; i < nopNodeVec.size(); i++)
1228 graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true);
1229}
1230
1231
1232// For all delayed instructions, choose instructions to put in the delay
1233// slots and pull those out of the graph. Mark them for the delay slots
1234// in the DelaySlotInfo object for that graph node. If no useful work
1235// is found for a delay slot, use the NOP that is currently in that slot.
1236//
1237// We try to fill the delay slots with useful work for all instructions
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001238// EXCEPT CALLS AND RETURNS.
1239// For CALLs and RETURNs, it is nearly always possible to use one of the
Vikram S. Advec5b46322001-09-30 23:43:34 +00001240// call sequence instrs and putting anything else in the delay slot could be
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001241// suboptimal. Also, it complicates generating the calling sequence code in
1242// regalloc.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001243//
1244static void
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001245ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB,
Chris Lattner3462cae2002-02-03 07:28:30 +00001246 SchedGraph *graph)
Vikram S. Advec5b46322001-09-30 23:43:34 +00001247{
Chris Lattner3501fea2003-01-14 22:00:31 +00001248 const TargetInstrInfo& mii = S.getInstrInfo();
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +00001249
1250 Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator();
Chris Lattner3462cae2002-02-03 07:28:30 +00001251 MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr);
Misha Brukmanc2312df2003-05-22 21:24:35 +00001252 std::vector<SchedGraphNode*> delayNodeVec;
Vikram S. Adve6db77c52001-10-10 20:58:11 +00001253 const MachineInstr* brInstr = NULL;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001254
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001255 if (EnableFillingDelaySlots &&
1256 termInstr->getOpcode() != Instruction::Ret)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001257 {
1258 // To find instructions that need delay slots without searching the full
1259 // machine code, we assume that the only delayed instructions are CALLs
1260 // or instructions generated for the terminator inst.
1261 // Find the first branch instr in the sequence of machine instrs for term
1262 //
1263 unsigned first = 0;
1264 while (first < termMvec.size() &&
Brian Gaeke918cdd42004-02-12 01:34:05 +00001265 ! mii.isBranch(termMvec[first]->getOpcode()))
Vikram S. Advec5b46322001-09-30 23:43:34 +00001266 {
Misha Brukman6b77ec42003-05-22 21:49:18 +00001267 ++first;
Vikram S. Advec5b46322001-09-30 23:43:34 +00001268 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001269 assert(first < termMvec.size() &&
1270 "No branch instructions for BR? Ok, but weird! Delete assertion.");
1271
1272 brInstr = (first < termMvec.size())? termMvec[first] : NULL;
1273
1274 // Compute a vector of the nodes chosen for delay slots and then
1275 // mark delay slots to replace NOPs with these useful instructions.
1276 //
1277 if (brInstr != NULL) {
1278 SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr);
1279 FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec);
1280 ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph);
1281 }
1282 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001283
1284 // Also mark delay slots for other delayed instructions to hold NOPs.
1285 // Simply passing in an empty delayNodeVec will have this effect.
Vikram S. Advebed4eff2003-09-16 05:55:15 +00001286 // If brInstr is not handled above (EnableFillingDelaySlots == false),
1287 // brInstr will be NULL so this will handle the branch instrs. as well.
Vikram S. Advec5b46322001-09-30 23:43:34 +00001288 //
1289 delayNodeVec.clear();
Chris Lattnerfdc01ce2004-02-18 16:38:18 +00001290 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
1291 if (I != brInstr && mii.getNumDelaySlots(I->getOpcode()) > 0) {
1292 SchedGraphNode* node = graph->getGraphNodeForInstr(I);
Misha Brukman6b77ec42003-05-22 21:49:18 +00001293 ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph);
1294 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001295}
1296
1297
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001298//
1299// Schedule the delayed branch and its delay slots
1300//
Vikram S. Advec5b46322001-09-30 23:43:34 +00001301unsigned
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001302DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S)
1303{
1304 assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch");
1305 assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL
1306 && "Slot for branch should be empty");
1307
1308 unsigned int nextSlot = delayedNodeSlotNum;
1309 cycles_t nextTime = delayedNodeCycle;
1310
1311 S.scheduleInstr(brNode, nextSlot, nextTime);
1312
Misha Brukman6b77ec42003-05-22 21:49:18 +00001313 for (unsigned d=0; d < ndelays; d++) {
1314 ++nextSlot;
1315 if (nextSlot == S.nslots) {
1316 nextSlot = 0;
1317 nextTime++;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001318 }
Misha Brukman6b77ec42003-05-22 21:49:18 +00001319
1320 // Find the first feasible instruction for this delay slot
1321 // Note that we only check for issue restrictions here.
1322 // We do *not* check for flow dependences but rely on pipeline
1323 // interlocks to resolve them. Machines without interlocks
1324 // will require this code to be modified.
1325 for (unsigned i=0; i < delayNodeVec.size(); i++) {
1326 const SchedGraphNode* dnode = delayNodeVec[i];
1327 if ( ! S.isScheduled(dnode)
Brian Gaeke918cdd42004-02-12 01:34:05 +00001328 && S.schedInfo.instrCanUseSlot(dnode->getOpcode(), nextSlot)
1329 && instrIsFeasible(S, dnode->getOpcode()))
Misha Brukman6b77ec42003-05-22 21:49:18 +00001330 {
Brian Gaeke918cdd42004-02-12 01:34:05 +00001331 assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpcode())
Misha Brukman6b77ec42003-05-22 21:49:18 +00001332 && "Instructions without interlocks not yet supported "
1333 "when filling branch delay slots");
1334 S.scheduleInstr(dnode, nextSlot, nextTime);
1335 break;
1336 }
1337 }
1338 }
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001339
1340 // Update current time if delay slots overflowed into later cycles.
1341 // Do this here because we know exactly which cycle is the last cycle
1342 // that contains delay slots. The next loop doesn't compute that.
1343 if (nextTime > S.getTime())
1344 S.updateTime(nextTime);
1345
1346 // Now put any remaining instructions in the unfilled delay slots.
1347 // This could lead to suboptimal performance but needed for correctness.
1348 nextSlot = delayedNodeSlotNum;
1349 nextTime = delayedNodeCycle;
1350 for (unsigned i=0; i < delayNodeVec.size(); i++)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001351 if (! S.isScheduled(delayNodeVec[i])) {
1352 do { // find the next empty slot
1353 ++nextSlot;
1354 if (nextSlot == S.nslots) {
1355 nextSlot = 0;
1356 nextTime++;
1357 }
1358 } while (S.isched.getInstr(nextSlot, nextTime) != NULL);
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001359
Misha Brukman6b77ec42003-05-22 21:49:18 +00001360 S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime);
1361 break;
1362 }
Vikram S. Advec5b46322001-09-30 23:43:34 +00001363
1364 return 1 + ndelays;
Vikram S. Adve0e1158f2001-08-28 23:07:19 +00001365}
1366
Vikram S. Advec5b46322001-09-30 23:43:34 +00001367
1368// Check if the instruction would conflict with instructions already
1369// chosen for the current cycle
1370//
1371static inline bool
1372ConflictsWithChoices(const SchedulingManager& S,
1373 MachineOpCode opCode)
1374{
1375 // Check if the instruction must issue by itself, and some feasible
1376 // choices have already been made for this cycle
1377 if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode))
1378 return true;
1379
1380 // For each class that opCode belongs to, check if there are too many
1381 // instructions of that class.
1382 //
1383 const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode);
1384 return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc));
1385}
1386
1387
1388//************************* External Functions *****************************/
1389
1390
1391//---------------------------------------------------------------------------
1392// Function: ViolatesMinimumGap
1393//
1394// Purpose:
1395// Check minimum gap requirements relative to instructions scheduled in
1396// previous cycles.
1397// Note that we do not need to consider `nextEarliestIssueTime' here because
1398// that is also captured in the earliest start times for each opcode.
1399//---------------------------------------------------------------------------
1400
1401static inline bool
1402ViolatesMinimumGap(const SchedulingManager& S,
1403 MachineOpCode opCode,
1404 const cycles_t inCycle)
1405{
1406 return (inCycle < S.getEarliestStartTimeForOp(opCode));
1407}
1408
1409
1410//---------------------------------------------------------------------------
1411// Function: instrIsFeasible
1412//
1413// Purpose:
1414// Check if any issue restrictions would prevent the instruction from
1415// being issued in the current cycle
1416//---------------------------------------------------------------------------
1417
1418bool
1419instrIsFeasible(const SchedulingManager& S,
1420 MachineOpCode opCode)
1421{
1422 // skip the instruction if it cannot be issued due to issue restrictions
1423 // caused by previously issued instructions
1424 if (ViolatesMinimumGap(S, opCode, S.getTime()))
1425 return false;
1426
1427 // skip the instruction if it cannot be issued due to issue restrictions
1428 // caused by previously chosen instructions for the current cycle
1429 if (ConflictsWithChoices(S, opCode))
1430 return false;
1431
1432 return true;
1433}
1434
1435//---------------------------------------------------------------------------
1436// Function: ScheduleInstructionsWithSSA
1437//
1438// Purpose:
1439// Entry point for instruction scheduling on SSA form.
1440// Schedules the machine instructions generated by instruction selection.
1441// Assumes that register allocation has not been done, i.e., operands
1442// are still in SSA form.
1443//---------------------------------------------------------------------------
1444
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001445namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +00001446 class InstructionSchedulingWithSSA : public FunctionPass {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001447 const TargetMachine &target;
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001448 public:
Vikram S. Adve802cec42002-03-24 03:44:55 +00001449 inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {}
Chris Lattner96c466b2002-04-29 14:57:45 +00001450
1451 const char *getPassName() const { return "Instruction Scheduling"; }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001452
Chris Lattnerf57b8452002-04-27 06:56:12 +00001453 // getAnalysisUsage - We use LiveVarInfo...
1454 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +00001455 AU.addRequired<FunctionLiveVarInfo>();
Chris Lattnera0877722002-10-23 03:30:47 +00001456 AU.setPreservesCFG();
Vikram S. Advec5b46322001-09-30 23:43:34 +00001457 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001458
Chris Lattner7e708292002-06-25 16:13:24 +00001459 bool runOnFunction(Function &F);
Chris Lattner9adb7ad2002-02-04 20:02:16 +00001460 };
1461} // end anonymous namespace
1462
Vikram S. Adve802cec42002-03-24 03:44:55 +00001463
Chris Lattner7e708292002-06-25 16:13:24 +00001464bool InstructionSchedulingWithSSA::runOnFunction(Function &F)
Vikram S. Adve802cec42002-03-24 03:44:55 +00001465{
Chris Lattner7e708292002-06-25 16:13:24 +00001466 SchedGraphSet graphSet(&F, target);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001467
Misha Brukman6b77ec42003-05-22 21:49:18 +00001468 if (SchedDebugLevel >= Sched_PrintSchedGraphs) {
Misha Brukmanc2312df2003-05-22 21:24:35 +00001469 std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001470 graphSet.dump();
1471 }
1472
1473 for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end();
1474 GI != GE; ++GI)
Misha Brukman6b77ec42003-05-22 21:49:18 +00001475 {
1476 SchedGraph* graph = (*GI);
1477 MachineBasicBlock &MBB = graph->getBasicBlock();
Vikram S. Adve802cec42002-03-24 03:44:55 +00001478
Misha Brukman6b77ec42003-05-22 21:49:18 +00001479 if (SchedDebugLevel >= Sched_PrintSchedTrace)
1480 std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n";
Vikram S. Adve802cec42002-03-24 03:44:55 +00001481
Misha Brukman6b77ec42003-05-22 21:49:18 +00001482 // expensive!
1483 SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>());
1484 SchedulingManager S(target, graph, schedPrio);
Vikram S. Adve802cec42002-03-24 03:44:55 +00001485
Misha Brukman6b77ec42003-05-22 21:49:18 +00001486 ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph
1487 ForwardListSchedule(S); // computes schedule in S
1488 RecordSchedule(MBB, S); // records schedule in BB
1489 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001490
Misha Brukman6b77ec42003-05-22 21:49:18 +00001491 if (SchedDebugLevel >= Sched_PrintMachineCode) {
1492 std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n";
1493 MachineFunction::get(&F).dump();
1494 }
Vikram S. Adve802cec42002-03-24 03:44:55 +00001495
1496 return false;
1497}
1498
1499
Brian Gaekebf3c4cf2003-08-14 06:09:32 +00001500FunctionPass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) {
Vikram S. Adve802cec42002-03-24 03:44:55 +00001501 return new InstructionSchedulingWithSSA(tgt);
Vikram S. Advec5b46322001-09-30 23:43:34 +00001502}
Brian Gaeked0fde302003-11-11 22:41:34 +00001503
1504} // End llvm namespace
1505