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 | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "InstrEmitter.h" |
| 17 | #include "ScheduleDAGSDNodes.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/InlineAsm.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | STATISTIC(NumUnfolds, "Number of nodes unfolded"); |
| 32 | STATISTIC(NumDups, "Number of duplicated nodes"); |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 33 | STATISTIC(NumPRCopies, "Number of physical copies"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 34 | |
| 35 | static RegisterScheduler |
Dan Gohman | b8cab92 | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 36 | fastDAGScheduler("fast", "Fast suboptimal list scheduling", |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 37 | createFastDAGScheduler); |
Evan Cheng | d4f7596 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 38 | static RegisterScheduler |
| 39 | linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling", |
| 40 | createDAGLinearizer); |
| 41 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 42 | |
| 43 | namespace { |
| 44 | /// FastPriorityQueue - A degenerate priority queue that considers |
| 45 | /// all nodes to have the same priority. |
| 46 | /// |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 47 | struct FastPriorityQueue { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 48 | SmallVector<SUnit *, 16> Queue; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 49 | |
| 50 | bool empty() const { return Queue.empty(); } |
Andrew Trick | dbdca36 | 2012-03-07 05:21:32 +0000 | [diff] [blame] | 51 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 52 | void push(SUnit *U) { |
| 53 | Queue.push_back(U); |
| 54 | } |
| 55 | |
| 56 | SUnit *pop() { |
| 57 | if (empty()) return NULL; |
| 58 | SUnit *V = Queue.back(); |
| 59 | Queue.pop_back(); |
| 60 | return V; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | /// ScheduleDAGFast - The actual "fast" list scheduler implementation. |
| 66 | /// |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 67 | class ScheduleDAGFast : public ScheduleDAGSDNodes { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 68 | private: |
| 69 | /// AvailableQueue - The priority queue to use for the available SUnits. |
| 70 | FastPriorityQueue AvailableQueue; |
| 71 | |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 72 | /// LiveRegDefs - A set of physical registers and their definition |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 73 | /// that are "live". These nodes must be scheduled before any other nodes that |
| 74 | /// modifies the registers can be scheduled. |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 75 | unsigned NumLiveRegs; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 76 | std::vector<SUnit*> LiveRegDefs; |
| 77 | std::vector<unsigned> LiveRegCycles; |
| 78 | |
| 79 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 80 | ScheduleDAGFast(MachineFunction &mf) |
| 81 | : ScheduleDAGSDNodes(mf) {} |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 82 | |
| 83 | void Schedule(); |
| 84 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 85 | /// AddPred - adds a predecessor edge to SUnit SU. |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 86 | /// This returns true if this is a new predecessor. |
Dan Gohman | ffa3912 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 87 | void AddPred(SUnit *SU, const SDep &D) { |
| 88 | SU->addPred(D); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 89 | } |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 90 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 91 | /// RemovePred - removes a predecessor edge from SUnit SU. |
| 92 | /// This returns true if an edge was removed. |
Dan Gohman | ffa3912 | 2008-12-16 01:00:55 +0000 | [diff] [blame] | 93 | void RemovePred(SUnit *SU, const SDep &D) { |
| 94 | SU->removePred(D); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 95 | } |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 96 | |
| 97 | private: |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 98 | void ReleasePred(SUnit *SU, SDep *PredEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 99 | void ReleasePredecessors(SUnit *SU, unsigned CurCycle); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 100 | void ScheduleNodeBottomUp(SUnit*, unsigned); |
| 101 | SUnit *CopyAndMoveSuccessors(SUnit*); |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 102 | void InsertCopiesAndMoveSuccs(SUnit*, unsigned, |
| 103 | const TargetRegisterClass*, |
| 104 | const TargetRegisterClass*, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame^] | 105 | SmallVectorImpl<SUnit*>&); |
| 106 | bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 107 | void ListScheduleBottomUp(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 108 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 109 | /// forceUnitLatencies - The fast scheduler doesn't care about real latencies. |
| 110 | bool forceUnitLatencies() const { return true; } |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 111 | }; |
| 112 | } // end anonymous namespace |
| 113 | |
| 114 | |
| 115 | /// Schedule - Schedule the DAG using list scheduling. |
| 116 | void ScheduleDAGFast::Schedule() { |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 117 | DEBUG(dbgs() << "********** List Scheduling **********\n"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 118 | |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 119 | NumLiveRegs = 0; |
Andrew Trick | dbdca36 | 2012-03-07 05:21:32 +0000 | [diff] [blame] | 120 | LiveRegDefs.resize(TRI->getNumRegs(), NULL); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 121 | LiveRegCycles.resize(TRI->getNumRegs(), 0); |
| 122 | |
Dan Gohman | c9a5b9e | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 123 | // Build the scheduling graph. |
Dan Gohman | 98976e4 | 2009-10-09 23:33:48 +0000 | [diff] [blame] | 124 | BuildSchedGraph(NULL); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 125 | |
| 126 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 127 | SUnits[su].dumpAll(this)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 128 | |
| 129 | // Execute the actual scheduling loop. |
| 130 | ListScheduleBottomUp(); |
| 131 | } |
| 132 | |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | // Bottom-Up Scheduling |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
| 137 | /// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to |
| 138 | /// the AvailableQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 139 | void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) { |
| 140 | SUnit *PredSU = PredEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 141 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 142 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 143 | if (PredSU->NumSuccsLeft == 0) { |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 144 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 3cc6243 | 2008-11-18 02:06:40 +0000 | [diff] [blame] | 145 | PredSU->dump(this); |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 146 | dbgs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 147 | llvm_unreachable(0); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 148 | } |
| 149 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 150 | --PredSU->NumSuccsLeft; |
| 151 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 152 | // If all the node's successors are scheduled, this node is ready |
| 153 | // to be scheduled. Ignore the special EntrySU node. |
| 154 | if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 155 | PredSU->isAvailable = true; |
| 156 | AvailableQueue.push(PredSU); |
| 157 | } |
| 158 | } |
| 159 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 160 | void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 161 | // Bottom up: release predecessors |
| 162 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 163 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 164 | ReleasePred(SU, &*I); |
| 165 | if (I->isAssignedRegDep()) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 166 | // This is a physical register dependency and it's impossible or |
Andrew Trick | dbdca36 | 2012-03-07 05:21:32 +0000 | [diff] [blame] | 167 | // expensive to copy the register. Make sure nothing that can |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 168 | // clobber the register is scheduled between the predecessor and |
| 169 | // this node. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 170 | if (!LiveRegDefs[I->getReg()]) { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 171 | ++NumLiveRegs; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 172 | LiveRegDefs[I->getReg()] = I->getSUnit(); |
| 173 | LiveRegCycles[I->getReg()] = CurCycle; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending |
| 180 | /// count of its predecessors. If a predecessor pending count is zero, add it to |
| 181 | /// the Available queue. |
| 182 | void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) { |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 183 | DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 184 | DEBUG(SU->dump(this)); |
| 185 | |
| 186 | assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!"); |
| 187 | SU->setHeightToAtLeast(CurCycle); |
| 188 | Sequence.push_back(SU); |
| 189 | |
| 190 | ReleasePredecessors(SU, CurCycle); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 191 | |
| 192 | // Release all the implicit physical register defs that are live. |
| 193 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 194 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 195 | if (I->isAssignedRegDep()) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 196 | if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) { |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 197 | assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!"); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 198 | assert(LiveRegDefs[I->getReg()] == SU && |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 199 | "Physical register dependency violated?"); |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 200 | --NumLiveRegs; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 201 | LiveRegDefs[I->getReg()] = NULL; |
| 202 | LiveRegCycles[I->getReg()] = 0; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | SU->isScheduled = true; |
| 208 | } |
| 209 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 210 | /// CopyAndMoveSuccessors - Clone the specified node and move its scheduled |
| 211 | /// successors to the newly created node. |
| 212 | SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) { |
Chris Lattner | 29d8f0c | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 213 | if (SU->getNode()->getGluedNode()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 214 | return NULL; |
| 215 | |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 216 | SDNode *N = SU->getNode(); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 217 | if (!N) |
| 218 | return NULL; |
| 219 | |
| 220 | SUnit *NewSU; |
| 221 | bool TryUnfold = false; |
| 222 | for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 223 | EVT VT = N->getValueType(i); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 224 | if (VT == MVT::Glue) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 225 | return NULL; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 226 | else if (VT == MVT::Other) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 227 | TryUnfold = true; |
| 228 | } |
| 229 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 230 | const SDValue &Op = N->getOperand(i); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 231 | EVT VT = Op.getNode()->getValueType(Op.getResNo()); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 232 | if (VT == MVT::Glue) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | if (TryUnfold) { |
| 237 | SmallVector<SDNode*, 2> NewNodes; |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 238 | if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes)) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 239 | return NULL; |
| 240 | |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 241 | DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 242 | assert(NewNodes.size() == 2 && "Expected a load folding node!"); |
| 243 | |
| 244 | N = NewNodes[1]; |
| 245 | SDNode *LoadNode = NewNodes[0]; |
| 246 | unsigned NumVals = N->getNumValues(); |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 247 | unsigned OldNumVals = SU->getNode()->getNumValues(); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 248 | for (unsigned i = 0; i != NumVals; ++i) |
Dan Gohman | 550f5af | 2008-11-13 21:36:12 +0000 | [diff] [blame] | 249 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i)); |
| 250 | DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1), |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 251 | SDValue(LoadNode, 1)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 252 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 253 | SUnit *NewSU = newSUnit(N); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 254 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 255 | N->setNodeId(NewSU->NodeNum); |
Andrew Trick | dbdca36 | 2012-03-07 05:21:32 +0000 | [diff] [blame] | 256 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 257 | const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); |
| 258 | for (unsigned i = 0; i != MCID.getNumOperands(); ++i) { |
| 259 | if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 260 | NewSU->isTwoAddress = true; |
| 261 | break; |
| 262 | } |
| 263 | } |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 264 | if (MCID.isCommutable()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 265 | NewSU->isCommutable = true; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 266 | |
| 267 | // LoadNode may already exist. This can happen when there is another |
| 268 | // load from the same location and producing the same type of value |
| 269 | // but it has different alignment or volatileness. |
| 270 | bool isNewLoad = true; |
| 271 | SUnit *LoadSU; |
| 272 | if (LoadNode->getNodeId() != -1) { |
| 273 | LoadSU = &SUnits[LoadNode->getNodeId()]; |
| 274 | isNewLoad = false; |
| 275 | } else { |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 276 | LoadSU = newSUnit(LoadNode); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 277 | LoadNode->setNodeId(LoadSU->NodeNum); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 280 | SDep ChainPred; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 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) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 287 | if (I->isCtrl()) |
| 288 | ChainPred = *I; |
| 289 | else if (I->getSUnit()->getNode() && |
| 290 | I->getSUnit()->getNode()->isOperandOf(LoadNode)) |
| 291 | LoadPreds.push_back(*I); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 292 | else |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 293 | NodePreds.push_back(*I); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 294 | } |
| 295 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 296 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 297 | if (I->isCtrl()) |
| 298 | ChainSuccs.push_back(*I); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 299 | else |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 300 | NodeSuccs.push_back(*I); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 303 | if (ChainPred.getSUnit()) { |
| 304 | RemovePred(SU, ChainPred); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 305 | if (isNewLoad) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 306 | AddPred(LoadSU, ChainPred); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 307 | } |
| 308 | for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 309 | const SDep &Pred = LoadPreds[i]; |
| 310 | RemovePred(SU, Pred); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 311 | if (isNewLoad) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 312 | AddPred(LoadSU, Pred); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 316 | const SDep &Pred = NodePreds[i]; |
| 317 | RemovePred(SU, Pred); |
| 318 | AddPred(NewSU, Pred); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 319 | } |
| 320 | for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 321 | SDep D = NodeSuccs[i]; |
| 322 | SUnit *SuccDep = D.getSUnit(); |
| 323 | D.setSUnit(SU); |
| 324 | RemovePred(SuccDep, D); |
| 325 | D.setSUnit(NewSU); |
| 326 | AddPred(SuccDep, D); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 327 | } |
| 328 | for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 329 | SDep D = ChainSuccs[i]; |
| 330 | SUnit *SuccDep = D.getSUnit(); |
| 331 | D.setSUnit(SU); |
| 332 | RemovePred(SuccDep, D); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 333 | if (isNewLoad) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 334 | D.setSUnit(LoadSU); |
| 335 | AddPred(SuccDep, D); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 336 | } |
Andrew Trick | dbdca36 | 2012-03-07 05:21:32 +0000 | [diff] [blame] | 337 | } |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 338 | if (isNewLoad) { |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 339 | SDep D(LoadSU, SDep::Barrier); |
| 340 | D.setLatency(LoadSU->Latency); |
| 341 | AddPred(NewSU, D); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | ++NumUnfolds; |
| 345 | |
| 346 | if (NewSU->NumSuccsLeft == 0) { |
| 347 | NewSU->isAvailable = true; |
| 348 | return NewSU; |
| 349 | } |
| 350 | SU = NewSU; |
| 351 | } |
| 352 | |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 353 | DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n"); |
Dan Gohman | cdb260d | 2008-11-19 23:39:02 +0000 | [diff] [blame] | 354 | NewSU = Clone(SU); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 355 | |
| 356 | // New SUnit has the exact same predecessors. |
| 357 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 358 | I != E; ++I) |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 359 | if (!I->isArtificial()) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 360 | AddPred(NewSU, *I); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 361 | |
| 362 | // Only copy scheduled successors. Cut them from old node's successor |
| 363 | // list and move them over. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 364 | SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 365 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 366 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 367 | if (I->isArtificial()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 368 | continue; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 369 | SUnit *SuccSU = I->getSUnit(); |
| 370 | if (SuccSU->isScheduled) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 371 | SDep D = *I; |
| 372 | D.setSUnit(NewSU); |
| 373 | AddPred(SuccSU, D); |
| 374 | D.setSUnit(SU); |
| 375 | DelDeps.push_back(std::make_pair(SuccSU, D)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 378 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 379 | RemovePred(DelDeps[i].first, DelDeps[i].second); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 380 | |
| 381 | ++NumDups; |
| 382 | return NewSU; |
| 383 | } |
| 384 | |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 385 | /// InsertCopiesAndMoveSuccs - Insert register copies and move all |
| 386 | /// scheduled successors of the given SUnit to the last copy. |
| 387 | void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg, |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 388 | const TargetRegisterClass *DestRC, |
| 389 | const TargetRegisterClass *SrcRC, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame^] | 390 | SmallVectorImpl<SUnit*> &Copies) { |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 391 | SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(NULL)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 392 | CopyFromSU->CopySrcRC = SrcRC; |
| 393 | CopyFromSU->CopyDstRC = DestRC; |
| 394 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 395 | SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(NULL)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 396 | CopyToSU->CopySrcRC = DestRC; |
| 397 | CopyToSU->CopyDstRC = SrcRC; |
| 398 | |
| 399 | // Only copy scheduled successors. Cut them from old node's successor |
| 400 | // list and move them over. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 401 | SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 402 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 403 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 404 | if (I->isArtificial()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 405 | continue; |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 406 | SUnit *SuccSU = I->getSUnit(); |
| 407 | if (SuccSU->isScheduled) { |
| 408 | SDep D = *I; |
| 409 | D.setSUnit(CopyToSU); |
| 410 | AddPred(SuccSU, D); |
| 411 | DelDeps.push_back(std::make_pair(SuccSU, *I)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 415 | RemovePred(DelDeps[i].first, DelDeps[i].second); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 416 | } |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 417 | SDep FromDep(SU, SDep::Data, Reg); |
| 418 | FromDep.setLatency(SU->Latency); |
| 419 | AddPred(CopyFromSU, FromDep); |
| 420 | SDep ToDep(CopyFromSU, SDep::Data, 0); |
| 421 | ToDep.setLatency(CopyFromSU->Latency); |
| 422 | AddPred(CopyToSU, ToDep); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 423 | |
| 424 | Copies.push_back(CopyFromSU); |
| 425 | Copies.push_back(CopyToSU); |
| 426 | |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 427 | ++NumPRCopies; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /// getPhysicalRegisterVT - Returns the ValueType of the physical register |
| 431 | /// definition of the specified node. |
| 432 | /// FIXME: Move to SelectionDAG? |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 433 | static EVT getPhysicalRegisterVT(SDNode *N, unsigned Reg, |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 434 | const TargetInstrInfo *TII) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 435 | const MCInstrDesc &MCID = TII->get(N->getMachineOpcode()); |
| 436 | assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!"); |
| 437 | unsigned NumRes = MCID.getNumDefs(); |
Craig Topper | fac2598 | 2012-03-08 08:22:45 +0000 | [diff] [blame] | 438 | for (const uint16_t *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) { |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 439 | if (Reg == *ImpDef) |
| 440 | break; |
| 441 | ++NumRes; |
| 442 | } |
| 443 | return N->getValueType(NumRes); |
| 444 | } |
| 445 | |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 446 | /// CheckForLiveRegDef - Return true and update live register vector if the |
| 447 | /// specified register def of the specified SUnit clobbers any "live" registers. |
| 448 | static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg, |
| 449 | std::vector<SUnit*> &LiveRegDefs, |
| 450 | SmallSet<unsigned, 4> &RegAdded, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame^] | 451 | SmallVectorImpl<unsigned> &LRegs, |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 452 | const TargetRegisterInfo *TRI) { |
| 453 | bool Added = false; |
Jakob Stoklund Olesen | 8c70ea4 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 454 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
| 455 | if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) { |
| 456 | if (RegAdded.insert(*AI)) { |
| 457 | LRegs.push_back(*AI); |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 458 | Added = true; |
| 459 | } |
| 460 | } |
Jakob Stoklund Olesen | 8c70ea4 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 461 | } |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 462 | return Added; |
| 463 | } |
| 464 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 465 | /// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay |
| 466 | /// scheduling of the given node to satisfy live physical register dependencies. |
| 467 | /// If the specific node is the last one that's available to schedule, do |
| 468 | /// whatever is necessary (i.e. backtracking or cloning) to make it possible. |
| 469 | bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame^] | 470 | SmallVectorImpl<unsigned> &LRegs){ |
Dan Gohman | 086ec99 | 2008-09-23 18:50:48 +0000 | [diff] [blame] | 471 | if (NumLiveRegs == 0) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 472 | return false; |
| 473 | |
| 474 | SmallSet<unsigned, 4> RegAdded; |
| 475 | // If this node would clobber any "live" register, then it's not ready. |
| 476 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 477 | I != E; ++I) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 478 | if (I->isAssignedRegDep()) { |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 479 | CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs, |
| 480 | RegAdded, LRegs, TRI); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
Chris Lattner | 29d8f0c | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 484 | for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) { |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 485 | if (Node->getOpcode() == ISD::INLINEASM) { |
| 486 | // Inline asm can clobber physical defs. |
| 487 | unsigned NumOps = Node->getNumOperands(); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 488 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue) |
Chris Lattner | 29d8f0c | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 489 | --NumOps; // Ignore the glue operand. |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 490 | |
| 491 | for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) { |
| 492 | unsigned Flags = |
| 493 | cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue(); |
| 494 | unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); |
| 495 | |
| 496 | ++i; // Skip the ID value. |
| 497 | if (InlineAsm::isRegDefKind(Flags) || |
Jakob Stoklund Olesen | f792fa9 | 2011-06-27 04:08:33 +0000 | [diff] [blame] | 498 | InlineAsm::isRegDefEarlyClobberKind(Flags) || |
| 499 | InlineAsm::isClobberKind(Flags)) { |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 500 | // Check for def of register or earlyclobber register. |
| 501 | for (; NumVals; --NumVals, ++i) { |
| 502 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 503 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 504 | CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI); |
| 505 | } |
| 506 | } else |
| 507 | i += NumVals; |
| 508 | } |
| 509 | continue; |
| 510 | } |
Dan Gohman | d23e0f8 | 2008-11-13 23:24:17 +0000 | [diff] [blame] | 511 | if (!Node->isMachineOpcode()) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 512 | continue; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 513 | const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode()); |
| 514 | if (!MCID.ImplicitDefs) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 515 | continue; |
Craig Topper | fac2598 | 2012-03-08 08:22:45 +0000 | [diff] [blame] | 516 | for (const uint16_t *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) { |
Dale Johannesen | 6cf64a6 | 2010-08-17 22:17:24 +0000 | [diff] [blame] | 517 | CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | return !LRegs.empty(); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /// ListScheduleBottomUp - The main loop of list scheduling for bottom-up |
| 525 | /// schedulers. |
| 526 | void ScheduleDAGFast::ListScheduleBottomUp() { |
| 527 | unsigned CurCycle = 0; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 528 | |
| 529 | // Release any predecessors of the special Exit node. |
| 530 | ReleasePredecessors(&ExitSU, CurCycle); |
| 531 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 532 | // Add root to Available queue. |
| 533 | if (!SUnits.empty()) { |
Dan Gohman | a23b3b8 | 2008-11-13 21:21:28 +0000 | [diff] [blame] | 534 | SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()]; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 535 | assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!"); |
| 536 | RootSU->isAvailable = true; |
| 537 | AvailableQueue.push(RootSU); |
| 538 | } |
| 539 | |
| 540 | // While Available queue is not empty, grab the node with the highest |
| 541 | // priority. If it is not ready put it back. Schedule the node. |
| 542 | SmallVector<SUnit*, 4> NotReady; |
| 543 | DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap; |
| 544 | Sequence.reserve(SUnits.size()); |
| 545 | while (!AvailableQueue.empty()) { |
| 546 | bool Delayed = false; |
| 547 | LRegsMap.clear(); |
| 548 | SUnit *CurSU = AvailableQueue.pop(); |
| 549 | while (CurSU) { |
Dan Gohman | e93483d | 2008-11-17 19:52:36 +0000 | [diff] [blame] | 550 | SmallVector<unsigned, 4> LRegs; |
| 551 | if (!DelayForLiveRegsBottomUp(CurSU, LRegs)) |
| 552 | break; |
| 553 | Delayed = true; |
| 554 | LRegsMap.insert(std::make_pair(CurSU, LRegs)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 555 | |
| 556 | CurSU->isPending = true; // This SU is not in AvailableQueue right now. |
| 557 | NotReady.push_back(CurSU); |
| 558 | CurSU = AvailableQueue.pop(); |
| 559 | } |
| 560 | |
| 561 | // All candidates are delayed due to live physical reg dependencies. |
| 562 | // Try code duplication or inserting cross class copies |
| 563 | // to resolve it. |
| 564 | if (Delayed && !CurSU) { |
| 565 | if (!CurSU) { |
| 566 | // Try duplicating the nodes that produces these |
| 567 | // "expensive to copy" values to break the dependency. In case even |
| 568 | // that doesn't work, insert cross class copies. |
| 569 | SUnit *TrySU = NotReady[0]; |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame^] | 570 | SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU]; |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 571 | assert(LRegs.size() == 1 && "Can't handle this yet!"); |
| 572 | unsigned Reg = LRegs[0]; |
| 573 | SUnit *LRDef = LiveRegDefs[Reg]; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 574 | EVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII); |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 575 | const TargetRegisterClass *RC = |
Rafael Espindola | d31f972 | 2010-06-29 14:02:34 +0000 | [diff] [blame] | 576 | TRI->getMinimalPhysRegClass(Reg, VT); |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 577 | const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC); |
| 578 | |
Evan Cheng | b0519e1 | 2011-03-10 00:16:32 +0000 | [diff] [blame] | 579 | // If cross copy register class is the same as RC, then it must be |
| 580 | // possible copy the value directly. Do not try duplicate the def. |
| 581 | // If cross copy register class is not the same as RC, then it's |
| 582 | // possible to copy the value but it require cross register class copies |
| 583 | // and it is expensive. |
| 584 | // If cross copy register class is null, then it's not possible to copy |
| 585 | // the value at all. |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 586 | SUnit *NewDef = 0; |
Evan Cheng | b0519e1 | 2011-03-10 00:16:32 +0000 | [diff] [blame] | 587 | if (DestRC != RC) { |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 588 | NewDef = CopyAndMoveSuccessors(LRDef); |
Evan Cheng | b0519e1 | 2011-03-10 00:16:32 +0000 | [diff] [blame] | 589 | if (!DestRC && !NewDef) |
| 590 | report_fatal_error("Can't handle live physical " |
| 591 | "register dependency!"); |
| 592 | } |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 593 | if (!NewDef) { |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 594 | // Issue copies, these can be expensive cross register class copies. |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 595 | SmallVector<SUnit*, 2> Copies; |
Evan Cheng | c29a56d | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 596 | InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies); |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 597 | DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 598 | << " to SU #" << Copies.front()->NodeNum << "\n"); |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 599 | AddPred(TrySU, SDep(Copies.front(), SDep::Artificial)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 600 | NewDef = Copies.back(); |
| 601 | } |
| 602 | |
David Greene | 33db62c | 2010-01-05 01:25:09 +0000 | [diff] [blame] | 603 | DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum |
Chris Lattner | bbbfa99 | 2009-08-23 06:35:02 +0000 | [diff] [blame] | 604 | << " to SU #" << TrySU->NodeNum << "\n"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 605 | LiveRegDefs[Reg] = NewDef; |
Andrew Trick | a78d322 | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 606 | AddPred(NewDef, SDep(TrySU, SDep::Artificial)); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 607 | TrySU->isAvailable = false; |
| 608 | CurSU = NewDef; |
| 609 | } |
| 610 | |
| 611 | if (!CurSU) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 612 | llvm_unreachable("Unable to resolve live physical register dependencies!"); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | // Add the nodes that aren't ready back onto the available list. |
| 617 | for (unsigned i = 0, e = NotReady.size(); i != e; ++i) { |
| 618 | NotReady[i]->isPending = false; |
| 619 | // May no longer be available due to backtracking. |
| 620 | if (NotReady[i]->isAvailable) |
| 621 | AvailableQueue.push(NotReady[i]); |
| 622 | } |
| 623 | NotReady.clear(); |
| 624 | |
Dan Gohman | 47d1a21 | 2008-11-21 00:10:42 +0000 | [diff] [blame] | 625 | if (CurSU) |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 626 | ScheduleNodeBottomUp(CurSU, CurCycle); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 627 | ++CurCycle; |
| 628 | } |
| 629 | |
Dan Gohman | 937d2d8 | 2009-09-28 16:09:41 +0000 | [diff] [blame] | 630 | // Reverse the order since it is bottom up. |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 631 | std::reverse(Sequence.begin(), Sequence.end()); |
Dan Gohman | 937d2d8 | 2009-09-28 16:09:41 +0000 | [diff] [blame] | 632 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 633 | #ifndef NDEBUG |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 634 | VerifyScheduledSequence(/*isBottomUp=*/true); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 635 | #endif |
| 636 | } |
| 637 | |
Evan Cheng | d4f7596 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 638 | |
Benjamin Kramer | 63a4c246 | 2012-10-20 12:53:26 +0000 | [diff] [blame] | 639 | namespace { |
Evan Cheng | d4f7596 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 640 | //===----------------------------------------------------------------------===// |
| 641 | // ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the |
| 642 | // DAG in topological order. |
| 643 | // IMPORTANT: this may not work for targets with phyreg dependency. |
| 644 | // |
| 645 | class ScheduleDAGLinearize : public ScheduleDAGSDNodes { |
| 646 | public: |
| 647 | ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {} |
| 648 | |
| 649 | void Schedule(); |
| 650 | |
| 651 | MachineBasicBlock *EmitSchedule(MachineBasicBlock::iterator &InsertPos); |
| 652 | |
| 653 | private: |
| 654 | std::vector<SDNode*> Sequence; |
| 655 | DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user |
| 656 | |
| 657 | void ScheduleNode(SDNode *N); |
| 658 | }; |
Benjamin Kramer | 63a4c246 | 2012-10-20 12:53:26 +0000 | [diff] [blame] | 659 | } // end anonymous namespace |
Evan Cheng | d4f7596 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 660 | |
| 661 | void ScheduleDAGLinearize::ScheduleNode(SDNode *N) { |
| 662 | if (N->getNodeId() != 0) |
| 663 | llvm_unreachable(0); |
| 664 | |
| 665 | if (!N->isMachineOpcode() && |
| 666 | (N->getOpcode() == ISD::EntryToken || isPassiveNode(N))) |
| 667 | // These nodes do not need to be translated into MIs. |
| 668 | return; |
| 669 | |
| 670 | DEBUG(dbgs() << "\n*** Scheduling: "); |
| 671 | DEBUG(N->dump(DAG)); |
| 672 | Sequence.push_back(N); |
| 673 | |
| 674 | unsigned NumOps = N->getNumOperands(); |
| 675 | if (unsigned NumLeft = NumOps) { |
| 676 | SDNode *GluedOpN = 0; |
| 677 | do { |
| 678 | const SDValue &Op = N->getOperand(NumLeft-1); |
| 679 | SDNode *OpN = Op.getNode(); |
| 680 | |
| 681 | if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) { |
| 682 | // Schedule glue operand right above N. |
| 683 | GluedOpN = OpN; |
| 684 | assert(OpN->getNodeId() != 0 && "Glue operand not ready?"); |
| 685 | OpN->setNodeId(0); |
| 686 | ScheduleNode(OpN); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | if (OpN == GluedOpN) |
| 691 | // Glue operand is already scheduled. |
| 692 | continue; |
| 693 | |
| 694 | DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN); |
| 695 | if (DI != GluedMap.end() && DI->second != N) |
| 696 | // Users of glues are counted against the glued users. |
| 697 | OpN = DI->second; |
| 698 | |
| 699 | unsigned Degree = OpN->getNodeId(); |
| 700 | assert(Degree > 0 && "Predecessor over-released!"); |
| 701 | OpN->setNodeId(--Degree); |
| 702 | if (Degree == 0) |
| 703 | ScheduleNode(OpN); |
| 704 | } while (--NumLeft); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /// findGluedUser - Find the representative use of a glue value by walking |
| 709 | /// the use chain. |
| 710 | static SDNode *findGluedUser(SDNode *N) { |
| 711 | while (SDNode *Glued = N->getGluedUser()) |
| 712 | N = Glued; |
| 713 | return N; |
| 714 | } |
| 715 | |
| 716 | void ScheduleDAGLinearize::Schedule() { |
| 717 | DEBUG(dbgs() << "********** DAG Linearization **********\n"); |
| 718 | |
| 719 | SmallVector<SDNode*, 8> Glues; |
| 720 | unsigned DAGSize = 0; |
| 721 | for (SelectionDAG::allnodes_iterator I = DAG->allnodes_begin(), |
| 722 | E = DAG->allnodes_end(); I != E; ++I) { |
| 723 | SDNode *N = I; |
| 724 | |
| 725 | // Use node id to record degree. |
| 726 | unsigned Degree = N->use_size(); |
| 727 | N->setNodeId(Degree); |
| 728 | unsigned NumVals = N->getNumValues(); |
| 729 | if (NumVals && N->getValueType(NumVals-1) == MVT::Glue && |
| 730 | N->hasAnyUseOfValue(NumVals-1)) { |
| 731 | SDNode *User = findGluedUser(N); |
| 732 | if (User) { |
| 733 | Glues.push_back(N); |
| 734 | GluedMap.insert(std::make_pair(N, User)); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (N->isMachineOpcode() || |
| 739 | (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N))) |
| 740 | ++DAGSize; |
| 741 | } |
| 742 | |
| 743 | for (unsigned i = 0, e = Glues.size(); i != e; ++i) { |
| 744 | SDNode *Glue = Glues[i]; |
| 745 | SDNode *GUser = GluedMap[Glue]; |
| 746 | unsigned Degree = Glue->getNodeId(); |
| 747 | unsigned UDegree = GUser->getNodeId(); |
| 748 | |
| 749 | // Glue user must be scheduled together with the glue operand. So other |
| 750 | // users of the glue operand must be treated as its users. |
| 751 | SDNode *ImmGUser = Glue->getGluedUser(); |
| 752 | for (SDNode::use_iterator ui = Glue->use_begin(), ue = Glue->use_end(); |
| 753 | ui != ue; ++ui) |
| 754 | if (*ui == ImmGUser) |
| 755 | --Degree; |
| 756 | GUser->setNodeId(UDegree + Degree); |
| 757 | Glue->setNodeId(1); |
| 758 | } |
| 759 | |
| 760 | Sequence.reserve(DAGSize); |
| 761 | ScheduleNode(DAG->getRoot().getNode()); |
| 762 | } |
| 763 | |
| 764 | MachineBasicBlock* |
| 765 | ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) { |
| 766 | InstrEmitter Emitter(BB, InsertPos); |
| 767 | DenseMap<SDValue, unsigned> VRBaseMap; |
| 768 | |
| 769 | DEBUG({ |
| 770 | dbgs() << "\n*** Final schedule ***\n"; |
| 771 | }); |
| 772 | |
| 773 | // FIXME: Handle dbg_values. |
| 774 | unsigned NumNodes = Sequence.size(); |
| 775 | for (unsigned i = 0; i != NumNodes; ++i) { |
| 776 | SDNode *N = Sequence[NumNodes-i-1]; |
| 777 | DEBUG(N->dump(DAG)); |
| 778 | Emitter.EmitNode(N, false, false, VRBaseMap); |
| 779 | } |
| 780 | |
| 781 | DEBUG(dbgs() << '\n'); |
| 782 | |
| 783 | InsertPos = Emitter.getInsertPos(); |
| 784 | return Emitter.getBlock(); |
| 785 | } |
| 786 | |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 787 | //===----------------------------------------------------------------------===// |
| 788 | // Public Constructor Functions |
| 789 | //===----------------------------------------------------------------------===// |
| 790 | |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 791 | llvm::ScheduleDAGSDNodes * |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 792 | llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) { |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 793 | return new ScheduleDAGFast(*IS->MF); |
Dan Gohman | ee2e403 | 2008-09-18 16:26:26 +0000 | [diff] [blame] | 794 | } |
Evan Cheng | d4f7596 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 795 | |
| 796 | llvm::ScheduleDAGSDNodes * |
| 797 | llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) { |
| 798 | return new ScheduleDAGLinearize(*IS->MF); |
| 799 | } |