blob: 2b7e44965b97542c0b6d15abde41eecc75f57ecf [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"
Chris Lattnere5a61cc2003-07-26 23:22:19 +000019#include "Support/hash_map"
Vikram S. Adve78ef1392001-08-28 23:06:02 +000020
21class Value;
22class Instruction;
Chris Lattner3ff43872001-09-28 22:56:31 +000023class TerminatorInst;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000024class BasicBlock;
Chris Lattnere7506a32002-03-23 22:51:58 +000025class Function;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000026class TargetMachine;
27class SchedGraphEdge;
28class SchedGraphNode;
29class SchedGraph;
Vikram S. Adve4a87b382001-09-30 23:37:26 +000030class RegToRefVecMap;
Vikram S. Advec352d2c2001-11-05 04:04:23 +000031class ValueToDefVecMap;
32class RefVec;
Chris Lattnerc0c77082001-09-14 17:55:51 +000033class MachineInstr;
Misha Brukmanfce11432002-10-28 00:28:31 +000034class MachineBasicBlock;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000035
Vikram S. Adve78ef1392001-08-28 23:06:02 +000036
37/******************** Exported Data Types and Constants ********************/
38
39typedef int ResourceId;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000040const ResourceId InvalidRID = -1;
41const ResourceId MachineCCRegsRID = -2; // use +ve numbers for actual regs
42const ResourceId MachineIntRegsRID = -3; // use +ve numbers for actual regs
43const ResourceId MachineFPRegsRID = -4; // use +ve numbers for actual regs
Vikram S. Adve78ef1392001-08-28 23:06:02 +000044
45
46//*********************** Public Class Declarations ************************/
47
Chris Lattner9efc4d62003-06-02 22:45:07 +000048class SchedGraphEdge {
49 SchedGraphEdge(const SchedGraphEdge &); // DO NOT IMPLEMENT
50 void operator=(const SchedGraphEdge &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +000051public:
52 enum SchedGraphEdgeDepType {
Vikram S. Adve200a4352001-11-12 18:53:43 +000053 CtrlDep, MemoryDep, ValueDep, MachineRegister, MachineResource
Vikram S. Adve78ef1392001-08-28 23:06:02 +000054 };
55 enum DataDepOrderType {
Vikram S. Adved0d79c02001-10-28 21:45:02 +000056 TrueDep = 0x1, AntiDep=0x2, OutputDep=0x4, NonDataDep=0x8
Vikram S. Adve78ef1392001-08-28 23:06:02 +000057 };
58
Chris Lattner9efc4d62003-06-02 22:45:07 +000059private:
Vikram S. Adve78ef1392001-08-28 23:06:02 +000060 SchedGraphNode* src;
61 SchedGraphNode* sink;
62 SchedGraphEdgeDepType depType;
Vikram S. Adved0d79c02001-10-28 21:45:02 +000063 unsigned int depOrderType;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000064 int minDelay; // cached latency (assumes fixed target arch)
65
66 union {
Vikram S. Adve4a87b382001-09-30 23:37:26 +000067 const Value* val;
68 int machineRegNum;
69 ResourceId resourceId;
Vikram S. Adve78ef1392001-08-28 23:06:02 +000070 };
71
72public:
73 // For all constructors, if minDelay is unspecified, minDelay is
74 // set to _src->getLatency().
75 // constructor for CtrlDep or MemoryDep edges, selected by 3rd argument
76 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
77 SchedGraphNode* _sink,
78 SchedGraphEdgeDepType _depType,
Vikram S. Adve200a4352001-11-12 18:53:43 +000079 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000080 int _minDelay = -1);
81
Vikram S. Adve200a4352001-11-12 18:53:43 +000082 // constructor for explicit value dependence (may be true/anti/output)
Vikram S. Adve78ef1392001-08-28 23:06:02 +000083 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
84 SchedGraphNode* _sink,
Vikram S. Adve4a87b382001-09-30 23:37:26 +000085 const Value* _val,
Vikram S. Adve200a4352001-11-12 18:53:43 +000086 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000087 int _minDelay = -1);
88
89 // constructor for machine register dependence
90 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
91 SchedGraphNode* _sink,
92 unsigned int _regNum,
Vikram S. Adve200a4352001-11-12 18:53:43 +000093 unsigned int _depOrderType,
Vikram S. Adve78ef1392001-08-28 23:06:02 +000094 int _minDelay = -1);
95
96 // constructor for any other machine resource dependences.
97 // DataDepOrderType is always NonDataDep. It it not an argument to
98 // avoid overloading ambiguity with previous constructor.
99 /*ctor*/ SchedGraphEdge(SchedGraphNode* _src,
100 SchedGraphNode* _sink,
101 ResourceId _resourceId,
102 int _minDelay = -1);
103
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000104 /*dtor*/ ~SchedGraphEdge();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000105
106 SchedGraphNode* getSrc () const { return src; }
107 SchedGraphNode* getSink () const { return sink; }
108 int getMinDelay () const { return minDelay; }
109 SchedGraphEdgeDepType getDepType () const { return depType; }
110
111 const Value* getValue () const {
Vikram S. Adve200a4352001-11-12 18:53:43 +0000112 assert(depType == ValueDep); return val;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000113 }
114 int getMachineReg () const {
115 assert(depType == MachineRegister); return machineRegNum;
116 }
117 int getResourceId () const {
118 assert(depType == MachineResource); return resourceId;
119 }
120
121public:
122 //
123 // Debugging support
124 //
Chris Lattner697954c2002-01-20 22:54:45 +0000125 friend std::ostream& operator<<(std::ostream& os, const SchedGraphEdge& edge);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000126
Chris Lattnercffebdc2001-09-07 21:07:10 +0000127 void dump (int indent=0) const;
128
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000129private:
130 // disable default ctor
131 /*ctor*/ SchedGraphEdge(); // DO NOT IMPLEMENT
132};
133
134
135
Chris Lattner9efc4d62003-06-02 22:45:07 +0000136class SchedGraphNode {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000137 unsigned nodeId;
138 MachineBasicBlock *MBB;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000139 const MachineInstr* minstr;
Chris Lattner697954c2002-01-20 22:54:45 +0000140 std::vector<SchedGraphEdge*> inEdges;
141 std::vector<SchedGraphEdge*> outEdges;
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000142 int origIndexInBB; // original position of machine instr in BB
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000143 int latency;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000144
145 SchedGraphNode(const SchedGraphNode &); // DO NOT IMPLEMENT
146 void operator=(const SchedGraphNode &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000147public:
Chris Lattner697954c2002-01-20 22:54:45 +0000148 typedef std::vector<SchedGraphEdge*>:: iterator iterator;
149 typedef std::vector<SchedGraphEdge*>::const_iterator const_iterator;
150 typedef std::vector<SchedGraphEdge*>:: reverse_iterator reverse_iterator;
151 typedef std::vector<SchedGraphEdge*>::const_reverse_iterator const_reverse_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000152
153public:
154 //
155 // Accessor methods
156 //
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000157 unsigned getNodeId () const { return nodeId; }
158 const MachineInstr* getMachineInstr () const { return minstr; }
159 const MachineOpCode getOpCode () const { return minstr->getOpCode(); }
160 int getLatency () const { return latency; }
161 unsigned getNumInEdges () const { return inEdges.size(); }
162 unsigned getNumOutEdges () const { return outEdges.size(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000163 bool isDummyNode () const { return (minstr == NULL); }
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000164 MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000165 int getOrigIndexInBB() const { return origIndexInBB; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000166
167 //
168 // Iterators
169 //
170 iterator beginInEdges () { return inEdges.begin(); }
171 iterator endInEdges () { return inEdges.end(); }
172 iterator beginOutEdges () { return outEdges.begin(); }
173 iterator endOutEdges () { return outEdges.end(); }
174
175 const_iterator beginInEdges () const { return inEdges.begin(); }
176 const_iterator endInEdges () const { return inEdges.end(); }
177 const_iterator beginOutEdges () const { return outEdges.begin(); }
178 const_iterator endOutEdges () const { return outEdges.end(); }
179
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000180public:
181 //
182 // Debugging support
183 //
Chris Lattner697954c2002-01-20 22:54:45 +0000184 friend std::ostream& operator<<(std::ostream& os, const SchedGraphNode& node);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000185
Chris Lattnercffebdc2001-09-07 21:07:10 +0000186 void dump (int indent=0) const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000187
188private:
189 friend class SchedGraph; // give access for ctor and dtor
190 friend class SchedGraphEdge; // give access for adding edges
191
192 void addInEdge (SchedGraphEdge* edge);
193 void addOutEdge (SchedGraphEdge* edge);
194
195 void removeInEdge (const SchedGraphEdge* edge);
196 void removeOutEdge (const SchedGraphEdge* edge);
197
198 // disable default constructor and provide a ctor for single-block graphs
199 /*ctor*/ SchedGraphNode(); // DO NOT IMPLEMENT
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000200 /*ctor*/ SchedGraphNode (unsigned nodeId,
201 MachineBasicBlock *mbb,
Vikram S. Adve5b43af92001-11-11 01:23:27 +0000202 int indexInBB,
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000203 const TargetMachine& Target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000204 /*dtor*/ ~SchedGraphNode ();
205};
206
207
208
Chris Lattner9efc4d62003-06-02 22:45:07 +0000209class SchedGraph : private hash_map<const MachineInstr*, SchedGraphNode*> {
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000210 MachineBasicBlock &MBB; // basic blocks for this graph
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000211 SchedGraphNode* graphRoot; // the root and leaf are not inserted
212 SchedGraphNode* graphLeaf; // in the hash_map (see getNumNodes())
213
Chris Lattner09ff1122002-07-24 21:21:32 +0000214 typedef hash_map<const MachineInstr*, SchedGraphNode*> map_base;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000215 SchedGraph(const SchedGraph &); // DO NOT IMPLEMENT
216 void operator=(const SchedGraph &); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000217public:
Chris Lattner697954c2002-01-20 22:54:45 +0000218 using map_base::iterator;
219 using map_base::const_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000220
221public:
222 //
223 // Accessor methods
224 //
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000225 MachineBasicBlock &getBasicBlock() const { return MBB; }
226 unsigned getNumNodes() const { return size()+2; }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000227 SchedGraphNode* getRoot() const { return graphRoot; }
228 SchedGraphNode* getLeaf() const { return graphLeaf; }
229
230 SchedGraphNode* getGraphNodeForInstr(const MachineInstr* minstr) const {
231 const_iterator onePair = this->find(minstr);
232 return (onePair != this->end())? (*onePair).second : NULL;
233 }
234
235 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000236 // Delete nodes or edges from the graph.
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000237 //
Vikram S. Advef0b6d792001-09-18 12:49:26 +0000238 void eraseNode (SchedGraphNode* node);
239
240 void eraseIncomingEdges (SchedGraphNode* node,
241 bool addDummyEdges = true);
242
243 void eraseOutgoingEdges (SchedGraphNode* node,
244 bool addDummyEdges = true);
245
246 void eraseIncidentEdges (SchedGraphNode* node,
247 bool addDummyEdges = true);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000248
249 //
250 // Unordered iterators.
251 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
252 //
Chris Lattner697954c2002-01-20 22:54:45 +0000253 using map_base::begin;
254 using map_base::end;
255
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000256 //
257 // Ordered iterators.
258 // Return values is pair<const MachineIntr*,SchedGraphNode*>.
259 //
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;
265
266 //
267 // Debugging support
268 //
269 void dump () const;
270
271private:
272 friend class SchedGraphSet; // give access to ctor
273
274 // disable default constructor and provide a ctor for single-block graphs
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000275 /*ctor*/ SchedGraph (MachineBasicBlock &bb,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000276 const TargetMachine& target);
277 /*dtor*/ ~SchedGraph ();
278
279 inline void noteGraphNodeForInstr (const MachineInstr* minstr,
280 SchedGraphNode* node)
281 {
282 assert((*this)[minstr] == NULL);
283 (*this)[minstr] = node;
284 }
285
286 //
287 // Graph builder
288 //
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000289 void buildGraph (const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000290
Chris Lattnerfb3a0aed2002-10-28 18:50:08 +0000291 void buildNodesForBB (const TargetMachine& target,
292 MachineBasicBlock &MBB,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000293 std::vector<SchedGraphNode*>& memNV,
294 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000295 RegToRefVecMap& regToRefVecMap,
296 ValueToDefVecMap& valueToDefVecMap);
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000297
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000298 void findDefUseInfoAtInstr (const TargetMachine& target,
299 SchedGraphNode* node,
Vikram S. Adve7952d602003-05-31 07:37:05 +0000300 std::vector<SchedGraphNode*>& memNV,
301 std::vector<SchedGraphNode*>& callNV,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000302 RegToRefVecMap& regToRefVecMap,
303 ValueToDefVecMap& valueToDefVecMap);
304
Vikram S. Advee64574c2001-11-08 05:20:23 +0000305 void addEdgesForInstruction(const MachineInstr& minstr,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000306 const ValueToDefVecMap& valueToDefVecMap,
307 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000308
309 void addCDEdges (const TerminatorInst* term,
310 const TargetMachine& target);
311
Vikram S. Adve7952d602003-05-31 07:37:05 +0000312 void addMemEdges (const std::vector<SchedGraphNode*>& memNV,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000313 const TargetMachine& target);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000314
Vikram S. Adve7952d602003-05-31 07:37:05 +0000315 void addCallDepEdges (const std::vector<SchedGraphNode*>& callNV,
Vikram S. Advee64574c2001-11-08 05:20:23 +0000316 const TargetMachine& target);
Vikram S. Adved0d79c02001-10-28 21:45:02 +0000317
Vikram S. Adve4a87b382001-09-30 23:37:26 +0000318 void addMachineRegEdges (RegToRefVecMap& regToRefVecMap,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000319 const TargetMachine& target);
320
Vikram S. Adve200a4352001-11-12 18:53:43 +0000321 void addEdgesForValue (SchedGraphNode* refNode,
Vikram S. Advec352d2c2001-11-05 04:04:23 +0000322 const RefVec& defVec,
Vikram S. Advef43e3362001-10-17 23:55:16 +0000323 const Value* defValue,
Vikram S. Adve200a4352001-11-12 18:53:43 +0000324 bool refNodeIsDef,
Vikram S. Adve0baf1c02002-07-08 22:59:23 +0000325 bool refNodeIsDefAndUse,
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000326 const TargetMachine& target);
327
328 void addDummyEdges ();
329};
330
331
Chris Lattner9efc4d62003-06-02 22:45:07 +0000332class SchedGraphSet : private std::vector<SchedGraph*> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000333private:
Chris Lattnere7506a32002-03-23 22:51:58 +0000334 const Function* method;
Chris Lattner9efc4d62003-06-02 22:45:07 +0000335
336 SchedGraphSet(const SchedGraphSet&); // DO NOT IMPLEMENT
337 void operator=(const SchedGraphSet&); // DO NOT IMPLEMENT
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000338public:
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000339 typedef std::vector<SchedGraph*> baseVector;
340 using baseVector::iterator;
341 using baseVector::const_iterator;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000342
343public:
Chris Lattner9efc4d62003-06-02 22:45:07 +0000344 SchedGraphSet(const Function *F, const TargetMachine &TM);
345 ~SchedGraphSet();
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000346
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000347 // Iterators
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000348 using baseVector::begin;
349 using baseVector::end;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000350
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000351 // Debugging support
Chris Lattner9efc4d62003-06-02 22:45:07 +0000352 void dump() const;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000353
354private:
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000355 inline void addGraph(SchedGraph* graph) {
356 assert(graph != NULL);
357 this->push_back(graph);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000358 }
Vikram S. Adve97fb99b2002-03-24 03:53:03 +0000359
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000360 // Graph builder
Chris Lattner9efc4d62003-06-02 22:45:07 +0000361 void buildGraphsForMethod(const Function *F, const TargetMachine &TM);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000362};
363
364
365//********************** Sched Graph Iterators *****************************/
366
367// Ok to make it a template because it shd get instantiated at most twice:
368// for <SchedGraphNode, SchedGraphNode::iterator> and
369// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
370//
371template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000372class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000373protected:
374 _EdgeIter oi;
375public:
Chris Lattner455889a2002-02-12 22:39:50 +0000376 typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000377
Chris Lattner455889a2002-02-12 22:39:50 +0000378 inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000379
380 inline bool operator==(const _Self& x) const { return oi == x.oi; }
381 inline bool operator!=(const _Self& x) const { return !operator==(x); }
382
383 // operator*() differs for pred or succ iterator
Chris Lattnercffebdc2001-09-07 21:07:10 +0000384 inline _NodeType* operator*() const { return (*oi)->getSrc(); }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000385 inline _NodeType* operator->() const { return operator*(); }
386
387 inline _EdgeType* getEdge() const { return *(oi); }
388
Chris Lattnercffebdc2001-09-07 21:07:10 +0000389 inline _Self &operator++() { ++oi; return *this; } // Preincrement
390 inline _Self operator++(int) { // Postincrement
391 _Self tmp(*this); ++*this; return tmp;
392 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000393
Chris Lattnercffebdc2001-09-07 21:07:10 +0000394 inline _Self &operator--() { --oi; return *this; } // Predecrement
395 inline _Self operator--(int) { // Postdecrement
396 _Self tmp = *this; --*this; return tmp;
397 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000398};
399
400template <class _NodeType, class _EdgeType, class _EdgeIter>
Chris Lattner0c0edf82002-07-25 06:17:51 +0000401class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
Chris Lattnercffebdc2001-09-07 21:07:10 +0000402protected:
403 _EdgeIter oi;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000404public:
Chris Lattner455889a2002-02-12 22:39:50 +0000405 typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000406
Chris Lattner455889a2002-02-12 22:39:50 +0000407 inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000408
Chris Lattnercffebdc2001-09-07 21:07:10 +0000409 inline bool operator==(const _Self& x) const { return oi == x.oi; }
410 inline bool operator!=(const _Self& x) const { return !operator==(x); }
411
412 inline _NodeType* operator*() const { return (*oi)->getSink(); }
413 inline _NodeType* operator->() const { return operator*(); }
414
415 inline _EdgeType* getEdge() const { return *(oi); }
416
417 inline _Self &operator++() { ++oi; return *this; } // Preincrement
418 inline _Self operator++(int) { // Postincrement
419 _Self tmp(*this); ++*this; return tmp;
420 }
421
422 inline _Self &operator--() { --oi; return *this; } // Predecrement
423 inline _Self operator--(int) { // Postdecrement
424 _Self tmp = *this; --*this; return tmp;
425 }
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000426};
427
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000428//
429// sg_pred_iterator
430// sg_pred_const_iterator
431//
Chris Lattner455889a2002-02-12 22:39:50 +0000432typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000433 sg_pred_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000434typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000435 sg_pred_const_iterator;
436
437inline sg_pred_iterator pred_begin( SchedGraphNode *N) {
438 return sg_pred_iterator(N->beginInEdges());
439}
440inline sg_pred_iterator pred_end( SchedGraphNode *N) {
441 return sg_pred_iterator(N->endInEdges());
442}
443inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
444 return sg_pred_const_iterator(N->beginInEdges());
445}
446inline sg_pred_const_iterator pred_end( const SchedGraphNode *N) {
447 return sg_pred_const_iterator(N->endInEdges());
448}
449
450
451//
452// sg_succ_iterator
453// sg_succ_const_iterator
454//
Chris Lattner455889a2002-02-12 22:39:50 +0000455typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000456 sg_succ_iterator;
Chris Lattner455889a2002-02-12 22:39:50 +0000457typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000458 sg_succ_const_iterator;
459
460inline sg_succ_iterator succ_begin( SchedGraphNode *N) {
461 return sg_succ_iterator(N->beginOutEdges());
462}
463inline sg_succ_iterator succ_end( SchedGraphNode *N) {
464 return sg_succ_iterator(N->endOutEdges());
465}
466inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
467 return sg_succ_const_iterator(N->beginOutEdges());
468}
469inline sg_succ_const_iterator succ_end( const SchedGraphNode *N) {
470 return sg_succ_const_iterator(N->endOutEdges());
471}
472
Chris Lattner3ff43872001-09-28 22:56:31 +0000473// Provide specializations of GraphTraits to be able to use graph iterators on
474// the scheduling graph!
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000475//
Chris Lattner3ff43872001-09-28 22:56:31 +0000476template <> struct GraphTraits<SchedGraph*> {
477 typedef SchedGraphNode NodeType;
478 typedef sg_succ_iterator ChildIteratorType;
479
480 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
481 static inline ChildIteratorType child_begin(NodeType *N) {
482 return succ_begin(N);
483 }
484 static inline ChildIteratorType child_end(NodeType *N) {
485 return succ_end(N);
486 }
487};
488
489template <> struct GraphTraits<const SchedGraph*> {
490 typedef const SchedGraphNode NodeType;
491 typedef sg_succ_const_iterator ChildIteratorType;
492
493 static inline NodeType *getEntryNode(const SchedGraph *SG) {
494 return SG->getRoot();
495 }
496 static inline ChildIteratorType child_begin(NodeType *N) {
497 return succ_begin(N);
498 }
499 static inline ChildIteratorType child_end(NodeType *N) {
500 return succ_end(N);
501 }
502};
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000503
504
Chris Lattner697954c2002-01-20 22:54:45 +0000505std::ostream &operator<<(std::ostream& os, const SchedGraphEdge& edge);
506std::ostream &operator<<(std::ostream &os, const SchedGraphNode& node);
Vikram S. Adve78ef1392001-08-28 23:06:02 +0000507
508#endif