blob: cfb753a6ca217c5ae5a3af4d38932f877fda9c10 [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 Lattner48e60792003-08-13 02:38:16 +00009#include "MappingInfo.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"
Chris Lattner48e60792003-08-13 02:38:16 +000012#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner4f946372002-10-28 01:03:43 +000013#include "llvm/Transforms/Scalar.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner78d881d2002-12-28 20:17:43 +000015#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner4f946372002-10-28 01:03:43 +000016#include "llvm/CodeGen/InstrSelection.h"
17#include "llvm/CodeGen/InstrScheduling.h"
18#include "llvm/CodeGen/RegisterAllocation.h"
19#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner48e60792003-08-13 02:38:16 +000020#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner4f946372002-10-28 01:03:43 +000021#include "Support/CommandLine.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000022
Chris Lattner6af20402002-12-03 05:41:54 +000023static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000024// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000025const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000026#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
27 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
28 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000029 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
30 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000031#include "SparcInstr.def"
32};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000033
Chris Lattner4f946372002-10-28 01:03:43 +000034//---------------------------------------------------------------------------
35// Command line options to control choice of code generation passes.
36//---------------------------------------------------------------------------
37
Misha Brukmanfe8a7962003-08-06 23:25:25 +000038static cl::opt<bool> DisablePreOpt("disable-preopt",
Misha Brukman58ba50f2003-08-06 23:06:21 +000039 cl::desc("Disable optimizations prior to instruction selection"));
Chris Lattner4f946372002-10-28 01:03:43 +000040
Misha Brukmanfe8a7962003-08-06 23:25:25 +000041static cl::opt<bool> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000042 cl::desc("Disable local scheduling pass"));
43
Misha Brukmanfe8a7962003-08-06 23:25:25 +000044static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000045 cl::desc("Disable peephole optimization pass"));
46
Brian Gaeke77d2f512003-08-16 00:23:16 +000047static cl::opt<bool> EmitMappingInfo("enable-maps",
Vikram S. Adve20a3be32003-08-12 15:51:02 +000048 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000049
Vikram S. Adve20a3be32003-08-12 15:51:02 +000050static cl::opt<bool> DisableStrip("disable-strip",
51 cl::desc("Do not strip the LLVM bytecode included in executable"));
52
53static cl::opt<bool> DumpInput("dump-input",
54 cl::desc("Print bytecode before native code generation"),
55 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000056
Vikram S. Adve0fb49802001-09-18 13:01:29 +000057//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000058// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
59// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000060//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000061
Chris Lattnerbb144a82003-08-24 19:49:48 +000062TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000063 return new UltraSparc();
64}
Vikram S. Adve9db43182001-10-22 13:44:23 +000065
Vikram S. Adve9db43182001-10-22 13:44:23 +000066//---------------------------------------------------------------------------
67// class UltraSparcFrameInfo
68//
Vikram S. Adve9db43182001-10-22 13:44:23 +000069// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000070// Starting offsets for each area of the stack frame are aligned at
71// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000072//---------------------------------------------------------------------------
73
74int
Misha Brukmanfce11432002-10-28 00:28:31 +000075UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000076 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000077{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000078 pos = false; // static stack area grows downwards
79 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000080}
81
82int
Misha Brukmanfce11432002-10-28 00:28:31 +000083UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000084 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000085{
Chris Lattner78d881d2002-12-28 20:17:43 +000086 // ensure no more auto vars are added
87 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000088
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000089 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000090 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000091 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000092}
93
94int
Misha Brukmanfce11432002-10-28 00:28:31 +000095UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000096 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000097{
Chris Lattner78d881d2002-12-28 20:17:43 +000098 MachineFunctionInfo *MFI = mcInfo.getInfo();
99 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
100 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000101
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000102 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000103 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
104 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000105 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000106 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000107}
108
109int
Misha Brukmanfce11432002-10-28 00:28:31 +0000110UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000111 bool& pos) const
112{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000113 // Dynamic stack area grows downwards starting at top of opt-args area.
114 // The opt-args, required-args, and register-save areas are empty except
115 // during calls and traps, so they are shifted downwards on each
116 // dynamic-size alloca.
117 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000118 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000119 if (int extra = optArgsSize % getStackFrameSizeAlignment())
120 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000121 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000122 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000123 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000124}
125
Chris Lattner20b1ea02001-09-14 03:47:57 +0000126//---------------------------------------------------------------------------
127// class UltraSparcMachine
128//
129// Purpose:
130// Primary interface to machine description for the UltraSPARC.
131// Primarily just initializes machine-dependent parameters in
132// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000133// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000134//
135//---------------------------------------------------------------------------
136
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000137UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000138 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000139 schedInfo(*this),
140 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000141 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000142 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000143}
144
Chris Lattner4f946372002-10-28 01:03:43 +0000145
146// addPassesToEmitAssembly - This method controls the entire code generation
147// process for the ultra sparc.
148//
Chris Lattner63342052002-10-29 21:12:46 +0000149bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000150{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000151 // The following 3 passes used to be inserted specially by llc.
152 // Replace malloc and free instructions with library calls.
153 PM.add(createLowerAllocationsPass());
154
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000155 // Strip all of the symbols from the bytecode so that it will be smaller...
156 if (!DisableStrip)
157 PM.add(createSymbolStrippingPass());
158
Chris Lattner155e68f2003-04-23 16:24:55 +0000159 // FIXME: implement the switch instruction in the instruction selector.
160 PM.add(createLowerSwitchPass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000161
Vikram S. Adve248932b2003-08-01 15:53:24 +0000162 // decompose multi-dimensional array references into single-dim refs
163 PM.add(createDecomposeMultiDimRefsPass());
164
Chris Lattner4f946372002-10-28 01:03:43 +0000165 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000166 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000167
168 //Insert empty stackslots in the stack frame of each function
169 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
170 PM.add(createStackSlotsPass(*this));
171
Misha Brukman58ba50f2003-08-06 23:06:21 +0000172 if (!DisablePreOpt) {
173 // Specialize LLVM code for this target machine
Misha Brukman86172ab2003-05-27 22:24:48 +0000174 PM.add(createPreSelectionPass(*this));
Misha Brukman58ba50f2003-08-06 23:06:21 +0000175 // Run basic dataflow optimizations on LLVM code
Misha Brukman86172ab2003-05-27 22:24:48 +0000176 PM.add(createReassociatePass());
177 PM.add(createLICMPass());
178 PM.add(createGCSEPass());
179 }
Vikram S. Advedeb96342003-07-29 19:58:00 +0000180
181 // If LLVM dumping after transformations is requested, add it to the pipeline
182 if (DumpInput)
Misha Brukman58ba50f2003-08-06 23:06:21 +0000183 PM.add(new PrintFunctionPass("Input code to instsr. selection:\n",
184 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000185
186 PM.add(createInstructionSelectionPass(*this));
187
188 if (!DisableSched)
189 PM.add(createInstructionSchedulingWithSSAPass(*this));
190
191 PM.add(getRegisterAllocator(*this));
192
193 PM.add(getPrologEpilogInsertionPass());
194
195 if (!DisablePeephole)
196 PM.add(createPeepholeOptsPass(*this));
197
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000198 if (EmitMappingInfo)
199 PM.add(getMappingInfoCollector(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000200
201 // Output assembly language to the .s file. Assembly emission is split into
202 // two parts: Function output and Global value output. This is because
203 // function output is pipelined with all of the rest of code generation stuff,
204 // allowing machine code representations for functions to be free'd after the
205 // function has been emitted.
206 //
207 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000208 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000209
210 // Emit Module level assembly after all of the functions have been processed.
211 PM.add(getModuleAsmPrinterPass(Out));
212
213 // Emit bytecode to the assembly file into its special section next
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000214 if (EmitMappingInfo) {
215 PM.add(getEmitBytecodeToAsmPass(Out));
216 PM.add(getFunctionInfo(Out));
217 }
218
Chris Lattner63342052002-10-29 21:12:46 +0000219 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000220}
Misha Brukman86172ab2003-05-27 22:24:48 +0000221
222// addPassesToJITCompile - This method controls the JIT method of code
223// generation for the UltraSparc.
224//
Brian Gaekee69f7272003-08-14 06:04:59 +0000225bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000226 const TargetData &TD = getTargetData();
227
228 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
229 TD.getPointerAlignment(), TD.getDoubleAlignment()));
230
231 // Replace malloc and free instructions with library calls.
232 // Do this after tracing until lli implements these lib calls.
233 // For now, it will emulate malloc and free internally.
234 PM.add(createLowerAllocationsPass());
235
Misha Brukman86172ab2003-05-27 22:24:48 +0000236 // FIXME: implement the switch instruction in the instruction selector.
237 PM.add(createLowerSwitchPass());
238
Misha Brukman58ba50f2003-08-06 23:06:21 +0000239 // decompose multi-dimensional array references into single-dim refs
240 PM.add(createDecomposeMultiDimRefsPass());
241
Misha Brukman86172ab2003-05-27 22:24:48 +0000242 // Construct and initialize the MachineFunction object for this fn.
243 PM.add(createMachineCodeConstructionPass(*this));
244
Misha Brukman86172ab2003-05-27 22:24:48 +0000245 PM.add(createInstructionSelectionPass(*this));
246
247 // new pass: convert Value* in MachineOperand to an unsigned register
248 // this brings it in line with what the X86 JIT's RegisterAllocator expects
249 //PM.add(createAddRegNumToValuesPass());
250
251 PM.add(getRegisterAllocator(*this));
252 PM.add(getPrologEpilogInsertionPass());
253
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000254 if (!DisablePeephole)
255 PM.add(createPeepholeOptsPass(*this));
256
Misha Brukman86172ab2003-05-27 22:24:48 +0000257 return false; // success!
258}