blob: 1640dcab6c68d3c405e136dc6d388093878ad81a [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//
120// If the single int CC register is used (either as icc or xcc)
121// mark the LR for spilling
122// else {
123// if (the LR is a 64-bit comparison) use %xcc
124// else /*32-bit or smaller*/ use %icc
125// }
126//
127// Note: The third name (%ccr) is essentially an assembly mnemonic and
128// depends solely on the opcode, so the name can be chosen in EmitAssembly.
129//-----------------------------------------------------------------------------
130void SparcIntCCRegClass::colorIGNode(IGNode *Node,
131 std::vector<bool> &IsColorUsedArr) const
132{
133 if (IsColorUsedArr[xcc] && IsColorUsedArr[icc])
134 Node->getParentLR()->markForSpill();
135 else {
136 // Choose whether to use %xcc or %icc based on type of value compared
137 const LiveRange* ccLR = Node->getParentLR();
138 const Type* setCCType = (* ccLR->begin())->getType(); // any Value in LR
139 assert(setCCType->isIntegral());
140 int ccReg = (setCCType == Type::LongTy)? xcc : icc;
141
142#ifndef NDEBUG
143 // Let's just make sure values of two different types have not been
144 // coalesced into this LR.
145 for (ValueSet::const_iterator I=ccLR->begin(), E=ccLR->end(); I != E; ++I)
146 assert(setCCType->isIntegral() &&
147 ((ccReg == xcc && (*I)->getType() == Type::LongTy) ||
148 (ccReg == icc && (*I)->getType() != Type::LongTy))
149 && "Comparisons needing different intCC regs coalesced in LR!");
150#endif
151
152 Node->setColor(ccReg); // only one int cc reg is available
153 }
154}
155
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000156
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000157//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000158// Float Register Class - method for coloring a node in the interference graph.
159//
160// Algorithm:
161//
162// If the LR is a double try to allocate f32 - f63
163// If the above fails or LR is single precision
164// If the LR does not interfere with a call
165// start allocating from f0
166// Else start allocating from f6
167// If a color is still not found because LR interferes with a call
168// Search in f0 - f6. If found mark for spill across calls.
169// If a color is still not fond, mark for spilling
170//
171//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000172void SparcFloatRegClass::colorIGNode(IGNode * Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000173 std::vector<bool> &IsColorUsedArr) const
174{
Chris Lattner37730942002-02-05 03:52:29 +0000175 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000176
Vikram S. Adve242a8082002-05-19 15:25:51 +0000177 // Mark the second color for double-precision registers:
178 // This is UGLY and should be merged into nearly identical code
179 // in RegClass::colorIGNode that handles the first color.
180 //
181 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000182 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
183 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000184 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000185
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000186 if (NeighLR->hasColor() &&
Vikram S. Adve242a8082002-05-19 15:25:51 +0000187 NeighLR->getType() == Type::DoubleTy) {
188 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
189
190 } else if (NeighLR->hasSuggestedColor() &&
191 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000192
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000193 // if the neighbour can use the suggested color
194 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
195 if (NeighLR->getType() == Type::DoubleTy)
196 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000197 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000198 }
199
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000200 // **NOTE: We don't check for call interferences in allocating suggested
201 // color in this class since ALL registers are volatile. If this fact
202 // changes, we should change the following part
203 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000204 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000205 if( LR->hasSuggestedColor() ) {
206 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
207 LR->setColor( LR->getSuggestedColor() );
208 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000209 } else if (DEBUG_RA) { // can't allocate the suggested col
Misha Brukmanee563cb2003-05-21 17:59:06 +0000210 std::cerr << " Could NOT allocate the suggested color for LR ";
211 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000212 }
213 }
214
Ruchira Sasanka91442282001-09-30 23:16:47 +0000215
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000216 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000217 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000218
Chris Lattner85c54652002-05-23 15:50:03 +0000219 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000220 // i.e. we try to allocate f32 - f63 first for doubles since singles
221 // cannot go there. By doing that, we provide more space for singles
222 // in f0 - f31
223 //
Chris Lattner37730942002-02-05 03:52:29 +0000224 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000225 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000226
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000227 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000228 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000229 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000230 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000231
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000232 // if we didn't find a color becuase the LR was single precision or
233 // all f32-f63 range is filled, we try to allocate a register from
234 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000235
236 unsigned SearchStart; // start pos of color in pref-order
237
238 //if this Node is between calls (i.e., no call interferences )
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000239 if (! isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000240 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000241 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000242 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000243 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000244 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000245 }
246
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000247 ColorFound = findFloatColor(LR, SearchStart, 32, IsColorUsedArr);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000248 }
249
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000250 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000251 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000252 return;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000253 } else if (isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000254 // We are here because there is a call interference and no non-volatile
255 // color could be found.
256 // Now try to allocate even a volatile color
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000257 ColorFound = findFloatColor(LR, SparcFloatRegClass::StartOfAllRegs,
Chris Lattner95685682002-08-12 21:25:05 +0000258 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000259 IsColorUsedArr);
260 }
261
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000262 if (ColorFound >= 0) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000263 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000264 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000265 } else {
266 // we are here because no color could be found
267 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000268 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000269}
270
271
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000272//-----------------------------------------------------------------------------
273// Helper method for coloring a node of Float Reg class.
274// Finds the first available color in the range [Start,End] depending on the
275// type of the Node (i.e., float/double)
276//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000277
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000278int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
279 unsigned Start,
280 unsigned End,
281 std::vector<bool> &IsColorUsedArr) const
Misha Brukmanee563cb2003-05-21 17:59:06 +0000282{
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000283 bool ColorFound = false;
284 unsigned c;
285
Chris Lattner37730942002-02-05 03:52:29 +0000286 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000287 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000288 for (c=Start; c < End ; c+= 2)
289 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
290 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000291 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000292 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000293 for (c = Start; c < End; c++)
294 if (!IsColorUsedArr[c])
295 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000296 }
297
Chris Lattner699683c2002-02-04 05:59:25 +0000298 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000299}