blob: b026ccd67a0d58fd21558cdf2b7991b5e6865ecc [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{
87 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
88 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
89 {
90 BasicBlock* exitBB = *I;
Chris Lattner0feb3582002-02-03 23:41:51 +000091 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000092
93 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +000094 MachineCodeForInstruction &termMvec =
95 MachineCodeForInstruction::get(exitBB->getTerminator());
Vikram S. Adve9db43182001-10-22 13:44:23 +000096
97 // Remove the NOPs in the delay slots of the return instruction
Chris Lattner0feb3582002-02-03 23:41:51 +000098 const MachineInstrInfo &mii = Target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +000099 unsigned numNOPs = 0;
100 while (termMvec.back()->getOpCode() == NOP)
101 {
102 assert( termMvec.back() == bbMvec.back());
103 termMvec.pop_back();
104 bbMvec.pop_back();
105 ++numNOPs;
106 }
107 assert(termMvec.back() == bbMvec.back());
108
109 // Check that we found the right number of NOPs and have the right
110 // number of instructions to replace them.
111 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
112 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
113 assert(N == ndelays && "Cannot use epilog code for delay slots?");
114
115 // Append the epilog code to the end of the basic block.
116 bbMvec.push_back(minstrVec[0]);
117 }
118}
119
120
Vikram S. Adve9db43182001-10-22 13:44:23 +0000121//---------------------------------------------------------------------------
122// class UltraSparcFrameInfo
123//
124// Purpose:
125// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000126// Starting offsets for each area of the stack frame are aligned at
127// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000128//---------------------------------------------------------------------------
129
130int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000131UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
132 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000133{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000134 pos = false; // static stack area grows downwards
135 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000136}
137
138int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000139UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
140 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000141{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000142 pos = false; // static stack area grows downwards
143 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000144 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
145 autoVarsSize += (getStackFrameSizeAlignment() - mod);
146 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000147}
148
149int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000150UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
151 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000152{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000153 pos = false; // static stack area grows downwards
154 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
155 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000156 int offset = autoVarsSize + spillAreaSize;
157 if (int mod = offset % getStackFrameSizeAlignment())
158 offset += (getStackFrameSizeAlignment() - mod);
159 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000160}
161
162int
163UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
164 bool& pos) const
165{
166 // dynamic stack area grows downwards starting at top of opt-args area
167 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000168 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
169 assert(offset % getStackFrameSizeAlignment() == 0);
170 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000171}
172
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000173
Chris Lattner20b1ea02001-09-14 03:47:57 +0000174//---------------------------------------------------------------------------
175// class UltraSparcMachine
176//
177// Purpose:
178// Primary interface to machine description for the UltraSPARC.
179// Primarily just initializes machine-dependent parameters in
180// class TargetMachine, and creates machine-dependent subclasses
181// for classes such as MachineInstrInfo.
182//
183//---------------------------------------------------------------------------
184
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000185UltraSparc::UltraSparc()
186 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000187 instrInfo(*this),
188 schedInfo(*this),
189 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000190 frameInfo(*this),
191 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000192{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000193 optSizeForSubWordData = 4;
194 minMemOpWordSize = 8;
195 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000196}
197
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000198
Chris Lattner0feb3582002-02-03 23:41:51 +0000199
200//===---------------------------------------------------------------------===//
201// GenerateCodeForTarget Pass
202//
203// Native code generation for a specified target.
204//===---------------------------------------------------------------------===//
205
206class ConstructMachineCodeForMethod : public MethodPass {
207 TargetMachine &Target;
208public:
209 inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {}
210 bool runOnMethod(Method *M) {
211 MachineCodeForMethod::construct(M, Target);
212 return false;
213 }
214};
215
216class InstructionSelection : public MethodPass {
217 TargetMachine &Target;
218public:
219 inline InstructionSelection(TargetMachine &T) : Target(T) {}
220 bool runOnMethod(Method *M) {
221 if (SelectInstructionsForMethod(M, Target))
222 cerr << "Instr selection failed for method " << M->getName() << "\n";
223 return false;
224 }
225};
226
Chris Lattner0feb3582002-02-03 23:41:51 +0000227struct FreeMachineCodeForMethod : public MethodPass {
228 static void freeMachineCode(Instruction *I) {
229 MachineCodeForInstruction::destroy(I);
230 }
231
232 bool runOnMethod(Method *M) {
Chris Lattner221d6882002-02-12 21:07:25 +0000233 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
234 for (BasicBlock::iterator I = (*MI)->begin(), E = (*MI)->end();
235 I != E; ++I)
236 freeMachineCode(*I);
237
Chris Lattner0feb3582002-02-03 23:41:51 +0000238 // Don't destruct MachineCodeForMethod - The global printer needs it
239 //MachineCodeForMethod::destruct(M);
240 return false;
241 }
242};
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000243
244
Chris Lattner6b04e712002-02-04 00:39:14 +0000245
246// addPassesToEmitAssembly - This method controls the entire code generation
247// process for the ultra sparc.
248//
Chris Lattner0feb3582002-02-03 23:41:51 +0000249void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000250 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattner0feb3582002-02-03 23:41:51 +0000251 PM.add(new ConstructMachineCodeForMethod(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000252
Chris Lattner0feb3582002-02-03 23:41:51 +0000253 PM.add(new InstructionSelection(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000254
Chris Lattner5ff562e2002-02-04 20:03:43 +0000255 //PM.add(createInstructionSchedulingWithSSAPass(*this));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000256
Chris Lattner2f9b28e2002-02-04 15:54:09 +0000257 PM.add(getRegisterAllocator(*this));
Chris Lattner0feb3582002-02-03 23:41:51 +0000258
259 //PM.add(new OptimizeLeafProcedures());
260 //PM.add(new DeleteFallThroughBranches());
261 //PM.add(new RemoveChainedBranches()); // should be folded with previous
262 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
263
264 PM.add(new InsertPrologEpilogCode(*this));
265
266 // Output assembly language to the .s file. Assembly emission is split into
267 // two parts: Method output and Global value output. This is because method
268 // output is pipelined with all of the rest of code generation stuff,
269 // allowing machine code representations for methods to be free'd after the
270 // method has been emitted.
271 //
272 PM.add(getMethodAsmPrinterPass(PM, Out));
273 PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed
Chris Lattnercf4525b2002-02-03 07:49:15 +0000274
Chris Lattner0feb3582002-02-03 23:41:51 +0000275 // Emit Module level assembly after all of the methods have been processed.
276 PM.add(getModuleAsmPrinterPass(PM, Out));
Chris Lattner9530a6f2002-02-11 22:35:46 +0000277
278 // Emit bytecode to the sparc assembly file into its special section next
279 PM.add(getEmitBytecodeToAsmPass(Out));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000280}