blob: 27014ad21d37eae494ec239aad3bb161d15b55c6 [file] [log] [blame]
Vikram S. Adve78ef1392001-08-28 23:06:02 +00001/* -*-C++-*-
2 ****************************************************************************
3 * File:
4 * SchedGraph.h
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 * Strategy:
12 * This graph tries to leverage the SSA graph as much as possible,
13 * but captures the extra dependences through a common interface.
14 *
15 * History:
16 * 7/20/01 - Vikram Adve - Created
17 ***************************************************************************/
18
19#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
20#define LLVM_CODEGEN_SCHEDGRAPH_H
21
Vikram S. Adve78ef1392001-08-28 23:06:02 +000022#include "llvm/Support/NonCopyable.h"
23#include "llvm/Support/HashExtras.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000024#include "llvm/Support/GraphTraits.h"
Chris Lattnercffebdc2001-09-07 21:07:10 +000025#include <hash_map>
Vikram S. Adve78ef1392001-08-28 23:06:02 +000026
27class Value;
28class Instruction;
Chris Lattner3ff43872001-09-28 22:56:31 +000029class TerminatorInst;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000030class BasicBlock;
31class Method;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000032class TargetMachine;
33class SchedGraphEdge;
34class SchedGraphNode;
35class SchedGraph;
Vikram S. Adve4a87b382001-09-30 23:37:26 +000036class RegToRefVecMap;
Chris Lattnerc0c77082001-09-14 17:55:51 +000037class MachineInstr;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000038class MachineCodeForBasicBlock;
39
Vikram S. Adve78ef1392001-08-28 23:06:02 +000040
41/******************** Exported Data Types and Constants ********************/
42
43typedef int ResourceId;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000044const ResourceId InvalidRID = -1;
45const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
46const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
47const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
Vikram S. Adve78ef1392001-08-28 23:06:02 +000048
49
50//*********************** Public Class Declarations ************************/
51
52class SchedGraphEdge: public NonCopyable {
53public:
54 enum SchedGraphEdgeDepType {
55 CtrlDep, MemoryDep, DefUseDep, MachineRegister, MachineResource
56 };
57 enum DataDepOrderType {
Vikram S. Adved0d79c02001-10-28 21:45:02 +000058 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
Vikram S. Adve78ef1392001-08-28 23:06:02 +000059 };
60
61protected:
62 SchedGraphNode* src;
63 SchedGraphNode* sink;
64 SchedGraphEdgeDepType depType;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000065 unsigned int depOrderType;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000066 int minDelay; // cached latency (assumes fixed target arch)
67
68 union {
Vikram S. Adve4a87b382001-09-30 23:37:26 +000069 const Value* val;
70 int machineRegNum;
71 ResourceId resourceId;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000072 };
73
74public:
75 // For all constructors, if minDelay is unspecified, minDelay is
76 // set to _src->getLatency().
77 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
78 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
79 SchedGraphNode* _sink,
80 SchedGraphEdgeDepType _depType,
Vikram S. Adved0d79c02001-10-28 21:45:02 +000081 unsigned int _depOrderType =TrueDep,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000082 int _minDelay = -1);
83
84 // constructor for explicit def-use or memory def-use edge
85 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
86 SchedGraphNode* _sink,
Vikram S. Adve4a87b382001-09-30 23:37:26 +000087 const Value* _val,
Vikram S. Adved0d79c02001-10-28 21:45:02 +000088 unsigned int _depOrderType =TrueDep,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000089 int _minDelay = -1);
90
91 // constructor for machine register dependence
92 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
93 SchedGraphNode* _sink,
94 unsigned int _regNum,
Vikram S. Adved0d79c02001-10-28 21:45:02 +000095 unsigned int _depOrderType =TrueDep,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000096 int _minDelay = -1);
97
98 // constructor for any other machine resource dependences.
99 // DataDepOrderType is always NonDataDep. It it not an argument to
100 // avoid overloading ambiguity with previous constructor.
101 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
102 SchedGraphNode* _sink,
103 ResourceId _resourceId,
104 int _minDelay = -1);
105
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000106 /*dtor*/ ~SchedGraphEdge();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000107
108 SchedGraphNode* getSrc () const { return src; }
109 SchedGraphNode* getSink () const { return sink; }
110 int getMinDelay () const { return minDelay; }
111 SchedGraphEdgeDepType getDepType () const { return depType; }
112
113 const Value* getValue () const {
114 assert(depType == DefUseDep || depType == MemoryDep); return val;
115 }
116 int getMachineReg () const {
117 assert(depType == MachineRegister); return machineRegNum;
118 }
119 int getResourceId () const {
120 assert(depType == MachineResource); return resourceId;
121 }
122
123public:
124 //
125 // Debugging support
126 //
127 friend ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
128
Chris Lattnercffebdc2001-09-07 21:07:10 +0000129 void dump (int indent=0) const;
130
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000131private:
132 // disable default ctor
133 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
134};
135
136
137
138class SchedGraphNode: public NonCopyable {
139private:
140 unsigned int nodeId;
141 const Instruction* instr;
142 const MachineInstr* minstr;
143 vector<SchedGraphEdge*> inEdges;
144 vector<SchedGraphEdge*> outEdges;
145 int latency;
146
147public:
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000148 typedef vector<SchedGraphEdge*>:: iterator iterator;
149 typedef vector<SchedGraphEdge*>::const_iterator const_iterator;
150 typedef vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
151 typedef vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000152
153public:
154 //
155 // Accessor methods
156 //
157 unsigned int getNodeId () const { return nodeId; }
158 const Instruction* getInstr () const { return instr; }
159 const MachineInstr* getMachineInstr () const { return minstr; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000160 int getLatency () const { return latency; }
161 unsigned int getNumInEdges () const { return inEdges.size(); }
162 unsigned int getNumOutEdges () const { return outEdges.size(); }
163 bool isDummyNode () const { return (minstr == NULL); }
164
165 //
166 // Iterators
167 //
168 iterator beginInEdges () { return inEdges.begin(); }
169 iterator endInEdges () { return inEdges.end(); }
170 iterator beginOutEdges () { return outEdges.begin(); }
171 iterator endOutEdges () { return outEdges.end(); }
172
173 const_iterator beginInEdges () const { return inEdges.begin(); }
174 const_iterator endInEdges () const { return inEdges.end(); }
175 const_iterator beginOutEdges () const { return outEdges.begin(); }
176 const_iterator endOutEdges () const { return outEdges.end(); }
177
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000178public:
179 //
180 // Debugging support
181 //
182 friend ostream& operator<<(ostream& os, const SchedGraphNode& node);
183
Chris Lattnercffebdc2001-09-07 21:07:10 +0000184 void dump (int indent=0) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000185
186private:
187 friend class SchedGraph; // give access for ctor and dtor
188 friend class SchedGraphEdge; // give access for adding edges
189
190 void addInEdge (SchedGraphEdge* edge);
191 void addOutEdge (SchedGraphEdge* edge);
192
193 void removeInEdge (const SchedGraphEdge* edge);
194 void removeOutEdge (const SchedGraphEdge* edge);
195
196 // disable default constructor and provide a ctor for single-block graphs
197 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
198 /*ctor*/ SchedGraphNode (unsigned int _nodeId,
199 const Instruction* _instr,
200 const MachineInstr* _minstr,
201 const TargetMachine& _target);
202 /*dtor*/ ~SchedGraphNode ();
203};
204
205
206
207class SchedGraph :
208 public NonCopyable,
209 private hash_map<const MachineInstr*, SchedGraphNode*>
210{
211private:
212 vector<const BasicBlock*> bbVec; // basic blocks included in the graph
213 SchedGraphNode* graphRoot; // the root and leaf are not inserted
214 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
215
216public:
217 typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator;
218 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
219
220public:
221 //
222 // Accessor methods
223 //
224 const vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
225 const unsigned int getNumNodes() const { return size()+2; }
226 SchedGraphNode* getRoot() const { return graphRoot; }
227 SchedGraphNode* getLeaf() const { return graphLeaf; }
228
229 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
230 const_iterator onePair = this->find(minstr);
231 return (onePair != this->end())? (*onePair).second : NULL;
232 }
233
234 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000235 // Delete nodes or edges from the graph.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000236 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000237 void eraseNode (SchedGraphNode* node);
238
239 void eraseIncomingEdges (SchedGraphNode* node,
240 bool addDummyEdges = true);
241
242 void eraseOutgoingEdges (SchedGraphNode* node,
243 bool addDummyEdges = true);
244
245 void eraseIncidentEdges (SchedGraphNode* node,
246 bool addDummyEdges = true);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000247
248 //
249 // Unordered iterators.
250 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
251 //
252 iterator begin() {
253 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
254 }
255 iterator end() {
256 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
257 }
258 const_iterator begin() const {
259 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
260 }
261 const_iterator end() const {
262 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
263 }
264
265 //
266 // Ordered iterators.
267 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
268 //
269 // void postord_init();
270 // postorder_iterator postord_begin();
271 // postorder_iterator postord_end();
272 // const_postorder_iterator postord_begin() const;
273 // const_postorder_iterator postord_end() const;
274
275 //
276 // Debugging support
277 //
278 void dump () const;
279
280private:
281 friend class SchedGraphSet; // give access to ctor
282
283 // disable default constructor and provide a ctor for single-block graphs
284 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
285 /*ctor*/ SchedGraph (const BasicBlock* bb,
286 const TargetMachine& target);
287 /*dtor*/ ~SchedGraph ();
288
289 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
290 SchedGraphNode* node)
291 {
292 assert((*this)[minstr] == NULL);
293 (*this)[minstr] = node;
294 }
295
296 //
297 // Graph builder
298 //
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000299 void buildGraph (const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000300
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000301 void buildNodesforVMInstr (const TargetMachine& target,
302 const Instruction* instr);
303
304 void addEdgesForInstruction (const MachineInstr& minstr,
305 RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000306 const TargetMachine& target);
307
308 void addCDEdges (const TerminatorInst* term,
309 const TargetMachine& target);
310
311 void addMemEdges (const vector<const Instruction*>& memVec,
312 const TargetMachine& target);
313
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000314 void addCallCCEdges (const vector<const Instruction*>& memVec,
315 MachineCodeForBasicBlock& bbMvec,
316 const TargetMachine& target);
317
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000318 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000319 const TargetMachine& target);
320
321 void addSSAEdge (SchedGraphNode* node,
Vikram S. Advef43e3362001-10-17 23:55:16 +0000322 const Instruction* defVMInstr,
323 const Value* defValue,
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000324 const TargetMachine& target);
325
326 void addNonSSAEdgesForValue (const Instruction* instr,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000327 const TargetMachine& target);
328
329 void addDummyEdges ();
330};
331
332
333class SchedGraphSet :
334 public NonCopyable,
335 private hash_map<const BasicBlock*, SchedGraph*>
336{
337private:
338 const Method* method;
339
340public:
341 typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator;
342 typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator;
343
344public:
345 /*ctor*/ SchedGraphSet (const Method* _method,
346 const TargetMachine& target);
347 /*dtor*/ ~SchedGraphSet ();
348
349 //
350 // Accessors
351 //
352 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
353 const_iterator onePair = this->find(bb);
354 return (onePair != this->end())? (*onePair).second : NULL;
355 }
356
357 //
358 // Iterators
359 //
360 iterator begin() {
361 return hash_map<const BasicBlock*, SchedGraph*>::begin();
362 }
363 iterator end() {
364 return hash_map<const BasicBlock*, SchedGraph*>::end();
365 }
366 const_iterator begin() const {
367 return hash_map<const BasicBlock*, SchedGraph*>::begin();
368 }
369 const_iterator end() const {
370 return hash_map<const BasicBlock*, SchedGraph*>::end();
371 }
372
373 //
374 // Debugging support
375 //
376 void dump () const;
377
378private:
379 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
380 assert((*this)[bb] == NULL);
381 (*this)[bb] = graph;
382 }
383
384 //
385 // Graph builder
386 //
387 void buildGraphsForMethod (const Method *method,
388 const TargetMachine& target);
389};
390
391
392//********************** Sched Graph Iterators *****************************/
393
394// Ok to make it a template because it shd get instantiated at most twice:
395// for <SchedGraphNode, SchedGraphNode::iterator> and
396// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
397//
398template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattnercffebdc2001-09-07 21:07:10 +0000399class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000400protected:
401 _EdgeIter oi;
402public:
Chris Lattnercffebdc2001-09-07 21:07:10 +0000403 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000404
Chris Lattnercffebdc2001-09-07 21:07:10 +0000405 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000406
407 inline bool operator==(const _Self& x) const { return oi == x.oi; }
408 inline bool operator!=(const _Self& x) const { return !operator==(x); }
409
410 // operator*() differs for pred or succ iterator
Chris Lattnercffebdc2001-09-07 21:07:10 +0000411 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000412 inline _NodeType* operator->() const { return operator*(); }
413
414 inline _EdgeType* getEdge() const { return *(oi); }
415
Chris Lattnercffebdc2001-09-07 21:07:10 +0000416 inline _Self &operator++() { ++oi; return *this; } // Preincrement
417 inline _Self operator++(int) { // Postincrement
418 _Self tmp(*this); ++*this; return tmp;
419 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000420
Chris Lattnercffebdc2001-09-07 21:07:10 +0000421 inline _Self &operator--() { --oi; return *this; } // Predecrement
422 inline _Self operator--(int) { // Postdecrement
423 _Self tmp = *this; --*this; return tmp;
424 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000425};
426
427template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattnercffebdc2001-09-07 21:07:10 +0000428class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
429protected:
430 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000431public:
Chris Lattnercffebdc2001-09-07 21:07:10 +0000432 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000433
Chris Lattnercffebdc2001-09-07 21:07:10 +0000434 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000435
Chris Lattnercffebdc2001-09-07 21:07:10 +0000436 inline bool operator==(const _Self& x) const { return oi == x.oi; }
437 inline bool operator!=(const _Self& x) const { return !operator==(x); }
438
439 inline _NodeType* operator*() const { return (*oi)->getSink(); }
440 inline _NodeType* operator->() const { return operator*(); }
441
442 inline _EdgeType* getEdge() const { return *(oi); }
443
444 inline _Self &operator++() { ++oi; return *this; } // Preincrement
445 inline _Self operator++(int) { // Postincrement
446 _Self tmp(*this); ++*this; return tmp;
447 }
448
449 inline _Self &operator--() { --oi; return *this; } // Predecrement
450 inline _Self operator--(int) { // Postdecrement
451 _Self tmp = *this; --*this; return tmp;
452 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000453};
454
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000455//
456// sg_pred_iterator
457// sg_pred_const_iterator
458//
459typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
460 sg_pred_iterator;
461typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
462 sg_pred_const_iterator;
463
464inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
465 return sg_pred_iterator(N->beginInEdges());
466}
467inline sg_pred_iterator pred_end( SchedGraphNode *N) {
468 return sg_pred_iterator(N->endInEdges());
469}
470inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
471 return sg_pred_const_iterator(N->beginInEdges());
472}
473inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
474 return sg_pred_const_iterator(N->endInEdges());
475}
476
477
478//
479// sg_succ_iterator
480// sg_succ_const_iterator
481//
482typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
483 sg_succ_iterator;
484typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
485 sg_succ_const_iterator;
486
487inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
488 return sg_succ_iterator(N->beginOutEdges());
489}
490inline sg_succ_iterator succ_end( SchedGraphNode *N) {
491 return sg_succ_iterator(N->endOutEdges());
492}
493inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
494 return sg_succ_const_iterator(N->beginOutEdges());
495}
496inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
497 return sg_succ_const_iterator(N->endOutEdges());
498}
499
Chris Lattner3ff43872001-09-28 22:56:31 +0000500// Provide specializations of GraphTraits to be able to use graph iterators on
501// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000502//
Chris Lattner3ff43872001-09-28 22:56:31 +0000503template <> struct GraphTraits<SchedGraph*> {
504 typedef SchedGraphNode NodeType;
505 typedef sg_succ_iterator ChildIteratorType;
506
507 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
508 static inline ChildIteratorType child_begin(NodeType *N) {
509 return succ_begin(N);
510 }
511 static inline ChildIteratorType child_end(NodeType *N) {
512 return succ_end(N);
513 }
514};
515
516template <> struct GraphTraits<const SchedGraph*> {
517 typedef const SchedGraphNode NodeType;
518 typedef sg_succ_const_iterator ChildIteratorType;
519
520 static inline NodeType *getEntryNode(const SchedGraph *SG) {
521 return SG->getRoot();
522 }
523 static inline ChildIteratorType child_begin(NodeType *N) {
524 return succ_begin(N);
525 }
526 static inline ChildIteratorType child_end(NodeType *N) {
527 return succ_end(N);
528 }
529};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000530
531
532//************************ External Functions *****************************/
533
534
535ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
536
537ostream& operator<<(ostream& os, const SchedGraphNode& node);
538
539
540/***************************************************************************/
541
542#endif