blob: 73f2fd81395e19ef82f2451544ca3f2cf2c9f96d [file] [log] [blame]
Chris Lattner1d21f3e2002-04-09 05:21:26 +00001//===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1d21f3e2002-04-09 05:21:26 +00009//
10// This file contains the code for the Sparc Target that does not fit in any of
11// the other files in this directory.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Adve9db43182001-10-22 13:44:23 +000014
Chris Lattner20b1ea02001-09-14 03:47:57 +000015#include "SparcInternals.h"
Chris Lattner48e60792003-08-13 02:38:16 +000016#include "MappingInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Chris Lattner4f946372002-10-28 01:03:43 +000018#include "llvm/PassManager.h"
Chris Lattner48e60792003-08-13 02:38:16 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner4f946372002-10-28 01:03:43 +000020#include "llvm/Transforms/Scalar.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner78d881d2002-12-28 20:17:43 +000022#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner4f946372002-10-28 01:03:43 +000023#include "llvm/CodeGen/InstrSelection.h"
24#include "llvm/CodeGen/InstrScheduling.h"
Chris Lattner4f946372002-10-28 01:03:43 +000025#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner56d22512003-09-30 22:24:00 +000026#include "llvm/CodeGen/Passes.h"
Chris Lattner48e60792003-08-13 02:38:16 +000027#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner4f946372002-10-28 01:03:43 +000028#include "Support/CommandLine.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
31
Chris Lattner6af20402002-12-03 05:41:54 +000032static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000033// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000034const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000035#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
36 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
37 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000038 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
39 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000040#include "SparcInstr.def"
41};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000042
Chris Lattner4f946372002-10-28 01:03:43 +000043//---------------------------------------------------------------------------
44// Command line options to control choice of code generation passes.
45//---------------------------------------------------------------------------
46
Misha Brukmanfe8a7962003-08-06 23:25:25 +000047static cl::opt<bool> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000048 cl::desc("Disable local scheduling pass"));
49
Misha Brukmanfe8a7962003-08-06 23:25:25 +000050static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000051 cl::desc("Disable peephole optimization pass"));
52
Brian Gaeke77d2f512003-08-16 00:23:16 +000053static cl::opt<bool> EmitMappingInfo("enable-maps",
Vikram S. Adve20a3be32003-08-12 15:51:02 +000054 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000055
Vikram S. Adve20a3be32003-08-12 15:51:02 +000056static cl::opt<bool> DisableStrip("disable-strip",
57 cl::desc("Do not strip the LLVM bytecode included in executable"));
58
59static cl::opt<bool> DumpInput("dump-input",
60 cl::desc("Print bytecode before native code generation"),
61 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000062
Vikram S. Adve0fb49802001-09-18 13:01:29 +000063//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000064// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
65// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000066//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000067
Chris Lattnerbb144a82003-08-24 19:49:48 +000068TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000069 return new UltraSparc();
70}
Vikram S. Adve9db43182001-10-22 13:44:23 +000071
Vikram S. Adve9db43182001-10-22 13:44:23 +000072//---------------------------------------------------------------------------
73// class UltraSparcFrameInfo
74//
Vikram S. Adve9db43182001-10-22 13:44:23 +000075// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000076// Starting offsets for each area of the stack frame are aligned at
77// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000078//---------------------------------------------------------------------------
79
80int
Misha Brukmanfce11432002-10-28 00:28:31 +000081UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000082 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000083{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000084 pos = false; // static stack area grows downwards
85 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000086}
87
88int
Misha Brukmanfce11432002-10-28 00:28:31 +000089UltraSparcFrameInfo::getRegSpillAreaOffset(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 // ensure no more auto vars are added
93 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000094
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000095 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000096 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000097 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000098}
99
100int
Misha Brukmanfce11432002-10-28 00:28:31 +0000101UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000102 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000103{
Chris Lattner78d881d2002-12-28 20:17:43 +0000104 MachineFunctionInfo *MFI = mcInfo.getInfo();
105 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
106 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000107
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000108 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000109 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
110 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000111 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000112 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000113}
114
115int
Misha Brukmanfce11432002-10-28 00:28:31 +0000116UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000117 bool& pos) const
118{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000119 // Dynamic stack area grows downwards starting at top of opt-args area.
120 // The opt-args, required-args, and register-save areas are empty except
121 // during calls and traps, so they are shifted downwards on each
122 // dynamic-size alloca.
123 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000124 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000125 if (int extra = optArgsSize % getStackFrameSizeAlignment())
126 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000127 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000128 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000129 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000130}
131
Chris Lattner20b1ea02001-09-14 03:47:57 +0000132//---------------------------------------------------------------------------
133// class UltraSparcMachine
134//
135// Purpose:
136// Primary interface to machine description for the UltraSPARC.
137// Primarily just initializes machine-dependent parameters in
138// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000139// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000140//
141//---------------------------------------------------------------------------
142
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000143UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000144 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000145 schedInfo(*this),
146 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000147 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000148 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000149}
150
Chris Lattner4f946372002-10-28 01:03:43 +0000151// addPassesToEmitAssembly - This method controls the entire code generation
152// process for the ultra sparc.
153//
Chris Lattner63342052002-10-29 21:12:46 +0000154bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000155{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000156 // The following 3 passes used to be inserted specially by llc.
157 // Replace malloc and free instructions with library calls.
158 PM.add(createLowerAllocationsPass());
159
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000160 // Strip all of the symbols from the bytecode so that it will be smaller...
161 if (!DisableStrip)
162 PM.add(createSymbolStrippingPass());
163
Chris Lattner155e68f2003-04-23 16:24:55 +0000164 // FIXME: implement the switch instruction in the instruction selector.
165 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000166
167 // FIXME: implement the invoke/unwind instructions!
168 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000169
Vikram S. Adve248932b2003-08-01 15:53:24 +0000170 // decompose multi-dimensional array references into single-dim refs
171 PM.add(createDecomposeMultiDimRefsPass());
172
Chris Lattner4f946372002-10-28 01:03:43 +0000173 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000174 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000175
176 //Insert empty stackslots in the stack frame of each function
177 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
178 PM.add(createStackSlotsPass(*this));
179
Misha Brukman35188172003-11-07 20:33:25 +0000180 // Specialize LLVM code for this target machine
181 PM.add(createPreSelectionPass(*this));
182 // Run basic dataflow optimizations on LLVM code
183 PM.add(createReassociatePass());
184 PM.add(createLICMPass());
185 PM.add(createGCSEPass());
186
Vikram S. Advedeb96342003-07-29 19:58:00 +0000187 // If LLVM dumping after transformations is requested, add it to the pipeline
188 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000189 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000190 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000191
192 PM.add(createInstructionSelectionPass(*this));
193
194 if (!DisableSched)
195 PM.add(createInstructionSchedulingWithSSAPass(*this));
196
197 PM.add(getRegisterAllocator(*this));
198
199 PM.add(getPrologEpilogInsertionPass());
200
201 if (!DisablePeephole)
202 PM.add(createPeepholeOptsPass(*this));
203
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000204 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000205 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000206
207 // Output assembly language to the .s file. Assembly emission is split into
208 // two parts: Function output and Global value output. This is because
209 // function output is pipelined with all of the rest of code generation stuff,
210 // allowing machine code representations for functions to be free'd after the
211 // function has been emitted.
212 //
213 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000214 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000215
216 // Emit Module level assembly after all of the functions have been processed.
217 PM.add(getModuleAsmPrinterPass(Out));
218
219 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000220 if (EmitMappingInfo)
221 PM.add(getBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000222
Chris Lattner63342052002-10-29 21:12:46 +0000223 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000224}
Misha Brukman86172ab2003-05-27 22:24:48 +0000225
226// addPassesToJITCompile - This method controls the JIT method of code
227// generation for the UltraSparc.
228//
Brian Gaekee69f7272003-08-14 06:04:59 +0000229bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000230 const TargetData &TD = getTargetData();
231
232 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
233 TD.getPointerAlignment(), TD.getDoubleAlignment()));
234
235 // Replace malloc and free instructions with library calls.
236 // Do this after tracing until lli implements these lib calls.
237 // For now, it will emulate malloc and free internally.
238 PM.add(createLowerAllocationsPass());
239
Misha Brukman86172ab2003-05-27 22:24:48 +0000240 // FIXME: implement the switch instruction in the instruction selector.
241 PM.add(createLowerSwitchPass());
242
Chris Lattnerd324e252003-10-05 19:16:09 +0000243 // FIXME: implement the invoke/unwind instructions!
244 PM.add(createLowerInvokePass());
245
Misha Brukman58ba50f2003-08-06 23:06:21 +0000246 // decompose multi-dimensional array references into single-dim refs
247 PM.add(createDecomposeMultiDimRefsPass());
248
Misha Brukman86172ab2003-05-27 22:24:48 +0000249 // Construct and initialize the MachineFunction object for this fn.
250 PM.add(createMachineCodeConstructionPass(*this));
251
Misha Brukman35188172003-11-07 20:33:25 +0000252 // Specialize LLVM code for this target machine and then
253 // run basic dataflow optimizations on LLVM code.
254 PM.add(createPreSelectionPass(*this));
255 // Run basic dataflow optimizations on LLVM code
256 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000257
258 // FIXME: these passes crash the FunctionPassManager when being added...
259 //PM.add(createLICMPass());
260 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000261
Misha Brukman86172ab2003-05-27 22:24:48 +0000262 PM.add(createInstructionSelectionPass(*this));
263
Misha Brukman86172ab2003-05-27 22:24:48 +0000264 PM.add(getRegisterAllocator(*this));
265 PM.add(getPrologEpilogInsertionPass());
266
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000267 if (!DisablePeephole)
268 PM.add(createPeepholeOptsPass(*this));
269
Misha Brukman86172ab2003-05-27 22:24:48 +0000270 return false; // success!
271}
Brian Gaeked0fde302003-11-11 22:41:34 +0000272
273} // End llvm namespace