blob: 2246643594612f06ed6a1c8b6e333402118314d7 [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();
153 long MinSpillCost = -1;
154 IGNode *MinCostIGNode = NULL;
155
156
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
168 unsigned SpillCost = IGNode->getParentLR()->getSpillCost();
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000169
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000170 if( MinSpillCost == -1) { // for the first IG node
171 MinSpillCost = SpillCost;
172 MinCostIGNode = IGNode;
173 }
174
175 else if( MinSpillCost > SpillCost) {
176 MinSpillCost = SpillCost;
177 MinCostIGNode = IGNode;
178 }
179
180 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000181 }
182
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000183 assert( MinCostIGNode && "No IGNode to spill");
184 return MinCostIGNode;
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000185}
186
187
188
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000189//----------------------------------------------------------------------------
190// Color the IGNode using the machine specific code.
191//----------------------------------------------------------------------------
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000192void RegClass::colorIGNode(IGNode *const Node)
193{
194
195 if( ! Node->hasColor() ) { // not colored as an arg etc.
196
197
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000198 // init all elements of to IsColorUsedAr false;
199 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000200 for( unsigned i=0; i < MRC->getNumOfAllRegs(); i++) {
201 IsColorUsedArr[ i ] = false;
202 }
203
204 // init all reserved_regs to true - we can't use them
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000205 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000206 for( unsigned i=0; i < ReservedColorList->size() ; i++) {
207 IsColorUsedArr[ (*ReservedColorList)[i] ] = true;
208 }
209
Ruchira Sasanka4f3eb222002-01-07 19:19:18 +0000210 // call the target specific code for coloring
211 //
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000212 MRC->colorIGNode(Node, IsColorUsedArr);
213 }
214 else {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000215 if( DEBUG_RA ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000216 cout << " Node " << Node->getIndex();
217 cout << " already colored with color " << Node->getColor() << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000218 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000219 }
220
221
222 if( !Node->hasColor() ) {
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000223 if( DEBUG_RA ) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000224 cout << " Node " << Node->getIndex();
225 cout << " - could not find a color (needs spilling)" << endl;
Ruchira Sasanka0931a012001-09-15 19:06:58 +0000226 }
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000227 }
228
229}
230
231
Ruchira Sasanka8e604792001-09-14 21:18:34 +0000232