blob: 728b43c4ac025101111a47bb5fd525d71c7d6885 [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"
Chris Lattner4f946372002-10-28 01:03:43 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner48e60792003-08-13 02:38:16 +000019#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner4f946372002-10-28 01:03:43 +000020#include "Support/CommandLine.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000021
Chris Lattner6af20402002-12-03 05:41:54 +000022static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000023// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000024const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000025#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
26 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
27 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000028 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
29 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000030#include "SparcInstr.def"
31};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000032
Chris Lattner4f946372002-10-28 01:03:43 +000033//---------------------------------------------------------------------------
34// Command line options to control choice of code generation passes.
35//---------------------------------------------------------------------------
36
Misha Brukmanfe8a7962003-08-06 23:25:25 +000037static cl::opt<bool> DisablePreOpt("disable-preopt",
Misha Brukman58ba50f2003-08-06 23:06:21 +000038 cl::desc("Disable optimizations prior to instruction selection"));
Chris Lattner4f946372002-10-28 01:03:43 +000039
Misha Brukmanfe8a7962003-08-06 23:25:25 +000040static cl::opt<bool> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000041 cl::desc("Disable local scheduling pass"));
42
Misha Brukmanfe8a7962003-08-06 23:25:25 +000043static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000044 cl::desc("Disable peephole optimization pass"));
45
Brian Gaeke77d2f512003-08-16 00:23:16 +000046static cl::opt<bool> EmitMappingInfo("enable-maps",
Vikram S. Adve20a3be32003-08-12 15:51:02 +000047 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000048
Vikram S. Adve20a3be32003-08-12 15:51:02 +000049static cl::opt<bool> DisableStrip("disable-strip",
50 cl::desc("Do not strip the LLVM bytecode included in executable"));
51
52static cl::opt<bool> DumpInput("dump-input",
53 cl::desc("Print bytecode before native code generation"),
54 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000055
Vikram S. Adve0fb49802001-09-18 13:01:29 +000056//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000057// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
58// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000059//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000060
Chris Lattnerbb144a82003-08-24 19:49:48 +000061TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000062 return new UltraSparc();
63}
Vikram S. Adve9db43182001-10-22 13:44:23 +000064
Vikram S. Adve9db43182001-10-22 13:44:23 +000065//---------------------------------------------------------------------------
66// class UltraSparcFrameInfo
67//
Vikram S. Adve9db43182001-10-22 13:44:23 +000068// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000069// Starting offsets for each area of the stack frame are aligned at
70// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000071//---------------------------------------------------------------------------
72
73int
Misha Brukmanfce11432002-10-28 00:28:31 +000074UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000075 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000076{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000077 pos = false; // static stack area grows downwards
78 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000079}
80
81int
Misha Brukmanfce11432002-10-28 00:28:31 +000082UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000083 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000084{
Chris Lattner78d881d2002-12-28 20:17:43 +000085 // ensure no more auto vars are added
86 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000087
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000088 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000089 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000090 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000091}
92
93int
Misha Brukmanfce11432002-10-28 00:28:31 +000094UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000095 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000096{
Chris Lattner78d881d2002-12-28 20:17:43 +000097 MachineFunctionInfo *MFI = mcInfo.getInfo();
98 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
99 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000100
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000101 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000102 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
103 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000104 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000105 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000106}
107
108int
Misha Brukmanfce11432002-10-28 00:28:31 +0000109UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000110 bool& pos) const
111{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000112 // Dynamic stack area grows downwards starting at top of opt-args area.
113 // The opt-args, required-args, and register-save areas are empty except
114 // during calls and traps, so they are shifted downwards on each
115 // dynamic-size alloca.
116 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000117 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000118 if (int extra = optArgsSize % getStackFrameSizeAlignment())
119 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000120 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000121 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000122 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000123}
124
Chris Lattner20b1ea02001-09-14 03:47:57 +0000125//---------------------------------------------------------------------------
126// class UltraSparcMachine
127//
128// Purpose:
129// Primary interface to machine description for the UltraSPARC.
130// Primarily just initializes machine-dependent parameters in
131// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000132// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000133//
134//---------------------------------------------------------------------------
135
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000136UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000137 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000138 schedInfo(*this),
139 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000140 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000141 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000142}
143
Chris Lattner4f946372002-10-28 01:03:43 +0000144
145// addPassesToEmitAssembly - This method controls the entire code generation
146// process for the ultra sparc.
147//
Chris Lattner63342052002-10-29 21:12:46 +0000148bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000149{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000150 // The following 3 passes used to be inserted specially by llc.
151 // Replace malloc and free instructions with library calls.
152 PM.add(createLowerAllocationsPass());
153
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000154 // Strip all of the symbols from the bytecode so that it will be smaller...
155 if (!DisableStrip)
156 PM.add(createSymbolStrippingPass());
157
Chris Lattner155e68f2003-04-23 16:24:55 +0000158 // FIXME: implement the switch instruction in the instruction selector.
159 PM.add(createLowerSwitchPass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000160
Vikram S. Adve248932b2003-08-01 15:53:24 +0000161 // decompose multi-dimensional array references into single-dim refs
162 PM.add(createDecomposeMultiDimRefsPass());
163
Chris Lattner4f946372002-10-28 01:03:43 +0000164 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000165 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000166
167 //Insert empty stackslots in the stack frame of each function
168 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
169 PM.add(createStackSlotsPass(*this));
170
Misha Brukman58ba50f2003-08-06 23:06:21 +0000171 if (!DisablePreOpt) {
172 // Specialize LLVM code for this target machine
Misha Brukman86172ab2003-05-27 22:24:48 +0000173 PM.add(createPreSelectionPass(*this));
Misha Brukman58ba50f2003-08-06 23:06:21 +0000174 // Run basic dataflow optimizations on LLVM code
Misha Brukman86172ab2003-05-27 22:24:48 +0000175 PM.add(createReassociatePass());
176 PM.add(createLICMPass());
177 PM.add(createGCSEPass());
178 }
Vikram S. Advedeb96342003-07-29 19:58:00 +0000179
180 // If LLVM dumping after transformations is requested, add it to the pipeline
181 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000182 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000183 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000184
185 PM.add(createInstructionSelectionPass(*this));
186
187 if (!DisableSched)
188 PM.add(createInstructionSchedulingWithSSAPass(*this));
189
190 PM.add(getRegisterAllocator(*this));
191
192 PM.add(getPrologEpilogInsertionPass());
193
194 if (!DisablePeephole)
195 PM.add(createPeepholeOptsPass(*this));
196
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000197 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000198 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000199
200 // Output assembly language to the .s file. Assembly emission is split into
201 // two parts: Function output and Global value output. This is because
202 // function output is pipelined with all of the rest of code generation stuff,
203 // allowing machine code representations for functions to be free'd after the
204 // function has been emitted.
205 //
206 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000207 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000208
209 // Emit Module level assembly after all of the functions have been processed.
210 PM.add(getModuleAsmPrinterPass(Out));
211
212 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000213 if (EmitMappingInfo)
214 PM.add(getBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000215
Chris Lattner63342052002-10-29 21:12:46 +0000216 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000217}
Misha Brukman86172ab2003-05-27 22:24:48 +0000218
219// addPassesToJITCompile - This method controls the JIT method of code
220// generation for the UltraSparc.
221//
Brian Gaekee69f7272003-08-14 06:04:59 +0000222bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000223 const TargetData &TD = getTargetData();
224
225 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
226 TD.getPointerAlignment(), TD.getDoubleAlignment()));
227
228 // Replace malloc and free instructions with library calls.
229 // Do this after tracing until lli implements these lib calls.
230 // For now, it will emulate malloc and free internally.
231 PM.add(createLowerAllocationsPass());
232
Misha Brukman86172ab2003-05-27 22:24:48 +0000233 // FIXME: implement the switch instruction in the instruction selector.
234 PM.add(createLowerSwitchPass());
235
Misha Brukman58ba50f2003-08-06 23:06:21 +0000236 // decompose multi-dimensional array references into single-dim refs
237 PM.add(createDecomposeMultiDimRefsPass());
238
Misha Brukman86172ab2003-05-27 22:24:48 +0000239 // Construct and initialize the MachineFunction object for this fn.
240 PM.add(createMachineCodeConstructionPass(*this));
241
Misha Brukman86172ab2003-05-27 22:24:48 +0000242 PM.add(createInstructionSelectionPass(*this));
243
244 // new pass: convert Value* in MachineOperand to an unsigned register
245 // this brings it in line with what the X86 JIT's RegisterAllocator expects
246 //PM.add(createAddRegNumToValuesPass());
247
248 PM.add(getRegisterAllocator(*this));
249 PM.add(getPrologEpilogInsertionPass());
250
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000251 if (!DisablePeephole)
252 PM.add(createPeepholeOptsPass(*this));
253
Misha Brukman86172ab2003-05-27 22:24:48 +0000254 return false; // success!
255}