Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Target / SparcV9 / InstrSched / SchedGraphCommon.cpp
1 //===- SchedGraphCommon.cpp - Scheduling Graphs Base Class- ---------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Scheduling graph base class that contains common information for SchedGraph
11 // and ModuloSchedGraph scheduling graphs.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CodeGen/SchedGraphCommon.h"
16 #include "Support/STLExtras.h"
17
18 namespace llvm {
19
20 class SchedGraphCommon;
21
22 //
23 // class SchedGraphEdge
24 // 
25 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
26                                SchedGraphNodeCommon* _sink,
27                                SchedGraphEdgeDepType _depType,
28                                unsigned int     _depOrderType,
29                                int _minDelay)
30   : src(_src), sink(_sink), depType(_depType), depOrderType(_depOrderType),
31     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()), val(NULL) {
32   
33   iteDiff=0;
34   assert(src != sink && "Self-loop in scheduling graph!");
35   src->addOutEdge(this);
36   sink->addInEdge(this);
37 }
38
39 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon*  _src,
40                                SchedGraphNodeCommon*  _sink,
41                                const Value*     _val,
42                                unsigned int     _depOrderType,
43                                int              _minDelay)
44   : src(_src), sink(_sink), depType(ValueDep), depOrderType(_depOrderType),
45     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()), val(_val) {
46   iteDiff=0;
47   assert(src != sink && "Self-loop in scheduling graph!");
48   src->addOutEdge(this);
49   sink->addInEdge(this);
50 }
51
52 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon*  _src,
53                                SchedGraphNodeCommon*  _sink,
54                                unsigned int     _regNum,
55                                unsigned int     _depOrderType,
56                                int             _minDelay)
57   : src(_src), sink(_sink), depType(MachineRegister),
58     depOrderType(_depOrderType),
59     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
60     machineRegNum(_regNum) {
61   iteDiff=0;
62   assert(src != sink && "Self-loop in scheduling graph!");
63   src->addOutEdge(this);
64   sink->addInEdge(this);
65 }
66
67 SchedGraphEdge::SchedGraphEdge(SchedGraphNodeCommon* _src,
68                                SchedGraphNodeCommon* _sink,
69                                ResourceId      _resourceId,
70                                int             _minDelay)
71   : src(_src), sink(_sink), depType(MachineResource), depOrderType(NonDataDep),
72     minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
73     resourceId(_resourceId) {
74   iteDiff=0;
75   assert(src != sink && "Self-loop in scheduling graph!");
76   src->addOutEdge(this);
77   sink->addInEdge(this);
78 }
79
80
81
82
83 void SchedGraphEdge::dump(int indent) const {
84   std::cerr << std::string(indent*2, ' ') << *this; 
85 }
86
87 /*dtor*/
88 SchedGraphNodeCommon::~SchedGraphNodeCommon()
89 {
90   // for each node, delete its out-edges
91   std::for_each(beginOutEdges(), endOutEdges(),
92                 deleter<SchedGraphEdge>);
93 }
94
95 void SchedGraphNodeCommon::removeInEdge(const SchedGraphEdge* edge) {
96   assert(edge->getSink() == this);
97   
98   for (iterator I = beginInEdges(); I != endInEdges(); ++I)
99     if ((*I) == edge) {
100       inEdges.erase(I);
101       break;
102     }
103 }
104
105 void SchedGraphNodeCommon::removeOutEdge(const SchedGraphEdge* edge) {
106   assert(edge->getSrc() == this);
107   
108   for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
109     if ((*I) == edge) {
110       outEdges.erase(I);
111       break;
112     }
113 }
114
115 void SchedGraphNodeCommon::dump(int indent) const {
116   std::cerr << std::string(indent*2, ' ') << *this; 
117 }
118
119 //class SchedGraphCommon
120
121 SchedGraphCommon::~SchedGraphCommon() {
122   delete graphRoot;
123   delete graphLeaf;
124 }
125
126
127 void SchedGraphCommon::eraseIncomingEdges(SchedGraphNodeCommon* node, 
128                                           bool addDummyEdges) {
129   // Delete and disconnect all in-edges for the node
130   for (SchedGraphNodeCommon::iterator I = node->beginInEdges();
131        I != node->endInEdges(); ++I) {
132     SchedGraphNodeCommon* srcNode = (*I)->getSrc();
133     srcNode->removeOutEdge(*I);
134     delete *I;
135     
136     if (addDummyEdges && srcNode != getRoot() &&
137         srcNode->beginOutEdges() == srcNode->endOutEdges()) { 
138       
139       // srcNode has no more out edges, so add an edge to dummy EXIT node
140       assert(node != getLeaf() && "Adding edge that was just removed?");
141       (void) new SchedGraphEdge(srcNode, getLeaf(),
142                                 SchedGraphEdge::CtrlDep, 
143                                 SchedGraphEdge::NonDataDep, 0);
144     }
145   }
146   
147   node->inEdges.clear();
148 }
149
150 void SchedGraphCommon::eraseOutgoingEdges(SchedGraphNodeCommon* node, 
151                                           bool addDummyEdges) {
152   // Delete and disconnect all out-edges for the node
153   for (SchedGraphNodeCommon::iterator I = node->beginOutEdges();
154        I != node->endOutEdges(); ++I) {
155     SchedGraphNodeCommon* sinkNode = (*I)->getSink();
156     sinkNode->removeInEdge(*I);
157     delete *I;
158     
159     if (addDummyEdges &&
160         sinkNode != getLeaf() &&
161         sinkNode->beginInEdges() == sinkNode->endInEdges()) {
162       
163       //sinkNode has no more in edges, so add an edge from dummy ENTRY node
164       assert(node != getRoot() && "Adding edge that was just removed?");
165       (void) new SchedGraphEdge(getRoot(), sinkNode,
166                                 SchedGraphEdge::CtrlDep, 
167                                 SchedGraphEdge::NonDataDep, 0);
168     }
169   }
170   
171   node->outEdges.clear();
172 }
173
174 void SchedGraphCommon::eraseIncidentEdges(SchedGraphNodeCommon* node, 
175                                           bool addDummyEdges) {
176   this->eraseIncomingEdges(node, addDummyEdges);        
177   this->eraseOutgoingEdges(node, addDummyEdges);        
178 }
179
180 } // End llvm namespace