59e84f92f1c02ea1237df5e65d1c8dcaee4df6a0
[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 // addPassesToEmitAssembly - This method controls the entire code generation
146 // process for the ultra sparc.
147 //
148 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
149 {
150   // The following 3 passes used to be inserted specially by llc.
151   // Replace malloc and free instructions with library calls.
152   PM.add(createLowerAllocationsPass());
153   
154   // Strip all of the symbols from the bytecode so that it will be smaller...
155   if (!DisableStrip)
156     PM.add(createSymbolStrippingPass());
157
158   // FIXME: implement the switch instruction in the instruction selector.
159   PM.add(createLowerSwitchPass());
160
161   // FIXME: implement the invoke/unwind instructions!
162   PM.add(createLowerInvokePass());
163   
164   // decompose multi-dimensional array references into single-dim refs
165   PM.add(createDecomposeMultiDimRefsPass());
166   
167   // Construct and initialize the MachineFunction object for this fn.
168   PM.add(createMachineCodeConstructionPass(*this));
169
170   //Insert empty stackslots in the stack frame of each function
171   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
172   PM.add(createStackSlotsPass(*this));
173
174   if (!DisablePreOpt) {
175     // Specialize LLVM code for this target machine
176     PM.add(createPreSelectionPass(*this));
177     // Run basic dataflow optimizations on LLVM code
178     PM.add(createReassociatePass());
179     PM.add(createLICMPass());
180     PM.add(createGCSEPass());
181   }
182   
183   // If LLVM dumping after transformations is requested, add it to the pipeline
184   if (DumpInput)
185     PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
186                                  &std::cerr));
187
188   PM.add(createInstructionSelectionPass(*this));
189
190   if (!DisableSched)
191     PM.add(createInstructionSchedulingWithSSAPass(*this));
192
193   PM.add(getRegisterAllocator(*this));
194
195   PM.add(getPrologEpilogInsertionPass());
196
197   if (!DisablePeephole)
198     PM.add(createPeepholeOptsPass(*this));
199
200   if (EmitMappingInfo)
201     PM.add(getMappingInfoAsmPrinterPass(Out));  
202
203   // Output assembly language to the .s file.  Assembly emission is split into
204   // two parts: Function output and Global value output.  This is because
205   // function output is pipelined with all of the rest of code generation stuff,
206   // allowing machine code representations for functions to be free'd after the
207   // function has been emitted.
208   //
209   PM.add(getFunctionAsmPrinterPass(Out));
210   PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
211
212   // Emit Module level assembly after all of the functions have been processed.
213   PM.add(getModuleAsmPrinterPass(Out));
214
215   // Emit bytecode to the assembly file into its special section next
216   if (EmitMappingInfo)
217     PM.add(getBytecodeAsmPrinterPass(Out));
218
219   return false;
220 }
221
222 // addPassesToJITCompile - This method controls the JIT method of code
223 // generation for the UltraSparc.
224 //
225 bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
226   const TargetData &TD = getTargetData();
227
228   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
229                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
230
231   // Replace malloc and free instructions with library calls.
232   // Do this after tracing until lli implements these lib calls.
233   // For now, it will emulate malloc and free internally.
234   PM.add(createLowerAllocationsPass());
235
236   // FIXME: implement the switch instruction in the instruction selector.
237   PM.add(createLowerSwitchPass());
238
239   // FIXME: implement the invoke/unwind instructions!
240   PM.add(createLowerInvokePass());
241
242   // decompose multi-dimensional array references into single-dim refs
243   PM.add(createDecomposeMultiDimRefsPass());
244   
245   // Construct and initialize the MachineFunction object for this fn.
246   PM.add(createMachineCodeConstructionPass(*this));
247
248   PM.add(createInstructionSelectionPass(*this));
249
250   // new pass: convert Value* in MachineOperand to an unsigned register
251   // this brings it in line with what the X86 JIT's RegisterAllocator expects
252   //PM.add(createAddRegNumToValuesPass());
253
254   PM.add(getRegisterAllocator(*this));
255   PM.add(getPrologEpilogInsertionPass());
256
257   if (!DisablePeephole)
258     PM.add(createPeepholeOptsPass(*this));
259
260   return false; // success!
261 }