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/CodeGen/MachineInstr.h"
23 #include "Support/NonCopyable.h"
24 #include "Support/HashExtras.h"
25 #include "Support/GraphTraits.h"
37 class ValueToDefVecMap;
40 class MachineCodeForBasicBlock;
43 /******************** Exported Data Types and Constants ********************/
45 typedef int ResourceId;
46 const ResourceId InvalidRID = -1;
47 const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
48 const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
49 const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
52 //*********************** Public Class Declarations ************************/
54 class SchedGraphEdge: public NonCopyable {
56 enum SchedGraphEdgeDepType {
57 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
59 enum DataDepOrderType {
60 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
66 SchedGraphEdgeDepType depType;
67 unsigned int depOrderType;
68 int minDelay; // cached latency (assumes fixed target arch)
73 ResourceId resourceId;
77 // For all constructors, if minDelay is unspecified, minDelay is
78 // set to _src->getLatency().
79 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
80 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
81 SchedGraphNode* _sink,
82 SchedGraphEdgeDepType _depType,
83 unsigned int _depOrderType,
86 // constructor for explicit value dependence (may be true/anti/output)
87 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
88 SchedGraphNode* _sink,
90 unsigned int _depOrderType,
93 // constructor for machine register dependence
94 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
95 SchedGraphNode* _sink,
97 unsigned int _depOrderType,
100 // constructor for any other machine resource dependences.
101 // DataDepOrderType is always NonDataDep. It it not an argument to
102 // avoid overloading ambiguity with previous constructor.
103 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
104 SchedGraphNode* _sink,
105 ResourceId _resourceId,
108 /*dtor*/ ~SchedGraphEdge();
110 SchedGraphNode* getSrc () const { return src; }
111 SchedGraphNode* getSink () const { return sink; }
112 int getMinDelay () const { return minDelay; }
113 SchedGraphEdgeDepType getDepType () const { return depType; }
115 const Value* getValue () const {
116 assert(depType == ValueDep); return val;
118 int getMachineReg () const {
119 assert(depType == MachineRegister); return machineRegNum;
121 int getResourceId () const {
122 assert(depType == MachineResource); return resourceId;
129 friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge);
131 void dump (int indent=0) const;
134 // disable default ctor
135 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
140 class SchedGraphNode: public NonCopyable {
143 const BasicBlock* bb;
144 const MachineInstr* minstr;
145 std::vector<SchedGraphEdge*> inEdges;
146 std::vector<SchedGraphEdge*> outEdges;
147 int origIndexInBB; // original position of machine instr in BB
151 typedef std::vector<SchedGraphEdge*>:: iterator iterator;
152 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
153 typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
154 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
160 unsigned int getNodeId () const { return nodeId; }
161 const MachineInstr* getMachineInstr () const { return minstr; }
162 const MachineOpCode getOpCode () const { return minstr->getOpCode();}
163 int getLatency () const { return latency; }
164 unsigned int getNumInEdges () const { return inEdges.size(); }
165 unsigned int getNumOutEdges () const { return outEdges.size(); }
166 bool isDummyNode () const { return (minstr == NULL); }
167 const BasicBlock* getBB () const { return bb; }
168 int getOrigIndexInBB() const { return origIndexInBB; }
173 iterator beginInEdges () { return inEdges.begin(); }
174 iterator endInEdges () { return inEdges.end(); }
175 iterator beginOutEdges () { return outEdges.begin(); }
176 iterator endOutEdges () { return outEdges.end(); }
178 const_iterator beginInEdges () const { return inEdges.begin(); }
179 const_iterator endInEdges () const { return inEdges.end(); }
180 const_iterator beginOutEdges () const { return outEdges.begin(); }
181 const_iterator endOutEdges () const { return outEdges.end(); }
187 friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node);
189 void dump (int indent=0) const;
192 friend class SchedGraph; // give access for ctor and dtor
193 friend class SchedGraphEdge; // give access for adding edges
195 void addInEdge (SchedGraphEdge* edge);
196 void addOutEdge (SchedGraphEdge* edge);
198 void removeInEdge (const SchedGraphEdge* edge);
199 void removeOutEdge (const SchedGraphEdge* edge);
201 // disable default constructor and provide a ctor for single-block graphs
202 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
203 /*ctor*/ SchedGraphNode (unsigned int _nodeId,
204 const BasicBlock* _bb,
205 const MachineInstr* _minstr,
207 const TargetMachine& _target);
208 /*dtor*/ ~SchedGraphNode ();
215 private std::hash_map<const MachineInstr*, SchedGraphNode*>
218 std::vector<const BasicBlock*> bbVec; // basic blocks included in the graph
219 SchedGraphNode* graphRoot; // the root and leaf are not inserted
220 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
222 typedef std::hash_map<const MachineInstr*, SchedGraphNode*> map_base;
224 using map_base::iterator;
225 using map_base::const_iterator;
231 const std::vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
232 const unsigned int getNumNodes() const { return size()+2; }
233 SchedGraphNode* getRoot() const { return graphRoot; }
234 SchedGraphNode* getLeaf() const { return graphLeaf; }
236 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
237 const_iterator onePair = this->find(minstr);
238 return (onePair != this->end())? (*onePair).second : NULL;
242 // Delete nodes or edges from the graph.
244 void eraseNode (SchedGraphNode* node);
246 void eraseIncomingEdges (SchedGraphNode* node,
247 bool addDummyEdges = true);
249 void eraseOutgoingEdges (SchedGraphNode* node,
250 bool addDummyEdges = true);
252 void eraseIncidentEdges (SchedGraphNode* node,
253 bool addDummyEdges = true);
256 // Unordered iterators.
257 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
259 using map_base::begin;
263 // Ordered iterators.
264 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
266 // void postord_init();
267 // postorder_iterator postord_begin();
268 // postorder_iterator postord_end();
269 // const_postorder_iterator postord_begin() const;
270 // const_postorder_iterator postord_end() const;
278 friend class SchedGraphSet; // give access to ctor
280 // disable default constructor and provide a ctor for single-block graphs
281 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
282 /*ctor*/ SchedGraph (const BasicBlock* bb,
283 const TargetMachine& target);
284 /*dtor*/ ~SchedGraph ();
286 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
287 SchedGraphNode* node)
289 assert((*this)[minstr] == NULL);
290 (*this)[minstr] = node;
296 void buildGraph (const TargetMachine& target);
298 void buildNodesforBB (const TargetMachine& target,
299 const BasicBlock* bb,
300 std::vector<SchedGraphNode*>& memNod,
301 RegToRefVecMap& regToRefVecMap,
302 ValueToDefVecMap& valueToDefVecMap);
304 void findDefUseInfoAtInstr (const TargetMachine& target,
305 SchedGraphNode* node,
306 std::vector<SchedGraphNode*>& memNode,
307 RegToRefVecMap& regToRefVecMap,
308 ValueToDefVecMap& valueToDefVecMap);
310 void addEdgesForInstruction(const MachineInstr& minstr,
311 const ValueToDefVecMap& valueToDefVecMap,
312 const TargetMachine& target);
314 void addCDEdges (const TerminatorInst* term,
315 const TargetMachine& target);
317 void addMemEdges (const std::vector<SchedGraphNode*>& memNod,
318 const TargetMachine& target);
320 void addCallCCEdges (const std::vector<SchedGraphNode*>& memNod,
321 MachineCodeForBasicBlock& bbMvec,
322 const TargetMachine& target);
324 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
325 const TargetMachine& target);
327 void addEdgesForValue (SchedGraphNode* refNode,
328 const RefVec& defVec,
329 const Value* defValue,
331 const TargetMachine& target);
333 void addDummyEdges ();
337 class SchedGraphSet :
339 private std::hash_map<const BasicBlock*, SchedGraph*>
342 const Method* method;
345 typedef std::hash_map<const BasicBlock*, SchedGraph*> map_base;
346 using map_base::iterator;
347 using map_base::const_iterator;
350 /*ctor*/ SchedGraphSet (const Method* _method,
351 const TargetMachine& target);
352 /*dtor*/ ~SchedGraphSet ();
357 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
358 const_iterator onePair = this->find(bb);
359 return (onePair != this->end())? (*onePair).second : NULL;
365 using map_base::begin;
374 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
375 assert((*this)[bb] == NULL);
382 void buildGraphsForMethod (const Method *method,
383 const TargetMachine& target);
387 //********************** Sched Graph Iterators *****************************/
389 // Ok to make it a template because it shd get instantiated at most twice:
390 // for <SchedGraphNode, SchedGraphNode::iterator> and
391 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
393 template <class _NodeType, class _EdgeType, class _EdgeIter>
394 class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
398 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
400 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
402 inline bool operator==(const _Self& x) const { return oi == x.oi; }
403 inline bool operator!=(const _Self& x) const { return !operator==(x); }
405 // operator*() differs for pred or succ iterator
406 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
407 inline _NodeType* operator->() const { return operator*(); }
409 inline _EdgeType* getEdge() const { return *(oi); }
411 inline _Self &operator++() { ++oi; return *this; } // Preincrement
412 inline _Self operator++(int) { // Postincrement
413 _Self tmp(*this); ++*this; return tmp;
416 inline _Self &operator--() { --oi; return *this; } // Predecrement
417 inline _Self operator--(int) { // Postdecrement
418 _Self tmp = *this; --*this; return tmp;
422 template <class _NodeType, class _EdgeType, class _EdgeIter>
423 class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
427 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
429 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
431 inline bool operator==(const _Self& x) const { return oi == x.oi; }
432 inline bool operator!=(const _Self& x) const { return !operator==(x); }
434 inline _NodeType* operator*() const { return (*oi)->getSink(); }
435 inline _NodeType* operator->() const { return operator*(); }
437 inline _EdgeType* getEdge() const { return *(oi); }
439 inline _Self &operator++() { ++oi; return *this; } // Preincrement
440 inline _Self operator++(int) { // Postincrement
441 _Self tmp(*this); ++*this; return tmp;
444 inline _Self &operator--() { --oi; return *this; } // Predecrement
445 inline _Self operator--(int) { // Postdecrement
446 _Self tmp = *this; --*this; return tmp;
452 // sg_pred_const_iterator
454 typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
456 typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
457 sg_pred_const_iterator;
459 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
460 return sg_pred_iterator(N->beginInEdges());
462 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
463 return sg_pred_iterator(N->endInEdges());
465 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
466 return sg_pred_const_iterator(N->beginInEdges());
468 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
469 return sg_pred_const_iterator(N->endInEdges());
475 // sg_succ_const_iterator
477 typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
479 typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
480 sg_succ_const_iterator;
482 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
483 return sg_succ_iterator(N->beginOutEdges());
485 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
486 return sg_succ_iterator(N->endOutEdges());
488 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
489 return sg_succ_const_iterator(N->beginOutEdges());
491 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
492 return sg_succ_const_iterator(N->endOutEdges());
495 // Provide specializations of GraphTraits to be able to use graph iterators on
496 // the scheduling graph!
498 template <> struct GraphTraits<SchedGraph*> {
499 typedef SchedGraphNode NodeType;
500 typedef sg_succ_iterator ChildIteratorType;
502 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
503 static inline ChildIteratorType child_begin(NodeType *N) {
504 return succ_begin(N);
506 static inline ChildIteratorType child_end(NodeType *N) {
511 template <> struct GraphTraits<const SchedGraph*> {
512 typedef const SchedGraphNode NodeType;
513 typedef sg_succ_const_iterator ChildIteratorType;
515 static inline NodeType *getEntryNode(const SchedGraph *SG) {
516 return SG->getRoot();
518 static inline ChildIteratorType child_begin(NodeType *N) {
519 return succ_begin(N);
521 static inline ChildIteratorType child_end(NodeType *N) {
527 std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
528 std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);