blob: 331fd4608dc587c9df53fcb77d267cffb02eb71b [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>
Anand Shuklacfb22d32002-06-25 20:55:50 +000017using std::vector;
Vikram S. Adve30764b82001-10-18 00:01:48 +000018
Vikram S. Adve53fd4002002-07-10 21:39:50 +000019static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
20static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
21
22
Chris Lattner795ba6c2003-01-15 21:36:50 +000023//---------------------------------------------------------------------------
24// Function GetConstantValueAsUnsignedInt
25// Function GetConstantValueAsSignedInt
26//
27// Convenience functions to get the value of an integral constant, for an
28// appropriate integer or non-integer type that can be held in a signed
29// or unsigned integer respectively. The type of the argument must be
30// the following:
31// Signed or unsigned integer
32// Boolean
33// Pointer
34//
35// isValidConstant is set to true if a valid constant was found.
36//---------------------------------------------------------------------------
37
38static uint64_t
39GetConstantValueAsUnsignedInt(const Value *V,
40 bool &isValidConstant)
41{
42 isValidConstant = true;
43
44 if (isa<Constant>(V))
45 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
46 return (int64_t)CB->getValue();
47 else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
48 return (uint64_t)CS->getValue();
49 else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
50 return CU->getValue();
51
52 isValidConstant = false;
53 return 0;
54}
55
56int64_t
57GetConstantValueAsSignedInt(const Value *V, bool &isValidConstant)
58{
59 uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant);
60 if (isValidConstant) {
61 if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed
62 return (int64_t) C;
63 else
64 isValidConstant = false;
65 }
66 return 0;
67}
68
69
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000070//----------------------------------------------------------------------------
71// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +000072//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000073// Set a 32-bit unsigned constant in the register `dest', using
74// SETHI, OR in the worst case. This function correctly emulates
75// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
76//
77// The isSigned=true case is used to implement SETSW without duplicating code.
78//
79// Optimize some common cases:
80// (1) Small value that fits in simm13 field of OR: don't need SETHI.
81// (2) isSigned = true and C is a small negative signed value, i.e.,
82// high bits are 1, and the remaining bits fit in simm13(OR).
83//----------------------------------------------------------------------------
84
Vikram S. Adve53fd4002002-07-10 21:39:50 +000085static inline void
86CreateSETUWConst(const TargetMachine& target, uint32_t C,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000087 Instruction* dest, vector<MachineInstr*>& mvec,
88 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000089{
90 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000091
Vikram S. Adve53fd4002002-07-10 21:39:50 +000092 // In order to get efficient code, we should not generate the SETHI if
93 // all high bits are 1 (i.e., this is a small signed value that fits in
94 // the simm13 field of OR). So we check for and handle that case specially.
95 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
96 // In fact, sC == -sC, so we have to check for this explicitly.
97 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000098 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
99
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000100 // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000101 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000102 {
Chris Lattner00dca912003-01-15 17:47:49 +0000103 miSETHI = BuildMI(SETHI, 2).addZImm(C).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000104 miSETHI->setOperandHi32(0);
105 mvec.push_back(miSETHI);
106 }
107
108 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
109 // was generated, or if the low 10 bits are non-zero.
110 if (miSETHI==NULL || C & MAXLO)
111 {
112 if (miSETHI)
113 { // unsigned value with high-order bits set using SETHI
Chris Lattner00dca912003-01-15 17:47:49 +0000114 miOR = BuildMI(OR, 3).addReg(dest).addZImm(C).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000115 miOR->setOperandLo32(1);
116 }
117 else
118 { // unsigned or small signed value that fits in simm13 field of OR
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000119 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Chris Lattner54e898e2003-01-15 19:23:34 +0000120 miOR = BuildMI(OR, 3).addMReg(target.getRegInfo().getZeroRegNum())
121 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000122 }
123 mvec.push_back(miOR);
124 }
125
126 assert((miSETHI || miOR) && "Oops, no code was generated!");
127}
128
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000129
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000130//----------------------------------------------------------------------------
131// Function: CreateSETSWConst
132//
133// Set a 32-bit signed constant in the register `dest', with sign-extension
134// to 64 bits. This uses SETHI, OR, SRA in the worst case.
135// This function correctly emulates the SETSW pseudo-op for SPARC v9.
136//
137// Optimize the same cases as SETUWConst, plus:
138// (1) SRA is not needed for positive or small negative values.
139//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000140
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000141static inline void
142CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000143 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000144{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000145 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000146 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
147
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000148 // Sign-extend to the high 32 bits if needed
149 if (C < 0 && (-C) > (int32_t) MAXSIMM)
Chris Lattner00dca912003-01-15 17:47:49 +0000150 mvec.push_back(BuildMI(SRA, 3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000151}
152
153
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000154//----------------------------------------------------------------------------
155// Function: CreateSETXConst
156//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000157// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000158// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
159// This function correctly emulates the SETX pseudo-op for SPARC v9.
160//
161// Optimize the same cases as SETUWConst for each 32 bit word.
162//----------------------------------------------------------------------------
163
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000164static inline void
165CreateSETXConst(const TargetMachine& target, uint64_t C,
166 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000167 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000168{
169 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
170
171 MachineInstr* MI;
172
173 // Code to set the upper 32 bits of the value in register `tmpReg'
174 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
175
176 // Shift tmpReg left by 32 bits
Chris Lattner00dca912003-01-15 17:47:49 +0000177 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000178
179 // Code to set the low 32 bits of the value in register `dest'
180 CreateSETUWConst(target, C, dest, mvec);
181
182 // dest = OR(tmpReg, dest)
Chris Lattner00dca912003-01-15 17:47:49 +0000183 mvec.push_back(BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000184}
185
186
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000187//----------------------------------------------------------------------------
188// Function: CreateSETUWLabel
189//
190// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
191//----------------------------------------------------------------------------
192
193static inline void
194CreateSETUWLabel(const TargetMachine& target, Value* val,
195 Instruction* dest, vector<MachineInstr*>& mvec)
196{
197 MachineInstr* MI;
198
199 // Set the high 22 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000200 MI = BuildMI(SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000201 MI->setOperandHi32(0);
202 mvec.push_back(MI);
203
204 // Set the low 10 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000205 MI = BuildMI(OR, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000206 MI->setOperandLo32(1);
207 mvec.push_back(MI);
208}
209
210
211//----------------------------------------------------------------------------
212// Function: CreateSETXLabel
213//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000214// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000215//----------------------------------------------------------------------------
216
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000217static inline void
218CreateSETXLabel(const TargetMachine& target,
219 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000220 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000221{
222 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
223 "I only know about constant values and global addresses");
224
225 MachineInstr* MI;
226
Chris Lattner54e898e2003-01-15 19:23:34 +0000227 MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000228 MI->setOperandHi64(0);
229 mvec.push_back(MI);
230
Chris Lattner00dca912003-01-15 17:47:49 +0000231 MI = BuildMI(OR, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000232 MI->setOperandLo64(1);
233 mvec.push_back(MI);
234
Chris Lattner00dca912003-01-15 17:47:49 +0000235 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
236 MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000237 MI->setOperandHi32(0);
238 mvec.push_back(MI);
239
Chris Lattner00dca912003-01-15 17:47:49 +0000240 MI = BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000241 mvec.push_back(MI);
242
Chris Lattner00dca912003-01-15 17:47:49 +0000243 MI = BuildMI(OR, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000244 MI->setOperandLo32(1);
245 mvec.push_back(MI);
246}
247
Vikram S. Adve30764b82001-10-18 00:01:48 +0000248
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000249//----------------------------------------------------------------------------
250// Function: CreateUIntSetInstruction
251//
252// Create code to Set an unsigned constant in the register `dest'.
253// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
254// CreateSETSWConst is an optimization for the case that the unsigned value
255// has all ones in the 33 high bits (so that sign-extension sets them all).
256//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000257
Vikram S. Adve242a8082002-05-19 15:25:51 +0000258static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000259CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000260 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000261 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000262 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000263{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000264 static const uint64_t lo32 = (uint32_t) ~0;
265 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
266 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
267 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
268 { // All high 33 (not 32) bits are 1s: sign-extension will take care
269 // of high 32 bits, so use the sequence for signed int
270 CreateSETSWConst(target, (int32_t) C, dest, mvec);
271 }
272 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000273 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000274 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000275 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000276 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000277 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000278}
279
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000280
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000281//----------------------------------------------------------------------------
282// Function: CreateIntSetInstruction
283//
284// Create code to Set a signed constant in the register `dest'.
285// Really the same as CreateUIntSetInstruction.
286//----------------------------------------------------------------------------
287
288static inline void
289CreateIntSetInstruction(const TargetMachine& target,
290 int64_t C, Instruction* dest,
291 std::vector<MachineInstr*>& mvec,
292 MachineCodeForInstruction& mcfi)
293{
294 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
295}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000296
Vikram S. Adve30764b82001-10-18 00:01:48 +0000297
298//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000299// Create a table of LLVM opcode -> max. immediate constant likely to
300// be usable for that operation.
301//---------------------------------------------------------------------------
302
303// Entry == 0 ==> no immediate constant field exists at all.
304// Entry > 0 ==> abs(immediate constant) <= Entry
305//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000306vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000307
308static int
309MaxConstantForInstr(unsigned llvmOpCode)
310{
311 int modelOpCode = -1;
312
Chris Lattner0b16ae22002-10-13 19:39:16 +0000313 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
314 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000315 modelOpCode = ADD;
316 else
317 switch(llvmOpCode) {
318 case Instruction::Ret: modelOpCode = JMPLCALL; break;
319
320 case Instruction::Malloc:
321 case Instruction::Alloca:
322 case Instruction::GetElementPtr:
323 case Instruction::PHINode:
324 case Instruction::Cast:
325 case Instruction::Call: modelOpCode = ADD; break;
326
327 case Instruction::Shl:
328 case Instruction::Shr: modelOpCode = SLLX; break;
329
330 default: break;
331 };
332
333 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
334}
335
336static void
337InitializeMaxConstantsTable()
338{
339 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000340 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000341 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000342 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++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::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++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::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000347 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000348 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000349 MaxConstantsTable[op] = MaxConstantForInstr(op);
350}
351
352
353//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000354// class UltraSparcInstrInfo
355//
356// Purpose:
357// Information about individual instructions.
358// Most information is stored in the SparcMachineInstrDesc array above.
359// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000360// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000361//---------------------------------------------------------------------------
362
363/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000364UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000365 : TargetInstrInfo(SparcMachineInstrDesc,
366 /*descSize = */ NUM_TOTAL_OPCODES,
367 /*numRealOpCodes = */ NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000368{
Vikram S. Adve49001162002-09-16 15:56:01 +0000369 InitializeMaxConstantsTable();
370}
371
372bool
373UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
374 const Instruction* I) const
375{
376 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
377 return true;
378
379 if (isa<ConstantPointerNull>(CV)) // can always use %g0
380 return false;
381
382 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000383 /* Large unsigned longs may really just be small negative signed longs */
384 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000385
386 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000387 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000388
389 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000390 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000391
392 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000393}
394
Vikram S. Advee76af292002-03-18 03:09:15 +0000395//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000396// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000397// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000398// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000399// The generated instructions are returned in `mvec'.
400// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000401// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000402//
403void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000404UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
405 Function* F,
406 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000407 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000408 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000409 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000410{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000411 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000412 "I only know about constant values and global addresses");
413
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000414 // Use a "set" instruction for known constants or symbolic constants (labels)
415 // that can go in an integer reg.
416 // We have to use a "load" instruction for all other constants,
417 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000418 //
419 const Type* valType = val->getType();
420
Vikram S. Adve893cace2002-10-13 00:04:26 +0000421 // Unfortunate special case: a ConstantPointerRef is just a
422 // reference to GlobalValue.
423 if (isa<ConstantPointerRef>(val))
424 val = cast<ConstantPointerRef>(val)->getValue();
425
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000426 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000427 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000428 TmpInstruction* tmpReg =
429 new TmpInstruction(PointerType::get(val->getType()), val);
430 mcfi.addTemp(tmpReg);
431 CreateSETXLabel(target, val, tmpReg, dest, mvec);
432 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000433 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000434 {
435 bool isValidConstant;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000436 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
437 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000438
439 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000440 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000441 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
442 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000443
Chris Lattner7a5adc32003-04-26 19:44:35 +0000444 if (opSize > destSize || (val->getType()->isSigned() && destSize < 8))
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000445 { // operand is larger than dest,
446 // OR both are equal but smaller than the full register size
447 // AND operand is signed, so it may have extra sign bits:
448 // mask high bits
449 C = C & ((1U << 8*destSize) - 1);
450 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000451 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000452 }
453 else
454 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000455 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
456 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000457
458 if (opSize > destSize)
459 // operand is larger than dest: mask high bits
460 C = C & ((1U << 8*destSize) - 1);
461
462 if (opSize > destSize ||
463 (opSize == destSize && !val->getType()->isSigned()))
464 // sign-extend from destSize to 64 bits
465 C = ((C & (1U << (8*destSize - 1)))
466 ? C | ~((1U << 8*destSize) - 1)
467 : C);
468
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000469 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000470 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000471 }
472 else
473 {
474 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000475 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000476 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000477
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000478 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000479 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000480 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000481 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000482
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000483 // Create another TmpInstruction for the address register
484 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000485 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000486 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000487
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000488 // Put the address (a symbolic name) into a register
489 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000490
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000491 // Generate the load instruction
492 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000493 unsigned Opcode = ChooseLoadInstruction(val->getType());
494 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
Chris Lattner00dca912003-01-15 17:47:49 +0000495 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000496
497 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000498 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000499 }
500}
501
502
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000503// Create an instruction sequence to copy an integer register `val'
504// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000505// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000506// The generated instructions are returned in `mvec'.
507// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000508// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000509//
510void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000511UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
512 Function* F,
513 Value* val,
514 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000515 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000516 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000517{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000518 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
519 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000520 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000521 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000522
523 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000524 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000525
526 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000527 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000528
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000529 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000530 // The store and load opCodes are based on the size of the source value.
531 // If the value is smaller than 32 bits, we must sign- or zero-extend it
532 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000533 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000534 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
535 Value* storeVal = val;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000536 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy))
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000537 { // sign- or zero-extend respectively
538 storeVal = new TmpInstruction(storeType, val);
539 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000540 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000541 mvec, mcfi);
542 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000543 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000544 mvec, mcfi);
545 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000546
547 unsigned FPReg = target.getRegInfo().getFramePointer();
548 mvec.push_back(BuildMI(ChooseStoreInstruction(storeType), 3)
549 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000550
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000551 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000552 // The type of the load opCode is the floating point type that matches the
553 // stored type in size:
554 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000555 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000556 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000557 mvec.push_back(BuildMI(ChooseLoadInstruction(loadType), 3)
558 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000559}
560
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000561// Similarly, create an instruction sequence to copy an FP register
562// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000563// The generated instructions are returned in `mvec'.
564// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000565// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000566//
567void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000568UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
569 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000570 Value* val,
571 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000572 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000573 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000574{
Vikram S. Advec190c012002-07-31 21:13:31 +0000575 const Type* opTy = val->getType();
576 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000577
Vikram S. Advec190c012002-07-31 21:13:31 +0000578 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000579 assert((destTy->isIntegral() || isa<PointerType>(destTy))
580 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000581
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000582 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000583
Chris Lattner54e898e2003-01-15 19:23:34 +0000584 unsigned FPReg = target.getRegInfo().getFramePointer();
585
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000586 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000587 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000588 //
Chris Lattner54e898e2003-01-15 19:23:34 +0000589 mvec.push_back(BuildMI(ChooseStoreInstruction(opTy), 3)
590 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000591
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000592 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000593 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000594 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000595 // On SparcV9: int for float, long for double.
596 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000597 // ensure correct sign-extension for UByte, UShort or UInt:
598 //
599 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000600 mvec.push_back(BuildMI(ChooseLoadInstruction(loadTy), 3).addMReg(FPReg)
601 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000602}
603
604
605// Create instruction(s) to copy src to dest, for arbitrary types
606// The generated instructions are returned in `mvec'.
607// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000608// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000609//
610void
611UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
612 Function *F,
613 Value* src,
614 Instruction* dest,
615 vector<MachineInstr*>& mvec,
616 MachineCodeForInstruction& mcfi) const
617{
618 bool loadConstantToReg = false;
619
620 const Type* resultType = dest->getType();
621
622 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
623 if (opCode == INVALID_OPCODE)
624 {
625 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
626 return;
627 }
628
629 // if `src' is a constant that doesn't fit in the immed field or if it is
630 // a global variable (i.e., a constant address), generate a load
631 // instruction instead of an add
632 //
633 if (isa<Constant>(src))
634 {
635 unsigned int machineRegNum;
636 int64_t immedValue;
637 MachineOperand::MachineOperandType opType =
638 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
639 machineRegNum, immedValue);
640
641 if (opType == MachineOperand::MO_VirtualRegister)
642 loadConstantToReg = true;
643 }
644 else if (isa<GlobalValue>(src))
645 loadConstantToReg = true;
646
647 if (loadConstantToReg)
648 { // `src' is constant and cannot fit in immed field for the ADD
649 // Insert instructions to "load" the constant into a register
650 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
651 mvec, mcfi);
652 }
653 else
654 { // Create an add-with-0 instruction of the appropriate type.
655 // Make `src' the second operand, in case it is a constant
656 // Use (unsigned long) 0 for a NULL pointer value.
657 //
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000658 const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType;
659 MachineInstr* MI =
660 BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty))
Chris Lattner00dca912003-01-15 17:47:49 +0000661 .addReg(src).addRegDef(dest);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000662 mvec.push_back(MI);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000663 }
664}
665
666
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000667// Helper function for sign-extension and zero-extension.
668// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
669inline void
670CreateBitExtensionInstructions(bool signExtend,
671 const TargetMachine& target,
672 Function* F,
673 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000674 Value* destVal,
675 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000676 vector<MachineInstr*>& mvec,
677 MachineCodeForInstruction& mcfi)
678{
679 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000680
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000681 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
682
683 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000684 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000685 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
686 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000687 mcfi.addTemp(tmpI);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000688 mvec.push_back(BuildMI(SLLX, 3).addReg(srcVal).addZImm(32-numLowBits)
Chris Lattner00dca912003-01-15 17:47:49 +0000689 .addRegDef(tmpI));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000690 srcVal = tmpI;
691 }
692
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000693 mvec.push_back(BuildMI(signExtend? SRA : SRL, 3).addReg(srcVal)
Chris Lattner00dca912003-01-15 17:47:49 +0000694 .addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000695}
696
697
Vikram S. Adve242a8082002-05-19 15:25:51 +0000698// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000699// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000700// The generated instructions are returned in `mvec'.
701// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000702// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000703//
704void
705UltraSparcInstrInfo::CreateSignExtensionInstructions(
706 const TargetMachine& target,
707 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000708 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000709 Value* destVal,
710 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000711 vector<MachineInstr*>& mvec,
712 MachineCodeForInstruction& mcfi) const
713{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000714 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000715 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000716}
717
718
719// Create instruction sequence to produce a zero-extended register value
720// from an arbitrary-sized integer value (sized in bits, not bytes).
721// For SPARC v9, we sign-extend the given operand using SLL; SRL.
722// The generated instructions are returned in `mvec'.
723// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000724// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000725//
726void
727UltraSparcInstrInfo::CreateZeroExtensionInstructions(
728 const TargetMachine& target,
729 Function* F,
730 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000731 Value* destVal,
732 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000733 vector<MachineInstr*>& mvec,
734 MachineCodeForInstruction& mcfi) const
735{
736 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000737 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000738}