blob: e00c871545c1a1df02e9e80cec018d3bab6f5e01 [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"
18#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000019#include "llvm/Method.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000020#include "llvm/ConstantVals.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000021#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000022#include "llvm/Type.h"
23
24
25//************************ Internal Functions ******************************/
26
27
28static inline MachineInstr*
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000029CreateIntSetInstruction(int64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000030 std::vector<TmpInstruction*>& tempVec)
Vikram S. Adve30764b82001-10-18 00:01:48 +000031{
32 MachineInstr* minstr;
Vikram S. Advea2a70942001-10-28 21:41:46 +000033 uint64_t absC = (C >= 0)? C : -C;
34 if (absC > (unsigned int) ~0)
35 { // C does not fit in 32 bits
36 TmpInstruction* tmpReg =
Vikram S. Adveb9c38632001-11-08 04:57:53 +000037 new TmpInstruction(Instruction::UserOp1, Type::IntTy, NULL, NULL);
Vikram S. Advea2a70942001-10-28 21:41:46 +000038 tempVec.push_back(tmpReg);
39
40 minstr = new MachineInstr(SETX);
41 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
42 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
43 /*isdef*/ true);
44 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
45 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000046 else
Vikram S. Adve30764b82001-10-18 00:01:48 +000047 {
48 minstr = new MachineInstr(SETSW);
49 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
Vikram S. Advea2a70942001-10-28 21:41:46 +000050 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000051 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000052
53 return minstr;
54}
55
56static inline MachineInstr*
57CreateUIntSetInstruction(uint64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000058 std::vector<TmpInstruction*>& tempVec)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000059{
60 MachineInstr* minstr;
61 if (C > (unsigned int) ~0)
62 { // C does not fit in 32 bits
63 TmpInstruction* tmpReg =
64 new TmpInstruction(Instruction::UserOp1, Type::IntTy, NULL, NULL);
65 tempVec.push_back(tmpReg);
66
67 minstr = new MachineInstr(SETX);
68 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
69 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
70 /*isdef*/ true);
71 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
72 }
Vikram S. Adve30764b82001-10-18 00:01:48 +000073 else
74 {
75 minstr = new MachineInstr(SETUW);
76 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
Vikram S. Advea2a70942001-10-28 21:41:46 +000077 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000078 }
79
Vikram S. Adve30764b82001-10-18 00:01:48 +000080 return minstr;
81}
82
83//************************* External Classes *******************************/
84
85//---------------------------------------------------------------------------
86// class UltraSparcInstrInfo
87//
88// Purpose:
89// Information about individual instructions.
90// Most information is stored in the SparcMachineInstrDesc array above.
91// Other information is computed on demand, and most such functions
92// default to member functions in base class MachineInstrInfo.
93//---------------------------------------------------------------------------
94
95/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +000096UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
97 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +000098 /*descSize = */ NUM_TOTAL_OPCODES,
99 /*numRealOpCodes = */ NUM_REAL_OPCODES)
100{
101}
102
103
104// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000105// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000106// GlobalValue, viz., the constant address of a global variable or function.
107// The generated instructions are returned in `minstrVec'.
108// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
109//
110void
111UltraSparcInstrInfo::CreateCodeToLoadConst(Value* val,
Chris Lattner697954c2002-01-20 22:54:45 +0000112 Instruction* dest,
113 std::vector<MachineInstr*>& minstrVec,
114 std::vector<TmpInstruction*>& tempVec) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000115{
116 MachineInstr* minstr;
117
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000118 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000119 "I only know about constant values and global addresses");
120
121 // Use a "set" instruction for known constants that can go in an integer reg.
122 // Use a "load" instruction for all other constants, in particular,
123 // floating point constants and addresses of globals.
124 //
125 const Type* valType = val->getType();
126
127 if (valType->isIntegral() || valType == Type::BoolTy)
128 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000129 if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
130 {
131 uint64_t C = uval->getValue();
132 minstr = CreateUIntSetInstruction(C, dest, tempVec);
133 }
134 else
135 {
136 bool isValidConstant;
137 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
138 assert(isValidConstant && "Unrecognized constant");
139 minstr = CreateIntSetInstruction(C, dest, tempVec);
140 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000141 minstrVec.push_back(minstr);
142 }
143 else
144 {
145 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000146 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000147 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Advea2a70942001-10-28 21:41:46 +0000148 // Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
Vikram S. Adve30764b82001-10-18 00:01:48 +0000149 // itself a constant address. Otherwise, both are needed.
150
151 Value* addrVal;
152 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
153
Vikram S. Advea2a70942001-10-28 21:41:46 +0000154 TmpInstruction* tmpReg =
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000155 new TmpInstruction(Instruction::UserOp1,
156 PointerType::get(val->getType()), val, NULL);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000157 tempVec.push_back(tmpReg);
158
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000159 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000160 {
161 // Create another TmpInstruction for the hidden integer register
162 TmpInstruction* addrReg =
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000163 new TmpInstruction(Instruction::UserOp1,
164 PointerType::get(val->getType()), val, NULL);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000165 tempVec.push_back(addrReg);
166 addrVal = addrReg;
167 }
168 else
169 addrVal = dest;
170
Vikram S. Advea2a70942001-10-28 21:41:46 +0000171 minstr = new MachineInstr(SETX);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000172 minstr->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp, val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000173 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
174 /*isdef*/ true);
175 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,addrVal);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000176 minstrVec.push_back(minstr);
177
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000178 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000179 {
180 // addrVal->addMachineInstruction(minstr);
181
182 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
183 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
184 addrVal);
185 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
186 zeroOffset);
187 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
188 dest);
189 minstrVec.push_back(minstr);
190 }
191 }
192}
193
194
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000195// Create an instruction sequence to copy an integer value `val'
196// to a floating point value `dest' by copying to memory and back.
197// val must be an integral type. dest must be a Float or Double.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000198// The generated instructions are returned in `minstrVec'.
199// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
200//
201void
202UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000203 Value* val,
204 Instruction* dest,
205 std::vector<MachineInstr*>& minstrVec,
206 std::vector<TmpInstruction*>& tempVec,
207 TargetMachine& target) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000208{
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000209 assert((val->getType()->isIntegral() || val->getType()->isPointerType())
210 && "Source type must be integral");
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000211 assert((dest->getType() ==Type::FloatTy || dest->getType() ==Type::DoubleTy)
212 && "Dest type must be float/double");
213
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000214 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
215 int offset = mcinfo.allocateLocalVar(target, val);
216
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000217 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000218 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000219 // they use integer and float types that accomodate the
220 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000221 // On SparcV9: int for float, long for double.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000222 //
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000223 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
224 : Type::LongTy;
225 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
226 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
227 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
228 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
229 minstrVec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000230
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000231 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000232 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000233 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000234 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
235 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
236 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
237 minstrVec.push_back(load);
238}
239
240
241// Similarly, create an instruction sequence to copy an FP value
242// `val' to an integer value `dest' by copying to memory and back.
243// See the previous function for information about return values.
244//
245void
246UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000247 Value* val,
248 Instruction* dest,
249 std::vector<MachineInstr*>& minstrVec,
250 std::vector<TmpInstruction*>& tempVec,
251 TargetMachine& target) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000252{
253 assert((val->getType() ==Type::FloatTy || val->getType() ==Type::DoubleTy)
254 && "Source type must be float/double");
255 assert((dest->getType()->isIntegral() || dest->getType()->isPointerType())
256 && "Dest type must be integral");
257
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000258 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
259 int offset = mcinfo.allocateLocalVar(target, val);
260
261 // Store instruction stores `val' to [%fp+offset].
262 // The store and load opCodes are based on the value being copied, and
263 // they use the integer type that matches the source type in size:
264 // On SparcV9: int for float, long for double.
265 //
266 Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
267 : Type::LongTy;
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000268 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000269 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
270 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
271 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
272 minstrVec.push_back(store);
273
274 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000275 //
276 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
277 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
278 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
279 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
280 minstrVec.push_back(load);
281}