blob: 60913123ac7b395de24355bdf02875d1b8d6ffee [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
Vikram S. Advee64574c2001-11-08 05:20:23 +0000449// Add a dependence edge between every pair of machine load/store/call
450// instructions, where at least one is a store or a call.
451// Use latency 1 just to ensure that memory operations are ordered;
452// latency does not otherwise matter (true dependences enforce that).
453//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000454void
Vikram S. Advee64574c2001-11-08 05:20:23 +0000455SchedGraph::addMemEdges(const vector<SchedGraphNode*>& memNodeVec,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000456 const TargetMachine& target)
457{
458 const MachineInstrInfo& mii = target.getInstrInfo();
459
Vikram S. Advee64574c2001-11-08 05:20:23 +0000460 // Instructions in memNodeVec are in execution order within the basic block,
461 // so simply look at all pairs <memNodeVec[i], memNodeVec[j: j > i]>.
462 //
463 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000464 {
Vikram S. Advee64574c2001-11-08 05:20:23 +0000465 MachineOpCode fromOpCode = memNodeVec[im]->getOpCode();
466 int fromType = mii.isCall(fromOpCode)? SG_CALL_REF
467 : mii.isLoad(fromOpCode)? SG_LOAD_REF
468 : SG_STORE_REF;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000469 for (unsigned jm=im+1; jm < NM; jm++)
470 {
Vikram S. Advee64574c2001-11-08 05:20:23 +0000471 MachineOpCode toOpCode = memNodeVec[jm]->getOpCode();
472 int toType = mii.isCall(toOpCode)? SG_CALL_REF
473 : mii.isLoad(toOpCode)? SG_LOAD_REF
474 : SG_STORE_REF;
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000475
Vikram S. Advee64574c2001-11-08 05:20:23 +0000476 if (fromType != SG_LOAD_REF || toType != SG_LOAD_REF)
477 (void) new SchedGraphEdge(memNodeVec[im], memNodeVec[jm],
478 SchedGraphEdge::MemoryDep,
479 SG_DepOrderArray[fromType][toType], 1);
480 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000481 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000482}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000483
Vikram S. Advee64574c2001-11-08 05:20:23 +0000484// Add edges from/to CC reg instrs to/from call instrs.
485// Essentially this prevents anything that sets or uses a CC reg from being
486// reordered w.r.t. a call.
487// Use a latency of 0 because we only need to prevent out-of-order issue,
488// like with control dependences.
489//
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000490void
Vikram S. Advee64574c2001-11-08 05:20:23 +0000491SchedGraph::addCallCCEdges(const vector<SchedGraphNode*>& memNodeVec,
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000492 MachineCodeForBasicBlock& bbMvec,
493 const TargetMachine& target)
494{
495 const MachineInstrInfo& mii = target.getInstrInfo();
496 vector<SchedGraphNode*> callNodeVec;
497
Vikram S. Advee64574c2001-11-08 05:20:23 +0000498 // Find the call instruction nodes and put them in a vector.
499 for (unsigned im=0, NM=memNodeVec.size(); im < NM; im++)
500 if (mii.isCall(memNodeVec[im]->getOpCode()))
501 callNodeVec.push_back(memNodeVec[im]);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000502
Vikram S. Advee64574c2001-11-08 05:20:23 +0000503 // Now walk the entire basic block, looking for CC instructions *and*
504 // call instructions, and keep track of the order of the instructions.
505 // Use the call node vec to quickly find earlier and later call nodes
506 // relative to the current CC instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000507 //
508 int lastCallNodeIdx = -1;
509 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
510 if (mii.isCall(bbMvec[i]->getOpCode()))
511 {
512 ++lastCallNodeIdx;
513 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
514 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
515 break;
516 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
517 }
518 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
519 { // Add incoming/outgoing edges from/to preceding/later calls
520 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
521 int j=0;
522 for ( ; j <= lastCallNodeIdx; j++)
523 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
524 MachineCCRegsRID, 0);
525 for ( ; j < (int) callNodeVec.size(); j++)
526 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
527 MachineCCRegsRID, 0);
528 }
529}
530
531
532void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000533SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000534 const TargetMachine& target)
535{
536 assert(bbVec.size() == 1 && "Only handling a single basic block here");
537
538 // This assumes that such hardwired registers are never allocated
539 // to any LLVM value (since register allocation happens later), i.e.,
540 // any uses or defs of this register have been made explicit!
541 // Also assumes that two registers with different numbers are
542 // not aliased!
543 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000544 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000545 I != regToRefVecMap.end(); ++I)
546 {
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000547 int regNum = (*I).first;
548 RefVec& regRefVec = (*I).second;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000549
550 // regRefVec is ordered by control flow order in the basic block
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000551 for (unsigned i=0; i < regRefVec.size(); ++i)
552 {
553 SchedGraphNode* node = regRefVec[i].first;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000554 unsigned int opNum = regRefVec[i].second;
555 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
556
557 for (unsigned p=0; p < i; ++p)
558 {
559 SchedGraphNode* prevNode = regRefVec[p].first;
560 if (prevNode != node)
561 {
562 unsigned int prevOpNum = regRefVec[p].second;
563 bool prevIsDef =
564 prevNode->getMachineInstr()->operandIsDefined(prevOpNum);
565
566 if (isDef)
567 new SchedGraphEdge(prevNode, node, regNum,
568 (prevIsDef)? SchedGraphEdge::OutputDep
569 : SchedGraphEdge::AntiDep);
570 else if (prevIsDef)
571 new SchedGraphEdge(prevNode, node, regNum,
572 SchedGraphEdge::TrueDep);
573 }
574 }
575 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000576 }
577}
578
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000579
580void
581SchedGraph::addSSAEdge(SchedGraphNode* destNode,
582 const RefVec& defVec,
583 const Value* defValue,
584 const TargetMachine& target)
585{
586 for (RefVec::const_iterator I=defVec.begin(), E=defVec.end(); I != E; ++I)
587 (void) new SchedGraphEdge((*I).first, destNode, defValue);
588}
589
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000590
591void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000592SchedGraph::addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000593 const ValueToDefVecMap& valueToDefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000594 const TargetMachine& target)
595{
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000596 SchedGraphNode* node = this->getGraphNodeForInstr(&minstr);
597 if (node == NULL)
598 return;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000599
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000600 assert(node->getInstr() && "Should be no dummy nodes here!");
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000601 const Instruction* instr = node->getInstr();
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000602
603 // Add edges for all operands of the machine instruction.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000604 //
605 for (unsigned i=0, numOps=minstr.getNumOperands(); i < numOps; i++)
606 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000607 // ignore def operands here
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000608 if (minstr.operandIsDefined(i))
609 continue;
610
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000611 const MachineOperand& mop = minstr.getOperand(i);
612
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000613 switch(mop.getOperandType())
614 {
615 case MachineOperand::MO_VirtualRegister:
616 case MachineOperand::MO_CCRegister:
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000617 if (const Instruction* srcI =
618 dyn_cast_or_null<Instruction>(mop.getVRegValue()))
619 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000620 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
621 if (I != valueToDefVecMap.end())
622 addSSAEdge(node, (*I).second, mop.getVRegValue(), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000623 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000624 break;
625
626 case MachineOperand::MO_MachineRegister:
627 break;
628
629 case MachineOperand::MO_SignExtendedImmed:
630 case MachineOperand::MO_UnextendedImmed:
631 case MachineOperand::MO_PCRelativeDisp:
632 break; // nothing to do for immediate fields
633
634 default:
635 assert(0 && "Unknown machine operand type in SchedGraph builder");
636 break;
637 }
638 }
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000639
640 // Add edges for values implicitly used by the machine instruction.
641 // Examples include function arguments to a Call instructions or the return
642 // value of a Ret instruction.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000643 //
Vikram S. Adve8d0ffa52001-10-11 04:22:45 +0000644 for (unsigned i=0, N=minstr.getNumImplicitRefs(); i < N; ++i)
645 if (! minstr.implicitRefIsDefined(i))
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000646 if (const Instruction* srcI =
647 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
648 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000649 ValueToDefVecMap::const_iterator I = valueToDefVecMap.find(srcI);
650 if (I != valueToDefVecMap.end())
651 addSSAEdge(node, (*I).second, minstr.getImplicitRef(i), target);
Vikram S. Adve85b46d62001-10-17 23:53:16 +0000652 }
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000653}
654
655
656void
657SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
658 const TargetMachine& target)
659{
Chris Lattnerb00c5822001-10-02 03:41:24 +0000660 if (isa<PHINode>(instr))
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000661 return;
662
663 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
664 const MachineInstrInfo& mii = target.getInstrInfo();
665 RefVec refVec;
666
667 for (unsigned i=0, N=mvec.size(); i < N; i++)
668 for (int o=0, N = mii.getNumOperands(mvec[i]->getOpCode()); o < N; o++)
669 {
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000670 const MachineOperand& mop = mvec[i]->getOperand(o);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000671
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000672 if ((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
673 mop.getOperandType() == MachineOperand::MO_CCRegister)
674 && mop.getVRegValue() == (Value*) instr)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000675 {
676 // this operand is a definition or use of value `instr'
677 SchedGraphNode* node = this->getGraphNodeForInstr(mvec[i]);
678 assert(node && "No node for machine instruction in this BB?");
679 refVec.push_back(make_pair(node, o));
680 }
681 }
682
683 // refVec is ordered by control flow order of the machine instructions
684 for (unsigned i=0; i < refVec.size(); ++i)
685 {
686 SchedGraphNode* node = refVec[i].first;
687 unsigned int opNum = refVec[i].second;
688 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
689
690 if (isDef)
691 // add output and/or anti deps to this definition
692 for (unsigned p=0; p < i; ++p)
693 {
694 SchedGraphNode* prevNode = refVec[p].first;
695 if (prevNode != node)
696 {
697 bool prevIsDef = prevNode->getMachineInstr()->
698 operandIsDefined(refVec[p].second);
699 new SchedGraphEdge(prevNode, node, SchedGraphEdge::DefUseDep,
700 (prevIsDef)? SchedGraphEdge::OutputDep
701 : SchedGraphEdge::AntiDep);
702 }
703 }
704 }
705}
706
707
708void
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000709SchedGraph::findDefUseInfoAtInstr(const TargetMachine& target,
710 SchedGraphNode* node,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000711 vector<SchedGraphNode*>& memNodeVec,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000712 RegToRefVecMap& regToRefVecMap,
713 ValueToDefVecMap& valueToDefVecMap)
714{
715 const MachineInstrInfo& mii = target.getInstrInfo();
716
Vikram S. Advee64574c2001-11-08 05:20:23 +0000717
718 MachineOpCode opCode = node->getOpCode();
719 if (mii.isLoad(opCode) || mii.isStore(opCode) || mii.isCall(opCode))
720 memNodeVec.push_back(node);
721
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000722 // Collect the register references and value defs. for explicit operands
723 //
724 const MachineInstr& minstr = * node->getMachineInstr();
725 for (int i=0, numOps = (int) minstr.getNumOperands(); i < numOps; i++)
726 {
727 const MachineOperand& mop = minstr.getOperand(i);
728
729 // if this references a register other than the hardwired
730 // "zero" register, record the reference.
731 if (mop.getOperandType() == MachineOperand::MO_MachineRegister)
732 {
733 int regNum = mop.getMachineRegNum();
734 if (regNum != target.getRegInfo().getZeroRegNum())
Vikram S. Advee64574c2001-11-08 05:20:23 +0000735 regToRefVecMap[mop.getMachineRegNum()].push_back(make_pair(node,
736 i));
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000737 continue; // nothing more to do
738 }
739
740 // ignore all other non-def operands
741 if (! minstr.operandIsDefined(i))
742 continue;
743
744 // We must be defining a value.
745 assert((mop.getOperandType() == MachineOperand::MO_VirtualRegister ||
746 mop.getOperandType() == MachineOperand::MO_CCRegister)
747 && "Do not expect any other kind of operand to be defined!");
748
749 const Instruction* defInstr = cast<Instruction>(mop.getVRegValue());
750 valueToDefVecMap[defInstr].push_back(make_pair(node, i));
751 }
Vikram S. Advee64574c2001-11-08 05:20:23 +0000752
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000753 //
754 // Collect value defs. for implicit operands. The interface to extract
755 // them assumes they must be virtual registers!
756 //
757 for (int i=0, N = (int) minstr.getNumImplicitRefs(); i < N; ++i)
758 if (minstr.implicitRefIsDefined(i))
759 if (const Instruction* defInstr =
760 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
761 {
762 valueToDefVecMap[defInstr].push_back(make_pair(node, -i));
763 }
764}
765
766
767void
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000768SchedGraph::buildNodesforVMInstr(const TargetMachine& target,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000769 const Instruction* instr,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000770 vector<SchedGraphNode*>& memNodeVec,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000771 RegToRefVecMap& regToRefVecMap,
772 ValueToDefVecMap& valueToDefVecMap)
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000773{
774 const MachineInstrInfo& mii = target.getInstrInfo();
775 const MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
776 for (unsigned i=0; i < mvec.size(); i++)
777 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
778 {
779 SchedGraphNode* node = new SchedGraphNode(getNumNodes(),
780 instr, mvec[i], target);
781 this->noteGraphNodeForInstr(mvec[i], node);
Vikram S. Advee64574c2001-11-08 05:20:23 +0000782
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000783 // Remember all register references and value defs
Vikram S. Advee64574c2001-11-08 05:20:23 +0000784 findDefUseInfoAtInstr(target, node,
785 memNodeVec, regToRefVecMap, valueToDefVecMap);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000786 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000787}
788
789
790void
791SchedGraph::buildGraph(const TargetMachine& target)
792{
793 const MachineInstrInfo& mii = target.getInstrInfo();
794 const BasicBlock* bb = bbVec[0];
795
796 assert(bbVec.size() == 1 && "Only handling a single basic block here");
797
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000798 // Use this data structure to note all machine operands that compute
799 // ordinary LLVM values. These must be computed defs (i.e., instructions).
800 // Note that there may be multiple machine instructions that define
801 // each Value.
802 ValueToDefVecMap valueToDefVecMap;
803
Vikram S. Advee64574c2001-11-08 05:20:23 +0000804 // Use this data structure to note all memory instructions.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000805 // We use this to add memory dependence edges without a second full walk.
806 //
Vikram S. Advee64574c2001-11-08 05:20:23 +0000807 // vector<const Instruction*> memVec;
808 vector<SchedGraphNode*> memNodeVec;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000809
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000810 // Use this data structure to note any uses or definitions of
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000811 // machine registers so we can add edges for those later without
812 // extra passes over the nodes.
813 // The vector holds an ordered list of references to the machine reg,
814 // ordered according to control-flow order. This only works for a
815 // single basic block, hence the assertion. Each reference is identified
816 // by the pair: <node, operand-number>.
817 //
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000818 RegToRefVecMap regToRefVecMap;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000819
820 // Make a dummy root node. We'll add edges to the real roots later.
821 graphRoot = new SchedGraphNode(0, NULL, NULL, target);
822 graphLeaf = new SchedGraphNode(1, NULL, NULL, target);
823
824 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000825 // First add nodes for all the machine instructions in the basic block
826 // because this greatly simplifies identifying which edges to add.
827 // Do this one VM instruction at a time since the SchedGraphNode needs that.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000828 // Also, remember the load/store instructions to add memory deps later.
829 //----------------------------------------------------------------
830
831 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
832 {
833 const Instruction *instr = *II;
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000834
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000835 // Build graph nodes for this VM instruction and gather def/use info.
836 // Do these together in a single pass over all machine instructions.
837 buildNodesforVMInstr(target, instr,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000838 memNodeVec, regToRefVecMap, valueToDefVecMap);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000839 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000840
841 //----------------------------------------------------------------
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000842 // Now add edges for the following (all are incoming edges except (4)):
843 // (1) operands of the machine instruction, including hidden operands
844 // (2) machine register dependences
845 // (3) memory load/store dependences
846 // (3) other resource dependences for the machine instruction, if any
847 // (4) output dependences when multiple machine instructions define the
848 // same value; all must have been generated from a single VM instrn
849 // (5) control dependences to branch instructions generated for the
850 // terminator instruction of the BB. Because of delay slots and
851 // 2-way conditional branches, multiple CD edges are needed
852 // (see addCDEdges for details).
853 // Also, note any uses or defs of machine registers.
854 //
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000855 //----------------------------------------------------------------
856
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000857 MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
858
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000859 // First, add edges to the terminator instruction of the basic block.
860 this->addCDEdges(bb->getTerminator(), target);
861
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000862 // Then add memory dep edges: store->load, load->store, and store->store.
863 // Call instructions are treated as both load and store.
Vikram S. Advee64574c2001-11-08 05:20:23 +0000864 this->addMemEdges(memNodeVec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000865
866 // Then add edges between call instructions and CC set/use instructions
Vikram S. Advee64574c2001-11-08 05:20:23 +0000867 this->addCallCCEdges(memNodeVec, bbMvec, target);
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000868
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000869 // Then add incoming def-use (SSA) edges for each machine instruction.
Vikram S. Advea93bbac2001-10-28 21:43:33 +0000870 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000871 addEdgesForInstruction(*bbMvec[i], valueToDefVecMap, target);
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000872
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000873 // Then add non-SSA edges for all VM instructions in the block.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000874 // We assume that all machine instructions that define a value are
875 // generated from the VM instruction corresponding to that value.
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000876 // TODO: This could probably be done much more efficiently.
Vikram S. Adve5316f8f2001-09-30 23:36:58 +0000877 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000878 this->addNonSSAEdgesForValue(*II, target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000879
880 // Then add edges for dependences on machine registers
881 this->addMachineRegEdges(regToRefVecMap, target);
882
883 // Finally, add edges from the dummy root and to dummy leaf
884 this->addDummyEdges();
885}
886
887
888//
889// class SchedGraphSet
890//
891
892/*ctor*/
893SchedGraphSet::SchedGraphSet(const Method* _method,
894 const TargetMachine& target) :
895 method(_method)
896{
897 buildGraphsForMethod(method, target);
898}
899
900
901/*dtor*/
902SchedGraphSet::~SchedGraphSet()
903{
904 // delete all the graphs
905 for (iterator I=begin(); I != end(); ++I)
906 delete (*I).second;
907}
908
909
910void
911SchedGraphSet::dump() const
912{
913 cout << "======== Sched graphs for method `"
914 << (method->hasName()? method->getName() : "???")
915 << "' ========" << endl << endl;
916
917 for (const_iterator I=begin(); I != end(); ++I)
918 (*I).second->dump();
919
920 cout << endl << "====== End graphs for method `"
921 << (method->hasName()? method->getName() : "")
922 << "' ========" << endl << endl;
923}
924
925
926void
927SchedGraphSet::buildGraphsForMethod(const Method *method,
928 const TargetMachine& target)
929{
930 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
931 {
932 SchedGraph* graph = new SchedGraph(*BI, target);
933 this->noteGraphForBlock(*BI, graph);
934 }
935}
936
937
938
939ostream&
940operator<<(ostream& os, const SchedGraphEdge& edge)
941{
942 os << "edge [" << edge.src->getNodeId() << "] -> ["
943 << edge.sink->getNodeId() << "] : ";
944
945 switch(edge.depType) {
946 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
947 case SchedGraphEdge::DefUseDep: os<< "Reg Value " << edge.val; break;
948 case SchedGraphEdge::MemoryDep: os<< "Mem Value " << edge.val; break;
949 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
950 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
951 default: assert(0); break;
952 }
953
954 os << " : delay = " << edge.minDelay << endl;
955
956 return os;
957}
958
959ostream&
960operator<<(ostream& os, const SchedGraphNode& node)
961{
962 printIndent(4, os);
963 os << "Node " << node.nodeId << " : "
964 << "latency = " << node.latency << endl;
965
966 printIndent(6, os);
967
968 if (node.getMachineInstr() == NULL)
969 os << "(Dummy node)" << endl;
970 else
971 {
972 os << *node.getMachineInstr() << endl;
973
974 printIndent(6, os);
975 os << node.inEdges.size() << " Incoming Edges:" << endl;
976 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
977 {
978 printIndent(8, os);
979 os << * node.inEdges[i];
980 }
981
982 printIndent(6, os);
983 os << node.outEdges.size() << " Outgoing Edges:" << endl;
984 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
985 {
986 printIndent(8, os);
987 os << * node.outEdges[i];
988 }
989 }
990
991 return os;
992}