blob: a44dc7fbaf5b146a38d0692e8261edadc58d8776 [file] [log] [blame]
Vikram S. Adve30764b82001-10-18 00:01:48 +00001// $Id$
2//***************************************************************************
3// File:
4// SparcInstrInfo.cpp
5//
6// Purpose:
7//
8// History:
9// 10/15/01 - Vikram Adve - Created
10//**************************************************************************/
11
12
13#include "SparcInternals.h"
14#include "SparcInstrSelectionSupport.h"
15#include "llvm/Target/Sparc.h"
16#include "llvm/CodeGen/InstrSelection.h"
17#include "llvm/CodeGen/InstrSelectionSupport.h"
18#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercb0a1202002-02-03 07:49:49 +000019#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000020#include "llvm/Method.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000021#include "llvm/ConstantVals.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000023
24
25//************************ Internal Functions ******************************/
26
27
28static inline MachineInstr*
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000029CreateIntSetInstruction(int64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000030 std::vector<TmpInstruction*>& tempVec)
Vikram S. Adve30764b82001-10-18 00:01:48 +000031{
32 MachineInstr* minstr;
Vikram S. Advea2a70942001-10-28 21:41:46 +000033 uint64_t absC = (C >= 0)? C : -C;
34 if (absC > (unsigned int) ~0)
35 { // C does not fit in 32 bits
Chris Lattnercb0a1202002-02-03 07:49:49 +000036 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Advea2a70942001-10-28 21:41:46 +000037 tempVec.push_back(tmpReg);
38
39 minstr = new MachineInstr(SETX);
Vikram S. Advee76af292002-03-18 03:09:15 +000040 minstr->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
41 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpReg,
Vikram S. Advea2a70942001-10-28 21:41:46 +000042 /*isdef*/ true);
Vikram S. Advee76af292002-03-18 03:09:15 +000043 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Advea2a70942001-10-28 21:41:46 +000044 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000045 else
Vikram S. Adve30764b82001-10-18 00:01:48 +000046 {
47 minstr = new MachineInstr(SETSW);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000048 minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
49 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000050 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000051
52 return minstr;
53}
54
55static inline MachineInstr*
56CreateUIntSetInstruction(uint64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000057 std::vector<TmpInstruction*>& tempVec)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000058{
59 MachineInstr* minstr;
60 if (C > (unsigned int) ~0)
61 { // C does not fit in 32 bits
Vikram S. Advef7cedec2002-03-31 00:13:12 +000062 assert(dest->getType() == Type::ULongTy && "Sign extension problems");
Chris Lattnercb0a1202002-02-03 07:49:49 +000063 TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000064 tempVec.push_back(tmpReg);
65
66 minstr = new MachineInstr(SETX);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000067 minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
68 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
69 tmpReg, /*isdef*/ true);
Vikram S. Advee76af292002-03-18 03:09:15 +000070 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000071 }
Vikram S. Advef7cedec2002-03-31 00:13:12 +000072 else if (dest->getType() == Type::ULongTy)
Vikram S. Adve30764b82001-10-18 00:01:48 +000073 {
74 minstr = new MachineInstr(SETUW);
Vikram S. Advee76af292002-03-18 03:09:15 +000075 minstr->SetMachineOperandConst(0, MachineOperand::MO_UnextendedImmed, C);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000076 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
77 }
78 else
79 { // cast to signed type of the right length and use signed op (SETSW)
80 // to get correct sign extension
81 //
82 minstr = new MachineInstr(SETSW);
83 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
84
85 switch (dest->getType()->getPrimitiveID())
86 {
87 case Type::UIntTyID:
88 minstr->SetMachineOperandConst(0,
89 MachineOperand::MO_SignExtendedImmed,
90 (int) C);
91 break;
92 case Type::UShortTyID:
93 minstr->SetMachineOperandConst(0,
94 MachineOperand::MO_SignExtendedImmed,
95 (short) C);
96 break;
97 case Type::UByteTyID:
98 minstr->SetMachineOperandConst(0,
99 MachineOperand::MO_SignExtendedImmed,
100 (char) C);
101 break;
102 default:
103 assert(0 && "Unexpected unsigned type");
104 break;
105 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000106 }
107
Vikram S. Adve30764b82001-10-18 00:01:48 +0000108 return minstr;
109}
110
111//************************* External Classes *******************************/
112
113//---------------------------------------------------------------------------
114// class UltraSparcInstrInfo
115//
116// Purpose:
117// Information about individual instructions.
118// Most information is stored in the SparcMachineInstrDesc array above.
119// Other information is computed on demand, and most such functions
120// default to member functions in base class MachineInstrInfo.
121//---------------------------------------------------------------------------
122
123/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000124UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
125 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000126 /*descSize = */ NUM_TOTAL_OPCODES,
127 /*numRealOpCodes = */ NUM_REAL_OPCODES)
128{
129}
130
Vikram S. Advee76af292002-03-18 03:09:15 +0000131//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000132// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000133// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000134// GlobalValue, viz., the constant address of a global variable or function.
135// The generated instructions are returned in `minstrVec'.
136// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
137//
138void
Vikram S. Advee76af292002-03-18 03:09:15 +0000139UltraSparcInstrInfo::CreateCodeToLoadConst(Method* method,
140 Value* val,
141 Instruction* dest,
142 std::vector<MachineInstr*>& minstrVec,
143 std::vector<TmpInstruction*>& tempVec) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000144{
145 MachineInstr* minstr;
146
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000147 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000148 "I only know about constant values and global addresses");
149
150 // Use a "set" instruction for known constants that can go in an integer reg.
151 // Use a "load" instruction for all other constants, in particular,
152 // floating point constants and addresses of globals.
153 //
154 const Type* valType = val->getType();
155
156 if (valType->isIntegral() || valType == Type::BoolTy)
157 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000158 if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
159 {
160 uint64_t C = uval->getValue();
161 minstr = CreateUIntSetInstruction(C, dest, tempVec);
162 }
163 else
164 {
165 bool isValidConstant;
166 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
167 assert(isValidConstant && "Unrecognized constant");
168 minstr = CreateIntSetInstruction(C, dest, tempVec);
169 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000170 minstrVec.push_back(minstr);
171 }
172 else
173 {
174 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000175 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000176 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Advea2a70942001-10-28 21:41:46 +0000177 // Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
Vikram S. Adve30764b82001-10-18 00:01:48 +0000178 // itself a constant address. Otherwise, both are needed.
179
180 Value* addrVal;
181 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
182
Vikram S. Advea2a70942001-10-28 21:41:46 +0000183 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000184 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000185 tempVec.push_back(tmpReg);
186
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000187 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000188 {
189 // Create another TmpInstruction for the hidden integer register
190 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000191 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000192 tempVec.push_back(addrReg);
193 addrVal = addrReg;
194 }
195 else
196 addrVal = dest;
197
Vikram S. Advea2a70942001-10-28 21:41:46 +0000198 minstr = new MachineInstr(SETX);
Vikram S. Advee76af292002-03-18 03:09:15 +0000199 minstr->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp, val);
200 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpReg,
Vikram S. Advea2a70942001-10-28 21:41:46 +0000201 /*isdef*/ true);
Vikram S. Advee76af292002-03-18 03:09:15 +0000202 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,addrVal);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000203 minstrVec.push_back(minstr);
204
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000205 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000206 {
Vikram S. Advee76af292002-03-18 03:09:15 +0000207 // Make sure constant is emitted to constant pool in assembly code.
208 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
209 mcinfo.addToConstantPool(cast<Constant>(val));
210
211 // Generate the load instruction
Vikram S. Adve30764b82001-10-18 00:01:48 +0000212 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000213 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000214 addrVal);
Vikram S. Advee76af292002-03-18 03:09:15 +0000215 minstr->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000216 zeroOffset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000217 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000218 dest);
219 minstrVec.push_back(minstr);
220 }
221 }
222}
223
224
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000225// Create an instruction sequence to copy an integer value `val'
226// to a floating point value `dest' by copying to memory and back.
227// val must be an integral type. dest must be a Float or Double.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000228// The generated instructions are returned in `minstrVec'.
229// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
230//
231void
232UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000233 Value* val,
234 Instruction* dest,
235 std::vector<MachineInstr*>& minstrVec,
236 std::vector<TmpInstruction*>& tempVec,
237 TargetMachine& target) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000238{
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000239 assert((val->getType()->isIntegral() || val->getType()->isPointerType())
240 && "Source type must be integral");
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000241 assert((dest->getType() ==Type::FloatTy || dest->getType() ==Type::DoubleTy)
242 && "Dest type must be float/double");
243
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000244 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
245 int offset = mcinfo.allocateLocalVar(target, val);
246
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000247 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000248 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000249 // they use integer and float types that accomodate the
250 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000251 // On SparcV9: int for float, long for double.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000252 //
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000253 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
254 : Type::LongTy;
255 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000256 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
257 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
258 store->SetMachineOperandConst(2, MachineOperand::MO_SignExtendedImmed, offset);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000259 minstrVec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000260
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000261 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000262 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000263 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000264 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
265 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
266 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000267 minstrVec.push_back(load);
268}
269
270
271// Similarly, create an instruction sequence to copy an FP value
272// `val' to an integer value `dest' by copying to memory and back.
273// See the previous function for information about return values.
274//
275void
276UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000277 Value* val,
278 Instruction* dest,
279 std::vector<MachineInstr*>& minstrVec,
280 std::vector<TmpInstruction*>& tempVec,
281 TargetMachine& target) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000282{
283 assert((val->getType() ==Type::FloatTy || val->getType() ==Type::DoubleTy)
284 && "Source type must be float/double");
285 assert((dest->getType()->isIntegral() || dest->getType()->isPointerType())
286 && "Dest type must be integral");
287
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000288 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
289 int offset = mcinfo.allocateLocalVar(target, val);
290
291 // Store instruction stores `val' to [%fp+offset].
292 // The store and load opCodes are based on the value being copied, and
293 // they use the integer type that matches the source type in size:
294 // On SparcV9: int for float, long for double.
295 //
296 Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
297 : Type::LongTy;
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000298 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000299 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
300 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
301 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000302 minstrVec.push_back(store);
303
304 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000305 //
306 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000307 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
308 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, offset);
309 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000310 minstrVec.push_back(load);
311}