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