blob: d4bd7146b2cf2b27c2e3a9f0a4e65e4d44ce4640 [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 Lattnerc083dcc2003-09-01 20:05:47 +00009#include "IGNode.h"
Vikram S. Advebc001b22003-07-25 21:06:09 +000010#include "llvm/Target/TargetRegInfo.h"
Ruchira Sasanka8e604792001-09-14 21:18:34 +000011
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000012//----------------------------------------------------------------------------
13// This constructor inits IG. The actual matrix is created by a call to
14// createInterferenceGraph() above.
15//----------------------------------------------------------------------------
Chris Lattner4d669b52002-04-08 22:01:15 +000016RegClass::RegClass(const Function *M,
Vikram S. Advebc001b22003-07-25 21:06:09 +000017 const TargetRegInfo *_MRI_,
18 const TargetRegClassInfo *_MRC_)
19 : Meth(M), MRI(_MRI_), MRC(_MRC_),
20 RegClassID( _MRC_->getRegClassID() ),
21 IG(this), IGNodeStack() {
Vikram S. Adve39c94e12002-09-14 23:05:33 +000022 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000023 std::cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000024
Vikram S. Advebc001b22003-07-25 21:06:09 +000025 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
Ruchira Sasanka8e604792001-09-14 21:18:34 +000026}
27
28
29
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000030//----------------------------------------------------------------------------
31// Main entry point for coloring a register class.
32//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000033void RegClass::colorAllRegs()
34{
Vikram S. Adve39c94e12002-09-14 23:05:33 +000035 if(DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattnerc083dcc2003-09-01 20:05:47 +000036 std::cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000037
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000038 // pre-color IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000039 pushAllIGNodes(); // push all IG Nodes
40
41 unsigned int StackSize = IGNodeStack.size();
42 IGNode *CurIGNode;
43
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000044 // for all LRs on stack
Ruchira Sasanka8e604792001-09-14 21:18:34 +000045 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
46
47 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
48 IGNodeStack.pop();
49 colorIGNode (CurIGNode); // color it
50 }
51
Ruchira Sasanka8e604792001-09-14 21:18:34 +000052}
53
54
55
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000056//----------------------------------------------------------------------------
57// The method for pushing all IGNodes on to the stack.
58//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000059void RegClass::pushAllIGNodes()
60{
61 bool NeedMoreSpills;
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000062
Ruchira Sasanka8e604792001-09-14 21:18:34 +000063
64 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
65
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000066 // push non-constrained IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000067 bool PushedAll = pushUnconstrainedIGNodes();
68
Vikram S. Adve39c94e12002-09-14 23:05:33 +000069 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +000070 std::cerr << " Puhsed all-unconstrained IGNodes. ";
71 if( PushedAll ) std::cerr << " No constrained nodes left.";
72 std::cerr << "\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000073 }
74
75 if( PushedAll ) // if NO constrained nodes left
76 return;
77
78
79 // now, we have constrained nodes. So, push one of them (the one with min
80 // spill cost) and try to push the others as unConstrained nodes.
81 // Repeat this.
82
Chris Lattner167b9622002-04-15 20:36:15 +000083 do {
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000084 //get node with min spill cost
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000085 //
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000086 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
87
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000088 // push that node on to stack
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000089 //
Chris Lattner167b9622002-04-15 20:36:15 +000090 IGNodeStack.push(IGNodeSpill);
Ruchira Sasanka8e604792001-09-14 21:18:34 +000091
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000092 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000093 //
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000094 IGNodeSpill->pushOnStack();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000095
96 // now push NON-constrined ones, if any
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000097 //
Chris Lattner167b9622002-04-15 20:36:15 +000098 NeedMoreSpills = !pushUnconstrainedIGNodes();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000099
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000100 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000101 std::cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Ruchira Sasanka65480b72001-11-10 21:21:36 +0000102
Chris Lattner167b9622002-04-15 20:36:15 +0000103 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000104
105}
106
107
108
109
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000110//--------------------------------------------------------------------------
111// This method goes thru all IG nodes in the IGNodeList of an IG of a
112// register class and push any unconstrained IG node left (that is not
113// already pushed)
114//--------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000115
116bool RegClass::pushUnconstrainedIGNodes()
117{
118 // # of LRs for this reg class
119 unsigned int IGNodeListSize = IG.getIGNodeList().size();
120 bool pushedall = true;
121
122 // a pass over IGNodeList
123 for( unsigned i =0; i < IGNodeListSize; i++) {
124
125 // get IGNode i from IGNodeList
126 IGNode *IGNode = IG.getIGNodeList()[i];
127
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000128 if( !IGNode ) // can be null due to merging
129 continue;
130
131 // if already pushed on stack, continue. This can happen since this
132 // method can be called repeatedly until all constrained nodes are
133 // pushed
134 if( IGNode->isOnStack() )
135 continue;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000136 // if the degree of IGNode is lower
Vikram S. Advebc001b22003-07-25 21:06:09 +0000137 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs()) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000138 IGNodeStack.push( IGNode ); // push IGNode on to the stack
139 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
140
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000141 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000142 std::cerr << " pushed un-constrained IGNode " << IGNode->getIndex()
143 << " on to stack\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000144 }
145 }
146 else pushedall = false; // we didn't push all live ranges
147
148 } // for
149
150 // returns true if we pushed all live ranges - else false
151 return pushedall;
152}
153
154
155
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000156//----------------------------------------------------------------------------
157// Get the IGNode withe the minimum spill cost
158//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000159IGNode * RegClass::getIGNodeWithMinSpillCost()
160{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000161
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000162 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner0ceeb422002-10-22 23:34:11 +0000163 double MinSpillCost = 0;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000164 IGNode *MinCostIGNode = NULL;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000165 bool isFirstNode = true;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000166
167 // pass over IGNodeList to find the IGNode with minimum spill cost
168 // among all IGNodes that are not yet pushed on to the stack
169 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000171 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000172
173 if( ! IGNode ) // can be null due to merging
174 continue;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000175
176 if( ! IGNode->isOnStack() ) {
177
Ruchira Sasankace773da2002-01-08 16:29:23 +0000178 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
179 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000180
Ruchira Sasankace773da2002-01-08 16:29:23 +0000181 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000182 MinSpillCost = SpillCost;
183 MinCostIGNode = IGNode;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000184 isFirstNode = false;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000185 }
186
187 else if( MinSpillCost > SpillCost) {
188 MinSpillCost = SpillCost;
189 MinCostIGNode = IGNode;
190 }
191
192 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000193 }
194
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000195 assert( MinCostIGNode && "No IGNode to spill");
196 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000197}
198
199
200
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000201//----------------------------------------------------------------------------
202// Color the IGNode using the machine specific code.
203//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000204void RegClass::colorIGNode(IGNode *const Node)
205{
206
207 if( ! Node->hasColor() ) { // not colored as an arg etc.
208
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000209 // init all elements of to IsColorUsedAr false;
Vikram S. Advebc001b22003-07-25 21:06:09 +0000210 clearColorsUsed();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000211
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000212 // initialize all colors used by neighbors of this node to true
213 LiveRange *LR = Node->getParentLR();
214 unsigned NumNeighbors = Node->getNumOfNeighbors();
215 for (unsigned n=0; n < NumNeighbors; n++) {
216 IGNode *NeighIGNode = Node->getAdjIGNode(n);
217 LiveRange *NeighLR = NeighIGNode->getParentLR();
218
Vikram S. Advebc001b22003-07-25 21:06:09 +0000219 // Don't use a color if it is in use by the neighbour,
220 // or is suggested for use by the neighbour,
221 // markColorsUsed() should be given the color and the reg type for
222 // LR, not for NeighLR, because it should mark registers used based on
223 // the type we are looking for, not on the regType for the neighbour.
224 if (NeighLR->hasColor())
225 this->markColorsUsed(NeighLR->getColor(),
226 MRI->getRegTypeForLR(NeighLR),
227 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
228 else if (NeighLR->hasSuggestedColor() &&
229 NeighLR->isSuggestedColorUsable())
230 this->markColorsUsed(NeighLR->getSuggestedColor(),
231 MRI->getRegTypeForLR(NeighLR),
232 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
Vikram S. Advedabb41d2002-05-19 15:29:31 +0000233 }
Vikram S. Advebc001b22003-07-25 21:06:09 +0000234
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000235 // call the target specific code for coloring
236 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000237 MRC->colorIGNode(Node, IsColorUsedArr);
238 }
239 else {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000240 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000241 std::cerr << " Node " << Node->getIndex();
242 std::cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000243 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000244 }
245
246
247 if( !Node->hasColor() ) {
Vikram S. Adve39c94e12002-09-14 23:05:33 +0000248 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattnerc083dcc2003-09-01 20:05:47 +0000249 std::cerr << " Node " << Node->getIndex();
250 std::cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000251 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000252 }
253
254}
255
Chris Lattnercb6b4bd2002-10-29 16:51:05 +0000256void RegClass::printIGNodeList() const {
257 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
258 IG.printIGNodeList();
259}
260
261void RegClass::printIG() {
262 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
263 IG.printIG();
264}
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000265
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000266