1*9880d681SAndroid Build Coastguard Worker //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This implements the ScheduleDAG class, which is a base class used by
11*9880d681SAndroid Build Coastguard Worker // scheduling implementation classes.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/ScheduleDAG.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAGNodes.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include <climits>
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "pre-RA-sched"
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
31*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> StressSchedOpt(
32*9880d681SAndroid Build Coastguard Worker "stress-sched", cl::Hidden, cl::init(false),
33*9880d681SAndroid Build Coastguard Worker cl::desc("Stress test instruction scheduling"));
34*9880d681SAndroid Build Coastguard Worker #endif
35*9880d681SAndroid Build Coastguard Worker
anchor()36*9880d681SAndroid Build Coastguard Worker void SchedulingPriorityQueue::anchor() { }
37*9880d681SAndroid Build Coastguard Worker
ScheduleDAG(MachineFunction & mf)38*9880d681SAndroid Build Coastguard Worker ScheduleDAG::ScheduleDAG(MachineFunction &mf)
39*9880d681SAndroid Build Coastguard Worker : TM(mf.getTarget()), TII(mf.getSubtarget().getInstrInfo()),
40*9880d681SAndroid Build Coastguard Worker TRI(mf.getSubtarget().getRegisterInfo()), MF(mf),
41*9880d681SAndroid Build Coastguard Worker MRI(mf.getRegInfo()), EntrySU(), ExitSU() {
42*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
43*9880d681SAndroid Build Coastguard Worker StressSched = StressSchedOpt;
44*9880d681SAndroid Build Coastguard Worker #endif
45*9880d681SAndroid Build Coastguard Worker }
46*9880d681SAndroid Build Coastguard Worker
~ScheduleDAG()47*9880d681SAndroid Build Coastguard Worker ScheduleDAG::~ScheduleDAG() {}
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker /// Clear the DAG state (e.g. between scheduling regions).
clearDAG()50*9880d681SAndroid Build Coastguard Worker void ScheduleDAG::clearDAG() {
51*9880d681SAndroid Build Coastguard Worker SUnits.clear();
52*9880d681SAndroid Build Coastguard Worker EntrySU = SUnit();
53*9880d681SAndroid Build Coastguard Worker ExitSU = SUnit();
54*9880d681SAndroid Build Coastguard Worker }
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker /// getInstrDesc helper to handle SDNodes.
getNodeDesc(const SDNode * Node) const57*9880d681SAndroid Build Coastguard Worker const MCInstrDesc *ScheduleDAG::getNodeDesc(const SDNode *Node) const {
58*9880d681SAndroid Build Coastguard Worker if (!Node || !Node->isMachineOpcode()) return nullptr;
59*9880d681SAndroid Build Coastguard Worker return &TII->get(Node->getMachineOpcode());
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker
62*9880d681SAndroid Build Coastguard Worker /// addPred - This adds the specified edge as a pred of the current node if
63*9880d681SAndroid Build Coastguard Worker /// not already. It also adds the current node as a successor of the
64*9880d681SAndroid Build Coastguard Worker /// specified node.
addPred(const SDep & D,bool Required)65*9880d681SAndroid Build Coastguard Worker bool SUnit::addPred(const SDep &D, bool Required) {
66*9880d681SAndroid Build Coastguard Worker // If this node already has this dependence, don't add a redundant one.
67*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
68*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
69*9880d681SAndroid Build Coastguard Worker // Zero-latency weak edges may be added purely for heuristic ordering. Don't
70*9880d681SAndroid Build Coastguard Worker // add them if another kind of edge already exists.
71*9880d681SAndroid Build Coastguard Worker if (!Required && I->getSUnit() == D.getSUnit())
72*9880d681SAndroid Build Coastguard Worker return false;
73*9880d681SAndroid Build Coastguard Worker if (I->overlaps(D)) {
74*9880d681SAndroid Build Coastguard Worker // Extend the latency if needed. Equivalent to removePred(I) + addPred(D).
75*9880d681SAndroid Build Coastguard Worker if (I->getLatency() < D.getLatency()) {
76*9880d681SAndroid Build Coastguard Worker SUnit *PredSU = I->getSUnit();
77*9880d681SAndroid Build Coastguard Worker // Find the corresponding successor in N.
78*9880d681SAndroid Build Coastguard Worker SDep ForwardD = *I;
79*9880d681SAndroid Build Coastguard Worker ForwardD.setSUnit(this);
80*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<SDep>::iterator II = PredSU->Succs.begin(),
81*9880d681SAndroid Build Coastguard Worker EE = PredSU->Succs.end(); II != EE; ++II) {
82*9880d681SAndroid Build Coastguard Worker if (*II == ForwardD) {
83*9880d681SAndroid Build Coastguard Worker II->setLatency(D.getLatency());
84*9880d681SAndroid Build Coastguard Worker break;
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker }
87*9880d681SAndroid Build Coastguard Worker I->setLatency(D.getLatency());
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker return false;
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker }
92*9880d681SAndroid Build Coastguard Worker // Now add a corresponding succ to N.
93*9880d681SAndroid Build Coastguard Worker SDep P = D;
94*9880d681SAndroid Build Coastguard Worker P.setSUnit(this);
95*9880d681SAndroid Build Coastguard Worker SUnit *N = D.getSUnit();
96*9880d681SAndroid Build Coastguard Worker // Update the bookkeeping.
97*9880d681SAndroid Build Coastguard Worker if (D.getKind() == SDep::Data) {
98*9880d681SAndroid Build Coastguard Worker assert(NumPreds < UINT_MAX && "NumPreds will overflow!");
99*9880d681SAndroid Build Coastguard Worker assert(N->NumSuccs < UINT_MAX && "NumSuccs will overflow!");
100*9880d681SAndroid Build Coastguard Worker ++NumPreds;
101*9880d681SAndroid Build Coastguard Worker ++N->NumSuccs;
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker if (!N->isScheduled) {
104*9880d681SAndroid Build Coastguard Worker if (D.isWeak()) {
105*9880d681SAndroid Build Coastguard Worker ++WeakPredsLeft;
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker else {
108*9880d681SAndroid Build Coastguard Worker assert(NumPredsLeft < UINT_MAX && "NumPredsLeft will overflow!");
109*9880d681SAndroid Build Coastguard Worker ++NumPredsLeft;
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker if (!isScheduled) {
113*9880d681SAndroid Build Coastguard Worker if (D.isWeak()) {
114*9880d681SAndroid Build Coastguard Worker ++N->WeakSuccsLeft;
115*9880d681SAndroid Build Coastguard Worker }
116*9880d681SAndroid Build Coastguard Worker else {
117*9880d681SAndroid Build Coastguard Worker assert(N->NumSuccsLeft < UINT_MAX && "NumSuccsLeft will overflow!");
118*9880d681SAndroid Build Coastguard Worker ++N->NumSuccsLeft;
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker }
121*9880d681SAndroid Build Coastguard Worker Preds.push_back(D);
122*9880d681SAndroid Build Coastguard Worker N->Succs.push_back(P);
123*9880d681SAndroid Build Coastguard Worker if (P.getLatency() != 0) {
124*9880d681SAndroid Build Coastguard Worker this->setDepthDirty();
125*9880d681SAndroid Build Coastguard Worker N->setHeightDirty();
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker return true;
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker /// removePred - This removes the specified edge as a pred of the current
131*9880d681SAndroid Build Coastguard Worker /// node if it exists. It also removes the current node as a successor of
132*9880d681SAndroid Build Coastguard Worker /// the specified node.
removePred(const SDep & D)133*9880d681SAndroid Build Coastguard Worker void SUnit::removePred(const SDep &D) {
134*9880d681SAndroid Build Coastguard Worker // Find the matching predecessor.
135*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<SDep>::iterator I = Preds.begin(), E = Preds.end();
136*9880d681SAndroid Build Coastguard Worker I != E; ++I)
137*9880d681SAndroid Build Coastguard Worker if (*I == D) {
138*9880d681SAndroid Build Coastguard Worker // Find the corresponding successor in N.
139*9880d681SAndroid Build Coastguard Worker SDep P = D;
140*9880d681SAndroid Build Coastguard Worker P.setSUnit(this);
141*9880d681SAndroid Build Coastguard Worker SUnit *N = D.getSUnit();
142*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<SDep>::iterator Succ = std::find(N->Succs.begin(),
143*9880d681SAndroid Build Coastguard Worker N->Succs.end(), P);
144*9880d681SAndroid Build Coastguard Worker assert(Succ != N->Succs.end() && "Mismatching preds / succs lists!");
145*9880d681SAndroid Build Coastguard Worker N->Succs.erase(Succ);
146*9880d681SAndroid Build Coastguard Worker Preds.erase(I);
147*9880d681SAndroid Build Coastguard Worker // Update the bookkeeping.
148*9880d681SAndroid Build Coastguard Worker if (P.getKind() == SDep::Data) {
149*9880d681SAndroid Build Coastguard Worker assert(NumPreds > 0 && "NumPreds will underflow!");
150*9880d681SAndroid Build Coastguard Worker assert(N->NumSuccs > 0 && "NumSuccs will underflow!");
151*9880d681SAndroid Build Coastguard Worker --NumPreds;
152*9880d681SAndroid Build Coastguard Worker --N->NumSuccs;
153*9880d681SAndroid Build Coastguard Worker }
154*9880d681SAndroid Build Coastguard Worker if (!N->isScheduled) {
155*9880d681SAndroid Build Coastguard Worker if (D.isWeak())
156*9880d681SAndroid Build Coastguard Worker --WeakPredsLeft;
157*9880d681SAndroid Build Coastguard Worker else {
158*9880d681SAndroid Build Coastguard Worker assert(NumPredsLeft > 0 && "NumPredsLeft will underflow!");
159*9880d681SAndroid Build Coastguard Worker --NumPredsLeft;
160*9880d681SAndroid Build Coastguard Worker }
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker if (!isScheduled) {
163*9880d681SAndroid Build Coastguard Worker if (D.isWeak())
164*9880d681SAndroid Build Coastguard Worker --N->WeakSuccsLeft;
165*9880d681SAndroid Build Coastguard Worker else {
166*9880d681SAndroid Build Coastguard Worker assert(N->NumSuccsLeft > 0 && "NumSuccsLeft will underflow!");
167*9880d681SAndroid Build Coastguard Worker --N->NumSuccsLeft;
168*9880d681SAndroid Build Coastguard Worker }
169*9880d681SAndroid Build Coastguard Worker }
170*9880d681SAndroid Build Coastguard Worker if (P.getLatency() != 0) {
171*9880d681SAndroid Build Coastguard Worker this->setDepthDirty();
172*9880d681SAndroid Build Coastguard Worker N->setHeightDirty();
173*9880d681SAndroid Build Coastguard Worker }
174*9880d681SAndroid Build Coastguard Worker return;
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker
setDepthDirty()178*9880d681SAndroid Build Coastguard Worker void SUnit::setDepthDirty() {
179*9880d681SAndroid Build Coastguard Worker if (!isDepthCurrent) return;
180*9880d681SAndroid Build Coastguard Worker SmallVector<SUnit*, 8> WorkList;
181*9880d681SAndroid Build Coastguard Worker WorkList.push_back(this);
182*9880d681SAndroid Build Coastguard Worker do {
183*9880d681SAndroid Build Coastguard Worker SUnit *SU = WorkList.pop_back_val();
184*9880d681SAndroid Build Coastguard Worker SU->isDepthCurrent = false;
185*9880d681SAndroid Build Coastguard Worker for (SUnit::const_succ_iterator I = SU->Succs.begin(),
186*9880d681SAndroid Build Coastguard Worker E = SU->Succs.end(); I != E; ++I) {
187*9880d681SAndroid Build Coastguard Worker SUnit *SuccSU = I->getSUnit();
188*9880d681SAndroid Build Coastguard Worker if (SuccSU->isDepthCurrent)
189*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SuccSU);
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
setHeightDirty()194*9880d681SAndroid Build Coastguard Worker void SUnit::setHeightDirty() {
195*9880d681SAndroid Build Coastguard Worker if (!isHeightCurrent) return;
196*9880d681SAndroid Build Coastguard Worker SmallVector<SUnit*, 8> WorkList;
197*9880d681SAndroid Build Coastguard Worker WorkList.push_back(this);
198*9880d681SAndroid Build Coastguard Worker do {
199*9880d681SAndroid Build Coastguard Worker SUnit *SU = WorkList.pop_back_val();
200*9880d681SAndroid Build Coastguard Worker SU->isHeightCurrent = false;
201*9880d681SAndroid Build Coastguard Worker for (SUnit::const_pred_iterator I = SU->Preds.begin(),
202*9880d681SAndroid Build Coastguard Worker E = SU->Preds.end(); I != E; ++I) {
203*9880d681SAndroid Build Coastguard Worker SUnit *PredSU = I->getSUnit();
204*9880d681SAndroid Build Coastguard Worker if (PredSU->isHeightCurrent)
205*9880d681SAndroid Build Coastguard Worker WorkList.push_back(PredSU);
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
208*9880d681SAndroid Build Coastguard Worker }
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker /// setDepthToAtLeast - Update this node's successors to reflect the
211*9880d681SAndroid Build Coastguard Worker /// fact that this node's depth just increased.
212*9880d681SAndroid Build Coastguard Worker ///
setDepthToAtLeast(unsigned NewDepth)213*9880d681SAndroid Build Coastguard Worker void SUnit::setDepthToAtLeast(unsigned NewDepth) {
214*9880d681SAndroid Build Coastguard Worker if (NewDepth <= getDepth())
215*9880d681SAndroid Build Coastguard Worker return;
216*9880d681SAndroid Build Coastguard Worker setDepthDirty();
217*9880d681SAndroid Build Coastguard Worker Depth = NewDepth;
218*9880d681SAndroid Build Coastguard Worker isDepthCurrent = true;
219*9880d681SAndroid Build Coastguard Worker }
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker /// setHeightToAtLeast - Update this node's predecessors to reflect the
222*9880d681SAndroid Build Coastguard Worker /// fact that this node's height just increased.
223*9880d681SAndroid Build Coastguard Worker ///
setHeightToAtLeast(unsigned NewHeight)224*9880d681SAndroid Build Coastguard Worker void SUnit::setHeightToAtLeast(unsigned NewHeight) {
225*9880d681SAndroid Build Coastguard Worker if (NewHeight <= getHeight())
226*9880d681SAndroid Build Coastguard Worker return;
227*9880d681SAndroid Build Coastguard Worker setHeightDirty();
228*9880d681SAndroid Build Coastguard Worker Height = NewHeight;
229*9880d681SAndroid Build Coastguard Worker isHeightCurrent = true;
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker /// ComputeDepth - Calculate the maximal path from the node to the exit.
233*9880d681SAndroid Build Coastguard Worker ///
ComputeDepth()234*9880d681SAndroid Build Coastguard Worker void SUnit::ComputeDepth() {
235*9880d681SAndroid Build Coastguard Worker SmallVector<SUnit*, 8> WorkList;
236*9880d681SAndroid Build Coastguard Worker WorkList.push_back(this);
237*9880d681SAndroid Build Coastguard Worker do {
238*9880d681SAndroid Build Coastguard Worker SUnit *Cur = WorkList.back();
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker bool Done = true;
241*9880d681SAndroid Build Coastguard Worker unsigned MaxPredDepth = 0;
242*9880d681SAndroid Build Coastguard Worker for (SUnit::const_pred_iterator I = Cur->Preds.begin(),
243*9880d681SAndroid Build Coastguard Worker E = Cur->Preds.end(); I != E; ++I) {
244*9880d681SAndroid Build Coastguard Worker SUnit *PredSU = I->getSUnit();
245*9880d681SAndroid Build Coastguard Worker if (PredSU->isDepthCurrent)
246*9880d681SAndroid Build Coastguard Worker MaxPredDepth = std::max(MaxPredDepth,
247*9880d681SAndroid Build Coastguard Worker PredSU->Depth + I->getLatency());
248*9880d681SAndroid Build Coastguard Worker else {
249*9880d681SAndroid Build Coastguard Worker Done = false;
250*9880d681SAndroid Build Coastguard Worker WorkList.push_back(PredSU);
251*9880d681SAndroid Build Coastguard Worker }
252*9880d681SAndroid Build Coastguard Worker }
253*9880d681SAndroid Build Coastguard Worker
254*9880d681SAndroid Build Coastguard Worker if (Done) {
255*9880d681SAndroid Build Coastguard Worker WorkList.pop_back();
256*9880d681SAndroid Build Coastguard Worker if (MaxPredDepth != Cur->Depth) {
257*9880d681SAndroid Build Coastguard Worker Cur->setDepthDirty();
258*9880d681SAndroid Build Coastguard Worker Cur->Depth = MaxPredDepth;
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker Cur->isDepthCurrent = true;
261*9880d681SAndroid Build Coastguard Worker }
262*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
263*9880d681SAndroid Build Coastguard Worker }
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker /// ComputeHeight - Calculate the maximal path from the node to the entry.
266*9880d681SAndroid Build Coastguard Worker ///
ComputeHeight()267*9880d681SAndroid Build Coastguard Worker void SUnit::ComputeHeight() {
268*9880d681SAndroid Build Coastguard Worker SmallVector<SUnit*, 8> WorkList;
269*9880d681SAndroid Build Coastguard Worker WorkList.push_back(this);
270*9880d681SAndroid Build Coastguard Worker do {
271*9880d681SAndroid Build Coastguard Worker SUnit *Cur = WorkList.back();
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker bool Done = true;
274*9880d681SAndroid Build Coastguard Worker unsigned MaxSuccHeight = 0;
275*9880d681SAndroid Build Coastguard Worker for (SUnit::const_succ_iterator I = Cur->Succs.begin(),
276*9880d681SAndroid Build Coastguard Worker E = Cur->Succs.end(); I != E; ++I) {
277*9880d681SAndroid Build Coastguard Worker SUnit *SuccSU = I->getSUnit();
278*9880d681SAndroid Build Coastguard Worker if (SuccSU->isHeightCurrent)
279*9880d681SAndroid Build Coastguard Worker MaxSuccHeight = std::max(MaxSuccHeight,
280*9880d681SAndroid Build Coastguard Worker SuccSU->Height + I->getLatency());
281*9880d681SAndroid Build Coastguard Worker else {
282*9880d681SAndroid Build Coastguard Worker Done = false;
283*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SuccSU);
284*9880d681SAndroid Build Coastguard Worker }
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard Worker if (Done) {
288*9880d681SAndroid Build Coastguard Worker WorkList.pop_back();
289*9880d681SAndroid Build Coastguard Worker if (MaxSuccHeight != Cur->Height) {
290*9880d681SAndroid Build Coastguard Worker Cur->setHeightDirty();
291*9880d681SAndroid Build Coastguard Worker Cur->Height = MaxSuccHeight;
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker Cur->isHeightCurrent = true;
294*9880d681SAndroid Build Coastguard Worker }
295*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
296*9880d681SAndroid Build Coastguard Worker }
297*9880d681SAndroid Build Coastguard Worker
biasCriticalPath()298*9880d681SAndroid Build Coastguard Worker void SUnit::biasCriticalPath() {
299*9880d681SAndroid Build Coastguard Worker if (NumPreds < 2)
300*9880d681SAndroid Build Coastguard Worker return;
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker SUnit::pred_iterator BestI = Preds.begin();
303*9880d681SAndroid Build Coastguard Worker unsigned MaxDepth = BestI->getSUnit()->getDepth();
304*9880d681SAndroid Build Coastguard Worker for (SUnit::pred_iterator I = std::next(BestI), E = Preds.end(); I != E;
305*9880d681SAndroid Build Coastguard Worker ++I) {
306*9880d681SAndroid Build Coastguard Worker if (I->getKind() == SDep::Data && I->getSUnit()->getDepth() > MaxDepth)
307*9880d681SAndroid Build Coastguard Worker BestI = I;
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker if (BestI != Preds.begin())
310*9880d681SAndroid Build Coastguard Worker std::swap(*Preds.begin(), *BestI);
311*9880d681SAndroid Build Coastguard Worker }
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
314*9880d681SAndroid Build Coastguard Worker /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
315*9880d681SAndroid Build Coastguard Worker /// a group of nodes flagged together.
dump(const ScheduleDAG * G) const316*9880d681SAndroid Build Coastguard Worker void SUnit::dump(const ScheduleDAG *G) const {
317*9880d681SAndroid Build Coastguard Worker dbgs() << "SU(" << NodeNum << "): ";
318*9880d681SAndroid Build Coastguard Worker G->dumpNode(this);
319*9880d681SAndroid Build Coastguard Worker }
320*9880d681SAndroid Build Coastguard Worker
dumpAll(const ScheduleDAG * G) const321*9880d681SAndroid Build Coastguard Worker void SUnit::dumpAll(const ScheduleDAG *G) const {
322*9880d681SAndroid Build Coastguard Worker dump(G);
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker dbgs() << " # preds left : " << NumPredsLeft << "\n";
325*9880d681SAndroid Build Coastguard Worker dbgs() << " # succs left : " << NumSuccsLeft << "\n";
326*9880d681SAndroid Build Coastguard Worker if (WeakPredsLeft)
327*9880d681SAndroid Build Coastguard Worker dbgs() << " # weak preds left : " << WeakPredsLeft << "\n";
328*9880d681SAndroid Build Coastguard Worker if (WeakSuccsLeft)
329*9880d681SAndroid Build Coastguard Worker dbgs() << " # weak succs left : " << WeakSuccsLeft << "\n";
330*9880d681SAndroid Build Coastguard Worker dbgs() << " # rdefs left : " << NumRegDefsLeft << "\n";
331*9880d681SAndroid Build Coastguard Worker dbgs() << " Latency : " << Latency << "\n";
332*9880d681SAndroid Build Coastguard Worker dbgs() << " Depth : " << getDepth() << "\n";
333*9880d681SAndroid Build Coastguard Worker dbgs() << " Height : " << getHeight() << "\n";
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker if (Preds.size() != 0) {
336*9880d681SAndroid Build Coastguard Worker dbgs() << " Predecessors:\n";
337*9880d681SAndroid Build Coastguard Worker for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
338*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
339*9880d681SAndroid Build Coastguard Worker dbgs() << " ";
340*9880d681SAndroid Build Coastguard Worker switch (I->getKind()) {
341*9880d681SAndroid Build Coastguard Worker case SDep::Data: dbgs() << "val "; break;
342*9880d681SAndroid Build Coastguard Worker case SDep::Anti: dbgs() << "anti"; break;
343*9880d681SAndroid Build Coastguard Worker case SDep::Output: dbgs() << "out "; break;
344*9880d681SAndroid Build Coastguard Worker case SDep::Order: dbgs() << "ch "; break;
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
347*9880d681SAndroid Build Coastguard Worker if (I->isArtificial())
348*9880d681SAndroid Build Coastguard Worker dbgs() << " *";
349*9880d681SAndroid Build Coastguard Worker dbgs() << ": Latency=" << I->getLatency();
350*9880d681SAndroid Build Coastguard Worker if (I->isAssignedRegDep())
351*9880d681SAndroid Build Coastguard Worker dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
352*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
353*9880d681SAndroid Build Coastguard Worker }
354*9880d681SAndroid Build Coastguard Worker }
355*9880d681SAndroid Build Coastguard Worker if (Succs.size() != 0) {
356*9880d681SAndroid Build Coastguard Worker dbgs() << " Successors:\n";
357*9880d681SAndroid Build Coastguard Worker for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
358*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
359*9880d681SAndroid Build Coastguard Worker dbgs() << " ";
360*9880d681SAndroid Build Coastguard Worker switch (I->getKind()) {
361*9880d681SAndroid Build Coastguard Worker case SDep::Data: dbgs() << "val "; break;
362*9880d681SAndroid Build Coastguard Worker case SDep::Anti: dbgs() << "anti"; break;
363*9880d681SAndroid Build Coastguard Worker case SDep::Output: dbgs() << "out "; break;
364*9880d681SAndroid Build Coastguard Worker case SDep::Order: dbgs() << "ch "; break;
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker dbgs() << "SU(" << I->getSUnit()->NodeNum << ")";
367*9880d681SAndroid Build Coastguard Worker if (I->isArtificial())
368*9880d681SAndroid Build Coastguard Worker dbgs() << " *";
369*9880d681SAndroid Build Coastguard Worker dbgs() << ": Latency=" << I->getLatency();
370*9880d681SAndroid Build Coastguard Worker if (I->isAssignedRegDep())
371*9880d681SAndroid Build Coastguard Worker dbgs() << " Reg=" << PrintReg(I->getReg(), G->TRI);
372*9880d681SAndroid Build Coastguard Worker dbgs() << "\n";
373*9880d681SAndroid Build Coastguard Worker }
374*9880d681SAndroid Build Coastguard Worker }
375*9880d681SAndroid Build Coastguard Worker }
376*9880d681SAndroid Build Coastguard Worker #endif
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
379*9880d681SAndroid Build Coastguard Worker /// VerifyScheduledDAG - Verify that all SUnits were scheduled and that
380*9880d681SAndroid Build Coastguard Worker /// their state is consistent. Return the number of scheduled nodes.
381*9880d681SAndroid Build Coastguard Worker ///
VerifyScheduledDAG(bool isBottomUp)382*9880d681SAndroid Build Coastguard Worker unsigned ScheduleDAG::VerifyScheduledDAG(bool isBottomUp) {
383*9880d681SAndroid Build Coastguard Worker bool AnyNotSched = false;
384*9880d681SAndroid Build Coastguard Worker unsigned DeadNodes = 0;
385*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
386*9880d681SAndroid Build Coastguard Worker if (!SUnits[i].isScheduled) {
387*9880d681SAndroid Build Coastguard Worker if (SUnits[i].NumPreds == 0 && SUnits[i].NumSuccs == 0) {
388*9880d681SAndroid Build Coastguard Worker ++DeadNodes;
389*9880d681SAndroid Build Coastguard Worker continue;
390*9880d681SAndroid Build Coastguard Worker }
391*9880d681SAndroid Build Coastguard Worker if (!AnyNotSched)
392*9880d681SAndroid Build Coastguard Worker dbgs() << "*** Scheduling failed! ***\n";
393*9880d681SAndroid Build Coastguard Worker SUnits[i].dump(this);
394*9880d681SAndroid Build Coastguard Worker dbgs() << "has not been scheduled!\n";
395*9880d681SAndroid Build Coastguard Worker AnyNotSched = true;
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker if (SUnits[i].isScheduled &&
398*9880d681SAndroid Build Coastguard Worker (isBottomUp ? SUnits[i].getHeight() : SUnits[i].getDepth()) >
399*9880d681SAndroid Build Coastguard Worker unsigned(INT_MAX)) {
400*9880d681SAndroid Build Coastguard Worker if (!AnyNotSched)
401*9880d681SAndroid Build Coastguard Worker dbgs() << "*** Scheduling failed! ***\n";
402*9880d681SAndroid Build Coastguard Worker SUnits[i].dump(this);
403*9880d681SAndroid Build Coastguard Worker dbgs() << "has an unexpected "
404*9880d681SAndroid Build Coastguard Worker << (isBottomUp ? "Height" : "Depth") << " value!\n";
405*9880d681SAndroid Build Coastguard Worker AnyNotSched = true;
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker if (isBottomUp) {
408*9880d681SAndroid Build Coastguard Worker if (SUnits[i].NumSuccsLeft != 0) {
409*9880d681SAndroid Build Coastguard Worker if (!AnyNotSched)
410*9880d681SAndroid Build Coastguard Worker dbgs() << "*** Scheduling failed! ***\n";
411*9880d681SAndroid Build Coastguard Worker SUnits[i].dump(this);
412*9880d681SAndroid Build Coastguard Worker dbgs() << "has successors left!\n";
413*9880d681SAndroid Build Coastguard Worker AnyNotSched = true;
414*9880d681SAndroid Build Coastguard Worker }
415*9880d681SAndroid Build Coastguard Worker } else {
416*9880d681SAndroid Build Coastguard Worker if (SUnits[i].NumPredsLeft != 0) {
417*9880d681SAndroid Build Coastguard Worker if (!AnyNotSched)
418*9880d681SAndroid Build Coastguard Worker dbgs() << "*** Scheduling failed! ***\n";
419*9880d681SAndroid Build Coastguard Worker SUnits[i].dump(this);
420*9880d681SAndroid Build Coastguard Worker dbgs() << "has predecessors left!\n";
421*9880d681SAndroid Build Coastguard Worker AnyNotSched = true;
422*9880d681SAndroid Build Coastguard Worker }
423*9880d681SAndroid Build Coastguard Worker }
424*9880d681SAndroid Build Coastguard Worker }
425*9880d681SAndroid Build Coastguard Worker assert(!AnyNotSched);
426*9880d681SAndroid Build Coastguard Worker return SUnits.size() - DeadNodes;
427*9880d681SAndroid Build Coastguard Worker }
428*9880d681SAndroid Build Coastguard Worker #endif
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard Worker /// InitDAGTopologicalSorting - create the initial topological
431*9880d681SAndroid Build Coastguard Worker /// ordering from the DAG to be scheduled.
432*9880d681SAndroid Build Coastguard Worker ///
433*9880d681SAndroid Build Coastguard Worker /// The idea of the algorithm is taken from
434*9880d681SAndroid Build Coastguard Worker /// "Online algorithms for managing the topological order of
435*9880d681SAndroid Build Coastguard Worker /// a directed acyclic graph" by David J. Pearce and Paul H.J. Kelly
436*9880d681SAndroid Build Coastguard Worker /// This is the MNR algorithm, which was first introduced by
437*9880d681SAndroid Build Coastguard Worker /// A. Marchetti-Spaccamela, U. Nanni and H. Rohnert in
438*9880d681SAndroid Build Coastguard Worker /// "Maintaining a topological order under edge insertions".
439*9880d681SAndroid Build Coastguard Worker ///
440*9880d681SAndroid Build Coastguard Worker /// Short description of the algorithm:
441*9880d681SAndroid Build Coastguard Worker ///
442*9880d681SAndroid Build Coastguard Worker /// Topological ordering, ord, of a DAG maps each node to a topological
443*9880d681SAndroid Build Coastguard Worker /// index so that for all edges X->Y it is the case that ord(X) < ord(Y).
444*9880d681SAndroid Build Coastguard Worker ///
445*9880d681SAndroid Build Coastguard Worker /// This means that if there is a path from the node X to the node Z,
446*9880d681SAndroid Build Coastguard Worker /// then ord(X) < ord(Z).
447*9880d681SAndroid Build Coastguard Worker ///
448*9880d681SAndroid Build Coastguard Worker /// This property can be used to check for reachability of nodes:
449*9880d681SAndroid Build Coastguard Worker /// if Z is reachable from X, then an insertion of the edge Z->X would
450*9880d681SAndroid Build Coastguard Worker /// create a cycle.
451*9880d681SAndroid Build Coastguard Worker ///
452*9880d681SAndroid Build Coastguard Worker /// The algorithm first computes a topological ordering for the DAG by
453*9880d681SAndroid Build Coastguard Worker /// initializing the Index2Node and Node2Index arrays and then tries to keep
454*9880d681SAndroid Build Coastguard Worker /// the ordering up-to-date after edge insertions by reordering the DAG.
455*9880d681SAndroid Build Coastguard Worker ///
456*9880d681SAndroid Build Coastguard Worker /// On insertion of the edge X->Y, the algorithm first marks by calling DFS
457*9880d681SAndroid Build Coastguard Worker /// the nodes reachable from Y, and then shifts them using Shift to lie
458*9880d681SAndroid Build Coastguard Worker /// immediately after X in Index2Node.
InitDAGTopologicalSorting()459*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::InitDAGTopologicalSorting() {
460*9880d681SAndroid Build Coastguard Worker unsigned DAGSize = SUnits.size();
461*9880d681SAndroid Build Coastguard Worker std::vector<SUnit*> WorkList;
462*9880d681SAndroid Build Coastguard Worker WorkList.reserve(DAGSize);
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker Index2Node.resize(DAGSize);
465*9880d681SAndroid Build Coastguard Worker Node2Index.resize(DAGSize);
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker // Initialize the data structures.
468*9880d681SAndroid Build Coastguard Worker if (ExitSU)
469*9880d681SAndroid Build Coastguard Worker WorkList.push_back(ExitSU);
470*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = DAGSize; i != e; ++i) {
471*9880d681SAndroid Build Coastguard Worker SUnit *SU = &SUnits[i];
472*9880d681SAndroid Build Coastguard Worker int NodeNum = SU->NodeNum;
473*9880d681SAndroid Build Coastguard Worker unsigned Degree = SU->Succs.size();
474*9880d681SAndroid Build Coastguard Worker // Temporarily use the Node2Index array as scratch space for degree counts.
475*9880d681SAndroid Build Coastguard Worker Node2Index[NodeNum] = Degree;
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker // Is it a node without dependencies?
478*9880d681SAndroid Build Coastguard Worker if (Degree == 0) {
479*9880d681SAndroid Build Coastguard Worker assert(SU->Succs.empty() && "SUnit should have no successors");
480*9880d681SAndroid Build Coastguard Worker // Collect leaf nodes.
481*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SU);
482*9880d681SAndroid Build Coastguard Worker }
483*9880d681SAndroid Build Coastguard Worker }
484*9880d681SAndroid Build Coastguard Worker
485*9880d681SAndroid Build Coastguard Worker int Id = DAGSize;
486*9880d681SAndroid Build Coastguard Worker while (!WorkList.empty()) {
487*9880d681SAndroid Build Coastguard Worker SUnit *SU = WorkList.back();
488*9880d681SAndroid Build Coastguard Worker WorkList.pop_back();
489*9880d681SAndroid Build Coastguard Worker if (SU->NodeNum < DAGSize)
490*9880d681SAndroid Build Coastguard Worker Allocate(SU->NodeNum, --Id);
491*9880d681SAndroid Build Coastguard Worker for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
492*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
493*9880d681SAndroid Build Coastguard Worker SUnit *SU = I->getSUnit();
494*9880d681SAndroid Build Coastguard Worker if (SU->NodeNum < DAGSize && !--Node2Index[SU->NodeNum])
495*9880d681SAndroid Build Coastguard Worker // If all dependencies of the node are processed already,
496*9880d681SAndroid Build Coastguard Worker // then the node can be computed now.
497*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SU);
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker }
500*9880d681SAndroid Build Coastguard Worker
501*9880d681SAndroid Build Coastguard Worker Visited.resize(DAGSize);
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
504*9880d681SAndroid Build Coastguard Worker // Check correctness of the ordering
505*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = DAGSize; i != e; ++i) {
506*9880d681SAndroid Build Coastguard Worker SUnit *SU = &SUnits[i];
507*9880d681SAndroid Build Coastguard Worker for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
508*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
509*9880d681SAndroid Build Coastguard Worker assert(Node2Index[SU->NodeNum] > Node2Index[I->getSUnit()->NodeNum] &&
510*9880d681SAndroid Build Coastguard Worker "Wrong topological sorting");
511*9880d681SAndroid Build Coastguard Worker }
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker #endif
514*9880d681SAndroid Build Coastguard Worker }
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard Worker /// AddPred - Updates the topological ordering to accommodate an edge
517*9880d681SAndroid Build Coastguard Worker /// to be added from SUnit X to SUnit Y.
AddPred(SUnit * Y,SUnit * X)518*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::AddPred(SUnit *Y, SUnit *X) {
519*9880d681SAndroid Build Coastguard Worker int UpperBound, LowerBound;
520*9880d681SAndroid Build Coastguard Worker LowerBound = Node2Index[Y->NodeNum];
521*9880d681SAndroid Build Coastguard Worker UpperBound = Node2Index[X->NodeNum];
522*9880d681SAndroid Build Coastguard Worker bool HasLoop = false;
523*9880d681SAndroid Build Coastguard Worker // Is Ord(X) < Ord(Y) ?
524*9880d681SAndroid Build Coastguard Worker if (LowerBound < UpperBound) {
525*9880d681SAndroid Build Coastguard Worker // Update the topological order.
526*9880d681SAndroid Build Coastguard Worker Visited.reset();
527*9880d681SAndroid Build Coastguard Worker DFS(Y, UpperBound, HasLoop);
528*9880d681SAndroid Build Coastguard Worker assert(!HasLoop && "Inserted edge creates a loop!");
529*9880d681SAndroid Build Coastguard Worker // Recompute topological indexes.
530*9880d681SAndroid Build Coastguard Worker Shift(Visited, LowerBound, UpperBound);
531*9880d681SAndroid Build Coastguard Worker }
532*9880d681SAndroid Build Coastguard Worker }
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker /// RemovePred - Updates the topological ordering to accommodate an
535*9880d681SAndroid Build Coastguard Worker /// an edge to be removed from the specified node N from the predecessors
536*9880d681SAndroid Build Coastguard Worker /// of the current node M.
RemovePred(SUnit * M,SUnit * N)537*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::RemovePred(SUnit *M, SUnit *N) {
538*9880d681SAndroid Build Coastguard Worker // InitDAGTopologicalSorting();
539*9880d681SAndroid Build Coastguard Worker }
540*9880d681SAndroid Build Coastguard Worker
541*9880d681SAndroid Build Coastguard Worker /// DFS - Make a DFS traversal to mark all nodes reachable from SU and mark
542*9880d681SAndroid Build Coastguard Worker /// all nodes affected by the edge insertion. These nodes will later get new
543*9880d681SAndroid Build Coastguard Worker /// topological indexes by means of the Shift method.
DFS(const SUnit * SU,int UpperBound,bool & HasLoop)544*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::DFS(const SUnit *SU, int UpperBound,
545*9880d681SAndroid Build Coastguard Worker bool &HasLoop) {
546*9880d681SAndroid Build Coastguard Worker std::vector<const SUnit*> WorkList;
547*9880d681SAndroid Build Coastguard Worker WorkList.reserve(SUnits.size());
548*9880d681SAndroid Build Coastguard Worker
549*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SU);
550*9880d681SAndroid Build Coastguard Worker do {
551*9880d681SAndroid Build Coastguard Worker SU = WorkList.back();
552*9880d681SAndroid Build Coastguard Worker WorkList.pop_back();
553*9880d681SAndroid Build Coastguard Worker Visited.set(SU->NodeNum);
554*9880d681SAndroid Build Coastguard Worker for (int I = SU->Succs.size()-1; I >= 0; --I) {
555*9880d681SAndroid Build Coastguard Worker unsigned s = SU->Succs[I].getSUnit()->NodeNum;
556*9880d681SAndroid Build Coastguard Worker // Edges to non-SUnits are allowed but ignored (e.g. ExitSU).
557*9880d681SAndroid Build Coastguard Worker if (s >= Node2Index.size())
558*9880d681SAndroid Build Coastguard Worker continue;
559*9880d681SAndroid Build Coastguard Worker if (Node2Index[s] == UpperBound) {
560*9880d681SAndroid Build Coastguard Worker HasLoop = true;
561*9880d681SAndroid Build Coastguard Worker return;
562*9880d681SAndroid Build Coastguard Worker }
563*9880d681SAndroid Build Coastguard Worker // Visit successors if not already and in affected region.
564*9880d681SAndroid Build Coastguard Worker if (!Visited.test(s) && Node2Index[s] < UpperBound) {
565*9880d681SAndroid Build Coastguard Worker WorkList.push_back(SU->Succs[I].getSUnit());
566*9880d681SAndroid Build Coastguard Worker }
567*9880d681SAndroid Build Coastguard Worker }
568*9880d681SAndroid Build Coastguard Worker } while (!WorkList.empty());
569*9880d681SAndroid Build Coastguard Worker }
570*9880d681SAndroid Build Coastguard Worker
571*9880d681SAndroid Build Coastguard Worker /// Shift - Renumber the nodes so that the topological ordering is
572*9880d681SAndroid Build Coastguard Worker /// preserved.
Shift(BitVector & Visited,int LowerBound,int UpperBound)573*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::Shift(BitVector& Visited, int LowerBound,
574*9880d681SAndroid Build Coastguard Worker int UpperBound) {
575*9880d681SAndroid Build Coastguard Worker std::vector<int> L;
576*9880d681SAndroid Build Coastguard Worker int shift = 0;
577*9880d681SAndroid Build Coastguard Worker int i;
578*9880d681SAndroid Build Coastguard Worker
579*9880d681SAndroid Build Coastguard Worker for (i = LowerBound; i <= UpperBound; ++i) {
580*9880d681SAndroid Build Coastguard Worker // w is node at topological index i.
581*9880d681SAndroid Build Coastguard Worker int w = Index2Node[i];
582*9880d681SAndroid Build Coastguard Worker if (Visited.test(w)) {
583*9880d681SAndroid Build Coastguard Worker // Unmark.
584*9880d681SAndroid Build Coastguard Worker Visited.reset(w);
585*9880d681SAndroid Build Coastguard Worker L.push_back(w);
586*9880d681SAndroid Build Coastguard Worker shift = shift + 1;
587*9880d681SAndroid Build Coastguard Worker } else {
588*9880d681SAndroid Build Coastguard Worker Allocate(w, i - shift);
589*9880d681SAndroid Build Coastguard Worker }
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker
592*9880d681SAndroid Build Coastguard Worker for (unsigned j = 0; j < L.size(); ++j) {
593*9880d681SAndroid Build Coastguard Worker Allocate(L[j], i - shift);
594*9880d681SAndroid Build Coastguard Worker i = i + 1;
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker }
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard Worker
599*9880d681SAndroid Build Coastguard Worker /// WillCreateCycle - Returns true if adding an edge to TargetSU from SU will
600*9880d681SAndroid Build Coastguard Worker /// create a cycle. If so, it is not safe to call AddPred(TargetSU, SU).
WillCreateCycle(SUnit * TargetSU,SUnit * SU)601*9880d681SAndroid Build Coastguard Worker bool ScheduleDAGTopologicalSort::WillCreateCycle(SUnit *TargetSU, SUnit *SU) {
602*9880d681SAndroid Build Coastguard Worker // Is SU reachable from TargetSU via successor edges?
603*9880d681SAndroid Build Coastguard Worker if (IsReachable(SU, TargetSU))
604*9880d681SAndroid Build Coastguard Worker return true;
605*9880d681SAndroid Build Coastguard Worker for (SUnit::pred_iterator
606*9880d681SAndroid Build Coastguard Worker I = TargetSU->Preds.begin(), E = TargetSU->Preds.end(); I != E; ++I)
607*9880d681SAndroid Build Coastguard Worker if (I->isAssignedRegDep() &&
608*9880d681SAndroid Build Coastguard Worker IsReachable(SU, I->getSUnit()))
609*9880d681SAndroid Build Coastguard Worker return true;
610*9880d681SAndroid Build Coastguard Worker return false;
611*9880d681SAndroid Build Coastguard Worker }
612*9880d681SAndroid Build Coastguard Worker
613*9880d681SAndroid Build Coastguard Worker /// IsReachable - Checks if SU is reachable from TargetSU.
IsReachable(const SUnit * SU,const SUnit * TargetSU)614*9880d681SAndroid Build Coastguard Worker bool ScheduleDAGTopologicalSort::IsReachable(const SUnit *SU,
615*9880d681SAndroid Build Coastguard Worker const SUnit *TargetSU) {
616*9880d681SAndroid Build Coastguard Worker // If insertion of the edge SU->TargetSU would create a cycle
617*9880d681SAndroid Build Coastguard Worker // then there is a path from TargetSU to SU.
618*9880d681SAndroid Build Coastguard Worker int UpperBound, LowerBound;
619*9880d681SAndroid Build Coastguard Worker LowerBound = Node2Index[TargetSU->NodeNum];
620*9880d681SAndroid Build Coastguard Worker UpperBound = Node2Index[SU->NodeNum];
621*9880d681SAndroid Build Coastguard Worker bool HasLoop = false;
622*9880d681SAndroid Build Coastguard Worker // Is Ord(TargetSU) < Ord(SU) ?
623*9880d681SAndroid Build Coastguard Worker if (LowerBound < UpperBound) {
624*9880d681SAndroid Build Coastguard Worker Visited.reset();
625*9880d681SAndroid Build Coastguard Worker // There may be a path from TargetSU to SU. Check for it.
626*9880d681SAndroid Build Coastguard Worker DFS(TargetSU, UpperBound, HasLoop);
627*9880d681SAndroid Build Coastguard Worker }
628*9880d681SAndroid Build Coastguard Worker return HasLoop;
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker /// Allocate - assign the topological index to the node n.
Allocate(int n,int index)632*9880d681SAndroid Build Coastguard Worker void ScheduleDAGTopologicalSort::Allocate(int n, int index) {
633*9880d681SAndroid Build Coastguard Worker Node2Index[n] = index;
634*9880d681SAndroid Build Coastguard Worker Index2Node[index] = n;
635*9880d681SAndroid Build Coastguard Worker }
636*9880d681SAndroid Build Coastguard Worker
637*9880d681SAndroid Build Coastguard Worker ScheduleDAGTopologicalSort::
ScheduleDAGTopologicalSort(std::vector<SUnit> & sunits,SUnit * exitsu)638*9880d681SAndroid Build Coastguard Worker ScheduleDAGTopologicalSort(std::vector<SUnit> &sunits, SUnit *exitsu)
639*9880d681SAndroid Build Coastguard Worker : SUnits(sunits), ExitSU(exitsu) {}
640*9880d681SAndroid Build Coastguard Worker
~ScheduleHazardRecognizer()641*9880d681SAndroid Build Coastguard Worker ScheduleHazardRecognizer::~ScheduleHazardRecognizer() {}
642