Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 1 | //===----- ScheduleDAGFast.cpp - Fast poor list scheduler -----------------===// |
| 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 a fast scheduler. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "pre-RA-sched" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/ScheduleDAGSDNodes.h" |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/SchedulerRegistry.h" |
| 17 | #include "llvm/Target/TargetRegisterInfo.h" |
| 18 | #include "llvm/Target/TargetData.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Target/TargetInstrInfo.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/ADT/SmallSet.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumUnfolds, "Number of nodes unfolded"); |
| 30 | STATISTIC(NumDups, "Number of duplicated nodes"); |
| 31 | STATISTIC(NumCCCopies, "Number of cross class copies"); |
| 32 | |
| 33 | static RegisterScheduler |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 34 | fastDAGScheduler("fast", "Fast suboptimal list scheduling", |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 35 | createFastDAGScheduler); |
| 36 | |
| 37 | namespace { |
| 38 | /// FastPriorityQueue - A degenerate priority queue that considers |
| 39 | /// all nodes to have the same priority. |
| 40 | /// |
| 41 | struct VISIBILITY_HIDDEN FastPriorityQueue { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 42 | SmallVector<SUnit *, 16> Queue; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 43 | |
| 44 | bool empty() const { return Queue.empty(); } |
| 45 | |
| 46 | void push(SUnit *U) { |
| 47 | Queue.push_back(U); |
| 48 | } |
| 49 | |
| 50 | SUnit *pop() { |
| 51 | if (empty()) return NULL; |
| 52 | SUnit *V = Queue.back(); |
| 53 | Queue.pop_back(); |
| 54 | return V; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | /// ScheduleDAGFast - The actual "fast" list scheduler implementation. |
| 60 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 61 | class VISIBILITY_HIDDEN ScheduleDAGFast : public ScheduleDAGSDNodes { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 62 | private: |
| 63 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 64 | FastPriorityQueue AvailableQueue; |
| 65 | |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 66 | /// LiveRegDefs - A set of physical registers and their definition |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 67 | /// that are "live". These nodes must be scheduled before any other nodes that |
| 68 | /// modifies the registers can be scheduled. |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 69 | unsigned NumLiveRegs; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 70 | std::vector<SUnit*> LiveRegDefs; |
| 71 | std::vector<unsigned> LiveRegCycles; |
| 72 | |
| 73 | public: |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 74 | ScheduleDAGFast(SelectionDAG *dag, MachineBasicBlock *bb, |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 75 | const TargetMachine &tm) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 76 | : ScheduleDAGSDNodes(dag, bb, tm) {} |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 77 | |
| 78 | void Schedule(); |
| 79 | |
| 80 | /// AddPred - This adds the specified node X as a predecessor of |
| 81 | /// the current node Y if not already. |
| 82 | /// This returns true if this is a new predecessor. |
| 83 | bool AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial, |
| 84 | unsigned PhyReg = 0, int Cost = 1); |
| 85 | |
| 86 | /// RemovePred - This removes the specified node N from the predecessors of |
| 87 | /// the current node M. |
| 88 | bool RemovePred(SUnit *M, SUnit *N, bool isCtrl, bool isSpecial); |
| 89 | |
| 90 | private: |
Dan Gohman | 2d093f3 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 91 | void ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 92 | void ScheduleNodeBottomUp(SUnit*, unsigned); |
| 93 | SUnit *CopyAndMoveSuccessors(SUnit*); |
| 94 | void InsertCCCopiesAndMoveSuccs(SUnit*, unsigned, |
| 95 | const TargetRegisterClass*, |
| 96 | const TargetRegisterClass*, |
| 97 | SmallVector<SUnit*, 2>&); |
| 98 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVector<unsigned, 4>&); |
| 99 | void ListScheduleBottomUp(); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 100 | }; |
| 101 | } // end anonymous namespace |
| 102 | |
| 103 | |
| 104 | /// Schedule - Schedule the DAG using list scheduling. |
| 105 | void ScheduleDAGFast::Schedule() { |
| 106 | DOUT << "********** List Scheduling **********\n"; |
| 107 | |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 108 | NumLiveRegs = 0; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 109 | LiveRegDefs.resize(TRI->getNumRegs(), NULL); |
| 110 | LiveRegCycles.resize(TRI->getNumRegs(), 0); |
| 111 | |
| 112 | // Build scheduling units. |
| 113 | BuildSchedUnits(); |
| 114 | |
| 115 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 116 | SUnits[su].dumpAll(this)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 117 | |
| 118 | // Execute the actual scheduling loop. |
| 119 | ListScheduleBottomUp(); |
| 120 | } |
| 121 | |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | // Bottom-Up Scheduling |
| 124 | //===----------------------------------------------------------------------===// |
| 125 | |
| 126 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 127 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 2d093f3 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 128 | void ScheduleDAGFast::ReleasePred(SUnit *SU, SUnit *PredSU, bool isChain) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 129 | --PredSU->NumSuccsLeft; |
| 130 | |
| 131 | #ifndef NDEBUG |
| 132 | if (PredSU->NumSuccsLeft < 0) { |
Dan Gohman | 2d093f3 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 133 | cerr << "*** Scheduling failed! ***\n"; |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 134 | PredSU->dump(this); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 135 | cerr << " has been released too many times!\n"; |
| 136 | assert(0); |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | if (PredSU->NumSuccsLeft == 0) { |
| 141 | PredSU->isAvailable = true; |
| 142 | AvailableQueue.push(PredSU); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 147 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 148 | /// the Available queue. |
| 149 | void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { |
| 150 | DOUT << "*** Scheduling [" << CurCycle << "]: "; |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 151 | DEBUG(SU->dump(this)); |
Dan Gohman | 1256f5f | 2008-11-18 21:22:20 +0000 | [diff] [blame] | 152 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 153 | SU->Cycle = CurCycle; |
Dan Gohman | 1256f5f | 2008-11-18 21:22:20 +0000 | [diff] [blame] | 154 | Sequence.push_back(SU); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 155 | |
| 156 | // Bottom up: release predecessors |
| 157 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 158 | I != E; ++I) { |
Dan Gohman | 2d093f3 | 2008-11-18 00:38:59 +0000 | [diff] [blame] | 159 | ReleasePred(SU, I->Dep, I->isCtrl); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 160 | if (I->Cost < 0) { |
| 161 | // This is a physical register dependency and it's impossible or |
| 162 | // expensive to copy the register. Make sure nothing that can |
| 163 | // clobber the register is scheduled between the predecessor and |
| 164 | // this node. |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 165 | if (!LiveRegDefs[I->Reg]) { |
| 166 | ++NumLiveRegs; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 167 | LiveRegDefs[I->Reg] = I->Dep; |
| 168 | LiveRegCycles[I->Reg] = CurCycle; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Release all the implicit physical register defs that are live. |
| 174 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 175 | I != E; ++I) { |
| 176 | if (I->Cost < 0) { |
| 177 | if (LiveRegCycles[I->Reg] == I->Dep->Cycle) { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 178 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 179 | assert(LiveRegDefs[I->Reg] == SU && |
| 180 | "Physical register dependency violated?"); |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 181 | --NumLiveRegs; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 182 | LiveRegDefs[I->Reg] = NULL; |
| 183 | LiveRegCycles[I->Reg] = 0; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | SU->isScheduled = true; |
| 189 | } |
| 190 | |
| 191 | /// AddPred - adds an edge from SUnit X to SUnit Y. |
| 192 | bool ScheduleDAGFast::AddPred(SUnit *Y, SUnit *X, bool isCtrl, bool isSpecial, |
| 193 | unsigned PhyReg, int Cost) { |
| 194 | return Y->addPred(X, isCtrl, isSpecial, PhyReg, Cost); |
| 195 | } |
| 196 | |
| 197 | /// RemovePred - This removes the specified node N from the predecessors of |
| 198 | /// the current node M. |
| 199 | bool ScheduleDAGFast::RemovePred(SUnit *M, SUnit *N, |
| 200 | bool isCtrl, bool isSpecial) { |
| 201 | return M->removePred(N, isCtrl, isSpecial); |
| 202 | } |
| 203 | |
| 204 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled |
| 205 | /// successors to the newly created node. |
| 206 | SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) { |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 207 | if (SU->getNode()->getFlaggedNode()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 208 | return NULL; |
| 209 | |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 210 | SDNode *N = SU->getNode(); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 211 | if (!N) |
| 212 | return NULL; |
| 213 | |
| 214 | SUnit *NewSU; |
| 215 | bool TryUnfold = false; |
| 216 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { |
| 217 | MVT VT = N->getValueType(i); |
| 218 | if (VT == MVT::Flag) |
| 219 | return NULL; |
| 220 | else if (VT == MVT::Other) |
| 221 | TryUnfold = true; |
| 222 | } |
| 223 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 224 | const SDValue &Op = N->getOperand(i); |
| 225 | MVT VT = Op.getNode()->getValueType(Op.getResNo()); |
| 226 | if (VT == MVT::Flag) |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | if (TryUnfold) { |
| 231 | SmallVector<SDNode*, 2> NewNodes; |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 232 | if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes)) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | |
| 235 | DOUT << "Unfolding SU # " << SU->NodeNum << "\n"; |
| 236 | assert(NewNodes.size() == 2 && "Expected a load folding node!"); |
| 237 | |
| 238 | N = NewNodes[1]; |
| 239 | SDNode *LoadNode = NewNodes[0]; |
| 240 | unsigned NumVals = N->getNumValues(); |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 241 | unsigned OldNumVals = SU->getNode()->getNumValues(); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 242 | for (unsigned i = 0; i != NumVals; ++i) |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 243 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i)); |
| 244 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1), |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 245 | SDValue(LoadNode, 1)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 246 | |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame^] | 247 | SUnit *NewSU = NewSUnit(N); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 248 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 249 | N->setNodeId(NewSU->NodeNum); |
| 250 | |
| 251 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); |
| 252 | for (unsigned i = 0; i != TID.getNumOperands(); ++i) { |
| 253 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { |
| 254 | NewSU->isTwoAddress = true; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | if (TID.isCommutable()) |
| 259 | NewSU->isCommutable = true; |
| 260 | // FIXME: Calculate height / depth and propagate the changes? |
| 261 | NewSU->Depth = SU->Depth; |
| 262 | NewSU->Height = SU->Height; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 263 | |
| 264 | // LoadNode may already exist. This can happen when there is another |
| 265 | // load from the same location and producing the same type of value |
| 266 | // but it has different alignment or volatileness. |
| 267 | bool isNewLoad = true; |
| 268 | SUnit *LoadSU; |
| 269 | if (LoadNode->getNodeId() != -1) { |
| 270 | LoadSU = &SUnits[LoadNode->getNodeId()]; |
| 271 | isNewLoad = false; |
| 272 | } else { |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame^] | 273 | LoadSU = NewSUnit(LoadNode); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 274 | LoadNode->setNodeId(LoadSU->NodeNum); |
| 275 | |
| 276 | LoadSU->Depth = SU->Depth; |
| 277 | LoadSU->Height = SU->Height; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | SUnit *ChainPred = NULL; |
| 281 | SmallVector<SDep, 4> ChainSuccs; |
| 282 | SmallVector<SDep, 4> LoadPreds; |
| 283 | SmallVector<SDep, 4> NodePreds; |
| 284 | SmallVector<SDep, 4> NodeSuccs; |
| 285 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 286 | I != E; ++I) { |
| 287 | if (I->isCtrl) |
| 288 | ChainPred = I->Dep; |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 289 | else if (I->Dep->getNode() && I->Dep->getNode()->isOperandOf(LoadNode)) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 290 | LoadPreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); |
| 291 | else |
| 292 | NodePreds.push_back(SDep(I->Dep, I->Reg, I->Cost, false, false)); |
| 293 | } |
| 294 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 295 | I != E; ++I) { |
| 296 | if (I->isCtrl) |
| 297 | ChainSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, |
| 298 | I->isCtrl, I->isSpecial)); |
| 299 | else |
| 300 | NodeSuccs.push_back(SDep(I->Dep, I->Reg, I->Cost, |
| 301 | I->isCtrl, I->isSpecial)); |
| 302 | } |
| 303 | |
| 304 | if (ChainPred) { |
| 305 | RemovePred(SU, ChainPred, true, false); |
| 306 | if (isNewLoad) |
| 307 | AddPred(LoadSU, ChainPred, true, false); |
| 308 | } |
| 309 | for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { |
| 310 | SDep *Pred = &LoadPreds[i]; |
| 311 | RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial); |
| 312 | if (isNewLoad) { |
| 313 | AddPred(LoadSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial, |
| 314 | Pred->Reg, Pred->Cost); |
| 315 | } |
| 316 | } |
| 317 | for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { |
| 318 | SDep *Pred = &NodePreds[i]; |
| 319 | RemovePred(SU, Pred->Dep, Pred->isCtrl, Pred->isSpecial); |
| 320 | AddPred(NewSU, Pred->Dep, Pred->isCtrl, Pred->isSpecial, |
| 321 | Pred->Reg, Pred->Cost); |
| 322 | } |
| 323 | for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { |
| 324 | SDep *Succ = &NodeSuccs[i]; |
| 325 | RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial); |
| 326 | AddPred(Succ->Dep, NewSU, Succ->isCtrl, Succ->isSpecial, |
| 327 | Succ->Reg, Succ->Cost); |
| 328 | } |
| 329 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { |
| 330 | SDep *Succ = &ChainSuccs[i]; |
| 331 | RemovePred(Succ->Dep, SU, Succ->isCtrl, Succ->isSpecial); |
| 332 | if (isNewLoad) { |
| 333 | AddPred(Succ->Dep, LoadSU, Succ->isCtrl, Succ->isSpecial, |
| 334 | Succ->Reg, Succ->Cost); |
| 335 | } |
| 336 | } |
| 337 | if (isNewLoad) { |
| 338 | AddPred(NewSU, LoadSU, false, false); |
| 339 | } |
| 340 | |
| 341 | ++NumUnfolds; |
| 342 | |
| 343 | if (NewSU->NumSuccsLeft == 0) { |
| 344 | NewSU->isAvailable = true; |
| 345 | return NewSU; |
| 346 | } |
| 347 | SU = NewSU; |
| 348 | } |
| 349 | |
| 350 | DOUT << "Duplicating SU # " << SU->NodeNum << "\n"; |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame^] | 351 | NewSU = Clone(SU); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 352 | |
| 353 | // New SUnit has the exact same predecessors. |
| 354 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 355 | I != E; ++I) |
| 356 | if (!I->isSpecial) { |
| 357 | AddPred(NewSU, I->Dep, I->isCtrl, false, I->Reg, I->Cost); |
| 358 | NewSU->Depth = std::max(NewSU->Depth, I->Dep->Depth+1); |
| 359 | } |
| 360 | |
| 361 | // Only copy scheduled successors. Cut them from old node's successor |
| 362 | // list and move them over. |
| 363 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
| 364 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 365 | I != E; ++I) { |
| 366 | if (I->isSpecial) |
| 367 | continue; |
| 368 | if (I->Dep->isScheduled) { |
| 369 | NewSU->Height = std::max(NewSU->Height, I->Dep->Height+1); |
| 370 | AddPred(I->Dep, NewSU, I->isCtrl, false, I->Reg, I->Cost); |
| 371 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
| 372 | } |
| 373 | } |
| 374 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
| 375 | SUnit *Succ = DelDeps[i].first; |
| 376 | bool isCtrl = DelDeps[i].second; |
| 377 | RemovePred(Succ, SU, isCtrl, false); |
| 378 | } |
| 379 | |
| 380 | ++NumDups; |
| 381 | return NewSU; |
| 382 | } |
| 383 | |
| 384 | /// InsertCCCopiesAndMoveSuccs - Insert expensive cross register class copies |
| 385 | /// and move all scheduled successors of the given SUnit to the last copy. |
| 386 | void ScheduleDAGFast::InsertCCCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, |
| 387 | const TargetRegisterClass *DestRC, |
| 388 | const TargetRegisterClass *SrcRC, |
| 389 | SmallVector<SUnit*, 2> &Copies) { |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame^] | 390 | SUnit *CopyFromSU = NewSUnit(static_cast<SDNode *>(NULL)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 391 | CopyFromSU->CopySrcRC = SrcRC; |
| 392 | CopyFromSU->CopyDstRC = DestRC; |
| 393 | |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame^] | 394 | SUnit *CopyToSU = NewSUnit(static_cast<SDNode *>(NULL)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 395 | CopyToSU->CopySrcRC = DestRC; |
| 396 | CopyToSU->CopyDstRC = SrcRC; |
| 397 | |
| 398 | // Only copy scheduled successors. Cut them from old node's successor |
| 399 | // list and move them over. |
| 400 | SmallVector<std::pair<SUnit*, bool>, 4> DelDeps; |
| 401 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 402 | I != E; ++I) { |
| 403 | if (I->isSpecial) |
| 404 | continue; |
| 405 | if (I->Dep->isScheduled) { |
| 406 | AddPred(I->Dep, CopyToSU, I->isCtrl, false, I->Reg, I->Cost); |
| 407 | DelDeps.push_back(std::make_pair(I->Dep, I->isCtrl)); |
| 408 | } |
| 409 | } |
| 410 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
| 411 | SUnit *Succ = DelDeps[i].first; |
| 412 | bool isCtrl = DelDeps[i].second; |
| 413 | RemovePred(Succ, SU, isCtrl, false); |
| 414 | } |
| 415 | |
| 416 | AddPred(CopyFromSU, SU, false, false, Reg, -1); |
| 417 | AddPred(CopyToSU, CopyFromSU, false, false, Reg, 1); |
| 418 | |
| 419 | Copies.push_back(CopyFromSU); |
| 420 | Copies.push_back(CopyToSU); |
| 421 | |
| 422 | ++NumCCCopies; |
| 423 | } |
| 424 | |
| 425 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register |
| 426 | /// definition of the specified node. |
| 427 | /// FIXME: Move to SelectionDAG? |
| 428 | static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, |
| 429 | const TargetInstrInfo *TII) { |
| 430 | const TargetInstrDesc &TID = TII->get(N->getMachineOpcode()); |
| 431 | assert(TID.ImplicitDefs && "Physical reg def must be in implicit def list!"); |
| 432 | unsigned NumRes = TID.getNumDefs(); |
| 433 | for (const unsigned *ImpDef = TID.getImplicitDefs(); *ImpDef; ++ImpDef) { |
| 434 | if (Reg == *ImpDef) |
| 435 | break; |
| 436 | ++NumRes; |
| 437 | } |
| 438 | return N->getValueType(NumRes); |
| 439 | } |
| 440 | |
| 441 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay |
| 442 | /// scheduling of the given node to satisfy live physical register dependencies. |
| 443 | /// If the specific node is the last one that's available to schedule, do |
| 444 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. |
| 445 | bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU, |
| 446 | SmallVector<unsigned, 4> &LRegs){ |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 447 | if (NumLiveRegs == 0) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 448 | return false; |
| 449 | |
| 450 | SmallSet<unsigned, 4> RegAdded; |
| 451 | // If this node would clobber any "live" register, then it's not ready. |
| 452 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 453 | I != E; ++I) { |
| 454 | if (I->Cost < 0) { |
| 455 | unsigned Reg = I->Reg; |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 456 | if (LiveRegDefs[Reg] && LiveRegDefs[Reg] != I->Dep) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 457 | if (RegAdded.insert(Reg)) |
| 458 | LRegs.push_back(Reg); |
| 459 | } |
| 460 | for (const unsigned *Alias = TRI->getAliasSet(Reg); |
| 461 | *Alias; ++Alias) |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 462 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != I->Dep) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 463 | if (RegAdded.insert(*Alias)) |
| 464 | LRegs.push_back(*Alias); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 469 | for (SDNode *Node = SU->getNode(); Node; Node = Node->getFlaggedNode()) { |
| 470 | if (!Node->isMachineOpcode()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 471 | continue; |
| 472 | const TargetInstrDesc &TID = TII->get(Node->getMachineOpcode()); |
| 473 | if (!TID.ImplicitDefs) |
| 474 | continue; |
| 475 | for (const unsigned *Reg = TID.ImplicitDefs; *Reg; ++Reg) { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 476 | if (LiveRegDefs[*Reg] && LiveRegDefs[*Reg] != SU) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 477 | if (RegAdded.insert(*Reg)) |
| 478 | LRegs.push_back(*Reg); |
| 479 | } |
| 480 | for (const unsigned *Alias = TRI->getAliasSet(*Reg); |
| 481 | *Alias; ++Alias) |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 482 | if (LiveRegDefs[*Alias] && LiveRegDefs[*Alias] != SU) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 483 | if (RegAdded.insert(*Alias)) |
| 484 | LRegs.push_back(*Alias); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | return !LRegs.empty(); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 493 | /// schedulers. |
| 494 | void ScheduleDAGFast::ListScheduleBottomUp() { |
| 495 | unsigned CurCycle = 0; |
| 496 | // Add root to Available queue. |
| 497 | if (!SUnits.empty()) { |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 498 | SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()]; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 499 | assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); |
| 500 | RootSU->isAvailable = true; |
| 501 | AvailableQueue.push(RootSU); |
| 502 | } |
| 503 | |
| 504 | // While Available queue is not empty, grab the node with the highest |
| 505 | // priority. If it is not ready put it back. Schedule the node. |
| 506 | SmallVector<SUnit*, 4> NotReady; |
| 507 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; |
| 508 | Sequence.reserve(SUnits.size()); |
| 509 | while (!AvailableQueue.empty()) { |
| 510 | bool Delayed = false; |
| 511 | LRegsMap.clear(); |
| 512 | SUnit *CurSU = AvailableQueue.pop(); |
| 513 | while (CurSU) { |
Dan Gohman | e93483d | 2008-11-17 19:52:36 +0000 | [diff] [blame] | 514 | SmallVector<unsigned, 4> LRegs; |
| 515 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) |
| 516 | break; |
| 517 | Delayed = true; |
| 518 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 519 | |
| 520 | CurSU->isPending = true; // This SU is not in AvailableQueue right now. |
| 521 | NotReady.push_back(CurSU); |
| 522 | CurSU = AvailableQueue.pop(); |
| 523 | } |
| 524 | |
| 525 | // All candidates are delayed due to live physical reg dependencies. |
| 526 | // Try code duplication or inserting cross class copies |
| 527 | // to resolve it. |
| 528 | if (Delayed && !CurSU) { |
| 529 | if (!CurSU) { |
| 530 | // Try duplicating the nodes that produces these |
| 531 | // "expensive to copy" values to break the dependency. In case even |
| 532 | // that doesn't work, insert cross class copies. |
| 533 | SUnit *TrySU = NotReady[0]; |
| 534 | SmallVector<unsigned, 4> &LRegs = LRegsMap[TrySU]; |
| 535 | assert(LRegs.size() == 1 && "Can't handle this yet!"); |
| 536 | unsigned Reg = LRegs[0]; |
| 537 | SUnit *LRDef = LiveRegDefs[Reg]; |
| 538 | SUnit *NewDef = CopyAndMoveSuccessors(LRDef); |
| 539 | if (!NewDef) { |
| 540 | // Issue expensive cross register class copies. |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 541 | MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 542 | const TargetRegisterClass *RC = |
| 543 | TRI->getPhysicalRegisterRegClass(Reg, VT); |
| 544 | const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); |
| 545 | if (!DestRC) { |
| 546 | assert(false && "Don't know how to copy this physical register!"); |
| 547 | abort(); |
| 548 | } |
| 549 | SmallVector<SUnit*, 2> Copies; |
| 550 | InsertCCCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); |
| 551 | DOUT << "Adding an edge from SU # " << TrySU->NodeNum |
| 552 | << " to SU #" << Copies.front()->NodeNum << "\n"; |
| 553 | AddPred(TrySU, Copies.front(), true, true); |
| 554 | NewDef = Copies.back(); |
| 555 | } |
| 556 | |
| 557 | DOUT << "Adding an edge from SU # " << NewDef->NodeNum |
| 558 | << " to SU #" << TrySU->NodeNum << "\n"; |
| 559 | LiveRegDefs[Reg] = NewDef; |
| 560 | AddPred(NewDef, TrySU, true, true); |
| 561 | TrySU->isAvailable = false; |
| 562 | CurSU = NewDef; |
| 563 | } |
| 564 | |
| 565 | if (!CurSU) { |
| 566 | assert(false && "Unable to resolve live physical register dependencies!"); |
| 567 | abort(); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // Add the nodes that aren't ready back onto the available list. |
| 572 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 573 | NotReady[i]->isPending = false; |
| 574 | // May no longer be available due to backtracking. |
| 575 | if (NotReady[i]->isAvailable) |
| 576 | AvailableQueue.push(NotReady[i]); |
| 577 | } |
| 578 | NotReady.clear(); |
| 579 | |
| 580 | if (!CurSU) |
| 581 | Sequence.push_back(0); |
Dan Gohman | 1256f5f | 2008-11-18 21:22:20 +0000 | [diff] [blame] | 582 | else |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 583 | ScheduleNodeBottomUp(CurSU, CurCycle); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 584 | ++CurCycle; |
| 585 | } |
| 586 | |
| 587 | // Reverse the order if it is bottom up. |
| 588 | std::reverse(Sequence.begin(), Sequence.end()); |
| 589 | |
| 590 | |
| 591 | #ifndef NDEBUG |
| 592 | // Verify that all SUnits were scheduled. |
| 593 | bool AnyNotSched = false; |
| 594 | unsigned DeadNodes = 0; |
| 595 | unsigned Noops = 0; |
| 596 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 597 | if (!SUnits[i].isScheduled) { |
| 598 | if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) { |
| 599 | ++DeadNodes; |
| 600 | continue; |
| 601 | } |
| 602 | if (!AnyNotSched) |
| 603 | cerr << "*** List scheduling failed! ***\n"; |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 604 | SUnits[i].dump(this); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 605 | cerr << "has not been scheduled!\n"; |
| 606 | AnyNotSched = true; |
| 607 | } |
| 608 | if (SUnits[i].NumSuccsLeft != 0) { |
| 609 | if (!AnyNotSched) |
| 610 | cerr << "*** List scheduling failed! ***\n"; |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 611 | SUnits[i].dump(this); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 612 | cerr << "has successors left!\n"; |
| 613 | AnyNotSched = true; |
| 614 | } |
| 615 | } |
| 616 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) |
| 617 | if (!Sequence[i]) |
| 618 | ++Noops; |
| 619 | assert(!AnyNotSched); |
| 620 | assert(Sequence.size() + DeadNodes - Noops == SUnits.size() && |
| 621 | "The number of nodes scheduled doesn't match the expected number!"); |
| 622 | #endif |
| 623 | } |
| 624 | |
| 625 | //===----------------------------------------------------------------------===// |
| 626 | // Public Constructor Functions |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | |
| 629 | llvm::ScheduleDAG* llvm::createFastDAGScheduler(SelectionDAGISel *IS, |
| 630 | SelectionDAG *DAG, |
Dan Gohman | 9b75b37 | 2008-11-11 17:50:47 +0000 | [diff] [blame] | 631 | const TargetMachine *TM, |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 632 | MachineBasicBlock *BB, bool) { |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 633 | return new ScheduleDAGFast(DAG, BB, *TM); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 634 | } |