blob: cca9ac27134f06c09e973c72dbb3f43f8b6cdcd2 [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"
Chris Lattnercb0a1202002-02-03 07:49:49 +000019#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000020#include "llvm/Method.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000021#include "llvm/ConstantVals.h"
Vikram S. Adveb9c38632001-11-08 04:57:53 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adve30764b82001-10-18 00:01:48 +000023
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
Chris Lattnercb0a1202002-02-03 07:49:49 +000036 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
Vikram S. Advea2a70942001-10-28 21:41:46 +000037 tempVec.push_back(tmpReg);
38
39 minstr = new MachineInstr(SETX);
40 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
41 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
42 /*isdef*/ true);
43 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
44 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000045 else
Vikram S. Adve30764b82001-10-18 00:01:48 +000046 {
47 minstr = new MachineInstr(SETSW);
48 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
Vikram S. Advea2a70942001-10-28 21:41:46 +000049 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000050 }
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000051
52 return minstr;
53}
54
55static inline MachineInstr*
56CreateUIntSetInstruction(uint64_t C, Value* dest,
Chris Lattner697954c2002-01-20 22:54:45 +000057 std::vector<TmpInstruction*>& tempVec)
Vikram S. Advecee9d1c2001-12-15 00:33:36 +000058{
59 MachineInstr* minstr;
60 if (C > (unsigned int) ~0)
61 { // C does not fit in 32 bits
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);
66 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
67 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
68 /*isdef*/ true);
69 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
70 }
Vikram S. Adve30764b82001-10-18 00:01:48 +000071 else
72 {
73 minstr = new MachineInstr(SETUW);
74 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
Vikram S. Advea2a70942001-10-28 21:41:46 +000075 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
Vikram S. Adve30764b82001-10-18 00:01:48 +000076 }
77
Vikram S. Adve30764b82001-10-18 00:01:48 +000078 return minstr;
79}
80
81//************************* External Classes *******************************/
82
83//---------------------------------------------------------------------------
84// class UltraSparcInstrInfo
85//
86// Purpose:
87// Information about individual instructions.
88// Most information is stored in the SparcMachineInstrDesc array above.
89// Other information is computed on demand, and most such functions
90// default to member functions in base class MachineInstrInfo.
91//---------------------------------------------------------------------------
92
93/*ctor*/
Vikram S. Adveb9c38632001-11-08 04:57:53 +000094UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
95 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
Vikram S. Adve30764b82001-10-18 00:01:48 +000096 /*descSize = */ NUM_TOTAL_OPCODES,
97 /*numRealOpCodes = */ NUM_REAL_OPCODES)
98{
99}
100
101
102// Create an instruction sequence to put the constant `val' into
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000103// the virtual register `dest'. `val' may be a Constant or a
Vikram S. Adve30764b82001-10-18 00:01:48 +0000104// GlobalValue, viz., the constant address of a global variable or function.
105// The generated instructions are returned in `minstrVec'.
106// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
107//
108void
109UltraSparcInstrInfo::CreateCodeToLoadConst(Value* val,
Chris Lattner697954c2002-01-20 22:54:45 +0000110 Instruction* dest,
111 std::vector<MachineInstr*>& minstrVec,
112 std::vector<TmpInstruction*>& tempVec) const
Vikram S. Adve30764b82001-10-18 00:01:48 +0000113{
114 MachineInstr* minstr;
115
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
Vikram S. Adve30764b82001-10-18 00:01:48 +0000117 "I only know about constant values and global addresses");
118
119 // Use a "set" instruction for known constants that can go in an integer reg.
120 // Use a "load" instruction for all other constants, in particular,
121 // floating point constants and addresses of globals.
122 //
123 const Type* valType = val->getType();
124
125 if (valType->isIntegral() || valType == Type::BoolTy)
126 {
Vikram S. Advecee9d1c2001-12-15 00:33:36 +0000127 if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
128 {
129 uint64_t C = uval->getValue();
130 minstr = CreateUIntSetInstruction(C, dest, tempVec);
131 }
132 else
133 {
134 bool isValidConstant;
135 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
136 assert(isValidConstant && "Unrecognized constant");
137 minstr = CreateIntSetInstruction(C, dest, tempVec);
138 }
Vikram S. Adve30764b82001-10-18 00:01:48 +0000139 minstrVec.push_back(minstr);
140 }
141 else
142 {
143 // Make an instruction sequence to load the constant, viz:
Vikram S. Advea2a70942001-10-28 21:41:46 +0000144 // SETX <addr-of-constant>, tmpReg, addrReg
Vikram S. Adve30764b82001-10-18 00:01:48 +0000145 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
Vikram S. Advea2a70942001-10-28 21:41:46 +0000146 // Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
Vikram S. Adve30764b82001-10-18 00:01:48 +0000147 // itself a constant address. Otherwise, both are needed.
148
149 Value* addrVal;
150 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
151
Vikram S. Advea2a70942001-10-28 21:41:46 +0000152 TmpInstruction* tmpReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000153 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000154 tempVec.push_back(tmpReg);
155
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000156 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000157 {
158 // Create another TmpInstruction for the hidden integer register
159 TmpInstruction* addrReg =
Chris Lattnercb0a1202002-02-03 07:49:49 +0000160 new TmpInstruction(PointerType::get(val->getType()), val);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000161 tempVec.push_back(addrReg);
162 addrVal = addrReg;
163 }
164 else
165 addrVal = dest;
166
Vikram S. Advea2a70942001-10-28 21:41:46 +0000167 minstr = new MachineInstr(SETX);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000168 minstr->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp, val);
Vikram S. Advea2a70942001-10-28 21:41:46 +0000169 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
170 /*isdef*/ true);
171 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,addrVal);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000172 minstrVec.push_back(minstr);
173
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000174 if (isa<Constant>(val))
Vikram S. Adve30764b82001-10-18 00:01:48 +0000175 {
176 // addrVal->addMachineInstruction(minstr);
177
178 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
179 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
180 addrVal);
181 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
182 zeroOffset);
183 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
184 dest);
185 minstrVec.push_back(minstr);
186 }
187 }
188}
189
190
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000191// Create an instruction sequence to copy an integer value `val'
192// to a floating point value `dest' by copying to memory and back.
193// val must be an integral type. dest must be a Float or Double.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000194// The generated instructions are returned in `minstrVec'.
195// Any temp. registers (TmpInstruction) created are returned in `tempVec'.
196//
197void
198UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000199 Value* val,
200 Instruction* dest,
201 std::vector<MachineInstr*>& minstrVec,
202 std::vector<TmpInstruction*>& tempVec,
203 TargetMachine& target) const
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000204{
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000205 assert((val->getType()->isIntegral() || val->getType()->isPointerType())
206 && "Source type must be integral");
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000207 assert((dest->getType() ==Type::FloatTy || dest->getType() ==Type::DoubleTy)
208 && "Dest type must be float/double");
209
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000210 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
211 int offset = mcinfo.allocateLocalVar(target, val);
212
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000213 // Store instruction stores `val' to [%fp+offset].
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000214 // The store and load opCodes are based on the value being copied, and
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000215 // they use integer and float types that accomodate the
216 // larger of the source type and the destination type:
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000217 // On SparcV9: int for float, long for double.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000218 //
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000219 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
220 : Type::LongTy;
221 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
222 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
223 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
224 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
225 minstrVec.push_back(store);
Vikram S. Adve30764b82001-10-18 00:01:48 +0000226
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000227 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000228 //
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000229 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000230 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
231 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
232 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
233 minstrVec.push_back(load);
234}
235
236
237// Similarly, create an instruction sequence to copy an FP value
238// `val' to an integer value `dest' by copying to memory and back.
239// See the previous function for information about return values.
240//
241void
242UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Method* method,
Chris Lattner697954c2002-01-20 22:54:45 +0000243 Value* val,
244 Instruction* dest,
245 std::vector<MachineInstr*>& minstrVec,
246 std::vector<TmpInstruction*>& tempVec,
247 TargetMachine& target) const
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000248{
249 assert((val->getType() ==Type::FloatTy || val->getType() ==Type::DoubleTy)
250 && "Source type must be float/double");
251 assert((dest->getType()->isIntegral() || dest->getType()->isPointerType())
252 && "Dest type must be integral");
253
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000254 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
255 int offset = mcinfo.allocateLocalVar(target, val);
256
257 // Store instruction stores `val' to [%fp+offset].
258 // The store and load opCodes are based on the value being copied, and
259 // they use the integer type that matches the source type in size:
260 // On SparcV9: int for float, long for double.
261 //
262 Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
263 : Type::LongTy;
Vikram S. Adveb9959d82001-11-15 14:59:56 +0000264 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
Vikram S. Adve5b6082e2001-11-09 02:16:40 +0000265 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
266 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
267 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
268 minstrVec.push_back(store);
269
270 // Load instruction loads [%fp+offset] to `dest'.
Vikram S. Adveb9c38632001-11-08 04:57:53 +0000271 //
272 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
273 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
274 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
275 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
276 minstrVec.push_back(load);
277}