1 //===-- InterferenceGraph.cpp ---------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Interference graph for coloring-based register allocation for LLVM.
12 //===----------------------------------------------------------------------===//
14 #include "RegAllocCommon.h"
15 #include "InterferenceGraph.h"
17 #include "Support/STLExtras.h"
21 // for asserting this IG node is infact in the IGNodeList of this class
22 inline static void assertIGNode(const InterferenceGraph *IG,
24 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
27 //-----------------------------------------------------------------------------
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 //-----------------------------------------------------------------------------
32 InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC) {
35 if( DEBUG_RA >= RA_DEBUG_Interference)
36 std::cerr << "Interference graph created!\n";
40 //-----------------------------------------------------------------------------
41 // destructor. Deletes the bit matrix and all IGNodes
42 //-----------------------------------------------------------------------------
43 InterferenceGraph:: ~InterferenceGraph() {
45 for(unsigned int r=0; r < IGNodeList.size(); ++r)
49 // delete all IGNodes in the IGNodeList
50 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
55 //-----------------------------------------------------------------------------
56 // Creates (dynamically allocates) the bit matrix necessary to hold the
57 // interference graph.
58 //-----------------------------------------------------------------------------
59 void InterferenceGraph::createGraph()
61 Size = IGNodeList.size();
63 for( unsigned int r=0; r < Size; ++r)
64 IG[r] = new char[Size];
67 for(unsigned int i=0; i < Size; i++)
68 for(unsigned int j=0; j < Size; j++)
72 //-----------------------------------------------------------------------------
73 // creates a new IGNode for the given live range and add to IG
74 //-----------------------------------------------------------------------------
75 void InterferenceGraph::addLRToIG(LiveRange *const LR)
77 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
81 //-----------------------------------------------------------------------------
82 // set interference for two live ranges
83 // 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.
87 //-----------------------------------------------------------------------------
88 void InterferenceGraph::setInterference(const LiveRange *const LR1,
89 const LiveRange *const LR2 ) {
92 IGNode *IGNode1 = LR1->getUserIGNode();
93 IGNode *IGNode2 = LR2->getUserIGNode();
95 assertIGNode(this, IGNode1);
96 assertIGNode(this, IGNode2);
98 unsigned row = IGNode1->getIndex();
99 unsigned col = IGNode2->getIndex();
103 if( DEBUG_RA >= RA_DEBUG_Interference)
104 std::cerr << "setting intf for: [" << row << "][" << col << "]\n";
106 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
108 if( ! (*val) ) { // if this interf is not previously set
109 *val = 1; // add edges between nodes
110 IGNode1->addAdjIGNode( IGNode2 );
111 IGNode2->addAdjIGNode( IGNode1 );
117 //----------------------------------------------------------------------------
118 // return whether two live ranges interfere
119 //----------------------------------------------------------------------------
120 unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
121 const LiveRange *const LR2) const {
123 assertIGNode(this, LR1->getUserIGNode());
124 assertIGNode(this, LR2->getUserIGNode());
126 const unsigned int row = LR1->getUserIGNode()->getIndex();
127 const unsigned int col = LR2->getUserIGNode()->getIndex();
139 //----------------------------------------------------------------------------
140 // 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.
144 //----------------------------------------------------------------------------
146 void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
149 assert( LR1 != LR2); // cannot merge the same live range
151 IGNode *const DestNode = LR1->getUserIGNode();
152 IGNode *SrcNode = LR2->getUserIGNode();
154 assertIGNode(this, DestNode);
155 assertIGNode(this, SrcNode);
157 if( DEBUG_RA >= RA_DEBUG_Interference) {
158 std::cerr << "Merging LRs: \""; printSet(*LR1);
159 std::cerr << "\" and \""; printSet(*LR2);
163 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
164 const unsigned SrcInd = SrcNode->getIndex();
167 // for all neighs of SrcNode
168 for(unsigned i=0; i < SrcDegree; i++) {
169 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
171 LiveRange *const LROfNeigh = NeighNode->getParentLR();
173 // delete edge between src and neigh - even neigh == dest
174 NeighNode->delAdjIGNode(SrcNode);
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) ;
181 if( LR1 != LROfNeigh) { // if the neigh != dest
183 // add edge betwn Dest and Neigh - if there is no current edge
184 setInterference(LR1, LROfNeigh );
189 IGNodeList[ SrcInd ] = NULL;
191 // SrcNode is no longer necessary - LR2 must be deleted by the caller
197 //----------------------------------------------------------------------------
198 // must be called after modifications to the graph are over but before
199 // pushing IGNodes on to the stack for coloring.
200 //----------------------------------------------------------------------------
201 void InterferenceGraph::setCurDegreeOfIGNodes()
203 unsigned Size = IGNodeList.size();
205 for( unsigned i=0; i < Size; i++) {
206 IGNode *Node = IGNodeList[i];
208 Node->setCurDegree();
216 //--------------------- debugging (Printing) methods -----------------------
218 //----------------------------------------------------------------------------
220 //----------------------------------------------------------------------------
221 void InterferenceGraph::printIG() const {
222 for(unsigned i=0; i < Size; i++) {
223 const IGNode *const Node = IGNodeList[i];
225 std::cerr << " [" << i << "] ";
227 for( unsigned int j=0; j < Size; j++)
229 std::cerr << "(" << i << "," << j << ") ";
235 //----------------------------------------------------------------------------
236 // Print the IGnodes in the IGNode List
237 //----------------------------------------------------------------------------
238 void InterferenceGraph::printIGNodeList() const {
239 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
240 const IGNode *const Node = IGNodeList[i];
243 std::cerr << " [" << Node->getIndex() << "] ";
244 printSet(*Node->getParentLR());
245 //int Deg = Node->getCurDegree();
246 std::cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";