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 | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 23 | #include "llvm/CodeGen/SSARegMap.h" |
| 24 | #include "llvm/Target/MRegisterInfo.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 29 | #include <climits> |
| 30 | #include <iostream> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 31 | #include <queue> |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 32 | #include <set> |
| 33 | #include <vector> |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 37 | namespace { |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 38 | cl::opt<bool> |
| 39 | SchedVertically("sched-vertically", cl::Hidden); |
| 40 | } |
| 41 | |
| 42 | namespace { |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 43 | Statistic<> NumNoops ("scheduler", "Number of noops inserted"); |
| 44 | Statistic<> NumStalls("scheduler", "Number of pipeline stalls"); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 46 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or |
| 47 | /// a group of nodes flagged together. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 48 | struct SUnit { |
| 49 | SDNode *Node; // Representative node. |
| 50 | std::vector<SDNode*> FlaggedNodes; // All nodes flagged to Node. |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 51 | |
| 52 | // Preds/Succs - The SUnits before/after us in the graph. The boolean value |
| 53 | // is true if the edge is a token chain edge, false if it is a value edge. |
| 54 | std::set<std::pair<SUnit*,bool> > Preds; // All sunit predecessors. |
| 55 | std::set<std::pair<SUnit*,bool> > Succs; // All sunit successors. |
| 56 | |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 57 | short NumPredsLeft; // # of preds not scheduled. |
| 58 | short NumSuccsLeft; // # of succs not scheduled. |
| 59 | short NumChainPredsLeft; // # of chain preds not scheduled. |
| 60 | short NumChainSuccsLeft; // # of chain succs not scheduled. |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 61 | bool isTwoAddress : 1; // Is a two-address instruction. |
| 62 | bool isDefNUseOperand : 1; // Is a def&use operand. |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 63 | bool isPending : 1; // True once pending. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 64 | bool isAvailable : 1; // True once available. |
| 65 | bool isScheduled : 1; // True once scheduled. |
Chris Lattner | 12c6d89 | 2006-03-08 04:41:06 +0000 | [diff] [blame] | 66 | unsigned short Latency; // Node latency. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 67 | unsigned CycleBound; // Upper/lower cycle to be scheduled at. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 68 | unsigned Cycle; // Once scheduled, the cycle of the op. |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 69 | unsigned NodeNum; // Entry # of node in the node vector. |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 70 | |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 71 | SUnit(SDNode *node, unsigned nodenum) |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 72 | : Node(node), NumPredsLeft(0), NumSuccsLeft(0), |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 73 | NumChainPredsLeft(0), NumChainSuccsLeft(0), |
| 74 | isTwoAddress(false), isDefNUseOperand(false), |
| 75 | isPending(false), isAvailable(false), isScheduled(false), |
| 76 | Latency(0), CycleBound(0), Cycle(0), NodeNum(nodenum) {} |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 77 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 78 | void dump(const SelectionDAG *G) const; |
| 79 | void dumpAll(const SelectionDAG *G) const; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 80 | }; |
| 81 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 82 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 83 | void SUnit::dump(const SelectionDAG *G) const { |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 84 | std::cerr << "SU(" << NodeNum << "): "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 85 | Node->dump(G); |
| 86 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 87 | if (FlaggedNodes.size() != 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 88 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 89 | std::cerr << " "; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 90 | FlaggedNodes[i]->dump(G); |
| 91 | std::cerr << "\n"; |
| 92 | } |
| 93 | } |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 94 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 95 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 96 | void SUnit::dumpAll(const SelectionDAG *G) const { |
| 97 | dump(G); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 98 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 99 | std::cerr << " # preds left : " << NumPredsLeft << "\n"; |
| 100 | std::cerr << " # succs left : " << NumSuccsLeft << "\n"; |
| 101 | std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n"; |
| 102 | std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n"; |
| 103 | std::cerr << " Latency : " << Latency << "\n"; |
| 104 | |
| 105 | if (Preds.size() != 0) { |
| 106 | std::cerr << " Predecessors:\n"; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 107 | for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(), |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 108 | E = Preds.end(); I != E; ++I) { |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 109 | if (I->second) |
| 110 | std::cerr << " ch "; |
| 111 | else |
| 112 | std::cerr << " val "; |
| 113 | I->first->dump(G); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | if (Succs.size() != 0) { |
| 117 | std::cerr << " Successors:\n"; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 118 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(), |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 119 | E = Succs.end(); I != E; ++I) { |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 120 | if (I->second) |
| 121 | std::cerr << " ch "; |
| 122 | else |
| 123 | std::cerr << " val "; |
| 124 | I->first->dump(G); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | std::cerr << "\n"; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 131 | /// SchedulingPriorityQueue - This interface is used to plug different |
| 132 | /// priorities computation algorithms into the list scheduler. It implements the |
| 133 | /// interface of a standard priority queue, where nodes are inserted in |
| 134 | /// arbitrary order and returned in priority order. The computation of the |
| 135 | /// priority and the representation of the queue are totally up to the |
| 136 | /// implementation to decide. |
| 137 | /// |
| 138 | namespace { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 139 | class SchedulingPriorityQueue { |
| 140 | public: |
| 141 | virtual ~SchedulingPriorityQueue() {} |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 143 | virtual void initNodes(const std::vector<SUnit> &SUnits) = 0; |
| 144 | virtual void releaseState() = 0; |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 146 | virtual bool empty() const = 0; |
| 147 | virtual void push(SUnit *U) = 0; |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 148 | |
| 149 | virtual void push_all(const std::vector<SUnit *> &Nodes) = 0; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 150 | virtual SUnit *pop() = 0; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 151 | |
| 152 | virtual void RemoveFromPriorityQueue(SUnit *SU) = 0; |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 153 | |
| 154 | /// ScheduledNode - As each node is scheduled, this method is invoked. This |
| 155 | /// allows the priority function to adjust the priority of node that have |
| 156 | /// already been emitted. |
| 157 | virtual void ScheduledNode(SUnit *Node) {} |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 158 | }; |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 160 | |
| 161 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 162 | |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 163 | namespace { |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 164 | //===----------------------------------------------------------------------===// |
| 165 | /// ScheduleDAGList - The actual list scheduler implementation. This supports |
| 166 | /// both top-down and bottom-up scheduling. |
| 167 | /// |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 168 | class ScheduleDAGList : public ScheduleDAG { |
| 169 | private: |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 170 | // SDNode to SUnit mapping (many to one). |
| 171 | std::map<SDNode*, SUnit*> SUnitMap; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 172 | |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 173 | // The schedule. Null SUnit*'s represent noop instructions. |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 174 | std::vector<SUnit*> Sequence; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 175 | |
| 176 | // The scheduling units. |
| 177 | std::vector<SUnit> SUnits; |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 179 | /// isBottomUp - This is true if the scheduling problem is bottom-up, false if |
| 180 | /// it is top-down. |
| 181 | bool isBottomUp; |
| 182 | |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 183 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 184 | /// |
| 185 | SchedulingPriorityQueue *AvailableQueue; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 187 | /// PendingQueue - This contains all of the instructions whose operands have |
| 188 | /// been issued, but their results are not ready yet (due to the latency of |
| 189 | /// the operation). Once the operands becomes available, the instruction is |
| 190 | /// added to the AvailableQueue. This keeps track of each SUnit and the |
| 191 | /// number of cycles left to execute before the operation is available. |
| 192 | std::vector<std::pair<unsigned, SUnit*> > PendingQueue; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 193 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 194 | /// HazardRec - The hazard recognizer to use. |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 195 | HazardRecognizer *HazardRec; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 196 | |
| 197 | /// OpenNodes - Nodes with open live ranges, i.e. predecessors or successors |
| 198 | /// of scheduled nodes which are not themselves scheduled. |
| 199 | std::map<const TargetRegisterClass*, std::set<SUnit*> > OpenNodes; |
| 200 | |
| 201 | std::map<const TargetRegisterClass*, unsigned> RegPressureLimits; |
| 202 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 203 | public: |
| 204 | ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb, |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 205 | const TargetMachine &tm, bool isbottomup, |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 206 | SchedulingPriorityQueue *availqueue, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 207 | HazardRecognizer *HR) |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 208 | : ScheduleDAG(dag, bb, tm), isBottomUp(isbottomup), |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 209 | AvailableQueue(availqueue), HazardRec(HR) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 210 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 211 | |
| 212 | ~ScheduleDAGList() { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 213 | delete HazardRec; |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 214 | delete AvailableQueue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 215 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 216 | |
| 217 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 218 | |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 219 | void dumpSchedule() const; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 220 | |
| 221 | private: |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 222 | SUnit *NewSUnit(SDNode *N); |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 223 | void ReleasePred(SUnit *PredSU, bool isChain, unsigned CurCycle); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 224 | void ReleaseSucc(SUnit *SuccSU, bool isChain); |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 225 | void ScheduleNodeBottomUp(SUnit *SU, unsigned& CurCycle, bool Veritical=true); |
| 226 | void ScheduleVertically(SUnit *SU, unsigned& CurCycle); |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 227 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 228 | void ListScheduleTopDown(); |
| 229 | void ListScheduleBottomUp(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 230 | void BuildSchedUnits(); |
| 231 | void EmitSchedule(); |
| 232 | }; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 233 | } // end anonymous namespace |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 235 | HazardRecognizer::~HazardRecognizer() {} |
| 236 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 237 | |
| 238 | /// NewSUnit - Creates a new SUnit and return a ptr to it. |
| 239 | SUnit *ScheduleDAGList::NewSUnit(SDNode *N) { |
Chris Lattner | fd22d42 | 2006-03-08 05:18:27 +0000 | [diff] [blame] | 240 | SUnits.push_back(SUnit(N, SUnits.size())); |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 241 | return &SUnits.back(); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 244 | /// BuildSchedUnits - Build SUnits from the selection dag that we are input. |
| 245 | /// This SUnit graph is similar to the SelectionDAG, but represents flagged |
| 246 | /// together nodes with a single SUnit. |
| 247 | void ScheduleDAGList::BuildSchedUnits() { |
| 248 | // Reserve entries in the vector for each of the SUnits we are creating. This |
| 249 | // ensure that reallocation of the vector won't happen, so SUnit*'s won't get |
| 250 | // invalidated. |
| 251 | SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end())); |
| 252 | |
| 253 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 254 | |
| 255 | for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(), |
| 256 | E = DAG.allnodes_end(); NI != E; ++NI) { |
| 257 | if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. |
| 258 | continue; |
| 259 | |
| 260 | // If this node has already been processed, stop now. |
| 261 | if (SUnitMap[NI]) continue; |
| 262 | |
| 263 | SUnit *NodeSUnit = NewSUnit(NI); |
| 264 | |
| 265 | // See if anything is flagged to this node, if so, add them to flagged |
| 266 | // nodes. Nodes can have at most one flag input and one flag output. Flags |
| 267 | // are required the be the last operand and result of a node. |
| 268 | |
| 269 | // Scan up, adding flagged preds to FlaggedNodes. |
| 270 | SDNode *N = NI; |
| 271 | while (N->getNumOperands() && |
| 272 | N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) { |
| 273 | N = N->getOperand(N->getNumOperands()-1).Val; |
| 274 | NodeSUnit->FlaggedNodes.push_back(N); |
| 275 | SUnitMap[N] = NodeSUnit; |
| 276 | } |
| 277 | |
| 278 | // Scan down, adding this node and any flagged succs to FlaggedNodes if they |
| 279 | // have a user of the flag operand. |
| 280 | N = NI; |
| 281 | while (N->getValueType(N->getNumValues()-1) == MVT::Flag) { |
| 282 | SDOperand FlagVal(N, N->getNumValues()-1); |
| 283 | |
| 284 | // There are either zero or one users of the Flag result. |
| 285 | bool HasFlagUse = false; |
| 286 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 287 | UI != E; ++UI) |
| 288 | if (FlagVal.isOperand(*UI)) { |
| 289 | HasFlagUse = true; |
| 290 | NodeSUnit->FlaggedNodes.push_back(N); |
| 291 | SUnitMap[N] = NodeSUnit; |
| 292 | N = *UI; |
| 293 | break; |
| 294 | } |
| 295 | if (!HasFlagUse) break; |
| 296 | } |
| 297 | |
| 298 | // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node. |
| 299 | // Update the SUnit |
| 300 | NodeSUnit->Node = N; |
| 301 | SUnitMap[N] = NodeSUnit; |
| 302 | |
| 303 | // Compute the latency for the node. We use the sum of the latencies for |
| 304 | // all nodes flagged together into this SUnit. |
| 305 | if (InstrItins.isEmpty()) { |
| 306 | // No latency information. |
| 307 | NodeSUnit->Latency = 1; |
| 308 | } else { |
| 309 | NodeSUnit->Latency = 0; |
| 310 | if (N->isTargetOpcode()) { |
| 311 | unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode()); |
| 312 | InstrStage *S = InstrItins.begin(SchedClass); |
| 313 | InstrStage *E = InstrItins.end(SchedClass); |
| 314 | for (; S != E; ++S) |
| 315 | NodeSUnit->Latency += S->Cycles; |
| 316 | } |
| 317 | for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) { |
| 318 | SDNode *FNode = NodeSUnit->FlaggedNodes[i]; |
| 319 | if (FNode->isTargetOpcode()) { |
| 320 | unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode()); |
| 321 | InstrStage *S = InstrItins.begin(SchedClass); |
| 322 | InstrStage *E = InstrItins.end(SchedClass); |
| 323 | for (; S != E; ++S) |
| 324 | NodeSUnit->Latency += S->Cycles; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Pass 2: add the preds, succs, etc. |
| 331 | for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { |
| 332 | SUnit *SU = &SUnits[su]; |
| 333 | SDNode *MainNode = SU->Node; |
| 334 | |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 335 | if (MainNode->isTargetOpcode()) { |
| 336 | unsigned Opc = MainNode->getTargetOpcode(); |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 337 | if (TII->isTwoAddrInstr(Opc)) { |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 338 | SU->isTwoAddress = true; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 339 | SDNode *OpN = MainNode->getOperand(0).Val; |
| 340 | SUnit *OpSU = SUnitMap[OpN]; |
| 341 | if (OpSU) |
| 342 | OpSU->isDefNUseOperand = true; |
| 343 | } |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 345 | |
| 346 | // Find all predecessors and successors of the group. |
| 347 | // Temporarily add N to make code simpler. |
| 348 | SU->FlaggedNodes.push_back(MainNode); |
| 349 | |
| 350 | for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) { |
| 351 | SDNode *N = SU->FlaggedNodes[n]; |
| 352 | |
| 353 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 354 | SDNode *OpN = N->getOperand(i).Val; |
| 355 | if (isPassiveNode(OpN)) continue; // Not scheduled. |
| 356 | SUnit *OpSU = SUnitMap[OpN]; |
| 357 | assert(OpSU && "Node has no SUnit!"); |
| 358 | if (OpSU == SU) continue; // In the same group. |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 360 | MVT::ValueType OpVT = N->getOperand(i).getValueType(); |
| 361 | assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!"); |
| 362 | bool isChain = OpVT == MVT::Other; |
| 363 | |
| 364 | if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) { |
| 365 | if (!isChain) { |
| 366 | SU->NumPredsLeft++; |
| 367 | } else { |
| 368 | SU->NumChainPredsLeft++; |
| 369 | } |
| 370 | } |
| 371 | if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) { |
| 372 | if (!isChain) { |
| 373 | OpSU->NumSuccsLeft++; |
| 374 | } else { |
| 375 | OpSU->NumChainSuccsLeft++; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Remove MainNode from FlaggedNodes again. |
| 382 | SU->FlaggedNodes.pop_back(); |
| 383 | } |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 385 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 386 | SUnits[su].dumpAll(&DAG)); |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 387 | return; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 391 | void ScheduleDAGList::EmitSchedule() { |
| 392 | std::map<SDNode*, unsigned> VRBaseMap; |
| 393 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 394 | if (SUnit *SU = Sequence[i]) { |
| 395 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) |
| 396 | EmitNode(SU->FlaggedNodes[j], VRBaseMap); |
| 397 | EmitNode(SU->Node, VRBaseMap); |
| 398 | } else { |
| 399 | // Null SUnit* is a noop. |
| 400 | EmitNoop(); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /// dump - dump the schedule. |
| 406 | void ScheduleDAGList::dumpSchedule() const { |
| 407 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 408 | if (SUnit *SU = Sequence[i]) |
| 409 | SU->dump(&DAG); |
| 410 | else |
| 411 | std::cerr << "**** NOOP ****\n"; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /// Schedule - Schedule the DAG using list scheduling. |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 416 | void ScheduleDAGList::Schedule() { |
| 417 | DEBUG(std::cerr << "********** List Scheduling **********\n"); |
| 418 | |
| 419 | // Build scheduling units. |
| 420 | BuildSchedUnits(); |
| 421 | |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 422 | AvailableQueue->initNodes(SUnits); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 423 | |
| 424 | // Execute the actual scheduling loop Top-Down or Bottom-Up as appropriate. |
| 425 | if (isBottomUp) |
| 426 | ListScheduleBottomUp(); |
| 427 | else |
| 428 | ListScheduleTopDown(); |
| 429 | |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 430 | AvailableQueue->releaseState(); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 431 | |
| 432 | DEBUG(std::cerr << "*** Final schedule ***\n"); |
| 433 | DEBUG(dumpSchedule()); |
| 434 | DEBUG(std::cerr << "\n"); |
| 435 | |
| 436 | // Emit in scheduled order |
| 437 | EmitSchedule(); |
| 438 | } |
| 439 | |
| 440 | //===----------------------------------------------------------------------===// |
| 441 | // Bottom-Up Scheduling |
| 442 | //===----------------------------------------------------------------------===// |
| 443 | |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 444 | static const TargetRegisterClass *getRegClass(SUnit *SU, |
| 445 | const TargetInstrInfo *TII, |
| 446 | const MRegisterInfo *MRI, |
| 447 | SSARegMap *RegMap) { |
| 448 | if (SU->Node->isTargetOpcode()) { |
| 449 | unsigned Opc = SU->Node->getTargetOpcode(); |
| 450 | const TargetInstrDescriptor &II = TII->get(Opc); |
| 451 | return II.OpInfo->RegClass; |
| 452 | } else { |
| 453 | assert(SU->Node->getOpcode() == ISD::CopyFromReg); |
| 454 | unsigned SrcReg = cast<RegisterSDNode>(SU->Node->getOperand(1))->getReg(); |
| 455 | if (MRegisterInfo::isVirtualRegister(SrcReg)) |
| 456 | return RegMap->getRegClass(SrcReg); |
| 457 | else { |
| 458 | for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(), |
| 459 | E = MRI->regclass_end(); I != E; ++I) |
| 460 | if ((*I)->hasType(SU->Node->getValueType(0)) && |
| 461 | (*I)->contains(SrcReg)) |
| 462 | return *I; |
| 463 | assert(false && "Couldn't find register class for reg copy!"); |
| 464 | } |
| 465 | return NULL; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static unsigned getNumResults(SUnit *SU) { |
| 470 | unsigned NumResults = 0; |
| 471 | for (unsigned i = 0, e = SU->Node->getNumValues(); i != e; ++i) { |
| 472 | MVT::ValueType VT = SU->Node->getValueType(i); |
| 473 | if (VT != MVT::Other && VT != MVT::Flag) |
| 474 | NumResults++; |
| 475 | } |
| 476 | return NumResults; |
| 477 | } |
| 478 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 479 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 480 | /// the Available queue is the count reaches zero. Also update its cycle bound. |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 481 | void ScheduleDAGList::ReleasePred(SUnit *PredSU, bool isChain, |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 482 | unsigned CurCycle) { |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 483 | // FIXME: the distance between two nodes is not always == the predecessor's |
| 484 | // latency. For example, the reader can very well read the register written |
| 485 | // by the predecessor later than the issue cycle. It also depends on the |
| 486 | // interrupt model (drain vs. freeze). |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 487 | PredSU->CycleBound = std::max(PredSU->CycleBound, CurCycle + PredSU->Latency); |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 488 | |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 489 | if (!isChain) |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 490 | PredSU->NumSuccsLeft--; |
Evan Cheng | c5c0658 | 2006-03-06 06:08:54 +0000 | [diff] [blame] | 491 | else |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 492 | PredSU->NumChainSuccsLeft--; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 493 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 494 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 495 | if (PredSU->NumSuccsLeft < 0 || PredSU->NumChainSuccsLeft < 0) { |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 496 | std::cerr << "*** List scheduling failed! ***\n"; |
| 497 | PredSU->dump(&DAG); |
| 498 | std::cerr << " has been released too many times!\n"; |
| 499 | assert(0); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 500 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 501 | #endif |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 502 | |
| 503 | if ((PredSU->NumSuccsLeft + PredSU->NumChainSuccsLeft) == 0) { |
| 504 | // EntryToken has to go last! Special case it here. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 505 | if (PredSU->Node->getOpcode() != ISD::EntryToken) { |
| 506 | PredSU->isAvailable = true; |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 507 | AvailableQueue->push(PredSU); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 508 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 509 | } |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 510 | |
| 511 | if (getNumResults(PredSU) > 0) { |
| 512 | const TargetRegisterClass *RegClass = getRegClass(PredSU, TII, MRI, RegMap); |
| 513 | OpenNodes[RegClass].insert(PredSU); |
| 514 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 515 | } |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 516 | |
| 517 | /// SharesOperandWithTwoAddr - Check if there is a unscheduled two-address node |
| 518 | /// with which SU shares an operand. If so, returns the node. |
| 519 | static SUnit *SharesOperandWithTwoAddr(SUnit *SU) { |
| 520 | assert(!SU->isTwoAddress && "Node cannot be two-address op"); |
| 521 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(), |
| 522 | E = SU->Preds.end(); I != E; ++I) { |
| 523 | if (I->second) continue; |
| 524 | SUnit *PredSU = I->first; |
| 525 | for (std::set<std::pair<SUnit*, bool> >::iterator II = |
| 526 | PredSU->Succs.begin(), EE = PredSU->Succs.end(); II != EE; ++II) { |
| 527 | if (II->second) continue; |
| 528 | SUnit *SSU = II->first; |
| 529 | if (SSU->isTwoAddress && !SSU->isScheduled) { |
| 530 | return SSU; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | static bool isFloater(const SUnit *SU) { |
| 538 | unsigned Opc = SU->Node->getOpcode(); |
| 539 | return (Opc != ISD::CopyFromReg && SU->NumPredsLeft == 0); |
| 540 | } |
| 541 | |
| 542 | static bool isSimpleFloaterUse(const SUnit *SU) { |
| 543 | unsigned NumOps = 0; |
| 544 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(), |
| 545 | E = SU->Preds.end(); I != E; ++I) { |
| 546 | if (I->second) continue; |
| 547 | if (++NumOps > 1) |
| 548 | return false; |
| 549 | if (!isFloater(I->first)) |
| 550 | return false; |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | |
| 555 | /// ScheduleVertically - Schedule vertically. That is, follow up the D&U chain |
| 556 | /// (of two-address code) and schedule floaters aggressively. |
| 557 | void ScheduleDAGList::ScheduleVertically(SUnit *SU, unsigned& CurCycle) { |
| 558 | // Try scheduling Def&Use operand if register pressure is low. |
| 559 | const TargetRegisterClass *RegClass = getRegClass(SU, TII, MRI, RegMap); |
| 560 | unsigned Pressure = OpenNodes[RegClass].size(); |
| 561 | unsigned Limit = RegPressureLimits[RegClass]; |
| 562 | |
| 563 | // See if we can schedule any predecessor that takes no registers. |
| 564 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(), |
| 565 | E = SU->Preds.end(); I != E; ++I) { |
| 566 | if (I->second) continue; |
| 567 | |
| 568 | SUnit *PredSU = I->first; |
| 569 | if (!PredSU->isAvailable || PredSU->isScheduled) |
| 570 | continue; |
| 571 | |
| 572 | if (isFloater(PredSU)) { |
| 573 | DEBUG(std::cerr<<"*** Scheduling floater\n"); |
| 574 | AvailableQueue->RemoveFromPriorityQueue(PredSU); |
| 575 | ScheduleNodeBottomUp(PredSU, CurCycle, false); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | SUnit *DUSU = NULL; |
| 580 | if (SU->isTwoAddress && Pressure < Limit) { |
| 581 | DUSU = SUnitMap[SU->Node->getOperand(0).Val]; |
| 582 | if (!DUSU->isAvailable || DUSU->isScheduled) |
| 583 | DUSU = NULL; |
| 584 | else if (!DUSU->isTwoAddress) { |
| 585 | SUnit *SSU = SharesOperandWithTwoAddr(DUSU); |
| 586 | if (SSU && SSU->isAvailable) { |
| 587 | AvailableQueue->RemoveFromPriorityQueue(SSU); |
| 588 | ScheduleNodeBottomUp(SSU, CurCycle, false); |
| 589 | Pressure = OpenNodes[RegClass].size(); |
| 590 | if (Pressure >= Limit) |
| 591 | DUSU = NULL; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (DUSU) { |
| 597 | DEBUG(std::cerr<<"*** Low register pressure: scheduling D&U operand\n"); |
| 598 | AvailableQueue->RemoveFromPriorityQueue(DUSU); |
| 599 | ScheduleNodeBottomUp(DUSU, CurCycle, false); |
| 600 | Pressure = OpenNodes[RegClass].size(); |
| 601 | ScheduleVertically(DUSU, CurCycle); |
| 602 | } |
| 603 | } |
| 604 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 605 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 606 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 607 | /// the Available queue. |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 608 | void ScheduleDAGList::ScheduleNodeBottomUp(SUnit *SU, unsigned& CurCycle, |
| 609 | bool Vertical) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 610 | DEBUG(std::cerr << "*** Scheduling [" << CurCycle << "]: "); |
Chris Lattner | d413037 | 2006-03-09 07:15:18 +0000 | [diff] [blame] | 611 | DEBUG(SU->dump(&DAG)); |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 612 | SU->Cycle = CurCycle; |
Evan Cheng | 5e9a695 | 2006-03-03 06:23:43 +0000 | [diff] [blame] | 613 | |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 614 | AvailableQueue->ScheduledNode(SU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 615 | Sequence.push_back(SU); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 616 | |
| 617 | // Bottom up: release predecessors |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 618 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(), |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 619 | E = SU->Preds.end(); I != E; ++I) |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 620 | ReleasePred(I->first, I->second, CurCycle); |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 621 | SU->isScheduled = true; |
| 622 | CurCycle++; |
| 623 | |
| 624 | if (getNumResults(SU) != 0) { |
| 625 | const TargetRegisterClass *RegClass = getRegClass(SU, TII, MRI, RegMap); |
| 626 | OpenNodes[RegClass].erase(SU); |
| 627 | |
| 628 | if (SchedVertically && Vertical) |
| 629 | ScheduleVertically(SU, CurCycle); |
Evan Cheng | 4e3904f | 2006-03-02 21:38:29 +0000 | [diff] [blame] | 630 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// isReady - True if node's lower cycle bound is less or equal to the current |
| 634 | /// scheduling cycle. Always true if all nodes have uniform latency 1. |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 635 | static inline bool isReady(SUnit *SU, unsigned CurCycle) { |
| 636 | return SU->CycleBound <= CurCycle; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 639 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 640 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 641 | void ScheduleDAGList::ListScheduleBottomUp() { |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 642 | // Determine rough register pressure limit. |
| 643 | for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(), |
| 644 | E = MRI->regclass_end(); RCI != E; ++RCI) { |
| 645 | const TargetRegisterClass *RC = *RCI; |
| 646 | unsigned Limit = RC->getNumRegs(); |
| 647 | Limit = (Limit > 2) ? Limit - 2 : 0; |
| 648 | std::map<const TargetRegisterClass*, unsigned>::iterator RPI = |
| 649 | RegPressureLimits.find(RC); |
| 650 | if (RPI == RegPressureLimits.end()) |
| 651 | RegPressureLimits[RC] = Limit; |
| 652 | else { |
| 653 | unsigned &OldLimit = RegPressureLimits[RC]; |
| 654 | if (Limit < OldLimit) |
| 655 | OldLimit = Limit; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | unsigned CurCycle = 0; |
Chris Lattner | 7a36d97 | 2006-03-05 20:21:55 +0000 | [diff] [blame] | 660 | // Add root to Available queue. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 661 | AvailableQueue->push(SUnitMap[DAG.getRoot().Val]); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 662 | |
| 663 | // While Available queue is not empty, grab the node with the highest |
| 664 | // priority. If it is not ready put it back. Schedule the node. |
| 665 | std::vector<SUnit*> NotReady; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 666 | SUnit *CurNode = NULL; |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 667 | while (!AvailableQueue->empty()) { |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 668 | SUnit *CurNode = AvailableQueue->pop(); |
| 669 | while (!isReady(CurNode, CurCycle)) { |
| 670 | NotReady.push_back(CurNode); |
| 671 | CurNode = AvailableQueue->pop(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 672 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 673 | |
| 674 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 675 | AvailableQueue->push_all(NotReady); |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 676 | NotReady.clear(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 677 | |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 678 | ScheduleNodeBottomUp(CurNode, CurCycle); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // Add entry node last |
| 682 | if (DAG.getEntryNode().Val != DAG.getRoot().Val) { |
| 683 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 684 | Sequence.push_back(Entry); |
| 685 | } |
| 686 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 687 | // Reverse the order if it is bottom up. |
| 688 | std::reverse(Sequence.begin(), Sequence.end()); |
| 689 | |
| 690 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 691 | #ifndef NDEBUG |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 692 | // Verify that all SUnits were scheduled. |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 693 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 694 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 695 | if (SUnits[i].NumSuccsLeft != 0 || SUnits[i].NumChainSuccsLeft != 0) { |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 696 | if (!AnyNotSched) |
| 697 | std::cerr << "*** List scheduling failed! ***\n"; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 698 | SUnits[i].dump(&DAG); |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 699 | std::cerr << "has not been scheduled!\n"; |
| 700 | AnyNotSched = true; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 701 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 702 | } |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 703 | assert(!AnyNotSched); |
Reid Spencer | 5edde66 | 2006-01-25 21:49:13 +0000 | [diff] [blame] | 704 | #endif |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 707 | //===----------------------------------------------------------------------===// |
| 708 | // Top-Down Scheduling |
| 709 | //===----------------------------------------------------------------------===// |
| 710 | |
| 711 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 712 | /// the PendingQueue if the count reaches zero. |
| 713 | void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) { |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 714 | if (!isChain) |
| 715 | SuccSU->NumPredsLeft--; |
| 716 | else |
| 717 | SuccSU->NumChainPredsLeft--; |
| 718 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 719 | assert(SuccSU->NumPredsLeft >= 0 && SuccSU->NumChainPredsLeft >= 0 && |
| 720 | "List scheduling internal error"); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 721 | |
| 722 | if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 723 | // Compute how many cycles it will be before this actually becomes |
| 724 | // available. This is the max of the start time of all predecessors plus |
| 725 | // their latencies. |
| 726 | unsigned AvailableCycle = 0; |
| 727 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SuccSU->Preds.begin(), |
| 728 | E = SuccSU->Preds.end(); I != E; ++I) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 729 | // If this is a token edge, we don't need to wait for the latency of the |
| 730 | // preceeding instruction (e.g. a long-latency load) unless there is also |
| 731 | // some other data dependence. |
Chris Lattner | 86a9b60 | 2006-03-12 03:52:09 +0000 | [diff] [blame] | 732 | unsigned PredDoneCycle = I->first->Cycle; |
| 733 | if (!I->second) |
| 734 | PredDoneCycle += I->first->Latency; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 735 | else if (I->first->Latency) |
| 736 | PredDoneCycle += 1; |
Chris Lattner | 86a9b60 | 2006-03-12 03:52:09 +0000 | [diff] [blame] | 737 | |
| 738 | AvailableCycle = std::max(AvailableCycle, PredDoneCycle); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU)); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
| 745 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 746 | /// count of its successors. If a successor pending count is zero, add it to |
| 747 | /// the Available queue. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 748 | void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 749 | DEBUG(std::cerr << "*** Scheduling [" << CurCycle << "]: "); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 750 | DEBUG(SU->dump(&DAG)); |
| 751 | |
| 752 | Sequence.push_back(SU); |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 753 | SU->Cycle = CurCycle; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 754 | |
| 755 | // Bottom up: release successors. |
| 756 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(), |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 757 | E = SU->Succs.end(); I != E; ++I) |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 758 | ReleaseSucc(I->first, I->second); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 761 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 762 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 763 | void ScheduleDAGList::ListScheduleTopDown() { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 764 | unsigned CurCycle = 0; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 765 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 767 | // All leaves to Available queue. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 768 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 769 | // It is available if it has no predecessors. |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 770 | if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) { |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 771 | AvailableQueue->push(&SUnits[i]); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 772 | SUnits[i].isAvailable = SUnits[i].isPending = true; |
| 773 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 776 | // Emit the entry node first. |
| 777 | ScheduleNodeTopDown(Entry, CurCycle); |
| 778 | HazardRec->EmitInstruction(Entry->Node); |
| 779 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 780 | // While Available queue is not empty, grab the node with the highest |
| 781 | // priority. If it is not ready put it back. Schedule the node. |
| 782 | std::vector<SUnit*> NotReady; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 783 | while (!AvailableQueue->empty() || !PendingQueue.empty()) { |
| 784 | // Check to see if any of the pending instructions are ready to issue. If |
| 785 | // so, add them to the available queue. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 786 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 787 | if (PendingQueue[i].first == CurCycle) { |
| 788 | AvailableQueue->push(PendingQueue[i].second); |
| 789 | PendingQueue[i].second->isAvailable = true; |
| 790 | PendingQueue[i] = PendingQueue.back(); |
| 791 | PendingQueue.pop_back(); |
| 792 | --i; --e; |
| 793 | } else { |
| 794 | assert(PendingQueue[i].first > CurCycle && "Negative latency?"); |
| 795 | } |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 796 | } |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 797 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 798 | // If there are no instructions available, don't try to issue anything, and |
| 799 | // don't advance the hazard recognizer. |
| 800 | if (AvailableQueue->empty()) { |
| 801 | ++CurCycle; |
| 802 | continue; |
| 803 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 804 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 805 | SUnit *FoundSUnit = 0; |
| 806 | SDNode *FoundNode = 0; |
| 807 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 808 | bool HasNoopHazards = false; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 809 | while (!AvailableQueue->empty()) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 810 | SUnit *CurSUnit = AvailableQueue->pop(); |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 811 | |
| 812 | // Get the node represented by this SUnit. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 813 | FoundNode = CurSUnit->Node; |
| 814 | |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 815 | // If this is a pseudo op, like copyfromreg, look to see if there is a |
| 816 | // real target node flagged to it. If so, use the target node. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 817 | for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size(); |
| 818 | FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i) |
| 819 | FoundNode = CurSUnit->FlaggedNodes[i]; |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 820 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 821 | HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 822 | if (HT == HazardRecognizer::NoHazard) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 823 | FoundSUnit = CurSUnit; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 824 | break; |
| 825 | } |
| 826 | |
| 827 | // Remember if this is a noop hazard. |
| 828 | HasNoopHazards |= HT == HazardRecognizer::NoopHazard; |
| 829 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 830 | NotReady.push_back(CurSUnit); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 831 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 832 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 833 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 834 | if (!NotReady.empty()) { |
| 835 | AvailableQueue->push_all(NotReady); |
| 836 | NotReady.clear(); |
| 837 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 838 | |
| 839 | // If we found a node to schedule, do it now. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 840 | if (FoundSUnit) { |
| 841 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
| 842 | HazardRec->EmitInstruction(FoundNode); |
| 843 | FoundSUnit->isScheduled = true; |
| 844 | AvailableQueue->ScheduledNode(FoundSUnit); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 845 | |
| 846 | // If this is a pseudo-op node, we don't want to increment the current |
| 847 | // cycle. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 848 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 849 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 850 | } else if (!HasNoopHazards) { |
| 851 | // Otherwise, we have a pipeline stall, but no other problem, just advance |
| 852 | // the current cycle and try again. |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 853 | DEBUG(std::cerr << "*** Advancing cycle, no work to do\n"); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 854 | HazardRec->AdvanceCycle(); |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 855 | ++NumStalls; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 856 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 857 | } else { |
| 858 | // Otherwise, we have no instructions to issue and we have instructions |
| 859 | // that will fault if we don't do this right. This is the case for |
| 860 | // processors without pipeline interlocks and other cases. |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 861 | DEBUG(std::cerr << "*** Emitting noop\n"); |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 862 | HazardRec->EmitNoop(); |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 863 | Sequence.push_back(0); // NULL SUnit* -> noop |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 864 | ++NumNoops; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 865 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 866 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | #ifndef NDEBUG |
| 870 | // Verify that all SUnits were scheduled. |
| 871 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 872 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 873 | if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 874 | if (!AnyNotSched) |
| 875 | std::cerr << "*** List scheduling failed! ***\n"; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 876 | SUnits[i].dump(&DAG); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 877 | std::cerr << "has not been scheduled!\n"; |
| 878 | AnyNotSched = true; |
| 879 | } |
| 880 | } |
| 881 | assert(!AnyNotSched); |
| 882 | #endif |
| 883 | } |
| 884 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 885 | //===----------------------------------------------------------------------===// |
| 886 | // RegReductionPriorityQueue Implementation |
| 887 | //===----------------------------------------------------------------------===// |
| 888 | // |
| 889 | // This is a SchedulingPriorityQueue that schedules using Sethi Ullman numbers |
| 890 | // to reduce register pressure. |
| 891 | // |
| 892 | namespace { |
| 893 | class RegReductionPriorityQueue; |
| 894 | |
| 895 | /// Sorting functions for the Available queue. |
| 896 | struct ls_rr_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 897 | RegReductionPriorityQueue *SPQ; |
| 898 | ls_rr_sort(RegReductionPriorityQueue *spq) : SPQ(spq) {} |
| 899 | ls_rr_sort(const ls_rr_sort &RHS) : SPQ(RHS.SPQ) {} |
| 900 | |
| 901 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 902 | }; |
| 903 | } // end anonymous namespace |
| 904 | |
| 905 | namespace { |
| 906 | class RegReductionPriorityQueue : public SchedulingPriorityQueue { |
| 907 | // SUnits - The SUnits for the current graph. |
| 908 | const std::vector<SUnit> *SUnits; |
| 909 | |
| 910 | // SethiUllmanNumbers - The SethiUllman number for each node. |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 911 | std::vector<int> SethiUllmanNumbers; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 912 | |
| 913 | std::priority_queue<SUnit*, std::vector<SUnit*>, ls_rr_sort> Queue; |
| 914 | public: |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 915 | RegReductionPriorityQueue() : |
| 916 | Queue(ls_rr_sort(this)) {} |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 917 | |
| 918 | void initNodes(const std::vector<SUnit> &sunits) { |
| 919 | SUnits = &sunits; |
| 920 | // Calculate node priorities. |
| 921 | CalculatePriorities(); |
| 922 | } |
| 923 | void releaseState() { |
| 924 | SUnits = 0; |
| 925 | SethiUllmanNumbers.clear(); |
| 926 | } |
| 927 | |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 928 | int getSethiUllmanNumber(unsigned NodeNum) const { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 929 | assert(NodeNum < SethiUllmanNumbers.size()); |
| 930 | return SethiUllmanNumbers[NodeNum]; |
| 931 | } |
| 932 | |
| 933 | bool empty() const { return Queue.empty(); } |
| 934 | |
| 935 | void push(SUnit *U) { |
| 936 | Queue.push(U); |
| 937 | } |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 938 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 939 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 940 | Queue.push(Nodes[i]); |
| 941 | } |
| 942 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 943 | SUnit *pop() { |
| 944 | SUnit *V = Queue.top(); |
| 945 | Queue.pop(); |
| 946 | return V; |
| 947 | } |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 948 | |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 949 | /// RemoveFromPriorityQueue - This is a really inefficient way to remove a |
| 950 | /// node from a priority queue. We should roll our own heap to make this |
| 951 | /// better or something. |
| 952 | void RemoveFromPriorityQueue(SUnit *SU) { |
| 953 | std::vector<SUnit*> Temp; |
| 954 | |
| 955 | assert(!Queue.empty() && "Not in queue!"); |
| 956 | while (Queue.top() != SU) { |
| 957 | Temp.push_back(Queue.top()); |
| 958 | Queue.pop(); |
| 959 | assert(!Queue.empty() && "Not in queue!"); |
| 960 | } |
| 961 | |
| 962 | // Remove the node from the PQ. |
| 963 | Queue.pop(); |
| 964 | |
| 965 | // Add all the other nodes back. |
| 966 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 967 | Queue.push(Temp[i]); |
| 968 | } |
| 969 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 970 | private: |
| 971 | void CalculatePriorities(); |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 972 | int CalcNodePriority(const SUnit *SU); |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 973 | }; |
| 974 | } |
| 975 | |
| 976 | bool ls_rr_sort::operator()(const SUnit *left, const SUnit *right) const { |
| 977 | unsigned LeftNum = left->NodeNum; |
| 978 | unsigned RightNum = right->NodeNum; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 979 | bool LIsTarget = left->Node->isTargetOpcode(); |
| 980 | bool RIsTarget = right->Node->isTargetOpcode(); |
| 981 | int LPriority = SPQ->getSethiUllmanNumber(LeftNum); |
| 982 | int RPriority = SPQ->getSethiUllmanNumber(RightNum); |
| 983 | bool LIsFloater = LIsTarget && (LPriority == 1 || LPriority == 0); |
| 984 | bool RIsFloater = RIsTarget && (RPriority == 1 || RPriority == 0); |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 985 | int LBonus = 0; |
| 986 | int RBonus = 0; |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 987 | |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 988 | // Schedule floaters (e.g. load from some constant address) and those nodes |
| 989 | // with a single predecessor each first. They maintain / reduce register |
| 990 | // pressure. |
| 991 | if (LIsFloater) |
| 992 | LBonus += 2; |
| 993 | if (RIsFloater) |
| 994 | RBonus += 2; |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 995 | |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 996 | // Special tie breaker: if two nodes share a operand, the one that use it |
| 997 | // as a def&use operand is preferred. |
| 998 | if (LIsTarget && RIsTarget) { |
| 999 | if (left->isTwoAddress && !right->isTwoAddress) { |
| 1000 | SDNode *DUNode = left->Node->getOperand(0).Val; |
| 1001 | if (DUNode->isOperand(right->Node)) |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1002 | LBonus += 2; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1003 | } |
| 1004 | if (!left->isTwoAddress && right->isTwoAddress) { |
| 1005 | SDNode *DUNode = right->Node->getOperand(0).Val; |
| 1006 | if (DUNode->isOperand(left->Node)) |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1007 | RBonus += 2; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1011 | if (LPriority+LBonus < RPriority+RBonus) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1012 | return true; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1013 | else if (LPriority+LBonus == RPriority+RBonus) |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1014 | if (left->NumPredsLeft > right->NumPredsLeft) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1015 | return true; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1016 | else if (left->NumPredsLeft+LBonus == right->NumPredsLeft+RBonus) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1017 | if (left->CycleBound > right->CycleBound) |
| 1018 | return true; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /// CalcNodePriority - Priority is the Sethi Ullman number. |
| 1024 | /// Smaller number is the higher priority. |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1025 | int RegReductionPriorityQueue::CalcNodePriority(const SUnit *SU) { |
| 1026 | int &SethiUllmanNumber = SethiUllmanNumbers[SU->NodeNum]; |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 1027 | if (SethiUllmanNumber != 0) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1028 | return SethiUllmanNumber; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1029 | |
| 1030 | unsigned Opc = SU->Node->getOpcode(); |
| 1031 | if (Opc == ISD::TokenFactor || Opc == ISD::CopyToReg) |
| 1032 | SethiUllmanNumber = INT_MAX - 10; |
| 1033 | else if (SU->NumSuccsLeft == 0) |
| 1034 | // If SU does not have a use, i.e. it doesn't produce a value that would |
| 1035 | // be consumed (e.g. store), then it terminates a chain of computation. |
| 1036 | // Give it a small SethiUllman number so it will be scheduled right before its |
| 1037 | // predecessors that it doesn't lengthen their live ranges. |
| 1038 | SethiUllmanNumber = INT_MIN + 10; |
| 1039 | else if (SU->NumPredsLeft == 0 && Opc != ISD::CopyFromReg) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1040 | SethiUllmanNumber = 1; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1041 | else { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1042 | int Extra = 0; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1043 | for (std::set<std::pair<SUnit*, bool> >::const_iterator |
| 1044 | I = SU->Preds.begin(), E = SU->Preds.end(); I != E; ++I) { |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1045 | if (I->second) continue; // ignore chain preds |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1046 | SUnit *PredSU = I->first; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1047 | int PredSethiUllman = CalcNodePriority(PredSU); |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1048 | if (PredSethiUllman > SethiUllmanNumber) { |
| 1049 | SethiUllmanNumber = PredSethiUllman; |
| 1050 | Extra = 0; |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1051 | } else if (PredSethiUllman == SethiUllmanNumber && !I->second) |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1052 | Extra++; |
| 1053 | } |
Evan Cheng | ffef8b9 | 2006-05-03 02:10:45 +0000 | [diff] [blame] | 1054 | |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 1055 | SethiUllmanNumber += Extra; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | return SethiUllmanNumber; |
| 1059 | } |
| 1060 | |
| 1061 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 1062 | void RegReductionPriorityQueue::CalculatePriorities() { |
Evan Cheng | 24e7954 | 2006-05-01 09:14:40 +0000 | [diff] [blame] | 1063 | SethiUllmanNumbers.assign(SUnits->size(), 0); |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1064 | |
| 1065 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
| 1066 | CalcNodePriority(&(*SUnits)[i]); |
| 1067 | } |
| 1068 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1069 | //===----------------------------------------------------------------------===// |
| 1070 | // LatencyPriorityQueue Implementation |
| 1071 | //===----------------------------------------------------------------------===// |
| 1072 | // |
| 1073 | // This is a SchedulingPriorityQueue that schedules using latency information to |
| 1074 | // reduce the length of the critical path through the basic block. |
| 1075 | // |
| 1076 | namespace { |
| 1077 | class LatencyPriorityQueue; |
| 1078 | |
| 1079 | /// Sorting functions for the Available queue. |
| 1080 | struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 1081 | LatencyPriorityQueue *PQ; |
| 1082 | latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {} |
| 1083 | latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {} |
| 1084 | |
| 1085 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 1086 | }; |
| 1087 | } // end anonymous namespace |
| 1088 | |
| 1089 | namespace { |
| 1090 | class LatencyPriorityQueue : public SchedulingPriorityQueue { |
| 1091 | // SUnits - The SUnits for the current graph. |
| 1092 | const std::vector<SUnit> *SUnits; |
| 1093 | |
| 1094 | // Latencies - The latency (max of latency from this node to the bb exit) |
| 1095 | // for each node. |
| 1096 | std::vector<int> Latencies; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1097 | |
| 1098 | /// NumNodesSolelyBlocking - This vector contains, for every node in the |
| 1099 | /// Queue, the number of nodes that the node is the sole unscheduled |
| 1100 | /// predecessor for. This is used as a tie-breaker heuristic for better |
| 1101 | /// mobility. |
| 1102 | std::vector<unsigned> NumNodesSolelyBlocking; |
| 1103 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1104 | std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue; |
| 1105 | public: |
| 1106 | LatencyPriorityQueue() : Queue(latency_sort(this)) { |
| 1107 | } |
| 1108 | |
| 1109 | void initNodes(const std::vector<SUnit> &sunits) { |
| 1110 | SUnits = &sunits; |
| 1111 | // Calculate node priorities. |
| 1112 | CalculatePriorities(); |
| 1113 | } |
| 1114 | void releaseState() { |
| 1115 | SUnits = 0; |
| 1116 | Latencies.clear(); |
| 1117 | } |
| 1118 | |
| 1119 | unsigned getLatency(unsigned NodeNum) const { |
| 1120 | assert(NodeNum < Latencies.size()); |
| 1121 | return Latencies[NodeNum]; |
| 1122 | } |
| 1123 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1124 | unsigned getNumSolelyBlockNodes(unsigned NodeNum) const { |
| 1125 | assert(NodeNum < NumNodesSolelyBlocking.size()); |
| 1126 | return NumNodesSolelyBlocking[NodeNum]; |
| 1127 | } |
| 1128 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1129 | bool empty() const { return Queue.empty(); } |
| 1130 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1131 | virtual void push(SUnit *U) { |
| 1132 | push_impl(U); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1133 | } |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1134 | void push_impl(SUnit *U); |
| 1135 | |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 1136 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 1137 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1138 | push_impl(Nodes[i]); |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1141 | SUnit *pop() { |
| 1142 | SUnit *V = Queue.top(); |
| 1143 | Queue.pop(); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1144 | return V; |
| 1145 | } |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1146 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1147 | /// RemoveFromPriorityQueue - This is a really inefficient way to remove a |
| 1148 | /// node from a priority queue. We should roll our own heap to make this |
| 1149 | /// better or something. |
| 1150 | void RemoveFromPriorityQueue(SUnit *SU) { |
| 1151 | std::vector<SUnit*> Temp; |
| 1152 | |
| 1153 | assert(!Queue.empty() && "Not in queue!"); |
| 1154 | while (Queue.top() != SU) { |
| 1155 | Temp.push_back(Queue.top()); |
| 1156 | Queue.pop(); |
| 1157 | assert(!Queue.empty() && "Not in queue!"); |
| 1158 | } |
| 1159 | |
| 1160 | // Remove the node from the PQ. |
| 1161 | Queue.pop(); |
| 1162 | |
| 1163 | // Add all the other nodes back. |
| 1164 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 1165 | Queue.push(Temp[i]); |
| 1166 | } |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame^] | 1167 | |
| 1168 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 1169 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 1170 | // single predecessor has a higher priority, since scheduling it will make |
| 1171 | // the node available. |
| 1172 | void ScheduledNode(SUnit *Node); |
| 1173 | |
| 1174 | private: |
| 1175 | void CalculatePriorities(); |
| 1176 | int CalcLatency(const SUnit &SU); |
| 1177 | void AdjustPriorityOfUnscheduledPreds(SUnit *SU); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1178 | }; |
| 1179 | } |
| 1180 | |
| 1181 | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
| 1182 | unsigned LHSNum = LHS->NodeNum; |
| 1183 | unsigned RHSNum = RHS->NodeNum; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1184 | |
| 1185 | // The most important heuristic is scheduling the critical path. |
| 1186 | unsigned LHSLatency = PQ->getLatency(LHSNum); |
| 1187 | unsigned RHSLatency = PQ->getLatency(RHSNum); |
| 1188 | if (LHSLatency < RHSLatency) return true; |
| 1189 | if (LHSLatency > RHSLatency) return false; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1191 | // After that, if two nodes have identical latencies, look to see if one will |
| 1192 | // unblock more other nodes than the other. |
| 1193 | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
| 1194 | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
| 1195 | if (LHSBlocked < RHSBlocked) return true; |
| 1196 | if (LHSBlocked > RHSBlocked) return false; |
| 1197 | |
| 1198 | // Finally, just to provide a stable ordering, use the node number as a |
| 1199 | // deciding factor. |
| 1200 | return LHSNum < RHSNum; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | /// CalcNodePriority - Calculate the maximal path from the node to the exit. |
| 1205 | /// |
| 1206 | int LatencyPriorityQueue::CalcLatency(const SUnit &SU) { |
| 1207 | int &Latency = Latencies[SU.NodeNum]; |
| 1208 | if (Latency != -1) |
| 1209 | return Latency; |
| 1210 | |
| 1211 | int MaxSuccLatency = 0; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1212 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU.Succs.begin(), |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1213 | E = SU.Succs.end(); I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1214 | MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(*I->first)); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1215 | |
| 1216 | return Latency = MaxSuccLatency + SU.Latency; |
| 1217 | } |
| 1218 | |
| 1219 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 1220 | void LatencyPriorityQueue::CalculatePriorities() { |
| 1221 | Latencies.assign(SUnits->size(), -1); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1222 | NumNodesSolelyBlocking.assign(SUnits->size(), 0); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1223 | |
| 1224 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
| 1225 | CalcLatency((*SUnits)[i]); |
| 1226 | } |
| 1227 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1228 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
| 1229 | /// of SU, return it, otherwise return null. |
| 1230 | static SUnit *getSingleUnscheduledPred(SUnit *SU) { |
| 1231 | SUnit *OnlyAvailablePred = 0; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1232 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Preds.begin(), |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1233 | E = SU->Preds.end(); I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1234 | if (!I->first->isScheduled) { |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1235 | // We found an available, but not scheduled, predecessor. If it's the |
| 1236 | // only one we have found, keep track of it... otherwise give up. |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1237 | if (OnlyAvailablePred && OnlyAvailablePred != I->first) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1238 | return 0; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1239 | OnlyAvailablePred = I->first; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | return OnlyAvailablePred; |
| 1243 | } |
| 1244 | |
| 1245 | void LatencyPriorityQueue::push_impl(SUnit *SU) { |
| 1246 | // Look at all of the successors of this node. Count the number of nodes that |
| 1247 | // this node is the sole unscheduled node for. |
| 1248 | unsigned NumNodesBlocking = 0; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1249 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Succs.begin(), |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1250 | E = SU->Succs.end(); I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1251 | if (getSingleUnscheduledPred(I->first) == SU) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1252 | ++NumNodesBlocking; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1253 | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1254 | |
| 1255 | Queue.push(SU); |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 1260 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 1261 | // single predecessor has a higher priority, since scheduling it will make |
| 1262 | // the node available. |
| 1263 | void LatencyPriorityQueue::ScheduledNode(SUnit *SU) { |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1264 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = SU->Succs.begin(), |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1265 | E = SU->Succs.end(); I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 1266 | AdjustPriorityOfUnscheduledPreds(I->first); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
| 1270 | /// scheduled. If SU is not itself available, then there is at least one |
| 1271 | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
| 1272 | /// unscheduled predecessor, we want to increase its priority: it getting |
| 1273 | /// scheduled will make this node available, so it is better than some other |
| 1274 | /// node of the same priority that will not make a node available. |
| 1275 | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 1276 | if (SU->isPending) return; // All preds scheduled. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 1277 | |
| 1278 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
| 1279 | if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return; |
| 1280 | |
| 1281 | // Okay, we found a single predecessor that is available, but not scheduled. |
| 1282 | // Since it is available, it must be in the priority queue. First remove it. |
| 1283 | RemoveFromPriorityQueue(OnlyAvailablePred); |
| 1284 | |
| 1285 | // Reinsert the node into the priority queue, which recomputes its |
| 1286 | // NumNodesSolelyBlocking value. |
| 1287 | push(OnlyAvailablePred); |
| 1288 | } |
| 1289 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1290 | |
| 1291 | //===----------------------------------------------------------------------===// |
| 1292 | // Public Constructor Functions |
| 1293 | //===----------------------------------------------------------------------===// |
| 1294 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 1295 | llvm::ScheduleDAG* llvm::createBURRListDAGScheduler(SelectionDAG &DAG, |
| 1296 | MachineBasicBlock *BB) { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1297 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), true, |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1298 | new RegReductionPriorityQueue(), |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1299 | new HazardRecognizer()); |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 1302 | /// createTDListDAGScheduler - This creates a top-down list scheduler with the |
| 1303 | /// specified hazard recognizer. |
| 1304 | ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAG &DAG, |
| 1305 | MachineBasicBlock *BB, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 1306 | HazardRecognizer *HR) { |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1307 | return new ScheduleDAGList(DAG, BB, DAG.getTarget(), false, |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 1308 | new LatencyPriorityQueue(), |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 1309 | HR); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 1310 | } |