blob: bf522d91067682ba8634bc7bba56cc3f1558638f [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrInfo.cpp ------------------------------------------------===//
2//
3//===----------------------------------------------------------------------===//
Vikram S. Adve30764b82001-10-18 00:01:48 +00004
5#include "SparcInternals.h"
6#include "SparcInstrSelectionSupport.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +00007#include "llvm/CodeGen/InstrSelection.h"
8#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnercb0a1202002-02-03 07:49:49 +00009#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000010#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000011#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000012#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000013#include "llvm/DerivedTypes.h"
Anand Shuklacfb22d32002-06-25 20:55:50 +000014using std::vector;
Vikram S. Adve30764b82001-10-18 00:01:48 +000015
Vikram S. Adve53fd4002002-07-10 21:39:50 +000016static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
17static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
18
19
20// Set a 32-bit unsigned constant in the register `dest'.
21//
22static inline void
23CreateSETUWConst(const TargetMachine& target, uint32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000024 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000025{
26 MachineInstr *miSETHI = NULL, *miOR = NULL;
27
28 // In order to get efficient code, we should not generate the SETHI if
29 // all high bits are 1 (i.e., this is a small signed value that fits in
30 // the simm13 field of OR). So we check for and handle that case specially.
31 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
32 // In fact, sC == -sC, so we have to check for this explicitly.
33 int32_t sC = (int32_t) C;
34 bool smallSignedValue = sC < 0 && sC != -sC && -sC < (int32_t) MAXSIMM;
35
36 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
37 if (!smallSignedValue && (C & ~MAXLO) && C > MAXSIMM)
38 {
39 miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest);
40 miSETHI->setOperandHi32(0);
41 mvec.push_back(miSETHI);
42 }
43
44 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
45 // was generated, or if the low 10 bits are non-zero.
46 if (miSETHI==NULL || C & MAXLO)
47 {
48 if (miSETHI)
49 { // unsigned value with high-order bits set using SETHI
50 miOR = Create3OperandInstr_UImmed(OR, dest, C, dest);
51 miOR->setOperandLo32(1);
52 }
53 else
54 { // unsigned or small signed value that fits in simm13 field of OR
55 assert(smallSignedValue || (C & ~MAXSIMM) == 0);
56 miOR = new MachineInstr(OR);
57 miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
58 miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
59 sC);
60 miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest);
61 }
62 mvec.push_back(miOR);
63 }
64
65 assert((miSETHI || miOR) && "Oops, no code was generated!");
66}
67
68// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
69// Not needed for SPARC v9 but useful to make the two SETX functions similar
70static inline void
71CreateSETUWLabel(const TargetMachine& target, Value* val,
Chris Lattner035dfbe2002-08-09 20:08:06 +000072 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000073{
74 MachineInstr* MI;
75
76 // Set the high 22 bits in dest
77 MI = Create2OperandInstr(SETHI, val, dest);
78 MI->setOperandHi32(0);
79 mvec.push_back(MI);
80
81 // Set the low 10 bits in dest
82 MI = Create3OperandInstr(OR, dest, val, dest);
83 MI->setOperandLo32(1);
84 mvec.push_back(MI);
85}
86
87
88// Set a 32-bit signed constant in the register `dest',
89// with sign-extension to 64 bits.
90static inline void
91CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000092 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000093{
94 MachineInstr* MI;
95
96 // Set the low 32 bits of dest
97 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
98
99 // Sign-extend to the high 32 bits if needed
100 if (C < 0 && (-C) > (int32_t) MAXSIMM)
101 {
102 MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest);
103 mvec.push_back(MI);
104 }
105}
106
107
108// Set a 64-bit signed or unsigned constant in the register `dest'.
109static inline void
110CreateSETXConst(const TargetMachine& target, uint64_t C,
111 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000112 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000113{
114 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
115
116 MachineInstr* MI;
117
118 // Code to set the upper 32 bits of the value in register `tmpReg'
119 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
120
121 // Shift tmpReg left by 32 bits
122 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
123 mvec.push_back(MI);
124
125 // Code to set the low 32 bits of the value in register `dest'
126 CreateSETUWConst(target, C, dest, mvec);
127
128 // dest = OR(tmpReg, dest)
129 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
130 mvec.push_back(MI);
131}
132
133
134// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
135static inline void
136CreateSETXLabel(const TargetMachine& target,
137 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000138 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000139{
140 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
141 "I only know about constant values and global addresses");
142
143 MachineInstr* MI;
144
145 MI = Create2OperandInstr_Addr(SETHI, val, tmpReg);
146 MI->setOperandHi64(0);
147 mvec.push_back(MI);
148
149 MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg);
150 MI->setOperandLo64(1);
151 mvec.push_back(MI);
152
153 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
154 mvec.push_back(MI);
155
156 MI = Create2OperandInstr_Addr(SETHI, val, dest);
157 MI->setOperandHi32(0);
158 mvec.push_back(MI);
159
160 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
161 mvec.push_back(MI);
162
163 MI = Create3OperandInstr_Addr(OR, dest, val, dest);
164 MI->setOperandLo32(1);
165 mvec.push_back(MI);
166}
167
Vikram S. Adve30764b82001-10-18 00:01:48 +0000168
Vikram S. Adve242a8082002-05-19 15:25:51 +0000169static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000170CreateIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000171 int64_t C, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000172 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000173 MachineCodeForInstruction& mcfi)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000174{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000175 assert(dest->getType()->isSigned() && "Use CreateUIntSetInstruction()");
176
Vikram S. Advea2a70942001-10-28 21:41:46 +0000177 uint64_t absC = (C >= 0)? C : -C;
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000178 if (absC > (uint32_t) ~0)
Vikram S. Advea2a70942001-10-28 21:41:46 +0000179 { // C does not fit in 32 bits
Chris Lattnercb0a1202002-02-03 07:49:49 +0000180 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000181 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000182 CreateSETXConst(target, (uint64_t) C, tmpReg, dest, mvec);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000183 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000184 else
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000185 CreateSETSWConst(target, (int32_t) C, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000186}
187
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000188
Vikram S. Adve242a8082002-05-19 15:25:51 +0000189static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000190CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000191 uint64_t C, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000192 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000193 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000194{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000195 assert(! dest->getType()->isSigned() && "Use CreateIntSetInstruction()");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000196 MachineInstr* M;
197
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000198 if (C > (uint32_t) ~0)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000199 { // C does not fit in 32 bits
Vikram S. Advef7cedec2002-03-31 00:13:12 +0000200 assert(dest->getType() == Type::ULongTy && "Sign extension problems");
Chris Lattnercb0a1202002-02-03 07:49:49 +0000201 TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000202 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000203 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000204 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000205 else
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000206 CreateSETUWConst(target, C, dest, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000207}
208
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000209
Chris Lattner035dfbe2002-08-09 20:08:06 +0000210
Vikram S. Adve30764b82001-10-18 00:01:48 +0000211
212//---------------------------------------------------------------------------
213// class UltraSparcInstrInfo
214//
215// Purpose:
216// Information about individual instructions.
217// Most information is stored in the SparcMachineInstrDesc array above.
218// Other information is computed on demand, and most such functions
219// default to member functions in base class MachineInstrInfo.
220//---------------------------------------------------------------------------
221
222/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000223UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
224 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000225 /*descSize = */ NUM_TOTAL_OPCODES,
226 /*numRealOpCodes = */ NUM_REAL_OPCODES)
227{
228}
229
Vikram S. Advee76af292002-03-18 03:09:15 +0000230//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000231// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000232// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000233// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000234// The generated instructions are returned in `mvec'.
235// Any temp. registers (TmpInstruction) created are recorded in mcfi.
236// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000237//
238void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000239UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
240 Function* F,
241 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000242 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000243 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000244 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000245{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000246 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000247 "I only know about constant values and global addresses");
248
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000249 // Use a "set" instruction for known constants or symbolic constants (labels)
250 // that can go in an integer reg.
251 // We have to use a "load" instruction for all other constants,
252 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000253 //
254 const Type* valType = val->getType();
255
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000256 if (isa<GlobalValue>(val) || valType->isIntegral() || valType == Type::BoolTy)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000257 {
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000258 if (isa<GlobalValue>(val))
259 {
260 TmpInstruction* tmpReg =
261 new TmpInstruction(PointerType::get(val->getType()), val);
262 mcfi.addTemp(tmpReg);
263 CreateSETXLabel(target, val, tmpReg, dest, mvec);
264 }
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000265 else if (! dest->getType()->isSigned())
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000266 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000267 bool isValidConstant;
268 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
269 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000270 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000271 }
272 else
273 {
274 bool isValidConstant;
275 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
276 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000277 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000278 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000279 }
280 else
281 {
282 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000283 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000284 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000285
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000286 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000287 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000288 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000289 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000290
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000291 // Create another TmpInstruction for the address register
292 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000293 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000294 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000295
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000296 // Put the address (a symbolic name) into a register
297 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000298
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000299 // Generate the load instruction
300 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
301 MachineInstr* MI =
302 Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()),
303 addrReg, zeroOffset, dest);
304 mvec.push_back(MI);
305
306 // Make sure constant is emitted to constant pool in assembly code.
307 MachineCodeForMethod::get(F).addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000308 }
309}
310
311
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000312// Create an instruction sequence to copy an integer value `val'
313// to a floating point value `dest' by copying to memory and back.
314// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000315// The generated instructions are returned in `mvec'.
316// Any temp. registers (TmpInstruction) created are recorded in mcfi.
317// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000318//
319void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000320UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
321 Function* F,
322 Value* val,
323 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000324 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000325 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000326{
Chris Lattner9b625032002-05-06 16:15:30 +0000327 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000328 && "Source type must be integral");
Chris Lattner9b625032002-05-06 16:15:30 +0000329 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000330 && "Dest type must be float/double");
331
Vikram S. Adve242a8082002-05-19 15:25:51 +0000332 int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000333
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000334 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000335 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000336 // they use integer and float types that accomodate the
337 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000338 // On SparcV9: int for float, long for double.
Vikram S. Advec190c012002-07-31 21:13:31 +0000339 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000340 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
341 : Type::LongTy;
342 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000343 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
344 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000345 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
346 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000347
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000348 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000349 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000350 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000351 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
352 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
353 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000354 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000355}
356
357
358// Similarly, create an instruction sequence to copy an FP value
359// `val' to an integer value `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000360// The generated instructions are returned in `mvec'.
361// Any temp. registers (TmpInstruction) created are recorded in mcfi.
362// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000363//
364void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000365UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
366 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000367 Value* val,
368 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000369 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000370 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000371{
Vikram S. Advec190c012002-07-31 21:13:31 +0000372 const Type* opTy = val->getType();
373 const Type* destTy = dest->getType();
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000374
Vikram S. Advec190c012002-07-31 21:13:31 +0000375 assert(opTy->isFloatingPoint() && "Source type must be float/double");
376 assert((destTy->isIntegral() || isa<PointerType>(destTy))
377 && "Dest type must be integral");
378
Vikram S. Adve242a8082002-05-19 15:25:51 +0000379 int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000380
381 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000382 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000383 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000384 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000385 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
386 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
387 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000388 mvec.push_back(store);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000389
390 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000391 // The type of the load opCode is the integer type that matches the
392 // source type in size: (and the dest type in sign):
393 // On SparcV9: int for float, long for double.
394 // Note that we *must* use signed loads even for unsigned dest types, to
395 // ensure that we get the right sign-extension for smaller-than-64-bit
396 // unsigned dest. types (i.e., UByte, UShort or UInt):
397 const Type* loadTy = opTy == Type::FloatTy? Type::IntTy : Type::LongTy;
398 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000399 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000400 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000401 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000402 mvec.push_back(load);
403}
404
405
406// Create instruction(s) to copy src to dest, for arbitrary types
407// The generated instructions are returned in `mvec'.
408// Any temp. registers (TmpInstruction) created are recorded in mcfi.
409// Any stack space required is allocated via MachineCodeForMethod.
410//
411void
412UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
413 Function *F,
414 Value* src,
415 Instruction* dest,
416 vector<MachineInstr*>& mvec,
417 MachineCodeForInstruction& mcfi) const
418{
419 bool loadConstantToReg = false;
420
421 const Type* resultType = dest->getType();
422
423 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
424 if (opCode == INVALID_OPCODE)
425 {
426 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
427 return;
428 }
429
430 // if `src' is a constant that doesn't fit in the immed field or if it is
431 // a global variable (i.e., a constant address), generate a load
432 // instruction instead of an add
433 //
434 if (isa<Constant>(src))
435 {
436 unsigned int machineRegNum;
437 int64_t immedValue;
438 MachineOperand::MachineOperandType opType =
439 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
440 machineRegNum, immedValue);
441
442 if (opType == MachineOperand::MO_VirtualRegister)
443 loadConstantToReg = true;
444 }
445 else if (isa<GlobalValue>(src))
446 loadConstantToReg = true;
447
448 if (loadConstantToReg)
449 { // `src' is constant and cannot fit in immed field for the ADD
450 // Insert instructions to "load" the constant into a register
451 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
452 mvec, mcfi);
453 }
454 else
455 { // Create an add-with-0 instruction of the appropriate type.
456 // Make `src' the second operand, in case it is a constant
457 // Use (unsigned long) 0 for a NULL pointer value.
458 //
459 const Type* zeroValueType =
460 isa<PointerType>(resultType) ? Type::ULongTy : resultType;
461 MachineInstr* minstr =
462 Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType),
463 src, dest);
464 mvec.push_back(minstr);
465 }
466}
467
468
469// Create instruction sequence to produce a sign-extended register value
470// from an arbitrary sized value (sized in bits, not bytes).
471// For SPARC v9, we sign-extend the given unsigned operand using SLL; SRA.
472// The generated instructions are returned in `mvec'.
473// Any temp. registers (TmpInstruction) created are recorded in mcfi.
474// Any stack space required is allocated via MachineCodeForMethod.
475//
476void
477UltraSparcInstrInfo::CreateSignExtensionInstructions(
478 const TargetMachine& target,
479 Function* F,
480 Value* unsignedSrcVal,
481 unsigned int srcSizeInBits,
482 Value* dest,
483 vector<MachineInstr*>& mvec,
484 MachineCodeForInstruction& mcfi) const
485{
486 MachineInstr* M;
Vikram S. Advec190c012002-07-31 21:13:31 +0000487 assert(srcSizeInBits < 64 && "Sign extension unnecessary!");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000488 assert(srcSizeInBits > 0 && srcSizeInBits <= 32
Vikram S. Advec190c012002-07-31 21:13:31 +0000489 && "Hmmm... 32 < srcSizeInBits < 64 unexpected but could be handled here.");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000490
491 if (srcSizeInBits < 32)
492 { // SLL is needed since operand size is < 32 bits.
493 TmpInstruction *tmpI = new TmpInstruction(dest->getType(),
494 unsignedSrcVal, dest,"make32");
495 mcfi.addTemp(tmpI);
496 M = Create3OperandInstr_UImmed(SLL,unsignedSrcVal,32-srcSizeInBits,tmpI);
497 mvec.push_back(M);
498 unsignedSrcVal = tmpI;
499 }
500
501 M = Create3OperandInstr_UImmed(SRA, unsignedSrcVal, 32-srcSizeInBits, dest);
502 mvec.push_back(M);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000503}