Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the ScheduleDAG class, which is a base class used by |
| 11 | // scheduling implementation classes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "pre-RA-sched" |
| 16 | #include "llvm/CodeGen/ScheduleDAG.h" |
Dan Gohman | fc54c55 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
| 21 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | 4cb971c | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 3f0e830 | 2009-07-24 09:53:24 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 4036206 | 2008-11-20 01:41:34 +0000 | [diff] [blame] | 25 | #include <climits> |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Andrew Trick | 4cb971c | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 28 | #ifndef NDEBUG |
Benjamin Kramer | a67f14b | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 29 | static cl::opt<bool> StressSchedOpt( |
Andrew Trick | 4cb971c | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 30 | "stress-sched", cl::Hidden, cl::init(false), |
| 31 | cl::desc("Stress test instruction scheduling")); |
| 32 | #endif |
| 33 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 34 | void SchedulingPriorityQueue::anchor() { } |
| 35 | |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 36 | ScheduleDAG::ScheduleDAG(MachineFunction &mf) |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 37 | : TM(mf.getTarget()), |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 38 | TII(TM.getInstrInfo()), |
| 39 | TRI(TM.getRegisterInfo()), |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 40 | MF(mf), MRI(mf.getRegInfo()), |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 41 | EntrySU(), ExitSU() { |
Andrew Trick | 4cb971c | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 42 | #ifndef NDEBUG |
| 43 | StressSched = StressSchedOpt; |
| 44 | #endif |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ScheduleDAG::~ScheduleDAG() {} |
| 48 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame^] | 49 | /// Clear the DAG state (e.g. between scheduling regions). |
| 50 | void ScheduleDAG::clearDAG() { |
| 51 | SUnits.clear(); |
| 52 | EntrySU = SUnit(); |
| 53 | ExitSU = SUnit(); |
| 54 | } |
| 55 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 56 | /// getInstrDesc helper to handle SDNodes. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 57 | const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const { |
Andrew Trick | 2431223 | 2010-12-24 06:46:50 +0000 | [diff] [blame] | 58 | if (!Node || !Node->isMachineOpcode()) return NULL; |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 59 | return &TII->get(Node->getMachineOpcode()); |
| 60 | } |
| 61 | |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 62 | /// addPred - This adds the specified edge as a pred of the current node if |
| 63 | /// not already. It also adds the current node as a successor of the |
| 64 | /// specified node. |
Andrew Trick | 92e9466 | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 65 | bool SUnit::addPred(const SDep &D) { |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 66 | // If this node already has this depenence, don't add a redundant one. |
Dan Gohman | 5cffa6f | 2009-02-11 00:12:28 +0000 | [diff] [blame] | 67 | for (SmallVector<SDep, 4>::const_iterator I = Preds.begin(), E = Preds.end(); |
| 68 | I != E; ++I) |
| 69 | if (*I == D) |
Andrew Trick | 92e9466 | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 70 | return false; |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 71 | // Now add a corresponding succ to N. |
| 72 | SDep P = D; |
| 73 | P.setSUnit(this); |
| 74 | SUnit *N = D.getSUnit(); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 75 | // Update the bookkeeping. |
| 76 | if (D.getKind() == SDep::Data) { |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 77 | assert(NumPreds < UINT_MAX && "NumPreds will overflow!"); |
| 78 | assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 79 | ++NumPreds; |
| 80 | ++N->NumSuccs; |
| 81 | } |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 82 | if (!N->isScheduled) { |
| 83 | assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 84 | ++NumPredsLeft; |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 85 | } |
| 86 | if (!isScheduled) { |
| 87 | assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 88 | ++N->NumSuccsLeft; |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 89 | } |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 90 | Preds.push_back(D); |
Dan Gohman | a1f50e2 | 2009-01-13 19:08:45 +0000 | [diff] [blame] | 91 | N->Succs.push_back(P); |
Dan Gohman | a80c859 | 2009-01-05 22:40:26 +0000 | [diff] [blame] | 92 | if (P.getLatency() != 0) { |
| 93 | this->setDepthDirty(); |
| 94 | N->setHeightDirty(); |
| 95 | } |
Andrew Trick | 92e9466 | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 96 | return true; |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// removePred - This removes the specified edge as a pred of the current |
| 100 | /// node if it exists. It also removes the current node as a successor of |
| 101 | /// the specified node. |
| 102 | void SUnit::removePred(const SDep &D) { |
| 103 | // Find the matching predecessor. |
| 104 | for (SmallVector<SDep, 4>::iterator I = Preds.begin(), E = Preds.end(); |
| 105 | I != E; ++I) |
| 106 | if (*I == D) { |
| 107 | bool FoundSucc = false; |
| 108 | // Find the corresponding successor in N. |
| 109 | SDep P = D; |
| 110 | P.setSUnit(this); |
| 111 | SUnit *N = D.getSUnit(); |
| 112 | for (SmallVector<SDep, 4>::iterator II = N->Succs.begin(), |
| 113 | EE = N->Succs.end(); II != EE; ++II) |
| 114 | if (*II == P) { |
| 115 | FoundSucc = true; |
| 116 | N->Succs.erase(II); |
| 117 | break; |
| 118 | } |
| 119 | assert(FoundSucc && "Mismatching preds / succs lists!"); |
Duncan Sands | 1f6a329 | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 120 | (void)FoundSucc; |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 121 | Preds.erase(I); |
Dan Gohman | a1f50e2 | 2009-01-13 19:08:45 +0000 | [diff] [blame] | 122 | // Update the bookkeeping. |
| 123 | if (P.getKind() == SDep::Data) { |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 124 | assert(NumPreds > 0 && "NumPreds will underflow!"); |
| 125 | assert(N->NumSuccs > 0 && "NumSuccs will underflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 126 | --NumPreds; |
| 127 | --N->NumSuccs; |
| 128 | } |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 129 | if (!N->isScheduled) { |
| 130 | assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 131 | --NumPredsLeft; |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 132 | } |
| 133 | if (!isScheduled) { |
| 134 | assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!"); |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 135 | --N->NumSuccsLeft; |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 136 | } |
Dan Gohman | a80c859 | 2009-01-05 22:40:26 +0000 | [diff] [blame] | 137 | if (P.getLatency() != 0) { |
| 138 | this->setDepthDirty(); |
| 139 | N->setHeightDirty(); |
| 140 | } |
Dan Gohman | c6b680e | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 145 | void SUnit::setDepthDirty() { |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 146 | if (!isDepthCurrent) return; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 147 | SmallVector<SUnit*, 8> WorkList; |
| 148 | WorkList.push_back(this); |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 149 | do { |
Dan Gohman | e19c636 | 2008-12-20 16:42:33 +0000 | [diff] [blame] | 150 | SUnit *SU = WorkList.pop_back_val(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 151 | SU->isDepthCurrent = false; |
Dan Gohman | f89e6e6 | 2008-12-20 16:34:57 +0000 | [diff] [blame] | 152 | for (SUnit::const_succ_iterator I = SU->Succs.begin(), |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 153 | E = SU->Succs.end(); I != E; ++I) { |
| 154 | SUnit *SuccSU = I->getSUnit(); |
| 155 | if (SuccSU->isDepthCurrent) |
| 156 | WorkList.push_back(SuccSU); |
| 157 | } |
| 158 | } while (!WorkList.empty()); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void SUnit::setHeightDirty() { |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 162 | if (!isHeightCurrent) return; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 163 | SmallVector<SUnit*, 8> WorkList; |
| 164 | WorkList.push_back(this); |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 165 | do { |
Dan Gohman | e19c636 | 2008-12-20 16:42:33 +0000 | [diff] [blame] | 166 | SUnit *SU = WorkList.pop_back_val(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 167 | SU->isHeightCurrent = false; |
Dan Gohman | f89e6e6 | 2008-12-20 16:34:57 +0000 | [diff] [blame] | 168 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), |
Dan Gohman | 8044e9b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 169 | E = SU->Preds.end(); I != E; ++I) { |
| 170 | SUnit *PredSU = I->getSUnit(); |
| 171 | if (PredSU->isHeightCurrent) |
| 172 | WorkList.push_back(PredSU); |
| 173 | } |
| 174 | } while (!WorkList.empty()); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /// setDepthToAtLeast - Update this node's successors to reflect the |
| 178 | /// fact that this node's depth just increased. |
| 179 | /// |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 180 | void SUnit::setDepthToAtLeast(unsigned NewDepth) { |
| 181 | if (NewDepth <= getDepth()) |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 182 | return; |
| 183 | setDepthDirty(); |
| 184 | Depth = NewDepth; |
| 185 | isDepthCurrent = true; |
| 186 | } |
| 187 | |
| 188 | /// setHeightToAtLeast - Update this node's predecessors to reflect the |
| 189 | /// fact that this node's height just increased. |
| 190 | /// |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 191 | void SUnit::setHeightToAtLeast(unsigned NewHeight) { |
| 192 | if (NewHeight <= getHeight()) |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 193 | return; |
| 194 | setHeightDirty(); |
| 195 | Height = NewHeight; |
| 196 | isHeightCurrent = true; |
| 197 | } |
| 198 | |
| 199 | /// ComputeDepth - Calculate the maximal path from the node to the exit. |
| 200 | /// |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 201 | void SUnit::ComputeDepth() { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 202 | SmallVector<SUnit*, 8> WorkList; |
| 203 | WorkList.push_back(this); |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 204 | do { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 205 | SUnit *Cur = WorkList.back(); |
| 206 | |
| 207 | bool Done = true; |
| 208 | unsigned MaxPredDepth = 0; |
| 209 | for (SUnit::const_pred_iterator I = Cur->Preds.begin(), |
| 210 | E = Cur->Preds.end(); I != E; ++I) { |
| 211 | SUnit *PredSU = I->getSUnit(); |
| 212 | if (PredSU->isDepthCurrent) |
| 213 | MaxPredDepth = std::max(MaxPredDepth, |
| 214 | PredSU->Depth + I->getLatency()); |
| 215 | else { |
| 216 | Done = false; |
| 217 | WorkList.push_back(PredSU); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (Done) { |
| 222 | WorkList.pop_back(); |
| 223 | if (MaxPredDepth != Cur->Depth) { |
| 224 | Cur->setDepthDirty(); |
| 225 | Cur->Depth = MaxPredDepth; |
| 226 | } |
| 227 | Cur->isDepthCurrent = true; |
| 228 | } |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 229 | } while (!WorkList.empty()); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /// ComputeHeight - Calculate the maximal path from the node to the entry. |
| 233 | /// |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 234 | void SUnit::ComputeHeight() { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 235 | SmallVector<SUnit*, 8> WorkList; |
| 236 | WorkList.push_back(this); |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 237 | do { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 238 | SUnit *Cur = WorkList.back(); |
| 239 | |
| 240 | bool Done = true; |
| 241 | unsigned MaxSuccHeight = 0; |
| 242 | for (SUnit::const_succ_iterator I = Cur->Succs.begin(), |
| 243 | E = Cur->Succs.end(); I != E; ++I) { |
| 244 | SUnit *SuccSU = I->getSUnit(); |
| 245 | if (SuccSU->isHeightCurrent) |
| 246 | MaxSuccHeight = std::max(MaxSuccHeight, |
| 247 | SuccSU->Height + I->getLatency()); |
| 248 | else { |
| 249 | Done = false; |
| 250 | WorkList.push_back(SuccSU); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (Done) { |
| 255 | WorkList.pop_back(); |
| 256 | if (MaxSuccHeight != Cur->Height) { |
| 257 | Cur->setHeightDirty(); |
| 258 | Cur->Height = MaxSuccHeight; |
| 259 | } |
| 260 | Cur->isHeightCurrent = true; |
| 261 | } |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 262 | } while (!WorkList.empty()); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 265 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or |
| 266 | /// a group of nodes flagged together. |
| 267 | void SUnit::dump(const ScheduleDAG *G) const { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 268 | dbgs() << "SU(" << NodeNum << "): "; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 269 | G->dumpNode(this); |
| 270 | } |
| 271 | |
| 272 | void SUnit::dumpAll(const ScheduleDAG *G) const { |
| 273 | dump(G); |
| 274 | |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 275 | dbgs() << " # preds left : " << NumPredsLeft << "\n"; |
| 276 | dbgs() << " # succs left : " << NumSuccsLeft << "\n"; |
Andrew Trick | 92e9466 | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 277 | dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n"; |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 278 | dbgs() << " Latency : " << Latency << "\n"; |
| 279 | dbgs() << " Depth : " << Depth << "\n"; |
| 280 | dbgs() << " Height : " << Height << "\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 281 | |
| 282 | if (Preds.size() != 0) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 283 | dbgs() << " Predecessors:\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 284 | for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end(); |
| 285 | I != E; ++I) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 286 | dbgs() << " "; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 287 | switch (I->getKind()) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 288 | case SDep::Data: dbgs() << "val "; break; |
| 289 | case SDep::Anti: dbgs() << "anti"; break; |
| 290 | case SDep::Output: dbgs() << "out "; break; |
| 291 | case SDep::Order: dbgs() << "ch "; break; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 292 | } |
Jakob Stoklund Olesen | 0b923d9 | 2012-02-17 21:44:51 +0000 | [diff] [blame] | 293 | dbgs() << "SU(" << I->getSUnit()->NodeNum << ")"; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 294 | if (I->isArtificial()) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 295 | dbgs() << " *"; |
| 296 | dbgs() << ": Latency=" << I->getLatency(); |
Andrew Trick | 4cb971c | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 297 | if (I->isAssignedRegDep()) |
Jakob Stoklund Olesen | 0b923d9 | 2012-02-17 21:44:51 +0000 | [diff] [blame] | 298 | dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI); |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 299 | dbgs() << "\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | if (Succs.size() != 0) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 303 | dbgs() << " Successors:\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 304 | for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end(); |
| 305 | I != E; ++I) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 306 | dbgs() << " "; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 307 | switch (I->getKind()) { |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 308 | case SDep::Data: dbgs() << "val "; break; |
| 309 | case SDep::Anti: dbgs() << "anti"; break; |
| 310 | case SDep::Output: dbgs() << "out "; break; |
| 311 | case SDep::Order: dbgs() << "ch "; break; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 312 | } |
Jakob Stoklund Olesen | 0b923d9 | 2012-02-17 21:44:51 +0000 | [diff] [blame] | 313 | dbgs() << "SU(" << I->getSUnit()->NodeNum << ")"; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 314 | if (I->isArtificial()) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 315 | dbgs() << " *"; |
| 316 | dbgs() << ": Latency=" << I->getLatency(); |
| 317 | dbgs() << "\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 320 | dbgs() << "\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 321 | } |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 322 | |
| 323 | #ifndef NDEBUG |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 324 | /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that |
| 325 | /// their state is consistent. Return the number of scheduled nodes. |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 326 | /// |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 327 | unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) { |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 328 | bool AnyNotSched = false; |
| 329 | unsigned DeadNodes = 0; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 330 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 331 | if (!SUnits[i].isScheduled) { |
| 332 | if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) { |
| 333 | ++DeadNodes; |
| 334 | continue; |
| 335 | } |
| 336 | if (!AnyNotSched) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 337 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 338 | SUnits[i].dump(this); |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 339 | dbgs() << "has not been scheduled!\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 340 | AnyNotSched = true; |
| 341 | } |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 342 | if (SUnits[i].isScheduled && |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 343 | (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) > |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 344 | unsigned(INT_MAX)) { |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 345 | if (!AnyNotSched) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 346 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 347 | SUnits[i].dump(this); |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 348 | dbgs() << "has an unexpected " |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 349 | << (isBottomUp ? "Height" : "Depth") << " value!\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 350 | AnyNotSched = true; |
| 351 | } |
| 352 | if (isBottomUp) { |
| 353 | if (SUnits[i].NumSuccsLeft != 0) { |
| 354 | if (!AnyNotSched) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 355 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 356 | SUnits[i].dump(this); |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 357 | dbgs() << "has successors left!\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 358 | AnyNotSched = true; |
| 359 | } |
| 360 | } else { |
| 361 | if (SUnits[i].NumPredsLeft != 0) { |
| 362 | if (!AnyNotSched) |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 363 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 364 | SUnits[i].dump(this); |
David Greene | 4b134d1 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 365 | dbgs() << "has predecessors left!\n"; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 366 | AnyNotSched = true; |
| 367 | } |
| 368 | } |
| 369 | } |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 370 | assert(!AnyNotSched); |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 371 | return SUnits.size() - DeadNodes; |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 372 | } |
| 373 | #endif |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 374 | |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 375 | /// InitDAGTopologicalSorting - create the initial topological |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 376 | /// ordering from the DAG to be scheduled. |
| 377 | /// |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 378 | /// The idea of the algorithm is taken from |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 379 | /// "Online algorithms for managing the topological order of |
| 380 | /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 381 | /// This is the MNR algorithm, which was first introduced by |
| 382 | /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 383 | /// "Maintaining a topological order under edge insertions". |
| 384 | /// |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 385 | /// Short description of the algorithm: |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 386 | /// |
| 387 | /// Topological ordering, ord, of a DAG maps each node to a topological |
| 388 | /// index so that for all edges X->Y it is the case that ord(X) < ord(Y). |
| 389 | /// |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 390 | /// This means that if there is a path from the node X to the node Z, |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 391 | /// then ord(X) < ord(Z). |
| 392 | /// |
| 393 | /// This property can be used to check for reachability of nodes: |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 394 | /// if Z is reachable from X, then an insertion of the edge Z->X would |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 395 | /// create a cycle. |
| 396 | /// |
| 397 | /// The algorithm first computes a topological ordering for the DAG by |
| 398 | /// initializing the Index2Node and Node2Index arrays and then tries to keep |
| 399 | /// the ordering up-to-date after edge insertions by reordering the DAG. |
| 400 | /// |
| 401 | /// On insertion of the edge X->Y, the algorithm first marks by calling DFS |
| 402 | /// the nodes reachable from Y, and then shifts them using Shift to lie |
| 403 | /// immediately after X in Index2Node. |
| 404 | void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() { |
| 405 | unsigned DAGSize = SUnits.size(); |
| 406 | std::vector<SUnit*> WorkList; |
| 407 | WorkList.reserve(DAGSize); |
| 408 | |
| 409 | Index2Node.resize(DAGSize); |
| 410 | Node2Index.resize(DAGSize); |
| 411 | |
| 412 | // Initialize the data structures. |
| 413 | for (unsigned i = 0, e = DAGSize; i != e; ++i) { |
| 414 | SUnit *SU = &SUnits[i]; |
| 415 | int NodeNum = SU->NodeNum; |
| 416 | unsigned Degree = SU->Succs.size(); |
| 417 | // Temporarily use the Node2Index array as scratch space for degree counts. |
| 418 | Node2Index[NodeNum] = Degree; |
| 419 | |
| 420 | // Is it a node without dependencies? |
| 421 | if (Degree == 0) { |
| 422 | assert(SU->Succs.empty() && "SUnit should have no successors"); |
| 423 | // Collect leaf nodes. |
| 424 | WorkList.push_back(SU); |
| 425 | } |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 426 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 427 | |
| 428 | int Id = DAGSize; |
| 429 | while (!WorkList.empty()) { |
| 430 | SUnit *SU = WorkList.back(); |
| 431 | WorkList.pop_back(); |
| 432 | Allocate(SU->NodeNum, --Id); |
| 433 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 434 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 435 | SUnit *SU = I->getSUnit(); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 436 | if (!--Node2Index[SU->NodeNum]) |
| 437 | // If all dependencies of the node are processed already, |
| 438 | // then the node can be computed now. |
| 439 | WorkList.push_back(SU); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | Visited.resize(DAGSize); |
| 444 | |
| 445 | #ifndef NDEBUG |
| 446 | // Check correctness of the ordering |
| 447 | for (unsigned i = 0, e = DAGSize; i != e; ++i) { |
| 448 | SUnit *SU = &SUnits[i]; |
| 449 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 450 | I != E; ++I) { |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 451 | assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] && |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 452 | "Wrong topological sorting"); |
| 453 | } |
| 454 | } |
| 455 | #endif |
| 456 | } |
| 457 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 458 | /// AddPred - Updates the topological ordering to accommodate an edge |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 459 | /// to be added from SUnit X to SUnit Y. |
| 460 | void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) { |
| 461 | int UpperBound, LowerBound; |
| 462 | LowerBound = Node2Index[Y->NodeNum]; |
| 463 | UpperBound = Node2Index[X->NodeNum]; |
| 464 | bool HasLoop = false; |
| 465 | // Is Ord(X) < Ord(Y) ? |
| 466 | if (LowerBound < UpperBound) { |
| 467 | // Update the topological order. |
| 468 | Visited.reset(); |
| 469 | DFS(Y, UpperBound, HasLoop); |
| 470 | assert(!HasLoop && "Inserted edge creates a loop!"); |
| 471 | // Recompute topological indexes. |
| 472 | Shift(Visited, LowerBound, UpperBound); |
| 473 | } |
| 474 | } |
| 475 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 476 | /// RemovePred - Updates the topological ordering to accommodate an |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 477 | /// an edge to be removed from the specified node N from the predecessors |
| 478 | /// of the current node M. |
| 479 | void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) { |
| 480 | // InitDAGTopologicalSorting(); |
| 481 | } |
| 482 | |
| 483 | /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark |
| 484 | /// all nodes affected by the edge insertion. These nodes will later get new |
| 485 | /// topological indexes by means of the Shift method. |
Dan Gohman | e3a49cd | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 486 | void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound, |
Chris Lattner | 5078293 | 2010-12-20 00:50:16 +0000 | [diff] [blame] | 487 | bool &HasLoop) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 488 | std::vector<const SUnit*> WorkList; |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 489 | WorkList.reserve(SUnits.size()); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 490 | |
| 491 | WorkList.push_back(SU); |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 492 | do { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 493 | SU = WorkList.back(); |
| 494 | WorkList.pop_back(); |
| 495 | Visited.set(SU->NodeNum); |
| 496 | for (int I = SU->Succs.size()-1; I >= 0; --I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 497 | int s = SU->Succs[I].getSUnit()->NodeNum; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 498 | if (Node2Index[s] == UpperBound) { |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 499 | HasLoop = true; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | // Visit successors if not already and in affected region. |
| 503 | if (!Visited.test(s) && Node2Index[s] < UpperBound) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 504 | WorkList.push_back(SU->Succs[I].getSUnit()); |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
Dan Gohman | 1578f84 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 507 | } while (!WorkList.empty()); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 508 | } |
| 509 | |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 510 | /// Shift - Renumber the nodes so that the topological ordering is |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 511 | /// preserved. |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 512 | void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound, |
Dan Gohman | e3a49cd | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 513 | int UpperBound) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 514 | std::vector<int> L; |
| 515 | int shift = 0; |
| 516 | int i; |
| 517 | |
| 518 | for (i = LowerBound; i <= UpperBound; ++i) { |
| 519 | // w is node at topological index i. |
| 520 | int w = Index2Node[i]; |
| 521 | if (Visited.test(w)) { |
| 522 | // Unmark. |
| 523 | Visited.reset(w); |
| 524 | L.push_back(w); |
| 525 | shift = shift + 1; |
| 526 | } else { |
| 527 | Allocate(w, i - shift); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | for (unsigned j = 0; j < L.size(); ++j) { |
| 532 | Allocate(L[j], i - shift); |
| 533 | i = i + 1; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /// WillCreateCycle - Returns true if adding an edge from SU to TargetSU will |
| 539 | /// create a cycle. |
| 540 | bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *SU, SUnit *TargetSU) { |
| 541 | if (IsReachable(TargetSU, SU)) |
| 542 | return true; |
| 543 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 544 | I != E; ++I) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 545 | if (I->isAssignedRegDep() && |
| 546 | IsReachable(TargetSU, I->getSUnit())) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 547 | return true; |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | /// IsReachable - Checks if SU is reachable from TargetSU. |
Dan Gohman | e3a49cd | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 552 | bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU, |
| 553 | const SUnit *TargetSU) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 554 | // If insertion of the edge SU->TargetSU would create a cycle |
| 555 | // then there is a path from TargetSU to SU. |
| 556 | int UpperBound, LowerBound; |
| 557 | LowerBound = Node2Index[TargetSU->NodeNum]; |
| 558 | UpperBound = Node2Index[SU->NodeNum]; |
| 559 | bool HasLoop = false; |
| 560 | // Is Ord(TargetSU) < Ord(SU) ? |
| 561 | if (LowerBound < UpperBound) { |
| 562 | Visited.reset(); |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 563 | // There may be a path from TargetSU to SU. Check for it. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 564 | DFS(TargetSU, UpperBound, HasLoop); |
| 565 | } |
| 566 | return HasLoop; |
| 567 | } |
| 568 | |
| 569 | /// Allocate - assign the topological index to the node n. |
| 570 | void ScheduleDAGTopologicalSort::Allocate(int n, int index) { |
| 571 | Node2Index[n] = index; |
| 572 | Index2Node[index] = n; |
| 573 | } |
| 574 | |
John Mosby | 9f71f80 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 575 | ScheduleDAGTopologicalSort:: |
| 576 | ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits) : SUnits(sunits) {} |
Dan Gohman | fc54c55 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 577 | |
| 578 | ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {} |