blob: 03eb104edb6186cb13e8d7aac88774c6c215bd43 [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{
Vikram S. Adve200a4352001-11-12 18:53:43 +000068 assert(src != sink && "Self-loop in scheduling graph!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +000069 src->addOutEdge(this);
70 sink->addInEdge(this);
71}
72
73
74/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000075SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
76 SchedGraphNode* _sink,
77 const Value* _val,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000078 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000079 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000080 : src(_src),
81 sink(_sink),
Vikram S. Adve200a4352001-11-12 18:53:43 +000082 depType(ValueDep),
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083 depOrderType(_depOrderType),
Chris Lattner80c685f2001-10-13 06:51:01 +000084 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
85 val(_val)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086{
Vikram S. Adve200a4352001-11-12 18:53:43 +000087 assert(src != sink && "Self-loop in scheduling graph!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +000088 src->addOutEdge(this);
89 sink->addInEdge(this);
90}
91
92
93/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000094SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
95 SchedGraphNode* _sink,
96 unsigned int _regNum,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000097 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000098 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000099 : src(_src),
100 sink(_sink),
101 depType(MachineRegister),
102 depOrderType(_depOrderType),
103 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
104 machineRegNum(_regNum)
105{
Vikram S. Adve200a4352001-11-12 18:53:43 +0000106 assert(src != sink && "Self-loop in scheduling graph!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000107 src->addOutEdge(this);
108 sink->addInEdge(this);
109}
110
111
112/*ctor*/
113SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
114 SchedGraphNode* _sink,
115 ResourceId _resourceId,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000116 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000117 : src(_src),
118 sink(_sink),
119 depType(MachineResource),
120 depOrderType(NonDataDep),
121 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
122 resourceId(_resourceId)
123{
Vikram S. Adve200a4352001-11-12 18:53:43 +0000124 assert(src != sink && "Self-loop in scheduling graph!");
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125 src->addOutEdge(this);
126 sink->addInEdge(this);
127}
128
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000129/*dtor*/
130SchedGraphEdge::~SchedGraphEdge()
131{
132}
133
Chris Lattnerc83e9542001-09-07 21:21:03 +0000134void SchedGraphEdge::dump(int indent=0) const {
135 printIndent(indent); cout << *this;
136}
137
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000138
139//
140// class SchedGraphNode
141//
142
143/*ctor*/
144SchedGraphNode::SchedGraphNode(unsigned int _nodeId,
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000145 const BasicBlock* _bb,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000146 const MachineInstr* _minstr,
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000147 int indexInBB,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000148 const TargetMachine& target)
149 : nodeId(_nodeId),
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000150 bb(_bb),
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151 minstr(_minstr),
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000152 origIndexInBB(indexInBB),
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000153 latency(0)
154{
155 if (minstr)
156 {
157 MachineOpCode mopCode = minstr->getOpCode();
158 latency = target.getInstrInfo().hasResultInterlock(mopCode)
159 ? target.getInstrInfo().minLatency(mopCode)
160 : target.getInstrInfo().maxLatency(mopCode);
161 }
162}
163
164
165/*dtor*/
166SchedGraphNode::~SchedGraphNode()
167{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000168}
169
Chris Lattnerc83e9542001-09-07 21:21:03 +0000170void SchedGraphNode::dump(int indent=0) const {
171 printIndent(indent); cout << *this;
172}
173
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000174
175inline void
176SchedGraphNode::addInEdge(SchedGraphEdge* edge)
177{
178 inEdges.push_back(edge);
179}
180
181
182inline void
183SchedGraphNode::addOutEdge(SchedGraphEdge* edge)
184{
185 outEdges.push_back(edge);
186}
187
188inline void
189SchedGraphNode::removeInEdge(const SchedGraphEdge* edge)
190{
191 assert(edge->getSink() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000192
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000193 for (iterator I = beginInEdges(); I != endInEdges(); ++I)
194 if ((*I) == edge)
195 {
196 inEdges.erase(I);
197 break;
198 }
199}
200
201inline void
202SchedGraphNode::removeOutEdge(const SchedGraphEdge* edge)
203{
204 assert(edge->getSrc() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000205
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000206 for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
207 if ((*I) == edge)
208 {
209 outEdges.erase(I);
210 break;
211 }
212}
213
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000214
215//
216// class SchedGraph
217//
218
219
220/*ctor*/
221SchedGraph::SchedGraph(const BasicBlock* bb,
222 const TargetMachine& target)
223{
224 bbVec.push_back(bb);
225 this->buildGraph(target);
226}
227
228
229/*dtor*/
230SchedGraph::~SchedGraph()
231{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000232 for (iterator I=begin(); I != end(); ++I)
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000233 {
234 SchedGraphNode* node = (*I).second;
235
236 // for each node, delete its out-edges
237 for (SchedGraphNode::iterator I = node->beginOutEdges();
238 I != node->endOutEdges(); ++I)
239 delete *I;
240
241 // then delete the node itself.
242 delete node;
243 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000244}
245
246
247void
248SchedGraph::dump() const
249{
250 cout << " Sched Graph for Basic Blocks: ";
251 for (unsigned i=0, N=bbVec.size(); i < N; i++)
252 {
253 cout << (bbVec[i]->hasName()? bbVec[i]->getName() : "block")
254 << " (" << bbVec[i] << ")"
255 << ((i == N-1)? "" : ", ");
256 }
257
258 cout << endl << endl << " Actual Root nodes : ";
259 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
260 cout << graphRoot->outEdges[i]->getSink()->getNodeId()
261 << ((i == N-1)? "" : ", ");
262
263 cout << endl << " Graph Nodes:" << endl;
264 for (const_iterator I=begin(); I != end(); ++I)
265 cout << endl << * (*I).second;
266
267 cout << endl;
268}
269
270
271void
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000272SchedGraph::eraseIncomingEdges(SchedGraphNode* node, bool addDummyEdges)
273{
274 // Delete and disconnect all in-edges for the node
275 for (SchedGraphNode::iterator I = node->beginInEdges();
276 I != node->endInEdges(); ++I)
277 {
278 SchedGraphNode* srcNode = (*I)->getSrc();
279 srcNode->removeOutEdge(*I);
280 delete *I;
281
282 if (addDummyEdges &&
283 srcNode != getRoot() &&
284 srcNode->beginOutEdges() == srcNode->endOutEdges())
285 { // srcNode has no more out edges, so add an edge to dummy EXIT node
286 assert(node != getLeaf() && "Adding edge that was just removed?");
287 (void) new SchedGraphEdge(srcNode, getLeaf(),
288 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
289 }
290 }
291
292 node->inEdges.clear();
293}
294
295void
296SchedGraph::eraseOutgoingEdges(SchedGraphNode* node, bool addDummyEdges)
297{
298 // Delete and disconnect all out-edges for the node
299 for (SchedGraphNode::iterator I = node->beginOutEdges();
300 I != node->endOutEdges(); ++I)
301 {
302 SchedGraphNode* sinkNode = (*I)->getSink();
303 sinkNode->removeInEdge(*I);
304 delete *I;
305
306 if (addDummyEdges &&
307 sinkNode != getLeaf() &&
308 sinkNode->beginInEdges() == sinkNode->endInEdges())
309 { //sinkNode has no more in edges, so add an edge from dummy ENTRY node
310 assert(node != getRoot() && "Adding edge that was just removed?");
311 (void) new SchedGraphEdge(getRoot(), sinkNode,
312 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
313 }
314 }
315
316 node->outEdges.clear();
317}
318
319void
320SchedGraph::eraseIncidentEdges(SchedGraphNode* node, bool addDummyEdges)
321{
322 this->eraseIncomingEdges(node, addDummyEdges);
323 this->eraseOutgoingEdges(node, addDummyEdges);
324}
325
326
327void
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000328SchedGraph::addDummyEdges()
329{
330 assert(graphRoot->outEdges.size() == 0);
331
332 for (const_iterator I=begin(); I != end(); ++I)
333 {
334 SchedGraphNode* node = (*I).second;
335 assert(node != graphRoot && node != graphLeaf);
336 if (node->beginInEdges() == node->endInEdges())
337 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
338 SchedGraphEdge::NonDataDep, 0);
339 if (node->beginOutEdges() == node->endOutEdges())
340 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
341 SchedGraphEdge::NonDataDep, 0);
342 }
343}
344
345
346void
347SchedGraph::addCDEdges(const TerminatorInst* term,
348 const TargetMachine& target)
349{
350 const MachineInstrInfo& mii = target.getInstrInfo();
351 MachineCodeForVMInstr& termMvec = term->getMachineInstrVec();
352
353 // Find the first branch instr in the sequence of machine instrs for term
354 //
355 unsigned first = 0;
356 while (! mii.isBranch(termMvec[first]->getOpCode()))
357 ++first;
358 assert(first < termMvec.size() &&
359 "No branch instructions for BR? Ok, but weird! Delete assertion.");
360 if (first == termMvec.size())
361 return;
362
363 SchedGraphNode* firstBrNode = this->getGraphNodeForInstr(termMvec[first]);
364
365 // Add CD edges from each instruction in the sequence to the
366 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000367 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000368 //
369 for (int i = (int) termMvec.size()-1; i > (int) first; i--)
370 {
371 SchedGraphNode* toNode = this->getGraphNodeForInstr(termMvec[i]);
372 assert(toNode && "No node for instr generated for branch?");
373
374 for (int j = i-1; j >= 0; j--)
375 if (mii.isBranch(termMvec[j]->getOpCode()))
376 {
377 SchedGraphNode* brNode = this->getGraphNodeForInstr(termMvec[j]);
378 assert(brNode && "No node for instr generated for branch?");
379 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
380 SchedGraphEdge::NonDataDep, 0);
381 break; // only one incoming edge is enough
382 }
383 }
384
385 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000386 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000387 //
388 for (int i = first-1; i >= 0; i--)
389 {
390 SchedGraphNode* fromNode = this->getGraphNodeForInstr(termMvec[i]);
391 assert(fromNode && "No node for instr generated for branch?");
392 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
393 SchedGraphEdge::NonDataDep, 0);
394 }
395
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000396 // Now add CD edges to the first branch instruction in the sequence from
397 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000398 //
Vikram S. Adve200a4352001-11-12 18:53:43 +0000399 const BasicBlock* bb = firstBrNode->getBB();
400 const MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
401 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000402 {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000403 if (mvec[i] == termMvec[first]) // reached the first branch
404 break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000405
Vikram S. Adve200a4352001-11-12 18:53:43 +0000406 SchedGraphNode* fromNode = this->getGraphNodeForInstr(mvec[i]);
407 if (fromNode == NULL)
408 continue; // dummy instruction, e.g., PHI
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000409
Vikram S. Adve200a4352001-11-12 18:53:43 +0000410 (void) new SchedGraphEdge(fromNode, firstBrNode,
411 SchedGraphEdge::CtrlDep,
412 SchedGraphEdge::NonDataDep, 0);
413
414 // If we find any other machine instructions (other than due to
415 // the terminator) that also have delay slots, add an outgoing edge
416 // from the instruction to the instructions in the delay slots.
417 //
418 unsigned d = mii.getNumDelaySlots(mvec[i]->getOpCode());
419 assert(i+d < N && "Insufficient delay slots for instruction?");
420
421 for (unsigned j=1; j <= d; j++)
422 {
423 SchedGraphNode* toNode = this->getGraphNodeForInstr(mvec[i+j]);
424 assert(toNode && "No node for machine instr in delay slot?");
425 (void) new SchedGraphEdge(fromNode, toNode,
426 SchedGraphEdge::CtrlDep,
427 SchedGraphEdge::NonDataDep, 0);
428 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000429 }
430}
431
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000432static const int SG_LOAD_REF = 0;
433static const int SG_STORE_REF = 1;
434static const int SG_CALL_REF = 2;
435
436static const unsigned int SG_DepOrderArray[][3] = {
437 { SchedGraphEdge::NonDataDep,
438 SchedGraphEdge::AntiDep,
439 SchedGraphEdge::AntiDep },
440 { SchedGraphEdge::TrueDep,
441 SchedGraphEdge::OutputDep,
442 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
443 { SchedGraphEdge::TrueDep,
444 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
445 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
446 | SchedGraphEdge::OutputDep }
447};
448
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000449
Vikram S. Advee64574c2001-11-08 05:20:23 +0000450// Add a dependence edge between every pair of machine load/store/call
451// instructions, where at least one is a store or a call.
452// Use latency 1 just to ensure that memory operations are ordered;
453// latency does not otherwise matter (true dependences enforce that).
454//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000455void
Vikram S. Advee64574c2001-11-08 05:20:23 +0000456SchedGraph::addMemEdges(const vector<SchedGraphNode*>& memNodeVec,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000457 const TargetMachine& target)
458{
459 const MachineInstrInfo& mii = target.getInstrInfo();
460
Vikram S. Advee64574c2001-11-08 05:20:23 +0000461 // Instructions in memNodeVec are in execution order within the basic block,
462 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
463 //
464 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000465 {
Vikram S. Advee64574c2001-11-08 05:20:23 +0000466 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
467 int fromType = mii.isCall(fromOpCode)? SG_CALL_REF
468 : mii.isLoad(fromOpCode)? SG_LOAD_REF
469 : SG_STORE_REF;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000470 for (unsigned jm=im+1; jm < NM; jm++)
471 {
Vikram S. Advee64574c2001-11-08 05:20:23 +0000472 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
473 int toType = mii.isCall(toOpCode)? SG_CALL_REF
474 : mii.isLoad(toOpCode)? SG_LOAD_REF
475 : SG_STORE_REF;
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000476
Vikram S. Advee64574c2001-11-08 05:20:23 +0000477 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
478 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
479 SchedGraphEdge::MemoryDep,
480 SG_DepOrderArray[fromType][toType], 1);
481 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000482 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000483}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000484
Vikram S. Advee64574c2001-11-08 05:20:23 +0000485// Add edges from/to CC reg instrs to/from call instrs.
486// Essentially this prevents anything that sets or uses a CC reg from being
487// reordered w.r.t. a call.
488// Use a latency of 0 because we only need to prevent out-of-order issue,
489// like with control dependences.
490//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000491void
Vikram S. Advee64574c2001-11-08 05:20:23 +0000492SchedGraph::addCallCCEdges(const vector<SchedGraphNode*>& memNodeVec,
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000493 MachineCodeForBasicBlock& bbMvec,
494 const TargetMachine& target)
495{
496 const MachineInstrInfo& mii = target.getInstrInfo();
497 vector<SchedGraphNode*> callNodeVec;
498
Vikram S. Advee64574c2001-11-08 05:20:23 +0000499 // Find the call instruction nodes and put them in a vector.
500 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
501 if (mii.isCall(memNodeVec[im]->getOpCode()))
502 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000503
Vikram S. Advee64574c2001-11-08 05:20:23 +0000504 // Now walk the entire basic block, looking for CC instructions *and*
505 // call instructions, and keep track of the order of the instructions.
506 // Use the call node vec to quickly find earlier and later call nodes
507 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000508 //
509 int lastCallNodeIdx = -1;
510 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
511 if (mii.isCall(bbMvec[i]->getOpCode()))
512 {
513 ++lastCallNodeIdx;
514 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
515 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
516 break;
517 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
518 }
519 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
520 { // Add incoming/outgoing edges from/to preceding/later calls
521 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
522 int j=0;
523 for ( ; j <= lastCallNodeIdx; j++)
524 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
525 MachineCCRegsRID, 0);
526 for ( ; j < (int) callNodeVec.size(); j++)
527 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
528 MachineCCRegsRID, 0);
529 }
530}
531
532
533void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000534SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000535 const TargetMachine& target)
536{
537 assert(bbVec.size() == 1 && "Only handling a single basic block here");
538
539 // This assumes that such hardwired registers are never allocated
540 // to any LLVM value (since register allocation happens later), i.e.,
541 // any uses or defs of this register have been made explicit!
542 // Also assumes that two registers with different numbers are
543 // not aliased!
544 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000545 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000546 I != regToRefVecMap.end(); ++I)
547 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000548 int regNum = (*I).first;
549 RefVec& regRefVec = (*I).second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000550
551 // regRefVec is ordered by control flow order in the basic block
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000552 for (unsigned i=0; i < regRefVec.size(); ++i)
553 {
554 SchedGraphNode* node = regRefVec[i].first;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000555 unsigned int opNum = regRefVec[i].second;
556 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
557
558 for (unsigned p=0; p < i; ++p)
559 {
560 SchedGraphNode* prevNode = regRefVec[p].first;
561 if (prevNode != node)
562 {
563 unsigned int prevOpNum = regRefVec[p].second;
564 bool prevIsDef =
565 prevNode->getMachineInstr()->operandIsDefined(prevOpNum);
566
567 if (isDef)
568 new SchedGraphEdge(prevNode, node, regNum,
569 (prevIsDef)? SchedGraphEdge::OutputDep
570 : SchedGraphEdge::AntiDep);
571 else if (prevIsDef)
572 new SchedGraphEdge(prevNode, node, regNum,
573 SchedGraphEdge::TrueDep);
574 }
575 }
576 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000577 }
578}
579
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000580
581void
Vikram S. Adve200a4352001-11-12 18:53:43 +0000582SchedGraph::addEdgesForValue(SchedGraphNode* refNode,
583 const RefVec& defVec,
584 const Value* defValue,
585 bool refNodeIsDef,
586 const TargetMachine& target)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000587{
Vikram S. Adve200a4352001-11-12 18:53:43 +0000588 // Add true or output dep edges from all def nodes before refNode in BB.
589 // Add anti or output dep edges to all def nodes after refNode.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000590 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I)
Vikram S. Adve200a4352001-11-12 18:53:43 +0000591 {
592 if ((*I).first == refNode)
593 continue; // Dont add any self-loops
594
595 if ((*I).first->getOrigIndexInBB() < refNode->getOrigIndexInBB())
596 // (*).first is before refNode
597 (void) new SchedGraphEdge((*I).first, refNode, defValue,
598 (refNodeIsDef)? SchedGraphEdge::OutputDep
599 : SchedGraphEdge::TrueDep);
600 else
601 // (*).first is after refNode
602 (void) new SchedGraphEdge(refNode, (*I).first, defValue,
603 (refNodeIsDef)? SchedGraphEdge::OutputDep
604 : SchedGraphEdge::AntiDep);
605 }
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000606}
607
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000608
609void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000610SchedGraph::addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000611 const ValueToDefVecMap& valueToDefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000612 const TargetMachine& target)
613{
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000614 SchedGraphNode* node = this->getGraphNodeForInstr(&minstr);
615 if (node == NULL)
616 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000617
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000618 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000619 //
620 for (unsigned i=0, numOps=minstr.getNumOperands(); i < numOps; i++)
621 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000622 const MachineOperand& mop = minstr.getOperand(i);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000623 switch(mop.getOperandType())
624 {
625 case MachineOperand::MO_VirtualRegister:
626 case MachineOperand::MO_CCRegister:
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000627 if (const Instruction* srcI =
628 dyn_cast_or_null<Instruction>(mop.getVRegValue()))
629 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000630 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
631 if (I != valueToDefVecMap.end())
Vikram S. Adve200a4352001-11-12 18:53:43 +0000632 addEdgesForValue(node, (*I).second, mop.getVRegValue(),
633 minstr.operandIsDefined(i), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000634 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000635 break;
636
637 case MachineOperand::MO_MachineRegister:
638 break;
639
640 case MachineOperand::MO_SignExtendedImmed:
641 case MachineOperand::MO_UnextendedImmed:
642 case MachineOperand::MO_PCRelativeDisp:
643 break; // nothing to do for immediate fields
644
645 default:
646 assert(0 && "Unknown machine operand type in SchedGraph builder");
647 break;
648 }
649 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000650
651 // Add edges for values implicitly used by the machine instruction.
652 // Examples include function arguments to a Call instructions or the return
653 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000654 //
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000655 for (unsigned i=0, N=minstr.getNumImplicitRefs(); i < N; ++i)
656 if (! minstr.implicitRefIsDefined(i))
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000657 if (const Instruction* srcI =
658 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
659 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000660 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
661 if (I != valueToDefVecMap.end())
Vikram S. Adve200a4352001-11-12 18:53:43 +0000662 addEdgesForValue(node, (*I).second, minstr.getImplicitRef(i),
663 minstr.implicitRefIsDefined(i), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000664 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000665}
666
667
Vikram S. Adve200a4352001-11-12 18:53:43 +0000668#undef NEED_SEPARATE_NONSSA_EDGES_CODE
669#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000670void
671SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
672 const TargetMachine& target)
673{
Chris Lattnerb00c5822001-10-02 03:41:24 +0000674 if (isa<PHINode>(instr))
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000675 return;
Vikram S. Adve200a4352001-11-12 18:53:43 +0000676
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000677 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
678 const MachineInstrInfo& mii = target.getInstrInfo();
679 RefVec refVec;
680
681 for (unsigned i=0, N=mvec.size(); i < N; i++)
682 for (int o=0, N = mii.getNumOperands(mvec[i]->getOpCode()); o < N; o++)
683 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000684 const MachineOperand& mop = mvec[i]->getOperand(o);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000685
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000686 if ((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
687 mop.getOperandType() == MachineOperand::MO_CCRegister)
688 && mop.getVRegValue() == (Value*) instr)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000689 {
690 // this operand is a definition or use of value `instr'
691 SchedGraphNode* node = this->getGraphNodeForInstr(mvec[i]);
692 assert(node && "No node for machine instruction in this BB?");
693 refVec.push_back(make_pair(node, o));
694 }
695 }
696
697 // refVec is ordered by control flow order of the machine instructions
698 for (unsigned i=0; i < refVec.size(); ++i)
699 {
700 SchedGraphNode* node = refVec[i].first;
701 unsigned int opNum = refVec[i].second;
702 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
703
704 if (isDef)
705 // add output and/or anti deps to this definition
706 for (unsigned p=0; p < i; ++p)
707 {
708 SchedGraphNode* prevNode = refVec[p].first;
709 if (prevNode != node)
710 {
711 bool prevIsDef = prevNode->getMachineInstr()->
712 operandIsDefined(refVec[p].second);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000713 new SchedGraphEdge(prevNode, node, SchedGraphEdge::ValueDep,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000714 (prevIsDef)? SchedGraphEdge::OutputDep
715 : SchedGraphEdge::AntiDep);
716 }
717 }
718 }
719}
Vikram S. Adve200a4352001-11-12 18:53:43 +0000720#endif NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000721
722
723void
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000724SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
725 SchedGraphNode* node,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000726 vector<SchedGraphNode*>& memNodeVec,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000727 RegToRefVecMap& regToRefVecMap,
728 ValueToDefVecMap& valueToDefVecMap)
729{
730 const MachineInstrInfo& mii = target.getInstrInfo();
731
Vikram S. Advee64574c2001-11-08 05:20:23 +0000732
733 MachineOpCode opCode = node->getOpCode();
734 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
735 memNodeVec.push_back(node);
736
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000737 // Collect the register references and value defs. for explicit operands
738 //
739 const MachineInstr& minstr = * node->getMachineInstr();
740 for (int i=0, numOps = (int) minstr.getNumOperands(); i < numOps; i++)
741 {
742 const MachineOperand& mop = minstr.getOperand(i);
743
744 // if this references a register other than the hardwired
745 // "zero" register, record the reference.
746 if (mop.getOperandType() == MachineOperand::MO_MachineRegister)
747 {
748 int regNum = mop.getMachineRegNum();
749 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Advee64574c2001-11-08 05:20:23 +0000750 regToRefVecMap[mop.getMachineRegNum()].push_back(make_pair(node,
751 i));
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000752 continue; // nothing more to do
753 }
754
755 // ignore all other non-def operands
756 if (! minstr.operandIsDefined(i))
757 continue;
758
759 // We must be defining a value.
760 assert((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
761 mop.getOperandType() == MachineOperand::MO_CCRegister)
762 && "Do not expect any other kind of operand to be defined!");
763
764 const Instruction* defInstr = cast<Instruction>(mop.getVRegValue());
765 valueToDefVecMap[defInstr].push_back(make_pair(node, i));
766 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000767
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000768 //
769 // Collect value defs. for implicit operands. The interface to extract
770 // them assumes they must be virtual registers!
771 //
772 for (int i=0, N = (int) minstr.getNumImplicitRefs(); i < N; ++i)
773 if (minstr.implicitRefIsDefined(i))
774 if (const Instruction* defInstr =
775 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
776 {
777 valueToDefVecMap[defInstr].push_back(make_pair(node, -i));
778 }
779}
780
781
782void
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000783SchedGraph::buildNodesforBB(const TargetMachine& target,
784 const BasicBlock* bb,
785 vector<SchedGraphNode*>& memNodeVec,
786 RegToRefVecMap& regToRefVecMap,
787 ValueToDefVecMap& valueToDefVecMap)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000788{
789 const MachineInstrInfo& mii = target.getInstrInfo();
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000790
791 // Build graph nodes for each VM instruction and gather def/use info.
792 // Do both those together in a single pass over all machine instructions.
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000793 const MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
794 for (unsigned i=0; i < mvec.size(); i++)
795 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
796 {
797 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), bb,
798 mvec[i], i, target);
799 this->noteGraphNodeForInstr(mvec[i], node);
800
801 // Remember all register references and value defs
802 findDefUseInfoAtInstr(target, node,
803 memNodeVec, regToRefVecMap,valueToDefVecMap);
804 }
805
806#undef REALLY_NEED_TO_SEARCH_SUCCESSOR_PHIS
807#ifdef REALLY_NEED_TO_SEARCH_SUCCESSOR_PHIS
808 // This is a BIG UGLY HACK. IT NEEDS TO BE ELIMINATED.
809 // Look for copy instructions inserted in this BB due to Phi instructions
810 // in the successor BBs.
811 // There MUST be exactly one copy per Phi in successor nodes.
812 //
813 for (BasicBlock::succ_const_iterator SI=bb->succ_begin(), SE=bb->succ_end();
814 SI != SE; ++SI)
815 for (BasicBlock::const_iterator PI=(*SI)->begin(), PE=(*SI)->end();
816 PI != PE; ++PI)
817 {
818 if ((*PI)->getOpcode() != Instruction::PHINode)
819 break; // No more Phis in this successor
820
821 // Find the incoming value from block bb to block (*SI)
822 int bbIndex = cast<PHINode>(*PI)->getBasicBlockIndex(bb);
823 assert(bbIndex >= 0 && "But I know bb is a predecessor of (*SI)?");
824 Value* inVal = cast<PHINode>(*PI)->getIncomingValue(bbIndex);
825 assert(inVal != NULL && "There must be an in-value on every edge");
826
827 // Find the machine instruction that makes a copy of inval to (*PI).
828 // This must be in the current basic block (bb).
829 const MachineCodeForVMInstr& mvec = (*PI)->getMachineInstrVec();
830 const MachineInstr* theCopy = NULL;
831 for (unsigned i=0; i < mvec.size() && theCopy == NULL; i++)
832 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
833 // not a Phi: assume this is a copy and examine its operands
834 for (int o=0, N=(int) mvec[i]->getNumOperands(); o < N; o++)
835 {
836 const MachineOperand& mop = mvec[i]->getOperand(o);
837 if (mvec[i]->operandIsDefined(o))
838 assert(mop.getVRegValue() == (*PI) && "dest shd be my Phi");
839 else if (mop.getVRegValue() == inVal)
840 { // found the copy!
841 theCopy = mvec[i];
842 break;
843 }
844 }
845
846 // Found the dang instruction. Now create a node and do the rest...
847 if (theCopy != NULL)
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000848 {
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000849 SchedGraphNode* node = new SchedGraphNode(getNumNodes(), bb,
850 theCopy, origIndexInBB++, target);
851 this->noteGraphNodeForInstr(theCopy, node);
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000852 findDefUseInfoAtInstr(target, node,
853 memNodeVec, regToRefVecMap,valueToDefVecMap);
854 }
Vikram S. Adveaf00d482001-11-12 14:18:01 +0000855 }
856#endif REALLY_NEED_TO_SEARCH_SUCCESSOR_PHIS
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000857}
858
859
860void
861SchedGraph::buildGraph(const TargetMachine& target)
862{
863 const MachineInstrInfo& mii = target.getInstrInfo();
864 const BasicBlock* bb = bbVec[0];
865
866 assert(bbVec.size() == 1 && "Only handling a single basic block here");
867
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000868 // Use this data structure to note all machine operands that compute
869 // ordinary LLVM values. These must be computed defs (i.e., instructions).
870 // Note that there may be multiple machine instructions that define
871 // each Value.
872 ValueToDefVecMap valueToDefVecMap;
873
Vikram S. Advee64574c2001-11-08 05:20:23 +0000874 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000875 // We use this to add memory dependence edges without a second full walk.
876 //
Vikram S. Advee64574c2001-11-08 05:20:23 +0000877 // vector<const Instruction*> memVec;
878 vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000879
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000880 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000881 // machine registers so we can add edges for those later without
882 // extra passes over the nodes.
883 // The vector holds an ordered list of references to the machine reg,
884 // ordered according to control-flow order. This only works for a
885 // single basic block, hence the assertion. Each reference is identified
886 // by the pair: <node, operand-number>.
887 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000888 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000889
890 // Make a dummy root node. We'll add edges to the real roots later.
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000891 graphRoot = new SchedGraphNode(0, NULL, NULL, -1, target);
892 graphLeaf = new SchedGraphNode(1, NULL, NULL, -1, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000893
894 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000895 // First add nodes for all the machine instructions in the basic block
896 // because this greatly simplifies identifying which edges to add.
897 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000898 // Also, remember the load/store instructions to add memory deps later.
899 //----------------------------------------------------------------
900
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000901 buildNodesforBB(target, bb, memNodeVec, regToRefVecMap, valueToDefVecMap);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000902
903 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000904 // Now add edges for the following (all are incoming edges except (4)):
905 // (1) operands of the machine instruction, including hidden operands
906 // (2) machine register dependences
907 // (3) memory load/store dependences
908 // (3) other resource dependences for the machine instruction, if any
909 // (4) output dependences when multiple machine instructions define the
910 // same value; all must have been generated from a single VM instrn
911 // (5) control dependences to branch instructions generated for the
912 // terminator instruction of the BB. Because of delay slots and
913 // 2-way conditional branches, multiple CD edges are needed
914 // (see addCDEdges for details).
915 // Also, note any uses or defs of machine registers.
916 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000917 //----------------------------------------------------------------
918
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000919 MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
920
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000921 // First, add edges to the terminator instruction of the basic block.
922 this->addCDEdges(bb->getTerminator(), target);
923
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000924 // Then add memory dep edges: store->load, load->store, and store->store.
925 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000926 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000927
928 // Then add edges between call instructions and CC set/use instructions
Vikram S. Advee64574c2001-11-08 05:20:23 +0000929 this->addCallCCEdges(memNodeVec, bbMvec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000930
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000931 // Then add incoming def-use (SSA) edges for each machine instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000932 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000933 addEdgesForInstruction(*bbMvec[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000934
Vikram S. Adve200a4352001-11-12 18:53:43 +0000935#ifdef NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000936 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000937 // We assume that all machine instructions that define a value are
938 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000939 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000940 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000941 this->addNonSSAEdgesForValue(*II, target);
Vikram S. Adve200a4352001-11-12 18:53:43 +0000942#endif NEED_SEPARATE_NONSSA_EDGES_CODE
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000943
944 // Then add edges for dependences on machine registers
945 this->addMachineRegEdges(regToRefVecMap, target);
946
947 // Finally, add edges from the dummy root and to dummy leaf
948 this->addDummyEdges();
949}
950
951
952//
953// class SchedGraphSet
954//
955
956/*ctor*/
957SchedGraphSet::SchedGraphSet(const Method* _method,
958 const TargetMachine& target) :
959 method(_method)
960{
961 buildGraphsForMethod(method, target);
962}
963
964
965/*dtor*/
966SchedGraphSet::~SchedGraphSet()
967{
968 // delete all the graphs
969 for (iterator I=begin(); I != end(); ++I)
970 delete (*I).second;
971}
972
973
974void
975SchedGraphSet::dump() const
976{
977 cout << "======== Sched graphs for method `"
978 << (method->hasName()? method->getName() : "???")
979 << "' ========" << endl << endl;
980
981 for (const_iterator I=begin(); I != end(); ++I)
982 (*I).second->dump();
983
984 cout << endl << "====== End graphs for method `"
985 << (method->hasName()? method->getName() : "")
986 << "' ========" << endl << endl;
987}
988
989
990void
991SchedGraphSet::buildGraphsForMethod(const Method *method,
992 const TargetMachine& target)
993{
994 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
995 {
996 SchedGraph* graph = new SchedGraph(*BI, target);
997 this->noteGraphForBlock(*BI, graph);
998 }
999}
1000
1001
1002
1003ostream&
1004operator<<(ostream& os, const SchedGraphEdge& edge)
1005{
1006 os << "edge [" << edge.src->getNodeId() << "] -> ["
1007 << edge.sink->getNodeId() << "] : ";
1008
1009 switch(edge.depType) {
1010 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
Vikram S. Adve200a4352001-11-12 18:53:43 +00001011 case SchedGraphEdge::ValueDep: os<< "Reg Value " << edge.val; break;
1012 case SchedGraphEdge::MemoryDep: os<< "Memory Dep"; break;
Vikram S. Adve78ef1392001-08-28 23:06:02 +00001013 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
1014 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
1015 default: assert(0); break;
1016 }
1017
1018 os << " : delay = " << edge.minDelay << endl;
1019
1020 return os;
1021}
1022
1023ostream&
1024operator<<(ostream& os, const SchedGraphNode& node)
1025{
1026 printIndent(4, os);
1027 os << "Node " << node.nodeId << " : "
1028 << "latency = " << node.latency << endl;
1029
1030 printIndent(6, os);
1031
1032 if (node.getMachineInstr() == NULL)
1033 os << "(Dummy node)" << endl;
1034 else
1035 {
1036 os << *node.getMachineInstr() << endl;
1037
1038 printIndent(6, os);
1039 os << node.inEdges.size() << " Incoming Edges:" << endl;
1040 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
1041 {
1042 printIndent(8, os);
1043 os << * node.inEdges[i];
1044 }
1045
1046 printIndent(6, os);
1047 os << node.outEdges.size() << " Outgoing Edges:" << endl;
1048 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
1049 {
1050 printIndent(8, os);
1051 os << * node.outEdges[i];
1052 }
1053 }
1054
1055 return os;
1056}