blob: 698e14453d1d7245e1cf662663d4c48d2ca5354a [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "InstrEmitter.h"
15#include "ScheduleDAGSDNodes.h"
16#include "llvm/ADT/STLExtras.h"
Dan Gohman95be7d72008-09-18 16:26:26 +000017#include "llvm/ADT/SmallSet.h"
18#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/SelectionDAGISel.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000021#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/InlineAsm.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000027#include "llvm/Support/raw_ostream.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
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000163 for (SDep &Pred : SU->Preds) {
164 ReleasePred(SU, &Pred);
165 if (Pred.isAssignedRegDep()) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000166 // This is a physical register dependency and it's impossible or
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000167 // expensive to copy the register. Make sure nothing that can
Dan Gohman95be7d72008-09-18 16:26:26 +0000168 // clobber the register is scheduled between the predecessor and
169 // this node.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000170 if (!LiveRegDefs[Pred.getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000171 ++NumLiveRegs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000172 LiveRegDefs[Pred.getReg()] = Pred.getSUnit();
173 LiveRegCycles[Pred.getReg()] = CurCycle;
Dan Gohman95be7d72008-09-18 16:26:26 +0000174 }
175 }
176 }
Dan Gohmanb9543432009-02-10 23:27:53 +0000177}
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.
182void ScheduleDAGFast::ScheduleNodeBottomUp(SUnit *SU, unsigned CurCycle) {
David Greened65bc152010-01-05 01:25:09 +0000183 DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
Dan Gohmanb9543432009-02-10 23:27:53 +0000184 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 Gohman95be7d72008-09-18 16:26:26 +0000191
192 // Release all the implicit physical register defs that are live.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000193 for (SDep &Succ : SU->Succs) {
194 if (Succ.isAssignedRegDep()) {
195 if (LiveRegCycles[Succ.getReg()] == Succ.getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000196 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000197 assert(LiveRegDefs[Succ.getReg()] == SU &&
Dan Gohman95be7d72008-09-18 16:26:26 +0000198 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000199 --NumLiveRegs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000200 LiveRegDefs[Succ.getReg()] = nullptr;
201 LiveRegCycles[Succ.getReg()] = 0;
Dan Gohman95be7d72008-09-18 16:26:26 +0000202 }
203 }
204 }
205
206 SU->isScheduled = true;
207}
208
Dan Gohman95be7d72008-09-18 16:26:26 +0000209/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
210/// successors to the newly created node.
211SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Chris Lattner11a33812010-12-23 17:24:32 +0000212 if (SU->getNode()->getGluedNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000213 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000214
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000215 SDNode *N = SU->getNode();
Dan Gohman95be7d72008-09-18 16:26:26 +0000216 if (!N)
Craig Topperc0196b12014-04-14 00:51:57 +0000217 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000218
219 SUnit *NewSU;
220 bool TryUnfold = false;
221 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Craig Topper7f416c82014-11-16 21:17:18 +0000222 MVT VT = N->getSimpleValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000223 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000224 return nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000225 else if (VT == MVT::Other)
Dan Gohman95be7d72008-09-18 16:26:26 +0000226 TryUnfold = true;
227 }
Pete Cooper9271ccc2015-06-26 19:18:49 +0000228 for (const SDValue &Op : N->op_values()) {
Craig Topper7f416c82014-11-16 21:17:18 +0000229 MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000230 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000231 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000232 }
233
234 if (TryUnfold) {
235 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000236 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Craig Topperc0196b12014-04-14 00:51:57 +0000237 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000238
David Greened65bc152010-01-05 01:25:09 +0000239 DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000240 assert(NewNodes.size() == 2 && "Expected a load folding node!");
241
242 N = NewNodes[1];
243 SDNode *LoadNode = NewNodes[0];
244 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000245 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohman95be7d72008-09-18 16:26:26 +0000246 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000247 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
248 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000249 SDValue(LoadNode, 1));
Dan Gohman95be7d72008-09-18 16:26:26 +0000250
Andrew Trick52226d42012-03-07 23:00:49 +0000251 SUnit *NewSU = newSUnit(N);
Dan Gohman95be7d72008-09-18 16:26:26 +0000252 assert(N->getNodeId() == -1 && "Node already inserted!");
253 N->setNodeId(NewSU->NodeNum);
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000254
Evan Cheng6cc775f2011-06-28 19:10:37 +0000255 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
256 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
257 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000258 NewSU->isTwoAddress = true;
259 break;
260 }
261 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000262 if (MCID.isCommutable())
Dan Gohman95be7d72008-09-18 16:26:26 +0000263 NewSU->isCommutable = true;
Dan Gohman95be7d72008-09-18 16:26:26 +0000264
265 // LoadNode may already exist. This can happen when there is another
266 // load from the same location and producing the same type of value
267 // but it has different alignment or volatileness.
268 bool isNewLoad = true;
269 SUnit *LoadSU;
270 if (LoadNode->getNodeId() != -1) {
271 LoadSU = &SUnits[LoadNode->getNodeId()];
272 isNewLoad = false;
273 } else {
Andrew Trick52226d42012-03-07 23:00:49 +0000274 LoadSU = newSUnit(LoadNode);
Dan Gohman95be7d72008-09-18 16:26:26 +0000275 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohman95be7d72008-09-18 16:26:26 +0000276 }
277
Dan Gohman2d170892008-12-09 22:54:47 +0000278 SDep ChainPred;
Dan Gohman95be7d72008-09-18 16:26:26 +0000279 SmallVector<SDep, 4> ChainSuccs;
280 SmallVector<SDep, 4> LoadPreds;
281 SmallVector<SDep, 4> NodePreds;
282 SmallVector<SDep, 4> NodeSuccs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000283 for (SDep &Pred : SU->Preds) {
284 if (Pred.isCtrl())
285 ChainPred = Pred;
286 else if (Pred.getSUnit()->getNode() &&
287 Pred.getSUnit()->getNode()->isOperandOf(LoadNode))
288 LoadPreds.push_back(Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000289 else
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000290 NodePreds.push_back(Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000291 }
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000292 for (SDep &Succ : SU->Succs) {
293 if (Succ.isCtrl())
294 ChainSuccs.push_back(Succ);
Dan Gohman95be7d72008-09-18 16:26:26 +0000295 else
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000296 NodeSuccs.push_back(Succ);
Dan Gohman95be7d72008-09-18 16:26:26 +0000297 }
298
Dan Gohman2d170892008-12-09 22:54:47 +0000299 if (ChainPred.getSUnit()) {
300 RemovePred(SU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000301 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000302 AddPred(LoadSU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000303 }
304 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000305 const SDep &Pred = LoadPreds[i];
306 RemovePred(SU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000307 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000308 AddPred(LoadSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000309 }
310 }
311 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000312 const SDep &Pred = NodePreds[i];
313 RemovePred(SU, Pred);
314 AddPred(NewSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000315 }
316 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000317 SDep D = NodeSuccs[i];
318 SUnit *SuccDep = D.getSUnit();
319 D.setSUnit(SU);
320 RemovePred(SuccDep, D);
321 D.setSUnit(NewSU);
322 AddPred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000323 }
324 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000325 SDep D = ChainSuccs[i];
326 SUnit *SuccDep = D.getSUnit();
327 D.setSUnit(SU);
328 RemovePred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000329 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000330 D.setSUnit(LoadSU);
331 AddPred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000332 }
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000333 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000334 if (isNewLoad) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000335 SDep D(LoadSU, SDep::Barrier);
336 D.setLatency(LoadSU->Latency);
337 AddPred(NewSU, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000338 }
339
340 ++NumUnfolds;
341
342 if (NewSU->NumSuccsLeft == 0) {
343 NewSU->isAvailable = true;
344 return NewSU;
345 }
346 SU = NewSU;
347 }
348
David Greened65bc152010-01-05 01:25:09 +0000349 DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Dan Gohman4c3034f2008-11-19 23:39:02 +0000350 NewSU = Clone(SU);
Dan Gohman95be7d72008-09-18 16:26:26 +0000351
352 // New SUnit has the exact same predecessors.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000353 for (SDep &Pred : SU->Preds)
354 if (!Pred.isArtificial())
355 AddPred(NewSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000356
357 // Only copy scheduled successors. Cut them from old node's successor
358 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000359 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000360 for (SDep &Succ : SU->Succs) {
361 if (Succ.isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000362 continue;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000363 SUnit *SuccSU = Succ.getSUnit();
Dan Gohman2d170892008-12-09 22:54:47 +0000364 if (SuccSU->isScheduled) {
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000365 SDep D = Succ;
Dan Gohman2d170892008-12-09 22:54:47 +0000366 D.setSUnit(NewSU);
367 AddPred(SuccSU, D);
368 D.setSUnit(SU);
369 DelDeps.push_back(std::make_pair(SuccSU, D));
Dan Gohman95be7d72008-09-18 16:26:26 +0000370 }
371 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000372 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000373 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000374
375 ++NumDups;
376 return NewSU;
377}
378
Evan Chengb2c42c62009-01-12 03:19:55 +0000379/// InsertCopiesAndMoveSuccs - Insert register copies and move all
380/// scheduled successors of the given SUnit to the last copy.
381void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000382 const TargetRegisterClass *DestRC,
383 const TargetRegisterClass *SrcRC,
Craig Topperb94011f2013-07-14 04:42:23 +0000384 SmallVectorImpl<SUnit*> &Copies) {
Craig Topperc0196b12014-04-14 00:51:57 +0000385 SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000386 CopyFromSU->CopySrcRC = SrcRC;
387 CopyFromSU->CopyDstRC = DestRC;
388
Craig Topperc0196b12014-04-14 00:51:57 +0000389 SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000390 CopyToSU->CopySrcRC = DestRC;
391 CopyToSU->CopyDstRC = SrcRC;
392
393 // Only copy scheduled successors. Cut them from old node's successor
394 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000395 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000396 for (SDep &Succ : SU->Succs) {
397 if (Succ.isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000398 continue;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000399 SUnit *SuccSU = Succ.getSUnit();
Dan Gohman2d170892008-12-09 22:54:47 +0000400 if (SuccSU->isScheduled) {
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000401 SDep D = Succ;
Dan Gohman2d170892008-12-09 22:54:47 +0000402 D.setSUnit(CopyToSU);
403 AddPred(SuccSU, D);
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000404 DelDeps.push_back(std::make_pair(SuccSU, Succ));
Dan Gohman95be7d72008-09-18 16:26:26 +0000405 }
406 }
407 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000408 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000409 }
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000410 SDep FromDep(SU, SDep::Data, Reg);
411 FromDep.setLatency(SU->Latency);
412 AddPred(CopyFromSU, FromDep);
413 SDep ToDep(CopyFromSU, SDep::Data, 0);
414 ToDep.setLatency(CopyFromSU->Latency);
415 AddPred(CopyToSU, ToDep);
Dan Gohman95be7d72008-09-18 16:26:26 +0000416
417 Copies.push_back(CopyFromSU);
418 Copies.push_back(CopyToSU);
419
Evan Chengb2c42c62009-01-12 03:19:55 +0000420 ++NumPRCopies;
Dan Gohman95be7d72008-09-18 16:26:26 +0000421}
422
423/// getPhysicalRegisterVT - Returns the ValueType of the physical register
424/// definition of the specified node.
425/// FIXME: Move to SelectionDAG?
Craig Topper7f416c82014-11-16 21:17:18 +0000426static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000427 const TargetInstrInfo *TII) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000428 unsigned NumRes;
429 if (N->getOpcode() == ISD::CopyFromReg) {
430 // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
431 NumRes = 1;
432 } else {
433 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
434 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
435 NumRes = MCID.getNumDefs();
Craig Toppere5e035a32015-12-05 07:13:35 +0000436 for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000437 if (Reg == *ImpDef)
438 break;
439 ++NumRes;
440 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000441 }
Craig Topper7f416c82014-11-16 21:17:18 +0000442 return N->getSimpleValueType(NumRes);
Dan Gohman95be7d72008-09-18 16:26:26 +0000443}
444
Dale Johannesen16f96442010-08-17 22:17:24 +0000445/// CheckForLiveRegDef - Return true and update live register vector if the
446/// specified register def of the specified SUnit clobbers any "live" registers.
447static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
448 std::vector<SUnit*> &LiveRegDefs,
449 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +0000450 SmallVectorImpl<unsigned> &LRegs,
Dale Johannesen16f96442010-08-17 22:17:24 +0000451 const TargetRegisterInfo *TRI) {
452 bool Added = false;
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000453 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
454 if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) {
David Blaikie70573dc2014-11-19 07:49:26 +0000455 if (RegAdded.insert(*AI).second) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000456 LRegs.push_back(*AI);
Dale Johannesen16f96442010-08-17 22:17:24 +0000457 Added = true;
458 }
459 }
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000460 }
Dale Johannesen16f96442010-08-17 22:17:24 +0000461 return Added;
462}
463
Dan Gohman95be7d72008-09-18 16:26:26 +0000464/// DelayForLiveRegsBottomUp - Returns true if it is necessary to delay
465/// scheduling of the given node to satisfy live physical register dependencies.
466/// If the specific node is the last one that's available to schedule, do
467/// whatever is necessary (i.e. backtracking or cloning) to make it possible.
468bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
Craig Topperb94011f2013-07-14 04:42:23 +0000469 SmallVectorImpl<unsigned> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000470 if (NumLiveRegs == 0)
Dan Gohman95be7d72008-09-18 16:26:26 +0000471 return false;
472
473 SmallSet<unsigned, 4> RegAdded;
474 // If this node would clobber any "live" register, then it's not ready.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000475 for (SDep &Pred : SU->Preds) {
476 if (Pred.isAssignedRegDep()) {
477 CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs,
Dale Johannesen16f96442010-08-17 22:17:24 +0000478 RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000479 }
480 }
481
Chris Lattner11a33812010-12-23 17:24:32 +0000482 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000483 if (Node->getOpcode() == ISD::INLINEASM) {
484 // Inline asm can clobber physical defs.
485 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000486 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +0000487 --NumOps; // Ignore the glue operand.
Dale Johannesen16f96442010-08-17 22:17:24 +0000488
489 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
490 unsigned Flags =
491 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
492 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
493
494 ++i; // Skip the ID value.
495 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +0000496 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
497 InlineAsm::isClobberKind(Flags)) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000498 // Check for def of register or earlyclobber register.
499 for (; NumVals; --NumVals, ++i) {
500 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
501 if (TargetRegisterInfo::isPhysicalRegister(Reg))
502 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
503 }
504 } else
505 i += NumVals;
506 }
507 continue;
508 }
Dan Gohman072734e2008-11-13 23:24:17 +0000509 if (!Node->isMachineOpcode())
Dan Gohman95be7d72008-09-18 16:26:26 +0000510 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000511 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
512 if (!MCID.ImplicitDefs)
Dan Gohman95be7d72008-09-18 16:26:26 +0000513 continue;
Craig Toppere5e035a32015-12-05 07:13:35 +0000514 for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000515 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000516 }
517 }
518 return !LRegs.empty();
519}
520
521
522/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
523/// schedulers.
524void ScheduleDAGFast::ListScheduleBottomUp() {
525 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000526
527 // Release any predecessors of the special Exit node.
528 ReleasePredecessors(&ExitSU, CurCycle);
529
Dan Gohman95be7d72008-09-18 16:26:26 +0000530 // Add root to Available queue.
531 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000532 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman95be7d72008-09-18 16:26:26 +0000533 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
534 RootSU->isAvailable = true;
535 AvailableQueue.push(RootSU);
536 }
537
538 // While Available queue is not empty, grab the node with the highest
539 // priority. If it is not ready put it back. Schedule the node.
540 SmallVector<SUnit*, 4> NotReady;
541 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
542 Sequence.reserve(SUnits.size());
543 while (!AvailableQueue.empty()) {
544 bool Delayed = false;
545 LRegsMap.clear();
546 SUnit *CurSU = AvailableQueue.pop();
547 while (CurSU) {
Dan Gohman4f474b02008-11-17 19:52:36 +0000548 SmallVector<unsigned, 4> LRegs;
549 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
550 break;
551 Delayed = true;
552 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohman95be7d72008-09-18 16:26:26 +0000553
554 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
555 NotReady.push_back(CurSU);
556 CurSU = AvailableQueue.pop();
557 }
558
559 // All candidates are delayed due to live physical reg dependencies.
560 // Try code duplication or inserting cross class copies
561 // to resolve it.
562 if (Delayed && !CurSU) {
563 if (!CurSU) {
564 // Try duplicating the nodes that produces these
565 // "expensive to copy" values to break the dependency. In case even
566 // that doesn't work, insert cross class copies.
567 SUnit *TrySU = NotReady[0];
Craig Topperb94011f2013-07-14 04:42:23 +0000568 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Dan Gohman95be7d72008-09-18 16:26:26 +0000569 assert(LRegs.size() == 1 && "Can't handle this yet!");
570 unsigned Reg = LRegs[0];
571 SUnit *LRDef = LiveRegDefs[Reg];
Craig Topper7f416c82014-11-16 21:17:18 +0000572 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000573 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000574 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000575 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
576
Evan Chengb4c6a342011-03-10 00:16:32 +0000577 // If cross copy register class is the same as RC, then it must be
578 // possible copy the value directly. Do not try duplicate the def.
579 // If cross copy register class is not the same as RC, then it's
580 // possible to copy the value but it require cross register class copies
581 // and it is expensive.
582 // If cross copy register class is null, then it's not possible to copy
583 // the value at all.
Craig Topperc0196b12014-04-14 00:51:57 +0000584 SUnit *NewDef = nullptr;
Evan Chengb4c6a342011-03-10 00:16:32 +0000585 if (DestRC != RC) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000586 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +0000587 if (!DestRC && !NewDef)
588 report_fatal_error("Can't handle live physical "
589 "register dependency!");
590 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000591 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000592 // Issue copies, these can be expensive cross register class copies.
Dan Gohman95be7d72008-09-18 16:26:26 +0000593 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000594 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
David Greened65bc152010-01-05 01:25:09 +0000595 DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000596 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000597 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000598 NewDef = Copies.back();
599 }
600
David Greened65bc152010-01-05 01:25:09 +0000601 DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000602 << " to SU #" << TrySU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000603 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000604 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000605 TrySU->isAvailable = false;
606 CurSU = NewDef;
607 }
608
609 if (!CurSU) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000610 llvm_unreachable("Unable to resolve live physical register dependencies!");
Dan Gohman95be7d72008-09-18 16:26:26 +0000611 }
612 }
613
614 // Add the nodes that aren't ready back onto the available list.
615 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
616 NotReady[i]->isPending = false;
617 // May no longer be available due to backtracking.
618 if (NotReady[i]->isAvailable)
619 AvailableQueue.push(NotReady[i]);
620 }
621 NotReady.clear();
622
Dan Gohmanc602dd42008-11-21 00:10:42 +0000623 if (CurSU)
Dan Gohman95be7d72008-09-18 16:26:26 +0000624 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000625 ++CurCycle;
626 }
627
Dan Gohman6905f152009-09-28 16:09:41 +0000628 // Reverse the order since it is bottom up.
Dan Gohman95be7d72008-09-18 16:26:26 +0000629 std::reverse(Sequence.begin(), Sequence.end());
Dan Gohman6905f152009-09-28 16:09:41 +0000630
Dan Gohman95be7d72008-09-18 16:26:26 +0000631#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000632 VerifyScheduledSequence(/*isBottomUp=*/true);
Dan Gohman95be7d72008-09-18 16:26:26 +0000633#endif
634}
635
Evan Cheng839fb652012-10-17 19:39:36 +0000636
Benjamin Kramera74129a2012-10-20 12:53:26 +0000637namespace {
Evan Cheng839fb652012-10-17 19:39:36 +0000638//===----------------------------------------------------------------------===//
639// ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
640// DAG in topological order.
641// IMPORTANT: this may not work for targets with phyreg dependency.
642//
643class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
644public:
645 ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
646
Craig Topper7b883b32014-03-08 06:31:39 +0000647 void Schedule() override;
Evan Cheng839fb652012-10-17 19:39:36 +0000648
Craig Topper7b883b32014-03-08 06:31:39 +0000649 MachineBasicBlock *
650 EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
Evan Cheng839fb652012-10-17 19:39:36 +0000651
652private:
653 std::vector<SDNode*> Sequence;
654 DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user
655
656 void ScheduleNode(SDNode *N);
657};
Benjamin Kramera74129a2012-10-20 12:53:26 +0000658} // end anonymous namespace
Evan Cheng839fb652012-10-17 19:39:36 +0000659
660void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
661 if (N->getNodeId() != 0)
Craig Topperc0196b12014-04-14 00:51:57 +0000662 llvm_unreachable(nullptr);
Evan Cheng839fb652012-10-17 19:39:36 +0000663
664 if (!N->isMachineOpcode() &&
665 (N->getOpcode() == ISD::EntryToken || isPassiveNode(N)))
666 // These nodes do not need to be translated into MIs.
667 return;
668
669 DEBUG(dbgs() << "\n*** Scheduling: ");
670 DEBUG(N->dump(DAG));
671 Sequence.push_back(N);
672
673 unsigned NumOps = N->getNumOperands();
674 if (unsigned NumLeft = NumOps) {
Craig Topperc0196b12014-04-14 00:51:57 +0000675 SDNode *GluedOpN = nullptr;
Evan Cheng839fb652012-10-17 19:39:36 +0000676 do {
677 const SDValue &Op = N->getOperand(NumLeft-1);
678 SDNode *OpN = Op.getNode();
679
680 if (NumLeft == NumOps && Op.getValueType() == MVT::Glue) {
681 // Schedule glue operand right above N.
682 GluedOpN = OpN;
683 assert(OpN->getNodeId() != 0 && "Glue operand not ready?");
684 OpN->setNodeId(0);
685 ScheduleNode(OpN);
686 continue;
687 }
688
689 if (OpN == GluedOpN)
690 // Glue operand is already scheduled.
691 continue;
692
693 DenseMap<SDNode*, SDNode*>::iterator DI = GluedMap.find(OpN);
694 if (DI != GluedMap.end() && DI->second != N)
695 // Users of glues are counted against the glued users.
696 OpN = DI->second;
697
698 unsigned Degree = OpN->getNodeId();
699 assert(Degree > 0 && "Predecessor over-released!");
700 OpN->setNodeId(--Degree);
701 if (Degree == 0)
702 ScheduleNode(OpN);
703 } while (--NumLeft);
704 }
705}
706
707/// findGluedUser - Find the representative use of a glue value by walking
708/// the use chain.
709static SDNode *findGluedUser(SDNode *N) {
710 while (SDNode *Glued = N->getGluedUser())
711 N = Glued;
712 return N;
713}
714
715void ScheduleDAGLinearize::Schedule() {
716 DEBUG(dbgs() << "********** DAG Linearization **********\n");
717
718 SmallVector<SDNode*, 8> Glues;
719 unsigned DAGSize = 0;
Pete Cooper65c69402015-07-14 22:10:54 +0000720 for (SDNode &Node : DAG->allnodes()) {
721 SDNode *N = &Node;
Evan Cheng839fb652012-10-17 19:39:36 +0000722
723 // Use node id to record degree.
724 unsigned Degree = N->use_size();
725 N->setNodeId(Degree);
726 unsigned NumVals = N->getNumValues();
727 if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
728 N->hasAnyUseOfValue(NumVals-1)) {
729 SDNode *User = findGluedUser(N);
730 if (User) {
731 Glues.push_back(N);
732 GluedMap.insert(std::make_pair(N, User));
733 }
734 }
735
736 if (N->isMachineOpcode() ||
737 (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
738 ++DAGSize;
739 }
740
741 for (unsigned i = 0, e = Glues.size(); i != e; ++i) {
742 SDNode *Glue = Glues[i];
743 SDNode *GUser = GluedMap[Glue];
744 unsigned Degree = Glue->getNodeId();
745 unsigned UDegree = GUser->getNodeId();
746
747 // Glue user must be scheduled together with the glue operand. So other
748 // users of the glue operand must be treated as its users.
749 SDNode *ImmGUser = Glue->getGluedUser();
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000750 for (const SDNode *U : Glue->uses())
751 if (U == ImmGUser)
Evan Cheng839fb652012-10-17 19:39:36 +0000752 --Degree;
753 GUser->setNodeId(UDegree + Degree);
754 Glue->setNodeId(1);
755 }
756
757 Sequence.reserve(DAGSize);
758 ScheduleNode(DAG->getRoot().getNode());
759}
760
761MachineBasicBlock*
762ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
763 InstrEmitter Emitter(BB, InsertPos);
764 DenseMap<SDValue, unsigned> VRBaseMap;
765
766 DEBUG({
767 dbgs() << "\n*** Final schedule ***\n";
768 });
769
770 // FIXME: Handle dbg_values.
771 unsigned NumNodes = Sequence.size();
772 for (unsigned i = 0; i != NumNodes; ++i) {
773 SDNode *N = Sequence[NumNodes-i-1];
774 DEBUG(N->dump(DAG));
775 Emitter.EmitNode(N, false, false, VRBaseMap);
776 }
777
778 DEBUG(dbgs() << '\n');
779
780 InsertPos = Emitter.getInsertPos();
781 return Emitter.getBlock();
782}
783
Dan Gohman95be7d72008-09-18 16:26:26 +0000784//===----------------------------------------------------------------------===//
785// Public Constructor Functions
786//===----------------------------------------------------------------------===//
787
Dan Gohmandfaf6462009-02-11 04:27:20 +0000788llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +0000789llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +0000790 return new ScheduleDAGFast(*IS->MF);
Dan Gohman95be7d72008-09-18 16:26:26 +0000791}
Evan Cheng839fb652012-10-17 19:39:36 +0000792
793llvm::ScheduleDAGSDNodes *
794llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) {
795 return new ScheduleDAGLinearize(*IS->MF);
796}