blob: bc28ed47d40eb6b18261d3fffe11ae63822aa777 [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"
Chris Lattner4cfd6222003-01-15 21:00:02 +00008#include "InterferenceGraph.h"
Chris Lattnerc083dcc2003-09-01 20:05:47 +00009#include "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//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000025InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000026 IG = NULL;
27 Size = 0;
Chris Lattnerc083dcc2003-09-01 20:05:47 +000028 if( DEBUG_RA >= RA_DEBUG_Interference)
29 std::cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000030}
31
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000032
33//-----------------------------------------------------------------------------
34// destructor. Deletes the bit matrix and all IGNodes
35//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000036InterferenceGraph:: ~InterferenceGraph() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000037 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000038 for(unsigned int r=0; r < IGNodeList.size(); ++r)
39 delete[] IG[r];
40 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000041
42 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000043 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000044}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000045
46
47
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000048//-----------------------------------------------------------------------------
49// Creates (dynamically allocates) the bit matrix necessary to hold the
50// interference graph.
51//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052void InterferenceGraph::createGraph()
53{
54 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000055 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000056 for( unsigned int r=0; r < Size; ++r)
57 IG[r] = new char[Size];
58
59 // init IG matrix
60 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000061 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062 IG[i][j] = 0;
63}
64
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000065//-----------------------------------------------------------------------------
66// creates a new IGNode for the given live range and add to IG
67//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068void InterferenceGraph::addLRToIG(LiveRange *const LR)
69{
Chris Lattner697954c2002-01-20 22:54:45 +000070 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000071}
72
73
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000074//-----------------------------------------------------------------------------
75// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076// update both the matrix and AdjLists of nodes.
77// If there is already an interference between LR1 and LR2, adj lists
78// are not updated. LR1 and LR2 must be distinct since if not, it suggests
79// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000080//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000081void InterferenceGraph::setInterference(const LiveRange *const LR1,
82 const LiveRange *const LR2 ) {
83 assert(LR1 != LR2);
84
Chris Lattner28760f42002-10-29 16:42:34 +000085 IGNode *IGNode1 = LR1->getUserIGNode();
86 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087
Chris Lattner28760f42002-10-29 16:42:34 +000088 assertIGNode(this, IGNode1);
89 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000090
Chris Lattner28760f42002-10-29 16:42:34 +000091 unsigned row = IGNode1->getIndex();
92 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093
94 char *val;
95
Vikram S. Adve993243e2002-09-15 15:33:48 +000096 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000097 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000098
99 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
100
101 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102 *val = 1; // add edges between nodes
103 IGNode1->addAdjIGNode( IGNode2 );
104 IGNode2->addAdjIGNode( IGNode1 );
105 }
106
107}
108
109
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000110//----------------------------------------------------------------------------
111// return whether two live ranges interfere
112//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000113unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
Chris Lattner4cfd6222003-01-15 21:00:02 +0000114 const LiveRange *const LR2) const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000116 assertIGNode(this, LR1->getUserIGNode());
117 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000118
119 const unsigned int row = LR1->getUserIGNode()->getIndex();
120 const unsigned int col = LR2->getUserIGNode()->getIndex();
121
122 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000123 if (row > col)
124 ret = IG[row][col];
125 else
126 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127 return ret;
128
129}
130
131
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000132//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
134// Then the IGNode2L will be deleted. Necessary for coalescing.
135// IMPORTANT: The live ranges are NOT merged by this method. Use
136// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000137//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138
Chris Lattner296b7732002-02-05 02:52:05 +0000139void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
140 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141
142 assert( LR1 != LR2); // cannot merge the same live range
143
144 IGNode *const DestNode = LR1->getUserIGNode();
145 IGNode *SrcNode = LR2->getUserIGNode();
146
Chris Lattner28760f42002-10-29 16:42:34 +0000147 assertIGNode(this, DestNode);
148 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149
Vikram S. Adve993243e2002-09-15 15:33:48 +0000150 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000151 std::cerr << "Merging LRs: \""; printSet(*LR1);
152 std::cerr << "\" and \""; printSet(*LR2);
153 std::cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000154 }
155
156 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
157 const unsigned SrcInd = SrcNode->getIndex();
158
159
160 // for all neighs of SrcNode
161 for(unsigned i=0; i < SrcDegree; i++) {
162 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
163
164 LiveRange *const LROfNeigh = NeighNode->getParentLR();
165
166 // delete edge between src and neigh - even neigh == dest
167 NeighNode->delAdjIGNode(SrcNode);
168
169 // set the matrix posn to 0 betn src and neigh - even neigh == dest
170 const unsigned NInd = NeighNode->getIndex();
171 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
172
173
174 if( LR1 != LROfNeigh) { // if the neigh != dest
175
176 // add edge betwn Dest and Neigh - if there is no current edge
177 setInterference(LR1, LROfNeigh );
178 }
179
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000180 }
181
182 IGNodeList[ SrcInd ] = NULL;
183
184 // SrcNode is no longer necessary - LR2 must be deleted by the caller
185 delete( SrcNode );
186
187}
188
189
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000190//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000191// must be called after modifications to the graph are over but before
192// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000193//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194void InterferenceGraph::setCurDegreeOfIGNodes()
195{
196 unsigned Size = IGNodeList.size();
197
198 for( unsigned i=0; i < Size; i++) {
199 IGNode *Node = IGNodeList[i];
200 if( Node )
201 Node->setCurDegree();
202 }
203}
204
205
206
207
208
209//--------------------- debugging (Printing) methods -----------------------
210
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000211//----------------------------------------------------------------------------
212// Print the IGnodes
213//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000214void InterferenceGraph::printIG() const {
215 for(unsigned i=0; i < Size; i++) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000216 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000217 if(Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000218 std::cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000220 for( unsigned int j=0; j < Size; j++)
Chris Lattner697954c2002-01-20 22:54:45 +0000221 if(IG[i][j])
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000222 std::cerr << "(" << i << "," << j << ") ";
223 std::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//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000231void InterferenceGraph::printIGNodeList() const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233 const IGNode *const Node = IGNodeList[i];
234
Chris Lattner697954c2002-01-20 22:54:45 +0000235 if (Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000236 std::cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000237 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000238 //int Deg = Node->getCurDegree();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000239 std::cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000240 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000241 }
242}