blob: fd18841dc18daa1e7c9391339c8c20a25866643b [file] [log] [blame]
Vikram S. Adve30764b82001-10-18 00:01:48 +00001// $Id$
2//***************************************************************************
3// File:
4// SparcInstrInfo.cpp
5//
6// Purpose:
7//
8// History:
9// 10/15/01 - Vikram Adve - Created
10//**************************************************************************/
11
12
13#include "SparcInternals.h"
14#include "SparcInstrSelectionSupport.h"
15#include "llvm/Target/Sparc.h"
16#include "llvm/CodeGen/InstrSelection.h"
17#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnercb0a1202002-02-03 07:49:49 +000018#include "llvm/CodeGen/MachineCodeForMethod.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000020#include "llvm/Constants.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000021#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000022
23
24//************************ Internal Functions ******************************/
25
26
27static inline MachineInstr*
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000028CreateIntSetInstruction(int64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000029 std::vector<TmpInstruction*>& tempVec)
Vikram S. Adve30764b82001-10-18 00:01:48 +000030{
31 MachineInstr* minstr;
Vikram S. Advea2a70942001-10-28 21:41:46 +000032 uint64_t absC = (C >= 0)? C : -C;
33 if (absC > (unsigned int) ~0)
34 { // C does not fit in 32 bits
Chris Lattnercb0a1202002-02-03 07:49:49 +000035 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Advea2a70942001-10-28 21:41:46 +000036 tempVec.push_back(tmpReg);
37
38 minstr = new MachineInstr(SETX);
Vikram S. Advee76af292002-03-18 03:09:15 +000039 minstr->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
40 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpReg,
Vikram S. Advea2a70942001-10-28 21:41:46 +000041 /*isdef*/ true);
Vikram S. Advee76af292002-03-18 03:09:15 +000042 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Advea2a70942001-10-28 21:41:46 +000043 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000044 else
Vikram S. Adve30764b82001-10-18 00:01:48 +000045 {
46 minstr = new MachineInstr(SETSW);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000047 minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
48 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000049 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000050
51 return minstr;
52}
53
54static inline MachineInstr*
55CreateUIntSetInstruction(uint64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::vector<TmpInstruction*>& tempVec)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000057{
58 MachineInstr* minstr;
59 if (C > (unsigned int) ~0)
60 { // C does not fit in 32 bits
Vikram S. Advef7cedec2002-03-31 00:13:12 +000061 assert(dest->getType() == Type::ULongTy && "Sign extension problems");
Chris Lattnercb0a1202002-02-03 07:49:49 +000062 TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000063 tempVec.push_back(tmpReg);
64
65 minstr = new MachineInstr(SETX);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000066 minstr->SetMachineOperandConst(0,MachineOperand::MO_SignExtendedImmed,C);
67 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
68 tmpReg, /*isdef*/ true);
Vikram S. Advee76af292002-03-18 03:09:15 +000069 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000070 }
Vikram S. Advef7cedec2002-03-31 00:13:12 +000071 else if (dest->getType() == Type::ULongTy)
Vikram S. Adve30764b82001-10-18 00:01:48 +000072 {
73 minstr = new MachineInstr(SETUW);
Vikram S. Advee76af292002-03-18 03:09:15 +000074 minstr->SetMachineOperandConst(0, MachineOperand::MO_UnextendedImmed, C);
Vikram S. Advef7cedec2002-03-31 00:13:12 +000075 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
76 }
77 else
78 { // cast to signed type of the right length and use signed op (SETSW)
79 // to get correct sign extension
80 //
81 minstr = new MachineInstr(SETSW);
82 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,dest);
83
84 switch (dest->getType()->getPrimitiveID())
85 {
86 case Type::UIntTyID:
87 minstr->SetMachineOperandConst(0,
88 MachineOperand::MO_SignExtendedImmed,
89 (int) C);
90 break;
91 case Type::UShortTyID:
92 minstr->SetMachineOperandConst(0,
93 MachineOperand::MO_SignExtendedImmed,
94 (short) C);
95 break;
96 case Type::UByteTyID:
97 minstr->SetMachineOperandConst(0,
98 MachineOperand::MO_SignExtendedImmed,
99 (char) C);
100 break;
101 default:
102 assert(0 && "Unexpected unsigned type");
103 break;
104 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000105 }
106
Vikram S. Adve30764b82001-10-18 00:01:48 +0000107 return minstr;
108}
109
110//************************* External Classes *******************************/
111
112//---------------------------------------------------------------------------
113// class UltraSparcInstrInfo
114//
115// Purpose:
116// Information about individual instructions.
117// Most information is stored in the SparcMachineInstrDesc array above.
118// Other information is computed on demand, and most such functions
119// default to member functions in base class MachineInstrInfo.
120//---------------------------------------------------------------------------
121
122/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000123UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
124 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000125 /*descSize = */ NUM_TOTAL_OPCODES,
126 /*numRealOpCodes = */ NUM_REAL_OPCODES)
127{
128}
129
Vikram S. Advee76af292002-03-18 03:09:15 +0000130//
Vikram S. Adve30764b82001-10-18 00:01:48 +0000131// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000132// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000133// GlobalValue, viz., the constant address of a global variable or function.
134// The generated instructions are returned in `minstrVec'.
135// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
136//
137void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000138UltraSparcInstrInfo::CreateCodeToLoadConst(Function *F, Value* val,
Vikram S. Advee76af292002-03-18 03:09:15 +0000139 Instruction* dest,
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000140 std::vector<MachineInstr*>&minstrVec,
141 std::vector<TmpInstruction*>& tempVec) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000142{
143 MachineInstr* minstr;
144
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000145 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000146 "I only know about constant values and global addresses");
147
148 // Use a "set" instruction for known constants that can go in an integer reg.
149 // Use a "load" instruction for all other constants, in particular,
150 // floating point constants and addresses of globals.
151 //
152 const Type* valType = val->getType();
153
154 if (valType->isIntegral() || valType == Type::BoolTy)
155 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000156 if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
157 {
158 uint64_t C = uval->getValue();
159 minstr = CreateUIntSetInstruction(C, dest, tempVec);
160 }
161 else
162 {
163 bool isValidConstant;
164 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
165 assert(isValidConstant && "Unrecognized constant");
166 minstr = CreateIntSetInstruction(C, dest, tempVec);
167 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000168 minstrVec.push_back(minstr);
169 }
170 else
171 {
172 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000173 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000174 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Advea2a70942001-10-28 21:41:46 +0000175 // Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
Vikram S. Adve30764b82001-10-18 00:01:48 +0000176 // itself a constant address. Otherwise, both are needed.
177
178 Value* addrVal;
179 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
180
Vikram S. Advea2a70942001-10-28 21:41:46 +0000181 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000182 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000183 tempVec.push_back(tmpReg);
184
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000185 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000186 {
187 // Create another TmpInstruction for the hidden integer register
188 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000189 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000190 tempVec.push_back(addrReg);
191 addrVal = addrReg;
192 }
193 else
194 addrVal = dest;
195
Vikram S. Advea2a70942001-10-28 21:41:46 +0000196 minstr = new MachineInstr(SETX);
Vikram S. Advee76af292002-03-18 03:09:15 +0000197 minstr->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp, val);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000198 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,tmpReg,
Vikram S. Advea2a70942001-10-28 21:41:46 +0000199 /*isdef*/ true);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000200 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
201 addrVal);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000202 minstrVec.push_back(minstr);
203
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000204 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000205 {
Vikram S. Advee76af292002-03-18 03:09:15 +0000206 // Make sure constant is emitted to constant pool in assembly code.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000207 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
Vikram S. Advee76af292002-03-18 03:09:15 +0000208 mcinfo.addToConstantPool(cast<Constant>(val));
209
210 // Generate the load instruction
Vikram S. Adve30764b82001-10-18 00:01:48 +0000211 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000212 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000213 addrVal);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000214 minstr->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000215 zeroOffset);
Vikram S. Advee76af292002-03-18 03:09:15 +0000216 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve30764b82001-10-18 00:01:48 +0000217 dest);
218 minstrVec.push_back(minstr);
219 }
220 }
221}
222
223
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000224// Create an instruction sequence to copy an integer value `val'
225// to a floating point value `dest' by copying to memory and back.
226// val must be an integral type. dest must be a Float or Double.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000227// The generated instructions are returned in `minstrVec'.
228// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
229//
230void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000231UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Function *F,
Chris Lattner697954c2002-01-20 22:54:45 +0000232 Value* val,
233 Instruction* dest,
234 std::vector<MachineInstr*>& minstrVec,
235 std::vector<TmpInstruction*>& tempVec,
236 TargetMachine& target) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000237{
Chris Lattner9b625032002-05-06 16:15:30 +0000238 assert((val->getType()->isIntegral() || isa<PointerType>(val->getType()))
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000239 && "Source type must be integral");
Chris Lattner9b625032002-05-06 16:15:30 +0000240 assert(dest->getType()->isFloatingPoint()
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000241 && "Dest type must be float/double");
242
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000243 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000244 int offset = mcinfo.allocateLocalVar(target, val);
245
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000246 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000247 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000248 // they use integer and float types that accomodate the
249 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000250 // On SparcV9: int for float, long for double.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000251 //
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000252 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
253 : Type::LongTy;
254 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000255 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
256 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000257 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed, offset);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000258 minstrVec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000259
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000260 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000261 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000262 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000263 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
264 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset);
265 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000266 minstrVec.push_back(load);
267}
268
269
270// Similarly, create an instruction sequence to copy an FP value
271// `val' to an integer value `dest' by copying to memory and back.
272// See the previous function for information about return values.
273//
274void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000275UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Function *F,
Chris Lattner697954c2002-01-20 22:54:45 +0000276 Value* val,
277 Instruction* dest,
278 std::vector<MachineInstr*>& minstrVec,
279 std::vector<TmpInstruction*>& tempVec,
280 TargetMachine& target) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000281{
Chris Lattner9b625032002-05-06 16:15:30 +0000282 assert(val->getType()->isFloatingPoint()
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000283 && "Source type must be float/double");
Chris Lattner9b625032002-05-06 16:15:30 +0000284 assert((dest->getType()->isIntegral() || isa<PointerType>(dest->getType()))
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000285 && "Dest type must be integral");
286
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000287 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(F);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000288 int offset = mcinfo.allocateLocalVar(target, val);
289
290 // Store instruction stores `val' to [%fp+offset].
291 // The store and load opCodes are based on the value being copied, and
292 // they use the integer type that matches the source type in size:
293 // On SparcV9: int for float, long for double.
294 //
295 Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
296 : Type::LongTy;
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000297 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Advee76af292002-03-18 03:09:15 +0000298 store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val);
299 store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer());
300 store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset);
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000301 minstrVec.push_back(store);
302
303 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000304 //
305 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
Vikram S. Advee76af292002-03-18 03:09:15 +0000306 load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
307 load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, offset);
308 load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000309 minstrVec.push_back(load);
310}