blob: b4460f0b537a2ea27ebb3808bd0287dbb98dcff1 [file] [log] [blame]
Vikram S. Adve30764b82001-10-18 00:01:48 +00001//***************************************************************************
2// File:
3// SparcInstrInfo.cpp
4//
5// Purpose:
6//
7// History:
8// 10/15/01 - Vikram Adve - Created
9//**************************************************************************/
10
11
12#include "SparcInternals.h"
13#include "SparcInstrSelectionSupport.h"
14#include "llvm/Target/Sparc.h"
15#include "llvm/CodeGen/InstrSelection.h"
16#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnercb0a1202002-02-03 07:49:49 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019#include "llvm/Function.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000020#include "llvm/BasicBlock.h"
21#include "llvm/Instruction.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000023#include "llvm/DerivedTypes.h"
Anand Shuklacfb22d32002-06-25 20:55:50 +000024using std::vector;
Vikram S. Adve30764b82001-10-18 00:01:48 +000025
26//************************ Internal Functions ******************************/
27
Vikram S. Adve53fd4002002-07-10 21:39:50 +000028static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
29static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
30
31
32// Set a 32-bit unsigned constant in the register `dest'.
33//
34static inline void
35CreateSETUWConst(const TargetMachine& target, uint32_t C,
36 Instruction* dest, std::vector<MachineInstr*>& mvec)
37{
38 MachineInstr *miSETHI = NULL, *miOR = NULL;
39
40 // In order to get efficient code, we should not generate the SETHI if
41 // all high bits are 1 (i.e., this is a small signed value that fits in
42 // the simm13 field of OR). So we check for and handle that case specially.
43 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
44 // In fact, sC == -sC, so we have to check for this explicitly.
45 int32_t sC = (int32_t) C;
46 bool smallSignedValue = sC < 0 && sC != -sC && -sC < (int32_t) MAXSIMM;
47
48 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
49 if (!smallSignedValue && (C & ~MAXLO) && C > MAXSIMM)
50 {
51 miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest);
52 miSETHI->setOperandHi32(0);
53 mvec.push_back(miSETHI);
54 }
55
56 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
57 // was generated, or if the low 10 bits are non-zero.
58 if (miSETHI==NULL || C & MAXLO)
59 {
60 if (miSETHI)
61 { // unsigned value with high-order bits set using SETHI
62 miOR = Create3OperandInstr_UImmed(OR, dest, C, dest);
63 miOR->setOperandLo32(1);
64 }
65 else
66 { // unsigned or small signed value that fits in simm13 field of OR
67 assert(smallSignedValue || (C & ~MAXSIMM) == 0);
68 miOR = new MachineInstr(OR);
69 miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
70 miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
71 sC);
72 miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest);
73 }
74 mvec.push_back(miOR);
75 }
76
77 assert((miSETHI || miOR) && "Oops, no code was generated!");
78}
79
80// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
81// Not needed for SPARC v9 but useful to make the two SETX functions similar
82static inline void
83CreateSETUWLabel(const TargetMachine& target, Value* val,
84 Instruction* dest, std::vector<MachineInstr*>& mvec)
85{
86 MachineInstr* MI;
87
88 // Set the high 22 bits in dest
89 MI = Create2OperandInstr(SETHI, val, dest);
90 MI->setOperandHi32(0);
91 mvec.push_back(MI);
92
93 // Set the low 10 bits in dest
94 MI = Create3OperandInstr(OR, dest, val, dest);
95 MI->setOperandLo32(1);
96 mvec.push_back(MI);
97}
98
99
100// Set a 32-bit signed constant in the register `dest',
101// with sign-extension to 64 bits.
102static inline void
103CreateSETSWConst(const TargetMachine& target, int32_t C,
104 Instruction* dest, std::vector<MachineInstr*>& mvec)
105{
106 MachineInstr* MI;
107
108 // Set the low 32 bits of dest
109 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
110
111 // Sign-extend to the high 32 bits if needed
112 if (C < 0 && (-C) > (int32_t) MAXSIMM)
113 {
114 MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest);
115 mvec.push_back(MI);
116 }
117}
118
119
120// Set a 64-bit signed or unsigned constant in the register `dest'.
121static inline void
122CreateSETXConst(const TargetMachine& target, uint64_t C,
123 Instruction* tmpReg, Instruction* dest,
124 std::vector<MachineInstr*>& mvec)
125{
126 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
127
128 MachineInstr* MI;
129
130 // Code to set the upper 32 bits of the value in register `tmpReg'
131 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
132
133 // Shift tmpReg left by 32 bits
134 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
135 mvec.push_back(MI);
136
137 // Code to set the low 32 bits of the value in register `dest'
138 CreateSETUWConst(target, C, dest, mvec);
139
140 // dest = OR(tmpReg, dest)
141 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
142 mvec.push_back(MI);
143}
144
145
146// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
147static inline void
148CreateSETXLabel(const TargetMachine& target,
149 Value* val, Instruction* tmpReg, Instruction* dest,
150 std::vector<MachineInstr*>& mvec)
151{
152 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
153 "I only know about constant values and global addresses");
154
155 MachineInstr* MI;
156
157 MI = Create2OperandInstr_Addr(SETHI, val, tmpReg);
158 MI->setOperandHi64(0);
159 mvec.push_back(MI);
160
161 MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg);
162 MI->setOperandLo64(1);
163 mvec.push_back(MI);
164
165 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
166 mvec.push_back(MI);
167
168 MI = Create2OperandInstr_Addr(SETHI, val, dest);
169 MI->setOperandHi32(0);
170 mvec.push_back(MI);
171
172 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
173 mvec.push_back(MI);
174
175 MI = Create3OperandInstr_Addr(OR, dest, val, dest);
176 MI->setOperandLo32(1);
177 mvec.push_back(MI);
178}
179
Vikram S. Adve30764b82001-10-18 00:01:48 +0000180
Vikram S. Adve242a8082002-05-19 15:25:51 +0000181static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000182CreateIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000183 int64_t C, Instruction* dest,
184 std::vector<MachineInstr*>& mvec,
185 MachineCodeForInstruction& mcfi)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000186{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000187 assert(dest->getType()->isSigned() && "Use CreateUIntSetInstruction()");
188
Vikram S. Advea2a70942001-10-28 21:41:46 +0000189 uint64_t absC = (C >= 0)? C : -C;
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000190 if (absC > (uint32_t) ~0)
Vikram S. Advea2a70942001-10-28 21:41:46 +0000191 { // C does not fit in 32 bits
Chris Lattnercb0a1202002-02-03 07:49:49 +0000192 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000193 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000194 CreateSETXConst(target, (uint64_t) C, tmpReg, dest, mvec);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000195 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000196 else
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197 CreateSETSWConst(target, (int32_t) C, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000198}
199
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000200
Vikram S. Adve242a8082002-05-19 15:25:51 +0000201static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000202CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000203 uint64_t C, Instruction* dest,
204 std::vector<MachineInstr*>& mvec,
205 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000206{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000207 assert(! dest->getType()->isSigned() && "Use CreateIntSetInstruction()");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000208 MachineInstr* M;
209
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000210 if (C > (uint32_t) ~0)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000211 { // C does not fit in 32 bits
Vikram S. Advef7cedec2002-03-31 00:13:12 +0000212 assert(dest->getType() == Type::ULongTy && "Sign extension problems");
Chris Lattnercb0a1202002-02-03 07:49:49 +0000213 TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000214 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000215 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000216 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000217 else
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000218 CreateSETUWConst(target, C, dest, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000219}
220
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000221
Vikram S. Adve30764b82001-10-18 00:01:48 +0000222//************************* External Classes *******************************/
223
224//---------------------------------------------------------------------------
225// class UltraSparcInstrInfo
226//
227// Purpose:
228// Information about individual instructions.
229// Most information is stored in the SparcMachineInstrDesc array above.
230// Other information is computed on demand, and most such functions
231// default to member functions in base class MachineInstrInfo.
232//---------------------------------------------------------------------------
233
234/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000235UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
236 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000237 /*descSize = */ NUM_TOTAL_OPCODES,
238 /*numRealOpCodes = */ NUM_REAL_OPCODES)
239{
240}
241
Vikram S. Advee76af292002-03-18 03:09:15 +0000242//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000243// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000244// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000245// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000246// The generated instructions are returned in `mvec'.
247// Any temp. registers (TmpInstruction) created are recorded in mcfi.
248// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000249//
250void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000251UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
252 Function* F,
253 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000254 Instruction* dest,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000255 std::vector<MachineInstr*>& mvec,
256 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000257{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000258 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000259 "I only know about constant values and global addresses");
260
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000261 // Use a "set" instruction for known constants or symbolic constants (labels)
262 // that can go in an integer reg.
263 // We have to use a "load" instruction for all other constants,
264 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000265 //
266 const Type* valType = val->getType();
267
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000268 if (isa<GlobalValue>(val) || valType->isIntegral() || valType == Type::BoolTy)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000269 {
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000270 if (isa<GlobalValue>(val))
271 {
272 TmpInstruction* tmpReg =
273 new TmpInstruction(PointerType::get(val->getType()), val);
274 mcfi.addTemp(tmpReg);
275 CreateSETXLabel(target, val, tmpReg, dest, mvec);
276 }
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000277 else if (! dest->getType()->isSigned())
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000278 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000279 bool isValidConstant;
280 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
281 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000282 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000283 }
284 else
285 {
286 bool isValidConstant;
287 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
288 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000289 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000290 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000291 }
292 else
293 {
294 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000295 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000296 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000297
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000298 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000299 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000300 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000301 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000302
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000303 // Create another TmpInstruction for the address register
304 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000305 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000306 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000307
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000308 // Put the address (a symbolic name) into a register
309 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000310
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000311 // Generate the load instruction
312 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
313 MachineInstr* MI =
314 Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()),
315 addrReg, zeroOffset, dest);
316 mvec.push_back(MI);
317
318 // Make sure constant is emitted to constant pool in assembly code.
319 MachineCodeForMethod::get(F).addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000320 }
321}
322
323
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000324// Create an instruction sequence to copy an integer value `val'
325// to a floating point value `dest' by copying to memory and back.
326// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000327// The generated instructions are returned in `mvec'.
328// Any temp. registers (TmpInstruction) created are recorded in mcfi.
329// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000330//
331void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000332UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
333 Function* F,
334 Value* val,
335 Instruction* dest,
336 std::vector<MachineInstr*>& mvec,
337 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000338{
Chris Lattner9b625032002-05-06 16:15:30 +0000339 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000340 && "Source type must be integral");
Chris Lattner9b625032002-05-06 16:15:30 +0000341 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000342 && "Dest type must be float/double");
343
Vikram S. Adve242a8082002-05-19 15:25:51 +0000344 int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000345
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000346 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000347 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000348 // they use integer and float types that accomodate the
349 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000350 // On SparcV9: int for float, long for double.
Vikram S. Advec190c012002-07-31 21:13:31 +0000351 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000352 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
353 : Type::LongTy;
354 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000355 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
356 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000357 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
358 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000359
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000360 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000361 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000362 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000363 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
364 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
365 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000366 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000367}
368
369
370// Similarly, create an instruction sequence to copy an FP value
371// `val' to an integer value `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000372// The generated instructions are returned in `mvec'.
373// Any temp. registers (TmpInstruction) created are recorded in mcfi.
374// Any stack space required is allocated via MachineCodeForMethod.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000375//
376void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000377UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
378 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000379 Value* val,
380 Instruction* dest,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000381 std::vector<MachineInstr*>& mvec,
382 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000383{
Vikram S. Advec190c012002-07-31 21:13:31 +0000384 const Type* opTy = val->getType();
385 const Type* destTy = dest->getType();
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000386
Vikram S. Advec190c012002-07-31 21:13:31 +0000387 assert(opTy->isFloatingPoint() && "Source type must be float/double");
388 assert((destTy->isIntegral() || isa<PointerType>(destTy))
389 && "Dest type must be integral");
390
Vikram S. Adve242a8082002-05-19 15:25:51 +0000391 int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000392
393 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000394 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000395 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000396 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000397 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
398 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
399 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000400 mvec.push_back(store);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000401
402 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000403 // The type of the load opCode is the integer type that matches the
404 // source type in size: (and the dest type in sign):
405 // On SparcV9: int for float, long for double.
406 // Note that we *must* use signed loads even for unsigned dest types, to
407 // ensure that we get the right sign-extension for smaller-than-64-bit
408 // unsigned dest. types (i.e., UByte, UShort or UInt):
409 const Type* loadTy = opTy == Type::FloatTy? Type::IntTy : Type::LongTy;
410 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000411 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000412 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000413 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000414 mvec.push_back(load);
415}
416
417
418// Create instruction(s) to copy src to dest, for arbitrary types
419// The generated instructions are returned in `mvec'.
420// Any temp. registers (TmpInstruction) created are recorded in mcfi.
421// Any stack space required is allocated via MachineCodeForMethod.
422//
423void
424UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
425 Function *F,
426 Value* src,
427 Instruction* dest,
428 vector<MachineInstr*>& mvec,
429 MachineCodeForInstruction& mcfi) const
430{
431 bool loadConstantToReg = false;
432
433 const Type* resultType = dest->getType();
434
435 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
436 if (opCode == INVALID_OPCODE)
437 {
438 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
439 return;
440 }
441
442 // if `src' is a constant that doesn't fit in the immed field or if it is
443 // a global variable (i.e., a constant address), generate a load
444 // instruction instead of an add
445 //
446 if (isa<Constant>(src))
447 {
448 unsigned int machineRegNum;
449 int64_t immedValue;
450 MachineOperand::MachineOperandType opType =
451 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
452 machineRegNum, immedValue);
453
454 if (opType == MachineOperand::MO_VirtualRegister)
455 loadConstantToReg = true;
456 }
457 else if (isa<GlobalValue>(src))
458 loadConstantToReg = true;
459
460 if (loadConstantToReg)
461 { // `src' is constant and cannot fit in immed field for the ADD
462 // Insert instructions to "load" the constant into a register
463 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
464 mvec, mcfi);
465 }
466 else
467 { // Create an add-with-0 instruction of the appropriate type.
468 // Make `src' the second operand, in case it is a constant
469 // Use (unsigned long) 0 for a NULL pointer value.
470 //
471 const Type* zeroValueType =
472 isa<PointerType>(resultType) ? Type::ULongTy : resultType;
473 MachineInstr* minstr =
474 Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType),
475 src, dest);
476 mvec.push_back(minstr);
477 }
478}
479
480
481// Create instruction sequence to produce a sign-extended register value
482// from an arbitrary sized value (sized in bits, not bytes).
483// For SPARC v9, we sign-extend the given unsigned operand using SLL; SRA.
484// The generated instructions are returned in `mvec'.
485// Any temp. registers (TmpInstruction) created are recorded in mcfi.
486// Any stack space required is allocated via MachineCodeForMethod.
487//
488void
489UltraSparcInstrInfo::CreateSignExtensionInstructions(
490 const TargetMachine& target,
491 Function* F,
492 Value* unsignedSrcVal,
493 unsigned int srcSizeInBits,
494 Value* dest,
495 vector<MachineInstr*>& mvec,
496 MachineCodeForInstruction& mcfi) const
497{
498 MachineInstr* M;
Vikram S. Advec190c012002-07-31 21:13:31 +0000499 assert(srcSizeInBits < 64 && "Sign extension unnecessary!");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000500 assert(srcSizeInBits > 0 && srcSizeInBits <= 32
Vikram S. Advec190c012002-07-31 21:13:31 +0000501 && "Hmmm... 32 < srcSizeInBits < 64 unexpected but could be handled here.");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000502
503 if (srcSizeInBits < 32)
504 { // SLL is needed since operand size is < 32 bits.
505 TmpInstruction *tmpI = new TmpInstruction(dest->getType(),
506 unsignedSrcVal, dest,"make32");
507 mcfi.addTemp(tmpI);
508 M = Create3OperandInstr_UImmed(SLL,unsignedSrcVal,32-srcSizeInBits,tmpI);
509 mvec.push_back(M);
510 unsignedSrcVal = tmpI;
511 }
512
513 M = Create3OperandInstr_UImmed(SRA, unsignedSrcVal, 32-srcSizeInBits, dest);
514 mvec.push_back(M);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000515}