1 //===- SchedGraphCommon.cpp - Scheduling Graphs Base Class- ---------------===//
3 // Scheduling graph base class that contains common information for SchedGraph
4 // and ModuloSchedGraph scheduling graphs.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/CodeGen/SchedGraphCommon.h"
9 #include "Support/STLExtras.h"
11 class SchedGraphCommon;
14 // class SchedGraphEdge
16 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
17 SchedGraphNodeCommon* _sink,
18 SchedGraphEdgeDepType _depType,
19 unsigned int _depOrderType,
21 : src(_src), sink(_sink), depType(_depType), depOrderType(_depOrderType),
22 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()), val(NULL) {
25 assert(src != sink && "Self-loop in scheduling graph!");
26 src->addOutEdge(this);
27 sink->addInEdge(this);
30 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
31 SchedGraphNodeCommon* _sink,
33 unsigned int _depOrderType,
35 : src(_src), sink(_sink), depType(ValueDep), depOrderType(_depOrderType),
36 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()), val(_val) {
38 assert(src != sink && "Self-loop in scheduling graph!");
39 src->addOutEdge(this);
40 sink->addInEdge(this);
43 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
44 SchedGraphNodeCommon* _sink,
46 unsigned int _depOrderType,
48 : src(_src), sink(_sink), depType(MachineRegister),
49 depOrderType(_depOrderType),
50 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
51 machineRegNum(_regNum) {
53 assert(src != sink && "Self-loop in scheduling graph!");
54 src->addOutEdge(this);
55 sink->addInEdge(this);
58 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
59 SchedGraphNodeCommon* _sink,
60 ResourceId _resourceId,
62 : src(_src), sink(_sink), depType(MachineResource), depOrderType(NonDataDep),
63 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
64 resourceId(_resourceId) {
66 assert(src != sink && "Self-loop in scheduling graph!");
67 src->addOutEdge(this);
68 sink->addInEdge(this);
74 void SchedGraphEdge::dump(int indent) const {
75 std::cerr << std::string(indent*2, ' ') << *this;
79 SchedGraphNodeCommon::~SchedGraphNodeCommon()
81 // for each node, delete its out-edges
82 std::for_each(beginOutEdges(), endOutEdges(),
83 deleter<SchedGraphEdge>);
86 void SchedGraphNodeCommon::removeInEdge(const SchedGraphEdge* edge) {
87 assert(edge->getSink() == this);
89 for (iterator I = beginInEdges(); I != endInEdges(); ++I)
96 void SchedGraphNodeCommon::removeOutEdge(const SchedGraphEdge* edge) {
97 assert(edge->getSrc() == this);
99 for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
106 void SchedGraphNodeCommon::dump(int indent) const {
107 std::cerr << std::string(indent*2, ' ') << *this;
110 //class SchedGraphCommon
112 SchedGraphCommon::~SchedGraphCommon() {
118 void SchedGraphCommon::eraseIncomingEdges(SchedGraphNodeCommon* node,
119 bool addDummyEdges) {
120 // Delete and disconnect all in-edges for the node
121 for (SchedGraphNodeCommon::iterator I = node->beginInEdges();
122 I != node->endInEdges(); ++I) {
123 SchedGraphNodeCommon* srcNode = (*I)->getSrc();
124 srcNode->removeOutEdge(*I);
127 if (addDummyEdges && srcNode != getRoot() &&
128 srcNode->beginOutEdges() == srcNode->endOutEdges()) {
130 // srcNode has no more out edges, so add an edge to dummy EXIT node
131 assert(node != getLeaf() && "Adding edge that was just removed?");
132 (void) new SchedGraphEdge(srcNode, getLeaf(),
133 SchedGraphEdge::CtrlDep,
134 SchedGraphEdge::NonDataDep, 0);
138 node->inEdges.clear();
141 void SchedGraphCommon::eraseOutgoingEdges(SchedGraphNodeCommon* node,
142 bool addDummyEdges) {
143 // Delete and disconnect all out-edges for the node
144 for (SchedGraphNodeCommon::iterator I = node->beginOutEdges();
145 I != node->endOutEdges(); ++I) {
146 SchedGraphNodeCommon* sinkNode = (*I)->getSink();
147 sinkNode->removeInEdge(*I);
151 sinkNode != getLeaf() &&
152 sinkNode->beginInEdges() == sinkNode->endInEdges()) {
154 //sinkNode has no more in edges, so add an edge from dummy ENTRY node
155 assert(node != getRoot() && "Adding edge that was just removed?");
156 (void) new SchedGraphEdge(getRoot(), sinkNode,
157 SchedGraphEdge::CtrlDep,
158 SchedGraphEdge::NonDataDep, 0);
162 node->outEdges.clear();
165 void SchedGraphCommon::eraseIncidentEdges(SchedGraphNodeCommon* node,
166 bool addDummyEdges) {
167 this->eraseIncomingEdges(node, addDummyEdges);
168 this->eraseOutgoingEdges(node, addDummyEdges);