blob: 71e323918060f2db97cc7f16ee06a61e7ee909b6 [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"
Chris Lattner4f946372002-10-28 01:03:43 +000023#include "Support/CommandLine.h"
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000024#include "llvm/Assembly/PrintModulePass.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000025
Chris Lattner6af20402002-12-03 05:41:54 +000026static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000027// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000028const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000029#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
30 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
31 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000032 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
33 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000034#include "SparcInstr.def"
35};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000036
Chris Lattner4f946372002-10-28 01:03:43 +000037//---------------------------------------------------------------------------
38// Command line options to control choice of code generation passes.
39//---------------------------------------------------------------------------
40
Misha Brukmanfe8a7962003-08-06 23:25:25 +000041static cl::opt<bool> DisablePreOpt("disable-preopt",
Misha Brukman58ba50f2003-08-06 23:06:21 +000042 cl::desc("Disable optimizations prior to instruction selection"));
Chris Lattner4f946372002-10-28 01:03:43 +000043
Misha Brukmanfe8a7962003-08-06 23:25:25 +000044static cl::opt<bool> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000045 cl::desc("Disable local scheduling pass"));
46
Misha Brukmanfe8a7962003-08-06 23:25:25 +000047static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000048 cl::desc("Disable peephole optimization pass"));
49
Vikram S. Adve20a3be32003-08-12 15:51:02 +000050static cl::opt<bool> EmitMappingInfo("emitmaps",
51 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000052
Vikram S. Adve20a3be32003-08-12 15:51:02 +000053static cl::opt<bool> DisableStrip("disable-strip",
54 cl::desc("Do not strip the LLVM bytecode included in executable"));
55
56static cl::opt<bool> DumpInput("dump-input",
57 cl::desc("Print bytecode before native code generation"),
58 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000059
Vikram S. Adve0fb49802001-09-18 13:01:29 +000060//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000061// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
62// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000063//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000064
Misha Brukman86172ab2003-05-27 22:24:48 +000065TargetMachine *allocateSparcTargetMachine(unsigned Configuration) {
66 return new UltraSparc();
67}
Vikram S. Adve9db43182001-10-22 13:44:23 +000068
Vikram S. Adve9db43182001-10-22 13:44:23 +000069//---------------------------------------------------------------------------
70// class UltraSparcFrameInfo
71//
Vikram S. Adve9db43182001-10-22 13:44:23 +000072// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000073// Starting offsets for each area of the stack frame are aligned at
74// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000075//---------------------------------------------------------------------------
76
77int
Misha Brukmanfce11432002-10-28 00:28:31 +000078UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000079 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000080{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000081 pos = false; // static stack area grows downwards
82 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000083}
84
85int
Misha Brukmanfce11432002-10-28 00:28:31 +000086UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000087 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000088{
Chris Lattner78d881d2002-12-28 20:17:43 +000089 // ensure no more auto vars are added
90 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000091
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000092 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000093 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000094 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000095}
96
97int
Misha Brukmanfce11432002-10-28 00:28:31 +000098UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000099 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000100{
Chris Lattner78d881d2002-12-28 20:17:43 +0000101 MachineFunctionInfo *MFI = mcInfo.getInfo();
102 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
103 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000104
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000105 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000106 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
107 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000108 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000109 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000110}
111
112int
Misha Brukmanfce11432002-10-28 00:28:31 +0000113UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000114 bool& pos) const
115{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000116 // Dynamic stack area grows downwards starting at top of opt-args area.
117 // The opt-args, required-args, and register-save areas are empty except
118 // during calls and traps, so they are shifted downwards on each
119 // dynamic-size alloca.
120 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000121 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000122 if (int extra = optArgsSize % getStackFrameSizeAlignment())
123 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000124 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000125 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000126 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000127}
128
Chris Lattner20b1ea02001-09-14 03:47:57 +0000129//---------------------------------------------------------------------------
130// class UltraSparcMachine
131//
132// Purpose:
133// Primary interface to machine description for the UltraSPARC.
134// Primarily just initializes machine-dependent parameters in
135// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000136// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000137//
138//---------------------------------------------------------------------------
139
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000140UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000141 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000142 schedInfo(*this),
143 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000144 frameInfo(*this),
Vikram S. Adved55697c2002-09-20 00:52:09 +0000145 cacheInfo(*this),
Chris Lattnerc56406c2002-10-29 21:48:17 +0000146 optInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000147}
148
Chris Lattner4f946372002-10-28 01:03:43 +0000149
150// addPassesToEmitAssembly - This method controls the entire code generation
151// process for the ultra sparc.
152//
Chris Lattner63342052002-10-29 21:12:46 +0000153bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000154{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000155 // The following 3 passes used to be inserted specially by llc.
156 // Replace malloc and free instructions with library calls.
157 PM.add(createLowerAllocationsPass());
158
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000159 // Strip all of the symbols from the bytecode so that it will be smaller...
160 if (!DisableStrip)
161 PM.add(createSymbolStrippingPass());
162
Chris Lattner155e68f2003-04-23 16:24:55 +0000163 // FIXME: implement the switch instruction in the instruction selector.
164 PM.add(createLowerSwitchPass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000165
Vikram S. Adve248932b2003-08-01 15:53:24 +0000166 // decompose multi-dimensional array references into single-dim refs
167 PM.add(createDecomposeMultiDimRefsPass());
168
Chris Lattner4f946372002-10-28 01:03:43 +0000169 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000170 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000171
172 //Insert empty stackslots in the stack frame of each function
173 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
174 PM.add(createStackSlotsPass(*this));
175
Misha Brukman58ba50f2003-08-06 23:06:21 +0000176 if (!DisablePreOpt) {
177 // Specialize LLVM code for this target machine
Misha Brukman86172ab2003-05-27 22:24:48 +0000178 PM.add(createPreSelectionPass(*this));
Misha Brukman58ba50f2003-08-06 23:06:21 +0000179 // Run basic dataflow optimizations on LLVM code
Misha Brukman86172ab2003-05-27 22:24:48 +0000180 PM.add(createReassociatePass());
181 PM.add(createLICMPass());
182 PM.add(createGCSEPass());
183 }
Vikram S. Advedeb96342003-07-29 19:58:00 +0000184
185 // If LLVM dumping after transformations is requested, add it to the pipeline
186 if (DumpInput)
Misha Brukman58ba50f2003-08-06 23:06:21 +0000187 PM.add(new PrintFunctionPass("Input code to instsr. selection:\n",
188 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000189
190 PM.add(createInstructionSelectionPass(*this));
191
192 if (!DisableSched)
193 PM.add(createInstructionSchedulingWithSSAPass(*this));
194
195 PM.add(getRegisterAllocator(*this));
196
197 PM.add(getPrologEpilogInsertionPass());
198
199 if (!DisablePeephole)
200 PM.add(createPeepholeOptsPass(*this));
201
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000202 if (EmitMappingInfo)
203 PM.add(getMappingInfoCollector(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000204
205 // Output assembly language to the .s file. Assembly emission is split into
206 // two parts: Function output and Global value output. This is because
207 // function output is pipelined with all of the rest of code generation stuff,
208 // allowing machine code representations for functions to be free'd after the
209 // function has been emitted.
210 //
211 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000212 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000213
214 // Emit Module level assembly after all of the functions have been processed.
215 PM.add(getModuleAsmPrinterPass(Out));
216
217 // Emit bytecode to the assembly file into its special section next
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000218 if (EmitMappingInfo) {
219 PM.add(getEmitBytecodeToAsmPass(Out));
220 PM.add(getFunctionInfo(Out));
221 }
222
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//
229bool UltraSparc::addPassesToJITCompile(PassManager &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
Misha Brukman58ba50f2003-08-06 23:06:21 +0000243 // decompose multi-dimensional array references into single-dim refs
244 PM.add(createDecomposeMultiDimRefsPass());
245
Misha Brukman86172ab2003-05-27 22:24:48 +0000246 // Construct and initialize the MachineFunction object for this fn.
247 PM.add(createMachineCodeConstructionPass(*this));
248
Misha Brukman86172ab2003-05-27 22:24:48 +0000249 PM.add(createInstructionSelectionPass(*this));
250
251 // new pass: convert Value* in MachineOperand to an unsigned register
252 // this brings it in line with what the X86 JIT's RegisterAllocator expects
253 //PM.add(createAddRegNumToValuesPass());
254
255 PM.add(getRegisterAllocator(*this));
256 PM.add(getPrologEpilogInsertionPass());
257
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000258 if (!DisablePeephole)
259 PM.add(createPeepholeOptsPass(*this));
260
Misha Brukman86172ab2003-05-27 22:24:48 +0000261 return false; // success!
262}