blob: 2bff552faf02eff68286344711851c330a203e49 [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. Adve78ef1392001-08-28 23:06:02 +000027
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000028
29//*********************** Internal Data Structures *************************/
30
31typedef vector< pair<SchedGraphNode*, unsigned int> > RefVec;
32
33// The following needs to be a class, not a typedef, so we can use
34// an opaque declaration in SchedGraph.h
Chris Lattner80c685f2001-10-13 06:51:01 +000035struct RegToRefVecMap: public hash_map<int, RefVec> {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000036 typedef hash_map<int, RefVec>:: iterator iterator;
37 typedef hash_map<int, RefVec>::const_iterator const_iterator;
38};
39
Vikram S. Adve78ef1392001-08-28 23:06:02 +000040//
41// class SchedGraphEdge
42//
43
44/*ctor*/
45SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
46 SchedGraphNode* _sink,
47 SchedGraphEdgeDepType _depType,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000048 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000049 int _minDelay)
50 : src(_src),
51 sink(_sink),
52 depType(_depType),
53 depOrderType(_depOrderType),
Chris Lattner80c685f2001-10-13 06:51:01 +000054 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
55 val(NULL)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000056{
57 src->addOutEdge(this);
58 sink->addInEdge(this);
59}
60
61
62/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000063SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
64 SchedGraphNode* _sink,
65 const Value* _val,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000066 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000067 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000068 : src(_src),
69 sink(_sink),
70 depType(DefUseDep),
71 depOrderType(_depOrderType),
Chris Lattner80c685f2001-10-13 06:51:01 +000072 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
73 val(_val)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000074{
75 src->addOutEdge(this);
76 sink->addInEdge(this);
77}
78
79
80/*ctor*/
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000081SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
82 SchedGraphNode* _sink,
83 unsigned int _regNum,
Vikram S. Advea93bbac2001-10-28 21:43:33 +000084 unsigned int _depOrderType,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +000085 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086 : src(_src),
87 sink(_sink),
88 depType(MachineRegister),
89 depOrderType(_depOrderType),
90 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
91 machineRegNum(_regNum)
92{
93 src->addOutEdge(this);
94 sink->addInEdge(this);
95}
96
97
98/*ctor*/
99SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
100 SchedGraphNode* _sink,
101 ResourceId _resourceId,
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000102 int _minDelay)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000103 : src(_src),
104 sink(_sink),
105 depType(MachineResource),
106 depOrderType(NonDataDep),
107 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
108 resourceId(_resourceId)
109{
110 src->addOutEdge(this);
111 sink->addInEdge(this);
112}
113
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000114/*dtor*/
115SchedGraphEdge::~SchedGraphEdge()
116{
117}
118
Chris Lattnerc83e9542001-09-07 21:21:03 +0000119void SchedGraphEdge::dump(int indent=0) const {
120 printIndent(indent); cout << *this;
121}
122
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000123
124//
125// class SchedGraphNode
126//
127
128/*ctor*/
129SchedGraphNode::SchedGraphNode(unsigned int _nodeId,
130 const Instruction* _instr,
131 const MachineInstr* _minstr,
132 const TargetMachine& target)
133 : nodeId(_nodeId),
134 instr(_instr),
135 minstr(_minstr),
136 latency(0)
137{
138 if (minstr)
139 {
140 MachineOpCode mopCode = minstr->getOpCode();
141 latency = target.getInstrInfo().hasResultInterlock(mopCode)
142 ? target.getInstrInfo().minLatency(mopCode)
143 : target.getInstrInfo().maxLatency(mopCode);
144 }
145}
146
147
148/*dtor*/
149SchedGraphNode::~SchedGraphNode()
150{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151}
152
Chris Lattnerc83e9542001-09-07 21:21:03 +0000153void SchedGraphNode::dump(int indent=0) const {
154 printIndent(indent); cout << *this;
155}
156
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000157
158inline void
159SchedGraphNode::addInEdge(SchedGraphEdge* edge)
160{
161 inEdges.push_back(edge);
162}
163
164
165inline void
166SchedGraphNode::addOutEdge(SchedGraphEdge* edge)
167{
168 outEdges.push_back(edge);
169}
170
171inline void
172SchedGraphNode::removeInEdge(const SchedGraphEdge* edge)
173{
174 assert(edge->getSink() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000175
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000176 for (iterator I = beginInEdges(); I != endInEdges(); ++I)
177 if ((*I) == edge)
178 {
179 inEdges.erase(I);
180 break;
181 }
182}
183
184inline void
185SchedGraphNode::removeOutEdge(const SchedGraphEdge* edge)
186{
187 assert(edge->getSrc() == this);
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000188
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000189 for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
190 if ((*I) == edge)
191 {
192 outEdges.erase(I);
193 break;
194 }
195}
196
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000197
198//
199// class SchedGraph
200//
201
202
203/*ctor*/
204SchedGraph::SchedGraph(const BasicBlock* bb,
205 const TargetMachine& target)
206{
207 bbVec.push_back(bb);
208 this->buildGraph(target);
209}
210
211
212/*dtor*/
213SchedGraph::~SchedGraph()
214{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000215 for (iterator I=begin(); I != end(); ++I)
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000216 {
217 SchedGraphNode* node = (*I).second;
218
219 // for each node, delete its out-edges
220 for (SchedGraphNode::iterator I = node->beginOutEdges();
221 I != node->endOutEdges(); ++I)
222 delete *I;
223
224 // then delete the node itself.
225 delete node;
226 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000227}
228
229
230void
231SchedGraph::dump() const
232{
233 cout << " Sched Graph for Basic Blocks: ";
234 for (unsigned i=0, N=bbVec.size(); i < N; i++)
235 {
236 cout << (bbVec[i]->hasName()? bbVec[i]->getName() : "block")
237 << " (" << bbVec[i] << ")"
238 << ((i == N-1)? "" : ", ");
239 }
240
241 cout << endl << endl << " Actual Root nodes : ";
242 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
243 cout << graphRoot->outEdges[i]->getSink()->getNodeId()
244 << ((i == N-1)? "" : ", ");
245
246 cout << endl << " Graph Nodes:" << endl;
247 for (const_iterator I=begin(); I != end(); ++I)
248 cout << endl << * (*I).second;
249
250 cout << endl;
251}
252
253
254void
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000255SchedGraph::eraseIncomingEdges(SchedGraphNode* node, bool addDummyEdges)
256{
257 // Delete and disconnect all in-edges for the node
258 for (SchedGraphNode::iterator I = node->beginInEdges();
259 I != node->endInEdges(); ++I)
260 {
261 SchedGraphNode* srcNode = (*I)->getSrc();
262 srcNode->removeOutEdge(*I);
263 delete *I;
264
265 if (addDummyEdges &&
266 srcNode != getRoot() &&
267 srcNode->beginOutEdges() == srcNode->endOutEdges())
268 { // srcNode has no more out edges, so add an edge to dummy EXIT node
269 assert(node != getLeaf() && "Adding edge that was just removed?");
270 (void) new SchedGraphEdge(srcNode, getLeaf(),
271 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
272 }
273 }
274
275 node->inEdges.clear();
276}
277
278void
279SchedGraph::eraseOutgoingEdges(SchedGraphNode* node, bool addDummyEdges)
280{
281 // Delete and disconnect all out-edges for the node
282 for (SchedGraphNode::iterator I = node->beginOutEdges();
283 I != node->endOutEdges(); ++I)
284 {
285 SchedGraphNode* sinkNode = (*I)->getSink();
286 sinkNode->removeInEdge(*I);
287 delete *I;
288
289 if (addDummyEdges &&
290 sinkNode != getLeaf() &&
291 sinkNode->beginInEdges() == sinkNode->endInEdges())
292 { //sinkNode has no more in edges, so add an edge from dummy ENTRY node
293 assert(node != getRoot() && "Adding edge that was just removed?");
294 (void) new SchedGraphEdge(getRoot(), sinkNode,
295 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
296 }
297 }
298
299 node->outEdges.clear();
300}
301
302void
303SchedGraph::eraseIncidentEdges(SchedGraphNode* node, bool addDummyEdges)
304{
305 this->eraseIncomingEdges(node, addDummyEdges);
306 this->eraseOutgoingEdges(node, addDummyEdges);
307}
308
309
310void
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000311SchedGraph::addDummyEdges()
312{
313 assert(graphRoot->outEdges.size() == 0);
314
315 for (const_iterator I=begin(); I != end(); ++I)
316 {
317 SchedGraphNode* node = (*I).second;
318 assert(node != graphRoot && node != graphLeaf);
319 if (node->beginInEdges() == node->endInEdges())
320 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
321 SchedGraphEdge::NonDataDep, 0);
322 if (node->beginOutEdges() == node->endOutEdges())
323 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
324 SchedGraphEdge::NonDataDep, 0);
325 }
326}
327
328
329void
330SchedGraph::addCDEdges(const TerminatorInst* term,
331 const TargetMachine& target)
332{
333 const MachineInstrInfo& mii = target.getInstrInfo();
334 MachineCodeForVMInstr& termMvec = term->getMachineInstrVec();
335
336 // Find the first branch instr in the sequence of machine instrs for term
337 //
338 unsigned first = 0;
339 while (! mii.isBranch(termMvec[first]->getOpCode()))
340 ++first;
341 assert(first < termMvec.size() &&
342 "No branch instructions for BR? Ok, but weird! Delete assertion.");
343 if (first == termMvec.size())
344 return;
345
346 SchedGraphNode* firstBrNode = this->getGraphNodeForInstr(termMvec[first]);
347
348 // Add CD edges from each instruction in the sequence to the
349 // *last preceding* branch instr. in the sequence
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000350 // Use a latency of 0 because we only need to prevent out-of-order issue.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000351 //
352 for (int i = (int) termMvec.size()-1; i > (int) first; i--)
353 {
354 SchedGraphNode* toNode = this->getGraphNodeForInstr(termMvec[i]);
355 assert(toNode && "No node for instr generated for branch?");
356
357 for (int j = i-1; j >= 0; j--)
358 if (mii.isBranch(termMvec[j]->getOpCode()))
359 {
360 SchedGraphNode* brNode = this->getGraphNodeForInstr(termMvec[j]);
361 assert(brNode && "No node for instr generated for branch?");
362 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
363 SchedGraphEdge::NonDataDep, 0);
364 break; // only one incoming edge is enough
365 }
366 }
367
368 // Add CD edges from each instruction preceding the first branch
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000369 // to the first branch. Use a latency of 0 as above.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000370 //
371 for (int i = first-1; i >= 0; i--)
372 {
373 SchedGraphNode* fromNode = this->getGraphNodeForInstr(termMvec[i]);
374 assert(fromNode && "No node for instr generated for branch?");
375 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
376 SchedGraphEdge::NonDataDep, 0);
377 }
378
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000379 // Now add CD edges to the first branch instruction in the sequence from
380 // all preceding instructions in the basic block. Use 0 latency again.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000381 //
382 const BasicBlock* bb = term->getParent();
383 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
384 {
385 if ((*II) == (const Instruction*) term) // special case, handled above
386 continue;
387
388 assert(! (*II)->isTerminator() && "Two terminators in basic block?");
389
390 const MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
391 for (unsigned i=0, N=mvec.size(); i < N; i++)
392 {
393 SchedGraphNode* fromNode = this->getGraphNodeForInstr(mvec[i]);
394 if (fromNode == NULL)
395 continue; // dummy instruction, e.g., PHI
396
397 (void) new SchedGraphEdge(fromNode, firstBrNode,
398 SchedGraphEdge::CtrlDep,
399 SchedGraphEdge::NonDataDep, 0);
400
401 // If we find any other machine instructions (other than due to
402 // the terminator) that also have delay slots, add an outgoing edge
403 // from the instruction to the instructions in the delay slots.
404 //
405 unsigned d = mii.getNumDelaySlots(mvec[i]->getOpCode());
406 assert(i+d < N && "Insufficient delay slots for instruction?");
407
408 for (unsigned j=1; j <= d; j++)
409 {
410 SchedGraphNode* toNode = this->getGraphNodeForInstr(mvec[i+j]);
411 assert(toNode && "No node for machine instr in delay slot?");
412 (void) new SchedGraphEdge(fromNode, toNode,
413 SchedGraphEdge::CtrlDep,
414 SchedGraphEdge::NonDataDep, 0);
415 }
416 }
417 }
418}
419
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000420static const int SG_LOAD_REF = 0;
421static const int SG_STORE_REF = 1;
422static const int SG_CALL_REF = 2;
423
424static const unsigned int SG_DepOrderArray[][3] = {
425 { SchedGraphEdge::NonDataDep,
426 SchedGraphEdge::AntiDep,
427 SchedGraphEdge::AntiDep },
428 { SchedGraphEdge::TrueDep,
429 SchedGraphEdge::OutputDep,
430 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
431 { SchedGraphEdge::TrueDep,
432 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
433 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
434 | SchedGraphEdge::OutputDep }
435};
436
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000437
438void
439SchedGraph::addMemEdges(const vector<const Instruction*>& memVec,
440 const TargetMachine& target)
441{
442 const MachineInstrInfo& mii = target.getInstrInfo();
443
444 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
445 {
446 const Instruction* fromInstr = memVec[im];
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000447 int fromType = (fromInstr->getOpcode() == Instruction::Call
448 ? SG_CALL_REF
449 : (fromInstr->getOpcode() == Instruction::Load
450 ? SG_LOAD_REF
451 : SG_STORE_REF));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000452
453 for (unsigned jm=im+1; jm < NM; jm++)
454 {
455 const Instruction* toInstr = memVec[jm];
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000456 int toType = (fromInstr->getOpcode() == Instruction::Call? 2
457 : ((fromInstr->getOpcode()==Instruction::Load)? 0:1));
458
459 if (fromType == SG_LOAD_REF && toType == SG_LOAD_REF)
460 continue;
461
462 unsigned int depOrderType = SG_DepOrderArray[fromType][toType];
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000463
464 MachineCodeForVMInstr& fromInstrMvec=fromInstr->getMachineInstrVec();
465 MachineCodeForVMInstr& toInstrMvec = toInstr->getMachineInstrVec();
466
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000467 // We have two VM memory instructions, and at least one is a store
468 // or a call. Add edges between all machine load/store/call instrs.
469 // Use a latency of 1 to ensure that memory operations are ordered;
470 // latency does not otherwise matter (true dependences enforce that).
471 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000472 for (unsigned i=0, N=fromInstrMvec.size(); i < N; i++)
473 {
474 MachineOpCode fromOpCode = fromInstrMvec[i]->getOpCode();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000475
476 if (mii.isLoad(fromOpCode) || mii.isStore(fromOpCode) ||
477 mii.isCall(fromOpCode))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000478 {
479 SchedGraphNode* fromNode =
480 this->getGraphNodeForInstr(fromInstrMvec[i]);
481 assert(fromNode && "No node for memory instr?");
482
483 for (unsigned j=0, M=toInstrMvec.size(); j < M; j++)
484 {
485 MachineOpCode toOpCode = toInstrMvec[j]->getOpCode();
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000486 if (mii.isLoad(toOpCode) || mii.isStore(toOpCode)
487 || mii.isCall(fromOpCode))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000488 {
489 SchedGraphNode* toNode =
490 this->getGraphNodeForInstr(toInstrMvec[j]);
491 assert(toNode && "No node for memory instr?");
492
493 (void) new SchedGraphEdge(fromNode, toNode,
494 SchedGraphEdge::MemoryDep,
495 depOrderType, 1);
496 }
497 }
498 }
499 }
500 }
501 }
502}
503
504
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000505void
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000506SchedGraph::addCallCCEdges(const vector<const Instruction*>& memVec,
507 MachineCodeForBasicBlock& bbMvec,
508 const TargetMachine& target)
509{
510 const MachineInstrInfo& mii = target.getInstrInfo();
511 vector<SchedGraphNode*> callNodeVec;
512
513 // Find the call machine instructions and put them in a vector.
514 // By using memVec, we avoid searching the entire machine code of the BB.
515 //
516 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
517 if (memVec[im]->getOpcode() == Instruction::Call)
518 {
519 MachineCodeForVMInstr& callMvec=memVec[im]->getMachineInstrVec();
520 for (unsigned i=0; i < callMvec.size(); ++i)
521 if (mii.isCall(callMvec[i]->getOpCode()))
522 callNodeVec.push_back(this->getGraphNodeForInstr(callMvec[i]));
523 }
524
525 // Now add additional edges from/to CC reg instrs to/from call instrs.
526 // Essentially this prevents anything that sets or uses a CC reg from being
527 // reordered w.r.t. a call.
528 // Use a latency of 0 because we only need to prevent out-of-order issue,
529 // like with control dependences.
530 //
531 int lastCallNodeIdx = -1;
532 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
533 if (mii.isCall(bbMvec[i]->getOpCode()))
534 {
535 ++lastCallNodeIdx;
536 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
537 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
538 break;
539 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
540 }
541 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
542 { // Add incoming/outgoing edges from/to preceding/later calls
543 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
544 int j=0;
545 for ( ; j <= lastCallNodeIdx; j++)
546 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
547 MachineCCRegsRID, 0);
548 for ( ; j < (int) callNodeVec.size(); j++)
549 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
550 MachineCCRegsRID, 0);
551 }
552}
553
554
555void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000556SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000557 const TargetMachine& target)
558{
559 assert(bbVec.size() == 1 && "Only handling a single basic block here");
560
561 // This assumes that such hardwired registers are never allocated
562 // to any LLVM value (since register allocation happens later), i.e.,
563 // any uses or defs of this register have been made explicit!
564 // Also assumes that two registers with different numbers are
565 // not aliased!
566 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000567 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000568 I != regToRefVecMap.end(); ++I)
569 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000570 int regNum = (*I).first;
571 RefVec& regRefVec = (*I).second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000572
573 // regRefVec is ordered by control flow order in the basic block
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000574 for (unsigned i=0; i < regRefVec.size(); ++i)
575 {
576 SchedGraphNode* node = regRefVec[i].first;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000577 unsigned int opNum = regRefVec[i].second;
578 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
579
580 for (unsigned p=0; p < i; ++p)
581 {
582 SchedGraphNode* prevNode = regRefVec[p].first;
583 if (prevNode != node)
584 {
585 unsigned int prevOpNum = regRefVec[p].second;
586 bool prevIsDef =
587 prevNode->getMachineInstr()->operandIsDefined(prevOpNum);
588
589 if (isDef)
590 new SchedGraphEdge(prevNode, node, regNum,
591 (prevIsDef)? SchedGraphEdge::OutputDep
592 : SchedGraphEdge::AntiDep);
593 else if (prevIsDef)
594 new SchedGraphEdge(prevNode, node, regNum,
595 SchedGraphEdge::TrueDep);
596 }
597 }
598 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000599 }
600}
601
602
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000603inline void
604CreateSSAEdge(SchedGraph* graph,
605 MachineInstr* defInstr,
606 SchedGraphNode* node,
607 const Value* val)
608{
609 // this instruction does define value `val'.
610 // if there is a node for it in the same graph, add an edge.
611 SchedGraphNode* defNode = graph->getGraphNodeForInstr(defInstr);
612 if (defNode != NULL && defNode != node)
613 (void) new SchedGraphEdge(defNode, node, val);
614}
615
616
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000617void
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000618SchedGraph::addSSAEdge(SchedGraphNode* destNode,
619 const Instruction* defVMInstr,
620 const Value* defValue,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000621 const TargetMachine& target)
622{
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000623 // Phi instructions are the only ones that produce a value but don't get
624 // any non-dummy machine instructions. Return here as an optimization.
625 //
Chris Lattnerb00c5822001-10-02 03:41:24 +0000626 if (isa<PHINode>(defVMInstr))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000627 return;
628
629 // Now add the graph edge for the appropriate machine instruction(s).
630 // Note that multiple machine instructions generated for the
631 // def VM instruction may modify the register for the def value.
632 //
633 MachineCodeForVMInstr& defMvec = defVMInstr->getMachineInstrVec();
634 const MachineInstrInfo& mii = target.getInstrInfo();
635
636 for (unsigned i=0, N=defMvec.size(); i < N; i++)
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000637 {
638 bool edgeAddedForInstr = false;
639
640 // First check the explicit operands
641 for (int o=0, N=mii.getNumOperands(defMvec[i]->getOpCode()); o < N; o++)
642 {
643 const MachineOperand& defOp = defMvec[i]->getOperand(o);
644
645 if (defOp.opIsDef()
646 && (defOp.getOperandType() == MachineOperand::MO_VirtualRegister
647 || defOp.getOperandType() == MachineOperand::MO_CCRegister)
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000648 && (defOp.getVRegValue() == defValue))
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000649 {
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000650 CreateSSAEdge(this, defMvec[i], destNode, defValue);
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000651 edgeAddedForInstr = true;
652 break;
653 }
654 }
655
656 // Then check the implicit operands
657 if (! edgeAddedForInstr)
658 {
659 for (unsigned o=0, N=defMvec[i]->getNumImplicitRefs(); o < N; ++o)
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000660 if (defMvec[i]->implicitRefIsDefined(o) &&
661 defMvec[i]->getImplicitRef(o) == defValue)
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000662 {
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000663 CreateSSAEdge(this, defMvec[i], destNode, defValue);
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000664 edgeAddedForInstr = true;
665 break;
666 }
667 }
668 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000669}
670
671
672void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000673SchedGraph::addEdgesForInstruction(const MachineInstr& minstr,
674 RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000675 const TargetMachine& target)
676{
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000677 SchedGraphNode* node = this->getGraphNodeForInstr(&minstr);
678 if (node == NULL)
679 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000680
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000681 assert(node->getInstr() && "Should be no dummy nodes here!");
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000682 const Instruction* instr = node->getInstr();
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000683
684 // Add edges for all operands of the machine instruction.
685 // Also, record all machine register references to add reg. deps. later.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000686 //
687 for (unsigned i=0, numOps=minstr.getNumOperands(); i < numOps; i++)
688 {
689 const MachineOperand& mop = minstr.getOperand(i);
690
691 // if this writes to a machine register other than the hardwired
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000692 // "zero" register, record the reference.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000693 if (mop.getOperandType() == MachineOperand::MO_MachineRegister
Vikram S. Adve8b6d2452001-09-18 12:50:40 +0000694 && (mop.getMachineRegNum()
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000695 != (unsigned) target.getRegInfo().getZeroRegNum()))
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000696 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000697 regToRefVecMap[mop.getMachineRegNum()].push_back(make_pair(node, i));
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000698 }
699
700 // ignore all other def operands
701 if (minstr.operandIsDefined(i))
702 continue;
703
704 switch(mop.getOperandType())
705 {
706 case MachineOperand::MO_VirtualRegister:
707 case MachineOperand::MO_CCRegister:
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000708 if (const Instruction* srcI =
709 dyn_cast_or_null<Instruction>(mop.getVRegValue()))
710 {
711 if (srcI->getOpcode() == TMP_INSTRUCTION_OPCODE)
712 srcI = instr;
713 addSSAEdge(node, srcI, mop.getVRegValue(), target);
714 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000715 break;
716
717 case MachineOperand::MO_MachineRegister:
718 break;
719
720 case MachineOperand::MO_SignExtendedImmed:
721 case MachineOperand::MO_UnextendedImmed:
722 case MachineOperand::MO_PCRelativeDisp:
723 break; // nothing to do for immediate fields
724
725 default:
726 assert(0 && "Unknown machine operand type in SchedGraph builder");
727 break;
728 }
729 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000730
731 // Add edges for values implicitly used by the machine instruction.
732 // Examples include function arguments to a Call instructions or the return
733 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000734 //
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000735 for (unsigned i=0, N=minstr.getNumImplicitRefs(); i < N; ++i)
736 if (! minstr.implicitRefIsDefined(i))
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000737 if (const Instruction* srcI =
738 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
739 {
740 if (srcI->getOpcode() == TMP_INSTRUCTION_OPCODE)
741 srcI = instr;
742 addSSAEdge(node, srcI, minstr.getImplicitRef(i), target);
743 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000744}
745
746
747void
748SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
749 const TargetMachine& target)
750{
Chris Lattnerb00c5822001-10-02 03:41:24 +0000751 if (isa<PHINode>(instr))
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000752 return;
753
754 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
755 const MachineInstrInfo& mii = target.getInstrInfo();
756 RefVec refVec;
757
758 for (unsigned i=0, N=mvec.size(); i < N; i++)
759 for (int o=0, N = mii.getNumOperands(mvec[i]->getOpCode()); o < N; o++)
760 {
761 const MachineOperand& op = mvec[i]->getOperand(o);
762
763 if ((op.getOperandType() == MachineOperand::MO_VirtualRegister ||
764 op.getOperandType() == MachineOperand::MO_CCRegister)
765 && op.getVRegValue() == (Value*) instr)
766 {
767 // this operand is a definition or use of value `instr'
768 SchedGraphNode* node = this->getGraphNodeForInstr(mvec[i]);
769 assert(node && "No node for machine instruction in this BB?");
770 refVec.push_back(make_pair(node, o));
771 }
772 }
773
774 // refVec is ordered by control flow order of the machine instructions
775 for (unsigned i=0; i < refVec.size(); ++i)
776 {
777 SchedGraphNode* node = refVec[i].first;
778 unsigned int opNum = refVec[i].second;
779 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
780
781 if (isDef)
782 // add output and/or anti deps to this definition
783 for (unsigned p=0; p < i; ++p)
784 {
785 SchedGraphNode* prevNode = refVec[p].first;
786 if (prevNode != node)
787 {
788 bool prevIsDef = prevNode->getMachineInstr()->
789 operandIsDefined(refVec[p].second);
790 new SchedGraphEdge(prevNode, node, SchedGraphEdge::DefUseDep,
791 (prevIsDef)? SchedGraphEdge::OutputDep
792 : SchedGraphEdge::AntiDep);
793 }
794 }
795 }
796}
797
798
799void
800SchedGraph::buildNodesforVMInstr(const TargetMachine& target,
801 const Instruction* instr)
802{
803 const MachineInstrInfo& mii = target.getInstrInfo();
804 const MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
805 for (unsigned i=0; i < mvec.size(); i++)
806 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
807 {
808 SchedGraphNode* node = new SchedGraphNode(getNumNodes(),
809 instr, mvec[i], target);
810 this->noteGraphNodeForInstr(mvec[i], node);
811 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000812}
813
814
815void
816SchedGraph::buildGraph(const TargetMachine& target)
817{
818 const MachineInstrInfo& mii = target.getInstrInfo();
819 const BasicBlock* bb = bbVec[0];
820
821 assert(bbVec.size() == 1 && "Only handling a single basic block here");
822
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000823 // Use these data structures to note all LLVM memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000824 // We use this to add memory dependence edges without a second full walk.
825 //
826 vector<const Instruction*> memVec;
827
828 // Use this data structures to note any uses or definitions of
829 // machine registers so we can add edges for those later without
830 // extra passes over the nodes.
831 // The vector holds an ordered list of references to the machine reg,
832 // ordered according to control-flow order. This only works for a
833 // single basic block, hence the assertion. Each reference is identified
834 // by the pair: <node, operand-number>.
835 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000836 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000837
838 // Make a dummy root node. We'll add edges to the real roots later.
839 graphRoot = new SchedGraphNode(0, NULL, NULL, target);
840 graphLeaf = new SchedGraphNode(1, NULL, NULL, target);
841
842 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000843 // First add nodes for all the machine instructions in the basic block
844 // because this greatly simplifies identifying which edges to add.
845 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000846 // Also, remember the load/store instructions to add memory deps later.
847 //----------------------------------------------------------------
848
849 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
850 {
851 const Instruction *instr = *II;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000852
853 // Build graph nodes for this VM instruction
854 buildNodesforVMInstr(target, instr);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000855
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000856 // Remember the load/store/call instructions to add memory deps later.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000857 if (instr->getOpcode() == Instruction::Load ||
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000858 instr->getOpcode() == Instruction::Store ||
859 instr->getOpcode() == Instruction::Call)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000860 memVec.push_back(instr);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000861 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000862
863 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000864 // Now add edges for the following (all are incoming edges except (4)):
865 // (1) operands of the machine instruction, including hidden operands
866 // (2) machine register dependences
867 // (3) memory load/store dependences
868 // (3) other resource dependences for the machine instruction, if any
869 // (4) output dependences when multiple machine instructions define the
870 // same value; all must have been generated from a single VM instrn
871 // (5) control dependences to branch instructions generated for the
872 // terminator instruction of the BB. Because of delay slots and
873 // 2-way conditional branches, multiple CD edges are needed
874 // (see addCDEdges for details).
875 // Also, note any uses or defs of machine registers.
876 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000877 //----------------------------------------------------------------
878
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000879 MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
880
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000881 // First, add edges to the terminator instruction of the basic block.
882 this->addCDEdges(bb->getTerminator(), target);
883
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000884 // Then add memory dep edges: store->load, load->store, and store->store.
885 // Call instructions are treated as both load and store.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000886 this->addMemEdges(memVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000887
888 // Then add edges between call instructions and CC set/use instructions
889 this->addCallCCEdges(memVec, bbMvec, target);
890
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000891 // Then add other edges for all instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000892 // Do this in machine code order and find all references to machine regs.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000893 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
894 addEdgesForInstruction(*bbMvec[i], regToRefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000895
896 // Since the code is no longer in SSA form, add output dep. edges
897 // between machine instructions that define the same Value, and anti-dep.
898 // edges from those to other machine instructions for the same VM instr.
899 // We assume that all machine instructions that define a value are
900 // generated from the VM instruction corresponding to that value.
901 //
902 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000903 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000904 const Instruction *instr = *II;
905 this->addNonSSAEdgesForValue(instr, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000906 }
907
908 // Then add edges for dependences on machine registers
909 this->addMachineRegEdges(regToRefVecMap, target);
910
911 // Finally, add edges from the dummy root and to dummy leaf
912 this->addDummyEdges();
913}
914
915
916//
917// class SchedGraphSet
918//
919
920/*ctor*/
921SchedGraphSet::SchedGraphSet(const Method* _method,
922 const TargetMachine& target) :
923 method(_method)
924{
925 buildGraphsForMethod(method, target);
926}
927
928
929/*dtor*/
930SchedGraphSet::~SchedGraphSet()
931{
932 // delete all the graphs
933 for (iterator I=begin(); I != end(); ++I)
934 delete (*I).second;
935}
936
937
938void
939SchedGraphSet::dump() const
940{
941 cout << "======== Sched graphs for method `"
942 << (method->hasName()? method->getName() : "???")
943 << "' ========" << endl << endl;
944
945 for (const_iterator I=begin(); I != end(); ++I)
946 (*I).second->dump();
947
948 cout << endl << "====== End graphs for method `"
949 << (method->hasName()? method->getName() : "")
950 << "' ========" << endl << endl;
951}
952
953
954void
955SchedGraphSet::buildGraphsForMethod(const Method *method,
956 const TargetMachine& target)
957{
958 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
959 {
960 SchedGraph* graph = new SchedGraph(*BI, target);
961 this->noteGraphForBlock(*BI, graph);
962 }
963}
964
965
966
967ostream&
968operator<<(ostream& os, const SchedGraphEdge& edge)
969{
970 os << "edge [" << edge.src->getNodeId() << "] -> ["
971 << edge.sink->getNodeId() << "] : ";
972
973 switch(edge.depType) {
974 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
975 case SchedGraphEdge::DefUseDep: os<< "Reg Value " << edge.val; break;
976 case SchedGraphEdge::MemoryDep: os<< "Mem Value " << edge.val; break;
977 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
978 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
979 default: assert(0); break;
980 }
981
982 os << " : delay = " << edge.minDelay << endl;
983
984 return os;
985}
986
987ostream&
988operator<<(ostream& os, const SchedGraphNode& node)
989{
990 printIndent(4, os);
991 os << "Node " << node.nodeId << " : "
992 << "latency = " << node.latency << endl;
993
994 printIndent(6, os);
995
996 if (node.getMachineInstr() == NULL)
997 os << "(Dummy node)" << endl;
998 else
999 {
1000 os << *node.getMachineInstr() << endl;
1001
1002 printIndent(6, os);
1003 os << node.inEdges.size() << " Incoming Edges:" << endl;
1004 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
1005 {
1006 printIndent(8, os);
1007 os << * node.inEdges[i];
1008 }
1009
1010 printIndent(6, os);
1011 os << node.outEdges.size() << " Outgoing Edges:" << endl;
1012 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
1013 {
1014 printIndent(8, os);
1015 os << * node.outEdges[i];
1016 }
1017 }
1018
1019 return os;
1020}