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/Target/MachineInstrInfo.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "Support/NonCopyable.h"
25 #include "Support/HashExtras.h"
26 #include "Support/GraphTraits.h"
27 #include <ext/hash_map>
39 class ValueToDefVecMap;
42 class MachineCodeForBasicBlock;
45 /******************** Exported Data Types and Constants ********************/
47 typedef int ResourceId;
48 const ResourceId InvalidRID = -1;
49 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
50 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
51 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
54 //*********************** Public Class Declarations ************************/
56 class SchedGraphEdge: public NonCopyable {
58 enum SchedGraphEdgeDepType {
59 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
61 enum DataDepOrderType {
62 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
68 SchedGraphEdgeDepType depType;
69 unsigned int depOrderType;
70 int minDelay; // cached latency (assumes fixed target arch)
75 ResourceId resourceId;
79 // For all constructors, if minDelay is unspecified, minDelay is
80 // set to _src->getLatency().
81 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
82 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
83 SchedGraphNode* _sink,
84 SchedGraphEdgeDepType _depType,
85 unsigned int _depOrderType,
88 // constructor for explicit value dependence (may be true/anti/output)
89 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
90 SchedGraphNode* _sink,
92 unsigned int _depOrderType,
95 // constructor for machine register dependence
96 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
97 SchedGraphNode* _sink,
99 unsigned int _depOrderType,
102 // constructor for any other machine resource dependences.
103 // DataDepOrderType is always NonDataDep. It it not an argument to
104 // avoid overloading ambiguity with previous constructor.
105 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
106 SchedGraphNode* _sink,
107 ResourceId _resourceId,
110 /*dtor*/ ~SchedGraphEdge();
112 SchedGraphNode* getSrc () const { return src; }
113 SchedGraphNode* getSink () const { return sink; }
114 int getMinDelay () const { return minDelay; }
115 SchedGraphEdgeDepType getDepType () const { return depType; }
117 const Value* getValue () const {
118 assert(depType == ValueDep); return val;
120 int getMachineReg () const {
121 assert(depType == MachineRegister); return machineRegNum;
123 int getResourceId () const {
124 assert(depType == MachineResource); return resourceId;
131 friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge);
133 void dump (int indent=0) const;
136 // disable default ctor
137 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
142 class SchedGraphNode: public NonCopyable {
145 const BasicBlock* bb;
146 const MachineInstr* minstr;
147 std::vector<SchedGraphEdge*> inEdges;
148 std::vector<SchedGraphEdge*> outEdges;
149 int origIndexInBB; // original position of machine instr in BB
153 typedef std::vector<SchedGraphEdge*>:: iterator iterator;
154 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
155 typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
156 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
162 unsigned int getNodeId () const { return nodeId; }
163 const MachineInstr* getMachineInstr () const { return minstr; }
164 const MachineOpCode getOpCode () const { return minstr->getOpCode();}
165 int getLatency () const { return latency; }
166 unsigned int getNumInEdges () const { return inEdges.size(); }
167 unsigned int getNumOutEdges () const { return outEdges.size(); }
168 bool isDummyNode () const { return (minstr == NULL); }
169 const BasicBlock* getBB () const { return bb; }
170 int getOrigIndexInBB() const { return origIndexInBB; }
175 iterator beginInEdges () { return inEdges.begin(); }
176 iterator endInEdges () { return inEdges.end(); }
177 iterator beginOutEdges () { return outEdges.begin(); }
178 iterator endOutEdges () { return outEdges.end(); }
180 const_iterator beginInEdges () const { return inEdges.begin(); }
181 const_iterator endInEdges () const { return inEdges.end(); }
182 const_iterator beginOutEdges () const { return outEdges.begin(); }
183 const_iterator endOutEdges () const { return outEdges.end(); }
189 friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node);
191 void dump (int indent=0) const;
194 friend class SchedGraph; // give access for ctor and dtor
195 friend class SchedGraphEdge; // give access for adding edges
197 void addInEdge (SchedGraphEdge* edge);
198 void addOutEdge (SchedGraphEdge* edge);
200 void removeInEdge (const SchedGraphEdge* edge);
201 void removeOutEdge (const SchedGraphEdge* edge);
203 // disable default constructor and provide a ctor for single-block graphs
204 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
205 /*ctor*/ SchedGraphNode (unsigned int _nodeId,
206 const BasicBlock* _bb,
207 const MachineInstr* _minstr,
209 const TargetMachine& _target);
210 /*dtor*/ ~SchedGraphNode ();
217 private std::hash_map<const MachineInstr*, SchedGraphNode*>
220 std::vector<const BasicBlock*> bbVec; // basic blocks included in the graph
221 SchedGraphNode* graphRoot; // the root and leaf are not inserted
222 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
224 typedef std::hash_map<const MachineInstr*, SchedGraphNode*> map_base;
226 using map_base::iterator;
227 using map_base::const_iterator;
233 const std::vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
234 const unsigned int getNumNodes() const { return size()+2; }
235 SchedGraphNode* getRoot() const { return graphRoot; }
236 SchedGraphNode* getLeaf() const { return graphLeaf; }
238 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
239 const_iterator onePair = this->find(minstr);
240 return (onePair != this->end())? (*onePair).second : NULL;
244 // Delete nodes or edges from the graph.
246 void eraseNode (SchedGraphNode* node);
248 void eraseIncomingEdges (SchedGraphNode* node,
249 bool addDummyEdges = true);
251 void eraseOutgoingEdges (SchedGraphNode* node,
252 bool addDummyEdges = true);
254 void eraseIncidentEdges (SchedGraphNode* node,
255 bool addDummyEdges = true);
258 // Unordered iterators.
259 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
261 using map_base::begin;
265 // Ordered iterators.
266 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
268 // void postord_init();
269 // postorder_iterator postord_begin();
270 // postorder_iterator postord_end();
271 // const_postorder_iterator postord_begin() const;
272 // const_postorder_iterator postord_end() const;
280 friend class SchedGraphSet; // give access to ctor
282 // disable default constructor and provide a ctor for single-block graphs
283 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
284 /*ctor*/ SchedGraph (const BasicBlock* bb,
285 const TargetMachine& target);
286 /*dtor*/ ~SchedGraph ();
288 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
289 SchedGraphNode* node)
291 assert((*this)[minstr] == NULL);
292 (*this)[minstr] = node;
298 void buildGraph (const TargetMachine& target);
300 void buildNodesforBB (const TargetMachine& target,
301 const BasicBlock* bb,
302 std::vector<SchedGraphNode*>& memNod,
303 RegToRefVecMap& regToRefVecMap,
304 ValueToDefVecMap& valueToDefVecMap);
306 void findDefUseInfoAtInstr (const TargetMachine& target,
307 SchedGraphNode* node,
308 std::vector<SchedGraphNode*>& memNode,
309 RegToRefVecMap& regToRefVecMap,
310 ValueToDefVecMap& valueToDefVecMap);
312 void addEdgesForInstruction(const MachineInstr& minstr,
313 const ValueToDefVecMap& valueToDefVecMap,
314 const TargetMachine& target);
316 void addCDEdges (const TerminatorInst* term,
317 const TargetMachine& target);
319 void addMemEdges (const std::vector<SchedGraphNode*>& memNod,
320 const TargetMachine& target);
322 void addCallCCEdges (const std::vector<SchedGraphNode*>& memNod,
323 MachineCodeForBasicBlock& bbMvec,
324 const TargetMachine& target);
326 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
327 const TargetMachine& target);
329 void addEdgesForValue (SchedGraphNode* refNode,
330 const RefVec& defVec,
331 const Value* defValue,
333 const TargetMachine& target);
335 void addDummyEdges ();
339 class SchedGraphSet :
341 private std::hash_map<const BasicBlock*, SchedGraph*>
344 const Method* method;
347 typedef std::hash_map<const BasicBlock*, SchedGraph*> map_base;
348 using map_base::iterator;
349 using map_base::const_iterator;
352 /*ctor*/ SchedGraphSet (const Method* _method,
353 const TargetMachine& target);
354 /*dtor*/ ~SchedGraphSet ();
359 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
360 const_iterator onePair = this->find(bb);
361 return (onePair != this->end())? (*onePair).second : NULL;
367 using map_base::begin;
376 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
377 assert((*this)[bb] == NULL);
384 void buildGraphsForMethod (const Method *method,
385 const TargetMachine& target);
389 //********************** Sched Graph Iterators *****************************/
391 // Ok to make it a template because it shd get instantiated at most twice:
392 // for <SchedGraphNode, SchedGraphNode::iterator> and
393 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
395 template <class _NodeType, class _EdgeType, class _EdgeIter>
396 class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
400 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
402 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
404 inline bool operator==(const _Self& x) const { return oi == x.oi; }
405 inline bool operator!=(const _Self& x) const { return !operator==(x); }
407 // operator*() differs for pred or succ iterator
408 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
409 inline _NodeType* operator->() const { return operator*(); }
411 inline _EdgeType* getEdge() const { return *(oi); }
413 inline _Self &operator++() { ++oi; return *this; } // Preincrement
414 inline _Self operator++(int) { // Postincrement
415 _Self tmp(*this); ++*this; return tmp;
418 inline _Self &operator--() { --oi; return *this; } // Predecrement
419 inline _Self operator--(int) { // Postdecrement
420 _Self tmp = *this; --*this; return tmp;
424 template <class _NodeType, class _EdgeType, class _EdgeIter>
425 class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
429 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
431 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
433 inline bool operator==(const _Self& x) const { return oi == x.oi; }
434 inline bool operator!=(const _Self& x) const { return !operator==(x); }
436 inline _NodeType* operator*() const { return (*oi)->getSink(); }
437 inline _NodeType* operator->() const { return operator*(); }
439 inline _EdgeType* getEdge() const { return *(oi); }
441 inline _Self &operator++() { ++oi; return *this; } // Preincrement
442 inline _Self operator++(int) { // Postincrement
443 _Self tmp(*this); ++*this; return tmp;
446 inline _Self &operator--() { --oi; return *this; } // Predecrement
447 inline _Self operator--(int) { // Postdecrement
448 _Self tmp = *this; --*this; return tmp;
454 // sg_pred_const_iterator
456 typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
458 typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
459 sg_pred_const_iterator;
461 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
462 return sg_pred_iterator(N->beginInEdges());
464 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
465 return sg_pred_iterator(N->endInEdges());
467 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
468 return sg_pred_const_iterator(N->beginInEdges());
470 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
471 return sg_pred_const_iterator(N->endInEdges());
477 // sg_succ_const_iterator
479 typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
481 typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
482 sg_succ_const_iterator;
484 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
485 return sg_succ_iterator(N->beginOutEdges());
487 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
488 return sg_succ_iterator(N->endOutEdges());
490 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
491 return sg_succ_const_iterator(N->beginOutEdges());
493 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
494 return sg_succ_const_iterator(N->endOutEdges());
497 // Provide specializations of GraphTraits to be able to use graph iterators on
498 // the scheduling graph!
500 template <> struct GraphTraits<SchedGraph*> {
501 typedef SchedGraphNode NodeType;
502 typedef sg_succ_iterator ChildIteratorType;
504 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
505 static inline ChildIteratorType child_begin(NodeType *N) {
506 return succ_begin(N);
508 static inline ChildIteratorType child_end(NodeType *N) {
513 template <> struct GraphTraits<const SchedGraph*> {
514 typedef const SchedGraphNode NodeType;
515 typedef sg_succ_const_iterator ChildIteratorType;
517 static inline NodeType *getEntryNode(const SchedGraph *SG) {
518 return SG->getRoot();
520 static inline ChildIteratorType child_begin(NodeType *N) {
521 return succ_begin(N);
523 static inline ChildIteratorType child_end(NodeType *N) {
529 std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
530 std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);