blob: ac7f6b9f56f4f02fa9eaf37876027af4c1e937b8 [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
Chris Lattnerd32b2362005-08-18 18:45:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerd32b2362005-08-18 18:45:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohman5e76c3b2008-07-11 22:39:58 +000010// This implements the ScheduleDAG class, which is a base class used by
11// scheduling implementation classes.
Chris Lattnerd32b2362005-08-18 18:45:24 +000012//
13//===----------------------------------------------------------------------===//
14
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000015#define DEBUG_TYPE "pre-RA-sched"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000016#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000017#include "llvm/Target/TargetMachine.h"
18#include "llvm/Target/TargetInstrInfo.h"
Dan Gohman94b8d7e2008-09-03 16:01:59 +000019#include "llvm/Target/TargetRegisterInfo.h"
Evan Chenge165a782006-05-11 23:55:42 +000020#include "llvm/Support/Debug.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000021using namespace llvm;
22
Dan Gohmana23b3b82008-11-13 21:21:28 +000023ScheduleDAG::ScheduleDAG(SelectionDAG *dag, MachineBasicBlock *bb,
Chris Lattner84bc5422007-12-31 04:13:23 +000024 const TargetMachine &tm)
Evan Cheng9e233362008-03-12 22:19:41 +000025 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +000026 TII = TM.getInstrInfo();
Dan Gohmaneb9dbf12008-11-11 21:31:56 +000027 MF = BB->getParent();
Evan Cheng8a50f1f2008-04-03 16:36:07 +000028 TRI = TM.getRegisterInfo();
Dan Gohmaneb9dbf12008-11-11 21:31:56 +000029 TLI = TM.getTargetLowering();
30 ConstPool = MF->getConstantPool();
Chris Lattner84bc5422007-12-31 04:13:23 +000031}
Evan Chenga6fb1b62007-09-25 01:54:36 +000032
Evan Chenga6fb1b62007-09-25 01:54:36 +000033/// CheckForPhysRegDependency - Check if the dependency between def and use of
34/// a specified operand is a physical register dependency. If so, returns the
35/// register and the cost of copying the register.
Dan Gohman89684502008-07-27 20:43:25 +000036static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000037 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000038 const TargetInstrInfo *TII,
39 unsigned &PhysReg, int &Cost) {
Dan Gohman89684502008-07-27 20:43:25 +000040 if (Op != 2 || User->getOpcode() != ISD::CopyToReg)
Evan Chenga6fb1b62007-09-25 01:54:36 +000041 return;
42
Dan Gohman89684502008-07-27 20:43:25 +000043 unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000044 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000045 return;
46
Gabor Greif99a6cb92008-08-26 22:36:50 +000047 unsigned ResNo = User->getOperand(2).getResNo();
Dan Gohmane8be6c62008-07-17 19:10:17 +000048 if (Def->isMachineOpcode()) {
49 const TargetInstrDesc &II = TII->get(Def->getMachineOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000050 if (ResNo >= II.getNumDefs() &&
51 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000052 PhysReg = Reg;
53 const TargetRegisterClass *RC =
Evan Cheng676dd7c2008-03-11 07:19:34 +000054 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Chenga6fb1b62007-09-25 01:54:36 +000055 Cost = RC->getCopyCost();
56 }
57 }
58}
59
60SUnit *ScheduleDAG::Clone(SUnit *Old) {
Dan Gohman550f5af2008-11-13 21:36:12 +000061 SUnit *SU = NewSUnit(Old->getNode());
Dan Gohman4c8c8302008-06-21 15:52:51 +000062 SU->OrigNode = Old->OrigNode;
Evan Chenga6fb1b62007-09-25 01:54:36 +000063 SU->Latency = Old->Latency;
64 SU->isTwoAddress = Old->isTwoAddress;
65 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000066 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000067 return SU;
68}
69
Evan Chengf10c9732007-10-05 01:39:18 +000070
Evan Chenge165a782006-05-11 23:55:42 +000071/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
72/// This SUnit graph is similar to the SelectionDAG, but represents flagged
73/// together nodes with a single SUnit.
74void ScheduleDAG::BuildSchedUnits() {
Dan Gohman7e6a1bc2008-11-14 21:47:58 +000075 // For post-regalloc scheduling, build the SUnits from the MachineInstrs
76 // in the MachineBasicBlock.
77 if (!DAG) {
78 BuildSchedUnitsFromMBB();
79 return;
80 }
81
Evan Chenge165a782006-05-11 23:55:42 +000082 // Reserve entries in the vector for each of the SUnits we are creating. This
83 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
84 // invalidated.
Dan Gohmana23b3b82008-11-13 21:21:28 +000085 SUnits.reserve(DAG->allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +000086
Dan Gohman94d7a5f2008-06-21 19:18:17 +000087 // During scheduling, the NodeId field of SDNode is used to map SDNodes
88 // to their associated SUnits by holding SUnits table indices. A value
89 // of -1 means the SDNode does not yet have an associated SUnit.
Dan Gohmana23b3b82008-11-13 21:21:28 +000090 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
91 E = DAG->allnodes_end(); NI != E; ++NI)
Dan Gohman94d7a5f2008-06-21 19:18:17 +000092 NI->setNodeId(-1);
93
Dan Gohmana23b3b82008-11-13 21:21:28 +000094 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
95 E = DAG->allnodes_end(); NI != E; ++NI) {
Evan Chenge165a782006-05-11 23:55:42 +000096 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
97 continue;
98
99 // If this node has already been processed, stop now.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000100 if (NI->getNodeId() != -1) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000101
102 SUnit *NodeSUnit = NewSUnit(NI);
103
104 // See if anything is flagged to this node, if so, add them to flagged
105 // nodes. Nodes can have at most one flag input and one flag output. Flags
106 // are required the be the last operand and result of a node.
107
Dan Gohmand23e0f82008-11-13 23:24:17 +0000108 // Scan up to find flagged preds.
Evan Chenge165a782006-05-11 23:55:42 +0000109 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000110 if (N->getNumOperands() &&
111 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
112 do {
Gabor Greifba36cb52008-08-28 21:40:38 +0000113 N = N->getOperand(N->getNumOperands()-1).getNode();
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000114 assert(N->getNodeId() == -1 && "Node already inserted!");
115 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000116 } while (N->getNumOperands() &&
117 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
Evan Chenge165a782006-05-11 23:55:42 +0000118 }
119
Dan Gohmand23e0f82008-11-13 23:24:17 +0000120 // Scan down to find any flagged succs.
Evan Chenge165a782006-05-11 23:55:42 +0000121 N = NI;
122 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue FlagVal(N, N->getNumValues()-1);
Evan Chenge165a782006-05-11 23:55:42 +0000124
125 // There are either zero or one users of the Flag result.
126 bool HasFlagUse = false;
127 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
128 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +0000129 if (FlagVal.isOperandOf(*UI)) {
Evan Chenge165a782006-05-11 23:55:42 +0000130 HasFlagUse = true;
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000131 assert(N->getNodeId() == -1 && "Node already inserted!");
132 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman89684502008-07-27 20:43:25 +0000133 N = *UI;
Evan Chenge165a782006-05-11 23:55:42 +0000134 break;
135 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000136 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000137 }
138
Dan Gohmand23e0f82008-11-13 23:24:17 +0000139 // If there are flag operands involved, N is now the bottom-most node
140 // of the sequence of nodes that are flagged together.
141 // Update the SUnit.
Dan Gohman550f5af2008-11-13 21:36:12 +0000142 NodeSUnit->setNode(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000143 assert(N->getNodeId() == -1 && "Node already inserted!");
144 N->setNodeId(NodeSUnit->NodeNum);
Evan Chengf10c9732007-10-05 01:39:18 +0000145
146 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000147 }
148
149 // Pass 2: add the preds, succs, etc.
150 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
151 SUnit *SU = &SUnits[su];
Dan Gohman550f5af2008-11-13 21:36:12 +0000152 SDNode *MainNode = SU->getNode();
Evan Chenge165a782006-05-11 23:55:42 +0000153
Dan Gohmane8be6c62008-07-17 19:10:17 +0000154 if (MainNode->isMachineOpcode()) {
155 unsigned Opc = MainNode->getMachineOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000156 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000157 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000158 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000159 SU->isTwoAddress = true;
160 break;
161 }
162 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000163 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000164 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000165 }
166
167 // Find all predecessors and successors of the group.
Dan Gohmand23e0f82008-11-13 23:24:17 +0000168 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
Dan Gohmane8be6c62008-07-17 19:10:17 +0000169 if (N->isMachineOpcode() &&
170 TII->get(N->getMachineOpcode()).getImplicitDefs() &&
171 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000172 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000173
174 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000175 SDNode *OpN = N->getOperand(i).getNode();
Evan Chenge165a782006-05-11 23:55:42 +0000176 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000177 SUnit *OpSU = &SUnits[OpN->getNodeId()];
Evan Chenge165a782006-05-11 23:55:42 +0000178 assert(OpSU && "Node has no SUnit!");
179 if (OpSU == SU) continue; // In the same group.
180
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000182 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
183 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000184
185 unsigned PhysReg = 0;
186 int Cost = 1;
187 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000188 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000189 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000190 }
191 }
Evan Chenge165a782006-05-11 23:55:42 +0000192 }
Evan Chenge165a782006-05-11 23:55:42 +0000193}
194
Dan Gohman7e6a1bc2008-11-14 21:47:58 +0000195void ScheduleDAG::BuildSchedUnitsFromMBB() {
196 SUnits.clear();
197 SUnits.reserve(BB->size());
198
199 std::vector<SUnit *> PendingLoads;
200 SUnit *Terminator = 0;
201 SUnit *Chain = 0;
202 SUnit *Defs[TargetRegisterInfo::FirstVirtualRegister] = {};
203 std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister] = {};
204 int Cost = 1; // FIXME
205
206 for (MachineBasicBlock::iterator MII = BB->end(), MIE = BB->begin();
207 MII != MIE; --MII) {
208 MachineInstr *MI = prior(MII);
209 SUnit *SU = NewSUnit(MI);
210
211 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
212 const MachineOperand &MO = MI->getOperand(j);
213 if (!MO.isReg()) continue;
214 unsigned Reg = MO.getReg();
215 if (Reg == 0) continue;
216
217 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
218 std::vector<SUnit *> &UseList = Uses[Reg];
219 SUnit *&Def = Defs[Reg];
220 // Optionally add output and anti dependences
221 if (Def && Def != SU)
222 Def->addPred(SU, /*isCtrl=*/true, /*isSpecial=*/false,
223 /*PhyReg=*/Reg, Cost);
224 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
225 SUnit *&Def = Defs[*Alias];
226 if (Def && Def != SU)
227 Def->addPred(SU, /*isCtrl=*/true, /*isSpecial=*/false,
228 /*PhyReg=*/*Alias, Cost);
229 }
230
231 if (MO.isDef()) {
232 // Add any data dependencies.
233 for (unsigned i = 0, e = UseList.size(); i != e; ++i)
234 if (UseList[i] != SU)
235 UseList[i]->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false,
236 /*PhysReg=*/Reg, Cost);
237 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
238 std::vector<SUnit *> &UseList = Uses[*Alias];
239 for (unsigned i = 0, e = UseList.size(); i != e; ++i)
240 if (UseList[i] != SU)
241 UseList[i]->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false,
242 /*PhysReg=*/*Alias, Cost);
243 }
244
245 UseList.clear();
246 Def = SU;
247 } else {
248 UseList.push_back(SU);
249 }
250 }
251 bool False = false;
252 bool True = true;
253 if (!MI->isSafeToMove(TII, False)) {
254 if (Chain)
255 Chain->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false);
256 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
257 PendingLoads[k]->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false);
258 PendingLoads.clear();
259 Chain = SU;
260 } else if (!MI->isSafeToMove(TII, True)) {
261 if (Chain)
262 Chain->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false);
263 PendingLoads.push_back(SU);
264 }
265 if (Terminator && SU->Succs.empty())
266 Terminator->addPred(SU, /*isCtrl=*/false, /*isSpecial=*/false);
267 if (MI->getDesc().isTerminator())
268 Terminator = SU;
269 }
270}
271
Evan Chengf10c9732007-10-05 01:39:18 +0000272void ScheduleDAG::ComputeLatency(SUnit *SU) {
273 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
274
275 // Compute the latency for the node. We use the sum of the latencies for
276 // all nodes flagged together into this SUnit.
277 if (InstrItins.isEmpty()) {
278 // No latency information.
279 SU->Latency = 1;
Evan Chengc6be7772008-07-02 09:23:51 +0000280 return;
281 }
282
283 SU->Latency = 0;
Dan Gohmand23e0f82008-11-13 23:24:17 +0000284 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
285 if (N->isMachineOpcode()) {
286 unsigned SchedClass = TII->get(N->getMachineOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000287 const InstrStage *S = InstrItins.begin(SchedClass);
288 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000289 for (; S != E; ++S)
290 SU->Latency += S->Cycles;
291 }
Evan Chengf10c9732007-10-05 01:39:18 +0000292 }
293}
294
Roman Levensteind86449e2008-03-04 11:19:43 +0000295/// CalculateDepths - compute depths using algorithms for the longest
296/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000297void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000298 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000299 std::vector<SUnit*> WorkList;
300 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000301
Roman Levensteind86449e2008-03-04 11:19:43 +0000302 // Initialize the data structures
303 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
304 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000305 unsigned Degree = SU->Preds.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000306 // Temporarily use the Depth field as scratch space for the degree count.
307 SU->Depth = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000308
309 // Is it a node without dependencies?
310 if (Degree == 0) {
311 assert(SU->Preds.empty() && "SUnit should have no predecessors");
312 // Collect leaf nodes
313 WorkList.push_back(SU);
314 }
315 }
316
317 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000318 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000319 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000320 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000321 unsigned SUDepth = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000322
323 // Use dynamic programming:
324 // When current node is being processed, all of its dependencies
325 // are already processed.
326 // So, just iterate over all predecessors and take the longest path
327 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
328 I != E; ++I) {
329 unsigned PredDepth = I->Dep->Depth;
330 if (PredDepth+1 > SUDepth) {
331 SUDepth = PredDepth + 1;
332 }
333 }
334
Dan Gohman3a09d892008-08-27 16:27:25 +0000335 SU->Depth = SUDepth;
336
337 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000338 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
339 I != E; ++I) {
340 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000341 if (!--SU->Depth)
Roman Levensteind86449e2008-03-04 11:19:43 +0000342 // If all dependencies of the node are processed already,
343 // then the longest path for the node can be computed now
344 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000345 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000346 }
Evan Chenge165a782006-05-11 23:55:42 +0000347}
Evan Cheng99126282007-07-06 01:37:28 +0000348
Roman Levensteind86449e2008-03-04 11:19:43 +0000349/// CalculateHeights - compute heights using algorithms for the longest
350/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000351void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000352 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000353 std::vector<SUnit*> WorkList;
354 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000355
Roman Levensteind86449e2008-03-04 11:19:43 +0000356 // Initialize the data structures
357 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
358 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000359 unsigned Degree = SU->Succs.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000360 // Temporarily use the Height field as scratch space for the degree count.
361 SU->Height = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000362
363 // Is it a node without dependencies?
364 if (Degree == 0) {
365 assert(SU->Succs.empty() && "Something wrong");
366 assert(WorkList.empty() && "Should be empty");
367 // Collect leaf nodes
368 WorkList.push_back(SU);
369 }
370 }
371
372 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000373 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000374 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000375 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000376 unsigned SUHeight = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000377
378 // Use dynamic programming:
379 // When current node is being processed, all of its dependencies
380 // are already processed.
381 // So, just iterate over all successors and take the longest path
382 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
383 I != E; ++I) {
384 unsigned SuccHeight = I->Dep->Height;
385 if (SuccHeight+1 > SUHeight) {
386 SUHeight = SuccHeight + 1;
387 }
388 }
389
Dan Gohman3a09d892008-08-27 16:27:25 +0000390 SU->Height = SUHeight;
391
392 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000393 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
394 I != E; ++I) {
395 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000396 if (!--SU->Height)
Roman Levensteind86449e2008-03-04 11:19:43 +0000397 // If all dependencies of the node are processed already,
398 // then the longest path for the node can be computed now
399 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000400 }
401 }
Evan Chenge165a782006-05-11 23:55:42 +0000402}
403
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000404/// CountResults - The results of target nodes have register or immediate
405/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000406/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000407unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000408 unsigned N = Node->getNumValues();
409 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000410 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000411 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000412 --N; // Skip over chain result.
413 return N;
414}
415
Dan Gohman69de1932008-02-06 22:27:42 +0000416/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000417/// followed by special operands that describe memory references, then an
Dan Gohman048ca552008-10-25 17:51:24 +0000418/// optional chain operand, then an optional flag operand. Compute the number
419/// of actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000420unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000421 unsigned N = ComputeMemOperandsEnd(Node);
Gabor Greifba36cb52008-08-28 21:40:38 +0000422 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
Dan Gohman36b5c132008-04-07 19:35:22 +0000423 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000424 return N;
425}
426
Dan Gohman42a77882008-02-16 00:36:48 +0000427/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
428/// operand
429unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000430 unsigned N = Node->getNumOperands();
431 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
432 --N;
433 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
434 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000435 return N;
436}
437
Evan Chenge165a782006-05-11 23:55:42 +0000438
439/// dump - dump the schedule.
440void ScheduleDAG::dumpSchedule() const {
441 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
442 if (SUnit *SU = Sequence[i])
Dan Gohman3cc62432008-11-18 02:06:40 +0000443 SU->dump(this);
Evan Chenge165a782006-05-11 23:55:42 +0000444 else
Bill Wendling832171c2006-12-07 20:04:42 +0000445 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000446 }
447}
448
449
Evan Chenga9c20912006-01-21 02:32:06 +0000450/// Run - perform scheduling.
451///
Dan Gohman5e843682008-07-14 18:19:29 +0000452void ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +0000453 Schedule();
Dan Gohman5e843682008-07-14 18:19:29 +0000454
455 DOUT << "*** Final schedule ***\n";
456 DEBUG(dumpSchedule());
457 DOUT << "\n";
Chris Lattnerd32b2362005-08-18 18:45:24 +0000458}
Evan Cheng4ef10862006-01-23 07:01:07 +0000459
Evan Chenge165a782006-05-11 23:55:42 +0000460/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
461/// a group of nodes flagged together.
Dan Gohman3cc62432008-11-18 02:06:40 +0000462void SUnit::dump(const ScheduleDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000463 cerr << "SU(" << NodeNum << "): ";
Dan Gohmand23e0f82008-11-13 23:24:17 +0000464 if (getNode())
Dan Gohman3cc62432008-11-18 02:06:40 +0000465 getNode()->dump(G->DAG);
Evan Cheng42d60272007-09-26 21:36:17 +0000466 else
467 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000468 cerr << "\n";
Dan Gohmand23e0f82008-11-13 23:24:17 +0000469 SmallVector<SDNode *, 4> FlaggedNodes;
470 for (SDNode *N = getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
471 FlaggedNodes.push_back(N);
472 while (!FlaggedNodes.empty()) {
473 cerr << " ";
Dan Gohman3cc62432008-11-18 02:06:40 +0000474 FlaggedNodes.back()->dump(G->DAG);
Dan Gohmand23e0f82008-11-13 23:24:17 +0000475 cerr << "\n";
476 FlaggedNodes.pop_back();
Evan Chenge165a782006-05-11 23:55:42 +0000477 }
478}
Evan Cheng4ef10862006-01-23 07:01:07 +0000479
Dan Gohman3cc62432008-11-18 02:06:40 +0000480void SUnit::dumpAll(const ScheduleDAG *G) const {
Evan Chenge165a782006-05-11 23:55:42 +0000481 dump(G);
482
Bill Wendling832171c2006-12-07 20:04:42 +0000483 cerr << " # preds left : " << NumPredsLeft << "\n";
484 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +0000485 cerr << " Latency : " << Latency << "\n";
486 cerr << " Depth : " << Depth << "\n";
487 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000488
489 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000490 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000491 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
492 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000493 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000494 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000495 else
Bill Wendling832171c2006-12-07 20:04:42 +0000496 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000497 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
498 if (I->isSpecial)
499 cerr << " *";
500 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000501 }
502 }
503 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000504 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000505 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
506 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000507 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000508 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000509 else
Bill Wendling832171c2006-12-07 20:04:42 +0000510 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000511 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
512 if (I->isSpecial)
513 cerr << " *";
514 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000515 }
516 }
Bill Wendling832171c2006-12-07 20:04:42 +0000517 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000518}