blob: a457abeb1ca341cac4a6022703aa657a2f8d2d90 [file] [log] [blame]
Vikram S. Adve0fb49802001-09-18 13:01:29 +00001// $Id$
Chris Lattner20b1ea02001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// Sparc.cpp
5//
6// Purpose:
7//
8// History:
9// 7/15/01 - Vikram Adve - Created
10//**************************************************************************/
11
Vikram S. Adve9db43182001-10-22 13:44:23 +000012
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "SparcInternals.h"
Vikram S. Adve9db43182001-10-22 13:44:23 +000014#include "llvm/Target/Sparc.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000015#include "llvm/CodeGen/InstrScheduling.h"
16#include "llvm/CodeGen/InstrSelection.h"
Chris Lattnercf4525b2002-02-03 07:49:15 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
18#include "llvm/CodeGen/MachineCodeForMethod.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000019#include "llvm/CodeGen/PhyRegAlloc.h"
Vikram S. Adve9db43182001-10-22 13:44:23 +000020#include "llvm/Method.h"
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000023
Chris Lattner9a3d63b2001-09-19 15:56:23 +000024// Build the MachineInstruction Description Array...
25const MachineInstrDescriptor SparcMachineInstrDesc[] = {
26#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
27 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
28 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
30#include "SparcInstr.def"
31};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000032
33//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000034// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
35// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000036//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000037//
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000038
Chris Lattner46cbff62001-09-14 16:56:32 +000039TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
Chris Lattner20b1ea02001-09-14 03:47:57 +000040
41
Vikram S. Adve0fb49802001-09-18 13:01:29 +000042//----------------------------------------------------------------------------
43// Entry point for register allocation for a module
44//----------------------------------------------------------------------------
45
Vikram S. Adve9db43182001-10-22 13:44:23 +000046void AllocateRegisters(Method *M, TargetMachine &target)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000047{
48
49 if ( (M)->isExternal() ) // don't process prototypes
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000050 return;
Vikram S. Adve0fb49802001-09-18 13:01:29 +000051
Chris Lattner697954c2002-01-20 22:54:45 +000052 if( DEBUG_RA )
53 cerr << "\n******************** Method "<< M->getName()
54 << " ********************\n";
Vikram S. Adve0fb49802001-09-18 13:01:29 +000055
56 MethodLiveVarInfo LVI(M ); // Analyze live varaibles
57 LVI.analyze();
58
59
Vikram S. Adve9db43182001-10-22 13:44:23 +000060 PhyRegAlloc PRA(M, target, &LVI); // allocate registers
Vikram S. Adve0fb49802001-09-18 13:01:29 +000061 PRA.allocateRegisters();
62
63
Chris Lattner697954c2002-01-20 22:54:45 +000064 if( DEBUG_RA ) cerr << "\nRegister allocation complete!\n";
Vikram S. Adve0fb49802001-09-18 13:01:29 +000065
Vikram S. Adve0fb49802001-09-18 13:01:29 +000066}
67
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000068
Vikram S. Adve9db43182001-10-22 13:44:23 +000069//---------------------------------------------------------------------------
70// Function InsertPrologCode
71// Function InsertEpilogCode
72// Function InsertPrologEpilog
73//
74// Insert prolog code at the unique method entry point.
75// Insert epilog code at each method exit point.
76// InsertPrologEpilog invokes these only if the method is not compiled
77// with the leaf method optimization.
78//---------------------------------------------------------------------------
79
80static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
81
82static void
83InsertPrologCode(Method* method, TargetMachine& target)
84{
85 BasicBlock* entryBB = method->getEntryNode();
86 unsigned N = GetInstructionsForProlog(entryBB, target, minstrVec);
87 assert(N <= MAX_INSTR_PER_VMINSTR);
88 if (N > 0)
89 {
90 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
91 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
92 }
93}
94
95
96static void
97InsertEpilogCode(Method* method, TargetMachine& target)
98{
99 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
100 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
101 {
102 BasicBlock* exitBB = *I;
103 unsigned N = GetInstructionsForEpilog(exitBB, target, minstrVec);
104
105 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +0000106 MachineCodeForInstruction &termMvec =
107 MachineCodeForInstruction::get(exitBB->getTerminator());
Vikram S. Adve9db43182001-10-22 13:44:23 +0000108
109 // Remove the NOPs in the delay slots of the return instruction
Chris Lattnercf4525b2002-02-03 07:49:15 +0000110 const MachineInstrInfo &mii = target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +0000111 unsigned numNOPs = 0;
112 while (termMvec.back()->getOpCode() == NOP)
113 {
114 assert( termMvec.back() == bbMvec.back());
115 termMvec.pop_back();
116 bbMvec.pop_back();
117 ++numNOPs;
118 }
119 assert(termMvec.back() == bbMvec.back());
120
121 // Check that we found the right number of NOPs and have the right
122 // number of instructions to replace them.
123 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
124 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
125 assert(N == ndelays && "Cannot use epilog code for delay slots?");
126
127 // Append the epilog code to the end of the basic block.
128 bbMvec.push_back(minstrVec[0]);
129 }
130}
131
132
133// Insert SAVE/RESTORE instructions for the method
134static void
135InsertPrologEpilog(Method *method, TargetMachine &target)
136{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000137 MachineCodeForMethod& mcodeInfo = MachineCodeForMethod::get(method);
Vikram S. Adve9db43182001-10-22 13:44:23 +0000138 if (mcodeInfo.isCompiledAsLeafMethod())
139 return; // nothing to do
140
141 InsertPrologCode(method, target);
142 InsertEpilogCode(method, target);
143}
144
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000145
Chris Lattner20b1ea02001-09-14 03:47:57 +0000146//---------------------------------------------------------------------------
Chris Lattner20b1ea02001-09-14 03:47:57 +0000147// class UltraSparcSchedInfo
148//
149// Purpose:
150// Scheduling information for the UltraSPARC.
151// Primarily just initializes machine-dependent parameters in
152// class MachineSchedInfo.
153//---------------------------------------------------------------------------
154
155/*ctor*/
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000156UltraSparcSchedInfo::UltraSparcSchedInfo(const TargetMachine& tgt)
157 : MachineSchedInfo(tgt,
158 (unsigned int) SPARC_NUM_SCHED_CLASSES,
Chris Lattner20b1ea02001-09-14 03:47:57 +0000159 SparcRUsageDesc,
160 SparcInstrUsageDeltas,
161 SparcInstrIssueDeltas,
162 sizeof(SparcInstrUsageDeltas)/sizeof(InstrRUsageDelta),
163 sizeof(SparcInstrIssueDeltas)/sizeof(InstrIssueDelta))
164{
165 maxNumIssueTotal = 4;
166 longestIssueConflict = 0; // computed from issuesGaps[]
167
168 branchMispredictPenalty = 4; // 4 for SPARC IIi
169 branchTargetUnknownPenalty = 2; // 2 for SPARC IIi
170 l1DCacheMissPenalty = 8; // 7 or 9 for SPARC IIi
171 l1ICacheMissPenalty = 8; // ? for SPARC IIi
172
173 inOrderLoads = true; // true for SPARC IIi
174 inOrderIssue = true; // true for SPARC IIi
175 inOrderExec = false; // false for most architectures
176 inOrderRetire= true; // true for most architectures
177
178 // must be called after above parameters are initialized.
179 this->initializeResources();
180}
181
182void
183UltraSparcSchedInfo::initializeResources()
184{
185 // Compute MachineSchedInfo::instrRUsages and MachineSchedInfo::issueGaps
186 MachineSchedInfo::initializeResources();
187
188 // Machine-dependent fixups go here. None for now.
189}
190
191
Vikram S. Adve9db43182001-10-22 13:44:23 +0000192//---------------------------------------------------------------------------
193// class UltraSparcFrameInfo
194//
195// Purpose:
196// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000197// Starting offsets for each area of the stack frame are aligned at
198// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000199//---------------------------------------------------------------------------
200
201int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000202UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
203 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000204{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000205 pos = false; // static stack area grows downwards
206 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000207}
208
209int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000210UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
211 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000212{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000213 pos = false; // static stack area grows downwards
214 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000215 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
216 autoVarsSize += (getStackFrameSizeAlignment() - mod);
217 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000218}
219
220int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000221UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
222 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000223{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000224 pos = false; // static stack area grows downwards
225 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
226 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000227 int offset = autoVarsSize + spillAreaSize;
228 if (int mod = offset % getStackFrameSizeAlignment())
229 offset += (getStackFrameSizeAlignment() - mod);
230 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000231}
232
233int
234UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
235 bool& pos) const
236{
237 // dynamic stack area grows downwards starting at top of opt-args area
238 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000239 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
240 assert(offset % getStackFrameSizeAlignment() == 0);
241 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000242}
243
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000244
Chris Lattner20b1ea02001-09-14 03:47:57 +0000245//---------------------------------------------------------------------------
246// class UltraSparcMachine
247//
248// Purpose:
249// Primary interface to machine description for the UltraSPARC.
250// Primarily just initializes machine-dependent parameters in
251// class TargetMachine, and creates machine-dependent subclasses
252// for classes such as MachineInstrInfo.
253//
254//---------------------------------------------------------------------------
255
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000256UltraSparc::UltraSparc()
257 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000258 instrInfo(*this),
259 schedInfo(*this),
260 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000261 frameInfo(*this),
262 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000263{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264 optSizeForSubWordData = 4;
265 minMemOpWordSize = 8;
266 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267}
268
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000269
Vikram S. Adve9db43182001-10-22 13:44:23 +0000270void
271ApplyPeepholeOptimizations(Method *method, TargetMachine &target)
272{
273 return;
274
275 // OptimizeLeafProcedures();
276 // DeleteFallThroughBranches();
277 // RemoveChainedBranches(); // should be folded with previous
278 // RemoveRedundantOps(); // operations with %g0, NOP, etc.
279}
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000280
281
282
Vikram S. Adve9db43182001-10-22 13:44:23 +0000283bool
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000284UltraSparc::compileMethod(Method *method)
Vikram S. Adve9db43182001-10-22 13:44:23 +0000285{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000286 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattnercf4525b2002-02-03 07:49:15 +0000287 MachineCodeForMethod::construct(method, *this);
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000288
289 if (SelectInstructionsForMethod(method, *this))
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000290 {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000291 cerr << "Instruction selection failed for method " << method->getName()
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000292 << "\n\n";
293 return true;
294 }
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000295
296 /*
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000297 if (ScheduleInstructionsWithSSA(method, *this))
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000298 {
299 cerr << "Instruction scheduling before allocation failed for method "
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000300 << method->getName() << "\n\n";
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000301 return true;
302 }
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000303 */
Chris Lattner20b1ea02001-09-14 03:47:57 +0000304
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000305
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000306 AllocateRegisters(method, *this); // allocate registers
Vikram S. Adve9db43182001-10-22 13:44:23 +0000307
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000308 ApplyPeepholeOptimizations(method, *this); // machine-dependent peephole opts
Vikram S. Adve9db43182001-10-22 13:44:23 +0000309
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000310 InsertPrologEpilog(method, *this);
Vikram S. Adve9db43182001-10-22 13:44:23 +0000311
Chris Lattner20b1ea02001-09-14 03:47:57 +0000312 return false;
313}
Chris Lattnercf4525b2002-02-03 07:49:15 +0000314
315static void freeMachineCode(Instruction *I) {
316 MachineCodeForInstruction::destroy(I);
317}
318
319//
320// freeCompiledMethod - Release all memory associated with the compiled image
321// for this method.
322//
323void
324UltraSparc::freeCompiledMethod(Method *M)
325{
326 for_each(M->inst_begin(), M->inst_end(), freeMachineCode);
327 // Don't destruct MachineCodeForMethod - The global printer needs it
328 //MachineCodeForMethod::destruct(M);
329}