blob: df4bbc3ecad5284fd4bfc836ccd2609a092be93e [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 Lattner2fbfdcf2002-04-07 20:49:59 +000012#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000013#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000014#include "llvm/DerivedTypes.h"
Vikram S. Adve49001162002-09-16 15:56:01 +000015#include <stdlib.h>
Anand Shuklacfb22d32002-06-25 20:55:50 +000016using std::vector;
Vikram S. Adve30764b82001-10-18 00:01:48 +000017
Vikram S. Adve53fd4002002-07-10 21:39:50 +000018static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*)
19static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR
20
21
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000022//----------------------------------------------------------------------------
23// Function: CreateSETUWConst
Vikram S. Adve53fd4002002-07-10 21:39:50 +000024//
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000025// Set a 32-bit unsigned constant in the register `dest', using
26// SETHI, OR in the worst case. This function correctly emulates
27// the SETUW pseudo-op for SPARC v9 (if argument isSigned == false).
28//
29// The isSigned=true case is used to implement SETSW without duplicating code.
30//
31// Optimize some common cases:
32// (1) Small value that fits in simm13 field of OR: don't need SETHI.
33// (2) isSigned = true and C is a small negative signed value, i.e.,
34// high bits are 1, and the remaining bits fit in simm13(OR).
35//----------------------------------------------------------------------------
36
Vikram S. Adve53fd4002002-07-10 21:39:50 +000037static inline void
38CreateSETUWConst(const TargetMachine& target, uint32_t C,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000039 Instruction* dest, vector<MachineInstr*>& mvec,
40 bool isSigned = false)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000041{
42 MachineInstr *miSETHI = NULL, *miOR = NULL;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000043
Vikram S. Adve53fd4002002-07-10 21:39:50 +000044 // In order to get efficient code, we should not generate the SETHI if
45 // all high bits are 1 (i.e., this is a small signed value that fits in
46 // the simm13 field of OR). So we check for and handle that case specially.
47 // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0.
48 // In fact, sC == -sC, so we have to check for this explicitly.
49 int32_t sC = (int32_t) C;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000050 bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM;
51
Vikram S. Adve53fd4002002-07-10 21:39:50 +000052 // 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 +000053 if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000054 {
55 miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest);
56 miSETHI->setOperandHi32(0);
57 mvec.push_back(miSETHI);
58 }
59
60 // Set the low 10 or 12 bits in dest. This is necessary if no SETHI
61 // was generated, or if the low 10 bits are non-zero.
62 if (miSETHI==NULL || C & MAXLO)
63 {
64 if (miSETHI)
65 { // unsigned value with high-order bits set using SETHI
66 miOR = Create3OperandInstr_UImmed(OR, dest, C, dest);
67 miOR->setOperandLo32(1);
68 }
69 else
70 { // unsigned or small signed value that fits in simm13 field of OR
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000071 assert(smallNegValue || (C & ~MAXSIMM) == 0);
Vikram S. Adve53fd4002002-07-10 21:39:50 +000072 miOR = new MachineInstr(OR);
73 miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
74 miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
75 sC);
76 miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest);
77 }
78 mvec.push_back(miOR);
79 }
80
81 assert((miSETHI || miOR) && "Oops, no code was generated!");
82}
83
Vikram S. Adve53fd4002002-07-10 21:39:50 +000084
Vikram S. Adve6c0c3012002-08-13 18:04:08 +000085//----------------------------------------------------------------------------
86// Function: CreateSETSWConst
87//
88// Set a 32-bit signed constant in the register `dest', with sign-extension
89// to 64 bits. This uses SETHI, OR, SRA in the worst case.
90// This function correctly emulates the SETSW pseudo-op for SPARC v9.
91//
92// Optimize the same cases as SETUWConst, plus:
93// (1) SRA is not needed for positive or small negative values.
94//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +000095
Vikram S. Adve53fd4002002-07-10 21:39:50 +000096static inline void
97CreateSETSWConst(const TargetMachine& target, int32_t C,
Chris Lattner035dfbe2002-08-09 20:08:06 +000098 Instruction* dest, vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +000099{
100 MachineInstr* MI;
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000101
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000102 // Set the low 32 bits of dest
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000103 CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true);
104
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000105 // Sign-extend to the high 32 bits if needed
106 if (C < 0 && (-C) > (int32_t) MAXSIMM)
107 {
108 MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest);
109 mvec.push_back(MI);
110 }
111}
112
113
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000114//----------------------------------------------------------------------------
115// Function: CreateSETXConst
116//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000117// Set a 64-bit signed or unsigned constant in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000118// Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between.
119// This function correctly emulates the SETX pseudo-op for SPARC v9.
120//
121// Optimize the same cases as SETUWConst for each 32 bit word.
122//----------------------------------------------------------------------------
123
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000124static inline void
125CreateSETXConst(const TargetMachine& target, uint64_t C,
126 Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000127 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000128{
129 assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!");
130
131 MachineInstr* MI;
132
133 // Code to set the upper 32 bits of the value in register `tmpReg'
134 CreateSETUWConst(target, (C >> 32), tmpReg, mvec);
135
136 // Shift tmpReg left by 32 bits
137 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
138 mvec.push_back(MI);
139
140 // Code to set the low 32 bits of the value in register `dest'
141 CreateSETUWConst(target, C, dest, mvec);
142
143 // dest = OR(tmpReg, dest)
144 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
145 mvec.push_back(MI);
146}
147
148
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000149//----------------------------------------------------------------------------
150// Function: CreateSETUWLabel
151//
152// Set a 32-bit constant (given by a symbolic label) in the register `dest'.
153//----------------------------------------------------------------------------
154
155static inline void
156CreateSETUWLabel(const TargetMachine& target, Value* val,
157 Instruction* dest, vector<MachineInstr*>& mvec)
158{
159 MachineInstr* MI;
160
161 // Set the high 22 bits in dest
162 MI = Create2OperandInstr(SETHI, val, dest);
163 MI->setOperandHi32(0);
164 mvec.push_back(MI);
165
166 // Set the low 10 bits in dest
167 MI = Create3OperandInstr(OR, dest, val, dest);
168 MI->setOperandLo32(1);
169 mvec.push_back(MI);
170}
171
172
173//----------------------------------------------------------------------------
174// Function: CreateSETXLabel
175//
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000176// Set a 64-bit constant (given by a symbolic label) in the register `dest'.
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000177//----------------------------------------------------------------------------
178
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000179static inline void
180CreateSETXLabel(const TargetMachine& target,
181 Value* val, Instruction* tmpReg, Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000182 vector<MachineInstr*>& mvec)
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000183{
184 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
185 "I only know about constant values and global addresses");
186
187 MachineInstr* MI;
188
189 MI = Create2OperandInstr_Addr(SETHI, val, tmpReg);
190 MI->setOperandHi64(0);
191 mvec.push_back(MI);
192
193 MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg);
194 MI->setOperandLo64(1);
195 mvec.push_back(MI);
196
197 MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg);
198 mvec.push_back(MI);
199
200 MI = Create2OperandInstr_Addr(SETHI, val, dest);
201 MI->setOperandHi32(0);
202 mvec.push_back(MI);
203
204 MI = Create3OperandInstr(OR, dest, tmpReg, dest);
205 mvec.push_back(MI);
206
207 MI = Create3OperandInstr_Addr(OR, dest, val, dest);
208 MI->setOperandLo32(1);
209 mvec.push_back(MI);
210}
211
Vikram S. Adve30764b82001-10-18 00:01:48 +0000212
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000213//----------------------------------------------------------------------------
214// Function: CreateUIntSetInstruction
215//
216// Create code to Set an unsigned constant in the register `dest'.
217// Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed.
218// CreateSETSWConst is an optimization for the case that the unsigned value
219// has all ones in the 33 high bits (so that sign-extension sets them all).
220//----------------------------------------------------------------------------
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000221
Vikram S. Adve242a8082002-05-19 15:25:51 +0000222static inline void
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000223CreateUIntSetInstruction(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000224 uint64_t C, Instruction* dest,
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000225 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000226 MachineCodeForInstruction& mcfi)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000227{
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000228 static const uint64_t lo32 = (uint32_t) ~0;
229 if (C <= lo32) // High 32 bits are 0. Set low 32 bits.
230 CreateSETUWConst(target, (uint32_t) C, dest, mvec);
231 else if ((C & ~lo32) == ~lo32 && (C & (1 << 31)))
232 { // All high 33 (not 32) bits are 1s: sign-extension will take care
233 // of high 32 bits, so use the sequence for signed int
234 CreateSETSWConst(target, (int32_t) C, dest, mvec);
235 }
236 else if (C > lo32)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000237 { // C does not fit in 32 bits
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000238 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000239 mcfi.addTemp(tmpReg);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000240 CreateSETXConst(target, C, tmpReg, dest, mvec);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000241 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000242}
243
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000244
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000245//----------------------------------------------------------------------------
246// Function: CreateIntSetInstruction
247//
248// Create code to Set a signed constant in the register `dest'.
249// Really the same as CreateUIntSetInstruction.
250//----------------------------------------------------------------------------
251
252static inline void
253CreateIntSetInstruction(const TargetMachine& target,
254 int64_t C, Instruction* dest,
255 std::vector<MachineInstr*>& mvec,
256 MachineCodeForInstruction& mcfi)
257{
258 CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi);
259}
Chris Lattner035dfbe2002-08-09 20:08:06 +0000260
Vikram S. Adve30764b82001-10-18 00:01:48 +0000261
262//---------------------------------------------------------------------------
Vikram S. Adve49001162002-09-16 15:56:01 +0000263// Create a table of LLVM opcode -> max. immediate constant likely to
264// be usable for that operation.
265//---------------------------------------------------------------------------
266
267// Entry == 0 ==> no immediate constant field exists at all.
268// Entry > 0 ==> abs(immediate constant) <= Entry
269//
Chris Lattner0b16ae22002-10-13 19:39:16 +0000270vector<int> MaxConstantsTable(Instruction::OtherOpsEnd);
Vikram S. Adve49001162002-09-16 15:56:01 +0000271
272static int
273MaxConstantForInstr(unsigned llvmOpCode)
274{
275 int modelOpCode = -1;
276
Chris Lattner0b16ae22002-10-13 19:39:16 +0000277 if (llvmOpCode >= Instruction::BinaryOpsBegin &&
278 llvmOpCode < Instruction::BinaryOpsEnd)
Vikram S. Adve49001162002-09-16 15:56:01 +0000279 modelOpCode = ADD;
280 else
281 switch(llvmOpCode) {
282 case Instruction::Ret: modelOpCode = JMPLCALL; break;
283
284 case Instruction::Malloc:
285 case Instruction::Alloca:
286 case Instruction::GetElementPtr:
287 case Instruction::PHINode:
288 case Instruction::Cast:
289 case Instruction::Call: modelOpCode = ADD; break;
290
291 case Instruction::Shl:
292 case Instruction::Shr: modelOpCode = SLLX; break;
293
294 default: break;
295 };
296
297 return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst;
298}
299
300static void
301InitializeMaxConstantsTable()
302{
303 unsigned op;
Chris Lattner0b16ae22002-10-13 19:39:16 +0000304 assert(MaxConstantsTable.size() == Instruction::OtherOpsEnd &&
Vikram S. Adve49001162002-09-16 15:56:01 +0000305 "assignments below will be illegal!");
Chris Lattner0b16ae22002-10-13 19:39:16 +0000306 for (op = Instruction::TermOpsBegin; op < Instruction::TermOpsEnd; ++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::BinaryOpsBegin; op < Instruction::BinaryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000309 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000310 for (op = Instruction::MemoryOpsBegin; op < Instruction::MemoryOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000311 MaxConstantsTable[op] = MaxConstantForInstr(op);
Chris Lattner0b16ae22002-10-13 19:39:16 +0000312 for (op = Instruction::OtherOpsBegin; op < Instruction::OtherOpsEnd; ++op)
Vikram S. Adve49001162002-09-16 15:56:01 +0000313 MaxConstantsTable[op] = MaxConstantForInstr(op);
314}
315
316
317//---------------------------------------------------------------------------
Vikram S. Adve30764b82001-10-18 00:01:48 +0000318// class UltraSparcInstrInfo
319//
320// Purpose:
321// Information about individual instructions.
322// Most information is stored in the SparcMachineInstrDesc array above.
323// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +0000324// default to member functions in base class TargetInstrInfo.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000325//---------------------------------------------------------------------------
326
327/*ctor*/
Chris Lattner047bbaf2002-10-29 15:45:20 +0000328UltraSparcInstrInfo::UltraSparcInstrInfo()
Chris Lattner3501fea2003-01-14 22:00:31 +0000329 : TargetInstrInfo(SparcMachineInstrDesc,
330 /*descSize = */ NUM_TOTAL_OPCODES,
331 /*numRealOpCodes = */ NUM_REAL_OPCODES)
Vikram S. Adve30764b82001-10-18 00:01:48 +0000332{
Vikram S. Adve49001162002-09-16 15:56:01 +0000333 InitializeMaxConstantsTable();
334}
335
336bool
337UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV,
338 const Instruction* I) const
339{
340 if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!)
341 return true;
342
343 if (isa<ConstantPointerNull>(CV)) // can always use %g0
344 return false;
345
346 if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000347 /* Large unsigned longs may really just be small negative signed longs */
348 return (labs((int64_t) U->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000349
350 if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000351 return (labs(S->getValue()) > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000352
353 if (isa<ConstantBool>(CV))
Vikram S. Adve893cace2002-10-13 00:04:26 +0000354 return (1 > MaxConstantsTable[I->getOpcode()]);
Vikram S. Adve49001162002-09-16 15:56:01 +0000355
356 return true;
Vikram S. Adve30764b82001-10-18 00:01:48 +0000357}
358
Vikram S. Advee76af292002-03-18 03:09:15 +0000359//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000360// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000361// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000362// GlobalValue, viz., the constant address of a global variable or function.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000363// The generated instructions are returned in `mvec'.
364// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000365// Any stack space required is allocated via MachineFunction.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000366//
367void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000368UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target,
369 Function* F,
370 Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000371 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000372 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000373 MachineCodeForInstruction& mcfi) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000374{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000375 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000376 "I only know about constant values and global addresses");
377
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000378 // Use a "set" instruction for known constants or symbolic constants (labels)
379 // that can go in an integer reg.
380 // We have to use a "load" instruction for all other constants,
381 // in particular, floating point constants.
Vikram S. Adve30764b82001-10-18 00:01:48 +0000382 //
383 const Type* valType = val->getType();
384
Vikram S. Adve893cace2002-10-13 00:04:26 +0000385 // Unfortunate special case: a ConstantPointerRef is just a
386 // reference to GlobalValue.
387 if (isa<ConstantPointerRef>(val))
388 val = cast<ConstantPointerRef>(val)->getValue();
389
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000390 if (isa<GlobalValue>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000391 {
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000392 TmpInstruction* tmpReg =
393 new TmpInstruction(PointerType::get(val->getType()), val);
394 mcfi.addTemp(tmpReg);
395 CreateSETXLabel(target, val, tmpReg, dest, mvec);
396 }
Chris Lattner0c4e8862002-09-03 01:08:28 +0000397 else if (valType->isIntegral())
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000398 {
399 bool isValidConstant;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000400 unsigned opSize = target.getTargetData().getTypeSize(val->getType());
401 unsigned destSize = target.getTargetData().getTypeSize(dest->getType());
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000402
403 if (! dest->getType()->isSigned())
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000404 {
Vikram S. Advea40cbb32002-08-04 20:55:37 +0000405 uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant);
406 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000407
408 if (opSize > destSize ||
409 (val->getType()->isSigned()
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000410 && destSize < target.getTargetData().getIntegerRegize()))
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000411 { // operand is larger than dest,
412 // OR both are equal but smaller than the full register size
413 // AND operand is signed, so it may have extra sign bits:
414 // mask high bits
415 C = C & ((1U << 8*destSize) - 1);
416 }
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000417 CreateUIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000418 }
419 else
420 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000421 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
422 assert(isValidConstant && "Unrecognized constant");
Vikram S. Adve6c0c3012002-08-13 18:04:08 +0000423
424 if (opSize > destSize)
425 // operand is larger than dest: mask high bits
426 C = C & ((1U << 8*destSize) - 1);
427
428 if (opSize > destSize ||
429 (opSize == destSize && !val->getType()->isSigned()))
430 // sign-extend from destSize to 64 bits
431 C = ((C & (1U << (8*destSize - 1)))
432 ? C | ~((1U << 8*destSize) - 1)
433 : C);
434
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000435 CreateIntSetInstruction(target, C, dest, mvec, mcfi);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000436 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000437 }
438 else
439 {
440 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000441 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000442 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Adve30764b82001-10-18 00:01:48 +0000443
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000444 // First, create a tmp register to be used by the SETX sequence.
Vikram S. Advea2a70942001-10-28 21:41:46 +0000445 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000446 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000447 mcfi.addTemp(tmpReg);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000448
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000449 // Create another TmpInstruction for the address register
450 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000451 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000452 mcfi.addTemp(addrReg);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000453
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000454 // Put the address (a symbolic name) into a register
455 CreateSETXLabel(target, val, tmpReg, addrReg, mvec);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000456
Vikram S. Adve53fd4002002-07-10 21:39:50 +0000457 // Generate the load instruction
458 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
459 MachineInstr* MI =
460 Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()),
461 addrReg, zeroOffset, dest);
462 mvec.push_back(MI);
463
464 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000465 MachineFunction::get(F).getInfo()->addToConstantPool(cast<Constant>(val));
Vikram S. Adve30764b82001-10-18 00:01:48 +0000466 }
467}
468
469
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000470// Create an instruction sequence to copy an integer register `val'
471// to a floating point register `dest' by copying to memory and back.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000472// val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000473// The generated instructions are returned in `mvec'.
474// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000475// Any stack space required is allocated via MachineFunction.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000476//
477void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000478UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target,
479 Function* F,
480 Value* val,
481 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000482 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000483 MachineCodeForInstruction& mcfi) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000484{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000485 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
486 && "Source type must be integral (integer or bool) or pointer");
Chris Lattner9b625032002-05-06 16:15:30 +0000487 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000488 && "Dest type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000489
490 // Get a stack slot to use for the copy
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000491 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000492
493 // Get the size of the source value being copied.
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000494 size_t srcSize = target.getTargetData().getTypeSize(val->getType());
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000495
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000496 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000497 // The store and load opCodes are based on the size of the source value.
498 // If the value is smaller than 32 bits, we must sign- or zero-extend it
499 // to 32 bits since the load-float will load 32 bits.
Vikram S. Advec190c012002-07-31 21:13:31 +0000500 // Note that the store instruction is the same for signed and unsigned ints.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000501 const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy;
502 Value* storeVal = val;
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000503 if (srcSize < target.getTargetData().getTypeSize(Type::FloatTy))
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000504 { // sign- or zero-extend respectively
505 storeVal = new TmpInstruction(storeType, val);
506 if (val->getType()->isSigned())
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000507 CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000508 mvec, mcfi);
509 else
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000510 CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000511 mvec, mcfi);
512 }
513 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType));
514 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal);
Vikram S. Advee76af292002-03-18 03:09:15 +0000515 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000516 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
517 mvec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000518
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000519 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000520 // The type of the load opCode is the floating point type that matches the
521 // stored type in size:
522 // On SparcV9: float for int or smaller, double for long.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000523 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000524 const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
525 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000526 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
527 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
528 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000529 mvec.push_back(load);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000530}
531
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000532// Similarly, create an instruction sequence to copy an FP register
533// `val' to an integer register `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000534// The generated instructions are returned in `mvec'.
535// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000536// Any stack space required is allocated via MachineFunction.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000537//
538void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000539UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target,
540 Function* F,
Chris Lattner697954c2002-01-20 22:54:45 +0000541 Value* val,
542 Instruction* dest,
Chris Lattner035dfbe2002-08-09 20:08:06 +0000543 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000544 MachineCodeForInstruction& mcfi) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000545{
Vikram S. Advec190c012002-07-31 21:13:31 +0000546 const Type* opTy = val->getType();
547 const Type* destTy = dest->getType();
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000548
Vikram S. Advec190c012002-07-31 21:13:31 +0000549 assert(opTy->isFloatingPoint() && "Source type must be float/double");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000550 assert((destTy->isIntegral() || isa<PointerType>(destTy))
551 && "Dest type must be integer, bool or pointer");
Vikram S. Advec190c012002-07-31 21:13:31 +0000552
Chris Lattner2ef9a6a2002-12-28 20:18:21 +0000553 int offset = MachineFunction::get(F).getInfo()->allocateLocalVar(val);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000554
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000555 // Store instruction stores `val' to [%fp+offset].
Vikram S. Advec190c012002-07-31 21:13:31 +0000556 // The store opCode is based only the source value being copied.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000557 //
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000558 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000559 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
560 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
561 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000562 mvec.push_back(store);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000563
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000564 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Advec190c012002-07-31 21:13:31 +0000565 // The type of the load opCode is the integer type that matches the
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000566 // source type in size:
Vikram S. Advec190c012002-07-31 21:13:31 +0000567 // On SparcV9: int for float, long for double.
568 // Note that we *must* use signed loads even for unsigned dest types, to
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000569 // ensure correct sign-extension for UByte, UShort or UInt:
570 //
571 const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy;
Vikram S. Advec190c012002-07-31 21:13:31 +0000572 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy));
Vikram S. Advee76af292002-03-18 03:09:15 +0000573 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000574 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000575 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000576 mvec.push_back(load);
577}
578
579
580// Create instruction(s) to copy src to dest, for arbitrary types
581// The generated instructions are returned in `mvec'.
582// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000583// Any stack space required is allocated via MachineFunction.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000584//
585void
586UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target,
587 Function *F,
588 Value* src,
589 Instruction* dest,
590 vector<MachineInstr*>& mvec,
591 MachineCodeForInstruction& mcfi) const
592{
593 bool loadConstantToReg = false;
594
595 const Type* resultType = dest->getType();
596
597 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
598 if (opCode == INVALID_OPCODE)
599 {
600 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
601 return;
602 }
603
604 // if `src' is a constant that doesn't fit in the immed field or if it is
605 // a global variable (i.e., a constant address), generate a load
606 // instruction instead of an add
607 //
608 if (isa<Constant>(src))
609 {
610 unsigned int machineRegNum;
611 int64_t immedValue;
612 MachineOperand::MachineOperandType opType =
613 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
614 machineRegNum, immedValue);
615
616 if (opType == MachineOperand::MO_VirtualRegister)
617 loadConstantToReg = true;
618 }
619 else if (isa<GlobalValue>(src))
620 loadConstantToReg = true;
621
622 if (loadConstantToReg)
623 { // `src' is constant and cannot fit in immed field for the ADD
624 // Insert instructions to "load" the constant into a register
625 target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest,
626 mvec, mcfi);
627 }
628 else
629 { // Create an add-with-0 instruction of the appropriate type.
630 // Make `src' the second operand, in case it is a constant
631 // Use (unsigned long) 0 for a NULL pointer value.
632 //
633 const Type* zeroValueType =
634 isa<PointerType>(resultType) ? Type::ULongTy : resultType;
635 MachineInstr* minstr =
636 Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType),
637 src, dest);
638 mvec.push_back(minstr);
639 }
640}
641
642
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000643// Helper function for sign-extension and zero-extension.
644// For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL.
645inline void
646CreateBitExtensionInstructions(bool signExtend,
647 const TargetMachine& target,
648 Function* F,
649 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000650 Value* destVal,
651 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000652 vector<MachineInstr*>& mvec,
653 MachineCodeForInstruction& mcfi)
654{
655 MachineInstr* M;
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000656
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000657 assert(numLowBits <= 32 && "Otherwise, nothing should be done here!");
658
659 if (numLowBits < 32)
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000660 { // SLL is needed since operand size is < 32 bits.
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000661 TmpInstruction *tmpI = new TmpInstruction(destVal->getType(),
662 srcVal, destVal, "make32");
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000663 mcfi.addTemp(tmpI);
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000664 M = Create3OperandInstr_UImmed(SLLX, srcVal, 32-numLowBits, tmpI);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000665 mvec.push_back(M);
666 srcVal = tmpI;
667 }
668
669 M = Create3OperandInstr_UImmed(signExtend? SRA : SRL,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000670 srcVal, 32-numLowBits, destVal);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000671 mvec.push_back(M);
672}
673
674
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675// Create instruction sequence to produce a sign-extended register value
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000676// from an arbitrary-sized integer value (sized in bits, not bytes).
Vikram S. Adve242a8082002-05-19 15:25:51 +0000677// 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. Adve242a8082002-05-19 15:25:51 +0000680//
681void
682UltraSparcInstrInfo::CreateSignExtensionInstructions(
683 const TargetMachine& target,
684 Function* F,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000685 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000686 Value* destVal,
687 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000688 vector<MachineInstr*>& mvec,
689 MachineCodeForInstruction& mcfi) const
690{
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000691 CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000692 destVal, numLowBits, mvec, mcfi);
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000693}
694
695
696// Create instruction sequence to produce a zero-extended register value
697// from an arbitrary-sized integer value (sized in bits, not bytes).
698// For SPARC v9, we sign-extend the given operand using SLL; SRL.
699// The generated instructions are returned in `mvec'.
700// Any temp. registers (TmpInstruction) created are recorded in mcfi.
Misha Brukmanfce11432002-10-28 00:28:31 +0000701// Any stack space required is allocated via MachineFunction.
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000702//
703void
704UltraSparcInstrInfo::CreateZeroExtensionInstructions(
705 const TargetMachine& target,
706 Function* F,
707 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000708 Value* destVal,
709 unsigned int numLowBits,
Vikram S. Adve84c0fcb2002-09-05 18:33:59 +0000710 vector<MachineInstr*>& mvec,
711 MachineCodeForInstruction& mcfi) const
712{
713 CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000714 destVal, numLowBits, mvec, mcfi);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000715}