blob: c855a463219e5e723cdde334c7381be5a33d8be0 [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"
Chris Lattner6dd98a62002-02-04 00:33:08 +000019#include "llvm/CodeGen/RegisterAllocation.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. Adve9db43182001-10-22 13:44:23 +000043//---------------------------------------------------------------------------
Chris Lattner0feb3582002-02-03 23:41:51 +000044// class InsertPrologEpilogCode
45//
46// Insert SAVE/RESTORE instructions for the method
47//
Vikram S. Adve9db43182001-10-22 13:44:23 +000048// Insert prolog code at the unique method entry point.
49// Insert epilog code at each method exit point.
50// InsertPrologEpilog invokes these only if the method is not compiled
51// with the leaf method optimization.
Chris Lattner0feb3582002-02-03 23:41:51 +000052//
Vikram S. Adve9db43182001-10-22 13:44:23 +000053//---------------------------------------------------------------------------
Chris Lattner6dd98a62002-02-04 00:33:08 +000054static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
Vikram S. Adve9db43182001-10-22 13:44:23 +000055
Chris Lattner0feb3582002-02-03 23:41:51 +000056class InsertPrologEpilogCode : public MethodPass {
57 TargetMachine &Target;
58public:
59 inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
60 bool runOnMethod(Method *M) {
61 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(M);
62 if (!mcodeInfo.isCompiledAsLeafMethod()) {
63 InsertPrologCode(M);
64 InsertEpilogCode(M);
65 }
66 return false;
67 }
Vikram S. Adve9db43182001-10-22 13:44:23 +000068
Chris Lattner0feb3582002-02-03 23:41:51 +000069 void InsertPrologCode(Method *M);
70 void InsertEpilogCode(Method *M);
71};
72
73void InsertPrologEpilogCode::InsertPrologCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000074{
75 BasicBlock* entryBB = method->getEntryNode();
Chris Lattner0feb3582002-02-03 23:41:51 +000076 unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000077 assert(N <= MAX_INSTR_PER_VMINSTR);
Chris Lattner0feb3582002-02-03 23:41:51 +000078 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
79 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
Vikram S. Adve9db43182001-10-22 13:44:23 +000080}
81
82
Chris Lattner0feb3582002-02-03 23:41:51 +000083void InsertPrologEpilogCode::InsertEpilogCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000084{
85 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
86 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
87 {
88 BasicBlock* exitBB = *I;
Chris Lattner0feb3582002-02-03 23:41:51 +000089 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000090
91 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +000092 MachineCodeForInstruction &termMvec =
93 MachineCodeForInstruction::get(exitBB->getTerminator());
Vikram S. Adve9db43182001-10-22 13:44:23 +000094
95 // Remove the NOPs in the delay slots of the return instruction
Chris Lattner0feb3582002-02-03 23:41:51 +000096 const MachineInstrInfo &mii = Target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +000097 unsigned numNOPs = 0;
98 while (termMvec.back()->getOpCode() == NOP)
99 {
100 assert( termMvec.back() == bbMvec.back());
101 termMvec.pop_back();
102 bbMvec.pop_back();
103 ++numNOPs;
104 }
105 assert(termMvec.back() == bbMvec.back());
106
107 // Check that we found the right number of NOPs and have the right
108 // number of instructions to replace them.
109 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
110 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
111 assert(N == ndelays && "Cannot use epilog code for delay slots?");
112
113 // Append the epilog code to the end of the basic block.
114 bbMvec.push_back(minstrVec[0]);
115 }
116}
117
118
Vikram S. Adve9db43182001-10-22 13:44:23 +0000119//---------------------------------------------------------------------------
120// class UltraSparcFrameInfo
121//
122// Purpose:
123// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000124// Starting offsets for each area of the stack frame are aligned at
125// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000126//---------------------------------------------------------------------------
127
128int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000129UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
130 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000131{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000132 pos = false; // static stack area grows downwards
133 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000134}
135
136int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000137UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
138 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000139{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000140 pos = false; // static stack area grows downwards
141 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000142 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
143 autoVarsSize += (getStackFrameSizeAlignment() - mod);
144 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000145}
146
147int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000148UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
149 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000150{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000151 pos = false; // static stack area grows downwards
152 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
153 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000154 int offset = autoVarsSize + spillAreaSize;
155 if (int mod = offset % getStackFrameSizeAlignment())
156 offset += (getStackFrameSizeAlignment() - mod);
157 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000158}
159
160int
161UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
162 bool& pos) const
163{
164 // dynamic stack area grows downwards starting at top of opt-args area
165 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000166 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
167 assert(offset % getStackFrameSizeAlignment() == 0);
168 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000169}
170
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000171
Chris Lattner20b1ea02001-09-14 03:47:57 +0000172//---------------------------------------------------------------------------
173// class UltraSparcMachine
174//
175// Purpose:
176// Primary interface to machine description for the UltraSPARC.
177// Primarily just initializes machine-dependent parameters in
178// class TargetMachine, and creates machine-dependent subclasses
179// for classes such as MachineInstrInfo.
180//
181//---------------------------------------------------------------------------
182
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000183UltraSparc::UltraSparc()
184 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000185 instrInfo(*this),
186 schedInfo(*this),
187 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000188 frameInfo(*this),
189 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000190{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000191 optSizeForSubWordData = 4;
192 minMemOpWordSize = 8;
193 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000194}
195
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000196
Chris Lattner0feb3582002-02-03 23:41:51 +0000197
198//===---------------------------------------------------------------------===//
199// GenerateCodeForTarget Pass
200//
201// Native code generation for a specified target.
202//===---------------------------------------------------------------------===//
203
204class ConstructMachineCodeForMethod : public MethodPass {
205 TargetMachine &Target;
206public:
207 inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {}
208 bool runOnMethod(Method *M) {
209 MachineCodeForMethod::construct(M, Target);
210 return false;
211 }
212};
213
214class InstructionSelection : public MethodPass {
215 TargetMachine &Target;
216public:
217 inline InstructionSelection(TargetMachine &T) : Target(T) {}
218 bool runOnMethod(Method *M) {
219 if (SelectInstructionsForMethod(M, Target))
220 cerr << "Instr selection failed for method " << M->getName() << "\n";
221 return false;
222 }
223};
224
225class InstructionScheduling : public MethodPass {
226 TargetMachine &Target;
227public:
228 inline InstructionScheduling(TargetMachine &T) : Target(T) {}
229 bool runOnMethod(Method *M) {
230 if (ScheduleInstructionsWithSSA(M, Target))
231 cerr << "Instr scheduling failed for method " << M->getName() << "\n\n";
232 return false;
233 }
234};
235
236struct FreeMachineCodeForMethod : public MethodPass {
237 static void freeMachineCode(Instruction *I) {
238 MachineCodeForInstruction::destroy(I);
239 }
240
241 bool runOnMethod(Method *M) {
242 for_each(M->inst_begin(), M->inst_end(), freeMachineCode);
243 // Don't destruct MachineCodeForMethod - The global printer needs it
244 //MachineCodeForMethod::destruct(M);
245 return false;
246 }
247};
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000248
249
Chris Lattner6b04e712002-02-04 00:39:14 +0000250
251// addPassesToEmitAssembly - This method controls the entire code generation
252// process for the ultra sparc.
253//
Chris Lattner0feb3582002-02-03 23:41:51 +0000254void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000255 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattner0feb3582002-02-03 23:41:51 +0000256 PM.add(new ConstructMachineCodeForMethod(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000257
Chris Lattner0feb3582002-02-03 23:41:51 +0000258 PM.add(new InstructionSelection(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000259
Chris Lattner0feb3582002-02-03 23:41:51 +0000260 //PM.add(new InstructionScheduling(*this));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000261
Chris Lattner0feb3582002-02-03 23:41:51 +0000262 PM.add(new RegisterAllocation(*this));
263
264 //PM.add(new OptimizeLeafProcedures());
265 //PM.add(new DeleteFallThroughBranches());
266 //PM.add(new RemoveChainedBranches()); // should be folded with previous
267 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
268
269 PM.add(new InsertPrologEpilogCode(*this));
270
271 // Output assembly language to the .s file. Assembly emission is split into
272 // two parts: Method output and Global value output. This is because method
273 // output is pipelined with all of the rest of code generation stuff,
274 // allowing machine code representations for methods to be free'd after the
275 // method has been emitted.
276 //
277 PM.add(getMethodAsmPrinterPass(PM, Out));
278 PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed
Chris Lattnercf4525b2002-02-03 07:49:15 +0000279
Chris Lattner0feb3582002-02-03 23:41:51 +0000280 // Emit Module level assembly after all of the methods have been processed.
281 PM.add(getModuleAsmPrinterPass(PM, Out));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000282}