blob: b70b7f143569ed9383dadbbbfb45e1320a51dbb5 [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 {
56 miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest);
57 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 Lattnere5b1ed92003-01-15 00:03:28 +000067 miOR = BuildMI(OR, 3).addReg(dest).addZImm(C).addReg(dest, MOTy::Def);
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 Lattnere5b1ed92003-01-15 00:03:28 +0000106 mvec.push_back(BuildMI(SRA, 3).addReg(dest).addZImm(0).addReg(dest,
107 MOTy::Def));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000108}
109
110
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000111//----------------------------------------------------------------------------
112// Function: CreateSETXConst
113//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000114// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000115// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
116// This function correctly emulates the SETX pseudo-op for SPARC v9.
117//
118// Optimize the same cases as SETUWConst for each 32 bit word.
119//----------------------------------------------------------------------------
120
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000121static inline void
122CreateSETXConst(const TargetMachine& target, uint64_t C,
123 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000124 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000125{
126 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
127
128 MachineInstr* MI;
129
130 // Code to set the upper 32 bits of the value in register `tmpReg'
131 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
132
133 // Shift tmpReg left by 32 bits
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000134 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addReg(tmpReg,
135 MOTy::Def));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000136
137 // Code to set the low 32 bits of the value in register `dest'
138 CreateSETUWConst(target, C, dest, mvec);
139
140 // dest = OR(tmpReg, dest)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000141 mvec.push_back(BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addReg(dest,
142 MOTy::Def));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000143}
144
145
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000146//----------------------------------------------------------------------------
147// Function: CreateSETUWLabel
148//
149// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
150//----------------------------------------------------------------------------
151
152static inline void
153CreateSETUWLabel(const TargetMachine& target, Value* val,
154 Instruction* dest, vector<MachineInstr*>& mvec)
155{
156 MachineInstr* MI;
157
158 // Set the high 22 bits in dest
159 MI = Create2OperandInstr(SETHI, val, dest);
160 MI->setOperandHi32(0);
161 mvec.push_back(MI);
162
163 // Set the low 10 bits in dest
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000164 MI = BuildMI(OR, 3).addReg(dest).addReg(val).addReg(dest, MOTy::Def);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000165 MI->setOperandLo32(1);
166 mvec.push_back(MI);
167}
168
169
170//----------------------------------------------------------------------------
171// Function: CreateSETXLabel
172//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000173// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000174//----------------------------------------------------------------------------
175
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000176static inline void
177CreateSETXLabel(const TargetMachine& target,
178 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000179 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000180{
181 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
182 "I only know about constant values and global addresses");
183
184 MachineInstr* MI;
185
186 MI = Create2OperandInstr_Addr(SETHI, val, tmpReg);
187 MI->setOperandHi64(0);
188 mvec.push_back(MI);
189
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000190 MI = BuildMI(OR, 3).addReg(tmpReg).addPCDisp(val).addReg(tmpReg, MOTy::Def);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000191 MI->setOperandLo64(1);
192 mvec.push_back(MI);
193
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000194 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addReg(tmpReg,
195 MOTy::Def));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000196 MI = Create2OperandInstr_Addr(SETHI, val, dest);
197 MI->setOperandHi32(0);
198 mvec.push_back(MI);
199
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000200 MI = BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addReg(dest, MOTy::Def);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000201 mvec.push_back(MI);
202
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000203 MI = BuildMI(OR, 3).addReg(dest).addPCDisp(val).addReg(dest, MOTy::Def);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000204 MI->setOperandLo32(1);
205 mvec.push_back(MI);
206}
207
Vikram S. Adve30764b82001-10-18 00:01:48 +0000208
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000209//----------------------------------------------------------------------------
210// Function: CreateUIntSetInstruction
211//
212// Create code to Set an unsigned constant in the register `dest'.
213// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
214// CreateSETSWConst is an optimization for the case that the unsigned value
215// has all ones in the 33 high bits (so that sign-extension sets them all).
216//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000217
Vikram S. Adve242a8082002-05-19 15:25:51 +0000218static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000219CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000220 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000221 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000222 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000223{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000224 static const uint64_t lo32 = (uint32_t) ~0;
225 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
226 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
227 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
228 { // All high 33 (not 32) bits are 1s: sign-extension will take care
229 // of high 32 bits, so use the sequence for signed int
230 CreateSETSWConst(target, (int32_t) C, dest, mvec);
231 }
232 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000233 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000234 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000235 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000236 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000237 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000238}
239
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000240
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000241//----------------------------------------------------------------------------
242// Function: CreateIntSetInstruction
243//
244// Create code to Set a signed constant in the register `dest'.
245// Really the same as CreateUIntSetInstruction.
246//----------------------------------------------------------------------------
247
248static inline void
249CreateIntSetInstruction(const TargetMachine& target,
250 int64_t C, Instruction* dest,
251 std::vector<MachineInstr*>& mvec,
252 MachineCodeForInstruction& mcfi)
253{
254 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
255}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000256
Vikram S. Adve30764b82001-10-18 00:01:48 +0000257
258//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000259// Create a table of LLVM opcode -> max. immediate constant likely to
260// be usable for that operation.
261//---------------------------------------------------------------------------
262
263// Entry == 0 ==> no immediate constant field exists at all.
264// Entry > 0 ==> abs(immediate constant) <= Entry
265//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000266vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000267
268static int
269MaxConstantForInstr(unsigned llvmOpCode)
270{
271 int modelOpCode = -1;
272
Chris Lattner0b16ae22002-10-13 19:39:16 +0000273 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
274 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000275 modelOpCode = ADD;
276 else
277 switch(llvmOpCode) {
278 case Instruction::Ret: modelOpCode = JMPLCALL; break;
279
280 case Instruction::Malloc:
281 case Instruction::Alloca:
282 case Instruction::GetElementPtr:
283 case Instruction::PHINode:
284 case Instruction::Cast:
285 case Instruction::Call: modelOpCode = ADD; break;
286
287 case Instruction::Shl:
288 case Instruction::Shr: modelOpCode = SLLX; break;
289
290 default: break;
291 };
292
293 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
294}
295
296static void
297InitializeMaxConstantsTable()
298{
299 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000300 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000301 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000302 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++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::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000305 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000306 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000307 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000308 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000309 MaxConstantsTable[op] = MaxConstantForInstr(op);
310}
311
312
313//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000314// class UltraSparcInstrInfo
315//
316// Purpose:
317// Information about individual instructions.
318// Most information is stored in the SparcMachineInstrDesc array above.
319// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000320// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000321//---------------------------------------------------------------------------
322
323/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000324UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000325 : TargetInstrInfo(SparcMachineInstrDesc,
326 /*descSize = */ NUM_TOTAL_OPCODES,
327 /*numRealOpCodes = */ NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000328{
Vikram S. Adve49001162002-09-16 15:56:01 +0000329 InitializeMaxConstantsTable();
330}
331
332bool
333UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
334 const Instruction* I) const
335{
336 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
337 return true;
338
339 if (isa<ConstantPointerNull>(CV)) // can always use %g0
340 return false;
341
342 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000343 /* Large unsigned longs may really just be small negative signed longs */
344 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000345
346 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000347 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000348
349 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000350 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000351
352 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000353}
354
Vikram S. Advee76af292002-03-18 03:09:15 +0000355//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000356// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000357// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000358// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000359// The generated instructions are returned in `mvec'.
360// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000361// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000362//
363void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000364UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
365 Function* F,
366 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000367 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000368 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000369 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000370{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000371 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000372 "I only know about constant values and global addresses");
373
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000374 // Use a "set" instruction for known constants or symbolic constants (labels)
375 // that can go in an integer reg.
376 // We have to use a "load" instruction for all other constants,
377 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000378 //
379 const Type* valType = val->getType();
380
Vikram S. Adve893cace2002-10-13 00:04:26 +0000381 // Unfortunate special case: a ConstantPointerRef is just a
382 // reference to GlobalValue.
383 if (isa<ConstantPointerRef>(val))
384 val = cast<ConstantPointerRef>(val)->getValue();
385
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000386 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000387 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000388 TmpInstruction* tmpReg =
389 new TmpInstruction(PointerType::get(val->getType()), val);
390 mcfi.addTemp(tmpReg);
391 CreateSETXLabel(target, val, tmpReg, dest, mvec);
392 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000393 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000394 {
395 bool isValidConstant;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000396 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
397 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000398
399 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000400 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000401 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
402 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000403
404 if (opSize > destSize ||
405 (val->getType()->isSigned()
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000406 && destSize < target.getTargetData().getIntegerRegize()))
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000407 { // operand is larger than dest,
408 // OR both are equal but smaller than the full register size
409 // AND operand is signed, so it may have extra sign bits:
410 // mask high bits
411 C = C & ((1U << 8*destSize) - 1);
412 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000413 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000414 }
415 else
416 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000417 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
418 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000419
420 if (opSize > destSize)
421 // operand is larger than dest: mask high bits
422 C = C & ((1U << 8*destSize) - 1);
423
424 if (opSize > destSize ||
425 (opSize == destSize && !val->getType()->isSigned()))
426 // sign-extend from destSize to 64 bits
427 C = ((C & (1U << (8*destSize - 1)))
428 ? C | ~((1U << 8*destSize) - 1)
429 : C);
430
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000431 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000432 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000433 }
434 else
435 {
436 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000437 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000438 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000439
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000440 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000441 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000442 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000443 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000444
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000445 // Create another TmpInstruction for the address register
446 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000447 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000448 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000449
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000450 // Put the address (a symbolic name) into a register
451 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000452
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000453 // Generate the load instruction
454 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000455 unsigned Opcode = ChooseLoadInstruction(val->getType());
456 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
457 addSImm(zeroOffset).addReg(dest, MOTy::Def));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000458
459 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000460 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000461 }
462}
463
464
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000465// Create an instruction sequence to copy an integer register `val'
466// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000467// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000468// The generated instructions are returned in `mvec'.
469// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000470// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000471//
472void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000473UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
474 Function* F,
475 Value* val,
476 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000477 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000478 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000479{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000480 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
481 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000482 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000483 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000484
485 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000486 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000487
488 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000489 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000490
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000491 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000492 // The store and load opCodes are based on the size of the source value.
493 // If the value is smaller than 32 bits, we must sign- or zero-extend it
494 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000495 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000496 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
497 Value* storeVal = val;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000498 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy))
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000499 { // sign- or zero-extend respectively
500 storeVal = new TmpInstruction(storeType, val);
501 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000502 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000503 mvec, mcfi);
504 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000505 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000506 mvec, mcfi);
507 }
508 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType));
509 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal);
Vikram S. Advee76af292002-03-18 03:09:15 +0000510 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000511 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
512 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000513
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000514 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000515 // The type of the load opCode is the floating point type that matches the
516 // stored type in size:
517 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000518 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000519 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
520 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000521 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
522 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
523 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000524 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000525}
526
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000527// Similarly, create an instruction sequence to copy an FP register
528// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000529// The generated instructions are returned in `mvec'.
530// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000531// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000532//
533void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000534UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
535 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000536 Value* val,
537 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000538 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000539 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000540{
Vikram S. Advec190c012002-07-31 21:13:31 +0000541 const Type* opTy = val->getType();
542 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000543
Vikram S. Advec190c012002-07-31 21:13:31 +0000544 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000545 assert((destTy->isIntegral() || isa<PointerType>(destTy))
546 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000547
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000548 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000549
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000550 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000551 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000552 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000553 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000554 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
555 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
556 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000557 mvec.push_back(store);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000558
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000559 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000560 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000561 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000562 // On SparcV9: int for float, long for double.
563 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000564 // ensure correct sign-extension for UByte, UShort or UInt:
565 //
566 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Vikram S. Advec190c012002-07-31 21:13:31 +0000567 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000568 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000569 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000570 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000571 mvec.push_back(load);
572}
573
574
575// Create instruction(s) to copy src to dest, for arbitrary types
576// The generated instructions are returned in `mvec'.
577// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000578// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000579//
580void
581UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
582 Function *F,
583 Value* src,
584 Instruction* dest,
585 vector<MachineInstr*>& mvec,
586 MachineCodeForInstruction& mcfi) const
587{
588 bool loadConstantToReg = false;
589
590 const Type* resultType = dest->getType();
591
592 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
593 if (opCode == INVALID_OPCODE)
594 {
595 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
596 return;
597 }
598
599 // if `src' is a constant that doesn't fit in the immed field or if it is
600 // a global variable (i.e., a constant address), generate a load
601 // instruction instead of an add
602 //
603 if (isa<Constant>(src))
604 {
605 unsigned int machineRegNum;
606 int64_t immedValue;
607 MachineOperand::MachineOperandType opType =
608 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
609 machineRegNum, immedValue);
610
611 if (opType == MachineOperand::MO_VirtualRegister)
612 loadConstantToReg = true;
613 }
614 else if (isa<GlobalValue>(src))
615 loadConstantToReg = true;
616
617 if (loadConstantToReg)
618 { // `src' is constant and cannot fit in immed field for the ADD
619 // Insert instructions to "load" the constant into a register
620 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
621 mvec, mcfi);
622 }
623 else
624 { // Create an add-with-0 instruction of the appropriate type.
625 // Make `src' the second operand, in case it is a constant
626 // Use (unsigned long) 0 for a NULL pointer value.
627 //
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000628 const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType;
629 MachineInstr* MI =
630 BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty))
631 .addReg(src).addReg(dest, MOTy::Def);
632 mvec.push_back(MI);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000633 }
634}
635
636
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000637// Helper function for sign-extension and zero-extension.
638// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
639inline void
640CreateBitExtensionInstructions(bool signExtend,
641 const TargetMachine& target,
642 Function* F,
643 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000644 Value* destVal,
645 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000646 vector<MachineInstr*>& mvec,
647 MachineCodeForInstruction& mcfi)
648{
649 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000650
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000651 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
652
653 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000654 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000655 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
656 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000657 mcfi.addTemp(tmpI);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000658 mvec.push_back(BuildMI(SLLX, 3).addReg(srcVal).addZImm(32-numLowBits)
659 .addReg(tmpI, MOTy::Def));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000660 srcVal = tmpI;
661 }
662
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000663 mvec.push_back(BuildMI(signExtend? SRA : SRL, 3).addReg(srcVal)
664 .addZImm(32-numLowBits).addReg(destVal, MOTy::Def));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000665}
666
667
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000669// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000670// The generated instructions are returned in `mvec'.
671// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000672// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000673//
674void
675UltraSparcInstrInfo::CreateSignExtensionInstructions(
676 const TargetMachine& target,
677 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000678 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000679 Value* destVal,
680 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000681 vector<MachineInstr*>& mvec,
682 MachineCodeForInstruction& mcfi) const
683{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000684 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000685 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000686}
687
688
689// Create instruction sequence to produce a zero-extended register value
690// from an arbitrary-sized integer value (sized in bits, not bytes).
691// For SPARC v9, we sign-extend the given operand using SLL; SRL.
692// The generated instructions are returned in `mvec'.
693// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000694// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000695//
696void
697UltraSparcInstrInfo::CreateZeroExtensionInstructions(
698 const TargetMachine& target,
699 Function* F,
700 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000701 Value* destVal,
702 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000703 vector<MachineInstr*>& mvec,
704 MachineCodeForInstruction& mcfi) const
705{
706 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000707 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000708}