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 | // |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 10 | // This implements a top-down list scheduler, using standard algorithms. |
| 11 | // The basic approach uses a priority queue of available nodes to schedule. |
| 12 | // One at a time, nodes are taken from the priority queue (thus in priority |
| 13 | // order), checked for legality to schedule, and emitted if legal. |
Chris Lattner | 01aa752 | 2006-03-06 17:58:04 +0000 | [diff] [blame] | 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" |
Jim Laskey | 29e635d | 2006-08-02 12:30:23 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/MRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compiler.h" |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 33 | #include <climits> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 34 | #include <queue> |
| 35 | using namespace llvm; |
| 36 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 37 | namespace { |
Chris Lattner | 700b873 | 2006-12-06 17:46:33 +0000 | [diff] [blame] | 38 | static Statistic NumNoops ("scheduler", "Number of noops inserted"); |
| 39 | static Statistic NumStalls("scheduler", "Number of pipeline stalls"); |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 40 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 41 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 42 | static RegisterScheduler |
| 43 | tdListDAGScheduler("list-td", " Top-down list scheduler", |
| 44 | createTDListDAGScheduler); |
| 45 | |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | /// ScheduleDAGList - The actual list scheduler implementation. This supports |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 49 | /// top-down scheduling. |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 50 | /// |
Chris Lattner | e097e6f | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 51 | class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG { |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 52 | private: |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 53 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 54 | /// |
| 55 | SchedulingPriorityQueue *AvailableQueue; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 57 | /// PendingQueue - This contains all of the instructions whose operands have |
| 58 | /// been issued, but their results are not ready yet (due to the latency of |
| 59 | /// the operation). Once the operands becomes available, the instruction is |
| 60 | /// added to the AvailableQueue. This keeps track of each SUnit and the |
| 61 | /// number of cycles left to execute before the operation is available. |
| 62 | std::vector<std::pair<unsigned, SUnit*> > PendingQueue; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame] | 63 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 64 | /// HazardRec - The hazard recognizer to use. |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 65 | HazardRecognizer *HazardRec; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame] | 66 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 67 | public: |
| 68 | ScheduleDAGList(SelectionDAG &dag, MachineBasicBlock *bb, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 69 | const TargetMachine &tm, |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 70 | SchedulingPriorityQueue *availqueue, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 71 | HazardRecognizer *HR) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 72 | : ScheduleDAG(dag, bb, tm), |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 73 | AvailableQueue(availqueue), HazardRec(HR) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 74 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 75 | |
| 76 | ~ScheduleDAGList() { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 77 | delete HazardRec; |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 78 | delete AvailableQueue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 79 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 80 | |
| 81 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 82 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 83 | private: |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 84 | void ReleaseSucc(SUnit *SuccSU, bool isChain); |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 85 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 86 | void ListScheduleTopDown(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 87 | }; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 88 | } // end anonymous namespace |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 90 | HazardRecognizer::~HazardRecognizer() {} |
| 91 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 93 | /// Schedule - Schedule the DAG using list scheduling. |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 94 | void ScheduleDAGList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 95 | DOUT << "********** List Scheduling **********\n"; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 96 | |
| 97 | // Build scheduling units. |
| 98 | BuildSchedUnits(); |
Evan Cheng | 7d69389 | 2006-05-09 07:13:34 +0000 | [diff] [blame] | 99 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 100 | AvailableQueue->initNodes(SUnitMap, SUnits); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 101 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 102 | ListScheduleTopDown(); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 104 | AvailableQueue->releaseState(); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 105 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 106 | DOUT << "*** Final schedule ***\n"; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 107 | DEBUG(dumpSchedule()); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 108 | DOUT << "\n"; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 109 | |
| 110 | // Emit in scheduled order |
| 111 | EmitSchedule(); |
| 112 | } |
| 113 | |
| 114 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 115 | // Top-Down Scheduling |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 119 | /// the PendingQueue if the count reaches zero. |
| 120 | void ScheduleDAGList::ReleaseSucc(SUnit *SuccSU, bool isChain) { |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 121 | if (!isChain) |
| 122 | SuccSU->NumPredsLeft--; |
| 123 | else |
| 124 | SuccSU->NumChainPredsLeft--; |
| 125 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 126 | assert(SuccSU->NumPredsLeft >= 0 && SuccSU->NumChainPredsLeft >= 0 && |
| 127 | "List scheduling internal error"); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 128 | |
| 129 | if ((SuccSU->NumPredsLeft + SuccSU->NumChainPredsLeft) == 0) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 130 | // Compute how many cycles it will be before this actually becomes |
| 131 | // available. This is the max of the start time of all predecessors plus |
| 132 | // their latencies. |
| 133 | unsigned AvailableCycle = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 134 | for (SUnit::pred_iterator I = SuccSU->Preds.begin(), |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 135 | E = SuccSU->Preds.end(); I != E; ++I) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 136 | // If this is a token edge, we don't need to wait for the latency of the |
| 137 | // preceeding instruction (e.g. a long-latency load) unless there is also |
| 138 | // some other data dependence. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 139 | SUnit &Pred = *I->first; |
| 140 | unsigned PredDoneCycle = Pred.Cycle; |
Chris Lattner | 86a9b60 | 2006-03-12 03:52:09 +0000 | [diff] [blame] | 141 | if (!I->second) |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 142 | PredDoneCycle += Pred.Latency; |
| 143 | else if (Pred.Latency) |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 144 | PredDoneCycle += 1; |
Chris Lattner | 86a9b60 | 2006-03-12 03:52:09 +0000 | [diff] [blame] | 145 | |
| 146 | AvailableCycle = std::max(AvailableCycle, PredDoneCycle); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | PendingQueue.push_back(std::make_pair(AvailableCycle, SuccSU)); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 154 | /// count of its successors. If a successor pending count is zero, add it to |
| 155 | /// the Available queue. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 156 | void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 157 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 158 | DEBUG(SU->dump(&DAG)); |
| 159 | |
| 160 | Sequence.push_back(SU); |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 161 | SU->Cycle = CurCycle; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 162 | |
| 163 | // Bottom up: release successors. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 164 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 165 | I != E; ++I) |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 166 | ReleaseSucc(I->first, I->second); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 169 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 170 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 171 | void ScheduleDAGList::ListScheduleTopDown() { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 172 | unsigned CurCycle = 0; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 173 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 175 | // All leaves to Available queue. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 176 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 177 | // It is available if it has no predecessors. |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 178 | if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) { |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 179 | AvailableQueue->push(&SUnits[i]); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 180 | SUnits[i].isAvailable = SUnits[i].isPending = true; |
| 181 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 184 | // Emit the entry node first. |
| 185 | ScheduleNodeTopDown(Entry, CurCycle); |
| 186 | HazardRec->EmitInstruction(Entry->Node); |
| 187 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 188 | // While Available queue is not empty, grab the node with the highest |
| 189 | // priority. If it is not ready put it back. Schedule the node. |
| 190 | std::vector<SUnit*> NotReady; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 191 | while (!AvailableQueue->empty() || !PendingQueue.empty()) { |
| 192 | // Check to see if any of the pending instructions are ready to issue. If |
| 193 | // so, add them to the available queue. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 194 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 195 | if (PendingQueue[i].first == CurCycle) { |
| 196 | AvailableQueue->push(PendingQueue[i].second); |
| 197 | PendingQueue[i].second->isAvailable = true; |
| 198 | PendingQueue[i] = PendingQueue.back(); |
| 199 | PendingQueue.pop_back(); |
| 200 | --i; --e; |
| 201 | } else { |
| 202 | assert(PendingQueue[i].first > CurCycle && "Negative latency?"); |
| 203 | } |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 205 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 206 | // If there are no instructions available, don't try to issue anything, and |
| 207 | // don't advance the hazard recognizer. |
| 208 | if (AvailableQueue->empty()) { |
| 209 | ++CurCycle; |
| 210 | continue; |
| 211 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 212 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 213 | SUnit *FoundSUnit = 0; |
| 214 | SDNode *FoundNode = 0; |
| 215 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 216 | bool HasNoopHazards = false; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 217 | while (!AvailableQueue->empty()) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 218 | SUnit *CurSUnit = AvailableQueue->pop(); |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 219 | |
| 220 | // Get the node represented by this SUnit. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 221 | FoundNode = CurSUnit->Node; |
| 222 | |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 223 | // If this is a pseudo op, like copyfromreg, look to see if there is a |
| 224 | // real target node flagged to it. If so, use the target node. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 225 | for (unsigned i = 0, e = CurSUnit->FlaggedNodes.size(); |
| 226 | FoundNode->getOpcode() < ISD::BUILTIN_OP_END && i != e; ++i) |
| 227 | FoundNode = CurSUnit->FlaggedNodes[i]; |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 228 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 229 | HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 230 | if (HT == HazardRecognizer::NoHazard) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 231 | FoundSUnit = CurSUnit; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | |
| 235 | // Remember if this is a noop hazard. |
| 236 | HasNoopHazards |= HT == HazardRecognizer::NoopHazard; |
| 237 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 238 | NotReady.push_back(CurSUnit); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 241 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 242 | if (!NotReady.empty()) { |
| 243 | AvailableQueue->push_all(NotReady); |
| 244 | NotReady.clear(); |
| 245 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 246 | |
| 247 | // If we found a node to schedule, do it now. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 248 | if (FoundSUnit) { |
| 249 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
| 250 | HazardRec->EmitInstruction(FoundNode); |
| 251 | FoundSUnit->isScheduled = true; |
| 252 | AvailableQueue->ScheduledNode(FoundSUnit); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 253 | |
| 254 | // If this is a pseudo-op node, we don't want to increment the current |
| 255 | // cycle. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 256 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 257 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 258 | } else if (!HasNoopHazards) { |
| 259 | // Otherwise, we have a pipeline stall, but no other problem, just advance |
| 260 | // the current cycle and try again. |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 261 | DOUT << "*** Advancing cycle, no work to do\n"; |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 262 | HazardRec->AdvanceCycle(); |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 263 | ++NumStalls; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 264 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 265 | } else { |
| 266 | // Otherwise, we have no instructions to issue and we have instructions |
| 267 | // that will fault if we don't do this right. This is the case for |
| 268 | // processors without pipeline interlocks and other cases. |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 269 | DOUT << "*** Emitting noop\n"; |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 270 | HazardRec->EmitNoop(); |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 271 | Sequence.push_back(0); // NULL SUnit* -> noop |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 272 | ++NumNoops; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 273 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | #ifndef NDEBUG |
| 278 | // Verify that all SUnits were scheduled. |
| 279 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 280 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 281 | if (SUnits[i].NumPredsLeft != 0 || SUnits[i].NumChainPredsLeft != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 282 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 283 | cerr << "*** List scheduling failed! ***\n"; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 284 | SUnits[i].dump(&DAG); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame^] | 285 | cerr << "has not been scheduled!\n"; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 286 | AnyNotSched = true; |
| 287 | } |
| 288 | } |
| 289 | assert(!AnyNotSched); |
| 290 | #endif |
| 291 | } |
| 292 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 293 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 294 | // LatencyPriorityQueue Implementation |
| 295 | //===----------------------------------------------------------------------===// |
| 296 | // |
| 297 | // This is a SchedulingPriorityQueue that schedules using latency information to |
| 298 | // reduce the length of the critical path through the basic block. |
| 299 | // |
| 300 | namespace { |
| 301 | class LatencyPriorityQueue; |
| 302 | |
| 303 | /// Sorting functions for the Available queue. |
| 304 | struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> { |
| 305 | LatencyPriorityQueue *PQ; |
| 306 | latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {} |
| 307 | latency_sort(const latency_sort &RHS) : PQ(RHS.PQ) {} |
| 308 | |
| 309 | bool operator()(const SUnit* left, const SUnit* right) const; |
| 310 | }; |
| 311 | } // end anonymous namespace |
| 312 | |
| 313 | namespace { |
| 314 | class LatencyPriorityQueue : public SchedulingPriorityQueue { |
| 315 | // SUnits - The SUnits for the current graph. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 316 | std::vector<SUnit> *SUnits; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 317 | |
| 318 | // Latencies - The latency (max of latency from this node to the bb exit) |
| 319 | // for each node. |
| 320 | std::vector<int> Latencies; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 321 | |
| 322 | /// NumNodesSolelyBlocking - This vector contains, for every node in the |
| 323 | /// Queue, the number of nodes that the node is the sole unscheduled |
| 324 | /// predecessor for. This is used as a tie-breaker heuristic for better |
| 325 | /// mobility. |
| 326 | std::vector<unsigned> NumNodesSolelyBlocking; |
| 327 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 328 | std::priority_queue<SUnit*, std::vector<SUnit*>, latency_sort> Queue; |
| 329 | public: |
| 330 | LatencyPriorityQueue() : Queue(latency_sort(this)) { |
| 331 | } |
| 332 | |
Evan Cheng | fd2c5dd | 2006-11-04 09:44:31 +0000 | [diff] [blame] | 333 | void initNodes(std::map<SDNode*, SUnit*> &sumap, |
| 334 | std::vector<SUnit> &sunits) { |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 335 | SUnits = &sunits; |
| 336 | // Calculate node priorities. |
| 337 | CalculatePriorities(); |
| 338 | } |
| 339 | void releaseState() { |
| 340 | SUnits = 0; |
| 341 | Latencies.clear(); |
| 342 | } |
| 343 | |
| 344 | unsigned getLatency(unsigned NodeNum) const { |
| 345 | assert(NodeNum < Latencies.size()); |
| 346 | return Latencies[NodeNum]; |
| 347 | } |
| 348 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 349 | unsigned getNumSolelyBlockNodes(unsigned NodeNum) const { |
| 350 | assert(NodeNum < NumNodesSolelyBlocking.size()); |
| 351 | return NumNodesSolelyBlocking[NodeNum]; |
| 352 | } |
| 353 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 354 | bool empty() const { return Queue.empty(); } |
| 355 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 356 | virtual void push(SUnit *U) { |
| 357 | push_impl(U); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 359 | void push_impl(SUnit *U); |
| 360 | |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 361 | void push_all(const std::vector<SUnit *> &Nodes) { |
| 362 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 363 | push_impl(Nodes[i]); |
Chris Lattner | 25e2556 | 2006-03-10 04:32:49 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 366 | SUnit *pop() { |
Evan Cheng | 61e9f0d | 2006-05-30 18:04:34 +0000 | [diff] [blame] | 367 | if (empty()) return NULL; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 368 | SUnit *V = Queue.top(); |
| 369 | Queue.pop(); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 370 | return V; |
| 371 | } |
Evan Cheng | 7d69389 | 2006-05-09 07:13:34 +0000 | [diff] [blame] | 372 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 373 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 374 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 375 | // single predecessor has a higher priority, since scheduling it will make |
| 376 | // the node available. |
| 377 | void ScheduledNode(SUnit *Node); |
| 378 | |
| 379 | private: |
| 380 | void CalculatePriorities(); |
| 381 | int CalcLatency(const SUnit &SU); |
| 382 | void AdjustPriorityOfUnscheduledPreds(SUnit *SU); |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 383 | SUnit *getSingleUnscheduledPred(SUnit *SU); |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 385 | /// RemoveFromPriorityQueue - This is a really inefficient way to remove a |
| 386 | /// node from a priority queue. We should roll our own heap to make this |
| 387 | /// better or something. |
| 388 | void RemoveFromPriorityQueue(SUnit *SU) { |
| 389 | std::vector<SUnit*> Temp; |
| 390 | |
| 391 | assert(!Queue.empty() && "Not in queue!"); |
| 392 | while (Queue.top() != SU) { |
| 393 | Temp.push_back(Queue.top()); |
| 394 | Queue.pop(); |
| 395 | assert(!Queue.empty() && "Not in queue!"); |
| 396 | } |
| 397 | |
| 398 | // Remove the node from the PQ. |
| 399 | Queue.pop(); |
| 400 | |
| 401 | // Add all the other nodes back. |
| 402 | for (unsigned i = 0, e = Temp.size(); i != e; ++i) |
| 403 | Queue.push(Temp[i]); |
| 404 | } |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 405 | }; |
| 406 | } |
| 407 | |
| 408 | bool latency_sort::operator()(const SUnit *LHS, const SUnit *RHS) const { |
| 409 | unsigned LHSNum = LHS->NodeNum; |
| 410 | unsigned RHSNum = RHS->NodeNum; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 411 | |
| 412 | // The most important heuristic is scheduling the critical path. |
| 413 | unsigned LHSLatency = PQ->getLatency(LHSNum); |
| 414 | unsigned RHSLatency = PQ->getLatency(RHSNum); |
| 415 | if (LHSLatency < RHSLatency) return true; |
| 416 | if (LHSLatency > RHSLatency) return false; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 418 | // After that, if two nodes have identical latencies, look to see if one will |
| 419 | // unblock more other nodes than the other. |
| 420 | unsigned LHSBlocked = PQ->getNumSolelyBlockNodes(LHSNum); |
| 421 | unsigned RHSBlocked = PQ->getNumSolelyBlockNodes(RHSNum); |
| 422 | if (LHSBlocked < RHSBlocked) return true; |
| 423 | if (LHSBlocked > RHSBlocked) return false; |
| 424 | |
| 425 | // Finally, just to provide a stable ordering, use the node number as a |
| 426 | // deciding factor. |
| 427 | return LHSNum < RHSNum; |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | /// CalcNodePriority - Calculate the maximal path from the node to the exit. |
| 432 | /// |
| 433 | int LatencyPriorityQueue::CalcLatency(const SUnit &SU) { |
| 434 | int &Latency = Latencies[SU.NodeNum]; |
| 435 | if (Latency != -1) |
| 436 | return Latency; |
| 437 | |
| 438 | int MaxSuccLatency = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 439 | for (SUnit::const_succ_iterator I = SU.Succs.begin(), E = SU.Succs.end(); |
| 440 | I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 441 | MaxSuccLatency = std::max(MaxSuccLatency, CalcLatency(*I->first)); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 442 | |
| 443 | return Latency = MaxSuccLatency + SU.Latency; |
| 444 | } |
| 445 | |
| 446 | /// CalculatePriorities - Calculate priorities of all scheduling units. |
| 447 | void LatencyPriorityQueue::CalculatePriorities() { |
| 448 | Latencies.assign(SUnits->size(), -1); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 449 | NumNodesSolelyBlocking.assign(SUnits->size(), 0); |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 450 | |
| 451 | for (unsigned i = 0, e = SUnits->size(); i != e; ++i) |
| 452 | CalcLatency((*SUnits)[i]); |
| 453 | } |
| 454 | |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 455 | /// getSingleUnscheduledPred - If there is exactly one unscheduled predecessor |
| 456 | /// of SU, return it, otherwise return null. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 457 | SUnit *LatencyPriorityQueue::getSingleUnscheduledPred(SUnit *SU) { |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 458 | SUnit *OnlyAvailablePred = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 459 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 460 | I != E; ++I) { |
| 461 | SUnit &Pred = *I->first; |
| 462 | if (!Pred.isScheduled) { |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 463 | // We found an available, but not scheduled, predecessor. If it's the |
| 464 | // only one we have found, keep track of it... otherwise give up. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 465 | if (OnlyAvailablePred && OnlyAvailablePred != &Pred) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 466 | return 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 467 | OnlyAvailablePred = &Pred; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 468 | } |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 469 | } |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 470 | |
| 471 | return OnlyAvailablePred; |
| 472 | } |
| 473 | |
| 474 | void LatencyPriorityQueue::push_impl(SUnit *SU) { |
| 475 | // Look at all of the successors of this node. Count the number of nodes that |
| 476 | // this node is the sole unscheduled node for. |
| 477 | unsigned NumNodesBlocking = 0; |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 478 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 479 | I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 480 | if (getSingleUnscheduledPred(I->first) == SU) |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 481 | ++NumNodesBlocking; |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 482 | NumNodesSolelyBlocking[SU->NodeNum] = NumNodesBlocking; |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 483 | |
| 484 | Queue.push(SU); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | // ScheduledNode - As nodes are scheduled, we look to see if there are any |
| 489 | // successor nodes that have a single unscheduled predecessor. If so, that |
| 490 | // single predecessor has a higher priority, since scheduling it will make |
| 491 | // the node available. |
| 492 | void LatencyPriorityQueue::ScheduledNode(SUnit *SU) { |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 493 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 494 | I != E; ++I) |
Chris Lattner | 578d8fc | 2006-03-11 22:24:20 +0000 | [diff] [blame] | 495 | AdjustPriorityOfUnscheduledPreds(I->first); |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /// AdjustPriorityOfUnscheduledPreds - One of the predecessors of SU was just |
| 499 | /// scheduled. If SU is not itself available, then there is at least one |
| 500 | /// predecessor node that has not been scheduled yet. If SU has exactly ONE |
| 501 | /// unscheduled predecessor, we want to increase its priority: it getting |
| 502 | /// scheduled will make this node available, so it is better than some other |
| 503 | /// node of the same priority that will not make a node available. |
| 504 | void LatencyPriorityQueue::AdjustPriorityOfUnscheduledPreds(SUnit *SU) { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 505 | if (SU->isPending) return; // All preds scheduled. |
Chris Lattner | 349e9dd | 2006-03-10 05:51:05 +0000 | [diff] [blame] | 506 | |
| 507 | SUnit *OnlyAvailablePred = getSingleUnscheduledPred(SU); |
| 508 | if (OnlyAvailablePred == 0 || !OnlyAvailablePred->isAvailable) return; |
| 509 | |
| 510 | // Okay, we found a single predecessor that is available, but not scheduled. |
| 511 | // Since it is available, it must be in the priority queue. First remove it. |
| 512 | RemoveFromPriorityQueue(OnlyAvailablePred); |
| 513 | |
| 514 | // Reinsert the node into the priority queue, which recomputes its |
| 515 | // NumNodesSolelyBlocking value. |
| 516 | push(OnlyAvailablePred); |
| 517 | } |
| 518 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 519 | |
| 520 | //===----------------------------------------------------------------------===// |
| 521 | // Public Constructor Functions |
| 522 | //===----------------------------------------------------------------------===// |
| 523 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 524 | /// createTDListDAGScheduler - This creates a top-down list scheduler with a |
| 525 | /// new hazard recognizer. This scheduler takes ownership of the hazard |
| 526 | /// recognizer and deletes it when done. |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 527 | ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS, |
| 528 | SelectionDAG *DAG, |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 529 | MachineBasicBlock *BB) { |
| 530 | return new ScheduleDAGList(*DAG, BB, DAG->getTarget(), |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 531 | new LatencyPriorityQueue(), |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 532 | IS->CreateTargetHazardRecognizer()); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 533 | } |