blob: 9e7372177b3bdf9daf1dccafd1ec2ae72d93f3ac [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
Chris Lattner28760f42002-10-29 16:42:34 +000013// for asserting this IG node is infact in the IGNodeList of this class
14inline static void assertIGNode(const InterferenceGraph *IG,
15 const IGNode *Node) {
16 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
17}
18
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000019//-----------------------------------------------------------------------------
20// Constructor: Records the RegClass and initalizes IGNodeList.
21// The matrix is NOT yet created by the constructor. Call createGraph()
22// to create it after adding all IGNodes to the IGNodeList.
23//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000024InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
25 IGNodeList()
26{
27 IG = NULL;
28 Size = 0;
Vikram S. Adve39c94e12002-09-14 23:05:33 +000029 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner697954c2002-01-20 22:54:45 +000030 cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000031 }
32}
33
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000034
35//-----------------------------------------------------------------------------
36// destructor. Deletes the bit matrix and all IGNodes
37//-----------------------------------------------------------------------------
38InterferenceGraph:: ~InterferenceGraph() {
39
40 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000041 for(unsigned int r=0; r < IGNodeList.size(); ++r)
42 delete[] IG[r];
43 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000044
45 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000046 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000047}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000048
49
50
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000051//-----------------------------------------------------------------------------
52// Creates (dynamically allocates) the bit matrix necessary to hold the
53// interference graph.
54//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000055void InterferenceGraph::createGraph()
56{
57 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000058 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059 for( unsigned int r=0; r < Size; ++r)
60 IG[r] = new char[Size];
61
62 // init IG matrix
63 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000064 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000065 IG[i][j] = 0;
66}
67
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000068//-----------------------------------------------------------------------------
69// creates a new IGNode for the given live range and add to IG
70//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000071void InterferenceGraph::addLRToIG(LiveRange *const LR)
72{
Chris Lattner697954c2002-01-20 22:54:45 +000073 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000074}
75
76
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000077//-----------------------------------------------------------------------------
78// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079// update both the matrix and AdjLists of nodes.
80// If there is already an interference between LR1 and LR2, adj lists
81// are not updated. LR1 and LR2 must be distinct since if not, it suggests
82// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000083//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084void InterferenceGraph::setInterference(const LiveRange *const LR1,
85 const LiveRange *const LR2 ) {
86 assert(LR1 != LR2);
87
Chris Lattner28760f42002-10-29 16:42:34 +000088 IGNode *IGNode1 = LR1->getUserIGNode();
89 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000090
Chris Lattner28760f42002-10-29 16:42:34 +000091 assertIGNode(this, IGNode1);
92 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093
Chris Lattner28760f42002-10-29 16:42:34 +000094 unsigned row = IGNode1->getIndex();
95 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000096
97 char *val;
98
Vikram S. Adve993243e2002-09-15 15:33:48 +000099 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +0000100 cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000101
102 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
103
104 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105 *val = 1; // add edges between nodes
106 IGNode1->addAdjIGNode( IGNode2 );
107 IGNode2->addAdjIGNode( IGNode1 );
108 }
109
110}
111
112
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000113//----------------------------------------------------------------------------
114// return whether two live ranges interfere
115//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
117 const LiveRange *const LR2 ) const {
118
119 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000120 assertIGNode(this, LR1->getUserIGNode());
121 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000122
123 const unsigned int row = LR1->getUserIGNode()->getIndex();
124 const unsigned int col = LR2->getUserIGNode()->getIndex();
125
126 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000127 if (row > col)
128 ret = IG[row][col];
129 else
130 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000131 return ret;
132
133}
134
135
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000136//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
138// Then the IGNode2L will be deleted. Necessary for coalescing.
139// IMPORTANT: The live ranges are NOT merged by this method. Use
140// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000141//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142
Chris Lattner296b7732002-02-05 02:52:05 +0000143void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
144 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145
146 assert( LR1 != LR2); // cannot merge the same live range
147
148 IGNode *const DestNode = LR1->getUserIGNode();
149 IGNode *SrcNode = LR2->getUserIGNode();
150
Chris Lattner28760f42002-10-29 16:42:34 +0000151 assertIGNode(this, DestNode);
152 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000153
Vikram S. Adve993243e2002-09-15 15:33:48 +0000154 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattner296b7732002-02-05 02:52:05 +0000155 cerr << "Merging LRs: \""; printSet(*LR1);
156 cerr << "\" and \""; printSet(*LR2);
Chris Lattner697954c2002-01-20 22:54:45 +0000157 cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158 }
159
160 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
161 const unsigned SrcInd = SrcNode->getIndex();
162
163
164 // for all neighs of SrcNode
165 for(unsigned i=0; i < SrcDegree; i++) {
166 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
167
168 LiveRange *const LROfNeigh = NeighNode->getParentLR();
169
170 // delete edge between src and neigh - even neigh == dest
171 NeighNode->delAdjIGNode(SrcNode);
172
173 // set the matrix posn to 0 betn src and neigh - even neigh == dest
174 const unsigned NInd = NeighNode->getIndex();
175 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
176
177
178 if( LR1 != LROfNeigh) { // if the neigh != dest
179
180 // add edge betwn Dest and Neigh - if there is no current edge
181 setInterference(LR1, LROfNeigh );
182 }
183
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000184 }
185
186 IGNodeList[ SrcInd ] = NULL;
187
188 // SrcNode is no longer necessary - LR2 must be deleted by the caller
189 delete( SrcNode );
190
191}
192
193
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000194//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000195// must be called after modifications to the graph are over but before
196// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000197//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198void InterferenceGraph::setCurDegreeOfIGNodes()
199{
200 unsigned Size = IGNodeList.size();
201
202 for( unsigned i=0; i < Size; i++) {
203 IGNode *Node = IGNodeList[i];
204 if( Node )
205 Node->setCurDegree();
206 }
207}
208
209
210
211
212
213//--------------------- debugging (Printing) methods -----------------------
214
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000215//----------------------------------------------------------------------------
216// Print the IGnodes
217//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000218void InterferenceGraph::printIG() const
219{
220
221 for(unsigned int i=0; i < Size; i++) {
222
223 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000224 if(Node) {
225 cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226
Chris Lattner697954c2002-01-20 22:54:45 +0000227 for( unsigned int j=0; j < i; j++) {
228 if(IG[i][j])
229 cerr << "(" << i << "," << j << ") ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000230 }
Chris Lattner697954c2002-01-20 22:54:45 +0000231 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232 }
Chris Lattner697954c2002-01-20 22:54:45 +0000233 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234}
235
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236//----------------------------------------------------------------------------
237// Print the IGnodes in the IGNode List
238//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000239void InterferenceGraph::printIGNodeList() const
240{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000241 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000242 const IGNode *const Node = IGNodeList[i];
243
Chris Lattner697954c2002-01-20 22:54:45 +0000244 if (Node) {
245 cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000246 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000247 //int Deg = Node->getCurDegree();
248 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
249 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000250 }
251}