blob: a366c117109b69ac7477fe40ab4ddb6492fc80b8 [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>
Chris Lattner30adeb62002-02-04 16:36:59 +00004#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +00005using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +00006
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00007//-----------------------------------------------------------------------------
8// Constructor: Records the RegClass and initalizes IGNodeList.
9// The matrix is NOT yet created by the constructor. Call createGraph()
10// to create it after adding all IGNodes to the IGNodeList.
11//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
13 IGNodeList()
14{
15 IG = NULL;
16 Size = 0;
17 if( DEBUG_RA) {
Chris Lattner697954c2002-01-20 22:54:45 +000018 cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000019 }
20}
21
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000022
23//-----------------------------------------------------------------------------
24// destructor. Deletes the bit matrix and all IGNodes
25//-----------------------------------------------------------------------------
26InterferenceGraph:: ~InterferenceGraph() {
27
28 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000029 for(unsigned int r=0; r < IGNodeList.size(); ++r)
30 delete[] IG[r];
31 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000032
33 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000034 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000035}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000036
37
38
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000039//-----------------------------------------------------------------------------
40// Creates (dynamically allocates) the bit matrix necessary to hold the
41// interference graph.
42//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000043void InterferenceGraph::createGraph()
44{
45 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000046 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000047 for( unsigned int r=0; r < Size; ++r)
48 IG[r] = new char[Size];
49
50 // init IG matrix
51 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000052 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053 IG[i][j] = 0;
54}
55
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000056//-----------------------------------------------------------------------------
57// creates a new IGNode for the given live range and add to IG
58//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059void InterferenceGraph::addLRToIG(LiveRange *const LR)
60{
Chris Lattner697954c2002-01-20 22:54:45 +000061 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062}
63
64
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000065//-----------------------------------------------------------------------------
66// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000067// update both the matrix and AdjLists of nodes.
68// If there is already an interference between LR1 and LR2, adj lists
69// are not updated. LR1 and LR2 must be distinct since if not, it suggests
70// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000071//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000072void InterferenceGraph::setInterference(const LiveRange *const LR1,
73 const LiveRange *const LR2 ) {
74 assert(LR1 != LR2);
75
76 IGNode *const IGNode1 = LR1->getUserIGNode();
77 IGNode *const IGNode2 = LR2->getUserIGNode();
78
79 if( DEBUG_RA) {
80 assertIGNode( IGNode1 );
81 assertIGNode( IGNode2 );
82 }
83
84 const unsigned int row = IGNode1->getIndex();
85 const unsigned int col = IGNode2->getIndex();
86
87 char *val;
88
89 if( DEBUG_RA > 1)
Chris Lattner697954c2002-01-20 22:54:45 +000090 cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091
92 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
93
94 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095 *val = 1; // add edges between nodes
96 IGNode1->addAdjIGNode( IGNode2 );
97 IGNode2->addAdjIGNode( IGNode1 );
98 }
99
100}
101
102
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000103//----------------------------------------------------------------------------
104// return whether two live ranges interfere
105//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000106unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
107 const LiveRange *const LR2 ) const {
108
109 assert(LR1 != LR2);
110
111 if( DEBUG_RA) {
112 assertIGNode( LR1->getUserIGNode() );
113 assertIGNode( LR2->getUserIGNode() );
114 }
115
116 const unsigned int row = LR1->getUserIGNode()->getIndex();
117 const unsigned int col = LR2->getUserIGNode()->getIndex();
118
119 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000120 if (row > col)
121 ret = IG[row][col];
122 else
123 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000124 return ret;
125
126}
127
128
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000129//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000130// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
131// Then the IGNode2L will be deleted. Necessary for coalescing.
132// IMPORTANT: The live ranges are NOT merged by this method. Use
133// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000134//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135
136void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *const LR1,
137 LiveRange *const LR2 ) {
138
139 assert( LR1 != LR2); // cannot merge the same live range
140
141 IGNode *const DestNode = LR1->getUserIGNode();
142 IGNode *SrcNode = LR2->getUserIGNode();
143
144 assertIGNode( DestNode );
145 assertIGNode( SrcNode );
146
147 if( DEBUG_RA > 1) {
Chris Lattner697954c2002-01-20 22:54:45 +0000148 cerr << "Merging LRs: \""; LR1->printSet();
149 cerr << "\" and \""; LR2->printSet();
150 cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151 }
152
153 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
154 const unsigned SrcInd = SrcNode->getIndex();
155
156
157 // for all neighs of SrcNode
158 for(unsigned i=0; i < SrcDegree; i++) {
159 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
160
161 LiveRange *const LROfNeigh = NeighNode->getParentLR();
162
163 // delete edge between src and neigh - even neigh == dest
164 NeighNode->delAdjIGNode(SrcNode);
165
166 // set the matrix posn to 0 betn src and neigh - even neigh == dest
167 const unsigned NInd = NeighNode->getIndex();
168 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
169
170
171 if( LR1 != LROfNeigh) { // if the neigh != dest
172
173 // add edge betwn Dest and Neigh - if there is no current edge
174 setInterference(LR1, LROfNeigh );
175 }
176
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000177 }
178
179 IGNodeList[ SrcInd ] = NULL;
180
181 // SrcNode is no longer necessary - LR2 must be deleted by the caller
182 delete( SrcNode );
183
184}
185
186
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000187//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188// must be called after modifications to the graph are over but before
189// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191void InterferenceGraph::setCurDegreeOfIGNodes()
192{
193 unsigned Size = IGNodeList.size();
194
195 for( unsigned i=0; i < Size; i++) {
196 IGNode *Node = IGNodeList[i];
197 if( Node )
198 Node->setCurDegree();
199 }
200}
201
202
203
204
205
206//--------------------- debugging (Printing) methods -----------------------
207
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000208//----------------------------------------------------------------------------
209// Print the IGnodes
210//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000211void InterferenceGraph::printIG() const
212{
213
214 for(unsigned int i=0; i < Size; i++) {
215
216 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000217 if(Node) {
218 cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219
Chris Lattner697954c2002-01-20 22:54:45 +0000220 for( unsigned int j=0; j < i; j++) {
221 if(IG[i][j])
222 cerr << "(" << i << "," << j << ") ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000223 }
Chris Lattner697954c2002-01-20 22:54:45 +0000224 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000225 }
Chris Lattner697954c2002-01-20 22:54:45 +0000226 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227}
228
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000229//----------------------------------------------------------------------------
230// Print the IGnodes in the IGNode List
231//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232void InterferenceGraph::printIGNodeList() const
233{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000235 const IGNode *const Node = IGNodeList[i];
236
Chris Lattner697954c2002-01-20 22:54:45 +0000237 if (Node) {
238 cerr << " [" << Node->getIndex() << "] ";
239 Node->getParentLR()->printSet();
240 //int Deg = Node->getCurDegree();
241 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
242 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000243 }
244}