Add support for the Invoke instruction by using the LowerInvoke pass
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9TargetMachine.cpp
1 //===-- 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 //===----------------------------------------------------------------------===//
7
8 #include "SparcInternals.h"
9 #include "MappingInfo.h" 
10 #include "llvm/Function.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Assembly/PrintModulePass.h"
13 #include "llvm/Transforms/Scalar.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineFunctionInfo.h"
16 #include "llvm/CodeGen/InstrSelection.h"
17 #include "llvm/CodeGen/InstrScheduling.h"
18 #include "llvm/CodeGen/MachineCodeForInstruction.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Target/TargetMachineImpls.h"
21 #include "Support/CommandLine.h"
22
23 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
24 // Build the MachineInstruction Description Array...
25 const TargetInstrDescriptor SparcMachineInstrDesc[] = {
26 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
27           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS)             \
28   { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE,             \
29           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0,          \
30           ImplicitRegUseList, ImplicitRegUseList },
31 #include "SparcInstr.def"
32 };
33
34 //---------------------------------------------------------------------------
35 // Command line options to control choice of code generation passes.
36 //---------------------------------------------------------------------------
37
38 static cl::opt<bool> DisablePreOpt("disable-preopt",
39               cl::desc("Disable optimizations prior to instruction selection"));
40
41 static cl::opt<bool> DisableSched("disable-sched",
42                                   cl::desc("Disable local scheduling pass"));
43
44 static cl::opt<bool> DisablePeephole("disable-peephole",
45                                 cl::desc("Disable peephole optimization pass"));
46
47 static cl::opt<bool> EmitMappingInfo("enable-maps",
48              cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
49
50 static cl::opt<bool> DisableStrip("disable-strip",
51              cl::desc("Do not strip the LLVM bytecode included in executable"));
52
53 static cl::opt<bool> DumpInput("dump-input",
54                       cl::desc("Print bytecode before native code generation"),
55                       cl::Hidden);
56
57 //----------------------------------------------------------------------------
58 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
59 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
60 //----------------------------------------------------------------------------
61
62 TargetMachine *allocateSparcTargetMachine(const Module &M) {
63   return new UltraSparc();
64 }
65
66 //---------------------------------------------------------------------------
67 // class UltraSparcFrameInfo 
68 // 
69 //   Interface to stack frame layout info for the UltraSPARC.
70 //   Starting offsets for each area of the stack frame are aligned at
71 //   a multiple of getStackFrameSizeAlignment().
72 //---------------------------------------------------------------------------
73
74 int
75 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
76                                                 bool& pos) const
77 {
78   pos = false;                          // static stack area grows downwards
79   return StaticAreaOffsetFromFP;
80 }
81
82 int
83 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
84                                            bool& pos) const
85 {
86   // ensure no more auto vars are added
87   mcInfo.getInfo()->freezeAutomaticVarsArea();
88   
89   pos = false;                          // static stack area grows downwards
90   unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
91   return StaticAreaOffsetFromFP - autoVarsSize; 
92 }
93
94 int
95 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
96                                       bool& pos) const
97 {
98   MachineFunctionInfo *MFI = mcInfo.getInfo();
99   MFI->freezeAutomaticVarsArea();     // ensure no more auto vars are added
100   MFI->freezeSpillsArea();            // ensure no more spill slots are added
101   
102   pos = false;                          // static stack area grows downwards
103   unsigned autoVarsSize = MFI->getAutomaticVarsSize();
104   unsigned spillAreaSize = MFI->getRegSpillsSize();
105   int offset = autoVarsSize + spillAreaSize;
106   return StaticAreaOffsetFromFP - offset;
107 }
108
109 int
110 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
111                                           bool& pos) const
112 {
113   // Dynamic stack area grows downwards starting at top of opt-args area.
114   // The opt-args, required-args, and register-save areas are empty except
115   // during calls and traps, so they are shifted downwards on each
116   // dynamic-size alloca.
117   pos = false;
118   unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
119   if (int extra = optArgsSize % getStackFrameSizeAlignment())
120     optArgsSize += (getStackFrameSizeAlignment() - extra);
121   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
122   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
123   return offset;
124 }
125
126 //---------------------------------------------------------------------------
127 // class UltraSparcMachine 
128 // 
129 // Purpose:
130 //   Primary interface to machine description for the UltraSPARC.
131 //   Primarily just initializes machine-dependent parameters in
132 //   class TargetMachine, and creates machine-dependent subclasses
133 //   for classes such as TargetInstrInfo. 
134 // 
135 //---------------------------------------------------------------------------
136
137 UltraSparc::UltraSparc()
138   : TargetMachine("UltraSparc-Native", false),
139     schedInfo(*this),
140     regInfo(*this),
141     frameInfo(*this),
142     cacheInfo(*this) {
143 }
144
145
146 // addPassesToEmitAssembly - This method controls the entire code generation
147 // process for the ultra sparc.
148 //
149 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
150 {
151   // The following 3 passes used to be inserted specially by llc.
152   // Replace malloc and free instructions with library calls.
153   PM.add(createLowerAllocationsPass());
154   
155   // Strip all of the symbols from the bytecode so that it will be smaller...
156   if (!DisableStrip)
157     PM.add(createSymbolStrippingPass());
158
159   // FIXME: implement the switch instruction in the instruction selector.
160   PM.add(createLowerSwitchPass());
161
162   // FIXME: implement the invoke/unwind instructions!
163   PM.add(createLowerInvokePass());
164   
165   // decompose multi-dimensional array references into single-dim refs
166   PM.add(createDecomposeMultiDimRefsPass());
167   
168   // Construct and initialize the MachineFunction object for this fn.
169   PM.add(createMachineCodeConstructionPass(*this));
170
171   //Insert empty stackslots in the stack frame of each function
172   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
173   PM.add(createStackSlotsPass(*this));
174
175   if (!DisablePreOpt) {
176     // Specialize LLVM code for this target machine
177     PM.add(createPreSelectionPass(*this));
178     // Run basic dataflow optimizations on LLVM code
179     PM.add(createReassociatePass());
180     PM.add(createLICMPass());
181     PM.add(createGCSEPass());
182   }
183   
184   // If LLVM dumping after transformations is requested, add it to the pipeline
185   if (DumpInput)
186     PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
187                                  &std::cerr));
188
189   PM.add(createInstructionSelectionPass(*this));
190
191   if (!DisableSched)
192     PM.add(createInstructionSchedulingWithSSAPass(*this));
193
194   PM.add(getRegisterAllocator(*this));
195
196   PM.add(getPrologEpilogInsertionPass());
197
198   if (!DisablePeephole)
199     PM.add(createPeepholeOptsPass(*this));
200
201   if (EmitMappingInfo)
202     PM.add(getMappingInfoAsmPrinterPass(Out));  
203
204   // Output assembly language to the .s file.  Assembly emission is split into
205   // two parts: Function output and Global value output.  This is because
206   // function output is pipelined with all of the rest of code generation stuff,
207   // allowing machine code representations for functions to be free'd after the
208   // function has been emitted.
209   //
210   PM.add(getFunctionAsmPrinterPass(Out));
211   PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
212
213   // Emit Module level assembly after all of the functions have been processed.
214   PM.add(getModuleAsmPrinterPass(Out));
215
216   // Emit bytecode to the assembly file into its special section next
217   if (EmitMappingInfo)
218     PM.add(getBytecodeAsmPrinterPass(Out));
219
220   return false;
221 }
222
223 // addPassesToJITCompile - This method controls the JIT method of code
224 // generation for the UltraSparc.
225 //
226 bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
227   const TargetData &TD = getTargetData();
228
229   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
230                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
231
232   // Replace malloc and free instructions with library calls.
233   // Do this after tracing until lli implements these lib calls.
234   // For now, it will emulate malloc and free internally.
235   PM.add(createLowerAllocationsPass());
236
237   // FIXME: implement the switch instruction in the instruction selector.
238   PM.add(createLowerSwitchPass());
239
240   // FIXME: implement the invoke/unwind instructions!
241   PM.add(createLowerInvokePass());
242
243   // decompose multi-dimensional array references into single-dim refs
244   PM.add(createDecomposeMultiDimRefsPass());
245   
246   // Construct and initialize the MachineFunction object for this fn.
247   PM.add(createMachineCodeConstructionPass(*this));
248
249   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
258   if (!DisablePeephole)
259     PM.add(createPeepholeOptsPass(*this));
260
261   return false; // success!
262 }