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