blob: 41a75474087567d493870c859661eb82debe5948 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInternals.h ----------------------------------------*- C++ -*-===//
Vikram S. Adve7f37fe52001-11-08 04:55:13 +00002//
Chris Lattner035dfbe2002-08-09 20:08:06 +00003// This file defines stuff that is to be private to the Sparc backend, but is
4// shared among different portions of the backend.
5//
6//===----------------------------------------------------------------------===//
Chris Lattnerc6495ee2001-09-14 03:56:45 +00007
8#ifndef SPARC_INTERNALS_H
9#define SPARC_INTERNALS_H
10
Misha Brukmane9d88382003-05-24 00:09:50 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000012#include "llvm/Target/TargetMachine.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000013#include "llvm/Target/TargetSchedInfo.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000014#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerdde12622002-12-29 02:50:33 +000015#include "llvm/Target/TargetCacheInfo.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000016#include "llvm/Target/TargetRegInfo.h"
Chris Lattnerc6495ee2001-09-14 03:56:45 +000017#include "llvm/Type.h"
Misha Brukmane9d88382003-05-24 00:09:50 +000018#include "SparcRegClassInfo.h"
John Criswell7a73b802003-06-30 21:59:07 +000019#include "Config/sys/types.h"
Chris Lattnerc6495ee2001-09-14 03:56:45 +000020
Chris Lattner4387e312002-02-03 23:42:19 +000021class LiveRange;
Chris Lattnerf6e0e282001-09-14 04:32:55 +000022class UltraSparc;
Chris Lattner9aa697b2002-04-09 05:16:36 +000023class Pass;
Chris Lattner4387e312002-02-03 23:42:19 +000024
Chris Lattnerc6495ee2001-09-14 03:56:45 +000025enum SparcInstrSchedClass {
26 SPARC_NONE, /* Instructions with no scheduling restrictions */
27 SPARC_IEUN, /* Integer class that can use IEU0 or IEU1 */
28 SPARC_IEU0, /* Integer class IEU0 */
29 SPARC_IEU1, /* Integer class IEU1 */
30 SPARC_FPM, /* FP Multiply or Divide instructions */
31 SPARC_FPA, /* All other FP instructions */
32 SPARC_CTI, /* Control-transfer instructions */
33 SPARC_LD, /* Load instructions */
34 SPARC_ST, /* Store instructions */
35 SPARC_SINGLE, /* Instructions that must issue by themselves */
36
37 SPARC_INV, /* This should stay at the end for the next value */
38 SPARC_NUM_SCHED_CLASSES = SPARC_INV
39};
40
Chris Lattnerc6495ee2001-09-14 03:56:45 +000041
42//---------------------------------------------------------------------------
43// enum SparcMachineOpCode.
Chris Lattner3501fea2003-01-14 22:00:31 +000044// const TargetInstrDescriptor SparcMachineInstrDesc[]
Chris Lattnerc6495ee2001-09-14 03:56:45 +000045//
46// Purpose:
47// Description of UltraSparc machine instructions.
48//
49//---------------------------------------------------------------------------
50
Misha Brukmana98cd452003-05-20 20:32:24 +000051namespace V9 {
52 enum SparcMachineOpCode {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000053#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
54 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
55 ENUM,
56#include "SparcInstr.def"
Chris Lattnerc6495ee2001-09-14 03:56:45 +000057
Misha Brukmana98cd452003-05-20 20:32:24 +000058 // End-of-array marker
59 INVALID_OPCODE,
60 NUM_REAL_OPCODES = PHI, // number of valid opcodes
61 NUM_TOTAL_OPCODES = INVALID_OPCODE
62 };
63}
Chris Lattnerc6495ee2001-09-14 03:56:45 +000064
Chris Lattnerc6495ee2001-09-14 03:56:45 +000065
Chris Lattner9a3d63b2001-09-19 15:56:23 +000066// Array of machine instruction descriptions...
Chris Lattner3501fea2003-01-14 22:00:31 +000067extern const TargetInstrDescriptor SparcMachineInstrDesc[];
Chris Lattnerc6495ee2001-09-14 03:56:45 +000068
69
70//---------------------------------------------------------------------------
71// class UltraSparcInstrInfo
72//
73// Purpose:
74// Information about individual instructions.
75// Most information is stored in the SparcMachineInstrDesc array above.
76// Other information is computed on demand, and most such functions
Chris Lattner3501fea2003-01-14 22:00:31 +000077// default to member functions in base class TargetInstrInfo.
Chris Lattnerc6495ee2001-09-14 03:56:45 +000078//---------------------------------------------------------------------------
79
Chris Lattner3501fea2003-01-14 22:00:31 +000080struct UltraSparcInstrInfo : public TargetInstrInfo {
Chris Lattner047bbaf2002-10-29 15:45:20 +000081 UltraSparcInstrInfo();
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000082
83 //
Vikram S. Advedd558992002-03-18 03:02:42 +000084 // All immediate constants are in position 1 except the
Vikram S. Advee1f72802002-09-16 15:39:26 +000085 // store instructions and SETxx.
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000086 //
Vikram S. Advedd558992002-03-18 03:02:42 +000087 virtual int getImmedConstantPos(MachineOpCode opCode) const {
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000088 bool ignore;
Misha Brukmana98cd452003-05-20 20:32:24 +000089 if (this->maxImmedConstant(opCode, ignore) != 0) {
90 // 1st store opcode
Misha Brukman3c4cf152003-05-27 22:44:44 +000091 assert(! this->isStore((MachineOpCode) V9::STBr - 1));
Misha Brukmana98cd452003-05-20 20:32:24 +000092 // last store opcode
Misha Brukman3c4cf152003-05-27 22:44:44 +000093 assert(! this->isStore((MachineOpCode) V9::STXFSRi + 1));
Misha Brukmana98cd452003-05-20 20:32:24 +000094
95 if (opCode == V9::SETSW || opCode == V9::SETUW ||
96 opCode == V9::SETX || opCode == V9::SETHI)
97 return 0;
Misha Brukman3c4cf152003-05-27 22:44:44 +000098 if (opCode >= V9::STBr && opCode <= V9::STXFSRi)
Misha Brukmana98cd452003-05-20 20:32:24 +000099 return 2;
100 return 1;
101 }
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +0000102 else
103 return -1;
104 }
Misha Brukmane9d88382003-05-24 00:09:50 +0000105
106 /// createNOPinstr - returns the target's implementation of NOP, which is
107 /// usually a pseudo-instruction, implemented by a degenerate version of
Misha Brukman79caf1f2003-05-27 22:01:10 +0000108 /// another instruction, e.g. X86: xchg ax, ax; SparcV9: sethi 0, g0
Misha Brukmane9d88382003-05-24 00:09:50 +0000109 ///
110 MachineInstr* createNOPinstr() const {
Misha Brukman79caf1f2003-05-27 22:01:10 +0000111 return BuildMI(V9::SETHI, 2).addZImm(0).addReg(SparcIntRegClass::g0);
Misha Brukmane9d88382003-05-24 00:09:50 +0000112 }
113
Misha Brukman12745c52003-05-24 01:08:43 +0000114 /// isNOPinstr - not having a special NOP opcode, we need to know if a given
115 /// instruction is interpreted as an `official' NOP instr, i.e., there may be
116 /// more than one way to `do nothing' but only one canonical way to slack off.
Misha Brukmane9d88382003-05-24 00:09:50 +0000117 ///
118 bool isNOPinstr(const MachineInstr &MI) const {
119 // Make sure the instruction is EXACTLY `sethi g0, 0'
120 if (MI.getOpcode() == V9::SETHI && MI.getNumOperands() == 2) {
121 const MachineOperand &op0 = MI.getOperand(0), &op1 = MI.getOperand(1);
Misha Brukman79caf1f2003-05-27 22:01:10 +0000122 if (op0.isImmediate() && op0.getImmedValue() == 0 &&
123 op1.isMachineRegister() &&
124 op1.getMachineRegNum() == SparcIntRegClass::g0)
Misha Brukmane9d88382003-05-24 00:09:50 +0000125 {
126 return true;
127 }
128 }
129 return false;
130 }
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000131
Misha Brukmana98cd452003-05-20 20:32:24 +0000132 virtual bool hasResultInterlock(MachineOpCode opCode) const
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000133 {
134 // All UltraSPARC instructions have interlocks (note that delay slots
135 // are not considered here).
136 // However, instructions that use the result of an FCMP produce a
137 // 9-cycle stall if they are issued less than 3 cycles after the FCMP.
138 // Force the compiler to insert a software interlock (i.e., gap of
139 // 2 other groups, including NOPs if necessary).
Misha Brukmana98cd452003-05-20 20:32:24 +0000140 return (opCode == V9::FCMPS || opCode == V9::FCMPD || opCode == V9::FCMPQ);
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000141 }
142
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000143 //-------------------------------------------------------------------------
Vikram S. Advee1f72802002-09-16 15:39:26 +0000144 // Queries about representation of LLVM quantities (e.g., constants)
145 //-------------------------------------------------------------------------
146
147 virtual bool ConstantMayNotFitInImmedField(const Constant* CV,
148 const Instruction* I) const;
149
150 //-------------------------------------------------------------------------
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000151 // Code generation support for creating individual machine instructions
152 //-------------------------------------------------------------------------
Vikram S. Adved55697c2002-09-20 00:52:09 +0000153
154 // Get certain common op codes for the current target. This and all the
155 // Create* methods below should be moved to a machine code generation class
156 //
Misha Brukmana98cd452003-05-20 20:32:24 +0000157 virtual MachineOpCode getNOPOpCode() const { return V9::NOP; }
Vikram S. Adved55697c2002-09-20 00:52:09 +0000158
Vikram S. Adveada280f2003-07-29 19:54:41 +0000159 // Get the value of an integral constant in the form that must
160 // be put into the machine register. The specified constant is interpreted
161 // as (i.e., converted if necessary to) the specified destination type. The
162 // result is always returned as an uint64_t, since the representation of
163 // int64_t and uint64_t are identical. The argument can be any known const.
164 //
165 // isValidConstant is set to true if a valid constant was found.
166 //
167 virtual uint64_t ConvertConstantToIntType(const TargetMachine &target,
168 const Value *V,
169 const Type *destType,
170 bool &isValidConstant) const;
171
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000172 // Create an instruction sequence to put the constant `val' into
Vikram S. Adve242a8082002-05-19 15:25:51 +0000173 // the virtual register `dest'. `val' may be a Constant or a
174 // GlobalValue, viz., the constant address of a global variable or function.
175 // The generated instructions are returned in `mvec'.
176 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
177 // Any stack space required is allocated via mcff.
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000178 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000179 virtual void CreateCodeToLoadConst(const TargetMachine& target,
180 Function* F,
Vikram S. Advedd558992002-03-18 03:02:42 +0000181 Value* val,
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000182 Instruction* dest,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000183 std::vector<MachineInstr*>& mvec,
184 MachineCodeForInstruction& mcfi) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000185
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000186 // Create an instruction sequence to copy an integer value `val'
187 // to a floating point value `dest' by copying to memory and back.
188 // val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000189 // The generated instructions are returned in `mvec'.
190 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
191 // Any stack space required is allocated via mcff.
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000192 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000193 virtual void CreateCodeToCopyIntToFloat(const TargetMachine& target,
194 Function* F,
195 Value* val,
196 Instruction* dest,
197 std::vector<MachineInstr*>& mvec,
198 MachineCodeForInstruction& mcfi) const;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000199
200 // Similarly, create an instruction sequence to copy an FP value
201 // `val' to an integer value `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000202 // The generated instructions are returned in `mvec'.
203 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
204 // Any stack space required is allocated via mcff.
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000205 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000206 virtual void CreateCodeToCopyFloatToInt(const TargetMachine& target,
207 Function* F,
208 Value* val,
209 Instruction* dest,
210 std::vector<MachineInstr*>& mvec,
211 MachineCodeForInstruction& mcfi) const;
212
213 // Create instruction(s) to copy src to dest, for arbitrary types
214 // The generated instructions are returned in `mvec'.
215 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
216 // Any stack space required is allocated via mcff.
217 //
Vikram S. Advedd558992002-03-18 03:02:42 +0000218 virtual void CreateCopyInstructionsByType(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000219 Function* F,
220 Value* src,
221 Instruction* dest,
222 std::vector<MachineInstr*>& mvec,
223 MachineCodeForInstruction& mcfi) const;
224
225 // Create instruction sequence to produce a sign-extended register value
226 // from an arbitrary sized value (sized in bits, not bytes).
Vikram S. Advef36f06b2002-09-05 18:34:31 +0000227 // The generated instructions are appended to `mvec'.
228 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000229 // Any stack space required is allocated via mcff.
230 //
231 virtual void CreateSignExtensionInstructions(const TargetMachine& target,
232 Function* F,
Vikram S. Advef36f06b2002-09-05 18:34:31 +0000233 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000234 Value* destVal,
235 unsigned int numLowBits,
Vikram S. Advef36f06b2002-09-05 18:34:31 +0000236 std::vector<MachineInstr*>& mvec,
237 MachineCodeForInstruction& mcfi) const;
238
239 // Create instruction sequence to produce a zero-extended register value
240 // from an arbitrary sized value (sized in bits, not bytes).
241 // The generated instructions are appended to `mvec'.
242 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
243 // Any stack space required is allocated via mcff.
244 //
245 virtual void CreateZeroExtensionInstructions(const TargetMachine& target,
246 Function* F,
247 Value* srcVal,
Vikram S. Adve5cedede2002-09-27 14:29:45 +0000248 Value* destVal,
249 unsigned int numLowBits,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000250 std::vector<MachineInstr*>& mvec,
251 MachineCodeForInstruction& mcfi) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000252};
253
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000254
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000255//----------------------------------------------------------------------------
256// class UltraSparcRegInfo
257//
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000258// This class implements the virtual class TargetRegInfo for Sparc.
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000259//
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000260//----------------------------------------------------------------------------
261
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000262class UltraSparcRegInfo : public TargetRegInfo {
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000263
Vikram S. Adve46d3f8e2003-07-06 19:53:59 +0000264private:
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000265
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000266 // Number of registers used for passing int args (usually 6: %o0 - %o5)
267 //
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000268 unsigned const NumOfIntArgRegs;
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000269
270 // Number of registers used for passing float args (usually 32: %f0 - %f31)
271 //
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000272 unsigned const NumOfFloatArgRegs;
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000273
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000274 // ======================== Private Methods =============================
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000275
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000276 // The following methods are used to color special live ranges (e.g.
Chris Lattnerf57b8452002-04-27 06:56:12 +0000277 // function args and return values etc.) with specific hardware registers
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000278 // as required. See SparcRegInfo.cpp for the implementation.
279 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000280 void suggestReg4RetAddr(MachineInstr *RetMI,
Chris Lattner699683c2002-02-04 05:59:25 +0000281 LiveRangeInfo &LRI) const;
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000282
Vikram S. Adve106604e2002-09-28 16:56:59 +0000283 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000284
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000285 // Helper used by the all the getRegType() functions.
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000286 int getRegTypeForClassAndType(unsigned regClassID, const Type* type) const;
Ruchira Sasanka3839e6e2001-11-03 19:59:59 +0000287
Chris Lattner699683c2002-02-04 05:59:25 +0000288public:
Misha Brukmand3d97be2003-05-30 20:12:42 +0000289 // Type of registers available in Sparc. There can be several reg types
290 // in the same class. For instace, the float reg class has Single/Double
291 // types
292 //
293 enum RegTypes {
294 IntRegType,
295 FPSingleRegType,
296 FPDoubleRegType,
297 IntCCRegType,
298 FloatCCRegType,
299 SpecialRegType
300 };
301
Misha Brukmande11f2d2003-07-07 16:52:39 +0000302 // The actual register classes in the Sparc
303 //
304 // **** WARNING: If this enum order is changed, also modify
305 // getRegisterClassOfValue method below since it assumes this particular
306 // order for efficiency.
307 //
308 enum RegClassIDs {
309 IntRegClassID, // Integer
310 FloatRegClassID, // Float (both single/double)
311 IntCCRegClassID, // Int Condition Code
312 FloatCCRegClassID, // Float Condition code
313 SpecialRegClassID // Special (unallocated) registers
314 };
315
Chris Lattner699683c2002-02-04 05:59:25 +0000316 UltraSparcRegInfo(const UltraSparc &tgt);
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000317
Vikram S. Advedd558992002-03-18 03:02:42 +0000318 // To find the register class used for a specified Type
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000319 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000320 unsigned getRegClassIDOfType(const Type *type,
321 bool isCCReg = false) const;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000322
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000323 // To find the register class to which a specified register belongs
324 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000325 unsigned getRegClassIDOfRegType(int regType) const;
Vikram S. Advedd558992002-03-18 03:02:42 +0000326
Chris Lattner699683c2002-02-04 05:59:25 +0000327 // getZeroRegNum - returns the register that contains always zero this is the
328 // unified register number
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000329 //
Chris Lattner699683c2002-02-04 05:59:25 +0000330 virtual int getZeroRegNum() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000331
Chris Lattner699683c2002-02-04 05:59:25 +0000332 // getCallAddressReg - returns the reg used for pushing the address when a
Chris Lattnerf57b8452002-04-27 06:56:12 +0000333 // function is called. This can be used for other purposes between calls
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000334 //
Chris Lattner699683c2002-02-04 05:59:25 +0000335 unsigned getCallAddressReg() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000336
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000337 // Returns the register containing the return address.
338 // It should be made sure that this register contains the return
339 // value when a return instruction is reached.
340 //
Chris Lattner699683c2002-02-04 05:59:25 +0000341 unsigned getReturnAddressReg() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000342
Vikram S. Adve242a8082002-05-19 15:25:51 +0000343 // Number of registers used for passing int args (usually 6: %o0 - %o5)
344 // and float args (usually 32: %f0 - %f31)
345 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +0000346 unsigned const getNumOfIntArgRegs() const { return NumOfIntArgRegs; }
347 unsigned const getNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000348
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000349 // Compute which register can be used for an argument, if any
350 //
351 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
352 unsigned argNo, unsigned& regClassId) const;
353
354 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
355 unsigned argNo, unsigned& regClassId) const;
356
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000357 // The following methods are used to color special live ranges (e.g.
Chris Lattnerf57b8452002-04-27 06:56:12 +0000358 // function args and return values etc.) with specific hardware registers
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000359 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
360 //
Chris Lattnerb7653df2002-04-08 22:03:57 +0000361 void suggestRegs4MethodArgs(const Function *Meth,
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000362 LiveRangeInfo& LRI) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000363
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000364 void suggestRegs4CallArgs(MachineInstr *CallMI,
Vikram S. Adve106604e2002-09-28 16:56:59 +0000365 LiveRangeInfo& LRI) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000366
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000367 void suggestReg4RetValue(MachineInstr *RetMI,
Chris Lattner697954c2002-01-20 22:54:45 +0000368 LiveRangeInfo& LRI) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000369
Vikram S. Adveada280f2003-07-29 19:54:41 +0000370 void colorMethodArgs(const Function *Meth, LiveRangeInfo& LRI,
371 std::vector<MachineInstr*>& InstrnsBefore,
372 std::vector<MachineInstr*>& InstrnsAfter) const;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000373
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000374 // method used for printing a register for debugging purposes
375 //
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000376 void printReg(const LiveRange *LR) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000377
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000378 // returns the # of bytes of stack space allocated for each register
379 // type. For Sparc, currently we allocate 8 bytes on stack for all
380 // register types. We can optimize this later if necessary to save stack
381 // space (However, should make sure that stack alignment is correct)
382 //
Chris Lattner699683c2002-02-04 05:59:25 +0000383 inline int getSpilledRegSize(int RegType) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000384 return 8;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000385 }
386
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000387
Vikram S. Advea44c6c02002-03-31 19:04:50 +0000388 // To obtain the return value and the indirect call address (if any)
389 // contained in a CALL machine instruction
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000390 //
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000391 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
Vikram S. Advea44c6c02002-03-31 19:04:50 +0000392 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000393
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000394 // The following methods are used to generate "copy" machine instructions
395 // for an architecture.
396 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000397 // The function regTypeNeedsScratchReg() can be used to check whether a
398 // scratch register is needed to copy a register of type `regType' to
399 // or from memory. If so, such a scratch register can be provided by
400 // the caller (e.g., if it knows which regsiters are free); otherwise
401 // an arbitrary one will be chosen and spilled by the copy instructions.
402 //
403 bool regTypeNeedsScratchReg(int RegType,
404 int& scratchRegClassId) const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000405
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000406 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
407 unsigned SrcReg, unsigned DestReg,
408 int RegType) const;
409
410 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
411 unsigned SrcReg, unsigned DestPtrReg,
412 int Offset, int RegType, int scratchReg = -1) const;
413
414 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
415 unsigned SrcPtrReg, int Offset, unsigned DestReg,
416 int RegType, int scratchReg = -1) const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000417
Vikram S. Adve242a8082002-05-19 15:25:51 +0000418 void cpValue2Value(Value *Src, Value *Dest,
Anand Shuklacfb22d32002-06-25 20:55:50 +0000419 std::vector<MachineInstr*>& mvec) const;
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000420
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000421 // Get the register type for a register identified different ways.
Vikram S. Adve7dc7de52003-07-25 21:12:15 +0000422 // Note that getRegTypeForLR(LR) != getRegTypeForDataType(LR->getType())!
423 // The reg class of a LR depends both on the Value types in it and whether
424 // they are CC registers or not (for example).
425 int getRegTypeForDataType(const Type* type) const;
426 int getRegTypeForLR(const LiveRange *LR) const;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000427 int getRegType(int unifiedRegNum) const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000428
Chris Lattner699683c2002-02-04 05:59:25 +0000429 virtual unsigned getFramePointer() const;
430 virtual unsigned getStackPointer() const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000431};
432
433
434
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000435
436//---------------------------------------------------------------------------
437// class UltraSparcSchedInfo
438//
439// Purpose:
440// Interface to instruction scheduling information for UltraSPARC.
441// The parameter values above are based on UltraSPARC IIi.
442//---------------------------------------------------------------------------
443
444
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000445class UltraSparcSchedInfo: public TargetSchedInfo {
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000446public:
Chris Lattner699683c2002-02-04 05:59:25 +0000447 UltraSparcSchedInfo(const TargetMachine &tgt);
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000448protected:
Chris Lattner699683c2002-02-04 05:59:25 +0000449 virtual void initializeResources();
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000450};
451
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000452
453//---------------------------------------------------------------------------
Vikram S. Advec1521632001-10-22 13:31:53 +0000454// class UltraSparcFrameInfo
455//
456// Purpose:
457// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000458// Starting offsets for each area of the stack frame are aligned at
459// a multiple of getStackFrameSizeAlignment().
Vikram S. Advec1521632001-10-22 13:31:53 +0000460//---------------------------------------------------------------------------
461
Chris Lattnerda62ac62002-12-28 20:20:24 +0000462class UltraSparcFrameInfo: public TargetFrameInfo {
463 const TargetMachine &target;
Vikram S. Advec1521632001-10-22 13:31:53 +0000464public:
Chris Lattnerda62ac62002-12-28 20:20:24 +0000465 UltraSparcFrameInfo(const TargetMachine &TM)
466 : TargetFrameInfo(StackGrowsDown, StackFrameSizeAlignment, 0), target(TM) {}
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000467
468public:
Vikram S. Advee1f72802002-09-16 15:39:26 +0000469 // These methods provide constant parameters of the frame layout.
470 //
Chris Lattnerf57b8452002-04-27 06:56:12 +0000471 int getStackFrameSizeAlignment() const { return StackFrameSizeAlignment;}
472 int getMinStackFrameSize() const { return MinStackFrameSize; }
473 int getNumFixedOutgoingArgs() const { return NumFixedOutgoingArgs; }
474 int getSizeOfEachArgOnStack() const { return SizeOfEachArgOnStack; }
475 bool argsOnStackHaveFixedSize() const { return true; }
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000476
Vikram S. Advee1f72802002-09-16 15:39:26 +0000477 // This method adjusts a stack offset to meet alignment rules of target.
478 // The fixed OFFSET (0x7ff) must be subtracted and the result aligned.
479 virtual int adjustAlignment (int unalignedOffset,
480 bool growUp,
481 unsigned int align) const {
482 return unalignedOffset + (growUp? +1:-1)*((unalignedOffset-OFFSET) % align);
483 }
484
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000485 // These methods compute offsets using the frame contents for a
Chris Lattnerf57b8452002-04-27 06:56:12 +0000486 // particular function. The frame contents are obtained from the
487 // MachineCodeInfoForMethod object for the given function.
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000488 //
Misha Brukmanfce11432002-10-28 00:28:31 +0000489 int getFirstIncomingArgOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000490 bool& growUp) const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000491 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000492 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000493 return FirstIncomingArgOffsetFromFP;
494 }
Misha Brukmanfce11432002-10-28 00:28:31 +0000495 int getFirstOutgoingArgOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000496 bool& growUp) const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000497 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000498 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000499 return FirstOutgoingArgOffsetFromSP;
500 }
Misha Brukmanfce11432002-10-28 00:28:31 +0000501 int getFirstOptionalOutgoingArgOffset(MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000502 bool& growUp)const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000503 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000504 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000505 return FirstOptionalOutgoingArgOffsetFromSP;
506 }
507
Misha Brukmanfce11432002-10-28 00:28:31 +0000508 int getFirstAutomaticVarOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000509 bool& growUp) const;
Misha Brukmanfce11432002-10-28 00:28:31 +0000510 int getRegSpillAreaOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000511 bool& growUp) const;
Misha Brukmanfce11432002-10-28 00:28:31 +0000512 int getTmpAreaOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000513 bool& growUp) const;
Misha Brukmanfce11432002-10-28 00:28:31 +0000514 int getDynamicAreaOffset (MachineFunction& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000515 bool& growUp) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000516
517 //
518 // These methods specify the base register used for each stack area
519 // (generally FP or SP)
520 //
521 virtual int getIncomingArgBaseRegNum() const {
522 return (int) target.getRegInfo().getFramePointer();
523 }
524 virtual int getOutgoingArgBaseRegNum() const {
525 return (int) target.getRegInfo().getStackPointer();
526 }
527 virtual int getOptionalOutgoingArgBaseRegNum() const {
528 return (int) target.getRegInfo().getStackPointer();
529 }
530 virtual int getAutomaticVarBaseRegNum() const {
531 return (int) target.getRegInfo().getFramePointer();
532 }
533 virtual int getRegSpillAreaBaseRegNum() const {
534 return (int) target.getRegInfo().getFramePointer();
535 }
536 virtual int getDynamicAreaBaseRegNum() const {
537 return (int) target.getRegInfo().getStackPointer();
538 }
Chris Lattnerda62ac62002-12-28 20:20:24 +0000539
540 virtual int getIncomingArgOffset(MachineFunction& mcInfo,
541 unsigned argNum) const {
542 assert(argsOnStackHaveFixedSize());
543
544 unsigned relativeOffset = argNum * getSizeOfEachArgOnStack();
545 bool growUp; // do args grow up or down
546 int firstArg = getFirstIncomingArgOffset(mcInfo, growUp);
547 return growUp ? firstArg + relativeOffset : firstArg - relativeOffset;
548 }
549
550 virtual int getOutgoingArgOffset(MachineFunction& mcInfo,
551 unsigned argNum) const {
552 assert(argsOnStackHaveFixedSize());
553 //assert(((int) argNum - this->getNumFixedOutgoingArgs())
554 // <= (int) mcInfo.getInfo()->getMaxOptionalNumArgs());
555
556 unsigned relativeOffset = argNum * getSizeOfEachArgOnStack();
557 bool growUp; // do args grow up or down
558 int firstArg = getFirstOutgoingArgOffset(mcInfo, growUp);
559 return growUp ? firstArg + relativeOffset : firstArg - relativeOffset;
560 }
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000561
562private:
Vikram S. Advee1f72802002-09-16 15:39:26 +0000563 /*----------------------------------------------------------------------
564 This diagram shows the stack frame layout used by llc on Sparc V9.
565 Note that only the location of automatic variables, spill area,
566 temporary storage, and dynamically allocated stack area are chosen
567 by us. The rest conform to the Sparc V9 ABI.
568 All stack addresses are offset by OFFSET = 0x7ff (2047).
569
Chris Lattnerda62ac62002-12-28 20:20:24 +0000570 Alignment assumptions and other invariants:
Vikram S. Advee1f72802002-09-16 15:39:26 +0000571 (1) %sp+OFFSET and %fp+OFFSET are always aligned on 16-byte boundary
572 (2) Variables in automatic, spill, temporary, or dynamic regions
573 are aligned according to their size as in all memory accesses.
574 (3) Everything below the dynamically allocated stack area is only used
575 during a call to another function, so it is never needed when
576 the current function is active. This is why space can be allocated
577 dynamically by incrementing %sp any time within the function.
578
579 STACK FRAME LAYOUT:
580
581 ...
582 %fp+OFFSET+176 Optional extra incoming arguments# 1..N
583 %fp+OFFSET+168 Incoming argument #6
584 ... ...
585 %fp+OFFSET+128 Incoming argument #1
586 ... ...
587 ---%fp+OFFSET-0--------Bottom of caller's stack frame--------------------
588 %fp+OFFSET-8 Automatic variables <-- ****TOP OF STACK FRAME****
589 Spill area
590 Temporary storage
591 ...
592
593 %sp+OFFSET+176+8N Bottom of dynamically allocated stack area
594 %sp+OFFSET+168+8N Optional extra outgoing argument# N
595 ... ...
596 %sp+OFFSET+176 Optional extra outgoing argument# 1
597 %sp+OFFSET+168 Outgoing argument #6
598 ... ...
599 %sp+OFFSET+128 Outgoing argument #1
600 %sp+OFFSET+120 Save area for %i7
601 ... ...
602 %sp+OFFSET+0 Save area for %l0 <-- ****BOTTOM OF STACK FRAME****
603
604 *----------------------------------------------------------------------*/
605
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000606 // All stack addresses must be offset by 0x7ff (2047) on Sparc V9.
607 static const int OFFSET = (int) 0x7ff;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000608 static const int StackFrameSizeAlignment = 16;
Vikram S. Advec1521632001-10-22 13:31:53 +0000609 static const int MinStackFrameSize = 176;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000610 static const int NumFixedOutgoingArgs = 6;
611 static const int SizeOfEachArgOnStack = 8;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000612 static const int FirstIncomingArgOffsetFromFP = 128 + OFFSET;
613 static const int FirstOptionalIncomingArgOffsetFromFP = 176 + OFFSET;
Vikram S. Advee1f72802002-09-16 15:39:26 +0000614 static const int StaticAreaOffsetFromFP = 0 + OFFSET;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000615 static const int FirstOutgoingArgOffsetFromSP = 128 + OFFSET;
616 static const int FirstOptionalOutgoingArgOffsetFromSP = 176 + OFFSET;
Vikram S. Advec1521632001-10-22 13:31:53 +0000617};
618
619
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000620//---------------------------------------------------------------------------
621// class UltraSparcCacheInfo
622//
623// Purpose:
624// Interface to cache parameters for the UltraSPARC.
625// Just use defaults for now.
626//---------------------------------------------------------------------------
627
Chris Lattnerdde12622002-12-29 02:50:33 +0000628struct UltraSparcCacheInfo: public TargetCacheInfo {
629 UltraSparcCacheInfo(const TargetMachine &T) : TargetCacheInfo(T) {}
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000630};
631
Vikram S. Advec1521632001-10-22 13:31:53 +0000632
Misha Brukman79caf1f2003-05-27 22:01:10 +0000633/// createAddRegNumToValuesPass - this pass adds unsigned register numbers to
634/// instructions, since that's not done by the Sparc InstSelector, but that's
635/// how the target-independent register allocator in the JIT likes to see
636/// instructions. This pass enables the usage of the JIT register allocator(s).
637Pass *createAddRegNumToValuesPass();
638
Chris Lattner48e60792003-08-13 02:38:16 +0000639/// createStackSlotsPass - External interface to stack-slots pass that enters 2
640/// empty slots at the top of each function stack
641Pass *createStackSlotsPass(const TargetMachine &TM);
642
Chris Lattner67699ff2003-09-01 20:33:07 +0000643// Interface to pre-selection pass that specializes LLVM code for a target
644// machine.
645Pass *createPreSelectionPass(TargetMachine &Target);
646
647// External interface to peephole optimization pass operating on machine code.
648FunctionPass *createPeepholeOptsPass(TargetMachine &Target);
649
Chris Lattner48e60792003-08-13 02:38:16 +0000650
Vikram S. Adved55697c2002-09-20 00:52:09 +0000651//---------------------------------------------------------------------------
Chris Lattner48e60792003-08-13 02:38:16 +0000652// class UltraSparc
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000653//
654// Purpose:
655// Primary interface to machine description for the UltraSPARC.
656// Primarily just initializes machine-dependent parameters in
657// class TargetMachine, and creates machine-dependent subclasses
Vikram S. Adve339084b2001-09-18 13:04:24 +0000658// for classes such as InstrInfo, SchedInfo and RegInfo.
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000659//---------------------------------------------------------------------------
660
661class UltraSparc : public TargetMachine {
Vikram S. Adve339084b2001-09-18 13:04:24 +0000662 UltraSparcInstrInfo instrInfo;
663 UltraSparcSchedInfo schedInfo;
664 UltraSparcRegInfo regInfo;
Vikram S. Advec1521632001-10-22 13:31:53 +0000665 UltraSparcFrameInfo frameInfo;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000666 UltraSparcCacheInfo cacheInfo;
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000667public:
668 UltraSparc();
Vikram S. Adved55697c2002-09-20 00:52:09 +0000669
Chris Lattner3501fea2003-01-14 22:00:31 +0000670 virtual const TargetInstrInfo &getInstrInfo() const { return instrInfo; }
Chris Lattnerd0f166a2002-12-29 03:13:05 +0000671 virtual const TargetSchedInfo &getSchedInfo() const { return schedInfo; }
672 virtual const TargetRegInfo &getRegInfo() const { return regInfo; }
Chris Lattnerda62ac62002-12-28 20:20:24 +0000673 virtual const TargetFrameInfo &getFrameInfo() const { return frameInfo; }
Chris Lattnerdde12622002-12-29 02:50:33 +0000674 virtual const TargetCacheInfo &getCacheInfo() const { return cacheInfo; }
Chris Lattner32f600a2001-09-19 13:47:12 +0000675
Chris Lattner63342052002-10-29 21:12:46 +0000676 virtual bool addPassesToEmitAssembly(PassManager &PM, std::ostream &Out);
Brian Gaeked7d6bbe2003-08-14 06:04:29 +0000677 virtual bool addPassesToJITCompile(FunctionPassManager &PM);
678 virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman79caf1f2003-05-27 22:01:10 +0000679 MachineCodeEmitter &MCE);
Brian Gaeke17b53a82003-10-20 15:17:12 +0000680 virtual void replaceMachineCodeForFunction(void *Old, void *New);
Chris Lattner4f946372002-10-28 01:03:43 +0000681
Chris Lattnerc66583e2002-10-29 22:01:44 +0000682 // getPrologEpilogInsertionPass - Inserts prolog/epilog code.
Brian Gaeked7d6bbe2003-08-14 06:04:29 +0000683 FunctionPass* getPrologEpilogInsertionPass();
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000684
Vikram S. Advee1f72802002-09-16 15:39:26 +0000685 // getFunctionAsmPrinterPass - Writes out machine code for a single function
Chris Lattnerc66583e2002-10-29 22:01:44 +0000686 Pass* getFunctionAsmPrinterPass(std::ostream &Out);
Vikram S. Advee1f72802002-09-16 15:39:26 +0000687
688 // getModuleAsmPrinterPass - Writes generated machine code to assembly file.
Chris Lattnerc66583e2002-10-29 22:01:44 +0000689 Pass* getModuleAsmPrinterPass(std::ostream &Out);
Vikram S. Advee1f72802002-09-16 15:39:26 +0000690
Brian Gaeke79c98032003-09-18 17:37:46 +0000691 // getBytecodeAsmPrinterPass - Emits final LLVM bytecode to assembly file.
692 Pass* getBytecodeAsmPrinterPass(std::ostream &Out);
Chris Lattner6edfcc52002-02-03 07:51:17 +0000693};
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000694
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000695#endif