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