9c57892392e6df1b15d27d4fb0d1096216921d5d
[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
326         // Now the arg is coming on stack. Since the LR recieved a register,
327         // we just have to load the arg on stack into that register
328         int ArgStakOffFromFP = 
329           UltraSparcFrameInfo::FirstIncomingArgOffsetFromFP + 
330           argNo * SizeOfOperandOnStack;
331
332         AdMI = cpMem2RegMI(getFramePointer(), ArgStakOffFromFP, 
333                            UniLRReg, RegType );
334       }
335
336       FirstAI->InstrnsBefore.push_back( AdMI );   
337       
338     } // if LR received a color
339
340     else {                             
341
342       // Now, the LR did not receive a color. But it has a stack offset for
343       // spilling.
344
345       // So, if the arg is coming in UniArgReg register,  we can just move
346       // that on to the stack pos of LR
347
348
349       if( isArgInReg ) {
350
351         MachineInstr *AdIBef = 
352           cpReg2MemMI(UniArgReg, getFramePointer(), 
353                       LR->getSpillOffFromFP(), RegType );
354
355         FirstAI->InstrnsBefore.push_back( AdMI );   
356       }
357
358       else {
359
360         // Now the arg is coming on stack. Since the LR did NOT 
361         // recieved a register as well, it is allocated a stack position. We
362         // can simply change the stack poistion of the LR. We can do this,
363         // since this method is called before any other method that makes
364         // uses of the stack pos of the LR (e.g., updateMachineInstr)
365
366         int ArgStakOffFromFP = 
367           UltraSparcFrameInfo::FirstIncomingArgOffsetFromFP + 
368           argNo * SizeOfOperandOnStack;
369
370         LR->modifySpillOffFromFP( ArgStakOffFromFP );
371       }
372
373     }
374
375   }  // for each incoming argument
376
377 }
378
379
380
381
382 //---------------------------------------------------------------------------
383 // This method is called before graph coloring to suggest colors to the
384 // outgoing call args and the return value of the call.
385 //---------------------------------------------------------------------------
386 void UltraSparcRegInfo::suggestRegs4CallArgs(const MachineInstr *const CallMI, 
387                                              LiveRangeInfo& LRI,
388                                              vector<RegClass *> RCList) const {
389
390   assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) );
391
392   suggestReg4CallAddr(CallMI, LRI, RCList);
393
394
395   // First color the return value of the call instruction. The return value
396   // will be in %o0 if the value is an integer type, or in %f0 if the 
397   // value is a float type.
398
399   // the return value cannot have a LR in machine instruction since it is
400   // only defined by the call instruction
401
402   // if type is not void,  create a new live range and set its 
403   // register class and add to LRI
404
405
406   const Value *RetVal = getCallInstRetVal( CallMI );
407
408
409   if( RetVal ) {
410
411     assert( (! LRI.getLiveRangeForValue( RetVal ) ) && 
412             "LR for ret Value of call already definded!");
413
414
415       // create a new LR for the return value
416
417     LiveRange * RetValLR = new LiveRange();  
418     RetValLR->add( RetVal );
419     unsigned RegClassID = getRegClassIDOfValue( RetVal );
420     RetValLR->setRegClass( RCList[RegClassID] );
421     LRI.addLRToMap( RetVal, RetValLR);
422     
423     // now suggest a register depending on the register class of ret arg
424
425     if( RegClassID == IntRegClassID ) 
426       RetValLR->setSuggestedColor(SparcIntRegOrder::o0);
427     else if (RegClassID == FloatRegClassID ) 
428       RetValLR->setSuggestedColor(SparcFloatRegOrder::f0 );
429     else assert( 0 && "Unknown reg class for return value of call\n");
430
431   }
432
433   
434   // Now suggest colors for arguments (operands) of the call instruction.
435   // Colors are suggested only if the arg number is smaller than the
436   // the number of registers allocated for argument passing.
437   // Now, go thru call args - implicit operands of the call MI
438
439   unsigned NumOfCallArgs =  getCallInstNumArgs( CallMI );
440   
441   for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) {
442
443     const Value *CallArg = CallMI->getImplicitRef(i);
444     
445     // get the LR of call operand (parameter)
446     LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); 
447
448     // not possible to have a null LR since all args (even consts)  
449     // must be defined before
450     if( !LR ) {          
451       if( DEBUG_RA) {
452         cerr << " ERROR: In call instr, no LR for arg:  " ;
453         printValue(CallArg); cerr << endl;
454       }
455       assert(0 && "NO LR for call arg");  
456       // continue;
457     }
458     
459     unsigned RegType = getRegType( LR );
460
461     // if the arg is in int class - allocate a reg for an int arg
462     if( RegType == IntRegType ) {
463
464       if( argNo < NumOfIntArgRegs) 
465         LR->setSuggestedColor( SparcIntRegOrder::o0 + argNo );
466
467       else if (DEBUG_RA) 
468         // Do NOTHING as this will be colored as a normal value.
469         cerr << " Regr not suggested for int call arg" << endl;
470       
471     }
472     else if( RegType == FPSingleRegType &&  (argNo*2 +1)< NumOfFloatArgRegs) 
473       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2 + 1) );
474     
475  
476     else if( RegType == FPDoubleRegType && (argNo*2) < NumOfFloatArgRegs) 
477       LR->setSuggestedColor( SparcFloatRegOrder::f0 + (argNo * 2) ); 
478     
479
480   } // for all call arguments
481
482 }
483
484
485 //---------------------------------------------------------------------------
486 // After graph coloring, we have call this method to see whehter the return
487 // value and the call args received the correct colors. If not, we have
488 // to instert copy instructions.
489 //---------------------------------------------------------------------------
490
491 void UltraSparcRegInfo::colorCallArgs(const MachineInstr *const CallMI,
492                                       LiveRangeInfo& LRI,
493                                       AddedInstrns *const CallAI,
494                                       PhyRegAlloc &PRA) const {
495
496   assert ( (UltraSparcInfo->getInstrInfo()).isCall(CallMI->getOpCode()) );
497
498   // First color the return value of the call.
499   // If there is a LR for the return value, it means this
500   // method returns a value
501   
502   MachineInstr *AdMI;
503
504   const Value *RetVal = getCallInstRetVal( CallMI );
505
506   if( RetVal ) {
507
508     LiveRange * RetValLR = LRI.getLiveRangeForValue( RetVal );
509
510     if( !RetValLR ) {
511       cerr << "\nNo LR for:";
512       printValue( RetVal );
513       cerr << endl;
514       assert( RetValLR && "ERR:No LR for non-void return value");
515       //return;
516     }
517
518     unsigned RegClassID = (RetValLR->getRegClass())->getID();    
519     bool recvCorrectColor = false;
520
521     unsigned CorrectCol;                // correct color for ret value
522     if(RegClassID == IntRegClassID)
523       CorrectCol = SparcIntRegOrder::o0;
524     else if(RegClassID == FloatRegClassID)
525       CorrectCol = SparcFloatRegOrder::f0;
526     else 
527       assert( 0 && "Unknown RegClass");
528
529
530     // if the LR received the correct color, NOTHING to do
531
532     if(  RetValLR->hasColor() )
533       if( RetValLR->getColor() == CorrectCol )
534         recvCorrectColor = true;
535
536
537     // if we didn't receive the correct color for some reason, 
538     // put copy instruction
539     
540     if( !recvCorrectColor ) {
541
542       unsigned RegType = getRegType( RetValLR );
543
544       // the  reg that LR must be colored with 
545       unsigned UniRetReg = getUnifiedRegNum( RegClassID, CorrectCol);   
546       
547       if( RetValLR->hasColor() ) {
548         
549         unsigned 
550           UniRetLRReg=getUnifiedRegNum(RegClassID,RetValLR->getColor());
551         
552         // the return value is coming in UniRetReg but has to go into
553         // the UniRetLRReg
554
555         AdMI = cpReg2RegMI( UniRetReg, UniRetLRReg, RegType );  
556
557       } // if LR has color
558       else {
559
560         // if the LR did NOT receive a color, we have to move the return
561         // value coming in UniRetReg to the stack pos of spilled LR
562         
563         AdMI =  cpReg2MemMI(UniRetReg, getFramePointer(), 
564                             RetValLR->getSpillOffFromFP(), RegType );
565       }
566
567       CallAI->InstrnsAfter.push_back( AdMI );
568       
569     } // the LR didn't receive the suggested color  
570     
571   } // if there a return value
572   
573
574   // Now color all args of the call instruction
575
576   unsigned NumOfCallArgs =  getCallInstNumArgs( CallMI );
577
578   for(unsigned argNo=0, i=0; i < NumOfCallArgs; ++i, ++argNo ) {
579
580     const Value *CallArg = CallMI->getImplicitRef(i);
581
582     // get the LR of call operand (parameter)
583     LiveRange *const LR = LRI.getLiveRangeForValue(CallArg); 
584
585     unsigned RegType = getRegType( CallArg );
586     unsigned RegClassID =  getRegClassIDOfValue( CallArg);
587     
588     // find whether this argument is coming in a register (if not, on stack)
589
590     bool isArgInReg = false;
591     unsigned UniArgReg = InvalidRegNum;  // reg that LR must be colored with
592
593     if( (RegType== IntRegType && argNo <  NumOfIntArgRegs)) {
594       isArgInReg = true;
595       UniArgReg = getUnifiedRegNum(RegClassID, SparcIntRegOrder::o0 + argNo );
596     }
597     else if(RegType == FPSingleRegType && argNo < NumOfFloatArgRegs)  { 
598       isArgInReg = true;
599       UniArgReg = getUnifiedRegNum(RegClassID, 
600                                    SparcFloatRegOrder::f0 + (argNo*2 + 1) );
601     }
602     else if(RegType == FPDoubleRegType && argNo < NumOfFloatArgRegs)  { 
603       isArgInReg = true;
604       UniArgReg = getUnifiedRegNum(RegClassID, SparcFloatRegOrder::f0+argNo*2);
605     }
606
607
608     // not possible to have a null LR since all args (even consts)  
609     // must be defined before
610     if( !LR ) {          
611       if( DEBUG_RA) {
612         cerr << " ERROR: In call instr, no LR for arg:  " ;
613         printValue(CallArg); cerr << endl;
614       }
615       assert(0 && "NO LR for call arg");  
616       // continue;
617     }
618
619
620     // if the LR received the suggested color, NOTHING to do
621
622
623     if( LR->hasColor() ) {
624
625
626       unsigned UniLRReg = getUnifiedRegNum( RegClassID,  LR->getColor() );
627
628       // if LR received the correct color, nothing to do
629       if( UniLRReg == UniArgReg )
630         continue;
631
632       // We are here because though the LR is allocated a register, it
633       // was not allocated the suggested register. So, we have to copy %ix reg 
634       // (or stack pos of arg) to the register it was colored with
635
636       // the LR is colored with UniLRReg but has to go into  UniArgReg
637       // to pass it as an argument
638
639       if( isArgInReg ) 
640         AdMI = cpReg2RegMI(UniLRReg, UniArgReg, RegType );
641
642       else {
643         // Now, we have to pass the arg on stack. Since LR received a register
644         // we just have to move that register to the stack position where
645         // the argument must be passed
646
647         int ArgStakOffFromSP = 
648           UltraSparcFrameInfo::FirstOutgoingArgOffsetFromSP + 
649           argNo * SizeOfOperandOnStack;
650
651         AdMI = cpReg2MemMI(UniLRReg, getStackPointer(), ArgStakOffFromSP, 
652                            RegType );
653       }
654
655       CallAI->InstrnsBefore.push_back( AdMI );  // Now add the instruction
656     }
657
658     else {                          // LR is not colored (i.e., spilled)      
659       
660       if( isArgInReg ) {
661
662         // Now the LR did NOT recieve a register but has a stack poistion.
663         // Since, the outgoing arg goes in a register we just have to insert
664         // a load instruction to load the LR to outgoing register
665
666
667         AdMI = cpMem2RegMI(getStackPointer(), LR->getSpillOffFromFP(),
668                            UniArgReg, RegType );
669
670         CallAI->InstrnsBefore.push_back( AdMI );  // Now add the instruction
671       }
672
673       else {
674         // Now, we have to pass the arg on stack. Since LR  also did NOT
675         // receive a register we have to move an argument in memory to 
676         // outgoing parameter on stack.
677         
678         // Optoimize: Optimize when reverse pointers in MahineInstr are
679         // introduced. 
680         // call PRA.getUnusedRegAtMI(....) to get an unused reg. Only if this
681         // fails, then use the following code. Currently, we cannot call the
682         // above method since we cannot find LVSetBefore without the BB 
683         
684         int TReg = PRA.getRegNotUsedByThisInst( LR->getRegClass(), CallMI );
685         int TmpOff = PRA.getStackOffsets().getNewTmpPosOffFromFP();
686         int ArgStakOffFromSP = 
687           UltraSparcFrameInfo::FirstOutgoingArgOffsetFromSP + 
688           argNo * SizeOfOperandOnStack;
689
690         MachineInstr *Ad1, *Ad2, *Ad3, *Ad4;
691
692         // Sequence:
693         // (1) Save TReg on stack    
694         // (2) Load LR value into TReg from stack pos of LR
695         // (3) Store Treg on outgoing Arg pos on stack
696         // (4) Load the old value of TReg from stack to TReg (restore it)
697
698         Ad1 = cpReg2MemMI(TReg, getFramePointer(), TmpOff, RegType );
699         Ad2 = cpMem2RegMI(getFramePointer(), LR->getSpillOffFromFP(), 
700                           TReg, RegType ); 
701         Ad3 = cpReg2MemMI(TReg, getStackPointer(), ArgStakOffFromSP, RegType );
702         Ad4 = cpMem2RegMI(getFramePointer(), TmpOff, TReg, RegType ); 
703
704         CallAI->InstrnsBefore.push_back( Ad1 );  
705         CallAI->InstrnsBefore.push_back( Ad2 );  
706         CallAI->InstrnsBefore.push_back( Ad3 );  
707         CallAI->InstrnsBefore.push_back( Ad4 );  
708       }
709
710     }
711
712   }  // for each parameter in call instruction
713
714 }
715
716 //---------------------------------------------------------------------------
717 // This method is called for an LLVM return instruction to identify which
718 // values will be returned from this method and to suggest colors.
719 //---------------------------------------------------------------------------
720 void UltraSparcRegInfo::suggestReg4RetValue(const MachineInstr *const RetMI, 
721                                              LiveRangeInfo& LRI) const {
722
723   assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) );
724
725     suggestReg4RetAddr(RetMI, LRI);
726
727   // if there is an implicit ref, that has to be the ret value
728   if(  RetMI->getNumImplicitRefs() > 0 ) {
729
730     // The first implicit operand is the return value of a return instr
731     const Value *RetVal =  RetMI->getImplicitRef(0);
732
733     MachineInstr *AdMI;
734     LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); 
735
736     if( !LR ) {
737      cerr << "\nNo LR for:";
738      printValue( RetVal );
739      cerr << endl;
740      assert( LR && "No LR for return value of non-void method");
741      //return;
742    }
743
744     unsigned RegClassID = (LR->getRegClass())->getID();
745       
746     if( RegClassID == IntRegClassID ) 
747       LR->setSuggestedColor(SparcIntRegOrder::i0);
748     
749     else if ( RegClassID == FloatRegClassID ) 
750       LR->setSuggestedColor(SparcFloatRegOrder::f0);
751       
752   }
753
754 }
755
756
757
758 //---------------------------------------------------------------------------
759 // Colors the return value of a method to %i0 or %f0, if possible. If it is
760 // not possilbe to directly color the LR, insert a copy instruction to move
761 // the LR to %i0 or %f0. When the LR is spilled, instead of the copy, we 
762 // have to put a load instruction.
763 //---------------------------------------------------------------------------
764 void UltraSparcRegInfo::colorRetValue(const  MachineInstr *const RetMI, 
765                                       LiveRangeInfo& LRI,
766                                       AddedInstrns *const RetAI) const {
767
768   assert( (UltraSparcInfo->getInstrInfo()).isReturn( RetMI->getOpCode() ) );
769
770   // if there is an implicit ref, that has to be the ret value
771   if(  RetMI->getNumImplicitRefs() > 0 ) {
772
773     // The first implicit operand is the return value of a return instr
774     const Value *RetVal =  RetMI->getImplicitRef(0);
775
776     MachineInstr *AdMI;
777     LiveRange *const LR = LRI.getLiveRangeForValue( RetVal ); 
778
779     if( ! LR ) {
780         cerr << "\nNo LR for:";
781         printValue( RetVal );
782         cerr << endl;
783         // assert( LR && "No LR for return value of non-void method");
784         return;
785     }
786
787     unsigned RegClassID =  getRegClassIDOfValue(RetVal);
788     unsigned RegType = getRegType( RetVal );
789
790
791     unsigned CorrectCol;
792     if(RegClassID == IntRegClassID)
793       CorrectCol = SparcIntRegOrder::i0;
794     else if(RegClassID == FloatRegClassID)
795       CorrectCol = SparcFloatRegOrder::f0;
796     else 
797       assert( 0 && "Unknown RegClass");
798
799
800     // if the LR received the correct color, NOTHING to do
801
802     if(  LR->hasColor() )
803       if( LR->getColor() == CorrectCol )
804         return;
805
806     unsigned UniRetReg =  getUnifiedRegNum( RegClassID, CorrectCol );
807
808     if( LR->hasColor() ) {
809
810       // We are here because the LR was allocted a regiter
811       // It may be the suggested register or not
812
813       // copy the LR of retun value to i0 or f0
814
815       unsigned UniLRReg =getUnifiedRegNum( RegClassID, LR->getColor());
816
817       // the LR received  UniLRReg but must be colored with UniRetReg
818       // to pass as the return value
819
820       AdMI = cpReg2RegMI( UniLRReg, UniRetReg, RegType); 
821       RetAI->InstrnsBefore.push_back( AdMI );
822     }
823     else {                              // if the LR is spilled
824
825       AdMI = cpMem2RegMI(getFramePointer(), LR->getSpillOffFromFP(), 
826                          UniRetReg, RegType); 
827       RetAI->InstrnsBefore.push_back( AdMI );
828       cout << "\nCopied the return value from stack";
829     }
830   
831   } // if there is a return value
832
833 }
834
835
836 //---------------------------------------------------------------------------
837 // Copy from a register to register. Register number must be the unified
838 // register number
839 //---------------------------------------------------------------------------
840
841
842 MachineInstr * UltraSparcRegInfo::cpReg2RegMI(const unsigned SrcReg, 
843                                               const unsigned DestReg,
844                                               const int RegType) const {
845
846   assert( ((int)SrcReg != InvalidRegNum) && ((int)DestReg != InvalidRegNum) &&
847           "Invalid Register");
848   
849   MachineInstr * MI = NULL;
850
851   switch( RegType ) {
852     
853   case IntRegType:
854   case IntCCRegType:
855   case FloatCCRegType: 
856     MI = new MachineInstr(ADD, 3);
857     MI->SetMachineOperand(0, SrcReg, false);
858     MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
859     MI->SetMachineOperand(2, DestReg, true);
860     break;
861
862   case FPSingleRegType:
863     MI = new MachineInstr(FMOVS, 2);
864     MI->SetMachineOperand(0, SrcReg, false);
865     MI->SetMachineOperand(1, DestReg, true);
866     break;
867
868   case FPDoubleRegType:
869     MI = new MachineInstr(FMOVD, 2);
870     MI->SetMachineOperand(0, SrcReg, false);    
871     MI->SetMachineOperand(1, DestReg, true);
872     break;
873
874   default:
875     assert(0 && "Unknow RegType");
876   }
877
878   return MI;
879 }
880
881
882 //---------------------------------------------------------------------------
883 // Copy from a register to memory (i.e., Store). Register number must 
884 // be the unified register number
885 //---------------------------------------------------------------------------
886
887
888 MachineInstr * UltraSparcRegInfo::cpReg2MemMI(const unsigned SrcReg, 
889                                               const unsigned DestPtrReg,
890                                               const int Offset,
891                                               const int RegType) const {
892
893
894   MachineInstr * MI = NULL;
895
896   switch( RegType ) {
897     
898   case IntRegType:
899   case FloatCCRegType: 
900     MI = new MachineInstr(STX, 3);
901     MI->SetMachineOperand(0, SrcReg, false);
902     MI->SetMachineOperand(1, DestPtrReg, false);
903     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
904                           (int64_t) Offset, false);
905     break;
906
907   case FPSingleRegType:
908     MI = new MachineInstr(ST, 3);
909     MI->SetMachineOperand(0, SrcReg, false);
910     MI->SetMachineOperand(1, DestPtrReg, false);
911     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
912                           (int64_t) Offset, false);
913     break;
914
915   case FPDoubleRegType:
916     MI = new MachineInstr(STD, 3);
917     MI->SetMachineOperand(0, SrcReg, false);
918     MI->SetMachineOperand(1, DestPtrReg, false);
919     MI->SetMachineOperand(2, MachineOperand:: MO_SignExtendedImmed, 
920                           (int64_t) Offset, false);
921     break;
922
923   case IntCCRegType:
924     assert( 0 && "Cannot directly store %ccr to memory");
925     
926   default:
927     assert(0 && "Unknow RegType in cpReg2MemMI");
928   }
929
930   return MI;
931 }
932
933
934 //---------------------------------------------------------------------------
935 // Copy from memory to a reg (i.e., Load) Register number must be the unified
936 // register number
937 //---------------------------------------------------------------------------
938
939
940 MachineInstr * UltraSparcRegInfo::cpMem2RegMI(const unsigned SrcPtrReg, 
941                                               const int Offset,
942                                               const unsigned DestReg,
943                                               const int RegType) const {
944   
945   MachineInstr * MI = NULL;
946
947   switch( RegType ) {
948     
949   case IntRegType:
950   case FloatCCRegType: 
951     MI = new MachineInstr(LDX, 3);
952     MI->SetMachineOperand(0, SrcPtrReg, false);
953     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
954                           (int64_t) Offset, false);
955     MI->SetMachineOperand(2, DestReg, false);
956     break;
957
958   case FPSingleRegType:
959     MI = new MachineInstr(LD, 3);
960     MI->SetMachineOperand(0, SrcPtrReg, false);
961     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
962                           (int64_t) Offset, false);
963     MI->SetMachineOperand(2, DestReg, false);
964
965     break;
966
967   case FPDoubleRegType:
968     MI = new MachineInstr(LDD, 3);
969     MI->SetMachineOperand(0, SrcPtrReg, false);
970     MI->SetMachineOperand(1, MachineOperand:: MO_SignExtendedImmed, 
971                           (int64_t) Offset, false);
972     MI->SetMachineOperand(2, DestReg, false);
973     break;
974
975   case IntCCRegType:
976     assert( 0 && "Cannot directly load into %ccr from memory");
977
978   default:
979     assert(0 && "Unknow RegType in cpMem2RegMI");
980   }
981
982   return MI;
983 }
984
985
986
987
988 // Following method is Not needed now
989
990 MachineInstr* UltraSparcRegInfo::cpValue2Value(Value *Src, Value *Dest) const {
991
992   MachineInstr * MI = NULL;
993
994   MI = new MachineInstr(ADD, 3);
995   MI->SetMachineOperand(0, MachineOperand:: MO_VirtualRegister, Src, false);
996   MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
997   MI->SetMachineOperand(2, MachineOperand:: MO_VirtualRegister, Dest, true);
998   
999
1000   return MI;
1001
1002 }
1003
1004
1005
1006 //----------------------------------------------------------------------------
1007 // This method inserts caller saving/restoring instructons before/after
1008 // a call machine instruction.
1009 //----------------------------------------------------------------------------
1010
1011
1012 void UltraSparcRegInfo::insertCallerSavingCode(const MachineInstr *MInst, 
1013                                                const BasicBlock *BB,
1014                                                PhyRegAlloc &PRA) const {
1015   // assert( (getInstrInfo()).isCall( MInst->getOpCode() ) );
1016
1017  
1018   PRA.StackOffsets.resetTmpPos();
1019
1020   hash_set<unsigned> PushedRegSet;
1021
1022   // Now find the LR of the return value of the call
1023   // The last *implicit operand* is the return value of a call
1024   // Insert it to to he PushedRegSet since we must not save that register
1025   // and restore it after the call.
1026   // We do this because, we look at the LV set *after* the instruction
1027   // to determine, which LRs must be saved across calls. The return value
1028   // of the call is live in this set - but we must not save/restore it.
1029
1030
1031   const Value *RetVal = getCallInstRetVal( MInst );
1032
1033   if( RetVal ) {
1034
1035     LiveRange *RetValLR = PRA.LRI.getLiveRangeForValue( RetVal );
1036     assert( RetValLR && "No LR for RetValue of call");
1037
1038     PushedRegSet.insert(
1039                         getUnifiedRegNum((RetValLR->getRegClass())->getID(), 
1040                                       RetValLR->getColor() ) );
1041   }
1042
1043
1044   const LiveVarSet *LVSetAft =  PRA.LVI->getLiveVarSetAfterMInst(MInst, BB);
1045
1046   LiveVarSet::const_iterator LIt = LVSetAft->begin();
1047
1048   // for each live var in live variable set after machine inst
1049   for( ; LIt != LVSetAft->end(); ++LIt) {
1050
1051    //  get the live range corresponding to live var
1052     LiveRange *const LR = PRA.LRI.getLiveRangeForValue(*LIt );    
1053
1054     // LR can be null if it is a const since a const 
1055     // doesn't have a dominating def - see Assumptions above
1056     if( LR )   {  
1057       
1058       if( LR->hasColor() ) {
1059
1060         unsigned RCID = (LR->getRegClass())->getID();
1061         unsigned Color = LR->getColor();
1062
1063         if ( isRegVolatile(RCID, Color) ) {
1064
1065           // if the value is in both LV sets (i.e., live before and after 
1066           // the call machine instruction)
1067
1068           unsigned Reg = getUnifiedRegNum(RCID, Color);
1069           
1070           if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
1071             
1072             // if we haven't already pushed that register
1073
1074             unsigned RegType = getRegType( LR );
1075
1076             // Now get two instructions - to push on stack and pop from stack
1077             // and add them to InstrnsBefore and InstrnsAfter of the
1078             // call instruction
1079
1080             int StackOff =  PRA.StackOffsets. getNewTmpPosOffFromFP();
1081
1082
1083             MachineInstr *AdIBefCC, *AdIAftCC, *AdICpCC;
1084
1085
1086             //---- Insert code for pushing the reg on stack ----------
1087                   
1088             if( RegType == IntCCRegType ) {
1089
1090               // Handle IntCCRegType specially since we cannot directly 
1091               // push %ccr on to the stack
1092
1093               const LiveVarSet *LVSetBef = 
1094                 PRA.LVI->getLiveVarSetBeforeMInst(MInst, BB);
1095
1096               // get a free INTEGER register
1097               int FreeIntReg = 
1098                 PRA.getUsableRegAtMI(LR->getRegClass(), IntRegType, MInst, 
1099                                      LVSetBef, AdIBefCC, AdIAftCC);
1100
1101               // insert the instructions in reverse order since we are
1102               // adding them to the front of InstrnsBefore
1103
1104               if(AdIAftCC)
1105                 (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdIAftCC);
1106
1107               AdICpCC = cpCCR2IntMI(FreeIntReg);
1108               (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdICpCC);
1109
1110               if(AdIBefCC)
1111                 (PRA.AddedInstrMap[MInst]->InstrnsBefore).push_front(AdIBefCC);
1112
1113               cerr << "\n!! Inserted caller saving (push) inst for %ccr:";
1114               if(AdIBefCC) cerr << "\t" <<  *(AdIBefCC);
1115               cerr  << "\t" << *AdICpCC;
1116               if(AdIAftCC) cerr  << "\t" << *(AdIAftCC);
1117
1118             } else  {  
1119               // for any other register type, just add the push inst
1120               MachineInstr *AdIBef = 
1121                 cpReg2MemMI(Reg, getStackPointer(), StackOff, RegType ); 
1122               ((PRA.AddedInstrMap[MInst])->InstrnsBefore).push_front(AdIBef);
1123             }
1124
1125
1126             //---- Insert code for popping the reg from the stack ----------
1127
1128             if( RegType == IntCCRegType ) {
1129
1130               // Handle IntCCRegType specially since we cannot directly 
1131               // pop %ccr on from the stack
1132               
1133               // get a free INT register
1134               int FreeIntReg = 
1135                 PRA.getUsableRegAtMI(LR->getRegClass(), IntRegType, MInst, 
1136                                      LVSetAft, AdIBefCC, AdIAftCC);
1137               
1138               if(AdIBefCC)
1139                 (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdIBefCC);
1140
1141               AdICpCC = cpInt2CCRMI(FreeIntReg);
1142               (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdICpCC);
1143             
1144               if(AdIAftCC)
1145                 (PRA.AddedInstrMap[MInst]->InstrnsAfter).push_back(AdIAftCC);
1146
1147               cerr << "\n!! Inserted caller saving (pop) inst for %ccr:";
1148               if(AdIBefCC) cerr << "\t" <<  *(AdIBefCC);
1149               cerr  << "\t" << *AdICpCC;
1150               if(AdIAftCC) cerr  << "\t" << *(AdIAftCC);
1151
1152             } else {
1153               // for any other register type, just add the pop inst
1154               MachineInstr *AdIAft = 
1155                 cpMem2RegMI(getStackPointer(), StackOff, Reg, RegType ); 
1156
1157               ((PRA.AddedInstrMap[MInst])->InstrnsAfter).push_back(AdIAft);
1158
1159             }
1160             
1161             PushedRegSet.insert( Reg );
1162
1163             if(1) {
1164               cerr << "\nFor call inst:" << *MInst;
1165               cerr << "\n  -inserted caller saving instrs:\n\t ";
1166               cerr << *AdIBefCC << "\n\t" << *AdIAftCC  ;
1167             }       
1168           } // if not already pushed
1169
1170         } // if LR has a volatile color
1171         
1172       } // if LR has color
1173
1174     } // if there is a LR for Var
1175     
1176   } // for each value in the LV set after instruction
1177   
1178 }
1179
1180 //---------------------------------------------------------------------------
1181 // Copies %ccr into an integer register. IntReg is the UNIFIED register
1182 // number.
1183 //---------------------------------------------------------------------------
1184
1185 MachineInstr * UltraSparcRegInfo::cpCCR2IntMI(const unsigned IntReg) const {
1186   MachineInstr * MI = NULL;
1187
1188   MI = new MachineInstr(RDCCR, 2);
1189   MI->SetMachineOperand(0, SparcIntCCRegOrder::ccr, false);
1190   MI->SetMachineOperand(1, IntReg, true);
1191
1192   return MI;
1193 }
1194
1195 //---------------------------------------------------------------------------
1196 // Copies an integer register into  %ccr. IntReg is the UNIFIED register
1197 // number.
1198 //---------------------------------------------------------------------------
1199
1200 MachineInstr * UltraSparcRegInfo::cpInt2CCRMI(const unsigned IntReg) const {
1201   MachineInstr * MI = NULL;
1202
1203   MI = new MachineInstr(WRCCR, 3);
1204   MI->SetMachineOperand(0, IntReg, false);
1205   MI->SetMachineOperand(1, SparcIntRegOrder::g0, false);
1206   MI->SetMachineOperand(2, SparcIntCCRegOrder::ccr, true);
1207
1208   return MI;
1209 }
1210
1211
1212
1213
1214 //---------------------------------------------------------------------------
1215 // Print the register assigned to a LR
1216 //---------------------------------------------------------------------------
1217
1218 void UltraSparcRegInfo::printReg(const LiveRange *const LR) {
1219
1220   unsigned RegClassID = (LR->getRegClass())->getID();
1221
1222   cerr << " *Node " << (LR->getUserIGNode())->getIndex();
1223
1224   if( ! LR->hasColor() ) {
1225     cerr << " - could not find a color" << endl;
1226     return;
1227   }
1228   
1229   // if a color is found
1230
1231   cerr << " colored with color "<< LR->getColor();
1232
1233   if( RegClassID == IntRegClassID ) {
1234
1235     cerr<< " [" << SparcIntRegOrder::getRegName(LR->getColor()) ;
1236     cerr << "]" << endl;
1237   }
1238   else if ( RegClassID == FloatRegClassID) {
1239     cerr << "[" << SparcFloatRegOrder::getRegName(LR->getColor());
1240     if( LR->getTypeID() == Type::DoubleTyID )
1241       cerr << "+" << SparcFloatRegOrder::getRegName(LR->getColor()+1);
1242     cerr << "]" << endl;
1243   }
1244 }