blob: 9988bbd43f28e42bbc199aa647120effd833b471 [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"
Misha Brukmanfce11432002-10-28 00:28:31 +00009#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner2ef9a6a2002-12-28 20:18:21 +000010#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000011#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000012#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000013#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000015#include "llvm/DerivedTypes.h"
John Criswell7a73b802003-06-30 21:59:07 +000016#include "Config/stdlib.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000017
Vikram S. Adve53fd4002002-07-10 21:39:50 +000018static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
19static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
20
21
Chris Lattner795ba6c2003-01-15 21:36:50 +000022//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000023// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000024//
Vikram S. Advee6124d32003-07-29 19:59:23 +000025// Function to get the value of an integral constant in the form
26// that must be put into the machine register. The specified constant is
27// interpreted as (i.e., converted if necessary to) the specified destination
28// type. The result is always returned as an uint64_t, since the representation
29// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000030//
31// isValidConstant is set to true if a valid constant was found.
32//---------------------------------------------------------------------------
33
Vikram S. Advee6124d32003-07-29 19:59:23 +000034uint64_t
35UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
36 const Value *V,
37 const Type *destType,
38 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000039{
Chris Lattner795ba6c2003-01-15 21:36:50 +000040 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000041 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000042
Vikram S. Advee6124d32003-07-29 19:59:23 +000043 if (! destType->isIntegral() && ! isa<PointerType>(destType))
44 return C;
45
46 if (! isa<Constant>(V))
47 return C;
48
49 // ConstantPointerRef: no conversions needed: get value and return it
50 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
51 // A ConstantPointerRef is just a reference to GlobalValue.
52 isValidConstant = true; // may be overwritten by recursive call
53 return (CPR->isNullValue()? 0
54 : ConvertConstantToIntType(target, CPR->getValue(), destType,
55 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000056 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000057
58 // ConstantBool: no conversions needed: get value and return it
59 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
60 isValidConstant = true;
61 return (uint64_t) CB->getValue();
62 }
63
64 // For other types of constants, some conversion may be needed.
65 // First, extract the constant operand according to its own type
66 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
67 switch(CE->getOpcode()) {
68 case Instruction::Cast: // recursively get the value as cast
69 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
70 isValidConstant);
71 break;
72 default: // not simplifying other ConstantExprs
73 break;
74 }
75 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
76 isValidConstant = true;
77 C = CI->getRawValue();
78 }
79 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
80 isValidConstant = true;
81 double fC = CFP->getValue();
82 C = (destType->isSigned()? (uint64_t) (int64_t) fC
83 : (uint64_t) fC);
84 }
85
86 // Now if a valid value was found, convert it to destType.
87 if (isValidConstant) {
88 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
89 unsigned destSize = target.getTargetData().getTypeSize(destType);
90 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
91 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
92
93 if (destType->isSigned()) {
94 if (opSize > destSize) // operand is larger than dest:
95 C = C & maskHi; // mask high bits
96
97 if (opSize > destSize ||
98 (opSize == destSize && ! V->getType()->isSigned()))
99 if (C & (1U << (8*destSize - 1)))
100 C = C | ~maskHi; // sign-extend from destSize to 64 bits
101 }
102 else {
103 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
104 // operand is larger than dest,
105 // OR both are equal but smaller than the full register size
106 // AND operand is signed, so it may have extra sign bits:
107 // mask high bits
108 C = C & maskHi;
109 }
110 }
111 }
112
113 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000114}
115
116
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000117//----------------------------------------------------------------------------
118// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000119//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000120// Set a 32-bit unsigned constant in the register `dest', using
121// SETHI, OR in the worst case. This function correctly emulates
122// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
123//
124// The isSigned=true case is used to implement SETSW without duplicating code.
125//
126// Optimize some common cases:
127// (1) Small value that fits in simm13 field of OR: don't need SETHI.
128// (2) isSigned = true and C is a small negative signed value, i.e.,
129// high bits are 1, and the remaining bits fit in simm13(OR).
130//----------------------------------------------------------------------------
131
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000132static inline void
133CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000134 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000135 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000136{
137 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000138
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000139 // In order to get efficient code, we should not generate the SETHI if
140 // all high bits are 1 (i.e., this is a small signed value that fits in
141 // the simm13 field of OR). So we check for and handle that case specially.
142 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
143 // In fact, sC == -sC, so we have to check for this explicitly.
144 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000145 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
146
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000147 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000148 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
149 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
150 miSETHI->setOperandHi32(0);
151 mvec.push_back(miSETHI);
152 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000153
154 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
155 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000156 if (miSETHI==NULL || C & MAXLO) {
157 if (miSETHI) {
158 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000159 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000160 miOR->setOperandLo32(1);
161 } else {
162 // unsigned or small signed value that fits in simm13 field of OR
163 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Misha Brukman71ed1c92003-05-27 22:35:43 +0000164 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
Misha Brukman81b06862003-05-21 18:48:06 +0000165 .getZeroRegNum())
166 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000167 }
Misha Brukman81b06862003-05-21 18:48:06 +0000168 mvec.push_back(miOR);
169 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000170
171 assert((miSETHI || miOR) && "Oops, no code was generated!");
172}
173
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000174
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000175//----------------------------------------------------------------------------
176// Function: CreateSETSWConst
177//
178// Set a 32-bit signed constant in the register `dest', with sign-extension
179// to 64 bits. This uses SETHI, OR, SRA in the worst case.
180// This function correctly emulates the SETSW pseudo-op for SPARC v9.
181//
182// Optimize the same cases as SETUWConst, plus:
183// (1) SRA is not needed for positive or small negative values.
184//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000185
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000186static inline void
187CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000188 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000189{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000190 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000191 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
192
Vikram S. Advec2f09392003-05-25 21:58:11 +0000193 // Sign-extend to the high 32 bits if needed.
194 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
195 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000196 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197}
198
199
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000200//----------------------------------------------------------------------------
201// Function: CreateSETXConst
202//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000203// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000204// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
205// This function correctly emulates the SETX pseudo-op for SPARC v9.
206//
207// Optimize the same cases as SETUWConst for each 32 bit word.
208//----------------------------------------------------------------------------
209
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000210static inline void
211CreateSETXConst(const TargetMachine& target, uint64_t C,
212 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000213 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000214{
215 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
216
217 MachineInstr* MI;
218
219 // Code to set the upper 32 bits of the value in register `tmpReg'
220 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
221
222 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000223 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000224 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000225
226 // Code to set the low 32 bits of the value in register `dest'
227 CreateSETUWConst(target, C, dest, mvec);
228
229 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000230 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000231}
232
233
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000234//----------------------------------------------------------------------------
235// Function: CreateSETUWLabel
236//
237// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
238//----------------------------------------------------------------------------
239
240static inline void
241CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000242 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000243{
244 MachineInstr* MI;
245
246 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000247 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000248 MI->setOperandHi32(0);
249 mvec.push_back(MI);
250
251 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000252 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000253 MI->setOperandLo32(1);
254 mvec.push_back(MI);
255}
256
257
258//----------------------------------------------------------------------------
259// Function: CreateSETXLabel
260//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000261// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000262//----------------------------------------------------------------------------
263
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000264static inline void
265CreateSETXLabel(const TargetMachine& target,
266 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000267 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000268{
269 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
270 "I only know about constant values and global addresses");
271
272 MachineInstr* MI;
273
Misha Brukmana98cd452003-05-20 20:32:24 +0000274 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000275 MI->setOperandHi64(0);
276 mvec.push_back(MI);
277
Misha Brukman71ed1c92003-05-27 22:35:43 +0000278 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000279 MI->setOperandLo64(1);
280 mvec.push_back(MI);
281
Misha Brukman71ed1c92003-05-27 22:35:43 +0000282 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000283 .addRegDef(tmpReg));
284 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000285 MI->setOperandHi32(0);
286 mvec.push_back(MI);
287
Misha Brukman71ed1c92003-05-27 22:35:43 +0000288 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000289 mvec.push_back(MI);
290
Misha Brukman71ed1c92003-05-27 22:35:43 +0000291 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000292 MI->setOperandLo32(1);
293 mvec.push_back(MI);
294}
295
Vikram S. Adve30764b82001-10-18 00:01:48 +0000296
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000297//----------------------------------------------------------------------------
298// Function: CreateUIntSetInstruction
299//
300// Create code to Set an unsigned constant in the register `dest'.
301// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
302// CreateSETSWConst is an optimization for the case that the unsigned value
303// has all ones in the 33 high bits (so that sign-extension sets them all).
304//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000305
Vikram S. Adve242a8082002-05-19 15:25:51 +0000306static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000307CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000308 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000309 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000310 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000311{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000312 static const uint64_t lo32 = (uint32_t) ~0;
313 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
314 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000315 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000316 // All high 33 (not 32) bits are 1s: sign-extension will take care
317 // of high 32 bits, so use the sequence for signed int
318 CreateSETSWConst(target, (int32_t) C, dest, mvec);
319 } else if (C > lo32) {
320 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000321 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000322 CreateSETXConst(target, C, tmpReg, dest, mvec);
323 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000324}
325
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000326
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000327//----------------------------------------------------------------------------
328// Function: CreateIntSetInstruction
329//
330// Create code to Set a signed constant in the register `dest'.
331// Really the same as CreateUIntSetInstruction.
332//----------------------------------------------------------------------------
333
334static inline void
335CreateIntSetInstruction(const TargetMachine& target,
336 int64_t C, Instruction* dest,
337 std::vector<MachineInstr*>& mvec,
338 MachineCodeForInstruction& mcfi)
339{
340 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
341}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000342
Vikram S. Adve30764b82001-10-18 00:01:48 +0000343
344//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000345// Create a table of LLVM opcode -> max. immediate constant likely to
346// be usable for that operation.
347//---------------------------------------------------------------------------
348
349// Entry == 0 ==> no immediate constant field exists at all.
350// Entry > 0 ==> abs(immediate constant) <= Entry
351//
Misha Brukmana98cd452003-05-20 20:32:24 +0000352std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000353
354static int
355MaxConstantForInstr(unsigned llvmOpCode)
356{
357 int modelOpCode = -1;
358
Chris Lattner0b16ae22002-10-13 19:39:16 +0000359 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
360 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000361 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000362 else
363 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000364 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000365
366 case Instruction::Malloc:
367 case Instruction::Alloca:
368 case Instruction::GetElementPtr:
369 case Instruction::PHINode:
370 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000371 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000372
373 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000374 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000375
376 default: break;
377 };
378
379 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
380}
381
382static void
383InitializeMaxConstantsTable()
384{
385 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000386 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000387 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000388 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000389 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000390 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000391 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000392 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000393 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000394 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000395 MaxConstantsTable[op] = MaxConstantForInstr(op);
396}
397
398
399//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000400// class UltraSparcInstrInfo
401//
402// Purpose:
403// Information about individual instructions.
404// Most information is stored in the SparcMachineInstrDesc array above.
405// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000406// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000407//---------------------------------------------------------------------------
408
409/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000410UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000411 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000412 /*descSize = */ V9::NUM_TOTAL_OPCODES,
413 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000414{
Vikram S. Adve49001162002-09-16 15:56:01 +0000415 InitializeMaxConstantsTable();
416}
417
418bool
419UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
420 const Instruction* I) const
421{
422 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
423 return true;
424
425 if (isa<ConstantPointerNull>(CV)) // can always use %g0
426 return false;
427
Chris Lattnerc07736a2003-07-23 15:22:26 +0000428 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
429 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000430
431 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000432 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000433
434 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000435}
436
Vikram S. Advee76af292002-03-18 03:09:15 +0000437//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000438// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000439// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000440// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000441// The generated instructions are returned in `mvec'.
442// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000443// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000444//
445void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000446UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
447 Function* F,
448 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000449 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000450 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000451 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000452{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000453 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000454 "I only know about constant values and global addresses");
455
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000456 // Use a "set" instruction for known constants or symbolic constants (labels)
457 // that can go in an integer reg.
458 // We have to use a "load" instruction for all other constants,
459 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000460 //
461 const Type* valType = val->getType();
462
Vikram S. Advee6124d32003-07-29 19:59:23 +0000463 // A ConstantPointerRef is just a reference to GlobalValue.
464 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000465 val = cast<ConstantPointerRef>(val)->getValue();
466
Misha Brukman81b06862003-05-21 18:48:06 +0000467 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000468 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000469 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000470 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000471 return;
472 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000473
Vikram S. Advee6124d32003-07-29 19:59:23 +0000474 bool isValid;
475 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
476 if (isValid) {
477 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000478 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000479 else
480 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000481
Misha Brukman81b06862003-05-21 18:48:06 +0000482 } else {
483 // Make an instruction sequence to load the constant, viz:
484 // SETX <addr-of-constant>, tmpReg, addrReg
485 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000486
Misha Brukman81b06862003-05-21 18:48:06 +0000487 // First, create a tmp register to be used by the SETX sequence.
488 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000489 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000490
Misha Brukman81b06862003-05-21 18:48:06 +0000491 // Create another TmpInstruction for the address register
492 TmpInstruction* addrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000493 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000494
Misha Brukman81b06862003-05-21 18:48:06 +0000495 // Put the address (a symbolic name) into a register
496 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000497
Misha Brukman81b06862003-05-21 18:48:06 +0000498 // Generate the load instruction
499 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
500 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000501 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman81b06862003-05-21 18:48:06 +0000502 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
503 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000504
Misha Brukman81b06862003-05-21 18:48:06 +0000505 // Make sure constant is emitted to constant pool in assembly code.
506 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
507 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000508}
509
510
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000511// Create an instruction sequence to copy an integer register `val'
512// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000513// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000514// The generated instructions are returned in `mvec'.
515// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000516// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000517//
518void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000519UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
520 Function* F,
521 Value* val,
522 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000523 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000524 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000525{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000526 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
527 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000528 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000529 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000530
531 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000532 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000533
534 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000535 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000536
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000537 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000538 // The store and load opCodes are based on the size of the source value.
539 // If the value is smaller than 32 bits, we must sign- or zero-extend it
540 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000541 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000542 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
543 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000544 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
545 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000546 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000547 if (val->getType()->isSigned())
548 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
549 mvec, mcfi);
550 else
551 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
552 mvec, mcfi);
553 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000554
555 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000556 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
557 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
558 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000559 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000560
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000561 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000562 // The type of the load opCode is the floating point type that matches the
563 // stored type in size:
564 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000565 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000566 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000567 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
568 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
569 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000570 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000571}
572
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000573// Similarly, create an instruction sequence to copy an FP register
574// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000576// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
577// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000578//
579void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000580UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
581 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000582 Value* val,
583 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000584 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000585 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000586{
Vikram S. Advec190c012002-07-31 21:13:31 +0000587 const Type* opTy = val->getType();
588 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000589
Vikram S. Advec190c012002-07-31 21:13:31 +0000590 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000591 assert((destTy->isIntegral() || isa<PointerType>(destTy))
592 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000593
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000594 // FIXME: For now, we allocate permanent space because the stack frame
595 // manager does not allow locals to be allocated (e.g., for alloca) after
596 // a temp is allocated!
597 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000598 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000599
Chris Lattner54e898e2003-01-15 19:23:34 +0000600 unsigned FPReg = target.getRegInfo().getFramePointer();
601
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000602 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000603 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000604 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000605 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
606 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
607 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000608 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000609
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000610 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000611 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000612 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000613 // On SparcV9: int for float, long for double.
614 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000615 // ensure correct sign-extension for UByte, UShort or UInt:
616 //
617 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000618 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
619 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
620 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000621 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622}
623
624
625// Create instruction(s) to copy src to dest, for arbitrary types
626// The generated instructions are returned in `mvec'.
627// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000628// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000629//
630void
631UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
632 Function *F,
633 Value* src,
634 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000635 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000636 MachineCodeForInstruction& mcfi) const
637{
638 bool loadConstantToReg = false;
639
640 const Type* resultType = dest->getType();
641
642 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000643 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000644 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
645 return;
646 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000647
648 // if `src' is a constant that doesn't fit in the immed field or if it is
649 // a global variable (i.e., a constant address), generate a load
650 // instruction instead of an add
651 //
Misha Brukman81b06862003-05-21 18:48:06 +0000652 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000653 unsigned int machineRegNum;
654 int64_t immedValue;
655 MachineOperand::MachineOperandType opType =
656 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
657 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658
Misha Brukmana98cd452003-05-20 20:32:24 +0000659 if (opType == MachineOperand::MO_VirtualRegister)
660 loadConstantToReg = true;
661 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000662 else if (isa<GlobalValue>(src))
663 loadConstantToReg = true;
664
Misha Brukman81b06862003-05-21 18:48:06 +0000665 if (loadConstantToReg) {
666 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000667 // Insert instructions to "load" the constant into a register
668 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
669 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000670 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000671 // Create a reg-to-reg copy instruction for the given type:
672 // -- For FP values, create a FMOVS or FMOVD instruction
673 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
674 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000675 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000676 MachineInstr* MI;
677 if (resultType->isFloatingPoint())
678 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
679 .addReg(src).addRegDef(dest));
680 else {
681 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
682 MI = (BuildMI(opCode, 3)
683 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
684 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000685 mvec.push_back(MI);
686 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687}
688
689
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000690// Helper function for sign-extension and zero-extension.
691// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
692inline void
693CreateBitExtensionInstructions(bool signExtend,
694 const TargetMachine& target,
695 Function* F,
696 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000697 Value* destVal,
698 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000699 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000700 MachineCodeForInstruction& mcfi)
701{
702 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000703
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000704 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
705
Misha Brukman81b06862003-05-21 18:48:06 +0000706 if (numLowBits < 32) {
707 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000708 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000709 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000710 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000711 .addZImm(32-numLowBits).addRegDef(tmpI));
712 srcVal = tmpI;
713 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000714
Misha Brukmand36e30e2003-06-06 09:52:23 +0000715 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000716 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000717}
718
719
Vikram S. Adve242a8082002-05-19 15:25:51 +0000720// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000721// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000722// The generated instructions are returned in `mvec'.
723// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000724// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000725//
726void
727UltraSparcInstrInfo::CreateSignExtensionInstructions(
728 const TargetMachine& target,
729 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000730 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000731 Value* destVal,
732 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000733 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000734 MachineCodeForInstruction& mcfi) const
735{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000736 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000737 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000738}
739
740
741// Create instruction sequence to produce a zero-extended register value
742// from an arbitrary-sized integer value (sized in bits, not bytes).
743// For SPARC v9, we sign-extend the given operand using SLL; SRL.
744// The generated instructions are returned in `mvec'.
745// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000746// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000747//
748void
749UltraSparcInstrInfo::CreateZeroExtensionInstructions(
750 const TargetMachine& target,
751 Function* F,
752 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000753 Value* destVal,
754 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000755 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000756 MachineCodeForInstruction& mcfi) const
757{
758 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000759 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000760}