blob: 1fcd4b852a8f80d08f17743975251274f30e6abc [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> DisableSched("disable-sched",
Chris Lattner4f946372002-10-28 01:03:43 +000046 cl::desc("Disable local scheduling pass"));
47
Misha Brukmanfe8a7962003-08-06 23:25:25 +000048static cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000049 cl::desc("Disable peephole optimization pass"));
50
Brian Gaeke77d2f512003-08-16 00:23:16 +000051static cl::opt<bool> EmitMappingInfo("enable-maps",
Vikram S. Adve20a3be32003-08-12 15:51:02 +000052 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000053
Vikram S. Adve20a3be32003-08-12 15:51:02 +000054static cl::opt<bool> DisableStrip("disable-strip",
55 cl::desc("Do not strip the LLVM bytecode included in executable"));
56
57static cl::opt<bool> DumpInput("dump-input",
58 cl::desc("Print bytecode before native code generation"),
59 cl::Hidden);
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000060
Vikram S. Adve0fb49802001-09-18 13:01:29 +000061//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000062// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
63// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000064//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000065
Chris Lattnerbb144a82003-08-24 19:49:48 +000066TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000067 return new UltraSparc();
68}
Vikram S. Adve9db43182001-10-22 13:44:23 +000069
Vikram S. Adve9db43182001-10-22 13:44:23 +000070//---------------------------------------------------------------------------
71// class UltraSparcFrameInfo
72//
Vikram S. Adve9db43182001-10-22 13:44:23 +000073// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000074// Starting offsets for each area of the stack frame are aligned at
75// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000076//---------------------------------------------------------------------------
77
78int
Misha Brukmanfce11432002-10-28 00:28:31 +000079UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000080 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000081{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000082 pos = false; // static stack area grows downwards
83 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000084}
85
86int
Misha Brukmanfce11432002-10-28 00:28:31 +000087UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000088 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000089{
Chris Lattner78d881d2002-12-28 20:17:43 +000090 // ensure no more auto vars are added
91 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000092
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000093 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000094 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000095 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000096}
97
98int
Misha Brukmanfce11432002-10-28 00:28:31 +000099UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000100 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000101{
Chris Lattner78d881d2002-12-28 20:17:43 +0000102 MachineFunctionInfo *MFI = mcInfo.getInfo();
103 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
104 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000105
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000106 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000107 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
108 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000109 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000110 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000111}
112
113int
Misha Brukmanfce11432002-10-28 00:28:31 +0000114UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000115 bool& pos) const
116{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000117 // Dynamic stack area grows downwards starting at top of opt-args area.
118 // The opt-args, required-args, and register-save areas are empty except
119 // during calls and traps, so they are shifted downwards on each
120 // dynamic-size alloca.
121 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000122 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000123 if (int extra = optArgsSize % getStackFrameSizeAlignment())
124 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000125 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000126 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000127 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000128}
129
Chris Lattner20b1ea02001-09-14 03:47:57 +0000130//---------------------------------------------------------------------------
131// class UltraSparcMachine
132//
133// Purpose:
134// Primary interface to machine description for the UltraSPARC.
135// Primarily just initializes machine-dependent parameters in
136// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000137// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000138//
139//---------------------------------------------------------------------------
140
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000141UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000142 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000143 schedInfo(*this),
144 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000145 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000146 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000147}
148
Chris Lattner4f946372002-10-28 01:03:43 +0000149// addPassesToEmitAssembly - This method controls the entire code generation
150// process for the ultra sparc.
151//
Chris Lattner63342052002-10-29 21:12:46 +0000152bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000153{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000154 // The following 3 passes used to be inserted specially by llc.
155 // Replace malloc and free instructions with library calls.
156 PM.add(createLowerAllocationsPass());
157
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000158 // Strip all of the symbols from the bytecode so that it will be smaller...
159 if (!DisableStrip)
160 PM.add(createSymbolStrippingPass());
161
Chris Lattner155e68f2003-04-23 16:24:55 +0000162 // FIXME: implement the switch instruction in the instruction selector.
163 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000164
165 // FIXME: implement the invoke/unwind instructions!
166 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000167
Vikram S. Adve248932b2003-08-01 15:53:24 +0000168 // decompose multi-dimensional array references into single-dim refs
169 PM.add(createDecomposeMultiDimRefsPass());
170
Chris Lattner4f946372002-10-28 01:03:43 +0000171 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000172 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000173
174 //Insert empty stackslots in the stack frame of each function
175 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
176 PM.add(createStackSlotsPass(*this));
177
Misha Brukman35188172003-11-07 20:33:25 +0000178 // Specialize LLVM code for this target machine
179 PM.add(createPreSelectionPass(*this));
180 // Run basic dataflow optimizations on LLVM code
181 PM.add(createReassociatePass());
182 PM.add(createLICMPass());
183 PM.add(createGCSEPass());
184
Vikram S. Advedeb96342003-07-29 19:58:00 +0000185 // If LLVM dumping after transformations is requested, add it to the pipeline
186 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000187 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000188 &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)
Brian Gaeke79c98032003-09-18 17:37:46 +0000203 PM.add(getMappingInfoAsmPrinterPass(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
Brian Gaeke79c98032003-09-18 17:37:46 +0000218 if (EmitMappingInfo)
219 PM.add(getBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000220
Chris Lattner63342052002-10-29 21:12:46 +0000221 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000222}
Misha Brukman86172ab2003-05-27 22:24:48 +0000223
224// addPassesToJITCompile - This method controls the JIT method of code
225// generation for the UltraSparc.
226//
Brian Gaekee69f7272003-08-14 06:04:59 +0000227bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000228 const TargetData &TD = getTargetData();
229
230 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
231 TD.getPointerAlignment(), TD.getDoubleAlignment()));
232
233 // Replace malloc and free instructions with library calls.
234 // Do this after tracing until lli implements these lib calls.
235 // For now, it will emulate malloc and free internally.
236 PM.add(createLowerAllocationsPass());
237
Misha Brukman86172ab2003-05-27 22:24:48 +0000238 // FIXME: implement the switch instruction in the instruction selector.
239 PM.add(createLowerSwitchPass());
240
Chris Lattnerd324e252003-10-05 19:16:09 +0000241 // FIXME: implement the invoke/unwind instructions!
242 PM.add(createLowerInvokePass());
243
Misha Brukman58ba50f2003-08-06 23:06:21 +0000244 // decompose multi-dimensional array references into single-dim refs
245 PM.add(createDecomposeMultiDimRefsPass());
246
Misha Brukman86172ab2003-05-27 22:24:48 +0000247 // Construct and initialize the MachineFunction object for this fn.
248 PM.add(createMachineCodeConstructionPass(*this));
249
Misha Brukman35188172003-11-07 20:33:25 +0000250 // Specialize LLVM code for this target machine and then
251 // run basic dataflow optimizations on LLVM code.
252 PM.add(createPreSelectionPass(*this));
253 // Run basic dataflow optimizations on LLVM code
254 PM.add(createReassociatePass());
255 PM.add(createLICMPass());
256 PM.add(createGCSEPass());
257
Misha Brukman86172ab2003-05-27 22:24:48 +0000258 PM.add(createInstructionSelectionPass(*this));
259
Misha Brukman86172ab2003-05-27 22:24:48 +0000260 PM.add(getRegisterAllocator(*this));
261 PM.add(getPrologEpilogInsertionPass());
262
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000263 if (!DisablePeephole)
264 PM.add(createPeepholeOptsPass(*this));
265
Misha Brukman86172ab2003-05-27 22:24:48 +0000266 return false; // success!
267}