blob: e8f9c9ee728c6a0794827e794222c17e8f669e1e [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 Lattner4f946372002-10-28 01:03:43 +000014#include "llvm/CodeGen/PreSelection.h"
15#include "llvm/CodeGen/StackSlots.h"
16#include "llvm/CodeGen/PeepholeOpts.h"
17#include "llvm/CodeGen/InstrSelection.h"
18#include "llvm/CodeGen/InstrScheduling.h"
19#include "llvm/CodeGen/RegisterAllocation.h"
20#include "llvm/CodeGen/MachineCodeForInstruction.h"
21#include "llvm/Reoptimizer/Mapping/MappingInfo.h"
22#include "llvm/Reoptimizer/Mapping/FInfo.h"
23#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::cerr;
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000025
Chris Lattner9a3d63b2001-09-19 15:56:23 +000026// Build the MachineInstruction Description Array...
27const MachineInstrDescriptor SparcMachineInstrDesc[] = {
28#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
30 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
31 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
32#include "SparcInstr.def"
33};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000034
Chris Lattner4f946372002-10-28 01:03:43 +000035//---------------------------------------------------------------------------
36// Command line options to control choice of code generation passes.
37//---------------------------------------------------------------------------
38
39static cl::opt<bool> DisablePreSelect("nopreselect",
40 cl::desc("Disable preselection pass"));
41
42static cl::opt<bool> DisableSched("nosched",
43 cl::desc("Disable local scheduling pass"));
44
45static cl::opt<bool> DisablePeephole("nopeephole",
46 cl::desc("Disable peephole optimization pass"));
47
Vikram S. Adve0fb49802001-09-18 13:01:29 +000048//----------------------------------------------------------------------------
Chris Lattner46cbff62001-09-14 16:56:32 +000049// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
50// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
Vikram S. Adve0fb49802001-09-18 13:01:29 +000051//----------------------------------------------------------------------------
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000052
Chris Lattner46cbff62001-09-14 16:56:32 +000053TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
Chris Lattner20b1ea02001-09-14 03:47:57 +000054
55
Vikram S. Adve9db43182001-10-22 13:44:23 +000056
Vikram S. Adve9db43182001-10-22 13:44:23 +000057//---------------------------------------------------------------------------
58// class UltraSparcFrameInfo
59//
60// Purpose:
61// 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{
Vikram S. Adve0bc05162002-04-25 04:43:45 +000078 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
79
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000080 pos = false; // static stack area grows downwards
81 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000082 return StaticAreaOffsetFromFP - autoVarsSize;
Vikram S. Adve9db43182001-10-22 13:44:23 +000083}
84
85int
Misha Brukmanfce11432002-10-28 00:28:31 +000086UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000087 bool& pos) const
Vikram S. Adve9db43182001-10-22 13:44:23 +000088{
Vikram S. Adve0bc05162002-04-25 04:43:45 +000089 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
90 mcInfo.freezeSpillsArea(); // ensure no more spill slots are added
91
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000092 pos = false; // static stack area grows downwards
93 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
94 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
Vikram S. Adve00521d72001-11-12 23:26:35 +000095 int offset = autoVarsSize + spillAreaSize;
Vikram S. Adve00521d72001-11-12 23:26:35 +000096 return StaticAreaOffsetFromFP - offset;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000097}
98
99int
Misha Brukmanfce11432002-10-28 00:28:31 +0000100UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000101 bool& pos) const
102{
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000103 // Dynamic stack area grows downwards starting at top of opt-args area.
104 // The opt-args, required-args, and register-save areas are empty except
105 // during calls and traps, so they are shifted downwards on each
106 // dynamic-size alloca.
107 pos = false;
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000108 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
Vikram S. Advee1f72802002-09-16 15:39:26 +0000109 if (int extra = optArgsSize % getStackFrameSizeAlignment())
110 optArgsSize += (getStackFrameSizeAlignment() - extra);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000111 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
Vikram S. Advee6d2c412002-03-18 03:08:07 +0000112 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
Vikram S. Adve00521d72001-11-12 23:26:35 +0000113 return offset;
Vikram S. Adve9db43182001-10-22 13:44:23 +0000114}
115
Chris Lattner20b1ea02001-09-14 03:47:57 +0000116//---------------------------------------------------------------------------
117// class UltraSparcMachine
118//
119// Purpose:
120// Primary interface to machine description for the UltraSPARC.
121// Primarily just initializes machine-dependent parameters in
122// class TargetMachine, and creates machine-dependent subclasses
123// for classes such as MachineInstrInfo.
124//
125//---------------------------------------------------------------------------
126
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000127UltraSparc::UltraSparc()
128 : TargetMachine("UltraSparc-Native"),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000129 schedInfo(*this),
130 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000131 frameInfo(*this),
Vikram S. Adved55697c2002-09-20 00:52:09 +0000132 cacheInfo(*this),
133 optInfo(*this)
Vikram S. Adve0fb49802001-09-18 13:01:29 +0000134{
Chris Lattner20b1ea02001-09-14 03:47:57 +0000135 optSizeForSubWordData = 4;
136 minMemOpWordSize = 8;
137 maxAtomicMemOpWordSize = 8;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000138}
139
Chris Lattner4f946372002-10-28 01:03:43 +0000140
141// addPassesToEmitAssembly - This method controls the entire code generation
142// process for the ultra sparc.
143//
Chris Lattner63342052002-10-29 21:12:46 +0000144bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000145{
146 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000147 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000148
149 //Insert empty stackslots in the stack frame of each function
150 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
151 PM.add(createStackSlotsPass(*this));
152
153 // Specialize LLVM code for this target machine and then
154 // run basic dataflow optimizations on LLVM code.
155 if (!DisablePreSelect)
156 {
157 PM.add(createPreSelectionPass(*this));
158 /* PM.add(createReassociatePass()); */
159 PM.add(createLICMPass());
160 PM.add(createGCSEPass());
161 }
162
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
175 PM.add(MappingInfoForFunction(Out));
176
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}