First version of SchedGraph common class and refactoring of SchedGraph.
[oota-llvm.git] / lib / CodeGen / InstrSched / SchedGraphCommon.cpp
1 #include "llvm/CodeGen/SchedGraphCommon.h"
2 #include "Support/STLExtras.h"
3
4 class SchedGraphCommon;
5
6 // 
7 // class SchedGraphEdge
8 // 
9
10 /*ctor*/
11 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
12                                SchedGraphNodeCommon* _sink,
13                                SchedGraphEdgeDepType _depType,
14                                unsigned int     _depOrderType,
15                                int _minDelay)
16   : src(_src),
17     sink(_sink),
18     depType(_depType),
19     depOrderType(_depOrderType),
20     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
21     val(NULL)
22 {
23   iteDiff=0;
24   assert(src != sink && "Self-loop in scheduling graph!");
25   src->addOutEdge(this);
26   sink->addInEdge(this);
27 }
28
29
30 /*ctor*/
31 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon*  _src,
32                                SchedGraphNodeCommon*  _sink,
33                                const Value*     _val,
34                                unsigned int     _depOrderType,
35                                int              _minDelay)
36   : src(_src),
37     sink(_sink),
38     depType(ValueDep),
39     depOrderType(_depOrderType),
40     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
41     val(_val)
42 {
43   iteDiff=0;
44   assert(src != sink && "Self-loop in scheduling graph!");
45   src->addOutEdge(this);
46   sink->addInEdge(this);
47 }
48
49
50 /*ctor*/
51 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon*  _src,
52                                SchedGraphNodeCommon*  _sink,
53                                unsigned int     _regNum,
54                                unsigned int     _depOrderType,
55                                int             _minDelay)
56   : src(_src),
57     sink(_sink),
58     depType(MachineRegister),
59     depOrderType(_depOrderType),
60     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
61     machineRegNum(_regNum)
62 {
63   iteDiff=0;
64   assert(src != sink && "Self-loop in scheduling graph!");
65   src->addOutEdge(this);
66   sink->addInEdge(this);
67 }
68
69
70 /*ctor*/
71 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
72                                SchedGraphNodeCommon* _sink,
73                                ResourceId      _resourceId,
74                                int             _minDelay)
75   : src(_src),
76     sink(_sink),
77     depType(MachineResource),
78     depOrderType(NonDataDep),
79     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
80     resourceId(_resourceId)
81 {
82   iteDiff=0;
83   assert(src != sink && "Self-loop in scheduling graph!");
84   src->addOutEdge(this);
85   sink->addInEdge(this);
86 }
87
88 /*dtor*/
89 SchedGraphEdge::~SchedGraphEdge()
90 {
91 }
92
93 void SchedGraphEdge::dump(int indent) const {
94   std::cerr << std::string(indent*2, ' ') << *this; 
95 }
96
97
98
99 /*ctor*/           
100
101 SchedGraphNodeCommon::SchedGraphNodeCommon(unsigned  _nodeId)
102   :ID(_nodeId),
103    latency(0){
104   
105 }
106
107 /*dtor*/
108 SchedGraphNodeCommon::~SchedGraphNodeCommon()
109 {
110   // for each node, delete its out-edges
111   std::for_each(beginOutEdges(), endOutEdges(),
112                 deleter<SchedGraphEdge>);
113 }
114
115
116 void SchedGraphNodeCommon::dump(int indent) const {
117   std::cerr << std::string(indent*2, ' ') << *this; 
118 }
119
120
121 inline void
122 SchedGraphNodeCommon::addInEdge(SchedGraphEdge* edge)
123 {
124   inEdges.push_back(edge);
125 }
126
127
128 inline void
129 SchedGraphNodeCommon::addOutEdge(SchedGraphEdge* edge)
130 {
131   outEdges.push_back(edge);
132 }
133
134 inline void
135 SchedGraphNodeCommon::removeInEdge(const SchedGraphEdge* edge)
136 {
137   assert(edge->getSink() == this);
138   
139   for (iterator I = beginInEdges(); I != endInEdges(); ++I)
140     if ((*I) == edge)
141       {
142         inEdges.erase(I);
143         break;
144       }
145 }
146
147 inline void
148 SchedGraphNodeCommon::removeOutEdge(const SchedGraphEdge* edge)
149 {
150   assert(edge->getSrc() == this);
151   
152   for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
153     if ((*I) == edge)
154       {
155         outEdges.erase(I);
156         break;
157       }
158 }
159
160
161 //class SchedGraphCommon
162
163 /*ctor*/
164 SchedGraphCommon::SchedGraphCommon()
165
166 }
167
168
169 /*dtor*/
170 SchedGraphCommon::~SchedGraphCommon()
171 {
172
173   delete graphRoot;
174   delete graphLeaf;
175 }
176
177
178 void
179 SchedGraphCommon::eraseIncomingEdges(SchedGraphNodeCommon* node, bool addDummyEdges)
180 {
181   // Delete and disconnect all in-edges for the node
182   for (SchedGraphNodeCommon::iterator I = node->beginInEdges();
183        I != node->endInEdges(); ++I)
184     {
185       SchedGraphNodeCommon* srcNode = (*I)->getSrc();
186       srcNode->removeOutEdge(*I);
187       delete *I;
188       
189       if (addDummyEdges &&
190           srcNode != getRoot() &&
191           srcNode->beginOutEdges() == srcNode->endOutEdges())
192         { // srcNode has no more out edges, so add an edge to dummy EXIT node
193           assert(node != getLeaf() && "Adding edge that was just removed?");
194           (void) new SchedGraphEdge(srcNode, getLeaf(),
195                     SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
196         }
197     }
198   
199   node->inEdges.clear();
200 }
201
202 void
203 SchedGraphCommon::eraseOutgoingEdges(SchedGraphNodeCommon* node, bool addDummyEdges)
204 {
205   // Delete and disconnect all out-edges for the node
206   for (SchedGraphNodeCommon::iterator I = node->beginOutEdges();
207        I != node->endOutEdges(); ++I)
208     {
209       SchedGraphNodeCommon* sinkNode = (*I)->getSink();
210       sinkNode->removeInEdge(*I);
211       delete *I;
212       
213       if (addDummyEdges &&
214           sinkNode != getLeaf() &&
215           sinkNode->beginInEdges() == sinkNode->endInEdges())
216         { //sinkNode has no more in edges, so add an edge from dummy ENTRY node
217           assert(node != getRoot() && "Adding edge that was just removed?");
218           (void) new SchedGraphEdge(getRoot(), sinkNode,
219                     SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
220         }
221     }
222   
223   node->outEdges.clear();
224 }
225
226 void
227 SchedGraphCommon::eraseIncidentEdges(SchedGraphNodeCommon* node, bool addDummyEdges)
228 {
229   this->eraseIncomingEdges(node, addDummyEdges);        
230   this->eraseOutgoingEdges(node, addDummyEdges);        
231 }
232
233 std::ostream &operator<<(std::ostream &os, const SchedGraphNodeCommon& node)
234 {
235   
236   return os;
237 }