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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 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 | |
Dale Johannesen | 2182f06 | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "pre-RA-sched" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 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" |
Dan Gohman | 3a4be0f | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegisterInfo.h" |
Owen Anderson | 8c2c1e9 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Dan Gohman | 0d8a61e | 2008-06-23 23:40:09 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/PriorityQueue.h" |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | d2760c0 | 2008-11-15 00:23:40 +0000 | [diff] [blame] | 33 | #include "LatencyPriorityQueue.h" |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 34 | #include <climits> |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Lattner | aee775a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 37 | STATISTIC(NumNoops , "Number of noops inserted"); |
| 38 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 39 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 40 | static RegisterScheduler |
Dan Gohman | 9c4b7d5 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 41 | tdListDAGScheduler("list-td", "Top-down list scheduler", |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 42 | createTDListDAGScheduler); |
| 43 | |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 44 | namespace { |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | /// ScheduleDAGList - The actual list scheduler implementation. This supports |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 47 | /// top-down scheduling. |
Chris Lattner | 9e95acc | 2006-03-09 06:37:29 +0000 | [diff] [blame] | 48 | /// |
Chris Lattner | e097e6f | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 49 | class VISIBILITY_HIDDEN ScheduleDAGList : public ScheduleDAG { |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 50 | private: |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 51 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 52 | /// |
| 53 | SchedulingPriorityQueue *AvailableQueue; |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 55 | /// PendingQueue - This contains all of the instructions whose operands have |
| 56 | /// been issued, but their results are not ready yet (due to the latency of |
| 57 | /// the operation). Once the operands becomes available, the instruction is |
Dan Gohman | a687fd8 | 2008-11-17 19:45:19 +0000 | [diff] [blame] | 58 | /// added to the AvailableQueue. |
| 59 | std::vector<SUnit*> PendingQueue; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame] | 60 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 61 | /// HazardRec - The hazard recognizer to use. |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 62 | HazardRecognizer *HazardRec; |
Evan Cheng | 9add880 | 2006-05-04 19:16:39 +0000 | [diff] [blame] | 63 | |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 64 | public: |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 65 | ScheduleDAGList(SelectionDAG *dag, MachineBasicBlock *bb, |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 66 | const TargetMachine &tm, |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 67 | SchedulingPriorityQueue *availqueue, |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 68 | HazardRecognizer *HR) |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 69 | : ScheduleDAG(dag, bb, tm), |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 70 | AvailableQueue(availqueue), HazardRec(HR) { |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 71 | } |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 72 | |
| 73 | ~ScheduleDAGList() { |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 74 | delete HazardRec; |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 75 | delete AvailableQueue; |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 76 | } |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 77 | |
| 78 | void Schedule(); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 79 | |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 80 | private: |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 81 | void ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain); |
Chris Lattner | 063086b | 2006-03-11 22:34:41 +0000 | [diff] [blame] | 82 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 83 | void ListScheduleTopDown(); |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 84 | }; |
Chris Lattner | af5e26c | 2006-03-08 04:37:58 +0000 | [diff] [blame] | 85 | } // end anonymous namespace |
Evan Cheng | ab49556 | 2006-01-25 09:14:32 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 47639db | 2006-03-06 00:22:00 +0000 | [diff] [blame] | 87 | HazardRecognizer::~HazardRecognizer() {} |
| 88 | |
Evan Cheng | c4c339c | 2006-01-26 00:30:29 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 90 | /// Schedule - Schedule the DAG using list scheduling. |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 91 | void ScheduleDAGList::Schedule() { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 92 | DOUT << "********** List Scheduling **********\n"; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 93 | |
| 94 | // Build scheduling units. |
| 95 | BuildSchedUnits(); |
Evan Cheng | 7d69389 | 2006-05-09 07:13:34 +0000 | [diff] [blame] | 96 | |
Dan Gohman | 46520a2 | 2008-06-21 19:18:17 +0000 | [diff] [blame] | 97 | AvailableQueue->initNodes(SUnits); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 98 | |
Evan Cheng | d38c22b | 2006-05-11 23:55:42 +0000 | [diff] [blame] | 99 | ListScheduleTopDown(); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 101 | AvailableQueue->releaseState(); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 105 | // Top-Down Scheduling |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 109 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
| 110 | void ScheduleDAGList::ReleaseSucc(SUnit *SU, SUnit *SuccSU, bool isChain) { |
| 111 | --SuccSU->NumPredsLeft; |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 112 | |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 113 | #ifndef NDEBUG |
| 114 | if (SuccSU->NumPredsLeft < 0) { |
| 115 | cerr << "*** Scheduling failed! ***\n"; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame^] | 116 | SuccSU->dump(this); |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 117 | cerr << " has been released too many times!\n"; |
| 118 | assert(0); |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | // Compute how many cycles it will be before this actually becomes |
| 123 | // available. This is the max of the start time of all predecessors plus |
| 124 | // their latencies. |
| 125 | // If this is a token edge, we don't need to wait for the latency of the |
| 126 | // preceeding instruction (e.g. a long-latency load) unless there is also |
| 127 | // some other data dependence. |
| 128 | unsigned PredDoneCycle = SU->Cycle; |
| 129 | if (!isChain) |
| 130 | PredDoneCycle += SU->Latency; |
| 131 | else if (SU->Latency) |
| 132 | PredDoneCycle += 1; |
| 133 | SuccSU->CycleBound = std::max(SuccSU->CycleBound, PredDoneCycle); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 134 | |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 135 | if (SuccSU->NumPredsLeft == 0) { |
Dan Gohman | a687fd8 | 2008-11-17 19:45:19 +0000 | [diff] [blame] | 136 | PendingQueue.push_back(SuccSU); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 141 | /// count of its successors. If a successor pending count is zero, add it to |
| 142 | /// the Available queue. |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 143 | void ScheduleDAGList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 144 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame^] | 145 | DEBUG(SU->dump(this)); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 146 | |
| 147 | Sequence.push_back(SU); |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 148 | SU->Cycle = CurCycle; |
Dan Gohman | 92a36d7 | 2008-11-17 21:31:02 +0000 | [diff] [blame] | 149 | |
Dan Gohman | 68294c0 | 2008-11-15 00:24:23 +0000 | [diff] [blame] | 150 | // Top down: release successors. |
Chris Lattner | d86418a | 2006-08-17 00:09:56 +0000 | [diff] [blame] | 151 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 152 | I != E; ++I) |
Dan Gohman | 5ebdb98 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 153 | ReleaseSucc(SU, I->Dep, I->isCtrl); |
Dan Gohman | 92a36d7 | 2008-11-17 21:31:02 +0000 | [diff] [blame] | 154 | |
| 155 | SU->isScheduled = true; |
| 156 | AvailableQueue->ScheduledNode(SU); |
Chris Lattner | 9995a0c | 2006-03-11 22:28:35 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 159 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 160 | /// schedulers. |
Chris Lattner | 399bee2 | 2006-03-09 06:48:37 +0000 | [diff] [blame] | 161 | void ScheduleDAGList::ListScheduleTopDown() { |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 162 | unsigned CurCycle = 0; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 164 | // All leaves to Available queue. |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 165 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 166 | // It is available if it has no predecessors. |
Dan Gohman | 4370f26 | 2008-04-15 01:22:18 +0000 | [diff] [blame] | 167 | if (SUnits[i].Preds.empty()) { |
Chris Lattner | 356183d | 2006-03-11 22:44:37 +0000 | [diff] [blame] | 168 | AvailableQueue->push(&SUnits[i]); |
Dan Gohman | 17c226b | 2008-11-17 16:37:30 +0000 | [diff] [blame] | 169 | SUnits[i].isAvailable = true; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // While Available queue is not empty, grab the node with the highest |
| 174 | // priority. If it is not ready put it back. Schedule the node. |
| 175 | std::vector<SUnit*> NotReady; |
Dan Gohman | e6e1348 | 2008-06-21 15:52:51 +0000 | [diff] [blame] | 176 | Sequence.reserve(SUnits.size()); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 177 | while (!AvailableQueue->empty() || !PendingQueue.empty()) { |
| 178 | // Check to see if any of the pending instructions are ready to issue. If |
| 179 | // so, add them to the available queue. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 180 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | a687fd8 | 2008-11-17 19:45:19 +0000 | [diff] [blame] | 181 | if (PendingQueue[i]->CycleBound == CurCycle) { |
| 182 | AvailableQueue->push(PendingQueue[i]); |
| 183 | PendingQueue[i]->isAvailable = true; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 184 | PendingQueue[i] = PendingQueue.back(); |
| 185 | PendingQueue.pop_back(); |
| 186 | --i; --e; |
| 187 | } else { |
Dan Gohman | a687fd8 | 2008-11-17 19:45:19 +0000 | [diff] [blame] | 188 | assert(PendingQueue[i]->CycleBound > CurCycle && "Negative latency?"); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 191 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 192 | // If there are no instructions available, don't try to issue anything, and |
| 193 | // don't advance the hazard recognizer. |
| 194 | if (AvailableQueue->empty()) { |
| 195 | ++CurCycle; |
| 196 | continue; |
| 197 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 198 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 199 | SUnit *FoundSUnit = 0; |
| 200 | SDNode *FoundNode = 0; |
| 201 | |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 202 | bool HasNoopHazards = false; |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 203 | while (!AvailableQueue->empty()) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 204 | SUnit *CurSUnit = AvailableQueue->pop(); |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 205 | |
| 206 | // Get the node represented by this SUnit. |
Dan Gohman | 1ddfcba | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 207 | FoundNode = CurSUnit->getNode(); |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 209 | // If this is a pseudo op, like copyfromreg, look to see if there is a |
| 210 | // real target node flagged to it. If so, use the target node. |
Dan Gohman | 072734e | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 211 | while (!FoundNode->isMachineOpcode()) { |
| 212 | SDNode *N = FoundNode->getFlaggedNode(); |
| 213 | if (!N) break; |
| 214 | FoundNode = N; |
| 215 | } |
Chris Lattner | 0c801bd | 2006-03-07 05:40:43 +0000 | [diff] [blame] | 216 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 217 | HazardRecognizer::HazardType HT = HazardRec->getHazardType(FoundNode); |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 218 | if (HT == HazardRecognizer::NoHazard) { |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 219 | FoundSUnit = CurSUnit; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | |
| 223 | // Remember if this is a noop hazard. |
| 224 | HasNoopHazards |= HT == HazardRecognizer::NoopHazard; |
| 225 | |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 226 | NotReady.push_back(CurSUnit); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 229 | // Add the nodes that aren't ready back onto the available list. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 230 | if (!NotReady.empty()) { |
| 231 | AvailableQueue->push_all(NotReady); |
| 232 | NotReady.clear(); |
| 233 | } |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 234 | |
| 235 | // If we found a node to schedule, do it now. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 236 | if (FoundSUnit) { |
| 237 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
| 238 | HazardRec->EmitInstruction(FoundNode); |
Chris Lattner | 572003c | 2006-03-12 00:38:57 +0000 | [diff] [blame] | 239 | |
| 240 | // If this is a pseudo-op node, we don't want to increment the current |
| 241 | // cycle. |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 242 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 243 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 244 | } else if (!HasNoopHazards) { |
| 245 | // Otherwise, we have a pipeline stall, but no other problem, just advance |
| 246 | // the current cycle and try again. |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 247 | DOUT << "*** Advancing cycle, no work to do\n"; |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 248 | HazardRec->AdvanceCycle(); |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 249 | ++NumStalls; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 250 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 251 | } else { |
| 252 | // Otherwise, we have no instructions to issue and we have instructions |
| 253 | // that will fault if we don't do this right. This is the case for |
| 254 | // processors without pipeline interlocks and other cases. |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 255 | DOUT << "*** Emitting noop\n"; |
Chris Lattner | 543832d | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 256 | HazardRec->EmitNoop(); |
Chris Lattner | 00b52ea | 2006-03-05 23:59:20 +0000 | [diff] [blame] | 257 | Sequence.push_back(0); // NULL SUnit* -> noop |
Chris Lattner | fa5e1c9 | 2006-03-05 23:13:56 +0000 | [diff] [blame] | 258 | ++NumNoops; |
Chris Lattner | a767dbf | 2006-03-12 09:01:41 +0000 | [diff] [blame] | 259 | ++CurCycle; |
Chris Lattner | e50c092 | 2006-03-05 22:45:01 +0000 | [diff] [blame] | 260 | } |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | #ifndef NDEBUG |
| 264 | // Verify that all SUnits were scheduled. |
| 265 | bool AnyNotSched = false; |
Chris Lattner | 42e2026 | 2006-03-08 04:54:34 +0000 | [diff] [blame] | 266 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Evan Cheng | 038dcc5 | 2007-09-28 19:24:24 +0000 | [diff] [blame] | 267 | if (SUnits[i].NumPredsLeft != 0) { |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 268 | if (!AnyNotSched) |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 269 | cerr << "*** List scheduling failed! ***\n"; |
Dan Gohman | 22d07b1 | 2008-11-18 02:06:40 +0000 | [diff] [blame^] | 270 | SUnits[i].dump(this); |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 271 | cerr << "has not been scheduled!\n"; |
Chris Lattner | 98ecb8e | 2006-03-05 21:10:33 +0000 | [diff] [blame] | 272 | AnyNotSched = true; |
| 273 | } |
| 274 | } |
| 275 | assert(!AnyNotSched); |
| 276 | #endif |
| 277 | } |
| 278 | |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 279 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9df6475 | 2006-03-09 06:35:14 +0000 | [diff] [blame] | 280 | // Public Constructor Functions |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | |
Jim Laskey | 95eda5b | 2006-08-01 14:21:23 +0000 | [diff] [blame] | 283 | /// createTDListDAGScheduler - This creates a top-down list scheduler with a |
| 284 | /// new hazard recognizer. This scheduler takes ownership of the hazard |
| 285 | /// recognizer and deletes it when done. |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 286 | ScheduleDAG* llvm::createTDListDAGScheduler(SelectionDAGISel *IS, |
| 287 | SelectionDAG *DAG, |
Dan Gohman | 5499e89 | 2008-11-11 17:50:47 +0000 | [diff] [blame] | 288 | const TargetMachine *TM, |
Evan Cheng | 2c97731 | 2008-07-01 18:05:03 +0000 | [diff] [blame] | 289 | MachineBasicBlock *BB, bool Fast) { |
Dan Gohman | 5a390b9 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 290 | return new ScheduleDAGList(DAG, BB, *TM, |
Chris Lattner | 6398c13 | 2006-03-09 07:38:27 +0000 | [diff] [blame] | 291 | new LatencyPriorityQueue(), |
Jim Laskey | 03593f7 | 2006-08-01 18:29:48 +0000 | [diff] [blame] | 292 | IS->CreateTargetHazardRecognizer()); |
Evan Cheng | 3127234 | 2006-01-23 08:26:10 +0000 | [diff] [blame] | 293 | } |