1 //===-- InterferenceGraph.cpp ---------------------------------------------===//
3 // Interference graph for coloring-based register allocation for LLVM.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/CodeGen/InterferenceGraph.h"
8 #include "llvm/CodeGen/RegAllocCommon.h"
9 #include "Support/STLExtras.h"
13 // for asserting this IG node is infact in the IGNodeList of this class
14 inline static void assertIGNode(const InterferenceGraph *IG,
16 assert(IG->getIGNodeList()[Node->getIndex()] == Node);
19 //-----------------------------------------------------------------------------
20 // Constructor: Records the RegClass and initalizes IGNodeList.
21 // The matrix is NOT yet created by the constructor. Call createGraph()
22 // to create it after adding all IGNodes to the IGNodeList.
23 //-----------------------------------------------------------------------------
24 InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
29 if( DEBUG_RA >= RA_DEBUG_Interference) {
30 cerr << "Interference graph created!\n";
35 //-----------------------------------------------------------------------------
36 // destructor. Deletes the bit matrix and all IGNodes
37 //-----------------------------------------------------------------------------
38 InterferenceGraph:: ~InterferenceGraph() {
41 for(unsigned int r=0; r < IGNodeList.size(); ++r)
45 // delete all IGNodes in the IGNodeList
46 for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>);
51 //-----------------------------------------------------------------------------
52 // Creates (dynamically allocates) the bit matrix necessary to hold the
53 // interference graph.
54 //-----------------------------------------------------------------------------
55 void InterferenceGraph::createGraph()
57 Size = IGNodeList.size();
59 for( unsigned int r=0; r < Size; ++r)
60 IG[r] = new char[Size];
63 for(unsigned int i=0; i < Size; i++)
64 for(unsigned int j=0; j < Size; j++)
68 //-----------------------------------------------------------------------------
69 // creates a new IGNode for the given live range and add to IG
70 //-----------------------------------------------------------------------------
71 void InterferenceGraph::addLRToIG(LiveRange *const LR)
73 IGNodeList.push_back(new IGNode(LR, IGNodeList.size()));
77 //-----------------------------------------------------------------------------
78 // set interference for two live ranges
79 // update both the matrix and AdjLists of nodes.
80 // If there is already an interference between LR1 and LR2, adj lists
81 // are not updated. LR1 and LR2 must be distinct since if not, it suggests
82 // that there is some wrong logic in some other method.
83 //-----------------------------------------------------------------------------
84 void InterferenceGraph::setInterference(const LiveRange *const LR1,
85 const LiveRange *const LR2 ) {
88 IGNode *IGNode1 = LR1->getUserIGNode();
89 IGNode *IGNode2 = LR2->getUserIGNode();
91 assertIGNode(this, IGNode1);
92 assertIGNode(this, IGNode2);
94 unsigned row = IGNode1->getIndex();
95 unsigned col = IGNode2->getIndex();
99 if( DEBUG_RA >= RA_DEBUG_Interference)
100 cerr << "setting intf for: [" << row << "][" << col << "]\n";
102 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
104 if( ! (*val) ) { // if this interf is not previously set
105 *val = 1; // add edges between nodes
106 IGNode1->addAdjIGNode( IGNode2 );
107 IGNode2->addAdjIGNode( IGNode1 );
113 //----------------------------------------------------------------------------
114 // return whether two live ranges interfere
115 //----------------------------------------------------------------------------
116 unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
117 const LiveRange *const LR2 ) const {
120 assertIGNode(this, LR1->getUserIGNode());
121 assertIGNode(this, LR2->getUserIGNode());
123 const unsigned int row = LR1->getUserIGNode()->getIndex();
124 const unsigned int col = LR2->getUserIGNode()->getIndex();
136 //----------------------------------------------------------------------------
137 // Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
138 // Then the IGNode2L will be deleted. Necessary for coalescing.
139 // IMPORTANT: The live ranges are NOT merged by this method. Use
140 // LiveRangeInfo::unionAndUpdateLRs for that purpose.
141 //----------------------------------------------------------------------------
143 void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *LR1,
146 assert( LR1 != LR2); // cannot merge the same live range
148 IGNode *const DestNode = LR1->getUserIGNode();
149 IGNode *SrcNode = LR2->getUserIGNode();
151 assertIGNode(this, DestNode);
152 assertIGNode(this, SrcNode);
154 if( DEBUG_RA >= RA_DEBUG_Interference) {
155 cerr << "Merging LRs: \""; printSet(*LR1);
156 cerr << "\" and \""; printSet(*LR2);
160 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
161 const unsigned SrcInd = SrcNode->getIndex();
164 // for all neighs of SrcNode
165 for(unsigned i=0; i < SrcDegree; i++) {
166 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
168 LiveRange *const LROfNeigh = NeighNode->getParentLR();
170 // delete edge between src and neigh - even neigh == dest
171 NeighNode->delAdjIGNode(SrcNode);
173 // set the matrix posn to 0 betn src and neigh - even neigh == dest
174 const unsigned NInd = NeighNode->getIndex();
175 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
178 if( LR1 != LROfNeigh) { // if the neigh != dest
180 // add edge betwn Dest and Neigh - if there is no current edge
181 setInterference(LR1, LROfNeigh );
186 IGNodeList[ SrcInd ] = NULL;
188 // SrcNode is no longer necessary - LR2 must be deleted by the caller
194 //----------------------------------------------------------------------------
195 // must be called after modifications to the graph are over but before
196 // pushing IGNodes on to the stack for coloring.
197 //----------------------------------------------------------------------------
198 void InterferenceGraph::setCurDegreeOfIGNodes()
200 unsigned Size = IGNodeList.size();
202 for( unsigned i=0; i < Size; i++) {
203 IGNode *Node = IGNodeList[i];
205 Node->setCurDegree();
213 //--------------------- debugging (Printing) methods -----------------------
215 //----------------------------------------------------------------------------
217 //----------------------------------------------------------------------------
218 void InterferenceGraph::printIG() const
221 for(unsigned int i=0; i < Size; i++) {
223 const IGNode *const Node = IGNodeList[i];
225 cerr << " [" << i << "] ";
227 for( unsigned int j=0; j < i; j++) {
229 cerr << "(" << i << "," << j << ") ";
236 //----------------------------------------------------------------------------
237 // Print the IGnodes in the IGNode List
238 //----------------------------------------------------------------------------
239 void InterferenceGraph::printIGNodeList() const
241 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
242 const IGNode *const Node = IGNodeList[i];
245 cerr << " [" << Node->getIndex() << "] ";
246 printSet(*Node->getParentLR());
247 //int Deg = Node->getCurDegree();
248 cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n";