1 #include "llvm/CodeGen/InterferenceGraph.h"
3 //-----------------------------------------------------------------------------
4 // Constructor: Records the RegClass and initalizes IGNodeList.
5 // The matrix is NOT yet created by the constructor. Call createGraph()
6 // to create it after adding all IGNodes to the IGNodeList.
7 //-----------------------------------------------------------------------------
8 InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC),
14 cout << "Interference graph created!" << endl;
19 //-----------------------------------------------------------------------------
20 // destructor. Deletes the bit matrix and all IGNodes
21 //-----------------------------------------------------------------------------
22 InterferenceGraph:: ~InterferenceGraph() {
29 // delete all IGNodes in the IGNodeList
31 vector<IGNode *>::const_iterator IGIt = IGNodeList.begin();
32 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
34 const IGNode *const Node = IGNodeList[i];
35 if( Node ) delete Node;
42 //-----------------------------------------------------------------------------
43 // Creates (dynamically allocates) the bit matrix necessary to hold the
44 // interference graph.
45 //-----------------------------------------------------------------------------
46 void InterferenceGraph::createGraph()
48 Size = IGNodeList.size();
49 IG = (char **) new char *[Size];
50 for( unsigned int r=0; r < Size; ++r)
51 IG[r] = new char[Size];
54 for(unsigned int i=0; i < Size; i++)
55 for( unsigned int j=0; j < Size ; j++)
59 //-----------------------------------------------------------------------------
60 // creates a new IGNode for the given live range and add to IG
61 //-----------------------------------------------------------------------------
62 void InterferenceGraph::addLRToIG(LiveRange *const LR)
64 IGNode *Node = new IGNode(LR, IGNodeList.size() );
65 IGNodeList.push_back( Node );
70 //-----------------------------------------------------------------------------
71 // set interference for two live ranges
72 // update both the matrix and AdjLists of nodes.
73 // If there is already an interference between LR1 and LR2, adj lists
74 // are not updated. LR1 and LR2 must be distinct since if not, it suggests
75 // that there is some wrong logic in some other method.
76 //-----------------------------------------------------------------------------
77 void InterferenceGraph::setInterference(const LiveRange *const LR1,
78 const LiveRange *const LR2 ) {
81 IGNode *const IGNode1 = LR1->getUserIGNode();
82 IGNode *const IGNode2 = LR2->getUserIGNode();
85 assertIGNode( IGNode1 );
86 assertIGNode( IGNode2 );
89 const unsigned int row = IGNode1->getIndex();
90 const unsigned int col = IGNode2->getIndex();
95 cout << "setting intf for: [" << row << "][" << col << "]" << endl;
97 ( row > col) ? val = &IG[row][col]: val = &IG[col][row];
99 if( ! (*val) ) { // if this interf is not previously set
101 *val = 1; // add edges between nodes
102 IGNode1->addAdjIGNode( IGNode2 );
103 IGNode2->addAdjIGNode( IGNode1 );
109 //----------------------------------------------------------------------------
110 // return whether two live ranges interfere
111 //----------------------------------------------------------------------------
112 unsigned InterferenceGraph::getInterference(const LiveRange *const LR1,
113 const LiveRange *const LR2 ) const {
118 assertIGNode( LR1->getUserIGNode() );
119 assertIGNode( LR2->getUserIGNode() );
122 const unsigned int row = LR1->getUserIGNode()->getIndex();
123 const unsigned int col = LR2->getUserIGNode()->getIndex();
126 ( row > col) ? (ret = IG[row][col]) : (ret = IG[col][row]) ;
132 //----------------------------------------------------------------------------
133 // Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode.
134 // Then the IGNode2L will be deleted. Necessary for coalescing.
135 // IMPORTANT: The live ranges are NOT merged by this method. Use
136 // LiveRangeInfo::unionAndUpdateLRs for that purpose.
137 //----------------------------------------------------------------------------
139 void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *const LR1,
140 LiveRange *const LR2 ) {
142 assert( LR1 != LR2); // cannot merge the same live range
144 IGNode *const DestNode = LR1->getUserIGNode();
145 IGNode *SrcNode = LR2->getUserIGNode();
147 assertIGNode( DestNode );
148 assertIGNode( SrcNode );
151 cout << "Merging LRs: \""; LR1->printSet();
152 cout << "\" and \""; LR2->printSet();
153 cout << "\"" << endl;
156 unsigned SrcDegree = SrcNode->getNumOfNeighbors();
157 const unsigned SrcInd = SrcNode->getIndex();
160 // for all neighs of SrcNode
161 for(unsigned i=0; i < SrcDegree; i++) {
162 IGNode *NeighNode = SrcNode->getAdjIGNode(i);
164 LiveRange *const LROfNeigh = NeighNode->getParentLR();
166 // delete edge between src and neigh - even neigh == dest
167 NeighNode->delAdjIGNode(SrcNode);
169 // set the matrix posn to 0 betn src and neigh - even neigh == dest
170 const unsigned NInd = NeighNode->getIndex();
171 ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ;
174 if( LR1 != LROfNeigh) { // if the neigh != dest
176 // add edge betwn Dest and Neigh - if there is no current edge
177 setInterference(LR1, LROfNeigh );
182 IGNodeList[ SrcInd ] = NULL;
184 // SrcNode is no longer necessary - LR2 must be deleted by the caller
190 //----------------------------------------------------------------------------
191 // must be called after modifications to the graph are over but before
192 // pushing IGNodes on to the stack for coloring.
193 //----------------------------------------------------------------------------
194 void InterferenceGraph::setCurDegreeOfIGNodes()
196 unsigned Size = IGNodeList.size();
198 for( unsigned i=0; i < Size; i++) {
199 IGNode *Node = IGNodeList[i];
201 Node->setCurDegree();
209 //--------------------- debugging (Printing) methods -----------------------
211 //----------------------------------------------------------------------------
213 //----------------------------------------------------------------------------
214 void InterferenceGraph::printIG() const
217 for(unsigned int i=0; i < Size; i++) {
219 const IGNode *const Node = IGNodeList[i];
221 continue; // skip empty rows
223 cout << " [" << i << "] ";
225 for( unsigned int j=0; j < Size; j++) {
227 if( IG[i][j] ) cout << "(" << i << "," << j << ") ";
233 //----------------------------------------------------------------------------
234 // Print the IGnodes in the IGNode List
235 //----------------------------------------------------------------------------
236 void InterferenceGraph::printIGNodeList() const
238 vector<IGNode *>::const_iterator IGIt = IGNodeList.begin(); // hash map iter
240 for(unsigned i=0; i < IGNodeList.size() ; ++i) {
242 const IGNode *const Node = IGNodeList[i];
247 cout << " [" << Node->getIndex() << "] ";
248 (Node->getParentLR())->printSet();
249 //int Deg = Node->getCurDegree();
250 cout << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">" << endl;