blob: 6d6b9c7c0f0c95b476b9d335d8abca393260d063 [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() {
75 // Reserve entries in the vector for each of the SUnits we are creating. This
76 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
77 // invalidated.
Dan Gohmana23b3b82008-11-13 21:21:28 +000078 SUnits.reserve(DAG->allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +000079
Dan Gohman94d7a5f2008-06-21 19:18:17 +000080 // During scheduling, the NodeId field of SDNode is used to map SDNodes
81 // to their associated SUnits by holding SUnits table indices. A value
82 // of -1 means the SDNode does not yet have an associated SUnit.
Dan Gohmana23b3b82008-11-13 21:21:28 +000083 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
84 E = DAG->allnodes_end(); NI != E; ++NI)
Dan Gohman94d7a5f2008-06-21 19:18:17 +000085 NI->setNodeId(-1);
86
Dan Gohmana23b3b82008-11-13 21:21:28 +000087 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
88 E = DAG->allnodes_end(); NI != E; ++NI) {
Evan Chenge165a782006-05-11 23:55:42 +000089 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
90 continue;
91
92 // If this node has already been processed, stop now.
Dan Gohman94d7a5f2008-06-21 19:18:17 +000093 if (NI->getNodeId() != -1) continue;
Evan Chenge165a782006-05-11 23:55:42 +000094
95 SUnit *NodeSUnit = NewSUnit(NI);
96
97 // See if anything is flagged to this node, if so, add them to flagged
98 // nodes. Nodes can have at most one flag input and one flag output. Flags
99 // are required the be the last operand and result of a node.
100
Dan Gohmand23e0f82008-11-13 23:24:17 +0000101 // Scan up to find flagged preds.
Evan Chenge165a782006-05-11 23:55:42 +0000102 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000103 if (N->getNumOperands() &&
104 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
105 do {
Gabor Greifba36cb52008-08-28 21:40:38 +0000106 N = N->getOperand(N->getNumOperands()-1).getNode();
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000107 assert(N->getNodeId() == -1 && "Node already inserted!");
108 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000109 } while (N->getNumOperands() &&
110 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
Evan Chenge165a782006-05-11 23:55:42 +0000111 }
112
Dan Gohmand23e0f82008-11-13 23:24:17 +0000113 // Scan down to find any flagged succs.
Evan Chenge165a782006-05-11 23:55:42 +0000114 N = NI;
115 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +0000116 SDValue FlagVal(N, N->getNumValues()-1);
Evan Chenge165a782006-05-11 23:55:42 +0000117
118 // There are either zero or one users of the Flag result.
119 bool HasFlagUse = false;
120 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
121 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +0000122 if (FlagVal.isOperandOf(*UI)) {
Evan Chenge165a782006-05-11 23:55:42 +0000123 HasFlagUse = true;
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000124 assert(N->getNodeId() == -1 && "Node already inserted!");
125 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman89684502008-07-27 20:43:25 +0000126 N = *UI;
Evan Chenge165a782006-05-11 23:55:42 +0000127 break;
128 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000129 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000130 }
131
Dan Gohmand23e0f82008-11-13 23:24:17 +0000132 // If there are flag operands involved, N is now the bottom-most node
133 // of the sequence of nodes that are flagged together.
134 // Update the SUnit.
Dan Gohman550f5af2008-11-13 21:36:12 +0000135 NodeSUnit->setNode(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000136 assert(N->getNodeId() == -1 && "Node already inserted!");
137 N->setNodeId(NodeSUnit->NodeNum);
Evan Chengf10c9732007-10-05 01:39:18 +0000138
139 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000140 }
141
142 // Pass 2: add the preds, succs, etc.
143 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
144 SUnit *SU = &SUnits[su];
Dan Gohman550f5af2008-11-13 21:36:12 +0000145 SDNode *MainNode = SU->getNode();
Evan Chenge165a782006-05-11 23:55:42 +0000146
Dan Gohmane8be6c62008-07-17 19:10:17 +0000147 if (MainNode->isMachineOpcode()) {
148 unsigned Opc = MainNode->getMachineOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000149 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000150 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000151 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000152 SU->isTwoAddress = true;
153 break;
154 }
155 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000156 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000157 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000158 }
159
160 // Find all predecessors and successors of the group.
Dan Gohmand23e0f82008-11-13 23:24:17 +0000161 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
Dan Gohmane8be6c62008-07-17 19:10:17 +0000162 if (N->isMachineOpcode() &&
163 TII->get(N->getMachineOpcode()).getImplicitDefs() &&
164 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000165 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000166
167 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000168 SDNode *OpN = N->getOperand(i).getNode();
Evan Chenge165a782006-05-11 23:55:42 +0000169 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000170 SUnit *OpSU = &SUnits[OpN->getNodeId()];
Evan Chenge165a782006-05-11 23:55:42 +0000171 assert(OpSU && "Node has no SUnit!");
172 if (OpSU == SU) continue; // In the same group.
173
Duncan Sands83ec4b62008-06-06 12:08:01 +0000174 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000175 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
176 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000177
178 unsigned PhysReg = 0;
179 int Cost = 1;
180 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000181 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000182 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000183 }
184 }
Evan Chenge165a782006-05-11 23:55:42 +0000185 }
Evan Chenge165a782006-05-11 23:55:42 +0000186}
187
Evan Chengf10c9732007-10-05 01:39:18 +0000188void ScheduleDAG::ComputeLatency(SUnit *SU) {
189 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
190
191 // Compute the latency for the node. We use the sum of the latencies for
192 // all nodes flagged together into this SUnit.
193 if (InstrItins.isEmpty()) {
194 // No latency information.
195 SU->Latency = 1;
Evan Chengc6be7772008-07-02 09:23:51 +0000196 return;
197 }
198
199 SU->Latency = 0;
Dan Gohmand23e0f82008-11-13 23:24:17 +0000200 for (SDNode *N = SU->getNode(); N; N = N->getFlaggedNode()) {
201 if (N->isMachineOpcode()) {
202 unsigned SchedClass = TII->get(N->getMachineOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000203 const InstrStage *S = InstrItins.begin(SchedClass);
204 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000205 for (; S != E; ++S)
206 SU->Latency += S->Cycles;
207 }
Evan Chengf10c9732007-10-05 01:39:18 +0000208 }
209}
210
Roman Levensteind86449e2008-03-04 11:19:43 +0000211/// CalculateDepths - compute depths using algorithms for the longest
212/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000213void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000214 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000215 std::vector<SUnit*> WorkList;
216 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000217
Roman Levensteind86449e2008-03-04 11:19:43 +0000218 // Initialize the data structures
219 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
220 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000221 unsigned Degree = SU->Preds.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000222 // Temporarily use the Depth field as scratch space for the degree count.
223 SU->Depth = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000224
225 // Is it a node without dependencies?
226 if (Degree == 0) {
227 assert(SU->Preds.empty() && "SUnit should have no predecessors");
228 // Collect leaf nodes
229 WorkList.push_back(SU);
230 }
231 }
232
233 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000234 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000235 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000236 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000237 unsigned SUDepth = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000238
239 // Use dynamic programming:
240 // When current node is being processed, all of its dependencies
241 // are already processed.
242 // So, just iterate over all predecessors and take the longest path
243 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
244 I != E; ++I) {
245 unsigned PredDepth = I->Dep->Depth;
246 if (PredDepth+1 > SUDepth) {
247 SUDepth = PredDepth + 1;
248 }
249 }
250
Dan Gohman3a09d892008-08-27 16:27:25 +0000251 SU->Depth = SUDepth;
252
253 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000254 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
255 I != E; ++I) {
256 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000257 if (!--SU->Depth)
Roman Levensteind86449e2008-03-04 11:19:43 +0000258 // If all dependencies of the node are processed already,
259 // then the longest path for the node can be computed now
260 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000261 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000262 }
Evan Chenge165a782006-05-11 23:55:42 +0000263}
Evan Cheng99126282007-07-06 01:37:28 +0000264
Roman Levensteind86449e2008-03-04 11:19:43 +0000265/// CalculateHeights - compute heights using algorithms for the longest
266/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000267void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000268 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000269 std::vector<SUnit*> WorkList;
270 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000271
Roman Levensteind86449e2008-03-04 11:19:43 +0000272 // Initialize the data structures
273 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
274 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000275 unsigned Degree = SU->Succs.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000276 // Temporarily use the Height field as scratch space for the degree count.
277 SU->Height = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000278
279 // Is it a node without dependencies?
280 if (Degree == 0) {
281 assert(SU->Succs.empty() && "Something wrong");
282 assert(WorkList.empty() && "Should be empty");
283 // Collect leaf nodes
284 WorkList.push_back(SU);
285 }
286 }
287
288 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000289 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000290 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000291 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000292 unsigned SUHeight = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000293
294 // Use dynamic programming:
295 // When current node is being processed, all of its dependencies
296 // are already processed.
297 // So, just iterate over all successors and take the longest path
298 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
299 I != E; ++I) {
300 unsigned SuccHeight = I->Dep->Height;
301 if (SuccHeight+1 > SUHeight) {
302 SUHeight = SuccHeight + 1;
303 }
304 }
305
Dan Gohman3a09d892008-08-27 16:27:25 +0000306 SU->Height = SUHeight;
307
308 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000309 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
310 I != E; ++I) {
311 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000312 if (!--SU->Height)
Roman Levensteind86449e2008-03-04 11:19:43 +0000313 // If all dependencies of the node are processed already,
314 // then the longest path for the node can be computed now
315 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000316 }
317 }
Evan Chenge165a782006-05-11 23:55:42 +0000318}
319
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000320/// CountResults - The results of target nodes have register or immediate
321/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000322/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000323unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000324 unsigned N = Node->getNumValues();
325 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000326 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000327 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000328 --N; // Skip over chain result.
329 return N;
330}
331
Dan Gohman69de1932008-02-06 22:27:42 +0000332/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000333/// followed by special operands that describe memory references, then an
Dan Gohman048ca552008-10-25 17:51:24 +0000334/// optional chain operand, then an optional flag operand. Compute the number
335/// of actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000336unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000337 unsigned N = ComputeMemOperandsEnd(Node);
Gabor Greifba36cb52008-08-28 21:40:38 +0000338 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
Dan Gohman36b5c132008-04-07 19:35:22 +0000339 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000340 return N;
341}
342
Dan Gohman42a77882008-02-16 00:36:48 +0000343/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
344/// operand
345unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000346 unsigned N = Node->getNumOperands();
347 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
348 --N;
349 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
350 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000351 return N;
352}
353
Evan Chenge165a782006-05-11 23:55:42 +0000354
355/// dump - dump the schedule.
356void ScheduleDAG::dumpSchedule() const {
357 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
358 if (SUnit *SU = Sequence[i])
Dan Gohmana23b3b82008-11-13 21:21:28 +0000359 SU->dump(DAG);
Evan Chenge165a782006-05-11 23:55:42 +0000360 else
Bill Wendling832171c2006-12-07 20:04:42 +0000361 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000362 }
363}
364
365
Evan Chenga9c20912006-01-21 02:32:06 +0000366/// Run - perform scheduling.
367///
Dan Gohman5e843682008-07-14 18:19:29 +0000368void ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +0000369 Schedule();
Dan Gohman5e843682008-07-14 18:19:29 +0000370
371 DOUT << "*** Final schedule ***\n";
372 DEBUG(dumpSchedule());
373 DOUT << "\n";
Chris Lattnerd32b2362005-08-18 18:45:24 +0000374}
Evan Cheng4ef10862006-01-23 07:01:07 +0000375
Evan Chenge165a782006-05-11 23:55:42 +0000376/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
377/// a group of nodes flagged together.
378void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000379 cerr << "SU(" << NodeNum << "): ";
Dan Gohmand23e0f82008-11-13 23:24:17 +0000380 if (getNode())
381 getNode()->dump(G);
Evan Cheng42d60272007-09-26 21:36:17 +0000382 else
383 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000384 cerr << "\n";
Dan Gohmand23e0f82008-11-13 23:24:17 +0000385 SmallVector<SDNode *, 4> FlaggedNodes;
386 for (SDNode *N = getNode()->getFlaggedNode(); N; N = N->getFlaggedNode())
387 FlaggedNodes.push_back(N);
388 while (!FlaggedNodes.empty()) {
389 cerr << " ";
390 FlaggedNodes.back()->dump(G);
391 cerr << "\n";
392 FlaggedNodes.pop_back();
Evan Chenge165a782006-05-11 23:55:42 +0000393 }
394}
Evan Cheng4ef10862006-01-23 07:01:07 +0000395
Evan Chenge165a782006-05-11 23:55:42 +0000396void SUnit::dumpAll(const SelectionDAG *G) const {
397 dump(G);
398
Bill Wendling832171c2006-12-07 20:04:42 +0000399 cerr << " # preds left : " << NumPredsLeft << "\n";
400 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +0000401 cerr << " Latency : " << Latency << "\n";
402 cerr << " Depth : " << Depth << "\n";
403 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000404
405 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000406 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000407 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
408 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000409 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000410 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000411 else
Bill Wendling832171c2006-12-07 20:04:42 +0000412 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000413 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
414 if (I->isSpecial)
415 cerr << " *";
416 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000417 }
418 }
419 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000420 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000421 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
422 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000423 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000424 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000425 else
Bill Wendling832171c2006-12-07 20:04:42 +0000426 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000427 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
428 if (I->isSpecial)
429 cerr << " *";
430 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000431 }
432 }
Bill Wendling832171c2006-12-07 20:04:42 +0000433 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000434}