Fixed logic for checking whether a LR received the correct color.
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9RegInfo.cpp
1 #include "llvm/Target/Sparc.h"
2 #include "SparcInternals.h"
3 #include "llvm/Method.h"
4 #include "llvm/iTerminators.h"
5 #include "llvm/iOther.h"
6 #include "llvm/CodeGen/InstrScheduling.h"
7 #include "llvm/CodeGen/InstrSelection.h"
8
9 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
10 #include "llvm/CodeGen/PhyRegAlloc.h"
11
12
13
14
15 //---------------------------------------------------------------------------
16 // UltraSparcRegInfo
17 //---------------------------------------------------------------------------
18
19 //---------------------------------------------------------------------------
20 // Finds the return value of a call instruction
21 //---------------------------------------------------------------------------
22
23 const Value * 
24 UltraSparcRegInfo::getCallInstRetVal(const MachineInstr *CallMI) const{
25
26   unsigned OpCode = CallMI->getOpCode();
27   unsigned NumOfImpRefs =  CallMI->getNumImplicitRefs();
28
29   if( OpCode == CALL ) {
30
31     // The one before the last implicit operand is the return value of 
32     // a CALL instr
33     if( NumOfImpRefs > 1 )
34       if(  CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) 
35         return  CallMI->getImplicitRef(NumOfImpRefs-2); 
36
37   }
38   else if( OpCode == JMPLCALL) {
39
40     // The last implicit operand is the return value of a JMPL in   
41     if( NumOfImpRefs > 0 )
42       if(  CallMI->implicitRefIsDefined(NumOfImpRefs-1) ) 
43         return  CallMI->getImplicitRef(NumOfImpRefs-1); 
44   }
45   else
46     assert(0 && "OpCode must be CALL/JMPL for a call instr");
47
48   return NULL;
49
50 }
51
52 //---------------------------------------------------------------------------
53 // Finds the return address of a call instruction
54 //---------------------------------------------------------------------------
55
56 const Value *
57 UltraSparcRegInfo::getCallInstRetAddr(const MachineInstr *CallMI)const {
58
59   unsigned OpCode = CallMI->getOpCode();
60
61   if( OpCode == CALL) {
62
63     unsigned NumOfImpRefs =  CallMI->getNumImplicitRefs();
64
65     assert( NumOfImpRefs && "CALL instr must have at least on ImpRef");
66     // The last implicit operand is the return address of a CALL instr
67     return  CallMI->getImplicitRef(NumOfImpRefs-1); 
68
69   }
70   else if( OpCode == JMPLCALL ) {
71
72     MachineOperand & MO  = ( MachineOperand &) CallMI->getOperand(2);
73     return MO.getVRegValue();
74
75   }
76   else
77     assert(0 && "OpCode must be CALL/JMPL for a call instr");
78
79   assert(0  && "There must be a return addr for a call instr");
80
81   return NULL;
82
83 }
84
85
86 //---------------------------------------------------------------------------
87 // Finds the # of actual arguments of the call instruction
88 //---------------------------------------------------------------------------
89
90 const unsigned 
91 UltraSparcRegInfo::getCallInstNumArgs(const MachineInstr *CallMI) const {
92
93   unsigned OpCode = CallMI->getOpCode();
94   unsigned NumOfImpRefs =  CallMI->getNumImplicitRefs();
95   int NumArgs = -1;
96
97   if( OpCode == CALL ) {
98
99     switch( NumOfImpRefs ) {
100
101     case 0: assert(0 && "A CALL inst must have at least one ImpRef (RetAddr)");
102
103     case 1: NumArgs = 0;
104             break;
105     
106     default:  // two or more implicit refs
107       if(  CallMI->implicitRefIsDefined(NumOfImpRefs-2) ) 
108         NumArgs = NumOfImpRefs - 2;    // i.e., NumOfImpRef-2 is the ret val
109       else 
110         NumArgs = NumOfImpRefs - 1;
111     }
112
113   }
114   else if( OpCode == JMPLCALL ) {
115
116     // The last implicit operand is the return value of a JMPL instr
117     if( NumOfImpRefs > 0 ) {
118       if(  CallMI->implicitRefIsDefined(NumOfImpRefs-1) ) 
119         NumArgs = NumOfImpRefs - 1;    // i.e., NumOfImpRef-1 is the ret val
120       else 
121         NumArgs = NumOfImpRefs;
122     }
123     else 
124       NumArgs = NumOfImpRefs;
125   }
126   else
127     assert(0 && "OpCode must be CALL/JMPL for a call instr");
128
129   assert( (NumArgs != -1)  && "Internal error in getCallInstNumArgs" );
130   return (unsigned) NumArgs;
131  
132
133 }
134
135
136 //---------------------------------------------------------------------------
137 // Suggests a register for the ret address in the RET machine instruction
138 //---------------------------------------------------------------------------
139
140 void UltraSparcRegInfo::suggestReg4RetAddr(const MachineInstr * RetMI, 
141                                            LiveRangeInfo& LRI) const {
142
143   assert( (RetMI->getNumOperands() >= 2)
144           && "JMPL/RETURN must have 3 and 2 operands respectively");
145   
146   MachineOperand & MO  = ( MachineOperand &) RetMI->getOperand(0);
147
148   MO.setRegForValue( getUnifiedRegNum( IntRegClassID, SparcIntRegOrder::i7) );
149   
150   // TODO (Optimize): 
151   // Instead of setting the color, we can suggest one. In that case,
152   // we have to test later whether it received the suggested color.
153   // In that case, a LR has to be created at the start of method.
154   // It has to be done as follows (remove the setRegVal above):
155
156   /*
157   const Value *RetAddrVal = MO.getVRegValue();
158
159   assert( RetAddrVal && "LR for ret address must be created at start");
160
161   LiveRange * RetAddrLR = LRI.getLiveRangeForValue( RetAddrVal);  
162   RetAddrLR->setSuggestedColor(getUnifiedRegNum( IntRegClassID, 
163   SparcIntRegOrdr::i7) );
164   */
165
166
167 }
168
169
170 //---------------------------------------------------------------------------
171 // Suggests a register for the ret address in the JMPL/CALL machine instr
172 //---------------------------------------------------------------------------
173 void UltraSparcRegInfo::suggestReg4CallAddr(const MachineInstr * CallMI,
174                                             LiveRangeInfo& LRI,
175                                             vector<RegClass *> RCList) const {
176
177
178   const Value *RetAddrVal = getCallInstRetAddr( CallMI );
179
180   // RetAddrVal cannot be NULL (asserted in  getCallInstRetAddr)
181   // create a new LR for the return address and color it
182   
183   LiveRange * RetAddrLR = new LiveRange();  
184   RetAddrLR->add( RetAddrVal );
185   unsigned RegClassID = getRegClassIDOfValue( RetAddrVal );
186   RetAddrLR->setRegClass( RCList[RegClassID] );
187   RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID,SparcIntRegOrder::o7));
188   LRI.addLRToMap( RetAddrVal, RetAddrLR);
189   
190
191   /*  
192   assert( (CallMI->getNumOperands() == 3) && "JMPL must have 3 operands");
193
194   // directly set color since the LR of ret address (if there were one) 
195   // will not extend after the call instr
196
197   MachineOperand & MO  = ( MachineOperand &) CallMI->getOperand(2);
198   MO.setRegForValue( getUnifiedRegNum( IntRegClassID,SparcIntRegOrder::o7) );
199
200   */
201
202 }
203
204
205
206
207 //---------------------------------------------------------------------------
208 //  This method will suggest colors to incoming args to a method. 
209 //  If the arg is passed on stack due to the lack of regs, NOTHING will be
210 //  done - it will be colored (or spilled) as a normal value.
211 //---------------------------------------------------------------------------
212
213 void UltraSparcRegInfo::suggestRegs4MethodArgs(const Method *const Meth, 
214                                                LiveRangeInfo& LRI) const 
215 {
216
217                                                  // get the argument list
218   const Method::ArgumentListType& ArgList = Meth->getArgumentList();           
219                                                  // get an iterator to arg list
220   Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); 
221
222   // for each argument
223   for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) {    
224
225     // get the LR of arg
226     LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); 
227     assert( LR && "No live range found for method arg");
228
229     unsigned RegType = getRegType( LR );
230
231
232     // if the arg is in int class - allocate a reg for an int arg
233     if( RegType == IntRegType ) {
234
235       if( argNo < NumOfIntArgRegs) {
236         LR->setSuggestedColor( SparcIntRegOrder::i0 + argNo );
237
238       }
239   
240       else {
241         // Do NOTHING as this will be colored as a normal value.
242         if (DEBUG_RA) cerr << " Int Regr not suggested for method arg\n";
243       }
244      
245     }
246     else if( RegType==FPSingleRegType && (argNo*2+1) < NumOfFloatArgRegs) 
247       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) );
248     
249  
250     else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) 
251       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); 
252     
253
254   }
255   
256 }
257
258 //---------------------------------------------------------------------------
259 // 
260 //---------------------------------------------------------------------------
261
262 void UltraSparcRegInfo::colorMethodArgs(const Method *const Meth, 
263                                         LiveRangeInfo& LRI,
264                                         AddedInstrns *const FirstAI) const {
265
266                                                  // get the argument list
267   const Method::ArgumentListType& ArgList = Meth->getArgumentList();           
268                                                  // get an iterator to arg list
269   Method::ArgumentListType::const_iterator ArgIt = ArgList.begin(); 
270
271   MachineInstr *AdMI;
272
273
274   // for each argument
275   for( unsigned argNo=0; ArgIt != ArgList.end() ; ++ArgIt, ++argNo) {    
276
277     // get the LR of arg
278     LiveRange *const LR = LRI.getLiveRangeForValue((const Value *) *ArgIt); 
279     assert( LR && "No live range found for method arg");
280
281
282     unsigned RegType = getRegType( LR );
283     unsigned RegClassID = (LR->getRegClass())->getID();
284
285
286     // find whether this argument is coming in a register (if not, on stack)
287
288     bool isArgInReg = false;
289     unsigned UniArgReg = InvalidRegNum;  // reg that LR MUST be colored with
290
291     if( (RegType== IntRegType && argNo <  NumOfIntArgRegs)) {
292       isArgInReg = true;
293       UniArgReg = getUnifiedRegNum( RegClassID, SparcIntRegOrder::i0 + argNo );
294     }
295     else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs)  { 
296       isArgInReg = true;
297       UniArgReg = getUnifiedRegNum( RegClassID, 
298                                     SparcFloatRegOrder::f0 + argNo*2 + 1 ) ;
299     }
300     else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs)  { 
301       isArgInReg = true;
302       UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2);
303     }
304
305     
306     if( LR->hasColor() ) {
307
308       unsigned UniLRReg = getUnifiedRegNum(  RegClassID, LR->getColor() );
309
310       // if LR received the correct color, nothing to do
311       if( UniLRReg == UniArgReg )
312         continue;
313
314       // We are here because the LR did not have a suggested 
315       // color or did not receive the suggested color but LR got a register.
316       // Now we have to copy %ix reg (or stack pos of arg) 
317       // to the register it was colored with.
318       
319       // if the arg is coming in UniArgReg register MUST go into
320       // the UniLRReg register
321       if( isArgInReg ) 
322         AdMI = cpReg2RegMI( UniArgReg, UniLRReg, RegType );
323
324       else 
325         assert(0 && "TODO: Color an Incoming arg on stack");
326
327       // Now add the instruction
328       FirstAI->InstrnsBefore.push_back( AdMI );
329
330     }
331
332     else {                                // LR is not colored (i.e., spilled)
333       
334       assert(0 && "TODO: Color a spilled arg ");
335       
336     }
337
338
339   }  // for each incoming argument
340
341 }
342
343
344
345
346 //---------------------------------------------------------------------------
347 // This method is called before graph coloring to suggest colors to the
348 // outgoing call args and the return value of the call.
349 //---------------------------------------------------------------------------
350 void UltraSparcRegInfo::suggestRegs4CallArgs(const MachineInstr *const CallMI, 
351                                              LiveRangeInfo& LRI,
352                                              vector<RegClass *> RCList) const {
353
354   assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) );
355
356   suggestReg4CallAddr(CallMI, LRI, RCList);
357
358
359   // First color the return value of the call instruction. The return value
360   // will be in %o0 if the value is an integer type, or in %f0 if the 
361   // value is a float type.
362
363   // the return value cannot have a LR in machine instruction since it is
364   // only defined by the call instruction
365
366   // if type is not void,  create a new live range and set its 
367   // register class and add to LRI
368
369
370   const Value *RetVal = getCallInstRetVal( CallMI );
371
372
373   if( RetVal ) {
374
375     assert( (! LRI.getLiveRangeForValue( RetVal ) ) && 
376             "LR for ret Value of call already definded!");
377
378
379       // create a new LR for the return value
380
381     LiveRange * RetValLR = new LiveRange();  
382     RetValLR->add( RetVal );
383     unsigned RegClassID = getRegClassIDOfValue( RetVal );
384     RetValLR->setRegClass( RCList[RegClassID] );
385     LRI.addLRToMap( RetVal, RetValLR);
386     
387     // now suggest a register depending on the register class of ret arg
388
389     if( RegClassID == IntRegClassID ) 
390       RetValLR->setSuggestedColor(SparcIntRegOrder::o0);
391     else if (RegClassID == FloatRegClassID ) 
392       RetValLR->setSuggestedColor(SparcFloatRegOrder::f0 );
393     else assert( 0 && "Unknown reg class for return value of call\n");
394
395   }
396
397   
398   // Now suggest colors for arguments (operands) of the call instruction.
399   // Colors are suggested only if the arg number is smaller than the
400   // the number of registers allocated for argument passing.
401   // Now, go thru call args - implicit operands of the call MI
402
403   unsigned NumOfCallArgs =  getCallInstNumArgs( CallMI );
404   
405   for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) {
406
407     const Value *CallArg = CallMI->getImplicitRef(i);
408     
409     // get the LR of call operand (parameter)
410     LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); 
411
412     // not possible to have a null LR since all args (even consts)  
413     // must be defined before
414     if( !LR ) {          
415       if( DEBUG_RA) {
416         cerr << " ERROR: In call instr, no LR for arg:  " ;
417         printValue(CallArg); cerr << endl;
418       }
419       assert(0 && "NO LR for call arg");  
420       // continue;
421     }
422     
423     unsigned RegType = getRegType( LR );
424
425     // if the arg is in int class - allocate a reg for an int arg
426     if( RegType == IntRegType ) {
427
428       if( argNo < NumOfIntArgRegs) 
429         LR->setSuggestedColor( SparcIntRegOrder::o0 + argNo );
430
431       else if (DEBUG_RA) 
432         // Do NOTHING as this will be colored as a normal value.
433         cerr << " Regr not suggested for int call arg" << endl;
434       
435     }
436     else if( RegType == FPSingleRegType &&  (argNo*2 +1)< NumOfFloatArgRegs) 
437       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) );
438     
439  
440     else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) 
441       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); 
442     
443
444   } // for all call arguments
445
446 }
447
448
449 //---------------------------------------------------------------------------
450 // After graph coloring, we have call this method to see whehter the return
451 // value and the call args received the correct colors. If not, we have
452 // to instert copy instructions.
453 //---------------------------------------------------------------------------
454
455
456 void UltraSparcRegInfo::colorCallArgs(const MachineInstr *const CallMI,
457                                       LiveRangeInfo& LRI,
458                                       AddedInstrns *const CallAI) const {
459
460   assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) );
461
462   // First color the return value of the call.
463   // If there is a LR for the return value, it means this
464   // method returns a value
465   
466   MachineInstr *AdMI;
467
468   const Value *RetVal = getCallInstRetVal( CallMI );
469
470   if( RetVal ) {
471
472     LiveRange * RetValLR = LRI.getLiveRangeForValue( RetVal );
473
474     if( !RetValLR ) {
475       cerr << "\nNo LR for:";
476       printValue( RetVal );
477       cerr << endl;
478       assert( RetValLR && "ERR:No LR for non-void return value");
479       //return;
480     }
481
482     unsigned RegClassID = (RetValLR->getRegClass())->getID();    
483     bool recvCorrectColor = false;
484
485     unsigned CorrectCol;                // correct color for ret value
486     if(RegClassID == IntRegClassID)
487       CorrectCol = SparcIntRegOrder::o0;
488     else if(RegClassID == FloatRegClassID)
489       CorrectCol = SparcFloatRegOrder::f0;
490     else 
491       assert( 0 && "Unknown RegClass");
492
493
494     // if the LR received the correct color, NOTHING to do
495
496     if(  RetValLR->hasColor() )
497       if( RetValLR->getColor() == CorrectCol )
498         recvCorrectColor = true;
499
500
501     // if we didn't receive the correct color for some reason, 
502     // put copy instruction
503     
504     if( !recvCorrectColor ) {
505       
506       if( RetValLR->hasColor() ) {
507         
508         unsigned RegType = getRegType( RetValLR );
509
510         unsigned 
511           UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor());
512
513         // the  reg that LR must be colored with
514         unsigned UniRetReg = getUnifiedRegNum( RegClassID, CorrectCol); 
515         
516         // the return value is coming in UniRetReg but has to go into
517         // the UniRetLRReg
518
519         AdMI = cpReg2RegMI( UniRetReg, UniRetLRReg, RegType );  
520         CallAI->InstrnsAfter.push_back( AdMI );
521         
522         
523       } // if LR has color
524       else {
525         
526         assert(0 && "LR of return value is splilled");
527       }
528       
529       
530     } // the LR didn't receive the suggested color  
531     
532   } // if there a return value
533   
534
535   // Now color all args of the call instruction
536
537   unsigned NumOfCallArgs =  getCallInstNumArgs( CallMI );
538
539   for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) {
540
541     const Value *CallArg = CallMI->getImplicitRef(i);
542
543     // get the LR of call operand (parameter)
544     LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); 
545
546     unsigned RegType = getRegType( CallArg );
547     unsigned RegClassID =  getRegClassIDOfValue( CallArg);
548     
549     // find whether this argument is coming in a register (if not, on stack)
550
551     bool isArgInReg = false;
552     unsigned UniArgReg = InvalidRegNum;  // reg that LR must be colored with
553
554     if( (RegType== IntRegType && argNo <  NumOfIntArgRegs)) {
555       isArgInReg = true;
556       UniArgReg = getUnifiedRegNum(RegClassID, SparcIntRegOrder::o0 + argNo );
557     }
558     else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs)  { 
559       isArgInReg = true;
560       UniArgReg = getUnifiedRegNum(RegClassID, 
561                                    SparcFloatRegOrder::f0 + (argNo*2 + 1) );
562     }
563     else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs)  { 
564       isArgInReg = true;
565       UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2);
566     }
567
568
569     // not possible to have a null LR since all args (even consts)  
570     // must be defined before
571     if( !LR ) {          
572       if( DEBUG_RA) {
573         cerr << " ERROR: In call instr, no LR for arg:  " ;
574         printValue(CallArg); cerr << endl;
575       }
576       assert(0 && "NO LR for call arg");  
577       // continue;
578     }
579
580
581     // if the LR received the suggested color, NOTHING to do
582
583
584     if( LR->hasColor() ) {
585
586
587       unsigned UniLRReg = getUnifiedRegNum( RegClassID,  LR->getColor() );
588
589       // if LR received the correct color, nothing to do
590       if( UniLRReg == UniArgReg )
591         continue;
592
593       // We are here because though the LR is allocated a register, it
594       // was not allocated the suggested register. So, we have to copy %ix reg 
595       // (or stack pos of arg) to the register it was colored with
596
597       // the LR is colored with UniLRReg but has to go into  UniArgReg
598       // to pass it as an argument
599
600       if( isArgInReg ) 
601         AdMI = cpReg2RegMI(UniLRReg, UniArgReg, RegType );
602
603       else 
604         assert(0 && "TODO: Push an outgoing arg on stack");
605
606       // Now add the instruction
607       CallAI->InstrnsBefore.push_back( AdMI );
608
609     }
610
611     else {                                // LR is not colored (i.e., spilled)
612       
613       assert(0 && "TODO: Copy a spilled call arg to an output reg ");
614       
615     }
616
617   }  // for each parameter in call instruction
618
619 }
620
621 //---------------------------------------------------------------------------
622 // This method is called for an LLVM return instruction to identify which
623 // values will be returned from this method and to suggest colors.
624 //---------------------------------------------------------------------------
625 void UltraSparcRegInfo::suggestReg4RetValue(const MachineInstr *const RetMI, 
626                                              LiveRangeInfo& LRI) const {
627
628   assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) );
629
630     suggestReg4RetAddr(RetMI, LRI);
631
632   // if there is an implicit ref, that has to be the ret value
633   if(  RetMI->getNumImplicitRefs() > 0 ) {
634
635     // The first implicit operand is the return value of a return instr
636     const Value *RetVal =  RetMI->getImplicitRef(0);
637
638     MachineInstr *AdMI;
639     LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); 
640
641     if( !LR ) {
642      cerr << "\nNo LR for:";
643      printValue( RetVal );
644      cerr << endl;
645      assert( LR && "No LR for return value of non-void method");
646      //return;
647    }
648
649     unsigned RegClassID = (LR->getRegClass())->getID();
650       
651     if( RegClassID == IntRegClassID ) 
652       LR->setSuggestedColor(SparcIntRegOrder::i0);
653     
654     else if ( RegClassID == FloatRegClassID ) 
655       LR->setSuggestedColor(SparcFloatRegOrder::f0);
656       
657   }
658
659 }
660
661 //---------------------------------------------------------------------------
662
663 //---------------------------------------------------------------------------
664 void UltraSparcRegInfo::colorRetValue(const  MachineInstr *const RetMI, 
665                                       LiveRangeInfo& LRI,
666                                       AddedInstrns *const RetAI) const {
667
668   assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) );
669
670   // if there is an implicit ref, that has to be the ret value
671   if(  RetMI->getNumImplicitRefs() > 0 ) {
672
673     // The first implicit operand is the return value of a return instr
674     const Value *RetVal =  RetMI->getImplicitRef(0);
675
676     MachineInstr *AdMI;
677     LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); 
678
679     if( ! LR ) {
680         cerr << "\nNo LR for:";
681         printValue( RetVal );
682         cerr << endl;
683         // assert( LR && "No LR for return value of non-void method");
684         return;
685    }
686
687     unsigned RegClassID =  getRegClassIDOfValue(RetVal);
688     unsigned RegType = getRegType( RetVal );
689
690
691     unsigned CorrectCol;
692     if(RegClassID == IntRegClassID)
693       CorrectCol = SparcIntRegOrder::i0;
694     else if(RegClassID == FloatRegClassID)
695       CorrectCol = SparcFloatRegOrder::f0;
696     else 
697       assert( 0 && "Unknown RegClass");
698
699
700     // if the LR received the correct color, NOTHING to do
701
702     if(  LR->hasColor() )
703       if( LR->getColor() == CorrectCol )
704         return;
705
706     unsigned UniRetReg =  getUnifiedRegNum( RegClassID, CorrectCol );
707
708     if( LR->hasColor() ) {
709
710       // We are here because the LR was allocted a regiter
711       // It may be the suggested register or not
712
713       // copy the LR of retun value to i0 or f0
714
715       unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor());
716
717       // the LR received  UniLRReg but must be colored with UniRetReg
718       // to pass as the return value
719
720       AdMI = cpReg2RegMI( UniLRReg, UniRetReg, RegType); 
721       RetAI->InstrnsBefore.push_back( AdMI );
722     }
723     else 
724       assert(0 && "TODO: Copy the return value from stack\n");
725
726   } // if there is a return value
727
728 }
729
730
731 //---------------------------------------------------------------------------
732 // Copy from a register to register. Register number must be the unified
733 // register number
734 //---------------------------------------------------------------------------
735
736
737 MachineInstr * UltraSparcRegInfo::cpReg2RegMI(const unsigned SrcReg, 
738                                               const unsigned DestReg,
739                                               const int RegType) const {
740
741   assert( ((int)SrcReg != InvalidRegNum) && ((int)DestReg != InvalidRegNum) &&
742           "Invalid Register");
743   
744   MachineInstr * MI = NULL;
745
746   switch( RegType ) {
747     
748   case IntRegType:
749   case IntCCRegType:
750   case FloatCCRegType: 
751     MI = new MachineInstr(ADD, 3);
752     MI->SetMachineOperand(0, SrcReg, false);
753     MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
754     MI->SetMachineOperand(2, DestReg, true);
755     break;
756
757   case FPSingleRegType:
758     MI = new MachineInstr(FMOVS, 2);
759     MI->SetMachineOperand(0, SrcReg, false);
760     MI->SetMachineOperand(1, DestReg, true);
761     break;
762
763   case FPDoubleRegType:
764     MI = new MachineInstr(FMOVD, 2);
765     MI->SetMachineOperand(0, SrcReg, false);    
766     MI->SetMachineOperand(1, DestReg, true);
767     break;
768
769   default:
770     assert(0 && "Unknow RegType");
771   }
772
773   return MI;
774 }
775
776
777 //---------------------------------------------------------------------------
778 // Copy from a register to memory. Register number must be the unified
779 // register number
780 //---------------------------------------------------------------------------
781
782
783 MachineInstr * UltraSparcRegInfo::cpReg2MemMI(const unsigned SrcReg, 
784                                               const unsigned DestPtrReg,
785                                               const int Offset,
786                                               const int RegType) const {
787
788
789   MachineInstr * MI = NULL;
790
791   switch( RegType ) {
792     
793   case IntRegType:
794   case IntCCRegType:
795   case FloatCCRegType: 
796     MI = new MachineInstr(STX, 3);
797     MI->SetMachineOperand(0, DestPtrReg, false);
798     MI->SetMachineOperand(1, SrcReg, false);
799     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
800                           (int64_t) Offset, false);
801     break;
802
803   case FPSingleRegType:
804     MI = new MachineInstr(ST, 3);
805     MI->SetMachineOperand(0, DestPtrReg, false);
806     MI->SetMachineOperand(1, SrcReg, false);
807     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
808                           (int64_t) Offset, false);
809     break;
810
811   case FPDoubleRegType:
812     MI = new MachineInstr(STD, 3);
813     MI->SetMachineOperand(0, DestPtrReg, false);
814     MI->SetMachineOperand(1, SrcReg, false);
815     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
816                           (int64_t) Offset, false);
817     break;
818
819   default:
820     assert(0 && "Unknow RegType");
821   }
822
823   return MI;
824 }
825
826
827 //---------------------------------------------------------------------------
828 // Copy from memory to a reg. Register number must be the unified
829 // register number
830 //---------------------------------------------------------------------------
831
832
833 MachineInstr * UltraSparcRegInfo::cpMem2RegMI(const unsigned SrcPtrReg, 
834                                               const int Offset,
835                                               const unsigned DestReg,
836                                               const int RegType) const {
837   
838   MachineInstr * MI = NULL;
839
840   switch( RegType ) {
841     
842   case IntRegType:
843   case IntCCRegType:
844   case FloatCCRegType: 
845     MI = new MachineInstr(LDX, 3);
846     MI->SetMachineOperand(0, SrcPtrReg, false);
847     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
848                           (int64_t) Offset, false);
849     MI->SetMachineOperand(2, DestReg, false);
850     break;
851
852   case FPSingleRegType:
853     MI = new MachineInstr(LD, 3);
854     MI->SetMachineOperand(0, SrcPtrReg, false);
855     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
856                           (int64_t) Offset, false);
857     MI->SetMachineOperand(2, DestReg, false);
858
859     break;
860
861   case FPDoubleRegType:
862     MI = new MachineInstr(LDD, 3);
863     MI->SetMachineOperand(0, SrcPtrReg, false);
864     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
865                           (int64_t) Offset, false);
866     MI->SetMachineOperand(2, DestReg, false);
867     break;
868
869   default:
870     assert(0 && "Unknow RegType");
871   }
872
873   return MI;
874 }
875
876
877
878
879
880
881
882
883
884 //---------------------------------------------------------------------------
885 // Only  constant/label values are accepted.
886 // ***This code is temporary ***
887 //---------------------------------------------------------------------------
888
889
890 MachineInstr * UltraSparcRegInfo::cpValue2RegMI(Value * Val, 
891                                                 const unsigned DestReg,
892                                                 const int RegType) const {
893
894   assert( ((int)DestReg != InvalidRegNum) && "Invalid Register");
895
896   /*
897   unsigned MReg;
898   int64_t Imm;
899
900   MachineOperand::MachineOperandType MOTypeInt = 
901     ChooseRegOrImmed(Val, ADD,  *UltraSparcInfo, true, MReg, Imm);
902   */
903
904   MachineOperand::MachineOperandType MOType;
905
906   switch( Val->getValueType() ) {
907
908   case Value::ConstantVal: 
909   case Value::GlobalVariableVal:
910     MOType = MachineOperand:: MO_UnextendedImmed;  // TODO**** correct???
911     break;
912
913   case Value::BasicBlockVal:
914   case Value::MethodVal:
915     MOType = MachineOperand::MO_PCRelativeDisp;
916     break;
917
918   default:
919     cerr << "Value Type: " << Val->getValueType() << endl;
920     assert(0 && "Unknown val type - Only constants/globals/labels are valid");
921   }
922
923
924
925   MachineInstr * MI = NULL;
926
927   switch( RegType ) {
928     
929   case IntRegType:
930     MI = new MachineInstr(ADD);
931     MI->SetMachineOperand(0, MOType, Val, false);
932     MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
933     MI->SetMachineOperand(2, DestReg, true);
934     break;
935
936   case FPSingleRegType:
937     assert(0 && "FP const move not yet implemented");
938     MI = new MachineInstr(FMOVS);
939     MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false);
940     MI->SetMachineOperand(1, DestReg, true);
941     break;
942
943   case FPDoubleRegType:    
944     assert(0 && "FP const move not yet implemented");
945     MI = new MachineInstr(FMOVD);
946     MI->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, Val, false);
947     MI->SetMachineOperand(1, DestReg, true);
948     break;
949
950   default:
951     assert(0 && "Unknow RegType");
952   }
953
954   return MI;
955 }
956
957
958
959
960
961
962
963 //---------------------------------------------------------------------------
964 // Print the register assigned to a LR
965 //---------------------------------------------------------------------------
966
967 void UltraSparcRegInfo::printReg(const LiveRange *const LR) {
968
969   unsigned RegClassID = (LR->getRegClass())->getID();
970
971   cerr << " *Node " << (LR->getUserIGNode())->getIndex();
972
973   if( ! LR->hasColor() ) {
974     cerr << " - could not find a color" << endl;
975     return;
976   }
977   
978   // if a color is found
979
980   cerr << " colored with color "<< LR->getColor();
981
982   if( RegClassID == IntRegClassID ) {
983
984     cerr<< " [" << SparcIntRegOrder::getRegName(LR->getColor()) ;
985     cerr << "]" << endl;
986   }
987   else if ( RegClassID == FloatRegClassID) {
988     cerr << "[" << SparcFloatRegOrder::getRegName(LR->getColor());
989     if( LR->getTypeID() == Type::DoubleTyID )
990       cerr << "+" << SparcFloatRegOrder::getRegName(LR->getColor()+1);
991     cerr << "]" << endl;
992   }
993 }