blob: 859159856974992f60154e178d621286c24ae73a [file] [log] [blame]
Chris Lattner029af0b2002-02-03 07:52:04 +00001//===-- SparcRegInfo.cpp - Sparc Target Register Information --------------===//
2//
3// This file contains implementation of Sparc specific helper methods
4// used for register allocation.
5//
6//===----------------------------------------------------------------------===//
7
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00008#include "SparcInternals.h"
Chris Lattner5216cc52002-02-04 05:59:25 +00009#include "SparcRegClassInfo.h"
Misha Brukman7ae7f842002-10-28 00:28:31 +000010#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd47aac92002-12-28 20:21:29 +000011#include "llvm/CodeGen/MachineFunctionInfo.h"
Ruchira Sasankadfc6c882001-09-18 22:52:44 +000012#include "llvm/CodeGen/PhyRegAlloc.h"
Vikram S. Advee9327f02002-05-19 15:25:51 +000013#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner1ebaa902003-01-15 17:47:49 +000014#include "llvm/CodeGen/MachineInstrBuilder.h"
Anand Shuklae6c3ee62003-06-01 02:48:23 +000015#include "llvm/CodeGen/MachineCodeForInstruction.h"
Vikram S. Advee9327f02002-05-19 15:25:51 +000016#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner24c1d5e2003-01-14 23:05:08 +000017#include "llvm/CodeGen/FunctionLiveVarInfo.h" // FIXME: Remove
Chris Lattner90fc6652003-01-15 19:50:44 +000018#include "../../CodeGen/RegAlloc/RegAllocCommon.h" // FIXME!
Chris Lattner5216cc52002-02-04 05:59:25 +000019#include "llvm/iTerminators.h"
20#include "llvm/iOther.h"
Chris Lattner06be1802002-04-09 19:08:28 +000021#include "llvm/Function.h"
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000023
Chris Lattner24c1d5e2003-01-14 23:05:08 +000024enum {
25 BadRegClass = ~0
26};
27
Chris Lattner5216cc52002-02-04 05:59:25 +000028UltraSparcRegInfo::UltraSparcRegInfo(const UltraSparc &tgt)
Vikram S. Advea83804a2003-05-31 07:32:01 +000029 : TargetRegInfo(tgt), NumOfIntArgRegs(6), NumOfFloatArgRegs(32)
30{
Chris Lattner5216cc52002-02-04 05:59:25 +000031 MachineRegClassArr.push_back(new SparcIntRegClass(IntRegClassID));
32 MachineRegClassArr.push_back(new SparcFloatRegClass(FloatRegClassID));
33 MachineRegClassArr.push_back(new SparcIntCCRegClass(IntCCRegClassID));
34 MachineRegClassArr.push_back(new SparcFloatCCRegClass(FloatCCRegClassID));
Vikram S. Adve8adb9942003-05-27 00:02:22 +000035 MachineRegClassArr.push_back(new SparcSpecialRegClass(SpecialRegClassID));
Vikram S. Adveaee67012002-07-08 23:23:12 +000036
Chris Lattner56e91662002-08-12 21:25:05 +000037 assert(SparcFloatRegClass::StartOfNonVolatileRegs == 32 &&
Chris Lattner5216cc52002-02-04 05:59:25 +000038 "32 Float regs are used for float arg passing");
39}
40
41
Vikram S. Advedb1435f2002-03-18 03:12:16 +000042// getZeroRegNum - returns the register that contains always zero.
43// this is the unified register number
Chris Lattner5216cc52002-02-04 05:59:25 +000044//
Vikram S. Advedb1435f2002-03-18 03:12:16 +000045int UltraSparcRegInfo::getZeroRegNum() const {
Chris Lattner56e91662002-08-12 21:25:05 +000046 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
47 SparcIntRegClass::g0);
Vikram S. Advedb1435f2002-03-18 03:12:16 +000048}
Chris Lattner5216cc52002-02-04 05:59:25 +000049
50// getCallAddressReg - returns the reg used for pushing the address when a
51// method is called. This can be used for other purposes between calls
52//
53unsigned UltraSparcRegInfo::getCallAddressReg() const {
Chris Lattner56e91662002-08-12 21:25:05 +000054 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
55 SparcIntRegClass::o7);
Chris Lattner5216cc52002-02-04 05:59:25 +000056}
57
58// Returns the register containing the return address.
59// It should be made sure that this register contains the return
60// value when a return instruction is reached.
61//
62unsigned UltraSparcRegInfo::getReturnAddressReg() const {
Chris Lattner56e91662002-08-12 21:25:05 +000063 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
64 SparcIntRegClass::i7);
65}
66
67// Register get name implementations...
68
69// Int register names in same order as enum in class SparcIntRegClass
70static const char * const IntRegNames[] = {
71 "o0", "o1", "o2", "o3", "o4", "o5", "o7",
72 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
73 "i0", "i1", "i2", "i3", "i4", "i5",
74 "i6", "i7",
75 "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
76 "o6"
77};
78
Vikram S. Adve8adb9942003-05-27 00:02:22 +000079const char * const SparcIntRegClass::getRegName(unsigned reg) const {
Chris Lattner56e91662002-08-12 21:25:05 +000080 assert(reg < NumOfAllRegs);
81 return IntRegNames[reg];
82}
83
84static const char * const FloatRegNames[] = {
85 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
86 "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19",
87 "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29",
88 "f30", "f31", "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",
89 "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47", "f48", "f49",
90 "f50", "f51", "f52", "f53", "f54", "f55", "f56", "f57", "f58", "f59",
91 "f60", "f61", "f62", "f63"
92};
93
Vikram S. Adve8adb9942003-05-27 00:02:22 +000094const char * const SparcFloatRegClass::getRegName(unsigned reg) const {
Chris Lattner56e91662002-08-12 21:25:05 +000095 assert (reg < NumOfAllRegs);
96 return FloatRegNames[reg];
97}
98
99
100static const char * const IntCCRegNames[] = {
Vikram S. Adved09c4c32003-07-06 20:13:59 +0000101 "xcc", "icc", "ccr"
Chris Lattner56e91662002-08-12 21:25:05 +0000102};
103
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000104const char * const SparcIntCCRegClass::getRegName(unsigned reg) const {
Vikram S. Adved09c4c32003-07-06 20:13:59 +0000105 assert(reg < 3);
Chris Lattner56e91662002-08-12 21:25:05 +0000106 return IntCCRegNames[reg];
107}
108
109static const char * const FloatCCRegNames[] = {
110 "fcc0", "fcc1", "fcc2", "fcc3"
111};
112
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000113const char * const SparcFloatCCRegClass::getRegName(unsigned reg) const {
114 assert (reg < 5);
Chris Lattner56e91662002-08-12 21:25:05 +0000115 return FloatCCRegNames[reg];
Chris Lattner5216cc52002-02-04 05:59:25 +0000116}
117
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000118static const char * const SpecialRegNames[] = {
119 "fsr"
120};
121
122const char * const SparcSpecialRegClass::getRegName(unsigned reg) const {
123 assert (reg < 1);
124 return SpecialRegNames[reg];
Chris Lattner5216cc52002-02-04 05:59:25 +0000125}
126
Vikram S. Advedb1435f2002-03-18 03:12:16 +0000127// Get unified reg number for frame pointer
Chris Lattner5216cc52002-02-04 05:59:25 +0000128unsigned UltraSparcRegInfo::getFramePointer() const {
Chris Lattner56e91662002-08-12 21:25:05 +0000129 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
130 SparcIntRegClass::i6);
Chris Lattner5216cc52002-02-04 05:59:25 +0000131}
132
Vikram S. Advedb1435f2002-03-18 03:12:16 +0000133// Get unified reg number for stack pointer
Chris Lattner5216cc52002-02-04 05:59:25 +0000134unsigned UltraSparcRegInfo::getStackPointer() const {
Chris Lattner56e91662002-08-12 21:25:05 +0000135 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
136 SparcIntRegClass::o6);
Chris Lattner5216cc52002-02-04 05:59:25 +0000137}
138
139
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000140//---------------------------------------------------------------------------
141// Finds whether a call is an indirect call
142//---------------------------------------------------------------------------
143
144inline bool
145isVarArgsFunction(const Type *funcType) {
146 return cast<FunctionType>(cast<PointerType>(funcType)
147 ->getElementType())->isVarArg();
148}
149
150inline bool
151isVarArgsCall(const MachineInstr *CallMI) {
152 Value* callee = CallMI->getOperand(0).getVRegValue();
153 // const Type* funcType = isa<Function>(callee)? callee->getType()
154 // : cast<PointerType>(callee->getType())->getElementType();
155 const Type* funcType = callee->getType();
156 return isVarArgsFunction(funcType);
157}
158
159
Vikram S. Advea83804a2003-05-31 07:32:01 +0000160// Get the register number for the specified argument #argNo,
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000161//
162// Return value:
Vikram S. Advea83804a2003-05-31 07:32:01 +0000163// getInvalidRegNum(), if there is no int register available for the arg.
164// regNum, otherwise (this is NOT the unified reg. num).
165// regClassId is set to the register class ID.
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000166//
Vikram S. Advea83804a2003-05-31 07:32:01 +0000167int
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000168UltraSparcRegInfo::regNumForIntArg(bool inCallee, bool isVarArgsCall,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000169 unsigned argNo, unsigned& regClassId) const
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000170{
Vikram S. Advee9327f02002-05-19 15:25:51 +0000171 regClassId = IntRegClassID;
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000172 if (argNo >= NumOfIntArgRegs)
Vikram S. Advea83804a2003-05-31 07:32:01 +0000173 return getInvalidRegNum();
Vikram S. Advee9327f02002-05-19 15:25:51 +0000174 else
Chris Lattner56e91662002-08-12 21:25:05 +0000175 return argNo + (inCallee? SparcIntRegClass::i0 : SparcIntRegClass::o0);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000176}
177
Vikram S. Advea83804a2003-05-31 07:32:01 +0000178// Get the register number for the specified FP argument #argNo,
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000179// Use INT regs for FP args if this is a varargs call.
180//
181// Return value:
Vikram S. Advea83804a2003-05-31 07:32:01 +0000182// getInvalidRegNum(), if there is no int register available for the arg.
183// regNum, otherwise (this is NOT the unified reg. num).
184// regClassId is set to the register class ID.
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000185//
Vikram S. Advea83804a2003-05-31 07:32:01 +0000186int
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000187UltraSparcRegInfo::regNumForFPArg(unsigned regType,
188 bool inCallee, bool isVarArgsCall,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000189 unsigned argNo, unsigned& regClassId) const
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000190{
Vikram S. Advee9327f02002-05-19 15:25:51 +0000191 if (isVarArgsCall)
Vikram S. Advea83804a2003-05-31 07:32:01 +0000192 return regNumForIntArg(inCallee, isVarArgsCall, argNo, regClassId);
Vikram S. Advee9327f02002-05-19 15:25:51 +0000193 else
194 {
195 regClassId = FloatRegClassID;
196 if (regType == FPSingleRegType)
197 return (argNo*2+1 >= NumOfFloatArgRegs)?
Vikram S. Advea83804a2003-05-31 07:32:01 +0000198 getInvalidRegNum() : SparcFloatRegClass::f0 + (argNo * 2 + 1);
Vikram S. Advee9327f02002-05-19 15:25:51 +0000199 else if (regType == FPDoubleRegType)
200 return (argNo*2 >= NumOfFloatArgRegs)?
Vikram S. Advea83804a2003-05-31 07:32:01 +0000201 getInvalidRegNum() : SparcFloatRegClass::f0 + (argNo * 2);
Vikram S. Advee9327f02002-05-19 15:25:51 +0000202 else
203 assert(0 && "Illegal FP register type");
Chris Lattner3091e112002-07-25 06:08:32 +0000204 return 0;
Vikram S. Advee9327f02002-05-19 15:25:51 +0000205 }
Vikram S. Adve02662bd2002-03-31 19:04:50 +0000206}
207
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000208
209//---------------------------------------------------------------------------
210// Finds the return address of a call sparc specific call instruction
211//---------------------------------------------------------------------------
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000212
Vikram S. Adveaee67012002-07-08 23:23:12 +0000213// The following 4 methods are used to find the RegType (SparcInternals.h)
Vikram S. Advee9327f02002-05-19 15:25:51 +0000214// of a LiveRange, a Value, and for a given register unified reg number.
Chris Lattner5216cc52002-02-04 05:59:25 +0000215//
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000216int UltraSparcRegInfo::getRegTypeForClassAndType(unsigned regClassID,
217 const Type* type) const
218{
Vikram S. Advee9327f02002-05-19 15:25:51 +0000219 switch (regClassID) {
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000220 case IntRegClassID: return IntRegType;
221 case FloatRegClassID:
222 if (type == Type::FloatTy) return FPSingleRegType;
223 else if (type == Type::DoubleTy) return FPDoubleRegType;
224 assert(0 && "Unknown type in FloatRegClass"); return 0;
225 case IntCCRegClassID: return IntCCRegType;
226 case FloatCCRegClassID: return FloatCCRegType;
227 case SpecialRegClassID: return SpecialRegType;
Vikram S. Advee9327f02002-05-19 15:25:51 +0000228 default: assert( 0 && "Unknown reg class ID"); return 0;
Chris Lattner5216cc52002-02-04 05:59:25 +0000229 }
230}
231
Vikram S. Adve536b1922003-07-25 21:12:15 +0000232int UltraSparcRegInfo::getRegTypeForDataType(const Type* type) const
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000233{
234 return getRegTypeForClassAndType(getRegClassIDOfType(type), type);
Vikram S. Advee9327f02002-05-19 15:25:51 +0000235}
236
Vikram S. Adve536b1922003-07-25 21:12:15 +0000237int UltraSparcRegInfo::getRegTypeForLR(const LiveRange *LR) const
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000238{
239 return getRegTypeForClassAndType(LR->getRegClassID(), LR->getType());
240}
Chris Lattner5216cc52002-02-04 05:59:25 +0000241
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000242int UltraSparcRegInfo::getRegType(int unifiedRegNum) const
243{
Vikram S. Adveaee67012002-07-08 23:23:12 +0000244 if (unifiedRegNum < 32)
Chris Lattner5216cc52002-02-04 05:59:25 +0000245 return IntRegType;
Vikram S. Adveaee67012002-07-08 23:23:12 +0000246 else if (unifiedRegNum < (32 + 32))
Chris Lattner5216cc52002-02-04 05:59:25 +0000247 return FPSingleRegType;
Vikram S. Adveaee67012002-07-08 23:23:12 +0000248 else if (unifiedRegNum < (64 + 32))
Chris Lattner5216cc52002-02-04 05:59:25 +0000249 return FPDoubleRegType;
Vikram S. Adveaee67012002-07-08 23:23:12 +0000250 else if (unifiedRegNum < (64+32+4))
Chris Lattner5216cc52002-02-04 05:59:25 +0000251 return FloatCCRegType;
Vikram S. Adveaee67012002-07-08 23:23:12 +0000252 else if (unifiedRegNum < (64+32+4+2))
Chris Lattner5216cc52002-02-04 05:59:25 +0000253 return IntCCRegType;
254 else
Vikram S. Adveaee67012002-07-08 23:23:12 +0000255 assert(0 && "Invalid unified register number in getRegType");
Chris Lattner5536c9c2002-02-24 23:02:40 +0000256 return 0;
Chris Lattner5216cc52002-02-04 05:59:25 +0000257}
258
259
Vikram S. Adveaee67012002-07-08 23:23:12 +0000260// To find the register class used for a specified Type
261//
262unsigned UltraSparcRegInfo::getRegClassIDOfType(const Type *type,
Chris Lattner3091e112002-07-25 06:08:32 +0000263 bool isCCReg) const {
Vikram S. Adveaee67012002-07-08 23:23:12 +0000264 Type::PrimitiveID ty = type->getPrimitiveID();
265 unsigned res;
266
267 // FIXME: Comparing types like this isn't very safe...
268 if ((ty && ty <= Type::LongTyID) || (ty == Type::LabelTyID) ||
269 (ty == Type::FunctionTyID) || (ty == Type::PointerTyID) )
270 res = IntRegClassID; // sparc int reg (ty=0: void)
271 else if (ty <= Type::DoubleTyID)
272 res = FloatRegClassID; // sparc float reg class
273 else {
274 //std::cerr << "TypeID: " << ty << "\n";
275 assert(0 && "Cannot resolve register class for type");
276 return 0;
277 }
278
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000279 if (isCCReg)
280 return res + 2; // corresponding condition code register
Vikram S. Adveaee67012002-07-08 23:23:12 +0000281 else
282 return res;
283}
284
Vikram S. Adveaee67012002-07-08 23:23:12 +0000285unsigned UltraSparcRegInfo::getRegClassIDOfRegType(int regType) const {
286 switch(regType) {
287 case IntRegType: return IntRegClassID;
288 case FPSingleRegType:
289 case FPDoubleRegType: return FloatRegClassID;
290 case IntCCRegType: return IntCCRegClassID;
291 case FloatCCRegType: return FloatCCRegClassID;
292 default:
293 assert(0 && "Invalid register type in getRegClassIDOfRegType");
294 return 0;
295 }
296}
297
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000298//---------------------------------------------------------------------------
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000299// Suggests a register for the ret address in the RET machine instruction.
300// We always suggest %i7 by convention.
301//---------------------------------------------------------------------------
Vikram S. Adveaee67012002-07-08 23:23:12 +0000302void UltraSparcRegInfo::suggestReg4RetAddr(MachineInstr *RetMI,
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000303 LiveRangeInfo& LRI) const {
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000304
Vikram S. Adveaee67012002-07-08 23:23:12 +0000305 assert(target.getInstrInfo().isReturn(RetMI->getOpCode()));
Vikram S. Adve84982772001-10-22 13:41:12 +0000306
Vikram S. Adveaee67012002-07-08 23:23:12 +0000307 // return address is always mapped to i7 so set it immediately
308 RetMI->SetRegForOperand(0, getUnifiedRegNum(IntRegClassID,
Chris Lattner56e91662002-08-12 21:25:05 +0000309 SparcIntRegClass::i7));
Vikram S. Adve84982772001-10-22 13:41:12 +0000310
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000311 // Possible Optimization:
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000312 // Instead of setting the color, we can suggest one. In that case,
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000313 // we have to test later whether it received the suggested color.
314 // In that case, a LR has to be created at the start of method.
315 // It has to be done as follows (remove the setRegVal above):
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000316
Vikram S. Adveaee67012002-07-08 23:23:12 +0000317 // MachineOperand & MO = RetMI->getOperand(0);
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000318 // const Value *RetAddrVal = MO.getVRegValue();
319 // assert( RetAddrVal && "LR for ret address must be created at start");
320 // LiveRange * RetAddrLR = LRI.getLiveRangeForValue( RetAddrVal);
321 // RetAddrLR->setSuggestedColor(getUnifiedRegNum( IntRegClassID,
Vikram S. Adveaee67012002-07-08 23:23:12 +0000322 // SparcIntRegOrdr::i7) );
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000323}
324
325
326//---------------------------------------------------------------------------
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000327// Suggests a register for the ret address in the JMPL/CALL machine instr.
328// Sparc ABI dictates that %o7 be used for this purpose.
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000329//---------------------------------------------------------------------------
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000330void
331UltraSparcRegInfo::suggestReg4CallAddr(MachineInstr * CallMI,
332 LiveRangeInfo& LRI) const
333{
Vikram S. Advee9327f02002-05-19 15:25:51 +0000334 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
335 const Value *RetAddrVal = argDesc->getReturnAddrReg();
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000336 assert(RetAddrVal && "INTERNAL ERROR: Return address value is required");
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000337
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000338 // A LR must already exist for the return address.
339 LiveRange *RetAddrLR = LRI.getLiveRangeForValue(RetAddrVal);
340 assert(RetAddrLR && "INTERNAL ERROR: No LR for return address of call!");
341
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000342 unsigned RegClassID = RetAddrLR->getRegClassID();
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000343 RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID, SparcIntRegClass::o7));
344}
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000345
346
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000347
348//---------------------------------------------------------------------------
349// This method will suggest colors to incoming args to a method.
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000350// According to the Sparc ABI, the first 6 incoming args are in
351// %i0 - %i5 (if they are integer) OR in %f0 - %f31 (if they are float).
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000352// If the arg is passed on stack due to the lack of regs, NOTHING will be
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000353// done - it will be colored (or spilled) as a normal live range.
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000354//---------------------------------------------------------------------------
Chris Lattnerf739fa82002-04-08 22:03:57 +0000355void UltraSparcRegInfo::suggestRegs4MethodArgs(const Function *Meth,
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000356 LiveRangeInfo& LRI) const
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000357{
Vikram S. Adve536b1922003-07-25 21:12:15 +0000358 // Check if this is a varArgs function. needed for choosing regs.
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000359 bool isVarArgs = isVarArgsFunction(Meth->getType());
360
Vikram S. Adve536b1922003-07-25 21:12:15 +0000361 // Count the arguments, *ignoring* whether they are int or FP args.
362 // Use this common arg numbering to pick the right int or fp register.
363 unsigned argNo=0;
Chris Lattner7076ff22002-06-25 16:13:21 +0000364 for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend();
365 I != E; ++I, ++argNo) {
Chris Lattner7076ff22002-06-25 16:13:21 +0000366 LiveRange *LR = LRI.getLiveRangeForValue(I);
367 assert(LR && "No live range found for method arg");
368
Vikram S. Adve536b1922003-07-25 21:12:15 +0000369 unsigned regType = getRegTypeForLR(LR);
370 unsigned regClassIDOfArgReg = BadRegClass; // for chosen reg (unused)
Chris Lattner7076ff22002-06-25 16:13:21 +0000371
372 int regNum = (regType == IntRegType)
Vikram S. Adve536b1922003-07-25 21:12:15 +0000373 ? regNumForIntArg(/*inCallee*/ true, isVarArgs, argNo, regClassIDOfArgReg)
374 : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs, argNo,
375 regClassIDOfArgReg);
Chris Lattner7076ff22002-06-25 16:13:21 +0000376
Vikram S. Adve536b1922003-07-25 21:12:15 +0000377 if (regNum != getInvalidRegNum())
Chris Lattner7076ff22002-06-25 16:13:21 +0000378 LR->setSuggestedColor(regNum);
379 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000380}
381
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000382
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000383//---------------------------------------------------------------------------
384// This method is called after graph coloring to move incoming args to
385// the correct hardware registers if they did not receive the correct
386// (suggested) color through graph coloring.
387//---------------------------------------------------------------------------
Chris Lattnerf739fa82002-04-08 22:03:57 +0000388void UltraSparcRegInfo::colorMethodArgs(const Function *Meth,
Chris Lattner5216cc52002-02-04 05:59:25 +0000389 LiveRangeInfo &LRI,
390 AddedInstrns *FirstAI) const {
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000391
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000392 // check if this is a varArgs function. needed for choosing regs.
393 bool isVarArgs = isVarArgsFunction(Meth->getType());
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000394 MachineInstr *AdMI;
395
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000396 // for each argument
Chris Lattner7076ff22002-06-25 16:13:21 +0000397 // for each argument. count INT and FP arguments separately.
398 unsigned argNo=0, intArgNo=0, fpArgNo=0;
399 for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend();
400 I != E; ++I, ++argNo) {
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000401 // get the LR of arg
Chris Lattner7076ff22002-06-25 16:13:21 +0000402 LiveRange *LR = LRI.getLiveRangeForValue(I);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000403 assert( LR && "No live range found for method arg");
404
Vikram S. Adve536b1922003-07-25 21:12:15 +0000405 unsigned regType = getRegTypeForLR(LR);
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000406 unsigned RegClassID = LR->getRegClassID();
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000407
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000408 // Find whether this argument is coming in a register (if not, on stack)
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000409 // Also find the correct register the argument must use (UniArgReg)
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000410 //
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000411 bool isArgInReg = false;
Vikram S. Advea83804a2003-05-31 07:32:01 +0000412 unsigned UniArgReg = getInvalidRegNum(); // reg that LR MUST be colored with
Chris Lattner24c1d5e2003-01-14 23:05:08 +0000413 unsigned regClassIDOfArgReg = BadRegClass; // reg class of chosen reg
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000414
415 int regNum = (regType == IntRegType)
Vikram S. Advee9327f02002-05-19 15:25:51 +0000416 ? regNumForIntArg(/*inCallee*/ true, isVarArgs,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000417 argNo, regClassIDOfArgReg)
Vikram S. Advee9327f02002-05-19 15:25:51 +0000418 : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000419 argNo, regClassIDOfArgReg);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000420
Vikram S. Advea83804a2003-05-31 07:32:01 +0000421 if(regNum != getInvalidRegNum()) {
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000422 isArgInReg = true;
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000423 UniArgReg = getUnifiedRegNum( regClassIDOfArgReg, regNum);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000424 }
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000425
Vikram S. Adve65280672003-07-10 19:42:11 +0000426 if( ! LR->isMarkedForSpill() ) { // if this arg received a register
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000427
Ruchira Sasanka36bcd792001-10-24 15:56:58 +0000428 unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() );
429
430 // if LR received the correct color, nothing to do
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000431 //
Ruchira Sasanka36bcd792001-10-24 15:56:58 +0000432 if( UniLRReg == UniArgReg )
433 continue;
434
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000435 // We are here because the LR did not receive the suggested
436 // but LR received another register.
437 // Now we have to copy the %i reg (or stack pos of arg)
438 // to the register the LR was colored with.
Ruchira Sasanka36bcd792001-10-24 15:56:58 +0000439
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000440 // if the arg is coming in UniArgReg register, it MUST go into
Ruchira Sasanka36bcd792001-10-24 15:56:58 +0000441 // the UniLRReg register
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000442 //
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000443 if( isArgInReg ) {
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000444 if( regClassIDOfArgReg != RegClassID ) {
Vikram S. Advee9327f02002-05-19 15:25:51 +0000445 assert(0 && "This could should work but it is not tested yet");
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000446
447 // It is a variable argument call: the float reg must go in a %o reg.
448 // We have to move an int reg to a float reg via memory.
449 //
450 assert(isVarArgs &&
451 RegClassID == FloatRegClassID &&
452 regClassIDOfArgReg == IntRegClassID &&
453 "This should only be an Int register for an FP argument");
454
Chris Lattnerd47aac92002-12-28 20:21:29 +0000455 int TmpOff = MachineFunction::get(Meth).getInfo()->pushTempValue(
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000456 getSpilledRegSize(regType));
Vikram S. Adveaee67012002-07-08 23:23:12 +0000457 cpReg2MemMI(FirstAI->InstrnsBefore,
458 UniArgReg, getFramePointer(), TmpOff, IntRegType);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000459
Vikram S. Adveaee67012002-07-08 23:23:12 +0000460 cpMem2RegMI(FirstAI->InstrnsBefore,
461 getFramePointer(), TmpOff, UniLRReg, regType);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000462 }
463 else {
Vikram S. Adveaee67012002-07-08 23:23:12 +0000464 cpReg2RegMI(FirstAI->InstrnsBefore, UniArgReg, UniLRReg, regType);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000465 }
466 }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000467 else {
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000468
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000469 // Now the arg is coming on stack. Since the LR recieved a register,
470 // we just have to load the arg on stack into that register
Ruchira Sasanka4cfbfd52002-01-07 19:20:28 +0000471 //
Chris Lattnerd47aac92002-12-28 20:21:29 +0000472 const TargetFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve7a1524f2001-11-08 04:56:41 +0000473 int offsetFromFP =
Misha Brukman7ae7f842002-10-28 00:28:31 +0000474 frameInfo.getIncomingArgOffset(MachineFunction::get(Meth),
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000475 argNo);
Vikram S. Advea83804a2003-05-31 07:32:01 +0000476
477 // float arguments on stack are right justified so adjust the offset!
478 // int arguments are also right justified but they are always loaded as
479 // a full double-word so the offset does not need to be adjusted.
480 if (regType == FPSingleRegType) {
481 unsigned argSize = target.getTargetData().getTypeSize(LR->getType());
482 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
483 assert(argSize <= slotSize && "Insufficient slot size!");
484 offsetFromFP += slotSize - argSize;
485 }
486
Vikram S. Adveaee67012002-07-08 23:23:12 +0000487 cpMem2RegMI(FirstAI->InstrnsBefore,
488 getFramePointer(), offsetFromFP, UniLRReg, regType);
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000489 }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000490
491 } // if LR received a color
492
493 else {
494
495 // Now, the LR did not receive a color. But it has a stack offset for
496 // spilling.
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000497 // So, if the arg is coming in UniArgReg register, we can just move
498 // that on to the stack pos of LR
499
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000500 if( isArgInReg ) {
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000501
502 if( regClassIDOfArgReg != RegClassID ) {
503 assert(0 &&
504 "FP arguments to a varargs function should be explicitly "
505 "copied to/from int registers by instruction selection!");
506
507 // It must be a float arg for a variable argument call, which
508 // must come in a %o reg. Move the int reg to the stack.
509 //
510 assert(isVarArgs && regClassIDOfArgReg == IntRegClassID &&
511 "This should only be an Int register for an FP argument");
512
Vikram S. Adveaee67012002-07-08 23:23:12 +0000513 cpReg2MemMI(FirstAI->InstrnsBefore, UniArgReg,
514 getFramePointer(), LR->getSpillOffFromFP(), IntRegType);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000515 }
516 else {
Vikram S. Adveaee67012002-07-08 23:23:12 +0000517 cpReg2MemMI(FirstAI->InstrnsBefore, UniArgReg,
518 getFramePointer(), LR->getSpillOffFromFP(), regType);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000519 }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000520 }
521
522 else {
523
524 // Now the arg is coming on stack. Since the LR did NOT
525 // recieved a register as well, it is allocated a stack position. We
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000526 // can simply change the stack position of the LR. We can do this,
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000527 // since this method is called before any other method that makes
528 // uses of the stack pos of the LR (e.g., updateMachineInstr)
Vikram S. Advea83804a2003-05-31 07:32:01 +0000529 //
Chris Lattnerd47aac92002-12-28 20:21:29 +0000530 const TargetFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve7a1524f2001-11-08 04:56:41 +0000531 int offsetFromFP =
Misha Brukman7ae7f842002-10-28 00:28:31 +0000532 frameInfo.getIncomingArgOffset(MachineFunction::get(Meth),
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000533 argNo);
Vikram S. Advea83804a2003-05-31 07:32:01 +0000534
535 // FP arguments on stack are right justified so adjust offset!
536 // int arguments are also right justified but they are always loaded as
537 // a full double-word so the offset does not need to be adjusted.
538 if (regType == FPSingleRegType) {
539 unsigned argSize = target.getTargetData().getTypeSize(LR->getType());
540 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
541 assert(argSize <= slotSize && "Insufficient slot size!");
542 offsetFromFP += slotSize - argSize;
543 }
Vikram S. Adve7a1524f2001-11-08 04:56:41 +0000544
545 LR->modifySpillOffFromFP( offsetFromFP );
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000546 }
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000547
548 }
549
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000550 } // for each incoming argument
551
552}
553
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000554
555
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000556//---------------------------------------------------------------------------
557// This method is called before graph coloring to suggest colors to the
558// outgoing call args and the return value of the call.
559//---------------------------------------------------------------------------
Vikram S. Adveaee67012002-07-08 23:23:12 +0000560void UltraSparcRegInfo::suggestRegs4CallArgs(MachineInstr *CallMI,
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000561 LiveRangeInfo& LRI) const {
Vikram S. Adve879eac92002-10-13 00:05:30 +0000562 assert ( (target.getInstrInfo()).isCall(CallMI->getOpCode()) );
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000563
Vikram S. Advee9327f02002-05-19 15:25:51 +0000564 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000565
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000566 suggestReg4CallAddr(CallMI, LRI);
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000567
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000568 // First color the return value of the call instruction, if any.
569 // The return value will be in %o0 if the value is an integer type,
570 // or in %f0 if the value is a float type.
571 //
572 if (const Value *RetVal = argDesc->getReturnValue()) {
573 LiveRange *RetValLR = LRI.getLiveRangeForValue(RetVal);
574 assert(RetValLR && "No LR for return Value of call!");
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000575
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000576 unsigned RegClassID = RetValLR->getRegClassID();
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000577
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000578 // now suggest a register depending on the register class of ret arg
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000579 if( RegClassID == IntRegClassID )
Chris Lattner56e91662002-08-12 21:25:05 +0000580 RetValLR->setSuggestedColor(SparcIntRegClass::o0);
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000581 else if (RegClassID == FloatRegClassID )
Chris Lattner56e91662002-08-12 21:25:05 +0000582 RetValLR->setSuggestedColor(SparcFloatRegClass::f0 );
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000583 else assert( 0 && "Unknown reg class for return value of call\n");
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000584 }
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000585
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000586 // Now suggest colors for arguments (operands) of the call instruction.
587 // Colors are suggested only if the arg number is smaller than the
588 // the number of registers allocated for argument passing.
Ruchira Sasanka24729a32001-10-21 16:43:41 +0000589 // Now, go thru call args - implicit operands of the call MI
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000590
Vikram S. Advee9327f02002-05-19 15:25:51 +0000591 unsigned NumOfCallArgs = argDesc->getNumArgs();
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000592
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000593 for(unsigned argNo=0, i=0, intArgNo=0, fpArgNo=0;
594 i < NumOfCallArgs; ++i, ++argNo) {
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000595
Vikram S. Advee9327f02002-05-19 15:25:51 +0000596 const Value *CallArg = argDesc->getArgInfo(i).getArgVal();
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000597
598 // get the LR of call operand (parameter)
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000599 LiveRange *const LR = LRI.getLiveRangeForValue(CallArg);
Vikram S. Advea83804a2003-05-31 07:32:01 +0000600 if (!LR)
601 continue; // no live ranges for constants and labels
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000602
Vikram S. Adve536b1922003-07-25 21:12:15 +0000603 unsigned regType = getRegTypeForLR(LR);
Vikram S. Advea83804a2003-05-31 07:32:01 +0000604 unsigned regClassIDOfArgReg = BadRegClass; // chosen reg class (unused)
Vikram S. Adve6d1036d2002-09-28 16:59:05 +0000605
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000606 // Choose a register for this arg depending on whether it is
Vikram S. Advee9327f02002-05-19 15:25:51 +0000607 // an INT or FP value. Here we ignore whether or not it is a
608 // varargs calls, because FP arguments will be explicitly copied
609 // to an integer Value and handled under (argCopy != NULL) below.
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000610 int regNum = (regType == IntRegType)
Vikram S. Advee9327f02002-05-19 15:25:51 +0000611 ? regNumForIntArg(/*inCallee*/ false, /*isVarArgs*/ false,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000612 argNo, regClassIDOfArgReg)
Vikram S. Advee9327f02002-05-19 15:25:51 +0000613 : regNumForFPArg(regType, /*inCallee*/ false, /*isVarArgs*/ false,
Vikram S. Advea83804a2003-05-31 07:32:01 +0000614 argNo, regClassIDOfArgReg);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000615
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000616 // If a register could be allocated, use it.
617 // If not, do NOTHING as this will be colored as a normal value.
Vikram S. Advea83804a2003-05-31 07:32:01 +0000618 if(regNum != getInvalidRegNum())
Vikram S. Advea6d94c92002-04-25 04:42:21 +0000619 LR->setSuggestedColor(regNum);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000620 } // for all call arguments
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000621}
622
623
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000624//---------------------------------------------------------------------------
Anand Shuklae6c3ee62003-06-01 02:48:23 +0000625// this method is called for an LLVM return instruction to identify which
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000626// values will be returned from this method and to suggest colors.
627//---------------------------------------------------------------------------
Vikram S. Adveaee67012002-07-08 23:23:12 +0000628void UltraSparcRegInfo::suggestReg4RetValue(MachineInstr *RetMI,
Chris Lattner5216cc52002-02-04 05:59:25 +0000629 LiveRangeInfo &LRI) const {
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000630
Vikram S. Adve879eac92002-10-13 00:05:30 +0000631 assert( (target.getInstrInfo()).isReturn( RetMI->getOpCode() ) );
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000632
Vikram S. Adveaee67012002-07-08 23:23:12 +0000633 suggestReg4RetAddr(RetMI, LRI);
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000634
Vikram S. Advea83804a2003-05-31 07:32:01 +0000635 // To find the return value (if any), we can get the LLVM return instr.
636 // from the return address register, which is the first operand
637 Value* tmpI = RetMI->getOperand(0).getVRegValue();
638 ReturnInst* retI=cast<ReturnInst>(cast<TmpInstruction>(tmpI)->getOperand(0));
639 if (const Value *RetVal = retI->getReturnValue())
640 if (LiveRange *const LR = LRI.getLiveRangeForValue(RetVal))
641 LR->setSuggestedColor(LR->getRegClassID() == IntRegClassID
642 ? (unsigned) SparcIntRegClass::i0
643 : (unsigned) SparcFloatRegClass::f0);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000644}
645
Vikram S. Adveaee67012002-07-08 23:23:12 +0000646//---------------------------------------------------------------------------
647// Check if a specified register type needs a scratch register to be
648// copied to/from memory. If it does, the reg. type that must be used
649// for scratch registers is returned in scratchRegType.
650//
651// Only the int CC register needs such a scratch register.
652// The FP CC registers can (and must) be copied directly to/from memory.
653//---------------------------------------------------------------------------
654
655bool
656UltraSparcRegInfo::regTypeNeedsScratchReg(int RegType,
657 int& scratchRegType) const
658{
659 if (RegType == IntCCRegType)
660 {
661 scratchRegType = IntRegType;
662 return true;
663 }
664 return false;
665}
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000666
667//---------------------------------------------------------------------------
668// Copy from a register to register. Register number must be the unified
Vikram S. Adveaee67012002-07-08 23:23:12 +0000669// register number.
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000670//---------------------------------------------------------------------------
671
Vikram S. Advee9327f02002-05-19 15:25:51 +0000672void
Misha Brukman352f7ac2003-05-21 17:59:06 +0000673UltraSparcRegInfo::cpReg2RegMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adveaee67012002-07-08 23:23:12 +0000674 unsigned SrcReg,
Vikram S. Advee9327f02002-05-19 15:25:51 +0000675 unsigned DestReg,
Vikram S. Adveaee67012002-07-08 23:23:12 +0000676 int RegType) const {
Misha Brukman2969ec52003-06-06 09:52:23 +0000677 assert( ((int)SrcReg != getInvalidRegNum()) &&
678 ((int)DestReg != getInvalidRegNum()) &&
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000679 "Invalid Register");
680
681 MachineInstr * MI = NULL;
Vikram S. Advee9327f02002-05-19 15:25:51 +0000682
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000683 switch( RegType ) {
684
Ruchira Sasanka5f629312001-10-18 22:38:52 +0000685 case IntCCRegType:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000686 if (getRegType(DestReg) == IntRegType) {
687 // copy intCC reg to int reg
Vikram S. Adve65280672003-07-10 19:42:11 +0000688 MI = (BuildMI(V9::RDCCR, 2)
689 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
690 SparcIntCCRegClass::ccr))
691 .addMReg(DestReg,MOTy::Def));
Misha Brukman56f4fa12003-05-20 20:32:24 +0000692 } else {
693 // copy int reg to intCC reg
Misha Brukman56f4fa12003-05-20 20:32:24 +0000694 assert(getRegType(SrcReg) == IntRegType
695 && "Can only copy CC reg to/from integer reg");
Vikram S. Adve65280672003-07-10 19:42:11 +0000696 MI = (BuildMI(V9::WRCCRr, 3)
697 .addMReg(SrcReg)
698 .addMReg(SparcIntRegClass::g0)
699 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
700 SparcIntCCRegClass::ccr), MOTy::Def));
Misha Brukman56f4fa12003-05-20 20:32:24 +0000701 }
Vikram S. Adveaee67012002-07-08 23:23:12 +0000702 break;
703
Ruchira Sasanka5f629312001-10-18 22:38:52 +0000704 case FloatCCRegType:
Vikram S. Adveaee67012002-07-08 23:23:12 +0000705 assert(0 && "Cannot copy FPCC register to any other register");
Vikram S. Advee9327f02002-05-19 15:25:51 +0000706 break;
707
708 case IntRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000709 MI = BuildMI(V9::ADDr, 3).addMReg(SrcReg).addMReg(getZeroRegNum())
Misha Brukman56f4fa12003-05-20 20:32:24 +0000710 .addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000711 break;
Vikram S. Advee9327f02002-05-19 15:25:51 +0000712
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000713 case FPSingleRegType:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000714 MI = BuildMI(V9::FMOVS, 2).addMReg(SrcReg).addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000715 break;
716
717 case FPDoubleRegType:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000718 MI = BuildMI(V9::FMOVD, 2).addMReg(SrcReg).addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000719 break;
720
721 default:
Vikram S. Advee9327f02002-05-19 15:25:51 +0000722 assert(0 && "Unknown RegType");
Vikram S. Adveaee67012002-07-08 23:23:12 +0000723 break;
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000724 }
Vikram S. Advee9327f02002-05-19 15:25:51 +0000725
726 if (MI)
727 mvec.push_back(MI);
Ruchira Sasankadfc6c882001-09-18 22:52:44 +0000728}
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000729
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000730//---------------------------------------------------------------------------
Ruchira Sasanka0863c162001-10-24 22:05:34 +0000731// Copy from a register to memory (i.e., Store). Register number must
732// be the unified register number
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000733//---------------------------------------------------------------------------
734
735
Vikram S. Advee9327f02002-05-19 15:25:51 +0000736void
Misha Brukman352f7ac2003-05-21 17:59:06 +0000737UltraSparcRegInfo::cpReg2MemMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adveaee67012002-07-08 23:23:12 +0000738 unsigned SrcReg,
Vikram S. Advee9327f02002-05-19 15:25:51 +0000739 unsigned DestPtrReg,
740 int Offset, int RegType,
Chris Lattner3091e112002-07-25 06:08:32 +0000741 int scratchReg) const {
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000742 MachineInstr * MI = NULL;
Chris Lattner1ebaa902003-01-15 17:47:49 +0000743 switch (RegType) {
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000744 case IntRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000745 assert(target.getInstrInfo().constantFitsInImmedField(V9::STXi, Offset));
746 MI = BuildMI(V9::STXi,3).addMReg(SrcReg).addMReg(DestPtrReg)
747 .addSImm(Offset);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000748 break;
749
750 case FPSingleRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000751 assert(target.getInstrInfo().constantFitsInImmedField(V9::STFi, Offset));
752 MI = BuildMI(V9::STFi, 3).addMReg(SrcReg).addMReg(DestPtrReg)
753 .addSImm(Offset);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000754 break;
755
756 case FPDoubleRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000757 assert(target.getInstrInfo().constantFitsInImmedField(V9::STDFi, Offset));
758 MI = BuildMI(V9::STDFi,3).addMReg(SrcReg).addMReg(DestPtrReg)
759 .addSImm(Offset);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000760 break;
761
Ruchira Sasanka9d8950d2001-11-03 19:59:59 +0000762 case IntCCRegType:
Vikram S. Adveaee67012002-07-08 23:23:12 +0000763 assert(scratchReg >= 0 && "Need scratch reg to store %ccr to memory");
Chris Lattner56e91662002-08-12 21:25:05 +0000764 assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg");
Vikram S. Adve65280672003-07-10 19:42:11 +0000765 MI = (BuildMI(V9::RDCCR, 2)
766 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
767 SparcIntCCRegClass::ccr))
Vikram S. Adved09c4c32003-07-06 20:13:59 +0000768 .addMReg(scratchReg, MOTy::Def));
Vikram S. Adveaee67012002-07-08 23:23:12 +0000769 mvec.push_back(MI);
770
Chris Lattner56e91662002-08-12 21:25:05 +0000771 cpReg2MemMI(mvec, scratchReg, DestPtrReg, Offset, IntRegType);
Chris Lattner1ebaa902003-01-15 17:47:49 +0000772 return;
Vikram S. Adveaee67012002-07-08 23:23:12 +0000773
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000774 case FloatCCRegType: {
Misha Brukmanaf96d392003-05-27 22:40:34 +0000775 assert(target.getInstrInfo().constantFitsInImmedField(V9::STXFSRi, Offset));
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000776 unsigned fsrRegNum = getUnifiedRegNum(UltraSparcRegInfo::SpecialRegClassID,
777 SparcSpecialRegClass::fsr);
Misha Brukmanaf96d392003-05-27 22:40:34 +0000778 MI = BuildMI(V9::STXFSRi, 3)
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000779 .addMReg(fsrRegNum).addMReg(DestPtrReg).addSImm(Offset);
Vikram S. Adveaee67012002-07-08 23:23:12 +0000780 break;
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000781 }
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000782 default:
Vikram S. Advee9327f02002-05-19 15:25:51 +0000783 assert(0 && "Unknown RegType in cpReg2MemMI");
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000784 }
Chris Lattner1ebaa902003-01-15 17:47:49 +0000785 mvec.push_back(MI);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000786}
787
788
789//---------------------------------------------------------------------------
Ruchira Sasanka0863c162001-10-24 22:05:34 +0000790// Copy from memory to a reg (i.e., Load) Register number must be the unified
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000791// register number
792//---------------------------------------------------------------------------
793
794
Vikram S. Advee9327f02002-05-19 15:25:51 +0000795void
Misha Brukman352f7ac2003-05-21 17:59:06 +0000796UltraSparcRegInfo::cpMem2RegMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adveaee67012002-07-08 23:23:12 +0000797 unsigned SrcPtrReg,
Vikram S. Advee9327f02002-05-19 15:25:51 +0000798 int Offset,
799 unsigned DestReg,
800 int RegType,
Chris Lattner3091e112002-07-25 06:08:32 +0000801 int scratchReg) const {
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000802 MachineInstr * MI = NULL;
Chris Lattner5216cc52002-02-04 05:59:25 +0000803 switch (RegType) {
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000804 case IntRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000805 assert(target.getInstrInfo().constantFitsInImmedField(V9::LDXi, Offset));
806 MI = BuildMI(V9::LDXi, 3).addMReg(SrcPtrReg).addSImm(Offset)
Misha Brukman56f4fa12003-05-20 20:32:24 +0000807 .addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000808 break;
809
810 case FPSingleRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000811 assert(target.getInstrInfo().constantFitsInImmedField(V9::LDFi, Offset));
812 MI = BuildMI(V9::LDFi, 3).addMReg(SrcPtrReg).addSImm(Offset)
Misha Brukman56f4fa12003-05-20 20:32:24 +0000813 .addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000814 break;
815
816 case FPDoubleRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000817 assert(target.getInstrInfo().constantFitsInImmedField(V9::LDDFi, Offset));
818 MI = BuildMI(V9::LDDFi, 3).addMReg(SrcPtrReg).addSImm(Offset)
819 .addMReg(DestReg, MOTy::Def);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000820 break;
821
Ruchira Sasanka9d8950d2001-11-03 19:59:59 +0000822 case IntCCRegType:
Vikram S. Adveaee67012002-07-08 23:23:12 +0000823 assert(scratchReg >= 0 && "Need scratch reg to load %ccr from memory");
Chris Lattner56e91662002-08-12 21:25:05 +0000824 assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg");
825 cpMem2RegMI(mvec, SrcPtrReg, Offset, scratchReg, IntRegType);
Vikram S. Adve65280672003-07-10 19:42:11 +0000826 MI = (BuildMI(V9::WRCCRr, 3)
827 .addMReg(scratchReg)
828 .addMReg(SparcIntRegClass::g0)
829 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
830 SparcIntCCRegClass::ccr), MOTy::Def));
Vikram S. Adveaee67012002-07-08 23:23:12 +0000831 break;
832
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000833 case FloatCCRegType: {
Misha Brukmanaf96d392003-05-27 22:40:34 +0000834 assert(target.getInstrInfo().constantFitsInImmedField(V9::LDXFSRi, Offset));
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000835 unsigned fsrRegNum = getUnifiedRegNum(UltraSparcRegInfo::SpecialRegClassID,
836 SparcSpecialRegClass::fsr);
Misha Brukmanaf96d392003-05-27 22:40:34 +0000837 MI = BuildMI(V9::LDXFSRi, 3).addMReg(SrcPtrReg).addSImm(Offset)
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000838 .addMReg(fsrRegNum, MOTy::UseAndDef);
Vikram S. Adveaee67012002-07-08 23:23:12 +0000839 break;
Vikram S. Adve8adb9942003-05-27 00:02:22 +0000840 }
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000841 default:
Ruchira Sasanka0c085982001-11-10 21:20:43 +0000842 assert(0 && "Unknown RegType in cpMem2RegMI");
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000843 }
Chris Lattner1ebaa902003-01-15 17:47:49 +0000844 mvec.push_back(MI);
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000845}
846
847
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000848//---------------------------------------------------------------------------
849// Generate a copy instruction to copy a value to another. Temporarily
850// used by PhiElimination code.
851//---------------------------------------------------------------------------
852
853
Vikram S. Advee9327f02002-05-19 15:25:51 +0000854void
Chris Lattner1ebaa902003-01-15 17:47:49 +0000855UltraSparcRegInfo::cpValue2Value(Value *Src, Value *Dest,
Misha Brukman352f7ac2003-05-21 17:59:06 +0000856 std::vector<MachineInstr*>& mvec) const {
Vikram S. Adve536b1922003-07-25 21:12:15 +0000857 int RegType = getRegTypeForDataType(Src->getType());
Ruchira Sasankab7a39722001-11-03 17:13:27 +0000858 MachineInstr * MI = NULL;
859
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000860 switch( RegType ) {
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000861 case IntRegType:
Misha Brukmanaf96d392003-05-27 22:40:34 +0000862 MI = BuildMI(V9::ADDr, 3).addReg(Src).addMReg(getZeroRegNum())
Misha Brukman56f4fa12003-05-20 20:32:24 +0000863 .addRegDef(Dest);
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000864 break;
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000865 case FPSingleRegType:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000866 MI = BuildMI(V9::FMOVS, 2).addReg(Src).addRegDef(Dest);
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000867 break;
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000868 case FPDoubleRegType:
Misha Brukman56f4fa12003-05-20 20:32:24 +0000869 MI = BuildMI(V9::FMOVD, 2).addReg(Src).addRegDef(Dest);
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000870 break;
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000871 default:
872 assert(0 && "Unknow RegType in CpValu2Value");
873 }
Ruchira Sasankab7a39722001-11-03 17:13:27 +0000874
Chris Lattner9bebf832002-10-28 20:10:56 +0000875 mvec.push_back(MI);
Ruchira Sasankab7a39722001-11-03 17:13:27 +0000876}
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000877
878
879
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +0000880
881
882
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000883//----------------------------------------------------------------------------
884// This method inserts caller saving/restoring instructons before/after
Ruchira Sasankaf4c2ddd2002-01-07 21:03:42 +0000885// a call machine instruction. The caller saving/restoring instructions are
886// inserted like:
887//
888// ** caller saving instructions
889// other instructions inserted for the call by ColorCallArg
890// CALL instruction
891// other instructions inserted for the call ColorCallArg
892// ** caller restoring instructions
893//
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000894//----------------------------------------------------------------------------
Ruchira Sasanka5b8971f2001-10-16 01:23:19 +0000895
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000896
Vikram S. Adve4aee77c702002-07-10 21:36:00 +0000897void
Misha Brukman352f7ac2003-05-21 17:59:06 +0000898UltraSparcRegInfo::insertCallerSavingCode
899(std::vector<MachineInstr*> &instrnsBefore,
900 std::vector<MachineInstr*> &instrnsAfter,
901 MachineInstr *CallMI,
902 const BasicBlock *BB,
903 PhyRegAlloc &PRA) const
Vikram S. Adve4aee77c702002-07-10 21:36:00 +0000904{
Chris Lattnerc7832972003-07-21 19:56:49 +0000905 assert(target.getInstrInfo().isCall(CallMI->getOpCode()));
Vikram S. Advee9327f02002-05-19 15:25:51 +0000906
Ruchira Sasankaf4c2ddd2002-01-07 21:03:42 +0000907 // has set to record which registers were saved/restored
908 //
Chris Lattnere98dd5f2002-07-24 21:21:32 +0000909 hash_set<unsigned> PushedRegSet;
Ruchira Sasankaf4c2ddd2002-01-07 21:03:42 +0000910
Vikram S. Advee9327f02002-05-19 15:25:51 +0000911 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
912
Chris Lattnerc7832972003-07-21 19:56:49 +0000913 // if the call is to a instrumentation function, do not insert save and
914 // restore instructions the instrumentation function takes care of save
915 // restore for volatile regs.
916 //
917 // FIXME: this should be made general, not specific to the reoptimizer!
918 //
919 const Function *Callee = argDesc->getCallInst()->getCalledFunction();
920 bool isLLVMFirstTrigger = Callee && Callee->getName() == "llvm_first_trigger";
Anand Shuklabd2d0572003-07-20 15:39:30 +0000921
Vikram S. Advea83804a2003-05-31 07:32:01 +0000922 // Now check if the call has a return value (using argDesc) and if so,
923 // find the LR of the TmpInstruction representing the return value register.
924 // (using the last or second-last *implicit operand* of the call MI).
925 // Insert it to to the PushedRegSet since we must not save that register
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000926 // and restore it after the call.
927 // We do this because, we look at the LV set *after* the instruction
928 // to determine, which LRs must be saved across calls. The return value
929 // of the call is live in this set - but we must not save/restore it.
Vikram S. Advea83804a2003-05-31 07:32:01 +0000930 //
931 if (const Value *origRetVal = argDesc->getReturnValue()) {
932 unsigned retValRefNum = (CallMI->getNumImplicitRefs() -
933 (argDesc->getIndirectFuncPtr()? 1 : 2));
934 const TmpInstruction* tmpRetVal =
935 cast<TmpInstruction>(CallMI->getImplicitRef(retValRefNum));
936 assert(tmpRetVal->getOperand(0) == origRetVal &&
937 tmpRetVal->getType() == origRetVal->getType() &&
938 "Wrong implicit ref?");
939 LiveRange *RetValLR = PRA.LRI.getLiveRangeForValue( tmpRetVal );
Chris Lattner5216cc52002-02-04 05:59:25 +0000940 assert(RetValLR && "No LR for RetValue of call");
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000941
Vikram S. Adve65280672003-07-10 19:42:11 +0000942 if (! RetValLR->isMarkedForSpill())
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000943 PushedRegSet.insert(getUnifiedRegNum(RetValLR->getRegClassID(),
944 RetValLR->getColor()));
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000945 }
946
Vikram S. Advee9327f02002-05-19 15:25:51 +0000947 const ValueSet &LVSetAft = PRA.LVI->getLiveVarSetAfterMInst(CallMI, BB);
Chris Lattner7e5ee422002-02-05 04:20:12 +0000948 ValueSet::const_iterator LIt = LVSetAft.begin();
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +0000949
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000950 // for each live var in live variable set after machine inst
Chris Lattner7e5ee422002-02-05 04:20:12 +0000951 for( ; LIt != LVSetAft.end(); ++LIt) {
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000952
953 // get the live range corresponding to live var
954 LiveRange *const LR = PRA.LRI.getLiveRangeForValue(*LIt );
955
956 // LR can be null if it is a const since a const
957 // doesn't have a dominating def - see Assumptions above
958 if( LR ) {
959
Vikram S. Adve65280672003-07-10 19:42:11 +0000960 if(! LR->isMarkedForSpill()) {
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000961
Vikram S. Adve65280672003-07-10 19:42:11 +0000962 assert(LR->hasColor() && "LR is neither spilled nor colored?");
Chris Lattnerf9fd5912003-01-15 21:14:32 +0000963 unsigned RCID = LR->getRegClassID();
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000964 unsigned Color = LR->getColor();
965
966 if ( isRegVolatile(RCID, Color) ) {
967
Anand Shuklabd2d0572003-07-20 15:39:30 +0000968 //if the function is special LLVM function,
969 //And the register is not modified by call, don't save and restore
970 if(isLLVMFirstTrigger && !modifiedByCall(RCID, Color))
971 continue;
972
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000973 // if the value is in both LV sets (i.e., live before and after
974 // the call machine instruction)
Vikram S. Adveaee67012002-07-08 23:23:12 +0000975
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000976 unsigned Reg = getUnifiedRegNum(RCID, Color);
977
978 if( PushedRegSet.find(Reg) == PushedRegSet.end() ) {
979
980 // if we haven't already pushed that register
981
Vikram S. Adve536b1922003-07-25 21:12:15 +0000982 unsigned RegType = getRegTypeForLR(LR);
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000983
984 // Now get two instructions - to push on stack and pop from stack
985 // and add them to InstrnsBefore and InstrnsAfter of the
986 // call instruction
Vikram S. Adveaee67012002-07-08 23:23:12 +0000987 //
Vikram S. Advea83804a2003-05-31 07:32:01 +0000988 int StackOff =
989 PRA.MF.getInfo()->pushTempValue(getSpilledRegSize(RegType));
Vikram S. Adve7a1524f2001-11-08 04:56:41 +0000990
Ruchira Sasanka9d8950d2001-11-03 19:59:59 +0000991 //---- Insert code for pushing the reg on stack ----------
Vikram S. Adveaee67012002-07-08 23:23:12 +0000992
Vikram S. Adveb5f8ada2003-07-02 01:13:57 +0000993 std::vector<MachineInstr*> AdIBef, AdIAft;
994
Vikram S. Adveaee67012002-07-08 23:23:12 +0000995 // We may need a scratch register to copy the saved value
996 // to/from memory. This may itself have to insert code to
997 // free up a scratch register. Any such code should go before
Vikram S. Advea83804a2003-05-31 07:32:01 +0000998 // the save code. The scratch register, if any, is by default
999 // temporary and not "used" by the instruction unless the
1000 // copy code itself decides to keep the value in the scratch reg.
Vikram S. Adveaee67012002-07-08 23:23:12 +00001001 int scratchRegType = -1;
1002 int scratchReg = -1;
Chris Lattner56e91662002-08-12 21:25:05 +00001003 if (regTypeNeedsScratchReg(RegType, scratchRegType))
Vikram S. Adveaee67012002-07-08 23:23:12 +00001004 { // Find a register not live in the LVSet before CallMI
1005 const ValueSet &LVSetBef =
1006 PRA.LVI->getLiveVarSetBeforeMInst(CallMI, BB);
1007 scratchReg = PRA.getUsableUniRegAtMI(scratchRegType, &LVSetBef,
1008 CallMI, AdIBef, AdIAft);
Chris Lattner56e91662002-08-12 21:25:05 +00001009 assert(scratchReg != getInvalidRegNum());
Vikram S. Adveaee67012002-07-08 23:23:12 +00001010 }
1011
1012 if (AdIBef.size() > 0)
1013 instrnsBefore.insert(instrnsBefore.end(),
1014 AdIBef.begin(), AdIBef.end());
1015
1016 cpReg2MemMI(instrnsBefore, Reg,getFramePointer(),StackOff,RegType,
1017 scratchReg);
1018
Vikram S. Adve4aee77c702002-07-10 21:36:00 +00001019 if (AdIAft.size() > 0)
Vikram S. Adveaee67012002-07-08 23:23:12 +00001020 instrnsBefore.insert(instrnsBefore.end(),
1021 AdIAft.begin(), AdIAft.end());
1022
Ruchira Sasanka9d8950d2001-11-03 19:59:59 +00001023 //---- Insert code for popping the reg from the stack ----------
1024
Vikram S. Adveb5f8ada2003-07-02 01:13:57 +00001025 AdIBef.clear();
1026 AdIAft.clear();
1027
Vikram S. Adveaee67012002-07-08 23:23:12 +00001028 // We may need a scratch register to copy the saved value
1029 // from memory. This may itself have to insert code to
1030 // free up a scratch register. Any such code should go
Vikram S. Advea83804a2003-05-31 07:32:01 +00001031 // after the save code. As above, scratch is not marked "used".
Vikram S. Adveaee67012002-07-08 23:23:12 +00001032 //
1033 scratchRegType = -1;
1034 scratchReg = -1;
Chris Lattner56e91662002-08-12 21:25:05 +00001035 if (regTypeNeedsScratchReg(RegType, scratchRegType))
Vikram S. Adveaee67012002-07-08 23:23:12 +00001036 { // Find a register not live in the LVSet after CallMI
1037 scratchReg = PRA.getUsableUniRegAtMI(scratchRegType, &LVSetAft,
1038 CallMI, AdIBef, AdIAft);
Chris Lattner56e91662002-08-12 21:25:05 +00001039 assert(scratchReg != getInvalidRegNum());
Vikram S. Adveaee67012002-07-08 23:23:12 +00001040 }
1041
1042 if (AdIBef.size() > 0)
1043 instrnsAfter.insert(instrnsAfter.end(),
1044 AdIBef.begin(), AdIBef.end());
1045
1046 cpMem2RegMI(instrnsAfter, getFramePointer(), StackOff,Reg,RegType,
1047 scratchReg);
1048
1049 if (AdIAft.size() > 0)
1050 instrnsAfter.insert(instrnsAfter.end(),
1051 AdIAft.begin(), AdIAft.end());
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +00001052
Chris Lattner7e5ee422002-02-05 04:20:12 +00001053 PushedRegSet.insert(Reg);
Vikram S. Adveaee67012002-07-08 23:23:12 +00001054
Ruchira Sasanka1812fc42001-11-10 00:26:55 +00001055 if(DEBUG_RA) {
Misha Brukman352f7ac2003-05-21 17:59:06 +00001056 std::cerr << "\nFor call inst:" << *CallMI;
1057 std::cerr << " -inserted caller saving instrs: Before:\n\t ";
Vikram S. Adveaee67012002-07-08 23:23:12 +00001058 for_each(instrnsBefore.begin(), instrnsBefore.end(),
Anand Shukla7e882db2002-07-09 19:16:59 +00001059 std::mem_fun(&MachineInstr::dump));
Misha Brukman352f7ac2003-05-21 17:59:06 +00001060 std::cerr << " -and After:\n\t ";
Vikram S. Adveaee67012002-07-08 23:23:12 +00001061 for_each(instrnsAfter.begin(), instrnsAfter.end(),
Anand Shukla7e882db2002-07-09 19:16:59 +00001062 std::mem_fun(&MachineInstr::dump));
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +00001063 }
1064 } // if not already pushed
1065
1066 } // if LR has a volatile color
1067
1068 } // if LR has color
1069
1070 } // if there is a LR for Var
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +00001071
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +00001072 } // for each value in the LV set after instruction
Ruchira Sasanka5867c7a2001-09-30 23:16:47 +00001073}
1074
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +00001075
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00001076//---------------------------------------------------------------------------
1077// Print the register assigned to a LR
1078//---------------------------------------------------------------------------
1079
Vikram S. Adve8adb9942003-05-27 00:02:22 +00001080void UltraSparcRegInfo::printReg(const LiveRange *LR) const {
Chris Lattnerf9fd5912003-01-15 21:14:32 +00001081 unsigned RegClassID = LR->getRegClassID();
Misha Brukman352f7ac2003-05-21 17:59:06 +00001082 std::cerr << " *Node " << (LR->getUserIGNode())->getIndex();
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00001083
Chris Lattner5216cc52002-02-04 05:59:25 +00001084 if (!LR->hasColor()) {
Misha Brukman352f7ac2003-05-21 17:59:06 +00001085 std::cerr << " - could not find a color\n";
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00001086 return;
1087 }
1088
1089 // if a color is found
1090
Misha Brukman352f7ac2003-05-21 17:59:06 +00001091 std::cerr << " colored with color "<< LR->getColor();
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00001092
Vikram S. Adve8adb9942003-05-27 00:02:22 +00001093 unsigned uRegName = getUnifiedRegNum(RegClassID, LR->getColor());
1094
1095 std::cerr << "[";
1096 std::cerr<< getUnifiedRegName(uRegName);
1097 if (RegClassID == FloatRegClassID && LR->getType() == Type::DoubleTy)
1098 std::cerr << "+" << getUnifiedRegName(uRegName+1);
1099 std::cerr << "]\n";
Ruchira Sasankadfc6c882001-09-18 22:52:44 +00001100}