blob: 289e871c4599f4dbdef3409049c1893f13a6b4a2 [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);
Chris Lattner54e898e2003-01-15 19:23:34 +000073 miOR = BuildMI(OR, 3).addMReg(target.getRegInfo().getZeroRegNum())
74 .addSImm(sC).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000075 }
76 mvec.push_back(miOR);
77 }
78
79 assert((miSETHI || miOR) && "Oops, no code was generated!");
80}
81
Vikram S. Adve53fd4002002-07-10 21:39:50 +000082
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000083//----------------------------------------------------------------------------
84// Function: CreateSETSWConst
85//
86// Set a 32-bit signed constant in the register `dest', with sign-extension
87// to 64 bits. This uses SETHI, OR, SRA in the worst case.
88// This function correctly emulates the SETSW pseudo-op for SPARC v9.
89//
90// Optimize the same cases as SETUWConst, plus:
91// (1) SRA is not needed for positive or small negative values.
92//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +000093
Vikram S. Adve53fd4002002-07-10 21:39:50 +000094static inline void
95CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000096 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000097{
Vikram S. Adve53fd4002002-07-10 21:39:50 +000098 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000099 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
100
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000101 // Sign-extend to the high 32 bits if needed
102 if (C < 0 && (-C) > (int32_t) MAXSIMM)
Chris Lattner00dca912003-01-15 17:47:49 +0000103 mvec.push_back(BuildMI(SRA, 3).addReg(dest).addZImm(0).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000104}
105
106
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000107//----------------------------------------------------------------------------
108// Function: CreateSETXConst
109//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000110// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000111// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
112// This function correctly emulates the SETX pseudo-op for SPARC v9.
113//
114// Optimize the same cases as SETUWConst for each 32 bit word.
115//----------------------------------------------------------------------------
116
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000117static inline void
118CreateSETXConst(const TargetMachine& target, uint64_t C,
119 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000120 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000121{
122 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
123
124 MachineInstr* MI;
125
126 // Code to set the upper 32 bits of the value in register `tmpReg'
127 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
128
129 // Shift tmpReg left by 32 bits
Chris Lattner00dca912003-01-15 17:47:49 +0000130 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000131
132 // Code to set the low 32 bits of the value in register `dest'
133 CreateSETUWConst(target, C, dest, mvec);
134
135 // dest = OR(tmpReg, dest)
Chris Lattner00dca912003-01-15 17:47:49 +0000136 mvec.push_back(BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000137}
138
139
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000140//----------------------------------------------------------------------------
141// Function: CreateSETUWLabel
142//
143// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
144//----------------------------------------------------------------------------
145
146static inline void
147CreateSETUWLabel(const TargetMachine& target, Value* val,
148 Instruction* dest, vector<MachineInstr*>& mvec)
149{
150 MachineInstr* MI;
151
152 // Set the high 22 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000153 MI = BuildMI(SETHI, 2).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000154 MI->setOperandHi32(0);
155 mvec.push_back(MI);
156
157 // Set the low 10 bits in dest
Chris Lattner00dca912003-01-15 17:47:49 +0000158 MI = BuildMI(OR, 3).addReg(dest).addReg(val).addRegDef(dest);
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000159 MI->setOperandLo32(1);
160 mvec.push_back(MI);
161}
162
163
164//----------------------------------------------------------------------------
165// Function: CreateSETXLabel
166//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000167// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000168//----------------------------------------------------------------------------
169
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000170static inline void
171CreateSETXLabel(const TargetMachine& target,
172 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000173 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000174{
175 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
176 "I only know about constant values and global addresses");
177
178 MachineInstr* MI;
179
Chris Lattner54e898e2003-01-15 19:23:34 +0000180 MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000181 MI->setOperandHi64(0);
182 mvec.push_back(MI);
183
Chris Lattner00dca912003-01-15 17:47:49 +0000184 MI = BuildMI(OR, 3).addReg(tmpReg).addPCDisp(val).addRegDef(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000185 MI->setOperandLo64(1);
186 mvec.push_back(MI);
187
Chris Lattner00dca912003-01-15 17:47:49 +0000188 mvec.push_back(BuildMI(SLLX, 3).addReg(tmpReg).addZImm(32).addRegDef(tmpReg));
189 MI = BuildMI(SETHI, 2).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000190 MI->setOperandHi32(0);
191 mvec.push_back(MI);
192
Chris Lattner00dca912003-01-15 17:47:49 +0000193 MI = BuildMI(OR, 3).addReg(dest).addReg(tmpReg).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000194 mvec.push_back(MI);
195
Chris Lattner00dca912003-01-15 17:47:49 +0000196 MI = BuildMI(OR, 3).addReg(dest).addPCDisp(val).addRegDef(dest);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000197 MI->setOperandLo32(1);
198 mvec.push_back(MI);
199}
200
Vikram S. Adve30764b82001-10-18 00:01:48 +0000201
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000202//----------------------------------------------------------------------------
203// Function: CreateUIntSetInstruction
204//
205// Create code to Set an unsigned constant in the register `dest'.
206// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
207// CreateSETSWConst is an optimization for the case that the unsigned value
208// has all ones in the 33 high bits (so that sign-extension sets them all).
209//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000210
Vikram S. Adve242a8082002-05-19 15:25:51 +0000211static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000212CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000213 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000214 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000215 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000216{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000217 static const uint64_t lo32 = (uint32_t) ~0;
218 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
219 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
220 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
221 { // All high 33 (not 32) bits are 1s: sign-extension will take care
222 // of high 32 bits, so use the sequence for signed int
223 CreateSETSWConst(target, (int32_t) C, dest, mvec);
224 }
225 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000226 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000227 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000228 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000229 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000230 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000231}
232
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000233
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000234//----------------------------------------------------------------------------
235// Function: CreateIntSetInstruction
236//
237// Create code to Set a signed constant in the register `dest'.
238// Really the same as CreateUIntSetInstruction.
239//----------------------------------------------------------------------------
240
241static inline void
242CreateIntSetInstruction(const TargetMachine& target,
243 int64_t C, Instruction* dest,
244 std::vector<MachineInstr*>& mvec,
245 MachineCodeForInstruction& mcfi)
246{
247 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
248}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000249
Vikram S. Adve30764b82001-10-18 00:01:48 +0000250
251//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000252// Create a table of LLVM opcode -> max. immediate constant likely to
253// be usable for that operation.
254//---------------------------------------------------------------------------
255
256// Entry == 0 ==> no immediate constant field exists at all.
257// Entry > 0 ==> abs(immediate constant) <= Entry
258//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000259vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000260
261static int
262MaxConstantForInstr(unsigned llvmOpCode)
263{
264 int modelOpCode = -1;
265
Chris Lattner0b16ae22002-10-13 19:39:16 +0000266 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
267 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000268 modelOpCode = ADD;
269 else
270 switch(llvmOpCode) {
271 case Instruction::Ret: modelOpCode = JMPLCALL; break;
272
273 case Instruction::Malloc:
274 case Instruction::Alloca:
275 case Instruction::GetElementPtr:
276 case Instruction::PHINode:
277 case Instruction::Cast:
278 case Instruction::Call: modelOpCode = ADD; break;
279
280 case Instruction::Shl:
281 case Instruction::Shr: modelOpCode = SLLX; break;
282
283 default: break;
284 };
285
286 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
287}
288
289static void
290InitializeMaxConstantsTable()
291{
292 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000293 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000294 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000295 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000296 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000297 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000298 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000299 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000300 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000301 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000302 MaxConstantsTable[op] = MaxConstantForInstr(op);
303}
304
305
306//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000307// class UltraSparcInstrInfo
308//
309// Purpose:
310// Information about individual instructions.
311// Most information is stored in the SparcMachineInstrDesc array above.
312// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000313// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000314//---------------------------------------------------------------------------
315
316/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000317UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000318 : TargetInstrInfo(SparcMachineInstrDesc,
319 /*descSize = */ NUM_TOTAL_OPCODES,
320 /*numRealOpCodes = */ NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000321{
Vikram S. Adve49001162002-09-16 15:56:01 +0000322 InitializeMaxConstantsTable();
323}
324
325bool
326UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
327 const Instruction* I) const
328{
329 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
330 return true;
331
332 if (isa<ConstantPointerNull>(CV)) // can always use %g0
333 return false;
334
335 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000336 /* Large unsigned longs may really just be small negative signed longs */
337 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000338
339 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000340 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000341
342 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000343 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000344
345 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000346}
347
Vikram S. Advee76af292002-03-18 03:09:15 +0000348//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000349// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000350// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000351// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000352// The generated instructions are returned in `mvec'.
353// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000354// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000355//
356void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000357UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
358 Function* F,
359 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000360 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000361 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000362 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000363{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000364 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000365 "I only know about constant values and global addresses");
366
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000367 // Use a "set" instruction for known constants or symbolic constants (labels)
368 // that can go in an integer reg.
369 // We have to use a "load" instruction for all other constants,
370 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000371 //
372 const Type* valType = val->getType();
373
Vikram S. Adve893cace2002-10-13 00:04:26 +0000374 // Unfortunate special case: a ConstantPointerRef is just a
375 // reference to GlobalValue.
376 if (isa<ConstantPointerRef>(val))
377 val = cast<ConstantPointerRef>(val)->getValue();
378
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000379 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000380 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000381 TmpInstruction* tmpReg =
382 new TmpInstruction(PointerType::get(val->getType()), val);
383 mcfi.addTemp(tmpReg);
384 CreateSETXLabel(target, val, tmpReg, dest, mvec);
385 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000386 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000387 {
388 bool isValidConstant;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000389 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
390 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000391
392 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000393 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000394 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
395 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000396
397 if (opSize > destSize ||
398 (val->getType()->isSigned()
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000399 && destSize < target.getTargetData().getIntegerRegize()))
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000400 { // operand is larger than dest,
401 // OR both are equal but smaller than the full register size
402 // AND operand is signed, so it may have extra sign bits:
403 // mask high bits
404 C = C & ((1U << 8*destSize) - 1);
405 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000406 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000407 }
408 else
409 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000410 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
411 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000412
413 if (opSize > destSize)
414 // operand is larger than dest: mask high bits
415 C = C & ((1U << 8*destSize) - 1);
416
417 if (opSize > destSize ||
418 (opSize == destSize && !val->getType()->isSigned()))
419 // sign-extend from destSize to 64 bits
420 C = ((C & (1U << (8*destSize - 1)))
421 ? C | ~((1U << 8*destSize) - 1)
422 : C);
423
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000424 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000425 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000426 }
427 else
428 {
429 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000430 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000431 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000432
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000433 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000434 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000435 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000436 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000437
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000438 // Create another TmpInstruction for the address register
439 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000440 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000441 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000442
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000443 // Put the address (a symbolic name) into a register
444 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000445
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000446 // Generate the load instruction
447 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000448 unsigned Opcode = ChooseLoadInstruction(val->getType());
449 mvec.push_back(BuildMI(Opcode, 3).addReg(addrReg).
Chris Lattner00dca912003-01-15 17:47:49 +0000450 addSImm(zeroOffset).addRegDef(dest));
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000451
452 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000453 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000454 }
455}
456
457
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000458// Create an instruction sequence to copy an integer register `val'
459// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000460// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000461// The generated instructions are returned in `mvec'.
462// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000463// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000464//
465void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000466UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
467 Function* F,
468 Value* val,
469 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000470 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000471 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000472{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000473 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
474 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000475 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000476 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000477
478 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000479 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000480
481 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000482 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000483
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000484 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000485 // The store and load opCodes are based on the size of the source value.
486 // If the value is smaller than 32 bits, we must sign- or zero-extend it
487 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000488 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000489 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
490 Value* storeVal = val;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000491 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy))
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000492 { // sign- or zero-extend respectively
493 storeVal = new TmpInstruction(storeType, val);
494 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000495 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000496 mvec, mcfi);
497 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000498 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000499 mvec, mcfi);
500 }
Chris Lattner54e898e2003-01-15 19:23:34 +0000501
502 unsigned FPReg = target.getRegInfo().getFramePointer();
503 mvec.push_back(BuildMI(ChooseStoreInstruction(storeType), 3)
504 .addReg(storeVal).addMReg(FPReg).addSImm(offset));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000505
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000506 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000507 // The type of the load opCode is the floating point type that matches the
508 // stored type in size:
509 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000510 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000511 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000512 mvec.push_back(BuildMI(ChooseLoadInstruction(loadType), 3)
513 .addMReg(FPReg).addSImm(offset).addRegDef(dest));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000514}
515
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000516// Similarly, create an instruction sequence to copy an FP register
517// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000518// The generated instructions are returned in `mvec'.
519// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000520// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000521//
522void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000523UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
524 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000525 Value* val,
526 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000527 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000528 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000529{
Vikram S. Advec190c012002-07-31 21:13:31 +0000530 const Type* opTy = val->getType();
531 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000532
Vikram S. Advec190c012002-07-31 21:13:31 +0000533 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000534 assert((destTy->isIntegral() || isa<PointerType>(destTy))
535 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000536
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000537 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000538
Chris Lattner54e898e2003-01-15 19:23:34 +0000539 unsigned FPReg = target.getRegInfo().getFramePointer();
540
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000541 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000542 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000543 //
Chris Lattner54e898e2003-01-15 19:23:34 +0000544 mvec.push_back(BuildMI(ChooseStoreInstruction(opTy), 3)
545 .addReg(val).addMReg(FPReg).addSImm(offset));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000546
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000547 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000548 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000549 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000550 // On SparcV9: int for float, long for double.
551 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000552 // ensure correct sign-extension for UByte, UShort or UInt:
553 //
554 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Chris Lattner54e898e2003-01-15 19:23:34 +0000555 mvec.push_back(BuildMI(ChooseLoadInstruction(loadTy), 3).addMReg(FPReg)
556 .addSImm(offset).addRegDef(dest));
Vikram S. Adve242a8082002-05-19 15:25:51 +0000557}
558
559
560// Create instruction(s) to copy src to dest, for arbitrary types
561// The generated instructions are returned in `mvec'.
562// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000563// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000564//
565void
566UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
567 Function *F,
568 Value* src,
569 Instruction* dest,
570 vector<MachineInstr*>& mvec,
571 MachineCodeForInstruction& mcfi) const
572{
573 bool loadConstantToReg = false;
574
575 const Type* resultType = dest->getType();
576
577 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
578 if (opCode == INVALID_OPCODE)
579 {
580 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
581 return;
582 }
583
584 // if `src' is a constant that doesn't fit in the immed field or if it is
585 // a global variable (i.e., a constant address), generate a load
586 // instruction instead of an add
587 //
588 if (isa<Constant>(src))
589 {
590 unsigned int machineRegNum;
591 int64_t immedValue;
592 MachineOperand::MachineOperandType opType =
593 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
594 machineRegNum, immedValue);
595
596 if (opType == MachineOperand::MO_VirtualRegister)
597 loadConstantToReg = true;
598 }
599 else if (isa<GlobalValue>(src))
600 loadConstantToReg = true;
601
602 if (loadConstantToReg)
603 { // `src' is constant and cannot fit in immed field for the ADD
604 // Insert instructions to "load" the constant into a register
605 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
606 mvec, mcfi);
607 }
608 else
609 { // Create an add-with-0 instruction of the appropriate type.
610 // Make `src' the second operand, in case it is a constant
611 // Use (unsigned long) 0 for a NULL pointer value.
612 //
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000613 const Type* Ty =isa<PointerType>(resultType) ? Type::ULongTy : resultType;
614 MachineInstr* MI =
615 BuildMI(opCode, 3).addReg(Constant::getNullValue(Ty))
Chris Lattner00dca912003-01-15 17:47:49 +0000616 .addReg(src).addRegDef(dest);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000617 mvec.push_back(MI);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000618 }
619}
620
621
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000622// Helper function for sign-extension and zero-extension.
623// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
624inline void
625CreateBitExtensionInstructions(bool signExtend,
626 const TargetMachine& target,
627 Function* F,
628 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000629 Value* destVal,
630 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000631 vector<MachineInstr*>& mvec,
632 MachineCodeForInstruction& mcfi)
633{
634 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000635
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000636 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
637
638 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000639 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000640 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
641 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000642 mcfi.addTemp(tmpI);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000643 mvec.push_back(BuildMI(SLLX, 3).addReg(srcVal).addZImm(32-numLowBits)
Chris Lattner00dca912003-01-15 17:47:49 +0000644 .addRegDef(tmpI));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000645 srcVal = tmpI;
646 }
647
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000648 mvec.push_back(BuildMI(signExtend? SRA : SRL, 3).addReg(srcVal)
Chris Lattner00dca912003-01-15 17:47:49 +0000649 .addZImm(32-numLowBits).addRegDef(destVal));
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000650}
651
652
Vikram S. Adve242a8082002-05-19 15:25:51 +0000653// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000654// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000655// The generated instructions are returned in `mvec'.
656// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000657// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658//
659void
660UltraSparcInstrInfo::CreateSignExtensionInstructions(
661 const TargetMachine& target,
662 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000663 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000664 Value* destVal,
665 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000666 vector<MachineInstr*>& mvec,
667 MachineCodeForInstruction& mcfi) const
668{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000669 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000670 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000671}
672
673
674// Create instruction sequence to produce a zero-extended register value
675// from an arbitrary-sized integer value (sized in bits, not bytes).
676// For SPARC v9, we sign-extend the given operand using SLL; SRL.
677// The generated instructions are returned in `mvec'.
678// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000679// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000680//
681void
682UltraSparcInstrInfo::CreateZeroExtensionInstructions(
683 const TargetMachine& target,
684 Function* F,
685 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000686 Value* destVal,
687 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000688 vector<MachineInstr*>& mvec,
689 MachineCodeForInstruction& mcfi) const
690{
691 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000692 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000693}