blob: a1e85c5acb7629d9df2dc48b907f5df0ba563288 [file] [log] [blame]
Chris Lattner179cdfb2002-08-09 20:08:03 +00001//===-- SchedGraph.h - Scheduling Graph --------------------------*- C++ -*--=//
2//
3// Purpose:
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).
7//
8// Strategy:
9// This graph tries to leverage the SSA graph as much as possible,
10// but captures the extra dependences through a common interface.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Adve78ef1392001-08-28 23:06:02 +000013
14#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
15#define LLVM_CODEGEN_SCHEDGRAPH_H
16
Vikram S. Advee64574c2001-11-08 05:20:23 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/GraphTraits.h"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000019
20class Value;
21class Instruction;
Chris Lattner3ff43872001-09-28 22:56:31 +000022class TerminatorInst;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000023class BasicBlock;
Chris Lattnere7506a32002-03-23 22:51:58 +000024class Function;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000025class TargetMachine;
26class SchedGraphEdge;
27class SchedGraphNode;
28class SchedGraph;
Vikram S. Adve4a87b382001-09-30 23:37:26 +000029class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000030class ValueToDefVecMap;
31class RefVec;
Chris Lattnerc0c77082001-09-14 17:55:51 +000032class MachineInstr;
Misha Brukmanfce11432002-10-28 00:28:31 +000033class MachineBasicBlock;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000034
Vikram S. Adve78ef1392001-08-28 23:06:02 +000035
36/******************** Exported Data Types and Constants ********************/
37
38typedef int ResourceId;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000039const ResourceId InvalidRID = -1;
40const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
41const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
42const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
Vikram S. Adve78ef1392001-08-28 23:06:02 +000043
44
45//*********************** Public Class Declarations ************************/
46
Chris Lattner9efc4d62003-06-02 22:45:07 +000047class SchedGraphEdge {
48 SchedGraphEdge(const SchedGraphEdge &); // DO NOT IMPLEMENT
49 void operator=(const SchedGraphEdge &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +000050public:
51 enum SchedGraphEdgeDepType {
Vikram S. Adve200a4352001-11-12 18:53:43 +000052 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
Vikram S. Adve78ef1392001-08-28 23:06:02 +000053 };
54 enum DataDepOrderType {
Vikram S. Adved0d79c02001-10-28 21:45:02 +000055 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
Vikram S. Adve78ef1392001-08-28 23:06:02 +000056 };
57
Chris Lattner9efc4d62003-06-02 22:45:07 +000058private:
Vikram S. Adve78ef1392001-08-28 23:06:02 +000059 SchedGraphNode* src;
60 SchedGraphNode* sink;
61 SchedGraphEdgeDepType depType;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000062 unsigned int depOrderType;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000063 int minDelay; // cached latency (assumes fixed target arch)
64
65 union {
Vikram S. Adve4a87b382001-09-30 23:37:26 +000066 const Value* val;
67 int machineRegNum;
68 ResourceId resourceId;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000069 };
70
71public:
72 // For all constructors, if minDelay is unspecified, minDelay is
73 // set to _src->getLatency().
74 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
75 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
76 SchedGraphNode* _sink,
77 SchedGraphEdgeDepType _depType,
Vikram S. Adve200a4352001-11-12 18:53:43 +000078 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000079 int _minDelay = -1);
80
Vikram S. Adve200a4352001-11-12 18:53:43 +000081 // constructor for explicit value dependence (may be true/anti/output)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000082 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
83 SchedGraphNode* _sink,
Vikram S. Adve4a87b382001-09-30 23:37:26 +000084 const Value* _val,
Vikram S. Adve200a4352001-11-12 18:53:43 +000085 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000086 int _minDelay = -1);
87
88 // constructor for machine register dependence
89 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
90 SchedGraphNode* _sink,
91 unsigned int _regNum,
Vikram S. Adve200a4352001-11-12 18:53:43 +000092 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000093 int _minDelay = -1);
94
95 // constructor for any other machine resource dependences.
96 // DataDepOrderType is always NonDataDep. It it not an argument to
97 // avoid overloading ambiguity with previous constructor.
98 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
99 SchedGraphNode* _sink,
100 ResourceId _resourceId,
101 int _minDelay = -1);
102
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000103 /*dtor*/ ~SchedGraphEdge();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000104
105 SchedGraphNode* getSrc () const { return src; }
106 SchedGraphNode* getSink () const { return sink; }
107 int getMinDelay () const { return minDelay; }
108 SchedGraphEdgeDepType getDepType () const { return depType; }
109
110 const Value* getValue () const {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000111 assert(depType == ValueDep); return val;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000112 }
113 int getMachineReg () const {
114 assert(depType == MachineRegister); return machineRegNum;
115 }
116 int getResourceId () const {
117 assert(depType == MachineResource); return resourceId;
118 }
119
120public:
121 //
122 // Debugging support
123 //
Chris Lattner697954c2002-01-20 22:54:45 +0000124 friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000125
Chris Lattnercffebdc2001-09-07 21:07:10 +0000126 void dump (int indent=0) const;
127
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000128private:
129 // disable default ctor
130 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
131};
132
133
134
Chris Lattner9efc4d62003-06-02 22:45:07 +0000135class SchedGraphNode {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000136 unsigned nodeId;
137 MachineBasicBlock *MBB;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000138 const MachineInstr* minstr;
Chris Lattner697954c2002-01-20 22:54:45 +0000139 std::vector<SchedGraphEdge*> inEdges;
140 std::vector<SchedGraphEdge*> outEdges;
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000141 int origIndexInBB; // original position of machine instr in BB
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000142 int latency;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000143
144 SchedGraphNode(const SchedGraphNode &); // DO NOT IMPLEMENT
145 void operator=(const SchedGraphNode &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000146public:
Chris Lattner697954c2002-01-20 22:54:45 +0000147 typedef std::vector<SchedGraphEdge*>:: iterator iterator;
148 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
149 typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
150 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000151
152public:
153 //
154 // Accessor methods
155 //
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000156 unsigned getNodeId () const { return nodeId; }
157 const MachineInstr* getMachineInstr () const { return minstr; }
158 const MachineOpCode getOpCode () const { return minstr->getOpCode(); }
159 int getLatency () const { return latency; }
160 unsigned getNumInEdges () const { return inEdges.size(); }
161 unsigned getNumOutEdges () const { return outEdges.size(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000162 bool isDummyNode () const { return (minstr == NULL); }
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000163 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000164 int getOrigIndexInBB() const { return origIndexInBB; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000165
166 //
167 // Iterators
168 //
169 iterator beginInEdges () { return inEdges.begin(); }
170 iterator endInEdges () { return inEdges.end(); }
171 iterator beginOutEdges () { return outEdges.begin(); }
172 iterator endOutEdges () { return outEdges.end(); }
173
174 const_iterator beginInEdges () const { return inEdges.begin(); }
175 const_iterator endInEdges () const { return inEdges.end(); }
176 const_iterator beginOutEdges () const { return outEdges.begin(); }
177 const_iterator endOutEdges () const { return outEdges.end(); }
178
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000179public:
180 //
181 // Debugging support
182 //
Chris Lattner697954c2002-01-20 22:54:45 +0000183 friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000184
Chris Lattnercffebdc2001-09-07 21:07:10 +0000185 void dump (int indent=0) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000186
187private:
188 friend class SchedGraph; // give access for ctor and dtor
189 friend class SchedGraphEdge; // give access for adding edges
190
191 void addInEdge (SchedGraphEdge* edge);
192 void addOutEdge (SchedGraphEdge* edge);
193
194 void removeInEdge (const SchedGraphEdge* edge);
195 void removeOutEdge (const SchedGraphEdge* edge);
196
197 // disable default constructor and provide a ctor for single-block graphs
198 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000199 /*ctor*/ SchedGraphNode (unsigned nodeId,
200 MachineBasicBlock *mbb,
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000201 int indexInBB,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000202 const TargetMachine& Target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000203 /*dtor*/ ~SchedGraphNode ();
204};
205
206
207
Chris Lattner9efc4d62003-06-02 22:45:07 +0000208class SchedGraph : private hash_map<const MachineInstr*, SchedGraphNode*> {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000209 MachineBasicBlock &MBB; // basic blocks for this graph
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000210 SchedGraphNode* graphRoot; // the root and leaf are not inserted
211 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
212
Chris Lattner09ff1122002-07-24 21:21:32 +0000213 typedef hash_map<const MachineInstr*, SchedGraphNode*> map_base;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000214 SchedGraph(const SchedGraph &); // DO NOT IMPLEMENT
215 void operator=(const SchedGraph &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000216public:
Chris Lattner697954c2002-01-20 22:54:45 +0000217 using map_base::iterator;
218 using map_base::const_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000219
220public:
221 //
222 // Accessor methods
223 //
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000224 MachineBasicBlock &getBasicBlock() const { return MBB; }
225 unsigned getNumNodes() const { return size()+2; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000226 SchedGraphNode* getRoot() const { return graphRoot; }
227 SchedGraphNode* getLeaf() const { return graphLeaf; }
228
229 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
230 const_iterator onePair = this->find(minstr);
231 return (onePair != this->end())? (*onePair).second : NULL;
232 }
233
234 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000235 // Delete nodes or edges from the graph.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000236 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000237 void eraseNode (SchedGraphNode* node);
238
239 void eraseIncomingEdges (SchedGraphNode* node,
240 bool addDummyEdges = true);
241
242 void eraseOutgoingEdges (SchedGraphNode* node,
243 bool addDummyEdges = true);
244
245 void eraseIncidentEdges (SchedGraphNode* node,
246 bool addDummyEdges = true);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000247
248 //
249 // Unordered iterators.
250 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
251 //
Chris Lattner697954c2002-01-20 22:54:45 +0000252 using map_base::begin;
253 using map_base::end;
254
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000255 //
256 // Ordered iterators.
257 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
258 //
259 // void postord_init();
260 // postorder_iterator postord_begin();
261 // postorder_iterator postord_end();
262 // const_postorder_iterator postord_begin() const;
263 // const_postorder_iterator postord_end() const;
264
265 //
266 // Debugging support
267 //
268 void dump () const;
269
270private:
271 friend class SchedGraphSet; // give access to ctor
272
273 // disable default constructor and provide a ctor for single-block graphs
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000274 /*ctor*/ SchedGraph (MachineBasicBlock &bb,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000275 const TargetMachine& target);
276 /*dtor*/ ~SchedGraph ();
277
278 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
279 SchedGraphNode* node)
280 {
281 assert((*this)[minstr] == NULL);
282 (*this)[minstr] = node;
283 }
284
285 //
286 // Graph builder
287 //
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000288 void buildGraph (const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000289
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000290 void buildNodesForBB (const TargetMachine& target,
291 MachineBasicBlock &MBB,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000292 std::vector<SchedGraphNode*>& memNV,
293 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000294 RegToRefVecMap& regToRefVecMap,
295 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000296
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000297 void findDefUseInfoAtInstr (const TargetMachine& target,
298 SchedGraphNode* node,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000299 std::vector<SchedGraphNode*>& memNV,
300 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000301 RegToRefVecMap& regToRefVecMap,
302 ValueToDefVecMap& valueToDefVecMap);
303
Vikram S. Advee64574c2001-11-08 05:20:23 +0000304 void addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000305 const ValueToDefVecMap& valueToDefVecMap,
306 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000307
308 void addCDEdges (const TerminatorInst* term,
309 const TargetMachine& target);
310
Vikram S. Adve7952d602003-05-31 07:37:05 +0000311 void addMemEdges (const std::vector<SchedGraphNode*>& memNV,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000312 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000313
Vikram S. Adve7952d602003-05-31 07:37:05 +0000314 void addCallDepEdges (const std::vector<SchedGraphNode*>& callNV,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000315 const TargetMachine& target);
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000316
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000317 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000318 const TargetMachine& target);
319
Vikram S. Adve200a4352001-11-12 18:53:43 +0000320 void addEdgesForValue (SchedGraphNode* refNode,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000321 const RefVec& defVec,
Vikram S. Advef43e3362001-10-17 23:55:16 +0000322 const Value* defValue,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000323 bool refNodeIsDef,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000324 bool refNodeIsDefAndUse,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000325 const TargetMachine& target);
326
327 void addDummyEdges ();
328};
329
330
Chris Lattner9efc4d62003-06-02 22:45:07 +0000331class SchedGraphSet : private std::vector<SchedGraph*> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000332private:
Chris Lattnere7506a32002-03-23 22:51:58 +0000333 const Function* method;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000334
335 SchedGraphSet(const SchedGraphSet&); // DO NOT IMPLEMENT
336 void operator=(const SchedGraphSet&); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000337public:
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000338 typedef std::vector<SchedGraph*> baseVector;
339 using baseVector::iterator;
340 using baseVector::const_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000341
342public:
Chris Lattner9efc4d62003-06-02 22:45:07 +0000343 SchedGraphSet(const Function *F, const TargetMachine &TM);
344 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000345
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000346 // Iterators
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000347 using baseVector::begin;
348 using baseVector::end;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000349
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000350 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000351 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000352
353private:
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000354 inline void addGraph(SchedGraph* graph) {
355 assert(graph != NULL);
356 this->push_back(graph);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000357 }
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000358
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000359 // Graph builder
Chris Lattner9efc4d62003-06-02 22:45:07 +0000360 void buildGraphsForMethod(const Function *F, const TargetMachine &TM);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000361};
362
363
364//********************** Sched Graph Iterators *****************************/
365
366// Ok to make it a template because it shd get instantiated at most twice:
367// for <SchedGraphNode, SchedGraphNode::iterator> and
368// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
369//
370template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000371class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000372protected:
373 _EdgeIter oi;
374public:
Chris Lattner455889a2002-02-12 22:39:50 +0000375 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000376
Chris Lattner455889a2002-02-12 22:39:50 +0000377 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000378
379 inline bool operator==(const _Self& x) const { return oi == x.oi; }
380 inline bool operator!=(const _Self& x) const { return !operator==(x); }
381
382 // operator*() differs for pred or succ iterator
Chris Lattnercffebdc2001-09-07 21:07:10 +0000383 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000384 inline _NodeType* operator->() const { return operator*(); }
385
386 inline _EdgeType* getEdge() const { return *(oi); }
387
Chris Lattnercffebdc2001-09-07 21:07:10 +0000388 inline _Self &operator++() { ++oi; return *this; } // Preincrement
389 inline _Self operator++(int) { // Postincrement
390 _Self tmp(*this); ++*this; return tmp;
391 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000392
Chris Lattnercffebdc2001-09-07 21:07:10 +0000393 inline _Self &operator--() { --oi; return *this; } // Predecrement
394 inline _Self operator--(int) { // Postdecrement
395 _Self tmp = *this; --*this; return tmp;
396 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000397};
398
399template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000400class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000401protected:
402 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000403public:
Chris Lattner455889a2002-02-12 22:39:50 +0000404 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000405
Chris Lattner455889a2002-02-12 22:39:50 +0000406 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000407
Chris Lattnercffebdc2001-09-07 21:07:10 +0000408 inline bool operator==(const _Self& x) const { return oi == x.oi; }
409 inline bool operator!=(const _Self& x) const { return !operator==(x); }
410
411 inline _NodeType* operator*() const { return (*oi)->getSink(); }
412 inline _NodeType* operator->() const { return operator*(); }
413
414 inline _EdgeType* getEdge() const { return *(oi); }
415
416 inline _Self &operator++() { ++oi; return *this; } // Preincrement
417 inline _Self operator++(int) { // Postincrement
418 _Self tmp(*this); ++*this; return tmp;
419 }
420
421 inline _Self &operator--() { --oi; return *this; } // Predecrement
422 inline _Self operator--(int) { // Postdecrement
423 _Self tmp = *this; --*this; return tmp;
424 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000425};
426
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000427//
428// sg_pred_iterator
429// sg_pred_const_iterator
430//
Chris Lattner455889a2002-02-12 22:39:50 +0000431typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000432 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000433typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000434 sg_pred_const_iterator;
435
436inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
437 return sg_pred_iterator(N->beginInEdges());
438}
439inline sg_pred_iterator pred_end( SchedGraphNode *N) {
440 return sg_pred_iterator(N->endInEdges());
441}
442inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
443 return sg_pred_const_iterator(N->beginInEdges());
444}
445inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
446 return sg_pred_const_iterator(N->endInEdges());
447}
448
449
450//
451// sg_succ_iterator
452// sg_succ_const_iterator
453//
Chris Lattner455889a2002-02-12 22:39:50 +0000454typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000455 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000456typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000457 sg_succ_const_iterator;
458
459inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
460 return sg_succ_iterator(N->beginOutEdges());
461}
462inline sg_succ_iterator succ_end( SchedGraphNode *N) {
463 return sg_succ_iterator(N->endOutEdges());
464}
465inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
466 return sg_succ_const_iterator(N->beginOutEdges());
467}
468inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
469 return sg_succ_const_iterator(N->endOutEdges());
470}
471
Chris Lattner3ff43872001-09-28 22:56:31 +0000472// Provide specializations of GraphTraits to be able to use graph iterators on
473// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000474//
Chris Lattner3ff43872001-09-28 22:56:31 +0000475template <> struct GraphTraits<SchedGraph*> {
476 typedef SchedGraphNode NodeType;
477 typedef sg_succ_iterator ChildIteratorType;
478
479 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
480 static inline ChildIteratorType child_begin(NodeType *N) {
481 return succ_begin(N);
482 }
483 static inline ChildIteratorType child_end(NodeType *N) {
484 return succ_end(N);
485 }
486};
487
488template <> struct GraphTraits<const SchedGraph*> {
489 typedef const SchedGraphNode NodeType;
490 typedef sg_succ_const_iterator ChildIteratorType;
491
492 static inline NodeType *getEntryNode(const SchedGraph *SG) {
493 return SG->getRoot();
494 }
495 static inline ChildIteratorType child_begin(NodeType *N) {
496 return succ_begin(N);
497 }
498 static inline ChildIteratorType child_end(NodeType *N) {
499 return succ_end(N);
500 }
501};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000502
503
Chris Lattner697954c2002-01-20 22:54:45 +0000504std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
505std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000506
507#endif