blob: 3f57eccd89cf27e10ad97f99c38c847fb943337d [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- InterferenceGraph.cpp ---------------------------------------------===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Vikram S. Adve39c94e12002-09-14 23:05:33 +000010// Interference graph for coloring-based register allocation for LLVM.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc083dcc2003-09-01 20:05:47 +000014#include "IGNode.h"
Misha Brukmanf54c4372003-10-23 18:10:02 +000015#include "InterferenceGraph.h"
16#include "RegAllocCommon.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000018#include <algorithm>
Reid Spencer954da372004-07-04 12:19:56 +000019#include <iostream>
Ruchira Sasanka8e604792001-09-14 21:18:34 +000020
Brian Gaeked0fde302003-11-11 22:41:34 +000021namespace llvm {
22
Chris Lattner28760f42002-10-29 16:42:34 +000023// for asserting this IG node is infact in the IGNodeList of this class
24inline static void assertIGNode(const InterferenceGraph *IG,
25 const IGNode *Node) {
26 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
27}
28
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000029//-----------------------------------------------------------------------------
30// Constructor: Records the RegClass and initalizes IGNodeList.
31// The matrix is NOT yet created by the constructor. Call createGraph()
32// to create it after adding all IGNodes to the IGNodeList.
33//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000034InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000035 IG = NULL;
36 Size = 0;
Chris Lattnerc083dcc2003-09-01 20:05:47 +000037 if( DEBUG_RA >= RA_DEBUG_Interference)
38 std::cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000039}
40
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000041
42//-----------------------------------------------------------------------------
43// destructor. Deletes the bit matrix and all IGNodes
44//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000045InterferenceGraph:: ~InterferenceGraph() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000046 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000047 for(unsigned int r=0; r < IGNodeList.size(); ++r)
48 delete[] IG[r];
49 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000050
51 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000052 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000053}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000054
55
56
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000057//-----------------------------------------------------------------------------
58// Creates (dynamically allocates) the bit matrix necessary to hold the
59// interference graph.
60//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000061void InterferenceGraph::createGraph()
62{
63 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000064 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000065 for( unsigned int r=0; r < Size; ++r)
66 IG[r] = new char[Size];
67
68 // init IG matrix
69 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000070 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000071 IG[i][j] = 0;
72}
73
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000074//-----------------------------------------------------------------------------
75// creates a new IGNode for the given live range and add to IG
76//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000077void InterferenceGraph::addLRToIG(LiveRange *const LR)
78{
Chris Lattner697954c2002-01-20 22:54:45 +000079 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000080}
81
82
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000083//-----------------------------------------------------------------------------
84// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000085// update both the matrix and AdjLists of nodes.
86// If there is already an interference between LR1 and LR2, adj lists
87// are not updated. LR1 and LR2 must be distinct since if not, it suggests
88// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000090void InterferenceGraph::setInterference(const LiveRange *const LR1,
91 const LiveRange *const LR2 ) {
92 assert(LR1 != LR2);
93
Chris Lattner28760f42002-10-29 16:42:34 +000094 IGNode *IGNode1 = LR1->getUserIGNode();
95 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000096
Chris Lattner28760f42002-10-29 16:42:34 +000097 assertIGNode(this, IGNode1);
98 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099
Chris Lattner28760f42002-10-29 16:42:34 +0000100 unsigned row = IGNode1->getIndex();
101 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000102
103 char *val;
104
Vikram S. Adve993243e2002-09-15 15:33:48 +0000105 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000106 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000107
108 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
109
110 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000111 *val = 1; // add edges between nodes
112 IGNode1->addAdjIGNode( IGNode2 );
113 IGNode2->addAdjIGNode( IGNode1 );
114 }
115
116}
117
118
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000119//----------------------------------------------------------------------------
120// return whether two live ranges interfere
121//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000122unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
Chris Lattner4cfd6222003-01-15 21:00:02 +0000123 const LiveRange *const LR2) const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000124 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000125 assertIGNode(this, LR1->getUserIGNode());
126 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000127
128 const unsigned int row = LR1->getUserIGNode()->getIndex();
129 const unsigned int col = LR2->getUserIGNode()->getIndex();
130
131 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000132 if (row > col)
133 ret = IG[row][col];
134 else
135 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 return ret;
137
138}
139
140
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000141//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000142// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
143// Then the IGNode2L will be deleted. Necessary for coalescing.
144// IMPORTANT: The live ranges are NOT merged by this method. Use
145// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147
Chris Lattner296b7732002-02-05 02:52:05 +0000148void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
149 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000150
151 assert( LR1 != LR2); // cannot merge the same live range
152
153 IGNode *const DestNode = LR1->getUserIGNode();
154 IGNode *SrcNode = LR2->getUserIGNode();
155
Chris Lattner28760f42002-10-29 16:42:34 +0000156 assertIGNode(this, DestNode);
157 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000158
Vikram S. Adve993243e2002-09-15 15:33:48 +0000159 if( DEBUG_RA >= RA_DEBUG_Interference) {
Brian Gaekea308f802004-07-29 06:43:09 +0000160 std::cerr << "Merging LRs: \"" << *LR1 << "\" and \"" << *LR2 << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161 }
162
163 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
164 const unsigned SrcInd = SrcNode->getIndex();
165
166
167 // for all neighs of SrcNode
168 for(unsigned i=0; i < SrcDegree; i++) {
169 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
170
171 LiveRange *const LROfNeigh = NeighNode->getParentLR();
172
173 // delete edge between src and neigh - even neigh == dest
174 NeighNode->delAdjIGNode(SrcNode);
175
176 // set the matrix posn to 0 betn src and neigh - even neigh == dest
177 const unsigned NInd = NeighNode->getIndex();
178 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
179
180
181 if( LR1 != LROfNeigh) { // if the neigh != dest
182
183 // add edge betwn Dest and Neigh - if there is no current edge
184 setInterference(LR1, LROfNeigh );
185 }
186
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187 }
188
189 IGNodeList[ SrcInd ] = NULL;
190
191 // SrcNode is no longer necessary - LR2 must be deleted by the caller
192 delete( SrcNode );
193
194}
195
196
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000197//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198// must be called after modifications to the graph are over but before
199// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000201void InterferenceGraph::setCurDegreeOfIGNodes()
202{
203 unsigned Size = IGNodeList.size();
204
205 for( unsigned i=0; i < Size; i++) {
206 IGNode *Node = IGNodeList[i];
207 if( Node )
208 Node->setCurDegree();
209 }
210}
211
212
213
214
215
216//--------------------- debugging (Printing) methods -----------------------
217
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000218//----------------------------------------------------------------------------
219// Print the IGnodes
220//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000221void InterferenceGraph::printIG() const {
222 for(unsigned i=0; i < Size; i++) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000223 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000224 if(Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000225 std::cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000226
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000227 for( unsigned int j=0; j < Size; j++)
Chris Lattner697954c2002-01-20 22:54:45 +0000228 if(IG[i][j])
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000229 std::cerr << "(" << i << "," << j << ") ";
230 std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000231 }
Chris Lattner697954c2002-01-20 22:54:45 +0000232 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000233}
234
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235//----------------------------------------------------------------------------
236// Print the IGnodes in the IGNode List
237//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000238void InterferenceGraph::printIGNodeList() const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000239 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000240 const IGNode *const Node = IGNodeList[i];
Brian Gaekea308f802004-07-29 06:43:09 +0000241 if (Node)
242 std::cerr << " [" << Node->getIndex() << "] " << *Node->getParentLR()
243 << "\t <# of Neighbors: " << Node->getNumOfNeighbors() << ">\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000244 }
245}
Brian Gaeked0fde302003-11-11 22:41:34 +0000246
247} // End llvm namespace