blob: 1807ea04144610aa304f8200d0a4ce554908637f [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"
Vikram S. Adve30764b82001-10-18 00:01:48 +000016
Vikram S. Adve53fd4002002-07-10 21:39:50 +000017static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
18static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
19
20
Chris Lattner795ba6c2003-01-15 21:36:50 +000021//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000022// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000023//
Vikram S. Advee6124d32003-07-29 19:59:23 +000024// Function to get the value of an integral constant in the form
25// that must be put into the machine register. The specified constant is
26// interpreted as (i.e., converted if necessary to) the specified destination
27// type. The result is always returned as an uint64_t, since the representation
28// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000029//
30// isValidConstant is set to true if a valid constant was found.
31//---------------------------------------------------------------------------
32
Vikram S. Advee6124d32003-07-29 19:59:23 +000033uint64_t
34UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
35 const Value *V,
36 const Type *destType,
37 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000038{
Chris Lattner795ba6c2003-01-15 21:36:50 +000039 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000040 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000041
Vikram S. Advee6124d32003-07-29 19:59:23 +000042 if (! destType->isIntegral() && ! isa<PointerType>(destType))
43 return C;
44
45 if (! isa<Constant>(V))
46 return C;
47
48 // ConstantPointerRef: no conversions needed: get value and return it
49 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
50 // A ConstantPointerRef is just a reference to GlobalValue.
51 isValidConstant = true; // may be overwritten by recursive call
52 return (CPR->isNullValue()? 0
53 : ConvertConstantToIntType(target, CPR->getValue(), destType,
54 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000055 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000056
57 // ConstantBool: no conversions needed: get value and return it
58 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
59 isValidConstant = true;
60 return (uint64_t) CB->getValue();
61 }
62
63 // For other types of constants, some conversion may be needed.
64 // First, extract the constant operand according to its own type
65 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
66 switch(CE->getOpcode()) {
67 case Instruction::Cast: // recursively get the value as cast
68 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
69 isValidConstant);
70 break;
71 default: // not simplifying other ConstantExprs
72 break;
73 }
74 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
75 isValidConstant = true;
76 C = CI->getRawValue();
77 }
78 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
79 isValidConstant = true;
80 double fC = CFP->getValue();
81 C = (destType->isSigned()? (uint64_t) (int64_t) fC
82 : (uint64_t) fC);
83 }
84
85 // Now if a valid value was found, convert it to destType.
86 if (isValidConstant) {
87 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
88 unsigned destSize = target.getTargetData().getTypeSize(destType);
89 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
90 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
91
92 if (destType->isSigned()) {
93 if (opSize > destSize) // operand is larger than dest:
94 C = C & maskHi; // mask high bits
95
96 if (opSize > destSize ||
97 (opSize == destSize && ! V->getType()->isSigned()))
98 if (C & (1U << (8*destSize - 1)))
99 C = C | ~maskHi; // sign-extend from destSize to 64 bits
100 }
101 else {
102 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
103 // operand is larger than dest,
104 // OR both are equal but smaller than the full register size
105 // AND operand is signed, so it may have extra sign bits:
106 // mask high bits
107 C = C & maskHi;
108 }
109 }
110 }
111
112 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000113}
114
115
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000116//----------------------------------------------------------------------------
117// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000118//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000119// Set a 32-bit unsigned constant in the register `dest', using
120// SETHI, OR in the worst case. This function correctly emulates
121// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
122//
123// The isSigned=true case is used to implement SETSW without duplicating code.
124//
125// Optimize some common cases:
126// (1) Small value that fits in simm13 field of OR: don't need SETHI.
127// (2) isSigned = true and C is a small negative signed value, i.e.,
128// high bits are 1, and the remaining bits fit in simm13(OR).
129//----------------------------------------------------------------------------
130
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000131static inline void
132CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000133 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000134 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000135{
136 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000137
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000138 // In order to get efficient code, we should not generate the SETHI if
139 // all high bits are 1 (i.e., this is a small signed value that fits in
140 // the simm13 field of OR). So we check for and handle that case specially.
141 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
142 // In fact, sC == -sC, so we have to check for this explicitly.
143 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000144 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
145
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000146 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000147 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
148 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
149 miSETHI->setOperandHi32(0);
150 mvec.push_back(miSETHI);
151 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000152
153 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
154 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000155 if (miSETHI==NULL || C & MAXLO) {
156 if (miSETHI) {
157 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000158 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000159 miOR->setOperandLo32(1);
160 } else {
161 // unsigned or small signed value that fits in simm13 field of OR
162 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Misha Brukman71ed1c92003-05-27 22:35:43 +0000163 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
Misha Brukman81b06862003-05-21 18:48:06 +0000164 .getZeroRegNum())
165 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000166 }
Misha Brukman81b06862003-05-21 18:48:06 +0000167 mvec.push_back(miOR);
168 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000169
170 assert((miSETHI || miOR) && "Oops, no code was generated!");
171}
172
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000173
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000174//----------------------------------------------------------------------------
175// Function: CreateSETSWConst
176//
177// Set a 32-bit signed constant in the register `dest', with sign-extension
178// to 64 bits. This uses SETHI, OR, SRA in the worst case.
179// This function correctly emulates the SETSW pseudo-op for SPARC v9.
180//
181// Optimize the same cases as SETUWConst, plus:
182// (1) SRA is not needed for positive or small negative values.
183//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000184
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000185static inline void
186CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000187 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000188{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000189 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000190 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
191
Vikram S. Advec2f09392003-05-25 21:58:11 +0000192 // Sign-extend to the high 32 bits if needed.
193 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
194 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000195 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000196}
197
198
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000199//----------------------------------------------------------------------------
200// Function: CreateSETXConst
201//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000202// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000203// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
204// This function correctly emulates the SETX pseudo-op for SPARC v9.
205//
206// Optimize the same cases as SETUWConst for each 32 bit word.
207//----------------------------------------------------------------------------
208
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000209static inline void
210CreateSETXConst(const TargetMachine& target, uint64_t C,
211 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000212 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000213{
214 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
215
216 MachineInstr* MI;
217
218 // Code to set the upper 32 bits of the value in register `tmpReg'
219 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
220
221 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000222 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000223 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000224
225 // Code to set the low 32 bits of the value in register `dest'
226 CreateSETUWConst(target, C, dest, mvec);
227
228 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000229 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000230}
231
232
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000233//----------------------------------------------------------------------------
234// Function: CreateSETUWLabel
235//
236// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
237//----------------------------------------------------------------------------
238
239static inline void
240CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000241 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000242{
243 MachineInstr* MI;
244
245 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000246 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000247 MI->setOperandHi32(0);
248 mvec.push_back(MI);
249
250 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000251 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000252 MI->setOperandLo32(1);
253 mvec.push_back(MI);
254}
255
256
257//----------------------------------------------------------------------------
258// Function: CreateSETXLabel
259//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000260// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000261//----------------------------------------------------------------------------
262
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000263static inline void
264CreateSETXLabel(const TargetMachine& target,
265 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000266 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000267{
268 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
269 "I only know about constant values and global addresses");
270
271 MachineInstr* MI;
272
Misha Brukmana98cd452003-05-20 20:32:24 +0000273 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000274 MI->setOperandHi64(0);
275 mvec.push_back(MI);
276
Misha Brukman71ed1c92003-05-27 22:35:43 +0000277 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000278 MI->setOperandLo64(1);
279 mvec.push_back(MI);
280
Misha Brukman71ed1c92003-05-27 22:35:43 +0000281 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000282 .addRegDef(tmpReg));
283 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000284 MI->setOperandHi32(0);
285 mvec.push_back(MI);
286
Misha Brukman71ed1c92003-05-27 22:35:43 +0000287 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000288 mvec.push_back(MI);
289
Misha Brukman71ed1c92003-05-27 22:35:43 +0000290 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000291 MI->setOperandLo32(1);
292 mvec.push_back(MI);
293}
294
Vikram S. Adve30764b82001-10-18 00:01:48 +0000295
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000296//----------------------------------------------------------------------------
297// Function: CreateUIntSetInstruction
298//
299// Create code to Set an unsigned constant in the register `dest'.
300// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
301// CreateSETSWConst is an optimization for the case that the unsigned value
302// has all ones in the 33 high bits (so that sign-extension sets them all).
303//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000304
Vikram S. Adve242a8082002-05-19 15:25:51 +0000305static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000306CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000307 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000308 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000309 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000310{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000311 static const uint64_t lo32 = (uint32_t) ~0;
312 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
313 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000314 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000315 // All high 33 (not 32) bits are 1s: sign-extension will take care
316 // of high 32 bits, so use the sequence for signed int
317 CreateSETSWConst(target, (int32_t) C, dest, mvec);
318 } else if (C > lo32) {
319 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000320 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000321 CreateSETXConst(target, C, tmpReg, dest, mvec);
322 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000323}
324
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000325
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000326//----------------------------------------------------------------------------
327// Function: CreateIntSetInstruction
328//
329// Create code to Set a signed constant in the register `dest'.
330// Really the same as CreateUIntSetInstruction.
331//----------------------------------------------------------------------------
332
333static inline void
334CreateIntSetInstruction(const TargetMachine& target,
335 int64_t C, Instruction* dest,
336 std::vector<MachineInstr*>& mvec,
337 MachineCodeForInstruction& mcfi)
338{
339 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
340}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000341
Vikram S. Adve30764b82001-10-18 00:01:48 +0000342
343//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000344// Create a table of LLVM opcode -> max. immediate constant likely to
345// be usable for that operation.
346//---------------------------------------------------------------------------
347
348// Entry == 0 ==> no immediate constant field exists at all.
349// Entry > 0 ==> abs(immediate constant) <= Entry
350//
Misha Brukmana98cd452003-05-20 20:32:24 +0000351std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000352
353static int
354MaxConstantForInstr(unsigned llvmOpCode)
355{
356 int modelOpCode = -1;
357
Chris Lattner0b16ae22002-10-13 19:39:16 +0000358 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
359 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000360 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000361 else
362 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000363 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000364
365 case Instruction::Malloc:
366 case Instruction::Alloca:
367 case Instruction::GetElementPtr:
368 case Instruction::PHINode:
369 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000370 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000371
372 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000373 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000374
375 default: break;
376 };
377
378 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
379}
380
381static void
382InitializeMaxConstantsTable()
383{
384 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000385 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000386 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000387 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000388 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000389 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000390 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000391 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000392 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000393 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000394 MaxConstantsTable[op] = MaxConstantForInstr(op);
395}
396
397
398//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000399// class UltraSparcInstrInfo
400//
401// Purpose:
402// Information about individual instructions.
403// Most information is stored in the SparcMachineInstrDesc array above.
404// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000405// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000406//---------------------------------------------------------------------------
407
408/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000409UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000410 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000411 /*descSize = */ V9::NUM_TOTAL_OPCODES,
412 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000413{
Vikram S. Adve49001162002-09-16 15:56:01 +0000414 InitializeMaxConstantsTable();
415}
416
417bool
418UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
419 const Instruction* I) const
420{
421 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
422 return true;
423
424 if (isa<ConstantPointerNull>(CV)) // can always use %g0
425 return false;
426
Chris Lattnerc07736a2003-07-23 15:22:26 +0000427 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
428 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000429
430 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000431 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000432
433 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000434}
435
Vikram S. Advee76af292002-03-18 03:09:15 +0000436//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000437// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000438// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000439// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000440// The generated instructions are returned in `mvec'.
441// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000442// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000443//
444void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000445UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
446 Function* F,
447 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000448 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000449 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000450 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000451{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000452 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000453 "I only know about constant values and global addresses");
454
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000455 // Use a "set" instruction for known constants or symbolic constants (labels)
456 // that can go in an integer reg.
457 // We have to use a "load" instruction for all other constants,
458 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000459 //
460 const Type* valType = val->getType();
461
Vikram S. Advee6124d32003-07-29 19:59:23 +0000462 // A ConstantPointerRef is just a reference to GlobalValue.
463 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000464 val = cast<ConstantPointerRef>(val)->getValue();
465
Misha Brukman81b06862003-05-21 18:48:06 +0000466 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000467 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000468 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000469 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000470 return;
471 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000472
Vikram S. Advee6124d32003-07-29 19:59:23 +0000473 bool isValid;
474 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
475 if (isValid) {
476 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000477 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000478 else
479 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000480
Misha Brukman81b06862003-05-21 18:48:06 +0000481 } else {
482 // Make an instruction sequence to load the constant, viz:
483 // SETX <addr-of-constant>, tmpReg, addrReg
484 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000485
Misha Brukman81b06862003-05-21 18:48:06 +0000486 // First, create a tmp register to be used by the SETX sequence.
487 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000488 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000489
Misha Brukman81b06862003-05-21 18:48:06 +0000490 // Create another TmpInstruction for the address register
491 TmpInstruction* addrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000492 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000493
Misha Brukman81b06862003-05-21 18:48:06 +0000494 // Put the address (a symbolic name) into a register
495 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000496
Misha Brukman81b06862003-05-21 18:48:06 +0000497 // Generate the load instruction
498 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
499 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000500 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman81b06862003-05-21 18:48:06 +0000501 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
502 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000503
Misha Brukman81b06862003-05-21 18:48:06 +0000504 // Make sure constant is emitted to constant pool in assembly code.
505 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
506 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000507}
508
509
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000510// Create an instruction sequence to copy an integer register `val'
511// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000512// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000513// The generated instructions are returned in `mvec'.
514// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000515// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000516//
517void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000518UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
519 Function* F,
520 Value* val,
521 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000522 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000523 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000524{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000525 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
526 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000527 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000528 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000529
530 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000531 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000532
533 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000534 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000535
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000536 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000537 // The store and load opCodes are based on the size of the source value.
538 // If the value is smaller than 32 bits, we must sign- or zero-extend it
539 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000540 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000541 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
542 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000543 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
544 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000545 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000546 if (val->getType()->isSigned())
547 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
548 mvec, mcfi);
549 else
550 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
551 mvec, mcfi);
552 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000553
554 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000555 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
556 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
557 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000558 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000559
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000560 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000561 // The type of the load opCode is the floating point type that matches the
562 // stored type in size:
563 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000564 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000565 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000566 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
567 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
568 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000569 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000570}
571
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000572// Similarly, create an instruction sequence to copy an FP register
573// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000574// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000575// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
576// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000577//
578void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000579UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
580 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000581 Value* val,
582 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000583 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000584 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000585{
Vikram S. Advec190c012002-07-31 21:13:31 +0000586 const Type* opTy = val->getType();
587 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000588
Vikram S. Advec190c012002-07-31 21:13:31 +0000589 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000590 assert((destTy->isIntegral() || isa<PointerType>(destTy))
591 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000592
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000593 // FIXME: For now, we allocate permanent space because the stack frame
594 // manager does not allow locals to be allocated (e.g., for alloca) after
595 // a temp is allocated!
596 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000597 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000598
Chris Lattner54e898e2003-01-15 19:23:34 +0000599 unsigned FPReg = target.getRegInfo().getFramePointer();
600
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000601 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000602 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000603 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000604 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
605 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
606 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000607 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000608
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000609 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000610 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000611 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000612 // On SparcV9: int for float, long for double.
613 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000614 // ensure correct sign-extension for UByte, UShort or UInt:
615 //
616 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000617 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
618 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
619 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000620 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000621}
622
623
624// Create instruction(s) to copy src to dest, for arbitrary types
625// The generated instructions are returned in `mvec'.
626// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000627// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000628//
629void
630UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
631 Function *F,
632 Value* src,
633 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000634 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000635 MachineCodeForInstruction& mcfi) const
636{
637 bool loadConstantToReg = false;
638
639 const Type* resultType = dest->getType();
640
641 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000642 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000643 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
644 return;
645 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646
647 // if `src' is a constant that doesn't fit in the immed field or if it is
648 // a global variable (i.e., a constant address), generate a load
649 // instruction instead of an add
650 //
Misha Brukman81b06862003-05-21 18:48:06 +0000651 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000652 unsigned int machineRegNum;
653 int64_t immedValue;
654 MachineOperand::MachineOperandType opType =
655 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
656 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000657
Misha Brukmana98cd452003-05-20 20:32:24 +0000658 if (opType == MachineOperand::MO_VirtualRegister)
659 loadConstantToReg = true;
660 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000661 else if (isa<GlobalValue>(src))
662 loadConstantToReg = true;
663
Misha Brukman81b06862003-05-21 18:48:06 +0000664 if (loadConstantToReg) {
665 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000666 // Insert instructions to "load" the constant into a register
667 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
668 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000669 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000670 // Create a reg-to-reg copy instruction for the given type:
671 // -- For FP values, create a FMOVS or FMOVD instruction
672 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
673 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000674 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000675 MachineInstr* MI;
676 if (resultType->isFloatingPoint())
677 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
678 .addReg(src).addRegDef(dest));
679 else {
680 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
681 MI = (BuildMI(opCode, 3)
682 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
683 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000684 mvec.push_back(MI);
685 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000686}
687
688
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000689// Helper function for sign-extension and zero-extension.
690// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
691inline void
692CreateBitExtensionInstructions(bool signExtend,
693 const TargetMachine& target,
694 Function* F,
695 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000696 Value* destVal,
697 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000698 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000699 MachineCodeForInstruction& mcfi)
700{
701 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000702
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000703 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
704
Misha Brukman81b06862003-05-21 18:48:06 +0000705 if (numLowBits < 32) {
706 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000707 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000708 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000709 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000710 .addZImm(32-numLowBits).addRegDef(tmpI));
711 srcVal = tmpI;
712 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000713
Misha Brukmand36e30e2003-06-06 09:52:23 +0000714 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000715 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000716}
717
718
Vikram S. Adve242a8082002-05-19 15:25:51 +0000719// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000720// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000721// The generated instructions are returned in `mvec'.
722// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000723// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000724//
725void
726UltraSparcInstrInfo::CreateSignExtensionInstructions(
727 const TargetMachine& target,
728 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000729 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000730 Value* destVal,
731 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000732 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000733 MachineCodeForInstruction& mcfi) const
734{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000735 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000736 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000737}
738
739
740// Create instruction sequence to produce a zero-extended register value
741// from an arbitrary-sized integer value (sized in bits, not bytes).
742// For SPARC v9, we sign-extend the given operand using SLL; SRL.
743// The generated instructions are returned in `mvec'.
744// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000745// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000746//
747void
748UltraSparcInstrInfo::CreateZeroExtensionInstructions(
749 const TargetMachine& target,
750 Function* F,
751 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000752 Value* destVal,
753 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000754 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000755 MachineCodeForInstruction& mcfi) const
756{
757 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000758 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000759}