blob: 62e7733ecd2bb45f99a6a1e85701d74dc0beb3ae [file] [log] [blame]
Dan Gohman95be7d72008-09-18 16:26:26 +00001//===----- 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
Dan Gohman95be7d72008-09-18 16:26:26 +000014#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "InstrEmitter.h"
16#include "ScheduleDAGSDNodes.h"
17#include "llvm/ADT/STLExtras.h"
Dan Gohman95be7d72008-09-18 16:26:26 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/InlineAsm.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000025#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetInstrInfo.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman95be7d72008-09-18 16:26:26 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "pre-RA-sched"
31
Dan Gohman95be7d72008-09-18 16:26:26 +000032STATISTIC(NumUnfolds, "Number of nodes unfolded");
33STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000034STATISTIC(NumPRCopies, "Number of physical copies");
Dan Gohman95be7d72008-09-18 16:26:26 +000035
36static RegisterScheduler
Dan Gohman9c4b7d52008-10-14 20:25:08 +000037 fastDAGScheduler("fast", "Fast suboptimal list scheduling",
Dan Gohman95be7d72008-09-18 16:26:26 +000038 createFastDAGScheduler);
Evan Cheng839fb652012-10-17 19:39:36 +000039static RegisterScheduler
40 linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling",
41 createDAGLinearizer);
42
Dan Gohman95be7d72008-09-18 16:26:26 +000043
44namespace {
45 /// FastPriorityQueue - A degenerate priority queue that considers
46 /// all nodes to have the same priority.
47 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000048 struct FastPriorityQueue {
Dan Gohmanc07f6862008-09-23 18:50:48 +000049 SmallVector<SUnit *, 16> Queue;
Dan Gohman95be7d72008-09-18 16:26:26 +000050
51 bool empty() const { return Queue.empty(); }
Andrew Trick7c6c41a2012-03-07 05:21:32 +000052
Dan Gohman95be7d72008-09-18 16:26:26 +000053 void push(SUnit *U) {
54 Queue.push_back(U);
55 }
56
57 SUnit *pop() {
Craig Topperc0196b12014-04-14 00:51:57 +000058 if (empty()) return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +000059 SUnit *V = Queue.back();
60 Queue.pop_back();
61 return V;
62 }
63 };
64
65//===----------------------------------------------------------------------===//
66/// ScheduleDAGFast - The actual "fast" list scheduler implementation.
67///
Nick Lewycky02d5f772009-10-25 06:33:48 +000068class ScheduleDAGFast : public ScheduleDAGSDNodes {
Dan Gohman95be7d72008-09-18 16:26:26 +000069private:
70 /// AvailableQueue - The priority queue to use for the available SUnits.
71 FastPriorityQueue AvailableQueue;
72
Dan Gohmanc07f6862008-09-23 18:50:48 +000073 /// LiveRegDefs - A set of physical registers and their definition
Dan Gohman95be7d72008-09-18 16:26:26 +000074 /// that are "live". These nodes must be scheduled before any other nodes that
75 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000076 unsigned NumLiveRegs;
Dan Gohman95be7d72008-09-18 16:26:26 +000077 std::vector<SUnit*> LiveRegDefs;
78 std::vector<unsigned> LiveRegCycles;
79
80public:
Dan Gohman619ef482009-01-15 19:20:50 +000081 ScheduleDAGFast(MachineFunction &mf)
82 : ScheduleDAGSDNodes(mf) {}
Dan Gohman95be7d72008-09-18 16:26:26 +000083
Craig Topper7b883b32014-03-08 06:31:39 +000084 void Schedule() override;
Dan Gohman95be7d72008-09-18 16:26:26 +000085
Dan Gohman2d170892008-12-09 22:54:47 +000086 /// AddPred - adds a predecessor edge to SUnit SU.
Dan Gohman95be7d72008-09-18 16:26:26 +000087 /// This returns true if this is a new predecessor.
Dan Gohman17214e62008-12-16 01:00:55 +000088 void AddPred(SUnit *SU, const SDep &D) {
89 SU->addPred(D);
Dan Gohman2d170892008-12-09 22:54:47 +000090 }
Dan Gohman95be7d72008-09-18 16:26:26 +000091
Dan Gohman2d170892008-12-09 22:54:47 +000092 /// RemovePred - removes a predecessor edge from SUnit SU.
93 /// This returns true if an edge was removed.
Dan Gohman17214e62008-12-16 01:00:55 +000094 void RemovePred(SUnit *SU, const SDep &D) {
95 SU->removePred(D);
Dan Gohman2d170892008-12-09 22:54:47 +000096 }
Dan Gohman95be7d72008-09-18 16:26:26 +000097
98private:
Dan Gohman2d170892008-12-09 22:54:47 +000099 void ReleasePred(SUnit *SU, SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000100 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000101 void ScheduleNodeBottomUp(SUnit*, unsigned);
102 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000103 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
104 const TargetRegisterClass*,
105 const TargetRegisterClass*,
Craig Topperb94011f2013-07-14 04:42:23 +0000106 SmallVectorImpl<SUnit*>&);
107 bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
Dan Gohman95be7d72008-09-18 16:26:26 +0000108 void ListScheduleBottomUp();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000109
Andrew Trick52226d42012-03-07 23:00:49 +0000110 /// forceUnitLatencies - The fast scheduler doesn't care about real latencies.
Craig Topper7b883b32014-03-08 06:31:39 +0000111 bool forceUnitLatencies() const override { return true; }
Dan Gohman95be7d72008-09-18 16:26:26 +0000112};
113} // end anonymous namespace
114
115
116/// Schedule - Schedule the DAG using list scheduling.
117void ScheduleDAGFast::Schedule() {
David Greened65bc152010-01-05 01:25:09 +0000118 DEBUG(dbgs() << "********** List Scheduling **********\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000119
Dan Gohmanc07f6862008-09-23 18:50:48 +0000120 NumLiveRegs = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000121 LiveRegDefs.resize(TRI->getNumRegs(), nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000122 LiveRegCycles.resize(TRI->getNumRegs(), 0);
123
Dan Gohman04543e72008-12-23 18:36:58 +0000124 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000125 BuildSchedGraph(nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000126
127 DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
Dan Gohman22d07b12008-11-18 02:06:40 +0000128 SUnits[su].dumpAll(this));
Dan Gohman95be7d72008-09-18 16:26:26 +0000129
130 // Execute the actual scheduling loop.
131 ListScheduleBottomUp();
132}
133
134//===----------------------------------------------------------------------===//
135// Bottom-Up Scheduling
136//===----------------------------------------------------------------------===//
137
138/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
139/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000140void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
141 SUnit *PredSU = PredEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000142
Dan Gohman95be7d72008-09-18 16:26:26 +0000143#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000144 if (PredSU->NumSuccsLeft == 0) {
David Greened65bc152010-01-05 01:25:09 +0000145 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000146 PredSU->dump(this);
David Greened65bc152010-01-05 01:25:09 +0000147 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000148 llvm_unreachable(nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000149 }
150#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000151 --PredSU->NumSuccsLeft;
152
Dan Gohmanb9543432009-02-10 23:27:53 +0000153 // If all the node's successors are scheduled, this node is ready
154 // to be scheduled. Ignore the special EntrySU node.
155 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000156 PredSU->isAvailable = true;
157 AvailableQueue.push(PredSU);
158 }
159}
160
Dan Gohmanb9543432009-02-10 23:27:53 +0000161void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000162 // Bottom up: release predecessors
163 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
164 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000165 ReleasePred(SU, &*I);
166 if (I->isAssignedRegDep()) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000167 // This is a physical register dependency and it's impossible or
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000168 // expensive to copy the register. Make sure nothing that can
Dan Gohman95be7d72008-09-18 16:26:26 +0000169 // clobber the register is scheduled between the predecessor and
170 // this node.
Dan Gohman2d170892008-12-09 22:54:47 +0000171 if (!LiveRegDefs[I->getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000172 ++NumLiveRegs;
Dan Gohman2d170892008-12-09 22:54:47 +0000173 LiveRegDefs[I->getReg()] = I->getSUnit();
174 LiveRegCycles[I->getReg()] = CurCycle;
Dan Gohman95be7d72008-09-18 16:26:26 +0000175 }
176 }
177 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000178}
179
180/// ScheduleNodeBottomUp - Add the node to the schedule. Decrement the pending
181/// count of its predecessors. If a predecessor pending count is zero, add it to
182/// the Available queue.
183void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
David Greened65bc152010-01-05 01:25:09 +0000184 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000185 DEBUG(SU->dump(this));
186
187 assert(CurCycle >= SU->getHeight() && "Node scheduled below its height!");
188 SU->setHeightToAtLeast(CurCycle);
189 Sequence.push_back(SU);
190
191 ReleasePredecessors(SU, CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000192
193 // Release all the implicit physical register defs that are live.
194 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
195 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000196 if (I->isAssignedRegDep()) {
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000197 if (LiveRegCycles[I->getReg()] == I->getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000198 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Dan Gohman2d170892008-12-09 22:54:47 +0000199 assert(LiveRegDefs[I->getReg()] == SU &&
Dan Gohman95be7d72008-09-18 16:26:26 +0000200 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000201 --NumLiveRegs;
Craig Topperc0196b12014-04-14 00:51:57 +0000202 LiveRegDefs[I->getReg()] = nullptr;
Dan Gohman2d170892008-12-09 22:54:47 +0000203 LiveRegCycles[I->getReg()] = 0;
Dan Gohman95be7d72008-09-18 16:26:26 +0000204 }
205 }
206 }
207
208 SU->isScheduled = true;
209}
210
Dan Gohman95be7d72008-09-18 16:26:26 +0000211/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
212/// successors to the newly created node.
213SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Chris Lattner11a33812010-12-23 17:24:32 +0000214 if (SU->getNode()->getGluedNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000215 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000216
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000217 SDNode *N = SU->getNode();
Dan Gohman95be7d72008-09-18 16:26:26 +0000218 if (!N)
Craig Topperc0196b12014-04-14 00:51:57 +0000219 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000220
221 SUnit *NewSU;
222 bool TryUnfold = false;
223 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Craig Topper7f416c82014-11-16 21:17:18 +0000224 MVT VT = N->getSimpleValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000225 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000226 return nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000227 else if (VT == MVT::Other)
Dan Gohman95be7d72008-09-18 16:26:26 +0000228 TryUnfold = true;
229 }
Pete Cooper9271ccc2015-06-26 19:18:49 +0000230 for (const SDValue &Op : N->op_values()) {
Craig Topper7f416c82014-11-16 21:17:18 +0000231 MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000232 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000233 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000234 }
235
236 if (TryUnfold) {
237 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000238 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Craig Topperc0196b12014-04-14 00:51:57 +0000239 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000240
David Greened65bc152010-01-05 01:25:09 +0000241 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000242 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 Gohman1ddfcba2008-11-13 21:36:12 +0000247 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohman95be7d72008-09-18 16:26:26 +0000248 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000249 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
250 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000251 SDValue(LoadNode, 1));
Dan Gohman95be7d72008-09-18 16:26:26 +0000252
Andrew Trick52226d42012-03-07 23:00:49 +0000253 SUnit *NewSU = newSUnit(N);
Dan Gohman95be7d72008-09-18 16:26:26 +0000254 assert(N->getNodeId() == -1 && "Node already inserted!");
255 N->setNodeId(NewSU->NodeNum);
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000256
Evan Cheng6cc775f2011-06-28 19:10:37 +0000257 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 Gohman95be7d72008-09-18 16:26:26 +0000260 NewSU->isTwoAddress = true;
261 break;
262 }
263 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000264 if (MCID.isCommutable())
Dan Gohman95be7d72008-09-18 16:26:26 +0000265 NewSU->isCommutable = true;
Dan Gohman95be7d72008-09-18 16:26:26 +0000266
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 Trick52226d42012-03-07 23:00:49 +0000276 LoadSU = newSUnit(LoadNode);
Dan Gohman95be7d72008-09-18 16:26:26 +0000277 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohman95be7d72008-09-18 16:26:26 +0000278 }
279
Dan Gohman2d170892008-12-09 22:54:47 +0000280 SDep ChainPred;
Dan Gohman95be7d72008-09-18 16:26:26 +0000281 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 Gohman2d170892008-12-09 22:54:47 +0000287 if (I->isCtrl())
288 ChainPred = *I;
289 else if (I->getSUnit()->getNode() &&
290 I->getSUnit()->getNode()->isOperandOf(LoadNode))
291 LoadPreds.push_back(*I);
Dan Gohman95be7d72008-09-18 16:26:26 +0000292 else
Dan Gohman2d170892008-12-09 22:54:47 +0000293 NodePreds.push_back(*I);
Dan Gohman95be7d72008-09-18 16:26:26 +0000294 }
295 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
296 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000297 if (I->isCtrl())
298 ChainSuccs.push_back(*I);
Dan Gohman95be7d72008-09-18 16:26:26 +0000299 else
Dan Gohman2d170892008-12-09 22:54:47 +0000300 NodeSuccs.push_back(*I);
Dan Gohman95be7d72008-09-18 16:26:26 +0000301 }
302
Dan Gohman2d170892008-12-09 22:54:47 +0000303 if (ChainPred.getSUnit()) {
304 RemovePred(SU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000305 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000306 AddPred(LoadSU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000307 }
308 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000309 const SDep &Pred = LoadPreds[i];
310 RemovePred(SU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000311 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000312 AddPred(LoadSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000313 }
314 }
315 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000316 const SDep &Pred = NodePreds[i];
317 RemovePred(SU, Pred);
318 AddPred(NewSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000319 }
320 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000321 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 Gohman95be7d72008-09-18 16:26:26 +0000327 }
328 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000329 SDep D = ChainSuccs[i];
330 SUnit *SuccDep = D.getSUnit();
331 D.setSUnit(SU);
332 RemovePred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000333 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000334 D.setSUnit(LoadSU);
335 AddPred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000336 }
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000337 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000338 if (isNewLoad) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000339 SDep D(LoadSU, SDep::Barrier);
340 D.setLatency(LoadSU->Latency);
341 AddPred(NewSU, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000342 }
343
344 ++NumUnfolds;
345
346 if (NewSU->NumSuccsLeft == 0) {
347 NewSU->isAvailable = true;
348 return NewSU;
349 }
350 SU = NewSU;
351 }
352
David Greened65bc152010-01-05 01:25:09 +0000353 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Dan Gohman4c3034f2008-11-19 23:39:02 +0000354 NewSU = Clone(SU);
Dan Gohman95be7d72008-09-18 16:26:26 +0000355
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 Gohmandddc1ac2008-12-16 03:25:46 +0000359 if (!I->isArtificial())
Dan Gohman2d170892008-12-09 22:54:47 +0000360 AddPred(NewSU, *I);
Dan Gohman95be7d72008-09-18 16:26:26 +0000361
362 // Only copy scheduled successors. Cut them from old node's successor
363 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000364 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohman95be7d72008-09-18 16:26:26 +0000365 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
366 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000367 if (I->isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000368 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000369 SUnit *SuccSU = I->getSUnit();
370 if (SuccSU->isScheduled) {
Dan Gohman2d170892008-12-09 22:54:47 +0000371 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 Gohman95be7d72008-09-18 16:26:26 +0000376 }
377 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000378 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000379 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000380
381 ++NumDups;
382 return NewSU;
383}
384
Evan Chengb2c42c62009-01-12 03:19:55 +0000385/// InsertCopiesAndMoveSuccs - Insert register copies and move all
386/// scheduled successors of the given SUnit to the last copy.
387void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000388 const TargetRegisterClass *DestRC,
389 const TargetRegisterClass *SrcRC,
Craig Topperb94011f2013-07-14 04:42:23 +0000390 SmallVectorImpl<SUnit*> &Copies) {
Craig Topperc0196b12014-04-14 00:51:57 +0000391 SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000392 CopyFromSU->CopySrcRC = SrcRC;
393 CopyFromSU->CopyDstRC = DestRC;
394
Craig Topperc0196b12014-04-14 00:51:57 +0000395 SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000396 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 Gohman2d170892008-12-09 22:54:47 +0000401 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Dan Gohman95be7d72008-09-18 16:26:26 +0000402 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
403 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000404 if (I->isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000405 continue;
Dan Gohman2d170892008-12-09 22:54:47 +0000406 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 Gohman95be7d72008-09-18 16:26:26 +0000412 }
413 }
414 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000415 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000416 }
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000417 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 Gohman95be7d72008-09-18 16:26:26 +0000423
424 Copies.push_back(CopyFromSU);
425 Copies.push_back(CopyToSU);
426
Evan Chengb2c42c62009-01-12 03:19:55 +0000427 ++NumPRCopies;
Dan Gohman95be7d72008-09-18 16:26:26 +0000428}
429
430/// getPhysicalRegisterVT - Returns the ValueType of the physical register
431/// definition of the specified node.
432/// FIXME: Move to SelectionDAG?
Craig Topper7f416c82014-11-16 21:17:18 +0000433static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000434 const TargetInstrInfo *TII) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000435 unsigned NumRes;
436 if (N->getOpcode() == ISD::CopyFromReg) {
437 // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
438 NumRes = 1;
439 } else {
440 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
441 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
442 NumRes = MCID.getNumDefs();
Craig Toppere5e035a32015-12-05 07:13:35 +0000443 for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000444 if (Reg == *ImpDef)
445 break;
446 ++NumRes;
447 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000448 }
Craig Topper7f416c82014-11-16 21:17:18 +0000449 return N->getSimpleValueType(NumRes);
Dan Gohman95be7d72008-09-18 16:26:26 +0000450}
451
Dale Johannesen16f96442010-08-17 22:17:24 +0000452/// CheckForLiveRegDef - Return true and update live register vector if the
453/// specified register def of the specified SUnit clobbers any "live" registers.
454static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
455 std::vector<SUnit*> &LiveRegDefs,
456 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +0000457 SmallVectorImpl<unsigned> &LRegs,
Dale Johannesen16f96442010-08-17 22:17:24 +0000458 const TargetRegisterInfo *TRI) {
459 bool Added = false;
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000460 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
461 if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) {
David Blaikie70573dc2014-11-19 07:49:26 +0000462 if (RegAdded.insert(*AI).second) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000463 LRegs.push_back(*AI);
Dale Johannesen16f96442010-08-17 22:17:24 +0000464 Added = true;
465 }
466 }
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000467 }
Dale Johannesen16f96442010-08-17 22:17:24 +0000468 return Added;
469}
470
Dan Gohman95be7d72008-09-18 16:26:26 +0000471/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
472/// scheduling of the given node to satisfy live physical register dependencies.
473/// If the specific node is the last one that's available to schedule, do
474/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
475bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
Craig Topperb94011f2013-07-14 04:42:23 +0000476 SmallVectorImpl<unsigned> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000477 if (NumLiveRegs == 0)
Dan Gohman95be7d72008-09-18 16:26:26 +0000478 return false;
479
480 SmallSet<unsigned, 4> RegAdded;
481 // If this node would clobber any "live" register, then it's not ready.
482 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
483 I != E; ++I) {
Dan Gohman2d170892008-12-09 22:54:47 +0000484 if (I->isAssignedRegDep()) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000485 CheckForLiveRegDef(I->getSUnit(), I->getReg(), LiveRegDefs,
486 RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000487 }
488 }
489
Chris Lattner11a33812010-12-23 17:24:32 +0000490 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000491 if (Node->getOpcode() == ISD::INLINEASM) {
492 // Inline asm can clobber physical defs.
493 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000494 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +0000495 --NumOps; // Ignore the glue operand.
Dale Johannesen16f96442010-08-17 22:17:24 +0000496
497 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
498 unsigned Flags =
499 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
500 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
501
502 ++i; // Skip the ID value.
503 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +0000504 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
505 InlineAsm::isClobberKind(Flags)) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000506 // Check for def of register or earlyclobber register.
507 for (; NumVals; --NumVals, ++i) {
508 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
509 if (TargetRegisterInfo::isPhysicalRegister(Reg))
510 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
511 }
512 } else
513 i += NumVals;
514 }
515 continue;
516 }
Dan Gohman072734e2008-11-13 23:24:17 +0000517 if (!Node->isMachineOpcode())
Dan Gohman95be7d72008-09-18 16:26:26 +0000518 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000519 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
520 if (!MCID.ImplicitDefs)
Dan Gohman95be7d72008-09-18 16:26:26 +0000521 continue;
Craig Toppere5e035a32015-12-05 07:13:35 +0000522 for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000523 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000524 }
525 }
526 return !LRegs.empty();
527}
528
529
530/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
531/// schedulers.
532void ScheduleDAGFast::ListScheduleBottomUp() {
533 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000534
535 // Release any predecessors of the special Exit node.
536 ReleasePredecessors(&ExitSU, CurCycle);
537
Dan Gohman95be7d72008-09-18 16:26:26 +0000538 // Add root to Available queue.
539 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000540 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman95be7d72008-09-18 16:26:26 +0000541 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
542 RootSU->isAvailable = true;
543 AvailableQueue.push(RootSU);
544 }
545
546 // While Available queue is not empty, grab the node with the highest
547 // priority. If it is not ready put it back. Schedule the node.
548 SmallVector<SUnit*, 4> NotReady;
549 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
550 Sequence.reserve(SUnits.size());
551 while (!AvailableQueue.empty()) {
552 bool Delayed = false;
553 LRegsMap.clear();
554 SUnit *CurSU = AvailableQueue.pop();
555 while (CurSU) {
Dan Gohman4f474b02008-11-17 19:52:36 +0000556 SmallVector<unsigned, 4> LRegs;
557 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
558 break;
559 Delayed = true;
560 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohman95be7d72008-09-18 16:26:26 +0000561
562 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
563 NotReady.push_back(CurSU);
564 CurSU = AvailableQueue.pop();
565 }
566
567 // All candidates are delayed due to live physical reg dependencies.
568 // Try code duplication or inserting cross class copies
569 // to resolve it.
570 if (Delayed && !CurSU) {
571 if (!CurSU) {
572 // Try duplicating the nodes that produces these
573 // "expensive to copy" values to break the dependency. In case even
574 // that doesn't work, insert cross class copies.
575 SUnit *TrySU = NotReady[0];
Craig Topperb94011f2013-07-14 04:42:23 +0000576 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Dan Gohman95be7d72008-09-18 16:26:26 +0000577 assert(LRegs.size() == 1 && "Can't handle this yet!");
578 unsigned Reg = LRegs[0];
579 SUnit *LRDef = LiveRegDefs[Reg];
Craig Topper7f416c82014-11-16 21:17:18 +0000580 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000581 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000582 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000583 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
584
Evan Chengb4c6a342011-03-10 00:16:32 +0000585 // If cross copy register class is the same as RC, then it must be
586 // possible copy the value directly. Do not try duplicate the def.
587 // If cross copy register class is not the same as RC, then it's
588 // possible to copy the value but it require cross register class copies
589 // and it is expensive.
590 // If cross copy register class is null, then it's not possible to copy
591 // the value at all.
Craig Topperc0196b12014-04-14 00:51:57 +0000592 SUnit *NewDef = nullptr;
Evan Chengb4c6a342011-03-10 00:16:32 +0000593 if (DestRC != RC) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000594 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +0000595 if (!DestRC && !NewDef)
596 report_fatal_error("Can't handle live physical "
597 "register dependency!");
598 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000599 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000600 // Issue copies, these can be expensive cross register class copies.
Dan Gohman95be7d72008-09-18 16:26:26 +0000601 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000602 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greened65bc152010-01-05 01:25:09 +0000603 DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000604 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000605 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000606 NewDef = Copies.back();
607 }
608
David Greened65bc152010-01-05 01:25:09 +0000609 DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000610 << " to SU #" << TrySU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000611 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000612 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000613 TrySU->isAvailable = false;
614 CurSU = NewDef;
615 }
616
617 if (!CurSU) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000618 llvm_unreachable("Unable to resolve live physical register dependencies!");
Dan Gohman95be7d72008-09-18 16:26:26 +0000619 }
620 }
621
622 // Add the nodes that aren't ready back onto the available list.
623 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
624 NotReady[i]->isPending = false;
625 // May no longer be available due to backtracking.
626 if (NotReady[i]->isAvailable)
627 AvailableQueue.push(NotReady[i]);
628 }
629 NotReady.clear();
630
Dan Gohmanc602dd42008-11-21 00:10:42 +0000631 if (CurSU)
Dan Gohman95be7d72008-09-18 16:26:26 +0000632 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000633 ++CurCycle;
634 }
635
Dan Gohman6905f152009-09-28 16:09:41 +0000636 // Reverse the order since it is bottom up.
Dan Gohman95be7d72008-09-18 16:26:26 +0000637 std::reverse(Sequence.begin(), Sequence.end());
Dan Gohman6905f152009-09-28 16:09:41 +0000638
Dan Gohman95be7d72008-09-18 16:26:26 +0000639#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000640 VerifyScheduledSequence(/*isBottomUp=*/true);
Dan Gohman95be7d72008-09-18 16:26:26 +0000641#endif
642}
643
Evan Cheng839fb652012-10-17 19:39:36 +0000644
Benjamin Kramera74129a2012-10-20 12:53:26 +0000645namespace {
Evan Cheng839fb652012-10-17 19:39:36 +0000646//===----------------------------------------------------------------------===//
647// ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
648// DAG in topological order.
649// IMPORTANT: this may not work for targets with phyreg dependency.
650//
651class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
652public:
653 ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
654
Craig Topper7b883b32014-03-08 06:31:39 +0000655 void Schedule() override;
Evan Cheng839fb652012-10-17 19:39:36 +0000656
Craig Topper7b883b32014-03-08 06:31:39 +0000657 MachineBasicBlock *
658 EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
Evan Cheng839fb652012-10-17 19:39:36 +0000659
660private:
661 std::vector<SDNode*> Sequence;
662 DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user
663
664 void ScheduleNode(SDNode *N);
665};
Benjamin Kramera74129a2012-10-20 12:53:26 +0000666} // end anonymous namespace
Evan Cheng839fb652012-10-17 19:39:36 +0000667
668void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
669 if (N->getNodeId() != 0)
Craig Topperc0196b12014-04-14 00:51:57 +0000670 llvm_unreachable(nullptr);
Evan Cheng839fb652012-10-17 19:39:36 +0000671
672 if (!N->isMachineOpcode() &&
673 (N->getOpcode() == ISD::EntryToken || isPassiveNode(N)))
674 // These nodes do not need to be translated into MIs.
675 return;
676
677 DEBUG(dbgs() << "\n*** Scheduling: ");
678 DEBUG(N->dump(DAG));
679 Sequence.push_back(N);
680
681 unsigned NumOps = N->getNumOperands();
682 if (unsigned NumLeft = NumOps) {
Craig Topperc0196b12014-04-14 00:51:57 +0000683 SDNode *GluedOpN = nullptr;
Evan Cheng839fb652012-10-17 19:39:36 +0000684 do {
685 const SDValue &Op = N->getOperand(NumLeft-1);
686 SDNode *OpN = Op.getNode();
687
688 if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) {
689 // Schedule glue operand right above N.
690 GluedOpN = OpN;
691 assert(OpN->getNodeId() != 0 && "Glue operand not ready?");
692 OpN->setNodeId(0);
693 ScheduleNode(OpN);
694 continue;
695 }
696
697 if (OpN == GluedOpN)
698 // Glue operand is already scheduled.
699 continue;
700
701 DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN);
702 if (DI != GluedMap.end() && DI->second != N)
703 // Users of glues are counted against the glued users.
704 OpN = DI->second;
705
706 unsigned Degree = OpN->getNodeId();
707 assert(Degree > 0 && "Predecessor over-released!");
708 OpN->setNodeId(--Degree);
709 if (Degree == 0)
710 ScheduleNode(OpN);
711 } while (--NumLeft);
712 }
713}
714
715/// findGluedUser - Find the representative use of a glue value by walking
716/// the use chain.
717static SDNode *findGluedUser(SDNode *N) {
718 while (SDNode *Glued = N->getGluedUser())
719 N = Glued;
720 return N;
721}
722
723void ScheduleDAGLinearize::Schedule() {
724 DEBUG(dbgs() << "********** DAG Linearization **********\n");
725
726 SmallVector<SDNode*, 8> Glues;
727 unsigned DAGSize = 0;
Pete Cooper65c69402015-07-14 22:10:54 +0000728 for (SDNode &Node : DAG->allnodes()) {
729 SDNode *N = &Node;
Evan Cheng839fb652012-10-17 19:39:36 +0000730
731 // Use node id to record degree.
732 unsigned Degree = N->use_size();
733 N->setNodeId(Degree);
734 unsigned NumVals = N->getNumValues();
735 if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
736 N->hasAnyUseOfValue(NumVals-1)) {
737 SDNode *User = findGluedUser(N);
738 if (User) {
739 Glues.push_back(N);
740 GluedMap.insert(std::make_pair(N, User));
741 }
742 }
743
744 if (N->isMachineOpcode() ||
745 (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
746 ++DAGSize;
747 }
748
749 for (unsigned i = 0, e = Glues.size(); i != e; ++i) {
750 SDNode *Glue = Glues[i];
751 SDNode *GUser = GluedMap[Glue];
752 unsigned Degree = Glue->getNodeId();
753 unsigned UDegree = GUser->getNodeId();
754
755 // Glue user must be scheduled together with the glue operand. So other
756 // users of the glue operand must be treated as its users.
757 SDNode *ImmGUser = Glue->getGluedUser();
758 for (SDNode::use_iterator ui = Glue->use_begin(), ue = Glue->use_end();
759 ui != ue; ++ui)
760 if (*ui == ImmGUser)
761 --Degree;
762 GUser->setNodeId(UDegree + Degree);
763 Glue->setNodeId(1);
764 }
765
766 Sequence.reserve(DAGSize);
767 ScheduleNode(DAG->getRoot().getNode());
768}
769
770MachineBasicBlock*
771ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
772 InstrEmitter Emitter(BB, InsertPos);
773 DenseMap<SDValue, unsigned> VRBaseMap;
774
775 DEBUG({
776 dbgs() << "\n*** Final schedule ***\n";
777 });
778
779 // FIXME: Handle dbg_values.
780 unsigned NumNodes = Sequence.size();
781 for (unsigned i = 0; i != NumNodes; ++i) {
782 SDNode *N = Sequence[NumNodes-i-1];
783 DEBUG(N->dump(DAG));
784 Emitter.EmitNode(N, false, false, VRBaseMap);
785 }
786
787 DEBUG(dbgs() << '\n');
788
789 InsertPos = Emitter.getInsertPos();
790 return Emitter.getBlock();
791}
792
Dan Gohman95be7d72008-09-18 16:26:26 +0000793//===----------------------------------------------------------------------===//
794// Public Constructor Functions
795//===----------------------------------------------------------------------===//
796
Dan Gohmandfaf6462009-02-11 04:27:20 +0000797llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +0000798llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +0000799 return new ScheduleDAGFast(*IS->MF);
Dan Gohman95be7d72008-09-18 16:26:26 +0000800}
Evan Cheng839fb652012-10-17 19:39:36 +0000801
802llvm::ScheduleDAGSDNodes *
803llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) {
804 return new ScheduleDAGLinearize(*IS->MF);
805}