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 | // |
Chris Lattner | 01aa752 | 2006-03-06 17:58:04 +0000 | [diff] [blame] | 10 | // This implements bottom-up and top-down list schedulers, using standard |
| 11 | // algorithms. The basic approach uses a priority queue of available nodes to |
| 12 | // schedule. One at a time, nodes are taken from the priority queue (thus in |
| 13 | // priority order), checked for legality to schedule, and emitted if legal. |
| 14 | // |
| 15 | // Nodes may not be legal to schedule either due to structural hazards (e.g. |
| 16 | // pipeline or resource constraints) or because an input to the instruction has |
| 17 | // not completed execution. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "sched" |
| 22 | #include "llvm/CodeGen/ScheduleDAG.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 27 | #include <climits> |
| 28 | #include <iostream> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 29 | #include <queue> |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 30 | #include <set> |
| 31 | #include <vector> |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 35 | namespace { |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 36 | Statistic<> NumNoops ("scheduler", "Number of noops inserted"); |
| 37 | Statistic<> NumStalls("scheduler", "Number of pipeline stalls"); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 39 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or |
| 40 | /// a group of nodes flagged together. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 41 | struct SUnit { |
| 42 | SDNode *Node; // Representative node. |
| 43 | std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node. |
| 44 | std::set<SUnit*> Preds; // All real predecessors. |
| 45 | std::set<SUnit*> ChainPreds; // All chain predecessors. |
| 46 | std::set<SUnit*> Succs; // All real successors. |
| 47 | std::set<SUnit*> ChainSuccs; // All chain successors. |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 48 | short NumPredsLeft; // # of preds not scheduled. |
| 49 | short NumSuccsLeft; // # of succs not scheduled. |
| 50 | short NumChainPredsLeft; // # of chain preds not scheduled. |
| 51 | short NumChainSuccsLeft; // # of chain succs not scheduled. |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 52 | bool isTwoAddress : 1; // Is a two-address instruction. |
| 53 | bool isDefNUseOperand : 1; // Is a def&use operand. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 54 | bool isAvailable : 1; // True once available. |
| 55 | bool isScheduled : 1; // True once scheduled. |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 56 | unsigned short Latency; // Node latency. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 57 | unsigned CycleBound; // Upper/lower cycle to be scheduled at. |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 58 | unsigned NodeNum; // Entry # of node in the node vector. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 59 | |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 60 | SUnit(SDNode *node, unsigned nodenum) |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 61 | : Node(node), NumPredsLeft(0), NumSuccsLeft(0), |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 62 | NumChainPredsLeft(0), NumChainSuccsLeft(0), |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 63 | isTwoAddress(false), isDefNUseOperand(false), |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 64 | isAvailable(false), isScheduled(false), |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 65 | Latency(0), CycleBound(0), NodeNum(nodenum) {} |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 66 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 67 | void dump(const SelectionDAG *G) const; |
| 68 | void dumpAll(const SelectionDAG *G) const; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 69 | }; |
| 70 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 71 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 72 | void SUnit::dump(const SelectionDAG *G) const { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 73 | std::cerr << "SU: "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 74 | Node->dump(G); |
| 75 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 76 | if (FlaggedNodes.size() != 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 77 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 78 | std::cerr << " "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 79 | FlaggedNodes[i]->dump(G); |
| 80 | std::cerr << "\n"; |
| 81 | } |
| 82 | } |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 83 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 84 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 85 | void SUnit::dumpAll(const SelectionDAG *G) const { |
| 86 | dump(G); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 87 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 88 | std::cerr << " # preds left : " << NumPredsLeft << "\n"; |
| 89 | std::cerr << " # succs left : " << NumSuccsLeft << "\n"; |
| 90 | std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n"; |
| 91 | std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n"; |
| 92 | std::cerr << " Latency : " << Latency << "\n"; |
| 93 | |
| 94 | if (Preds.size() != 0) { |
| 95 | std::cerr << " Predecessors:\n"; |
| 96 | for (std::set<SUnit*>::const_iterator I = Preds.begin(), |
| 97 | E = Preds.end(); I != E; ++I) { |
| 98 | std::cerr << " "; |
| 99 | (*I)->dump(G); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 102 | if (ChainPreds.size() != 0) { |
| 103 | std::cerr << " Chained Preds:\n"; |
| 104 | for (std::set<SUnit*>::const_iterator I = ChainPreds.begin(), |
| 105 | E = ChainPreds.end(); I != E; ++I) { |
| 106 | std::cerr << " "; |
| 107 | (*I)->dump(G); |
| 108 | } |
| 109 | } |
| 110 | if (Succs.size() != 0) { |
| 111 | std::cerr << " Successors:\n"; |
| 112 | for (std::set<SUnit*>::const_iterator I = Succs.begin(), |
| 113 | E = Succs.end(); I != E; ++I) { |
| 114 | std::cerr << " "; |
| 115 | (*I)->dump(G); |
| 116 | } |
| 117 | } |
| 118 | if (ChainSuccs.size() != 0) { |
| 119 | std::cerr << " Chained succs:\n"; |
| 120 | for (std::set<SUnit*>::const_iterator I = ChainSuccs.begin(), |
| 121 | E = ChainSuccs.end(); I != E; ++I) { |
| 122 | std::cerr << " "; |
| 123 | (*I)->dump(G); |
| 124 | } |
| 125 | } |
| 126 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 130 | /// SchedulingPriorityQueue - This interface is used to plug different |
| 131 | /// priorities computation algorithms into the list scheduler. It implements the |
| 132 | /// interface of a standard priority queue, where nodes are inserted in |
| 133 | /// arbitrary order and returned in priority order. The computation of the |
| 134 | /// priority and the representation of the queue are totally up to the |
| 135 | /// implementation to decide. |
| 136 | /// |
| 137 | namespace { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 138 | class SchedulingPriorityQueue { |
| 139 | public: |
| 140 | virtual ~SchedulingPriorityQueue() {} |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 142 | virtual void initNodes(const std::vector<SUnit> &SUnits) = 0; |
| 143 | virtual void releaseState() = 0; |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 145 | virtual bool empty() const = 0; |
| 146 | virtual void push(SUnit *U) = 0; |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 147 | |
| 148 | virtual void push_all(const std::vector<SUnit *> &Nodes) = 0; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 149 | virtual SUnit *pop() = 0; |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 150 | |
| 151 | /// ScheduledNode - As each node is scheduled, this method is invoked. This |
| 152 | /// allows the priority function to adjust the priority of node that have |
| 153 | /// already been emitted. |
| 154 | virtual void ScheduledNode(SUnit *Node) {} |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 155 | }; |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 157 | |
| 158 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 159 | |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 160 | namespace { |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 161 | //===----------------------------------------------------------------------===// |
| 162 | /// ScheduleDAGList - The actual list scheduler implementation. This supports |
| 163 | /// both top-down and bottom-up scheduling. |
| 164 | /// |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 165 | class ScheduleDAGList : public ScheduleDAG { |
| 166 | private: |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 167 | // SDNode to SUnit mapping (many to one). |
| 168 | std::map<SDNode*, SUnit*> SUnitMap; |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 169 | // The schedule. Null SUnit*'s represent noop instructions. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 170 | std::vector<SUnit*> Sequence; |
| 171 | // Current scheduling cycle. |
| 172 | unsigned CurrCycle; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 173 | |
| 174 | // The scheduling units. |
| 175 | std::vector<SUnit> SUnits; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 177 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 178 | /// it is top-down. |
| 179 | bool isBottomUp; |
| 180 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 181 | /// PriorityQueue - The priority queue to use. |
| 182 | SchedulingPriorityQueue *PriorityQueue; |
| 183 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 184 | /// HazardRec - The hazard recognizer to use. |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 185 | HazardRecognizer *HazardRec; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 186 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 187 | public: |
| 188 | ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb, |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 189 | const TargetMachine &tm, bool isbottomup, |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 190 | SchedulingPriorityQueue *priorityqueue, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 191 | HazardRecognizer *HR) |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 192 | : ScheduleDAG(listSchedulingBURR, dag, bb, tm), |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 193 | CurrCycle(0), isBottomUp(isbottomup), |
| 194 | PriorityQueue(priorityqueue), HazardRec(HR) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 195 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 196 | |
| 197 | ~ScheduleDAGList() { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 198 | delete HazardRec; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 199 | delete PriorityQueue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 200 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 201 | |
| 202 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 203 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 204 | void dumpSchedule() const; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 205 | |
| 206 | private: |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 207 | SUnit *NewSUnit(SDNode *N); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 208 | void ReleasePred(SUnit *PredSU, bool isChain = false); |
| 209 | void ReleaseSucc(SUnit *SuccSU, bool isChain = false); |
| 210 | void ScheduleNodeBottomUp(SUnit *SU); |
| 211 | void ScheduleNodeTopDown(SUnit *SU); |
| 212 | void ListScheduleTopDown(); |
| 213 | void ListScheduleBottomUp(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 214 | void BuildSchedUnits(); |
| 215 | void EmitSchedule(); |
| 216 | }; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 217 | } // end anonymous namespace |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 219 | HazardRecognizer::~HazardRecognizer() {} |
| 220 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 221 | |
| 222 | /// NewSUnit - Creates a new SUnit and return a ptr to it. |
| 223 | SUnit *ScheduleDAGList::NewSUnit(SDNode *N) { |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 224 | SUnits.push_back(SUnit(N, SUnits.size())); |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 225 | return &SUnits.back(); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 229 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 230 | void ScheduleDAGList::ReleasePred(SUnit *PredSU, bool isChain) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 231 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 232 | // latency. For example, the reader can very well read the register written |
| 233 | // by the predecessor later than the issue cycle. It also depends on the |
| 234 | // interrupt model (drain vs. freeze). |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 235 | PredSU->CycleBound = std::max(PredSU->CycleBound,CurrCycle + PredSU->Latency); |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 236 | |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 237 | if (!isChain) |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 238 | PredSU->NumSuccsLeft--; |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 239 | else |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 240 | PredSU->NumChainSuccsLeft--; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 241 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 242 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 243 | if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 244 | std::cerr << "*** List scheduling failed! ***\n"; |
| 245 | PredSU->dump(&DAG); |
| 246 | std::cerr << " has been released too many times!\n"; |
| 247 | assert(0); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 248 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 249 | #endif |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 250 | |
| 251 | if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) { |
| 252 | // EntryToken has to go last! Special case it here. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 253 | if (PredSU->Node->getOpcode() != ISD::EntryToken) { |
| 254 | PredSU->isAvailable = true; |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 255 | PriorityQueue->push(PredSU); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 256 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 257 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 260 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 261 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 262 | void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 263 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 264 | // latency. For example, the reader can very well read the register written |
| 265 | // by the predecessor later than the issue cycle. It also depends on the |
| 266 | // interrupt model (drain vs. freeze). |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 267 | SuccSU->CycleBound = std::max(SuccSU->CycleBound,CurrCycle + SuccSU->Latency); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 268 | |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 269 | if (!isChain) |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 270 | SuccSU->NumPredsLeft--; |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 271 | else |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 272 | SuccSU->NumChainPredsLeft--; |
| 273 | |
| 274 | #ifndef NDEBUG |
| 275 | if (SuccSU->NumPredsLeft < 0 || SuccSU->NumChainPredsLeft < 0) { |
| 276 | std::cerr << "*** List scheduling failed! ***\n"; |
| 277 | SuccSU->dump(&DAG); |
| 278 | std::cerr << " has been released too many times!\n"; |
| 279 | abort(); |
| 280 | } |
| 281 | #endif |
| 282 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 283 | if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) { |
| 284 | SuccSU->isAvailable = true; |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 285 | PriorityQueue->push(SuccSU); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 286 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 290 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 291 | /// the Available queue. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 292 | void ScheduleDAGList::ScheduleNodeBottomUp(SUnit *SU) { |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 293 | DEBUG(std::cerr << "*** Scheduling: "); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 294 | DEBUG(SU->dump(&DAG)); |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 295 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 296 | Sequence.push_back(SU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 297 | |
| 298 | // Bottom up: release predecessors |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 299 | for (std::set<SUnit*>::iterator I1 = SU->Preds.begin(), |
| 300 | E1 = SU->Preds.end(); I1 != E1; ++I1) { |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 301 | ReleasePred(*I1); |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 302 | SU->NumPredsLeft--; |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 303 | } |
| 304 | for (std::set<SUnit*>::iterator I2 = SU->ChainPreds.begin(), |
| 305 | E2 = SU->ChainPreds.end(); I2 != E2; ++I2) |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 306 | ReleasePred(*I2, true); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 307 | |
| 308 | CurrCycle++; |
| 309 | } |
| 310 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 311 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 312 | /// count of its successors. If a successor pending count is zero, add it to |
| 313 | /// the Available queue. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 314 | void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 315 | DEBUG(std::cerr << "*** Scheduling: "); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 316 | DEBUG(SU->dump(&DAG)); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 317 | |
| 318 | Sequence.push_back(SU); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 319 | |
| 320 | // Bottom up: release successors. |
| 321 | for (std::set<SUnit*>::iterator I1 = SU->Succs.begin(), |
| 322 | E1 = SU->Succs.end(); I1 != E1; ++I1) { |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 323 | ReleaseSucc(*I1); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 324 | SU->NumSuccsLeft--; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 325 | } |
| 326 | for (std::set<SUnit*>::iterator I2 = SU->ChainSuccs.begin(), |
| 327 | E2 = SU->ChainSuccs.end(); I2 != E2; ++I2) |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 328 | ReleaseSucc(*I2, true); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 329 | |
| 330 | CurrCycle++; |
| 331 | } |
| 332 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 333 | /// isReady - True if node's lower cycle bound is less or equal to the current |
| 334 | /// scheduling cycle. Always true if all nodes have uniform latency 1. |
| 335 | static inline bool isReady(SUnit *SU, unsigned CurrCycle) { |
| 336 | return SU->CycleBound <= CurrCycle; |
| 337 | } |
| 338 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 339 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 340 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 341 | void ScheduleDAGList::ListScheduleBottomUp() { |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 342 | // Add root to Available queue. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 343 | PriorityQueue->push(SUnitMap[DAG.getRoot().Val]); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 344 | |
| 345 | // While Available queue is not empty, grab the node with the highest |
| 346 | // priority. If it is not ready put it back. Schedule the node. |
| 347 | std::vector<SUnit*> NotReady; |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 348 | while (!PriorityQueue->empty()) { |
| 349 | SUnit *CurrNode = PriorityQueue->pop(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 350 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 351 | while (!isReady(CurrNode, CurrCycle)) { |
| 352 | NotReady.push_back(CurrNode); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 353 | CurrNode = PriorityQueue->pop(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 354 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 355 | |
| 356 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 357 | PriorityQueue->push_all(NotReady); |
| 358 | NotReady.clear(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 360 | ScheduleNodeBottomUp(CurrNode); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 361 | CurrNode->isScheduled = true; |
| 362 | PriorityQueue->ScheduledNode(CurrNode); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | // Add entry node last |
| 366 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
| 367 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 368 | Sequence.push_back(Entry); |
| 369 | } |
| 370 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 371 | // Reverse the order if it is bottom up. |
| 372 | std::reverse(Sequence.begin(), Sequence.end()); |
| 373 | |
| 374 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 375 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 376 | // Verify that all SUnits were scheduled. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 377 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 378 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 379 | if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 380 | if (!AnyNotSched) |
| 381 | std::cerr << "*** List scheduling failed! ***\n"; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 382 | SUnits[i].dump(&DAG); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 383 | std::cerr << "has not been scheduled!\n"; |
| 384 | AnyNotSched = true; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 385 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 386 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 387 | assert(!AnyNotSched); |
Reid Spencer | 5edde66 | 2006-01-25 21:49:13 +0000 | [diff] [blame] | 388 | #endif |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 391 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 392 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 393 | void ScheduleDAGList::ListScheduleTopDown() { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 394 | // Emit the entry node first. |
| 395 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 396 | ScheduleNodeTopDown(Entry); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 397 | HazardRec->EmitInstruction(Entry->Node); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 399 | // All leaves to Available queue. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 400 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 401 | // It is available if it has no predecessors. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 402 | if ((SUnits[i].Preds.size() + SUnits[i].ChainPreds.size()) == 0 && |
| 403 | &SUnits[i] != Entry) |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 404 | PriorityQueue->push(&SUnits[i]); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // While Available queue is not empty, grab the node with the highest |
| 408 | // priority. If it is not ready put it back. Schedule the node. |
| 409 | std::vector<SUnit*> NotReady; |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 410 | while (!PriorityQueue->empty()) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 411 | SUnit *FoundNode = 0; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 412 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 413 | bool HasNoopHazards = false; |
| 414 | do { |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 415 | SUnit *CurNode = PriorityQueue->pop(); |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 416 | |
| 417 | // Get the node represented by this SUnit. |
| 418 | SDNode *N = CurNode->Node; |
| 419 | // If this is a pseudo op, like copyfromreg, look to see if there is a |
| 420 | // real target node flagged to it. If so, use the target node. |
| 421 | for (unsigned i = 0, e = CurNode->FlaggedNodes.size(); |
| 422 | N->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i) |
| 423 | N = CurNode->FlaggedNodes[i]; |
| 424 | |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 425 | HazardRecognizer::HazardType HT = HazardRec->getHazardType(N); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 426 | if (HT == HazardRecognizer::NoHazard) { |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 427 | FoundNode = CurNode; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | |
| 431 | // Remember if this is a noop hazard. |
| 432 | HasNoopHazards |= HT == HazardRecognizer::NoopHazard; |
| 433 | |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 434 | NotReady.push_back(CurNode); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 435 | } while (!PriorityQueue->empty()); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 437 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 438 | PriorityQueue->push_all(NotReady); |
| 439 | NotReady.clear(); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 440 | |
| 441 | // If we found a node to schedule, do it now. |
| 442 | if (FoundNode) { |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 443 | ScheduleNodeTopDown(FoundNode); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 444 | HazardRec->EmitInstruction(FoundNode->Node); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 445 | FoundNode->isScheduled = true; |
| 446 | PriorityQueue->ScheduledNode(FoundNode); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 447 | } else if (!HasNoopHazards) { |
| 448 | // Otherwise, we have a pipeline stall, but no other problem, just advance |
| 449 | // the current cycle and try again. |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 450 | DEBUG(std::cerr << "*** Advancing cycle, no work to do\n"); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 451 | HazardRec->AdvanceCycle(); |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 452 | ++NumStalls; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 453 | } else { |
| 454 | // Otherwise, we have no instructions to issue and we have instructions |
| 455 | // that will fault if we don't do this right. This is the case for |
| 456 | // processors without pipeline interlocks and other cases. |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 457 | DEBUG(std::cerr << "*** Emitting noop\n"); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 458 | HazardRec->EmitNoop(); |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 459 | Sequence.push_back(0); // NULL SUnit* -> noop |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 460 | ++NumNoops; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 461 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | #ifndef NDEBUG |
| 465 | // Verify that all SUnits were scheduled. |
| 466 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 467 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 468 | if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 469 | if (!AnyNotSched) |
| 470 | std::cerr << "*** List scheduling failed! ***\n"; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 471 | SUnits[i].dump(&DAG); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 472 | std::cerr << "has not been scheduled!\n"; |
| 473 | AnyNotSched = true; |
| 474 | } |
| 475 | } |
| 476 | assert(!AnyNotSched); |
| 477 | #endif |
| 478 | } |
| 479 | |
| 480 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 481 | void ScheduleDAGList::BuildSchedUnits() { |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 482 | // Reserve entries in the vector for each of the SUnits we are creating. This |
| 483 | // ensure that reallocation of the vector won't happen, so SUnit*'s won't get |
| 484 | // invalidated. |
| 485 | SUnits.reserve(NodeCount); |
| 486 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 487 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 488 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 489 | // Pass 1: create the SUnit's. |
Jeff Cohen | fb20616 | 2006-01-25 17:17:49 +0000 | [diff] [blame] | 490 | for (unsigned i = 0, NC = NodeCount; i < NC; i++) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 491 | NodeInfo *NI = &Info[i]; |
| 492 | SDNode *N = NI->Node; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 493 | if (isPassiveNode(N)) |
| 494 | continue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 495 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 496 | SUnit *SU; |
| 497 | if (NI->isInGroup()) { |
| 498 | if (NI != NI->Group->getBottom()) // Bottom up, so only look at bottom |
| 499 | continue; // node of the NodeGroup |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 500 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 501 | SU = NewSUnit(N); |
| 502 | // Find the flagged nodes. |
| 503 | SDOperand FlagOp = N->getOperand(N->getNumOperands() - 1); |
| 504 | SDNode *Flag = FlagOp.Val; |
| 505 | unsigned ResNo = FlagOp.ResNo; |
| 506 | while (Flag->getValueType(ResNo) == MVT::Flag) { |
| 507 | NodeInfo *FNI = getNI(Flag); |
| 508 | assert(FNI->Group == NI->Group); |
| 509 | SU->FlaggedNodes.insert(SU->FlaggedNodes.begin(), Flag); |
| 510 | SUnitMap[Flag] = SU; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 511 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 512 | FlagOp = Flag->getOperand(Flag->getNumOperands() - 1); |
| 513 | Flag = FlagOp.Val; |
| 514 | ResNo = FlagOp.ResNo; |
| 515 | } |
| 516 | } else { |
| 517 | SU = NewSUnit(N); |
| 518 | } |
| 519 | SUnitMap[N] = SU; |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 520 | |
| 521 | // Compute the latency for the node. We use the sum of the latencies for |
| 522 | // all nodes flagged together into this SUnit. |
Chris Lattner | c6c9e65 | 2006-03-09 17:31:22 +0000 | [diff] [blame] | 523 | if (InstrItins.isEmpty()) { |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 524 | // No latency information. |
| 525 | SU->Latency = 1; |
| 526 | } else { |
| 527 | SU->Latency = 0; |
| 528 | if (N->isTargetOpcode()) { |
| 529 | unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode()); |
| 530 | InstrStage *S = InstrItins.begin(SchedClass); |
| 531 | InstrStage *E = InstrItins.end(SchedClass); |
| 532 | for (; S != E; ++S) |
| 533 | SU->Latency += S->Cycles; |
| 534 | } |
| 535 | for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) { |
| 536 | SDNode *FNode = SU->FlaggedNodes[i]; |
| 537 | if (FNode->isTargetOpcode()) { |
| 538 | unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode()); |
| 539 | InstrStage *S = InstrItins.begin(SchedClass); |
| 540 | InstrStage *E = InstrItins.end(SchedClass); |
| 541 | for (; S != E; ++S) |
| 542 | SU->Latency += S->Cycles; |
| 543 | } |
| 544 | } |
| 545 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 546 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 547 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 548 | // Pass 2: add the preds, succs, etc. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 549 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 550 | SUnit *SU = &SUnits[i]; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 551 | SDNode *N = SU->Node; |
| 552 | NodeInfo *NI = getNI(N); |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 553 | |
| 554 | if (N->isTargetOpcode() && TII->isTwoAddrInstr(N->getTargetOpcode())) |
| 555 | SU->isTwoAddress = true; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 556 | |
| 557 | if (NI->isInGroup()) { |
| 558 | // Find all predecessors (of the group). |
| 559 | NodeGroupOpIterator NGOI(NI); |
| 560 | while (!NGOI.isEnd()) { |
| 561 | SDOperand Op = NGOI.next(); |
| 562 | SDNode *OpN = Op.Val; |
| 563 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 564 | NodeInfo *OpNI = getNI(OpN); |
| 565 | if (OpNI->Group != NI->Group && !isPassiveNode(OpN)) { |
| 566 | assert(VT != MVT::Flag); |
| 567 | SUnit *OpSU = SUnitMap[OpN]; |
| 568 | if (VT == MVT::Other) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 569 | if (SU->ChainPreds.insert(OpSU).second) |
| 570 | SU->NumChainPredsLeft++; |
| 571 | if (OpSU->ChainSuccs.insert(SU).second) |
| 572 | OpSU->NumChainSuccsLeft++; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 573 | } else { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 574 | if (SU->Preds.insert(OpSU).second) |
| 575 | SU->NumPredsLeft++; |
| 576 | if (OpSU->Succs.insert(SU).second) |
| 577 | OpSU->NumSuccsLeft++; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 578 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 581 | } else { |
| 582 | // Find node predecessors. |
| 583 | for (unsigned j = 0, e = N->getNumOperands(); j != e; j++) { |
| 584 | SDOperand Op = N->getOperand(j); |
| 585 | SDNode *OpN = Op.Val; |
| 586 | MVT::ValueType VT = OpN->getValueType(Op.ResNo); |
| 587 | if (!isPassiveNode(OpN)) { |
| 588 | assert(VT != MVT::Flag); |
| 589 | SUnit *OpSU = SUnitMap[OpN]; |
| 590 | if (VT == MVT::Other) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 591 | if (SU->ChainPreds.insert(OpSU).second) |
| 592 | SU->NumChainPredsLeft++; |
| 593 | if (OpSU->ChainSuccs.insert(SU).second) |
| 594 | OpSU->NumChainSuccsLeft++; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 595 | } else { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 596 | if (SU->Preds.insert(OpSU).second) |
| 597 | SU->NumPredsLeft++; |
| 598 | if (OpSU->Succs.insert(SU).second) |
| 599 | OpSU->NumSuccsLeft++; |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 600 | if (j == 0 && SU->isTwoAddress) |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 601 | OpSU->isDefNUseOperand = true; |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 602 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 606 | |
| 607 | DEBUG(SU->dumpAll(&DAG)); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 608 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 612 | void ScheduleDAGList::EmitSchedule() { |
| 613 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
Chris Lattner | 2d945ba | 2006-03-05 23:51:47 +0000 | [diff] [blame] | 614 | if (SUnit *SU = Sequence[i]) { |
| 615 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) { |
| 616 | SDNode *N = SU->FlaggedNodes[j]; |
| 617 | EmitNode(getNI(N)); |
| 618 | } |
| 619 | EmitNode(getNI(SU->Node)); |
| 620 | } else { |
| 621 | // Null SUnit* is a noop. |
| 622 | EmitNoop(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 623 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /// dump - dump the schedule. |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 628 | void ScheduleDAGList::dumpSchedule() const { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 629 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
Chris Lattner | 2d945ba | 2006-03-05 23:51:47 +0000 | [diff] [blame] | 630 | if (SUnit *SU = Sequence[i]) |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 631 | SU->dump(&DAG); |
Chris Lattner | 2d945ba | 2006-03-05 23:51:47 +0000 | [diff] [blame] | 632 | else |
| 633 | std::cerr << "**** NOOP ****\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /// Schedule - Schedule the DAG using list scheduling. |
| 638 | /// FIXME: Right now it only supports the burr (bottom up register reducing) |
| 639 | /// heuristic. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 640 | void ScheduleDAGList::Schedule() { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 641 | DEBUG(std::cerr << "********** List Scheduling **********\n"); |
| 642 | |
| 643 | // Build scheduling units. |
| 644 | BuildSchedUnits(); |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 646 | PriorityQueue->initNodes(SUnits); |
| 647 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 648 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 649 | if (isBottomUp) |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 650 | ListScheduleBottomUp(); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 651 | else |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 652 | ListScheduleTopDown(); |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 653 | |
| 654 | PriorityQueue->releaseState(); |
| 655 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 656 | DEBUG(std::cerr << "*** Final schedule ***\n"); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 657 | DEBUG(dumpSchedule()); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 658 | DEBUG(std::cerr << "\n"); |
| 659 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 660 | // Emit in scheduled order |
| 661 | EmitSchedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
| 665 | // RegReductionPriorityQueue Implementation |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | // |
| 668 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 669 | // to reduce register pressure. |
| 670 | // |
| 671 | namespace { |
| 672 | class RegReductionPriorityQueue; |
| 673 | |
| 674 | /// Sorting functions for the Available queue. |
| 675 | struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 676 | RegReductionPriorityQueue *SPQ; |
| 677 | ls_rr_sort(RegReductionPriorityQueue *spq) : SPQ(spq) {} |
| 678 | ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 679 | |
| 680 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 681 | }; |
| 682 | } // end anonymous namespace |
| 683 | |
| 684 | namespace { |
| 685 | class RegReductionPriorityQueue : public SchedulingPriorityQueue { |
| 686 | // SUnits - The SUnits for the current graph. |
| 687 | const std::vector<SUnit> *SUnits; |
| 688 | |
| 689 | // SethiUllmanNumbers - The SethiUllman number for each node. |
| 690 | std::vector<int> SethiUllmanNumbers; |
| 691 | |
| 692 | std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue; |
| 693 | public: |
| 694 | RegReductionPriorityQueue() : Queue(ls_rr_sort(this)) { |
| 695 | } |
| 696 | |
| 697 | void initNodes(const std::vector<SUnit> &sunits) { |
| 698 | SUnits = &sunits; |
| 699 | // Calculate node priorities. |
| 700 | CalculatePriorities(); |
| 701 | } |
| 702 | void releaseState() { |
| 703 | SUnits = 0; |
| 704 | SethiUllmanNumbers.clear(); |
| 705 | } |
| 706 | |
| 707 | unsigned getSethiUllmanNumber(unsigned NodeNum) const { |
| 708 | assert(NodeNum < SethiUllmanNumbers.size()); |
| 709 | return SethiUllmanNumbers[NodeNum]; |
| 710 | } |
| 711 | |
| 712 | bool empty() const { return Queue.empty(); } |
| 713 | |
| 714 | void push(SUnit *U) { |
| 715 | Queue.push(U); |
| 716 | } |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 717 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 718 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 719 | Queue.push(Nodes[i]); |
| 720 | } |
| 721 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 722 | SUnit *pop() { |
| 723 | SUnit *V = Queue.top(); |
| 724 | Queue.pop(); |
| 725 | return V; |
| 726 | } |
| 727 | private: |
| 728 | void CalculatePriorities(); |
| 729 | int CalcNodePriority(const SUnit *SU); |
| 730 | }; |
| 731 | } |
| 732 | |
| 733 | bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
| 734 | unsigned LeftNum = left->NodeNum; |
| 735 | unsigned RightNum = right->NodeNum; |
| 736 | |
| 737 | int LBonus = (int)left ->isDefNUseOperand; |
| 738 | int RBonus = (int)right->isDefNUseOperand; |
| 739 | |
| 740 | // Special tie breaker: if two nodes share a operand, the one that |
| 741 | // use it as a def&use operand is preferred. |
| 742 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 743 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 744 | if (DUNode->isOperand(right->Node)) |
| 745 | LBonus++; |
| 746 | } |
| 747 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 748 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 749 | if (DUNode->isOperand(left->Node)) |
| 750 | RBonus++; |
| 751 | } |
| 752 | |
| 753 | // Priority1 is just the number of live range genned. |
| 754 | int LPriority1 = left ->NumPredsLeft - LBonus; |
| 755 | int RPriority1 = right->NumPredsLeft - RBonus; |
| 756 | int LPriority2 = SPQ->getSethiUllmanNumber(LeftNum) + LBonus; |
| 757 | int RPriority2 = SPQ->getSethiUllmanNumber(RightNum) + RBonus; |
| 758 | |
| 759 | if (LPriority1 > RPriority1) |
| 760 | return true; |
| 761 | else if (LPriority1 == RPriority1) |
| 762 | if (LPriority2 < RPriority2) |
| 763 | return true; |
| 764 | else if (LPriority2 == RPriority2) |
| 765 | if (left->CycleBound > right->CycleBound) |
| 766 | return true; |
| 767 | |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | |
| 772 | /// CalcNodePriority - Priority is the Sethi Ullman number. |
| 773 | /// Smaller number is the higher priority. |
| 774 | int RegReductionPriorityQueue::CalcNodePriority(const SUnit *SU) { |
| 775 | int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
| 776 | if (SethiUllmanNumber != INT_MIN) |
| 777 | return SethiUllmanNumber; |
| 778 | |
| 779 | if (SU->Preds.size() == 0) { |
| 780 | SethiUllmanNumber = 1; |
| 781 | } else { |
| 782 | int Extra = 0; |
Jeff Cohen | 6ce9768 | 2006-03-10 03:57:45 +0000 | [diff] [blame] | 783 | for (std::set<SUnit*>::const_iterator I = SU->Preds.begin(), |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 784 | E = SU->Preds.end(); I != E; ++I) { |
| 785 | SUnit *PredSU = *I; |
| 786 | int PredSethiUllman = CalcNodePriority(PredSU); |
| 787 | if (PredSethiUllman > SethiUllmanNumber) { |
| 788 | SethiUllmanNumber = PredSethiUllman; |
| 789 | Extra = 0; |
| 790 | } else if (PredSethiUllman == SethiUllmanNumber) |
| 791 | Extra++; |
| 792 | } |
| 793 | |
| 794 | if (SU->Node->getOpcode() != ISD::TokenFactor) |
| 795 | SethiUllmanNumber += Extra; |
| 796 | else |
| 797 | SethiUllmanNumber = (Extra == 1) ? 0 : Extra-1; |
| 798 | } |
| 799 | |
| 800 | return SethiUllmanNumber; |
| 801 | } |
| 802 | |
| 803 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 804 | void RegReductionPriorityQueue::CalculatePriorities() { |
| 805 | SethiUllmanNumbers.assign(SUnits->size(), INT_MIN); |
| 806 | |
| 807 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
| 808 | CalcNodePriority(&(*SUnits)[i]); |
| 809 | } |
| 810 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 811 | //===----------------------------------------------------------------------===// |
| 812 | // LatencyPriorityQueue Implementation |
| 813 | //===----------------------------------------------------------------------===// |
| 814 | // |
| 815 | // This is a SchedulingPriorityQueue that schedules using latency information to |
| 816 | // reduce the length of the critical path through the basic block. |
| 817 | // |
| 818 | namespace { |
| 819 | class LatencyPriorityQueue; |
| 820 | |
| 821 | /// Sorting functions for the Available queue. |
| 822 | struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 823 | LatencyPriorityQueue *PQ; |
| 824 | latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {} |
| 825 | latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {} |
| 826 | |
| 827 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 828 | }; |
| 829 | } // end anonymous namespace |
| 830 | |
| 831 | namespace { |
| 832 | class LatencyPriorityQueue : public SchedulingPriorityQueue { |
| 833 | // SUnits - The SUnits for the current graph. |
| 834 | const std::vector<SUnit> *SUnits; |
| 835 | |
| 836 | // Latencies - The latency (max of latency from this node to the bb exit) |
| 837 | // for each node. |
| 838 | std::vector<int> Latencies; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 839 | |
| 840 | /// NumNodesSolelyBlocking - This vector contains, for every node in the |
| 841 | /// Queue, the number of nodes that the node is the sole unscheduled |
| 842 | /// predecessor for. This is used as a tie-breaker heuristic for better |
| 843 | /// mobility. |
| 844 | std::vector<unsigned> NumNodesSolelyBlocking; |
| 845 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 846 | std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue; |
| 847 | public: |
| 848 | LatencyPriorityQueue() : Queue(latency_sort(this)) { |
| 849 | } |
| 850 | |
| 851 | void initNodes(const std::vector<SUnit> &sunits) { |
| 852 | SUnits = &sunits; |
| 853 | // Calculate node priorities. |
| 854 | CalculatePriorities(); |
| 855 | } |
| 856 | void releaseState() { |
| 857 | SUnits = 0; |
| 858 | Latencies.clear(); |
| 859 | } |
| 860 | |
| 861 | unsigned getLatency(unsigned NodeNum) const { |
| 862 | assert(NodeNum < Latencies.size()); |
| 863 | return Latencies[NodeNum]; |
| 864 | } |
| 865 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 866 | unsigned getNumSolelyBlockNodes(unsigned NodeNum) const { |
| 867 | assert(NodeNum < NumNodesSolelyBlocking.size()); |
| 868 | return NumNodesSolelyBlocking[NodeNum]; |
| 869 | } |
| 870 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 871 | bool empty() const { return Queue.empty(); } |
| 872 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 873 | virtual void push(SUnit *U) { |
| 874 | push_impl(U); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 875 | } |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 876 | void push_impl(SUnit *U); |
| 877 | |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 878 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 879 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 880 | push_impl(Nodes[i]); |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 883 | SUnit *pop() { |
| 884 | SUnit *V = Queue.top(); |
| 885 | Queue.pop(); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 886 | return V; |
| 887 | } |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 888 | |
| 889 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 890 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 891 | // single predecessor has a higher priority, since scheduling it will make |
| 892 | // the node available. |
| 893 | void ScheduledNode(SUnit *Node); |
| 894 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 895 | private: |
| 896 | void CalculatePriorities(); |
| 897 | int CalcLatency(const SUnit &SU); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 898 | void AdjustPriorityOfUnscheduledPreds(SUnit *SU); |
| 899 | |
| 900 | /// RemoveFromPriorityQueue - This is a really inefficient way to remove a |
| 901 | /// node from a priority queue. We should roll our own heap to make this |
| 902 | /// better or something. |
| 903 | void RemoveFromPriorityQueue(SUnit *SU) { |
| 904 | std::vector<SUnit*> Temp; |
| 905 | |
| 906 | assert(!Queue.empty() && "Not in queue!"); |
| 907 | while (Queue.top() != SU) { |
| 908 | Temp.push_back(Queue.top()); |
| 909 | Queue.pop(); |
| 910 | assert(!Queue.empty() && "Not in queue!"); |
| 911 | } |
| 912 | |
| 913 | // Remove the node from the PQ. |
| 914 | Queue.pop(); |
| 915 | |
| 916 | // Add all the other nodes back. |
| 917 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 918 | Queue.push(Temp[i]); |
| 919 | } |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 920 | }; |
| 921 | } |
| 922 | |
| 923 | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
| 924 | unsigned LHSNum = LHS->NodeNum; |
| 925 | unsigned RHSNum = RHS->NodeNum; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 926 | |
| 927 | // The most important heuristic is scheduling the critical path. |
| 928 | unsigned LHSLatency = PQ->getLatency(LHSNum); |
| 929 | unsigned RHSLatency = PQ->getLatency(RHSNum); |
| 930 | if (LHSLatency < RHSLatency) return true; |
| 931 | if (LHSLatency > RHSLatency) return false; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 933 | // After that, if two nodes have identical latencies, look to see if one will |
| 934 | // unblock more other nodes than the other. |
| 935 | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
| 936 | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
| 937 | if (LHSBlocked < RHSBlocked) return true; |
| 938 | if (LHSBlocked > RHSBlocked) return false; |
| 939 | |
| 940 | // Finally, just to provide a stable ordering, use the node number as a |
| 941 | // deciding factor. |
| 942 | return LHSNum < RHSNum; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | |
| 946 | /// CalcNodePriority - Calculate the maximal path from the node to the exit. |
| 947 | /// |
| 948 | int LatencyPriorityQueue::CalcLatency(const SUnit &SU) { |
| 949 | int &Latency = Latencies[SU.NodeNum]; |
| 950 | if (Latency != -1) |
| 951 | return Latency; |
| 952 | |
| 953 | int MaxSuccLatency = 0; |
Jeff Cohen | 6ce9768 | 2006-03-10 03:57:45 +0000 | [diff] [blame] | 954 | for (std::set<SUnit*>::const_iterator I = SU.Succs.begin(), |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 955 | E = SU.Succs.end(); I != E; ++I) |
| 956 | MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I)); |
| 957 | |
Jeff Cohen | 6ce9768 | 2006-03-10 03:57:45 +0000 | [diff] [blame] | 958 | for (std::set<SUnit*>::const_iterator I = SU.ChainSuccs.begin(), |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 959 | E = SU.ChainSuccs.end(); I != E; ++I) |
| 960 | MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(**I)); |
| 961 | |
| 962 | return Latency = MaxSuccLatency + SU.Latency; |
| 963 | } |
| 964 | |
| 965 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 966 | void LatencyPriorityQueue::CalculatePriorities() { |
| 967 | Latencies.assign(SUnits->size(), -1); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 968 | NumNodesSolelyBlocking.assign(SUnits->size(), 0); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 969 | |
| 970 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
| 971 | CalcLatency((*SUnits)[i]); |
| 972 | } |
| 973 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame^] | 974 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
| 975 | /// of SU, return it, otherwise return null. |
| 976 | static SUnit *getSingleUnscheduledPred(SUnit *SU) { |
| 977 | SUnit *OnlyAvailablePred = 0; |
| 978 | for (std::set<SUnit*>::const_iterator I = SU->Preds.begin(), |
| 979 | E = SU->Preds.end(); I != E; ++I) |
| 980 | if (!(*I)->isScheduled) { |
| 981 | // We found an available, but not scheduled, predecessor. If it's the |
| 982 | // only one we have found, keep track of it... otherwise give up. |
| 983 | if (OnlyAvailablePred && OnlyAvailablePred != *I) |
| 984 | return 0; |
| 985 | OnlyAvailablePred = *I; |
| 986 | } |
| 987 | |
| 988 | for (std::set<SUnit*>::const_iterator I = SU->ChainSuccs.begin(), |
| 989 | E = SU->ChainSuccs.end(); I != E; ++I) |
| 990 | if (!(*I)->isScheduled) { |
| 991 | // We found an available, but not scheduled, predecessor. If it's the |
| 992 | // only one we have found, keep track of it... otherwise give up. |
| 993 | if (OnlyAvailablePred && OnlyAvailablePred != *I) |
| 994 | return 0; |
| 995 | OnlyAvailablePred = *I; |
| 996 | } |
| 997 | |
| 998 | return OnlyAvailablePred; |
| 999 | } |
| 1000 | |
| 1001 | void LatencyPriorityQueue::push_impl(SUnit *SU) { |
| 1002 | // Look at all of the successors of this node. Count the number of nodes that |
| 1003 | // this node is the sole unscheduled node for. |
| 1004 | unsigned NumNodesBlocking = 0; |
| 1005 | for (std::set<SUnit*>::const_iterator I = SU->Succs.begin(), |
| 1006 | E = SU->Succs.end(); I != E; ++I) |
| 1007 | if (getSingleUnscheduledPred(*I) == SU) |
| 1008 | ++NumNodesBlocking; |
| 1009 | |
| 1010 | for (std::set<SUnit*>::const_iterator I = SU->ChainSuccs.begin(), |
| 1011 | E = SU->ChainSuccs.end(); I != E; ++I) |
| 1012 | if (getSingleUnscheduledPred(*I) == SU) |
| 1013 | ++NumNodesBlocking; |
| 1014 | |
| 1015 | Queue.push(SU); |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 1020 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 1021 | // single predecessor has a higher priority, since scheduling it will make |
| 1022 | // the node available. |
| 1023 | void LatencyPriorityQueue::ScheduledNode(SUnit *SU) { |
| 1024 | for (std::set<SUnit*>::const_iterator I = SU->Succs.begin(), |
| 1025 | E = SU->Succs.end(); I != E; ++I) |
| 1026 | AdjustPriorityOfUnscheduledPreds(*I); |
| 1027 | |
| 1028 | for (std::set<SUnit*>::const_iterator I = SU->ChainSuccs.begin(), |
| 1029 | E = SU->ChainSuccs.end(); I != E; ++I) |
| 1030 | AdjustPriorityOfUnscheduledPreds(*I); |
| 1031 | } |
| 1032 | |
| 1033 | /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
| 1034 | /// scheduled. If SU is not itself available, then there is at least one |
| 1035 | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
| 1036 | /// unscheduled predecessor, we want to increase its priority: it getting |
| 1037 | /// scheduled will make this node available, so it is better than some other |
| 1038 | /// node of the same priority that will not make a node available. |
| 1039 | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { |
| 1040 | if (SU->isAvailable) return; // All preds scheduled. |
| 1041 | |
| 1042 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
| 1043 | if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return; |
| 1044 | |
| 1045 | // Okay, we found a single predecessor that is available, but not scheduled. |
| 1046 | // Since it is available, it must be in the priority queue. First remove it. |
| 1047 | RemoveFromPriorityQueue(OnlyAvailablePred); |
| 1048 | |
| 1049 | // Reinsert the node into the priority queue, which recomputes its |
| 1050 | // NumNodesSolelyBlocking value. |
| 1051 | push(OnlyAvailablePred); |
| 1052 | } |
| 1053 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1054 | |
| 1055 | //===----------------------------------------------------------------------===// |
| 1056 | // Public Constructor Functions |
| 1057 | //===----------------------------------------------------------------------===// |
| 1058 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 1059 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG, |
| 1060 | MachineBasicBlock *BB) { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1061 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true, |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1062 | new RegReductionPriorityQueue(), |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1063 | new HazardRecognizer()); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 1066 | /// createTDListDAGScheduler - This creates a top-down list scheduler with the |
| 1067 | /// specified hazard recognizer. |
| 1068 | ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG, |
| 1069 | MachineBasicBlock *BB, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1070 | HazardRecognizer *HR) { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1071 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1072 | new LatencyPriorityQueue(), |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1073 | HR); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 1074 | } |