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/CFGdecls.h" // just for graph iterators
23 #include "llvm/Support/NonCopyable.h"
24 #include "llvm/Support/HashExtras.h"
35 class NodeToRegRefMap;
38 /******************** Exported Data Types and Constants ********************/
40 typedef int ResourceId;
41 const ResourceId InvalidResourceId = -1;
44 //*********************** Public Class Declarations ************************/
46 class SchedGraphEdge: public NonCopyable {
48 enum SchedGraphEdgeDepType {
49 CtrlDep, MemoryDep, DefUseDep, MachineRegister, MachineResource
51 enum DataDepOrderType {
52 TrueDep, AntiDep, OutputDep, NonDataDep
58 SchedGraphEdgeDepType depType;
59 DataDepOrderType depOrderType;
60 int minDelay; // cached latency (assumes fixed target arch)
65 ResourceId resourceId;
69 // For all constructors, if minDelay is unspecified, minDelay is
70 // set to _src->getLatency().
71 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
72 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
73 SchedGraphNode* _sink,
74 SchedGraphEdgeDepType _depType,
75 DataDepOrderType _depOrderType =TrueDep,
78 // constructor for explicit def-use or memory def-use edge
79 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
80 SchedGraphNode* _sink,
82 DataDepOrderType _depOrderType =TrueDep,
85 // constructor for machine register dependence
86 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
87 SchedGraphNode* _sink,
89 DataDepOrderType _depOrderType =TrueDep,
92 // constructor for any other machine resource dependences.
93 // DataDepOrderType is always NonDataDep. It it not an argument to
94 // avoid overloading ambiguity with previous constructor.
95 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
96 SchedGraphNode* _sink,
97 ResourceId _resourceId,
100 /*dtor*/ ~SchedGraphEdge() {}
102 SchedGraphNode* getSrc () const { return src; }
103 SchedGraphNode* getSink () const { return sink; }
104 int getMinDelay () const { return minDelay; }
105 SchedGraphEdgeDepType getDepType () const { return depType; }
107 const Value* getValue () const {
108 assert(depType == DefUseDep || depType == MemoryDep); return val;
110 int getMachineReg () const {
111 assert(depType == MachineRegister); return machineRegNum;
113 int getResourceId () const {
114 assert(depType == MachineResource); return resourceId;
121 friend ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
123 void dump (int indent=0) const;
126 // disable default ctor
127 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
132 class SchedGraphNode: public NonCopyable {
135 const Instruction* instr;
136 const MachineInstr* minstr;
137 vector<SchedGraphEdge*> inEdges;
138 vector<SchedGraphEdge*> outEdges;
142 typedef vector<SchedGraphEdge*>::iterator iterator;
143 typedef vector<SchedGraphEdge*>::const_iterator const_iterator;
149 unsigned int getNodeId () const { return nodeId; }
150 const Instruction* getInstr () const { return instr; }
151 const MachineInstr* getMachineInstr () const { return minstr; }
152 int getLatency () const { return latency; }
153 unsigned int getNumInEdges () const { return inEdges.size(); }
154 unsigned int getNumOutEdges () const { return outEdges.size(); }
155 bool isDummyNode () const { return (minstr == NULL); }
160 iterator beginInEdges () { return inEdges.begin(); }
161 iterator endInEdges () { return inEdges.end(); }
162 iterator beginOutEdges () { return outEdges.begin(); }
163 iterator endOutEdges () { return outEdges.end(); }
165 const_iterator beginInEdges () const { return inEdges.begin(); }
166 const_iterator endInEdges () const { return inEdges.end(); }
167 const_iterator beginOutEdges () const { return outEdges.begin(); }
168 const_iterator endOutEdges () const { return outEdges.end(); }
171 // Limited modifier methods
173 void eraseAllEdges ();
179 friend ostream& operator<<(ostream& os, const SchedGraphNode& node);
181 void dump (int indent=0) const;
184 friend class SchedGraph; // give access for ctor and dtor
185 friend class SchedGraphEdge; // give access for adding edges
187 void addInEdge (SchedGraphEdge* edge);
188 void addOutEdge (SchedGraphEdge* edge);
190 void removeInEdge (const SchedGraphEdge* edge);
191 void removeOutEdge (const SchedGraphEdge* edge);
193 // disable default constructor and provide a ctor for single-block graphs
194 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
195 /*ctor*/ SchedGraphNode (unsigned int _nodeId,
196 const Instruction* _instr,
197 const MachineInstr* _minstr,
198 const TargetMachine& _target);
199 /*dtor*/ ~SchedGraphNode ();
206 private hash_map<const MachineInstr*, SchedGraphNode*>
209 vector<const BasicBlock*> bbVec; // basic blocks included in the graph
210 SchedGraphNode* graphRoot; // the root and leaf are not inserted
211 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
214 typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator;
215 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
221 const vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
222 const unsigned int getNumNodes() const { return size()+2; }
223 SchedGraphNode* getRoot() const { return graphRoot; }
224 SchedGraphNode* getLeaf() const { return graphLeaf; }
226 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
227 const_iterator onePair = this->find(minstr);
228 return (onePair != this->end())? (*onePair).second : NULL;
232 // Delete a node from the graph.
234 void eraseNode(SchedGraphNode* node);
237 // Unordered iterators.
238 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
241 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
244 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
246 const_iterator begin() const {
247 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
249 const_iterator end() const {
250 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
254 // Ordered iterators.
255 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
257 // void postord_init();
258 // postorder_iterator postord_begin();
259 // postorder_iterator postord_end();
260 // const_postorder_iterator postord_begin() const;
261 // const_postorder_iterator postord_end() const;
269 friend class SchedGraphSet; // give access to ctor
271 // disable default constructor and provide a ctor for single-block graphs
272 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
273 /*ctor*/ SchedGraph (const BasicBlock* bb,
274 const TargetMachine& target);
275 /*dtor*/ ~SchedGraph ();
277 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
278 SchedGraphNode* node)
280 assert((*this)[minstr] == NULL);
281 (*this)[minstr] = node;
287 void buildGraph (const TargetMachine& target);
289 void addEdgesForInstruction (SchedGraphNode* node,
290 NodeToRegRefMap& regToRefVecMap,
291 const TargetMachine& target);
293 void addCDEdges (const TerminatorInst* term,
294 const TargetMachine& target);
296 void addMemEdges (const vector<const Instruction*>& memVec,
297 const TargetMachine& target);
299 void addMachineRegEdges (NodeToRegRefMap& regToRefVecMap,
300 const TargetMachine& target);
302 void addSSAEdge (SchedGraphNode* node,
304 const TargetMachine& target);
306 void addDummyEdges ();
310 class SchedGraphSet :
312 private hash_map<const BasicBlock*, SchedGraph*>
315 const Method* method;
318 typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator;
319 typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator;
322 /*ctor*/ SchedGraphSet (const Method* _method,
323 const TargetMachine& target);
324 /*dtor*/ ~SchedGraphSet ();
329 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
330 const_iterator onePair = this->find(bb);
331 return (onePair != this->end())? (*onePair).second : NULL;
338 return hash_map<const BasicBlock*, SchedGraph*>::begin();
341 return hash_map<const BasicBlock*, SchedGraph*>::end();
343 const_iterator begin() const {
344 return hash_map<const BasicBlock*, SchedGraph*>::begin();
346 const_iterator end() const {
347 return hash_map<const BasicBlock*, SchedGraph*>::end();
356 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
357 assert((*this)[bb] == NULL);
364 void buildGraphsForMethod (const Method *method,
365 const TargetMachine& target);
369 //********************** Sched Graph Iterators *****************************/
371 // Ok to make it a template because it shd get instantiated at most twice:
372 // for <SchedGraphNode, SchedGraphNode::iterator> and
373 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
375 template <class _NodeType, class _EdgeType, class _EdgeIter>
376 class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
380 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
382 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
384 inline bool operator==(const _Self& x) const { return oi == x.oi; }
385 inline bool operator!=(const _Self& x) const { return !operator==(x); }
387 // operator*() differs for pred or succ iterator
388 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
389 inline _NodeType* operator->() const { return operator*(); }
391 inline _EdgeType* getEdge() const { return *(oi); }
393 inline _Self &operator++() { ++oi; return *this; } // Preincrement
394 inline _Self operator++(int) { // Postincrement
395 _Self tmp(*this); ++*this; return tmp;
398 inline _Self &operator--() { --oi; return *this; } // Predecrement
399 inline _Self operator--(int) { // Postdecrement
400 _Self tmp = *this; --*this; return tmp;
404 template <class _NodeType, class _EdgeType, class _EdgeIter>
405 class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
409 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
411 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
413 inline bool operator==(const _Self& x) const { return oi == x.oi; }
414 inline bool operator!=(const _Self& x) const { return !operator==(x); }
416 inline _NodeType* operator*() const { return (*oi)->getSink(); }
417 inline _NodeType* operator->() const { return operator*(); }
419 inline _EdgeType* getEdge() const { return *(oi); }
421 inline _Self &operator++() { ++oi; return *this; } // Preincrement
422 inline _Self operator++(int) { // Postincrement
423 _Self tmp(*this); ++*this; return tmp;
426 inline _Self &operator--() { --oi; return *this; } // Predecrement
427 inline _Self operator--(int) { // Postdecrement
428 _Self tmp = *this; --*this; return tmp;
434 // sg_pred_const_iterator
436 typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
438 typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
439 sg_pred_const_iterator;
441 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
442 return sg_pred_iterator(N->beginInEdges());
444 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
445 return sg_pred_iterator(N->endInEdges());
447 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
448 return sg_pred_const_iterator(N->beginInEdges());
450 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
451 return sg_pred_const_iterator(N->endInEdges());
457 // sg_succ_const_iterator
459 typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
461 typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
462 sg_succ_const_iterator;
464 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
465 return sg_succ_iterator(N->beginOutEdges());
467 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
468 return sg_succ_iterator(N->endOutEdges());
470 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
471 return sg_succ_const_iterator(N->beginOutEdges());
473 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
474 return sg_succ_const_iterator(N->endOutEdges());
481 typedef cfg::POIterator<SchedGraphNode, sg_succ_iterator> sg_po_iterator;
482 typedef cfg::POIterator<const SchedGraphNode,
483 sg_succ_const_iterator> sg_po_const_iterator;
486 //************************ External Functions *****************************/
489 ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
491 ostream& operator<<(ostream& os, const SchedGraphNode& node);
494 /***************************************************************************/