blob: 0de7275acf7c9e180e9f1371dc95fe1b636005aa [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/InterferenceGraph.h"
Chris Lattner697954c2002-01-20 22:54:45 +00002#include "Support/STLExtras.h"
3#include <iostream>
4using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +00005
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00006//-----------------------------------------------------------------------------
7// Constructor: Records the RegClass and initalizes IGNodeList.
8// The matrix is NOT yet created by the constructor. Call createGraph()
9// to create it after adding all IGNodes to the IGNodeList.
10//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000011InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
12 IGNodeList()
13{
14 IG = NULL;
15 Size = 0;
16 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +000017 cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018 }
19}
20
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000021
22//-----------------------------------------------------------------------------
23// destructor. Deletes the bit matrix and all IGNodes
24//-----------------------------------------------------------------------------
25InterferenceGraph:: ~InterferenceGraph() {
26
27 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000028 for(unsigned int r=0; r < IGNodeList.size(); ++r)
29 delete[] IG[r];
30 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000031
32 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000033 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000034}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000035
36
37
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000038//-----------------------------------------------------------------------------
39// Creates (dynamically allocates) the bit matrix necessary to hold the
40// interference graph.
41//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000042void InterferenceGraph::createGraph()
43{
44 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000045 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000046 for( unsigned int r=0; r < Size; ++r)
47 IG[r] = new char[Size];
48
49 // init IG matrix
50 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000051 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052 IG[i][j] = 0;
53}
54
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000055//-----------------------------------------------------------------------------
56// creates a new IGNode for the given live range and add to IG
57//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000058void InterferenceGraph::addLRToIG(LiveRange *const LR)
59{
Chris Lattner697954c2002-01-20 22:54:45 +000060 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000061}
62
63
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000064//-----------------------------------------------------------------------------
65// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000066// update both the matrix and AdjLists of nodes.
67// If there is already an interference between LR1 and LR2, adj lists
68// are not updated. LR1 and LR2 must be distinct since if not, it suggests
69// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000070//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000071void InterferenceGraph::setInterference(const LiveRange *const LR1,
72 const LiveRange *const LR2 ) {
73 assert(LR1 != LR2);
74
75 IGNode *const IGNode1 = LR1->getUserIGNode();
76 IGNode *const IGNode2 = LR2->getUserIGNode();
77
78 if( DEBUG_RA) {
79 assertIGNode( IGNode1 );
80 assertIGNode( IGNode2 );
81 }
82
83 const unsigned int row = IGNode1->getIndex();
84 const unsigned int col = IGNode2->getIndex();
85
86 char *val;
87
88 if( DEBUG_RA > 1)
Chris Lattner697954c2002-01-20 22:54:45 +000089 cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000090
91 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
92
93 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094 *val = 1; // add edges between nodes
95 IGNode1->addAdjIGNode( IGNode2 );
96 IGNode2->addAdjIGNode( IGNode1 );
97 }
98
99}
100
101
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000102//----------------------------------------------------------------------------
103// return whether two live ranges interfere
104//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
106 const LiveRange *const LR2 ) const {
107
108 assert(LR1 != LR2);
109
110 if( DEBUG_RA) {
111 assertIGNode( LR1->getUserIGNode() );
112 assertIGNode( LR2->getUserIGNode() );
113 }
114
115 const unsigned int row = LR1->getUserIGNode()->getIndex();
116 const unsigned int col = LR2->getUserIGNode()->getIndex();
117
118 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000119 if (row > col)
120 ret = IG[row][col];
121 else
122 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123 return ret;
124
125}
126
127
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000128//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000129// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
130// Then the IGNode2L will be deleted. Necessary for coalescing.
131// IMPORTANT: The live ranges are NOT merged by this method. Use
132// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000133//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134
135void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *const LR1,
136 LiveRange *const LR2 ) {
137
138 assert( LR1 != LR2); // cannot merge the same live range
139
140 IGNode *const DestNode = LR1->getUserIGNode();
141 IGNode *SrcNode = LR2->getUserIGNode();
142
143 assertIGNode( DestNode );
144 assertIGNode( SrcNode );
145
146 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000147 cerr << "Merging LRs: \""; LR1->printSet();
148 cerr << "\" and \""; LR2->printSet();
149 cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150 }
151
152 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
153 const unsigned SrcInd = SrcNode->getIndex();
154
155
156 // for all neighs of SrcNode
157 for(unsigned i=0; i < SrcDegree; i++) {
158 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
159
160 LiveRange *const LROfNeigh = NeighNode->getParentLR();
161
162 // delete edge between src and neigh - even neigh == dest
163 NeighNode->delAdjIGNode(SrcNode);
164
165 // set the matrix posn to 0 betn src and neigh - even neigh == dest
166 const unsigned NInd = NeighNode->getIndex();
167 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
168
169
170 if( LR1 != LROfNeigh) { // if the neigh != dest
171
172 // add edge betwn Dest and Neigh - if there is no current edge
173 setInterference(LR1, LROfNeigh );
174 }
175
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000176 }
177
178 IGNodeList[ SrcInd ] = NULL;
179
180 // SrcNode is no longer necessary - LR2 must be deleted by the caller
181 delete( SrcNode );
182
183}
184
185
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000186//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187// must be called after modifications to the graph are over but before
188// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000190void InterferenceGraph::setCurDegreeOfIGNodes()
191{
192 unsigned Size = IGNodeList.size();
193
194 for( unsigned i=0; i < Size; i++) {
195 IGNode *Node = IGNodeList[i];
196 if( Node )
197 Node->setCurDegree();
198 }
199}
200
201
202
203
204
205//--------------------- debugging (Printing) methods -----------------------
206
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000207//----------------------------------------------------------------------------
208// Print the IGnodes
209//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000210void InterferenceGraph::printIG() const
211{
212
213 for(unsigned int i=0; i < Size; i++) {
214
215 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000216 if(Node) {
217 cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000218
Chris Lattner697954c2002-01-20 22:54:45 +0000219 for( unsigned int j=0; j < i; j++) {
220 if(IG[i][j])
221 cerr << "(" << i << "," << j << ") ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000222 }
Chris Lattner697954c2002-01-20 22:54:45 +0000223 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000224 }
Chris Lattner697954c2002-01-20 22:54:45 +0000225 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226}
227
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000228//----------------------------------------------------------------------------
229// Print the IGnodes in the IGNode List
230//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231void InterferenceGraph::printIGNodeList() const
232{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234 const IGNode *const Node = IGNodeList[i];
235
Chris Lattner697954c2002-01-20 22:54:45 +0000236 if (Node) {
237 cerr << " [" << Node->getIndex() << "] ";
238 Node->getParentLR()->printSet();
239 //int Deg = Node->getCurDegree();
240 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
241 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000242 }
243}