blob: c19515ed05c92d38051aead9f650138c92a8f179 [file] [log] [blame]
Vikram S. Adve8b6d2452001-09-18 12:50:40 +00001// $Id$
2//***************************************************************************
3// File:
4// SchedGraph.cpp
5//
6// Purpose:
7// Scheduling graph based on SSA graph plus extra dependence edges
8// capturing dependences due to machine resources (machine registers,
9// CC registers, and any others).
10//
11// History:
12// 7/20/01 - Vikram Adve - Created
13//**************************************************************************/
Vikram S. Adve78ef1392001-08-28 23:06:02 +000014
Chris Lattner46cbff62001-09-14 16:56:32 +000015#include "SchedGraph.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000016#include "llvm/InstrTypes.h"
17#include "llvm/Instruction.h"
18#include "llvm/BasicBlock.h"
19#include "llvm/Method.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000020#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve85b46d62001-10-17 23:53:16 +000021#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve8b6d2452001-09-18 12:50:40 +000022#include "llvm/Target/MachineInstrInfo.h"
23#include "llvm/Target/MachineRegInfo.h"
Chris Lattnerc83e9542001-09-07 21:21:03 +000024#include "llvm/Support/StringExtras.h"
Chris Lattnerb00c5822001-10-02 03:41:24 +000025#include "llvm/iOther.h"
Chris Lattnerc83e9542001-09-07 21:21:03 +000026#include <algorithm>
Vikram S. Advec352d2c2001-11-05 04:04:23 +000027#include <hash_map>
28#include <vector>
Vikram S. Adve78ef1392001-08-28 23:06:02 +000029
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000030
31//*********************** Internal Data Structures *************************/
32
Vikram S. Advec352d2c2001-11-05 04:04:23 +000033// The following two types need to be classes, not typedefs, so we can use
34// opaque declarations in SchedGraph.h
35//
36struct RefVec: public vector< pair<SchedGraphNode*, int> > {
37 typedef vector< pair<SchedGraphNode*, int> >:: iterator iterator;
38 typedef vector< pair<SchedGraphNode*, int> >::const_iterator const_iterator;
39};
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000040
Chris Lattner80c685f2001-10-13 06:51:01 +000041struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Advec352d2c2001-11-05 04:04:23 +000042 typedef hash_map<int, RefVec>:: iterator iterator;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000043 typedef hash_map<int, RefVec>::const_iterator const_iterator;
44};
45
Vikram S. Advec352d2c2001-11-05 04:04:23 +000046struct ValueToDefVecMap: public hash_map<const Instruction*, RefVec> {
47 typedef hash_map<const Instruction*, RefVec>:: iterator iterator;
48 typedef hash_map<const Instruction*, RefVec>::const_iterator const_iterator;
49};
50
Vikram S. Adve78ef1392001-08-28 23:06:02 +000051//
52// class SchedGraphEdge
53//
54
55/*ctor*/
56SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
57 SchedGraphNode* _sink,
58 SchedGraphEdgeDepType _depType,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000059 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000060 int _minDelay)
61 : src(_src),
62 sink(_sink),
63 depType(_depType),
64 depOrderType(_depOrderType),
Chris Lattner80c685f2001-10-13 06:51:01 +000065 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
66 val(NULL)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000067{
68 src->addOutEdge(this);
69 sink->addInEdge(this);
70}
71
72
73/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000074SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
75 SchedGraphNode* _sink,
76 const Value* _val,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000077 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000078 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000079 : src(_src),
80 sink(_sink),
81 depType(DefUseDep),
82 depOrderType(_depOrderType),
Chris Lattner80c685f2001-10-13 06:51:01 +000083 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
84 val(_val)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000085{
86 src->addOutEdge(this);
87 sink->addInEdge(this);
88}
89
90
91/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000092SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
93 SchedGraphNode* _sink,
94 unsigned int _regNum,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000095 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000096 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000097 : src(_src),
98 sink(_sink),
99 depType(MachineRegister),
100 depOrderType(_depOrderType),
101 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
102 machineRegNum(_regNum)
103{
104 src->addOutEdge(this);
105 sink->addInEdge(this);
106}
107
108
109/*ctor*/
110SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
111 SchedGraphNode* _sink,
112 ResourceId _resourceId,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000113 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000114 : src(_src),
115 sink(_sink),
116 depType(MachineResource),
117 depOrderType(NonDataDep),
118 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
119 resourceId(_resourceId)
120{
121 src->addOutEdge(this);
122 sink->addInEdge(this);
123}
124
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000125/*dtor*/
126SchedGraphEdge::~SchedGraphEdge()
127{
128}
129
Chris Lattnerc83e9542001-09-07 21:21:03 +0000130void SchedGraphEdge::dump(int indent=0) const {
131 printIndent(indent); cout << *this;
132}
133
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000134
135//
136// class SchedGraphNode
137//
138
139/*ctor*/
140SchedGraphNode::SchedGraphNode(unsigned int _nodeId,
141 const Instruction* _instr,
142 const MachineInstr* _minstr,
143 const TargetMachine& target)
144 : nodeId(_nodeId),
145 instr(_instr),
146 minstr(_minstr),
147 latency(0)
148{
149 if (minstr)
150 {
151 MachineOpCode mopCode = minstr->getOpCode();
152 latency = target.getInstrInfo().hasResultInterlock(mopCode)
153 ? target.getInstrInfo().minLatency(mopCode)
154 : target.getInstrInfo().maxLatency(mopCode);
155 }
156}
157
158
159/*dtor*/
160SchedGraphNode::~SchedGraphNode()
161{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162}
163
Chris Lattnerc83e9542001-09-07 21:21:03 +0000164void SchedGraphNode::dump(int indent=0) const {
165 printIndent(indent); cout << *this;
166}
167
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000168
169inline void
170SchedGraphNode::addInEdge(SchedGraphEdge* edge)
171{
172 inEdges.push_back(edge);
173}
174
175
176inline void
177SchedGraphNode::addOutEdge(SchedGraphEdge* edge)
178{
179 outEdges.push_back(edge);
180}
181
182inline void
183SchedGraphNode::removeInEdge(const SchedGraphEdge* edge)
184{
185 assert(edge->getSink() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000186
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000187 for (iterator I = beginInEdges(); I != endInEdges(); ++I)
188 if ((*I) == edge)
189 {
190 inEdges.erase(I);
191 break;
192 }
193}
194
195inline void
196SchedGraphNode::removeOutEdge(const SchedGraphEdge* edge)
197{
198 assert(edge->getSrc() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000199
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000200 for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
201 if ((*I) == edge)
202 {
203 outEdges.erase(I);
204 break;
205 }
206}
207
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000208
209//
210// class SchedGraph
211//
212
213
214/*ctor*/
215SchedGraph::SchedGraph(const BasicBlock* bb,
216 const TargetMachine& target)
217{
218 bbVec.push_back(bb);
219 this->buildGraph(target);
220}
221
222
223/*dtor*/
224SchedGraph::~SchedGraph()
225{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000226 for (iterator I=begin(); I != end(); ++I)
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000227 {
228 SchedGraphNode* node = (*I).second;
229
230 // for each node, delete its out-edges
231 for (SchedGraphNode::iterator I = node->beginOutEdges();
232 I != node->endOutEdges(); ++I)
233 delete *I;
234
235 // then delete the node itself.
236 delete node;
237 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000238}
239
240
241void
242SchedGraph::dump() const
243{
244 cout << " Sched Graph for Basic Blocks: ";
245 for (unsigned i=0, N=bbVec.size(); i < N; i++)
246 {
247 cout << (bbVec[i]->hasName()? bbVec[i]->getName() : "block")
248 << " (" << bbVec[i] << ")"
249 << ((i == N-1)? "" : ", ");
250 }
251
252 cout << endl << endl << " Actual Root nodes : ";
253 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
254 cout << graphRoot->outEdges[i]->getSink()->getNodeId()
255 << ((i == N-1)? "" : ", ");
256
257 cout << endl << " Graph Nodes:" << endl;
258 for (const_iterator I=begin(); I != end(); ++I)
259 cout << endl << * (*I).second;
260
261 cout << endl;
262}
263
264
265void
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000266SchedGraph::eraseIncomingEdges(SchedGraphNode* node, bool addDummyEdges)
267{
268 // Delete and disconnect all in-edges for the node
269 for (SchedGraphNode::iterator I = node->beginInEdges();
270 I != node->endInEdges(); ++I)
271 {
272 SchedGraphNode* srcNode = (*I)->getSrc();
273 srcNode->removeOutEdge(*I);
274 delete *I;
275
276 if (addDummyEdges &&
277 srcNode != getRoot() &&
278 srcNode->beginOutEdges() == srcNode->endOutEdges())
279 { // srcNode has no more out edges, so add an edge to dummy EXIT node
280 assert(node != getLeaf() && "Adding edge that was just removed?");
281 (void) new SchedGraphEdge(srcNode, getLeaf(),
282 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
283 }
284 }
285
286 node->inEdges.clear();
287}
288
289void
290SchedGraph::eraseOutgoingEdges(SchedGraphNode* node, bool addDummyEdges)
291{
292 // Delete and disconnect all out-edges for the node
293 for (SchedGraphNode::iterator I = node->beginOutEdges();
294 I != node->endOutEdges(); ++I)
295 {
296 SchedGraphNode* sinkNode = (*I)->getSink();
297 sinkNode->removeInEdge(*I);
298 delete *I;
299
300 if (addDummyEdges &&
301 sinkNode != getLeaf() &&
302 sinkNode->beginInEdges() == sinkNode->endInEdges())
303 { //sinkNode has no more in edges, so add an edge from dummy ENTRY node
304 assert(node != getRoot() && "Adding edge that was just removed?");
305 (void) new SchedGraphEdge(getRoot(), sinkNode,
306 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
307 }
308 }
309
310 node->outEdges.clear();
311}
312
313void
314SchedGraph::eraseIncidentEdges(SchedGraphNode* node, bool addDummyEdges)
315{
316 this->eraseIncomingEdges(node, addDummyEdges);
317 this->eraseOutgoingEdges(node, addDummyEdges);
318}
319
320
321void
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000322SchedGraph::addDummyEdges()
323{
324 assert(graphRoot->outEdges.size() == 0);
325
326 for (const_iterator I=begin(); I != end(); ++I)
327 {
328 SchedGraphNode* node = (*I).second;
329 assert(node != graphRoot && node != graphLeaf);
330 if (node->beginInEdges() == node->endInEdges())
331 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
332 SchedGraphEdge::NonDataDep, 0);
333 if (node->beginOutEdges() == node->endOutEdges())
334 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
335 SchedGraphEdge::NonDataDep, 0);
336 }
337}
338
339
340void
341SchedGraph::addCDEdges(const TerminatorInst* term,
342 const TargetMachine& target)
343{
344 const MachineInstrInfo& mii = target.getInstrInfo();
345 MachineCodeForVMInstr& termMvec = term->getMachineInstrVec();
346
347 // Find the first branch instr in the sequence of machine instrs for term
348 //
349 unsigned first = 0;
350 while (! mii.isBranch(termMvec[first]->getOpCode()))
351 ++first;
352 assert(first < termMvec.size() &&
353 "No branch instructions for BR? Ok, but weird! Delete assertion.");
354 if (first == termMvec.size())
355 return;
356
357 SchedGraphNode* firstBrNode = this->getGraphNodeForInstr(termMvec[first]);
358
359 // Add CD edges from each instruction in the sequence to the
360 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000361 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000362 //
363 for (int i = (int) termMvec.size()-1; i > (int) first; i--)
364 {
365 SchedGraphNode* toNode = this->getGraphNodeForInstr(termMvec[i]);
366 assert(toNode && "No node for instr generated for branch?");
367
368 for (int j = i-1; j >= 0; j--)
369 if (mii.isBranch(termMvec[j]->getOpCode()))
370 {
371 SchedGraphNode* brNode = this->getGraphNodeForInstr(termMvec[j]);
372 assert(brNode && "No node for instr generated for branch?");
373 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
374 SchedGraphEdge::NonDataDep, 0);
375 break; // only one incoming edge is enough
376 }
377 }
378
379 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000380 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000381 //
382 for (int i = first-1; i >= 0; i--)
383 {
384 SchedGraphNode* fromNode = this->getGraphNodeForInstr(termMvec[i]);
385 assert(fromNode && "No node for instr generated for branch?");
386 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
387 SchedGraphEdge::NonDataDep, 0);
388 }
389
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000390 // Now add CD edges to the first branch instruction in the sequence from
391 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000392 //
393 const BasicBlock* bb = term->getParent();
394 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
395 {
396 if ((*II) == (const Instruction*) term) // special case, handled above
397 continue;
398
399 assert(! (*II)->isTerminator() && "Two terminators in basic block?");
400
401 const MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
402 for (unsigned i=0, N=mvec.size(); i < N; i++)
403 {
404 SchedGraphNode* fromNode = this->getGraphNodeForInstr(mvec[i]);
405 if (fromNode == NULL)
406 continue; // dummy instruction, e.g., PHI
407
408 (void) new SchedGraphEdge(fromNode, firstBrNode,
409 SchedGraphEdge::CtrlDep,
410 SchedGraphEdge::NonDataDep, 0);
411
412 // If we find any other machine instructions (other than due to
413 // the terminator) that also have delay slots, add an outgoing edge
414 // from the instruction to the instructions in the delay slots.
415 //
416 unsigned d = mii.getNumDelaySlots(mvec[i]->getOpCode());
417 assert(i+d < N && "Insufficient delay slots for instruction?");
418
419 for (unsigned j=1; j <= d; j++)
420 {
421 SchedGraphNode* toNode = this->getGraphNodeForInstr(mvec[i+j]);
422 assert(toNode && "No node for machine instr in delay slot?");
423 (void) new SchedGraphEdge(fromNode, toNode,
424 SchedGraphEdge::CtrlDep,
425 SchedGraphEdge::NonDataDep, 0);
426 }
427 }
428 }
429}
430
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000431static const int SG_LOAD_REF = 0;
432static const int SG_STORE_REF = 1;
433static const int SG_CALL_REF = 2;
434
435static const unsigned int SG_DepOrderArray[][3] = {
436 { SchedGraphEdge::NonDataDep,
437 SchedGraphEdge::AntiDep,
438 SchedGraphEdge::AntiDep },
439 { SchedGraphEdge::TrueDep,
440 SchedGraphEdge::OutputDep,
441 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
442 { SchedGraphEdge::TrueDep,
443 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
444 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
445 | SchedGraphEdge::OutputDep }
446};
447
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000448
449void
450SchedGraph::addMemEdges(const vector<const Instruction*>& memVec,
451 const TargetMachine& target)
452{
453 const MachineInstrInfo& mii = target.getInstrInfo();
454
455 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
456 {
457 const Instruction* fromInstr = memVec[im];
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000458 int fromType = (fromInstr->getOpcode() == Instruction::Call
459 ? SG_CALL_REF
460 : (fromInstr->getOpcode() == Instruction::Load
461 ? SG_LOAD_REF
462 : SG_STORE_REF));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000463
464 for (unsigned jm=im+1; jm < NM; jm++)
465 {
466 const Instruction* toInstr = memVec[jm];
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000467 int toType = (fromInstr->getOpcode() == Instruction::Call? 2
468 : ((fromInstr->getOpcode()==Instruction::Load)? 0:1));
469
470 if (fromType == SG_LOAD_REF && toType == SG_LOAD_REF)
471 continue;
472
473 unsigned int depOrderType = SG_DepOrderArray[fromType][toType];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000474
475 MachineCodeForVMInstr& fromInstrMvec=fromInstr->getMachineInstrVec();
476 MachineCodeForVMInstr& toInstrMvec = toInstr->getMachineInstrVec();
477
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000478 // We have two VM memory instructions, and at least one is a store
479 // or a call. Add edges between all machine load/store/call instrs.
480 // Use a latency of 1 to ensure that memory operations are ordered;
481 // latency does not otherwise matter (true dependences enforce that).
482 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000483 for (unsigned i=0, N=fromInstrMvec.size(); i < N; i++)
484 {
485 MachineOpCode fromOpCode = fromInstrMvec[i]->getOpCode();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000486
487 if (mii.isLoad(fromOpCode) || mii.isStore(fromOpCode) ||
488 mii.isCall(fromOpCode))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000489 {
490 SchedGraphNode* fromNode =
491 this->getGraphNodeForInstr(fromInstrMvec[i]);
492 assert(fromNode && "No node for memory instr?");
493
494 for (unsigned j=0, M=toInstrMvec.size(); j < M; j++)
495 {
496 MachineOpCode toOpCode = toInstrMvec[j]->getOpCode();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000497 if (mii.isLoad(toOpCode) || mii.isStore(toOpCode)
498 || mii.isCall(fromOpCode))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000499 {
500 SchedGraphNode* toNode =
501 this->getGraphNodeForInstr(toInstrMvec[j]);
502 assert(toNode && "No node for memory instr?");
503
504 (void) new SchedGraphEdge(fromNode, toNode,
505 SchedGraphEdge::MemoryDep,
506 depOrderType, 1);
507 }
508 }
509 }
510 }
511 }
512 }
513}
514
515
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000516void
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000517SchedGraph::addCallCCEdges(const vector<const Instruction*>& memVec,
518 MachineCodeForBasicBlock& bbMvec,
519 const TargetMachine& target)
520{
521 const MachineInstrInfo& mii = target.getInstrInfo();
522 vector<SchedGraphNode*> callNodeVec;
523
524 // Find the call machine instructions and put them in a vector.
525 // By using memVec, we avoid searching the entire machine code of the BB.
526 //
527 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
528 if (memVec[im]->getOpcode() == Instruction::Call)
529 {
530 MachineCodeForVMInstr& callMvec=memVec[im]->getMachineInstrVec();
531 for (unsigned i=0; i < callMvec.size(); ++i)
532 if (mii.isCall(callMvec[i]->getOpCode()))
533 callNodeVec.push_back(this->getGraphNodeForInstr(callMvec[i]));
534 }
535
536 // Now add additional edges from/to CC reg instrs to/from call instrs.
537 // Essentially this prevents anything that sets or uses a CC reg from being
538 // reordered w.r.t. a call.
539 // Use a latency of 0 because we only need to prevent out-of-order issue,
540 // like with control dependences.
541 //
542 int lastCallNodeIdx = -1;
543 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
544 if (mii.isCall(bbMvec[i]->getOpCode()))
545 {
546 ++lastCallNodeIdx;
547 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
548 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
549 break;
550 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
551 }
552 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
553 { // Add incoming/outgoing edges from/to preceding/later calls
554 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
555 int j=0;
556 for ( ; j <= lastCallNodeIdx; j++)
557 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
558 MachineCCRegsRID, 0);
559 for ( ; j < (int) callNodeVec.size(); j++)
560 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
561 MachineCCRegsRID, 0);
562 }
563}
564
565
566void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000567SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000568 const TargetMachine& target)
569{
570 assert(bbVec.size() == 1 && "Only handling a single basic block here");
571
572 // This assumes that such hardwired registers are never allocated
573 // to any LLVM value (since register allocation happens later), i.e.,
574 // any uses or defs of this register have been made explicit!
575 // Also assumes that two registers with different numbers are
576 // not aliased!
577 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000578 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000579 I != regToRefVecMap.end(); ++I)
580 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000581 int regNum = (*I).first;
582 RefVec& regRefVec = (*I).second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000583
584 // regRefVec is ordered by control flow order in the basic block
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000585 for (unsigned i=0; i < regRefVec.size(); ++i)
586 {
587 SchedGraphNode* node = regRefVec[i].first;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000588 unsigned int opNum = regRefVec[i].second;
589 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
590
591 for (unsigned p=0; p < i; ++p)
592 {
593 SchedGraphNode* prevNode = regRefVec[p].first;
594 if (prevNode != node)
595 {
596 unsigned int prevOpNum = regRefVec[p].second;
597 bool prevIsDef =
598 prevNode->getMachineInstr()->operandIsDefined(prevOpNum);
599
600 if (isDef)
601 new SchedGraphEdge(prevNode, node, regNum,
602 (prevIsDef)? SchedGraphEdge::OutputDep
603 : SchedGraphEdge::AntiDep);
604 else if (prevIsDef)
605 new SchedGraphEdge(prevNode, node, regNum,
606 SchedGraphEdge::TrueDep);
607 }
608 }
609 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000610 }
611}
612
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000613#undef OLD_SSA_EDGE_CONSTRUCTION
614#ifdef OLD_SSA_EDGE_CONSTRUCTION
615//
616// Delete this code once a few more tests pass.
617//
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000618inline void
619CreateSSAEdge(SchedGraph* graph,
620 MachineInstr* defInstr,
621 SchedGraphNode* node,
622 const Value* val)
623{
624 // this instruction does define value `val'.
625 // if there is a node for it in the same graph, add an edge.
626 SchedGraphNode* defNode = graph->getGraphNodeForInstr(defInstr);
627 if (defNode != NULL && defNode != node)
628 (void) new SchedGraphEdge(defNode, node, val);
629}
630
631
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000632void
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000633SchedGraph::addSSAEdge(SchedGraphNode* destNode,
634 const Instruction* defVMInstr,
635 const Value* defValue,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000636 const TargetMachine& target)
637{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000638 // Phi instructions are the only ones that produce a value but don't get
639 // any non-dummy machine instructions. Return here as an optimization.
640 //
Chris Lattnerb00c5822001-10-02 03:41:24 +0000641 if (isa<PHINode>(defVMInstr))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000642 return;
643
644 // Now add the graph edge for the appropriate machine instruction(s).
645 // Note that multiple machine instructions generated for the
646 // def VM instruction may modify the register for the def value.
647 //
648 MachineCodeForVMInstr& defMvec = defVMInstr->getMachineInstrVec();
649 const MachineInstrInfo& mii = target.getInstrInfo();
650
651 for (unsigned i=0, N=defMvec.size(); i < N; i++)
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000652 {
653 bool edgeAddedForInstr = false;
654
655 // First check the explicit operands
656 for (int o=0, N=mii.getNumOperands(defMvec[i]->getOpCode()); o < N; o++)
657 {
658 const MachineOperand& defOp = defMvec[i]->getOperand(o);
659
660 if (defOp.opIsDef()
661 && (defOp.getOperandType() == MachineOperand::MO_VirtualRegister
662 || defOp.getOperandType() == MachineOperand::MO_CCRegister)
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000663 && (defOp.getVRegValue() == defValue))
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000664 {
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000665 CreateSSAEdge(this, defMvec[i], destNode, defValue);
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000666 edgeAddedForInstr = true;
667 break;
668 }
669 }
670
671 // Then check the implicit operands
672 if (! edgeAddedForInstr)
673 {
674 for (unsigned o=0, N=defMvec[i]->getNumImplicitRefs(); o < N; ++o)
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000675 if (defMvec[i]->implicitRefIsDefined(o) &&
676 defMvec[i]->getImplicitRef(o) == defValue)
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000677 {
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000678 CreateSSAEdge(this, defMvec[i], destNode, defValue);
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000679 edgeAddedForInstr = true;
680 break;
681 }
682 }
683 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000684}
685
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000686#endif OLD_SSA_EDGE_CONSTRUCTION
687
688
689void
690SchedGraph::addSSAEdge(SchedGraphNode* destNode,
691 const RefVec& defVec,
692 const Value* defValue,
693 const TargetMachine& target)
694{
695 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I)
696 (void) new SchedGraphEdge((*I).first, destNode, defValue);
697}
698
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000699
700void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000701SchedGraph::addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000702 const ValueToDefVecMap& valueToDefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000703 const TargetMachine& target)
704{
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000705 SchedGraphNode* node = this->getGraphNodeForInstr(&minstr);
706 if (node == NULL)
707 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000708
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000709 assert(node->getInstr() && "Should be no dummy nodes here!");
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000710 const Instruction* instr = node->getInstr();
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000711
712 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000713 //
714 for (unsigned i=0, numOps=minstr.getNumOperands(); i < numOps; i++)
715 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000716 // ignore def operands here
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000717 if (minstr.operandIsDefined(i))
718 continue;
719
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000720 const MachineOperand& mop = minstr.getOperand(i);
721
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000722 switch(mop.getOperandType())
723 {
724 case MachineOperand::MO_VirtualRegister:
725 case MachineOperand::MO_CCRegister:
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000726 if (const Instruction* srcI =
727 dyn_cast_or_null<Instruction>(mop.getVRegValue()))
728 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000729 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
730 if (I != valueToDefVecMap.end())
731 addSSAEdge(node, (*I).second, mop.getVRegValue(), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000732 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000733 break;
734
735 case MachineOperand::MO_MachineRegister:
736 break;
737
738 case MachineOperand::MO_SignExtendedImmed:
739 case MachineOperand::MO_UnextendedImmed:
740 case MachineOperand::MO_PCRelativeDisp:
741 break; // nothing to do for immediate fields
742
743 default:
744 assert(0 && "Unknown machine operand type in SchedGraph builder");
745 break;
746 }
747 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000748
749 // Add edges for values implicitly used by the machine instruction.
750 // Examples include function arguments to a Call instructions or the return
751 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000752 //
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000753 for (unsigned i=0, N=minstr.getNumImplicitRefs(); i < N; ++i)
754 if (! minstr.implicitRefIsDefined(i))
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000755 if (const Instruction* srcI =
756 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
757 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000758 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
759 if (I != valueToDefVecMap.end())
760 addSSAEdge(node, (*I).second, minstr.getImplicitRef(i), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000761 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000762}
763
764
765void
766SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
767 const TargetMachine& target)
768{
Chris Lattnerb00c5822001-10-02 03:41:24 +0000769 if (isa<PHINode>(instr))
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000770 return;
771
772 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
773 const MachineInstrInfo& mii = target.getInstrInfo();
774 RefVec refVec;
775
776 for (unsigned i=0, N=mvec.size(); i < N; i++)
777 for (int o=0, N = mii.getNumOperands(mvec[i]->getOpCode()); o < N; o++)
778 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000779 const MachineOperand& mop = mvec[i]->getOperand(o);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000780
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000781 if ((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
782 mop.getOperandType() == MachineOperand::MO_CCRegister)
783 && mop.getVRegValue() == (Value*) instr)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000784 {
785 // this operand is a definition or use of value `instr'
786 SchedGraphNode* node = this->getGraphNodeForInstr(mvec[i]);
787 assert(node && "No node for machine instruction in this BB?");
788 refVec.push_back(make_pair(node, o));
789 }
790 }
791
792 // refVec is ordered by control flow order of the machine instructions
793 for (unsigned i=0; i < refVec.size(); ++i)
794 {
795 SchedGraphNode* node = refVec[i].first;
796 unsigned int opNum = refVec[i].second;
797 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
798
799 if (isDef)
800 // add output and/or anti deps to this definition
801 for (unsigned p=0; p < i; ++p)
802 {
803 SchedGraphNode* prevNode = refVec[p].first;
804 if (prevNode != node)
805 {
806 bool prevIsDef = prevNode->getMachineInstr()->
807 operandIsDefined(refVec[p].second);
808 new SchedGraphEdge(prevNode, node, SchedGraphEdge::DefUseDep,
809 (prevIsDef)? SchedGraphEdge::OutputDep
810 : SchedGraphEdge::AntiDep);
811 }
812 }
813 }
814}
815
816
817void
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000818SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
819 SchedGraphNode* node,
820 RegToRefVecMap& regToRefVecMap,
821 ValueToDefVecMap& valueToDefVecMap)
822{
823 const MachineInstrInfo& mii = target.getInstrInfo();
824
825 // Collect the register references and value defs. for explicit operands
826 //
827 const MachineInstr& minstr = * node->getMachineInstr();
828 for (int i=0, numOps = (int) minstr.getNumOperands(); i < numOps; i++)
829 {
830 const MachineOperand& mop = minstr.getOperand(i);
831
832 // if this references a register other than the hardwired
833 // "zero" register, record the reference.
834 if (mop.getOperandType() == MachineOperand::MO_MachineRegister)
835 {
836 int regNum = mop.getMachineRegNum();
837 if (regNum != target.getRegInfo().getZeroRegNum())
838 regToRefVecMap[mop.getMachineRegNum()].push_back(make_pair(node, i));
839 continue; // nothing more to do
840 }
841
842 // ignore all other non-def operands
843 if (! minstr.operandIsDefined(i))
844 continue;
845
846 // We must be defining a value.
847 assert((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
848 mop.getOperandType() == MachineOperand::MO_CCRegister)
849 && "Do not expect any other kind of operand to be defined!");
850
851 const Instruction* defInstr = cast<Instruction>(mop.getVRegValue());
852 valueToDefVecMap[defInstr].push_back(make_pair(node, i));
853 }
854
855 //
856 // Collect value defs. for implicit operands. The interface to extract
857 // them assumes they must be virtual registers!
858 //
859 for (int i=0, N = (int) minstr.getNumImplicitRefs(); i < N; ++i)
860 if (minstr.implicitRefIsDefined(i))
861 if (const Instruction* defInstr =
862 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
863 {
864 valueToDefVecMap[defInstr].push_back(make_pair(node, -i));
865 }
866}
867
868
869void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000870SchedGraph::buildNodesforVMInstr(const TargetMachine& target,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000871 const Instruction* instr,
872 vector<const Instruction*>& memVec,
873 RegToRefVecMap& regToRefVecMap,
874 ValueToDefVecMap& valueToDefVecMap)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000875{
876 const MachineInstrInfo& mii = target.getInstrInfo();
877 const MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
878 for (unsigned i=0; i < mvec.size(); i++)
879 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
880 {
881 SchedGraphNode* node = new SchedGraphNode(getNumNodes(),
882 instr, mvec[i], target);
883 this->noteGraphNodeForInstr(mvec[i], node);
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000884
885 // Remember all register references and value defs
886 findDefUseInfoAtInstr(target, node, regToRefVecMap, valueToDefVecMap);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000887 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000888
889 // Remember load/store/call instructions to add memory deps later.
890 if (instr->getOpcode() == Instruction::Load ||
891 instr->getOpcode() == Instruction::Store ||
892 instr->getOpcode() == Instruction::Call)
893 memVec.push_back(instr);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000894}
895
896
897void
898SchedGraph::buildGraph(const TargetMachine& target)
899{
900 const MachineInstrInfo& mii = target.getInstrInfo();
901 const BasicBlock* bb = bbVec[0];
902
903 assert(bbVec.size() == 1 && "Only handling a single basic block here");
904
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000905 // Use this data structure to note all machine operands that compute
906 // ordinary LLVM values. These must be computed defs (i.e., instructions).
907 // Note that there may be multiple machine instructions that define
908 // each Value.
909 ValueToDefVecMap valueToDefVecMap;
910
911 // Use this data structure to note all LLVM memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000912 // We use this to add memory dependence edges without a second full walk.
913 //
914 vector<const Instruction*> memVec;
915
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000916 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000917 // machine registers so we can add edges for those later without
918 // extra passes over the nodes.
919 // The vector holds an ordered list of references to the machine reg,
920 // ordered according to control-flow order. This only works for a
921 // single basic block, hence the assertion. Each reference is identified
922 // by the pair: <node, operand-number>.
923 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000924 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000925
926 // Make a dummy root node. We'll add edges to the real roots later.
927 graphRoot = new SchedGraphNode(0, NULL, NULL, target);
928 graphLeaf = new SchedGraphNode(1, NULL, NULL, target);
929
930 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000931 // First add nodes for all the machine instructions in the basic block
932 // because this greatly simplifies identifying which edges to add.
933 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000934 // Also, remember the load/store instructions to add memory deps later.
935 //----------------------------------------------------------------
936
937 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
938 {
939 const Instruction *instr = *II;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000940
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000941 // Build graph nodes for this VM instruction and gather def/use info.
942 // Do these together in a single pass over all machine instructions.
943 buildNodesforVMInstr(target, instr,
944 memVec, regToRefVecMap, valueToDefVecMap);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000945 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000946
947 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000948 // Now add edges for the following (all are incoming edges except (4)):
949 // (1) operands of the machine instruction, including hidden operands
950 // (2) machine register dependences
951 // (3) memory load/store dependences
952 // (3) other resource dependences for the machine instruction, if any
953 // (4) output dependences when multiple machine instructions define the
954 // same value; all must have been generated from a single VM instrn
955 // (5) control dependences to branch instructions generated for the
956 // terminator instruction of the BB. Because of delay slots and
957 // 2-way conditional branches, multiple CD edges are needed
958 // (see addCDEdges for details).
959 // Also, note any uses or defs of machine registers.
960 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000961 //----------------------------------------------------------------
962
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000963 MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
964
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000965 // First, add edges to the terminator instruction of the basic block.
966 this->addCDEdges(bb->getTerminator(), target);
967
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000968 // Then add memory dep edges: store->load, load->store, and store->store.
969 // Call instructions are treated as both load and store.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000970 this->addMemEdges(memVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000971
972 // Then add edges between call instructions and CC set/use instructions
973 this->addCallCCEdges(memVec, bbMvec, target);
974
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000975 // Then add incoming def-use (SSA) edges for each machine instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000976 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000977 addEdgesForInstruction(*bbMvec[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000978
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000979 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000980 // We assume that all machine instructions that define a value are
981 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000982 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000983 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000984 this->addNonSSAEdgesForValue(*II, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000985
986 // Then add edges for dependences on machine registers
987 this->addMachineRegEdges(regToRefVecMap, target);
988
989 // Finally, add edges from the dummy root and to dummy leaf
990 this->addDummyEdges();
991}
992
993
994//
995// class SchedGraphSet
996//
997
998/*ctor*/
999SchedGraphSet::SchedGraphSet(const Method* _method,
1000 const TargetMachine& target) :
1001 method(_method)
1002{
1003 buildGraphsForMethod(method, target);
1004}
1005
1006
1007/*dtor*/
1008SchedGraphSet::~SchedGraphSet()
1009{
1010 // delete all the graphs
1011 for (iterator I=begin(); I != end(); ++I)
1012 delete (*I).second;
1013}
1014
1015
1016void
1017SchedGraphSet::dump() const
1018{
1019 cout << "======== Sched graphs for method `"
1020 << (method->hasName()? method->getName() : "???")
1021 << "' ========" << endl << endl;
1022
1023 for (const_iterator I=begin(); I != end(); ++I)
1024 (*I).second->dump();
1025
1026 cout << endl << "====== End graphs for method `"
1027 << (method->hasName()? method->getName() : "")
1028 << "' ========" << endl << endl;
1029}
1030
1031
1032void
1033SchedGraphSet::buildGraphsForMethod(const Method *method,
1034 const TargetMachine& target)
1035{
1036 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
1037 {
1038 SchedGraph* graph = new SchedGraph(*BI, target);
1039 this->noteGraphForBlock(*BI, graph);
1040 }
1041}
1042
1043
1044
1045ostream&
1046operator<<(ostream& os, const SchedGraphEdge& edge)
1047{
1048 os << "edge [" << edge.src->getNodeId() << "] -> ["
1049 << edge.sink->getNodeId() << "] : ";
1050
1051 switch(edge.depType) {
1052 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
1053 case SchedGraphEdge::DefUseDep: os<< "Reg Value " << edge.val; break;
1054 case SchedGraphEdge::MemoryDep: os<< "Mem Value " << edge.val; break;
1055 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
1056 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
1057 default: assert(0); break;
1058 }
1059
1060 os << " : delay = " << edge.minDelay << endl;
1061
1062 return os;
1063}
1064
1065ostream&
1066operator<<(ostream& os, const SchedGraphNode& node)
1067{
1068 printIndent(4, os);
1069 os << "Node " << node.nodeId << " : "
1070 << "latency = " << node.latency << endl;
1071
1072 printIndent(6, os);
1073
1074 if (node.getMachineInstr() == NULL)
1075 os << "(Dummy node)" << endl;
1076 else
1077 {
1078 os << *node.getMachineInstr() << endl;
1079
1080 printIndent(6, os);
1081 os << node.inEdges.size() << " Incoming Edges:" << endl;
1082 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
1083 {
1084 printIndent(8, os);
1085 os << * node.inEdges[i];
1086 }
1087
1088 printIndent(6, os);
1089 os << node.outEdges.size() << " Outgoing Edges:" << endl;
1090 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
1091 {
1092 printIndent(8, os);
1093 os << * node.outEdges[i];
1094 }
1095 }
1096
1097 return os;
1098}