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"
25 #include "llvm/Target/MachineInstrInfo.h"
26 #include "llvm/CodeGen/MachineInstr.h"
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, DefUseDep, 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 =TrueDep,
88 // constructor for explicit def-use or memory def-use edge
89 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
90 SchedGraphNode* _sink,
92 unsigned int _depOrderType =TrueDep,
95 // constructor for machine register dependence
96 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
97 SchedGraphNode* _sink,
99 unsigned int _depOrderType =TrueDep,
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 == DefUseDep || depType == MemoryDep); return val;
120 int getMachineReg () const {
121 assert(depType == MachineRegister); return machineRegNum;
123 int getResourceId () const {
124 assert(depType == MachineResource); return resourceId;
131 friend ostream& operator<<(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 Instruction* instr;
146 const MachineInstr* minstr;
147 vector<SchedGraphEdge*> inEdges;
148 vector<SchedGraphEdge*> outEdges;
152 typedef vector<SchedGraphEdge*>:: iterator iterator;
153 typedef vector<SchedGraphEdge*>::const_iterator const_iterator;
154 typedef vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
155 typedef vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
161 unsigned int getNodeId () const { return nodeId; }
162 const Instruction* getInstr () const { return instr; }
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); }
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 ostream& operator<<(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 Instruction* _instr,
205 const MachineInstr* _minstr,
206 const TargetMachine& _target);
207 /*dtor*/ ~SchedGraphNode ();
214 private hash_map<const MachineInstr*, SchedGraphNode*>
217 vector<const BasicBlock*> bbVec; // basic blocks included in the graph
218 SchedGraphNode* graphRoot; // the root and leaf are not inserted
219 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
222 typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator;
223 typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
229 const vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; }
230 const unsigned int getNumNodes() const { return size()+2; }
231 SchedGraphNode* getRoot() const { return graphRoot; }
232 SchedGraphNode* getLeaf() const { return graphLeaf; }
234 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
235 const_iterator onePair = this->find(minstr);
236 return (onePair != this->end())? (*onePair).second : NULL;
240 // Delete nodes or edges from the graph.
242 void eraseNode (SchedGraphNode* node);
244 void eraseIncomingEdges (SchedGraphNode* node,
245 bool addDummyEdges = true);
247 void eraseOutgoingEdges (SchedGraphNode* node,
248 bool addDummyEdges = true);
250 void eraseIncidentEdges (SchedGraphNode* node,
251 bool addDummyEdges = true);
254 // Unordered iterators.
255 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
258 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
261 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
263 const_iterator begin() const {
264 return hash_map<const MachineInstr*, SchedGraphNode*>::begin();
266 const_iterator end() const {
267 return hash_map<const MachineInstr*, SchedGraphNode*>::end();
271 // Ordered iterators.
272 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
274 // void postord_init();
275 // postorder_iterator postord_begin();
276 // postorder_iterator postord_end();
277 // const_postorder_iterator postord_begin() const;
278 // const_postorder_iterator postord_end() const;
286 friend class SchedGraphSet; // give access to ctor
288 // disable default constructor and provide a ctor for single-block graphs
289 /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT
290 /*ctor*/ SchedGraph (const BasicBlock* bb,
291 const TargetMachine& target);
292 /*dtor*/ ~SchedGraph ();
294 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
295 SchedGraphNode* node)
297 assert((*this)[minstr] == NULL);
298 (*this)[minstr] = node;
304 void buildGraph (const TargetMachine& target);
306 void buildNodesforVMInstr (const TargetMachine& target,
307 const Instruction* instr,
308 vector<SchedGraphNode*>& memNodeVec,
309 RegToRefVecMap& regToRefVecMap,
310 ValueToDefVecMap& valueToDefVecMap);
312 void findDefUseInfoAtInstr (const TargetMachine& target,
313 SchedGraphNode* node,
314 vector<SchedGraphNode*>& memNodeVec,
315 RegToRefVecMap& regToRefVecMap,
316 ValueToDefVecMap& valueToDefVecMap);
318 void addEdgesForInstruction(const MachineInstr& minstr,
319 const ValueToDefVecMap& valueToDefVecMap,
320 const TargetMachine& target);
322 void addCDEdges (const TerminatorInst* term,
323 const TargetMachine& target);
325 void addMemEdges (const vector<SchedGraphNode*>& memNodeVec,
326 const TargetMachine& target);
328 void addCallCCEdges (const vector<SchedGraphNode*>& memNodeVec,
329 MachineCodeForBasicBlock& bbMvec,
330 const TargetMachine& target);
332 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
333 const TargetMachine& target);
335 void addSSAEdge (SchedGraphNode* node,
336 const RefVec& defVec,
337 const Value* defValue,
338 const TargetMachine& target);
340 void addNonSSAEdgesForValue (const Instruction* instr,
341 const TargetMachine& target);
343 void addDummyEdges ();
347 class SchedGraphSet :
349 private hash_map<const BasicBlock*, SchedGraph*>
352 const Method* method;
355 typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator;
356 typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator;
359 /*ctor*/ SchedGraphSet (const Method* _method,
360 const TargetMachine& target);
361 /*dtor*/ ~SchedGraphSet ();
366 SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const {
367 const_iterator onePair = this->find(bb);
368 return (onePair != this->end())? (*onePair).second : NULL;
375 return hash_map<const BasicBlock*, SchedGraph*>::begin();
378 return hash_map<const BasicBlock*, SchedGraph*>::end();
380 const_iterator begin() const {
381 return hash_map<const BasicBlock*, SchedGraph*>::begin();
383 const_iterator end() const {
384 return hash_map<const BasicBlock*, SchedGraph*>::end();
393 inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) {
394 assert((*this)[bb] == NULL);
401 void buildGraphsForMethod (const Method *method,
402 const TargetMachine& target);
406 //********************** Sched Graph Iterators *****************************/
408 // Ok to make it a template because it shd get instantiated at most twice:
409 // for <SchedGraphNode, SchedGraphNode::iterator> and
410 // for <const SchedGraphNode, SchedGraphNode::const_iterator>.
412 template <class _NodeType, class _EdgeType, class _EdgeIter>
413 class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
417 typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
419 inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {}
421 inline bool operator==(const _Self& x) const { return oi == x.oi; }
422 inline bool operator!=(const _Self& x) const { return !operator==(x); }
424 // operator*() differs for pred or succ iterator
425 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
426 inline _NodeType* operator->() const { return operator*(); }
428 inline _EdgeType* getEdge() const { return *(oi); }
430 inline _Self &operator++() { ++oi; return *this; } // Preincrement
431 inline _Self operator++(int) { // Postincrement
432 _Self tmp(*this); ++*this; return tmp;
435 inline _Self &operator--() { --oi; return *this; } // Predecrement
436 inline _Self operator--(int) { // Postdecrement
437 _Self tmp = *this; --*this; return tmp;
441 template <class _NodeType, class _EdgeType, class _EdgeIter>
442 class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> {
446 typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
448 inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
450 inline bool operator==(const _Self& x) const { return oi == x.oi; }
451 inline bool operator!=(const _Self& x) const { return !operator==(x); }
453 inline _NodeType* operator*() const { return (*oi)->getSink(); }
454 inline _NodeType* operator->() const { return operator*(); }
456 inline _EdgeType* getEdge() const { return *(oi); }
458 inline _Self &operator++() { ++oi; return *this; } // Preincrement
459 inline _Self operator++(int) { // Postincrement
460 _Self tmp(*this); ++*this; return tmp;
463 inline _Self &operator--() { --oi; return *this; } // Predecrement
464 inline _Self operator--(int) { // Postdecrement
465 _Self tmp = *this; --*this; return tmp;
471 // sg_pred_const_iterator
473 typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
475 typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
476 sg_pred_const_iterator;
478 inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
479 return sg_pred_iterator(N->beginInEdges());
481 inline sg_pred_iterator pred_end( SchedGraphNode *N) {
482 return sg_pred_iterator(N->endInEdges());
484 inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
485 return sg_pred_const_iterator(N->beginInEdges());
487 inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
488 return sg_pred_const_iterator(N->endInEdges());
494 // sg_succ_const_iterator
496 typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
498 typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
499 sg_succ_const_iterator;
501 inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
502 return sg_succ_iterator(N->beginOutEdges());
504 inline sg_succ_iterator succ_end( SchedGraphNode *N) {
505 return sg_succ_iterator(N->endOutEdges());
507 inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
508 return sg_succ_const_iterator(N->beginOutEdges());
510 inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
511 return sg_succ_const_iterator(N->endOutEdges());
514 // Provide specializations of GraphTraits to be able to use graph iterators on
515 // the scheduling graph!
517 template <> struct GraphTraits<SchedGraph*> {
518 typedef SchedGraphNode NodeType;
519 typedef sg_succ_iterator ChildIteratorType;
521 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
522 static inline ChildIteratorType child_begin(NodeType *N) {
523 return succ_begin(N);
525 static inline ChildIteratorType child_end(NodeType *N) {
530 template <> struct GraphTraits<const SchedGraph*> {
531 typedef const SchedGraphNode NodeType;
532 typedef sg_succ_const_iterator ChildIteratorType;
534 static inline NodeType *getEntryNode(const SchedGraph *SG) {
535 return SG->getRoot();
537 static inline ChildIteratorType child_begin(NodeType *N) {
538 return succ_begin(N);
540 static inline ChildIteratorType child_end(NodeType *N) {
546 //************************ External Functions *****************************/
549 ostream& operator<<(ostream& os, const SchedGraphEdge& edge);
551 ostream& operator<<(ostream& os, const SchedGraphNode& node);
554 /***************************************************************************/