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