blob: b5e26887c4e5a11b123dcb90a067229a28851238 [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
Chris Lattner4309e732003-01-15 19:57:07 +00007#include "RegAllocCommon.h"
Ruchira Sasanka8e604792001-09-14 21:18:34 +00008#include "llvm/CodeGen/InterferenceGraph.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +00009#include "llvm/CodeGen/IGNode.h"
Vikram S. Adve39c94e12002-09-14 23:05:33 +000010#include "Support/STLExtras.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000011#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000012using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000013
Chris Lattner28760f42002-10-29 16:42:34 +000014// for asserting this IG node is infact in the IGNodeList of this class
15inline static void assertIGNode(const InterferenceGraph *IG,
16 const IGNode *Node) {
17 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
18}
19
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000020//-----------------------------------------------------------------------------
21// Constructor: Records the RegClass and initalizes IGNodeList.
22// The matrix is NOT yet created by the constructor. Call createGraph()
23// to create it after adding all IGNodes to the IGNodeList.
24//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
26 IGNodeList()
27{
28 IG = NULL;
29 Size = 0;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000030 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner697954c2002-01-20 22:54:45 +000031 cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000032 }
33}
34
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000035
36//-----------------------------------------------------------------------------
37// destructor. Deletes the bit matrix and all IGNodes
38//-----------------------------------------------------------------------------
39InterferenceGraph:: ~InterferenceGraph() {
40
41 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000042 for(unsigned int r=0; r < IGNodeList.size(); ++r)
43 delete[] IG[r];
44 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000045
46 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000047 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000048}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000049
50
51
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000052//-----------------------------------------------------------------------------
53// Creates (dynamically allocates) the bit matrix necessary to hold the
54// interference graph.
55//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000056void InterferenceGraph::createGraph()
57{
58 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000059 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060 for( unsigned int r=0; r < Size; ++r)
61 IG[r] = new char[Size];
62
63 // init IG matrix
64 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000065 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000066 IG[i][j] = 0;
67}
68
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000069//-----------------------------------------------------------------------------
70// creates a new IGNode for the given live range and add to IG
71//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000072void InterferenceGraph::addLRToIG(LiveRange *const LR)
73{
Chris Lattner697954c2002-01-20 22:54:45 +000074 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075}
76
77
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000078//-----------------------------------------------------------------------------
79// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000080// update both the matrix and AdjLists of nodes.
81// If there is already an interference between LR1 and LR2, adj lists
82// are not updated. LR1 and LR2 must be distinct since if not, it suggests
83// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000084//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085void InterferenceGraph::setInterference(const LiveRange *const LR1,
86 const LiveRange *const LR2 ) {
87 assert(LR1 != LR2);
88
Chris Lattner28760f42002-10-29 16:42:34 +000089 IGNode *IGNode1 = LR1->getUserIGNode();
90 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091
Chris Lattner28760f42002-10-29 16:42:34 +000092 assertIGNode(this, IGNode1);
93 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094
Chris Lattner28760f42002-10-29 16:42:34 +000095 unsigned row = IGNode1->getIndex();
96 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000097
98 char *val;
99
Vikram S. Adve993243e2002-09-15 15:33:48 +0000100 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000101 cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102
103 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
104
105 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000106 *val = 1; // add edges between nodes
107 IGNode1->addAdjIGNode( IGNode2 );
108 IGNode2->addAdjIGNode( IGNode1 );
109 }
110
111}
112
113
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000114//----------------------------------------------------------------------------
115// return whether two live ranges interfere
116//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000117unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
118 const LiveRange *const LR2 ) const {
119
120 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000121 assertIGNode(this, LR1->getUserIGNode());
122 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123
124 const unsigned int row = LR1->getUserIGNode()->getIndex();
125 const unsigned int col = LR2->getUserIGNode()->getIndex();
126
127 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000128 if (row > col)
129 ret = IG[row][col];
130 else
131 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000132 return ret;
133
134}
135
136
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000137//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
139// Then the IGNode2L will be deleted. Necessary for coalescing.
140// IMPORTANT: The live ranges are NOT merged by this method. Use
141// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000142//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000143
Chris Lattner296b7732002-02-05 02:52:05 +0000144void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
145 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146
147 assert( LR1 != LR2); // cannot merge the same live range
148
149 IGNode *const DestNode = LR1->getUserIGNode();
150 IGNode *SrcNode = LR2->getUserIGNode();
151
Chris Lattner28760f42002-10-29 16:42:34 +0000152 assertIGNode(this, DestNode);
153 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154
Vikram S. Adve993243e2002-09-15 15:33:48 +0000155 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner296b7732002-02-05 02:52:05 +0000156 cerr << "Merging LRs: \""; printSet(*LR1);
157 cerr << "\" and \""; printSet(*LR2);
Chris Lattner697954c2002-01-20 22:54:45 +0000158 cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159 }
160
161 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
162 const unsigned SrcInd = SrcNode->getIndex();
163
164
165 // for all neighs of SrcNode
166 for(unsigned i=0; i < SrcDegree; i++) {
167 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
168
169 LiveRange *const LROfNeigh = NeighNode->getParentLR();
170
171 // delete edge between src and neigh - even neigh == dest
172 NeighNode->delAdjIGNode(SrcNode);
173
174 // set the matrix posn to 0 betn src and neigh - even neigh == dest
175 const unsigned NInd = NeighNode->getIndex();
176 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
177
178
179 if( LR1 != LROfNeigh) { // if the neigh != dest
180
181 // add edge betwn Dest and Neigh - if there is no current edge
182 setInterference(LR1, LROfNeigh );
183 }
184
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185 }
186
187 IGNodeList[ SrcInd ] = NULL;
188
189 // SrcNode is no longer necessary - LR2 must be deleted by the caller
190 delete( SrcNode );
191
192}
193
194
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000195//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000196// must be called after modifications to the graph are over but before
197// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000198//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199void InterferenceGraph::setCurDegreeOfIGNodes()
200{
201 unsigned Size = IGNodeList.size();
202
203 for( unsigned i=0; i < Size; i++) {
204 IGNode *Node = IGNodeList[i];
205 if( Node )
206 Node->setCurDegree();
207 }
208}
209
210
211
212
213
214//--------------------- debugging (Printing) methods -----------------------
215
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000216//----------------------------------------------------------------------------
217// Print the IGnodes
218//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219void InterferenceGraph::printIG() const
220{
221
222 for(unsigned int i=0; i < Size; i++) {
223
224 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000225 if(Node) {
226 cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227
Chris Lattner697954c2002-01-20 22:54:45 +0000228 for( unsigned int j=0; j < i; j++) {
229 if(IG[i][j])
230 cerr << "(" << i << "," << j << ") ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231 }
Chris Lattner697954c2002-01-20 22:54:45 +0000232 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233 }
Chris Lattner697954c2002-01-20 22:54:45 +0000234 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000235}
236
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000237//----------------------------------------------------------------------------
238// Print the IGnodes in the IGNode List
239//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000240void InterferenceGraph::printIGNodeList() const
241{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000242 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000243 const IGNode *const Node = IGNodeList[i];
244
Chris Lattner697954c2002-01-20 22:54:45 +0000245 if (Node) {
246 cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000247 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000248 //int Deg = Node->getCurDegree();
249 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
250 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000251 }
252}