Eliminate the distinction between "real" and "unreal" instructions
[oota-llvm.git] / lib / CodeGen / 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 void SchedGraphEdge::dump(int indent) const {
82   std::cerr << std::string(indent*2, ' ') << *this; 
83 }
84
85 /*dtor*/
86 SchedGraphNodeCommon::~SchedGraphNodeCommon()
87 {
88   // for each node, delete its out-edges
89   std::for_each(beginOutEdges(), endOutEdges(),
90                 deleter<SchedGraphEdge>);
91 }
92
93 void SchedGraphNodeCommon::removeInEdge(const SchedGraphEdge* edge) {
94   assert(edge->getSink() == this);
95   
96   for (iterator I = beginInEdges(); I != endInEdges(); ++I)
97     if ((*I) == edge) {
98       inEdges.erase(I);
99       break;
100     }
101 }
102
103 void SchedGraphNodeCommon::removeOutEdge(const SchedGraphEdge* edge) {
104   assert(edge->getSrc() == this);
105   
106   for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
107     if ((*I) == edge) {
108       outEdges.erase(I);
109       break;
110     }
111 }
112
113 void SchedGraphNodeCommon::dump(int indent) const {
114   std::cerr << std::string(indent*2, ' ') << *this; 
115 }
116
117 //class SchedGraphCommon
118
119 SchedGraphCommon::~SchedGraphCommon() {
120   delete graphRoot;
121   delete graphLeaf;
122 }
123
124
125 void SchedGraphCommon::eraseIncomingEdges(SchedGraphNodeCommon* node, 
126                                           bool addDummyEdges) {
127   // Delete and disconnect all in-edges for the node
128   for (SchedGraphNodeCommon::iterator I = node->beginInEdges();
129        I != node->endInEdges(); ++I) {
130     SchedGraphNodeCommon* srcNode = (*I)->getSrc();
131     srcNode->removeOutEdge(*I);
132     delete *I;
133     
134     if (addDummyEdges && srcNode != getRoot() &&
135         srcNode->beginOutEdges() == srcNode->endOutEdges()) { 
136       
137       // srcNode has no more out edges, so add an edge to dummy EXIT node
138       assert(node != getLeaf() && "Adding edge that was just removed?");
139       (void) new SchedGraphEdge(srcNode, getLeaf(),
140                                 SchedGraphEdge::CtrlDep, 
141                                 SchedGraphEdge::NonDataDep, 0);
142     }
143   }
144   
145   node->inEdges.clear();
146 }
147
148 void SchedGraphCommon::eraseOutgoingEdges(SchedGraphNodeCommon* node, 
149                                           bool addDummyEdges) {
150   // Delete and disconnect all out-edges for the node
151   for (SchedGraphNodeCommon::iterator I = node->beginOutEdges();
152        I != node->endOutEdges(); ++I) {
153     SchedGraphNodeCommon* sinkNode = (*I)->getSink();
154     sinkNode->removeInEdge(*I);
155     delete *I;
156     
157     if (addDummyEdges &&
158         sinkNode != getLeaf() &&
159         sinkNode->beginInEdges() == sinkNode->endInEdges()) {
160       
161       //sinkNode has no more in edges, so add an edge from dummy ENTRY node
162       assert(node != getRoot() && "Adding edge that was just removed?");
163       (void) new SchedGraphEdge(getRoot(), sinkNode,
164                                 SchedGraphEdge::CtrlDep, 
165                                 SchedGraphEdge::NonDataDep, 0);
166     }
167   }
168   
169   node->outEdges.clear();
170 }
171
172 void SchedGraphCommon::eraseIncidentEdges(SchedGraphNodeCommon* node, 
173                                           bool addDummyEdges) {
174   this->eraseIncomingEdges(node, addDummyEdges);        
175   this->eraseOutgoingEdges(node, addDummyEdges);        
176 }
177
178 } // End llvm namespace