blob: 8ef493b06e912c5da03b5a6b46e49aa1fc70d4bb [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) {
61 SUnit *SU = NewSUnit(Old->Node);
Dan Gohman4c8c8302008-06-21 15:52:51 +000062 SU->OrigNode = Old->OrigNode;
Dan Gohman45f36ea2008-03-10 23:48:14 +000063 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Chenga6fb1b62007-09-25 01:54:36 +000064 SU->Latency = Old->Latency;
65 SU->isTwoAddress = Old->isTwoAddress;
66 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000067 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000068 return SU;
69}
70
Evan Chengf10c9732007-10-05 01:39:18 +000071
Evan Chenge165a782006-05-11 23:55:42 +000072/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
73/// This SUnit graph is similar to the SelectionDAG, but represents flagged
74/// together nodes with a single SUnit.
75void ScheduleDAG::BuildSchedUnits() {
76 // Reserve entries in the vector for each of the SUnits we are creating. This
77 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
78 // invalidated.
Dan Gohmana23b3b82008-11-13 21:21:28 +000079 SUnits.reserve(DAG->allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +000080
Dan Gohman94d7a5f2008-06-21 19:18:17 +000081 // During scheduling, the NodeId field of SDNode is used to map SDNodes
82 // to their associated SUnits by holding SUnits table indices. A value
83 // of -1 means the SDNode does not yet have an associated SUnit.
Dan Gohmana23b3b82008-11-13 21:21:28 +000084 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
85 E = DAG->allnodes_end(); NI != E; ++NI)
Dan Gohman94d7a5f2008-06-21 19:18:17 +000086 NI->setNodeId(-1);
87
Dan Gohmana23b3b82008-11-13 21:21:28 +000088 for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(),
89 E = DAG->allnodes_end(); NI != E; ++NI) {
Evan Chenge165a782006-05-11 23:55:42 +000090 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
91 continue;
92
93 // If this node has already been processed, stop now.
Dan Gohman94d7a5f2008-06-21 19:18:17 +000094 if (NI->getNodeId() != -1) continue;
Evan Chenge165a782006-05-11 23:55:42 +000095
96 SUnit *NodeSUnit = NewSUnit(NI);
97
98 // See if anything is flagged to this node, if so, add them to flagged
99 // nodes. Nodes can have at most one flag input and one flag output. Flags
100 // are required the be the last operand and result of a node.
101
102 // Scan up, adding flagged preds to FlaggedNodes.
103 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000104 if (N->getNumOperands() &&
105 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
106 do {
Gabor Greifba36cb52008-08-28 21:40:38 +0000107 N = N->getOperand(N->getNumOperands()-1).getNode();
Evan Cheng3b97acd2006-08-07 22:12:12 +0000108 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000109 assert(N->getNodeId() == -1 && "Node already inserted!");
110 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000111 } while (N->getNumOperands() &&
112 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
113 std::reverse(NodeSUnit->FlaggedNodes.begin(),
114 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000115 }
116
117 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
118 // have a user of the flag operand.
119 N = NI;
120 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue FlagVal(N, N->getNumValues()-1);
Evan Chenge165a782006-05-11 23:55:42 +0000122
123 // There are either zero or one users of the Flag result.
124 bool HasFlagUse = false;
125 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
126 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +0000127 if (FlagVal.isOperandOf(*UI)) {
Evan Chenge165a782006-05-11 23:55:42 +0000128 HasFlagUse = true;
129 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000130 assert(N->getNodeId() == -1 && "Node already inserted!");
131 N->setNodeId(NodeSUnit->NodeNum);
Dan Gohman89684502008-07-27 20:43:25 +0000132 N = *UI;
Evan Chenge165a782006-05-11 23:55:42 +0000133 break;
134 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000135 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000136 }
137
138 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
139 // Update the SUnit
140 NodeSUnit->Node = N;
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000141 assert(N->getNodeId() == -1 && "Node already inserted!");
142 N->setNodeId(NodeSUnit->NodeNum);
Evan Chengf10c9732007-10-05 01:39:18 +0000143
144 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000145 }
146
147 // Pass 2: add the preds, succs, etc.
148 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
149 SUnit *SU = &SUnits[su];
150 SDNode *MainNode = SU->Node;
151
Dan Gohmane8be6c62008-07-17 19:10:17 +0000152 if (MainNode->isMachineOpcode()) {
153 unsigned Opc = MainNode->getMachineOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000154 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000155 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000156 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000157 SU->isTwoAddress = true;
158 break;
159 }
160 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000161 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000162 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000163 }
164
165 // Find all predecessors and successors of the group.
166 // Temporarily add N to make code simpler.
167 SU->FlaggedNodes.push_back(MainNode);
168
169 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
170 SDNode *N = SU->FlaggedNodes[n];
Dan Gohmane8be6c62008-07-17 19:10:17 +0000171 if (N->isMachineOpcode() &&
172 TII->get(N->getMachineOpcode()).getImplicitDefs() &&
173 CountResults(N) > TII->get(N->getMachineOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000174 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000175
176 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000177 SDNode *OpN = N->getOperand(i).getNode();
Evan Chenge165a782006-05-11 23:55:42 +0000178 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000179 SUnit *OpSU = &SUnits[OpN->getNodeId()];
Evan Chenge165a782006-05-11 23:55:42 +0000180 assert(OpSU && "Node has no SUnit!");
181 if (OpSU == SU) continue; // In the same group.
182
Duncan Sands83ec4b62008-06-06 12:08:01 +0000183 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000184 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
185 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000186
187 unsigned PhysReg = 0;
188 int Cost = 1;
189 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000190 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000191 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000192 }
193 }
194
195 // Remove MainNode from FlaggedNodes again.
196 SU->FlaggedNodes.pop_back();
197 }
Evan Chenge165a782006-05-11 23:55:42 +0000198}
199
Evan Chengf10c9732007-10-05 01:39:18 +0000200void ScheduleDAG::ComputeLatency(SUnit *SU) {
201 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
202
203 // Compute the latency for the node. We use the sum of the latencies for
204 // all nodes flagged together into this SUnit.
205 if (InstrItins.isEmpty()) {
206 // No latency information.
207 SU->Latency = 1;
Evan Chengc6be7772008-07-02 09:23:51 +0000208 return;
209 }
210
211 SU->Latency = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000212 if (SU->Node->isMachineOpcode()) {
213 unsigned SchedClass = TII->get(SU->Node->getMachineOpcode()).getSchedClass();
Evan Chengc6be7772008-07-02 09:23:51 +0000214 const InstrStage *S = InstrItins.begin(SchedClass);
215 const InstrStage *E = InstrItins.end(SchedClass);
216 for (; S != E; ++S)
217 SU->Latency += S->Cycles;
218 }
219 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
220 SDNode *FNode = SU->FlaggedNodes[i];
Dan Gohmane8be6c62008-07-17 19:10:17 +0000221 if (FNode->isMachineOpcode()) {
222 unsigned SchedClass = TII->get(FNode->getMachineOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000223 const InstrStage *S = InstrItins.begin(SchedClass);
224 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000225 for (; S != E; ++S)
226 SU->Latency += S->Cycles;
227 }
Evan Chengf10c9732007-10-05 01:39:18 +0000228 }
229}
230
Roman Levensteind86449e2008-03-04 11:19:43 +0000231/// CalculateDepths - compute depths using algorithms for the longest
232/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000233void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000234 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000235 std::vector<SUnit*> WorkList;
236 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000237
Roman Levensteind86449e2008-03-04 11:19:43 +0000238 // Initialize the data structures
239 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
240 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000241 unsigned Degree = SU->Preds.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000242 // Temporarily use the Depth field as scratch space for the degree count.
243 SU->Depth = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000244
245 // Is it a node without dependencies?
246 if (Degree == 0) {
247 assert(SU->Preds.empty() && "SUnit should have no predecessors");
248 // Collect leaf nodes
249 WorkList.push_back(SU);
250 }
251 }
252
253 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000254 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000255 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000256 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000257 unsigned SUDepth = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000258
259 // Use dynamic programming:
260 // When current node is being processed, all of its dependencies
261 // are already processed.
262 // So, just iterate over all predecessors and take the longest path
263 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
264 I != E; ++I) {
265 unsigned PredDepth = I->Dep->Depth;
266 if (PredDepth+1 > SUDepth) {
267 SUDepth = PredDepth + 1;
268 }
269 }
270
Dan Gohman3a09d892008-08-27 16:27:25 +0000271 SU->Depth = SUDepth;
272
273 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000274 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
275 I != E; ++I) {
276 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000277 if (!--SU->Depth)
Roman Levensteind86449e2008-03-04 11:19:43 +0000278 // If all dependencies of the node are processed already,
279 // then the longest path for the node can be computed now
280 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000281 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000282 }
Evan Chenge165a782006-05-11 23:55:42 +0000283}
Evan Cheng99126282007-07-06 01:37:28 +0000284
Roman Levensteind86449e2008-03-04 11:19:43 +0000285/// CalculateHeights - compute heights using algorithms for the longest
286/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000287void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000288 unsigned DAGSize = SUnits.size();
Roman Levensteind86449e2008-03-04 11:19:43 +0000289 std::vector<SUnit*> WorkList;
290 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000291
Roman Levensteind86449e2008-03-04 11:19:43 +0000292 // Initialize the data structures
293 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
294 SUnit *SU = &SUnits[i];
Roman Levensteind86449e2008-03-04 11:19:43 +0000295 unsigned Degree = SU->Succs.size();
Dan Gohman3a09d892008-08-27 16:27:25 +0000296 // Temporarily use the Height field as scratch space for the degree count.
297 SU->Height = Degree;
Roman Levensteind86449e2008-03-04 11:19:43 +0000298
299 // Is it a node without dependencies?
300 if (Degree == 0) {
301 assert(SU->Succs.empty() && "Something wrong");
302 assert(WorkList.empty() && "Should be empty");
303 // Collect leaf nodes
304 WorkList.push_back(SU);
305 }
306 }
307
308 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000309 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000310 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000311 WorkList.pop_back();
Dan Gohman3a09d892008-08-27 16:27:25 +0000312 unsigned SUHeight = 0;
Roman Levensteind86449e2008-03-04 11:19:43 +0000313
314 // Use dynamic programming:
315 // When current node is being processed, all of its dependencies
316 // are already processed.
317 // So, just iterate over all successors and take the longest path
318 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
319 I != E; ++I) {
320 unsigned SuccHeight = I->Dep->Height;
321 if (SuccHeight+1 > SUHeight) {
322 SUHeight = SuccHeight + 1;
323 }
324 }
325
Dan Gohman3a09d892008-08-27 16:27:25 +0000326 SU->Height = SUHeight;
327
328 // Update degrees of all nodes depending on current SUnit
Roman Levensteind86449e2008-03-04 11:19:43 +0000329 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
330 I != E; ++I) {
331 SUnit *SU = I->Dep;
Dan Gohman3a09d892008-08-27 16:27:25 +0000332 if (!--SU->Height)
Roman Levensteind86449e2008-03-04 11:19:43 +0000333 // If all dependencies of the node are processed already,
334 // then the longest path for the node can be computed now
335 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000336 }
337 }
Evan Chenge165a782006-05-11 23:55:42 +0000338}
339
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000340/// CountResults - The results of target nodes have register or immediate
341/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000342/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000343unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000344 unsigned N = Node->getNumValues();
345 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000346 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000347 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000348 --N; // Skip over chain result.
349 return N;
350}
351
Dan Gohman69de1932008-02-06 22:27:42 +0000352/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000353/// followed by special operands that describe memory references, then an
Dan Gohman048ca552008-10-25 17:51:24 +0000354/// optional chain operand, then an optional flag operand. Compute the number
355/// of actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000356unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000357 unsigned N = ComputeMemOperandsEnd(Node);
Gabor Greifba36cb52008-08-28 21:40:38 +0000358 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).getNode()))
Dan Gohman36b5c132008-04-07 19:35:22 +0000359 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000360 return N;
361}
362
Dan Gohman42a77882008-02-16 00:36:48 +0000363/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
364/// operand
365unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000366 unsigned N = Node->getNumOperands();
367 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
368 --N;
369 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
370 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000371 return N;
372}
373
Evan Chenge165a782006-05-11 23:55:42 +0000374
375/// dump - dump the schedule.
376void ScheduleDAG::dumpSchedule() const {
377 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
378 if (SUnit *SU = Sequence[i])
Dan Gohmana23b3b82008-11-13 21:21:28 +0000379 SU->dump(DAG);
Evan Chenge165a782006-05-11 23:55:42 +0000380 else
Bill Wendling832171c2006-12-07 20:04:42 +0000381 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000382 }
383}
384
385
Evan Chenga9c20912006-01-21 02:32:06 +0000386/// Run - perform scheduling.
387///
Dan Gohman5e843682008-07-14 18:19:29 +0000388void ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +0000389 Schedule();
Dan Gohman5e843682008-07-14 18:19:29 +0000390
391 DOUT << "*** Final schedule ***\n";
392 DEBUG(dumpSchedule());
393 DOUT << "\n";
Chris Lattnerd32b2362005-08-18 18:45:24 +0000394}
Evan Cheng4ef10862006-01-23 07:01:07 +0000395
Evan Chenge165a782006-05-11 23:55:42 +0000396/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
397/// a group of nodes flagged together.
398void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000399 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +0000400 if (Node)
401 Node->dump(G);
402 else
403 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000404 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000405 if (FlaggedNodes.size() != 0) {
406 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000407 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000408 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000409 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000410 }
411 }
412}
Evan Cheng4ef10862006-01-23 07:01:07 +0000413
Evan Chenge165a782006-05-11 23:55:42 +0000414void SUnit::dumpAll(const SelectionDAG *G) const {
415 dump(G);
416
Bill Wendling832171c2006-12-07 20:04:42 +0000417 cerr << " # preds left : " << NumPredsLeft << "\n";
418 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +0000419 cerr << " Latency : " << Latency << "\n";
420 cerr << " Depth : " << Depth << "\n";
421 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000422
423 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000424 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000425 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
426 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000427 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000428 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000429 else
Bill Wendling832171c2006-12-07 20:04:42 +0000430 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000431 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
432 if (I->isSpecial)
433 cerr << " *";
434 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000435 }
436 }
437 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000438 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000439 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
440 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000441 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000442 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000443 else
Bill Wendling832171c2006-12-07 20:04:42 +0000444 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000445 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
446 if (I->isSpecial)
447 cerr << " *";
448 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000449 }
450 }
Bill Wendling832171c2006-12-07 20:04:42 +0000451 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000452}