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