blob: 8304712bb97f0464dee85d3916f48b41d9f96405 [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"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000024
Chris Lattner6af20402002-12-03 05:41:54 +000025static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000026// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000027const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000028#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
30 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000031 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
32 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000033#include "SparcInstr.def"
34};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000035
Chris Lattner4f946372002-10-28 01:03:43 +000036//---------------------------------------------------------------------------
37// Command line options to control choice of code generation passes.
38//---------------------------------------------------------------------------
39
40static cl::opt<bool> DisablePreSelect("nopreselect",
41 cl::desc("Disable preselection pass"));
42
43static cl::opt<bool> DisableSched("nosched",
44 cl::desc("Disable local scheduling pass"));
45
46static cl::opt<bool> DisablePeephole("nopeephole",
47 cl::desc("Disable peephole optimization pass"));
48
Vikram S. Adve0fb49802001-09-18 13:01:29 +000049//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000050// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
51// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000052//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000053
Misha Brukman86172ab2003-05-27 22:24:48 +000054TargetMachine *allocateSparcTargetMachine(unsigned Configuration) {
55 return new UltraSparc();
56}
Vikram S. Adve9db43182001-10-22 13:44:23 +000057
Vikram S. Adve9db43182001-10-22 13:44:23 +000058//---------------------------------------------------------------------------
59// class UltraSparcFrameInfo
60//
Vikram S. Adve9db43182001-10-22 13:44:23 +000061// Interface to stack frame layout info for the UltraSPARC.
Vikram S. Adve00521d72001-11-12 23:26:35 +000062// Starting offsets for each area of the stack frame are aligned at
63// a multiple of getStackFrameSizeAlignment().
Vikram S. Adve9db43182001-10-22 13:44:23 +000064//---------------------------------------------------------------------------
65
66int
Misha Brukmanfce11432002-10-28 00:28:31 +000067UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000068 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000069{
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000070 pos = false; // static stack area grows downwards
71 return StaticAreaOffsetFromFP;
Vikram S. Adve9db43182001-10-22 13:44:23 +000072}
73
74int
Misha Brukmanfce11432002-10-28 00:28:31 +000075UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000076 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000077{
Chris Lattner78d881d2002-12-28 20:17:43 +000078 // ensure no more auto vars are added
79 mcInfo.getInfo()->freezeAutomaticVarsArea();
Vikram S. Adve0bc05162002-04-25 04:43:45 +000080
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000081 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000082 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000083 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000084}
85
86int
Misha Brukmanfce11432002-10-28 00:28:31 +000087UltraSparcFrameInfo::getTmpAreaOffset(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 MachineFunctionInfo *MFI = mcInfo.getInfo();
91 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
92 MFI->freezeSpillsArea(); // ensure no more spill slots are added
Vikram S. Adve0bc05162002-04-25 04:43:45 +000093
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000094 pos = false; // static stack area grows downwards
Chris Lattner78d881d2002-12-28 20:17:43 +000095 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
96 unsigned spillAreaSize = MFI->getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000097 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +000098 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000099}
100
101int
Misha Brukmanfce11432002-10-28 00:28:31 +0000102UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000103 bool& pos) const
104{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000105 // Dynamic stack area grows downwards starting at top of opt-args area.
106 // The opt-args, required-args, and register-save areas are empty except
107 // during calls and traps, so they are shifted downwards on each
108 // dynamic-size alloca.
109 pos = false;
Chris Lattner78d881d2002-12-28 20:17:43 +0000110 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000111 if (int extra = optArgsSize % getStackFrameSizeAlignment())
112 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000113 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000114 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000115 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000116}
117
Chris Lattner20b1ea02001-09-14 03:47:57 +0000118//---------------------------------------------------------------------------
119// class UltraSparcMachine
120//
121// Purpose:
122// Primary interface to machine description for the UltraSPARC.
123// Primarily just initializes machine-dependent parameters in
124// class TargetMachine, and creates machine-dependent subclasses
Chris Lattner3501fea2003-01-14 22:00:31 +0000125// for classes such as TargetInstrInfo.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000126//
127//---------------------------------------------------------------------------
128
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000129UltraSparc::UltraSparc()
Chris Lattner10daaa12003-04-26 20:11:09 +0000130 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000131 schedInfo(*this),
132 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000133 frameInfo(*this),
Vikram S. Adved55697c2002-09-20 00:52:09 +0000134 cacheInfo(*this),
Chris Lattnerc56406c2002-10-29 21:48:17 +0000135 optInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000136}
137
Chris Lattner4f946372002-10-28 01:03:43 +0000138
139// addPassesToEmitAssembly - This method controls the entire code generation
140// process for the ultra sparc.
141//
Chris Lattner63342052002-10-29 21:12:46 +0000142bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000143{
Chris Lattner155e68f2003-04-23 16:24:55 +0000144 // FIXME: implement the switch instruction in the instruction selector.
145 PM.add(createLowerSwitchPass());
146
Chris Lattner4f946372002-10-28 01:03:43 +0000147 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000148 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000149
150 //Insert empty stackslots in the stack frame of each function
151 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
152 PM.add(createStackSlotsPass(*this));
153
154 // Specialize LLVM code for this target machine and then
155 // run basic dataflow optimizations on LLVM code.
Misha Brukman86172ab2003-05-27 22:24:48 +0000156 if (!DisablePreSelect) {
157 PM.add(createPreSelectionPass(*this));
158 PM.add(createReassociatePass());
159 PM.add(createLICMPass());
160 PM.add(createGCSEPass());
161 }
Chris Lattner4f946372002-10-28 01:03:43 +0000162
163 PM.add(createInstructionSelectionPass(*this));
164
165 if (!DisableSched)
166 PM.add(createInstructionSchedulingWithSSAPass(*this));
167
168 PM.add(getRegisterAllocator(*this));
169
170 PM.add(getPrologEpilogInsertionPass());
171
172 if (!DisablePeephole)
173 PM.add(createPeepholeOptsPass(*this));
174
Brian Gaekeaeab1e12003-06-04 22:07:12 +0000175 PM.add(getMappingInfoCollector(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000176
177 // Output assembly language to the .s file. Assembly emission is split into
178 // two parts: Function output and Global value output. This is because
179 // function output is pipelined with all of the rest of code generation stuff,
180 // allowing machine code representations for functions to be free'd after the
181 // function has been emitted.
182 //
183 PM.add(getFunctionAsmPrinterPass(Out));
Chris Lattner227c3d32002-10-28 01:12:41 +0000184 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000185
186 // Emit Module level assembly after all of the functions have been processed.
187 PM.add(getModuleAsmPrinterPass(Out));
188
189 // Emit bytecode to the assembly file into its special section next
190 PM.add(getEmitBytecodeToAsmPass(Out));
191 PM.add(getFunctionInfo(Out));
Chris Lattner63342052002-10-29 21:12:46 +0000192 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000193}
Misha Brukman86172ab2003-05-27 22:24:48 +0000194
195// addPassesToJITCompile - This method controls the JIT method of code
196// generation for the UltraSparc.
197//
198bool UltraSparc::addPassesToJITCompile(PassManager &PM) {
Misha Brukmana5124502003-06-06 07:11:16 +0000199 const TargetData &TD = getTargetData();
200
201 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
202 TD.getPointerAlignment(), TD.getDoubleAlignment()));
203
204 // Replace malloc and free instructions with library calls.
205 // Do this after tracing until lli implements these lib calls.
206 // For now, it will emulate malloc and free internally.
207 PM.add(createLowerAllocationsPass());
208
Misha Brukman86172ab2003-05-27 22:24:48 +0000209 // FIXME: implement the switch instruction in the instruction selector.
210 PM.add(createLowerSwitchPass());
211
212 // Construct and initialize the MachineFunction object for this fn.
213 PM.add(createMachineCodeConstructionPass(*this));
214
Misha Brukman86172ab2003-05-27 22:24:48 +0000215 PM.add(createInstructionSelectionPass(*this));
216
217 // new pass: convert Value* in MachineOperand to an unsigned register
218 // this brings it in line with what the X86 JIT's RegisterAllocator expects
219 //PM.add(createAddRegNumToValuesPass());
220
221 PM.add(getRegisterAllocator(*this));
222 PM.add(getPrologEpilogInsertionPass());
223
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000224 if (!DisablePeephole)
225 PM.add(createPeepholeOptsPass(*this));
226
Misha Brukman86172ab2003-05-27 22:24:48 +0000227 return false; // success!
228}