blob: b4e069453cc43f1a183736359b3c9fde5643967c [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 Lattner0feb3582002-02-03 23:41:51 +000022#include "llvm/PassManager.h"
Chris Lattner697954c2002-01-20 22:54:45 +000023#include <iostream>
24using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000025
Chris Lattner9a3d63b2001-09-19 15:56:23 +000026// Build the MachineInstruction Description Array...
27const MachineInstrDescriptor SparcMachineInstrDesc[] = {
28#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
30 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
31 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
32#include "SparcInstr.def"
33};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000034
35//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000036// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
37// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000038//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000039//
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000040
Chris Lattner46cbff62001-09-14 16:56:32 +000041TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
Chris Lattner20b1ea02001-09-14 03:47:57 +000042
43
Vikram S. Adve9db43182001-10-22 13:44:23 +000044//---------------------------------------------------------------------------
Chris Lattner0feb3582002-02-03 23:41:51 +000045// class InsertPrologEpilogCode
46//
47// Insert SAVE/RESTORE instructions for the method
48//
Vikram S. Adve9db43182001-10-22 13:44:23 +000049// Insert prolog code at the unique method entry point.
50// Insert epilog code at each method exit point.
51// InsertPrologEpilog invokes these only if the method is not compiled
52// with the leaf method optimization.
Chris Lattner0feb3582002-02-03 23:41:51 +000053//
Vikram S. Adve9db43182001-10-22 13:44:23 +000054//---------------------------------------------------------------------------
Chris Lattner6dd98a62002-02-04 00:33:08 +000055static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
Vikram S. Adve9db43182001-10-22 13:44:23 +000056
Chris Lattner0feb3582002-02-03 23:41:51 +000057class InsertPrologEpilogCode : public MethodPass {
58 TargetMachine &Target;
59public:
60 inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
61 bool runOnMethod(Method *M) {
62 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(M);
63 if (!mcodeInfo.isCompiledAsLeafMethod()) {
64 InsertPrologCode(M);
65 InsertEpilogCode(M);
66 }
67 return false;
68 }
Vikram S. Adve9db43182001-10-22 13:44:23 +000069
Chris Lattner0feb3582002-02-03 23:41:51 +000070 void InsertPrologCode(Method *M);
71 void InsertEpilogCode(Method *M);
72};
73
74void InsertPrologEpilogCode::InsertPrologCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000075{
76 BasicBlock* entryBB = method->getEntryNode();
Chris Lattner0feb3582002-02-03 23:41:51 +000077 unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000078 assert(N <= MAX_INSTR_PER_VMINSTR);
Chris Lattner0feb3582002-02-03 23:41:51 +000079 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
80 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
Vikram S. Adve9db43182001-10-22 13:44:23 +000081}
82
83
Chris Lattner0feb3582002-02-03 23:41:51 +000084void InsertPrologEpilogCode::InsertEpilogCode(Method* method)
Vikram S. Adve9db43182001-10-22 13:44:23 +000085{
86 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
87 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
88 {
89 BasicBlock* exitBB = *I;
Chris Lattner0feb3582002-02-03 23:41:51 +000090 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
Vikram S. Adve9db43182001-10-22 13:44:23 +000091
92 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
Chris Lattnercf4525b2002-02-03 07:49:15 +000093 MachineCodeForInstruction &termMvec =
94 MachineCodeForInstruction::get(exitBB->getTerminator());
Vikram S. Adve9db43182001-10-22 13:44:23 +000095
96 // Remove the NOPs in the delay slots of the return instruction
Chris Lattner0feb3582002-02-03 23:41:51 +000097 const MachineInstrInfo &mii = Target.getInstrInfo();
Vikram S. Adve9db43182001-10-22 13:44:23 +000098 unsigned numNOPs = 0;
99 while (termMvec.back()->getOpCode() == NOP)
100 {
101 assert( termMvec.back() == bbMvec.back());
102 termMvec.pop_back();
103 bbMvec.pop_back();
104 ++numNOPs;
105 }
106 assert(termMvec.back() == bbMvec.back());
107
108 // Check that we found the right number of NOPs and have the right
109 // number of instructions to replace them.
110 unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode());
111 assert(numNOPs == ndelays && "Missing NOPs in delay slots?");
112 assert(N == ndelays && "Cannot use epilog code for delay slots?");
113
114 // Append the epilog code to the end of the basic block.
115 bbMvec.push_back(minstrVec[0]);
116 }
117}
118
119
Vikram S. Adve9db43182001-10-22 13:44:23 +0000120//---------------------------------------------------------------------------
121// class UltraSparcFrameInfo
122//
123// Purpose:
124// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +0000125// Starting offsets for each area of the stack frame are aligned at
126// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +0000127//---------------------------------------------------------------------------
128
129int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000130UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
131 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000132{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000133 pos = false; // static stack area grows downwards
134 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000135}
136
137int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000138UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
139 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000140{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000141 pos = false; // static stack area grows downwards
142 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000143 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
144 autoVarsSize += (getStackFrameSizeAlignment() - mod);
145 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000146}
147
148int
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000149UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
150 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000151{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000152 pos = false; // static stack area grows downwards
153 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
154 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000155 int offset = autoVarsSize + spillAreaSize;
156 if (int mod = offset % getStackFrameSizeAlignment())
157 offset += (getStackFrameSizeAlignment() - mod);
158 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000159}
160
161int
162UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
163 bool& pos) const
164{
165 // dynamic stack area grows downwards starting at top of opt-args area
166 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000167 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
168 assert(offset % getStackFrameSizeAlignment() == 0);
169 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000170}
171
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000172
Chris Lattner20b1ea02001-09-14 03:47:57 +0000173//---------------------------------------------------------------------------
174// class UltraSparcMachine
175//
176// Purpose:
177// Primary interface to machine description for the UltraSPARC.
178// Primarily just initializes machine-dependent parameters in
179// class TargetMachine, and creates machine-dependent subclasses
180// for classes such as MachineInstrInfo.
181//
182//---------------------------------------------------------------------------
183
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000184UltraSparc::UltraSparc()
185 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000186 instrInfo(*this),
187 schedInfo(*this),
188 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000189 frameInfo(*this),
190 cacheInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000191{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000192 optSizeForSubWordData = 4;
193 minMemOpWordSize = 8;
194 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000195}
196
Ruchira Sasankae38bd5332001-09-15 00:30:44 +0000197
Chris Lattner0feb3582002-02-03 23:41:51 +0000198
199//===---------------------------------------------------------------------===//
200// GenerateCodeForTarget Pass
201//
202// Native code generation for a specified target.
203//===---------------------------------------------------------------------===//
204
205class ConstructMachineCodeForMethod : public MethodPass {
206 TargetMachine &Target;
207public:
208 inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {}
209 bool runOnMethod(Method *M) {
210 MachineCodeForMethod::construct(M, Target);
211 return false;
212 }
213};
214
215class InstructionSelection : public MethodPass {
216 TargetMachine &Target;
217public:
218 inline InstructionSelection(TargetMachine &T) : Target(T) {}
219 bool runOnMethod(Method *M) {
220 if (SelectInstructionsForMethod(M, Target))
221 cerr << "Instr selection failed for method " << M->getName() << "\n";
222 return false;
223 }
224};
225
226class InstructionScheduling : public MethodPass {
227 TargetMachine &Target;
228public:
229 inline InstructionScheduling(TargetMachine &T) : Target(T) {}
230 bool runOnMethod(Method *M) {
231 if (ScheduleInstructionsWithSSA(M, Target))
232 cerr << "Instr scheduling failed for method " << M->getName() << "\n\n";
233 return false;
234 }
235};
236
237struct FreeMachineCodeForMethod : public MethodPass {
238 static void freeMachineCode(Instruction *I) {
239 MachineCodeForInstruction::destroy(I);
240 }
241
242 bool runOnMethod(Method *M) {
243 for_each(M->inst_begin(), M->inst_end(), freeMachineCode);
244 // Don't destruct MachineCodeForMethod - The global printer needs it
245 //MachineCodeForMethod::destruct(M);
246 return false;
247 }
248};
Ruchira Sasanka89fb46b2001-09-18 22:52:44 +0000249
250
Chris Lattner6b04e712002-02-04 00:39:14 +0000251
252// addPassesToEmitAssembly - This method controls the entire code generation
253// process for the ultra sparc.
254//
Chris Lattner0feb3582002-02-03 23:41:51 +0000255void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000256 // Construct and initialize the MachineCodeForMethod object for this method.
Chris Lattner0feb3582002-02-03 23:41:51 +0000257 PM.add(new ConstructMachineCodeForMethod(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000258
Chris Lattner0feb3582002-02-03 23:41:51 +0000259 PM.add(new InstructionSelection(*this));
Ruchira Sasankad00982a2002-01-07 19:20:28 +0000260
Chris Lattner0feb3582002-02-03 23:41:51 +0000261 //PM.add(new InstructionScheduling(*this));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000262
Chris Lattner0feb3582002-02-03 23:41:51 +0000263 PM.add(new RegisterAllocation(*this));
264
265 //PM.add(new OptimizeLeafProcedures());
266 //PM.add(new DeleteFallThroughBranches());
267 //PM.add(new RemoveChainedBranches()); // should be folded with previous
268 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
269
270 PM.add(new InsertPrologEpilogCode(*this));
271
272 // Output assembly language to the .s file. Assembly emission is split into
273 // two parts: Method output and Global value output. This is because method
274 // output is pipelined with all of the rest of code generation stuff,
275 // allowing machine code representations for methods to be free'd after the
276 // method has been emitted.
277 //
278 PM.add(getMethodAsmPrinterPass(PM, Out));
279 PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed
Chris Lattnercf4525b2002-02-03 07:49:15 +0000280
Chris Lattner0feb3582002-02-03 23:41:51 +0000281 // Emit Module level assembly after all of the methods have been processed.
282 PM.add(getModuleAsmPrinterPass(PM, Out));
Chris Lattnercf4525b2002-02-03 07:49:15 +0000283}