blob: e8a63a81014e297ed3ac96528142e98d083da9ca [file] [log] [blame]
Chris Lattner95685682002-08-12 21:25:05 +00001//===-- SparcRegClassInfo.cpp - Register class def'ns for Sparc -----------===//
2//
3// This file defines the register classes used by the Sparc target description.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner699683c2002-02-04 05:59:25 +00007#include "SparcRegClassInfo.h"
Chris Lattner37730942002-02-05 03:52:29 +00008#include "llvm/Type.h"
Chris Lattner0e104332003-01-15 19:50:44 +00009#include "../../CodeGen/RegAlloc/RegAllocCommon.h" // FIXME!
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000010
11//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +000012// Int Register Class - method for coloring a node in the interference graph.
13//
14// Algorithm:
15// Record the colors/suggested colors of all neighbors.
16//
17// If there is a suggested color, try to allocate it
18// If there is no call interf, try to allocate volatile, then non volatile
19// If there is call interf, try to allocate non-volatile. If that fails
20// try to allocate a volatile and insert save across calls
21// If both above fail, spill.
22//
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000023//-----------------------------------------------------------------------------
Misha Brukmanee563cb2003-05-21 17:59:06 +000024void SparcIntRegClass::colorIGNode(IGNode * Node,
25 std::vector<bool> &IsColorUsedArr) const
26{
Chris Lattner699683c2002-02-04 05:59:25 +000027 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000028
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000029 if( DEBUG_RA ) {
Misha Brukmanee563cb2003-05-21 17:59:06 +000030 std::cerr << "\nColoring LR [CallInt=" << LR->isCallInterference() <<"]:";
Chris Lattner296b7732002-02-05 02:52:05 +000031 printSet(*LR);
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000032 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000033
Ruchira Sasanka91442282001-09-30 23:16:47 +000034 if( LR->hasSuggestedColor() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000035
36 unsigned SugCol = LR->getSuggestedColor();
37
Chris Lattner85c54652002-05-23 15:50:03 +000038 if (!IsColorUsedArr[SugCol]) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000039
Ruchira Sasankab49865f2001-10-19 21:41:16 +000040 if( LR->isSuggestedColorUsable() ) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000041
42 // if the suggested color is volatile, we should use it only if
43 // there are no call interferences. Otherwise, it will get spilled.
44
45 if (DEBUG_RA)
Misha Brukmanee563cb2003-05-21 17:59:06 +000046 std::cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000047
48 LR->setColor( LR->getSuggestedColor() );
49 return;
50 }
51 else if(DEBUG_RA)
Misha Brukmanee563cb2003-05-21 17:59:06 +000052 std::cerr << "\n Couldn't alloc Sug col - LR voloatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000053
Ruchira Sasanka91442282001-09-30 23:16:47 +000054 }
Misha Brukmanee563cb2003-05-21 17:59:06 +000055 else if (DEBUG_RA) { // can't allocate the suggested col
56 std::cerr << "\n Could NOT allocate the suggested color (already used) ";
57 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000058 }
59 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000060
61 unsigned SearchStart; // start pos of color in pref-order
62 bool ColorFound= false; // have we found a color yet?
63
64 //if this Node is between calls
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000065 if( ! LR->isCallInterference() ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000066
67 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +000068 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000069 }
70 else {
71 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +000072 SearchStart = SparcIntRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000073 }
74
75 unsigned c=0; // color
76
77 // find first unused color
Chris Lattner95685682002-08-12 21:25:05 +000078 for( c=SearchStart; c < SparcIntRegClass::NumOfAvailRegs; c++) {
Chris Lattner85c54652002-05-23 15:50:03 +000079 if(!IsColorUsedArr[c] ) { ColorFound = true; break; }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000080 }
81
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000082 if( ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000083 LR->setColor(c); // first color found in preffered order
Misha Brukmanee563cb2003-05-21 17:59:06 +000084 if (DEBUG_RA) std::cerr << "\n Colored after first search with col " << c;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000085 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000086
87 // if color is not found because of call interference
88 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000089 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000090 else if( LR->isCallInterference() )
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000091 {
92 // start from 0 - try to find even a volatile this time
Chris Lattner95685682002-08-12 21:25:05 +000093 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000094
95 // find first unused volatile color
Chris Lattner95685682002-08-12 21:25:05 +000096 for(c=SearchStart; c < SparcIntRegClass::StartOfNonVolatileRegs; c++) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000097 if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
98 }
99
Chris Lattner699683c2002-02-04 05:59:25 +0000100 if (ColorFound) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000101 LR->setColor(c);
102 // get the live range corresponding to live var
103 // since LR span across calls, must save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000104 //
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000105 LR->markForSaveAcrossCalls();
Misha Brukmanee563cb2003-05-21 17:59:06 +0000106 if (DEBUG_RA)
107 std::cerr << "\n Colored after SECOND search with col " << c;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000108 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000109 }
110
Ruchira Sasanka91442282001-09-30 23:16:47 +0000111
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000112 // If we couldn't find a color regardless of call interference - i.e., we
113 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000114 //
Chris Lattner699683c2002-02-04 05:59:25 +0000115 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000116 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000117}
118
119
120
121
122
123
124//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000125// Float Register Class - method for coloring a node in the interference graph.
126//
127// Algorithm:
128//
129// If the LR is a double try to allocate f32 - f63
130// If the above fails or LR is single precision
131// If the LR does not interfere with a call
132// start allocating from f0
133// Else start allocating from f6
134// If a color is still not found because LR interferes with a call
135// Search in f0 - f6. If found mark for spill across calls.
136// If a color is still not fond, mark for spilling
137//
138//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000139void SparcFloatRegClass::colorIGNode(IGNode * Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000140 std::vector<bool> &IsColorUsedArr) const
141{
Chris Lattner37730942002-02-05 03:52:29 +0000142 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000143
Vikram S. Adve242a8082002-05-19 15:25:51 +0000144 // Mark the second color for double-precision registers:
145 // This is UGLY and should be merged into nearly identical code
146 // in RegClass::colorIGNode that handles the first color.
147 //
148 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000149 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
150 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000151 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000152
153 if( NeighLR->hasColor() &&
154 NeighLR->getType() == Type::DoubleTy) {
155 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
156
157 } else if (NeighLR->hasSuggestedColor() &&
158 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000159
Vikram S. Adve242a8082002-05-19 15:25:51 +0000160 // if the neighbour can use the suggested color
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000161 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
Chris Lattner37730942002-02-05 03:52:29 +0000162 if (NeighLR->getType() == Type::DoubleTy)
Ruchira Sasankab49865f2001-10-19 21:41:16 +0000163 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000164 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000165 }
166
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000167 // **NOTE: We don't check for call interferences in allocating suggested
168 // color in this class since ALL registers are volatile. If this fact
169 // changes, we should change the following part
170 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000171 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000172 if( LR->hasSuggestedColor() ) {
173 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
174 LR->setColor( LR->getSuggestedColor() );
175 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000176 } else if (DEBUG_RA) { // can't allocate the suggested col
Misha Brukmanee563cb2003-05-21 17:59:06 +0000177 std::cerr << " Could NOT allocate the suggested color for LR ";
178 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000179 }
180 }
181
Ruchira Sasanka91442282001-09-30 23:16:47 +0000182
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000183 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000184 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000185
Chris Lattner85c54652002-05-23 15:50:03 +0000186 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000187 // i.e. we try to allocate f32 - f63 first for doubles since singles
188 // cannot go there. By doing that, we provide more space for singles
189 // in f0 - f31
190 //
Chris Lattner37730942002-02-05 03:52:29 +0000191 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000192 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000193
194
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000195 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000196 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000197 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000198 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000199
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000200 // if we didn't find a color becuase the LR was single precision or
201 // all f32-f63 range is filled, we try to allocate a register from
202 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000203
204 unsigned SearchStart; // start pos of color in pref-order
205
206 //if this Node is between calls (i.e., no call interferences )
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000207 if( ! isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000208 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000209 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000210 }
211 else {
212 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000213 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000214 }
215
Ruchira Sasanka91442282001-09-30 23:16:47 +0000216 ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000217 }
218
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000219
220
221 if( ColorFound >= 0 ) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000222 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000223 return;
224 }
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000225 else if( isCallInterf ) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000226
227 // We are here because there is a call interference and no non-volatile
228 // color could be found.
229 // Now try to allocate even a volatile color
230
Chris Lattner95685682002-08-12 21:25:05 +0000231 ColorFound = findFloatColor( LR, SparcFloatRegClass::StartOfAllRegs,
232 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000233 IsColorUsedArr);
234 }
235
236 if( ColorFound >= 0 ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000237 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000238 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000239 } else {
240 // we are here because no color could be found
241 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000242 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000243}
244
245
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000246//-----------------------------------------------------------------------------
247// Helper method for coloring a node of Float Reg class.
248// Finds the first available color in the range [Start,End] depending on the
249// type of the Node (i.e., float/double)
250//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000251
Misha Brukmanee563cb2003-05-21 17:59:06 +0000252int SparcFloatRegClass::findFloatColor
253(const LiveRange *LR,
254 unsigned Start, unsigned End,
255 std::vector<bool> &IsColorUsedArr) const
256{
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000257 bool ColorFound = false;
258 unsigned c;
259
Chris Lattner37730942002-02-05 03:52:29 +0000260 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000261 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000262 for (c=Start; c < End ; c+= 2)
263 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
264 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000265 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000266 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000267 for (c = Start; c < End; c++)
268 if (!IsColorUsedArr[c])
269 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000270 }
271
Chris Lattner699683c2002-02-04 05:59:25 +0000272 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000273}