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> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 24 | #include <queue> |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <vector> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 29 | namespace { |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 30 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 31 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or a |
| 32 | /// group of nodes flagged together. |
| 33 | struct SUnit { |
| 34 | SDNode *Node; // Representative node. |
| 35 | std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node. |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 36 | std::set<SUnit*> Preds; // All real predecessors. |
| 37 | std::set<SUnit*> ChainPreds; // All chain predecessors. |
| 38 | std::set<SUnit*> Succs; // All real successors. |
| 39 | std::set<SUnit*> ChainSuccs; // All chain successors. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 40 | int NumPredsLeft; // # of preds not scheduled. |
| 41 | int NumSuccsLeft; // # of succs not scheduled. |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 42 | int NumChainPredsLeft; // # of chain preds not scheduled. |
| 43 | int NumChainSuccsLeft; // # of chain succs not scheduled. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 44 | int Priority1; // Scheduling priority 1. |
| 45 | int Priority2; // Scheduling priority 2. |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 46 | bool isTwoAddress; // Is a two-address instruction. |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 47 | bool isDefNUseOperand; // Is a def&use operand. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 48 | unsigned Latency; // Node latency. |
| 49 | unsigned CycleBound; // Upper/lower cycle to be scheduled at. |
| 50 | unsigned Slot; // Cycle node is scheduled at. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 51 | SUnit *Next; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 52 | |
| 53 | SUnit(SDNode *node) |
| 54 | : Node(node), NumPredsLeft(0), NumSuccsLeft(0), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 55 | NumChainPredsLeft(0), NumChainSuccsLeft(0), |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 56 | Priority1(INT_MIN), Priority2(INT_MIN), |
| 57 | isTwoAddress(false), isDefNUseOperand(false), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 58 | Latency(0), CycleBound(0), Slot(0), Next(NULL) {} |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 59 | |
| 60 | void dump(const SelectionDAG *G, bool All=true) const; |
| 61 | }; |
| 62 | |
| 63 | void SUnit::dump(const SelectionDAG *G, bool All) const { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 64 | std::cerr << "SU: "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 65 | Node->dump(G); |
| 66 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 67 | if (FlaggedNodes.size() != 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 68 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 69 | std::cerr << " "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 70 | FlaggedNodes[i]->dump(G); |
| 71 | std::cerr << "\n"; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (All) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 76 | std::cerr << " # preds left : " << NumPredsLeft << "\n"; |
| 77 | std::cerr << " # succs left : " << NumSuccsLeft << "\n"; |
| 78 | std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n"; |
| 79 | std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n"; |
| 80 | std::cerr << " Latency : " << Latency << "\n"; |
| 81 | std::cerr << " Priority : " << Priority1 << " , " |
| 82 | << Priority2 << "\n"; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 83 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 84 | if (Preds.size() != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 85 | std::cerr << " Predecessors:\n"; |
Jeff Cohen | 55c1173 | 2006-03-03 03:25:07 +0000 | [diff] [blame] | 86 | for (std::set<SUnit*>::const_iterator I = Preds.begin(), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 87 | E = Preds.end(); I != E; ++I) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 88 | std::cerr << " "; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 89 | (*I)->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | if (ChainPreds.size() != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 93 | std::cerr << " Chained Preds:\n"; |
Jeff Cohen | 55c1173 | 2006-03-03 03:25:07 +0000 | [diff] [blame] | 94 | for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 95 | E = ChainPreds.end(); I != E; ++I) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 96 | std::cerr << " "; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 97 | (*I)->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | if (Succs.size() != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 101 | std::cerr << " Successors:\n"; |
Jeff Cohen | 55c1173 | 2006-03-03 03:25:07 +0000 | [diff] [blame] | 102 | for (std::set<SUnit*>::const_iterator I = Succs.begin(), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 103 | E = Succs.end(); I != E; ++I) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 104 | std::cerr << " "; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 105 | (*I)->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | if (ChainSuccs.size() != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 109 | std::cerr << " Chained succs:\n"; |
Jeff Cohen | 55c1173 | 2006-03-03 03:25:07 +0000 | [diff] [blame] | 110 | for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 111 | E = ChainSuccs.end(); I != E; ++I) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 112 | std::cerr << " "; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 113 | (*I)->dump(G, false); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /// Sorting functions for the Available queue. |
| 120 | struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 121 | bool operator()(const SUnit* left, const SUnit* right) const { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 122 | bool LFloater = (left ->Preds.size() == 0); |
| 123 | bool RFloater = (right->Preds.size() == 0); |
| 124 | int LBonus = (int)left ->isDefNUseOperand; |
| 125 | int RBonus = (int)right->isDefNUseOperand; |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 126 | |
| 127 | // Special tie breaker: if two nodes share a operand, the one that |
| 128 | // use it as a def&use operand is preferred. |
| 129 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 130 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 131 | if (DUNode->isOperand(right->Node)) |
| 132 | LBonus++; |
| 133 | } |
| 134 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 135 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 136 | if (DUNode->isOperand(left->Node)) |
| 137 | RBonus++; |
| 138 | } |
| 139 | |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 140 | int LPriority1 = left ->Priority1 - LBonus; |
| 141 | int RPriority1 = right->Priority1 - RBonus; |
| 142 | int LPriority2 = left ->Priority2 + LBonus; |
| 143 | int RPriority2 = right->Priority2 + RBonus; |
| 144 | |
| 145 | // Favor floaters (i.e. node with no non-passive predecessors): |
| 146 | // e.g. MOV32ri. |
| 147 | if (!LFloater && RFloater) |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 148 | return true; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 149 | else if (LFloater == RFloater) |
| 150 | if (LPriority1 > RPriority1) |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 151 | return true; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 152 | else if (LPriority1 == RPriority1) |
| 153 | if (LPriority2 < RPriority2) |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 154 | return true; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 155 | else if (LPriority1 == RPriority1) |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 156 | if (left->CycleBound > right->CycleBound) |
| 157 | return true; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 158 | |
| 159 | return false; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 160 | } |
| 161 | }; |
| 162 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 163 | |
| 164 | /// HazardRecognizer - This determines whether or not an instruction can be |
| 165 | /// issued this cycle, and whether or not a noop needs to be inserted to handle |
| 166 | /// the hazard. |
| 167 | namespace { |
| 168 | class HazardRecognizer { |
| 169 | public: |
| 170 | virtual ~HazardRecognizer() {} |
| 171 | |
| 172 | enum HazardType { |
| 173 | NoHazard, // This instruction can be emitted at this cycle. |
| 174 | Hazard, // This instruction can't be emitted at this cycle. |
| 175 | NoopHazard, // This instruction can't be emitted, and needs noops. |
| 176 | }; |
| 177 | |
| 178 | /// getHazardType - Return the hazard type of emitting this node. There are |
| 179 | /// three possible results. Either: |
| 180 | /// * NoHazard: it is legal to issue this instruction on this cycle. |
| 181 | /// * Hazard: issuing this instruction would stall the machine. If some |
| 182 | /// other instruction is available, issue it first. |
| 183 | /// * NoopHazard: issuing this instruction would break the program. If |
| 184 | /// some other instruction can be issued, do so, otherwise issue a noop. |
| 185 | virtual HazardType getHazardType(SDNode *Node) { |
| 186 | return NoHazard; |
| 187 | } |
| 188 | |
| 189 | /// EmitInstruction - This callback is invoked when an instruction is |
| 190 | /// emitted, to advance the hazard state. |
| 191 | virtual void EmitInstruction(SDNode *Node) { |
| 192 | } |
| 193 | |
| 194 | /// AdvanceCycle - This callback is invoked when no instructions can be |
| 195 | /// issued on this cycle without a hazard. This should increment the |
| 196 | /// internal state of the hazard recognizer so that previously "Hazard" |
| 197 | /// instructions will now not be hazards. |
| 198 | virtual void AdvanceCycle() { |
| 199 | } |
| 200 | |
| 201 | /// EmitNoop - This callback is invoked when a noop was added to the |
| 202 | /// instruction stream. |
| 203 | virtual void EmitNoop() { |
| 204 | } |
| 205 | }; |
| 206 | } |
| 207 | |
| 208 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 209 | /// ScheduleDAGList - List scheduler. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 210 | class ScheduleDAGList : public ScheduleDAG { |
| 211 | private: |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 212 | // SDNode to SUnit mapping (many to one). |
| 213 | std::map<SDNode*, SUnit*> SUnitMap; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 214 | // The schedule. |
| 215 | std::vector<SUnit*> Sequence; |
| 216 | // Current scheduling cycle. |
| 217 | unsigned CurrCycle; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 218 | // First and last SUnit created. |
| 219 | SUnit *HeadSUnit, *TailSUnit; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 221 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 222 | /// it is top-down. |
| 223 | bool isBottomUp; |
| 224 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 225 | /// HazardRec - The hazard recognizer to use. |
| 226 | HazardRecognizer *HazardRec; |
| 227 | |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 228 | typedef std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> |
| 229 | AvailableQueueTy; |
| 230 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 231 | public: |
| 232 | ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb, |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 233 | const TargetMachine &tm, bool isbottomup, |
| 234 | HazardRecognizer *HR = 0) |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 235 | : ScheduleDAG(listSchedulingBURR, dag, bb, tm), |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 236 | CurrCycle(0), HeadSUnit(NULL), TailSUnit(NULL), isBottomUp(isbottomup) { |
| 237 | if (HR == 0) HR = new HazardRecognizer(); |
| 238 | HazardRec = HR; |
| 239 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 240 | |
| 241 | ~ScheduleDAGList() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 242 | SUnit *SU = HeadSUnit; |
| 243 | while (SU) { |
| 244 | SUnit *NextSU = SU->Next; |
| 245 | delete SU; |
| 246 | SU = NextSU; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 247 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 248 | |
| 249 | delete HazardRec; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 250 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 251 | |
| 252 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 253 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 254 | void dump() const; |
| 255 | |
| 256 | private: |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 257 | SUnit *NewSUnit(SDNode *N); |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 258 | void ReleasePred(AvailableQueueTy &Avail,SUnit *PredSU, bool isChain = false); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 259 | void ReleaseSucc(AvailableQueueTy &Avail,SUnit *SuccSU, bool isChain = false); |
| 260 | void ScheduleNodeBottomUp(AvailableQueueTy &Avail, SUnit *SU); |
| 261 | void ScheduleNodeTopDown(AvailableQueueTy &Avail, SUnit *SU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 262 | int CalcNodePriority(SUnit *SU); |
| 263 | void CalculatePriorities(); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 264 | void ListScheduleTopDown(); |
| 265 | void ListScheduleBottomUp(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 266 | void BuildSchedUnits(); |
| 267 | void EmitSchedule(); |
| 268 | }; |
| 269 | } // end namespace |
| 270 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 271 | |
| 272 | /// NewSUnit - Creates a new SUnit and return a ptr to it. |
| 273 | SUnit *ScheduleDAGList::NewSUnit(SDNode *N) { |
| 274 | SUnit *CurrSUnit = new SUnit(N); |
| 275 | |
| 276 | if (HeadSUnit == NULL) |
| 277 | HeadSUnit = CurrSUnit; |
| 278 | if (TailSUnit != NULL) |
| 279 | TailSUnit->Next = CurrSUnit; |
| 280 | TailSUnit = CurrSUnit; |
| 281 | |
| 282 | return CurrSUnit; |
| 283 | } |
| 284 | |
| 285 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 286 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 287 | void ScheduleDAGList::ReleasePred(AvailableQueueTy &Available, |
| 288 | SUnit *PredSU, bool isChain) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 289 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 290 | // latency. For example, the reader can very well read the register written |
| 291 | // by the predecessor later than the issue cycle. It also depends on the |
| 292 | // interrupt model (drain vs. freeze). |
| 293 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurrCycle + PredSU->Latency); |
| 294 | |
| 295 | if (!isChain) { |
| 296 | PredSU->NumSuccsLeft--; |
| 297 | PredSU->Priority1++; |
| 298 | } else |
| 299 | PredSU->NumChainSuccsLeft--; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 300 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 301 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 302 | if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 303 | std::cerr << "*** List scheduling failed! ***\n"; |
| 304 | PredSU->dump(&DAG); |
| 305 | std::cerr << " has been released too many times!\n"; |
| 306 | assert(0); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 307 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 308 | #endif |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 309 | |
| 310 | if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) { |
| 311 | // EntryToken has to go last! Special case it here. |
| 312 | if (PredSU->Node->getOpcode() != ISD::EntryToken) |
| 313 | Available.push(PredSU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 314 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 317 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 318 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
| 319 | void ScheduleDAGList::ReleaseSucc(AvailableQueueTy &Available, |
| 320 | SUnit *SuccSU, bool isChain) { |
| 321 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 322 | // latency. For example, the reader can very well read the register written |
| 323 | // by the predecessor later than the issue cycle. It also depends on the |
| 324 | // interrupt model (drain vs. freeze). |
| 325 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, CurrCycle + SuccSU->Latency); |
| 326 | |
| 327 | if (!isChain) { |
| 328 | SuccSU->NumPredsLeft--; |
| 329 | SuccSU->Priority1++; // FIXME: ?? |
| 330 | } else |
| 331 | SuccSU->NumChainPredsLeft--; |
| 332 | |
| 333 | #ifndef NDEBUG |
| 334 | if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) { |
| 335 | std::cerr << "*** List scheduling failed! ***\n"; |
| 336 | SuccSU->dump(&DAG); |
| 337 | std::cerr << " has been released too many times!\n"; |
| 338 | abort(); |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) |
| 343 | Available.push(SuccSU); |
| 344 | } |
| 345 | |
| 346 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 347 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 348 | /// the Available queue. |
| 349 | void ScheduleDAGList::ScheduleNodeBottomUp(AvailableQueueTy &Available, |
| 350 | SUnit *SU) { |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 351 | DEBUG(std::cerr << "*** Scheduling: "); |
| 352 | DEBUG(SU->dump(&DAG, false)); |
| 353 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 354 | Sequence.push_back(SU); |
| 355 | SU->Slot = CurrCycle; |
| 356 | |
| 357 | // Bottom up: release predecessors |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 358 | for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(), |
| 359 | E1 = SU->Preds.end(); I1 != E1; ++I1) { |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 360 | ReleasePred(Available, *I1); |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 361 | SU->NumPredsLeft--; |
| 362 | SU->Priority1--; |
| 363 | } |
| 364 | for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(), |
| 365 | E2 = SU->ChainPreds.end(); I2 != E2; ++I2) |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 366 | ReleasePred(Available, *I2, true); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 367 | |
| 368 | CurrCycle++; |
| 369 | } |
| 370 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 371 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 372 | /// count of its successors. If a successor pending count is zero, add it to |
| 373 | /// the Available queue. |
| 374 | void ScheduleDAGList::ScheduleNodeTopDown(AvailableQueueTy &Available, |
| 375 | SUnit *SU) { |
| 376 | DEBUG(std::cerr << "*** Scheduling: "); |
| 377 | DEBUG(SU->dump(&DAG, false)); |
| 378 | |
| 379 | Sequence.push_back(SU); |
| 380 | SU->Slot = CurrCycle; |
| 381 | |
| 382 | // Bottom up: release successors. |
| 383 | for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(), |
| 384 | E1 = SU->Succs.end(); I1 != E1; ++I1) { |
| 385 | ReleaseSucc(Available, *I1); |
| 386 | SU->NumSuccsLeft--; |
| 387 | SU->Priority1--; // FIXME: what is this?? |
| 388 | } |
| 389 | for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(), |
| 390 | E2 = SU->ChainSuccs.end(); I2 != E2; ++I2) |
| 391 | ReleaseSucc(Available, *I2, true); |
| 392 | |
| 393 | CurrCycle++; |
| 394 | } |
| 395 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 396 | /// isReady - True if node's lower cycle bound is less or equal to the current |
| 397 | /// scheduling cycle. Always true if all nodes have uniform latency 1. |
| 398 | static inline bool isReady(SUnit *SU, unsigned CurrCycle) { |
| 399 | return SU->CycleBound <= CurrCycle; |
| 400 | } |
| 401 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 402 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 403 | /// schedulers. |
| 404 | void ScheduleDAGList::ListScheduleBottomUp() { |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 405 | // Available queue. |
| 406 | AvailableQueueTy Available; |
| 407 | |
| 408 | // Add root to Available queue. |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 409 | Available.push(SUnitMap[DAG.getRoot().Val]); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 410 | |
| 411 | // While Available queue is not empty, grab the node with the highest |
| 412 | // priority. If it is not ready put it back. Schedule the node. |
| 413 | std::vector<SUnit*> NotReady; |
| 414 | while (!Available.empty()) { |
| 415 | SUnit *CurrNode = Available.top(); |
| 416 | Available.pop(); |
| 417 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 418 | while (!isReady(CurrNode, CurrCycle)) { |
| 419 | NotReady.push_back(CurrNode); |
| 420 | CurrNode = Available.top(); |
| 421 | Available.pop(); |
| 422 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 423 | |
| 424 | // Add the nodes that aren't ready back onto the available list. |
| 425 | while (!NotReady.empty()) { |
| 426 | Available.push(NotReady.back()); |
| 427 | NotReady.pop_back(); |
| 428 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 430 | ScheduleNodeBottomUp(Available, CurrNode); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // Add entry node last |
| 434 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
| 435 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 436 | Entry->Slot = CurrCycle; |
| 437 | Sequence.push_back(Entry); |
| 438 | } |
| 439 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 440 | // Reverse the order if it is bottom up. |
| 441 | std::reverse(Sequence.begin(), Sequence.end()); |
| 442 | |
| 443 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 444 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 445 | // Verify that all SUnits were scheduled. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 446 | bool AnyNotSched = false; |
| 447 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 448 | if (SU->NumSuccsLeft != 0 || SU->NumChainSuccsLeft != 0) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 449 | if (!AnyNotSched) |
| 450 | std::cerr << "*** List scheduling failed! ***\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 451 | SU->dump(&DAG); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 452 | std::cerr << "has not been scheduled!\n"; |
| 453 | AnyNotSched = true; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 454 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 455 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 456 | assert(!AnyNotSched); |
Reid Spencer | 5edde66 | 2006-01-25 21:49:13 +0000 | [diff] [blame] | 457 | #endif |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 460 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 461 | /// schedulers. |
| 462 | void ScheduleDAGList::ListScheduleTopDown() { |
| 463 | // Available queue. |
| 464 | AvailableQueueTy Available; |
| 465 | |
| 466 | // Emit the entry node first. |
| 467 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 468 | ScheduleNodeTopDown(Available, Entry); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 469 | HazardRec->EmitInstruction(Entry->Node); |
| 470 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 471 | // All leaves to Available queue. |
| 472 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
| 473 | // It is available if it has no predecessors. |
| 474 | if ((SU->Preds.size() + SU->ChainPreds.size()) == 0 && SU != Entry) |
| 475 | Available.push(SU); |
| 476 | } |
| 477 | |
| 478 | // While Available queue is not empty, grab the node with the highest |
| 479 | // priority. If it is not ready put it back. Schedule the node. |
| 480 | std::vector<SUnit*> NotReady; |
| 481 | while (!Available.empty()) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 482 | SUnit *FoundNode = 0; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 483 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 484 | bool HasNoopHazards = false; |
| 485 | do { |
| 486 | SUnit *CurrNode = Available.top(); |
| 487 | Available.pop(); |
| 488 | HazardRecognizer::HazardType HT = |
| 489 | HazardRec->getHazardType(CurrNode->Node); |
| 490 | if (HT == HazardRecognizer::NoHazard) { |
| 491 | FoundNode = CurrNode; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | // Remember if this is a noop hazard. |
| 496 | HasNoopHazards |= HT == HazardRecognizer::NoopHazard; |
| 497 | |
| 498 | NotReady.push_back(CurrNode); |
| 499 | } while (!Available.empty()); |
| 500 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 501 | // Add the nodes that aren't ready back onto the available list. |
| 502 | while (!NotReady.empty()) { |
| 503 | Available.push(NotReady.back()); |
| 504 | NotReady.pop_back(); |
| 505 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 506 | |
| 507 | // If we found a node to schedule, do it now. |
| 508 | if (FoundNode) { |
| 509 | ScheduleNodeTopDown(Available, FoundNode); |
| 510 | HazardRec->EmitInstruction(FoundNode->Node); |
| 511 | } else if (!HasNoopHazards) { |
| 512 | // Otherwise, we have a pipeline stall, but no other problem, just advance |
| 513 | // the current cycle and try again. |
| 514 | HazardRec->AdvanceCycle(); |
| 515 | } else { |
| 516 | // Otherwise, we have no instructions to issue and we have instructions |
| 517 | // that will fault if we don't do this right. This is the case for |
| 518 | // processors without pipeline interlocks and other cases. |
| 519 | HazardRec->EmitNoop(); |
| 520 | // FIXME: Add a noop to the schedule!! |
| 521 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | #ifndef NDEBUG |
| 525 | // Verify that all SUnits were scheduled. |
| 526 | bool AnyNotSched = false; |
| 527 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
| 528 | if (SU->NumPredsLeft != 0 || SU->NumChainPredsLeft != 0) { |
| 529 | if (!AnyNotSched) |
| 530 | std::cerr << "*** List scheduling failed! ***\n"; |
| 531 | SU->dump(&DAG); |
| 532 | std::cerr << "has not been scheduled!\n"; |
| 533 | AnyNotSched = true; |
| 534 | } |
| 535 | } |
| 536 | assert(!AnyNotSched); |
| 537 | #endif |
| 538 | } |
| 539 | |
| 540 | |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 541 | /// CalcNodePriority - Priority1 is just the number of live range genned - |
| 542 | /// number of live range killed. Priority2 is the Sethi Ullman number. It |
| 543 | /// returns Priority2 since it is calculated recursively. |
| 544 | /// Smaller number is the higher priority for Priority2. Reverse is true for |
| 545 | /// Priority1. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 546 | int ScheduleDAGList::CalcNodePriority(SUnit *SU) { |
| 547 | if (SU->Priority2 != INT_MIN) |
| 548 | return SU->Priority2; |
| 549 | |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 550 | SU->Priority1 = SU->NumPredsLeft - SU->NumSuccsLeft; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 551 | |
| 552 | if (SU->Preds.size() == 0) { |
| 553 | SU->Priority2 = 1; |
| 554 | } else { |
| 555 | int Extra = 0; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 556 | for (std::set<SUnit*>::iterator I = SU->Preds.begin(), |
| 557 | E = SU->Preds.end(); I != E; ++I) { |
| 558 | SUnit *PredSU = *I; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 559 | int PredPriority = CalcNodePriority(PredSU); |
| 560 | if (PredPriority > SU->Priority2) { |
| 561 | SU->Priority2 = PredPriority; |
| 562 | Extra = 0; |
| 563 | } else if (PredPriority == SU->Priority2) |
| 564 | Extra++; |
| 565 | } |
| 566 | |
| 567 | if (SU->Node->getOpcode() != ISD::TokenFactor) |
| 568 | SU->Priority2 += Extra; |
| 569 | else |
| 570 | SU->Priority2 = (Extra == 1) ? 0 : Extra-1; |
| 571 | } |
| 572 | |
| 573 | return SU->Priority2; |
| 574 | } |
| 575 | |
| 576 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 577 | void ScheduleDAGList::CalculatePriorities() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 578 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 579 | // FIXME: assumes uniform latency for now. |
| 580 | SU->Latency = 1; |
| 581 | (void)CalcNodePriority(SU); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 582 | DEBUG(SU->dump(&DAG)); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 583 | DEBUG(std::cerr << "\n"); |
| 584 | } |
| 585 | } |
| 586 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 587 | void ScheduleDAGList::BuildSchedUnits() { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 588 | // Pass 1: create the SUnit's. |
Jeff Cohen | fb20616 | 2006-01-25 17:17:49 +0000 | [diff] [blame] | 589 | for (unsigned i = 0, NC = NodeCount; i < NC; i++) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 590 | NodeInfo *NI = &Info[i]; |
| 591 | SDNode *N = NI->Node; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 592 | if (isPassiveNode(N)) |
| 593 | continue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 594 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 595 | SUnit *SU; |
| 596 | if (NI->isInGroup()) { |
| 597 | if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom |
| 598 | continue; // node of the NodeGroup |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 599 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 600 | SU = NewSUnit(N); |
| 601 | // Find the flagged nodes. |
| 602 | SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1); |
| 603 | SDNode *Flag = FlagOp.Val; |
| 604 | unsigned ResNo = FlagOp.ResNo; |
| 605 | while (Flag->getValueType(ResNo) == MVT::Flag) { |
| 606 | NodeInfo *FNI = getNI(Flag); |
| 607 | assert(FNI->Group == NI->Group); |
| 608 | SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag); |
| 609 | SUnitMap[Flag] = SU; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 610 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 611 | FlagOp = Flag->getOperand(Flag->getNumOperands() - 1); |
| 612 | Flag = FlagOp.Val; |
| 613 | ResNo = FlagOp.ResNo; |
| 614 | } |
| 615 | } else { |
| 616 | SU = NewSUnit(N); |
| 617 | } |
| 618 | SUnitMap[N] = SU; |
| 619 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 620 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 621 | // Pass 2: add the preds, succs, etc. |
| 622 | for (SUnit *SU = HeadSUnit; SU != NULL; SU = SU->Next) { |
| 623 | SDNode *N = SU->Node; |
| 624 | NodeInfo *NI = getNI(N); |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 625 | |
| 626 | if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode())) |
| 627 | SU->isTwoAddress = true; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 628 | |
| 629 | if (NI->isInGroup()) { |
| 630 | // Find all predecessors (of the group). |
| 631 | NodeGroupOpIterator NGOI(NI); |
| 632 | while (!NGOI.isEnd()) { |
| 633 | SDOperand Op = NGOI.next(); |
| 634 | SDNode *OpN = Op.Val; |
| 635 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 636 | NodeInfo *OpNI = getNI(OpN); |
| 637 | if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) { |
| 638 | assert(VT != MVT::Flag); |
| 639 | SUnit *OpSU = SUnitMap[OpN]; |
| 640 | if (VT == MVT::Other) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 641 | if (SU->ChainPreds.insert(OpSU).second) |
| 642 | SU->NumChainPredsLeft++; |
| 643 | if (OpSU->ChainSuccs.insert(SU).second) |
| 644 | OpSU->NumChainSuccsLeft++; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 645 | } else { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 646 | if (SU->Preds.insert(OpSU).second) |
| 647 | SU->NumPredsLeft++; |
| 648 | if (OpSU->Succs.insert(SU).second) |
| 649 | OpSU->NumSuccsLeft++; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 650 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 653 | } else { |
| 654 | // Find node predecessors. |
| 655 | for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) { |
| 656 | SDOperand Op = N->getOperand(j); |
| 657 | SDNode *OpN = Op.Val; |
| 658 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 659 | if (!isPassiveNode(OpN)) { |
| 660 | assert(VT != MVT::Flag); |
| 661 | SUnit *OpSU = SUnitMap[OpN]; |
| 662 | if (VT == MVT::Other) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 663 | if (SU->ChainPreds.insert(OpSU).second) |
| 664 | SU->NumChainPredsLeft++; |
| 665 | if (OpSU->ChainSuccs.insert(SU).second) |
| 666 | OpSU->NumChainSuccsLeft++; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 667 | } else { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 668 | if (SU->Preds.insert(OpSU).second) |
| 669 | SU->NumPredsLeft++; |
| 670 | if (OpSU->Succs.insert(SU).second) |
| 671 | OpSU->NumSuccsLeft++; |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 672 | if (j == 0 && SU->isTwoAddress) |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 673 | OpSU->isDefNUseOperand = true; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 674 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 682 | void ScheduleDAGList::EmitSchedule() { |
| 683 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 684 | SDNode *N; |
| 685 | SUnit *SU = Sequence[i]; |
| 686 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) { |
| 687 | N = SU->FlaggedNodes[j]; |
| 688 | EmitNode(getNI(N)); |
| 689 | } |
| 690 | EmitNode(getNI(SU->Node)); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /// dump - dump the schedule. |
| 695 | void ScheduleDAGList::dump() const { |
| 696 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 697 | SUnit *SU = Sequence[i]; |
| 698 | SU->dump(&DAG, false); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /// Schedule - Schedule the DAG using list scheduling. |
| 703 | /// FIXME: Right now it only supports the burr (bottom up register reducing) |
| 704 | /// heuristic. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 705 | void ScheduleDAGList::Schedule() { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 706 | DEBUG(std::cerr << "********** List Scheduling **********\n"); |
| 707 | |
| 708 | // Build scheduling units. |
| 709 | BuildSchedUnits(); |
| 710 | |
| 711 | // Calculate node prirorities. |
| 712 | CalculatePriorities(); |
| 713 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 714 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 715 | if (isBottomUp) |
| 716 | ListScheduleBottomUp(); |
| 717 | else |
| 718 | ListScheduleTopDown(); |
| 719 | |
| 720 | DEBUG(std::cerr << "*** Final schedule ***\n"); |
| 721 | DEBUG(dump()); |
| 722 | DEBUG(std::cerr << "\n"); |
| 723 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 724 | // Emit in scheduled order |
| 725 | EmitSchedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 726 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 727 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 728 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG, |
| 729 | MachineBasicBlock *BB) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 730 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true); |
| 731 | } |
| 732 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 733 | /// G5HazardRecognizer - A hazard recognizer for the PowerPC G5 processor. |
| 734 | /// FIXME: Implement |
| 735 | /// FIXME: Move to the PowerPC backend. |
| 736 | class G5HazardRecognizer : public HazardRecognizer { |
| 737 | public: |
| 738 | G5HazardRecognizer() {} |
| 739 | }; |
| 740 | |
| 741 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 742 | /// createTDG5ListDAGScheduler - This creates a top-down list scheduler for |
| 743 | /// the PowerPC G5. FIXME: pull the priority function out into the PPC |
| 744 | /// backend! |
| 745 | ScheduleDAG* llvm::createTDG5ListDAGScheduler(SelectionDAG &DAG, |
| 746 | MachineBasicBlock *BB) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame^] | 747 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, |
| 748 | new G5HazardRecognizer()); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 749 | } |