Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 1 | #include "llvm/CodeGen/InterferenceGraph.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 2 | #include "Support/STLExtras.h" |
| 3 | #include <iostream> |
Chris Lattner | 30adeb6 | 2002-02-04 16:36:59 +0000 | [diff] [blame^] | 4 | #include <algorithm> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 5 | using std::cerr; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 6 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 7 | //----------------------------------------------------------------------------- |
| 8 | // Constructor: Records the RegClass and initalizes IGNodeList. |
| 9 | // The matrix is NOT yet created by the constructor. Call createGraph() |
| 10 | // to create it after adding all IGNodes to the IGNodeList. |
| 11 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 12 | InterferenceGraph::InterferenceGraph(RegClass *const RC) : RegCl(RC), |
| 13 | IGNodeList() |
| 14 | { |
| 15 | IG = NULL; |
| 16 | Size = 0; |
| 17 | if( DEBUG_RA) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 18 | cerr << "Interference graph created!\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 19 | } |
| 20 | } |
| 21 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 22 | |
| 23 | //----------------------------------------------------------------------------- |
| 24 | // destructor. Deletes the bit matrix and all IGNodes |
| 25 | //----------------------------------------------------------------------------- |
| 26 | InterferenceGraph:: ~InterferenceGraph() { |
| 27 | |
| 28 | // delete the matrix |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 29 | for(unsigned int r=0; r < IGNodeList.size(); ++r) |
| 30 | delete[] IG[r]; |
| 31 | delete[] IG; |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 32 | |
| 33 | // delete all IGNodes in the IGNodeList |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 34 | for_each(IGNodeList.begin(), IGNodeList.end(), deleter<IGNode>); |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 35 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 39 | //----------------------------------------------------------------------------- |
| 40 | // Creates (dynamically allocates) the bit matrix necessary to hold the |
| 41 | // interference graph. |
| 42 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 43 | void InterferenceGraph::createGraph() |
| 44 | { |
| 45 | Size = IGNodeList.size(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 46 | IG = new char*[Size]; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 47 | for( unsigned int r=0; r < Size; ++r) |
| 48 | IG[r] = new char[Size]; |
| 49 | |
| 50 | // init IG matrix |
| 51 | for(unsigned int i=0; i < Size; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 52 | for(unsigned int j=0; j < Size; j++) |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 53 | IG[i][j] = 0; |
| 54 | } |
| 55 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 56 | //----------------------------------------------------------------------------- |
| 57 | // creates a new IGNode for the given live range and add to IG |
| 58 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 59 | void InterferenceGraph::addLRToIG(LiveRange *const LR) |
| 60 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 61 | IGNodeList.push_back(new IGNode(LR, IGNodeList.size())); |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 65 | //----------------------------------------------------------------------------- |
| 66 | // set interference for two live ranges |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 67 | // update both the matrix and AdjLists of nodes. |
| 68 | // If there is already an interference between LR1 and LR2, adj lists |
| 69 | // are not updated. LR1 and LR2 must be distinct since if not, it suggests |
| 70 | // that there is some wrong logic in some other method. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 71 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 72 | void InterferenceGraph::setInterference(const LiveRange *const LR1, |
| 73 | const LiveRange *const LR2 ) { |
| 74 | assert(LR1 != LR2); |
| 75 | |
| 76 | IGNode *const IGNode1 = LR1->getUserIGNode(); |
| 77 | IGNode *const IGNode2 = LR2->getUserIGNode(); |
| 78 | |
| 79 | if( DEBUG_RA) { |
| 80 | assertIGNode( IGNode1 ); |
| 81 | assertIGNode( IGNode2 ); |
| 82 | } |
| 83 | |
| 84 | const unsigned int row = IGNode1->getIndex(); |
| 85 | const unsigned int col = IGNode2->getIndex(); |
| 86 | |
| 87 | char *val; |
| 88 | |
| 89 | if( DEBUG_RA > 1) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | cerr << "setting intf for: [" << row << "][" << col << "]\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 91 | |
| 92 | ( row > col) ? val = &IG[row][col]: val = &IG[col][row]; |
| 93 | |
| 94 | if( ! (*val) ) { // if this interf is not previously set |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 95 | *val = 1; // add edges between nodes |
| 96 | IGNode1->addAdjIGNode( IGNode2 ); |
| 97 | IGNode2->addAdjIGNode( IGNode1 ); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 103 | //---------------------------------------------------------------------------- |
| 104 | // return whether two live ranges interfere |
| 105 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 106 | unsigned InterferenceGraph::getInterference(const LiveRange *const LR1, |
| 107 | const LiveRange *const LR2 ) const { |
| 108 | |
| 109 | assert(LR1 != LR2); |
| 110 | |
| 111 | if( DEBUG_RA) { |
| 112 | assertIGNode( LR1->getUserIGNode() ); |
| 113 | assertIGNode( LR2->getUserIGNode() ); |
| 114 | } |
| 115 | |
| 116 | const unsigned int row = LR1->getUserIGNode()->getIndex(); |
| 117 | const unsigned int col = LR2->getUserIGNode()->getIndex(); |
| 118 | |
| 119 | char ret; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | if (row > col) |
| 121 | ret = IG[row][col]; |
| 122 | else |
| 123 | ret = IG[col][row]; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 124 | return ret; |
| 125 | |
| 126 | } |
| 127 | |
| 128 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 129 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 130 | // Merge 2 IGNodes. The neighbors of the SrcNode will be added to the DestNode. |
| 131 | // Then the IGNode2L will be deleted. Necessary for coalescing. |
| 132 | // IMPORTANT: The live ranges are NOT merged by this method. Use |
| 133 | // LiveRangeInfo::unionAndUpdateLRs for that purpose. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 134 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 135 | |
| 136 | void InterferenceGraph::mergeIGNodesOfLRs(const LiveRange *const LR1, |
| 137 | LiveRange *const LR2 ) { |
| 138 | |
| 139 | assert( LR1 != LR2); // cannot merge the same live range |
| 140 | |
| 141 | IGNode *const DestNode = LR1->getUserIGNode(); |
| 142 | IGNode *SrcNode = LR2->getUserIGNode(); |
| 143 | |
| 144 | assertIGNode( DestNode ); |
| 145 | assertIGNode( SrcNode ); |
| 146 | |
| 147 | if( DEBUG_RA > 1) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 148 | cerr << "Merging LRs: \""; LR1->printSet(); |
| 149 | cerr << "\" and \""; LR2->printSet(); |
| 150 | cerr << "\"\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | unsigned SrcDegree = SrcNode->getNumOfNeighbors(); |
| 154 | const unsigned SrcInd = SrcNode->getIndex(); |
| 155 | |
| 156 | |
| 157 | // for all neighs of SrcNode |
| 158 | for(unsigned i=0; i < SrcDegree; i++) { |
| 159 | IGNode *NeighNode = SrcNode->getAdjIGNode(i); |
| 160 | |
| 161 | LiveRange *const LROfNeigh = NeighNode->getParentLR(); |
| 162 | |
| 163 | // delete edge between src and neigh - even neigh == dest |
| 164 | NeighNode->delAdjIGNode(SrcNode); |
| 165 | |
| 166 | // set the matrix posn to 0 betn src and neigh - even neigh == dest |
| 167 | const unsigned NInd = NeighNode->getIndex(); |
| 168 | ( SrcInd > NInd) ? (IG[SrcInd][NInd]=0) : (IG[NInd][SrcInd]=0) ; |
| 169 | |
| 170 | |
| 171 | if( LR1 != LROfNeigh) { // if the neigh != dest |
| 172 | |
| 173 | // add edge betwn Dest and Neigh - if there is no current edge |
| 174 | setInterference(LR1, LROfNeigh ); |
| 175 | } |
| 176 | |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | IGNodeList[ SrcInd ] = NULL; |
| 180 | |
| 181 | // SrcNode is no longer necessary - LR2 must be deleted by the caller |
| 182 | delete( SrcNode ); |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 187 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 188 | // must be called after modifications to the graph are over but before |
| 189 | // pushing IGNodes on to the stack for coloring. |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 190 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 191 | void InterferenceGraph::setCurDegreeOfIGNodes() |
| 192 | { |
| 193 | unsigned Size = IGNodeList.size(); |
| 194 | |
| 195 | for( unsigned i=0; i < Size; i++) { |
| 196 | IGNode *Node = IGNodeList[i]; |
| 197 | if( Node ) |
| 198 | Node->setCurDegree(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | //--------------------- debugging (Printing) methods ----------------------- |
| 207 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 208 | //---------------------------------------------------------------------------- |
| 209 | // Print the IGnodes |
| 210 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 211 | void InterferenceGraph::printIG() const |
| 212 | { |
| 213 | |
| 214 | for(unsigned int i=0; i < Size; i++) { |
| 215 | |
| 216 | const IGNode *const Node = IGNodeList[i]; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 217 | if(Node) { |
| 218 | cerr << " [" << i << "] "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 220 | for( unsigned int j=0; j < i; j++) { |
| 221 | if(IG[i][j]) |
| 222 | cerr << "(" << i << "," << j << ") "; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 223 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 224 | cerr << "\n"; |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 226 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ruchira Sasanka | 4f3eb22 | 2002-01-07 19:19:18 +0000 | [diff] [blame] | 229 | //---------------------------------------------------------------------------- |
| 230 | // Print the IGnodes in the IGNode List |
| 231 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 232 | void InterferenceGraph::printIGNodeList() const |
| 233 | { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 234 | for(unsigned i=0; i < IGNodeList.size() ; ++i) { |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 235 | const IGNode *const Node = IGNodeList[i]; |
| 236 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 237 | if (Node) { |
| 238 | cerr << " [" << Node->getIndex() << "] "; |
| 239 | Node->getParentLR()->printSet(); |
| 240 | //int Deg = Node->getCurDegree(); |
| 241 | cerr << "\t <# of Neighs: " << Node->getNumOfNeighbors() << ">\n"; |
| 242 | } |
Ruchira Sasanka | 8e60479 | 2001-09-14 21:18:34 +0000 | [diff] [blame] | 243 | } |
| 244 | } |