blob: e79906de9ab2c71b87861b522438f665a85fd850 [file] [log] [blame]
Chris Lattner1d21f3e2002-04-09 05:21:26 +00001//===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
2//
3// This file contains the code for the Sparc Target that does not fit in any of
4// the other files in this directory.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Adve9db43182001-10-22 13:44:23 +00007
Chris Lattner20b1ea02001-09-14 03:47:57 +00008#include "SparcInternals.h"
Chris Lattner600e6992002-10-29 20:47:26 +00009#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattner4f946372002-10-28 01:03:43 +000011#include "llvm/PassManager.h"
12#include "llvm/Transforms/Scalar.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner78d881d2002-12-28 20:17:43 +000014#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner4f946372002-10-28 01:03:43 +000015#include "llvm/CodeGen/PreSelection.h"
16#include "llvm/CodeGen/StackSlots.h"
17#include "llvm/CodeGen/PeepholeOpts.h"
18#include "llvm/CodeGen/InstrSelection.h"
19#include "llvm/CodeGen/InstrScheduling.h"
20#include "llvm/CodeGen/RegisterAllocation.h"
21#include "llvm/CodeGen/MachineCodeForInstruction.h"
22#include "llvm/Reoptimizer/Mapping/MappingInfo.h"
23#include "llvm/Reoptimizer/Mapping/FInfo.h"
24#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000026
Chris Lattner6af20402002-12-03 05:41:54 +000027static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000028// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000029const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000030#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
31 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
32 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000033 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
34 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000035#include "SparcInstr.def"
36};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000037
Chris Lattner4f946372002-10-28 01:03:43 +000038//---------------------------------------------------------------------------
39// Command line options to control choice of code generation passes.
40//---------------------------------------------------------------------------
41
42static cl::opt<bool> DisablePreSelect("nopreselect",
43 cl::desc("Disable preselection pass"));
44
45static cl::opt<bool> DisableSched("nosched",
46 cl::desc("Disable local scheduling pass"));
47
48static cl::opt<bool> DisablePeephole("nopeephole",
49 cl::desc("Disable peephole optimization pass"));
50
Vikram S. Adve0fb49802001-09-18 13:01:29 +000051//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000052// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
53// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000054//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000055
Misha Brukman86172ab2003-05-27 22:24:48 +000056TargetMachine *allocateSparcTargetMachine(unsigned Configuration) {
57 return new UltraSparc();
58}
Vikram S. Adve9db43182001-10-22 13:44:23 +000059
Vikram S. Adve9db43182001-10-22 13:44:23 +000060//---------------------------------------------------------------------------
61// class UltraSparcFrameInfo
62//
Vikram S. Adve9db43182001-10-22 13:44:23 +000063// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000064// Starting offsets for each area of the stack frame are aligned at
65// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000066//---------------------------------------------------------------------------
67
68int
Misha Brukmanfce11432002-10-28 00:28:31 +000069UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000070 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000071{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000072 pos = false; // static stack area grows downwards
73 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000074}
75
76int
Misha Brukmanfce11432002-10-28 00:28:31 +000077UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000078 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000079{
Chris Lattner78d881d2002-12-28 20:17:43 +000080 // ensure no more auto vars are added
81 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000082
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000083 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000084 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000085 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000086}
87
88int
Misha Brukmanfce11432002-10-28 00:28:31 +000089UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000090 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000091{
Chris Lattner78d881d2002-12-28 20:17:43 +000092 MachineFunctionInfo *MFI = mcInfo.getInfo();
93 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
94 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +000095
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000096 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000097 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
98 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000099 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000100 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000101}
102
103int
Misha Brukmanfce11432002-10-28 00:28:31 +0000104UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000105 bool& pos) const
106{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000107 // Dynamic stack area grows downwards starting at top of opt-args area.
108 // The opt-args, required-args, and register-save areas are empty except
109 // during calls and traps, so they are shifted downwards on each
110 // dynamic-size alloca.
111 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000112 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000113 if (int extra = optArgsSize % getStackFrameSizeAlignment())
114 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000115 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000116 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000117 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000118}
119
Chris Lattner20b1ea02001-09-14 03:47:57 +0000120//---------------------------------------------------------------------------
121// class UltraSparcMachine
122//
123// Purpose:
124// Primary interface to machine description for the UltraSPARC.
125// Primarily just initializes machine-dependent parameters in
126// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000127// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000128//
129//---------------------------------------------------------------------------
130
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000131UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000132 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000133 schedInfo(*this),
134 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000135 frameInfo(*this),
Vikram S. Adved55697c2002-09-20 00:52:09 +0000136 cacheInfo(*this),
Chris Lattnerc56406c2002-10-29 21:48:17 +0000137 optInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000138}
139
Chris Lattner4f946372002-10-28 01:03:43 +0000140
141// addPassesToEmitAssembly - This method controls the entire code generation
142// process for the ultra sparc.
143//
Chris Lattner63342052002-10-29 21:12:46 +0000144bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000145{
Chris Lattner155e68f2003-04-23 16:24:55 +0000146 // FIXME: implement the switch instruction in the instruction selector.
147 PM.add(createLowerSwitchPass());
148
Chris Lattner4f946372002-10-28 01:03:43 +0000149 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000150 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000151
152 //Insert empty stackslots in the stack frame of each function
153 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
154 PM.add(createStackSlotsPass(*this));
155
156 // Specialize LLVM code for this target machine and then
157 // run basic dataflow optimizations on LLVM code.
Misha Brukman86172ab2003-05-27 22:24:48 +0000158 if (!DisablePreSelect) {
159 PM.add(createPreSelectionPass(*this));
160 PM.add(createReassociatePass());
161 PM.add(createLICMPass());
162 PM.add(createGCSEPass());
163 }
Chris Lattner4f946372002-10-28 01:03:43 +0000164
165 PM.add(createInstructionSelectionPass(*this));
166
167 if (!DisableSched)
168 PM.add(createInstructionSchedulingWithSSAPass(*this));
169
170 PM.add(getRegisterAllocator(*this));
171
172 PM.add(getPrologEpilogInsertionPass());
173
174 if (!DisablePeephole)
175 PM.add(createPeepholeOptsPass(*this));
176
177 PM.add(MappingInfoForFunction(Out));
178
179 // Output assembly language to the .s file. Assembly emission is split into
180 // two parts: Function output and Global value output. This is because
181 // function output is pipelined with all of the rest of code generation stuff,
182 // allowing machine code representations for functions to be free'd after the
183 // function has been emitted.
184 //
185 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000186 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000187
188 // Emit Module level assembly after all of the functions have been processed.
189 PM.add(getModuleAsmPrinterPass(Out));
190
191 // Emit bytecode to the assembly file into its special section next
192 PM.add(getEmitBytecodeToAsmPass(Out));
193 PM.add(getFunctionInfo(Out));
Chris Lattner63342052002-10-29 21:12:46 +0000194 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000195}
Misha Brukman86172ab2003-05-27 22:24:48 +0000196
197// addPassesToJITCompile - This method controls the JIT method of code
198// generation for the UltraSparc.
199//
200bool UltraSparc::addPassesToJITCompile(PassManager &PM) {
201 // FIXME: implement the switch instruction in the instruction selector.
202 PM.add(createLowerSwitchPass());
203
204 // Construct and initialize the MachineFunction object for this fn.
205 PM.add(createMachineCodeConstructionPass(*this));
206
207 //Insert empty stackslots in the stack frame of each function
208 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
209 PM.add(createStackSlotsPass(*this));
210
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000211 // Specialize LLVM code for this target machine and then
212 // run basic dataflow optimizations on LLVM code.
213 if (!DisablePreSelect) {
214 PM.add(createPreSelectionPass(*this));
215 PM.add(createReassociatePass());
216 PM.add(createLICMPass());
217 PM.add(createGCSEPass());
218 }
219
Misha Brukman86172ab2003-05-27 22:24:48 +0000220 PM.add(createInstructionSelectionPass(*this));
221
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000222 if (!DisableSched)
223 PM.add(createInstructionSchedulingWithSSAPass(*this));
224
Misha Brukman86172ab2003-05-27 22:24:48 +0000225 // new pass: convert Value* in MachineOperand to an unsigned register
226 // this brings it in line with what the X86 JIT's RegisterAllocator expects
227 //PM.add(createAddRegNumToValuesPass());
228
229 PM.add(getRegisterAllocator(*this));
230 PM.add(getPrologEpilogInsertionPass());
231
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000232 if (!DisablePeephole)
233 PM.add(createPeepholeOptsPass(*this));
234
Misha Brukman86172ab2003-05-27 22:24:48 +0000235 return false; // success!
236}