blob: 5edbbe06470f3386dd2f9dd93c2d2a50776f6fd3 [file] [log] [blame]
Chris Lattnered5171e2002-02-03 07:52:04 +00001//===-- SparcRegInfo.cpp - Sparc Target Register Information --------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnered5171e2002-02-03 07:52:04 +00009//
10// This file contains implementation of Sparc specific helper methods
11// used for register allocation.
12//
13//===----------------------------------------------------------------------===//
14
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000015#include "SparcInternals.h"
Chris Lattner699683c2002-02-04 05:59:25 +000016#include "SparcRegClassInfo.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000017#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b849be2002-12-28 20:21:29 +000018#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000019#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner00dca912003-01-15 17:47:49 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Anand Shukla55afc332003-06-01 02:48:23 +000021#include "llvm/CodeGen/MachineCodeForInstruction.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000022#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner893f9542003-09-01 20:12:17 +000023#include "../../CodeGen/RegAlloc/LiveRangeInfo.h" // FIXME!!
Chris Lattner84681f12003-09-01 20:17:13 +000024#include "../../CodeGen/RegAlloc/LiveRange.h" // FIXME!!
Chris Lattner699683c2002-02-04 05:59:25 +000025#include "llvm/iTerminators.h"
26#include "llvm/iOther.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000027#include "llvm/Function.h"
Ruchira Sasankad00982a2002-01-07 19:20:28 +000028#include "llvm/DerivedTypes.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
31
Chris Lattner92ba2aa2003-01-14 23:05:08 +000032enum {
33 BadRegClass = ~0
34};
35
Chris Lattner699683c2002-02-04 05:59:25 +000036UltraSparcRegInfo::UltraSparcRegInfo(const UltraSparc &tgt)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +000037 : TargetRegInfo(tgt), NumOfIntArgRegs(6), NumOfFloatArgRegs(32)
38{
Chris Lattner699683c2002-02-04 05:59:25 +000039 MachineRegClassArr.push_back(new SparcIntRegClass(IntRegClassID));
40 MachineRegClassArr.push_back(new SparcFloatRegClass(FloatRegClassID));
41 MachineRegClassArr.push_back(new SparcIntCCRegClass(IntCCRegClassID));
42 MachineRegClassArr.push_back(new SparcFloatCCRegClass(FloatCCRegClassID));
Vikram S. Adve78a4f232003-05-27 00:02:22 +000043 MachineRegClassArr.push_back(new SparcSpecialRegClass(SpecialRegClassID));
Vikram S. Adve76ee6f72002-07-08 23:23:12 +000044
Chris Lattner95685682002-08-12 21:25:05 +000045 assert(SparcFloatRegClass::StartOfNonVolatileRegs == 32 &&
Chris Lattner699683c2002-02-04 05:59:25 +000046 "32 Float regs are used for float arg passing");
47}
48
49
Vikram S. Advef1c15ee2002-03-18 03:12:16 +000050// getZeroRegNum - returns the register that contains always zero.
51// this is the unified register number
Chris Lattner699683c2002-02-04 05:59:25 +000052//
Vikram S. Advef1c15ee2002-03-18 03:12:16 +000053int UltraSparcRegInfo::getZeroRegNum() const {
Chris Lattner95685682002-08-12 21:25:05 +000054 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
55 SparcIntRegClass::g0);
Vikram S. Advef1c15ee2002-03-18 03:12:16 +000056}
Chris Lattner699683c2002-02-04 05:59:25 +000057
58// getCallAddressReg - returns the reg used for pushing the address when a
59// method is called. This can be used for other purposes between calls
60//
61unsigned UltraSparcRegInfo::getCallAddressReg() const {
Chris Lattner95685682002-08-12 21:25:05 +000062 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
63 SparcIntRegClass::o7);
Chris Lattner699683c2002-02-04 05:59:25 +000064}
65
66// Returns the register containing the return address.
67// It should be made sure that this register contains the return
68// value when a return instruction is reached.
69//
70unsigned UltraSparcRegInfo::getReturnAddressReg() const {
Chris Lattner95685682002-08-12 21:25:05 +000071 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
72 SparcIntRegClass::i7);
73}
74
75// Register get name implementations...
76
77// Int register names in same order as enum in class SparcIntRegClass
78static const char * const IntRegNames[] = {
79 "o0", "o1", "o2", "o3", "o4", "o5", "o7",
80 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
81 "i0", "i1", "i2", "i3", "i4", "i5",
82 "i6", "i7",
83 "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
84 "o6"
85};
86
Vikram S. Adve78a4f232003-05-27 00:02:22 +000087const char * const SparcIntRegClass::getRegName(unsigned reg) const {
Chris Lattner95685682002-08-12 21:25:05 +000088 assert(reg < NumOfAllRegs);
89 return IntRegNames[reg];
90}
91
92static const char * const FloatRegNames[] = {
93 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
94 "f10", "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19",
95 "f20", "f21", "f22", "f23", "f24", "f25", "f26", "f27", "f28", "f29",
96 "f30", "f31", "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",
97 "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47", "f48", "f49",
98 "f50", "f51", "f52", "f53", "f54", "f55", "f56", "f57", "f58", "f59",
99 "f60", "f61", "f62", "f63"
100};
101
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000102const char * const SparcFloatRegClass::getRegName(unsigned reg) const {
Chris Lattner95685682002-08-12 21:25:05 +0000103 assert (reg < NumOfAllRegs);
104 return FloatRegNames[reg];
105}
106
107
108static const char * const IntCCRegNames[] = {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000109 "xcc", "icc", "ccr"
Chris Lattner95685682002-08-12 21:25:05 +0000110};
111
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000112const char * const SparcIntCCRegClass::getRegName(unsigned reg) const {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000113 assert(reg < 3);
Chris Lattner95685682002-08-12 21:25:05 +0000114 return IntCCRegNames[reg];
115}
116
117static const char * const FloatCCRegNames[] = {
118 "fcc0", "fcc1", "fcc2", "fcc3"
119};
120
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000121const char * const SparcFloatCCRegClass::getRegName(unsigned reg) const {
122 assert (reg < 5);
Chris Lattner95685682002-08-12 21:25:05 +0000123 return FloatCCRegNames[reg];
Chris Lattner699683c2002-02-04 05:59:25 +0000124}
125
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000126static const char * const SpecialRegNames[] = {
127 "fsr"
128};
129
130const char * const SparcSpecialRegClass::getRegName(unsigned reg) const {
131 assert (reg < 1);
132 return SpecialRegNames[reg];
Chris Lattner699683c2002-02-04 05:59:25 +0000133}
134
Vikram S. Advef1c15ee2002-03-18 03:12:16 +0000135// Get unified reg number for frame pointer
Chris Lattner699683c2002-02-04 05:59:25 +0000136unsigned UltraSparcRegInfo::getFramePointer() const {
Chris Lattner95685682002-08-12 21:25:05 +0000137 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
138 SparcIntRegClass::i6);
Chris Lattner699683c2002-02-04 05:59:25 +0000139}
140
Vikram S. Advef1c15ee2002-03-18 03:12:16 +0000141// Get unified reg number for stack pointer
Chris Lattner699683c2002-02-04 05:59:25 +0000142unsigned UltraSparcRegInfo::getStackPointer() const {
Chris Lattner95685682002-08-12 21:25:05 +0000143 return getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
144 SparcIntRegClass::o6);
Chris Lattner699683c2002-02-04 05:59:25 +0000145}
146
147
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000148//---------------------------------------------------------------------------
149// Finds whether a call is an indirect call
150//---------------------------------------------------------------------------
151
152inline bool
153isVarArgsFunction(const Type *funcType) {
154 return cast<FunctionType>(cast<PointerType>(funcType)
155 ->getElementType())->isVarArg();
156}
157
158inline bool
159isVarArgsCall(const MachineInstr *CallMI) {
160 Value* callee = CallMI->getOperand(0).getVRegValue();
161 // const Type* funcType = isa<Function>(callee)? callee->getType()
162 // : cast<PointerType>(callee->getType())->getElementType();
163 const Type* funcType = callee->getType();
164 return isVarArgsFunction(funcType);
165}
166
167
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000168// Get the register number for the specified argument #argNo,
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000169//
170// Return value:
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000171// getInvalidRegNum(), if there is no int register available for the arg.
172// regNum, otherwise (this is NOT the unified reg. num).
173// regClassId is set to the register class ID.
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000174//
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000175int
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000176UltraSparcRegInfo::regNumForIntArg(bool inCallee, bool isVarArgsCall,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000177 unsigned argNo, unsigned& regClassId) const
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000178{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000179 regClassId = IntRegClassID;
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000180 if (argNo >= NumOfIntArgRegs)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000181 return getInvalidRegNum();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000182 else
Chris Lattner95685682002-08-12 21:25:05 +0000183 return argNo + (inCallee? SparcIntRegClass::i0 : SparcIntRegClass::o0);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000184}
185
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000186// Get the register number for the specified FP argument #argNo,
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000187// Use INT regs for FP args if this is a varargs call.
188//
189// Return value:
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000190// getInvalidRegNum(), if there is no int register available for the arg.
191// regNum, otherwise (this is NOT the unified reg. num).
192// regClassId is set to the register class ID.
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000193//
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000194int
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000195UltraSparcRegInfo::regNumForFPArg(unsigned regType,
196 bool inCallee, bool isVarArgsCall,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000197 unsigned argNo, unsigned& regClassId) const
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000198{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000199 if (isVarArgsCall)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000200 return regNumForIntArg(inCallee, isVarArgsCall, argNo, regClassId);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000201 else
202 {
203 regClassId = FloatRegClassID;
204 if (regType == FPSingleRegType)
205 return (argNo*2+1 >= NumOfFloatArgRegs)?
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000206 getInvalidRegNum() : SparcFloatRegClass::f0 + (argNo * 2 + 1);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000207 else if (regType == FPDoubleRegType)
208 return (argNo*2 >= NumOfFloatArgRegs)?
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000209 getInvalidRegNum() : SparcFloatRegClass::f0 + (argNo * 2);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000210 else
211 assert(0 && "Illegal FP register type");
Chris Lattnerb82d97e2002-07-25 06:08:32 +0000212 return 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000213 }
Vikram S. Advea44c6c02002-03-31 19:04:50 +0000214}
215
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000216
217//---------------------------------------------------------------------------
218// Finds the return address of a call sparc specific call instruction
219//---------------------------------------------------------------------------
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000220
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000221// The following 4 methods are used to find the RegType (SparcInternals.h)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000222// of a LiveRange, a Value, and for a given register unified reg number.
Chris Lattner699683c2002-02-04 05:59:25 +0000223//
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000224int UltraSparcRegInfo::getRegTypeForClassAndType(unsigned regClassID,
225 const Type* type) const
226{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000227 switch (regClassID) {
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000228 case IntRegClassID: return IntRegType;
229 case FloatRegClassID:
230 if (type == Type::FloatTy) return FPSingleRegType;
231 else if (type == Type::DoubleTy) return FPDoubleRegType;
232 assert(0 && "Unknown type in FloatRegClass"); return 0;
233 case IntCCRegClassID: return IntCCRegType;
234 case FloatCCRegClassID: return FloatCCRegType;
235 case SpecialRegClassID: return SpecialRegType;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000236 default: assert( 0 && "Unknown reg class ID"); return 0;
Chris Lattner699683c2002-02-04 05:59:25 +0000237 }
238}
239
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000240int UltraSparcRegInfo::getRegTypeForDataType(const Type* type) const
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000241{
242 return getRegTypeForClassAndType(getRegClassIDOfType(type), type);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000243}
244
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000245int UltraSparcRegInfo::getRegTypeForLR(const LiveRange *LR) const
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000246{
247 return getRegTypeForClassAndType(LR->getRegClassID(), LR->getType());
248}
Chris Lattner699683c2002-02-04 05:59:25 +0000249
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000250int UltraSparcRegInfo::getRegType(int unifiedRegNum) const
251{
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000252 if (unifiedRegNum < 32)
Chris Lattner699683c2002-02-04 05:59:25 +0000253 return IntRegType;
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000254 else if (unifiedRegNum < (32 + 32))
Chris Lattner699683c2002-02-04 05:59:25 +0000255 return FPSingleRegType;
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000256 else if (unifiedRegNum < (64 + 32))
Chris Lattner699683c2002-02-04 05:59:25 +0000257 return FPDoubleRegType;
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000258 else if (unifiedRegNum < (64+32+4))
Chris Lattner699683c2002-02-04 05:59:25 +0000259 return FloatCCRegType;
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000260 else if (unifiedRegNum < (64+32+4+2))
Chris Lattner699683c2002-02-04 05:59:25 +0000261 return IntCCRegType;
262 else
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000263 assert(0 && "Invalid unified register number in getRegType");
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000264 return 0;
Chris Lattner699683c2002-02-04 05:59:25 +0000265}
266
267
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000268// To find the register class used for a specified Type
269//
270unsigned UltraSparcRegInfo::getRegClassIDOfType(const Type *type,
Chris Lattnerb82d97e2002-07-25 06:08:32 +0000271 bool isCCReg) const {
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000272 Type::PrimitiveID ty = type->getPrimitiveID();
273 unsigned res;
274
275 // FIXME: Comparing types like this isn't very safe...
276 if ((ty && ty <= Type::LongTyID) || (ty == Type::LabelTyID) ||
277 (ty == Type::FunctionTyID) || (ty == Type::PointerTyID) )
278 res = IntRegClassID; // sparc int reg (ty=0: void)
279 else if (ty <= Type::DoubleTyID)
280 res = FloatRegClassID; // sparc float reg class
281 else {
282 //std::cerr << "TypeID: " << ty << "\n";
283 assert(0 && "Cannot resolve register class for type");
284 return 0;
285 }
286
Chris Lattner3c3c82d2003-01-15 21:14:32 +0000287 if (isCCReg)
288 return res + 2; // corresponding condition code register
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000289 else
290 return res;
291}
292
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000293unsigned UltraSparcRegInfo::getRegClassIDOfRegType(int regType) const {
294 switch(regType) {
295 case IntRegType: return IntRegClassID;
296 case FPSingleRegType:
297 case FPDoubleRegType: return FloatRegClassID;
298 case IntCCRegType: return IntCCRegClassID;
299 case FloatCCRegType: return FloatCCRegClassID;
300 default:
301 assert(0 && "Invalid register type in getRegClassIDOfRegType");
302 return 0;
303 }
304}
305
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000306//---------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000307// Suggests a register for the ret address in the RET machine instruction.
308// We always suggest %i7 by convention.
309//---------------------------------------------------------------------------
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000310void UltraSparcRegInfo::suggestReg4RetAddr(MachineInstr *RetMI,
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000311 LiveRangeInfo& LRI) const {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000312
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000313 assert(target.getInstrInfo().isReturn(RetMI->getOpCode()));
Vikram S. Adve53fec862001-10-22 13:41:12 +0000314
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000315 // return address is always mapped to i7 so set it immediately
316 RetMI->SetRegForOperand(0, getUnifiedRegNum(IntRegClassID,
Chris Lattner95685682002-08-12 21:25:05 +0000317 SparcIntRegClass::i7));
Vikram S. Adve53fec862001-10-22 13:41:12 +0000318
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000319 // Possible Optimization:
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000320 // Instead of setting the color, we can suggest one. In that case,
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000321 // we have to test later whether it received the suggested color.
322 // In that case, a LR has to be created at the start of method.
323 // It has to be done as follows (remove the setRegVal above):
Ruchira Sasanka91442282001-09-30 23:16:47 +0000324
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000325 // MachineOperand & MO = RetMI->getOperand(0);
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000326 // const Value *RetAddrVal = MO.getVRegValue();
327 // assert( RetAddrVal && "LR for ret address must be created at start");
328 // LiveRange * RetAddrLR = LRI.getLiveRangeForValue( RetAddrVal);
329 // RetAddrLR->setSuggestedColor(getUnifiedRegNum( IntRegClassID,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000330 // SparcIntRegOrdr::i7) );
Ruchira Sasanka91442282001-09-30 23:16:47 +0000331}
332
333
334//---------------------------------------------------------------------------
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000335// Suggests a register for the ret address in the JMPL/CALL machine instr.
336// Sparc ABI dictates that %o7 be used for this purpose.
Ruchira Sasanka91442282001-09-30 23:16:47 +0000337//---------------------------------------------------------------------------
Vikram S. Adve87817652002-09-28 16:59:05 +0000338void
339UltraSparcRegInfo::suggestReg4CallAddr(MachineInstr * CallMI,
340 LiveRangeInfo& LRI) const
341{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000342 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
343 const Value *RetAddrVal = argDesc->getReturnAddrReg();
Vikram S. Adve87817652002-09-28 16:59:05 +0000344 assert(RetAddrVal && "INTERNAL ERROR: Return address value is required");
Ruchira Sasanka91442282001-09-30 23:16:47 +0000345
Vikram S. Adve87817652002-09-28 16:59:05 +0000346 // A LR must already exist for the return address.
347 LiveRange *RetAddrLR = LRI.getLiveRangeForValue(RetAddrVal);
348 assert(RetAddrLR && "INTERNAL ERROR: No LR for return address of call!");
349
Chris Lattner3c3c82d2003-01-15 21:14:32 +0000350 unsigned RegClassID = RetAddrLR->getRegClassID();
Vikram S. Adve87817652002-09-28 16:59:05 +0000351 RetAddrLR->setColor(getUnifiedRegNum(IntRegClassID, SparcIntRegClass::o7));
352}
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000353
354
Ruchira Sasanka91442282001-09-30 23:16:47 +0000355
356//---------------------------------------------------------------------------
357// This method will suggest colors to incoming args to a method.
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000358// According to the Sparc ABI, the first 6 incoming args are in
359// %i0 - %i5 (if they are integer) OR in %f0 - %f31 (if they are float).
Ruchira Sasanka91442282001-09-30 23:16:47 +0000360// If the arg is passed on stack due to the lack of regs, NOTHING will be
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000361// done - it will be colored (or spilled) as a normal live range.
Ruchira Sasanka91442282001-09-30 23:16:47 +0000362//---------------------------------------------------------------------------
Chris Lattnerb7653df2002-04-08 22:03:57 +0000363void UltraSparcRegInfo::suggestRegs4MethodArgs(const Function *Meth,
Ruchira Sasanka91442282001-09-30 23:16:47 +0000364 LiveRangeInfo& LRI) const
Chris Lattner20b1ea02001-09-14 03:47:57 +0000365{
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000366 // Check if this is a varArgs function. needed for choosing regs.
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000367 bool isVarArgs = isVarArgsFunction(Meth->getType());
368
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000369 // Count the arguments, *ignoring* whether they are int or FP args.
370 // Use this common arg numbering to pick the right int or fp register.
371 unsigned argNo=0;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000372 for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend();
373 I != E; ++I, ++argNo) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000374 LiveRange *LR = LRI.getLiveRangeForValue(I);
375 assert(LR && "No live range found for method arg");
376
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000377 unsigned regType = getRegTypeForLR(LR);
378 unsigned regClassIDOfArgReg = BadRegClass; // for chosen reg (unused)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000379
380 int regNum = (regType == IntRegType)
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000381 ? regNumForIntArg(/*inCallee*/ true, isVarArgs, argNo, regClassIDOfArgReg)
382 : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs, argNo,
383 regClassIDOfArgReg);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000384
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000385 if (regNum != getInvalidRegNum())
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000386 LR->setSuggestedColor(regNum);
387 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000388}
389
Ruchira Sasanka91442282001-09-30 23:16:47 +0000390
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000391//---------------------------------------------------------------------------
392// This method is called after graph coloring to move incoming args to
393// the correct hardware registers if they did not receive the correct
394// (suggested) color through graph coloring.
395//---------------------------------------------------------------------------
Chris Lattnerb7653df2002-04-08 22:03:57 +0000396void UltraSparcRegInfo::colorMethodArgs(const Function *Meth,
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000397 LiveRangeInfo &LRI,
398 std::vector<MachineInstr*>& InstrnsBefore,
399 std::vector<MachineInstr*>& InstrnsAfter) const {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000400
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000401 // check if this is a varArgs function. needed for choosing regs.
402 bool isVarArgs = isVarArgsFunction(Meth->getType());
Ruchira Sasanka91442282001-09-30 23:16:47 +0000403 MachineInstr *AdMI;
404
Ruchira Sasanka91442282001-09-30 23:16:47 +0000405 // for each argument
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000406 // for each argument. count INT and FP arguments separately.
407 unsigned argNo=0, intArgNo=0, fpArgNo=0;
408 for(Function::const_aiterator I = Meth->abegin(), E = Meth->aend();
409 I != E; ++I, ++argNo) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000410 // get the LR of arg
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000411 LiveRange *LR = LRI.getLiveRangeForValue(I);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000412 assert( LR && "No live range found for method arg");
413
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000414 unsigned regType = getRegTypeForLR(LR);
Chris Lattner3c3c82d2003-01-15 21:14:32 +0000415 unsigned RegClassID = LR->getRegClassID();
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000416
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000417 // Find whether this argument is coming in a register (if not, on stack)
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000418 // Also find the correct register the argument must use (UniArgReg)
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000419 //
Ruchira Sasanka91442282001-09-30 23:16:47 +0000420 bool isArgInReg = false;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000421 unsigned UniArgReg = getInvalidRegNum(); // reg that LR MUST be colored with
Chris Lattner92ba2aa2003-01-14 23:05:08 +0000422 unsigned regClassIDOfArgReg = BadRegClass; // reg class of chosen reg
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000423
424 int regNum = (regType == IntRegType)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000425 ? regNumForIntArg(/*inCallee*/ true, isVarArgs,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000426 argNo, regClassIDOfArgReg)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000427 : regNumForFPArg(regType, /*inCallee*/ true, isVarArgs,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000428 argNo, regClassIDOfArgReg);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000429
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000430 if(regNum != getInvalidRegNum()) {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000431 isArgInReg = true;
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000432 UniArgReg = getUnifiedRegNum( regClassIDOfArgReg, regNum);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000433 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000434
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000435 if( ! LR->isMarkedForSpill() ) { // if this arg received a register
Ruchira Sasanka91442282001-09-30 23:16:47 +0000436
Ruchira Sasankac74a7202001-10-24 15:56:58 +0000437 unsigned UniLRReg = getUnifiedRegNum( RegClassID, LR->getColor() );
438
439 // if LR received the correct color, nothing to do
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000440 //
Ruchira Sasankac74a7202001-10-24 15:56:58 +0000441 if( UniLRReg == UniArgReg )
442 continue;
443
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000444 // We are here because the LR did not receive the suggested
445 // but LR received another register.
446 // Now we have to copy the %i reg (or stack pos of arg)
447 // to the register the LR was colored with.
Ruchira Sasankac74a7202001-10-24 15:56:58 +0000448
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000449 // if the arg is coming in UniArgReg register, it MUST go into
Ruchira Sasankac74a7202001-10-24 15:56:58 +0000450 // the UniLRReg register
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000451 //
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000452 if( isArgInReg ) {
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000453 if( regClassIDOfArgReg != RegClassID ) {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000454 assert(0 && "This could should work but it is not tested yet");
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000455
456 // It is a variable argument call: the float reg must go in a %o reg.
457 // We have to move an int reg to a float reg via memory.
458 //
459 assert(isVarArgs &&
460 RegClassID == FloatRegClassID &&
461 regClassIDOfArgReg == IntRegClassID &&
462 "This should only be an Int register for an FP argument");
463
Chris Lattner1b849be2002-12-28 20:21:29 +0000464 int TmpOff = MachineFunction::get(Meth).getInfo()->pushTempValue(
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000465 getSpilledRegSize(regType));
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000466 cpReg2MemMI(InstrnsBefore,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000467 UniArgReg, getFramePointer(), TmpOff, IntRegType);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000468
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000469 cpMem2RegMI(InstrnsBefore,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000470 getFramePointer(), TmpOff, UniLRReg, regType);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000471 }
472 else {
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000473 cpReg2RegMI(InstrnsBefore, UniArgReg, UniLRReg, regType);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000474 }
475 }
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000476 else {
Ruchira Sasanka91442282001-09-30 23:16:47 +0000477
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000478 // Now the arg is coming on stack. Since the LR received a register,
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000479 // we just have to load the arg on stack into that register
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000480 //
Chris Lattner1b849be2002-12-28 20:21:29 +0000481 const TargetFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve1c0fba62001-11-08 04:56:41 +0000482 int offsetFromFP =
Misha Brukmanfce11432002-10-28 00:28:31 +0000483 frameInfo.getIncomingArgOffset(MachineFunction::get(Meth),
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000484 argNo);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000485
486 // float arguments on stack are right justified so adjust the offset!
487 // int arguments are also right justified but they are always loaded as
488 // a full double-word so the offset does not need to be adjusted.
489 if (regType == FPSingleRegType) {
490 unsigned argSize = target.getTargetData().getTypeSize(LR->getType());
491 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
492 assert(argSize <= slotSize && "Insufficient slot size!");
493 offsetFromFP += slotSize - argSize;
494 }
495
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000496 cpMem2RegMI(InstrnsBefore,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000497 getFramePointer(), offsetFromFP, UniLRReg, regType);
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000498 }
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000499
500 } // if LR received a color
501
502 else {
503
504 // Now, the LR did not receive a color. But it has a stack offset for
505 // spilling.
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000506 // So, if the arg is coming in UniArgReg register, we can just move
507 // that on to the stack pos of LR
508
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000509 if( isArgInReg ) {
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000510
511 if( regClassIDOfArgReg != RegClassID ) {
512 assert(0 &&
513 "FP arguments to a varargs function should be explicitly "
514 "copied to/from int registers by instruction selection!");
515
516 // It must be a float arg for a variable argument call, which
517 // must come in a %o reg. Move the int reg to the stack.
518 //
519 assert(isVarArgs && regClassIDOfArgReg == IntRegClassID &&
520 "This should only be an Int register for an FP argument");
521
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000522 cpReg2MemMI(InstrnsBefore, UniArgReg,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000523 getFramePointer(), LR->getSpillOffFromFP(), IntRegType);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000524 }
525 else {
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000526 cpReg2MemMI(InstrnsBefore, UniArgReg,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000527 getFramePointer(), LR->getSpillOffFromFP(), regType);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000528 }
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000529 }
530
531 else {
532
533 // Now the arg is coming on stack. Since the LR did NOT
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000534 // received a register as well, it is allocated a stack position. We
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000535 // can simply change the stack position of the LR. We can do this,
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000536 // since this method is called before any other method that makes
537 // uses of the stack pos of the LR (e.g., updateMachineInstr)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000538 //
Chris Lattner1b849be2002-12-28 20:21:29 +0000539 const TargetFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve1c0fba62001-11-08 04:56:41 +0000540 int offsetFromFP =
Misha Brukmanfce11432002-10-28 00:28:31 +0000541 frameInfo.getIncomingArgOffset(MachineFunction::get(Meth),
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000542 argNo);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000543
544 // FP arguments on stack are right justified so adjust offset!
545 // int arguments are also right justified but they are always loaded as
546 // a full double-word so the offset does not need to be adjusted.
547 if (regType == FPSingleRegType) {
548 unsigned argSize = target.getTargetData().getTypeSize(LR->getType());
549 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
550 assert(argSize <= slotSize && "Insufficient slot size!");
551 offsetFromFP += slotSize - argSize;
552 }
Vikram S. Adve1c0fba62001-11-08 04:56:41 +0000553
554 LR->modifySpillOffFromFP( offsetFromFP );
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000555 }
Ruchira Sasanka91442282001-09-30 23:16:47 +0000556
557 }
558
Ruchira Sasanka91442282001-09-30 23:16:47 +0000559 } // for each incoming argument
560
561}
562
Chris Lattner20b1ea02001-09-14 03:47:57 +0000563
564
Ruchira Sasanka91442282001-09-30 23:16:47 +0000565//---------------------------------------------------------------------------
566// This method is called before graph coloring to suggest colors to the
567// outgoing call args and the return value of the call.
568//---------------------------------------------------------------------------
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000569void UltraSparcRegInfo::suggestRegs4CallArgs(MachineInstr *CallMI,
Vikram S. Adve87817652002-09-28 16:59:05 +0000570 LiveRangeInfo& LRI) const {
Vikram S. Advec654c782002-10-13 00:05:30 +0000571 assert ( (target.getInstrInfo()).isCall(CallMI->getOpCode()) );
Chris Lattner20b1ea02001-09-14 03:47:57 +0000572
Vikram S. Adve242a8082002-05-19 15:25:51 +0000573 CallArgsDescriptor* argDesc = CallArgsDescriptor::get(CallMI);
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000574
Vikram S. Adve87817652002-09-28 16:59:05 +0000575 suggestReg4CallAddr(CallMI, LRI);
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000576
Vikram S. Adve87817652002-09-28 16:59:05 +0000577 // First color the return value of the call instruction, if any.
578 // The return value will be in %o0 if the value is an integer type,
579 // or in %f0 if the value is a float type.
580 //
581 if (const Value *RetVal = argDesc->getReturnValue()) {
582 LiveRange *RetValLR = LRI.getLiveRangeForValue(RetVal);
583 assert(RetValLR && "No LR for return Value of call!");
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000584
Chris Lattner3c3c82d2003-01-15 21:14:32 +0000585 unsigned RegClassID = RetValLR->getRegClassID();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000586
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000587 // now suggest a register depending on the register class of ret arg
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000588 if( RegClassID == IntRegClassID )
Chris Lattner95685682002-08-12 21:25:05 +0000589 RetValLR->setSuggestedColor(SparcIntRegClass::o0);
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000590 else if (RegClassID == FloatRegClassID )
Chris Lattner95685682002-08-12 21:25:05 +0000591 RetValLR->setSuggestedColor(SparcFloatRegClass::f0 );
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000592 else assert( 0 && "Unknown reg class for return value of call\n");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000593 }
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000594
Ruchira Sasanka91442282001-09-30 23:16:47 +0000595 // Now suggest colors for arguments (operands) of the call instruction.
596 // Colors are suggested only if the arg number is smaller than the
597 // the number of registers allocated for argument passing.
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000598 // Now, go thru call args - implicit operands of the call MI
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000599
Vikram S. Adve242a8082002-05-19 15:25:51 +0000600 unsigned NumOfCallArgs = argDesc->getNumArgs();
Ruchira Sasanka91442282001-09-30 23:16:47 +0000601
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000602 for(unsigned argNo=0, i=0, intArgNo=0, fpArgNo=0;
603 i < NumOfCallArgs; ++i, ++argNo) {
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000604
Vikram S. Adve242a8082002-05-19 15:25:51 +0000605 const Value *CallArg = argDesc->getArgInfo(i).getArgVal();
Ruchira Sasanka91442282001-09-30 23:16:47 +0000606
607 // get the LR of call operand (parameter)
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000608 LiveRange *const LR = LRI.getLiveRangeForValue(CallArg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000609 if (!LR)
610 continue; // no live ranges for constants and labels
Vikram S. Adve87817652002-09-28 16:59:05 +0000611
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000612 unsigned regType = getRegTypeForLR(LR);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000613 unsigned regClassIDOfArgReg = BadRegClass; // chosen reg class (unused)
Vikram S. Adve87817652002-09-28 16:59:05 +0000614
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000615 // Choose a register for this arg depending on whether it is
Vikram S. Adve242a8082002-05-19 15:25:51 +0000616 // an INT or FP value. Here we ignore whether or not it is a
617 // varargs calls, because FP arguments will be explicitly copied
618 // to an integer Value and handled under (argCopy != NULL) below.
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000619 int regNum = (regType == IntRegType)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 ? regNumForIntArg(/*inCallee*/ false, /*isVarArgs*/ false,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000621 argNo, regClassIDOfArgReg)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 : regNumForFPArg(regType, /*inCallee*/ false, /*isVarArgs*/ false,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000623 argNo, regClassIDOfArgReg);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000624
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000625 // If a register could be allocated, use it.
626 // If not, do NOTHING as this will be colored as a normal value.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000627 if(regNum != getInvalidRegNum())
Vikram S. Adve31f78c42002-04-25 04:42:21 +0000628 LR->setSuggestedColor(regNum);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000629 } // for all call arguments
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630}
631
632
Ruchira Sasanka91442282001-09-30 23:16:47 +0000633//---------------------------------------------------------------------------
Anand Shukla55afc332003-06-01 02:48:23 +0000634// this method is called for an LLVM return instruction to identify which
Ruchira Sasanka91442282001-09-30 23:16:47 +0000635// values will be returned from this method and to suggest colors.
636//---------------------------------------------------------------------------
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000637void UltraSparcRegInfo::suggestReg4RetValue(MachineInstr *RetMI,
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000638 LiveRangeInfo& LRI) const {
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000639
Vikram S. Advec654c782002-10-13 00:05:30 +0000640 assert( (target.getInstrInfo()).isReturn( RetMI->getOpCode() ) );
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000641
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000642 suggestReg4RetAddr(RetMI, LRI);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000643
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000644 // To find the return value (if any), we can get the LLVM return instr.
645 // from the return address register, which is the first operand
646 Value* tmpI = RetMI->getOperand(0).getVRegValue();
647 ReturnInst* retI=cast<ReturnInst>(cast<TmpInstruction>(tmpI)->getOperand(0));
648 if (const Value *RetVal = retI->getReturnValue())
649 if (LiveRange *const LR = LRI.getLiveRangeForValue(RetVal))
650 LR->setSuggestedColor(LR->getRegClassID() == IntRegClassID
651 ? (unsigned) SparcIntRegClass::i0
652 : (unsigned) SparcFloatRegClass::f0);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000653}
654
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000655//---------------------------------------------------------------------------
656// Check if a specified register type needs a scratch register to be
657// copied to/from memory. If it does, the reg. type that must be used
658// for scratch registers is returned in scratchRegType.
659//
660// Only the int CC register needs such a scratch register.
661// The FP CC registers can (and must) be copied directly to/from memory.
662//---------------------------------------------------------------------------
663
664bool
665UltraSparcRegInfo::regTypeNeedsScratchReg(int RegType,
666 int& scratchRegType) const
667{
668 if (RegType == IntCCRegType)
669 {
670 scratchRegType = IntRegType;
671 return true;
672 }
673 return false;
674}
Ruchira Sasanka91442282001-09-30 23:16:47 +0000675
676//---------------------------------------------------------------------------
677// Copy from a register to register. Register number must be the unified
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000678// register number.
Ruchira Sasanka91442282001-09-30 23:16:47 +0000679//---------------------------------------------------------------------------
680
Vikram S. Adve242a8082002-05-19 15:25:51 +0000681void
Misha Brukmanee563cb2003-05-21 17:59:06 +0000682UltraSparcRegInfo::cpReg2RegMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000683 unsigned SrcReg,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000684 unsigned DestReg,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000685 int RegType) const {
Misha Brukmand36e30e2003-06-06 09:52:23 +0000686 assert( ((int)SrcReg != getInvalidRegNum()) &&
687 ((int)DestReg != getInvalidRegNum()) &&
Ruchira Sasanka91442282001-09-30 23:16:47 +0000688 "Invalid Register");
689
690 MachineInstr * MI = NULL;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691
Ruchira Sasanka91442282001-09-30 23:16:47 +0000692 switch( RegType ) {
693
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000694 case IntCCRegType:
Misha Brukmana98cd452003-05-20 20:32:24 +0000695 if (getRegType(DestReg) == IntRegType) {
696 // copy intCC reg to int reg
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000697 MI = (BuildMI(V9::RDCCR, 2)
698 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
699 SparcIntCCRegClass::ccr))
700 .addMReg(DestReg,MOTy::Def));
Misha Brukmana98cd452003-05-20 20:32:24 +0000701 } else {
702 // copy int reg to intCC reg
Misha Brukmana98cd452003-05-20 20:32:24 +0000703 assert(getRegType(SrcReg) == IntRegType
704 && "Can only copy CC reg to/from integer reg");
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000705 MI = (BuildMI(V9::WRCCRr, 3)
706 .addMReg(SrcReg)
707 .addMReg(SparcIntRegClass::g0)
708 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
709 SparcIntCCRegClass::ccr), MOTy::Def));
Misha Brukmana98cd452003-05-20 20:32:24 +0000710 }
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000711 break;
712
Ruchira Sasanka735d6e32001-10-18 22:38:52 +0000713 case FloatCCRegType:
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000714 assert(0 && "Cannot copy FPCC register to any other register");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000715 break;
716
717 case IntRegType:
Misha Brukmanaf6f38e2003-05-27 22:40:34 +0000718 MI = BuildMI(V9::ADDr, 3).addMReg(SrcReg).addMReg(getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000719 .addMReg(DestReg, MOTy::Def);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000720 break;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000721
Ruchira Sasanka91442282001-09-30 23:16:47 +0000722 case FPSingleRegType:
Misha Brukmana98cd452003-05-20 20:32:24 +0000723 MI = BuildMI(V9::FMOVS, 2).addMReg(SrcReg).addMReg(DestReg, MOTy::Def);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000724 break;
725
726 case FPDoubleRegType:
Misha Brukmana98cd452003-05-20 20:32:24 +0000727 MI = BuildMI(V9::FMOVD, 2).addMReg(SrcReg).addMReg(DestReg, MOTy::Def);
Ruchira Sasanka91442282001-09-30 23:16:47 +0000728 break;
729
730 default:
Vikram S. Adve242a8082002-05-19 15:25:51 +0000731 assert(0 && "Unknown RegType");
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000732 break;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000733 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000734
735 if (MI)
736 mvec.push_back(MI);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000737}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000738
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000739//---------------------------------------------------------------------------
Ruchira Sasanka7dcd6122001-10-24 22:05:34 +0000740// Copy from a register to memory (i.e., Store). Register number must
741// be the unified register number
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000742//---------------------------------------------------------------------------
743
744
Vikram S. Adve242a8082002-05-19 15:25:51 +0000745void
Misha Brukmanee563cb2003-05-21 17:59:06 +0000746UltraSparcRegInfo::cpReg2MemMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000747 unsigned SrcReg,
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000748 unsigned PtrReg,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000749 int Offset, int RegType,
Chris Lattnerb82d97e2002-07-25 06:08:32 +0000750 int scratchReg) const {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000751 MachineInstr * MI = NULL;
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000752 int OffReg = -1;
753
754 // If the Offset will not fit in the signed-immediate field, find an
755 // unused register to hold the offset value. This takes advantage of
756 // the fact that all the opcodes used below have the same size immed. field.
757 // Use the register allocator, PRA, to find an unused reg. at this MI.
758 //
759 if (RegType != IntCCRegType) // does not use offset below
760 if (! target.getInstrInfo().constantFitsInImmedField(V9::LDXi, Offset)) {
761#ifdef CAN_FIND_FREE_REGISTER_TRANSPARENTLY
762 RegClass* RC = PRA.getRegClassByID(this->getRegClassIDOfRegType(RegType));
763 OffReg = PRA.getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
764#else
Brian Gaeke641271d2003-11-08 18:12:24 +0000765 // Default to using register g4 for holding large offsets
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000766 OffReg = getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
767 SparcIntRegClass::g4);
768#endif
769 assert(OffReg >= 0 && "FIXME: cpReg2MemMI cannot find an unused reg.");
770 mvec.push_back(BuildMI(V9::SETSW, 2).addZImm(Offset).addReg(OffReg));
771 }
772
Chris Lattner00dca912003-01-15 17:47:49 +0000773 switch (RegType) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000774 case IntRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000775 if (target.getInstrInfo().constantFitsInImmedField(V9::STXi, Offset))
776 MI = BuildMI(V9::STXi,3).addMReg(SrcReg).addMReg(PtrReg).addSImm(Offset);
777 else
778 MI = BuildMI(V9::STXr,3).addMReg(SrcReg).addMReg(PtrReg).addMReg(OffReg);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000779 break;
780
781 case FPSingleRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000782 if (target.getInstrInfo().constantFitsInImmedField(V9::STFi, Offset))
783 MI = BuildMI(V9::STFi, 3).addMReg(SrcReg).addMReg(PtrReg).addSImm(Offset);
784 else
785 MI = BuildMI(V9::STFr, 3).addMReg(SrcReg).addMReg(PtrReg).addMReg(OffReg);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000786 break;
787
788 case FPDoubleRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000789 if (target.getInstrInfo().constantFitsInImmedField(V9::STDFi, Offset))
790 MI = BuildMI(V9::STDFi,3).addMReg(SrcReg).addMReg(PtrReg).addSImm(Offset);
791 else
792 MI = BuildMI(V9::STDFr,3).addMReg(SrcReg).addMReg(PtrReg).addSImm(OffReg);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000793 break;
794
Ruchira Sasanka3839e6e2001-11-03 19:59:59 +0000795 case IntCCRegType:
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000796 assert(scratchReg >= 0 && "Need scratch reg to store %ccr to memory");
Chris Lattner95685682002-08-12 21:25:05 +0000797 assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg");
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000798 MI = (BuildMI(V9::RDCCR, 2)
799 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
800 SparcIntCCRegClass::ccr))
Vikram S. Adve786833a2003-07-06 20:13:59 +0000801 .addMReg(scratchReg, MOTy::Def));
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000802 mvec.push_back(MI);
803
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000804 cpReg2MemMI(mvec, scratchReg, PtrReg, Offset, IntRegType);
Chris Lattner00dca912003-01-15 17:47:49 +0000805 return;
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000806
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000807 case FloatCCRegType: {
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000808 unsigned fsrReg = getUnifiedRegNum(UltraSparcRegInfo::SpecialRegClassID,
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000809 SparcSpecialRegClass::fsr);
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000810 if (target.getInstrInfo().constantFitsInImmedField(V9::STXFSRi, Offset))
811 MI=BuildMI(V9::STXFSRi,3).addMReg(fsrReg).addMReg(PtrReg).addSImm(Offset);
812 else
813 MI=BuildMI(V9::STXFSRr,3).addMReg(fsrReg).addMReg(PtrReg).addMReg(OffReg);
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000814 break;
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000815 }
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000816 default:
Vikram S. Adve242a8082002-05-19 15:25:51 +0000817 assert(0 && "Unknown RegType in cpReg2MemMI");
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000818 }
Chris Lattner00dca912003-01-15 17:47:49 +0000819 mvec.push_back(MI);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000820}
821
822
823//---------------------------------------------------------------------------
Ruchira Sasanka7dcd6122001-10-24 22:05:34 +0000824// Copy from memory to a reg (i.e., Load) Register number must be the unified
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000825// register number
826//---------------------------------------------------------------------------
827
828
Vikram S. Adve242a8082002-05-19 15:25:51 +0000829void
Misha Brukmanee563cb2003-05-21 17:59:06 +0000830UltraSparcRegInfo::cpMem2RegMI(std::vector<MachineInstr*>& mvec,
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000831 unsigned PtrReg,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000832 int Offset,
833 unsigned DestReg,
834 int RegType,
Chris Lattnerb82d97e2002-07-25 06:08:32 +0000835 int scratchReg) const {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000836 MachineInstr * MI = NULL;
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000837 int OffReg = -1;
838
839 // If the Offset will not fit in the signed-immediate field, find an
840 // unused register to hold the offset value. This takes advantage of
841 // the fact that all the opcodes used below have the same size immed. field.
842 // Use the register allocator, PRA, to find an unused reg. at this MI.
843 //
844 if (RegType != IntCCRegType) // does not use offset below
845 if (! target.getInstrInfo().constantFitsInImmedField(V9::LDXi, Offset)) {
846#ifdef CAN_FIND_FREE_REGISTER_TRANSPARENTLY
847 RegClass* RC = PRA.getRegClassByID(this->getRegClassIDOfRegType(RegType));
848 OffReg = PRA.getUnusedUniRegAtMI(RC, RegType, MInst, LVSetBef);
849#else
Brian Gaeke641271d2003-11-08 18:12:24 +0000850 // Default to using register g4 for holding large offsets
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000851 OffReg = getUnifiedRegNum(UltraSparcRegInfo::IntRegClassID,
852 SparcIntRegClass::g4);
853#endif
854 assert(OffReg >= 0 && "FIXME: cpReg2MemMI cannot find an unused reg.");
855 mvec.push_back(BuildMI(V9::SETSW, 2).addZImm(Offset).addReg(OffReg));
856 }
857
Chris Lattner699683c2002-02-04 05:59:25 +0000858 switch (RegType) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000859 case IntRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000860 if (target.getInstrInfo().constantFitsInImmedField(V9::LDXi, Offset))
861 MI = BuildMI(V9::LDXi, 3).addMReg(PtrReg).addSImm(Offset).addMReg(DestReg,
862 MOTy::Def);
863 else
864 MI = BuildMI(V9::LDXr, 3).addMReg(PtrReg).addMReg(OffReg).addMReg(DestReg,
865 MOTy::Def);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000866 break;
867
868 case FPSingleRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000869 if (target.getInstrInfo().constantFitsInImmedField(V9::LDFi, Offset))
870 MI = BuildMI(V9::LDFi, 3).addMReg(PtrReg).addSImm(Offset).addMReg(DestReg,
871 MOTy::Def);
872 else
873 MI = BuildMI(V9::LDFr, 3).addMReg(PtrReg).addMReg(OffReg).addMReg(DestReg,
874 MOTy::Def);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000875 break;
876
877 case FPDoubleRegType:
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000878 if (target.getInstrInfo().constantFitsInImmedField(V9::LDDFi, Offset))
879 MI= BuildMI(V9::LDDFi, 3).addMReg(PtrReg).addSImm(Offset).addMReg(DestReg,
880 MOTy::Def);
881 else
882 MI= BuildMI(V9::LDDFr, 3).addMReg(PtrReg).addMReg(OffReg).addMReg(DestReg,
883 MOTy::Def);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000884 break;
885
Ruchira Sasanka3839e6e2001-11-03 19:59:59 +0000886 case IntCCRegType:
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000887 assert(scratchReg >= 0 && "Need scratch reg to load %ccr from memory");
Chris Lattner95685682002-08-12 21:25:05 +0000888 assert(getRegType(scratchReg) ==IntRegType && "Invalid scratch reg");
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000889 cpMem2RegMI(mvec, PtrReg, Offset, scratchReg, IntRegType);
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000890 MI = (BuildMI(V9::WRCCRr, 3)
891 .addMReg(scratchReg)
892 .addMReg(SparcIntRegClass::g0)
893 .addMReg(getUnifiedRegNum(UltraSparcRegInfo::IntCCRegClassID,
894 SparcIntCCRegClass::ccr), MOTy::Def));
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000895 break;
896
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000897 case FloatCCRegType: {
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000898 unsigned fsrRegNum = getUnifiedRegNum(UltraSparcRegInfo::SpecialRegClassID,
899 SparcSpecialRegClass::fsr);
Vikram S. Adve83d30c82003-07-29 19:53:21 +0000900 if (target.getInstrInfo().constantFitsInImmedField(V9::LDXFSRi, Offset))
901 MI = BuildMI(V9::LDXFSRi, 3).addMReg(PtrReg).addSImm(Offset)
902 .addMReg(fsrRegNum, MOTy::UseAndDef);
903 else
904 MI = BuildMI(V9::LDXFSRr, 3).addMReg(PtrReg).addMReg(OffReg)
905 .addMReg(fsrRegNum, MOTy::UseAndDef);
Vikram S. Adve76ee6f72002-07-08 23:23:12 +0000906 break;
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000907 }
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000908 default:
Ruchira Sasankaae4bcd72001-11-10 21:20:43 +0000909 assert(0 && "Unknown RegType in cpMem2RegMI");
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000910 }
Chris Lattner00dca912003-01-15 17:47:49 +0000911 mvec.push_back(MI);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000912}
913
914
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000915//---------------------------------------------------------------------------
916// Generate a copy instruction to copy a value to another. Temporarily
917// used by PhiElimination code.
918//---------------------------------------------------------------------------
919
920
Vikram S. Adve242a8082002-05-19 15:25:51 +0000921void
Chris Lattner00dca912003-01-15 17:47:49 +0000922UltraSparcRegInfo::cpValue2Value(Value *Src, Value *Dest,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000923 std::vector<MachineInstr*>& mvec) const {
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000924 int RegType = getRegTypeForDataType(Src->getType());
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000925 MachineInstr * MI = NULL;
926
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000927 switch( RegType ) {
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000928 case IntRegType:
Misha Brukmanaf6f38e2003-05-27 22:40:34 +0000929 MI = BuildMI(V9::ADDr, 3).addReg(Src).addMReg(getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000930 .addRegDef(Dest);
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000931 break;
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000932 case FPSingleRegType:
Misha Brukmana98cd452003-05-20 20:32:24 +0000933 MI = BuildMI(V9::FMOVS, 2).addReg(Src).addRegDef(Dest);
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000934 break;
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000935 case FPDoubleRegType:
Misha Brukmana98cd452003-05-20 20:32:24 +0000936 MI = BuildMI(V9::FMOVD, 2).addReg(Src).addRegDef(Dest);
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000937 break;
Ruchira Sasanka67a463a2001-11-12 14:45:33 +0000938 default:
939 assert(0 && "Unknow RegType in CpValu2Value");
940 }
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000941
Chris Lattner0fa600d2002-10-28 20:10:56 +0000942 mvec.push_back(MI);
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000943}
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000944
945
946
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000947//---------------------------------------------------------------------------
948// Print the register assigned to a LR
949//---------------------------------------------------------------------------
950
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000951void UltraSparcRegInfo::printReg(const LiveRange *LR) const {
Chris Lattner3c3c82d2003-01-15 21:14:32 +0000952 unsigned RegClassID = LR->getRegClassID();
Chris Lattnerfdba3932003-09-01 19:58:02 +0000953 std::cerr << " Node ";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000954
Chris Lattner699683c2002-02-04 05:59:25 +0000955 if (!LR->hasColor()) {
Misha Brukmanee563cb2003-05-21 17:59:06 +0000956 std::cerr << " - could not find a color\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000957 return;
958 }
959
960 // if a color is found
961
Misha Brukmanee563cb2003-05-21 17:59:06 +0000962 std::cerr << " colored with color "<< LR->getColor();
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000963
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000964 unsigned uRegName = getUnifiedRegNum(RegClassID, LR->getColor());
965
966 std::cerr << "[";
967 std::cerr<< getUnifiedRegName(uRegName);
968 if (RegClassID == FloatRegClassID && LR->getType() == Type::DoubleTy)
969 std::cerr << "+" << getUnifiedRegName(uRegName+1);
970 std::cerr << "]\n";
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000971}
Brian Gaeked0fde302003-11-11 22:41:34 +0000972
973} // End llvm namespace