1 //===-- SchedGraph.h - Scheduling Graph --------------------------*- C++ -*--=//
4 // Scheduling graph based on SSA graph plus extra dependence edges
5 // capturing dependences due to machine resources (machine registers,
6 // CC registers, and any others).
9 // This graph tries to leverage the SSA graph as much as possible,
10 // but captures the extra dependences through a common interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_SCHEDGRAPH_H
15 #define LLVM_CODEGEN_SCHEDGRAPH_H
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "Support/HashExtras.h"
19 #include "Support/GraphTraits.h"
31 class ValueToDefVecMap;
34 class MachineBasicBlock;
37 /******************** Exported Data Types and Constants ********************/
39 typedef int ResourceId;
40 const ResourceId InvalidRID = -1;
41 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
42 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
43 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
46 //*********************** Public Class Declarations ************************/
48 class SchedGraphEdge: public NonCopyable {
50 enum SchedGraphEdgeDepType {
51 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
53 enum DataDepOrderType {
54 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
60 SchedGraphEdgeDepType depType;
61 unsigned int depOrderType;
62 int minDelay; // cached latency (assumes fixed target arch)
67 ResourceId resourceId;
71 // For all constructors, if minDelay is unspecified, minDelay is
72 // set to _src->getLatency().
73 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
74 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
75 SchedGraphNode* _sink,
76 SchedGraphEdgeDepType _depType,
77 unsigned int _depOrderType,
80 // constructor for explicit value dependence (may be true/anti/output)
81 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
82 SchedGraphNode* _sink,
84 unsigned int _depOrderType,
87 // constructor for machine register dependence
88 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
89 SchedGraphNode* _sink,
91 unsigned int _depOrderType,
94 // constructor for any other machine resource dependences.
95 // DataDepOrderType is always NonDataDep. It it not an argument to
96 // avoid overloading ambiguity with previous constructor.
97 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
98 SchedGraphNode* _sink,
99 ResourceId _resourceId,
102 /*dtor*/ ~SchedGraphEdge();
104 SchedGraphNode* getSrc () const { return src; }
105 SchedGraphNode* getSink () const { return sink; }
106 int getMinDelay () const { return minDelay; }
107 SchedGraphEdgeDepType getDepType () const { return depType; }
109 const Value* getValue () const {
110 assert(depType == ValueDep); return val;
112 int getMachineReg () const {
113 assert(depType == MachineRegister); return machineRegNum;
115 int getResourceId () const {
116 assert(depType == MachineResource); return resourceId;
123 friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge);
125 void dump (int indent=0) const;
128 // disable default ctor
129 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
134 class SchedGraphNode: public NonCopyable {
137 const BasicBlock* bb;
138 const MachineInstr* minstr;
139 std::vector<SchedGraphEdge*> inEdges;
140 std::vector<SchedGraphEdge*> outEdges;
141 int origIndexInBB; // original position of machine instr in BB
145 typedef std::vector<SchedGraphEdge*>:: iterator iterator;
146 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
147 typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
148 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
154 unsigned int getNodeId () const { return nodeId; }
155 const MachineInstr* getMachineInstr () const { return minstr; }
156 const MachineOpCode getOpCode () const { return minstr->getOpCode();}
157 int getLatency () const { return latency; }
158 unsigned int getNumInEdges () const { return inEdges.size(); }
159 unsigned int getNumOutEdges () const { return outEdges.size(); }
160 bool isDummyNode () const { return (minstr == NULL); }
161 const BasicBlock* getBB () const { return bb; }
162 int getOrigIndexInBB() const { return origIndexInBB; }
167 iterator beginInEdges () { return inEdges.begin(); }
168 iterator endInEdges () { return inEdges.end(); }
169 iterator beginOutEdges () { return outEdges.begin(); }
170 iterator endOutEdges () { return outEdges.end(); }
172 const_iterator beginInEdges () const { return inEdges.begin(); }
173 const_iterator endInEdges () const { return inEdges.end(); }
174 const_iterator beginOutEdges () const { return outEdges.begin(); }
175 const_iterator endOutEdges () const { return outEdges.end(); }
181 friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node);
183 void dump (int indent=0) const;
186 friend class SchedGraph; // give access for ctor and dtor
187 friend class SchedGraphEdge; // give access for adding edges
189 void addInEdge (SchedGraphEdge* edge);
190 void addOutEdge (SchedGraphEdge* edge);
192 void removeInEdge (const SchedGraphEdge* edge);
193 void removeOutEdge (const SchedGraphEdge* edge);
195 // disable default constructor and provide a ctor for single-block graphs
196 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
197 /*ctor*/ SchedGraphNode (unsigned int _nodeId,
198 const BasicBlock* _bb,
199 const MachineInstr* _minstr,
201 const TargetMachine& _target);
202 /*dtor*/ ~SchedGraphNode ();
209 private hash_map<const MachineInstr*, SchedGraphNode*>
212 std::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())
216 typedef hash_map<const MachineInstr*, SchedGraphNode*> map_base;
218 using map_base::iterator;
219 using map_base::const_iterator;
225 const std::vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
226 const unsigned int getNumNodes() const { return size()+2; }
227 SchedGraphNode* getRoot() const { return graphRoot; }
228 SchedGraphNode* getLeaf() const { return graphLeaf; }
230 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
231 const_iterator onePair = this->find(minstr);
232 return (onePair != this->end())? (*onePair).second : NULL;
236 // Delete nodes or edges from the graph.
238 void eraseNode (SchedGraphNode* node);
240 void eraseIncomingEdges (SchedGraphNode* node,
241 bool addDummyEdges = true);
243 void eraseOutgoingEdges (SchedGraphNode* node,
244 bool addDummyEdges = true);
246 void eraseIncidentEdges (SchedGraphNode* node,
247 bool addDummyEdges = true);
250 // Unordered iterators.
251 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
253 using map_base::begin;
257 // Ordered iterators.
258 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
260 // void postord_init();
261 // postorder_iterator postord_begin();
262 // postorder_iterator postord_end();
263 // const_postorder_iterator postord_begin() const;
264 // const_postorder_iterator postord_end() const;
272 friend class SchedGraphSet; // give access to ctor
274 // disable default constructor and provide a ctor for single-block graphs
275 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
276 /*ctor*/ SchedGraph (const BasicBlock* bb,
277 const TargetMachine& target);
278 /*dtor*/ ~SchedGraph ();
280 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
281 SchedGraphNode* node)
283 assert((*this)[minstr] == NULL);
284 (*this)[minstr] = node;
290 void buildGraph (const TargetMachine& target);
292 void buildNodesforBB (const TargetMachine& target,
293 const BasicBlock* bb,
294 std::vector<SchedGraphNode*>& memNod,
295 RegToRefVecMap& regToRefVecMap,
296 ValueToDefVecMap& valueToDefVecMap);
298 void findDefUseInfoAtInstr (const TargetMachine& target,
299 SchedGraphNode* node,
300 std::vector<SchedGraphNode*>& memNode,
301 RegToRefVecMap& regToRefVecMap,
302 ValueToDefVecMap& valueToDefVecMap);
304 void addEdgesForInstruction(const MachineInstr& minstr,
305 const ValueToDefVecMap& valueToDefVecMap,
306 const TargetMachine& target);
308 void addCDEdges (const TerminatorInst* term,
309 const TargetMachine& target);
311 void addMemEdges (const std::vector<SchedGraphNode*>& memNod,
312 const TargetMachine& target);
314 void addCallCCEdges (const std::vector<SchedGraphNode*>& memNod,
315 MachineBasicBlock& bbMvec,
316 const TargetMachine& target);
318 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
319 const TargetMachine& target);
321 void addEdgesForValue (SchedGraphNode* refNode,
322 const RefVec& defVec,
323 const Value* defValue,
325 bool refNodeIsDefAndUse,
326 const TargetMachine& target);
328 void addDummyEdges ();
332 class SchedGraphSet :
334 private std::vector<SchedGraph*>
337 const Function* method;
340 typedef std::vector<SchedGraph*> baseVector;
341 using baseVector::iterator;
342 using baseVector::const_iterator;
345 /*ctor*/ SchedGraphSet (const Function * function,
346 const TargetMachine& target);
347 /*dtor*/ ~SchedGraphSet ();
350 using baseVector::begin;
351 using baseVector::end;
357 inline void addGraph(SchedGraph* graph) {
358 assert(graph != NULL);
359 this->push_back(graph);
363 void buildGraphsForMethod (const Function *F,
364 const TargetMachine& target);
368 //********************** Sched Graph Iterators *****************************/
370 // Ok to make it a template because it shd get instantiated at most twice:
371 // for <SchedGraphNode, SchedGraphNode::iterator> and
372 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
374 template <class _NodeType, class _EdgeType, class _EdgeIter>
375 class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
379 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
381 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
383 inline bool operator==(const _Self& x) const { return oi == x.oi; }
384 inline bool operator!=(const _Self& x) const { return !operator==(x); }
386 // operator*() differs for pred or succ iterator
387 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
388 inline _NodeType* operator->() const { return operator*(); }
390 inline _EdgeType* getEdge() const { return *(oi); }
392 inline _Self &operator++() { ++oi; return *this; } // Preincrement
393 inline _Self operator++(int) { // Postincrement
394 _Self tmp(*this); ++*this; return tmp;
397 inline _Self &operator--() { --oi; return *this; } // Predecrement
398 inline _Self operator--(int) { // Postdecrement
399 _Self tmp = *this; --*this; return tmp;
403 template <class _NodeType, class _EdgeType, class _EdgeIter>
404 class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
408 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
410 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
412 inline bool operator==(const _Self& x) const { return oi == x.oi; }
413 inline bool operator!=(const _Self& x) const { return !operator==(x); }
415 inline _NodeType* operator*() const { return (*oi)->getSink(); }
416 inline _NodeType* operator->() const { return operator*(); }
418 inline _EdgeType* getEdge() const { return *(oi); }
420 inline _Self &operator++() { ++oi; return *this; } // Preincrement
421 inline _Self operator++(int) { // Postincrement
422 _Self tmp(*this); ++*this; return tmp;
425 inline _Self &operator--() { --oi; return *this; } // Predecrement
426 inline _Self operator--(int) { // Postdecrement
427 _Self tmp = *this; --*this; return tmp;
433 // sg_pred_const_iterator
435 typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
437 typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
438 sg_pred_const_iterator;
440 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
441 return sg_pred_iterator(N->beginInEdges());
443 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
444 return sg_pred_iterator(N->endInEdges());
446 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
447 return sg_pred_const_iterator(N->beginInEdges());
449 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
450 return sg_pred_const_iterator(N->endInEdges());
456 // sg_succ_const_iterator
458 typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
460 typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
461 sg_succ_const_iterator;
463 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
464 return sg_succ_iterator(N->beginOutEdges());
466 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
467 return sg_succ_iterator(N->endOutEdges());
469 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
470 return sg_succ_const_iterator(N->beginOutEdges());
472 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
473 return sg_succ_const_iterator(N->endOutEdges());
476 // Provide specializations of GraphTraits to be able to use graph iterators on
477 // the scheduling graph!
479 template <> struct GraphTraits<SchedGraph*> {
480 typedef SchedGraphNode NodeType;
481 typedef sg_succ_iterator ChildIteratorType;
483 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
484 static inline ChildIteratorType child_begin(NodeType *N) {
485 return succ_begin(N);
487 static inline ChildIteratorType child_end(NodeType *N) {
492 template <> struct GraphTraits<const SchedGraph*> {
493 typedef const SchedGraphNode NodeType;
494 typedef sg_succ_const_iterator ChildIteratorType;
496 static inline NodeType *getEntryNode(const SchedGraph *SG) {
497 return SG->getRoot();
499 static inline ChildIteratorType child_begin(NodeType *N) {
500 return succ_begin(N);
502 static inline ChildIteratorType child_end(NodeType *N) {
508 std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
509 std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);