aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Target/SparcV9/SparcV9RegClassInfo.cpp
blob: 883ed52d0fc3c0ee5faa44e786221b939f27ad72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "llvm/CodeGen/IGNode.h"
#include "SparcInternals.h"

#include "llvm/Target/Sparc.h"

//-----------------------------------------------------------------------------
// Int Register Class
//-----------------------------------------------------------------------------

void SparcIntRegClass::colorIGNode(IGNode * Node, bool IsColorUsedArr[]) const 
{

  /* Algorithm:
     Record the colors/suggested colors of all neighbors.

     If there is a suggested color, try to allocate it
     If there is no call interf, try to allocate volatile, then non volatile
     If there is call interf, try to allocate non-volatile. If that fails
     try to allocate a volatile and insert save across calls
     If both above fail, spill.

  */

  LiveRange * LR = Node->getParentLR();
  unsigned NumNeighbors =  Node->getNumOfNeighbors();   // total # of neighbors

  for(unsigned n=0; n < NumNeighbors; n++) {            // for each neigh 
    IGNode *NeighIGNode = Node->getAdjIGNode(n);
    LiveRange *NeighLR = NeighIGNode->getParentLR();

    if( NeighLR->hasColor() )                        // if has a color
      IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color

    else if( NeighLR->hasSuggestedColor() )        // or has a suggest col   
      IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true; 
    
  }


  if( LR->hasSuggestedColor() ) {
    if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
      LR->setColor(  LR->getSuggestedColor() );
      return;
    }
    else {                              // can't allocate the suggested col
      cout << " Coud NOT allocate the suggested color for LR ";
      LR->printSet(); cout << endl;
    }
  }

  unsigned SearchStart;                 // start pos of color in pref-order
  bool ColorFound= false;               // have we found a color yet?

  //if this Node is between calls
  if( LR->getNumOfCallInterferences() == 0) { 

    // start with volatiles (we can  allocate volatiles safely)
    SearchStart = SparcIntRegOrder::StartOfAllRegs;  
  }
  else {           
    // start with non volatiles (no non-volatiles)
    SearchStart =  SparcIntRegOrder::StartOfNonVolatileRegs;  
  }

  unsigned c=0;                         // color
 
  // find first unused color
  for( c=SearchStart; c < SparcIntRegOrder::NumOfAvailRegs; c++) { 
    if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
  }

  if( ColorFound) 
    LR->setColor(c);                  // first color found in preffered order


  // if color is not found because of call interference
  // try even finding a volatile color and insert save across calls
  else if( LR->getNumOfCallInterferences() ) 
  { 
    // start from 0 - try to find even a volatile this time
    SearchStart = SparcIntRegOrder::StartOfAllRegs;  

    // find first unused volatile color
    for(c=SearchStart; c < SparcIntRegOrder::StartOfNonVolatileRegs; c++) { 
      if( ! IsColorUsedArr[ c ] ) { ColorFound = true; break; }
    }

    if( ColorFound) { 
      LR->setColor(c);  
      // since LR span across calls, must save across calls 
      LR->markForSaveAcrossCalls();       
    }

  }


  // If we couldn't find a color regardless of call interference - i.e., we
  // don't have either a volatile or non-volatile color left
  if( !ColorFound )  
    LR->markForSpill();               // no color found - must spill


  if( DEBUG_RA)                  
      UltraSparcRegInfo::printReg(  LR  );

}






//-----------------------------------------------------------------------------
// Float Register Class
//-----------------------------------------------------------------------------

// find the first available color in the range [Start,End] depending on the
// type of the Node (i.e., float/double)

int SparcFloatRegClass::findFloatColor(const LiveRange *const LR, 
				       unsigned Start,
 				       unsigned End, 
				       bool IsColorUsedArr[] ) const
{

  bool ColorFound = false;
  unsigned c;

  if( LR->getTypeID() == Type::DoubleTyID ) { 
      
    // find first unused color for a double 
    for( c=Start; c < End ;c+= 2){
      if( ! IsColorUsedArr[ c ] &&  ! IsColorUsedArr[ c+1 ]) 
	{ ColorFound=true;  break; }
    }
    
  } else {
    
    // find first unused color for a single
    for( c=Start; c < End; c++) { 
      if( ! IsColorUsedArr[ c ] ) { ColorFound=true;  break; }
    }
  }
  
  if( ColorFound ) return c;
  else return -1;
}





