blob: 4ff60644cd506be2baa6017638ec2f17dc48f2fe [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
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000011#include "llvm/Target/TargetMachine.h"
Vikram S. Adve339084b2001-09-18 13:04:24 +000012#include "llvm/Target/MachineSchedInfo.h"
Vikram S. Adve5afff3b2001-11-09 02:15:52 +000013#include "llvm/Target/MachineFrameInfo.h"
14#include "llvm/Target/MachineCacheInfo.h"
Chris Lattner699683c2002-02-04 05:59:25 +000015#include "llvm/Target/MachineRegInfo.h"
Chris Lattnerc6495ee2001-09-14 03:56:45 +000016#include "llvm/Type.h"
Chris Lattner46cbff62001-09-14 16:56:32 +000017#include <sys/types.h>
Chris Lattnerc6495ee2001-09-14 03:56:45 +000018
Chris Lattner4387e312002-02-03 23:42:19 +000019class LiveRange;
Chris Lattnerf6e0e282001-09-14 04:32:55 +000020class UltraSparc;
Chris Lattner4387e312002-02-03 23:42:19 +000021class PhyRegAlloc;
Chris Lattner9aa697b2002-04-09 05:16:36 +000022class Pass;
Chris Lattner4387e312002-02-03 23:42:19 +000023
Chris Lattnerc6495ee2001-09-14 03:56:45 +000024// OpCodeMask definitions for the Sparc V9
25//
26const OpCodeMask Immed = 0x00002000; // immed or reg operand?
27const OpCodeMask Annul = 0x20000000; // annul delay instr?
28const OpCodeMask PredictTaken = 0x00080000; // predict branch taken?
29
30
31enum SparcInstrSchedClass {
32 SPARC_NONE, /* Instructions with no scheduling restrictions */
33 SPARC_IEUN, /* Integer class that can use IEU0 or IEU1 */
34 SPARC_IEU0, /* Integer class IEU0 */
35 SPARC_IEU1, /* Integer class IEU1 */
36 SPARC_FPM, /* FP Multiply or Divide instructions */
37 SPARC_FPA, /* All other FP instructions */
38 SPARC_CTI, /* Control-transfer instructions */
39 SPARC_LD, /* Load instructions */
40 SPARC_ST, /* Store instructions */
41 SPARC_SINGLE, /* Instructions that must issue by themselves */
42
43 SPARC_INV, /* This should stay at the end for the next value */
44 SPARC_NUM_SCHED_CLASSES = SPARC_INV
45};
46
Chris Lattnerc6495ee2001-09-14 03:56:45 +000047
48//---------------------------------------------------------------------------
49// enum SparcMachineOpCode.
50// const MachineInstrDescriptor SparcMachineInstrDesc[]
51//
52// Purpose:
53// Description of UltraSparc machine instructions.
54//
55//---------------------------------------------------------------------------
56
Chris Lattnerc6495ee2001-09-14 03:56:45 +000057enum SparcMachineOpCode {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000058#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
59 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
60 ENUM,
61#include "SparcInstr.def"
Chris Lattnerc6495ee2001-09-14 03:56:45 +000062
Chris Lattnerc6495ee2001-09-14 03:56:45 +000063 // End-of-array marker
64 INVALID_OPCODE,
Vikram S. Advec1521632001-10-22 13:31:53 +000065 NUM_REAL_OPCODES = PHI, // number of valid opcodes
Chris Lattnerc6495ee2001-09-14 03:56:45 +000066 NUM_TOTAL_OPCODES = INVALID_OPCODE
67};
68
Chris Lattnerc6495ee2001-09-14 03:56:45 +000069
Chris Lattner9a3d63b2001-09-19 15:56:23 +000070// Array of machine instruction descriptions...
71extern const MachineInstrDescriptor SparcMachineInstrDesc[];
Chris Lattnerc6495ee2001-09-14 03:56:45 +000072
73
74//---------------------------------------------------------------------------
75// class UltraSparcInstrInfo
76//
77// Purpose:
78// Information about individual instructions.
79// Most information is stored in the SparcMachineInstrDesc array above.
80// Other information is computed on demand, and most such functions
81// default to member functions in base class MachineInstrInfo.
82//---------------------------------------------------------------------------
83
Chris Lattner035dfbe2002-08-09 20:08:06 +000084struct UltraSparcInstrInfo : public MachineInstrInfo {
85 UltraSparcInstrInfo(const TargetMachine& tgt);
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000086
87 //
Vikram S. Advedd558992002-03-18 03:02:42 +000088 // All immediate constants are in position 1 except the
Vikram S. Advee1f72802002-09-16 15:39:26 +000089 // store instructions and SETxx.
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000090 //
Vikram S. Advedd558992002-03-18 03:02:42 +000091 virtual int getImmedConstantPos(MachineOpCode opCode) const {
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +000092 bool ignore;
93 if (this->maxImmedConstant(opCode, ignore) != 0)
94 {
Vikram S. Advefe09fb22002-07-08 23:34:10 +000095 assert(! this->isStore((MachineOpCode) STB - 1)); // 1st store opcode
96 assert(! this->isStore((MachineOpCode) STXFSR+1));// last store opcode
Vikram S. Advee1f72802002-09-16 15:39:26 +000097 if (opCode==SETSW || opCode==SETUW || opCode==SETX || opCode==SETHI)
98 return 0;
99 if (opCode >= STB && opCode <= STXFSR)
100 return 2;
101 return 1;
Vikram S. Adve4c5fe2d2001-11-14 18:48:36 +0000102 }
103 else
104 return -1;
105 }
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000106
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000107 virtual bool hasResultInterlock (MachineOpCode opCode) const
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000108 {
109 // All UltraSPARC instructions have interlocks (note that delay slots
110 // are not considered here).
111 // However, instructions that use the result of an FCMP produce a
112 // 9-cycle stall if they are issued less than 3 cycles after the FCMP.
113 // Force the compiler to insert a software interlock (i.e., gap of
114 // 2 other groups, including NOPs if necessary).
115 return (opCode == FCMPS || opCode == FCMPD || opCode == FCMPQ);
116 }
117
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000118 //-------------------------------------------------------------------------
Vikram S. Advee1f72802002-09-16 15:39:26 +0000119 // Queries about representation of LLVM quantities (e.g., constants)
120 //-------------------------------------------------------------------------
121
122 virtual bool ConstantMayNotFitInImmedField(const Constant* CV,
123 const Instruction* I) const;
124
125 //-------------------------------------------------------------------------
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000126 // Code generation support for creating individual machine instructions
127 //-------------------------------------------------------------------------
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000128
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000129 // Create an instruction sequence to put the constant `val' into
Vikram S. Adve242a8082002-05-19 15:25:51 +0000130 // the virtual register `dest'. `val' may be a Constant or a
131 // GlobalValue, viz., the constant address of a global variable or function.
132 // The generated instructions are returned in `mvec'.
133 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
134 // Any stack space required is allocated via mcff.
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000135 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000136 virtual void CreateCodeToLoadConst(const TargetMachine& target,
137 Function* F,
Vikram S. Advedd558992002-03-18 03:02:42 +0000138 Value* val,
Vikram S. Adve5684c4e2001-10-18 00:02:06 +0000139 Instruction* dest,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000140 std::vector<MachineInstr*>& mvec,
141 MachineCodeForInstruction& mcfi) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000142
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000143 // Create an instruction sequence to copy an integer value `val'
144 // to a floating point value `dest' by copying to memory and back.
145 // val must be an integral type. dest must be a Float or Double.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000146 // The generated instructions are returned in `mvec'.
147 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
148 // Any stack space required is allocated via mcff.
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000149 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000150 virtual void CreateCodeToCopyIntToFloat(const TargetMachine& target,
151 Function* F,
152 Value* val,
153 Instruction* dest,
154 std::vector<MachineInstr*>& mvec,
155 MachineCodeForInstruction& mcfi) const;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000156
157 // Similarly, create an instruction sequence to copy an FP value
158 // `val' to an integer value `dest' by copying to memory and back.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000159 // The generated instructions are returned in `mvec'.
160 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
161 // Any stack space required is allocated via mcff.
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000162 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000163 virtual void CreateCodeToCopyFloatToInt(const TargetMachine& target,
164 Function* F,
165 Value* val,
166 Instruction* dest,
167 std::vector<MachineInstr*>& mvec,
168 MachineCodeForInstruction& mcfi) const;
169
170 // Create instruction(s) to copy src to dest, for arbitrary types
171 // The generated instructions are returned in `mvec'.
172 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
173 // Any stack space required is allocated via mcff.
174 //
Vikram S. Advedd558992002-03-18 03:02:42 +0000175 virtual void CreateCopyInstructionsByType(const TargetMachine& target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000176 Function* F,
177 Value* src,
178 Instruction* dest,
179 std::vector<MachineInstr*>& mvec,
180 MachineCodeForInstruction& mcfi) const;
181
182 // Create instruction sequence to produce a sign-extended register value
183 // from an arbitrary sized value (sized in bits, not bytes).
Vikram S. Advef36f06b2002-09-05 18:34:31 +0000184 // The generated instructions are appended to `mvec'.
185 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000186 // Any stack space required is allocated via mcff.
187 //
188 virtual void CreateSignExtensionInstructions(const TargetMachine& target,
189 Function* F,
Vikram S. Advef36f06b2002-09-05 18:34:31 +0000190 Value* srcVal,
191 unsigned int srcSizeInBits,
192 Value* dest,
193 std::vector<MachineInstr*>& mvec,
194 MachineCodeForInstruction& mcfi) const;
195
196 // Create instruction sequence to produce a zero-extended register value
197 // from an arbitrary sized value (sized in bits, not bytes).
198 // The generated instructions are appended to `mvec'.
199 // Any temp. registers (TmpInstruction) created are recorded in mcfi.
200 // Any stack space required is allocated via mcff.
201 //
202 virtual void CreateZeroExtensionInstructions(const TargetMachine& target,
203 Function* F,
204 Value* srcVal,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000205 unsigned int srcSizeInBits,
206 Value* dest,
207 std::vector<MachineInstr*>& mvec,
208 MachineCodeForInstruction& mcfi) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000209};
210
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000211
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000212//----------------------------------------------------------------------------
213// class UltraSparcRegInfo
214//
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000215// This class implements the virtual class MachineRegInfo for Sparc.
216//
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000217//----------------------------------------------------------------------------
218
Chris Lattner699683c2002-02-04 05:59:25 +0000219class UltraSparcRegInfo : public MachineRegInfo {
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000220 // The actual register classes in the Sparc
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000221 //
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000222 enum RegClassIDs {
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000223 IntRegClassID, // Integer
224 FloatRegClassID, // Float (both single/double)
225 IntCCRegClassID, // Int Condition Code
226 FloatCCRegClassID // Float Condition code
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000227 };
228
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000229
230 // Type of registers available in Sparc. There can be several reg types
231 // in the same class. For instace, the float reg class has Single/Double
232 // types
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000233 //
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000234 enum RegTypes {
235 IntRegType,
236 FPSingleRegType,
237 FPDoubleRegType,
238 IntCCRegType,
239 FloatCCRegType
240 };
241
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000242 // **** WARNING: If the above enum order is changed, also modify
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000243 // getRegisterClassOfValue method below since it assumes this particular
244 // order for efficiency.
245
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000246
247 // reverse pointer to get info about the ultra sparc machine
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000248 //
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000249 const UltraSparc *const UltraSparcInfo;
250
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000251 // Number of registers used for passing int args (usually 6: %o0 - %o5)
252 //
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000253 unsigned const NumOfIntArgRegs;
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000254
255 // Number of registers used for passing float args (usually 32: %f0 - %f31)
256 //
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000257 unsigned const NumOfFloatArgRegs;
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000258
259 // An out of bound register number that can be used to initialize register
260 // numbers. Useful for error detection.
261 //
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000262 int const InvalidRegNum;
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000263
264
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000265 // ======================== Private Methods =============================
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000266
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000267 // The following methods are used to color special live ranges (e.g.
Chris Lattnerf57b8452002-04-27 06:56:12 +0000268 // function args and return values etc.) with specific hardware registers
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000269 // as required. See SparcRegInfo.cpp for the implementation.
270 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000271 void suggestReg4RetAddr(MachineInstr *RetMI,
Chris Lattner699683c2002-02-04 05:59:25 +0000272 LiveRangeInfo &LRI) const;
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000273
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000274 void suggestReg4CallAddr(MachineInstr *CallMI, LiveRangeInfo &LRI,
Chris Lattner697954c2002-01-20 22:54:45 +0000275 std::vector<RegClass *> RCList) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000276
277 void InitializeOutgoingArg(MachineInstr* CallMI, AddedInstrns *CallAI,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000278 PhyRegAlloc &PRA, LiveRange* LR,
279 unsigned regType, unsigned RegClassID,
280 int UniArgReg, unsigned int argNo,
281 std::vector<MachineInstr *>& AddedInstrnsBefore)
282 const;
283
284 // The following 4 methods are used to find the RegType (see enum above)
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000285 // for a reg class and a given primitive type, a LiveRange, a Value,
286 // or a particular machine register.
287 // The fifth function gives the reg class of the given RegType.
288 //
Vikram S. Adve242a8082002-05-19 15:25:51 +0000289 int getRegType(unsigned regClassID, const Type* type) const;
Chris Lattner699683c2002-02-04 05:59:25 +0000290 int getRegType(const LiveRange *LR) const;
291 int getRegType(const Value *Val) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000292 int getRegType(int unifiedRegNum) const;
Ruchira Sasanka3839e6e2001-11-03 19:59:59 +0000293
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000294 // Used to generate a copy instruction based on the register class of
295 // value.
296 //
Chris Lattner699683c2002-02-04 05:59:25 +0000297 MachineInstr *cpValue2RegMI(Value *Val, unsigned DestReg,
298 int RegType) const;
Ruchira Sasankaae4bcd72001-11-10 21:20:43 +0000299
300
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000301 // The following 2 methods are used to order the instructions addeed by
Chris Lattnerf57b8452002-04-27 06:56:12 +0000302 // the register allocator in association with function calling. See
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000303 // SparcRegInfo.cpp for more details
304 //
Chris Lattner697954c2002-01-20 22:54:45 +0000305 void moveInst2OrdVec(std::vector<MachineInstr *> &OrdVec,
306 MachineInstr *UnordInst,
307 PhyRegAlloc &PRA) const;
Ruchira Sasankaae4bcd72001-11-10 21:20:43 +0000308
Chris Lattner697954c2002-01-20 22:54:45 +0000309 void OrderAddedInstrns(std::vector<MachineInstr *> &UnordVec,
310 std::vector<MachineInstr *> &OrdVec,
311 PhyRegAlloc &PRA) const;
Ruchira Sasankaae4bcd72001-11-10 21:20:43 +0000312
313
Vikram S. Adve6d783112002-04-25 04:40:24 +0000314 // Compute which register can be used for an argument, if any
315 //
316 int regNumForIntArg(bool inCallee, bool isVarArgsCall,
317 unsigned argNo, unsigned intArgNo, unsigned fpArgNo,
318 unsigned& regClassId) const;
Ruchira Sasankaae4bcd72001-11-10 21:20:43 +0000319
Vikram S. Adve6d783112002-04-25 04:40:24 +0000320 int regNumForFPArg(unsigned RegType, bool inCallee, bool isVarArgsCall,
321 unsigned argNo, unsigned intArgNo, unsigned fpArgNo,
322 unsigned& regClassId) const;
323
Chris Lattner699683c2002-02-04 05:59:25 +0000324public:
325 UltraSparcRegInfo(const UltraSparc &tgt);
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000326
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000327 // To get complete machine information structure using the machine register
328 // information
329 //
Chris Lattner699683c2002-02-04 05:59:25 +0000330 inline const UltraSparc &getUltraSparcInfo() const {
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000331 return *UltraSparcInfo;
332 }
333
Vikram S. Advedd558992002-03-18 03:02:42 +0000334 // To find the register class used for a specified Type
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000335 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000336 unsigned getRegClassIDOfType(const Type *type,
337 bool isCCReg = false) const;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000338
Vikram S. Advedd558992002-03-18 03:02:42 +0000339 // To find the register class of a Value
340 //
341 inline unsigned getRegClassIDOfValue(const Value *Val,
342 bool isCCReg = false) const {
343 return getRegClassIDOfType(Val->getType(), isCCReg);
344 }
345
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000346 // To find the register class to which a specified register belongs
347 //
348 unsigned getRegClassIDOfReg(int unifiedRegNum) const;
349 unsigned getRegClassIDOfRegType(int regType) const;
Vikram S. Advedd558992002-03-18 03:02:42 +0000350
Chris Lattner699683c2002-02-04 05:59:25 +0000351 // getZeroRegNum - returns the register that contains always zero this is the
352 // unified register number
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000353 //
Chris Lattner699683c2002-02-04 05:59:25 +0000354 virtual int getZeroRegNum() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000355
Chris Lattner699683c2002-02-04 05:59:25 +0000356 // getCallAddressReg - returns the reg used for pushing the address when a
Chris Lattnerf57b8452002-04-27 06:56:12 +0000357 // function is called. This can be used for other purposes between calls
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000358 //
Chris Lattner699683c2002-02-04 05:59:25 +0000359 unsigned getCallAddressReg() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000360
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000361 // Returns the register containing the return address.
362 // It should be made sure that this register contains the return
363 // value when a return instruction is reached.
364 //
Chris Lattner699683c2002-02-04 05:59:25 +0000365 unsigned getReturnAddressReg() const;
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000366
Vikram S. Adve242a8082002-05-19 15:25:51 +0000367 // Number of registers used for passing int args (usually 6: %o0 - %o5)
368 // and float args (usually 32: %f0 - %f31)
369 //
370 unsigned const GetNumOfIntArgRegs() const { return NumOfIntArgRegs; }
371 unsigned const GetNumOfFloatArgRegs() const { return NumOfFloatArgRegs; }
372
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000373 // The following methods are used to color special live ranges (e.g.
Chris Lattnerf57b8452002-04-27 06:56:12 +0000374 // function args and return values etc.) with specific hardware registers
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000375 // as required. See SparcRegInfo.cpp for the implementation for Sparc.
376 //
Chris Lattnerb7653df2002-04-08 22:03:57 +0000377 void suggestRegs4MethodArgs(const Function *Meth,
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000378 LiveRangeInfo& LRI) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000379
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000380 void suggestRegs4CallArgs(MachineInstr *CallMI,
Chris Lattner697954c2002-01-20 22:54:45 +0000381 LiveRangeInfo& LRI,
382 std::vector<RegClass *> RCL) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000383
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000384 void suggestReg4RetValue(MachineInstr *RetMI,
Chris Lattner697954c2002-01-20 22:54:45 +0000385 LiveRangeInfo& LRI) const;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000386
Chris Lattnerb7653df2002-04-08 22:03:57 +0000387 void colorMethodArgs(const Function *Meth, LiveRangeInfo &LRI,
Chris Lattner699683c2002-02-04 05:59:25 +0000388 AddedInstrns *FirstAI) const;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000389
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000390 void colorCallArgs(MachineInstr *CallMI, LiveRangeInfo &LRI,
Chris Lattner699683c2002-02-04 05:59:25 +0000391 AddedInstrns *CallAI, PhyRegAlloc &PRA,
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000392 const BasicBlock *BB) const;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000393
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000394 void colorRetValue(MachineInstr *RetI, LiveRangeInfo& LRI,
Chris Lattner699683c2002-02-04 05:59:25 +0000395 AddedInstrns *RetAI) const;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000396
397
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000398 // method used for printing a register for debugging purposes
399 //
Chris Lattner699683c2002-02-04 05:59:25 +0000400 static void printReg(const LiveRange *LR);
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000401
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000402 // Each register class has a seperate space for register IDs. To convert
403 // a regId in a register class to a common Id, or vice versa,
404 // we use the folloing methods.
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000405 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000406 // This method provides a unique number for each register
407 inline int getUnifiedRegNum(unsigned regClassID, int reg) const {
408
409 if (regClassID == IntRegClassID) {
410 assert(reg < 32 && "Invalid reg. number");
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000411 return reg;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000412 }
413 else if (regClassID == FloatRegClassID) {
414 assert(reg < 64 && "Invalid reg. number");
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000415 return reg + 32; // we have 32 int regs
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000416 }
417 else if (regClassID == FloatCCRegClassID) {
418 assert(reg < 4 && "Invalid reg. number");
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000419 return reg + 32 + 64; // 32 int, 64 float
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000420 }
421 else if (regClassID == IntCCRegClassID ) {
422 assert(reg == 0 && "Invalid reg. number");
423 return reg + 4+ 32 + 64; // only one int CC reg
424 }
425 else if (reg==InvalidRegNum) {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000426 return InvalidRegNum;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000427 }
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000428 else
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000429 assert(0 && "Invalid register class");
Chris Lattner6dad5062001-11-07 13:49:12 +0000430 return 0;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000431 }
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000432
433 // This method converts the unified number to the number in its class,
434 // and returns the class ID in regClassID.
435 inline int getClassRegNum(int ureg, unsigned& regClassID) const {
436 if (ureg < 32) { regClassID = IntRegClassID; return ureg; }
437 else if (ureg < 32+64) { regClassID = FloatRegClassID; return ureg-32; }
438 else if (ureg < 4 +96) { regClassID = FloatCCRegClassID; return ureg-96; }
439 else if (ureg < 1 +100) { regClassID = IntCCRegClassID; return ureg-100;}
440 else if (ureg == InvalidRegNum) { return InvalidRegNum; }
441 else { assert(0 && "Invalid unified register number"); }
Chris Lattnerb82d97e2002-07-25 06:08:32 +0000442 return 0;
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000443 }
444
445 // Returns the assembly-language name of the specified machine register.
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000446 //
Chris Lattner95685682002-08-12 21:25:05 +0000447 virtual const char * const getUnifiedRegName(int reg) const;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000448
449
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000450 // returns the # of bytes of stack space allocated for each register
451 // type. For Sparc, currently we allocate 8 bytes on stack for all
452 // register types. We can optimize this later if necessary to save stack
453 // space (However, should make sure that stack alignment is correct)
454 //
Chris Lattner699683c2002-02-04 05:59:25 +0000455 inline int getSpilledRegSize(int RegType) const {
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000456 return 8;
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000457 }
458
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000459
Vikram S. Advea44c6c02002-03-31 19:04:50 +0000460 // To obtain the return value and the indirect call address (if any)
461 // contained in a CALL machine instruction
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000462 //
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000463 const Value * getCallInstRetVal(const MachineInstr *CallMI) const;
Vikram S. Advea44c6c02002-03-31 19:04:50 +0000464 const Value * getCallInstIndirectAddrVal(const MachineInstr *CallMI) const;
Ruchira Sasankab3b6f532001-10-21 16:43:41 +0000465
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000466 // The following methods are used to generate "copy" machine instructions
467 // for an architecture.
468 //
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000469 // The function regTypeNeedsScratchReg() can be used to check whether a
470 // scratch register is needed to copy a register of type `regType' to
471 // or from memory. If so, such a scratch register can be provided by
472 // the caller (e.g., if it knows which regsiters are free); otherwise
473 // an arbitrary one will be chosen and spilled by the copy instructions.
474 //
475 bool regTypeNeedsScratchReg(int RegType,
476 int& scratchRegClassId) const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000477
Vikram S. Advefe09fb22002-07-08 23:34:10 +0000478 void cpReg2RegMI(std::vector<MachineInstr*>& mvec,
479 unsigned SrcReg, unsigned DestReg,
480 int RegType) const;
481
482 void cpReg2MemMI(std::vector<MachineInstr*>& mvec,
483 unsigned SrcReg, unsigned DestPtrReg,
484 int Offset, int RegType, int scratchReg = -1) const;
485
486 void cpMem2RegMI(std::vector<MachineInstr*>& mvec,
487 unsigned SrcPtrReg, int Offset, unsigned DestReg,
488 int RegType, int scratchReg = -1) const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000489
Vikram S. Adve242a8082002-05-19 15:25:51 +0000490 void cpValue2Value(Value *Src, Value *Dest,
Anand Shuklacfb22d32002-06-25 20:55:50 +0000491 std::vector<MachineInstr*>& mvec) const;
Ruchira Sasankaef1b0cb2001-11-03 17:13:27 +0000492
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000493 // To see whether a register is a volatile (i.e., whehter it must be
494 // preserved acorss calls)
495 //
Chris Lattner699683c2002-02-04 05:59:25 +0000496 inline bool isRegVolatile(int RegClassID, int Reg) const {
497 return MachineRegClassArr[RegClassID]->isRegVolatile(Reg);
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000498 }
499
500
Chris Lattner699683c2002-02-04 05:59:25 +0000501 virtual unsigned getFramePointer() const;
502 virtual unsigned getStackPointer() const;
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000503
Chris Lattner699683c2002-02-04 05:59:25 +0000504 virtual int getInvalidRegNum() const {
Ruchira Sasankac4d4b762001-10-16 01:23:19 +0000505 return InvalidRegNum;
506 }
507
Ruchira Sasanka2563a982002-01-07 20:28:49 +0000508 // This method inserts the caller saving code for call instructions
509 //
Anand Shukla24787fa2002-07-11 00:16:28 +0000510 void insertCallerSavingCode(std::vector<MachineInstr*>& instrnsBefore,
511 std::vector<MachineInstr*>& instrnsAfter,
Vikram S. Adve6a49a1e2002-07-10 21:42:42 +0000512 MachineInstr *MInst,
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000513 const BasicBlock *BB, PhyRegAlloc &PRA ) const;
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000514};
515
516
517
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000518
519//---------------------------------------------------------------------------
520// class UltraSparcSchedInfo
521//
522// Purpose:
523// Interface to instruction scheduling information for UltraSPARC.
524// The parameter values above are based on UltraSPARC IIi.
525//---------------------------------------------------------------------------
526
527
528class UltraSparcSchedInfo: public MachineSchedInfo {
529public:
Chris Lattner699683c2002-02-04 05:59:25 +0000530 UltraSparcSchedInfo(const TargetMachine &tgt);
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000531protected:
Chris Lattner699683c2002-02-04 05:59:25 +0000532 virtual void initializeResources();
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000533};
534
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000535
536//---------------------------------------------------------------------------
Vikram S. Advec1521632001-10-22 13:31:53 +0000537// class UltraSparcFrameInfo
538//
539// Purpose:
540// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000541// Starting offsets for each area of the stack frame are aligned at
542// a multiple of getStackFrameSizeAlignment().
Vikram S. Advec1521632001-10-22 13:31:53 +0000543//---------------------------------------------------------------------------
544
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000545class UltraSparcFrameInfo: public MachineFrameInfo {
Vikram S. Advec1521632001-10-22 13:31:53 +0000546public:
Chris Lattner699683c2002-02-04 05:59:25 +0000547 UltraSparcFrameInfo(const TargetMachine &tgt) : MachineFrameInfo(tgt) {}
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000548
549public:
Vikram S. Advee1f72802002-09-16 15:39:26 +0000550 // These methods provide constant parameters of the frame layout.
551 //
Chris Lattnerf57b8452002-04-27 06:56:12 +0000552 int getStackFrameSizeAlignment() const { return StackFrameSizeAlignment;}
553 int getMinStackFrameSize() const { return MinStackFrameSize; }
554 int getNumFixedOutgoingArgs() const { return NumFixedOutgoingArgs; }
555 int getSizeOfEachArgOnStack() const { return SizeOfEachArgOnStack; }
556 bool argsOnStackHaveFixedSize() const { return true; }
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000557
Vikram S. Advee1f72802002-09-16 15:39:26 +0000558 // This method adjusts a stack offset to meet alignment rules of target.
559 // The fixed OFFSET (0x7ff) must be subtracted and the result aligned.
560 virtual int adjustAlignment (int unalignedOffset,
561 bool growUp,
562 unsigned int align) const {
563 return unalignedOffset + (growUp? +1:-1)*((unalignedOffset-OFFSET) % align);
564 }
565
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000566 // These methods compute offsets using the frame contents for a
Chris Lattnerf57b8452002-04-27 06:56:12 +0000567 // particular function. The frame contents are obtained from the
568 // MachineCodeInfoForMethod object for the given function.
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000569 //
570 int getFirstIncomingArgOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000571 bool& growUp) const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000572 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000573 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000574 return FirstIncomingArgOffsetFromFP;
575 }
576 int getFirstOutgoingArgOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000577 bool& growUp) const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000578 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000579 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000580 return FirstOutgoingArgOffsetFromSP;
581 }
582 int getFirstOptionalOutgoingArgOffset(MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000583 bool& growUp)const
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000584 {
Vikram S. Adve6d783112002-04-25 04:40:24 +0000585 growUp = true; // arguments area grows upwards
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000586 return FirstOptionalOutgoingArgOffsetFromSP;
587 }
588
589 int getFirstAutomaticVarOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000590 bool& growUp) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000591 int getRegSpillAreaOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000592 bool& growUp) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000593 int getTmpAreaOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000594 bool& growUp) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000595 int getDynamicAreaOffset (MachineCodeForMethod& mcInfo,
Vikram S. Adve6d783112002-04-25 04:40:24 +0000596 bool& growUp) const;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000597
598 //
599 // These methods specify the base register used for each stack area
600 // (generally FP or SP)
601 //
602 virtual int getIncomingArgBaseRegNum() const {
603 return (int) target.getRegInfo().getFramePointer();
604 }
605 virtual int getOutgoingArgBaseRegNum() const {
606 return (int) target.getRegInfo().getStackPointer();
607 }
608 virtual int getOptionalOutgoingArgBaseRegNum() const {
609 return (int) target.getRegInfo().getStackPointer();
610 }
611 virtual int getAutomaticVarBaseRegNum() const {
612 return (int) target.getRegInfo().getFramePointer();
613 }
614 virtual int getRegSpillAreaBaseRegNum() const {
615 return (int) target.getRegInfo().getFramePointer();
616 }
617 virtual int getDynamicAreaBaseRegNum() const {
618 return (int) target.getRegInfo().getStackPointer();
619 }
620
621private:
Vikram S. Advee1f72802002-09-16 15:39:26 +0000622 /*----------------------------------------------------------------------
623 This diagram shows the stack frame layout used by llc on Sparc V9.
624 Note that only the location of automatic variables, spill area,
625 temporary storage, and dynamically allocated stack area are chosen
626 by us. The rest conform to the Sparc V9 ABI.
627 All stack addresses are offset by OFFSET = 0x7ff (2047).
628
629 Alignment assumpteions and other invariants:
630 (1) %sp+OFFSET and %fp+OFFSET are always aligned on 16-byte boundary
631 (2) Variables in automatic, spill, temporary, or dynamic regions
632 are aligned according to their size as in all memory accesses.
633 (3) Everything below the dynamically allocated stack area is only used
634 during a call to another function, so it is never needed when
635 the current function is active. This is why space can be allocated
636 dynamically by incrementing %sp any time within the function.
637
638 STACK FRAME LAYOUT:
639
640 ...
641 %fp+OFFSET+176 Optional extra incoming arguments# 1..N
642 %fp+OFFSET+168 Incoming argument #6
643 ... ...
644 %fp+OFFSET+128 Incoming argument #1
645 ... ...
646 ---%fp+OFFSET-0--------Bottom of caller's stack frame--------------------
647 %fp+OFFSET-8 Automatic variables <-- ****TOP OF STACK FRAME****
648 Spill area
649 Temporary storage
650 ...
651
652 %sp+OFFSET+176+8N Bottom of dynamically allocated stack area
653 %sp+OFFSET+168+8N Optional extra outgoing argument# N
654 ... ...
655 %sp+OFFSET+176 Optional extra outgoing argument# 1
656 %sp+OFFSET+168 Outgoing argument #6
657 ... ...
658 %sp+OFFSET+128 Outgoing argument #1
659 %sp+OFFSET+120 Save area for %i7
660 ... ...
661 %sp+OFFSET+0 Save area for %l0 <-- ****BOTTOM OF STACK FRAME****
662
663 *----------------------------------------------------------------------*/
664
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000665 // All stack addresses must be offset by 0x7ff (2047) on Sparc V9.
666 static const int OFFSET = (int) 0x7ff;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000667 static const int StackFrameSizeAlignment = 16;
Vikram S. Advec1521632001-10-22 13:31:53 +0000668 static const int MinStackFrameSize = 176;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000669 static const int NumFixedOutgoingArgs = 6;
670 static const int SizeOfEachArgOnStack = 8;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000671 static const int FirstIncomingArgOffsetFromFP = 128 + OFFSET;
672 static const int FirstOptionalIncomingArgOffsetFromFP = 176 + OFFSET;
Vikram S. Advee1f72802002-09-16 15:39:26 +0000673 static const int StaticAreaOffsetFromFP = 0 + OFFSET;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000674 static const int FirstOutgoingArgOffsetFromSP = 128 + OFFSET;
675 static const int FirstOptionalOutgoingArgOffsetFromSP = 176 + OFFSET;
Vikram S. Advec1521632001-10-22 13:31:53 +0000676};
677
678
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000679//---------------------------------------------------------------------------
680// class UltraSparcCacheInfo
681//
682// Purpose:
683// Interface to cache parameters for the UltraSPARC.
684// Just use defaults for now.
685//---------------------------------------------------------------------------
686
687class UltraSparcCacheInfo: public MachineCacheInfo {
688public:
Chris Lattner7327d7e2002-02-04 00:04:35 +0000689 UltraSparcCacheInfo(const TargetMachine &T) : MachineCacheInfo(T) {}
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000690};
691
Vikram S. Advec1521632001-10-22 13:31:53 +0000692
693//---------------------------------------------------------------------------
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000694// class UltraSparcMachine
695//
696// Purpose:
697// Primary interface to machine description for the UltraSPARC.
698// Primarily just initializes machine-dependent parameters in
699// class TargetMachine, and creates machine-dependent subclasses
Vikram S. Adve339084b2001-09-18 13:04:24 +0000700// for classes such as InstrInfo, SchedInfo and RegInfo.
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000701//---------------------------------------------------------------------------
702
703class UltraSparc : public TargetMachine {
Vikram S. Adve339084b2001-09-18 13:04:24 +0000704private:
705 UltraSparcInstrInfo instrInfo;
706 UltraSparcSchedInfo schedInfo;
707 UltraSparcRegInfo regInfo;
Vikram S. Advec1521632001-10-22 13:31:53 +0000708 UltraSparcFrameInfo frameInfo;
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000709 UltraSparcCacheInfo cacheInfo;
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000710public:
711 UltraSparc();
Vikram S. Adve339084b2001-09-18 13:04:24 +0000712
Chris Lattner32f600a2001-09-19 13:47:12 +0000713 virtual const MachineInstrInfo &getInstrInfo() const { return instrInfo; }
714 virtual const MachineSchedInfo &getSchedInfo() const { return schedInfo; }
715 virtual const MachineRegInfo &getRegInfo() const { return regInfo; }
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000716 virtual const MachineFrameInfo &getFrameInfo() const { return frameInfo; }
Vikram S. Adve5afff3b2001-11-09 02:15:52 +0000717 virtual const MachineCacheInfo &getCacheInfo() const { return cacheInfo; }
Chris Lattner32f600a2001-09-19 13:47:12 +0000718
Vikram S. Advee1f72802002-09-16 15:39:26 +0000719 // getPrologEpilogCodeInserter - Inserts prolog/epilog code.
720 virtual Pass* getPrologEpilogInsertionPass();
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000721
Vikram S. Advee1f72802002-09-16 15:39:26 +0000722 // getFunctionAsmPrinterPass - Writes out machine code for a single function
723 virtual Pass* getFunctionAsmPrinterPass(std::ostream &Out);
724
725 // getModuleAsmPrinterPass - Writes generated machine code to assembly file.
726 virtual Pass* getModuleAsmPrinterPass(std::ostream &Out);
727
728 // getEmitBytecodeToAsmPass - Emits final LLVM bytecode to assembly file.
729 virtual Pass* getEmitBytecodeToAsmPass(std::ostream &Out);
Chris Lattner6edfcc52002-02-03 07:51:17 +0000730};
Chris Lattnerf6e0e282001-09-14 04:32:55 +0000731
Chris Lattnerc6495ee2001-09-14 03:56:45 +0000732#endif