blob: a4556d161d0187a1baa92a18346f9bb995db64dd [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"
Chris Lattner699683c2002-02-04 05:59:25 +000020#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve9db43182001-10-22 13:44:23 +000021#include "llvm/Method.h"
Chris Lattner221d6882002-02-12 21:07:25 +000022#include "llvm/BasicBlock.h"
Chris Lattner0feb3582002-02-03 23:41:51 +000023#include "llvm/PassManager.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024#include <iostream>
25using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000026
Chris Lattner9a3d63b2001-09-19 15:56:23 +000027// Build the MachineInstruction Description Array...
28const MachineInstrDescriptor SparcMachineInstrDesc[] = {
29#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
30 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
31 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
32 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
33#include "SparcInstr.def"
34};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000035
36//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000037// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
38// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000039//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000040//
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000041
Chris Lattner46cbff62001-09-14 16:56:32 +000042TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
Chris Lattner20b1ea02001-09-14 03:47:57 +000043
44
Vikram S. Adve9db43182001-10-22 13:44:23 +000045//---------------------------------------------------------------------------
Chris Lattner0feb3582002-02-03 23:41:51 +000046// class InsertPrologEpilogCode
47//
48// Insert SAVE/RESTORE instructions for the method
49//
Vikram S. Adve9db43182001-10-22 13:44:23 +000050// Insert prolog code at the unique method entry point.
51// Insert epilog code at each method exit point.
52// InsertPrologEpilog invokes these only if the method is not compiled
53// with the leaf method optimization.
Chris Lattner0feb3582002-02-03 23:41:51 +000054//
Vikram S. Adve9db43182001-10-22 13:44:23 +000055//---------------------------------------------------------------------------
Chris Lattner6dd98a62002-02-04 00:33:08 +000056static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
Vikram S. Adve9db43182001-10-22 13:44:23 +000057
Chris Lattner0feb3582002-02-03 23:41:51 +000058class InsertPrologEpilogCode : public MethodPass {
59 TargetMachine &Target;
60public:
61 inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
62 bool runOnMethod(Method *M) {
63 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(M);
64 if (!mcodeInfo.isCompiledAsLeafMethod()) {
65 InsertPrologCode(M);
66 InsertEpilogCode(M);
67 }
68 return false;
69 }
Vikram S. Adve9db43182001-10-22 13:44:23 +000070
Chris Lattner0feb3582002-02-03 23:41:51 +000071 void InsertPrologCode(Method *M);
72 void InsertEpilogCode(Method *M);
73};
74
75void InsertPrologEpilogCode::InsertPrologCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000076{
77 BasicBlock* entryBB = method->getEntryNode();
Chris Lattner0feb3582002-02-03 23:41:51 +000078 unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000079 assert(N <= MAX_INSTR_PER_VMINSTR);
Chris Lattner0feb3582002-02-03 23:41:51 +000080 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
81 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
Vikram S. Adve9db43182001-10-22 13:44:23 +000082}
83
84
Chris Lattner0feb3582002-02-03 23:41:51 +000085void InsertPrologEpilogCode::InsertEpilogCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000086{
Chris Lattner455889a2002-02-12 22:39:50 +000087 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I) {
88 Instruction *TermInst = (Instruction*)(*I)->getTerminator();
89 if (TermInst->getOpcode() == Instruction::Ret)
Vikram S. Adve9db43182001-10-22 13:44:23 +000090 {
91 BasicBlock* exitBB = *I;
Chris Lattner0feb3582002-02-03 23:41:51 +000092 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000093
94 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +000095 MachineCodeForInstruction &termMvec =
Chris Lattner455889a2002-02-12 22:39:50 +000096 MachineCodeForInstruction::get(TermInst);
Vikram S. Adve9db43182001-10-22 13:44:23 +000097
98 // Remove the NOPs in the delay slots of the return instruction
Chris Lattner0feb3582002-02-03 23:41:51 +000099 const MachineInstrInfo &mii = Target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +0000100 unsigned numNOPs = 0;
101 while (termMvec.back()->getOpCode() == NOP)
102 {
103 assert( termMvec.back() == bbMvec.back());
104 termMvec.pop_back();
105 bbMvec.pop_back();
106 ++numNOPs;
107 }
108 assert(termMvec.back() == bbMvec.back());
109
110 // Check that we found the right number of NOPs and have the right
111 // number of instructions to replace them.
112 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
113 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
114 assert(N == ndelays && "Cannot use epilog code for delay slots?");
115
116 // Append the epilog code to the end of the basic block.
117 bbMvec.push_back(minstrVec[0]);
118 }
Chris Lattner455889a2002-02-12 22:39:50 +0000119 }
Vikram S. Adve9db43182001-10-22 13:44:23 +0000120}
121
122
Vikram S. Adve9db43182001-10-22 13:44:23 +0000123//---------------------------------------------------------------------------
124// class UltraSparcFrameInfo
125//
126// Purpose:
127// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000128// Starting offsets for each area of the stack frame are aligned at
129// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000130//---------------------------------------------------------------------------
131
132int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000133UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
134 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000135{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000136 pos = false; // static stack area grows downwards
137 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000138}
139
140int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000141UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
142 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000143{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000144 pos = false; // static stack area grows downwards
145 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000146 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
147 autoVarsSize += (getStackFrameSizeAlignment() - mod);
148 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000149}
150
151int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000152UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
153 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000154{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000155 pos = false; // static stack area grows downwards
156 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
157 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000158 int offset = autoVarsSize + spillAreaSize;
159 if (int mod = offset % getStackFrameSizeAlignment())
160 offset += (getStackFrameSizeAlignment() - mod);
161 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000162}
163
164int
165UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
166 bool& pos) const
167{
168 // dynamic stack area grows downwards starting at top of opt-args area
169 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000170 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
171 assert(offset % getStackFrameSizeAlignment() == 0);
172 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000173}
174
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000175
Chris Lattner20b1ea02001-09-14 03:47:57 +0000176//---------------------------------------------------------------------------
177// class UltraSparcMachine
178//
179// Purpose:
180// Primary interface to machine description for the UltraSPARC.
181// Primarily just initializes machine-dependent parameters in
182// class TargetMachine, and creates machine-dependent subclasses
183// for classes such as MachineInstrInfo.
184//
185//---------------------------------------------------------------------------
186
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000187UltraSparc::UltraSparc()
188 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000189 instrInfo(*this),
190 schedInfo(*this),
191 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000192 frameInfo(*this),
193 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000194{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000195 optSizeForSubWordData = 4;
196 minMemOpWordSize = 8;
197 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000198}
199
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000200
Chris Lattner0feb3582002-02-03 23:41:51 +0000201
202//===---------------------------------------------------------------------===//
203// GenerateCodeForTarget Pass
204//
205// Native code generation for a specified target.
206//===---------------------------------------------------------------------===//
207
208class ConstructMachineCodeForMethod : public MethodPass {
209 TargetMachine &Target;
210public:
211 inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {}
212 bool runOnMethod(Method *M) {
213 MachineCodeForMethod::construct(M, Target);
214 return false;
215 }
216};
217
218class InstructionSelection : public MethodPass {
219 TargetMachine &Target;
220public:
221 inline InstructionSelection(TargetMachine &T) : Target(T) {}
222 bool runOnMethod(Method *M) {
223 if (SelectInstructionsForMethod(M, Target))
224 cerr << "Instr selection failed for method " << M->getName() << "\n";
225 return false;
226 }
227};
228
Chris Lattner0feb3582002-02-03 23:41:51 +0000229struct FreeMachineCodeForMethod : public MethodPass {
230 static void freeMachineCode(Instruction *I) {
231 MachineCodeForInstruction::destroy(I);
232 }
233
234 bool runOnMethod(Method *M) {
Chris Lattner221d6882002-02-12 21:07:25 +0000235 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
236 for (BasicBlock::iterator I = (*MI)->begin(), E = (*MI)->end();
237 I != E; ++I)
238 freeMachineCode(*I);
239
Chris Lattner0feb3582002-02-03 23:41:51 +0000240 // Don't destruct MachineCodeForMethod - The global printer needs it
241 //MachineCodeForMethod::destruct(M);
242 return false;
243 }
244};
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000245
246
Chris Lattner6b04e712002-02-04 00:39:14 +0000247
248// addPassesToEmitAssembly - This method controls the entire code generation
249// process for the ultra sparc.
250//
Chris Lattner0feb3582002-02-03 23:41:51 +0000251void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000252 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattner0feb3582002-02-03 23:41:51 +0000253 PM.add(new ConstructMachineCodeForMethod(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000254
Chris Lattner0feb3582002-02-03 23:41:51 +0000255 PM.add(new InstructionSelection(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000256
Chris Lattner5ff562e2002-02-04 20:03:43 +0000257 //PM.add(createInstructionSchedulingWithSSAPass(*this));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000258
Chris Lattner2f9b28e2002-02-04 15:54:09 +0000259 PM.add(getRegisterAllocator(*this));
Chris Lattner0feb3582002-02-03 23:41:51 +0000260
261 //PM.add(new OptimizeLeafProcedures());
262 //PM.add(new DeleteFallThroughBranches());
263 //PM.add(new RemoveChainedBranches()); // should be folded with previous
264 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
265
266 PM.add(new InsertPrologEpilogCode(*this));
267
268 // Output assembly language to the .s file. Assembly emission is split into
269 // two parts: Method output and Global value output. This is because method
270 // output is pipelined with all of the rest of code generation stuff,
271 // allowing machine code representations for methods to be free'd after the
272 // method has been emitted.
273 //
274 PM.add(getMethodAsmPrinterPass(PM, Out));
275 PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed
Chris Lattnercf4525b2002-02-03 07:49:15 +0000276
Chris Lattner0feb3582002-02-03 23:41:51 +0000277 // Emit Module level assembly after all of the methods have been processed.
278 PM.add(getModuleAsmPrinterPass(PM, Out));
Chris Lattner9530a6f2002-02-11 22:35:46 +0000279
280 // Emit bytecode to the sparc assembly file into its special section next
281 PM.add(getEmitBytecodeToAsmPass(Out));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000282}