blob: 952b7d2fcb4bbae7c8b98d17ed4eea2d46acc613 [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"
Chris Lattner08d702b2003-10-21 17:22:23 +000022#include "llvm/iTerminators.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000023#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000024
Vikram S. Adve53fd4002002-07-10 21:39:50 +000025static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
26static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
27
28
Chris Lattner795ba6c2003-01-15 21:36:50 +000029//---------------------------------------------------------------------------
Vikram S. Advee6124d32003-07-29 19:59:23 +000030// Function ConvertConstantToIntType
Chris Lattner795ba6c2003-01-15 21:36:50 +000031//
Vikram S. Advee6124d32003-07-29 19:59:23 +000032// Function to get the value of an integral constant in the form
33// that must be put into the machine register. The specified constant is
34// interpreted as (i.e., converted if necessary to) the specified destination
35// type. The result is always returned as an uint64_t, since the representation
36// of int64_t and uint64_t are identical. The argument can be any known const.
Chris Lattner795ba6c2003-01-15 21:36:50 +000037//
38// isValidConstant is set to true if a valid constant was found.
39//---------------------------------------------------------------------------
40
Vikram S. Advee6124d32003-07-29 19:59:23 +000041uint64_t
42UltraSparcInstrInfo::ConvertConstantToIntType(const TargetMachine &target,
43 const Value *V,
44 const Type *destType,
45 bool &isValidConstant) const
Chris Lattner795ba6c2003-01-15 21:36:50 +000046{
Chris Lattner795ba6c2003-01-15 21:36:50 +000047 isValidConstant = false;
Vikram S. Advee6124d32003-07-29 19:59:23 +000048 uint64_t C = 0;
Chris Lattner795ba6c2003-01-15 21:36:50 +000049
Vikram S. Advee6124d32003-07-29 19:59:23 +000050 if (! destType->isIntegral() && ! isa<PointerType>(destType))
51 return C;
52
53 if (! isa<Constant>(V))
54 return C;
55
56 // ConstantPointerRef: no conversions needed: get value and return it
57 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(V)) {
58 // A ConstantPointerRef is just a reference to GlobalValue.
59 isValidConstant = true; // may be overwritten by recursive call
60 return (CPR->isNullValue()? 0
61 : ConvertConstantToIntType(target, CPR->getValue(), destType,
62 isValidConstant));
Chris Lattner795ba6c2003-01-15 21:36:50 +000063 }
Vikram S. Advee6124d32003-07-29 19:59:23 +000064
65 // ConstantBool: no conversions needed: get value and return it
66 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V)) {
67 isValidConstant = true;
68 return (uint64_t) CB->getValue();
69 }
70
71 // For other types of constants, some conversion may be needed.
72 // First, extract the constant operand according to its own type
73 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
74 switch(CE->getOpcode()) {
75 case Instruction::Cast: // recursively get the value as cast
76 C = ConvertConstantToIntType(target, CE->getOperand(0), CE->getType(),
77 isValidConstant);
78 break;
79 default: // not simplifying other ConstantExprs
80 break;
81 }
82 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
83 isValidConstant = true;
84 C = CI->getRawValue();
85 }
86 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
87 isValidConstant = true;
88 double fC = CFP->getValue();
89 C = (destType->isSigned()? (uint64_t) (int64_t) fC
90 : (uint64_t) fC);
91 }
92
93 // Now if a valid value was found, convert it to destType.
94 if (isValidConstant) {
95 unsigned opSize = target.getTargetData().getTypeSize(V->getType());
96 unsigned destSize = target.getTargetData().getTypeSize(destType);
97 uint64_t maskHi = (destSize < 8)? (1U << 8*destSize) - 1 : ~0;
98 assert(opSize <= 8 && destSize <= 8 && ">8-byte int type unexpected");
99
100 if (destType->isSigned()) {
101 if (opSize > destSize) // operand is larger than dest:
102 C = C & maskHi; // mask high bits
103
104 if (opSize > destSize ||
105 (opSize == destSize && ! V->getType()->isSigned()))
106 if (C & (1U << (8*destSize - 1)))
107 C = C | ~maskHi; // sign-extend from destSize to 64 bits
108 }
109 else {
110 if (opSize > destSize || (V->getType()->isSigned() && destSize < 8)) {
111 // operand is larger than dest,
112 // OR both are equal but smaller than the full register size
113 // AND operand is signed, so it may have extra sign bits:
114 // mask high bits
115 C = C & maskHi;
116 }
117 }
118 }
119
120 return C;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000121}
122
123
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000124//----------------------------------------------------------------------------
125// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000126//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000127// Set a 32-bit unsigned constant in the register `dest', using
128// SETHI, OR in the worst case. This function correctly emulates
129// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
130//
131// The isSigned=true case is used to implement SETSW without duplicating code.
132//
133// Optimize some common cases:
134// (1) Small value that fits in simm13 field of OR: don't need SETHI.
135// (2) isSigned = true and C is a small negative signed value, i.e.,
136// high bits are 1, and the remaining bits fit in simm13(OR).
137//----------------------------------------------------------------------------
138
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000139static inline void
140CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000141 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000142 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000143{
144 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000145
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000146 // In order to get efficient code, we should not generate the SETHI if
147 // all high bits are 1 (i.e., this is a small signed value that fits in
148 // the simm13 field of OR). So we check for and handle that case specially.
149 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
150 // In fact, sC == -sC, so we have to check for this explicitly.
151 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000152 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
153
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000154 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000155 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
156 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
157 miSETHI->setOperandHi32(0);
158 mvec.push_back(miSETHI);
159 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000160
161 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
162 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000163 if (miSETHI==NULL || C & MAXLO) {
164 if (miSETHI) {
165 // unsigned value with high-order bits set using SETHI
Misha Brukman71ed1c92003-05-27 22:35:43 +0000166 miOR = BuildMI(V9::ORi,3).addReg(dest).addZImm(C).addRegDef(dest);
Misha Brukman81b06862003-05-21 18:48:06 +0000167 miOR->setOperandLo32(1);
168 } else {
169 // unsigned or small signed value that fits in simm13 field of OR
170 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Misha Brukman71ed1c92003-05-27 22:35:43 +0000171 miOR = BuildMI(V9::ORi, 3).addMReg(target.getRegInfo()
Misha Brukman81b06862003-05-21 18:48:06 +0000172 .getZeroRegNum())
173 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000174 }
Misha Brukman81b06862003-05-21 18:48:06 +0000175 mvec.push_back(miOR);
176 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000177
178 assert((miSETHI || miOR) && "Oops, no code was generated!");
179}
180
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000181
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000182//----------------------------------------------------------------------------
183// Function: CreateSETSWConst
184//
185// Set a 32-bit signed constant in the register `dest', with sign-extension
186// to 64 bits. This uses SETHI, OR, SRA in the worst case.
187// This function correctly emulates the SETSW pseudo-op for SPARC v9.
188//
189// Optimize the same cases as SETUWConst, plus:
190// (1) SRA is not needed for positive or small negative values.
191//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000192
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000193static inline void
194CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000195 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000196{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000198 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
199
Vikram S. Advec2f09392003-05-25 21:58:11 +0000200 // Sign-extend to the high 32 bits if needed.
201 // NOTE: The value C = 0x80000000 is bad: -C == C and so -C is < MAXSIMM
202 if (C < 0 && (C == -C || -C > (int32_t) MAXSIMM))
Misha Brukmand36e30e2003-06-06 09:52:23 +0000203 mvec.push_back(BuildMI(V9::SRAi5,3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000204}
205
206
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000207//----------------------------------------------------------------------------
208// Function: CreateSETXConst
209//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000210// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000211// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
212// This function correctly emulates the SETX pseudo-op for SPARC v9.
213//
214// Optimize the same cases as SETUWConst for each 32 bit word.
215//----------------------------------------------------------------------------
216
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000217static inline void
218CreateSETXConst(const TargetMachine& target, uint64_t C,
219 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000220 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000221{
222 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
223
224 MachineInstr* MI;
225
226 // Code to set the upper 32 bits of the value in register `tmpReg'
227 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
228
229 // Shift tmpReg left by 32 bits
Misha Brukman71ed1c92003-05-27 22:35:43 +0000230 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000231 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000232
233 // Code to set the low 32 bits of the value in register `dest'
234 CreateSETUWConst(target, C, dest, mvec);
235
236 // dest = OR(tmpReg, dest)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000237 mvec.push_back(BuildMI(V9::ORr,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000238}
239
240
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000241//----------------------------------------------------------------------------
242// Function: CreateSETUWLabel
243//
244// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
245//----------------------------------------------------------------------------
246
247static inline void
248CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000249 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000250{
251 MachineInstr* MI;
252
253 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000254 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000255 MI->setOperandHi32(0);
256 mvec.push_back(MI);
257
258 // Set the low 10 bits in dest
Misha Brukman71ed1c92003-05-27 22:35:43 +0000259 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000260 MI->setOperandLo32(1);
261 mvec.push_back(MI);
262}
263
264
265//----------------------------------------------------------------------------
266// Function: CreateSETXLabel
267//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000268// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000269//----------------------------------------------------------------------------
270
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000271static inline void
272CreateSETXLabel(const TargetMachine& target,
273 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000274 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000275{
276 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
277 "I only know about constant values and global addresses");
278
279 MachineInstr* MI;
280
Misha Brukmana98cd452003-05-20 20:32:24 +0000281 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000282 MI->setOperandHi64(0);
283 mvec.push_back(MI);
284
Misha Brukman71ed1c92003-05-27 22:35:43 +0000285 MI = BuildMI(V9::ORi, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000286 MI->setOperandLo64(1);
287 mvec.push_back(MI);
288
Misha Brukman71ed1c92003-05-27 22:35:43 +0000289 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpReg).addZImm(32)
Misha Brukmana98cd452003-05-20 20:32:24 +0000290 .addRegDef(tmpReg));
291 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000292 MI->setOperandHi32(0);
293 mvec.push_back(MI);
294
Misha Brukman71ed1c92003-05-27 22:35:43 +0000295 MI = BuildMI(V9::ORr, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000296 mvec.push_back(MI);
297
Misha Brukman71ed1c92003-05-27 22:35:43 +0000298 MI = BuildMI(V9::ORi, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000299 MI->setOperandLo32(1);
300 mvec.push_back(MI);
301}
302
Vikram S. Adve30764b82001-10-18 00:01:48 +0000303
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000304//----------------------------------------------------------------------------
305// Function: CreateUIntSetInstruction
306//
307// Create code to Set an unsigned constant in the register `dest'.
308// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
309// CreateSETSWConst is an optimization for the case that the unsigned value
310// has all ones in the 33 high bits (so that sign-extension sets them all).
311//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000312
Vikram S. Adve242a8082002-05-19 15:25:51 +0000313static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000314CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000315 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000316 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000317 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000318{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000319 static const uint64_t lo32 = (uint32_t) ~0;
320 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
321 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Vikram S. Adve940a3a42003-07-10 19:48:19 +0000322 else if ((C & ~lo32) == ~lo32 && (C & (1U << 31))) {
Misha Brukman81b06862003-05-21 18:48:06 +0000323 // All high 33 (not 32) bits are 1s: sign-extension will take care
324 // of high 32 bits, so use the sequence for signed int
325 CreateSETSWConst(target, (int32_t) C, dest, mvec);
326 } else if (C > lo32) {
327 // C does not fit in 32 bits
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000328 TmpInstruction* tmpReg = new TmpInstruction(mcfi, Type::IntTy);
Misha Brukman81b06862003-05-21 18:48:06 +0000329 CreateSETXConst(target, C, tmpReg, dest, mvec);
330 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000331}
332
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000333
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000334//----------------------------------------------------------------------------
335// Function: CreateIntSetInstruction
336//
337// Create code to Set a signed constant in the register `dest'.
338// Really the same as CreateUIntSetInstruction.
339//----------------------------------------------------------------------------
340
341static inline void
342CreateIntSetInstruction(const TargetMachine& target,
343 int64_t C, Instruction* dest,
344 std::vector<MachineInstr*>& mvec,
345 MachineCodeForInstruction& mcfi)
346{
347 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
348}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000349
Vikram S. Adve30764b82001-10-18 00:01:48 +0000350
351//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000352// Create a table of LLVM opcode -> max. immediate constant likely to
353// be usable for that operation.
354//---------------------------------------------------------------------------
355
356// Entry == 0 ==> no immediate constant field exists at all.
357// Entry > 0 ==> abs(immediate constant) <= Entry
358//
Misha Brukmana98cd452003-05-20 20:32:24 +0000359std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000360
361static int
362MaxConstantForInstr(unsigned llvmOpCode)
363{
364 int modelOpCode = -1;
365
Chris Lattner0b16ae22002-10-13 19:39:16 +0000366 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
367 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukman71ed1c92003-05-27 22:35:43 +0000368 modelOpCode = V9::ADDi;
Vikram S. Adve49001162002-09-16 15:56:01 +0000369 else
370 switch(llvmOpCode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000371 case Instruction::Ret: modelOpCode = V9::JMPLCALLi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000372
373 case Instruction::Malloc:
374 case Instruction::Alloca:
375 case Instruction::GetElementPtr:
Chris Lattner3b237fc2003-10-19 21:34:28 +0000376 case Instruction::PHI:
Vikram S. Adve49001162002-09-16 15:56:01 +0000377 case Instruction::Cast:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000378 case Instruction::Call: modelOpCode = V9::ADDi; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000379
380 case Instruction::Shl:
Misha Brukman71ed1c92003-05-27 22:35:43 +0000381 case Instruction::Shr: modelOpCode = V9::SLLXi6; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000382
383 default: break;
384 };
385
386 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
387}
388
389static void
390InitializeMaxConstantsTable()
391{
392 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000393 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000394 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000395 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000396 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000397 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++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::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++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::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000402 MaxConstantsTable[op] = MaxConstantForInstr(op);
403}
404
405
406//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000407// class UltraSparcInstrInfo
408//
409// Purpose:
410// Information about individual instructions.
411// Most information is stored in the SparcMachineInstrDesc array above.
412// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000413// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000414//---------------------------------------------------------------------------
415
416/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000417UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000418 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000419 /*descSize = */ V9::NUM_TOTAL_OPCODES,
420 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000421{
Vikram S. Adve49001162002-09-16 15:56:01 +0000422 InitializeMaxConstantsTable();
423}
424
425bool
426UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
427 const Instruction* I) const
428{
429 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
430 return true;
431
432 if (isa<ConstantPointerNull>(CV)) // can always use %g0
433 return false;
434
Chris Lattnerff3d5d92003-10-21 16:29:23 +0000435 if (isa<SwitchInst>(I)) // Switch instructions will be lowered!
436 return false;
437
Chris Lattnerc07736a2003-07-23 15:22:26 +0000438 if (const ConstantInt* CI = dyn_cast<ConstantInt>(CV))
439 return labs((int64_t)CI->getRawValue()) > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000440
441 if (isa<ConstantBool>(CV))
Chris Lattnerc07736a2003-07-23 15:22:26 +0000442 return 1 > MaxConstantsTable[I->getOpcode()];
Vikram S. Adve49001162002-09-16 15:56:01 +0000443
444 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000445}
446
Vikram S. Advee76af292002-03-18 03:09:15 +0000447//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000448// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000449// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000450// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000451// The generated instructions are returned in `mvec'.
452// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000453// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000454//
455void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000456UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
457 Function* F,
458 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000459 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000460 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000461 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000462{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000463 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000464 "I only know about constant values and global addresses");
465
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000466 // Use a "set" instruction for known constants or symbolic constants (labels)
467 // that can go in an integer reg.
468 // We have to use a "load" instruction for all other constants,
469 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000470 //
471 const Type* valType = val->getType();
472
Vikram S. Advee6124d32003-07-29 19:59:23 +0000473 // A ConstantPointerRef is just a reference to GlobalValue.
474 while (isa<ConstantPointerRef>(val))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000475 val = cast<ConstantPointerRef>(val)->getValue();
476
Misha Brukman81b06862003-05-21 18:48:06 +0000477 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000478 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000479 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000480 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000481 return;
482 }
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000483
Vikram S. Advee6124d32003-07-29 19:59:23 +0000484 bool isValid;
485 uint64_t C = ConvertConstantToIntType(target, val, dest->getType(), isValid);
486 if (isValid) {
487 if (dest->getType()->isSigned())
Misha Brukman81b06862003-05-21 18:48:06 +0000488 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000489 else
490 CreateIntSetInstruction(target, (int64_t) C, dest, mvec, mcfi);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000491
Misha Brukman81b06862003-05-21 18:48:06 +0000492 } else {
493 // Make an instruction sequence to load the constant, viz:
494 // SETX <addr-of-constant>, tmpReg, addrReg
495 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000496
Misha Brukman81b06862003-05-21 18:48:06 +0000497 // First, create a tmp register to be used by the SETX sequence.
498 TmpInstruction* tmpReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000499 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000500
Misha Brukman81b06862003-05-21 18:48:06 +0000501 // Create another TmpInstruction for the address register
502 TmpInstruction* addrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000503 new TmpInstruction(mcfi, PointerType::get(val->getType()), val);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000504
Misha Brukman81b06862003-05-21 18:48:06 +0000505 // Put the address (a symbolic name) into a register
506 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Advee6124d32003-07-29 19:59:23 +0000507
Misha Brukman81b06862003-05-21 18:48:06 +0000508 // Generate the load instruction
509 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
510 unsigned Opcode = ChooseLoadInstruction(val->getType());
Misha Brukmanc559e052003-06-03 03:20:57 +0000511 Opcode = convertOpcodeFromRegToImm(Opcode);
Misha Brukman81b06862003-05-21 18:48:06 +0000512 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
513 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000514
Misha Brukman81b06862003-05-21 18:48:06 +0000515 // Make sure constant is emitted to constant pool in assembly code.
516 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
517 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000518}
519
520
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000521// Create an instruction sequence to copy an integer register `val'
522// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000523// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000524// The generated instructions are returned in `mvec'.
525// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000526// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000527//
528void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000529UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
530 Function* F,
531 Value* val,
532 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000533 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000534 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000535{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000536 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
537 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000538 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000539 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000540
541 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000542 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000543
544 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000545 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000546
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000547 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000548 // The store and load opCodes are based on the size of the source value.
549 // If the value is smaller than 32 bits, we must sign- or zero-extend it
550 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000551 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000552 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
553 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000554 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
555 // sign- or zero-extend respectively
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000556 storeVal = new TmpInstruction(mcfi, storeType, val);
Misha Brukman81b06862003-05-21 18:48:06 +0000557 if (val->getType()->isSigned())
558 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
559 mvec, mcfi);
560 else
561 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
562 mvec, mcfi);
563 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000564
565 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmanc559e052003-06-03 03:20:57 +0000566 unsigned StoreOpcode = ChooseStoreInstruction(storeType);
567 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
568 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000569 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000570
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000571 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000572 // The type of the load opCode is the floating point type that matches the
573 // stored type in size:
574 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000575 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000576 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000577 unsigned LoadOpcode = ChooseLoadInstruction(loadType);
578 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
579 mvec.push_back(BuildMI(LoadOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000580 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000581}
582
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000583// Similarly, create an instruction sequence to copy an FP register
584// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000585// The generated instructions are returned in `mvec'.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000586// Any temp. virtual registers (TmpInstruction) created are recorded in mcfi.
587// Temporary stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000588//
589void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000590UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
591 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000592 Value* val,
593 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000594 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000595 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000596{
Vikram S. Advec190c012002-07-31 21:13:31 +0000597 const Type* opTy = val->getType();
598 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000599
Vikram S. Advec190c012002-07-31 21:13:31 +0000600 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000601 assert((destTy->isIntegral() || isa<PointerType>(destTy))
602 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000603
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000604 // FIXME: For now, we allocate permanent space because the stack frame
605 // manager does not allow locals to be allocated (e.g., for alloca) after
606 // a temp is allocated!
607 //
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000608 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000609
Chris Lattner54e898e2003-01-15 19:23:34 +0000610 unsigned FPReg = target.getRegInfo().getFramePointer();
611
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000612 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000613 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000614 //
Misha Brukmanc559e052003-06-03 03:20:57 +0000615 unsigned StoreOpcode = ChooseStoreInstruction(opTy);
616 StoreOpcode = convertOpcodeFromRegToImm(StoreOpcode);
617 mvec.push_back(BuildMI(StoreOpcode, 3)
Chris Lattner54e898e2003-01-15 19:23:34 +0000618 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000619
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000620 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000621 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000622 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000623 // On SparcV9: int for float, long for double.
624 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000625 // ensure correct sign-extension for UByte, UShort or UInt:
626 //
627 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Misha Brukmanc559e052003-06-03 03:20:57 +0000628 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
629 LoadOpcode = convertOpcodeFromRegToImm(LoadOpcode);
630 mvec.push_back(BuildMI(LoadOpcode, 3).addMReg(FPReg)
Chris Lattner54e898e2003-01-15 19:23:34 +0000631 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000632}
633
634
635// Create instruction(s) to copy src to dest, for arbitrary types
636// The generated instructions are returned in `mvec'.
637// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000638// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000639//
640void
641UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
642 Function *F,
643 Value* src,
644 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000645 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 MachineCodeForInstruction& mcfi) const
647{
648 bool loadConstantToReg = false;
649
650 const Type* resultType = dest->getType();
651
652 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000653 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000654 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
655 return;
656 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000657
658 // if `src' is a constant that doesn't fit in the immed field or if it is
659 // a global variable (i.e., a constant address), generate a load
660 // instruction instead of an add
661 //
Misha Brukman81b06862003-05-21 18:48:06 +0000662 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000663 unsigned int machineRegNum;
664 int64_t immedValue;
665 MachineOperand::MachineOperandType opType =
666 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
667 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668
Misha Brukmana98cd452003-05-20 20:32:24 +0000669 if (opType == MachineOperand::MO_VirtualRegister)
670 loadConstantToReg = true;
671 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000672 else if (isa<GlobalValue>(src))
673 loadConstantToReg = true;
674
Misha Brukman81b06862003-05-21 18:48:06 +0000675 if (loadConstantToReg) {
676 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000677 // Insert instructions to "load" the constant into a register
678 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
679 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000680 } else {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000681 // Create a reg-to-reg copy instruction for the given type:
682 // -- For FP values, create a FMOVS or FMOVD instruction
683 // -- For non-FP values, create an add-with-0 instruction (opCode as above)
684 // Make `src' the second operand, in case it is a small constant!
Misha Brukmana98cd452003-05-20 20:32:24 +0000685 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000686 MachineInstr* MI;
687 if (resultType->isFloatingPoint())
688 MI = (BuildMI(resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
689 .addReg(src).addRegDef(dest));
690 else {
691 const Type* Ty =isa<PointerType>(resultType)? Type::ULongTy :resultType;
692 MI = (BuildMI(opCode, 3)
693 .addSImm((int64_t) 0).addReg(src).addRegDef(dest));
694 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000695 mvec.push_back(MI);
696 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000697}
698
699
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000700// Helper function for sign-extension and zero-extension.
701// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
702inline void
703CreateBitExtensionInstructions(bool signExtend,
704 const TargetMachine& target,
705 Function* F,
706 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000707 Value* destVal,
708 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000709 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000710 MachineCodeForInstruction& mcfi)
711{
712 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000713
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000714 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
715
Misha Brukman81b06862003-05-21 18:48:06 +0000716 if (numLowBits < 32) {
717 // SLL is needed since operand size is < 32 bits.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000718 TmpInstruction *tmpI = new TmpInstruction(mcfi, destVal->getType(),
Misha Brukmana98cd452003-05-20 20:32:24 +0000719 srcVal, destVal, "make32");
Misha Brukman71ed1c92003-05-27 22:35:43 +0000720 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(srcVal)
Misha Brukmana98cd452003-05-20 20:32:24 +0000721 .addZImm(32-numLowBits).addRegDef(tmpI));
722 srcVal = tmpI;
723 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000724
Misha Brukmand36e30e2003-06-06 09:52:23 +0000725 mvec.push_back(BuildMI(signExtend? V9::SRAi5 : V9::SRLi5, 3)
Misha Brukmana98cd452003-05-20 20:32:24 +0000726 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000727}
728
729
Vikram S. Adve242a8082002-05-19 15:25:51 +0000730// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000731// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000732// The generated instructions are returned in `mvec'.
733// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000734// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000735//
736void
737UltraSparcInstrInfo::CreateSignExtensionInstructions(
738 const TargetMachine& target,
739 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000740 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000741 Value* destVal,
742 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000743 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000744 MachineCodeForInstruction& mcfi) const
745{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000746 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000747 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000748}
749
750
751// Create instruction sequence to produce a zero-extended register value
752// from an arbitrary-sized integer value (sized in bits, not bytes).
753// For SPARC v9, we sign-extend the given operand using SLL; SRL.
754// The generated instructions are returned in `mvec'.
755// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000756// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000757//
758void
759UltraSparcInstrInfo::CreateZeroExtensionInstructions(
760 const TargetMachine& target,
761 Function* F,
762 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000763 Value* destVal,
764 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000765 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000766 MachineCodeForInstruction& mcfi) const
767{
768 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000769 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000770}