blob: 6ab6d6675ffead7d684649c39daeccbd97cf3195 [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) {
46 std::cerr << "\n Couldn't alloc Sug col - LR voloatile & 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
115
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000116//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000117// Float Register Class - method for coloring a node in the interference graph.
118//
119// Algorithm:
120//
121// If the LR is a double try to allocate f32 - f63
122// If the above fails or LR is single precision
123// If the LR does not interfere with a call
124// start allocating from f0
125// Else start allocating from f6
126// If a color is still not found because LR interferes with a call
127// Search in f0 - f6. If found mark for spill across calls.
128// If a color is still not fond, mark for spilling
129//
130//----------------------------------------------------------------------------
Chris Lattner85c54652002-05-23 15:50:03 +0000131void SparcFloatRegClass::colorIGNode(IGNode * Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000132 std::vector<bool> &IsColorUsedArr) const
133{
Chris Lattner37730942002-02-05 03:52:29 +0000134 LiveRange *LR = Node->getParentLR();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000135
Vikram S. Adve242a8082002-05-19 15:25:51 +0000136 // Mark the second color for double-precision registers:
137 // This is UGLY and should be merged into nearly identical code
138 // in RegClass::colorIGNode that handles the first color.
139 //
140 unsigned NumNeighbors = Node->getNumOfNeighbors(); // total # of neighbors
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000141 for(unsigned n=0; n < NumNeighbors; n++) { // for each neigh
142 IGNode *NeighIGNode = Node->getAdjIGNode(n);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000143 LiveRange *NeighLR = NeighIGNode->getParentLR();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000144
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000145 if (NeighLR->hasColor() &&
Vikram S. Adve242a8082002-05-19 15:25:51 +0000146 NeighLR->getType() == Type::DoubleTy) {
147 IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;
148
149 } else if (NeighLR->hasSuggestedColor() &&
150 NeighLR-> isSuggestedColorUsable() ) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000151
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000152 // if the neighbour can use the suggested color
153 IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
154 if (NeighLR->getType() == Type::DoubleTy)
155 IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000156 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000157 }
158
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000159 // **NOTE: We don't check for call interferences in allocating suggested
160 // color in this class since ALL registers are volatile. If this fact
161 // changes, we should change the following part
162 //- see SparcIntRegClass::colorIGNode()
Vikram S. Adve242a8082002-05-19 15:25:51 +0000163 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000164 if( LR->hasSuggestedColor() ) {
165 if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
166 LR->setColor( LR->getSuggestedColor() );
167 return;
Chris Lattner296b7732002-02-05 02:52:05 +0000168 } else if (DEBUG_RA) { // can't allocate the suggested col
Misha Brukmanee563cb2003-05-21 17:59:06 +0000169 std::cerr << " Could NOT allocate the suggested color for LR ";
170 printSet(*LR); std::cerr << "\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000171 }
172 }
173
Ruchira Sasanka91442282001-09-30 23:16:47 +0000174
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000175 int ColorFound = -1; // have we found a color yet?
Ruchira Sasanka0f5e9882001-10-19 17:23:43 +0000176 bool isCallInterf = LR->isCallInterference();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000177
Chris Lattner85c54652002-05-23 15:50:03 +0000178 // if value is a double - search the double only region (f32 - f63)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000179 // i.e. we try to allocate f32 - f63 first for doubles since singles
180 // cannot go there. By doing that, we provide more space for singles
181 // in f0 - f31
182 //
Chris Lattner37730942002-02-05 03:52:29 +0000183 if (LR->getType() == Type::DoubleTy)
Ruchira Sasanka91442282001-09-30 23:16:47 +0000184 ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000185
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000186 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000187 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000188 return;
Chris Lattner699683c2002-02-04 05:59:25 +0000189 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000190
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000191 // if we didn't find a color becuase the LR was single precision or
192 // all f32-f63 range is filled, we try to allocate a register from
193 // the f0 - f31 region
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000194
195 unsigned SearchStart; // start pos of color in pref-order
196
197 //if this Node is between calls (i.e., no call interferences )
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000198 if (! isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000199 // start with volatiles (we can allocate volatiles safely)
Chris Lattner95685682002-08-12 21:25:05 +0000200 SearchStart = SparcFloatRegClass::StartOfAllRegs;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000201 } else {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000202 // start with non volatiles (no non-volatiles)
Chris Lattner95685682002-08-12 21:25:05 +0000203 SearchStart = SparcFloatRegClass::StartOfNonVolatileRegs;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000204 }
205
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000206 ColorFound = findFloatColor(LR, SearchStart, 32, IsColorUsedArr);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000207 }
208
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000209 if (ColorFound >= 0) { // if we could find a color
Ruchira Sasanka91442282001-09-30 23:16:47 +0000210 LR->setColor(ColorFound);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000211 return;
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000212 } else if (isCallInterf) {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000213 // We are here because there is a call interference and no non-volatile
214 // color could be found.
215 // Now try to allocate even a volatile color
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000216 ColorFound = findFloatColor(LR, SparcFloatRegClass::StartOfAllRegs,
Chris Lattner95685682002-08-12 21:25:05 +0000217 SparcFloatRegClass::StartOfNonVolatileRegs,
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000218 IsColorUsedArr);
219 }
220
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000221 if (ColorFound >= 0) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000222 LR->setColor(ColorFound); // first color found in prefered order
Ruchira Sasanka91442282001-09-30 23:16:47 +0000223 LR->markForSaveAcrossCalls();
Chris Lattner699683c2002-02-04 05:59:25 +0000224 } else {
225 // we are here because no color could be found
226 LR->markForSpill(); // no color found - must spill
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000227 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000228}
229
230
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000231//-----------------------------------------------------------------------------
232// Helper method for coloring a node of Float Reg class.
233// Finds the first available color in the range [Start,End] depending on the
234// type of the Node (i.e., float/double)
235//-----------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000236
Misha Brukman77c9fcb2003-05-21 18:05:35 +0000237int SparcFloatRegClass::findFloatColor(const LiveRange *LR,
238 unsigned Start,
239 unsigned End,
240 std::vector<bool> &IsColorUsedArr) const
Misha Brukmanee563cb2003-05-21 17:59:06 +0000241{
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000242 bool ColorFound = false;
243 unsigned c;
244
Chris Lattner37730942002-02-05 03:52:29 +0000245 if (LR->getType() == Type::DoubleTy) {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000246 // find first unused color for a double
Chris Lattner699683c2002-02-04 05:59:25 +0000247 for (c=Start; c < End ; c+= 2)
248 if (!IsColorUsedArr[c] && !IsColorUsedArr[c+1])
249 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000250 } else {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000251 // find first unused color for a single
Chris Lattner699683c2002-02-04 05:59:25 +0000252 for (c = Start; c < End; c++)
253 if (!IsColorUsedArr[c])
254 return c;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000255 }
256
Chris Lattner699683c2002-02-04 05:59:25 +0000257 return -1;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000258}