Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 1 | //===- ScheduleDAG.cpp - Implement the ScheduleDAG class ------------------===// |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 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 | // |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 10 | /// \file Implements the ScheduleDAG class, which is a base class used by |
| 11 | /// scheduling implementation classes. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/iterator_range.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/ScheduleDAG.h" |
Dan Gohman | 7e105f0 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Andrew Trick | 3013b6a | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 8ef0735 | 2009-07-24 09:53:24 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetSubtargetInfo.h" |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <iterator> |
| 32 | #include <limits> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "pre-RA-sched" |
| 39 | |
Andrew Trick | 3013b6a | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 40 | #ifndef NDEBUG |
Benjamin Kramer | 4938edb | 2011-08-19 01:42:18 +0000 | [diff] [blame] | 41 | static cl::opt<bool> StressSchedOpt( |
Andrew Trick | 3013b6a | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 42 | "stress-sched", cl::Hidden, cl::init(false), |
| 43 | cl::desc("Stress test instruction scheduling")); |
| 44 | #endif |
| 45 | |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 46 | void SchedulingPriorityQueue::anchor() {} |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 47 | |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 48 | ScheduleDAG::ScheduleDAG(MachineFunction &mf) |
Eric Christopher | 3372620 | 2015-01-27 08:48:42 +0000 | [diff] [blame] | 49 | : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()), |
| 50 | TRI(mf.getSubtarget().getRegisterInfo()), MF(mf), |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 51 | MRI(mf.getRegInfo()) { |
Andrew Trick | 3013b6a | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 52 | #ifndef NDEBUG |
| 53 | StressSched = StressSchedOpt; |
| 54 | #endif |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 57 | ScheduleDAG::~ScheduleDAG() = default; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 58 | |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 59 | void ScheduleDAG::clearDAG() { |
| 60 | SUnits.clear(); |
| 61 | EntrySU = SUnit(); |
| 62 | ExitSU = SUnit(); |
| 63 | } |
| 64 | |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 65 | const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 66 | if (!Node || !Node->isMachineOpcode()) return nullptr; |
Andrew Trick | 10ffc2b | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 67 | return &TII->get(Node->getMachineOpcode()); |
| 68 | } |
| 69 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 70 | bool SUnit::addPred(const SDep &D, bool Required) { |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 71 | // If this node already has this dependence, don't add a redundant one. |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 72 | for (SDep &PredDep : Preds) { |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 73 | // Zero-latency weak edges may be added purely for heuristic ordering. Don't |
| 74 | // add them if another kind of edge already exists. |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 75 | if (!Required && PredDep.getSUnit() == D.getSUnit()) |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 76 | return false; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 77 | if (PredDep.overlaps(D)) { |
| 78 | // Extend the latency if needed. Equivalent to |
| 79 | // removePred(PredDep) + addPred(D). |
| 80 | if (PredDep.getLatency() < D.getLatency()) { |
| 81 | SUnit *PredSU = PredDep.getSUnit(); |
Andrew Trick | 5b90645 | 2012-06-13 02:39:00 +0000 | [diff] [blame] | 82 | // Find the corresponding successor in N. |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 83 | SDep ForwardD = PredDep; |
Andrew Trick | 5b90645 | 2012-06-13 02:39:00 +0000 | [diff] [blame] | 84 | ForwardD.setSUnit(this); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 85 | for (SDep &SuccDep : PredSU->Succs) { |
| 86 | if (SuccDep == ForwardD) { |
| 87 | SuccDep.setLatency(D.getLatency()); |
Andrew Trick | 5b90645 | 2012-06-13 02:39:00 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | } |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 91 | PredDep.setLatency(D.getLatency()); |
Andrew Trick | 5b90645 | 2012-06-13 02:39:00 +0000 | [diff] [blame] | 92 | } |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 93 | return false; |
Andrew Trick | 5b90645 | 2012-06-13 02:39:00 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 96 | // Now add a corresponding succ to N. |
| 97 | SDep P = D; |
| 98 | P.setSUnit(this); |
| 99 | SUnit *N = D.getSUnit(); |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 100 | // Update the bookkeeping. |
| 101 | if (D.getKind() == SDep::Data) { |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 102 | assert(NumPreds < std::numeric_limits<unsigned>::max() && |
| 103 | "NumPreds will overflow!"); |
| 104 | assert(N->NumSuccs < std::numeric_limits<unsigned>::max() && |
| 105 | "NumSuccs will overflow!"); |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 106 | ++NumPreds; |
| 107 | ++N->NumSuccs; |
| 108 | } |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 109 | if (!N->isScheduled) { |
Andrew Trick | 4b1f9e3 | 2012-11-13 02:35:06 +0000 | [diff] [blame] | 110 | if (D.isWeak()) { |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 111 | ++WeakPredsLeft; |
| 112 | } |
| 113 | else { |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 114 | assert(NumPredsLeft < std::numeric_limits<unsigned>::max() && |
| 115 | "NumPredsLeft will overflow!"); |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 116 | ++NumPredsLeft; |
| 117 | } |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 118 | } |
| 119 | if (!isScheduled) { |
Andrew Trick | 4b1f9e3 | 2012-11-13 02:35:06 +0000 | [diff] [blame] | 120 | if (D.isWeak()) { |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 121 | ++N->WeakSuccsLeft; |
| 122 | } |
| 123 | else { |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 124 | assert(N->NumSuccsLeft < std::numeric_limits<unsigned>::max() && |
| 125 | "NumSuccsLeft will overflow!"); |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 126 | ++N->NumSuccsLeft; |
| 127 | } |
Reid Kleckner | 8ff5c19 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 128 | } |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 129 | Preds.push_back(D); |
Dan Gohman | 13141d5 | 2009-01-13 19:08:45 +0000 | [diff] [blame] | 130 | N->Succs.push_back(P); |
Dan Gohman | 52d4d82 | 2009-01-05 22:40:26 +0000 | [diff] [blame] | 131 | if (P.getLatency() != 0) { |
| 132 | this->setDepthDirty(); |
| 133 | N->setHeightDirty(); |
| 134 | } |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 135 | return true; |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 138 | void SUnit::removePred(const SDep &D) { |
| 139 | // Find the matching predecessor. |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 140 | SmallVectorImpl<SDep>::iterator I = llvm::find(Preds, D); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 141 | if (I == Preds.end()) |
| 142 | return; |
| 143 | // Find the corresponding successor in N. |
| 144 | SDep P = D; |
| 145 | P.setSUnit(this); |
| 146 | SUnit *N = D.getSUnit(); |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 147 | SmallVectorImpl<SDep>::iterator Succ = llvm::find(N->Succs, P); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 148 | assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!"); |
| 149 | N->Succs.erase(Succ); |
| 150 | Preds.erase(I); |
| 151 | // Update the bookkeeping. |
| 152 | if (P.getKind() == SDep::Data) { |
| 153 | assert(NumPreds > 0 && "NumPreds will underflow!"); |
| 154 | assert(N->NumSuccs > 0 && "NumSuccs will underflow!"); |
| 155 | --NumPreds; |
| 156 | --N->NumSuccs; |
| 157 | } |
| 158 | if (!N->isScheduled) { |
| 159 | if (D.isWeak()) |
| 160 | --WeakPredsLeft; |
| 161 | else { |
| 162 | assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!"); |
| 163 | --NumPredsLeft; |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 164 | } |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 165 | } |
| 166 | if (!isScheduled) { |
| 167 | if (D.isWeak()) |
| 168 | --N->WeakSuccsLeft; |
| 169 | else { |
| 170 | assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!"); |
| 171 | --N->NumSuccsLeft; |
| 172 | } |
| 173 | } |
| 174 | if (P.getLatency() != 0) { |
| 175 | this->setDepthDirty(); |
| 176 | N->setHeightDirty(); |
| 177 | } |
Dan Gohman | e3a6351 | 2008-12-16 01:05:52 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 180 | void SUnit::setDepthDirty() { |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 181 | if (!isDepthCurrent) return; |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 182 | SmallVector<SUnit*, 8> WorkList; |
| 183 | WorkList.push_back(this); |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 184 | do { |
Dan Gohman | 24fe9a1 | 2008-12-20 16:42:33 +0000 | [diff] [blame] | 185 | SUnit *SU = WorkList.pop_back_val(); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 186 | SU->isDepthCurrent = false; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 187 | for (SDep &SuccDep : SU->Succs) { |
| 188 | SUnit *SuccSU = SuccDep.getSUnit(); |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 189 | if (SuccSU->isDepthCurrent) |
| 190 | WorkList.push_back(SuccSU); |
| 191 | } |
| 192 | } while (!WorkList.empty()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void SUnit::setHeightDirty() { |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 196 | if (!isHeightCurrent) return; |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 197 | SmallVector<SUnit*, 8> WorkList; |
| 198 | WorkList.push_back(this); |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 199 | do { |
Dan Gohman | 24fe9a1 | 2008-12-20 16:42:33 +0000 | [diff] [blame] | 200 | SUnit *SU = WorkList.pop_back_val(); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 201 | SU->isHeightCurrent = false; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 202 | for (SDep &PredDep : SU->Preds) { |
| 203 | SUnit *PredSU = PredDep.getSUnit(); |
Dan Gohman | a04542b | 2008-12-22 21:11:33 +0000 | [diff] [blame] | 204 | if (PredSU->isHeightCurrent) |
| 205 | WorkList.push_back(PredSU); |
| 206 | } |
| 207 | } while (!WorkList.empty()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 210 | void SUnit::setDepthToAtLeast(unsigned NewDepth) { |
| 211 | if (NewDepth <= getDepth()) |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 212 | return; |
| 213 | setDepthDirty(); |
| 214 | Depth = NewDepth; |
| 215 | isDepthCurrent = true; |
| 216 | } |
| 217 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 218 | void SUnit::setHeightToAtLeast(unsigned NewHeight) { |
| 219 | if (NewHeight <= getHeight()) |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 220 | return; |
| 221 | setHeightDirty(); |
| 222 | Height = NewHeight; |
| 223 | isHeightCurrent = true; |
| 224 | } |
| 225 | |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 226 | /// Calculates the maximal path from the node to the exit. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 227 | void SUnit::ComputeDepth() { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 228 | SmallVector<SUnit*, 8> WorkList; |
| 229 | WorkList.push_back(this); |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 230 | do { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 231 | SUnit *Cur = WorkList.back(); |
| 232 | |
| 233 | bool Done = true; |
| 234 | unsigned MaxPredDepth = 0; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 235 | for (const SDep &PredDep : Cur->Preds) { |
| 236 | SUnit *PredSU = PredDep.getSUnit(); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 237 | if (PredSU->isDepthCurrent) |
| 238 | MaxPredDepth = std::max(MaxPredDepth, |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 239 | PredSU->Depth + PredDep.getLatency()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 240 | else { |
| 241 | Done = false; |
| 242 | WorkList.push_back(PredSU); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (Done) { |
| 247 | WorkList.pop_back(); |
| 248 | if (MaxPredDepth != Cur->Depth) { |
| 249 | Cur->setDepthDirty(); |
| 250 | Cur->Depth = MaxPredDepth; |
| 251 | } |
| 252 | Cur->isDepthCurrent = true; |
| 253 | } |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 254 | } while (!WorkList.empty()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 257 | /// Calculates the maximal path from the node to the entry. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 258 | void SUnit::ComputeHeight() { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 259 | SmallVector<SUnit*, 8> WorkList; |
| 260 | WorkList.push_back(this); |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 261 | do { |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 262 | SUnit *Cur = WorkList.back(); |
| 263 | |
| 264 | bool Done = true; |
| 265 | unsigned MaxSuccHeight = 0; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 266 | for (const SDep &SuccDep : Cur->Succs) { |
| 267 | SUnit *SuccSU = SuccDep.getSUnit(); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 268 | if (SuccSU->isHeightCurrent) |
| 269 | MaxSuccHeight = std::max(MaxSuccHeight, |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 270 | SuccSU->Height + SuccDep.getLatency()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 271 | else { |
| 272 | Done = false; |
| 273 | WorkList.push_back(SuccSU); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (Done) { |
| 278 | WorkList.pop_back(); |
| 279 | if (MaxSuccHeight != Cur->Height) { |
| 280 | Cur->setHeightDirty(); |
| 281 | Cur->Height = MaxSuccHeight; |
| 282 | } |
| 283 | Cur->isHeightCurrent = true; |
| 284 | } |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 285 | } while (!WorkList.empty()); |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Andrew Trick | d3b8629 | 2013-01-24 02:09:55 +0000 | [diff] [blame] | 288 | void SUnit::biasCriticalPath() { |
| 289 | if (NumPreds < 2) |
| 290 | return; |
| 291 | |
| 292 | SUnit::pred_iterator BestI = Preds.begin(); |
| 293 | unsigned MaxDepth = BestI->getSUnit()->getDepth(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 294 | for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E; |
| 295 | ++I) { |
Andrew Trick | d3b8629 | 2013-01-24 02:09:55 +0000 | [diff] [blame] | 296 | if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth) |
| 297 | BestI = I; |
| 298 | } |
| 299 | if (BestI != Preds.begin()) |
| 300 | std::swap(*Preds.begin(), *BestI); |
| 301 | } |
| 302 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 303 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 304 | LLVM_DUMP_METHOD |
Evandro Menezes | 063259d | 2017-01-10 01:08:01 +0000 | [diff] [blame] | 305 | void SUnit::print(raw_ostream &OS, const ScheduleDAG *DAG) const { |
| 306 | if (this == &DAG->ExitSU) |
| 307 | OS << "ExitSU"; |
| 308 | else if (this == &DAG->EntrySU) |
| 309 | OS << "EntrySU"; |
Matthias Braun | 636a597 | 2016-11-11 22:37:26 +0000 | [diff] [blame] | 310 | else |
Evandro Menezes | 063259d | 2017-01-10 01:08:01 +0000 | [diff] [blame] | 311 | OS << "SU(" << NodeNum << ")"; |
Matthias Braun | 636a597 | 2016-11-11 22:37:26 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 314 | LLVM_DUMP_METHOD void SUnit::dump(const ScheduleDAG *G) const { |
Evandro Menezes | 063259d | 2017-01-10 01:08:01 +0000 | [diff] [blame] | 315 | print(dbgs(), G); |
Matthias Braun | 636a597 | 2016-11-11 22:37:26 +0000 | [diff] [blame] | 316 | dbgs() << ": "; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 317 | G->dumpNode(this); |
| 318 | } |
| 319 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 320 | LLVM_DUMP_METHOD void SUnit::dumpAll(const ScheduleDAG *G) const { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 321 | dump(G); |
| 322 | |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 323 | dbgs() << " # preds left : " << NumPredsLeft << "\n"; |
| 324 | dbgs() << " # succs left : " << NumSuccsLeft << "\n"; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 325 | if (WeakPredsLeft) |
| 326 | dbgs() << " # weak preds left : " << WeakPredsLeft << "\n"; |
| 327 | if (WeakSuccsLeft) |
| 328 | dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n"; |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 329 | dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n"; |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 330 | dbgs() << " Latency : " << Latency << "\n"; |
Andrew Trick | 2a8edef | 2013-03-01 00:19:09 +0000 | [diff] [blame] | 331 | dbgs() << " Depth : " << getDepth() << "\n"; |
| 332 | dbgs() << " Height : " << getHeight() << "\n"; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 333 | |
| 334 | if (Preds.size() != 0) { |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 335 | dbgs() << " Predecessors:\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 336 | for (const SDep &SuccDep : Preds) { |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 337 | dbgs() << " "; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 338 | switch (SuccDep.getKind()) { |
Matthias Braun | 1acb55e | 2016-09-23 18:28:31 +0000 | [diff] [blame] | 339 | case SDep::Data: dbgs() << "data "; break; |
| 340 | case SDep::Anti: dbgs() << "anti "; break; |
| 341 | case SDep::Output: dbgs() << "out "; break; |
| 342 | case SDep::Order: dbgs() << "ord "; break; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 343 | } |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 344 | SuccDep.getSUnit()->print(dbgs(), G); |
| 345 | if (SuccDep.isArtificial()) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 346 | dbgs() << " *"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 347 | dbgs() << ": Latency=" << SuccDep.getLatency(); |
| 348 | if (SuccDep.isAssignedRegDep()) |
| 349 | dbgs() << " Reg=" << PrintReg(SuccDep.getReg(), G->TRI); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 350 | dbgs() << "\n"; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | if (Succs.size() != 0) { |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 354 | dbgs() << " Successors:\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 355 | for (const SDep &SuccDep : Succs) { |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 356 | dbgs() << " "; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 357 | switch (SuccDep.getKind()) { |
Matthias Braun | 1acb55e | 2016-09-23 18:28:31 +0000 | [diff] [blame] | 358 | case SDep::Data: dbgs() << "data "; break; |
| 359 | case SDep::Anti: dbgs() << "anti "; break; |
| 360 | case SDep::Output: dbgs() << "out "; break; |
| 361 | case SDep::Order: dbgs() << "ord "; break; |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 362 | } |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 363 | SuccDep.getSUnit()->print(dbgs(), G); |
| 364 | if (SuccDep.isArtificial()) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 365 | dbgs() << " *"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 366 | dbgs() << ": Latency=" << SuccDep.getLatency(); |
| 367 | if (SuccDep.isAssignedRegDep()) |
| 368 | dbgs() << " Reg=" << PrintReg(SuccDep.getReg(), G->TRI); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 369 | dbgs() << "\n"; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 372 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 373 | #endif |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 374 | |
| 375 | #ifndef NDEBUG |
Andrew Trick | 46a5866 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 376 | unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) { |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 377 | bool AnyNotSched = false; |
| 378 | unsigned DeadNodes = 0; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 379 | for (const SUnit &SUnit : SUnits) { |
| 380 | if (!SUnit.isScheduled) { |
| 381 | if (SUnit.NumPreds == 0 && SUnit.NumSuccs == 0) { |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 382 | ++DeadNodes; |
| 383 | continue; |
| 384 | } |
| 385 | if (!AnyNotSched) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 386 | dbgs() << "*** Scheduling failed! ***\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 387 | SUnit.dump(this); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 388 | dbgs() << "has not been scheduled!\n"; |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 389 | AnyNotSched = true; |
| 390 | } |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 391 | if (SUnit.isScheduled && |
| 392 | (isBottomUp ? SUnit.getHeight() : SUnit.getDepth()) > |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 393 | unsigned(std::numeric_limits<int>::max())) { |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 394 | if (!AnyNotSched) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 395 | dbgs() << "*** Scheduling failed! ***\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 396 | SUnit.dump(this); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 397 | dbgs() << "has an unexpected " |
Dan Gohman | dddc1ac | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 398 | << (isBottomUp ? "Height" : "Depth") << " value!\n"; |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 399 | AnyNotSched = true; |
| 400 | } |
| 401 | if (isBottomUp) { |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 402 | if (SUnit.NumSuccsLeft != 0) { |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 403 | if (!AnyNotSched) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 404 | dbgs() << "*** Scheduling failed! ***\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 405 | SUnit.dump(this); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 406 | dbgs() << "has successors left!\n"; |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 407 | AnyNotSched = true; |
| 408 | } |
| 409 | } else { |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 410 | if (SUnit.NumPredsLeft != 0) { |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 411 | if (!AnyNotSched) |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 412 | dbgs() << "*** Scheduling failed! ***\n"; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 413 | SUnit.dump(this); |
David Greene | 94745d0 | 2010-01-05 01:25:41 +0000 | [diff] [blame] | 414 | dbgs() << "has predecessors left!\n"; |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 415 | AnyNotSched = true; |
| 416 | } |
| 417 | } |
| 418 | } |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 419 | assert(!AnyNotSched); |
Andrew Trick | 46a5866 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 420 | return SUnits.size() - DeadNodes; |
Dan Gohman | 4ce15e1 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 421 | } |
| 422 | #endif |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 423 | |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 424 | void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() { |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 425 | // The idea of the algorithm is taken from |
| 426 | // "Online algorithms for managing the topological order of |
| 427 | // a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly |
| 428 | // This is the MNR algorithm, which was first introduced by |
| 429 | // A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in |
| 430 | // "Maintaining a topological order under edge insertions". |
| 431 | // |
| 432 | // Short description of the algorithm: |
| 433 | // |
| 434 | // Topological ordering, ord, of a DAG maps each node to a topological |
| 435 | // index so that for all edges X->Y it is the case that ord(X) < ord(Y). |
| 436 | // |
| 437 | // This means that if there is a path from the node X to the node Z, |
| 438 | // then ord(X) < ord(Z). |
| 439 | // |
| 440 | // This property can be used to check for reachability of nodes: |
| 441 | // if Z is reachable from X, then an insertion of the edge Z->X would |
| 442 | // create a cycle. |
| 443 | // |
| 444 | // The algorithm first computes a topological ordering for the DAG by |
| 445 | // initializing the Index2Node and Node2Index arrays and then tries to keep |
| 446 | // the ordering up-to-date after edge insertions by reordering the DAG. |
| 447 | // |
| 448 | // On insertion of the edge X->Y, the algorithm first marks by calling DFS |
| 449 | // the nodes reachable from Y, and then shifts them using Shift to lie |
| 450 | // immediately after X in Index2Node. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 451 | unsigned DAGSize = SUnits.size(); |
| 452 | std::vector<SUnit*> WorkList; |
| 453 | WorkList.reserve(DAGSize); |
| 454 | |
| 455 | Index2Node.resize(DAGSize); |
| 456 | Node2Index.resize(DAGSize); |
| 457 | |
| 458 | // Initialize the data structures. |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 459 | if (ExitSU) |
| 460 | WorkList.push_back(ExitSU); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 461 | for (SUnit &SU : SUnits) { |
| 462 | int NodeNum = SU.NodeNum; |
| 463 | unsigned Degree = SU.Succs.size(); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 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) { |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 469 | assert(SU.Succs.empty() && "SUnit should have no successors"); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 470 | // Collect leaf nodes. |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 471 | WorkList.push_back(&SU); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 472 | } |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 473 | } |
Dan Gohman | ad2134d | 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 | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 479 | if (SU->NodeNum < DAGSize) |
| 480 | Allocate(SU->NodeNum, --Id); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 481 | for (const SDep &PredDep : SU->Preds) { |
| 482 | SUnit *SU = PredDep.getSUnit(); |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 483 | if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum]) |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 484 | // If all dependencies of the node are processed already, |
| 485 | // then the node can be computed now. |
| 486 | WorkList.push_back(SU); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | Visited.resize(DAGSize); |
| 491 | |
| 492 | #ifndef NDEBUG |
| 493 | // Check correctness of the ordering |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 494 | for (SUnit &SU : SUnits) { |
| 495 | for (const SDep &PD : SU.Preds) { |
| 496 | assert(Node2Index[SU.NodeNum] > Node2Index[PD.getSUnit()->NodeNum] && |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 497 | "Wrong topological sorting"); |
| 498 | } |
| 499 | } |
| 500 | #endif |
| 501 | } |
| 502 | |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 503 | void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) { |
| 504 | int UpperBound, LowerBound; |
| 505 | LowerBound = Node2Index[Y->NodeNum]; |
| 506 | UpperBound = Node2Index[X->NodeNum]; |
| 507 | bool HasLoop = false; |
| 508 | // Is Ord(X) < Ord(Y) ? |
| 509 | if (LowerBound < UpperBound) { |
| 510 | // Update the topological order. |
| 511 | Visited.reset(); |
| 512 | DFS(Y, UpperBound, HasLoop); |
| 513 | assert(!HasLoop && "Inserted edge creates a loop!"); |
| 514 | // Recompute topological indexes. |
| 515 | Shift(Visited, LowerBound, UpperBound); |
| 516 | } |
| 517 | } |
| 518 | |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 519 | void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) { |
| 520 | // InitDAGTopologicalSorting(); |
| 521 | } |
| 522 | |
Dan Gohman | 7d32974 | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 523 | void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound, |
Chris Lattner | ed69c6e | 2010-12-20 00:50:16 +0000 | [diff] [blame] | 524 | bool &HasLoop) { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 525 | std::vector<const SUnit*> WorkList; |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 526 | WorkList.reserve(SUnits.size()); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 527 | |
| 528 | WorkList.push_back(SU); |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 529 | do { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 530 | SU = WorkList.back(); |
| 531 | WorkList.pop_back(); |
| 532 | Visited.set(SU->NodeNum); |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 533 | for (const SDep &SuccDep |
| 534 | : make_range(SU->Succs.rbegin(), SU->Succs.rend())) { |
| 535 | unsigned s = SuccDep.getSUnit()->NodeNum; |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 536 | // Edges to non-SUnits are allowed but ignored (e.g. ExitSU). |
| 537 | if (s >= Node2Index.size()) |
| 538 | continue; |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 539 | if (Node2Index[s] == UpperBound) { |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 540 | HasLoop = true; |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | // Visit successors if not already and in affected region. |
| 544 | if (!Visited.test(s) && Node2Index[s] < UpperBound) { |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 545 | WorkList.push_back(SuccDep.getSUnit()); |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
Dan Gohman | 3a57213 | 2008-12-23 17:22:32 +0000 | [diff] [blame] | 548 | } while (!WorkList.empty()); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Valery Pykhtin | 910da13 | 2017-03-28 05:12:31 +0000 | [diff] [blame] | 551 | std::vector<int> ScheduleDAGTopologicalSort::GetSubGraph(const SUnit &StartSU, |
| 552 | const SUnit &TargetSU, |
| 553 | bool &Success) { |
| 554 | std::vector<const SUnit*> WorkList; |
| 555 | int LowerBound = Node2Index[StartSU.NodeNum]; |
| 556 | int UpperBound = Node2Index[TargetSU.NodeNum]; |
| 557 | bool Found = false; |
| 558 | BitVector VisitedBack; |
| 559 | std::vector<int> Nodes; |
| 560 | |
| 561 | if (LowerBound > UpperBound) { |
| 562 | Success = false; |
| 563 | return Nodes; |
| 564 | } |
| 565 | |
| 566 | WorkList.reserve(SUnits.size()); |
| 567 | Visited.reset(); |
| 568 | |
| 569 | // Starting from StartSU, visit all successors up |
| 570 | // to UpperBound. |
| 571 | WorkList.push_back(&StartSU); |
| 572 | do { |
| 573 | const SUnit *SU = WorkList.back(); |
| 574 | WorkList.pop_back(); |
| 575 | for (int I = SU->Succs.size()-1; I >= 0; --I) { |
| 576 | const SUnit *Succ = SU->Succs[I].getSUnit(); |
| 577 | unsigned s = Succ->NodeNum; |
| 578 | // Edges to non-SUnits are allowed but ignored (e.g. ExitSU). |
| 579 | if (Succ->isBoundaryNode()) |
| 580 | continue; |
| 581 | if (Node2Index[s] == UpperBound) { |
| 582 | Found = true; |
| 583 | continue; |
| 584 | } |
| 585 | // Visit successors if not already and in affected region. |
| 586 | if (!Visited.test(s) && Node2Index[s] < UpperBound) { |
| 587 | Visited.set(s); |
| 588 | WorkList.push_back(Succ); |
| 589 | } |
| 590 | } |
| 591 | } while (!WorkList.empty()); |
| 592 | |
| 593 | if (!Found) { |
| 594 | Success = false; |
| 595 | return Nodes; |
| 596 | } |
| 597 | |
| 598 | WorkList.clear(); |
| 599 | VisitedBack.resize(SUnits.size()); |
| 600 | Found = false; |
| 601 | |
| 602 | // Starting from TargetSU, visit all predecessors up |
| 603 | // to LowerBound. SUs that are visited by the two |
| 604 | // passes are added to Nodes. |
| 605 | WorkList.push_back(&TargetSU); |
| 606 | do { |
| 607 | const SUnit *SU = WorkList.back(); |
| 608 | WorkList.pop_back(); |
| 609 | for (int I = SU->Preds.size()-1; I >= 0; --I) { |
| 610 | const SUnit *Pred = SU->Preds[I].getSUnit(); |
| 611 | unsigned s = Pred->NodeNum; |
| 612 | // Edges to non-SUnits are allowed but ignored (e.g. EntrySU). |
| 613 | if (Pred->isBoundaryNode()) |
| 614 | continue; |
| 615 | if (Node2Index[s] == LowerBound) { |
| 616 | Found = true; |
| 617 | continue; |
| 618 | } |
| 619 | if (!VisitedBack.test(s) && Visited.test(s)) { |
| 620 | VisitedBack.set(s); |
| 621 | WorkList.push_back(Pred); |
| 622 | Nodes.push_back(s); |
| 623 | } |
| 624 | } |
| 625 | } while (!WorkList.empty()); |
| 626 | |
| 627 | assert(Found && "Error in SUnit Graph!"); |
| 628 | Success = true; |
| 629 | return Nodes; |
| 630 | } |
| 631 | |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 632 | void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound, |
Dan Gohman | 7d32974 | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 633 | int UpperBound) { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 634 | std::vector<int> L; |
| 635 | int shift = 0; |
| 636 | int i; |
| 637 | |
| 638 | for (i = LowerBound; i <= UpperBound; ++i) { |
| 639 | // w is node at topological index i. |
| 640 | int w = Index2Node[i]; |
| 641 | if (Visited.test(w)) { |
| 642 | // Unmark. |
| 643 | Visited.reset(w); |
| 644 | L.push_back(w); |
| 645 | shift = shift + 1; |
| 646 | } else { |
| 647 | Allocate(w, i - shift); |
| 648 | } |
| 649 | } |
| 650 | |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 651 | for (unsigned LI : L) { |
| 652 | Allocate(LI, i - shift); |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 653 | i = i + 1; |
| 654 | } |
| 655 | } |
| 656 | |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 657 | bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) { |
| 658 | // Is SU reachable from TargetSU via successor edges? |
| 659 | if (IsReachable(SU, TargetSU)) |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 660 | return true; |
Matthias Braun | 9ab4039 | 2017-02-21 01:27:33 +0000 | [diff] [blame] | 661 | for (const SDep &PredDep : TargetSU->Preds) |
| 662 | if (PredDep.isAssignedRegDep() && |
| 663 | IsReachable(SU, PredDep.getSUnit())) |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 664 | return true; |
| 665 | return false; |
| 666 | } |
| 667 | |
Dan Gohman | 7d32974 | 2008-12-09 16:37:48 +0000 | [diff] [blame] | 668 | bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU, |
| 669 | const SUnit *TargetSU) { |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 670 | // If insertion of the edge SU->TargetSU would create a cycle |
| 671 | // then there is a path from TargetSU to SU. |
| 672 | int UpperBound, LowerBound; |
| 673 | LowerBound = Node2Index[TargetSU->NodeNum]; |
| 674 | UpperBound = Node2Index[SU->NodeNum]; |
| 675 | bool HasLoop = false; |
| 676 | // Is Ord(TargetSU) < Ord(SU) ? |
| 677 | if (LowerBound < UpperBound) { |
| 678 | Visited.reset(); |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 679 | // There may be a path from TargetSU to SU. Check for it. |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 680 | DFS(TargetSU, UpperBound, HasLoop); |
| 681 | } |
| 682 | return HasLoop; |
| 683 | } |
| 684 | |
Dan Gohman | ad2134d | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 685 | void ScheduleDAGTopologicalSort::Allocate(int n, int index) { |
| 686 | Node2Index[n] = index; |
| 687 | Index2Node[index] = n; |
| 688 | } |
| 689 | |
John Mosby | 5364655 | 2010-06-30 03:40:54 +0000 | [diff] [blame] | 690 | ScheduleDAGTopologicalSort:: |
Andrew Trick | f1ff84c | 2012-11-12 19:28:57 +0000 | [diff] [blame] | 691 | ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu) |
| 692 | : SUnits(sunits), ExitSU(exitsu) {} |
Dan Gohman | 7e105f0 | 2009-01-15 22:18:12 +0000 | [diff] [blame] | 693 | |
Eugene Zelenko | db56e5a | 2017-02-22 22:32:51 +0000 | [diff] [blame] | 694 | ScheduleHazardRecognizer::~ScheduleHazardRecognizer() = default; |