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