blob: 3cef19ea0e0539940c35c2598f9e5d2ed5cc5321 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
21
Chris Lattner28760f42002-10-29 16:42:34 +000022// for asserting this IG node is infact in the IGNodeList of this class
23inline static void assertIGNode(const InterferenceGraph *IG,
24 const IGNode *Node) {
25 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
26}
27
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028//-----------------------------------------------------------------------------
29// Constructor: Records the RegClass and initalizes IGNodeList.
30// The matrix is NOT yet created by the constructor. Call createGraph()
31// to create it after adding all IGNodes to the IGNodeList.
32//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000033InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000034 IG = NULL;
35 Size = 0;
Chris Lattnerc083dcc2003-09-01 20:05:47 +000036 if( DEBUG_RA >= RA_DEBUG_Interference)
37 std::cerr << "Interference graph created!\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038}
39
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000040
41//-----------------------------------------------------------------------------
42// destructor. Deletes the bit matrix and all IGNodes
43//-----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +000044InterferenceGraph:: ~InterferenceGraph() {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000045 // delete the matrix
Chris Lattner697954c2002-01-20 22:54:45 +000046 for(unsigned int r=0; r < IGNodeList.size(); ++r)
47 delete[] IG[r];
48 delete[] IG;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000049
50 // delete all IGNodes in the IGNodeList
Chris Lattner697954c2002-01-20 22:54:45 +000051 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000052}
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053
54
55
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000056//-----------------------------------------------------------------------------
57// Creates (dynamically allocates) the bit matrix necessary to hold the
58// interference graph.
59//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060void InterferenceGraph::createGraph()
61{
62 Size = IGNodeList.size();
Chris Lattner697954c2002-01-20 22:54:45 +000063 IG = new char*[Size];
Ruchira Sasanka8e604792001-09-14 21:18:34 +000064 for( unsigned int r=0; r < Size; ++r)
65 IG[r] = new char[Size];
66
67 // init IG matrix
68 for(unsigned int i=0; i < Size; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000069 for(unsigned int j=0; j < Size; j++)
Ruchira Sasanka8e604792001-09-14 21:18:34 +000070 IG[i][j] = 0;
71}
72
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000073//-----------------------------------------------------------------------------
74// creates a new IGNode for the given live range and add to IG
75//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000076void InterferenceGraph::addLRToIG(LiveRange *const LR)
77{
Chris Lattner697954c2002-01-20 22:54:45 +000078 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
Ruchira Sasanka8e604792001-09-14 21:18:34 +000079}
80
81
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000082//-----------------------------------------------------------------------------
83// set interference for two live ranges
Ruchira Sasanka8e604792001-09-14 21:18:34 +000084// update both the matrix and AdjLists of nodes.
85// If there is already an interference between LR1 and LR2, adj lists
86// are not updated. LR1 and LR2 must be distinct since if not, it suggests
87// that there is some wrong logic in some other method.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088//-----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089void InterferenceGraph::setInterference(const LiveRange *const LR1,
90 const LiveRange *const LR2 ) {
91 assert(LR1 != LR2);
92
Chris Lattner28760f42002-10-29 16:42:34 +000093 IGNode *IGNode1 = LR1->getUserIGNode();
94 IGNode *IGNode2 = LR2->getUserIGNode();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095
Chris Lattner28760f42002-10-29 16:42:34 +000096 assertIGNode(this, IGNode1);
97 assertIGNode(this, IGNode2);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000098
Chris Lattner28760f42002-10-29 16:42:34 +000099 unsigned row = IGNode1->getIndex();
100 unsigned col = IGNode2->getIndex();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000101
102 char *val;
103
Vikram S. Adve993243e2002-09-15 15:33:48 +0000104 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000105 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000106
107 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
108
109 if( ! (*val) ) { // if this interf is not previously set
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000110 *val = 1; // add edges between nodes
111 IGNode1->addAdjIGNode( IGNode2 );
112 IGNode2->addAdjIGNode( IGNode1 );
113 }
114
115}
116
117
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000118//----------------------------------------------------------------------------
119// return whether two live ranges interfere
120//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000121unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
Chris Lattner4cfd6222003-01-15 21:00:02 +0000122 const LiveRange *const LR2) const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000123 assert(LR1 != LR2);
Chris Lattner28760f42002-10-29 16:42:34 +0000124 assertIGNode(this, LR1->getUserIGNode());
125 assertIGNode(this, LR2->getUserIGNode());
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000126
127 const unsigned int row = LR1->getUserIGNode()->getIndex();
128 const unsigned int col = LR2->getUserIGNode()->getIndex();
129
130 char ret;
Chris Lattner697954c2002-01-20 22:54:45 +0000131 if (row > col)
132 ret = IG[row][col];
133 else
134 ret = IG[col][row];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000135 return ret;
136
137}
138
139
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000140//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000141// Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
142// Then the IGNode2L will be deleted. Necessary for coalescing.
143// IMPORTANT: The live ranges are NOT merged by this method. Use
144// LiveRangeInfo::unionAndUpdateLRs for that purpose.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000145//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000146
Chris Lattner296b7732002-02-05 02:52:05 +0000147void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
148 LiveRange *LR2) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149
150 assert( LR1 != LR2); // cannot merge the same live range
151
152 IGNode *const DestNode = LR1->getUserIGNode();
153 IGNode *SrcNode = LR2->getUserIGNode();
154
Chris Lattner28760f42002-10-29 16:42:34 +0000155 assertIGNode(this, DestNode);
156 assertIGNode(this, SrcNode);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000157
Vikram S. Adve993243e2002-09-15 15:33:48 +0000158 if( DEBUG_RA >= RA_DEBUG_Interference) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000159 std::cerr << "Merging LRs: \""; printSet(*LR1);
160 std::cerr << "\" and \""; printSet(*LR2);
161 std::cerr << "\"\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162 }
163
164 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
165 const unsigned SrcInd = SrcNode->getIndex();
166
167
168 // for all neighs of SrcNode
169 for(unsigned i=0; i < SrcDegree; i++) {
170 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
171
172 LiveRange *const LROfNeigh = NeighNode->getParentLR();
173
174 // delete edge between src and neigh - even neigh == dest
175 NeighNode->delAdjIGNode(SrcNode);
176
177 // set the matrix posn to 0 betn src and neigh - even neigh == dest
178 const unsigned NInd = NeighNode->getIndex();
179 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
180
181
182 if( LR1 != LROfNeigh) { // if the neigh != dest
183
184 // add edge betwn Dest and Neigh - if there is no current edge
185 setInterference(LR1, LROfNeigh );
186 }
187
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000188 }
189
190 IGNodeList[ SrcInd ] = NULL;
191
192 // SrcNode is no longer necessary - LR2 must be deleted by the caller
193 delete( SrcNode );
194
195}
196
197
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000198//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000199// must be called after modifications to the graph are over but before
200// pushing IGNodes on to the stack for coloring.
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000201//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000202void InterferenceGraph::setCurDegreeOfIGNodes()
203{
204 unsigned Size = IGNodeList.size();
205
206 for( unsigned i=0; i < Size; i++) {
207 IGNode *Node = IGNodeList[i];
208 if( Node )
209 Node->setCurDegree();
210 }
211}
212
213
214
215
216
217//--------------------- debugging (Printing) methods -----------------------
218
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000219//----------------------------------------------------------------------------
220// Print the IGnodes
221//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000222void InterferenceGraph::printIG() const {
223 for(unsigned i=0; i < Size; i++) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000224 const IGNode *const Node = IGNodeList[i];
Chris Lattner697954c2002-01-20 22:54:45 +0000225 if(Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000226 std::cerr << " [" << i << "] ";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000228 for( unsigned int j=0; j < Size; j++)
Chris Lattner697954c2002-01-20 22:54:45 +0000229 if(IG[i][j])
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000230 std::cerr << "(" << i << "," << j << ") ";
231 std::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//----------------------------------------------------------------------------
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000239void InterferenceGraph::printIGNodeList() const {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000240 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000241 const IGNode *const Node = IGNodeList[i];
242
Chris Lattner697954c2002-01-20 22:54:45 +0000243 if (Node) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000244 std::cerr << " [" << Node->getIndex() << "] ";
Chris Lattner296b7732002-02-05 02:52:05 +0000245 printSet(*Node->getParentLR());
Chris Lattner697954c2002-01-20 22:54:45 +0000246 //int Deg = Node->getCurDegree();
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000247 std::cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000248 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000249 }
250}
Brian Gaeked0fde302003-11-11 22:41:34 +0000251
252} // End llvm namespace