blob: 5f14631b069d594e5cc6361a91d13e87b35624a7 [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- InterferenceGraph.cpp ---------------------------------------------===//
2//
3// Interference graph for coloring-based register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
6
Ruchira Sasanka8e604792001-09-14 21:18:34 +00007#include "llvm/CodeGen/InterferenceGraph.h"
Chris Lattnerc6f3ae52002-04-29 17:42:12 +00008#include "llvm/CodeGen/RegAllocCommon.h"
Vikram S. Adve39c94e12002-09-14 23:05:33 +00009#include "Support/STLExtras.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000010#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000013//-----------------------------------------------------------------------------
14// Constructor: Records the RegClass and initalizes IGNodeList.
15// The matrix is NOT yet created by the constructor. Call createGraph()
16// to create it after adding all IGNodes to the IGNodeList.
17//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000018InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
19 IGNodeList()
20{
21 IG = NULL;
22 Size = 0;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000023 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner697954c2002-01-20 22:54:45 +000024 cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025 }
26}
27
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028
29//-----------------------------------------------------------------------------
30// destructor. Deletes the bit matrix and all IGNodes
31//-----------------------------------------------------------------------------
32InterferenceGraph:: ~InterferenceGraph() {
33
34 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000035 for(unsigned int r=0; r < IGNodeList.size(); ++r)
36 delete[] IG[r];
37 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000038
39 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000040 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000041}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000042
43
44
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000045//-----------------------------------------------------------------------------
46// Creates (dynamically allocates) the bit matrix necessary to hold the
47// interference graph.
48//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000049void InterferenceGraph::createGraph()
50{
51 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000052 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053 for( unsigned int r=0; r < Size; ++r)
54 IG[r] = new char[Size];
55
56 // init IG matrix
57 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000058 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059 IG[i][j] = 0;
60}
61
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000062//-----------------------------------------------------------------------------
63// creates a new IGNode for the given live range and add to IG
64//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000065void InterferenceGraph::addLRToIG(LiveRange *const LR)
66{
Chris Lattner697954c2002-01-20 22:54:45 +000067 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068}
69
70
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000071//-----------------------------------------------------------------------------
72// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000073// update both the matrix and AdjLists of nodes.
74// If there is already an interference between LR1 and LR2, adj lists
75// are not updated. LR1 and LR2 must be distinct since if not, it suggests
76// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000077//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000078void InterferenceGraph::setInterference(const LiveRange *const LR1,
79 const LiveRange *const LR2 ) {
80 assert(LR1 != LR2);
81
82 IGNode *const IGNode1 = LR1->getUserIGNode();
83 IGNode *const IGNode2 = LR2->getUserIGNode();
84
Vikram S. Adve39c94e12002-09-14 23:05:33 +000085 assertIGNode( IGNode1 );
86 assertIGNode( IGNode2 );
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
88 const unsigned int row = IGNode1->getIndex();
89 const unsigned int col = IGNode2->getIndex();
90
91 char *val;
92
Vikram S. Adve993243e2002-09-15 15:33:48 +000093 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +000094 cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095
96 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
97
98 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099 *val = 1; // add edges between nodes
100 IGNode1->addAdjIGNode( IGNode2 );
101 IGNode2->addAdjIGNode( IGNode1 );
102 }
103
104}
105
106
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000107//----------------------------------------------------------------------------
108// return whether two live ranges interfere
109//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
111 const LiveRange *const LR2 ) const {
112
113 assert(LR1 != LR2);
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000114 assertIGNode( LR1->getUserIGNode() );
115 assertIGNode( LR2->getUserIGNode() );
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
117 const unsigned int row = LR1->getUserIGNode()->getIndex();
118 const unsigned int col = LR2->getUserIGNode()->getIndex();
119
120 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000121 if (row > col)
122 ret = IG[row][col];
123 else
124 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125 return ret;
126
127}
128
129
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000130//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
132// Then the IGNode2L will be deleted. Necessary for coalescing.
133// IMPORTANT: The live ranges are NOT merged by this method. Use
134// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000135//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136
Chris Lattner296b7732002-02-05 02:52:05 +0000137void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
138 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139
140 assert( LR1 != LR2); // cannot merge the same live range
141
142 IGNode *const DestNode = LR1->getUserIGNode();
143 IGNode *SrcNode = LR2->getUserIGNode();
144
145 assertIGNode( DestNode );
146 assertIGNode( SrcNode );
147
Vikram S. Adve993243e2002-09-15 15:33:48 +0000148 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner296b7732002-02-05 02:52:05 +0000149 cerr << "Merging LRs: \""; printSet(*LR1);
150 cerr << "\" and \""; printSet(*LR2);
Chris Lattner697954c2002-01-20 22:54:45 +0000151 cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000152 }
153
154 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
155 const unsigned SrcInd = SrcNode->getIndex();
156
157
158 // for all neighs of SrcNode
159 for(unsigned i=0; i < SrcDegree; i++) {
160 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
161
162 LiveRange *const LROfNeigh = NeighNode->getParentLR();
163
164 // delete edge between src and neigh - even neigh == dest
165 NeighNode->delAdjIGNode(SrcNode);
166
167 // set the matrix posn to 0 betn src and neigh - even neigh == dest
168 const unsigned NInd = NeighNode->getIndex();
169 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
170
171
172 if( LR1 != LROfNeigh) { // if the neigh != dest
173
174 // add edge betwn Dest and Neigh - if there is no current edge
175 setInterference(LR1, LROfNeigh );
176 }
177
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000178 }
179
180 IGNodeList[ SrcInd ] = NULL;
181
182 // SrcNode is no longer necessary - LR2 must be deleted by the caller
183 delete( SrcNode );
184
185}
186
187
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000188//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000189// must be called after modifications to the graph are over but before
190// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000191//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192void InterferenceGraph::setCurDegreeOfIGNodes()
193{
194 unsigned Size = IGNodeList.size();
195
196 for( unsigned i=0; i < Size; i++) {
197 IGNode *Node = IGNodeList[i];
198 if( Node )
199 Node->setCurDegree();
200 }
201}
202
203
204
205
206
207//--------------------- debugging (Printing) methods -----------------------
208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209//----------------------------------------------------------------------------
210// Print the IGnodes
211//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212void InterferenceGraph::printIG() const
213{
214
215 for(unsigned int i=0; i < Size; i++) {
216
217 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000218 if(Node) {
219 cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000220
Chris Lattner697954c2002-01-20 22:54:45 +0000221 for( unsigned int j=0; j < i; j++) {
222 if(IG[i][j])
223 cerr << "(" << i << "," << j << ") ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000224 }
Chris Lattner697954c2002-01-20 22:54:45 +0000225 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226 }
Chris Lattner697954c2002-01-20 22:54:45 +0000227 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000228}
229
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000230//----------------------------------------------------------------------------
231// Print the IGnodes in the IGNode List
232//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233void InterferenceGraph::printIGNodeList() const
234{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000235 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000236 const IGNode *const Node = IGNodeList[i];
237
Chris Lattner697954c2002-01-20 22:54:45 +0000238 if (Node) {
239 cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000240 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000241 //int Deg = Node->getCurDegree();
242 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
243 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000244 }
245}