blob: 738413fc599502fed7f0b57c21e8921fb4721d86 [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
Chris Lattner6af20402002-12-03 05:41:54 +000030static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000031// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000032const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000033#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
34 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
35 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000036 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
37 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000038#include "SparcInstr.def"
39};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000040
Chris Lattner4f946372002-10-28 01:03:43 +000041//---------------------------------------------------------------------------
42// Command line options to control choice of code generation passes.
43//---------------------------------------------------------------------------
44
Misha Brukmanfe8a7962003-08-06 23:25:25 +000045static cl::opt<bool> DisablePreOpt("disable-preopt",
Misha Brukman58ba50f2003-08-06 23:06:21 +000046 cl::desc("Disable optimizations prior to instruction selection"));
Chris Lattner4f946372002-10-28 01:03:43 +000047
Misha Brukmanfe8a7962003-08-06 23:25:25 +000048static cl::opt<bool> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000049 cl::desc("Disable local scheduling pass"));
50
Misha Brukmanfe8a7962003-08-06 23:25:25 +000051static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000052 cl::desc("Disable peephole optimization pass"));
53
Brian Gaeke77d2f512003-08-16 00:23:16 +000054static cl::opt<bool> EmitMappingInfo("enable-maps",
Vikram S. Adve20a3be32003-08-12 15:51:02 +000055 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000056
Vikram S. Adve20a3be32003-08-12 15:51:02 +000057static cl::opt<bool> DisableStrip("disable-strip",
58 cl::desc("Do not strip the LLVM bytecode included in executable"));
59
60static cl::opt<bool> DumpInput("dump-input",
61 cl::desc("Print bytecode before native code generation"),
62 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000063
Vikram S. Adve0fb49802001-09-18 13:01:29 +000064//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000065// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
66// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000067//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000068
Chris Lattnerbb144a82003-08-24 19:49:48 +000069TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000070 return new UltraSparc();
71}
Vikram S. Adve9db43182001-10-22 13:44:23 +000072
Vikram S. Adve9db43182001-10-22 13:44:23 +000073//---------------------------------------------------------------------------
74// class UltraSparcFrameInfo
75//
Vikram S. Adve9db43182001-10-22 13:44:23 +000076// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000077// Starting offsets for each area of the stack frame are aligned at
78// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000079//---------------------------------------------------------------------------
80
81int
Misha Brukmanfce11432002-10-28 00:28:31 +000082UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000083 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000084{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000085 pos = false; // static stack area grows downwards
86 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000087}
88
89int
Misha Brukmanfce11432002-10-28 00:28:31 +000090UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000091 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000092{
Chris Lattner78d881d2002-12-28 20:17:43 +000093 // ensure no more auto vars are added
94 mcInfo.getInfo()->freezeAutomaticVarsArea();
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 = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000098 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000099}
100
101int
Misha Brukmanfce11432002-10-28 00:28:31 +0000102UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000103 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000104{
Chris Lattner78d881d2002-12-28 20:17:43 +0000105 MachineFunctionInfo *MFI = mcInfo.getInfo();
106 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
107 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000108
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000109 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000110 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
111 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000112 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000113 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000114}
115
116int
Misha Brukmanfce11432002-10-28 00:28:31 +0000117UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000118 bool& pos) const
119{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000120 // Dynamic stack area grows downwards starting at top of opt-args area.
121 // The opt-args, required-args, and register-save areas are empty except
122 // during calls and traps, so they are shifted downwards on each
123 // dynamic-size alloca.
124 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000125 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000126 if (int extra = optArgsSize % getStackFrameSizeAlignment())
127 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000128 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000129 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000130 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000131}
132
Chris Lattner20b1ea02001-09-14 03:47:57 +0000133//---------------------------------------------------------------------------
134// class UltraSparcMachine
135//
136// Purpose:
137// Primary interface to machine description for the UltraSPARC.
138// Primarily just initializes machine-dependent parameters in
139// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000140// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000141//
142//---------------------------------------------------------------------------
143
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000144UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000145 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000146 schedInfo(*this),
147 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000148 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000149 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000150}
151
Chris Lattner4f946372002-10-28 01:03:43 +0000152// addPassesToEmitAssembly - This method controls the entire code generation
153// process for the ultra sparc.
154//
Chris Lattner63342052002-10-29 21:12:46 +0000155bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000156{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000157 // The following 3 passes used to be inserted specially by llc.
158 // Replace malloc and free instructions with library calls.
159 PM.add(createLowerAllocationsPass());
160
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000161 // Strip all of the symbols from the bytecode so that it will be smaller...
162 if (!DisableStrip)
163 PM.add(createSymbolStrippingPass());
164
Chris Lattner155e68f2003-04-23 16:24:55 +0000165 // FIXME: implement the switch instruction in the instruction selector.
166 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000167
168 // FIXME: implement the invoke/unwind instructions!
169 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000170
Vikram S. Adve248932b2003-08-01 15:53:24 +0000171 // decompose multi-dimensional array references into single-dim refs
172 PM.add(createDecomposeMultiDimRefsPass());
173
Chris Lattner4f946372002-10-28 01:03:43 +0000174 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000175 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000176
177 //Insert empty stackslots in the stack frame of each function
178 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
179 PM.add(createStackSlotsPass(*this));
180
Misha Brukman58ba50f2003-08-06 23:06:21 +0000181 if (!DisablePreOpt) {
182 // Specialize LLVM code for this target machine
Misha Brukman86172ab2003-05-27 22:24:48 +0000183 PM.add(createPreSelectionPass(*this));
Misha Brukman58ba50f2003-08-06 23:06:21 +0000184 // Run basic dataflow optimizations on LLVM code
Misha Brukman86172ab2003-05-27 22:24:48 +0000185 PM.add(createReassociatePass());
186 PM.add(createLICMPass());
187 PM.add(createGCSEPass());
188 }
Vikram S. Advedeb96342003-07-29 19:58:00 +0000189
190 // If LLVM dumping after transformations is requested, add it to the pipeline
191 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000192 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000193 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000194
195 PM.add(createInstructionSelectionPass(*this));
196
197 if (!DisableSched)
198 PM.add(createInstructionSchedulingWithSSAPass(*this));
199
200 PM.add(getRegisterAllocator(*this));
201
202 PM.add(getPrologEpilogInsertionPass());
203
204 if (!DisablePeephole)
205 PM.add(createPeepholeOptsPass(*this));
206
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000207 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000208 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000209
210 // Output assembly language to the .s file. Assembly emission is split into
211 // two parts: Function output and Global value output. This is because
212 // function output is pipelined with all of the rest of code generation stuff,
213 // allowing machine code representations for functions to be free'd after the
214 // function has been emitted.
215 //
216 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000217 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000218
219 // Emit Module level assembly after all of the functions have been processed.
220 PM.add(getModuleAsmPrinterPass(Out));
221
222 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000223 if (EmitMappingInfo)
224 PM.add(getBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000225
Chris Lattner63342052002-10-29 21:12:46 +0000226 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000227}
Misha Brukman86172ab2003-05-27 22:24:48 +0000228
229// addPassesToJITCompile - This method controls the JIT method of code
230// generation for the UltraSparc.
231//
Brian Gaekee69f7272003-08-14 06:04:59 +0000232bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000233 const TargetData &TD = getTargetData();
234
235 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
236 TD.getPointerAlignment(), TD.getDoubleAlignment()));
237
238 // Replace malloc and free instructions with library calls.
239 // Do this after tracing until lli implements these lib calls.
240 // For now, it will emulate malloc and free internally.
241 PM.add(createLowerAllocationsPass());
242
Misha Brukman86172ab2003-05-27 22:24:48 +0000243 // FIXME: implement the switch instruction in the instruction selector.
244 PM.add(createLowerSwitchPass());
245
Chris Lattnerd324e252003-10-05 19:16:09 +0000246 // FIXME: implement the invoke/unwind instructions!
247 PM.add(createLowerInvokePass());
248
Misha Brukman58ba50f2003-08-06 23:06:21 +0000249 // decompose multi-dimensional array references into single-dim refs
250 PM.add(createDecomposeMultiDimRefsPass());
251
Misha Brukman86172ab2003-05-27 22:24:48 +0000252 // Construct and initialize the MachineFunction object for this fn.
253 PM.add(createMachineCodeConstructionPass(*this));
254
Misha Brukman86172ab2003-05-27 22:24:48 +0000255 PM.add(createInstructionSelectionPass(*this));
256
Misha Brukman86172ab2003-05-27 22:24:48 +0000257 PM.add(getRegisterAllocator(*this));
258 PM.add(getPrologEpilogInsertionPass());
259
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000260 if (!DisablePeephole)
261 PM.add(createPeepholeOptsPass(*this));
262
Misha Brukman86172ab2003-05-27 22:24:48 +0000263 return false; // success!
264}