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