blob: 038ccb369eec5614335430f0bb3719a8e23c4e7c [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
Misha Brukman77c9fcb2003-05-21 18:05:35 +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
Misha Brukman77c9fcb2003-05-21 18:05:35 +000034 if (LR->hasSuggestedColor()) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000035 unsigned SugCol = LR->getSuggestedColor();
Chris Lattner85c54652002-05-23 15:50:03 +000036 if (!IsColorUsedArr[SugCol]) {
Misha Brukman77c9fcb2003-05-21 18:05:35 +000037 if (LR->isSuggestedColorUsable()) {
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000038 // if the suggested color is volatile, we should use it only if
39 // there are no call interferences. Otherwise, it will get spilled.
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000040 if (DEBUG_RA)
Misha Brukmanee563cb2003-05-21 17:59:06 +000041 std::cerr << "\n -Coloring with sug color: " << SugCol;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000042
Misha Brukman77c9fcb2003-05-21 18:05:35 +000043 LR->setColor(LR->getSuggestedColor());
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000044 return;
Misha Brukman77c9fcb2003-05-21 18:05:35 +000045 } else if(DEBUG_RA) {
Misha Brukmanc97a2072003-05-21 19:34:28 +000046 std::cerr << "\n Couldn't alloc Sug col - LR volatile & calls interf";
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000047 }
Misha Brukman77c9fcb2003-05-21 18:05:35 +000048 } else if (DEBUG_RA) { // can't allocate the suggested col
Misha Brukmanee563cb2003-05-21 17:59:06 +000049 std::cerr << "\n Could NOT allocate the suggested color (already used) ";
50 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka91442282001-09-30 23:16:47 +000051 }
52 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000053
54 unsigned SearchStart; // start pos of color in pref-order
55 bool ColorFound= false; // have we found a color yet?
56
57 //if this Node is between calls
Misha Brukman77c9fcb2003-05-21 18:05:35 +000058 if (! LR->isCallInterference()) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000059 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +000060 SearchStart = SparcIntRegClass::StartOfAllRegs;
Misha Brukman77c9fcb2003-05-21 18:05:35 +000061 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000062 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +000063 SearchStart = SparcIntRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000064 }
65
66 unsigned c=0; // color
67
68 // find first unused color
Misha Brukman77c9fcb2003-05-21 18:05:35 +000069 for (c=SearchStart; c < SparcIntRegClass::NumOfAvailRegs; c++) {
70 if (!IsColorUsedArr[c]) {
71 ColorFound = true;
72 break;
73 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000074 }
75
Misha Brukman77c9fcb2003-05-21 18:05:35 +000076 if (ColorFound) {
Ruchira Sasanka91442282001-09-30 23:16:47 +000077 LR->setColor(c); // first color found in preffered order
Misha Brukmanee563cb2003-05-21 17:59:06 +000078 if (DEBUG_RA) std::cerr << "\n Colored after first search with col " << c;
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +000079 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000080
81 // if color is not found because of call interference
82 // try even finding a volatile color and insert save across calls
Ruchira Sasankad00982a2002-01-07 19:20:28 +000083 //
Misha Brukman77c9fcb2003-05-21 18:05:35 +000084 else if (LR->isCallInterference()) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000085 // start from 0 - try to find even a volatile this time
Chris Lattner95685682002-08-12 21:25:05 +000086 SearchStart = SparcIntRegClass::StartOfAllRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000087
88 // find first unused volatile color
Chris Lattner95685682002-08-12 21:25:05 +000089 for(c=SearchStart; c < SparcIntRegClass::StartOfNonVolatileRegs; c++) {
Misha Brukman77c9fcb2003-05-21 18:05:35 +000090 if (! IsColorUsedArr[c]) {
91 ColorFound = true;
92 break;
93 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000094 }
95
Chris Lattner699683c2002-02-04 05:59:25 +000096 if (ColorFound) {
Misha Brukman77c9fcb2003-05-21 18:05:35 +000097 LR->setColor(c);
98 // get the live range corresponding to live var
99 // since LR span across calls, must save across calls
100 //
101 LR->markForSaveAcrossCalls();
102 if (DEBUG_RA)
103 std::cerr << "\n Colored after SECOND search with col " << c;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000104 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000105 }
106
Ruchira Sasanka91442282001-09-30 23:16:47 +0000107
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000108 // If we couldn't find a color regardless of call interference - i.e., we
109 // don't have either a volatile or non-volatile color left
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000110 //
Chris Lattner699683c2002-02-04 05:59:25 +0000111 if (!ColorFound)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000112 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000113}
114
Vikram S. Adve786833a2003-07-06 20:13:59 +0000115//-----------------------------------------------------------------------------
116// Int CC Register Class - method for coloring a node in the interference graph.
117//
118// Algorithm:
119//
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000120// If (node has any interferences)
121// /* all interference operations can use only one register! */
Vikram S. Adve786833a2003-07-06 20:13:59 +0000122// mark the LR for spilling
123// else {
124// if (the LR is a 64-bit comparison) use %xcc
125// else /*32-bit or smaller*/ use %icc
126// }
127//
128// Note: The third name (%ccr) is essentially an assembly mnemonic and
129// depends solely on the opcode, so the name can be chosen in EmitAssembly.
130//-----------------------------------------------------------------------------
131void SparcIntCCRegClass::colorIGNode(IGNode *Node,
132 std::vector<bool> &IsColorUsedArr) const
133{
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000134 if (Node->getNumOfNeighbors() > 0)
Vikram S. Adve786833a2003-07-06 20:13:59 +0000135 Node->getParentLR()->markForSpill();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000136
137 // Mark the appropriate register in any case (even if it needs to be spilled)
138 // because there is only one possible register, but more importantly, the
139 // spill algorithm cannot find it. In particular, we have to choose
140 // whether to use %xcc or %icc based on type of value compared
141 //
142 const LiveRange* ccLR = Node->getParentLR();
143 const Type* setCCType = (* ccLR->begin())->getType(); // any Value in LR
144 assert(setCCType->isIntegral() || isa<PointerType>(setCCType));
145 int ccReg = ((isa<PointerType>(setCCType) || setCCType == Type::LongTy)
146 ? xcc : icc);
Vikram S. Adve786833a2003-07-06 20:13:59 +0000147
148#ifndef NDEBUG
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000149 // Let's just make sure values of two different types have not been
150 // coalesced into this LR.
151 for (ValueSet::const_iterator I=ccLR->begin(), E=ccLR->end(); I!=E; ++I) {
152 const Type* ccType = (*I)->getType();
153 assert((ccReg == xcc && (isa<PointerType>(ccType)
154 || ccType == Type::LongTy)) ||
155 (ccReg == icc && ccType->isIntegral() && ccType != Type::LongTy)
156 && "Comparisons needing different intCC regs coalesced in LR!");
157 }
Vikram S. Adve786833a2003-07-06 20:13:59 +0000158#endif
159
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000160 Node->setColor(ccReg); // only one int cc reg is available
Vikram S. Adve786833a2003-07-06 20:13:59 +0000161}
162
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000163
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000164//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000165// Float Register Class - method for coloring a node in the interference graph.
166//
167// Algorithm:
168//
169// If the LR is a double try to allocate f32 - f63
170// If the above fails or LR is single precision
171// If the LR does not interfere with a call
172// start allocating from f0
173// Else start allocating from f6
174// If a color is still not found because LR interferes with a call
175// Search in f0 - f6. If found mark for spill across calls.
176// If a color is still not fond, mark for spilling
177//
178//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000179void SparcFloatRegClass::colorIGNode(IGNode * Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000180 std::vector<bool> &IsColorUsedArr) const
181{
Chris Lattner37730942002-02-05 03:52:29 +0000182 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000183
Vikram S. Adve242a8082002-05-19 15:25:51 +0000184 // Mark the second color for double-precision registers:
185 // This is UGLY and should be merged into nearly identical code
186 // in RegClass::colorIGNode that handles the first color.
187 //
188 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000189 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
190 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000191 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000192
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000193 if (NeighLR->hasColor() &&
Vikram S. Adve242a8082002-05-19 15:25:51 +0000194 NeighLR->getType() == Type::DoubleTy) {
195 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
196
197 } else if (NeighLR->hasSuggestedColor() &&
198 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000199
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000200 // if the neighbour can use the suggested color
201 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
202 if (NeighLR->getType() == Type::DoubleTy)
203 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000204 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000205 }
206
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000207 // **NOTE: We don't check for call interferences in allocating suggested
208 // color in this class since ALL registers are volatile. If this fact
209 // changes, we should change the following part
210 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000211 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000212 if( LR->hasSuggestedColor() ) {
213 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
214 LR->setColor( LR->getSuggestedColor() );
215 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000216 } else if (DEBUG_RA) { // can't allocate the suggested col
Misha Brukmanee563cb2003-05-21 17:59:06 +0000217 std::cerr << " Could NOT allocate the suggested color for LR ";
218 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000219 }
220 }
221
Ruchira Sasanka91442282001-09-30 23:16:47 +0000222
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000223 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000224 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000225
Chris Lattner85c54652002-05-23 15:50:03 +0000226 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000227 // i.e. we try to allocate f32 - f63 first for doubles since singles
228 // cannot go there. By doing that, we provide more space for singles
229 // in f0 - f31
230 //
Chris Lattner37730942002-02-05 03:52:29 +0000231 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000232 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000233
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000234 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000235 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000236 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000237 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000238
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000239 // if we didn't find a color becuase the LR was single precision or
240 // all f32-f63 range is filled, we try to allocate a register from
241 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000242
243 unsigned SearchStart; // start pos of color in pref-order
244
245 //if this Node is between calls (i.e., no call interferences )
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000246 if (! isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000247 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000248 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000249 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000250 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000251 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000252 }
253
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000254 ColorFound = findFloatColor(LR, SearchStart, 32, IsColorUsedArr);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000255 }
256
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000257 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000258 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000259 return;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000260 } else if (isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000261 // We are here because there is a call interference and no non-volatile
262 // color could be found.
263 // Now try to allocate even a volatile color
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000264 ColorFound = findFloatColor(LR, SparcFloatRegClass::StartOfAllRegs,
Chris Lattner95685682002-08-12 21:25:05 +0000265 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000266 IsColorUsedArr);
267 }
268
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000269 if (ColorFound >= 0) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000270 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000271 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000272 } else {
273 // we are here because no color could be found
274 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000275 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000276}
277
278
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000279//-----------------------------------------------------------------------------
280// Helper method for coloring a node of Float Reg class.
281// Finds the first available color in the range [Start,End] depending on the
282// type of the Node (i.e., float/double)
283//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000284
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000285int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
286 unsigned Start,
287 unsigned End,
288 std::vector<bool> &IsColorUsedArr) const
Misha Brukmanee563cb2003-05-21 17:59:06 +0000289{
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000290 bool ColorFound = false;
291 unsigned c;
292
Chris Lattner37730942002-02-05 03:52:29 +0000293 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000294 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000295 for (c=Start; c < End ; c+= 2)
296 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
297 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000298 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000299 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000300 for (c = Start; c < End; c++)
301 if (!IsColorUsedArr[c])
302 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000303 }
304
Chris Lattner699683c2002-02-04 05:59:25 +0000305 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000306}