blob: cbbe154ccfdee16fe0e15400dfda79680645575a [file] [log] [blame]
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +00001//===-- SparcV9InstrInfo.cpp - SparcV9 Instr. Selection Support Methods ---===//
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//
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +000010// This file contains various methods of the class SparcV9InstrInfo, many of
11// which appear to build canned sequences of MachineInstrs, and are
12// used in instruction selection.
13//
Chris Lattner035dfbe2002-08-09 20:08:06 +000014//===----------------------------------------------------------------------===//
Vikram S. Adve30764b82001-10-18 00:01:48 +000015
Misha Brukman49ab7f22003-11-07 17:29:48 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/iTerminators.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000020#include "llvm/CodeGen/InstrSelection.h"
Misha Brukman49ab7f22003-11-07 17:29:48 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000022#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner2ef9a6a2002-12-28 20:18:21 +000023#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000024#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaekee3d68072004-02-25 18:44:15 +000026#include "SparcV9Internals.h"
27#include "SparcV9InstrSelectionSupport.h"
28#include "SparcV9InstrInfo.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
31
Vikram S. Adve53fd4002002-07-10 21:39:50 +000032static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
33static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
34
Chris Lattner795ba6c2003-01-15 21:36:50 +000035//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000036// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000037//
Vikram S. Advee6124d32003-07-29 19:59:23 +000038// Function to get the value of an integral constant in the form
39// that must be put into the machine register. The specified constant is
40// interpreted as (i.e., converted if necessary to) the specified destination
41// type. The result is always returned as an uint64_t, since the representation
42// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000043//
44// isValidConstant is set to true if a valid constant was found.
45//---------------------------------------------------------------------------
46
Vikram S. Advee6124d32003-07-29 19:59:23 +000047uint64_t
Brian Gaekee3d68072004-02-25 18:44:15 +000048SparcV9InstrInfo::ConvertConstantToIntType(const TargetMachine &target,
Vikram S. Advee6124d32003-07-29 19:59:23 +000049 const Value *V,
50 const Type *destType,
51 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000052{
Chris Lattner795ba6c2003-01-15 21:36:50 +000053 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000054 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000055
Vikram S. Advee6124d32003-07-29 19:59:23 +000056 if (! destType->isIntegral() && ! isa<PointerType>(destType))
57 return C;
58
59 if (! isa<Constant>(V))
60 return C;
61
62 // ConstantPointerRef: no conversions needed: get value and return it
63 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
64 // A ConstantPointerRef is just a reference to GlobalValue.
65 isValidConstant = true; // may be overwritten by recursive call
66 return (CPR->isNullValue()? 0
67 : ConvertConstantToIntType(target, CPR->getValue(), destType,
68 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000069 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000070
71 // ConstantBool: no conversions needed: get value and return it
72 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
73 isValidConstant = true;
74 return (uint64_t) CB->getValue();
75 }
76
77 // For other types of constants, some conversion may be needed.
78 // First, extract the constant operand according to its own type
79 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
80 switch(CE->getOpcode()) {
81 case Instruction::Cast: // recursively get the value as cast
82 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
83 isValidConstant);
84 break;
85 default: // not simplifying other ConstantExprs
86 break;
87 }
88 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
89 isValidConstant = true;
90 C = CI->getRawValue();
91 }
92 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
93 isValidConstant = true;
94 double fC = CFP->getValue();
95 C = (destType->isSigned()? (uint64_t) (int64_t) fC
96 : (uint64_t) fC);
97 }
98
99 // Now if a valid value was found, convert it to destType.
100 if (isValidConstant) {
101 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
102 unsigned destSize = target.getTargetData().getTypeSize(destType);
103 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
104 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
105
106 if (destType->isSigned()) {
107 if (opSize > destSize) // operand is larger than dest:
108 C = C & maskHi; // mask high bits
109
110 if (opSize > destSize ||
111 (opSize == destSize && ! V->getType()->isSigned()))
112 if (C & (1U << (8*destSize - 1)))
113 C = C | ~maskHi; // sign-extend from destSize to 64 bits
114 }
115 else {
116 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
117 // operand is larger than dest,
118 // OR both are equal but smaller than the full register size
119 // AND operand is signed, so it may have extra sign bits:
120 // mask high bits
121 C = C & maskHi;
122 }
123 }
124 }
125
126 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000127}
128
129
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000130//----------------------------------------------------------------------------
131// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000132//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000133// Set a 32-bit unsigned constant in the register `dest', using
134// SETHI, OR in the worst case. This function correctly emulates
135// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
136//
137// The isSigned=true case is used to implement SETSW without duplicating code.
138//
139// Optimize some common cases:
140// (1) Small value that fits in simm13 field of OR: don't need SETHI.
141// (2) isSigned = true and C is a small negative signed value, i.e.,
142// high bits are 1, and the remaining bits fit in simm13(OR).
143//----------------------------------------------------------------------------
144
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000145static inline void
146CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000147 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000148 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000149{
150 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000151
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000152 // In order to get efficient code, we should not generate the SETHI if
153 // all high bits are 1 (i.e., this is a small signed value that fits in
154 // the simm13 field of OR). So we check for and handle that case specially.
155 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
156 // In fact, sC == -sC, so we have to check for this explicitly.
157 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000158 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
159
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000160 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000161 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
162 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
163 miSETHI->setOperandHi32(0);
164 mvec.push_back(miSETHI);
165 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000166
167 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
168 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000169 if (miSETHI==NULL || C & MAXLO) {
170 if (miSETHI) {
171 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000172 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000173 miOR->setOperandLo32(1);
174 } else {
175 // unsigned or small signed value that fits in simm13 field of OR
176 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Chris Lattnerd029cd22004-06-02 05:55:25 +0000177 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()->getZeroRegNum())
Misha Brukman81b06862003-05-21 18:48:06 +0000178 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000179 }
Misha Brukman81b06862003-05-21 18:48:06 +0000180 mvec.push_back(miOR);
181 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000182
183 assert((miSETHI || miOR) && "Oops, no code was generated!");
184}
185
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000186
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000187//----------------------------------------------------------------------------
188// Function: CreateSETSWConst
189//
190// Set a 32-bit signed constant in the register `dest', with sign-extension
191// to 64 bits. This uses SETHI, OR, SRA in the worst case.
192// This function correctly emulates the SETSW pseudo-op for SPARC v9.
193//
194// Optimize the same cases as SETUWConst, plus:
195// (1) SRA is not needed for positive or small negative values.
196//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000198static inline void
199CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000200 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000201{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000202 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000203 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
204
Vikram S. Advec2f09392003-05-25 21:58:11 +0000205 // Sign-extend to the high 32 bits if needed.
206 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
207 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000208 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000209}
210
211
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000212//----------------------------------------------------------------------------
213// Function: CreateSETXConst
214//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000215// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000216// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
217// This function correctly emulates the SETX pseudo-op for SPARC v9.
218//
219// Optimize the same cases as SETUWConst for each 32 bit word.
220//----------------------------------------------------------------------------
221
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000222static inline void
223CreateSETXConst(const TargetMachine& target, uint64_t C,
224 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000225 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000226{
227 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
228
229 MachineInstr* MI;
230
231 // Code to set the upper 32 bits of the value in register `tmpReg'
232 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
233
234 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000235 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000236 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000237
238 // Code to set the low 32 bits of the value in register `dest'
239 CreateSETUWConst(target, C, dest, mvec);
240
241 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000242 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000243}
244
245
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000246//----------------------------------------------------------------------------
247// Function: CreateSETUWLabel
248//
249// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
250//----------------------------------------------------------------------------
251
252static inline void
253CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000254 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000255{
256 MachineInstr* MI;
257
258 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000259 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000260 MI->setOperandHi32(0);
261 mvec.push_back(MI);
262
263 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000264 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000265 MI->setOperandLo32(1);
266 mvec.push_back(MI);
267}
268
269
270//----------------------------------------------------------------------------
271// Function: CreateSETXLabel
272//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000273// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000274//----------------------------------------------------------------------------
275
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000276static inline void
277CreateSETXLabel(const TargetMachine& target,
278 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000279 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000280{
281 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
282 "I only know about constant values and global addresses");
283
284 MachineInstr* MI;
285
Misha Brukmana98cd452003-05-20 20:32:24 +0000286 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000287 MI->setOperandHi64(0);
288 mvec.push_back(MI);
289
Misha Brukman71ed1c92003-05-27 22:35:43 +0000290 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000291 MI->setOperandLo64(1);
292 mvec.push_back(MI);
293
Misha Brukman71ed1c92003-05-27 22:35:43 +0000294 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000295 .addRegDef(tmpReg));
296 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000297 MI->setOperandHi32(0);
298 mvec.push_back(MI);
299
Misha Brukman71ed1c92003-05-27 22:35:43 +0000300 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000301 mvec.push_back(MI);
302
Misha Brukman71ed1c92003-05-27 22:35:43 +0000303 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000304 MI->setOperandLo32(1);
305 mvec.push_back(MI);
306}
307
Vikram S. Adve30764b82001-10-18 00:01:48 +0000308
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000309//----------------------------------------------------------------------------
310// Function: CreateUIntSetInstruction
311//
312// Create code to Set an unsigned constant in the register `dest'.
313// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
314// CreateSETSWConst is an optimization for the case that the unsigned value
315// has all ones in the 33 high bits (so that sign-extension sets them all).
316//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000317
Vikram S. Adve242a8082002-05-19 15:25:51 +0000318static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000319CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000320 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000321 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000322 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000323{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000324 static const uint64_t lo32 = (uint32_t) ~0;
325 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
326 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000327 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000328 // All high 33 (not 32) bits are 1s: sign-extension will take care
329 // of high 32 bits, so use the sequence for signed int
330 CreateSETSWConst(target, (int32_t) C, dest, mvec);
331 } else if (C > lo32) {
332 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000333 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000334 CreateSETXConst(target, C, tmpReg, dest, mvec);
335 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000336}
337
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000338
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000339//----------------------------------------------------------------------------
340// Function: CreateIntSetInstruction
341//
342// Create code to Set a signed constant in the register `dest'.
343// Really the same as CreateUIntSetInstruction.
344//----------------------------------------------------------------------------
345
346static inline void
347CreateIntSetInstruction(const TargetMachine& target,
348 int64_t C, Instruction* dest,
349 std::vector<MachineInstr*>& mvec,
350 MachineCodeForInstruction& mcfi)
351{
352 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
353}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000354
Vikram S. Adve30764b82001-10-18 00:01:48 +0000355
356//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000357// Create a table of LLVM opcode -> max. immediate constant likely to
358// be usable for that operation.
359//---------------------------------------------------------------------------
360
361// Entry == 0 ==> no immediate constant field exists at all.
362// Entry > 0 ==> abs(immediate constant) <= Entry
363//
Misha Brukmana98cd452003-05-20 20:32:24 +0000364std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000365
366static int
367MaxConstantForInstr(unsigned llvmOpCode)
368{
369 int modelOpCode = -1;
370
Chris Lattner0b16ae22002-10-13 19:39:16 +0000371 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
372 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000373 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000374 else
375 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000376 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000377
378 case Instruction::Malloc:
379 case Instruction::Alloca:
380 case Instruction::GetElementPtr:
Chris Lattner3b237fc2003-10-19 21:34:28 +0000381 case Instruction::PHI:
Vikram S. Adve49001162002-09-16 15:56:01 +0000382 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000383 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000384
385 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000386 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000387
388 default: break;
389 };
390
Brian Gaekee3d68072004-02-25 18:44:15 +0000391 return (modelOpCode < 0)? 0: SparcV9MachineInstrDesc[modelOpCode].maxImmedConst;
Vikram S. Adve49001162002-09-16 15:56:01 +0000392}
393
394static void
395InitializeMaxConstantsTable()
396{
397 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000398 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000399 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000400 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000401 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000402 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000403 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000404 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000405 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000406 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000407 MaxConstantsTable[op] = MaxConstantForInstr(op);
408}
409
410
411//---------------------------------------------------------------------------
Brian Gaekee3d68072004-02-25 18:44:15 +0000412// class SparcV9InstrInfo
Vikram S. Adve30764b82001-10-18 00:01:48 +0000413//
414// Purpose:
415// Information about individual instructions.
Brian Gaekee3d68072004-02-25 18:44:15 +0000416// Most information is stored in the SparcV9MachineInstrDesc array above.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000417// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000418// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000419//---------------------------------------------------------------------------
420
Brian Gaekee3d68072004-02-25 18:44:15 +0000421SparcV9InstrInfo::SparcV9InstrInfo()
Chris Lattnerdce363d2004-02-29 06:31:44 +0000422 : TargetInstrInfo(SparcV9MachineInstrDesc, V9::NUM_TOTAL_OPCODES) {
Vikram S. Adve49001162002-09-16 15:56:01 +0000423 InitializeMaxConstantsTable();
424}
425
426bool
Brian Gaekee3d68072004-02-25 18:44:15 +0000427SparcV9InstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
Vikram S. Adve49001162002-09-16 15:56:01 +0000428 const Instruction* I) const
429{
430 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
431 return true;
432
433 if (isa<ConstantPointerNull>(CV)) // can always use %g0
434 return false;
435
Chris Lattnerff3d5d92003-10-21 16:29:23 +0000436 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
437 return false;
438
Chris Lattnerc07736a2003-07-23 15:22:26 +0000439 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
440 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000441
442 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000443 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000444
445 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000446}
447
Vikram S. Advee76af292002-03-18 03:09:15 +0000448//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000449// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000450// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000451// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000452// The generated instructions are returned in `mvec'.
453// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000454// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000455//
456void
Brian Gaekee3d68072004-02-25 18:44:15 +0000457SparcV9InstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
Misha Brukmand71295a2003-12-17 22:04:00 +0000458 Function* F,
459 Value* val,
460 Instruction* dest,
461 std::vector<MachineInstr*>& mvec,
462 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000463{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000464 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000465 "I only know about constant values and global addresses");
466
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000467 // Use a "set" instruction for known constants or symbolic constants (labels)
468 // that can go in an integer reg.
469 // We have to use a "load" instruction for all other constants,
470 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000471 //
472 const Type* valType = val->getType();
473
Vikram S. Advee6124d32003-07-29 19:59:23 +0000474 // A ConstantPointerRef is just a reference to GlobalValue.
475 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000476 val = cast<ConstantPointerRef>(val)->getValue();
477
Misha Brukman81b06862003-05-21 18:48:06 +0000478 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000479 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000480 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000481 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000482 return;
483 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000484
Vikram S. Advee6124d32003-07-29 19:59:23 +0000485 bool isValid;
486 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
487 if (isValid) {
488 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000489 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000490 else
491 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000492
Misha Brukman81b06862003-05-21 18:48:06 +0000493 } else {
494 // Make an instruction sequence to load the constant, viz:
495 // SETX <addr-of-constant>, tmpReg, addrReg
496 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000497
Misha Brukman81b06862003-05-21 18:48:06 +0000498 // First, create a tmp register to be used by the SETX sequence.
499 TmpInstruction* tmpReg =
Misha Brukman49ab7f22003-11-07 17:29:48 +0000500 new TmpInstruction(mcfi, PointerType::get(val->getType()));
Vikram S. Advea2a70942001-10-28 21:41:46 +0000501
Misha Brukman81b06862003-05-21 18:48:06 +0000502 // Create another TmpInstruction for the address register
503 TmpInstruction* addrReg =
Misha Brukman49ab7f22003-11-07 17:29:48 +0000504 new TmpInstruction(mcfi, PointerType::get(val->getType()));
Vikram S. Advee6124d32003-07-29 19:59:23 +0000505
Misha Brukman49ab7f22003-11-07 17:29:48 +0000506 // Get the constant pool index for this constant
507 MachineConstantPool *CP = MachineFunction::get(F).getConstantPool();
508 Constant *C = cast<Constant>(val);
509 unsigned CPI = CP->getConstantPoolIndex(C);
510
511 // Put the address of the constant into a register
512 MachineInstr* MI;
513
514 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(tmpReg);
515 MI->setOperandHi64(0);
516 mvec.push_back(MI);
517
518 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addConstantPoolIndex(CPI)
519 .addRegDef(tmpReg);
520 MI->setOperandLo64(1);
521 mvec.push_back(MI);
522
523 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
524 .addRegDef(tmpReg));
525 MI = BuildMI(V9::SETHI, 2).addConstantPoolIndex(CPI).addRegDef(addrReg);
526 MI->setOperandHi32(0);
527 mvec.push_back(MI);
528
529 MI = BuildMI(V9::ORr, 3).addReg(addrReg).addReg(tmpReg).addRegDef(addrReg);
530 mvec.push_back(MI);
531
532 MI = BuildMI(V9::ORi, 3).addReg(addrReg).addConstantPoolIndex(CPI)
533 .addRegDef(addrReg);
534 MI->setOperandLo32(1);
535 mvec.push_back(MI);
536
537 // Now load the constant from out ConstantPool label
Misha Brukman81b06862003-05-21 18:48:06 +0000538 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000539 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman49ab7f22003-11-07 17:29:48 +0000540 mvec.push_back(BuildMI(Opcode, 3)
541 .addReg(addrReg).addSImm((int64_t)0).addRegDef(dest));
Misha Brukman81b06862003-05-21 18:48:06 +0000542 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000543}
544
545
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000546// Create an instruction sequence to copy an integer register `val'
547// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000548// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000549// The generated instructions are returned in `mvec'.
550// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000551// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000552//
553void
Brian Gaekee3d68072004-02-25 18:44:15 +0000554SparcV9InstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000555 Function* F,
556 Value* val,
557 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000558 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000559 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000560{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000561 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
562 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000563 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000564 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000565
566 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000567 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000568
569 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000570 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000571
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000572 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000573 // The store and load opCodes are based on the size of the source value.
574 // If the value is smaller than 32 bits, we must sign- or zero-extend it
575 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000576 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000577 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
578 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000579 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
580 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000581 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000582 if (val->getType()->isSigned())
583 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
584 mvec, mcfi);
585 else
586 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
587 mvec, mcfi);
588 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000589
Chris Lattnerd029cd22004-06-02 05:55:25 +0000590 unsigned FPReg = target.getRegInfo()->getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000591 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
592 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
593 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000594 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000595
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000596 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000597 // The type of the load opCode is the floating point type that matches the
598 // stored type in size:
599 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000600 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000601 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000602 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
603 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
604 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000605 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000606}
607
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000608// Similarly, create an instruction sequence to copy an FP register
609// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000610// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000611// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
612// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000613//
614void
Brian Gaekee3d68072004-02-25 18:44:15 +0000615SparcV9InstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000616 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000617 Value* val,
618 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000619 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000621{
Vikram S. Advec190c012002-07-31 21:13:31 +0000622 const Type* opTy = val->getType();
623 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000624
Vikram S. Advec190c012002-07-31 21:13:31 +0000625 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000626 assert((destTy->isIntegral() || isa<PointerType>(destTy))
627 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000628
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000629 // FIXME: For now, we allocate permanent space because the stack frame
630 // manager does not allow locals to be allocated (e.g., for alloca) after
631 // a temp is allocated!
632 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000633 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000634
Chris Lattnerd029cd22004-06-02 05:55:25 +0000635 unsigned FPReg = target.getRegInfo()->getFramePointer();
Chris Lattner54e898e2003-01-15 19:23:34 +0000636
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000637 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000638 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000639 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000640 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
641 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
642 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000643 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000644
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000645 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000646 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000647 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000648 // On SparcV9: int for float, long for double.
649 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000650 // ensure correct sign-extension for UByte, UShort or UInt:
651 //
652 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000653 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
654 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
655 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000656 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000657}
658
659
660// Create instruction(s) to copy src to dest, for arbitrary types
661// The generated instructions are returned in `mvec'.
662// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000663// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000664//
665void
Brian Gaekee3d68072004-02-25 18:44:15 +0000666SparcV9InstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
Misha Brukmand71295a2003-12-17 22:04:00 +0000667 Function *F,
668 Value* src,
669 Instruction* dest,
670 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000671 MachineCodeForInstruction& mcfi) const
672{
673 bool loadConstantToReg = false;
674
675 const Type* resultType = dest->getType();
676
677 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Brian Gaekef561b082004-05-30 07:34:01 +0000678 assert (opCode != V9::INVALID_OPCODE
679 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve242a8082002-05-19 15:25:51 +0000680
681 // if `src' is a constant that doesn't fit in the immed field or if it is
682 // a global variable (i.e., a constant address), generate a load
683 // instruction instead of an add
684 //
Misha Brukman81b06862003-05-21 18:48:06 +0000685 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000686 unsigned int machineRegNum;
687 int64_t immedValue;
688 MachineOperand::MachineOperandType opType =
689 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
690 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691
Misha Brukmana98cd452003-05-20 20:32:24 +0000692 if (opType == MachineOperand::MO_VirtualRegister)
693 loadConstantToReg = true;
694 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000695 else if (isa<GlobalValue>(src))
696 loadConstantToReg = true;
697
Misha Brukman81b06862003-05-21 18:48:06 +0000698 if (loadConstantToReg) {
699 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000700 // Insert instructions to "load" the constant into a register
Chris Lattnerd029cd22004-06-02 05:55:25 +0000701 target.getInstrInfo()->CreateCodeToLoadConst(target, F, src, dest,
702 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000703 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000704 // Create a reg-to-reg copy instruction for the given type:
705 // -- For FP values, create a FMOVS or FMOVD instruction
706 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
707 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000708 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000709 MachineInstr* MI;
710 if (resultType->isFloatingPoint())
711 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
712 .addReg(src).addRegDef(dest));
713 else {
714 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
715 MI = (BuildMI(opCode, 3)
716 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
717 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000718 mvec.push_back(MI);
719 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000720}
721
722
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000723// Helper function for sign-extension and zero-extension.
724// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
725inline void
726CreateBitExtensionInstructions(bool signExtend,
727 const TargetMachine& target,
728 Function* F,
729 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. Adve84c0fcb2002-09-05 18:33:59 +0000733 MachineCodeForInstruction& mcfi)
734{
735 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000736
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000737 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
738
Misha Brukman81b06862003-05-21 18:48:06 +0000739 if (numLowBits < 32) {
740 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000741 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000742 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000743 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000744 .addZImm(32-numLowBits).addRegDef(tmpI));
745 srcVal = tmpI;
746 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000747
Misha Brukmand36e30e2003-06-06 09:52:23 +0000748 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000749 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000750}
751
752
Vikram S. Adve242a8082002-05-19 15:25:51 +0000753// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000754// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000755// The generated instructions are returned in `mvec'.
756// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000757// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000758//
759void
Brian Gaekee3d68072004-02-25 18:44:15 +0000760SparcV9InstrInfo::CreateSignExtensionInstructions(
Vikram S. Adve242a8082002-05-19 15:25:51 +0000761 const TargetMachine& target,
762 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000763 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000764 Value* destVal,
765 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000766 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000767 MachineCodeForInstruction& mcfi) const
768{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000769 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000770 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000771}
772
773
774// Create instruction sequence to produce a zero-extended register value
775// from an arbitrary-sized integer value (sized in bits, not bytes).
776// For SPARC v9, we sign-extend the given operand using SLL; SRL.
777// The generated instructions are returned in `mvec'.
778// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000779// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000780//
781void
Brian Gaekee3d68072004-02-25 18:44:15 +0000782SparcV9InstrInfo::CreateZeroExtensionInstructions(
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000783 const TargetMachine& target,
784 Function* F,
785 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000786 Value* destVal,
787 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000788 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000789 MachineCodeForInstruction& mcfi) const
790{
791 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000792 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000793}
Brian Gaeked0fde302003-11-11 22:41:34 +0000794
795} // End llvm namespace