2 ****************************************************************************
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).
12 * This graph tries to leverage the SSA graph as much as possible,
13 * but captures the extra dependences through a common interface.
16 * 7/20/01 - Vikram Adve - Created
17 ***************************************************************************/
19 #ifndef LLVM_CODEGEN_SCHEDGRAPH_H
20 #define LLVM_CODEGEN_SCHEDGRAPH_H
22 #include "llvm/Support/NonCopyable.h"
23 #include "llvm/Support/HashExtras.h"
24 #include "llvm/Support/GraphTraits.h"
38 class MachineCodeForBasicBlock;
41 /******************** Exported Data Types and Constants ********************/
43 typedef int ResourceId;
44 const ResourceId InvalidRID = -1;
45 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
46 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
47 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
50 //*********************** Public Class Declarations ************************/
52 class SchedGraphEdge: public NonCopyable {
54 enum SchedGraphEdgeDepType {
55 CtrlDep, MemoryDep, DefUseDep, MachineRegister, MachineResource
57 enum DataDepOrderType {
58 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
64 SchedGraphEdgeDepType depType;
65 unsigned int depOrderType;
66 int minDelay; // cached latency (assumes fixed target arch)
71 ResourceId resourceId;
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,
81 unsigned int _depOrderType =TrueDep,
84 // constructor for explicit def-use or memory def-use edge
85 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
86 SchedGraphNode* _sink,
88 unsigned int _depOrderType =TrueDep,
91 // constructor for machine register dependence
92 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
93 SchedGraphNode* _sink,
95 unsigned int _depOrderType =TrueDep,
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,
106 /*dtor*/ ~SchedGraphEdge();
108 SchedGraphNode* getSrc () const { return src; }
109 SchedGraphNode* getSink () const { return sink; }
110 int getMinDelay () const { return minDelay; }
111 SchedGraphEdgeDepType getDepType () const { return depType; }
113 const Value* getValue () const {
114 assert(depType == DefUseDep || depType == MemoryDep); return val;
116 int getMachineReg () const {
117 assert(depType == MachineRegister); return machineRegNum;
119 int getResourceId () const {
120 assert(depType == MachineResource); return resourceId;
127 friend ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
129 void dump (int indent=0) const;
132 // disable default ctor
133 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
138 class SchedGraphNode: public NonCopyable {
141 const Instruction* instr;
142 const MachineInstr* minstr;
143 vector<SchedGraphEdge*> inEdges;
144 vector<SchedGraphEdge*> outEdges;
148 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;
157 unsigned int getNodeId () const { return nodeId; }
158 const Instruction* getInstr () const { return instr; }
159 const MachineInstr* getMachineInstr () const { return minstr; }
160 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); }
168 iterator beginInEdges () { return inEdges.begin(); }
169 iterator endInEdges () { return inEdges.end(); }
170 iterator beginOutEdges () { return outEdges.begin(); }
171 iterator endOutEdges () { return outEdges.end(); }
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(); }
182 friend ostream& operator<<(ostream& os, const SchedGraphNode& node);
184 void dump (int indent=0) const;
187 friend class SchedGraph; // give access for ctor and dtor
188 friend class SchedGraphEdge; // give access for adding edges
190 void addInEdge (SchedGraphEdge* edge);
191 void addOutEdge (SchedGraphEdge* edge);
193 void removeInEdge (const SchedGraphEdge* edge);
194 void removeOutEdge (const SchedGraphEdge* edge);
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 ();
209 private hash_map<const MachineInstr*, SchedGraphNode*>
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())
217 typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator;
218 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
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; }
229 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
230 const_iterator onePair = this->find(minstr);
231 return (onePair != this->end())? (*onePair).second : NULL;
235 // Delete nodes or edges from the graph.
237 void eraseNode (SchedGraphNode* node);
239 void eraseIncomingEdges (SchedGraphNode* node,
240 bool addDummyEdges = true);
242 void eraseOutgoingEdges (SchedGraphNode* node,
243 bool addDummyEdges = true);
245 void eraseIncidentEdges (SchedGraphNode* node,
246 bool addDummyEdges = true);
249 // Unordered iterators.
250 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
253 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
256 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
258 const_iterator begin() const {
259 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
261 const_iterator end() const {
262 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
266 // Ordered iterators.
267 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
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;
281 friend class SchedGraphSet; // give access to ctor
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 ();
289 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
290 SchedGraphNode* node)
292 assert((*this)[minstr] == NULL);
293 (*this)[minstr] = node;
299 void buildGraph (const TargetMachine& target);
301 void buildNodesforVMInstr (const TargetMachine& target,
302 const Instruction* instr);
304 void addEdgesForInstruction (const MachineInstr& minstr,
305 RegToRefVecMap& regToRefVecMap,
306 const TargetMachine& target);
308 void addCDEdges (const TerminatorInst* term,
309 const TargetMachine& target);
311 void addMemEdges (const vector<const Instruction*>& memVec,
312 const TargetMachine& target);
314 void addCallCCEdges (const vector<const Instruction*>& memVec,
315 MachineCodeForBasicBlock& bbMvec,
316 const TargetMachine& target);
318 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
319 const TargetMachine& target);
321 void addSSAEdge (SchedGraphNode* node,
322 const Instruction* defVMInstr,
323 const Value* defValue,
324 const TargetMachine& target);
326 void addNonSSAEdgesForValue (const Instruction* instr,
327 const TargetMachine& target);
329 void addDummyEdges ();
333 class SchedGraphSet :
335 private hash_map<const BasicBlock*, SchedGraph*>
338 const Method* method;
341 typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator;
342 typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator;
345 /*ctor*/ SchedGraphSet (const Method* _method,
346 const TargetMachine& target);
347 /*dtor*/ ~SchedGraphSet ();
352 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
353 const_iterator onePair = this->find(bb);
354 return (onePair != this->end())? (*onePair).second : NULL;
361 return hash_map<const BasicBlock*, SchedGraph*>::begin();
364 return hash_map<const BasicBlock*, SchedGraph*>::end();
366 const_iterator begin() const {
367 return hash_map<const BasicBlock*, SchedGraph*>::begin();
369 const_iterator end() const {
370 return hash_map<const BasicBlock*, SchedGraph*>::end();
379 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
380 assert((*this)[bb] == NULL);
387 void buildGraphsForMethod (const Method *method,
388 const TargetMachine& target);
392 //********************** Sched Graph Iterators *****************************/
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>.
398 template <class _NodeType, class _EdgeType, class _EdgeIter>
399 class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
403 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
405 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
407 inline bool operator==(const _Self& x) const { return oi == x.oi; }
408 inline bool operator!=(const _Self& x) const { return !operator==(x); }
410 // operator*() differs for pred or succ iterator
411 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
412 inline _NodeType* operator->() const { return operator*(); }
414 inline _EdgeType* getEdge() const { return *(oi); }
416 inline _Self &operator++() { ++oi; return *this; } // Preincrement
417 inline _Self operator++(int) { // Postincrement
418 _Self tmp(*this); ++*this; return tmp;
421 inline _Self &operator--() { --oi; return *this; } // Predecrement
422 inline _Self operator--(int) { // Postdecrement
423 _Self tmp = *this; --*this; return tmp;
427 template <class _NodeType, class _EdgeType, class _EdgeIter>
428 class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
432 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
434 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
436 inline bool operator==(const _Self& x) const { return oi == x.oi; }
437 inline bool operator!=(const _Self& x) const { return !operator==(x); }
439 inline _NodeType* operator*() const { return (*oi)->getSink(); }
440 inline _NodeType* operator->() const { return operator*(); }
442 inline _EdgeType* getEdge() const { return *(oi); }
444 inline _Self &operator++() { ++oi; return *this; } // Preincrement
445 inline _Self operator++(int) { // Postincrement
446 _Self tmp(*this); ++*this; return tmp;
449 inline _Self &operator--() { --oi; return *this; } // Predecrement
450 inline _Self operator--(int) { // Postdecrement
451 _Self tmp = *this; --*this; return tmp;
457 // sg_pred_const_iterator
459 typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
461 typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
462 sg_pred_const_iterator;
464 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
465 return sg_pred_iterator(N->beginInEdges());
467 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
468 return sg_pred_iterator(N->endInEdges());
470 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
471 return sg_pred_const_iterator(N->beginInEdges());
473 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
474 return sg_pred_const_iterator(N->endInEdges());
480 // sg_succ_const_iterator
482 typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
484 typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
485 sg_succ_const_iterator;
487 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
488 return sg_succ_iterator(N->beginOutEdges());
490 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
491 return sg_succ_iterator(N->endOutEdges());
493 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
494 return sg_succ_const_iterator(N->beginOutEdges());
496 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
497 return sg_succ_const_iterator(N->endOutEdges());
500 // Provide specializations of GraphTraits to be able to use graph iterators on
501 // the scheduling graph!
503 template <> struct GraphTraits<SchedGraph*> {
504 typedef SchedGraphNode NodeType;
505 typedef sg_succ_iterator ChildIteratorType;
507 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
508 static inline ChildIteratorType child_begin(NodeType *N) {
509 return succ_begin(N);
511 static inline ChildIteratorType child_end(NodeType *N) {
516 template <> struct GraphTraits<const SchedGraph*> {
517 typedef const SchedGraphNode NodeType;
518 typedef sg_succ_const_iterator ChildIteratorType;
520 static inline NodeType *getEntryNode(const SchedGraph *SG) {
521 return SG->getRoot();
523 static inline ChildIteratorType child_begin(NodeType *N) {
524 return succ_begin(N);
526 static inline ChildIteratorType child_end(NodeType *N) {
532 //************************ External Functions *****************************/
535 ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
537 ostream& operator<<(ostream& os, const SchedGraphNode& node);
540 /***************************************************************************/