blob: b213dc7e7d8865702832dfb8965be23f0bfb74c7 [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 Lattner4309e732003-01-15 19:57:07 +000014#include "RegAllocCommon.h"
Chris Lattner4cfd6222003-01-15 21:00:02 +000015#include "InterferenceGraph.h"
Chris Lattnerc083dcc2003-09-01 20:05:47 +000016#include "IGNode.h"
Vikram S. Adve39c94e12002-09-14 23:05:33 +000017#include "Support/STLExtras.h"
Chris Lattner30adeb62002-02-04 16:36:59 +000018#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000019using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000020
Chris Lattner28760f42002-10-29 16:42:34 +000021// for asserting this IG node is infact in the IGNodeList of this class
22inline static void assertIGNode(const InterferenceGraph *IG,
23 const IGNode *Node) {
24 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
25}
26
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000027//-----------------------------------------------------------------------------
28// Constructor: Records the RegClass and initalizes IGNodeList.
29// The matrix is NOT yet created by the constructor. Call createGraph()
30// to create it after adding all IGNodes to the IGNodeList.
31//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000032InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000033 IG = NULL;
34 Size = 0;
Chris Lattnerc083dcc2003-09-01 20:05:47 +000035 if( DEBUG_RA >= RA_DEBUG_Interference)
36 std::cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000037}
38
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000039
40//-----------------------------------------------------------------------------
41// destructor. Deletes the bit matrix and all IGNodes
42//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000043InterferenceGraph:: ~InterferenceGraph() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000044 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000045 for(unsigned int r=0; r < IGNodeList.size(); ++r)
46 delete[] IG[r];
47 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000048
49 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000050 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000051}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052
53
54
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000055//-----------------------------------------------------------------------------
56// Creates (dynamically allocates) the bit matrix necessary to hold the
57// interference graph.
58//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059void InterferenceGraph::createGraph()
60{
61 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000062 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000063 for( unsigned int r=0; r < Size; ++r)
64 IG[r] = new char[Size];
65
66 // init IG matrix
67 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000068 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000069 IG[i][j] = 0;
70}
71
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000072//-----------------------------------------------------------------------------
73// creates a new IGNode for the given live range and add to IG
74//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000075void InterferenceGraph::addLRToIG(LiveRange *const LR)
76{
Chris Lattner697954c2002-01-20 22:54:45 +000077 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000078}
79
80
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000081//-----------------------------------------------------------------------------
82// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000083// update both the matrix and AdjLists of nodes.
84// If there is already an interference between LR1 and LR2, adj lists
85// are not updated. LR1 and LR2 must be distinct since if not, it suggests
86// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000087//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000088void InterferenceGraph::setInterference(const LiveRange *const LR1,
89 const LiveRange *const LR2 ) {
90 assert(LR1 != LR2);
91
Chris Lattner28760f42002-10-29 16:42:34 +000092 IGNode *IGNode1 = LR1->getUserIGNode();
93 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000094
Chris Lattner28760f42002-10-29 16:42:34 +000095 assertIGNode(this, IGNode1);
96 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000097
Chris Lattner28760f42002-10-29 16:42:34 +000098 unsigned row = IGNode1->getIndex();
99 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000100
101 char *val;
102
Vikram S. Adve993243e2002-09-15 15:33:48 +0000103 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000104 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105
106 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
107
108 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000109 *val = 1; // add edges between nodes
110 IGNode1->addAdjIGNode( IGNode2 );
111 IGNode2->addAdjIGNode( IGNode1 );
112 }
113
114}
115
116
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000117//----------------------------------------------------------------------------
118// return whether two live ranges interfere
119//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000120unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
Chris Lattner4cfd6222003-01-15 21:00:02 +0000121 const LiveRange *const LR2) const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000122 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000123 assertIGNode(this, LR1->getUserIGNode());
124 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000125
126 const unsigned int row = LR1->getUserIGNode()->getIndex();
127 const unsigned int col = LR2->getUserIGNode()->getIndex();
128
129 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000130 if (row > col)
131 ret = IG[row][col];
132 else
133 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 return ret;
135
136}
137
138
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000139//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000140// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
141// Then the IGNode2L will be deleted. Necessary for coalescing.
142// IMPORTANT: The live ranges are NOT merged by this method. Use
143// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000144//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145
Chris Lattner296b7732002-02-05 02:52:05 +0000146void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
147 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000148
149 assert( LR1 != LR2); // cannot merge the same live range
150
151 IGNode *const DestNode = LR1->getUserIGNode();
152 IGNode *SrcNode = LR2->getUserIGNode();
153
Chris Lattner28760f42002-10-29 16:42:34 +0000154 assertIGNode(this, DestNode);
155 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000156
Vikram S. Adve993243e2002-09-15 15:33:48 +0000157 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000158 std::cerr << "Merging LRs: \""; printSet(*LR1);
159 std::cerr << "\" and \""; printSet(*LR2);
160 std::cerr << "\"\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];
241
Chris Lattner697954c2002-01-20 22:54:45 +0000242 if (Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000243 std::cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000244 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000245 //int Deg = Node->getCurDegree();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000246 std::cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000247 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000248 }
249}