blob: f06e1929680a19ed1c43a95dfe3c94f43b494e5e [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
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000023//----------------------------------------------------------------------------
24// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +000025//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000026// Set a 32-bit unsigned constant in the register `dest', using
27// SETHI, OR in the worst case. This function correctly emulates
28// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
29//
30// The isSigned=true case is used to implement SETSW without duplicating code.
31//
32// Optimize some common cases:
33// (1) Small value that fits in simm13 field of OR: don't need SETHI.
34// (2) isSigned = true and C is a small negative signed value, i.e.,
35// high bits are 1, and the remaining bits fit in simm13(OR).
36//----------------------------------------------------------------------------
37
Vikram S. Adve53fd4002002-07-10 21:39:50 +000038static inline void
39CreateSETUWConst(const TargetMachine& target, uint32_t C,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000040 Instruction* dest, vector<MachineInstr*>& mvec,
41 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000042{
43 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000044
Vikram S. Adve53fd4002002-07-10 21:39:50 +000045 // In order to get efficient code, we should not generate the SETHI if
46 // all high bits are 1 (i.e., this is a small signed value that fits in
47 // the simm13 field of OR). So we check for and handle that case specially.
48 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
49 // In fact, sC == -sC, so we have to check for this explicitly.
50 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000051 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
52
Vikram S. Adve53fd4002002-07-10 21:39:50 +000053 // 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 +000054 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000055 {
Chris Lattner00dca912003-01-15 17:47:49 +000056 miSETHI = BuildMI(SETHI, 2).addZImm(C).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000057 miSETHI->setOperandHi32(0);
58 mvec.push_back(miSETHI);
59 }
60
61 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
62 // was generated, or if the low 10 bits are non-zero.
63 if (miSETHI==NULL || C & MAXLO)
64 {
65 if (miSETHI)
66 { // unsigned value with high-order bits set using SETHI
Chris Lattner00dca912003-01-15 17:47:49 +000067 miOR = BuildMI(OR, 3).addReg(dest).addZImm(C).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000068 miOR->setOperandLo32(1);
69 }
70 else
71 { // unsigned or small signed value that fits in simm13 field of OR
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000072 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000073 miOR = new MachineInstr(OR);
74 miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
75 miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
76 sC);
77 miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest);
78 }
79 mvec.push_back(miOR);
80 }
81
82 assert((miSETHI || miOR) && "Oops, no code was generated!");
83}
84
Vikram S. Adve53fd4002002-07-10 21:39:50 +000085
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000086//----------------------------------------------------------------------------
87// Function: CreateSETSWConst
88//
89// Set a 32-bit signed constant in the register `dest', with sign-extension
90// to 64 bits. This uses SETHI, OR, SRA in the worst case.
91// This function correctly emulates the SETSW pseudo-op for SPARC v9.
92//
93// Optimize the same cases as SETUWConst, plus:
94// (1) SRA is not needed for positive or small negative values.
95//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +000096
Vikram S. Adve53fd4002002-07-10 21:39:50 +000097static inline void
98CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000099 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000100{
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000101 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000102 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
103
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000104 // Sign-extend to the high 32 bits if needed
105 if (C < 0 && (-C) > (int32_t) MAXSIMM)
Chris Lattner00dca912003-01-15 17:47:49 +0000106 mvec.push_back(BuildMI(SRA, 3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000107}
108
109
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000110//----------------------------------------------------------------------------
111// Function: CreateSETXConst
112//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000113// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000114// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
115// This function correctly emulates the SETX pseudo-op for SPARC v9.
116//
117// Optimize the same cases as SETUWConst for each 32 bit word.
118//----------------------------------------------------------------------------
119
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000120static inline void
121CreateSETXConst(const TargetMachine& target, uint64_t C,
122 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000123 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000124{
125 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
126
127 MachineInstr* MI;
128
129 // Code to set the upper 32 bits of the value in register `tmpReg'
130 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
131
132 // Shift tmpReg left by 32 bits
Chris Lattner00dca912003-01-15 17:47:49 +0000133 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000134
135 // Code to set the low 32 bits of the value in register `dest'
136 CreateSETUWConst(target, C, dest, mvec);
137
138 // dest = OR(tmpReg, dest)
Chris Lattner00dca912003-01-15 17:47:49 +0000139 mvec.push_back(BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000140}
141
142
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000143//----------------------------------------------------------------------------
144// Function: CreateSETUWLabel
145//
146// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
147//----------------------------------------------------------------------------
148
149static inline void
150CreateSETUWLabel(const TargetMachine& target, Value* val,
151 Instruction* dest, vector<MachineInstr*>& mvec)
152{
153 MachineInstr* MI;
154
155 // Set the high 22 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000156 MI = BuildMI(SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000157 MI->setOperandHi32(0);
158 mvec.push_back(MI);
159
160 // Set the low 10 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000161 MI = BuildMI(OR, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000162 MI->setOperandLo32(1);
163 mvec.push_back(MI);
164}
165
166
167//----------------------------------------------------------------------------
168// Function: CreateSETXLabel
169//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000170// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000171//----------------------------------------------------------------------------
172
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000173static inline void
174CreateSETXLabel(const TargetMachine& target,
175 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000176 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000177{
178 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
179 "I only know about constant values and global addresses");
180
181 MachineInstr* MI;
182
Chris Lattner00dca912003-01-15 17:47:49 +0000183 MI = BuildMI(SETHI, 2).addReg(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000184 MI->setOperandHi64(0);
185 mvec.push_back(MI);
186
Chris Lattner00dca912003-01-15 17:47:49 +0000187 MI = BuildMI(OR, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000188 MI->setOperandLo64(1);
189 mvec.push_back(MI);
190
Chris Lattner00dca912003-01-15 17:47:49 +0000191 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
192 MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000193 MI->setOperandHi32(0);
194 mvec.push_back(MI);
195
Chris Lattner00dca912003-01-15 17:47:49 +0000196 MI = BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197 mvec.push_back(MI);
198
Chris Lattner00dca912003-01-15 17:47:49 +0000199 MI = BuildMI(OR, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000200 MI->setOperandLo32(1);
201 mvec.push_back(MI);
202}
203
Vikram S. Adve30764b82001-10-18 00:01:48 +0000204
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000205//----------------------------------------------------------------------------
206// Function: CreateUIntSetInstruction
207//
208// Create code to Set an unsigned constant in the register `dest'.
209// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
210// CreateSETSWConst is an optimization for the case that the unsigned value
211// has all ones in the 33 high bits (so that sign-extension sets them all).
212//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000213
Vikram S. Adve242a8082002-05-19 15:25:51 +0000214static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000215CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000216 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000217 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000218 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000219{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000220 static const uint64_t lo32 = (uint32_t) ~0;
221 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
222 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
223 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
224 { // All high 33 (not 32) bits are 1s: sign-extension will take care
225 // of high 32 bits, so use the sequence for signed int
226 CreateSETSWConst(target, (int32_t) C, dest, mvec);
227 }
228 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000229 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000230 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000231 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000232 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000233 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000234}
235
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000236
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000237//----------------------------------------------------------------------------
238// Function: CreateIntSetInstruction
239//
240// Create code to Set a signed constant in the register `dest'.
241// Really the same as CreateUIntSetInstruction.
242//----------------------------------------------------------------------------
243
244static inline void
245CreateIntSetInstruction(const TargetMachine& target,
246 int64_t C, Instruction* dest,
247 std::vector<MachineInstr*>& mvec,
248 MachineCodeForInstruction& mcfi)
249{
250 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
251}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000252
Vikram S. Adve30764b82001-10-18 00:01:48 +0000253
254//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000255// Create a table of LLVM opcode -> max. immediate constant likely to
256// be usable for that operation.
257//---------------------------------------------------------------------------
258
259// Entry == 0 ==> no immediate constant field exists at all.
260// Entry > 0 ==> abs(immediate constant) <= Entry
261//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000262vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000263
264static int
265MaxConstantForInstr(unsigned llvmOpCode)
266{
267 int modelOpCode = -1;
268
Chris Lattner0b16ae22002-10-13 19:39:16 +0000269 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
270 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000271 modelOpCode = ADD;
272 else
273 switch(llvmOpCode) {
274 case Instruction::Ret: modelOpCode = JMPLCALL; break;
275
276 case Instruction::Malloc:
277 case Instruction::Alloca:
278 case Instruction::GetElementPtr:
279 case Instruction::PHINode:
280 case Instruction::Cast:
281 case Instruction::Call: modelOpCode = ADD; break;
282
283 case Instruction::Shl:
284 case Instruction::Shr: modelOpCode = SLLX; break;
285
286 default: break;
287 };
288
289 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
290}
291
292static void
293InitializeMaxConstantsTable()
294{
295 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000296 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000297 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000298 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000299 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000300 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000301 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000302 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000303 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000304 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000305 MaxConstantsTable[op] = MaxConstantForInstr(op);
306}
307
308
309//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000310// class UltraSparcInstrInfo
311//
312// Purpose:
313// Information about individual instructions.
314// Most information is stored in the SparcMachineInstrDesc array above.
315// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000316// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000317//---------------------------------------------------------------------------
318
319/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000320UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000321 : TargetInstrInfo(SparcMachineInstrDesc,
322 /*descSize = */ NUM_TOTAL_OPCODES,
323 /*numRealOpCodes = */ NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000324{
Vikram S. Adve49001162002-09-16 15:56:01 +0000325 InitializeMaxConstantsTable();
326}
327
328bool
329UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
330 const Instruction* I) const
331{
332 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
333 return true;
334
335 if (isa<ConstantPointerNull>(CV)) // can always use %g0
336 return false;
337
338 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000339 /* Large unsigned longs may really just be small negative signed longs */
340 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000341
342 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000343 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000344
345 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000346 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000347
348 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000349}
350
Vikram S. Advee76af292002-03-18 03:09:15 +0000351//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000352// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000354// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000355// The generated instructions are returned in `mvec'.
356// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000357// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000358//
359void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000360UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
361 Function* F,
362 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000363 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000364 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000365 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000366{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000367 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000368 "I only know about constant values and global addresses");
369
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000370 // Use a "set" instruction for known constants or symbolic constants (labels)
371 // that can go in an integer reg.
372 // We have to use a "load" instruction for all other constants,
373 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000374 //
375 const Type* valType = val->getType();
376
Vikram S. Adve893cace2002-10-13 00:04:26 +0000377 // Unfortunate special case: a ConstantPointerRef is just a
378 // reference to GlobalValue.
379 if (isa<ConstantPointerRef>(val))
380 val = cast<ConstantPointerRef>(val)->getValue();
381
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000382 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000383 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000384 TmpInstruction* tmpReg =
385 new TmpInstruction(PointerType::get(val->getType()), val);
386 mcfi.addTemp(tmpReg);
387 CreateSETXLabel(target, val, tmpReg, dest, mvec);
388 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000389 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000390 {
391 bool isValidConstant;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000392 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
393 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000394
395 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000396 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000397 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
398 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000399
400 if (opSize > destSize ||
401 (val->getType()->isSigned()
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000402 && destSize < target.getTargetData().getIntegerRegize()))
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000403 { // operand is larger than dest,
404 // OR both are equal but smaller than the full register size
405 // AND operand is signed, so it may have extra sign bits:
406 // mask high bits
407 C = C & ((1U << 8*destSize) - 1);
408 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000409 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000410 }
411 else
412 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000413 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
414 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000415
416 if (opSize > destSize)
417 // operand is larger than dest: mask high bits
418 C = C & ((1U << 8*destSize) - 1);
419
420 if (opSize > destSize ||
421 (opSize == destSize && !val->getType()->isSigned()))
422 // sign-extend from destSize to 64 bits
423 C = ((C & (1U << (8*destSize - 1)))
424 ? C | ~((1U << 8*destSize) - 1)
425 : C);
426
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000427 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000428 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000429 }
430 else
431 {
432 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000433 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000434 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000435
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000436 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000437 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000438 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000439 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000440
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000441 // Create another TmpInstruction for the address register
442 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000443 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000444 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000445
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000446 // Put the address (a symbolic name) into a register
447 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000448
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000449 // Generate the load instruction
450 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000451 unsigned Opcode = ChooseLoadInstruction(val->getType());
452 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
Chris Lattner00dca912003-01-15 17:47:49 +0000453 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000454
455 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000456 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000457 }
458}
459
460
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000461// Create an instruction sequence to copy an integer register `val'
462// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000463// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000464// The generated instructions are returned in `mvec'.
465// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000466// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000467//
468void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000469UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
470 Function* F,
471 Value* val,
472 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000473 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000474 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000475{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000476 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
477 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000478 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000479 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000480
481 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000482 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000483
484 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000485 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000486
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000487 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000488 // The store and load opCodes are based on the size of the source value.
489 // If the value is smaller than 32 bits, we must sign- or zero-extend it
490 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000491 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000492 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
493 Value* storeVal = val;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000494 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy))
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000495 { // sign- or zero-extend respectively
496 storeVal = new TmpInstruction(storeType, val);
497 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000498 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000499 mvec, mcfi);
500 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000501 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000502 mvec, mcfi);
503 }
504 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType));
505 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal);
Vikram S. Advee76af292002-03-18 03:09:15 +0000506 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000507 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
508 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000509
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000510 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000511 // The type of the load opCode is the floating point type that matches the
512 // stored type in size:
513 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000514 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000515 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
516 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000517 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
518 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
519 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000520 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000521}
522
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000523// Similarly, create an instruction sequence to copy an FP register
524// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000525// The generated instructions are returned in `mvec'.
526// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000527// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000528//
529void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000530UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
531 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000532 Value* val,
533 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000534 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000535 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000536{
Vikram S. Advec190c012002-07-31 21:13:31 +0000537 const Type* opTy = val->getType();
538 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000539
Vikram S. Advec190c012002-07-31 21:13:31 +0000540 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000541 assert((destTy->isIntegral() || isa<PointerType>(destTy))
542 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000543
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000544 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000545
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000546 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000547 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000548 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000549 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000550 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
551 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
552 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000553 mvec.push_back(store);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000554
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000555 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000556 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000557 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000558 // On SparcV9: int for float, long for double.
559 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000560 // ensure correct sign-extension for UByte, UShort or UInt:
561 //
562 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Vikram S. Advec190c012002-07-31 21:13:31 +0000563 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000564 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000565 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000566 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000567 mvec.push_back(load);
568}
569
570
571// Create instruction(s) to copy src to dest, for arbitrary types
572// The generated instructions are returned in `mvec'.
573// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000574// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575//
576void
577UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
578 Function *F,
579 Value* src,
580 Instruction* dest,
581 vector<MachineInstr*>& mvec,
582 MachineCodeForInstruction& mcfi) const
583{
584 bool loadConstantToReg = false;
585
586 const Type* resultType = dest->getType();
587
588 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
589 if (opCode == INVALID_OPCODE)
590 {
591 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
592 return;
593 }
594
595 // if `src' is a constant that doesn't fit in the immed field or if it is
596 // a global variable (i.e., a constant address), generate a load
597 // instruction instead of an add
598 //
599 if (isa<Constant>(src))
600 {
601 unsigned int machineRegNum;
602 int64_t immedValue;
603 MachineOperand::MachineOperandType opType =
604 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
605 machineRegNum, immedValue);
606
607 if (opType == MachineOperand::MO_VirtualRegister)
608 loadConstantToReg = true;
609 }
610 else if (isa<GlobalValue>(src))
611 loadConstantToReg = true;
612
613 if (loadConstantToReg)
614 { // `src' is constant and cannot fit in immed field for the ADD
615 // Insert instructions to "load" the constant into a register
616 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
617 mvec, mcfi);
618 }
619 else
620 { // Create an add-with-0 instruction of the appropriate type.
621 // Make `src' the second operand, in case it is a constant
622 // Use (unsigned long) 0 for a NULL pointer value.
623 //
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000624 const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType;
625 MachineInstr* MI =
626 BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty))
Chris Lattner00dca912003-01-15 17:47:49 +0000627 .addReg(src).addRegDef(dest);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000628 mvec.push_back(MI);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000629 }
630}
631
632
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000633// Helper function for sign-extension and zero-extension.
634// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
635inline void
636CreateBitExtensionInstructions(bool signExtend,
637 const TargetMachine& target,
638 Function* F,
639 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000640 Value* destVal,
641 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000642 vector<MachineInstr*>& mvec,
643 MachineCodeForInstruction& mcfi)
644{
645 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000646
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000647 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
648
649 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000650 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000651 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
652 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000653 mcfi.addTemp(tmpI);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000654 mvec.push_back(BuildMI(SLLX, 3).addReg(srcVal).addZImm(32-numLowBits)
Chris Lattner00dca912003-01-15 17:47:49 +0000655 .addRegDef(tmpI));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000656 srcVal = tmpI;
657 }
658
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000659 mvec.push_back(BuildMI(signExtend? SRA : SRL, 3).addReg(srcVal)
Chris Lattner00dca912003-01-15 17:47:49 +0000660 .addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000661}
662
663
Vikram S. Adve242a8082002-05-19 15:25:51 +0000664// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000665// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000666// The generated instructions are returned in `mvec'.
667// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000668// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000669//
670void
671UltraSparcInstrInfo::CreateSignExtensionInstructions(
672 const TargetMachine& target,
673 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000674 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000675 Value* destVal,
676 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000677 vector<MachineInstr*>& mvec,
678 MachineCodeForInstruction& mcfi) const
679{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000680 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000681 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000682}
683
684
685// Create instruction sequence to produce a zero-extended register value
686// from an arbitrary-sized integer value (sized in bits, not bytes).
687// For SPARC v9, we sign-extend the given operand using SLL; SRL.
688// The generated instructions are returned in `mvec'.
689// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000690// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000691//
692void
693UltraSparcInstrInfo::CreateZeroExtensionInstructions(
694 const TargetMachine& target,
695 Function* F,
696 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000697 Value* destVal,
698 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000699 vector<MachineInstr*>& mvec,
700 MachineCodeForInstruction& mcfi) const
701{
702 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000703 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000704}