void SparcFloatRegClass::colorIGNode(IGNode * Node,bool IsColorUsedArr[]) const
{

  /* Algorithm:

     If the LR is a double try to allocate f32 - f63
     If the above fails or LR is single precision
        If the LR does not interfere with a call
	   start allocating from f0
	Else start allocating from f6
     If a color is still not found because LR interferes with a call
        Search in f0 - f6. If found mark for spill across calls.
     If a color is still not fond, mark for spilling
  */


  LiveRange * LR = Node->getParentLR();
  unsigned NumNeighbors =  Node->getNumOfNeighbors();   // total # of neighbors

  for(unsigned n=0; n < NumNeighbors; n++) {            // for each neigh 
    IGNode *NeighIGNode = Node->getAdjIGNode(n);
    LiveRange *NeighLR = NeighIGNode->getParentLR();

      if( NeighLR->hasColor() )   {                     // if neigh has a color
      	IsColorUsedArr[ NeighLR->getColor() ] = true; // record that color
	if( NeighLR->getTypeID() == Type::DoubleTyID )
	  IsColorUsedArr[ (NeighLR->getColor()) + 1 ] = true;  
      }
      else if( NeighLR->hasSuggestedColor() )   {   // if neigh has sugg color
      	IsColorUsedArr[ NeighLR->getSuggestedColor() ] = true;
	if( NeighLR->getTypeID() == Type::DoubleTyID )
	  IsColorUsedArr[ (NeighLR->getSuggestedColor()) + 1 ] = true;  
      }

  }


  if( LR->hasSuggestedColor() ) {
    if( ! IsColorUsedArr[ LR->getSuggestedColor() ] ) {
      LR->setColor(  LR->getSuggestedColor() );
      return;
    }
    else {                              // can't allocate the suggested col
      cout << " Coud NOT allocate the suggested color for LR ";
      LR->printSet(); cout << endl;
    }
  }


  int ColorFound = -1;               // have we found a color yet?
  unsigned NumOfCallInterf = LR->getNumOfCallInterferences();

  // if value is a double - search the double only reigon (f32 - f63)
  if( LR->getTypeID() == Type::DoubleTyID )       
    ColorFound = findFloatColor( LR, 32, 64, IsColorUsedArr );
    

  if( ColorFound >= 0 ) {
    LR->setColor(ColorFound);                
    if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
    return;
  }

  else { // the above fails or LR is single precision

    unsigned SearchStart;                 // start pos of color in pref-order

    //if this Node is between calls (i.e., no call interferences )
    if( ! NumOfCallInterf ) {
      // start with volatiles (we can  allocate volatiles safely)
      SearchStart = SparcFloatRegOrder::StartOfAllRegs;  
    }
    else {           
      // start with non volatiles (no non-volatiles)
      SearchStart =  SparcFloatRegOrder::StartOfNonVolatileRegs;  
    }
    
    ColorFound = findFloatColor( LR, SearchStart, 32, IsColorUsedArr );

  }

  if( ColorFound >= 0 ) {
    LR->setColor(ColorFound);                  
    if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
    return;
  }


  else if( NumOfCallInterf ) { 

    // We are here because there is a call interference and no non-volatile
    // color could be found.
    // Now try to allocate even a volatile color

    ColorFound = findFloatColor( LR, SparcFloatRegOrder::StartOfAllRegs, 
				SparcFloatRegOrder::StartOfNonVolatileRegs,
				IsColorUsedArr);
  }



  if( ColorFound >= 0 ) {
    LR->setColor(ColorFound);         // first color found in preffered order
    LR->markForSaveAcrossCalls();  
    if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
    return;
  }


  // we are here because no color could be found

  LR->markForSpill();               // no color found - must spill
  if( DEBUG_RA) UltraSparcRegInfo::printReg( LR );
  

}