blob: 134bdacae07ce6faf68a3ad4dbb2c5605cfb0651 [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"
Vikram S. Adve242a8082002-05-19 15:25:51 +000010#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000011#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000012#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000013#include "llvm/DerivedTypes.h"
Vikram S. Adve49001162002-09-16 15:56:01 +000014#include <stdlib.h>
Anand Shuklacfb22d32002-06-25 20:55:50 +000015using std::vector;
Vikram S. Adve30764b82001-10-18 00:01:48 +000016
Vikram S. Adve53fd4002002-07-10 21:39:50 +000017static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
18static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
19
20
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000021//----------------------------------------------------------------------------
22// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +000023//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000024// Set a 32-bit unsigned constant in the register `dest', using
25// SETHI, OR in the worst case. This function correctly emulates
26// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
27//
28// The isSigned=true case is used to implement SETSW without duplicating code.
29//
30// Optimize some common cases:
31// (1) Small value that fits in simm13 field of OR: don't need SETHI.
32// (2) isSigned = true and C is a small negative signed value, i.e.,
33// high bits are 1, and the remaining bits fit in simm13(OR).
34//----------------------------------------------------------------------------
35
Vikram S. Adve53fd4002002-07-10 21:39:50 +000036static inline void
37CreateSETUWConst(const TargetMachine& target, uint32_t C,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000038 Instruction* dest, vector<MachineInstr*>& mvec,
39 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000040{
41 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000042
Vikram S. Adve53fd4002002-07-10 21:39:50 +000043 // In order to get efficient code, we should not generate the SETHI if
44 // all high bits are 1 (i.e., this is a small signed value that fits in
45 // the simm13 field of OR). So we check for and handle that case specially.
46 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
47 // In fact, sC == -sC, so we have to check for this explicitly.
48 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000049 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
50
Vikram S. Adve53fd4002002-07-10 21:39:50 +000051 // 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 +000052 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000053 {
54 miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest);
55 miSETHI->setOperandHi32(0);
56 mvec.push_back(miSETHI);
57 }
58
59 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
60 // was generated, or if the low 10 bits are non-zero.
61 if (miSETHI==NULL || C & MAXLO)
62 {
63 if (miSETHI)
64 { // unsigned value with high-order bits set using SETHI
65 miOR = Create3OperandInstr_UImmed(OR, dest, C, dest);
66 miOR->setOperandLo32(1);
67 }
68 else
69 { // unsigned or small signed value that fits in simm13 field of OR
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000070 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000071 miOR = new MachineInstr(OR);
72 miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
73 miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
74 sC);
75 miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest);
76 }
77 mvec.push_back(miOR);
78 }
79
80 assert((miSETHI || miOR) && "Oops, no code was generated!");
81}
82
Vikram S. Adve53fd4002002-07-10 21:39:50 +000083
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000084//----------------------------------------------------------------------------
85// Function: CreateSETSWConst
86//
87// Set a 32-bit signed constant in the register `dest', with sign-extension
88// to 64 bits. This uses SETHI, OR, SRA in the worst case.
89// This function correctly emulates the SETSW pseudo-op for SPARC v9.
90//
91// Optimize the same cases as SETUWConst, plus:
92// (1) SRA is not needed for positive or small negative values.
93//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +000094
Vikram S. Adve53fd4002002-07-10 21:39:50 +000095static inline void
96CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000097 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000098{
99 MachineInstr* MI;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +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)
106 {
107 MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest);
108 mvec.push_back(MI);
109 }
110}
111
112
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000113//----------------------------------------------------------------------------
114// Function: CreateSETXConst
115//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000116// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000117// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
118// This function correctly emulates the SETX pseudo-op for SPARC v9.
119//
120// Optimize the same cases as SETUWConst for each 32 bit word.
121//----------------------------------------------------------------------------
122
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000123static inline void
124CreateSETXConst(const TargetMachine& target, uint64_t C,
125 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000126 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000127{
128 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
129
130 MachineInstr* MI;
131
132 // Code to set the upper 32 bits of the value in register `tmpReg'
133 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
134
135 // Shift tmpReg left by 32 bits
136 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
137 mvec.push_back(MI);
138
139 // Code to set the low 32 bits of the value in register `dest'
140 CreateSETUWConst(target, C, dest, mvec);
141
142 // dest = OR(tmpReg, dest)
143 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
144 mvec.push_back(MI);
145}
146
147
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000148//----------------------------------------------------------------------------
149// Function: CreateSETUWLabel
150//
151// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
152//----------------------------------------------------------------------------
153
154static inline void
155CreateSETUWLabel(const TargetMachine& target, Value* val,
156 Instruction* dest, vector<MachineInstr*>& mvec)
157{
158 MachineInstr* MI;
159
160 // Set the high 22 bits in dest
161 MI = Create2OperandInstr(SETHI, val, dest);
162 MI->setOperandHi32(0);
163 mvec.push_back(MI);
164
165 // Set the low 10 bits in dest
166 MI = Create3OperandInstr(OR, dest, val, dest);
167 MI->setOperandLo32(1);
168 mvec.push_back(MI);
169}
170
171
172//----------------------------------------------------------------------------
173// Function: CreateSETXLabel
174//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000175// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000176//----------------------------------------------------------------------------
177
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000178static inline void
179CreateSETXLabel(const TargetMachine& target,
180 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000181 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000182{
183 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
184 "I only know about constant values and global addresses");
185
186 MachineInstr* MI;
187
188 MI = Create2OperandInstr_Addr(SETHI, val, tmpReg);
189 MI->setOperandHi64(0);
190 mvec.push_back(MI);
191
192 MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg);
193 MI->setOperandLo64(1);
194 mvec.push_back(MI);
195
196 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
197 mvec.push_back(MI);
198
199 MI = Create2OperandInstr_Addr(SETHI, val, dest);
200 MI->setOperandHi32(0);
201 mvec.push_back(MI);
202
203 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
204 mvec.push_back(MI);
205
206 MI = Create3OperandInstr_Addr(OR, dest, val, dest);
207 MI->setOperandLo32(1);
208 mvec.push_back(MI);
209}
210
Vikram S. Adve30764b82001-10-18 00:01:48 +0000211
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000212//----------------------------------------------------------------------------
213// Function: CreateUIntSetInstruction
214//
215// Create code to Set an unsigned constant in the register `dest'.
216// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
217// CreateSETSWConst is an optimization for the case that the unsigned value
218// has all ones in the 33 high bits (so that sign-extension sets them all).
219//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000220
Vikram S. Adve242a8082002-05-19 15:25:51 +0000221static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000222CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000223 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000224 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000225 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000226{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000227 static const uint64_t lo32 = (uint32_t) ~0;
228 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
229 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
230 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
231 { // All high 33 (not 32) bits are 1s: sign-extension will take care
232 // of high 32 bits, so use the sequence for signed int
233 CreateSETSWConst(target, (int32_t) C, dest, mvec);
234 }
235 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000236 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000237 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000238 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000239 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000240 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000241}
242
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000243
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000244//----------------------------------------------------------------------------
245// Function: CreateIntSetInstruction
246//
247// Create code to Set a signed constant in the register `dest'.
248// Really the same as CreateUIntSetInstruction.
249//----------------------------------------------------------------------------
250
251static inline void
252CreateIntSetInstruction(const TargetMachine& target,
253 int64_t C, Instruction* dest,
254 std::vector<MachineInstr*>& mvec,
255 MachineCodeForInstruction& mcfi)
256{
257 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
258}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000259
Vikram S. Adve30764b82001-10-18 00:01:48 +0000260
261//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000262// Create a table of LLVM opcode -> max. immediate constant likely to
263// be usable for that operation.
264//---------------------------------------------------------------------------
265
266// Entry == 0 ==> no immediate constant field exists at all.
267// Entry > 0 ==> abs(immediate constant) <= Entry
268//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000269vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000270
271static int
272MaxConstantForInstr(unsigned llvmOpCode)
273{
274 int modelOpCode = -1;
275
Chris Lattner0b16ae22002-10-13 19:39:16 +0000276 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
277 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000278 modelOpCode = ADD;
279 else
280 switch(llvmOpCode) {
281 case Instruction::Ret: modelOpCode = JMPLCALL; break;
282
283 case Instruction::Malloc:
284 case Instruction::Alloca:
285 case Instruction::GetElementPtr:
286 case Instruction::PHINode:
287 case Instruction::Cast:
288 case Instruction::Call: modelOpCode = ADD; break;
289
290 case Instruction::Shl:
291 case Instruction::Shr: modelOpCode = SLLX; break;
292
293 default: break;
294 };
295
296 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
297}
298
299static void
300InitializeMaxConstantsTable()
301{
302 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000303 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000304 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000305 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000306 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000307 for (op = Instruction::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000308 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000309 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000310 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000311 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000312 MaxConstantsTable[op] = MaxConstantForInstr(op);
313}
314
315
316//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000317// class UltraSparcInstrInfo
318//
319// Purpose:
320// Information about individual instructions.
321// Most information is stored in the SparcMachineInstrDesc array above.
322// Other information is computed on demand, and most such functions
323// default to member functions in base class MachineInstrInfo.
324//---------------------------------------------------------------------------
325
326/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000327UltraSparcInstrInfo::UltraSparcInstrInfo()
328 : MachineInstrInfo(SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000329 /*descSize = */ NUM_TOTAL_OPCODES,
330 /*numRealOpCodes = */ NUM_REAL_OPCODES)
331{
Vikram S. Adve49001162002-09-16 15:56:01 +0000332 InitializeMaxConstantsTable();
333}
334
335bool
336UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
337 const Instruction* I) const
338{
339 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
340 return true;
341
342 if (isa<ConstantPointerNull>(CV)) // can always use %g0
343 return false;
344
345 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000346 /* Large unsigned longs may really just be small negative signed longs */
347 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000348
349 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000350 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000351
352 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000353 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000354
355 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000356}
357
Vikram S. Advee76af292002-03-18 03:09:15 +0000358//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000359// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000360// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000361// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000362// The generated instructions are returned in `mvec'.
363// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000364// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000365//
366void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000367UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
368 Function* F,
369 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000370 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000371 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000372 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000373{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000374 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000375 "I only know about constant values and global addresses");
376
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000377 // Use a "set" instruction for known constants or symbolic constants (labels)
378 // that can go in an integer reg.
379 // We have to use a "load" instruction for all other constants,
380 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000381 //
382 const Type* valType = val->getType();
383
Vikram S. Adve893cace2002-10-13 00:04:26 +0000384 // Unfortunate special case: a ConstantPointerRef is just a
385 // reference to GlobalValue.
386 if (isa<ConstantPointerRef>(val))
387 val = cast<ConstantPointerRef>(val)->getValue();
388
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000389 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000390 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000391 TmpInstruction* tmpReg =
392 new TmpInstruction(PointerType::get(val->getType()), val);
393 mcfi.addTemp(tmpReg);
394 CreateSETXLabel(target, val, tmpReg, dest, mvec);
395 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000396 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000397 {
398 bool isValidConstant;
399 unsigned opSize = target.DataLayout.getTypeSize(val->getType());
400 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
401
402 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000403 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000404 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
405 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000406
407 if (opSize > destSize ||
408 (val->getType()->isSigned()
409 && destSize < target.DataLayout.getIntegerRegize()))
410 { // operand is larger than dest,
411 // OR both are equal but smaller than the full register size
412 // AND operand is signed, so it may have extra sign bits:
413 // mask high bits
414 C = C & ((1U << 8*destSize) - 1);
415 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000416 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000417 }
418 else
419 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000420 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
421 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000422
423 if (opSize > destSize)
424 // operand is larger than dest: mask high bits
425 C = C & ((1U << 8*destSize) - 1);
426
427 if (opSize > destSize ||
428 (opSize == destSize && !val->getType()->isSigned()))
429 // sign-extend from destSize to 64 bits
430 C = ((C & (1U << (8*destSize - 1)))
431 ? C | ~((1U << 8*destSize) - 1)
432 : C);
433
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000434 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000435 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000436 }
437 else
438 {
439 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000440 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000441 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000442
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000443 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000444 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000445 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000446 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000447
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000448 // Create another TmpInstruction for the address register
449 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000450 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000451 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000452
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000453 // Put the address (a symbolic name) into a register
454 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000455
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000456 // Generate the load instruction
457 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
458 MachineInstr* MI =
459 Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()),
460 addrReg, zeroOffset, dest);
461 mvec.push_back(MI);
462
463 // Make sure constant is emitted to constant pool in assembly code.
Misha Brukmanfce11432002-10-28 00:28:31 +0000464 MachineFunction::get(F).addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000465 }
466}
467
468
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000469// Create an instruction sequence to copy an integer register `val'
470// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000471// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000472// The generated instructions are returned in `mvec'.
473// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000474// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000475//
476void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000477UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
478 Function* F,
479 Value* val,
480 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000481 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000482 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000483{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000484 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
485 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000486 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000487 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000488
489 // Get a stack slot to use for the copy
Misha Brukmanfce11432002-10-28 00:28:31 +0000490 int offset = MachineFunction::get(F).allocateLocalVar(target, val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000491
492 // Get the size of the source value being copied.
493 size_t srcSize = target.DataLayout.getTypeSize(val->getType());
494
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000495 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000496 // The store and load opCodes are based on the size of the source value.
497 // If the value is smaller than 32 bits, we must sign- or zero-extend it
498 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000499 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000500 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
501 Value* storeVal = val;
502 if (srcSize < target.DataLayout.getTypeSize(Type::FloatTy))
503 { // sign- or zero-extend respectively
504 storeVal = new TmpInstruction(storeType, val);
505 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000506 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000507 mvec, mcfi);
508 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000509 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000510 mvec, mcfi);
511 }
512 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType));
513 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal);
Vikram S. Advee76af292002-03-18 03:09:15 +0000514 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000515 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
516 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000517
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000518 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000519 // The type of the load opCode is the floating point type that matches the
520 // stored type in size:
521 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000522 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000523 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
524 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000525 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
526 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
527 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000528 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000529}
530
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000531// Similarly, create an instruction sequence to copy an FP register
532// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000533// The generated instructions are returned in `mvec'.
534// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000535// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000536//
537void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000538UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
539 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000540 Value* val,
541 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000542 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000543 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000544{
Vikram S. Advec190c012002-07-31 21:13:31 +0000545 const Type* opTy = val->getType();
546 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000547
Vikram S. Advec190c012002-07-31 21:13:31 +0000548 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000549 assert((destTy->isIntegral() || isa<PointerType>(destTy))
550 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000551
Misha Brukmanfce11432002-10-28 00:28:31 +0000552 int offset = MachineFunction::get(F).allocateLocalVar(target, val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000553
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000554 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000555 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000556 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000557 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000558 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
559 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
560 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000561 mvec.push_back(store);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000562
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000563 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000564 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000565 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000566 // On SparcV9: int for float, long for double.
567 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000568 // ensure correct sign-extension for UByte, UShort or UInt:
569 //
570 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Vikram S. Advec190c012002-07-31 21:13:31 +0000571 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000572 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000573 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000574 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575 mvec.push_back(load);
576}
577
578
579// Create instruction(s) to copy src to dest, for arbitrary types
580// The generated instructions are returned in `mvec'.
581// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000582// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000583//
584void
585UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
586 Function *F,
587 Value* src,
588 Instruction* dest,
589 vector<MachineInstr*>& mvec,
590 MachineCodeForInstruction& mcfi) const
591{
592 bool loadConstantToReg = false;
593
594 const Type* resultType = dest->getType();
595
596 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
597 if (opCode == INVALID_OPCODE)
598 {
599 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
600 return;
601 }
602
603 // if `src' is a constant that doesn't fit in the immed field or if it is
604 // a global variable (i.e., a constant address), generate a load
605 // instruction instead of an add
606 //
607 if (isa<Constant>(src))
608 {
609 unsigned int machineRegNum;
610 int64_t immedValue;
611 MachineOperand::MachineOperandType opType =
612 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
613 machineRegNum, immedValue);
614
615 if (opType == MachineOperand::MO_VirtualRegister)
616 loadConstantToReg = true;
617 }
618 else if (isa<GlobalValue>(src))
619 loadConstantToReg = true;
620
621 if (loadConstantToReg)
622 { // `src' is constant and cannot fit in immed field for the ADD
623 // Insert instructions to "load" the constant into a register
624 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
625 mvec, mcfi);
626 }
627 else
628 { // Create an add-with-0 instruction of the appropriate type.
629 // Make `src' the second operand, in case it is a constant
630 // Use (unsigned long) 0 for a NULL pointer value.
631 //
632 const Type* zeroValueType =
633 isa<PointerType>(resultType) ? Type::ULongTy : resultType;
634 MachineInstr* minstr =
635 Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType),
636 src, dest);
637 mvec.push_back(minstr);
638 }
639}
640
641
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000642// Helper function for sign-extension and zero-extension.
643// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
644inline void
645CreateBitExtensionInstructions(bool signExtend,
646 const TargetMachine& target,
647 Function* F,
648 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000649 Value* destVal,
650 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000651 vector<MachineInstr*>& mvec,
652 MachineCodeForInstruction& mcfi)
653{
654 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000655
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000656 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
657
658 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000659 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000660 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
661 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000662 mcfi.addTemp(tmpI);
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000663 M = Create3OperandInstr_UImmed(SLLX, srcVal, 32-numLowBits, tmpI);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000664 mvec.push_back(M);
665 srcVal = tmpI;
666 }
667
668 M = Create3OperandInstr_UImmed(signExtend? SRA : SRL,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000669 srcVal, 32-numLowBits, destVal);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000670 mvec.push_back(M);
671}
672
673
Vikram S. Adve242a8082002-05-19 15:25:51 +0000674// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000675// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000676// The generated instructions are returned in `mvec'.
677// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000678// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000679//
680void
681UltraSparcInstrInfo::CreateSignExtensionInstructions(
682 const TargetMachine& target,
683 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000684 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000685 Value* destVal,
686 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687 vector<MachineInstr*>& mvec,
688 MachineCodeForInstruction& mcfi) const
689{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000690 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000691 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000692}
693
694
695// Create instruction sequence to produce a zero-extended register value
696// from an arbitrary-sized integer value (sized in bits, not bytes).
697// For SPARC v9, we sign-extend the given operand using SLL; SRL.
698// The generated instructions are returned in `mvec'.
699// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000700// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000701//
702void
703UltraSparcInstrInfo::CreateZeroExtensionInstructions(
704 const TargetMachine& target,
705 Function* F,
706 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000707 Value* destVal,
708 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000709 vector<MachineInstr*>& mvec,
710 MachineCodeForInstruction& mcfi) const
711{
712 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000713 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000714}