blob: 392a96c11c622c2baaa4357f09ff5bdf5e120521 [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"
Vikram S. Adve39c94e12002-09-14 23:05:33 +000017#include "Support/STLExtras.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000018#include <algorithm>
Ruchira Sasanka8e604792001-09-14 21:18:34 +000019
Chris Lattner28760f42002-10-29 16:42:34 +000020// for asserting this IG node is infact in the IGNodeList of this class
21inline static void assertIGNode(const InterferenceGraph *IG,
22 const IGNode *Node) {
23 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
24}
25
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000026//-----------------------------------------------------------------------------
27// Constructor: Records the RegClass and initalizes IGNodeList.
28// The matrix is NOT yet created by the constructor. Call createGraph()
29// to create it after adding all IGNodes to the IGNodeList.
30//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000031InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000032 IG = NULL;
33 Size = 0;
Chris Lattnerc083dcc2003-09-01 20:05:47 +000034 if( DEBUG_RA >= RA_DEBUG_Interference)
35 std::cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000036}
37
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000038
39//-----------------------------------------------------------------------------
40// destructor. Deletes the bit matrix and all IGNodes
41//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000042InterferenceGraph:: ~InterferenceGraph() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000043 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000044 for(unsigned int r=0; r < IGNodeList.size(); ++r)
45 delete[] IG[r];
46 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000047
48 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000049 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000050}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000051
52
53
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000054//-----------------------------------------------------------------------------
55// Creates (dynamically allocates) the bit matrix necessary to hold the
56// interference graph.
57//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000058void InterferenceGraph::createGraph()
59{
60 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000061 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000062 for( unsigned int r=0; r < Size; ++r)
63 IG[r] = new char[Size];
64
65 // init IG matrix
66 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000067 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068 IG[i][j] = 0;
69}
70
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000071//-----------------------------------------------------------------------------
72// creates a new IGNode for the given live range and add to IG
73//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000074void InterferenceGraph::addLRToIG(LiveRange *const LR)
75{
Chris Lattner697954c2002-01-20 22:54:45 +000076 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000077}
78
79
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000080//-----------------------------------------------------------------------------
81// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000082// update both the matrix and AdjLists of nodes.
83// If there is already an interference between LR1 and LR2, adj lists
84// are not updated. LR1 and LR2 must be distinct since if not, it suggests
85// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000086//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000087void InterferenceGraph::setInterference(const LiveRange *const LR1,
88 const LiveRange *const LR2 ) {
89 assert(LR1 != LR2);
90
Chris Lattner28760f42002-10-29 16:42:34 +000091 IGNode *IGNode1 = LR1->getUserIGNode();
92 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093
Chris Lattner28760f42002-10-29 16:42:34 +000094 assertIGNode(this, IGNode1);
95 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000096
Chris Lattner28760f42002-10-29 16:42:34 +000097 unsigned row = IGNode1->getIndex();
98 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099
100 char *val;
101
Vikram S. Adve993243e2002-09-15 15:33:48 +0000102 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000103 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000104
105 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
106
107 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000108 *val = 1; // add edges between nodes
109 IGNode1->addAdjIGNode( IGNode2 );
110 IGNode2->addAdjIGNode( IGNode1 );
111 }
112
113}
114
115
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000116//----------------------------------------------------------------------------
117// return whether two live ranges interfere
118//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000119unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
Chris Lattner4cfd6222003-01-15 21:00:02 +0000120 const LiveRange *const LR2) const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000121 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000122 assertIGNode(this, LR1->getUserIGNode());
123 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000124
125 const unsigned int row = LR1->getUserIGNode()->getIndex();
126 const unsigned int col = LR2->getUserIGNode()->getIndex();
127
128 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000129 if (row > col)
130 ret = IG[row][col];
131 else
132 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000133 return ret;
134
135}
136
137
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000138//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
140// Then the IGNode2L will be deleted. Necessary for coalescing.
141// IMPORTANT: The live ranges are NOT merged by this method. Use
142// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000143//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144
Chris Lattner296b7732002-02-05 02:52:05 +0000145void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
146 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000147
148 assert( LR1 != LR2); // cannot merge the same live range
149
150 IGNode *const DestNode = LR1->getUserIGNode();
151 IGNode *SrcNode = LR2->getUserIGNode();
152
Chris Lattner28760f42002-10-29 16:42:34 +0000153 assertIGNode(this, DestNode);
154 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000155
Vikram S. Adve993243e2002-09-15 15:33:48 +0000156 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000157 std::cerr << "Merging LRs: \""; printSet(*LR1);
158 std::cerr << "\" and \""; printSet(*LR2);
159 std::cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160 }
161
162 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
163 const unsigned SrcInd = SrcNode->getIndex();
164
165
166 // for all neighs of SrcNode
167 for(unsigned i=0; i < SrcDegree; i++) {
168 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
169
170 LiveRange *const LROfNeigh = NeighNode->getParentLR();
171
172 // delete edge between src and neigh - even neigh == dest
173 NeighNode->delAdjIGNode(SrcNode);
174
175 // set the matrix posn to 0 betn src and neigh - even neigh == dest
176 const unsigned NInd = NeighNode->getIndex();
177 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
178
179
180 if( LR1 != LROfNeigh) { // if the neigh != dest
181
182 // add edge betwn Dest and Neigh - if there is no current edge
183 setInterference(LR1, LROfNeigh );
184 }
185
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000186 }
187
188 IGNodeList[ SrcInd ] = NULL;
189
190 // SrcNode is no longer necessary - LR2 must be deleted by the caller
191 delete( SrcNode );
192
193}
194
195
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197// must be called after modifications to the graph are over but before
198// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000199//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200void InterferenceGraph::setCurDegreeOfIGNodes()
201{
202 unsigned Size = IGNodeList.size();
203
204 for( unsigned i=0; i < Size; i++) {
205 IGNode *Node = IGNodeList[i];
206 if( Node )
207 Node->setCurDegree();
208 }
209}
210
211
212
213
214
215//--------------------- debugging (Printing) methods -----------------------
216
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000217//----------------------------------------------------------------------------
218// Print the IGnodes
219//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000220void InterferenceGraph::printIG() const {
221 for(unsigned i=0; i < Size; i++) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000222 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000223 if(Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000224 std::cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000225
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000226 for( unsigned int j=0; j < Size; j++)
Chris Lattner697954c2002-01-20 22:54:45 +0000227 if(IG[i][j])
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000228 std::cerr << "(" << i << "," << j << ") ";
229 std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000230 }
Chris Lattner697954c2002-01-20 22:54:45 +0000231 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232}
233
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000234//----------------------------------------------------------------------------
235// Print the IGnodes in the IGNode List
236//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000237void InterferenceGraph::printIGNodeList() const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000238 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000239 const IGNode *const Node = IGNodeList[i];
240
Chris Lattner697954c2002-01-20 22:54:45 +0000241 if (Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000242 std::cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000243 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000244 //int Deg = Node->getCurDegree();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000245 std::cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000246 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000247 }
248}