blob: 0a3ccc84e837b88dced8a126bc00cb24afb9801c [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrInfo.cpp ------------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner035dfbe2002-08-09 20:08:06 +00009//
10//===----------------------------------------------------------------------===//
Vikram S. Adve30764b82001-10-18 00:01:48 +000011
12#include "SparcInternals.h"
13#include "SparcInstrSelectionSupport.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000014#include "llvm/CodeGen/InstrSelection.h"
15#include "llvm/CodeGen/InstrSelectionSupport.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner2ef9a6a2002-12-28 20:18:21 +000017#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000020#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000023
Vikram S. Adve53fd4002002-07-10 21:39:50 +000024static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
25static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
26
27
Chris Lattner795ba6c2003-01-15 21:36:50 +000028//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000029// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000030//
Vikram S. Advee6124d32003-07-29 19:59:23 +000031// Function to get the value of an integral constant in the form
32// that must be put into the machine register. The specified constant is
33// interpreted as (i.e., converted if necessary to) the specified destination
34// type. The result is always returned as an uint64_t, since the representation
35// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000036//
37// isValidConstant is set to true if a valid constant was found.
38//---------------------------------------------------------------------------
39
Vikram S. Advee6124d32003-07-29 19:59:23 +000040uint64_t
41UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
42 const Value *V,
43 const Type *destType,
44 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000045{
Chris Lattner795ba6c2003-01-15 21:36:50 +000046 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000047 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000048
Vikram S. Advee6124d32003-07-29 19:59:23 +000049 if (! destType->isIntegral() && ! isa<PointerType>(destType))
50 return C;
51
52 if (! isa<Constant>(V))
53 return C;
54
55 // ConstantPointerRef: no conversions needed: get value and return it
56 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
57 // A ConstantPointerRef is just a reference to GlobalValue.
58 isValidConstant = true; // may be overwritten by recursive call
59 return (CPR->isNullValue()? 0
60 : ConvertConstantToIntType(target, CPR->getValue(), destType,
61 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000062 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000063
64 // ConstantBool: no conversions needed: get value and return it
65 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
66 isValidConstant = true;
67 return (uint64_t) CB->getValue();
68 }
69
70 // For other types of constants, some conversion may be needed.
71 // First, extract the constant operand according to its own type
72 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
73 switch(CE->getOpcode()) {
74 case Instruction::Cast: // recursively get the value as cast
75 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
76 isValidConstant);
77 break;
78 default: // not simplifying other ConstantExprs
79 break;
80 }
81 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
82 isValidConstant = true;
83 C = CI->getRawValue();
84 }
85 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
86 isValidConstant = true;
87 double fC = CFP->getValue();
88 C = (destType->isSigned()? (uint64_t) (int64_t) fC
89 : (uint64_t) fC);
90 }
91
92 // Now if a valid value was found, convert it to destType.
93 if (isValidConstant) {
94 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
95 unsigned destSize = target.getTargetData().getTypeSize(destType);
96 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
97 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
98
99 if (destType->isSigned()) {
100 if (opSize > destSize) // operand is larger than dest:
101 C = C & maskHi; // mask high bits
102
103 if (opSize > destSize ||
104 (opSize == destSize && ! V->getType()->isSigned()))
105 if (C & (1U << (8*destSize - 1)))
106 C = C | ~maskHi; // sign-extend from destSize to 64 bits
107 }
108 else {
109 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
110 // operand is larger than dest,
111 // OR both are equal but smaller than the full register size
112 // AND operand is signed, so it may have extra sign bits:
113 // mask high bits
114 C = C & maskHi;
115 }
116 }
117 }
118
119 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000120}
121
122
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000123//----------------------------------------------------------------------------
124// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000125//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000126// Set a 32-bit unsigned constant in the register `dest', using
127// SETHI, OR in the worst case. This function correctly emulates
128// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
129//
130// The isSigned=true case is used to implement SETSW without duplicating code.
131//
132// Optimize some common cases:
133// (1) Small value that fits in simm13 field of OR: don't need SETHI.
134// (2) isSigned = true and C is a small negative signed value, i.e.,
135// high bits are 1, and the remaining bits fit in simm13(OR).
136//----------------------------------------------------------------------------
137
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000138static inline void
139CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000140 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000141 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000142{
143 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000144
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000145 // In order to get efficient code, we should not generate the SETHI if
146 // all high bits are 1 (i.e., this is a small signed value that fits in
147 // the simm13 field of OR). So we check for and handle that case specially.
148 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
149 // In fact, sC == -sC, so we have to check for this explicitly.
150 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000151 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
152
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000153 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000154 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
155 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
156 miSETHI->setOperandHi32(0);
157 mvec.push_back(miSETHI);
158 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000159
160 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
161 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000162 if (miSETHI==NULL || C & MAXLO) {
163 if (miSETHI) {
164 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000165 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000166 miOR->setOperandLo32(1);
167 } else {
168 // unsigned or small signed value that fits in simm13 field of OR
169 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Misha Brukman71ed1c92003-05-27 22:35:43 +0000170 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
Misha Brukman81b06862003-05-21 18:48:06 +0000171 .getZeroRegNum())
172 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000173 }
Misha Brukman81b06862003-05-21 18:48:06 +0000174 mvec.push_back(miOR);
175 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000176
177 assert((miSETHI || miOR) && "Oops, no code was generated!");
178}
179
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000180
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000181//----------------------------------------------------------------------------
182// Function: CreateSETSWConst
183//
184// Set a 32-bit signed constant in the register `dest', with sign-extension
185// to 64 bits. This uses SETHI, OR, SRA in the worst case.
186// This function correctly emulates the SETSW pseudo-op for SPARC v9.
187//
188// Optimize the same cases as SETUWConst, plus:
189// (1) SRA is not needed for positive or small negative values.
190//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000191
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000192static inline void
193CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000194 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000195{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000196 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000197 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
198
Vikram S. Advec2f09392003-05-25 21:58:11 +0000199 // Sign-extend to the high 32 bits if needed.
200 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
201 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000202 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000203}
204
205
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000206//----------------------------------------------------------------------------
207// Function: CreateSETXConst
208//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000209// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000210// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
211// This function correctly emulates the SETX pseudo-op for SPARC v9.
212//
213// Optimize the same cases as SETUWConst for each 32 bit word.
214//----------------------------------------------------------------------------
215
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000216static inline void
217CreateSETXConst(const TargetMachine& target, uint64_t C,
218 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000219 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000220{
221 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
222
223 MachineInstr* MI;
224
225 // Code to set the upper 32 bits of the value in register `tmpReg'
226 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
227
228 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000229 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000230 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000231
232 // Code to set the low 32 bits of the value in register `dest'
233 CreateSETUWConst(target, C, dest, mvec);
234
235 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000236 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000237}
238
239
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000240//----------------------------------------------------------------------------
241// Function: CreateSETUWLabel
242//
243// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
244//----------------------------------------------------------------------------
245
246static inline void
247CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000248 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000249{
250 MachineInstr* MI;
251
252 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000253 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000254 MI->setOperandHi32(0);
255 mvec.push_back(MI);
256
257 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000258 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000259 MI->setOperandLo32(1);
260 mvec.push_back(MI);
261}
262
263
264//----------------------------------------------------------------------------
265// Function: CreateSETXLabel
266//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000267// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000268//----------------------------------------------------------------------------
269
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000270static inline void
271CreateSETXLabel(const TargetMachine& target,
272 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000273 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000274{
275 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
276 "I only know about constant values and global addresses");
277
278 MachineInstr* MI;
279
Misha Brukmana98cd452003-05-20 20:32:24 +0000280 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000281 MI->setOperandHi64(0);
282 mvec.push_back(MI);
283
Misha Brukman71ed1c92003-05-27 22:35:43 +0000284 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000285 MI->setOperandLo64(1);
286 mvec.push_back(MI);
287
Misha Brukman71ed1c92003-05-27 22:35:43 +0000288 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000289 .addRegDef(tmpReg));
290 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000291 MI->setOperandHi32(0);
292 mvec.push_back(MI);
293
Misha Brukman71ed1c92003-05-27 22:35:43 +0000294 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000295 mvec.push_back(MI);
296
Misha Brukman71ed1c92003-05-27 22:35:43 +0000297 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000298 MI->setOperandLo32(1);
299 mvec.push_back(MI);
300}
301
Vikram S. Adve30764b82001-10-18 00:01:48 +0000302
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000303//----------------------------------------------------------------------------
304// Function: CreateUIntSetInstruction
305//
306// Create code to Set an unsigned constant in the register `dest'.
307// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
308// CreateSETSWConst is an optimization for the case that the unsigned value
309// has all ones in the 33 high bits (so that sign-extension sets them all).
310//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000311
Vikram S. Adve242a8082002-05-19 15:25:51 +0000312static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000313CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000314 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000315 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000316 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000317{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000318 static const uint64_t lo32 = (uint32_t) ~0;
319 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
320 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000321 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000322 // All high 33 (not 32) bits are 1s: sign-extension will take care
323 // of high 32 bits, so use the sequence for signed int
324 CreateSETSWConst(target, (int32_t) C, dest, mvec);
325 } else if (C > lo32) {
326 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000327 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000328 CreateSETXConst(target, C, tmpReg, dest, mvec);
329 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000330}
331
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000332
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000333//----------------------------------------------------------------------------
334// Function: CreateIntSetInstruction
335//
336// Create code to Set a signed constant in the register `dest'.
337// Really the same as CreateUIntSetInstruction.
338//----------------------------------------------------------------------------
339
340static inline void
341CreateIntSetInstruction(const TargetMachine& target,
342 int64_t C, Instruction* dest,
343 std::vector<MachineInstr*>& mvec,
344 MachineCodeForInstruction& mcfi)
345{
346 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
347}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000348
Vikram S. Adve30764b82001-10-18 00:01:48 +0000349
350//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000351// Create a table of LLVM opcode -> max. immediate constant likely to
352// be usable for that operation.
353//---------------------------------------------------------------------------
354
355// Entry == 0 ==> no immediate constant field exists at all.
356// Entry > 0 ==> abs(immediate constant) <= Entry
357//
Misha Brukmana98cd452003-05-20 20:32:24 +0000358std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000359
360static int
361MaxConstantForInstr(unsigned llvmOpCode)
362{
363 int modelOpCode = -1;
364
Chris Lattner0b16ae22002-10-13 19:39:16 +0000365 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
366 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000367 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000368 else
369 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000370 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000371
372 case Instruction::Malloc:
373 case Instruction::Alloca:
374 case Instruction::GetElementPtr:
Chris Lattner3b237fc2003-10-19 21:34:28 +0000375 case Instruction::PHI:
Vikram S. Adve49001162002-09-16 15:56:01 +0000376 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000377 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000378
379 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000380 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000381
382 default: break;
383 };
384
385 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
386}
387
388static void
389InitializeMaxConstantsTable()
390{
391 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000392 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000393 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000394 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000395 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000396 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000397 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000398 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000399 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000400 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000401 MaxConstantsTable[op] = MaxConstantForInstr(op);
402}
403
404
405//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000406// class UltraSparcInstrInfo
407//
408// Purpose:
409// Information about individual instructions.
410// Most information is stored in the SparcMachineInstrDesc array above.
411// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000412// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000413//---------------------------------------------------------------------------
414
415/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000416UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000417 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000418 /*descSize = */ V9::NUM_TOTAL_OPCODES,
419 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000420{
Vikram S. Adve49001162002-09-16 15:56:01 +0000421 InitializeMaxConstantsTable();
422}
423
424bool
425UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
426 const Instruction* I) const
427{
428 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
429 return true;
430
431 if (isa<ConstantPointerNull>(CV)) // can always use %g0
432 return false;
433
Chris Lattnerc07736a2003-07-23 15:22:26 +0000434 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
435 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000436
437 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000438 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000439
440 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000441}
442
Vikram S. Advee76af292002-03-18 03:09:15 +0000443//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000444// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000445// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000446// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000447// The generated instructions are returned in `mvec'.
448// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000449// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000450//
451void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000452UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
453 Function* F,
454 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000455 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000456 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000457 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000458{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000459 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000460 "I only know about constant values and global addresses");
461
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000462 // Use a "set" instruction for known constants or symbolic constants (labels)
463 // that can go in an integer reg.
464 // We have to use a "load" instruction for all other constants,
465 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000466 //
467 const Type* valType = val->getType();
468
Vikram S. Advee6124d32003-07-29 19:59:23 +0000469 // A ConstantPointerRef is just a reference to GlobalValue.
470 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000471 val = cast<ConstantPointerRef>(val)->getValue();
472
Misha Brukman81b06862003-05-21 18:48:06 +0000473 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000474 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000475 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000476 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000477 return;
478 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000479
Vikram S. Advee6124d32003-07-29 19:59:23 +0000480 bool isValid;
481 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
482 if (isValid) {
483 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000484 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000485 else
486 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000487
Misha Brukman81b06862003-05-21 18:48:06 +0000488 } else {
489 // Make an instruction sequence to load the constant, viz:
490 // SETX <addr-of-constant>, tmpReg, addrReg
491 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000492
Misha Brukman81b06862003-05-21 18:48:06 +0000493 // First, create a tmp register to be used by the SETX sequence.
494 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000495 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000496
Misha Brukman81b06862003-05-21 18:48:06 +0000497 // Create another TmpInstruction for the address register
498 TmpInstruction* addrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000499 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000500
Misha Brukman81b06862003-05-21 18:48:06 +0000501 // Put the address (a symbolic name) into a register
502 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000503
Misha Brukman81b06862003-05-21 18:48:06 +0000504 // Generate the load instruction
505 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
506 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000507 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman81b06862003-05-21 18:48:06 +0000508 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
509 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000510
Misha Brukman81b06862003-05-21 18:48:06 +0000511 // Make sure constant is emitted to constant pool in assembly code.
512 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
513 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000514}
515
516
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000517// Create an instruction sequence to copy an integer register `val'
518// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000519// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000520// The generated instructions are returned in `mvec'.
521// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000522// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000523//
524void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000525UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
526 Function* F,
527 Value* val,
528 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000529 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000530 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000531{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000532 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
533 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000534 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000535 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000536
537 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000538 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000539
540 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000541 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000542
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000543 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000544 // The store and load opCodes are based on the size of the source value.
545 // If the value is smaller than 32 bits, we must sign- or zero-extend it
546 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000547 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000548 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
549 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000550 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
551 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000552 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000553 if (val->getType()->isSigned())
554 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
555 mvec, mcfi);
556 else
557 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
558 mvec, mcfi);
559 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000560
561 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000562 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
563 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
564 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000565 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000566
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000567 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000568 // The type of the load opCode is the floating point type that matches the
569 // stored type in size:
570 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000571 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000572 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000573 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
574 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
575 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000576 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000577}
578
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000579// Similarly, create an instruction sequence to copy an FP register
580// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000581// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000582// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
583// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000584//
585void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000586UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
587 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000588 Value* val,
589 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000590 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000592{
Vikram S. Advec190c012002-07-31 21:13:31 +0000593 const Type* opTy = val->getType();
594 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000595
Vikram S. Advec190c012002-07-31 21:13:31 +0000596 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000597 assert((destTy->isIntegral() || isa<PointerType>(destTy))
598 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000599
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000600 // FIXME: For now, we allocate permanent space because the stack frame
601 // manager does not allow locals to be allocated (e.g., for alloca) after
602 // a temp is allocated!
603 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000604 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000605
Chris Lattner54e898e2003-01-15 19:23:34 +0000606 unsigned FPReg = target.getRegInfo().getFramePointer();
607
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000608 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000609 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000610 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000611 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
612 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
613 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000614 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000615
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000616 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000617 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000618 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000619 // On SparcV9: int for float, long for double.
620 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000621 // ensure correct sign-extension for UByte, UShort or UInt:
622 //
623 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000624 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
625 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
626 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000627 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000628}
629
630
631// Create instruction(s) to copy src to dest, for arbitrary types
632// The generated instructions are returned in `mvec'.
633// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000634// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000635//
636void
637UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
638 Function *F,
639 Value* src,
640 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000641 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000642 MachineCodeForInstruction& mcfi) const
643{
644 bool loadConstantToReg = false;
645
646 const Type* resultType = dest->getType();
647
648 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000649 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000650 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
651 return;
652 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000653
654 // if `src' is a constant that doesn't fit in the immed field or if it is
655 // a global variable (i.e., a constant address), generate a load
656 // instruction instead of an add
657 //
Misha Brukman81b06862003-05-21 18:48:06 +0000658 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000659 unsigned int machineRegNum;
660 int64_t immedValue;
661 MachineOperand::MachineOperandType opType =
662 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
663 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000664
Misha Brukmana98cd452003-05-20 20:32:24 +0000665 if (opType == MachineOperand::MO_VirtualRegister)
666 loadConstantToReg = true;
667 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668 else if (isa<GlobalValue>(src))
669 loadConstantToReg = true;
670
Misha Brukman81b06862003-05-21 18:48:06 +0000671 if (loadConstantToReg) {
672 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000673 // Insert instructions to "load" the constant into a register
674 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
675 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000676 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000677 // Create a reg-to-reg copy instruction for the given type:
678 // -- For FP values, create a FMOVS or FMOVD instruction
679 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
680 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000681 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000682 MachineInstr* MI;
683 if (resultType->isFloatingPoint())
684 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
685 .addReg(src).addRegDef(dest));
686 else {
687 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
688 MI = (BuildMI(opCode, 3)
689 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
690 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000691 mvec.push_back(MI);
692 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000693}
694
695
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000696// Helper function for sign-extension and zero-extension.
697// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
698inline void
699CreateBitExtensionInstructions(bool signExtend,
700 const TargetMachine& target,
701 Function* F,
702 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000703 Value* destVal,
704 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000705 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000706 MachineCodeForInstruction& mcfi)
707{
708 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000709
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000710 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
711
Misha Brukman81b06862003-05-21 18:48:06 +0000712 if (numLowBits < 32) {
713 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000714 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000715 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000716 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000717 .addZImm(32-numLowBits).addRegDef(tmpI));
718 srcVal = tmpI;
719 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000720
Misha Brukmand36e30e2003-06-06 09:52:23 +0000721 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000722 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000723}
724
725
Vikram S. Adve242a8082002-05-19 15:25:51 +0000726// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000727// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000728// The generated instructions are returned in `mvec'.
729// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000730// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000731//
732void
733UltraSparcInstrInfo::CreateSignExtensionInstructions(
734 const TargetMachine& target,
735 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000736 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000737 Value* destVal,
738 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000739 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000740 MachineCodeForInstruction& mcfi) const
741{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000742 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000743 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000744}
745
746
747// Create instruction sequence to produce a zero-extended register value
748// from an arbitrary-sized integer value (sized in bits, not bytes).
749// For SPARC v9, we sign-extend the given operand using SLL; SRL.
750// The generated instructions are returned in `mvec'.
751// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000752// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000753//
754void
755UltraSparcInstrInfo::CreateZeroExtensionInstructions(
756 const TargetMachine& target,
757 Function* F,
758 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000759 Value* destVal,
760 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000761 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000762 MachineCodeForInstruction& mcfi) const
763{
764 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000765 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000766}