blob: 3944d7df286dc36d21cf90d7badd2e566435d604 [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"
Adrian Prantldfe15f32018-03-02 22:59:51 +000016#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#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 Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/SelectionDAGISel.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000022#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000023#include "llvm/CodeGen/TargetRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/InlineAsm.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000028#include "llvm/Support/raw_ostream.h"
Dan Gohman95be7d72008-09-18 16:26:26 +000029using namespace llvm;
30
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031#define DEBUG_TYPE "pre-RA-sched"
32
Dan Gohman95be7d72008-09-18 16:26:26 +000033STATISTIC(NumUnfolds, "Number of nodes unfolded");
34STATISTIC(NumDups, "Number of duplicated nodes");
Evan Chengb2c42c62009-01-12 03:19:55 +000035STATISTIC(NumPRCopies, "Number of physical copies");
Dan Gohman95be7d72008-09-18 16:26:26 +000036
37static RegisterScheduler
Dan Gohman9c4b7d52008-10-14 20:25:08 +000038 fastDAGScheduler("fast", "Fast suboptimal list scheduling",
Dan Gohman95be7d72008-09-18 16:26:26 +000039 createFastDAGScheduler);
Evan Cheng839fb652012-10-17 19:39:36 +000040static RegisterScheduler
41 linearizeDAGScheduler("linearize", "Linearize DAG, no scheduling",
42 createDAGLinearizer);
43
Dan Gohman95be7d72008-09-18 16:26:26 +000044
45namespace {
46 /// FastPriorityQueue - A degenerate priority queue that considers
47 /// all nodes to have the same priority.
48 ///
Nick Lewycky02d5f772009-10-25 06:33:48 +000049 struct FastPriorityQueue {
Dan Gohmanc07f6862008-09-23 18:50:48 +000050 SmallVector<SUnit *, 16> Queue;
Dan Gohman95be7d72008-09-18 16:26:26 +000051
52 bool empty() const { return Queue.empty(); }
Andrew Trick7c6c41a2012-03-07 05:21:32 +000053
Dan Gohman95be7d72008-09-18 16:26:26 +000054 void push(SUnit *U) {
55 Queue.push_back(U);
56 }
57
58 SUnit *pop() {
Craig Topperc0196b12014-04-14 00:51:57 +000059 if (empty()) return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +000060 SUnit *V = Queue.back();
61 Queue.pop_back();
62 return V;
63 }
64 };
65
66//===----------------------------------------------------------------------===//
67/// ScheduleDAGFast - The actual "fast" list scheduler implementation.
68///
Nick Lewycky02d5f772009-10-25 06:33:48 +000069class ScheduleDAGFast : public ScheduleDAGSDNodes {
Dan Gohman95be7d72008-09-18 16:26:26 +000070private:
71 /// AvailableQueue - The priority queue to use for the available SUnits.
72 FastPriorityQueue AvailableQueue;
73
Dan Gohmanc07f6862008-09-23 18:50:48 +000074 /// LiveRegDefs - A set of physical registers and their definition
Dan Gohman95be7d72008-09-18 16:26:26 +000075 /// that are "live". These nodes must be scheduled before any other nodes that
76 /// modifies the registers can be scheduled.
Dan Gohmanc07f6862008-09-23 18:50:48 +000077 unsigned NumLiveRegs;
Dan Gohman95be7d72008-09-18 16:26:26 +000078 std::vector<SUnit*> LiveRegDefs;
79 std::vector<unsigned> LiveRegCycles;
80
81public:
Dan Gohman619ef482009-01-15 19:20:50 +000082 ScheduleDAGFast(MachineFunction &mf)
83 : ScheduleDAGSDNodes(mf) {}
Dan Gohman95be7d72008-09-18 16:26:26 +000084
Craig Topper7b883b32014-03-08 06:31:39 +000085 void Schedule() override;
Dan Gohman95be7d72008-09-18 16:26:26 +000086
Dan Gohman2d170892008-12-09 22:54:47 +000087 /// AddPred - adds a predecessor edge to SUnit SU.
Dan Gohman95be7d72008-09-18 16:26:26 +000088 /// This returns true if this is a new predecessor.
Dan Gohman17214e62008-12-16 01:00:55 +000089 void AddPred(SUnit *SU, const SDep &D) {
90 SU->addPred(D);
Dan Gohman2d170892008-12-09 22:54:47 +000091 }
Dan Gohman95be7d72008-09-18 16:26:26 +000092
Dan Gohman2d170892008-12-09 22:54:47 +000093 /// RemovePred - removes a predecessor edge from SUnit SU.
94 /// This returns true if an edge was removed.
Dan Gohman17214e62008-12-16 01:00:55 +000095 void RemovePred(SUnit *SU, const SDep &D) {
96 SU->removePred(D);
Dan Gohman2d170892008-12-09 22:54:47 +000097 }
Dan Gohman95be7d72008-09-18 16:26:26 +000098
99private:
Dan Gohman2d170892008-12-09 22:54:47 +0000100 void ReleasePred(SUnit *SU, SDep *PredEdge);
Dan Gohmanb9543432009-02-10 23:27:53 +0000101 void ReleasePredecessors(SUnit *SU, unsigned CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000102 void ScheduleNodeBottomUp(SUnit*, unsigned);
103 SUnit *CopyAndMoveSuccessors(SUnit*);
Evan Chengb2c42c62009-01-12 03:19:55 +0000104 void InsertCopiesAndMoveSuccs(SUnit*, unsigned,
105 const TargetRegisterClass*,
106 const TargetRegisterClass*,
Craig Topperb94011f2013-07-14 04:42:23 +0000107 SmallVectorImpl<SUnit*>&);
108 bool DelayForLiveRegsBottomUp(SUnit*, SmallVectorImpl<unsigned>&);
Dan Gohman95be7d72008-09-18 16:26:26 +0000109 void ListScheduleBottomUp();
Dan Gohmandddc1ac2008-12-16 03:25:46 +0000110
Andrew Trick52226d42012-03-07 23:00:49 +0000111 /// forceUnitLatencies - The fast scheduler doesn't care about real latencies.
Craig Topper7b883b32014-03-08 06:31:39 +0000112 bool forceUnitLatencies() const override { return true; }
Dan Gohman95be7d72008-09-18 16:26:26 +0000113};
114} // end anonymous namespace
115
116
117/// Schedule - Schedule the DAG using list scheduling.
118void ScheduleDAGFast::Schedule() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000119 LLVM_DEBUG(dbgs() << "********** List Scheduling **********\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000120
Dan Gohmanc07f6862008-09-23 18:50:48 +0000121 NumLiveRegs = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000122 LiveRegDefs.resize(TRI->getNumRegs(), nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000123 LiveRegCycles.resize(TRI->getNumRegs(), 0);
124
Dan Gohman04543e72008-12-23 18:36:58 +0000125 // Build the scheduling graph.
Craig Topperc0196b12014-04-14 00:51:57 +0000126 BuildSchedGraph(nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000127
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000128 LLVM_DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) SUnits[su]
129 .dumpAll(this));
Dan Gohman95be7d72008-09-18 16:26:26 +0000130
131 // Execute the actual scheduling loop.
132 ListScheduleBottomUp();
133}
134
135//===----------------------------------------------------------------------===//
136// Bottom-Up Scheduling
137//===----------------------------------------------------------------------===//
138
139/// ReleasePred - Decrement the NumSuccsLeft count of a predecessor. Add it to
140/// the AvailableQueue if the count reaches zero. Also update its cycle bound.
Dan Gohman2d170892008-12-09 22:54:47 +0000141void ScheduleDAGFast::ReleasePred(SUnit *SU, SDep *PredEdge) {
142 SUnit *PredSU = PredEdge->getSUnit();
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000143
Dan Gohman95be7d72008-09-18 16:26:26 +0000144#ifndef NDEBUG
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000145 if (PredSU->NumSuccsLeft == 0) {
David Greened65bc152010-01-05 01:25:09 +0000146 dbgs() << "*** Scheduling failed! ***\n";
Dan Gohman22d07b12008-11-18 02:06:40 +0000147 PredSU->dump(this);
David Greened65bc152010-01-05 01:25:09 +0000148 dbgs() << " has been released too many times!\n";
Craig Topperc0196b12014-04-14 00:51:57 +0000149 llvm_unreachable(nullptr);
Dan Gohman95be7d72008-09-18 16:26:26 +0000150 }
151#endif
Reid Kleckner8ff5c192009-09-30 20:15:38 +0000152 --PredSU->NumSuccsLeft;
153
Dan Gohmanb9543432009-02-10 23:27:53 +0000154 // If all the node's successors are scheduled, this node is ready
155 // to be scheduled. Ignore the special EntrySU node.
156 if (PredSU->NumSuccsLeft == 0 && PredSU != &EntrySU) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000157 PredSU->isAvailable = true;
158 AvailableQueue.push(PredSU);
159 }
160}
161
Dan Gohmanb9543432009-02-10 23:27:53 +0000162void ScheduleDAGFast::ReleasePredecessors(SUnit *SU, unsigned CurCycle) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000163 // Bottom up: release predecessors
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000164 for (SDep &Pred : SU->Preds) {
165 ReleasePred(SU, &Pred);
166 if (Pred.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.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000171 if (!LiveRegDefs[Pred.getReg()]) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000172 ++NumLiveRegs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000173 LiveRegDefs[Pred.getReg()] = Pred.getSUnit();
174 LiveRegCycles[Pred.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) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000184 LLVM_DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
185 LLVM_DEBUG(SU->dump(this));
Dan Gohmanb9543432009-02-10 23:27:53 +0000186
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.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000194 for (SDep &Succ : SU->Succs) {
195 if (Succ.isAssignedRegDep()) {
196 if (LiveRegCycles[Succ.getReg()] == Succ.getSUnit()->getHeight()) {
Dan Gohmanc07f6862008-09-23 18:50:48 +0000197 assert(NumLiveRegs > 0 && "NumLiveRegs is already zero!");
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000198 assert(LiveRegDefs[Succ.getReg()] == SU &&
Dan Gohman95be7d72008-09-18 16:26:26 +0000199 "Physical register dependency violated?");
Dan Gohmanc07f6862008-09-23 18:50:48 +0000200 --NumLiveRegs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000201 LiveRegDefs[Succ.getReg()] = nullptr;
202 LiveRegCycles[Succ.getReg()] = 0;
Dan Gohman95be7d72008-09-18 16:26:26 +0000203 }
204 }
205 }
206
207 SU->isScheduled = true;
208}
209
Dan Gohman95be7d72008-09-18 16:26:26 +0000210/// CopyAndMoveSuccessors - Clone the specified node and move its scheduled
211/// successors to the newly created node.
212SUnit *ScheduleDAGFast::CopyAndMoveSuccessors(SUnit *SU) {
Chris Lattner11a33812010-12-23 17:24:32 +0000213 if (SU->getNode()->getGluedNode())
Craig Topperc0196b12014-04-14 00:51:57 +0000214 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000215
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000216 SDNode *N = SU->getNode();
Dan Gohman95be7d72008-09-18 16:26:26 +0000217 if (!N)
Craig Topperc0196b12014-04-14 00:51:57 +0000218 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000219
220 SUnit *NewSU;
221 bool TryUnfold = false;
222 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Craig Topper7f416c82014-11-16 21:17:18 +0000223 MVT VT = N->getSimpleValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000224 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000225 return nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000226 else if (VT == MVT::Other)
Dan Gohman95be7d72008-09-18 16:26:26 +0000227 TryUnfold = true;
228 }
Pete Cooper9271ccc2015-06-26 19:18:49 +0000229 for (const SDValue &Op : N->op_values()) {
Craig Topper7f416c82014-11-16 21:17:18 +0000230 MVT VT = Op.getNode()->getSimpleValueType(Op.getResNo());
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000231 if (VT == MVT::Glue)
Craig Topperc0196b12014-04-14 00:51:57 +0000232 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000233 }
234
235 if (TryUnfold) {
236 SmallVector<SDNode*, 2> NewNodes;
Dan Gohman5a390b92008-11-13 21:21:28 +0000237 if (!TII->unfoldMemoryOperand(*DAG, N, NewNodes))
Craig Topperc0196b12014-04-14 00:51:57 +0000238 return nullptr;
Dan Gohman95be7d72008-09-18 16:26:26 +0000239
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000240 LLVM_DEBUG(dbgs() << "Unfolding SU # " << SU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000241 assert(NewNodes.size() == 2 && "Expected a load folding node!");
242
243 N = NewNodes[1];
244 SDNode *LoadNode = NewNodes[0];
245 unsigned NumVals = N->getNumValues();
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000246 unsigned OldNumVals = SU->getNode()->getNumValues();
Dan Gohman95be7d72008-09-18 16:26:26 +0000247 for (unsigned i = 0; i != NumVals; ++i)
Dan Gohman1ddfcba2008-11-13 21:36:12 +0000248 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), i), SDValue(N, i));
249 DAG->ReplaceAllUsesOfValueWith(SDValue(SU->getNode(), OldNumVals-1),
Dan Gohman5a390b92008-11-13 21:21:28 +0000250 SDValue(LoadNode, 1));
Dan Gohman95be7d72008-09-18 16:26:26 +0000251
Andrew Trick52226d42012-03-07 23:00:49 +0000252 SUnit *NewSU = newSUnit(N);
Dan Gohman95be7d72008-09-18 16:26:26 +0000253 assert(N->getNodeId() == -1 && "Node already inserted!");
254 N->setNodeId(NewSU->NodeNum);
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000255
Evan Cheng6cc775f2011-06-28 19:10:37 +0000256 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
257 for (unsigned i = 0; i != MCID.getNumOperands(); ++i) {
258 if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) {
Dan Gohman95be7d72008-09-18 16:26:26 +0000259 NewSU->isTwoAddress = true;
260 break;
261 }
262 }
Evan Cheng6cc775f2011-06-28 19:10:37 +0000263 if (MCID.isCommutable())
Dan Gohman95be7d72008-09-18 16:26:26 +0000264 NewSU->isCommutable = true;
Dan Gohman95be7d72008-09-18 16:26:26 +0000265
266 // LoadNode may already exist. This can happen when there is another
267 // load from the same location and producing the same type of value
268 // but it has different alignment or volatileness.
269 bool isNewLoad = true;
270 SUnit *LoadSU;
271 if (LoadNode->getNodeId() != -1) {
272 LoadSU = &SUnits[LoadNode->getNodeId()];
273 isNewLoad = false;
274 } else {
Andrew Trick52226d42012-03-07 23:00:49 +0000275 LoadSU = newSUnit(LoadNode);
Dan Gohman95be7d72008-09-18 16:26:26 +0000276 LoadNode->setNodeId(LoadSU->NodeNum);
Dan Gohman95be7d72008-09-18 16:26:26 +0000277 }
278
Dan Gohman2d170892008-12-09 22:54:47 +0000279 SDep ChainPred;
Dan Gohman95be7d72008-09-18 16:26:26 +0000280 SmallVector<SDep, 4> ChainSuccs;
281 SmallVector<SDep, 4> LoadPreds;
282 SmallVector<SDep, 4> NodePreds;
283 SmallVector<SDep, 4> NodeSuccs;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000284 for (SDep &Pred : SU->Preds) {
285 if (Pred.isCtrl())
286 ChainPred = Pred;
287 else if (Pred.getSUnit()->getNode() &&
288 Pred.getSUnit()->getNode()->isOperandOf(LoadNode))
289 LoadPreds.push_back(Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000290 else
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000291 NodePreds.push_back(Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000292 }
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000293 for (SDep &Succ : SU->Succs) {
294 if (Succ.isCtrl())
295 ChainSuccs.push_back(Succ);
Dan Gohman95be7d72008-09-18 16:26:26 +0000296 else
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000297 NodeSuccs.push_back(Succ);
Dan Gohman95be7d72008-09-18 16:26:26 +0000298 }
299
Dan Gohman2d170892008-12-09 22:54:47 +0000300 if (ChainPred.getSUnit()) {
301 RemovePred(SU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000302 if (isNewLoad)
Dan Gohman2d170892008-12-09 22:54:47 +0000303 AddPred(LoadSU, ChainPred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000304 }
305 for (unsigned i = 0, e = LoadPreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000306 const SDep &Pred = LoadPreds[i];
307 RemovePred(SU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000308 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000309 AddPred(LoadSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000310 }
311 }
312 for (unsigned i = 0, e = NodePreds.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000313 const SDep &Pred = NodePreds[i];
314 RemovePred(SU, Pred);
315 AddPred(NewSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000316 }
317 for (unsigned i = 0, e = NodeSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000318 SDep D = NodeSuccs[i];
319 SUnit *SuccDep = D.getSUnit();
320 D.setSUnit(SU);
321 RemovePred(SuccDep, D);
322 D.setSUnit(NewSU);
323 AddPred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000324 }
325 for (unsigned i = 0, e = ChainSuccs.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000326 SDep D = ChainSuccs[i];
327 SUnit *SuccDep = D.getSUnit();
328 D.setSUnit(SU);
329 RemovePred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000330 if (isNewLoad) {
Dan Gohman2d170892008-12-09 22:54:47 +0000331 D.setSUnit(LoadSU);
332 AddPred(SuccDep, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000333 }
Andrew Trick7c6c41a2012-03-07 05:21:32 +0000334 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000335 if (isNewLoad) {
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000336 SDep D(LoadSU, SDep::Barrier);
337 D.setLatency(LoadSU->Latency);
338 AddPred(NewSU, D);
Dan Gohman95be7d72008-09-18 16:26:26 +0000339 }
340
341 ++NumUnfolds;
342
343 if (NewSU->NumSuccsLeft == 0) {
344 NewSU->isAvailable = true;
345 return NewSU;
346 }
347 SU = NewSU;
348 }
349
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000350 LLVM_DEBUG(dbgs() << "Duplicating SU # " << SU->NodeNum << "\n");
Dan Gohman4c3034f2008-11-19 23:39:02 +0000351 NewSU = Clone(SU);
Dan Gohman95be7d72008-09-18 16:26:26 +0000352
353 // New SUnit has the exact same predecessors.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000354 for (SDep &Pred : SU->Preds)
355 if (!Pred.isArtificial())
356 AddPred(NewSU, Pred);
Dan Gohman95be7d72008-09-18 16:26:26 +0000357
358 // Only copy scheduled successors. Cut them from old node's successor
359 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000360 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000361 for (SDep &Succ : SU->Succs) {
362 if (Succ.isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000363 continue;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000364 SUnit *SuccSU = Succ.getSUnit();
Dan Gohman2d170892008-12-09 22:54:47 +0000365 if (SuccSU->isScheduled) {
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000366 SDep D = Succ;
Dan Gohman2d170892008-12-09 22:54:47 +0000367 D.setSUnit(NewSU);
368 AddPred(SuccSU, D);
369 D.setSUnit(SU);
370 DelDeps.push_back(std::make_pair(SuccSU, D));
Dan Gohman95be7d72008-09-18 16:26:26 +0000371 }
372 }
Evan Chengb2c42c62009-01-12 03:19:55 +0000373 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i)
Dan Gohman2d170892008-12-09 22:54:47 +0000374 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000375
376 ++NumDups;
377 return NewSU;
378}
379
Evan Chengb2c42c62009-01-12 03:19:55 +0000380/// InsertCopiesAndMoveSuccs - Insert register copies and move all
381/// scheduled successors of the given SUnit to the last copy.
382void ScheduleDAGFast::InsertCopiesAndMoveSuccs(SUnit *SU, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000383 const TargetRegisterClass *DestRC,
384 const TargetRegisterClass *SrcRC,
Craig Topperb94011f2013-07-14 04:42:23 +0000385 SmallVectorImpl<SUnit*> &Copies) {
Craig Topperc0196b12014-04-14 00:51:57 +0000386 SUnit *CopyFromSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000387 CopyFromSU->CopySrcRC = SrcRC;
388 CopyFromSU->CopyDstRC = DestRC;
389
Craig Topperc0196b12014-04-14 00:51:57 +0000390 SUnit *CopyToSU = newSUnit(static_cast<SDNode *>(nullptr));
Dan Gohman95be7d72008-09-18 16:26:26 +0000391 CopyToSU->CopySrcRC = DestRC;
392 CopyToSU->CopyDstRC = SrcRC;
393
394 // Only copy scheduled successors. Cut them from old node's successor
395 // list and move them over.
Dan Gohman2d170892008-12-09 22:54:47 +0000396 SmallVector<std::pair<SUnit *, SDep>, 4> DelDeps;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000397 for (SDep &Succ : SU->Succs) {
398 if (Succ.isArtificial())
Dan Gohman95be7d72008-09-18 16:26:26 +0000399 continue;
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000400 SUnit *SuccSU = Succ.getSUnit();
Dan Gohman2d170892008-12-09 22:54:47 +0000401 if (SuccSU->isScheduled) {
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000402 SDep D = Succ;
Dan Gohman2d170892008-12-09 22:54:47 +0000403 D.setSUnit(CopyToSU);
404 AddPred(SuccSU, D);
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000405 DelDeps.push_back(std::make_pair(SuccSU, Succ));
Dan Gohman95be7d72008-09-18 16:26:26 +0000406 }
407 }
408 for (unsigned i = 0, e = DelDeps.size(); i != e; ++i) {
Dan Gohman2d170892008-12-09 22:54:47 +0000409 RemovePred(DelDeps[i].first, DelDeps[i].second);
Dan Gohman95be7d72008-09-18 16:26:26 +0000410 }
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000411 SDep FromDep(SU, SDep::Data, Reg);
412 FromDep.setLatency(SU->Latency);
413 AddPred(CopyFromSU, FromDep);
414 SDep ToDep(CopyFromSU, SDep::Data, 0);
415 ToDep.setLatency(CopyFromSU->Latency);
416 AddPred(CopyToSU, ToDep);
Dan Gohman95be7d72008-09-18 16:26:26 +0000417
418 Copies.push_back(CopyFromSU);
419 Copies.push_back(CopyToSU);
420
Evan Chengb2c42c62009-01-12 03:19:55 +0000421 ++NumPRCopies;
Dan Gohman95be7d72008-09-18 16:26:26 +0000422}
423
424/// getPhysicalRegisterVT - Returns the ValueType of the physical register
425/// definition of the specified node.
426/// FIXME: Move to SelectionDAG?
Craig Topper7f416c82014-11-16 21:17:18 +0000427static MVT getPhysicalRegisterVT(SDNode *N, unsigned Reg,
Dan Gohman95be7d72008-09-18 16:26:26 +0000428 const TargetInstrInfo *TII) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000429 unsigned NumRes;
430 if (N->getOpcode() == ISD::CopyFromReg) {
431 // CopyFromReg has: "chain, Val, glue" so operand 1 gives the type.
432 NumRes = 1;
433 } else {
434 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
435 assert(MCID.ImplicitDefs && "Physical reg def must be in implicit def list!");
436 NumRes = MCID.getNumDefs();
Craig Toppere5e035a32015-12-05 07:13:35 +0000437 for (const MCPhysReg *ImpDef = MCID.getImplicitDefs(); *ImpDef; ++ImpDef) {
Tim Northovere4c7be52014-10-23 22:31:48 +0000438 if (Reg == *ImpDef)
439 break;
440 ++NumRes;
441 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000442 }
Craig Topper7f416c82014-11-16 21:17:18 +0000443 return N->getSimpleValueType(NumRes);
Dan Gohman95be7d72008-09-18 16:26:26 +0000444}
445
Dale Johannesen16f96442010-08-17 22:17:24 +0000446/// CheckForLiveRegDef - Return true and update live register vector if the
447/// specified register def of the specified SUnit clobbers any "live" registers.
448static bool CheckForLiveRegDef(SUnit *SU, unsigned Reg,
449 std::vector<SUnit*> &LiveRegDefs,
450 SmallSet<unsigned, 4> &RegAdded,
Craig Topperb94011f2013-07-14 04:42:23 +0000451 SmallVectorImpl<unsigned> &LRegs,
Dale Johannesen16f96442010-08-17 22:17:24 +0000452 const TargetRegisterInfo *TRI) {
453 bool Added = false;
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000454 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
455 if (LiveRegDefs[*AI] && LiveRegDefs[*AI] != SU) {
David Blaikie70573dc2014-11-19 07:49:26 +0000456 if (RegAdded.insert(*AI).second) {
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000457 LRegs.push_back(*AI);
Dale Johannesen16f96442010-08-17 22:17:24 +0000458 Added = true;
459 }
460 }
Jakob Stoklund Olesen9b09cf02012-06-01 22:38:17 +0000461 }
Dale Johannesen16f96442010-08-17 22:17:24 +0000462 return Added;
463}
464
Dan Gohman95be7d72008-09-18 16:26:26 +0000465/// 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.
469bool ScheduleDAGFast::DelayForLiveRegsBottomUp(SUnit *SU,
Craig Topperb94011f2013-07-14 04:42:23 +0000470 SmallVectorImpl<unsigned> &LRegs){
Dan Gohmanc07f6862008-09-23 18:50:48 +0000471 if (NumLiveRegs == 0)
Dan Gohman95be7d72008-09-18 16:26:26 +0000472 return false;
473
474 SmallSet<unsigned, 4> RegAdded;
475 // If this node would clobber any "live" register, then it's not ready.
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000476 for (SDep &Pred : SU->Preds) {
477 if (Pred.isAssignedRegDep()) {
478 CheckForLiveRegDef(Pred.getSUnit(), Pred.getReg(), LiveRegDefs,
Dale Johannesen16f96442010-08-17 22:17:24 +0000479 RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000480 }
481 }
482
Chris Lattner11a33812010-12-23 17:24:32 +0000483 for (SDNode *Node = SU->getNode(); Node; Node = Node->getGluedNode()) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000484 if (Node->getOpcode() == ISD::INLINEASM) {
485 // Inline asm can clobber physical defs.
486 unsigned NumOps = Node->getNumOperands();
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000487 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
Chris Lattner11a33812010-12-23 17:24:32 +0000488 --NumOps; // Ignore the glue operand.
Dale Johannesen16f96442010-08-17 22:17:24 +0000489
490 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
491 unsigned Flags =
492 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
493 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
494
495 ++i; // Skip the ID value.
496 if (InlineAsm::isRegDefKind(Flags) ||
Jakob Stoklund Olesen537a3022011-06-27 04:08:33 +0000497 InlineAsm::isRegDefEarlyClobberKind(Flags) ||
498 InlineAsm::isClobberKind(Flags)) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000499 // Check for def of register or earlyclobber register.
500 for (; NumVals; --NumVals, ++i) {
501 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
502 if (TargetRegisterInfo::isPhysicalRegister(Reg))
503 CheckForLiveRegDef(SU, Reg, LiveRegDefs, RegAdded, LRegs, TRI);
504 }
505 } else
506 i += NumVals;
507 }
508 continue;
509 }
Dan Gohman072734e2008-11-13 23:24:17 +0000510 if (!Node->isMachineOpcode())
Dan Gohman95be7d72008-09-18 16:26:26 +0000511 continue;
Evan Cheng6cc775f2011-06-28 19:10:37 +0000512 const MCInstrDesc &MCID = TII->get(Node->getMachineOpcode());
513 if (!MCID.ImplicitDefs)
Dan Gohman95be7d72008-09-18 16:26:26 +0000514 continue;
Craig Toppere5e035a32015-12-05 07:13:35 +0000515 for (const MCPhysReg *Reg = MCID.getImplicitDefs(); *Reg; ++Reg) {
Dale Johannesen16f96442010-08-17 22:17:24 +0000516 CheckForLiveRegDef(SU, *Reg, LiveRegDefs, RegAdded, LRegs, TRI);
Dan Gohman95be7d72008-09-18 16:26:26 +0000517 }
518 }
519 return !LRegs.empty();
520}
521
522
523/// ListScheduleBottomUp - The main loop of list scheduling for bottom-up
524/// schedulers.
525void ScheduleDAGFast::ListScheduleBottomUp() {
526 unsigned CurCycle = 0;
Dan Gohmanb9543432009-02-10 23:27:53 +0000527
528 // Release any predecessors of the special Exit node.
529 ReleasePredecessors(&ExitSU, CurCycle);
530
Dan Gohman95be7d72008-09-18 16:26:26 +0000531 // Add root to Available queue.
532 if (!SUnits.empty()) {
Dan Gohman5a390b92008-11-13 21:21:28 +0000533 SUnit *RootSU = &SUnits[DAG->getRoot().getNode()->getNodeId()];
Dan Gohman95be7d72008-09-18 16:26:26 +0000534 assert(RootSU->Succs.empty() && "Graph root shouldn't have successors!");
535 RootSU->isAvailable = true;
536 AvailableQueue.push(RootSU);
537 }
538
539 // While Available queue is not empty, grab the node with the highest
540 // priority. If it is not ready put it back. Schedule the node.
541 SmallVector<SUnit*, 4> NotReady;
542 DenseMap<SUnit*, SmallVector<unsigned, 4> > LRegsMap;
543 Sequence.reserve(SUnits.size());
544 while (!AvailableQueue.empty()) {
545 bool Delayed = false;
546 LRegsMap.clear();
547 SUnit *CurSU = AvailableQueue.pop();
548 while (CurSU) {
Dan Gohman4f474b02008-11-17 19:52:36 +0000549 SmallVector<unsigned, 4> LRegs;
550 if (!DelayForLiveRegsBottomUp(CurSU, LRegs))
551 break;
552 Delayed = true;
553 LRegsMap.insert(std::make_pair(CurSU, LRegs));
Dan Gohman95be7d72008-09-18 16:26:26 +0000554
555 CurSU->isPending = true; // This SU is not in AvailableQueue right now.
556 NotReady.push_back(CurSU);
557 CurSU = AvailableQueue.pop();
558 }
559
560 // All candidates are delayed due to live physical reg dependencies.
561 // Try code duplication or inserting cross class copies
562 // to resolve it.
563 if (Delayed && !CurSU) {
564 if (!CurSU) {
565 // Try duplicating the nodes that produces these
566 // "expensive to copy" values to break the dependency. In case even
567 // that doesn't work, insert cross class copies.
568 SUnit *TrySU = NotReady[0];
Craig Topperb94011f2013-07-14 04:42:23 +0000569 SmallVectorImpl<unsigned> &LRegs = LRegsMap[TrySU];
Dan Gohman95be7d72008-09-18 16:26:26 +0000570 assert(LRegs.size() == 1 && "Can't handle this yet!");
571 unsigned Reg = LRegs[0];
572 SUnit *LRDef = LiveRegDefs[Reg];
Craig Topper7f416c82014-11-16 21:17:18 +0000573 MVT VT = getPhysicalRegisterVT(LRDef->getNode(), Reg, TII);
Evan Chengb2c42c62009-01-12 03:19:55 +0000574 const TargetRegisterClass *RC =
Rafael Espindola38a7d7c2010-06-29 14:02:34 +0000575 TRI->getMinimalPhysRegClass(Reg, VT);
Evan Chengb2c42c62009-01-12 03:19:55 +0000576 const TargetRegisterClass *DestRC = TRI->getCrossCopyRegClass(RC);
577
Evan Chengb4c6a342011-03-10 00:16:32 +0000578 // If cross copy register class is the same as RC, then it must be
579 // possible copy the value directly. Do not try duplicate the def.
580 // If cross copy register class is not the same as RC, then it's
581 // possible to copy the value but it require cross register class copies
582 // and it is expensive.
583 // If cross copy register class is null, then it's not possible to copy
584 // the value at all.
Craig Topperc0196b12014-04-14 00:51:57 +0000585 SUnit *NewDef = nullptr;
Evan Chengb4c6a342011-03-10 00:16:32 +0000586 if (DestRC != RC) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000587 NewDef = CopyAndMoveSuccessors(LRDef);
Evan Chengb4c6a342011-03-10 00:16:32 +0000588 if (!DestRC && !NewDef)
589 report_fatal_error("Can't handle live physical "
590 "register dependency!");
591 }
Dan Gohman95be7d72008-09-18 16:26:26 +0000592 if (!NewDef) {
Evan Chengb2c42c62009-01-12 03:19:55 +0000593 // Issue copies, these can be expensive cross register class copies.
Dan Gohman95be7d72008-09-18 16:26:26 +0000594 SmallVector<SUnit*, 2> Copies;
Evan Chengb2c42c62009-01-12 03:19:55 +0000595 InsertCopiesAndMoveSuccs(LRDef, Reg, DestRC, RC, Copies);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000596 LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << TrySU->NodeNum
597 << " to SU #" << Copies.front()->NodeNum << "\n");
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000598 AddPred(TrySU, SDep(Copies.front(), SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000599 NewDef = Copies.back();
600 }
601
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000602 LLVM_DEBUG(dbgs() << "Adding an edge from SU # " << NewDef->NodeNum
603 << " to SU #" << TrySU->NodeNum << "\n");
Dan Gohman95be7d72008-09-18 16:26:26 +0000604 LiveRegDefs[Reg] = NewDef;
Andrew Trickbaeaabb2012-11-06 03:13:46 +0000605 AddPred(NewDef, SDep(TrySU, SDep::Artificial));
Dan Gohman95be7d72008-09-18 16:26:26 +0000606 TrySU->isAvailable = false;
607 CurSU = NewDef;
608 }
609
610 if (!CurSU) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000611 llvm_unreachable("Unable to resolve live physical register dependencies!");
Dan Gohman95be7d72008-09-18 16:26:26 +0000612 }
613 }
614
615 // Add the nodes that aren't ready back onto the available list.
616 for (unsigned i = 0, e = NotReady.size(); i != e; ++i) {
617 NotReady[i]->isPending = false;
618 // May no longer be available due to backtracking.
619 if (NotReady[i]->isAvailable)
620 AvailableQueue.push(NotReady[i]);
621 }
622 NotReady.clear();
623
Dan Gohmanc602dd42008-11-21 00:10:42 +0000624 if (CurSU)
Dan Gohman95be7d72008-09-18 16:26:26 +0000625 ScheduleNodeBottomUp(CurSU, CurCycle);
Dan Gohman95be7d72008-09-18 16:26:26 +0000626 ++CurCycle;
627 }
628
Dan Gohman6905f152009-09-28 16:09:41 +0000629 // Reverse the order since it is bottom up.
Dan Gohman95be7d72008-09-18 16:26:26 +0000630 std::reverse(Sequence.begin(), Sequence.end());
Dan Gohman6905f152009-09-28 16:09:41 +0000631
Dan Gohman95be7d72008-09-18 16:26:26 +0000632#ifndef NDEBUG
Andrew Trick46a58662012-03-07 05:21:36 +0000633 VerifyScheduledSequence(/*isBottomUp=*/true);
Dan Gohman95be7d72008-09-18 16:26:26 +0000634#endif
635}
636
Evan Cheng839fb652012-10-17 19:39:36 +0000637
Benjamin Kramera74129a2012-10-20 12:53:26 +0000638namespace {
Evan Cheng839fb652012-10-17 19:39:36 +0000639//===----------------------------------------------------------------------===//
640// ScheduleDAGLinearize - No scheduling scheduler, it simply linearize the
641// DAG in topological order.
642// IMPORTANT: this may not work for targets with phyreg dependency.
643//
644class ScheduleDAGLinearize : public ScheduleDAGSDNodes {
645public:
646 ScheduleDAGLinearize(MachineFunction &mf) : ScheduleDAGSDNodes(mf) {}
647
Craig Topper7b883b32014-03-08 06:31:39 +0000648 void Schedule() override;
Evan Cheng839fb652012-10-17 19:39:36 +0000649
Craig Topper7b883b32014-03-08 06:31:39 +0000650 MachineBasicBlock *
651 EmitSchedule(MachineBasicBlock::iterator &InsertPos) override;
Evan Cheng839fb652012-10-17 19:39:36 +0000652
653private:
654 std::vector<SDNode*> Sequence;
655 DenseMap<SDNode*, SDNode*> GluedMap; // Cache glue to its user
656
657 void ScheduleNode(SDNode *N);
658};
Benjamin Kramera74129a2012-10-20 12:53:26 +0000659} // end anonymous namespace
Evan Cheng839fb652012-10-17 19:39:36 +0000660
661void ScheduleDAGLinearize::ScheduleNode(SDNode *N) {
662 if (N->getNodeId() != 0)
Craig Topperc0196b12014-04-14 00:51:57 +0000663 llvm_unreachable(nullptr);
Evan Cheng839fb652012-10-17 19:39:36 +0000664
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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000670 LLVM_DEBUG(dbgs() << "\n*** Scheduling: ");
671 LLVM_DEBUG(N->dump(DAG));
Evan Cheng839fb652012-10-17 19:39:36 +0000672 Sequence.push_back(N);
673
674 unsigned NumOps = N->getNumOperands();
675 if (unsigned NumLeft = NumOps) {
Craig Topperc0196b12014-04-14 00:51:57 +0000676 SDNode *GluedOpN = nullptr;
Evan Cheng839fb652012-10-17 19:39:36 +0000677 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.
710static SDNode *findGluedUser(SDNode *N) {
711 while (SDNode *Glued = N->getGluedUser())
712 N = Glued;
713 return N;
714}
715
716void ScheduleDAGLinearize::Schedule() {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000717 LLVM_DEBUG(dbgs() << "********** DAG Linearization **********\n");
Evan Cheng839fb652012-10-17 19:39:36 +0000718
719 SmallVector<SDNode*, 8> Glues;
720 unsigned DAGSize = 0;
Pete Cooper65c69402015-07-14 22:10:54 +0000721 for (SDNode &Node : DAG->allnodes()) {
722 SDNode *N = &Node;
Evan Cheng839fb652012-10-17 19:39:36 +0000723
724 // Use node id to record degree.
725 unsigned Degree = N->use_size();
726 N->setNodeId(Degree);
727 unsigned NumVals = N->getNumValues();
728 if (NumVals && N->getValueType(NumVals-1) == MVT::Glue &&
729 N->hasAnyUseOfValue(NumVals-1)) {
730 SDNode *User = findGluedUser(N);
731 if (User) {
732 Glues.push_back(N);
733 GluedMap.insert(std::make_pair(N, User));
734 }
735 }
736
737 if (N->isMachineOpcode() ||
738 (N->getOpcode() != ISD::EntryToken && !isPassiveNode(N)))
739 ++DAGSize;
740 }
741
742 for (unsigned i = 0, e = Glues.size(); i != e; ++i) {
743 SDNode *Glue = Glues[i];
744 SDNode *GUser = GluedMap[Glue];
745 unsigned Degree = Glue->getNodeId();
746 unsigned UDegree = GUser->getNodeId();
747
748 // Glue user must be scheduled together with the glue operand. So other
749 // users of the glue operand must be treated as its users.
750 SDNode *ImmGUser = Glue->getGluedUser();
Krzysztof Parzyszek41b6e142017-05-04 13:35:17 +0000751 for (const SDNode *U : Glue->uses())
752 if (U == ImmGUser)
Evan Cheng839fb652012-10-17 19:39:36 +0000753 --Degree;
754 GUser->setNodeId(UDegree + Degree);
755 Glue->setNodeId(1);
756 }
757
758 Sequence.reserve(DAGSize);
759 ScheduleNode(DAG->getRoot().getNode());
760}
761
762MachineBasicBlock*
763ScheduleDAGLinearize::EmitSchedule(MachineBasicBlock::iterator &InsertPos) {
764 InstrEmitter Emitter(BB, InsertPos);
765 DenseMap<SDValue, unsigned> VRBaseMap;
766
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000767 LLVM_DEBUG({ dbgs() << "\n*** Final schedule ***\n"; });
Evan Cheng839fb652012-10-17 19:39:36 +0000768
Evan Cheng839fb652012-10-17 19:39:36 +0000769 unsigned NumNodes = Sequence.size();
Adrian Prantldfe15f32018-03-02 22:59:51 +0000770 MachineBasicBlock *BB = Emitter.getBlock();
Evan Cheng839fb652012-10-17 19:39:36 +0000771 for (unsigned i = 0; i != NumNodes; ++i) {
772 SDNode *N = Sequence[NumNodes-i-1];
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000773 LLVM_DEBUG(N->dump(DAG));
Evan Cheng839fb652012-10-17 19:39:36 +0000774 Emitter.EmitNode(N, false, false, VRBaseMap);
Adrian Prantldfe15f32018-03-02 22:59:51 +0000775
776 // Emit any debug values associated with the node.
777 if (N->getHasDebugValue()) {
778 MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos();
779 for (auto DV : DAG->GetDbgValues(N)) {
780 if (DV->isInvalidated())
781 continue;
782 if (auto *DbgMI = Emitter.EmitDbgValue(DV, VRBaseMap))
783 BB->insert(InsertPos, DbgMI);
784 DV->setIsInvalidated();
785 }
786 }
Evan Cheng839fb652012-10-17 19:39:36 +0000787 }
788
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000789 LLVM_DEBUG(dbgs() << '\n');
Evan Cheng839fb652012-10-17 19:39:36 +0000790
791 InsertPos = Emitter.getInsertPos();
792 return Emitter.getBlock();
793}
794
Dan Gohman95be7d72008-09-18 16:26:26 +0000795//===----------------------------------------------------------------------===//
796// Public Constructor Functions
797//===----------------------------------------------------------------------===//
798
Dan Gohmandfaf6462009-02-11 04:27:20 +0000799llvm::ScheduleDAGSDNodes *
Bill Wendling026e5d72009-04-29 23:29:43 +0000800llvm::createFastDAGScheduler(SelectionDAGISel *IS, CodeGenOpt::Level) {
Dan Gohman619ef482009-01-15 19:20:50 +0000801 return new ScheduleDAGFast(*IS->MF);
Dan Gohman95be7d72008-09-18 16:26:26 +0000802}
Evan Cheng839fb652012-10-17 19:39:36 +0000803
804llvm::ScheduleDAGSDNodes *
805llvm::createDAGLinearizer(SelectionDAGISel *IS, CodeGenOpt::Level) {
806 return new ScheduleDAGLinearize(*IS->MF);
807}