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