blob: d390c73ebebcac2e365ccef5a274a0a7a71d286c [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 Lattner0feb3582002-02-03 23:41:51 +000021#include "llvm/PassManager.h"
Chris Lattner697954c2002-01-20 22:54:45 +000022#include <iostream>
23using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000024
Chris Lattner9a3d63b2001-09-19 15:56:23 +000025// Build the MachineInstruction Description Array...
26const MachineInstrDescriptor SparcMachineInstrDesc[] = {
27#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
28 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
29 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
30 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
31#include "SparcInstr.def"
32};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000033
34//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000035// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
36// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000037//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000038//
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000039
Chris Lattner46cbff62001-09-14 16:56:32 +000040TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
42
Vikram S. Adve0fb49802001-09-18 13:01:29 +000043//----------------------------------------------------------------------------
44// Entry point for register allocation for a module
45//----------------------------------------------------------------------------
46
Chris Lattner0feb3582002-02-03 23:41:51 +000047class RegisterAllocation : public MethodPass {
48 TargetMachine &Target;
49public:
50 inline RegisterAllocation(TargetMachine &T) : Target(T) {}
51 bool runOnMethod(Method *M) {
52 if (DEBUG_RA)
53 cerr << "\n******************** Method "<< M->getName()
54 << " ********************\n";
Vikram S. Adve0fb49802001-09-18 13:01:29 +000055
Chris Lattner0feb3582002-02-03 23:41:51 +000056 MethodLiveVarInfo LVI(M ); // Analyze live varaibles
57 LVI.analyze();
Vikram S. Adve0fb49802001-09-18 13:01:29 +000058
Chris Lattner0feb3582002-02-03 23:41:51 +000059 PhyRegAlloc PRA(M, Target, &LVI); // allocate registers
60 PRA.allocateRegisters();
Vikram S. Adve0fb49802001-09-18 13:01:29 +000061
Chris Lattner0feb3582002-02-03 23:41:51 +000062 if (DEBUG_RA) cerr << "\nRegister allocation complete!\n";
63 return false;
64 }
65};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000066
Chris Lattner0feb3582002-02-03 23:41:51 +000067static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +000068
Vikram S. Adve9db43182001-10-22 13:44:23 +000069//---------------------------------------------------------------------------
Chris Lattner0feb3582002-02-03 23:41:51 +000070// class InsertPrologEpilogCode
71//
72// Insert SAVE/RESTORE instructions for the method
73//
Vikram S. Adve9db43182001-10-22 13:44:23 +000074// 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.
Chris Lattner0feb3582002-02-03 23:41:51 +000078//
Vikram S. Adve9db43182001-10-22 13:44:23 +000079//---------------------------------------------------------------------------
80
Chris Lattner0feb3582002-02-03 23:41:51 +000081class InsertPrologEpilogCode : public MethodPass {
82 TargetMachine &Target;
83public:
84 inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
85 bool runOnMethod(Method *M) {
86 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(M);
87 if (!mcodeInfo.isCompiledAsLeafMethod()) {
88 InsertPrologCode(M);
89 InsertEpilogCode(M);
90 }
91 return false;
92 }
Vikram S. Adve9db43182001-10-22 13:44:23 +000093
Chris Lattner0feb3582002-02-03 23:41:51 +000094 void InsertPrologCode(Method *M);
95 void InsertEpilogCode(Method *M);
96};
97
98void InsertPrologEpilogCode::InsertPrologCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000099{
100 BasicBlock* entryBB = method->getEntryNode();
Chris Lattner0feb3582002-02-03 23:41:51 +0000101 unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +0000102 assert(N <= MAX_INSTR_PER_VMINSTR);
Chris Lattner0feb3582002-02-03 23:41:51 +0000103 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
104 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
Vikram S. Adve9db43182001-10-22 13:44:23 +0000105}
106
107
Chris Lattner0feb3582002-02-03 23:41:51 +0000108void InsertPrologEpilogCode::InsertEpilogCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +0000109{
110 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
111 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
112 {
113 BasicBlock* exitBB = *I;
Chris Lattner0feb3582002-02-03 23:41:51 +0000114 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +0000115
116 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +0000117 MachineCodeForInstruction &termMvec =
118 MachineCodeForInstruction::get(exitBB->getTerminator());
Vikram S. Adve9db43182001-10-22 13:44:23 +0000119
120 // Remove the NOPs in the delay slots of the return instruction
Chris Lattner0feb3582002-02-03 23:41:51 +0000121 const MachineInstrInfo &mii = Target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +0000122 unsigned numNOPs = 0;
123 while (termMvec.back()->getOpCode() == NOP)
124 {
125 assert( termMvec.back() == bbMvec.back());
126 termMvec.pop_back();
127 bbMvec.pop_back();
128 ++numNOPs;
129 }
130 assert(termMvec.back() == bbMvec.back());
131
132 // Check that we found the right number of NOPs and have the right
133 // number of instructions to replace them.
134 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
135 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
136 assert(N == ndelays && "Cannot use epilog code for delay slots?");
137
138 // Append the epilog code to the end of the basic block.
139 bbMvec.push_back(minstrVec[0]);
140 }
141}
142
143
Chris Lattner0feb3582002-02-03 23:41:51 +0000144
Vikram S. Adve9db43182001-10-22 13:44:23 +0000145
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000146
Chris Lattner20b1ea02001-09-14 03:47:57 +0000147//---------------------------------------------------------------------------
Chris Lattner20b1ea02001-09-14 03:47:57 +0000148// class UltraSparcSchedInfo
149//
150// Purpose:
151// Scheduling information for the UltraSPARC.
152// Primarily just initializes machine-dependent parameters in
153// class MachineSchedInfo.
154//---------------------------------------------------------------------------
155
156/*ctor*/
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000157UltraSparcSchedInfo::UltraSparcSchedInfo(const TargetMachine& tgt)
158 : MachineSchedInfo(tgt,
159 (unsigned int) SPARC_NUM_SCHED_CLASSES,
Chris Lattner20b1ea02001-09-14 03:47:57 +0000160 SparcRUsageDesc,
161 SparcInstrUsageDeltas,
162 SparcInstrIssueDeltas,
163 sizeof(SparcInstrUsageDeltas)/sizeof(InstrRUsageDelta),
164 sizeof(SparcInstrIssueDeltas)/sizeof(InstrIssueDelta))
165{
166 maxNumIssueTotal = 4;
167 longestIssueConflict = 0; // computed from issuesGaps[]
168
169 branchMispredictPenalty = 4; // 4 for SPARC IIi
170 branchTargetUnknownPenalty = 2; // 2 for SPARC IIi
171 l1DCacheMissPenalty = 8; // 7 or 9 for SPARC IIi
172 l1ICacheMissPenalty = 8; // ? for SPARC IIi
173
174 inOrderLoads = true; // true for SPARC IIi
175 inOrderIssue = true; // true for SPARC IIi
176 inOrderExec = false; // false for most architectures
177 inOrderRetire= true; // true for most architectures
178
179 // must be called after above parameters are initialized.
180 this->initializeResources();
181}
182
183void
184UltraSparcSchedInfo::initializeResources()
185{
186 // Compute MachineSchedInfo::instrRUsages and MachineSchedInfo::issueGaps
187 MachineSchedInfo::initializeResources();
188
189 // Machine-dependent fixups go here. None for now.
190}
191
192
Vikram S. Adve9db43182001-10-22 13:44:23 +0000193//---------------------------------------------------------------------------
194// class UltraSparcFrameInfo
195//
196// Purpose:
197// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000198// Starting offsets for each area of the stack frame are aligned at
199// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000200//---------------------------------------------------------------------------
201
202int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000203UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
204 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000205{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000206 pos = false; // static stack area grows downwards
207 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000208}
209
210int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000211UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
212 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000213{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000214 pos = false; // static stack area grows downwards
215 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000216 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
217 autoVarsSize += (getStackFrameSizeAlignment() - mod);
218 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000219}
220
221int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000222UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
223 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000224{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000225 pos = false; // static stack area grows downwards
226 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
227 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000228 int offset = autoVarsSize + spillAreaSize;
229 if (int mod = offset % getStackFrameSizeAlignment())
230 offset += (getStackFrameSizeAlignment() - mod);
231 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000232}
233
234int
235UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
236 bool& pos) const
237{
238 // dynamic stack area grows downwards starting at top of opt-args area
239 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000240 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
241 assert(offset % getStackFrameSizeAlignment() == 0);
242 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000243}
244
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000245
Chris Lattner20b1ea02001-09-14 03:47:57 +0000246//---------------------------------------------------------------------------
247// class UltraSparcMachine
248//
249// Purpose:
250// Primary interface to machine description for the UltraSPARC.
251// Primarily just initializes machine-dependent parameters in
252// class TargetMachine, and creates machine-dependent subclasses
253// for classes such as MachineInstrInfo.
254//
255//---------------------------------------------------------------------------
256
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000257UltraSparc::UltraSparc()
258 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000259 instrInfo(*this),
260 schedInfo(*this),
261 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000262 frameInfo(*this),
263 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000264{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000265 optSizeForSubWordData = 4;
266 minMemOpWordSize = 8;
267 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000268}
269
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000270
Chris Lattner0feb3582002-02-03 23:41:51 +0000271
272//===---------------------------------------------------------------------===//
273// GenerateCodeForTarget Pass
274//
275// Native code generation for a specified target.
276//===---------------------------------------------------------------------===//
277
278class ConstructMachineCodeForMethod : public MethodPass {
279 TargetMachine &Target;
280public:
281 inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {}
282 bool runOnMethod(Method *M) {
283 MachineCodeForMethod::construct(M, Target);
284 return false;
285 }
286};
287
288class InstructionSelection : public MethodPass {
289 TargetMachine &Target;
290public:
291 inline InstructionSelection(TargetMachine &T) : Target(T) {}
292 bool runOnMethod(Method *M) {
293 if (SelectInstructionsForMethod(M, Target))
294 cerr << "Instr selection failed for method " << M->getName() << "\n";
295 return false;
296 }
297};
298
299class InstructionScheduling : public MethodPass {
300 TargetMachine &Target;
301public:
302 inline InstructionScheduling(TargetMachine &T) : Target(T) {}
303 bool runOnMethod(Method *M) {
304 if (ScheduleInstructionsWithSSA(M, Target))
305 cerr << "Instr scheduling failed for method " << M->getName() << "\n\n";
306 return false;
307 }
308};
309
310struct FreeMachineCodeForMethod : public MethodPass {
311 static void freeMachineCode(Instruction *I) {
312 MachineCodeForInstruction::destroy(I);
313 }
314
315 bool runOnMethod(Method *M) {
316 for_each(M->inst_begin(), M->inst_end(), freeMachineCode);
317 // Don't destruct MachineCodeForMethod - The global printer needs it
318 //MachineCodeForMethod::destruct(M);
319 return false;
320 }
321};
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000322
323
Chris Lattner0feb3582002-02-03 23:41:51 +0000324void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000325 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattner0feb3582002-02-03 23:41:51 +0000326 PM.add(new ConstructMachineCodeForMethod(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000327
Chris Lattner0feb3582002-02-03 23:41:51 +0000328 PM.add(new InstructionSelection(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000329
Chris Lattner0feb3582002-02-03 23:41:51 +0000330 //PM.add(new InstructionScheduling(*this));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000331
Chris Lattner0feb3582002-02-03 23:41:51 +0000332 PM.add(new RegisterAllocation(*this));
333
334 //PM.add(new OptimizeLeafProcedures());
335 //PM.add(new DeleteFallThroughBranches());
336 //PM.add(new RemoveChainedBranches()); // should be folded with previous
337 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
338
339 PM.add(new InsertPrologEpilogCode(*this));
340
341 // Output assembly language to the .s file. Assembly emission is split into
342 // two parts: Method output and Global value output. This is because method
343 // output is pipelined with all of the rest of code generation stuff,
344 // allowing machine code representations for methods to be free'd after the
345 // method has been emitted.
346 //
347 PM.add(getMethodAsmPrinterPass(PM, Out));
348 PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed
Chris Lattnercf4525b2002-02-03 07:49:15 +0000349
Chris Lattner0feb3582002-02-03 23:41:51 +0000350 // Emit Module level assembly after all of the methods have been processed.
351 PM.add(getModuleAsmPrinterPass(PM, Out));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000352}