blob: 977360bf006dd226616407dec33fdc1935454ebc [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrInfo.cpp ------------------------------------------------===//
2//
3//===----------------------------------------------------------------------===//
Vikram S. Adve30764b82001-10-18 00:01:48 +00004
5#include "SparcInternals.h"
6#include "SparcInstrSelectionSupport.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +00007#include "llvm/CodeGen/InstrSelection.h"
8#include "llvm/CodeGen/InstrSelectionSupport.h"
Misha Brukmanfce11432002-10-28 00:28:31 +00009#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner2ef9a6a2002-12-28 20:18:21 +000010#include "llvm/CodeGen/MachineFunctionInfo.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000011#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000012#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000013#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000015#include "llvm/DerivedTypes.h"
Vikram S. Adve49001162002-09-16 15:56:01 +000016#include <stdlib.h>
Vikram S. Adve30764b82001-10-18 00:01:48 +000017
Vikram S. Adve53fd4002002-07-10 21:39:50 +000018static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
19static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
20
21
Chris Lattner795ba6c2003-01-15 21:36:50 +000022//---------------------------------------------------------------------------
23// Function GetConstantValueAsUnsignedInt
24// Function GetConstantValueAsSignedInt
25//
26// Convenience functions to get the value of an integral constant, for an
27// appropriate integer or non-integer type that can be held in a signed
28// or unsigned integer respectively. The type of the argument must be
29// the following:
30// Signed or unsigned integer
31// Boolean
32// Pointer
33//
34// isValidConstant is set to true if a valid constant was found.
35//---------------------------------------------------------------------------
36
37static uint64_t
38GetConstantValueAsUnsignedInt(const Value *V,
39 bool &isValidConstant)
40{
41 isValidConstant = true;
42
43 if (isa<Constant>(V))
44 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
45 return (int64_t)CB->getValue();
46 else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
47 return (uint64_t)CS->getValue();
48 else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
49 return CU->getValue();
50
51 isValidConstant = false;
52 return 0;
53}
54
55int64_t
56GetConstantValueAsSignedInt(const Value *V, bool &isValidConstant)
57{
58 uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant);
59 if (isValidConstant) {
60 if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed
61 return (int64_t) C;
62 else
63 isValidConstant = false;
64 }
65 return 0;
66}
67
68
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000069//----------------------------------------------------------------------------
70// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +000071//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000072// Set a 32-bit unsigned constant in the register `dest', using
73// SETHI, OR in the worst case. This function correctly emulates
74// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
75//
76// The isSigned=true case is used to implement SETSW without duplicating code.
77//
78// Optimize some common cases:
79// (1) Small value that fits in simm13 field of OR: don't need SETHI.
80// (2) isSigned = true and C is a small negative signed value, i.e.,
81// high bits are 1, and the remaining bits fit in simm13(OR).
82//----------------------------------------------------------------------------
83
Vikram S. Adve53fd4002002-07-10 21:39:50 +000084static inline void
85CreateSETUWConst(const TargetMachine& target, uint32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +000086 Instruction* dest, std::vector<MachineInstr*>& mvec,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000087 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000088{
89 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000090
Vikram S. Adve53fd4002002-07-10 21:39:50 +000091 // In order to get efficient code, we should not generate the SETHI if
92 // all high bits are 1 (i.e., this is a small signed value that fits in
93 // the simm13 field of OR). So we check for and handle that case specially.
94 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
95 // In fact, sC == -sC, so we have to check for this explicitly.
96 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000097 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
98
Vikram S. Adve53fd4002002-07-10 21:39:50 +000099 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Misha Brukman81b06862003-05-21 18:48:06 +0000100 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) {
101 miSETHI = BuildMI(V9::SETHI, 2).addZImm(C).addRegDef(dest);
102 miSETHI->setOperandHi32(0);
103 mvec.push_back(miSETHI);
104 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000105
106 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
107 // was generated, or if the low 10 bits are non-zero.
Misha Brukman81b06862003-05-21 18:48:06 +0000108 if (miSETHI==NULL || C & MAXLO) {
109 if (miSETHI) {
110 // unsigned value with high-order bits set using SETHI
111 miOR = BuildMI(V9::OR,3).addReg(dest).addZImm(C).addRegDef(dest);
112 miOR->setOperandLo32(1);
113 } else {
114 // unsigned or small signed value that fits in simm13 field of OR
115 assert(smallNegValue || (C & ~MAXSIMM) == 0);
116 miOR = BuildMI(V9::OR, 3).addMReg(target.getRegInfo()
117 .getZeroRegNum())
118 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000119 }
Misha Brukman81b06862003-05-21 18:48:06 +0000120 mvec.push_back(miOR);
121 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000122
123 assert((miSETHI || miOR) && "Oops, no code was generated!");
124}
125
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000126
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000127//----------------------------------------------------------------------------
128// Function: CreateSETSWConst
129//
130// Set a 32-bit signed constant in the register `dest', with sign-extension
131// to 64 bits. This uses SETHI, OR, SRA in the worst case.
132// This function correctly emulates the SETSW pseudo-op for SPARC v9.
133//
134// Optimize the same cases as SETUWConst, plus:
135// (1) SRA is not needed for positive or small negative values.
136//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000137
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000138static inline void
139CreateSETSWConst(const TargetMachine& target, int32_t C,
Misha Brukmana98cd452003-05-20 20:32:24 +0000140 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000141{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000142 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000143 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
144
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000145 // Sign-extend to the high 32 bits if needed
146 if (C < 0 && (-C) > (int32_t) MAXSIMM)
Misha Brukmana98cd452003-05-20 20:32:24 +0000147 mvec.push_back(BuildMI(V9::SRA, 3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000148}
149
150
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000151//----------------------------------------------------------------------------
152// Function: CreateSETXConst
153//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000154// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000155// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
156// This function correctly emulates the SETX pseudo-op for SPARC v9.
157//
158// Optimize the same cases as SETUWConst for each 32 bit word.
159//----------------------------------------------------------------------------
160
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000161static inline void
162CreateSETXConst(const TargetMachine& target, uint64_t C,
163 Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000164 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000165{
166 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
167
168 MachineInstr* MI;
169
170 // Code to set the upper 32 bits of the value in register `tmpReg'
171 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
172
173 // Shift tmpReg left by 32 bits
Misha Brukmana98cd452003-05-20 20:32:24 +0000174 mvec.push_back(BuildMI(V9::SLLX, 3).addReg(tmpReg).addZImm(32)
175 .addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000176
177 // Code to set the low 32 bits of the value in register `dest'
178 CreateSETUWConst(target, C, dest, mvec);
179
180 // dest = OR(tmpReg, dest)
Misha Brukmana98cd452003-05-20 20:32:24 +0000181 mvec.push_back(BuildMI(V9::OR,3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000182}
183
184
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000185//----------------------------------------------------------------------------
186// Function: CreateSETUWLabel
187//
188// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
189//----------------------------------------------------------------------------
190
191static inline void
192CreateSETUWLabel(const TargetMachine& target, Value* val,
Misha Brukmana98cd452003-05-20 20:32:24 +0000193 Instruction* dest, std::vector<MachineInstr*>& mvec)
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000194{
195 MachineInstr* MI;
196
197 // Set the high 22 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000198 MI = BuildMI(V9::SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000199 MI->setOperandHi32(0);
200 mvec.push_back(MI);
201
202 // Set the low 10 bits in dest
Misha Brukmana98cd452003-05-20 20:32:24 +0000203 MI = BuildMI(V9::OR, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000204 MI->setOperandLo32(1);
205 mvec.push_back(MI);
206}
207
208
209//----------------------------------------------------------------------------
210// Function: CreateSETXLabel
211//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000212// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000213//----------------------------------------------------------------------------
214
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000215static inline void
216CreateSETXLabel(const TargetMachine& target,
217 Value* val, Instruction* tmpReg, Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000218 std::vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000219{
220 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
221 "I only know about constant values and global addresses");
222
223 MachineInstr* MI;
224
Misha Brukmana98cd452003-05-20 20:32:24 +0000225 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000226 MI->setOperandHi64(0);
227 mvec.push_back(MI);
228
Misha Brukmana98cd452003-05-20 20:32:24 +0000229 MI = BuildMI(V9::OR, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000230 MI->setOperandLo64(1);
231 mvec.push_back(MI);
232
Misha Brukmana98cd452003-05-20 20:32:24 +0000233 mvec.push_back(BuildMI(V9::SLLX, 3).addReg(tmpReg).addZImm(32)
234 .addRegDef(tmpReg));
235 MI = BuildMI(V9::SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000236 MI->setOperandHi32(0);
237 mvec.push_back(MI);
238
Misha Brukmana98cd452003-05-20 20:32:24 +0000239 MI = BuildMI(V9::OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000240 mvec.push_back(MI);
241
Misha Brukmana98cd452003-05-20 20:32:24 +0000242 MI = BuildMI(V9::OR, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000243 MI->setOperandLo32(1);
244 mvec.push_back(MI);
245}
246
Vikram S. Adve30764b82001-10-18 00:01:48 +0000247
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000248//----------------------------------------------------------------------------
249// Function: CreateUIntSetInstruction
250//
251// Create code to Set an unsigned constant in the register `dest'.
252// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
253// CreateSETSWConst is an optimization for the case that the unsigned value
254// has all ones in the 33 high bits (so that sign-extension sets them all).
255//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000256
Vikram S. Adve242a8082002-05-19 15:25:51 +0000257static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000258CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000259 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000260 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000261 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000262{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000263 static const uint64_t lo32 = (uint32_t) ~0;
264 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
265 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
Misha Brukman81b06862003-05-21 18:48:06 +0000266 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31))) {
267 // All high 33 (not 32) bits are 1s: sign-extension will take care
268 // of high 32 bits, so use the sequence for signed int
269 CreateSETSWConst(target, (int32_t) C, dest, mvec);
270 } else if (C > lo32) {
271 // C does not fit in 32 bits
272 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
273 mcfi.addTemp(tmpReg);
274 CreateSETXConst(target, C, tmpReg, dest, mvec);
275 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000276}
277
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000278
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000279//----------------------------------------------------------------------------
280// Function: CreateIntSetInstruction
281//
282// Create code to Set a signed constant in the register `dest'.
283// Really the same as CreateUIntSetInstruction.
284//----------------------------------------------------------------------------
285
286static inline void
287CreateIntSetInstruction(const TargetMachine& target,
288 int64_t C, Instruction* dest,
289 std::vector<MachineInstr*>& mvec,
290 MachineCodeForInstruction& mcfi)
291{
292 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
293}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000294
Vikram S. Adve30764b82001-10-18 00:01:48 +0000295
296//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000297// Create a table of LLVM opcode -> max. immediate constant likely to
298// be usable for that operation.
299//---------------------------------------------------------------------------
300
301// Entry == 0 ==> no immediate constant field exists at all.
302// Entry > 0 ==> abs(immediate constant) <= Entry
303//
Misha Brukmana98cd452003-05-20 20:32:24 +0000304std::vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000305
306static int
307MaxConstantForInstr(unsigned llvmOpCode)
308{
309 int modelOpCode = -1;
310
Chris Lattner0b16ae22002-10-13 19:39:16 +0000311 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
312 llvmOpCode < Instruction::BinaryOpsEnd)
Misha Brukmana98cd452003-05-20 20:32:24 +0000313 modelOpCode = V9::ADD;
Vikram S. Adve49001162002-09-16 15:56:01 +0000314 else
315 switch(llvmOpCode) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000316 case Instruction::Ret: modelOpCode = V9::JMPLCALL; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000317
318 case Instruction::Malloc:
319 case Instruction::Alloca:
320 case Instruction::GetElementPtr:
321 case Instruction::PHINode:
322 case Instruction::Cast:
Misha Brukmana98cd452003-05-20 20:32:24 +0000323 case Instruction::Call: modelOpCode = V9::ADD; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000324
325 case Instruction::Shl:
Misha Brukmana98cd452003-05-20 20:32:24 +0000326 case Instruction::Shr: modelOpCode = V9::SLLX; break;
Vikram S. Adve49001162002-09-16 15:56:01 +0000327
328 default: break;
329 };
330
331 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
332}
333
334static void
335InitializeMaxConstantsTable()
336{
337 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000338 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000339 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000340 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000341 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000342 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000343 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000344 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000345 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000346 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000347 MaxConstantsTable[op] = MaxConstantForInstr(op);
348}
349
350
351//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000352// class UltraSparcInstrInfo
353//
354// Purpose:
355// Information about individual instructions.
356// Most information is stored in the SparcMachineInstrDesc array above.
357// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000358// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000359//---------------------------------------------------------------------------
360
361/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000362UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000363 : TargetInstrInfo(SparcMachineInstrDesc,
Misha Brukmana98cd452003-05-20 20:32:24 +0000364 /*descSize = */ V9::NUM_TOTAL_OPCODES,
365 /*numRealOpCodes = */ V9::NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000366{
Vikram S. Adve49001162002-09-16 15:56:01 +0000367 InitializeMaxConstantsTable();
368}
369
370bool
371UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
372 const Instruction* I) const
373{
374 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
375 return true;
376
377 if (isa<ConstantPointerNull>(CV)) // can always use %g0
378 return false;
379
380 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000381 /* Large unsigned longs may really just be small negative signed longs */
382 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000383
384 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000385 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000386
387 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000388 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000389
390 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000391}
392
Vikram S. Advee76af292002-03-18 03:09:15 +0000393//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000394// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000395// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000396// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000397// The generated instructions are returned in `mvec'.
398// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000399// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000400//
401void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000402UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
403 Function* F,
404 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000405 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000406 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000407 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000408{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000409 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000410 "I only know about constant values and global addresses");
411
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000412 // Use a "set" instruction for known constants or symbolic constants (labels)
413 // that can go in an integer reg.
414 // We have to use a "load" instruction for all other constants,
415 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000416 //
417 const Type* valType = val->getType();
418
Vikram S. Adve893cace2002-10-13 00:04:26 +0000419 // Unfortunate special case: a ConstantPointerRef is just a
420 // reference to GlobalValue.
421 if (isa<ConstantPointerRef>(val))
422 val = cast<ConstantPointerRef>(val)->getValue();
423
Misha Brukman81b06862003-05-21 18:48:06 +0000424 if (isa<GlobalValue>(val)) {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000425 TmpInstruction* tmpReg =
426 new TmpInstruction(PointerType::get(val->getType()), val);
427 mcfi.addTemp(tmpReg);
428 CreateSETXLabel(target, val, tmpReg, dest, mvec);
Misha Brukman81b06862003-05-21 18:48:06 +0000429 } else if (valType->isIntegral()) {
430 bool isValidConstant;
431 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
432 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000433
Misha Brukman81b06862003-05-21 18:48:06 +0000434 if (! dest->getType()->isSigned()) {
435 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
436 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000437
Misha Brukman81b06862003-05-21 18:48:06 +0000438 if (opSize > destSize || (val->getType()->isSigned() && destSize < 8)) {
439 // operand is larger than dest,
440 // OR both are equal but smaller than the full register size
441 // AND operand is signed, so it may have extra sign bits:
442 // mask high bits
443 C = C & ((1U << 8*destSize) - 1);
444 }
445 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
446 } else {
447 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
448 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000449
Misha Brukman81b06862003-05-21 18:48:06 +0000450 if (opSize > destSize)
451 // operand is larger than dest: mask high bits
452 C = C & ((1U << 8*destSize) - 1);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000453
Misha Brukman81b06862003-05-21 18:48:06 +0000454 if (opSize > destSize ||
455 (opSize == destSize && !val->getType()->isSigned()))
456 // sign-extend from destSize to 64 bits
457 C = ((C & (1U << (8*destSize - 1)))
458 ? C | ~((1U << 8*destSize) - 1)
459 : C);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000460
Misha Brukman81b06862003-05-21 18:48:06 +0000461 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000462 }
Misha Brukman81b06862003-05-21 18:48:06 +0000463 } else {
464 // Make an instruction sequence to load the constant, viz:
465 // SETX <addr-of-constant>, tmpReg, addrReg
466 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000467
Misha Brukman81b06862003-05-21 18:48:06 +0000468 // First, create a tmp register to be used by the SETX sequence.
469 TmpInstruction* tmpReg =
470 new TmpInstruction(PointerType::get(val->getType()), val);
471 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000472
Misha Brukman81b06862003-05-21 18:48:06 +0000473 // Create another TmpInstruction for the address register
474 TmpInstruction* addrReg =
475 new TmpInstruction(PointerType::get(val->getType()), val);
476 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000477
Misha Brukman81b06862003-05-21 18:48:06 +0000478 // Put the address (a symbolic name) into a register
479 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000480
Misha Brukman81b06862003-05-21 18:48:06 +0000481 // Generate the load instruction
482 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
483 unsigned Opcode = ChooseLoadInstruction(val->getType());
484 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
485 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000486
Misha Brukman81b06862003-05-21 18:48:06 +0000487 // Make sure constant is emitted to constant pool in assembly code.
488 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
489 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000490}
491
492
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000493// Create an instruction sequence to copy an integer register `val'
494// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000495// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000496// The generated instructions are returned in `mvec'.
497// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000498// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000499//
500void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000501UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
502 Function* F,
503 Value* val,
504 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000505 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000506 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000507{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000508 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
509 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000510 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000511 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000512
513 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000514 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000515
516 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000517 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000518
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000519 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000520 // The store and load opCodes are based on the size of the source value.
521 // If the value is smaller than 32 bits, we must sign- or zero-extend it
522 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000523 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000524 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
525 Value* storeVal = val;
Misha Brukman81b06862003-05-21 18:48:06 +0000526 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy)) {
527 // sign- or zero-extend respectively
528 storeVal = new TmpInstruction(storeType, val);
529 if (val->getType()->isSigned())
530 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
531 mvec, mcfi);
532 else
533 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
534 mvec, mcfi);
535 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000536
537 unsigned FPReg = target.getRegInfo().getFramePointer();
538 mvec.push_back(BuildMI(ChooseStoreInstruction(storeType), 3)
539 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000540
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000541 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000542 // The type of the load opCode is the floating point type that matches the
543 // stored type in size:
544 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000545 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000546 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000547 mvec.push_back(BuildMI(ChooseLoadInstruction(loadType), 3)
548 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000549}
550
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000551// Similarly, create an instruction sequence to copy an FP register
552// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000553// The generated instructions are returned in `mvec'.
554// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000555// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000556//
557void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000558UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
559 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000560 Value* val,
561 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000562 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000563 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000564{
Vikram S. Advec190c012002-07-31 21:13:31 +0000565 const Type* opTy = val->getType();
566 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000567
Vikram S. Advec190c012002-07-31 21:13:31 +0000568 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000569 assert((destTy->isIntegral() || isa<PointerType>(destTy))
570 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000571
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000572 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000573
Chris Lattner54e898e2003-01-15 19:23:34 +0000574 unsigned FPReg = target.getRegInfo().getFramePointer();
575
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000576 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000577 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000578 //
Chris Lattner54e898e2003-01-15 19:23:34 +0000579 mvec.push_back(BuildMI(ChooseStoreInstruction(opTy), 3)
580 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000581
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000582 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000583 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000584 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000585 // On SparcV9: int for float, long for double.
586 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000587 // ensure correct sign-extension for UByte, UShort or UInt:
588 //
589 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000590 mvec.push_back(BuildMI(ChooseLoadInstruction(loadTy), 3).addMReg(FPReg)
591 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000592}
593
594
595// Create instruction(s) to copy src to dest, for arbitrary types
596// The generated instructions are returned in `mvec'.
597// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000598// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000599//
600void
601UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
602 Function *F,
603 Value* src,
604 Instruction* dest,
Misha Brukmana98cd452003-05-20 20:32:24 +0000605 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000606 MachineCodeForInstruction& mcfi) const
607{
608 bool loadConstantToReg = false;
609
610 const Type* resultType = dest->getType();
611
612 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
Misha Brukman81b06862003-05-21 18:48:06 +0000613 if (opCode == V9::INVALID_OPCODE) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000614 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
615 return;
616 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000617
618 // if `src' is a constant that doesn't fit in the immed field or if it is
619 // a global variable (i.e., a constant address), generate a load
620 // instruction instead of an add
621 //
Misha Brukman81b06862003-05-21 18:48:06 +0000622 if (isa<Constant>(src)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000623 unsigned int machineRegNum;
624 int64_t immedValue;
625 MachineOperand::MachineOperandType opType =
626 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
627 machineRegNum, immedValue);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000628
Misha Brukmana98cd452003-05-20 20:32:24 +0000629 if (opType == MachineOperand::MO_VirtualRegister)
630 loadConstantToReg = true;
631 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000632 else if (isa<GlobalValue>(src))
633 loadConstantToReg = true;
634
Misha Brukman81b06862003-05-21 18:48:06 +0000635 if (loadConstantToReg) {
636 // `src' is constant and cannot fit in immed field for the ADD
Misha Brukmana98cd452003-05-20 20:32:24 +0000637 // Insert instructions to "load" the constant into a register
638 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
639 mvec, mcfi);
Misha Brukman81b06862003-05-21 18:48:06 +0000640 } else {
641 // Create an add-with-0 instruction of the appropriate type.
Misha Brukmana98cd452003-05-20 20:32:24 +0000642 // Make `src' the second operand, in case it is a constant
643 // Use (unsigned long) 0 for a NULL pointer value.
644 //
645 const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType;
646 MachineInstr* MI =
647 BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty))
648 .addReg(src).addRegDef(dest);
649 mvec.push_back(MI);
650 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000651}
652
653
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000654// Helper function for sign-extension and zero-extension.
655// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
656inline void
657CreateBitExtensionInstructions(bool signExtend,
658 const TargetMachine& target,
659 Function* F,
660 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000661 Value* destVal,
662 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000663 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000664 MachineCodeForInstruction& mcfi)
665{
666 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000667
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000668 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
669
Misha Brukman81b06862003-05-21 18:48:06 +0000670 if (numLowBits < 32) {
671 // SLL is needed since operand size is < 32 bits.
Misha Brukmana98cd452003-05-20 20:32:24 +0000672 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
673 srcVal, destVal, "make32");
674 mcfi.addTemp(tmpI);
675 mvec.push_back(BuildMI(V9::SLLX, 3).addReg(srcVal)
676 .addZImm(32-numLowBits).addRegDef(tmpI));
677 srcVal = tmpI;
678 }
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000679
Misha Brukmana98cd452003-05-20 20:32:24 +0000680 mvec.push_back(BuildMI(signExtend? V9::SRA : V9::SRL, 3)
681 .addReg(srcVal).addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000682}
683
684
Vikram S. Adve242a8082002-05-19 15:25:51 +0000685// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000686// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687// The generated instructions are returned in `mvec'.
688// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000689// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000690//
691void
692UltraSparcInstrInfo::CreateSignExtensionInstructions(
693 const TargetMachine& target,
694 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000695 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000696 Value* destVal,
697 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000698 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000699 MachineCodeForInstruction& mcfi) const
700{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000701 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000702 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000703}
704
705
706// Create instruction sequence to produce a zero-extended register value
707// from an arbitrary-sized integer value (sized in bits, not bytes).
708// For SPARC v9, we sign-extend the given operand using SLL; SRL.
709// The generated instructions are returned in `mvec'.
710// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000711// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000712//
713void
714UltraSparcInstrInfo::CreateZeroExtensionInstructions(
715 const TargetMachine& target,
716 Function* F,
717 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000718 Value* destVal,
719 unsigned int numLowBits,
Misha Brukmana98cd452003-05-20 20:32:24 +0000720 std::vector<MachineInstr*>& mvec,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000721 MachineCodeForInstruction& mcfi) const
722{
723 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000724 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000725}