Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 1 | /* -*-C++-*- |
| 2 | **************************************************************************** |
| 3 | * File: |
| 4 | * SchedGraph.h |
| 5 | * |
| 6 | * Purpose: |
| 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). |
| 10 | * |
| 11 | * Strategy: |
| 12 | * This graph tries to leverage the SSA graph as much as possible, |
| 13 | * but captures the extra dependences through a common interface. |
| 14 | * |
| 15 | * History: |
| 16 | * 7/20/01 - Vikram Adve - Created |
| 17 | ***************************************************************************/ |
| 18 | |
| 19 | #ifndef LLVM_CODEGEN_SCHEDGRAPH_H |
| 20 | #define LLVM_CODEGEN_SCHEDGRAPH_H |
| 21 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 22 | #include "llvm/Support/NonCopyable.h" |
| 23 | #include "llvm/Support/HashExtras.h" |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/GraphTraits.h" |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 25 | #include <hash_map> |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 26 | |
| 27 | class Value; |
| 28 | class Instruction; |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 29 | class TerminatorInst; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 30 | class BasicBlock; |
| 31 | class Method; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 32 | class TargetMachine; |
| 33 | class SchedGraphEdge; |
| 34 | class SchedGraphNode; |
| 35 | class SchedGraph; |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 36 | class RegToRefVecMap; |
Chris Lattner | c0c7708 | 2001-09-14 17:55:51 +0000 | [diff] [blame] | 37 | class MachineInstr; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 38 | |
| 39 | /******************** Exported Data Types and Constants ********************/ |
| 40 | |
| 41 | typedef int ResourceId; |
| 42 | const ResourceId InvalidResourceId = -1; |
| 43 | |
| 44 | |
| 45 | //*********************** Public Class Declarations ************************/ |
| 46 | |
| 47 | class SchedGraphEdge: public NonCopyable { |
| 48 | public: |
| 49 | enum SchedGraphEdgeDepType { |
| 50 | CtrlDep, MemoryDep, DefUseDep, MachineRegister, MachineResource |
| 51 | }; |
| 52 | enum DataDepOrderType { |
| 53 | TrueDep, AntiDep, OutputDep, NonDataDep |
| 54 | }; |
| 55 | |
| 56 | protected: |
| 57 | SchedGraphNode* src; |
| 58 | SchedGraphNode* sink; |
| 59 | SchedGraphEdgeDepType depType; |
| 60 | DataDepOrderType depOrderType; |
| 61 | int minDelay; // cached latency (assumes fixed target arch) |
| 62 | |
| 63 | union { |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 64 | const Value* val; |
| 65 | int machineRegNum; |
| 66 | ResourceId resourceId; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | public: |
| 70 | // For all constructors, if minDelay is unspecified, minDelay is |
| 71 | // set to _src->getLatency(). |
| 72 | // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument |
| 73 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 74 | SchedGraphNode* _sink, |
| 75 | SchedGraphEdgeDepType _depType, |
| 76 | DataDepOrderType _depOrderType =TrueDep, |
| 77 | int _minDelay = -1); |
| 78 | |
| 79 | // constructor for explicit def-use or memory def-use edge |
| 80 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 81 | SchedGraphNode* _sink, |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 82 | const Value* _val, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 83 | DataDepOrderType _depOrderType =TrueDep, |
| 84 | int _minDelay = -1); |
| 85 | |
| 86 | // constructor for machine register dependence |
| 87 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 88 | SchedGraphNode* _sink, |
| 89 | unsigned int _regNum, |
| 90 | DataDepOrderType _depOrderType =TrueDep, |
| 91 | int _minDelay = -1); |
| 92 | |
| 93 | // constructor for any other machine resource dependences. |
| 94 | // DataDepOrderType is always NonDataDep. It it not an argument to |
| 95 | // avoid overloading ambiguity with previous constructor. |
| 96 | /*ctor*/ SchedGraphEdge(SchedGraphNode* _src, |
| 97 | SchedGraphNode* _sink, |
| 98 | ResourceId _resourceId, |
| 99 | int _minDelay = -1); |
| 100 | |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 101 | /*dtor*/ ~SchedGraphEdge(); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 102 | |
| 103 | SchedGraphNode* getSrc () const { return src; } |
| 104 | SchedGraphNode* getSink () const { return sink; } |
| 105 | int getMinDelay () const { return minDelay; } |
| 106 | SchedGraphEdgeDepType getDepType () const { return depType; } |
| 107 | |
| 108 | const Value* getValue () const { |
| 109 | assert(depType == DefUseDep || depType == MemoryDep); return val; |
| 110 | } |
| 111 | int getMachineReg () const { |
| 112 | assert(depType == MachineRegister); return machineRegNum; |
| 113 | } |
| 114 | int getResourceId () const { |
| 115 | assert(depType == MachineResource); return resourceId; |
| 116 | } |
| 117 | |
| 118 | public: |
| 119 | // |
| 120 | // Debugging support |
| 121 | // |
| 122 | friend ostream& operator<<(ostream& os, const SchedGraphEdge& edge); |
| 123 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 124 | void dump (int indent=0) const; |
| 125 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 126 | private: |
| 127 | // disable default ctor |
| 128 | /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | |
| 133 | class SchedGraphNode: public NonCopyable { |
| 134 | private: |
| 135 | unsigned int nodeId; |
| 136 | const Instruction* instr; |
| 137 | const MachineInstr* minstr; |
| 138 | vector<SchedGraphEdge*> inEdges; |
| 139 | vector<SchedGraphEdge*> outEdges; |
| 140 | int latency; |
| 141 | |
| 142 | public: |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 143 | typedef vector<SchedGraphEdge*>:: iterator iterator; |
| 144 | typedef vector<SchedGraphEdge*>::const_iterator const_iterator; |
| 145 | typedef vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator; |
| 146 | typedef vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 147 | |
| 148 | public: |
| 149 | // |
| 150 | // Accessor methods |
| 151 | // |
| 152 | unsigned int getNodeId () const { return nodeId; } |
| 153 | const Instruction* getInstr () const { return instr; } |
| 154 | const MachineInstr* getMachineInstr () const { return minstr; } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 155 | int getLatency () const { return latency; } |
| 156 | unsigned int getNumInEdges () const { return inEdges.size(); } |
| 157 | unsigned int getNumOutEdges () const { return outEdges.size(); } |
| 158 | bool isDummyNode () const { return (minstr == NULL); } |
| 159 | |
| 160 | // |
| 161 | // Iterators |
| 162 | // |
| 163 | iterator beginInEdges () { return inEdges.begin(); } |
| 164 | iterator endInEdges () { return inEdges.end(); } |
| 165 | iterator beginOutEdges () { return outEdges.begin(); } |
| 166 | iterator endOutEdges () { return outEdges.end(); } |
| 167 | |
| 168 | const_iterator beginInEdges () const { return inEdges.begin(); } |
| 169 | const_iterator endInEdges () const { return inEdges.end(); } |
| 170 | const_iterator beginOutEdges () const { return outEdges.begin(); } |
| 171 | const_iterator endOutEdges () const { return outEdges.end(); } |
| 172 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 173 | public: |
| 174 | // |
| 175 | // Debugging support |
| 176 | // |
| 177 | friend ostream& operator<<(ostream& os, const SchedGraphNode& node); |
| 178 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 179 | void dump (int indent=0) const; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | friend class SchedGraph; // give access for ctor and dtor |
| 183 | friend class SchedGraphEdge; // give access for adding edges |
| 184 | |
| 185 | void addInEdge (SchedGraphEdge* edge); |
| 186 | void addOutEdge (SchedGraphEdge* edge); |
| 187 | |
| 188 | void removeInEdge (const SchedGraphEdge* edge); |
| 189 | void removeOutEdge (const SchedGraphEdge* edge); |
| 190 | |
| 191 | // disable default constructor and provide a ctor for single-block graphs |
| 192 | /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT |
| 193 | /*ctor*/ SchedGraphNode (unsigned int _nodeId, |
| 194 | const Instruction* _instr, |
| 195 | const MachineInstr* _minstr, |
| 196 | const TargetMachine& _target); |
| 197 | /*dtor*/ ~SchedGraphNode (); |
| 198 | }; |
| 199 | |
| 200 | |
| 201 | |
| 202 | class SchedGraph : |
| 203 | public NonCopyable, |
| 204 | private hash_map<const MachineInstr*, SchedGraphNode*> |
| 205 | { |
| 206 | private: |
| 207 | vector<const BasicBlock*> bbVec; // basic blocks included in the graph |
| 208 | SchedGraphNode* graphRoot; // the root and leaf are not inserted |
| 209 | SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes()) |
| 210 | |
| 211 | public: |
| 212 | typedef hash_map<const MachineInstr*, SchedGraphNode*>::iterator iterator; |
| 213 | typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator; |
| 214 | |
| 215 | public: |
| 216 | // |
| 217 | // Accessor methods |
| 218 | // |
| 219 | const vector<const BasicBlock*>& getBasicBlocks() const { return bbVec; } |
| 220 | const unsigned int getNumNodes() const { return size()+2; } |
| 221 | SchedGraphNode* getRoot() const { return graphRoot; } |
| 222 | SchedGraphNode* getLeaf() const { return graphLeaf; } |
| 223 | |
| 224 | SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const { |
| 225 | const_iterator onePair = this->find(minstr); |
| 226 | return (onePair != this->end())? (*onePair).second : NULL; |
| 227 | } |
| 228 | |
| 229 | // |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 230 | // Delete nodes or edges from the graph. |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 231 | // |
Vikram S. Adve | f0b6d79 | 2001-09-18 12:49:26 +0000 | [diff] [blame] | 232 | void eraseNode (SchedGraphNode* node); |
| 233 | |
| 234 | void eraseIncomingEdges (SchedGraphNode* node, |
| 235 | bool addDummyEdges = true); |
| 236 | |
| 237 | void eraseOutgoingEdges (SchedGraphNode* node, |
| 238 | bool addDummyEdges = true); |
| 239 | |
| 240 | void eraseIncidentEdges (SchedGraphNode* node, |
| 241 | bool addDummyEdges = true); |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 242 | |
| 243 | // |
| 244 | // Unordered iterators. |
| 245 | // Return values is pair<const MachineIntr*,SchedGraphNode*>. |
| 246 | // |
| 247 | iterator begin() { |
| 248 | return hash_map<const MachineInstr*, SchedGraphNode*>::begin(); |
| 249 | } |
| 250 | iterator end() { |
| 251 | return hash_map<const MachineInstr*, SchedGraphNode*>::end(); |
| 252 | } |
| 253 | const_iterator begin() const { |
| 254 | return hash_map<const MachineInstr*, SchedGraphNode*>::begin(); |
| 255 | } |
| 256 | const_iterator end() const { |
| 257 | return hash_map<const MachineInstr*, SchedGraphNode*>::end(); |
| 258 | } |
| 259 | |
| 260 | // |
| 261 | // Ordered iterators. |
| 262 | // Return values is pair<const MachineIntr*,SchedGraphNode*>. |
| 263 | // |
| 264 | // void postord_init(); |
| 265 | // postorder_iterator postord_begin(); |
| 266 | // postorder_iterator postord_end(); |
| 267 | // const_postorder_iterator postord_begin() const; |
| 268 | // const_postorder_iterator postord_end() const; |
| 269 | |
| 270 | // |
| 271 | // Debugging support |
| 272 | // |
| 273 | void dump () const; |
| 274 | |
| 275 | private: |
| 276 | friend class SchedGraphSet; // give access to ctor |
| 277 | |
| 278 | // disable default constructor and provide a ctor for single-block graphs |
| 279 | /*ctor*/ SchedGraph (); // DO NOT IMPLEMENT |
| 280 | /*ctor*/ SchedGraph (const BasicBlock* bb, |
| 281 | const TargetMachine& target); |
| 282 | /*dtor*/ ~SchedGraph (); |
| 283 | |
| 284 | inline void noteGraphNodeForInstr (const MachineInstr* minstr, |
| 285 | SchedGraphNode* node) |
| 286 | { |
| 287 | assert((*this)[minstr] == NULL); |
| 288 | (*this)[minstr] = node; |
| 289 | } |
| 290 | |
| 291 | // |
| 292 | // Graph builder |
| 293 | // |
| 294 | void buildGraph (const TargetMachine& target); |
| 295 | |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 296 | void buildNodesforVMInstr (const TargetMachine& target, |
| 297 | const Instruction* instr); |
| 298 | |
| 299 | void addEdgesForInstruction (const MachineInstr& minstr, |
| 300 | RegToRefVecMap& regToRefVecMap, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 301 | const TargetMachine& target); |
| 302 | |
| 303 | void addCDEdges (const TerminatorInst* term, |
| 304 | const TargetMachine& target); |
| 305 | |
| 306 | void addMemEdges (const vector<const Instruction*>& memVec, |
| 307 | const TargetMachine& target); |
| 308 | |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 309 | void addMachineRegEdges (RegToRefVecMap& regToRefVecMap, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 310 | const TargetMachine& target); |
| 311 | |
| 312 | void addSSAEdge (SchedGraphNode* node, |
Vikram S. Adve | f43e336 | 2001-10-17 23:55:16 +0000 | [diff] [blame^] | 313 | const Instruction* defVMInstr, |
| 314 | const Value* defValue, |
Vikram S. Adve | 4a87b38 | 2001-09-30 23:37:26 +0000 | [diff] [blame] | 315 | const TargetMachine& target); |
| 316 | |
| 317 | void addNonSSAEdgesForValue (const Instruction* instr, |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 318 | const TargetMachine& target); |
| 319 | |
| 320 | void addDummyEdges (); |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | class SchedGraphSet : |
| 325 | public NonCopyable, |
| 326 | private hash_map<const BasicBlock*, SchedGraph*> |
| 327 | { |
| 328 | private: |
| 329 | const Method* method; |
| 330 | |
| 331 | public: |
| 332 | typedef hash_map<const BasicBlock*, SchedGraph*>::iterator iterator; |
| 333 | typedef hash_map<const BasicBlock*, SchedGraph*>::const_iterator const_iterator; |
| 334 | |
| 335 | public: |
| 336 | /*ctor*/ SchedGraphSet (const Method* _method, |
| 337 | const TargetMachine& target); |
| 338 | /*dtor*/ ~SchedGraphSet (); |
| 339 | |
| 340 | // |
| 341 | // Accessors |
| 342 | // |
| 343 | SchedGraph* getGraphForBasicBlock (const BasicBlock* bb) const { |
| 344 | const_iterator onePair = this->find(bb); |
| 345 | return (onePair != this->end())? (*onePair).second : NULL; |
| 346 | } |
| 347 | |
| 348 | // |
| 349 | // Iterators |
| 350 | // |
| 351 | iterator begin() { |
| 352 | return hash_map<const BasicBlock*, SchedGraph*>::begin(); |
| 353 | } |
| 354 | iterator end() { |
| 355 | return hash_map<const BasicBlock*, SchedGraph*>::end(); |
| 356 | } |
| 357 | const_iterator begin() const { |
| 358 | return hash_map<const BasicBlock*, SchedGraph*>::begin(); |
| 359 | } |
| 360 | const_iterator end() const { |
| 361 | return hash_map<const BasicBlock*, SchedGraph*>::end(); |
| 362 | } |
| 363 | |
| 364 | // |
| 365 | // Debugging support |
| 366 | // |
| 367 | void dump () const; |
| 368 | |
| 369 | private: |
| 370 | inline void noteGraphForBlock(const BasicBlock* bb, SchedGraph* graph) { |
| 371 | assert((*this)[bb] == NULL); |
| 372 | (*this)[bb] = graph; |
| 373 | } |
| 374 | |
| 375 | // |
| 376 | // Graph builder |
| 377 | // |
| 378 | void buildGraphsForMethod (const Method *method, |
| 379 | const TargetMachine& target); |
| 380 | }; |
| 381 | |
| 382 | |
| 383 | //********************** Sched Graph Iterators *****************************/ |
| 384 | |
| 385 | // Ok to make it a template because it shd get instantiated at most twice: |
| 386 | // for <SchedGraphNode, SchedGraphNode::iterator> and |
| 387 | // for <const SchedGraphNode, SchedGraphNode::const_iterator>. |
| 388 | // |
| 389 | template <class _NodeType, class _EdgeType, class _EdgeIter> |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 390 | class PredIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> { |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 391 | protected: |
| 392 | _EdgeIter oi; |
| 393 | public: |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 394 | typedef PredIterator<_NodeType, _EdgeType, _EdgeIter> _Self; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 395 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 396 | inline PredIterator(_EdgeIter startEdge) : oi(startEdge) {} |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 397 | |
| 398 | inline bool operator==(const _Self& x) const { return oi == x.oi; } |
| 399 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 400 | |
| 401 | // operator*() differs for pred or succ iterator |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 402 | inline _NodeType* operator*() const { return (*oi)->getSrc(); } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 403 | inline _NodeType* operator->() const { return operator*(); } |
| 404 | |
| 405 | inline _EdgeType* getEdge() const { return *(oi); } |
| 406 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 407 | inline _Self &operator++() { ++oi; return *this; } // Preincrement |
| 408 | inline _Self operator++(int) { // Postincrement |
| 409 | _Self tmp(*this); ++*this; return tmp; |
| 410 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 411 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 412 | inline _Self &operator--() { --oi; return *this; } // Predecrement |
| 413 | inline _Self operator--(int) { // Postdecrement |
| 414 | _Self tmp = *this; --*this; return tmp; |
| 415 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | template <class _NodeType, class _EdgeType, class _EdgeIter> |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 419 | class SuccIterator: public std::bidirectional_iterator<_NodeType, ptrdiff_t> { |
| 420 | protected: |
| 421 | _EdgeIter oi; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 422 | public: |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 423 | typedef SuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 424 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 425 | inline SuccIterator(_EdgeIter startEdge) : oi(startEdge) {} |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 426 | |
Chris Lattner | cffebdc | 2001-09-07 21:07:10 +0000 | [diff] [blame] | 427 | inline bool operator==(const _Self& x) const { return oi == x.oi; } |
| 428 | inline bool operator!=(const _Self& x) const { return !operator==(x); } |
| 429 | |
| 430 | inline _NodeType* operator*() const { return (*oi)->getSink(); } |
| 431 | inline _NodeType* operator->() const { return operator*(); } |
| 432 | |
| 433 | inline _EdgeType* getEdge() const { return *(oi); } |
| 434 | |
| 435 | inline _Self &operator++() { ++oi; return *this; } // Preincrement |
| 436 | inline _Self operator++(int) { // Postincrement |
| 437 | _Self tmp(*this); ++*this; return tmp; |
| 438 | } |
| 439 | |
| 440 | inline _Self &operator--() { --oi; return *this; } // Predecrement |
| 441 | inline _Self operator--(int) { // Postdecrement |
| 442 | _Self tmp = *this; --*this; return tmp; |
| 443 | } |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 444 | }; |
| 445 | |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 446 | // |
| 447 | // sg_pred_iterator |
| 448 | // sg_pred_const_iterator |
| 449 | // |
| 450 | typedef PredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator> |
| 451 | sg_pred_iterator; |
| 452 | typedef PredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator> |
| 453 | sg_pred_const_iterator; |
| 454 | |
| 455 | inline sg_pred_iterator pred_begin( SchedGraphNode *N) { |
| 456 | return sg_pred_iterator(N->beginInEdges()); |
| 457 | } |
| 458 | inline sg_pred_iterator pred_end( SchedGraphNode *N) { |
| 459 | return sg_pred_iterator(N->endInEdges()); |
| 460 | } |
| 461 | inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) { |
| 462 | return sg_pred_const_iterator(N->beginInEdges()); |
| 463 | } |
| 464 | inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) { |
| 465 | return sg_pred_const_iterator(N->endInEdges()); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | // |
| 470 | // sg_succ_iterator |
| 471 | // sg_succ_const_iterator |
| 472 | // |
| 473 | typedef SuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator> |
| 474 | sg_succ_iterator; |
| 475 | typedef SuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator> |
| 476 | sg_succ_const_iterator; |
| 477 | |
| 478 | inline sg_succ_iterator succ_begin( SchedGraphNode *N) { |
| 479 | return sg_succ_iterator(N->beginOutEdges()); |
| 480 | } |
| 481 | inline sg_succ_iterator succ_end( SchedGraphNode *N) { |
| 482 | return sg_succ_iterator(N->endOutEdges()); |
| 483 | } |
| 484 | inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) { |
| 485 | return sg_succ_const_iterator(N->beginOutEdges()); |
| 486 | } |
| 487 | inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) { |
| 488 | return sg_succ_const_iterator(N->endOutEdges()); |
| 489 | } |
| 490 | |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 491 | // Provide specializations of GraphTraits to be able to use graph iterators on |
| 492 | // the scheduling graph! |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 493 | // |
Chris Lattner | 3ff4387 | 2001-09-28 22:56:31 +0000 | [diff] [blame] | 494 | template <> struct GraphTraits<SchedGraph*> { |
| 495 | typedef SchedGraphNode NodeType; |
| 496 | typedef sg_succ_iterator ChildIteratorType; |
| 497 | |
| 498 | static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); } |
| 499 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 500 | return succ_begin(N); |
| 501 | } |
| 502 | static inline ChildIteratorType child_end(NodeType *N) { |
| 503 | return succ_end(N); |
| 504 | } |
| 505 | }; |
| 506 | |
| 507 | template <> struct GraphTraits<const SchedGraph*> { |
| 508 | typedef const SchedGraphNode NodeType; |
| 509 | typedef sg_succ_const_iterator ChildIteratorType; |
| 510 | |
| 511 | static inline NodeType *getEntryNode(const SchedGraph *SG) { |
| 512 | return SG->getRoot(); |
| 513 | } |
| 514 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 515 | return succ_begin(N); |
| 516 | } |
| 517 | static inline ChildIteratorType child_end(NodeType *N) { |
| 518 | return succ_end(N); |
| 519 | } |
| 520 | }; |
Vikram S. Adve | 78ef139 | 2001-08-28 23:06:02 +0000 | [diff] [blame] | 521 | |
| 522 | |
| 523 | //************************ External Functions *****************************/ |
| 524 | |
| 525 | |
| 526 | ostream& operator<<(ostream& os, const SchedGraphEdge& edge); |
| 527 | |
| 528 | ostream& operator<<(ostream& os, const SchedGraphNode& node); |
| 529 | |
| 530 | |
| 531 | /***************************************************************************/ |
| 532 | |
| 533 | #endif |