Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAGList.cpp - Implement a list scheduler for isel DAG ---===// |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Evan Cheng and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a simple two pass scheduler. The first pass attempts to push |
| 11 | // backward any lengthy instructions and critical paths. The second pass packs |
| 12 | // instructions into semi-optimal time slots. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "sched" |
| 17 | #include "llvm/CodeGen/ScheduleDAG.h" |
| 18 | #include "llvm/CodeGen/SelectionDAG.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | #include <climits> |
| 23 | #include <iostream> |
| 24 | #include <memory> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 25 | #include <queue> |
| 26 | using namespace llvm; |
| 27 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 28 | namespace { |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 29 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 30 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a |
| 31 | /// group of nodes flagged together. |
| 32 | struct SUnit { |
| 33 | SDNode *Node; // Representative node. |
| 34 | std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 35 | std::vector<SUnit*> Preds; // All real predecessors. |
| 36 | std::vector<SUnit*> ChainPreds; // All chain predecessors. |
| 37 | std::vector<SUnit*> Succs; // All real successors. |
| 38 | std::vector<SUnit*> ChainSuccs; // All chain successors. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 39 | int NumPredsLeft; // # of preds not scheduled. |
| 40 | int NumSuccsLeft; // # of succs not scheduled. |
| 41 | int Priority1; // Scheduling priority 1. |
| 42 | int Priority2; // Scheduling priority 2. |
| 43 | unsigned Latency; // Node latency. |
| 44 | unsigned CycleBound; // Upper/lower cycle to be scheduled at. |
| 45 | unsigned Slot; // Cycle node is scheduled at. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 46 | SUnit *Next; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 47 | |
| 48 | SUnit(SDNode *node) |
| 49 | : Node(node), NumPredsLeft(0), NumSuccsLeft(0), |
| 50 | Priority1(INT_MIN), Priority2(INT_MIN), Latency(0), |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 51 | CycleBound(0), Slot(0), Next(NULL) {} |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 52 | |
| 53 | void dump(const SelectionDAG *G, bool All=true) const; |
| 54 | }; |
| 55 | |
| 56 | void SUnit::dump(const SelectionDAG *G, bool All) const { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 57 | std::cerr << "SU: "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 58 | Node->dump(G); |
| 59 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 60 | if (FlaggedNodes.size() != 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 62 | std::cerr << " "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 63 | FlaggedNodes[i]->dump(G); |
| 64 | std::cerr << "\n"; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (All) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 69 | std::cerr << "# preds left : " << NumPredsLeft << "\n"; |
| 70 | std::cerr << "# succs left : " << NumSuccsLeft << "\n"; |
| 71 | std::cerr << "Latency : " << Latency << "\n"; |
| 72 | std::cerr << "Priority : " << Priority1 << " , " << Priority2 << "\n"; |
| 73 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 74 | if (Preds.size() != 0) { |
| 75 | std::cerr << "Predecessors :\n"; |
| 76 | for (unsigned i = 0, e = Preds.size(); i != e; i++) { |
| 77 | std::cerr << " "; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 78 | Preds[i]->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | if (ChainPreds.size() != 0) { |
| 82 | std::cerr << "Chained Preds :\n"; |
| 83 | for (unsigned i = 0, e = ChainPreds.size(); i != e; i++) { |
| 84 | std::cerr << " "; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 85 | ChainPreds[i]->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | if (Succs.size() != 0) { |
| 89 | std::cerr << "Successors :\n"; |
| 90 | for (unsigned i = 0, e = Succs.size(); i != e; i++) { |
| 91 | std::cerr << " "; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 92 | Succs[i]->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | if (ChainSuccs.size() != 0) { |
| 96 | std::cerr << "Chained succs :\n"; |
| 97 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; i++) { |
| 98 | std::cerr << " "; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 99 | ChainSuccs[i]->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Sorting functions for the Available queue. |
| 106 | struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 107 | bool operator()(const SUnit* left, const SUnit* right) const { |
| 108 | if (left->Priority1 > right->Priority1) { |
| 109 | return true; |
| 110 | } else if (left->Priority1 == right->Priority1) { |
| 111 | unsigned lf = left->FlaggedNodes.size(); |
| 112 | unsigned rf = right->FlaggedNodes.size(); |
| 113 | if (lf > rf) |
| 114 | return true; |
| 115 | else if (lf == rf) { |
| 116 | if (left->Priority2 > right->Priority2) |
| 117 | return true; |
| 118 | else if (left->Priority2 == right->Priority2) { |
| 119 | if (left->CycleBound > right->CycleBound) |
| 120 | return true; |
| 121 | else |
| 122 | return left->Node->getNodeDepth() < right->Node->getNodeDepth(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return false; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 128 | } |
| 129 | }; |
| 130 | |
| 131 | /// ScheduleDAGList - List scheduler. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 132 | class ScheduleDAGList : public ScheduleDAG { |
| 133 | private: |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 134 | // SDNode to SUnit mapping (many to one). |
| 135 | std::map<SDNode*, SUnit*> SUnitMap; |
| 136 | // Available queue. |
| 137 | std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Available; |
| 138 | // The schedule. |
| 139 | std::vector<SUnit*> Sequence; |
| 140 | // Current scheduling cycle. |
| 141 | unsigned CurrCycle; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 142 | // First and last SUnit created. |
| 143 | SUnit *HeadSUnit, *TailSUnit; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 144 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 145 | public: |
| 146 | ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb, |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 147 | const TargetMachine &tm) |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 148 | : ScheduleDAG(listSchedulingBURR, dag, bb, tm), |
| 149 | CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL) {}; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 150 | |
| 151 | ~ScheduleDAGList() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 152 | SUnit *SU = HeadSUnit; |
| 153 | while (SU) { |
| 154 | SUnit *NextSU = SU->Next; |
| 155 | delete SU; |
| 156 | SU = NextSU; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 159 | |
| 160 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 161 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 162 | void dump() const; |
| 163 | |
| 164 | private: |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 165 | SUnit *NewSUnit(SDNode *N); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 166 | void ReleasePred(SUnit *PredSU); |
| 167 | void ScheduleNode(SUnit *SU); |
| 168 | int CalcNodePriority(SUnit *SU); |
| 169 | void CalculatePriorities(); |
| 170 | void ListSchedule(); |
| 171 | void BuildSchedUnits(); |
| 172 | void EmitSchedule(); |
| 173 | }; |
| 174 | } // end namespace |
| 175 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 176 | |
| 177 | /// NewSUnit - Creates a new SUnit and return a ptr to it. |
| 178 | SUnit *ScheduleDAGList::NewSUnit(SDNode *N) { |
| 179 | SUnit *CurrSUnit = new SUnit(N); |
| 180 | |
| 181 | if (HeadSUnit == NULL) |
| 182 | HeadSUnit = CurrSUnit; |
| 183 | if (TailSUnit != NULL) |
| 184 | TailSUnit->Next = CurrSUnit; |
| 185 | TailSUnit = CurrSUnit; |
| 186 | |
| 187 | return CurrSUnit; |
| 188 | } |
| 189 | |
| 190 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 191 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 192 | void ScheduleDAGList::ReleasePred(SUnit *PredSU) { |
| 193 | SDNode *PredNode = PredSU->Node; |
| 194 | |
| 195 | PredSU->NumSuccsLeft--; |
| 196 | if (PredSU->NumSuccsLeft == 0) { |
| 197 | // EntryToken has to go last! |
| 198 | if (PredNode->getOpcode() != ISD::EntryToken) |
| 199 | Available.push(PredSU); |
| 200 | } else if (PredSU->NumSuccsLeft < 0) { |
| 201 | #ifndef NDEBUG |
| 202 | std::cerr << "*** List scheduling failed! ***\n"; |
| 203 | PredSU->dump(&DAG); |
| 204 | std::cerr << " has been released too many times!\n"; |
| 205 | assert(0); |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 210 | // latency. For example, the reader can very well read the register written |
| 211 | // by the predecessor later than the issue cycle. It also depends on the |
| 212 | // interrupt model (drain vs. freeze). |
| 213 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency); |
| 214 | } |
| 215 | |
| 216 | /// ScheduleNode - Add the node to the schedule. Decrement the pending count of |
| 217 | /// its predecessors. If a predecessor pending count is zero, add it to the |
| 218 | /// Available queue. |
| 219 | void ScheduleDAGList::ScheduleNode(SUnit *SU) { |
| 220 | Sequence.push_back(SU); |
| 221 | SU->Slot = CurrCycle; |
| 222 | |
| 223 | // Bottom up: release predecessors |
| 224 | for (unsigned i = 0, e = SU->Preds.size(); i != e; i++) |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 225 | ReleasePred(SU->Preds[i]); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 226 | for (unsigned i = 0, e = SU->ChainPreds.size(); i != e; i++) |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 227 | ReleasePred(SU->ChainPreds[i]); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 228 | |
| 229 | CurrCycle++; |
| 230 | } |
| 231 | |
| 232 | /// isReady - True if node's lower cycle bound is less or equal to the current |
| 233 | /// scheduling cycle. Always true if all nodes have uniform latency 1. |
| 234 | static inline bool isReady(SUnit *SU, unsigned CurrCycle) { |
| 235 | return SU->CycleBound <= CurrCycle; |
| 236 | } |
| 237 | |
| 238 | /// ListSchedule - The main loop of list scheduling. |
| 239 | void ScheduleDAGList::ListSchedule() { |
| 240 | // Add root to Available queue |
| 241 | SUnit *Root = SUnitMap[DAG.getRoot().Val]; |
| 242 | Available.push(Root); |
| 243 | |
| 244 | // While Available queue is not empty, grab the node with the highest |
| 245 | // priority. If it is not ready put it back. Schedule the node. |
| 246 | std::vector<SUnit*> NotReady; |
| 247 | while (!Available.empty()) { |
| 248 | SUnit *CurrNode = Available.top(); |
| 249 | Available.pop(); |
| 250 | |
| 251 | NotReady.clear(); |
| 252 | while (!isReady(CurrNode, CurrCycle)) { |
| 253 | NotReady.push_back(CurrNode); |
| 254 | CurrNode = Available.top(); |
| 255 | Available.pop(); |
| 256 | } |
| 257 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) |
| 258 | Available.push(NotReady[i]); |
| 259 | |
| 260 | DEBUG(std::cerr << "\n*** Scheduling: "); |
| 261 | DEBUG(CurrNode->dump(&DAG, false)); |
| 262 | DEBUG(std::cerr << "\n"); |
| 263 | ScheduleNode(CurrNode); |
| 264 | } |
| 265 | |
| 266 | // Add entry node last |
| 267 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
| 268 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 269 | Entry->Slot = CurrCycle; |
| 270 | Sequence.push_back(Entry); |
| 271 | } |
| 272 | |
| 273 | #ifndef NDEBUG |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 274 | bool AnyNotSched = false; |
| 275 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 276 | if (SU->NumSuccsLeft != 0) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 277 | if (!AnyNotSched) |
| 278 | std::cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 279 | SU->dump(&DAG); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 280 | std::cerr << "has not been scheduled!\n"; |
| 281 | AnyNotSched = true; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 282 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 283 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 284 | assert(!AnyNotSched); |
Reid Spencer | 5edde66 | 2006-01-25 21:49:13 +0000 | [diff] [blame] | 285 | #endif |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | // Reverse the order if it is bottom up. |
| 289 | std::reverse(Sequence.begin(), Sequence.end()); |
| 290 | |
| 291 | DEBUG(std::cerr << "*** Final schedule ***\n"); |
| 292 | DEBUG(dump()); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 293 | DEBUG(std::cerr << "\n"); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /// CalcNodePriority - Priority 1 is just the number of live range genned - number |
| 297 | /// of live range killed. Priority 2 is the Sethi Ullman number. It returns |
| 298 | /// priority 2 since it is calculated recursively. |
| 299 | /// Smaller number is the higher priority in both cases. |
| 300 | int ScheduleDAGList::CalcNodePriority(SUnit *SU) { |
| 301 | if (SU->Priority2 != INT_MIN) |
| 302 | return SU->Priority2; |
| 303 | |
| 304 | SU->Priority1 = SU->Preds.size() - SU->Succs.size(); |
| 305 | |
| 306 | if (SU->Preds.size() == 0) { |
| 307 | SU->Priority2 = 1; |
| 308 | } else { |
| 309 | int Extra = 0; |
| 310 | for (unsigned i = 0, e = SU->Preds.size(); i != e; i++) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 311 | SUnit *PredSU = SU->Preds[i]; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 312 | int PredPriority = CalcNodePriority(PredSU); |
| 313 | if (PredPriority > SU->Priority2) { |
| 314 | SU->Priority2 = PredPriority; |
| 315 | Extra = 0; |
| 316 | } else if (PredPriority == SU->Priority2) |
| 317 | Extra++; |
| 318 | } |
| 319 | |
| 320 | if (SU->Node->getOpcode() != ISD::TokenFactor) |
| 321 | SU->Priority2 += Extra; |
| 322 | else |
| 323 | SU->Priority2 = (Extra == 1) ? 0 : Extra-1; |
| 324 | } |
| 325 | |
| 326 | return SU->Priority2; |
| 327 | } |
| 328 | |
| 329 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 330 | void ScheduleDAGList::CalculatePriorities() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 331 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 332 | // FIXME: assumes uniform latency for now. |
| 333 | SU->Latency = 1; |
| 334 | (void)CalcNodePriority(SU); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 335 | DEBUG(SU->dump(&DAG)); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 336 | DEBUG(std::cerr << "\n"); |
| 337 | } |
| 338 | } |
| 339 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 340 | void ScheduleDAGList::BuildSchedUnits() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 341 | // Pass 1: create the SUnit's. |
Jeff Cohen | fb20616 | 2006-01-25 17:17:49 +0000 | [diff] [blame] | 342 | for (unsigned i = 0, NC = NodeCount; i < NC; i++) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 343 | NodeInfo *NI = &Info[i]; |
| 344 | SDNode *N = NI->Node; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 345 | if (isPassiveNode(N)) |
| 346 | continue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 347 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 348 | SUnit *SU; |
| 349 | if (NI->isInGroup()) { |
| 350 | if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom |
| 351 | continue; // node of the NodeGroup |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 352 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 353 | SU = NewSUnit(N); |
| 354 | // Find the flagged nodes. |
| 355 | SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1); |
| 356 | SDNode *Flag = FlagOp.Val; |
| 357 | unsigned ResNo = FlagOp.ResNo; |
| 358 | while (Flag->getValueType(ResNo) == MVT::Flag) { |
| 359 | NodeInfo *FNI = getNI(Flag); |
| 360 | assert(FNI->Group == NI->Group); |
| 361 | SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag); |
| 362 | SUnitMap[Flag] = SU; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 363 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 364 | FlagOp = Flag->getOperand(Flag->getNumOperands() - 1); |
| 365 | Flag = FlagOp.Val; |
| 366 | ResNo = FlagOp.ResNo; |
| 367 | } |
| 368 | } else { |
| 369 | SU = NewSUnit(N); |
| 370 | } |
| 371 | SUnitMap[N] = SU; |
| 372 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 373 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 374 | // Pass 2: add the preds, succs, etc. |
| 375 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
| 376 | SDNode *N = SU->Node; |
| 377 | NodeInfo *NI = getNI(N); |
| 378 | |
| 379 | if (NI->isInGroup()) { |
| 380 | // Find all predecessors (of the group). |
| 381 | NodeGroupOpIterator NGOI(NI); |
| 382 | while (!NGOI.isEnd()) { |
| 383 | SDOperand Op = NGOI.next(); |
| 384 | SDNode *OpN = Op.Val; |
| 385 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 386 | NodeInfo *OpNI = getNI(OpN); |
| 387 | if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) { |
| 388 | assert(VT != MVT::Flag); |
| 389 | SUnit *OpSU = SUnitMap[OpN]; |
| 390 | if (VT == MVT::Other) { |
| 391 | SU ->ChainPreds.push_back(OpSU); |
| 392 | OpSU->ChainSuccs.push_back(SU); |
| 393 | } else { |
| 394 | SU ->Preds.push_back(OpSU); |
| 395 | OpSU->Succs.push_back(SU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 396 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 397 | SU->NumPredsLeft++; |
| 398 | OpSU->NumSuccsLeft++; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame^] | 401 | } else { |
| 402 | // Find node predecessors. |
| 403 | for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) { |
| 404 | SDOperand Op = N->getOperand(j); |
| 405 | SDNode *OpN = Op.Val; |
| 406 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 407 | if (!isPassiveNode(OpN)) { |
| 408 | assert(VT != MVT::Flag); |
| 409 | SUnit *OpSU = SUnitMap[OpN]; |
| 410 | if (VT == MVT::Other) { |
| 411 | SU ->ChainPreds.push_back(OpSU); |
| 412 | OpSU->ChainSuccs.push_back(SU); |
| 413 | } else { |
| 414 | SU ->Preds.push_back(OpSU); |
| 415 | OpSU->Succs.push_back(SU); |
| 416 | } |
| 417 | SU->NumPredsLeft++; |
| 418 | OpSU->NumSuccsLeft++; |
| 419 | } |
| 420 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 426 | void ScheduleDAGList::EmitSchedule() { |
| 427 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 428 | SDNode *N; |
| 429 | SUnit *SU = Sequence[i]; |
| 430 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) { |
| 431 | N = SU->FlaggedNodes[j]; |
| 432 | EmitNode(getNI(N)); |
| 433 | } |
| 434 | EmitNode(getNI(SU->Node)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /// dump - dump the schedule. |
| 439 | void ScheduleDAGList::dump() const { |
| 440 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 441 | SUnit *SU = Sequence[i]; |
| 442 | SU->dump(&DAG, false); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// Schedule - Schedule the DAG using list scheduling. |
| 447 | /// FIXME: Right now it only supports the burr (bottom up register reducing) |
| 448 | /// heuristic. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 449 | void ScheduleDAGList::Schedule() { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 450 | DEBUG(std::cerr << "********** List Scheduling **********\n"); |
| 451 | |
| 452 | // Build scheduling units. |
| 453 | BuildSchedUnits(); |
| 454 | |
| 455 | // Calculate node prirorities. |
| 456 | CalculatePriorities(); |
| 457 | |
| 458 | // Execute the actual scheduling loop. |
| 459 | ListSchedule(); |
| 460 | |
| 461 | // Emit in scheduled order |
| 462 | EmitSchedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 465 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG, |
| 466 | MachineBasicBlock *BB) { |
| 467 | return new ScheduleDAGList(DAG, BB, DAG.getTarget()); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 468 | } |