Chris Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 1 | //===- InstrScheduling.cpp - Generic Instruction Scheduling support -------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the llvm/CodeGen/InstrScheduling.h interface, along with |
| 11 | // generic support routines for instruction scheduling. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 14 | |
Chris Lattner | c6f3ae5 | 2002-04-29 17:42:12 +0000 | [diff] [blame] | 15 | #include "SchedPriorities.h" |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 3462cae | 2002-02-03 07:28:30 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 92ba2aa | 2003-01-14 23:05:08 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/FunctionLiveVarInfo.h" |
Chris Lattner | 3462cae | 2002-02-03 07:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | f35f2fb | 2002-02-04 16:35:45 +0000 | [diff] [blame] | 21 | #include "llvm/BasicBlock.h" |
Chris Lattner | 70e60cb | 2002-05-22 17:08:27 +0000 | [diff] [blame] | 22 | #include "Support/CommandLine.h" |
Chris Lattner | 1ff63a1 | 2001-09-07 21:19:42 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 70e60cb | 2002-05-22 17:08:27 +0000 | [diff] [blame] | 25 | SchedDebugLevel_t SchedDebugLevel; |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 26 | |
Vikram S. Adve | bed4eff | 2003-09-16 05:55:15 +0000 | [diff] [blame] | 27 | static cl::opt<bool> EnableFillingDelaySlots("sched-fill-delay-slots", |
| 28 | cl::desc("Fill branch delay slots during local scheduling")); |
| 29 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 30 | static cl::opt<SchedDebugLevel_t, true> |
| 31 | SDL_opt("dsched", cl::Hidden, cl::location(SchedDebugLevel), |
| 32 | cl::desc("enable instruction scheduling debugging information"), |
| 33 | cl::values( |
| 34 | clEnumValN(Sched_NoDebugInfo, "n", "disable debug output"), |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 35 | clEnumValN(Sched_PrintMachineCode, "y", "print machine code after scheduling"), |
| 36 | clEnumValN(Sched_PrintSchedTrace, "t", "print trace of scheduling actions"), |
| 37 | clEnumValN(Sched_PrintSchedGraphs, "g", "print scheduling graphs"), |
| 38 | 0)); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 39 | |
| 40 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 41 | //************************* Internal Data Types *****************************/ |
| 42 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 43 | class InstrSchedule; |
| 44 | class SchedulingManager; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 45 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 46 | |
| 47 | //---------------------------------------------------------------------- |
| 48 | // class InstrGroup: |
| 49 | // |
| 50 | // Represents a group of instructions scheduled to be issued |
| 51 | // in a single cycle. |
| 52 | //---------------------------------------------------------------------- |
| 53 | |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 54 | class InstrGroup { |
| 55 | InstrGroup(const InstrGroup&); // DO NOT IMPLEMENT |
| 56 | void operator=(const InstrGroup&); // DO NOT IMPLEMENT |
| 57 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 58 | public: |
| 59 | inline const SchedGraphNode* operator[](unsigned int slotNum) const { |
| 60 | assert(slotNum < group.size()); |
| 61 | return group[slotNum]; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | friend class InstrSchedule; |
| 66 | |
| 67 | inline void addInstr(const SchedGraphNode* node, unsigned int slotNum) { |
| 68 | assert(slotNum < group.size()); |
| 69 | group[slotNum] = node; |
| 70 | } |
| 71 | |
| 72 | /*ctor*/ InstrGroup(unsigned int nslots) |
| 73 | : group(nslots, NULL) {} |
| 74 | |
| 75 | /*ctor*/ InstrGroup(); // disable: DO NOT IMPLEMENT |
| 76 | |
| 77 | private: |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 78 | std::vector<const SchedGraphNode*> group; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
| 82 | //---------------------------------------------------------------------- |
| 83 | // class ScheduleIterator: |
| 84 | // |
| 85 | // Iterates over the machine instructions in the for a single basic block. |
| 86 | // The schedule is represented by an InstrSchedule object. |
| 87 | //---------------------------------------------------------------------- |
| 88 | |
| 89 | template<class _NodeType> |
Chris Lattner | d8bbc06 | 2002-07-25 18:04:48 +0000 | [diff] [blame] | 90 | class ScheduleIterator : public forward_iterator<_NodeType, ptrdiff_t> { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 91 | private: |
| 92 | unsigned cycleNum; |
| 93 | unsigned slotNum; |
| 94 | const InstrSchedule& S; |
| 95 | public: |
| 96 | typedef ScheduleIterator<_NodeType> _Self; |
| 97 | |
| 98 | /*ctor*/ inline ScheduleIterator(const InstrSchedule& _schedule, |
| 99 | unsigned _cycleNum, |
| 100 | unsigned _slotNum) |
| 101 | : cycleNum(_cycleNum), slotNum(_slotNum), S(_schedule) { |
| 102 | skipToNextInstr(); |
| 103 | } |
| 104 | |
| 105 | /*ctor*/ inline ScheduleIterator(const _Self& x) |
| 106 | : cycleNum(x.cycleNum), slotNum(x.slotNum), S(x.S) {} |
| 107 | |
| 108 | inline bool operator==(const _Self& x) const { |
| 109 | return (slotNum == x.slotNum && cycleNum== x.cycleNum && &S==&x.S); |
| 110 | } |
| 111 | |
| 112 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 113 | |
Chris Lattner | 414d9d2 | 2003-11-05 06:25:06 +0000 | [diff] [blame^] | 114 | inline _NodeType* operator*() const; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 115 | inline _NodeType* operator->() const { return operator*(); } |
| 116 | |
| 117 | _Self& operator++(); // Preincrement |
| 118 | inline _Self operator++(int) { // Postincrement |
| 119 | _Self tmp(*this); ++*this; return tmp; |
| 120 | } |
| 121 | |
| 122 | static _Self begin(const InstrSchedule& _schedule); |
| 123 | static _Self end( const InstrSchedule& _schedule); |
| 124 | |
| 125 | private: |
| 126 | inline _Self& operator=(const _Self& x); // DISABLE -- DO NOT IMPLEMENT |
| 127 | void skipToNextInstr(); |
| 128 | }; |
| 129 | |
| 130 | |
| 131 | //---------------------------------------------------------------------- |
| 132 | // class InstrSchedule: |
| 133 | // |
| 134 | // Represents the schedule of machine instructions for a single basic block. |
| 135 | //---------------------------------------------------------------------- |
| 136 | |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 137 | class InstrSchedule { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 138 | const unsigned int nslots; |
| 139 | unsigned int numInstr; |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 140 | std::vector<InstrGroup*> groups; // indexed by cycle number |
| 141 | std::vector<cycles_t> startTime; // indexed by node id |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 142 | |
| 143 | InstrSchedule(InstrSchedule&); // DO NOT IMPLEMENT |
| 144 | void operator=(InstrSchedule&); // DO NOT IMPLEMENT |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 145 | |
| 146 | public: // iterators |
| 147 | typedef ScheduleIterator<SchedGraphNode> iterator; |
| 148 | typedef ScheduleIterator<const SchedGraphNode> const_iterator; |
| 149 | |
| 150 | iterator begin(); |
| 151 | const_iterator begin() const; |
| 152 | iterator end(); |
| 153 | const_iterator end() const; |
| 154 | |
| 155 | public: // constructors and destructor |
| 156 | /*ctor*/ InstrSchedule (unsigned int _nslots, |
| 157 | unsigned int _numNodes); |
| 158 | /*dtor*/ ~InstrSchedule (); |
| 159 | |
| 160 | public: // accessor functions to query chosen schedule |
| 161 | const SchedGraphNode* getInstr (unsigned int slotNum, |
| 162 | cycles_t c) const { |
| 163 | const InstrGroup* igroup = this->getIGroup(c); |
| 164 | return (igroup == NULL)? NULL : (*igroup)[slotNum]; |
| 165 | } |
| 166 | |
| 167 | inline InstrGroup* getIGroup (cycles_t c) { |
Chris Lattner | dfb8b95 | 2002-02-24 23:01:50 +0000 | [diff] [blame] | 168 | if ((unsigned)c >= groups.size()) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 169 | groups.resize(c+1); |
| 170 | if (groups[c] == NULL) |
| 171 | groups[c] = new InstrGroup(nslots); |
| 172 | return groups[c]; |
| 173 | } |
| 174 | |
| 175 | inline const InstrGroup* getIGroup (cycles_t c) const { |
Chris Lattner | dfb8b95 | 2002-02-24 23:01:50 +0000 | [diff] [blame] | 176 | assert((unsigned)c < groups.size()); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 177 | return groups[c]; |
| 178 | } |
| 179 | |
| 180 | inline cycles_t getStartTime (unsigned int nodeId) const { |
| 181 | assert(nodeId < startTime.size()); |
| 182 | return startTime[nodeId]; |
| 183 | } |
| 184 | |
| 185 | unsigned int getNumInstructions() const { |
| 186 | return numInstr; |
| 187 | } |
| 188 | |
| 189 | inline void scheduleInstr (const SchedGraphNode* node, |
| 190 | unsigned int slotNum, |
| 191 | cycles_t cycle) { |
| 192 | InstrGroup* igroup = this->getIGroup(cycle); |
| 193 | assert((*igroup)[slotNum] == NULL && "Slot already filled?"); |
| 194 | igroup->addInstr(node, slotNum); |
| 195 | assert(node->getNodeId() < startTime.size()); |
| 196 | startTime[node->getNodeId()] = cycle; |
| 197 | ++numInstr; |
| 198 | } |
| 199 | |
| 200 | private: |
Chris Lattner | 414d9d2 | 2003-11-05 06:25:06 +0000 | [diff] [blame^] | 201 | friend class ScheduleIterator<SchedGraphNode>; |
| 202 | friend class ScheduleIterator<const SchedGraphNode>; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 203 | /*ctor*/ InstrSchedule (); // Disable: DO NOT IMPLEMENT. |
| 204 | }; |
| 205 | |
Chris Lattner | 414d9d2 | 2003-11-05 06:25:06 +0000 | [diff] [blame^] | 206 | template<class NodeType> |
| 207 | inline NodeType *ScheduleIterator<NodeType>::operator*() const { |
| 208 | assert(cycleNum < S.groups.size()); |
| 209 | return (*S.groups[cycleNum])[slotNum]; |
| 210 | } |
| 211 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 212 | |
| 213 | /*ctor*/ |
| 214 | InstrSchedule::InstrSchedule(unsigned int _nslots, unsigned int _numNodes) |
| 215 | : nslots(_nslots), |
| 216 | numInstr(0), |
| 217 | groups(2 * _numNodes / _nslots), // 2 x lower-bound for #cycles |
| 218 | startTime(_numNodes, (cycles_t) -1) // set all to -1 |
| 219 | { |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /*dtor*/ |
| 224 | InstrSchedule::~InstrSchedule() |
| 225 | { |
| 226 | for (unsigned c=0, NC=groups.size(); c < NC; c++) |
| 227 | if (groups[c] != NULL) |
| 228 | delete groups[c]; // delete InstrGroup objects |
| 229 | } |
| 230 | |
| 231 | |
| 232 | template<class _NodeType> |
| 233 | inline |
| 234 | void |
| 235 | ScheduleIterator<_NodeType>::skipToNextInstr() |
| 236 | { |
| 237 | while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL) |
| 238 | ++cycleNum; // skip cycles with no instructions |
| 239 | |
| 240 | while (cycleNum < S.groups.size() && |
| 241 | (*S.groups[cycleNum])[slotNum] == NULL) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 242 | { |
| 243 | ++slotNum; |
| 244 | if (slotNum == S.nslots) { |
| 245 | ++cycleNum; |
| 246 | slotNum = 0; |
| 247 | while(cycleNum < S.groups.size() && S.groups[cycleNum] == NULL) |
| 248 | ++cycleNum; // skip cycles with no instructions |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 249 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 250 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | template<class _NodeType> |
| 254 | inline |
| 255 | ScheduleIterator<_NodeType>& |
| 256 | ScheduleIterator<_NodeType>::operator++() // Preincrement |
| 257 | { |
| 258 | ++slotNum; |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 259 | if (slotNum == S.nslots) { |
| 260 | ++cycleNum; |
| 261 | slotNum = 0; |
| 262 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 263 | skipToNextInstr(); |
| 264 | return *this; |
| 265 | } |
| 266 | |
| 267 | template<class _NodeType> |
| 268 | ScheduleIterator<_NodeType> |
| 269 | ScheduleIterator<_NodeType>::begin(const InstrSchedule& _schedule) |
| 270 | { |
| 271 | return _Self(_schedule, 0, 0); |
| 272 | } |
| 273 | |
| 274 | template<class _NodeType> |
| 275 | ScheduleIterator<_NodeType> |
| 276 | ScheduleIterator<_NodeType>::end(const InstrSchedule& _schedule) |
| 277 | { |
| 278 | return _Self(_schedule, _schedule.groups.size(), 0); |
| 279 | } |
| 280 | |
| 281 | InstrSchedule::iterator |
| 282 | InstrSchedule::begin() |
| 283 | { |
| 284 | return iterator::begin(*this); |
| 285 | } |
| 286 | |
| 287 | InstrSchedule::const_iterator |
| 288 | InstrSchedule::begin() const |
| 289 | { |
| 290 | return const_iterator::begin(*this); |
| 291 | } |
| 292 | |
| 293 | InstrSchedule::iterator |
| 294 | InstrSchedule::end() |
| 295 | { |
| 296 | return iterator::end(*this); |
| 297 | } |
| 298 | |
| 299 | InstrSchedule::const_iterator |
| 300 | InstrSchedule::end() const |
| 301 | { |
| 302 | return const_iterator::end( *this); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | //---------------------------------------------------------------------- |
| 307 | // class DelaySlotInfo: |
| 308 | // |
| 309 | // Record information about delay slots for a single branch instruction. |
| 310 | // Delay slots are simply indexed by slot number 1 ... numDelaySlots |
| 311 | //---------------------------------------------------------------------- |
| 312 | |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 313 | class DelaySlotInfo { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 314 | const SchedGraphNode* brNode; |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 315 | unsigned ndelays; |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 316 | std::vector<const SchedGraphNode*> delayNodeVec; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 317 | cycles_t delayedNodeCycle; |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 318 | unsigned delayedNodeSlotNum; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 319 | |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 320 | DelaySlotInfo(const DelaySlotInfo &); // DO NOT IMPLEMENT |
| 321 | void operator=(const DelaySlotInfo&); // DO NOT IMPLEMENT |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 322 | public: |
| 323 | /*ctor*/ DelaySlotInfo (const SchedGraphNode* _brNode, |
| 324 | unsigned _ndelays) |
| 325 | : brNode(_brNode), ndelays(_ndelays), |
| 326 | delayedNodeCycle(0), delayedNodeSlotNum(0) {} |
| 327 | |
| 328 | inline unsigned getNumDelays () { |
| 329 | return ndelays; |
| 330 | } |
| 331 | |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 332 | inline const std::vector<const SchedGraphNode*>& getDelayNodeVec() { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 333 | return delayNodeVec; |
| 334 | } |
| 335 | |
| 336 | inline void addDelayNode (const SchedGraphNode* node) { |
| 337 | delayNodeVec.push_back(node); |
| 338 | assert(delayNodeVec.size() <= ndelays && "Too many delay slot instrs!"); |
| 339 | } |
| 340 | |
| 341 | inline void recordChosenSlot (cycles_t cycle, unsigned slotNum) { |
| 342 | delayedNodeCycle = cycle; |
| 343 | delayedNodeSlotNum = slotNum; |
| 344 | } |
| 345 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 346 | unsigned scheduleDelayedNode (SchedulingManager& S); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | |
| 350 | //---------------------------------------------------------------------- |
| 351 | // class SchedulingManager: |
| 352 | // |
| 353 | // Represents the schedule of machine instructions for a single basic block. |
| 354 | //---------------------------------------------------------------------- |
| 355 | |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 356 | class SchedulingManager { |
| 357 | SchedulingManager(SchedulingManager &); // DO NOT IMPLEMENT |
| 358 | void operator=(const SchedulingManager &); // DO NOT IMPLEMENT |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 359 | public: // publicly accessible data members |
Chris Lattner | d0f166a | 2002-12-29 03:13:05 +0000 | [diff] [blame] | 360 | const unsigned nslots; |
| 361 | const TargetSchedInfo& schedInfo; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 362 | SchedPriorities& schedPrio; |
| 363 | InstrSchedule isched; |
| 364 | |
| 365 | private: |
Chris Lattner | e3561c2 | 2003-08-15 05:20:06 +0000 | [diff] [blame] | 366 | unsigned totalInstrCount; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 367 | cycles_t curTime; |
| 368 | cycles_t nextEarliestIssueTime; // next cycle we can issue |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 369 | // indexed by slot# |
| 370 | std::vector<hash_set<const SchedGraphNode*> > choicesForSlot; |
| 371 | std::vector<const SchedGraphNode*> choiceVec; // indexed by node ptr |
| 372 | std::vector<int> numInClass; // indexed by sched class |
| 373 | std::vector<cycles_t> nextEarliestStartTime; // indexed by opCode |
Chris Lattner | d8bbc06 | 2002-07-25 18:04:48 +0000 | [diff] [blame] | 374 | hash_map<const SchedGraphNode*, DelaySlotInfo*> delaySlotInfoForBranches; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 375 | // indexed by branch node ptr |
| 376 | |
| 377 | public: |
Chris Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 378 | SchedulingManager(const TargetMachine& _target, const SchedGraph* graph, |
| 379 | SchedPriorities& schedPrio); |
| 380 | ~SchedulingManager() { |
Chris Lattner | d8bbc06 | 2002-07-25 18:04:48 +0000 | [diff] [blame] | 381 | for (hash_map<const SchedGraphNode*, |
Chris Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 382 | DelaySlotInfo*>::iterator I = delaySlotInfoForBranches.begin(), |
| 383 | E = delaySlotInfoForBranches.end(); I != E; ++I) |
| 384 | delete I->second; |
| 385 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 386 | |
| 387 | //---------------------------------------------------------------------- |
| 388 | // Simplify access to the machine instruction info |
| 389 | //---------------------------------------------------------------------- |
| 390 | |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 391 | inline const TargetInstrInfo& getInstrInfo () const { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 392 | return schedInfo.getInstrInfo(); |
| 393 | } |
| 394 | |
| 395 | //---------------------------------------------------------------------- |
| 396 | // Interface for checking and updating the current time |
| 397 | //---------------------------------------------------------------------- |
| 398 | |
| 399 | inline cycles_t getTime () const { |
| 400 | return curTime; |
| 401 | } |
| 402 | |
| 403 | inline cycles_t getEarliestIssueTime() const { |
| 404 | return nextEarliestIssueTime; |
| 405 | } |
| 406 | |
| 407 | inline cycles_t getEarliestStartTimeForOp(MachineOpCode opCode) const { |
| 408 | assert(opCode < (int) nextEarliestStartTime.size()); |
| 409 | return nextEarliestStartTime[opCode]; |
| 410 | } |
| 411 | |
| 412 | // Update current time to specified cycle |
| 413 | inline void updateTime (cycles_t c) { |
| 414 | curTime = c; |
| 415 | schedPrio.updateTime(c); |
| 416 | } |
| 417 | |
| 418 | //---------------------------------------------------------------------- |
| 419 | // Functions to manage the choices for the current cycle including: |
| 420 | // -- a vector of choices by priority (choiceVec) |
| 421 | // -- vectors of the choices for each instruction slot (choicesForSlot[]) |
| 422 | // -- number of choices in each sched class, used to check issue conflicts |
| 423 | // between choices for a single cycle |
| 424 | //---------------------------------------------------------------------- |
| 425 | |
| 426 | inline unsigned int getNumChoices () const { |
| 427 | return choiceVec.size(); |
| 428 | } |
| 429 | |
| 430 | inline unsigned getNumChoicesInClass (const InstrSchedClass& sc) const { |
Chris Lattner | c5ddc8b | 2002-10-28 04:53:02 +0000 | [diff] [blame] | 431 | assert(sc < numInClass.size() && "Invalid op code or sched class!"); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 432 | return numInClass[sc]; |
| 433 | } |
| 434 | |
| 435 | inline const SchedGraphNode* getChoice(unsigned int i) const { |
| 436 | // assert(i < choiceVec.size()); don't check here. |
| 437 | return choiceVec[i]; |
| 438 | } |
| 439 | |
Chris Lattner | d8bbc06 | 2002-07-25 18:04:48 +0000 | [diff] [blame] | 440 | inline hash_set<const SchedGraphNode*>& getChoicesForSlot(unsigned slotNum) { |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 441 | assert(slotNum < nslots); |
| 442 | return choicesForSlot[slotNum]; |
| 443 | } |
| 444 | |
| 445 | inline void addChoice (const SchedGraphNode* node) { |
| 446 | // Append the instruction to the vector of choices for current cycle. |
| 447 | // Increment numInClass[c] for the sched class to which the instr belongs. |
| 448 | choiceVec.push_back(node); |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 449 | const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode()); |
Chris Lattner | c5ddc8b | 2002-10-28 04:53:02 +0000 | [diff] [blame] | 450 | assert(sc < numInClass.size()); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 451 | numInClass[sc]++; |
| 452 | } |
| 453 | |
| 454 | inline void addChoiceToSlot (unsigned int slotNum, |
| 455 | const SchedGraphNode* node) { |
| 456 | // Add the instruction to the choice set for the specified slot |
| 457 | assert(slotNum < nslots); |
| 458 | choicesForSlot[slotNum].insert(node); |
| 459 | } |
| 460 | |
| 461 | inline void resetChoices () { |
| 462 | choiceVec.clear(); |
| 463 | for (unsigned int s=0; s < nslots; s++) |
| 464 | choicesForSlot[s].clear(); |
| 465 | for (unsigned int c=0; c < numInClass.size(); c++) |
| 466 | numInClass[c] = 0; |
| 467 | } |
| 468 | |
| 469 | //---------------------------------------------------------------------- |
| 470 | // Code to query and manage the partial instruction schedule so far |
| 471 | //---------------------------------------------------------------------- |
| 472 | |
| 473 | inline unsigned int getNumScheduled () const { |
| 474 | return isched.getNumInstructions(); |
| 475 | } |
| 476 | |
| 477 | inline unsigned int getNumUnscheduled() const { |
| 478 | return totalInstrCount - isched.getNumInstructions(); |
| 479 | } |
| 480 | |
| 481 | inline bool isScheduled (const SchedGraphNode* node) const { |
| 482 | return (isched.getStartTime(node->getNodeId()) >= 0); |
| 483 | } |
| 484 | |
| 485 | inline void scheduleInstr (const SchedGraphNode* node, |
| 486 | unsigned int slotNum, |
| 487 | cycles_t cycle) |
| 488 | { |
| 489 | assert(! isScheduled(node) && "Instruction already scheduled?"); |
| 490 | |
| 491 | // add the instruction to the schedule |
| 492 | isched.scheduleInstr(node, slotNum, cycle); |
| 493 | |
| 494 | // update the earliest start times of all nodes that conflict with `node' |
| 495 | // and the next-earliest time anything can issue if `node' causes bubbles |
| 496 | updateEarliestStartTimes(node, cycle); |
| 497 | |
| 498 | // remove the instruction from the choice sets for all slots |
| 499 | for (unsigned s=0; s < nslots; s++) |
| 500 | choicesForSlot[s].erase(node); |
| 501 | |
| 502 | // and decrement the instr count for the sched class to which it belongs |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 503 | const InstrSchedClass& sc = schedInfo.getSchedClass(node->getOpCode()); |
Chris Lattner | c5ddc8b | 2002-10-28 04:53:02 +0000 | [diff] [blame] | 504 | assert(sc < numInClass.size()); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 505 | numInClass[sc]--; |
| 506 | } |
Chris Lattner | 1ff63a1 | 2001-09-07 21:19:42 +0000 | [diff] [blame] | 507 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 508 | //---------------------------------------------------------------------- |
| 509 | // Create and retrieve delay slot info for delayed instructions |
| 510 | //---------------------------------------------------------------------- |
| 511 | |
| 512 | inline DelaySlotInfo* getDelaySlotInfoForInstr(const SchedGraphNode* bn, |
| 513 | bool createIfMissing=false) |
| 514 | { |
Chris Lattner | d8bbc06 | 2002-07-25 18:04:48 +0000 | [diff] [blame] | 515 | hash_map<const SchedGraphNode*, DelaySlotInfo*>::const_iterator |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 516 | I = delaySlotInfoForBranches.find(bn); |
Chris Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 517 | if (I != delaySlotInfoForBranches.end()) |
| 518 | return I->second; |
| 519 | |
| 520 | if (!createIfMissing) return 0; |
| 521 | |
| 522 | DelaySlotInfo *dinfo = |
| 523 | new DelaySlotInfo(bn, getInstrInfo().getNumDelaySlots(bn->getOpCode())); |
| 524 | return delaySlotInfoForBranches[bn] = dinfo; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | private: |
Chris Lattner | af50d00 | 2002-04-09 05:45:58 +0000 | [diff] [blame] | 528 | SchedulingManager(); // DISABLED: DO NOT IMPLEMENT |
| 529 | void updateEarliestStartTimes(const SchedGraphNode* node, cycles_t schedTime); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | |
| 533 | /*ctor*/ |
| 534 | SchedulingManager::SchedulingManager(const TargetMachine& target, |
| 535 | const SchedGraph* graph, |
| 536 | SchedPriorities& _schedPrio) |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 537 | : nslots(target.getSchedInfo().getMaxNumIssueTotal()), |
| 538 | schedInfo(target.getSchedInfo()), |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 539 | schedPrio(_schedPrio), |
| 540 | isched(nslots, graph->getNumNodes()), |
| 541 | totalInstrCount(graph->getNumNodes() - 2), |
| 542 | nextEarliestIssueTime(0), |
| 543 | choicesForSlot(nslots), |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 544 | numInClass(target.getSchedInfo().getNumSchedClasses(), 0), // set all to 0 |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 545 | nextEarliestStartTime(target.getInstrInfo().getNumRealOpCodes(), |
| 546 | (cycles_t) 0) // set all to 0 |
| 547 | { |
| 548 | updateTime(0); |
| 549 | |
| 550 | // Note that an upper bound on #choices for each slot is = nslots since |
| 551 | // we use this vector to hold a feasible set of instructions, and more |
| 552 | // would be infeasible. Reserve that much memory since it is probably small. |
| 553 | for (unsigned int i=0; i < nslots; i++) |
| 554 | choicesForSlot[i].resize(nslots); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | void |
| 559 | SchedulingManager::updateEarliestStartTimes(const SchedGraphNode* node, |
| 560 | cycles_t schedTime) |
| 561 | { |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 562 | if (schedInfo.numBubblesAfter(node->getOpCode()) > 0) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 563 | { // Update next earliest time before which *nothing* can issue. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 564 | nextEarliestIssueTime = std::max(nextEarliestIssueTime, |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 565 | curTime + 1 + schedInfo.numBubblesAfter(node->getOpCode())); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Vikram S. Adve | 1632e88 | 2002-10-13 00:40:37 +0000 | [diff] [blame] | 568 | const std::vector<MachineOpCode>& |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 569 | conflictVec = schedInfo.getConflictList(node->getOpCode()); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 570 | |
Vikram S. Adve | 1632e88 | 2002-10-13 00:40:37 +0000 | [diff] [blame] | 571 | for (unsigned i=0; i < conflictVec.size(); i++) |
| 572 | { |
| 573 | MachineOpCode toOp = conflictVec[i]; |
| 574 | cycles_t est=schedTime + schedInfo.getMinIssueGap(node->getOpCode(),toOp); |
| 575 | assert(toOp < (int) nextEarliestStartTime.size()); |
| 576 | if (nextEarliestStartTime[toOp] < est) |
| 577 | nextEarliestStartTime[toOp] = est; |
| 578 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 581 | //************************* Internal Functions *****************************/ |
| 582 | |
| 583 | |
| 584 | static void |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 585 | AssignInstructionsToSlots(class SchedulingManager& S, unsigned maxIssue) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 586 | { |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 587 | // find the slot to start from, in the current cycle |
| 588 | unsigned int startSlot = 0; |
| 589 | cycles_t curTime = S.getTime(); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 590 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 591 | assert(maxIssue > 0 && maxIssue <= S.nslots - startSlot); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 592 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 593 | // If only one instruction can be issued, do so. |
| 594 | if (maxIssue == 1) |
| 595 | for (unsigned s=startSlot; s < S.nslots; s++) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 596 | if (S.getChoicesForSlot(s).size() > 0) { |
| 597 | // found the one instruction |
| 598 | S.scheduleInstr(*S.getChoicesForSlot(s).begin(), s, curTime); |
| 599 | return; |
| 600 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 601 | |
| 602 | // Otherwise, choose from the choices for each slot |
| 603 | // |
| 604 | InstrGroup* igroup = S.isched.getIGroup(S.getTime()); |
| 605 | assert(igroup != NULL && "Group creation failed?"); |
| 606 | |
| 607 | // Find a slot that has only a single choice, and take it. |
| 608 | // If all slots have 0 or multiple choices, pick the first slot with |
| 609 | // choices and use its last instruction (just to avoid shifting the vector). |
| 610 | unsigned numIssued; |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 611 | for (numIssued = 0; numIssued < maxIssue; numIssued++) { |
| 612 | int chosenSlot = -1; |
| 613 | for (unsigned s=startSlot; s < S.nslots; s++) |
| 614 | if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() == 1) { |
| 615 | chosenSlot = (int) s; |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | if (chosenSlot == -1) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 620 | for (unsigned s=startSlot; s < S.nslots; s++) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 621 | if ((*igroup)[s] == NULL && S.getChoicesForSlot(s).size() > 0) { |
| 622 | chosenSlot = (int) s; |
| 623 | break; |
| 624 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 625 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 626 | if (chosenSlot != -1) { |
| 627 | // Insert the chosen instr in the chosen slot and |
| 628 | // erase it from all slots. |
| 629 | const SchedGraphNode* node= *S.getChoicesForSlot(chosenSlot).begin(); |
| 630 | S.scheduleInstr(node, chosenSlot, curTime); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 631 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 632 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 633 | |
| 634 | assert(numIssued > 0 && "Should not happen when maxIssue > 0!"); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
| 638 | // |
| 639 | // For now, just assume we are scheduling within a single basic block. |
| 640 | // Get the machine instruction vector for the basic block and clear it, |
| 641 | // then append instructions in scheduled order. |
| 642 | // Also, re-insert the dummy PHI instructions that were at the beginning |
| 643 | // of the basic block, since they are not part of the schedule. |
| 644 | // |
| 645 | static void |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 646 | RecordSchedule(MachineBasicBlock &MBB, const SchedulingManager& S) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 647 | { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 648 | const TargetInstrInfo& mii = S.schedInfo.getInstrInfo(); |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 649 | |
| 650 | #ifndef NDEBUG |
| 651 | // Lets make sure we didn't lose any instructions, except possibly |
| 652 | // some NOPs from delay slots. Also, PHIs are not included in the schedule. |
| 653 | unsigned numInstr = 0; |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 654 | for (MachineBasicBlock::iterator I=MBB.begin(); I != MBB.end(); ++I) |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 655 | if (! mii.isNop((*I)->getOpCode()) && |
| 656 | ! mii.isDummyPhiInstr((*I)->getOpCode())) |
| 657 | ++numInstr; |
| 658 | assert(S.isched.getNumInstructions() >= numInstr && |
| 659 | "Lost some non-NOP instructions during scheduling!"); |
| 660 | #endif |
| 661 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 662 | if (S.isched.getNumInstructions() == 0) |
| 663 | return; // empty basic block! |
| 664 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 665 | // First find the dummy instructions at the start of the basic block |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 666 | MachineBasicBlock::iterator I = MBB.begin(); |
| 667 | for ( ; I != MBB.end(); ++I) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 668 | if (! mii.isDummyPhiInstr((*I)->getOpCode())) |
| 669 | break; |
| 670 | |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 671 | // Erase all except the dummy PHI instructions from MBB, and |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 672 | // pre-allocate create space for the ones we will put back in. |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 673 | MBB.erase(I, MBB.end()); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 674 | |
| 675 | InstrSchedule::const_iterator NIend = S.isched.end(); |
| 676 | for (InstrSchedule::const_iterator NI = S.isched.begin(); NI != NIend; ++NI) |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 677 | MBB.push_back(const_cast<MachineInstr*>((*NI)->getMachineInstr())); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 681 | |
| 682 | static void |
| 683 | MarkSuccessorsReady(SchedulingManager& S, const SchedGraphNode* node) |
| 684 | { |
| 685 | // Check if any successors are now ready that were not already marked |
| 686 | // ready before, and that have not yet been scheduled. |
| 687 | // |
| 688 | for (sg_succ_const_iterator SI = succ_begin(node); SI !=succ_end(node); ++SI) |
| 689 | if (! (*SI)->isDummyNode() |
| 690 | && ! S.isScheduled(*SI) |
| 691 | && ! S.schedPrio.nodeIsReady(*SI)) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 692 | { |
| 693 | // successor not scheduled and not marked ready; check *its* preds. |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 694 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 695 | bool succIsReady = true; |
| 696 | for (sg_pred_const_iterator P=pred_begin(*SI); P != pred_end(*SI); ++P) |
| 697 | if (! (*P)->isDummyNode() && ! S.isScheduled(*P)) { |
| 698 | succIsReady = false; |
| 699 | break; |
| 700 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 701 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 702 | if (succIsReady) // add the successor to the ready list |
| 703 | S.schedPrio.insertReady(*SI); |
| 704 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | |
| 708 | // Choose up to `nslots' FEASIBLE instructions and assign each |
| 709 | // instruction to all possible slots that do not violate feasibility. |
| 710 | // FEASIBLE means it should be guaranteed that the set |
| 711 | // of chosen instructions can be issued in a single group. |
| 712 | // |
| 713 | // Return value: |
| 714 | // maxIssue : total number of feasible instructions |
| 715 | // S.choicesForSlot[i=0..nslots] : set of instructions feasible in slot i |
| 716 | // |
| 717 | static unsigned |
| 718 | FindSlotChoices(SchedulingManager& S, |
| 719 | DelaySlotInfo*& getDelaySlotInfo) |
| 720 | { |
| 721 | // initialize result vectors to empty |
| 722 | S.resetChoices(); |
| 723 | |
| 724 | // find the slot to start from, in the current cycle |
| 725 | unsigned int startSlot = 0; |
| 726 | InstrGroup* igroup = S.isched.getIGroup(S.getTime()); |
| 727 | for (int s = S.nslots - 1; s >= 0; s--) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 728 | if ((*igroup)[s] != NULL) { |
| 729 | startSlot = s+1; |
| 730 | break; |
| 731 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 732 | |
| 733 | // Make sure we pick at most one instruction that would break the group. |
| 734 | // Also, if we do pick one, remember which it was. |
| 735 | unsigned int indexForBreakingNode = S.nslots; |
| 736 | unsigned int indexForDelayedInstr = S.nslots; |
| 737 | DelaySlotInfo* delaySlotInfo = NULL; |
| 738 | |
| 739 | getDelaySlotInfo = NULL; |
| 740 | |
| 741 | // Choose instructions in order of priority. |
| 742 | // Add choices to the choice vector in the SchedulingManager class as |
| 743 | // we choose them so that subsequent choices will be correctly tested |
| 744 | // for feasibility, w.r.t. higher priority choices for the same cycle. |
| 745 | // |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 746 | while (S.getNumChoices() < S.nslots - startSlot) { |
| 747 | const SchedGraphNode* nextNode=S.schedPrio.getNextHighest(S,S.getTime()); |
| 748 | if (nextNode == NULL) |
| 749 | break; // no more instructions for this cycle |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 750 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 751 | if (S.getInstrInfo().getNumDelaySlots(nextNode->getOpCode()) > 0) { |
| 752 | delaySlotInfo = S.getDelaySlotInfoForInstr(nextNode); |
| 753 | if (delaySlotInfo != NULL) { |
| 754 | if (indexForBreakingNode < S.nslots) |
| 755 | // cannot issue a delayed instr in the same cycle as one |
| 756 | // that breaks the issue group or as another delayed instr |
| 757 | nextNode = NULL; |
| 758 | else |
| 759 | indexForDelayedInstr = S.getNumChoices(); |
| 760 | } |
| 761 | } else if (S.schedInfo.breaksIssueGroup(nextNode->getOpCode())) { |
| 762 | if (indexForBreakingNode < S.nslots) |
| 763 | // have a breaking instruction already so throw this one away |
| 764 | nextNode = NULL; |
| 765 | else |
| 766 | indexForBreakingNode = S.getNumChoices(); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 767 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 768 | |
| 769 | if (nextNode != NULL) { |
| 770 | S.addChoice(nextNode); |
| 771 | |
| 772 | if (S.schedInfo.isSingleIssue(nextNode->getOpCode())) { |
| 773 | assert(S.getNumChoices() == 1 && |
| 774 | "Prioritizer returned invalid instr for this cycle!"); |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (indexForDelayedInstr < S.nslots) |
| 780 | break; // leave the rest for delay slots |
| 781 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 782 | |
| 783 | assert(S.getNumChoices() <= S.nslots); |
| 784 | assert(! (indexForDelayedInstr < S.nslots && |
| 785 | indexForBreakingNode < S.nslots) && "Cannot have both in a cycle"); |
| 786 | |
| 787 | // Assign each chosen instruction to all possible slots for that instr. |
| 788 | // But if only one instruction was chosen, put it only in the first |
| 789 | // feasible slot; no more analysis will be needed. |
| 790 | // |
| 791 | if (indexForDelayedInstr >= S.nslots && |
| 792 | indexForBreakingNode >= S.nslots) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 793 | { // No instructions that break the issue group or that have delay slots. |
| 794 | // This is the common case, so handle it separately for efficiency. |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 795 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 796 | if (S.getNumChoices() == 1) { |
| 797 | MachineOpCode opCode = S.getChoice(0)->getOpCode(); |
| 798 | unsigned int s; |
| 799 | for (s=startSlot; s < S.nslots; s++) |
| 800 | if (S.schedInfo.instrCanUseSlot(opCode, s)) |
| 801 | break; |
| 802 | assert(s < S.nslots && "No feasible slot for this opCode?"); |
| 803 | S.addChoiceToSlot(s, S.getChoice(0)); |
| 804 | } else { |
| 805 | for (unsigned i=0; i < S.getNumChoices(); i++) { |
| 806 | MachineOpCode opCode = S.getChoice(i)->getOpCode(); |
| 807 | for (unsigned int s=startSlot; s < S.nslots; s++) |
| 808 | if (S.schedInfo.instrCanUseSlot(opCode, s)) |
| 809 | S.addChoiceToSlot(s, S.getChoice(i)); |
| 810 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 811 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 812 | } else if (indexForDelayedInstr < S.nslots) { |
| 813 | // There is an instruction that needs delay slots. |
| 814 | // Try to assign that instruction to a higher slot than any other |
| 815 | // instructions in the group, so that its delay slots can go |
| 816 | // right after it. |
| 817 | // |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 818 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 819 | assert(indexForDelayedInstr == S.getNumChoices() - 1 && |
| 820 | "Instruction with delay slots should be last choice!"); |
| 821 | assert(delaySlotInfo != NULL && "No delay slot info for instr?"); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 822 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 823 | const SchedGraphNode* delayedNode = S.getChoice(indexForDelayedInstr); |
| 824 | MachineOpCode delayOpCode = delayedNode->getOpCode(); |
| 825 | unsigned ndelays= S.getInstrInfo().getNumDelaySlots(delayOpCode); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 826 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 827 | unsigned delayedNodeSlot = S.nslots; |
| 828 | int highestSlotUsed; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 829 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 830 | // Find the last possible slot for the delayed instruction that leaves |
| 831 | // at least `d' slots vacant after it (d = #delay slots) |
| 832 | for (int s = S.nslots-ndelays-1; s >= (int) startSlot; s--) |
| 833 | if (S.schedInfo.instrCanUseSlot(delayOpCode, s)) { |
| 834 | delayedNodeSlot = s; |
| 835 | break; |
| 836 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 837 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 838 | highestSlotUsed = -1; |
| 839 | for (unsigned i=0; i < S.getNumChoices() - 1; i++) { |
| 840 | // Try to assign every other instruction to a lower numbered |
| 841 | // slot than delayedNodeSlot. |
| 842 | MachineOpCode opCode =S.getChoice(i)->getOpCode(); |
| 843 | bool noSlotFound = true; |
| 844 | unsigned int s; |
| 845 | for (s=startSlot; s < delayedNodeSlot; s++) |
| 846 | if (S.schedInfo.instrCanUseSlot(opCode, s)) { |
| 847 | S.addChoiceToSlot(s, S.getChoice(i)); |
| 848 | noSlotFound = false; |
| 849 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 850 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 851 | // No slot before `delayedNodeSlot' was found for this opCode |
| 852 | // Use a later slot, and allow some delay slots to fall in |
| 853 | // the next cycle. |
| 854 | if (noSlotFound) |
| 855 | for ( ; s < S.nslots; s++) |
| 856 | if (S.schedInfo.instrCanUseSlot(opCode, s)) { |
| 857 | S.addChoiceToSlot(s, S.getChoice(i)); |
| 858 | break; |
| 859 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 860 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 861 | assert(s < S.nslots && "No feasible slot for instruction?"); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 862 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 863 | highestSlotUsed = std::max(highestSlotUsed, (int) s); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 864 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 865 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 866 | assert(highestSlotUsed <= (int) S.nslots-1 && "Invalid slot used?"); |
| 867 | |
| 868 | // We will put the delayed node in the first slot after the |
| 869 | // highest slot used. But we just mark that for now, and |
| 870 | // schedule it separately because we want to schedule the delay |
| 871 | // slots for the node at the same time. |
| 872 | cycles_t dcycle = S.getTime(); |
| 873 | unsigned int dslot = highestSlotUsed + 1; |
| 874 | if (dslot == S.nslots) { |
| 875 | dslot = 0; |
| 876 | ++dcycle; |
| 877 | } |
| 878 | delaySlotInfo->recordChosenSlot(dcycle, dslot); |
| 879 | getDelaySlotInfo = delaySlotInfo; |
| 880 | } else { |
| 881 | // There is an instruction that breaks the issue group. |
| 882 | // For such an instruction, assign to the last possible slot in |
| 883 | // the current group, and then don't assign any other instructions |
| 884 | // to later slots. |
| 885 | assert(indexForBreakingNode < S.nslots); |
| 886 | const SchedGraphNode* breakingNode=S.getChoice(indexForBreakingNode); |
| 887 | unsigned breakingSlot = INT_MAX; |
| 888 | unsigned int nslotsToUse = S.nslots; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 889 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 890 | // Find the last possible slot for this instruction. |
| 891 | for (int s = S.nslots-1; s >= (int) startSlot; s--) |
| 892 | if (S.schedInfo.instrCanUseSlot(breakingNode->getOpCode(), s)) { |
| 893 | breakingSlot = s; |
| 894 | break; |
| 895 | } |
| 896 | assert(breakingSlot < S.nslots && |
| 897 | "No feasible slot for `breakingNode'?"); |
| 898 | |
| 899 | // Higher priority instructions than the one that breaks the group: |
| 900 | // These can be assigned to all slots, but will be assigned only |
| 901 | // to earlier slots if possible. |
| 902 | for (unsigned i=0; |
| 903 | i < S.getNumChoices() && i < indexForBreakingNode; i++) |
| 904 | { |
| 905 | MachineOpCode opCode =S.getChoice(i)->getOpCode(); |
| 906 | |
| 907 | // If a higher priority instruction cannot be assigned to |
| 908 | // any earlier slots, don't schedule the breaking instruction. |
| 909 | // |
| 910 | bool foundLowerSlot = false; |
| 911 | nslotsToUse = S.nslots; // May be modified in the loop |
| 912 | for (unsigned int s=startSlot; s < nslotsToUse; s++) |
| 913 | if (S.schedInfo.instrCanUseSlot(opCode, s)) { |
| 914 | if (breakingSlot < S.nslots && s < breakingSlot) { |
| 915 | foundLowerSlot = true; |
| 916 | nslotsToUse = breakingSlot; // RESETS LOOP UPPER BOUND! |
| 917 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 918 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 919 | S.addChoiceToSlot(s, S.getChoice(i)); |
| 920 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 921 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 922 | if (!foundLowerSlot) |
| 923 | breakingSlot = INT_MAX; // disable breaking instr |
| 924 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 925 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 926 | // Assign the breaking instruction (if any) to a single slot |
| 927 | // Otherwise, just ignore the instruction. It will simply be |
| 928 | // scheduled in a later cycle. |
| 929 | if (breakingSlot < S.nslots) { |
| 930 | S.addChoiceToSlot(breakingSlot, breakingNode); |
| 931 | nslotsToUse = breakingSlot; |
| 932 | } else |
| 933 | nslotsToUse = S.nslots; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 934 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 935 | // For lower priority instructions than the one that breaks the |
| 936 | // group, only assign them to slots lower than the breaking slot. |
| 937 | // Otherwise, just ignore the instruction. |
| 938 | for (unsigned i=indexForBreakingNode+1; i < S.getNumChoices(); i++) { |
| 939 | MachineOpCode opCode = S.getChoice(i)->getOpCode(); |
| 940 | for (unsigned int s=startSlot; s < nslotsToUse; s++) |
| 941 | if (S.schedInfo.instrCanUseSlot(opCode, s)) |
| 942 | S.addChoiceToSlot(s, S.getChoice(i)); |
| 943 | } |
| 944 | } // endif (no delay slots and no breaking slots) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 945 | |
| 946 | return S.getNumChoices(); |
| 947 | } |
| 948 | |
| 949 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 950 | static unsigned |
| 951 | ChooseOneGroup(SchedulingManager& S) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 952 | { |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 953 | assert(S.schedPrio.getNumReady() > 0 |
| 954 | && "Don't get here without ready instructions."); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 955 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 956 | cycles_t firstCycle = S.getTime(); |
| 957 | DelaySlotInfo* getDelaySlotInfo = NULL; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 958 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 959 | // Choose up to `nslots' feasible instructions and their possible slots. |
| 960 | unsigned numIssued = FindSlotChoices(S, getDelaySlotInfo); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 961 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 962 | while (numIssued == 0) { |
| 963 | S.updateTime(S.getTime()+1); |
| 964 | numIssued = FindSlotChoices(S, getDelaySlotInfo); |
| 965 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 966 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 967 | AssignInstructionsToSlots(S, numIssued); |
| 968 | |
| 969 | if (getDelaySlotInfo != NULL) |
| 970 | numIssued += getDelaySlotInfo->scheduleDelayedNode(S); |
| 971 | |
| 972 | // Print trace of scheduled instructions before newly ready ones |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 973 | if (SchedDebugLevel >= Sched_PrintSchedTrace) { |
| 974 | for (cycles_t c = firstCycle; c <= S.getTime(); c++) { |
| 975 | std::cerr << " Cycle " << (long)c <<" : Scheduled instructions:\n"; |
| 976 | const InstrGroup* igroup = S.isched.getIGroup(c); |
| 977 | for (unsigned int s=0; s < S.nslots; s++) { |
| 978 | std::cerr << " "; |
| 979 | if ((*igroup)[s] != NULL) |
| 980 | std::cerr << * ((*igroup)[s])->getMachineInstr() << "\n"; |
| 981 | else |
| 982 | std::cerr << "<none>\n"; |
| 983 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 984 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 985 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 986 | |
| 987 | return numIssued; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 991 | static void |
| 992 | ForwardListSchedule(SchedulingManager& S) |
| 993 | { |
| 994 | unsigned N; |
| 995 | const SchedGraphNode* node; |
| 996 | |
| 997 | S.schedPrio.initialize(); |
| 998 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 999 | while ((N = S.schedPrio.getNumReady()) > 0) { |
| 1000 | cycles_t nextCycle = S.getTime(); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1001 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1002 | // Choose one group of instructions for a cycle, plus any delay slot |
| 1003 | // instructions (which may overflow into successive cycles). |
| 1004 | // This will advance S.getTime() to the last cycle in which |
| 1005 | // instructions are actually issued. |
| 1006 | // |
| 1007 | unsigned numIssued = ChooseOneGroup(S); |
| 1008 | assert(numIssued > 0 && "Deadlock in list scheduling algorithm?"); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1009 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1010 | // Notify the priority manager of scheduled instructions and mark |
| 1011 | // any successors that may now be ready |
| 1012 | // |
| 1013 | for (cycles_t c = nextCycle; c <= S.getTime(); c++) { |
| 1014 | const InstrGroup* igroup = S.isched.getIGroup(c); |
| 1015 | for (unsigned int s=0; s < S.nslots; s++) |
| 1016 | if ((node = (*igroup)[s]) != NULL) { |
| 1017 | S.schedPrio.issuedReadyNodeAt(S.getTime(), node); |
| 1018 | MarkSuccessorsReady(S, node); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1019 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1020 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Move to the next the next earliest cycle for which |
| 1023 | // an instruction can be issued, or the next earliest in which |
| 1024 | // one will be ready, or to the next cycle, whichever is latest. |
| 1025 | // |
| 1026 | S.updateTime(std::max(S.getTime() + 1, |
| 1027 | std::max(S.getEarliestIssueTime(), |
| 1028 | S.schedPrio.getEarliestReadyTime()))); |
| 1029 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1032 | |
| 1033 | //--------------------------------------------------------------------- |
| 1034 | // Code for filling delay slots for delayed terminator instructions |
| 1035 | // (e.g., BRANCH and RETURN). Delay slots for non-terminator |
| 1036 | // instructions (e.g., CALL) are not handled here because they almost |
| 1037 | // always can be filled with instructions from the call sequence code |
| 1038 | // before a call. That's preferable because we incur many tradeoffs here |
| 1039 | // when we cannot find single-cycle instructions that can be reordered. |
| 1040 | //---------------------------------------------------------------------- |
| 1041 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1042 | static bool |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1043 | NodeCanFillDelaySlot(const SchedulingManager& S, |
| 1044 | const SchedGraphNode* node, |
| 1045 | const SchedGraphNode* brNode, |
| 1046 | bool nodeIsPredecessor) |
| 1047 | { |
| 1048 | assert(! node->isDummyNode()); |
| 1049 | |
| 1050 | // don't put a branch in the delay slot of another branch |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 1051 | if (S.getInstrInfo().isBranch(node->getOpCode())) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1052 | return false; |
| 1053 | |
| 1054 | // don't put a single-issue instruction in the delay slot of a branch |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 1055 | if (S.schedInfo.isSingleIssue(node->getOpCode())) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1056 | return false; |
| 1057 | |
| 1058 | // don't put a load-use dependence in the delay slot of a branch |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 1059 | const TargetInstrInfo& mii = S.getInstrInfo(); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1060 | |
| 1061 | for (SchedGraphNode::const_iterator EI = node->beginInEdges(); |
| 1062 | EI != node->endInEdges(); ++EI) |
Tanya Lattner | b6489f3 | 2003-08-25 22:42:20 +0000 | [diff] [blame] | 1063 | if (! ((SchedGraphNode*)(*EI)->getSrc())->isDummyNode() |
| 1064 | && mii.isLoad(((SchedGraphNode*)(*EI)->getSrc())->getOpCode()) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1065 | && (*EI)->getDepType() == SchedGraphEdge::CtrlDep) |
| 1066 | return false; |
| 1067 | |
| 1068 | // for now, don't put an instruction that does not have operand |
| 1069 | // interlocks in the delay slot of a branch |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 1070 | if (! S.getInstrInfo().hasOperandInterlock(node->getOpCode())) |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1071 | return false; |
| 1072 | |
Misha Brukman | 6eba07a | 2003-09-17 21:34:23 +0000 | [diff] [blame] | 1073 | // Finally, if the instruction precedes the branch, we make sure the |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1074 | // instruction can be reordered relative to the branch. We simply check |
| 1075 | // if the instr. has only 1 outgoing edge, viz., a CD edge to the branch. |
| 1076 | // |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1077 | if (nodeIsPredecessor) { |
| 1078 | bool onlyCDEdgeToBranch = true; |
| 1079 | for (SchedGraphNode::const_iterator OEI = node->beginOutEdges(); |
| 1080 | OEI != node->endOutEdges(); ++OEI) |
Tanya Lattner | b6489f3 | 2003-08-25 22:42:20 +0000 | [diff] [blame] | 1081 | if (! ((SchedGraphNode*)(*OEI)->getSink())->isDummyNode() |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1082 | && ((*OEI)->getSink() != brNode |
| 1083 | || (*OEI)->getDepType() != SchedGraphEdge::CtrlDep)) |
| 1084 | { |
| 1085 | onlyCDEdgeToBranch = false; |
| 1086 | break; |
| 1087 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1088 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1089 | if (!onlyCDEdgeToBranch) |
| 1090 | return false; |
| 1091 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1092 | |
| 1093 | return true; |
| 1094 | } |
| 1095 | |
| 1096 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1097 | static void |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1098 | MarkNodeForDelaySlot(SchedulingManager& S, |
Vikram S. Adve | f0ba280 | 2001-09-18 12:51:38 +0000 | [diff] [blame] | 1099 | SchedGraph* graph, |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1100 | SchedGraphNode* node, |
| 1101 | const SchedGraphNode* brNode, |
| 1102 | bool nodeIsPredecessor) |
| 1103 | { |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1104 | if (nodeIsPredecessor) { |
Misha Brukman | 6eba07a | 2003-09-17 21:34:23 +0000 | [diff] [blame] | 1105 | // If node is in the same basic block (i.e., precedes brNode), |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1106 | // remove it and all its incident edges from the graph. Make sure we |
| 1107 | // add dummy edges for pred/succ nodes that become entry/exit nodes. |
| 1108 | graph->eraseIncidentEdges(node, /*addDummyEdges*/ true); |
| 1109 | } else { |
| 1110 | // If the node was from a target block, add the node to the graph |
| 1111 | // and add a CD edge from brNode to node. |
| 1112 | assert(0 && "NOT IMPLEMENTED YET"); |
| 1113 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1114 | |
| 1115 | DelaySlotInfo* dinfo = S.getDelaySlotInfoForInstr(brNode, /*create*/ true); |
| 1116 | dinfo->addDelayNode(node); |
| 1117 | } |
| 1118 | |
| 1119 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1120 | void |
| 1121 | FindUsefulInstructionsForDelaySlots(SchedulingManager& S, |
| 1122 | SchedGraphNode* brNode, |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1123 | std::vector<SchedGraphNode*>& sdelayNodeVec) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1124 | { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 1125 | const TargetInstrInfo& mii = S.getInstrInfo(); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1126 | unsigned ndelays = |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 1127 | mii.getNumDelaySlots(brNode->getOpCode()); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (ndelays == 0) |
| 1130 | return; |
| 1131 | |
| 1132 | sdelayNodeVec.reserve(ndelays); |
| 1133 | |
| 1134 | // Use a separate vector to hold the feasible multi-cycle nodes. |
| 1135 | // These will be used if not enough single-cycle nodes are found. |
| 1136 | // |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1137 | std::vector<SchedGraphNode*> mdelayNodeVec; |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1138 | |
| 1139 | for (sg_pred_iterator P = pred_begin(brNode); |
| 1140 | P != pred_end(brNode) && sdelayNodeVec.size() < ndelays; ++P) |
| 1141 | if (! (*P)->isDummyNode() && |
Vikram S. Adve | fb1a6c8 | 2001-11-09 02:14:20 +0000 | [diff] [blame] | 1142 | ! mii.isNop((*P)->getOpCode()) && |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1143 | NodeCanFillDelaySlot(S, *P, brNode, /*pred*/ true)) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1144 | { |
| 1145 | if (mii.maxLatency((*P)->getOpCode()) > 1) |
| 1146 | mdelayNodeVec.push_back(*P); |
| 1147 | else |
| 1148 | sdelayNodeVec.push_back(*P); |
| 1149 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1150 | |
| 1151 | // If not enough single-cycle instructions were found, select the |
| 1152 | // lowest-latency multi-cycle instructions and use them. |
| 1153 | // Note that this is the most efficient code when only 1 (or even 2) |
| 1154 | // values need to be selected. |
| 1155 | // |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1156 | while (sdelayNodeVec.size() < ndelays && mdelayNodeVec.size() > 0) { |
| 1157 | unsigned lmin = |
| 1158 | mii.maxLatency(mdelayNodeVec[0]->getOpCode()); |
| 1159 | unsigned minIndex = 0; |
| 1160 | for (unsigned i=1; i < mdelayNodeVec.size(); i++) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1161 | { |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1162 | unsigned li = |
| 1163 | mii.maxLatency(mdelayNodeVec[i]->getOpCode()); |
| 1164 | if (lmin >= li) |
| 1165 | { |
| 1166 | lmin = li; |
| 1167 | minIndex = i; |
| 1168 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1169 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1170 | sdelayNodeVec.push_back(mdelayNodeVec[minIndex]); |
| 1171 | if (sdelayNodeVec.size() < ndelays) // avoid the last erase! |
| 1172 | mdelayNodeVec.erase(mdelayNodeVec.begin() + minIndex); |
| 1173 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | // Remove the NOPs currently in delay slots from the graph. |
| 1178 | // Mark instructions specified in sdelayNodeVec to replace them. |
| 1179 | // If not enough useful instructions were found, mark the NOPs to be used |
| 1180 | // for filling delay slots, otherwise, otherwise just discard them. |
| 1181 | // |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1182 | static void ReplaceNopsWithUsefulInstr(SchedulingManager& S, |
| 1183 | SchedGraphNode* node, |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1184 | // FIXME: passing vector BY VALUE!!! |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1185 | std::vector<SchedGraphNode*> sdelayNodeVec, |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1186 | SchedGraph* graph) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1187 | { |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1188 | std::vector<SchedGraphNode*> nopNodeVec; // this will hold unused NOPs |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 1189 | const TargetInstrInfo& mii = S.getInstrInfo(); |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1190 | const MachineInstr* brInstr = node->getMachineInstr(); |
| 1191 | unsigned ndelays= mii.getNumDelaySlots(brInstr->getOpCode()); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1192 | assert(ndelays > 0 && "Unnecessary call to replace NOPs"); |
| 1193 | |
| 1194 | // Remove the NOPs currently in delay slots from the graph. |
| 1195 | // If not enough useful instructions were found, use the NOPs to |
| 1196 | // fill delay slots, otherwise, just discard them. |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1197 | // |
Vikram S. Adve | af00d48 | 2001-11-12 14:18:01 +0000 | [diff] [blame] | 1198 | unsigned int firstDelaySlotIdx = node->getOrigIndexInBB() + 1; |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1199 | MachineBasicBlock& MBB = node->getMachineBasicBlock(); |
| 1200 | assert(MBB[firstDelaySlotIdx - 1] == brInstr && |
Vikram S. Adve | af00d48 | 2001-11-12 14:18:01 +0000 | [diff] [blame] | 1201 | "Incorrect instr. index in basic block for brInstr"); |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1202 | |
| 1203 | // First find all useful instructions already in the delay slots |
| 1204 | // and USE THEM. We'll throw away the unused alternatives below |
| 1205 | // |
| 1206 | for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i) |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1207 | if (! mii.isNop(MBB[i]->getOpCode())) |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1208 | sdelayNodeVec.insert(sdelayNodeVec.begin(), |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1209 | graph->getGraphNodeForInstr(MBB[i])); |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Then find the NOPs and keep only as many as are needed. |
| 1212 | // Put the rest in nopNodeVec to be deleted. |
| 1213 | for (unsigned i=firstDelaySlotIdx; i < firstDelaySlotIdx + ndelays; ++i) |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1214 | if (mii.isNop(MBB[i]->getOpCode())) |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1215 | if (sdelayNodeVec.size() < ndelays) |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1216 | sdelayNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i])); |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1217 | else { |
| 1218 | nopNodeVec.push_back(graph->getGraphNodeForInstr(MBB[i])); |
Mehwish Nagda | e95ce74 | 2002-07-25 17:31:05 +0000 | [diff] [blame] | 1219 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1220 | //remove the MI from the Machine Code For Instruction |
Chris Lattner | 9cdaa63 | 2003-07-26 23:23:41 +0000 | [diff] [blame] | 1221 | const TerminatorInst *TI = MBB.getBasicBlock()->getTerminator(); |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1222 | MachineCodeForInstruction& llvmMvec = |
Chris Lattner | 9cdaa63 | 2003-07-26 23:23:41 +0000 | [diff] [blame] | 1223 | MachineCodeForInstruction::get((const Instruction *)TI); |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1224 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1225 | for(MachineCodeForInstruction::iterator mciI=llvmMvec.begin(), |
| 1226 | mciE=llvmMvec.end(); mciI!=mciE; ++mciI){ |
| 1227 | if (*mciI==MBB[i]) |
| 1228 | llvmMvec.erase(mciI); |
| 1229 | } |
| 1230 | } |
Mehwish Nagda | e95ce74 | 2002-07-25 17:31:05 +0000 | [diff] [blame] | 1231 | |
Vikram S. Adve | fb8c053 | 2001-10-22 13:49:27 +0000 | [diff] [blame] | 1232 | assert(sdelayNodeVec.size() >= ndelays); |
| 1233 | |
| 1234 | // If some delay slots were already filled, throw away that many new choices |
| 1235 | if (sdelayNodeVec.size() > ndelays) |
| 1236 | sdelayNodeVec.resize(ndelays); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Mark the nodes chosen for delay slots. This removes them from the graph. |
| 1239 | for (unsigned i=0; i < sdelayNodeVec.size(); i++) |
| 1240 | MarkNodeForDelaySlot(S, graph, sdelayNodeVec[i], node, true); |
| 1241 | |
| 1242 | // And remove the unused NOPs from the graph. |
| 1243 | for (unsigned i=0; i < nopNodeVec.size(); i++) |
| 1244 | graph->eraseIncidentEdges(nopNodeVec[i], /*addDummyEdges*/ true); |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | // For all delayed instructions, choose instructions to put in the delay |
| 1249 | // slots and pull those out of the graph. Mark them for the delay slots |
| 1250 | // in the DelaySlotInfo object for that graph node. If no useful work |
| 1251 | // is found for a delay slot, use the NOP that is currently in that slot. |
| 1252 | // |
| 1253 | // We try to fill the delay slots with useful work for all instructions |
Vikram S. Adve | 6db77c5 | 2001-10-10 20:58:11 +0000 | [diff] [blame] | 1254 | // EXCEPT CALLS AND RETURNS. |
| 1255 | // For CALLs and RETURNs, it is nearly always possible to use one of the |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1256 | // call sequence instrs and putting anything else in the delay slot could be |
Vikram S. Adve | 6db77c5 | 2001-10-10 20:58:11 +0000 | [diff] [blame] | 1257 | // suboptimal. Also, it complicates generating the calling sequence code in |
| 1258 | // regalloc. |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1259 | // |
| 1260 | static void |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1261 | ChooseInstructionsForDelaySlots(SchedulingManager& S, MachineBasicBlock &MBB, |
Chris Lattner | 3462cae | 2002-02-03 07:28:30 +0000 | [diff] [blame] | 1262 | SchedGraph *graph) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1263 | { |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 1264 | const TargetInstrInfo& mii = S.getInstrInfo(); |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1265 | |
| 1266 | Instruction *termInstr = (Instruction*)MBB.getBasicBlock()->getTerminator(); |
Chris Lattner | 3462cae | 2002-02-03 07:28:30 +0000 | [diff] [blame] | 1267 | MachineCodeForInstruction &termMvec=MachineCodeForInstruction::get(termInstr); |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1268 | std::vector<SchedGraphNode*> delayNodeVec; |
Vikram S. Adve | 6db77c5 | 2001-10-10 20:58:11 +0000 | [diff] [blame] | 1269 | const MachineInstr* brInstr = NULL; |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1270 | |
Vikram S. Adve | bed4eff | 2003-09-16 05:55:15 +0000 | [diff] [blame] | 1271 | if (EnableFillingDelaySlots && |
| 1272 | termInstr->getOpcode() != Instruction::Ret) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1273 | { |
| 1274 | // To find instructions that need delay slots without searching the full |
| 1275 | // machine code, we assume that the only delayed instructions are CALLs |
| 1276 | // or instructions generated for the terminator inst. |
| 1277 | // Find the first branch instr in the sequence of machine instrs for term |
| 1278 | // |
| 1279 | unsigned first = 0; |
| 1280 | while (first < termMvec.size() && |
| 1281 | ! mii.isBranch(termMvec[first]->getOpCode())) |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1282 | { |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1283 | ++first; |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1284 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1285 | assert(first < termMvec.size() && |
| 1286 | "No branch instructions for BR? Ok, but weird! Delete assertion."); |
| 1287 | |
| 1288 | brInstr = (first < termMvec.size())? termMvec[first] : NULL; |
| 1289 | |
| 1290 | // Compute a vector of the nodes chosen for delay slots and then |
| 1291 | // mark delay slots to replace NOPs with these useful instructions. |
| 1292 | // |
| 1293 | if (brInstr != NULL) { |
| 1294 | SchedGraphNode* brNode = graph->getGraphNodeForInstr(brInstr); |
| 1295 | FindUsefulInstructionsForDelaySlots(S, brNode, delayNodeVec); |
| 1296 | ReplaceNopsWithUsefulInstr(S, brNode, delayNodeVec, graph); |
| 1297 | } |
| 1298 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1299 | |
| 1300 | // Also mark delay slots for other delayed instructions to hold NOPs. |
| 1301 | // Simply passing in an empty delayNodeVec will have this effect. |
Vikram S. Adve | bed4eff | 2003-09-16 05:55:15 +0000 | [diff] [blame] | 1302 | // If brInstr is not handled above (EnableFillingDelaySlots == false), |
| 1303 | // brInstr will be NULL so this will handle the branch instrs. as well. |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1304 | // |
| 1305 | delayNodeVec.clear(); |
Chris Lattner | fb3a0aed | 2002-10-28 18:50:08 +0000 | [diff] [blame] | 1306 | for (unsigned i=0; i < MBB.size(); ++i) |
| 1307 | if (MBB[i] != brInstr && |
| 1308 | mii.getNumDelaySlots(MBB[i]->getOpCode()) > 0) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1309 | { |
| 1310 | SchedGraphNode* node = graph->getGraphNodeForInstr(MBB[i]); |
| 1311 | ReplaceNopsWithUsefulInstr(S, node, delayNodeVec, graph); |
| 1312 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1316 | // |
| 1317 | // Schedule the delayed branch and its delay slots |
| 1318 | // |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1319 | unsigned |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1320 | DelaySlotInfo::scheduleDelayedNode(SchedulingManager& S) |
| 1321 | { |
| 1322 | assert(delayedNodeSlotNum < S.nslots && "Illegal slot for branch"); |
| 1323 | assert(S.isched.getInstr(delayedNodeSlotNum, delayedNodeCycle) == NULL |
| 1324 | && "Slot for branch should be empty"); |
| 1325 | |
| 1326 | unsigned int nextSlot = delayedNodeSlotNum; |
| 1327 | cycles_t nextTime = delayedNodeCycle; |
| 1328 | |
| 1329 | S.scheduleInstr(brNode, nextSlot, nextTime); |
| 1330 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1331 | for (unsigned d=0; d < ndelays; d++) { |
| 1332 | ++nextSlot; |
| 1333 | if (nextSlot == S.nslots) { |
| 1334 | nextSlot = 0; |
| 1335 | nextTime++; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1336 | } |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Find the first feasible instruction for this delay slot |
| 1339 | // Note that we only check for issue restrictions here. |
| 1340 | // We do *not* check for flow dependences but rely on pipeline |
| 1341 | // interlocks to resolve them. Machines without interlocks |
| 1342 | // will require this code to be modified. |
| 1343 | for (unsigned i=0; i < delayNodeVec.size(); i++) { |
| 1344 | const SchedGraphNode* dnode = delayNodeVec[i]; |
| 1345 | if ( ! S.isScheduled(dnode) |
| 1346 | && S.schedInfo.instrCanUseSlot(dnode->getOpCode(), nextSlot) |
| 1347 | && instrIsFeasible(S, dnode->getOpCode())) |
| 1348 | { |
| 1349 | assert(S.getInstrInfo().hasOperandInterlock(dnode->getOpCode()) |
| 1350 | && "Instructions without interlocks not yet supported " |
| 1351 | "when filling branch delay slots"); |
| 1352 | S.scheduleInstr(dnode, nextSlot, nextTime); |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
| 1356 | } |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1357 | |
| 1358 | // Update current time if delay slots overflowed into later cycles. |
| 1359 | // Do this here because we know exactly which cycle is the last cycle |
| 1360 | // that contains delay slots. The next loop doesn't compute that. |
| 1361 | if (nextTime > S.getTime()) |
| 1362 | S.updateTime(nextTime); |
| 1363 | |
| 1364 | // Now put any remaining instructions in the unfilled delay slots. |
| 1365 | // This could lead to suboptimal performance but needed for correctness. |
| 1366 | nextSlot = delayedNodeSlotNum; |
| 1367 | nextTime = delayedNodeCycle; |
| 1368 | for (unsigned i=0; i < delayNodeVec.size(); i++) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1369 | if (! S.isScheduled(delayNodeVec[i])) { |
| 1370 | do { // find the next empty slot |
| 1371 | ++nextSlot; |
| 1372 | if (nextSlot == S.nslots) { |
| 1373 | nextSlot = 0; |
| 1374 | nextTime++; |
| 1375 | } |
| 1376 | } while (S.isched.getInstr(nextSlot, nextTime) != NULL); |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1377 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1378 | S.scheduleInstr(delayNodeVec[i], nextSlot, nextTime); |
| 1379 | break; |
| 1380 | } |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1381 | |
| 1382 | return 1 + ndelays; |
Vikram S. Adve | 0e1158f | 2001-08-28 23:07:19 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1385 | |
| 1386 | // Check if the instruction would conflict with instructions already |
| 1387 | // chosen for the current cycle |
| 1388 | // |
| 1389 | static inline bool |
| 1390 | ConflictsWithChoices(const SchedulingManager& S, |
| 1391 | MachineOpCode opCode) |
| 1392 | { |
| 1393 | // Check if the instruction must issue by itself, and some feasible |
| 1394 | // choices have already been made for this cycle |
| 1395 | if (S.getNumChoices() > 0 && S.schedInfo.isSingleIssue(opCode)) |
| 1396 | return true; |
| 1397 | |
| 1398 | // For each class that opCode belongs to, check if there are too many |
| 1399 | // instructions of that class. |
| 1400 | // |
| 1401 | const InstrSchedClass sc = S.schedInfo.getSchedClass(opCode); |
| 1402 | return (S.getNumChoicesInClass(sc) == S.schedInfo.getMaxIssueForClass(sc)); |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | //************************* External Functions *****************************/ |
| 1407 | |
| 1408 | |
| 1409 | //--------------------------------------------------------------------------- |
| 1410 | // Function: ViolatesMinimumGap |
| 1411 | // |
| 1412 | // Purpose: |
| 1413 | // Check minimum gap requirements relative to instructions scheduled in |
| 1414 | // previous cycles. |
| 1415 | // Note that we do not need to consider `nextEarliestIssueTime' here because |
| 1416 | // that is also captured in the earliest start times for each opcode. |
| 1417 | //--------------------------------------------------------------------------- |
| 1418 | |
| 1419 | static inline bool |
| 1420 | ViolatesMinimumGap(const SchedulingManager& S, |
| 1421 | MachineOpCode opCode, |
| 1422 | const cycles_t inCycle) |
| 1423 | { |
| 1424 | return (inCycle < S.getEarliestStartTimeForOp(opCode)); |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | //--------------------------------------------------------------------------- |
| 1429 | // Function: instrIsFeasible |
| 1430 | // |
| 1431 | // Purpose: |
| 1432 | // Check if any issue restrictions would prevent the instruction from |
| 1433 | // being issued in the current cycle |
| 1434 | //--------------------------------------------------------------------------- |
| 1435 | |
| 1436 | bool |
| 1437 | instrIsFeasible(const SchedulingManager& S, |
| 1438 | MachineOpCode opCode) |
| 1439 | { |
| 1440 | // skip the instruction if it cannot be issued due to issue restrictions |
| 1441 | // caused by previously issued instructions |
| 1442 | if (ViolatesMinimumGap(S, opCode, S.getTime())) |
| 1443 | return false; |
| 1444 | |
| 1445 | // skip the instruction if it cannot be issued due to issue restrictions |
| 1446 | // caused by previously chosen instructions for the current cycle |
| 1447 | if (ConflictsWithChoices(S, opCode)) |
| 1448 | return false; |
| 1449 | |
| 1450 | return true; |
| 1451 | } |
| 1452 | |
| 1453 | //--------------------------------------------------------------------------- |
| 1454 | // Function: ScheduleInstructionsWithSSA |
| 1455 | // |
| 1456 | // Purpose: |
| 1457 | // Entry point for instruction scheduling on SSA form. |
| 1458 | // Schedules the machine instructions generated by instruction selection. |
| 1459 | // Assumes that register allocation has not been done, i.e., operands |
| 1460 | // are still in SSA form. |
| 1461 | //--------------------------------------------------------------------------- |
| 1462 | |
Chris Lattner | 9adb7ad | 2002-02-04 20:02:16 +0000 | [diff] [blame] | 1463 | namespace { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 1464 | class InstructionSchedulingWithSSA : public FunctionPass { |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1465 | const TargetMachine ⌖ |
Chris Lattner | 9adb7ad | 2002-02-04 20:02:16 +0000 | [diff] [blame] | 1466 | public: |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1467 | inline InstructionSchedulingWithSSA(const TargetMachine &T) : target(T) {} |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 1468 | |
| 1469 | const char *getPassName() const { return "Instruction Scheduling"; } |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 1471 | // getAnalysisUsage - We use LiveVarInfo... |
| 1472 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5f0eb8d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 1473 | AU.addRequired<FunctionLiveVarInfo>(); |
Chris Lattner | a087772 | 2002-10-23 03:30:47 +0000 | [diff] [blame] | 1474 | AU.setPreservesCFG(); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1475 | } |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1476 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1477 | bool runOnFunction(Function &F); |
Chris Lattner | 9adb7ad | 2002-02-04 20:02:16 +0000 | [diff] [blame] | 1478 | }; |
| 1479 | } // end anonymous namespace |
| 1480 | |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1482 | bool InstructionSchedulingWithSSA::runOnFunction(Function &F) |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1483 | { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1484 | SchedGraphSet graphSet(&F, target); |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1485 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1486 | if (SchedDebugLevel >= Sched_PrintSchedGraphs) { |
Misha Brukman | c2312df | 2003-05-22 21:24:35 +0000 | [diff] [blame] | 1487 | std::cerr << "\n*** SCHEDULING GRAPHS FOR INSTRUCTION SCHEDULING\n"; |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1488 | graphSet.dump(); |
| 1489 | } |
| 1490 | |
| 1491 | for (SchedGraphSet::const_iterator GI=graphSet.begin(), GE=graphSet.end(); |
| 1492 | GI != GE; ++GI) |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1493 | { |
| 1494 | SchedGraph* graph = (*GI); |
| 1495 | MachineBasicBlock &MBB = graph->getBasicBlock(); |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1496 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1497 | if (SchedDebugLevel >= Sched_PrintSchedTrace) |
| 1498 | std::cerr << "\n*** TRACE OF INSTRUCTION SCHEDULING OPERATIONS\n\n"; |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1499 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1500 | // expensive! |
| 1501 | SchedPriorities schedPrio(&F, graph, getAnalysis<FunctionLiveVarInfo>()); |
| 1502 | SchedulingManager S(target, graph, schedPrio); |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1503 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1504 | ChooseInstructionsForDelaySlots(S, MBB, graph); // modifies graph |
| 1505 | ForwardListSchedule(S); // computes schedule in S |
| 1506 | RecordSchedule(MBB, S); // records schedule in BB |
| 1507 | } |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1508 | |
Misha Brukman | 6b77ec4 | 2003-05-22 21:49:18 +0000 | [diff] [blame] | 1509 | if (SchedDebugLevel >= Sched_PrintMachineCode) { |
| 1510 | std::cerr << "\n*** Machine instructions after INSTRUCTION SCHEDULING\n"; |
| 1511 | MachineFunction::get(&F).dump(); |
| 1512 | } |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1513 | |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
| 1517 | |
Brian Gaeke | bf3c4cf | 2003-08-14 06:09:32 +0000 | [diff] [blame] | 1518 | FunctionPass *createInstructionSchedulingWithSSAPass(const TargetMachine &tgt) { |
Vikram S. Adve | 802cec4 | 2002-03-24 03:44:55 +0000 | [diff] [blame] | 1519 | return new InstructionSchedulingWithSSA(tgt); |
Vikram S. Adve | c5b4632 | 2001-09-30 23:43:34 +0000 | [diff] [blame] | 1520 | } |