blob: 11b0c7beab58433b4ac82faf0128ec323199f42c [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"
Misha Brukman49ab7f22003-11-07 17:29:48 +000014#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Function.h"
17#include "llvm/iTerminators.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000018#include "llvm/CodeGen/InstrSelection.h"
19#include "llvm/CodeGen/InstrSelectionSupport.h"
Misha Brukman49ab7f22003-11-07 17:29:48 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner2ef9a6a2002-12-28 20:18:21 +000022#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000023#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Vikram S. Adve53fd4002002-07-10 21:39:50 +000028static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
29static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
30
Chris Lattner795ba6c2003-01-15 21:36:50 +000031//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000032// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000033//
Vikram S. Advee6124d32003-07-29 19:59:23 +000034// Function to get the value of an integral constant in the form
35// that must be put into the machine register. The specified constant is
36// interpreted as (i.e., converted if necessary to) the specified destination
37// type. The result is always returned as an uint64_t, since the representation
38// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000039//
40// isValidConstant is set to true if a valid constant was found.
41//---------------------------------------------------------------------------
42
Vikram S. Advee6124d32003-07-29 19:59:23 +000043uint64_t
44UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
45 const Value *V,
46 const Type *destType,
47 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000048{
Chris Lattner795ba6c2003-01-15 21:36:50 +000049 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000050 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000051
Vikram S. Advee6124d32003-07-29 19:59:23 +000052 if (! destType->isIntegral() && ! isa<PointerType>(destType))
53 return C;
54
55 if (! isa<Constant>(V))
56 return C;
57
58 // ConstantPointerRef: no conversions needed: get value and return it
59 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
60 // A ConstantPointerRef is just a reference to GlobalValue.
61 isValidConstant = true; // may be overwritten by recursive call
62 return (CPR->isNullValue()? 0
63 : ConvertConstantToIntType(target, CPR->getValue(), destType,
64 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000065 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000066
67 // ConstantBool: no conversions needed: get value and return it
68 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
69 isValidConstant = true;
70 return (uint64_t) CB->getValue();
71 }
72
73 // For other types of constants, some conversion may be needed.
74 // First, extract the constant operand according to its own type
75 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
76 switch(CE->getOpcode()) {
77 case Instruction::Cast: // recursively get the value as cast
78 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
79 isValidConstant);
80 break;
81 default: // not simplifying other ConstantExprs
82 break;
83 }
84 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
85 isValidConstant = true;
86 C = CI->getRawValue();
87 }
88 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
89 isValidConstant = true;
90 double fC = CFP->getValue();
91 C = (destType->isSigned()? (uint64_t) (int64_t) fC
92 : (uint64_t) fC);
93 }
94
95 // Now if a valid value was found, convert it to destType.
96 if (isValidConstant) {
97 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
98 unsigned destSize = target.getTargetData().getTypeSize(destType);
99 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
100 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
101
102 if (destType->isSigned()) {
103 if (opSize > destSize) // operand is larger than dest:
104 C = C & maskHi; // mask high bits
105
106 if (opSize > destSize ||
107 (opSize == destSize && ! V->getType()->isSigned()))
108 if (C & (1U << (8*destSize - 1)))
109 C = C | ~maskHi; // sign-extend from destSize to 64 bits
110 }
111 else {
112 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
113 // operand is larger than dest,
114 // OR both are equal but smaller than the full register size
115 // AND operand is signed, so it may have extra sign bits:
116 // mask high bits
117 C = C & maskHi;
118 }
119 }
120 }
121
122 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000123}
124
125
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000126//----------------------------------------------------------------------------
127// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000128//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000129// Set a 32-bit unsigned constant in the register `dest', using
130// SETHI, OR in the worst case. This function correctly emulates
131// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
132//
133// The isSigned=true case is used to implement SETSW without duplicating code.
134//
135// Optimize some common cases:
136// (1) Small value that fits in simm13 field of OR: don't need SETHI.
137// (2) isSigned = true and C is a small negative signed value, i.e.,
138// high bits are 1, and the remaining bits fit in simm13(OR).
139//----------------------------------------------------------------------------
140
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000141static inline void
142CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000143 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000144 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000145{
146 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000147
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000148 // In order to get efficient code, we should not generate the SETHI if
149 // all high bits are 1 (i.e., this is a small signed value that fits in
150 // the simm13 field of OR). So we check for and handle that case specially.
151 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
152 // In fact, sC == -sC, so we have to check for this explicitly.
153 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000154 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
155
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000156 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000157 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
158 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
159 miSETHI->setOperandHi32(0);
160 mvec.push_back(miSETHI);
161 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000162
163 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
164 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000165 if (miSETHI==NULL || C & MAXLO) {
166 if (miSETHI) {
167 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000168 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000169 miOR->setOperandLo32(1);
170 } else {
171 // unsigned or small signed value that fits in simm13 field of OR
172 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Misha Brukman71ed1c92003-05-27 22:35:43 +0000173 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
Misha Brukman81b06862003-05-21 18:48:06 +0000174 .getZeroRegNum())
175 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000176 }
Misha Brukman81b06862003-05-21 18:48:06 +0000177 mvec.push_back(miOR);
178 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000179
180 assert((miSETHI || miOR) && "Oops, no code was generated!");
181}
182
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000183
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000184//----------------------------------------------------------------------------
185// Function: CreateSETSWConst
186//
187// Set a 32-bit signed constant in the register `dest', with sign-extension
188// to 64 bits. This uses SETHI, OR, SRA in the worst case.
189// This function correctly emulates the SETSW pseudo-op for SPARC v9.
190//
191// Optimize the same cases as SETUWConst, plus:
192// (1) SRA is not needed for positive or small negative values.
193//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000194
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000195static inline void
196CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000197 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000198{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000199 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000200 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
201
Vikram S. Advec2f09392003-05-25 21:58:11 +0000202 // Sign-extend to the high 32 bits if needed.
203 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
204 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000205 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000206}
207
208
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000209//----------------------------------------------------------------------------
210// Function: CreateSETXConst
211//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000212// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000213// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
214// This function correctly emulates the SETX pseudo-op for SPARC v9.
215//
216// Optimize the same cases as SETUWConst for each 32 bit word.
217//----------------------------------------------------------------------------
218
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000219static inline void
220CreateSETXConst(const TargetMachine& target, uint64_t C,
221 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000222 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000223{
224 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
225
226 MachineInstr* MI;
227
228 // Code to set the upper 32 bits of the value in register `tmpReg'
229 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
230
231 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000232 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000233 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000234
235 // Code to set the low 32 bits of the value in register `dest'
236 CreateSETUWConst(target, C, dest, mvec);
237
238 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000239 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000240}
241
242
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000243//----------------------------------------------------------------------------
244// Function: CreateSETUWLabel
245//
246// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
247//----------------------------------------------------------------------------
248
249static inline void
250CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000251 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000252{
253 MachineInstr* MI;
254
255 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000256 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000257 MI->setOperandHi32(0);
258 mvec.push_back(MI);
259
260 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000261 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000262 MI->setOperandLo32(1);
263 mvec.push_back(MI);
264}
265
266
267//----------------------------------------------------------------------------
268// Function: CreateSETXLabel
269//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000270// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000271//----------------------------------------------------------------------------
272
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000273static inline void
274CreateSETXLabel(const TargetMachine& target,
275 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000276 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000277{
278 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
279 "I only know about constant values and global addresses");
280
281 MachineInstr* MI;
282
Misha Brukmana98cd452003-05-20 20:32:24 +0000283 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000284 MI->setOperandHi64(0);
285 mvec.push_back(MI);
286
Misha Brukman71ed1c92003-05-27 22:35:43 +0000287 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000288 MI->setOperandLo64(1);
289 mvec.push_back(MI);
290
Misha Brukman71ed1c92003-05-27 22:35:43 +0000291 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000292 .addRegDef(tmpReg));
293 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000294 MI->setOperandHi32(0);
295 mvec.push_back(MI);
296
Misha Brukman71ed1c92003-05-27 22:35:43 +0000297 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000298 mvec.push_back(MI);
299
Misha Brukman71ed1c92003-05-27 22:35:43 +0000300 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000301 MI->setOperandLo32(1);
302 mvec.push_back(MI);
303}
304
Vikram S. Adve30764b82001-10-18 00:01:48 +0000305
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000306//----------------------------------------------------------------------------
307// Function: CreateUIntSetInstruction
308//
309// Create code to Set an unsigned constant in the register `dest'.
310// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
311// CreateSETSWConst is an optimization for the case that the unsigned value
312// has all ones in the 33 high bits (so that sign-extension sets them all).
313//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000314
Vikram S. Adve242a8082002-05-19 15:25:51 +0000315static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000316CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000317 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000318 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000319 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000320{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000321 static const uint64_t lo32 = (uint32_t) ~0;
322 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
323 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000324 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000325 // All high 33 (not 32) bits are 1s: sign-extension will take care
326 // of high 32 bits, so use the sequence for signed int
327 CreateSETSWConst(target, (int32_t) C, dest, mvec);
328 } else if (C > lo32) {
329 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000330 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000331 CreateSETXConst(target, C, tmpReg, dest, mvec);
332 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000333}
334
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000335
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000336//----------------------------------------------------------------------------
337// Function: CreateIntSetInstruction
338//
339// Create code to Set a signed constant in the register `dest'.
340// Really the same as CreateUIntSetInstruction.
341//----------------------------------------------------------------------------
342
343static inline void
344CreateIntSetInstruction(const TargetMachine& target,
345 int64_t C, Instruction* dest,
346 std::vector<MachineInstr*>& mvec,
347 MachineCodeForInstruction& mcfi)
348{
349 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
350}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000351
Vikram S. Adve30764b82001-10-18 00:01:48 +0000352
353//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000354// Create a table of LLVM opcode -> max. immediate constant likely to
355// be usable for that operation.
356//---------------------------------------------------------------------------
357
358// Entry == 0 ==> no immediate constant field exists at all.
359// Entry > 0 ==> abs(immediate constant) <= Entry
360//
Misha Brukmana98cd452003-05-20 20:32:24 +0000361std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000362
363static int
364MaxConstantForInstr(unsigned llvmOpCode)
365{
366 int modelOpCode = -1;
367
Chris Lattner0b16ae22002-10-13 19:39:16 +0000368 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
369 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000370 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000371 else
372 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000373 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000374
375 case Instruction::Malloc:
376 case Instruction::Alloca:
377 case Instruction::GetElementPtr:
Chris Lattner3b237fc2003-10-19 21:34:28 +0000378 case Instruction::PHI:
Vikram S. Adve49001162002-09-16 15:56:01 +0000379 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000380 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000381
382 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000383 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000384
385 default: break;
386 };
387
388 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
389}
390
391static void
392InitializeMaxConstantsTable()
393{
394 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000395 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000396 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000397 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000398 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000399 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000400 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000401 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000402 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000403 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000404 MaxConstantsTable[op] = MaxConstantForInstr(op);
405}
406
407
408//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000409// class UltraSparcInstrInfo
410//
411// Purpose:
412// Information about individual instructions.
413// Most information is stored in the SparcMachineInstrDesc array above.
414// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000415// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000416//---------------------------------------------------------------------------
417
418/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000419UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000420 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000421 /*descSize = */ V9::NUM_TOTAL_OPCODES,
422 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000423{
Vikram S. Adve49001162002-09-16 15:56:01 +0000424 InitializeMaxConstantsTable();
425}
426
427bool
428UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
429 const Instruction* I) const
430{
431 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
432 return true;
433
434 if (isa<ConstantPointerNull>(CV)) // can always use %g0
435 return false;
436
Chris Lattnerff3d5d92003-10-21 16:29:23 +0000437 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
438 return false;
439
Chris Lattnerc07736a2003-07-23 15:22:26 +0000440 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
441 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000442
443 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000444 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000445
446 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000447}
448
Vikram S. Advee76af292002-03-18 03:09:15 +0000449//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000450// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000451// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000452// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000453// The generated instructions are returned in `mvec'.
454// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000455// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000456//
457void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000458UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
459 Function* F,
460 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000461 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000462 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000463 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000464{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000465 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000466 "I only know about constant values and global addresses");
467
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000468 // Use a "set" instruction for known constants or symbolic constants (labels)
469 // that can go in an integer reg.
470 // We have to use a "load" instruction for all other constants,
471 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000472 //
473 const Type* valType = val->getType();
474
Vikram S. Advee6124d32003-07-29 19:59:23 +0000475 // A ConstantPointerRef is just a reference to GlobalValue.
476 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000477 val = cast<ConstantPointerRef>(val)->getValue();
478
Misha Brukman81b06862003-05-21 18:48:06 +0000479 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000480 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000481 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000482 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000483 return;
484 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000485
Vikram S. Advee6124d32003-07-29 19:59:23 +0000486 bool isValid;
487 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
488 if (isValid) {
489 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000490 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000491 else
492 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000493
Misha Brukman81b06862003-05-21 18:48:06 +0000494 } else {
495 // Make an instruction sequence to load the constant, viz:
496 // SETX <addr-of-constant>, tmpReg, addrReg
497 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000498
Misha Brukman81b06862003-05-21 18:48:06 +0000499 // First, create a tmp register to be used by the SETX sequence.
500 TmpInstruction* tmpReg =
Misha Brukman49ab7f22003-11-07 17:29:48 +0000501 new TmpInstruction(mcfi, PointerType::get(val->getType()));
Vikram S. Advea2a70942001-10-28 21:41:46 +0000502
Misha Brukman81b06862003-05-21 18:48:06 +0000503 // Create another TmpInstruction for the address register
504 TmpInstruction* addrReg =
Misha Brukman49ab7f22003-11-07 17:29:48 +0000505 new TmpInstruction(mcfi, PointerType::get(val->getType()));
Vikram S. Advee6124d32003-07-29 19:59:23 +0000506
Misha Brukman49ab7f22003-11-07 17:29:48 +0000507 // Get the constant pool index for this constant
508 MachineConstantPool *CP = MachineFunction::get(F).getConstantPool();
509 Constant *C = cast<Constant>(val);
510 unsigned CPI = CP->getConstantPoolIndex(C);
511
512 // Put the address of the constant into a register
513 MachineInstr* MI;
514
515 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(tmpReg);
516 MI->setOperandHi64(0);
517 mvec.push_back(MI);
518
519 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addConstantPoolIndex(CPI)
520 .addRegDef(tmpReg);
521 MI->setOperandLo64(1);
522 mvec.push_back(MI);
523
524 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
525 .addRegDef(tmpReg));
526 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(addrReg);
527 MI->setOperandHi32(0);
528 mvec.push_back(MI);
529
530 MI = BuildMI(V9::ORr, 3).addReg(addrReg).addReg(tmpReg).addRegDef(addrReg);
531 mvec.push_back(MI);
532
533 MI = BuildMI(V9::ORi, 3).addReg(addrReg).addConstantPoolIndex(CPI)
534 .addRegDef(addrReg);
535 MI->setOperandLo32(1);
536 mvec.push_back(MI);
537
538 // Now load the constant from out ConstantPool label
Misha Brukman81b06862003-05-21 18:48:06 +0000539 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000540 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman49ab7f22003-11-07 17:29:48 +0000541 mvec.push_back(BuildMI(Opcode, 3)
542 .addReg(addrReg).addSImm((int64_t)0).addRegDef(dest));
Misha Brukman81b06862003-05-21 18:48:06 +0000543 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000544}
545
546
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000547// Create an instruction sequence to copy an integer register `val'
548// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000549// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000550// The generated instructions are returned in `mvec'.
551// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000552// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000553//
554void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000555UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
556 Function* F,
557 Value* val,
558 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000559 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000560 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000561{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000562 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
563 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000564 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000565 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000566
567 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000568 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000569
570 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000571 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000572
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000573 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000574 // The store and load opCodes are based on the size of the source value.
575 // If the value is smaller than 32 bits, we must sign- or zero-extend it
576 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000577 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000578 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
579 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000580 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
581 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000582 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000583 if (val->getType()->isSigned())
584 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
585 mvec, mcfi);
586 else
587 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
588 mvec, mcfi);
589 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000590
591 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000592 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
593 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
594 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000595 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000596
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000597 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000598 // The type of the load opCode is the floating point type that matches the
599 // stored type in size:
600 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000601 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000602 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000603 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
604 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
605 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000606 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000607}
608
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000609// Similarly, create an instruction sequence to copy an FP register
610// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000611// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000612// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
613// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000614//
615void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000616UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
617 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000618 Value* val,
619 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000620 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000621 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000622{
Vikram S. Advec190c012002-07-31 21:13:31 +0000623 const Type* opTy = val->getType();
624 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000625
Vikram S. Advec190c012002-07-31 21:13:31 +0000626 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000627 assert((destTy->isIntegral() || isa<PointerType>(destTy))
628 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000629
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000630 // FIXME: For now, we allocate permanent space because the stack frame
631 // manager does not allow locals to be allocated (e.g., for alloca) after
632 // a temp is allocated!
633 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000634 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000635
Chris Lattner54e898e2003-01-15 19:23:34 +0000636 unsigned FPReg = target.getRegInfo().getFramePointer();
637
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000638 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000639 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000640 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000641 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
642 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
643 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000644 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000645
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000646 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000647 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000648 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000649 // On SparcV9: int for float, long for double.
650 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000651 // ensure correct sign-extension for UByte, UShort or UInt:
652 //
653 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000654 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
655 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
656 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000657 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658}
659
660
661// Create instruction(s) to copy src to dest, for arbitrary types
662// The generated instructions are returned in `mvec'.
663// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000664// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000665//
666void
667UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
668 Function *F,
669 Value* src,
670 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000671 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000672 MachineCodeForInstruction& mcfi) const
673{
674 bool loadConstantToReg = false;
675
676 const Type* resultType = dest->getType();
677
678 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000679 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000680 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
681 return;
682 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000683
684 // if `src' is a constant that doesn't fit in the immed field or if it is
685 // a global variable (i.e., a constant address), generate a load
686 // instruction instead of an add
687 //
Misha Brukman81b06862003-05-21 18:48:06 +0000688 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000689 unsigned int machineRegNum;
690 int64_t immedValue;
691 MachineOperand::MachineOperandType opType =
692 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
693 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000694
Misha Brukmana98cd452003-05-20 20:32:24 +0000695 if (opType == MachineOperand::MO_VirtualRegister)
696 loadConstantToReg = true;
697 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000698 else if (isa<GlobalValue>(src))
699 loadConstantToReg = true;
700
Misha Brukman81b06862003-05-21 18:48:06 +0000701 if (loadConstantToReg) {
702 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000703 // Insert instructions to "load" the constant into a register
704 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
705 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000706 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000707 // Create a reg-to-reg copy instruction for the given type:
708 // -- For FP values, create a FMOVS or FMOVD instruction
709 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
710 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000711 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000712 MachineInstr* MI;
713 if (resultType->isFloatingPoint())
714 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
715 .addReg(src).addRegDef(dest));
716 else {
717 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
718 MI = (BuildMI(opCode, 3)
719 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
720 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000721 mvec.push_back(MI);
722 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000723}
724
725
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000726// Helper function for sign-extension and zero-extension.
727// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
728inline void
729CreateBitExtensionInstructions(bool signExtend,
730 const TargetMachine& target,
731 Function* F,
732 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000733 Value* destVal,
734 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000735 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000736 MachineCodeForInstruction& mcfi)
737{
738 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000739
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000740 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
741
Misha Brukman81b06862003-05-21 18:48:06 +0000742 if (numLowBits < 32) {
743 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000744 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000745 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000746 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000747 .addZImm(32-numLowBits).addRegDef(tmpI));
748 srcVal = tmpI;
749 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000750
Misha Brukmand36e30e2003-06-06 09:52:23 +0000751 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000752 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000753}
754
755
Vikram S. Adve242a8082002-05-19 15:25:51 +0000756// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000757// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000758// The generated instructions are returned in `mvec'.
759// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000760// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000761//
762void
763UltraSparcInstrInfo::CreateSignExtensionInstructions(
764 const TargetMachine& target,
765 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000766 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000767 Value* destVal,
768 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000769 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000770 MachineCodeForInstruction& mcfi) const
771{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000772 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000773 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000774}
775
776
777// Create instruction sequence to produce a zero-extended register value
778// from an arbitrary-sized integer value (sized in bits, not bytes).
779// For SPARC v9, we sign-extend the given operand using SLL; SRL.
780// The generated instructions are returned in `mvec'.
781// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000782// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000783//
784void
785UltraSparcInstrInfo::CreateZeroExtensionInstructions(
786 const TargetMachine& target,
787 Function* F,
788 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000789 Value* destVal,
790 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000791 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000792 MachineCodeForInstruction& mcfi) const
793{
794 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000795 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000796}
Brian Gaeked0fde302003-11-11 22:41:34 +0000797
798} // End llvm namespace