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