blob: 3918871d69f16f71ea94632caf78306acf1cff11 [file] [log] [blame]
Ruchira Sasanka8e604792001-09-14 21:18:34 +00001#include "llvm/CodeGen/RegClass.h"
2
3
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +00004//----------------------------------------------------------------------------
5// This constructor inits IG. The actual matrix is created by a call to
6// createInterferenceGraph() above.
7//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +00008RegClass::RegClass(const Method *const M,
9 const MachineRegClassInfo *const Mrc,
10 const ReservedColorListType *const RCL)
11 : Meth(M), MRC(Mrc), RegClassID( Mrc->getRegClassID() ),
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000012 IG(this), IGNodeStack(), ReservedColorList(RCL) {
Ruchira Sasanka8e604792001-09-14 21:18:34 +000013 if( DEBUG_RA)
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000014 cout << "Created Reg Class: " << RegClassID << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000015
Ruchira Sasanka8e604792001-09-14 21:18:34 +000016 IsColorUsedArr = new bool[ Mrc->getNumOfAllRegs() ];
17}
18
19
20
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000021//----------------------------------------------------------------------------
22// Main entry point for coloring a register class.
23//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000024void RegClass::colorAllRegs()
25{
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000026 if(DEBUG_RA) cout << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8e604792001-09-14 21:18:34 +000027
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000028 // pre-color IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000029 pushAllIGNodes(); // push all IG Nodes
30
31 unsigned int StackSize = IGNodeStack.size();
32 IGNode *CurIGNode;
33
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000034 // for all LRs on stack
Ruchira Sasanka8e604792001-09-14 21:18:34 +000035 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
36
37 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
38 IGNodeStack.pop();
39 colorIGNode (CurIGNode); // color it
40 }
41
Ruchira Sasanka8e604792001-09-14 21:18:34 +000042}
43
44
45
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000046//----------------------------------------------------------------------------
47// The method for pushing all IGNodes on to the stack.
48//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +000049void RegClass::pushAllIGNodes()
50{
51 bool NeedMoreSpills;
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000052
Ruchira Sasanka8e604792001-09-14 21:18:34 +000053
54 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
55
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000056 // push non-constrained IGNodes
Ruchira Sasanka8e604792001-09-14 21:18:34 +000057 bool PushedAll = pushUnconstrainedIGNodes();
58
59 if( DEBUG_RA) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +000060 cout << " Puhsed all-unconstrained IGNodes. ";
61 if( PushedAll ) cout << " No constrained nodes left.";
62 cout << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +000063 }
64
65 if( PushedAll ) // if NO constrained nodes left
66 return;
67
68
69 // now, we have constrained nodes. So, push one of them (the one with min
70 // spill cost) and try to push the others as unConstrained nodes.
71 // Repeat this.
72
73 do{
74
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000075 //get node with min spill cost
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000076 //
Ruchira Sasankad1565ab2001-11-06 15:25:38 +000077 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
78
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000079 // push that node on to stack
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000080 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +000081 IGNodeStack.push( IGNodeSpill );
82
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000083 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000084 //
Vikram S. Adve04cc49b2001-11-06 05:11:05 +000085 IGNodeSpill->pushOnStack();
Ruchira Sasanka8e604792001-09-14 21:18:34 +000086
87 // now push NON-constrined ones, if any
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +000088 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +000089 NeedMoreSpills = ! pushUnconstrainedIGNodes();
90
Ruchira Sasanka65480b72001-11-10 21:21:36 +000091 cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
92
Ruchira Sasanka8e604792001-09-14 21:18:34 +000093 } while( NeedMoreSpills ); // repeat until we have pushed all
94
95}
96
97
98
99
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000100//--------------------------------------------------------------------------
101// This method goes thru all IG nodes in the IGNodeList of an IG of a
102// register class and push any unconstrained IG node left (that is not
103// already pushed)
104//--------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000105
106bool RegClass::pushUnconstrainedIGNodes()
107{
108 // # of LRs for this reg class
109 unsigned int IGNodeListSize = IG.getIGNodeList().size();
110 bool pushedall = true;
111
112 // a pass over IGNodeList
113 for( unsigned i =0; i < IGNodeListSize; i++) {
114
115 // get IGNode i from IGNodeList
116 IGNode *IGNode = IG.getIGNodeList()[i];
117
Ruchira Sasankad1565ab2001-11-06 15:25:38 +0000118 if( !IGNode ) // can be null due to merging
119 continue;
120
121 // if already pushed on stack, continue. This can happen since this
122 // method can be called repeatedly until all constrained nodes are
123 // pushed
124 if( IGNode->isOnStack() )
125 continue;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000126 // if the degree of IGNode is lower
127 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs() ) {
128 IGNodeStack.push( IGNode ); // push IGNode on to the stack
129 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
130
131 if (DEBUG_RA > 1) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000132 cout << " pushed un-constrained IGNode " << IGNode->getIndex() ;
133 cout << " on to stack" << endl;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000134 }
135 }
136 else pushedall = false; // we didn't push all live ranges
137
138 } // for
139
140 // returns true if we pushed all live ranges - else false
141 return pushedall;
142}
143
144
145
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000146//----------------------------------------------------------------------------
147// Get the IGNode withe the minimum spill cost
148//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000149IGNode * RegClass::getIGNodeWithMinSpillCost()
150{
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000151
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000152 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Ruchira Sasankace773da2002-01-08 16:29:23 +0000153 double MinSpillCost;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000154 IGNode *MinCostIGNode = NULL;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000155 bool isFirstNode = true;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000156
157 // pass over IGNodeList to find the IGNode with minimum spill cost
158 // among all IGNodes that are not yet pushed on to the stack
159 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000160 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000161 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000162
163 if( ! IGNode ) // can be null due to merging
164 continue;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000165
166 if( ! IGNode->isOnStack() ) {
167
Ruchira Sasankace773da2002-01-08 16:29:23 +0000168 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
169 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000170
Ruchira Sasankace773da2002-01-08 16:29:23 +0000171 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000172 MinSpillCost = SpillCost;
173 MinCostIGNode = IGNode;
Ruchira Sasankace773da2002-01-08 16:29:23 +0000174 isFirstNode = false;
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000175 }
176
177 else if( MinSpillCost > SpillCost) {
178 MinSpillCost = SpillCost;
179 MinCostIGNode = IGNode;
180 }
181
182 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000183 }
184
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000185 assert( MinCostIGNode && "No IGNode to spill");
186 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000187}
188
189
190
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000191//----------------------------------------------------------------------------
192// Color the IGNode using the machine specific code.
193//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000194void RegClass::colorIGNode(IGNode *const Node)
195{
196
197 if( ! Node->hasColor() ) { // not colored as an arg etc.
198
199
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000200 // init all elements of to IsColorUsedAr false;
201 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000202 for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) {
203 IsColorUsedArr[ i ] = false;
204 }
205
206 // init all reserved_regs to true - we can't use them
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000207 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000208 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
209 IsColorUsedArr[ (*ReservedColorList)[i] ] = true;
210 }
211
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000212 // call the target specific code for coloring
213 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000214 MRC->colorIGNode(Node, IsColorUsedArr);
215 }
216 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000217 if( DEBUG_RA ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000218 cout << " Node " << Node->getIndex();
219 cout << " already colored with color " << Node->getColor() << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000220 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000221 }
222
223
224 if( !Node->hasColor() ) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000225 if( DEBUG_RA ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000226 cout << " Node " << Node->getIndex();
227 cout << " - could not find a color (needs spilling)" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000228 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000229 }
230
231}
232
233
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000234