blob: 6e461fea428f94da0e4d391125d627cc13d27b6e [file] [log] [blame]
Vikram S. Adve39c94e12002-09-14 23:05:33 +00001//===-- RegClass.cpp -----------------------------------------------------===//
2//
3// class RegClass for coloring-based register allocation for LLVM.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattneraf3cdcf2003-01-15 21:02:16 +00007#include "RegClass.h"
Chris Lattner4309e732003-01-15 19:57:07 +00008#include "RegAllocCommon.h"
Chris Lattnercb6b4bd2002-10-29 16:51:05 +00009#include "llvm/CodeGen/IGNode.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000010#include "llvm/Target/TargetRegInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::cerr;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000012
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000013//----------------------------------------------------------------------------
14// This constructor inits IG. The actual matrix is created by a call to
15// createInterferenceGraph() above.
16//----------------------------------------------------------------------------
Chris Lattner4d669b52002-04-08 22:01:15 +000017RegClass::RegClass(const Function *M,
Vikram S. Advebc001b22003-07-25 21:06:09 +000018 const TargetRegInfo *_MRI_,
19 const TargetRegClassInfo *_MRC_)
20 : Meth(M), MRI(_MRI_), MRC(_MRC_),
21 RegClassID( _MRC_->getRegClassID() ),
22 IG(this), IGNodeStack() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000023 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner697954c2002-01-20 22:54:45 +000024 cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000025
Vikram S. Advebc001b22003-07-25 21:06:09 +000026 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
Ruchira Sasanka8e604792001-09-14 21:18:34 +000027}
28
29
30
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000031//----------------------------------------------------------------------------
32// Main entry point for coloring a register class.
33//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000034void RegClass::colorAllRegs()
35{
Vikram S. Adve39c94e12002-09-14 23:05:33 +000036 if(DEBUG_RA >= RA_DEBUG_Coloring)
37 cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000038
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000039 // pre-color IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000040 pushAllIGNodes(); // push all IG Nodes
41
42 unsigned int StackSize = IGNodeStack.size();
43 IGNode *CurIGNode;
44
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000045 // for all LRs on stack
Ruchira Sasanka8e604792001-09-14 21:18:34 +000046 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
47
48 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
49 IGNodeStack.pop();
50 colorIGNode (CurIGNode); // color it
51 }
52
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053}
54
55
56
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000057//----------------------------------------------------------------------------
58// The method for pushing all IGNodes on to the stack.
59//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000060void RegClass::pushAllIGNodes()
61{
62 bool NeedMoreSpills;
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000063
Ruchira Sasanka8e604792001-09-14 21:18:34 +000064
65 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
66
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000067 // push non-constrained IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000068 bool PushedAll = pushUnconstrainedIGNodes();
69
Vikram S. Adve39c94e12002-09-14 23:05:33 +000070 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +000071 cerr << " Puhsed all-unconstrained IGNodes. ";
72 if( PushedAll ) cerr << " No constrained nodes left.";
73 cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000074 }
75
76 if( PushedAll ) // if NO constrained nodes left
77 return;
78
79
80 // now, we have constrained nodes. So, push one of them (the one with min
81 // spill cost) and try to push the others as unConstrained nodes.
82 // Repeat this.
83
Chris Lattner167b9622002-04-15 20:36:15 +000084 do {
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000085 //get node with min spill cost
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000086 //
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000087 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
88
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000089 // push that node on to stack
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000090 //
Chris Lattner167b9622002-04-15 20:36:15 +000091 IGNodeStack.push(IGNodeSpill);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000092
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000093 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000094 //
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000095 IGNodeSpill->pushOnStack();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000096
97 // now push NON-constrined ones, if any
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000098 //
Chris Lattner167b9622002-04-15 20:36:15 +000099 NeedMoreSpills = !pushUnconstrainedIGNodes();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000100
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000101 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner167b9622002-04-15 20:36:15 +0000102 cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000103
Chris Lattner167b9622002-04-15 20:36:15 +0000104 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105
106}
107
108
109
110
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000111//--------------------------------------------------------------------------
112// This method goes thru all IG nodes in the IGNodeList of an IG of a
113// register class and push any unconstrained IG node left (that is not
114// already pushed)
115//--------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000116
117bool RegClass::pushUnconstrainedIGNodes()
118{
119 // # of LRs for this reg class
120 unsigned int IGNodeListSize = IG.getIGNodeList().size();
121 bool pushedall = true;
122
123 // a pass over IGNodeList
124 for( unsigned i =0; i < IGNodeListSize; i++) {
125
126 // get IGNode i from IGNodeList
127 IGNode *IGNode = IG.getIGNodeList()[i];
128
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000129 if( !IGNode ) // can be null due to merging
130 continue;
131
132 // if already pushed on stack, continue. This can happen since this
133 // method can be called repeatedly until all constrained nodes are
134 // pushed
135 if( IGNode->isOnStack() )
136 continue;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000137 // if the degree of IGNode is lower
Vikram S. Advebc001b22003-07-25 21:06:09 +0000138 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000139 IGNodeStack.push( IGNode ); // push IGNode on to the stack
140 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
141
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000142 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << " pushed un-constrained IGNode " << IGNode->getIndex() ;
144 cerr << " on to stack\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000145 }
146 }
147 else pushedall = false; // we didn't push all live ranges
148
149 } // for
150
151 // returns true if we pushed all live ranges - else false
152 return pushedall;
153}
154
155
156
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000157//----------------------------------------------------------------------------
158// Get the IGNode withe the minimum spill cost
159//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160IGNode * RegClass::getIGNodeWithMinSpillCost()
161{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000163 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner0ceeb422002-10-22 23:34:11 +0000164 double MinSpillCost = 0;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165 IGNode *MinCostIGNode = NULL;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000166 bool isFirstNode = true;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000167
168 // pass over IGNodeList to find the IGNode with minimum spill cost
169 // among all IGNodes that are not yet pushed on to the stack
170 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000171 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000173
174 if( ! IGNode ) // can be null due to merging
175 continue;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000176
177 if( ! IGNode->isOnStack() ) {
178
Ruchira Sasankace773da2002-01-08 16:29:23 +0000179 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
180 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181
Ruchira Sasankace773da2002-01-08 16:29:23 +0000182 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 MinSpillCost = SpillCost;
184 MinCostIGNode = IGNode;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000185 isFirstNode = false;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000186 }
187
188 else if( MinSpillCost > SpillCost) {
189 MinSpillCost = SpillCost;
190 MinCostIGNode = IGNode;
191 }
192
193 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194 }
195
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000196 assert( MinCostIGNode && "No IGNode to spill");
197 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000198}
199
200
201
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000202//----------------------------------------------------------------------------
203// Color the IGNode using the machine specific code.
204//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000205void RegClass::colorIGNode(IGNode *const Node)
206{
207
208 if( ! Node->hasColor() ) { // not colored as an arg etc.
209
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210 // init all elements of to IsColorUsedAr false;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000211 clearColorsUsed();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000213 // initialize all colors used by neighbors of this node to true
214 LiveRange *LR = Node->getParentLR();
215 unsigned NumNeighbors = Node->getNumOfNeighbors();
216 for (unsigned n=0; n < NumNeighbors; n++) {
217 IGNode *NeighIGNode = Node->getAdjIGNode(n);
218 LiveRange *NeighLR = NeighIGNode->getParentLR();
219
Vikram S. Advebc001b22003-07-25 21:06:09 +0000220 // Don't use a color if it is in use by the neighbour,
221 // or is suggested for use by the neighbour,
222 // markColorsUsed() should be given the color and the reg type for
223 // LR, not for NeighLR, because it should mark registers used based on
224 // the type we are looking for, not on the regType for the neighbour.
225 if (NeighLR->hasColor())
226 this->markColorsUsed(NeighLR->getColor(),
227 MRI->getRegTypeForLR(NeighLR),
228 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
229 else if (NeighLR->hasSuggestedColor() &&
230 NeighLR->isSuggestedColorUsable())
231 this->markColorsUsed(NeighLR->getSuggestedColor(),
232 MRI->getRegTypeForLR(NeighLR),
233 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000234 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000235
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000236 // call the target specific code for coloring
237 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000238 MRC->colorIGNode(Node, IsColorUsedArr);
239 }
240 else {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000241 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000242 cerr << " Node " << Node->getIndex();
243 cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000244 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000245 }
246
247
248 if( !Node->hasColor() ) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000249 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner697954c2002-01-20 22:54:45 +0000250 cerr << " Node " << Node->getIndex();
251 cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000252 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000253 }
254
255}
256
Chris Lattnercb6b4bd2002-10-29 16:51:05 +0000257void RegClass::printIGNodeList() const {
258 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
259 IG.printIGNodeList();
260}
261
262void RegClass::printIG() {
263 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
264 IG.printIG();
265}
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000267