blob: 1502dfc0f303d504cc8131f6330702d854aa6ac8 [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 Brukman1be1a232003-11-13 00:16:28 +000047namespace {
48 cl::opt<bool> DisableSched("disable-sched",
49 cl::desc("Disable local scheduling pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000050
Misha Brukman1be1a232003-11-13 00:16:28 +000051 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000052 cl::desc("Disable peephole optimization pass"));
53
Misha Brukman1be1a232003-11-13 00:16:28 +000054 cl::opt<bool> EmitMappingInfo("enable-maps",
55 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000056
Misha Brukman1be1a232003-11-13 00:16:28 +000057 cl::opt<bool> DisableStrip("disable-strip",
58 cl::desc("Do not strip the LLVM bytecode in executable"));
Vikram S. Adve20a3be32003-08-12 15:51:02 +000059
Misha Brukman1be1a232003-11-13 00:16:28 +000060 cl::opt<bool> DumpInput("dump-input",
61 cl::desc("Print bytecode before code generation"),
62 cl::Hidden);
63}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000064
Vikram S. Adve0fb49802001-09-18 13:01:29 +000065//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000066// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
67// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000068//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000069
Chris Lattnerbb144a82003-08-24 19:49:48 +000070TargetMachine *allocateSparcTargetMachine(const Module &M) {
Misha Brukman86172ab2003-05-27 22:24:48 +000071 return new UltraSparc();
72}
Vikram S. Adve9db43182001-10-22 13:44:23 +000073
Vikram S. Adve9db43182001-10-22 13:44:23 +000074//---------------------------------------------------------------------------
75// class UltraSparcFrameInfo
76//
Vikram S. Adve9db43182001-10-22 13:44:23 +000077// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000078// Starting offsets for each area of the stack frame are aligned at
79// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000080//---------------------------------------------------------------------------
81
82int
Misha Brukmanfce11432002-10-28 00:28:31 +000083UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000084 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000085{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000086 pos = false; // static stack area grows downwards
87 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000088}
89
90int
Misha Brukmanfce11432002-10-28 00:28:31 +000091UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000092 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000093{
Chris Lattner78d881d2002-12-28 20:17:43 +000094 // ensure no more auto vars are added
95 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000096
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000097 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000098 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000099 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000100}
101
102int
Misha Brukmanfce11432002-10-28 00:28:31 +0000103UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000104 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +0000105{
Chris Lattner78d881d2002-12-28 20:17:43 +0000106 MachineFunctionInfo *MFI = mcInfo.getInfo();
107 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
108 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +0000109
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000110 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +0000111 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
112 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +0000113 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +0000114 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000115}
116
117int
Misha Brukmanfce11432002-10-28 00:28:31 +0000118UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000119 bool& pos) const
120{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000121 // Dynamic stack area grows downwards starting at top of opt-args area.
122 // The opt-args, required-args, and register-save areas are empty except
123 // during calls and traps, so they are shifted downwards on each
124 // dynamic-size alloca.
125 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000126 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000127 if (int extra = optArgsSize % getStackFrameSizeAlignment())
128 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000129 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000130 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000131 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000132}
133
Chris Lattner20b1ea02001-09-14 03:47:57 +0000134//---------------------------------------------------------------------------
135// class UltraSparcMachine
136//
137// Purpose:
138// Primary interface to machine description for the UltraSPARC.
139// Primarily just initializes machine-dependent parameters in
140// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000141// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000142//
143//---------------------------------------------------------------------------
144
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000145UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000146 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000147 schedInfo(*this),
148 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000149 frameInfo(*this),
Chris Lattner84be5ad2003-09-01 20:40:59 +0000150 cacheInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000151}
152
Chris Lattner4f946372002-10-28 01:03:43 +0000153// addPassesToEmitAssembly - This method controls the entire code generation
154// process for the ultra sparc.
155//
Chris Lattner63342052002-10-29 21:12:46 +0000156bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000157{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000158 // The following 3 passes used to be inserted specially by llc.
159 // Replace malloc and free instructions with library calls.
160 PM.add(createLowerAllocationsPass());
161
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000162 // Strip all of the symbols from the bytecode so that it will be smaller...
163 if (!DisableStrip)
164 PM.add(createSymbolStrippingPass());
165
Chris Lattner155e68f2003-04-23 16:24:55 +0000166 // FIXME: implement the switch instruction in the instruction selector.
167 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000168
169 // FIXME: implement the invoke/unwind instructions!
170 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000171
Vikram S. Adve248932b2003-08-01 15:53:24 +0000172 // decompose multi-dimensional array references into single-dim refs
173 PM.add(createDecomposeMultiDimRefsPass());
174
Chris Lattner4f946372002-10-28 01:03:43 +0000175 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000176 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000177
178 //Insert empty stackslots in the stack frame of each function
179 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
180 PM.add(createStackSlotsPass(*this));
181
Misha Brukman35188172003-11-07 20:33:25 +0000182 // Specialize LLVM code for this target machine
183 PM.add(createPreSelectionPass(*this));
184 // Run basic dataflow optimizations on LLVM code
185 PM.add(createReassociatePass());
186 PM.add(createLICMPass());
187 PM.add(createGCSEPass());
188
Vikram S. Advedeb96342003-07-29 19:58:00 +0000189 // If LLVM dumping after transformations is requested, add it to the pipeline
190 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000191 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000192 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000193
194 PM.add(createInstructionSelectionPass(*this));
195
196 if (!DisableSched)
197 PM.add(createInstructionSchedulingWithSSAPass(*this));
198
199 PM.add(getRegisterAllocator(*this));
200
Misha Brukman1be1a232003-11-13 00:16:28 +0000201 PM.add(createPrologEpilogInsertionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000202
203 if (!DisablePeephole)
204 PM.add(createPeepholeOptsPass(*this));
205
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000206 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000207 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000208
209 // Output assembly language to the .s file. Assembly emission is split into
210 // two parts: Function output and Global value output. This is because
211 // function output is pipelined with all of the rest of code generation stuff,
212 // allowing machine code representations for functions to be free'd after the
213 // function has been emitted.
214 //
Misha Brukman1be1a232003-11-13 00:16:28 +0000215 PM.add(createAsmPrinterPass(Out, *this));
Chris Lattner227c3d32002-10-28 01:12:41 +0000216 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000217
Chris Lattner4f946372002-10-28 01:03:43 +0000218 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000219 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000220 PM.add(createBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000221
Chris Lattner63342052002-10-29 21:12:46 +0000222 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000223}
Misha Brukman86172ab2003-05-27 22:24:48 +0000224
225// addPassesToJITCompile - This method controls the JIT method of code
226// generation for the UltraSparc.
227//
Brian Gaekee69f7272003-08-14 06:04:59 +0000228bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000229 const TargetData &TD = getTargetData();
230
231 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
232 TD.getPointerAlignment(), TD.getDoubleAlignment()));
233
234 // Replace malloc and free instructions with library calls.
235 // Do this after tracing until lli implements these lib calls.
236 // For now, it will emulate malloc and free internally.
237 PM.add(createLowerAllocationsPass());
238
Misha Brukman86172ab2003-05-27 22:24:48 +0000239 // FIXME: implement the switch instruction in the instruction selector.
240 PM.add(createLowerSwitchPass());
241
Chris Lattnerd324e252003-10-05 19:16:09 +0000242 // FIXME: implement the invoke/unwind instructions!
243 PM.add(createLowerInvokePass());
244
Misha Brukman58ba50f2003-08-06 23:06:21 +0000245 // decompose multi-dimensional array references into single-dim refs
246 PM.add(createDecomposeMultiDimRefsPass());
247
Misha Brukman86172ab2003-05-27 22:24:48 +0000248 // Construct and initialize the MachineFunction object for this fn.
249 PM.add(createMachineCodeConstructionPass(*this));
250
Misha Brukman35188172003-11-07 20:33:25 +0000251 // Specialize LLVM code for this target machine and then
252 // run basic dataflow optimizations on LLVM code.
253 PM.add(createPreSelectionPass(*this));
254 // Run basic dataflow optimizations on LLVM code
255 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000256
257 // FIXME: these passes crash the FunctionPassManager when being added...
258 //PM.add(createLICMPass());
259 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000260
Misha Brukman86172ab2003-05-27 22:24:48 +0000261 PM.add(createInstructionSelectionPass(*this));
262
Misha Brukman86172ab2003-05-27 22:24:48 +0000263 PM.add(getRegisterAllocator(*this));
Misha Brukman1be1a232003-11-13 00:16:28 +0000264 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000265
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000266 if (!DisablePeephole)
267 PM.add(createPeepholeOptsPass(*this));
268
Misha Brukman86172ab2003-05-27 22:24:48 +0000269 return false; // success!
270}
Brian Gaeked0fde302003-11-11 22:41:34 +0000271
272} // End llvm namespace