Rename -emitmaps to -enable-maps
[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/PreSelection.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/Target/TargetMachineImpls.h"
23 #include "Support/CommandLine.h"
24
25 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
26 // Build the MachineInstruction Description Array...
27 const TargetInstrDescriptor 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, 0,          \
32           ImplicitRegUseList, ImplicitRegUseList },
33 #include "SparcInstr.def"
34 };
35
36 //---------------------------------------------------------------------------
37 // Command line options to control choice of code generation passes.
38 //---------------------------------------------------------------------------
39
40 static cl::opt<bool> DisablePreOpt("disable-preopt",
41               cl::desc("Disable optimizations prior to instruction selection"));
42
43 static cl::opt<bool> DisableSched("disable-sched",
44                                   cl::desc("Disable local scheduling pass"));
45
46 static cl::opt<bool> DisablePeephole("disable-peephole",
47                                 cl::desc("Disable peephole optimization pass"));
48
49 static cl::opt<bool> EmitMappingInfo("enable-maps",
50              cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
51
52 static cl::opt<bool> DisableStrip("disable-strip",
53              cl::desc("Do not strip the LLVM bytecode included in executable"));
54
55 static cl::opt<bool> DumpInput("dump-input",
56                       cl::desc("Print bytecode before native code generation"),
57                       cl::Hidden);
58
59 //----------------------------------------------------------------------------
60 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
61 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
62 //----------------------------------------------------------------------------
63
64 TargetMachine *allocateSparcTargetMachine(unsigned Configuration) {
65   return new UltraSparc();
66 }
67
68 //---------------------------------------------------------------------------
69 // class UltraSparcFrameInfo 
70 // 
71 //   Interface to stack frame layout info for the UltraSPARC.
72 //   Starting offsets for each area of the stack frame are aligned at
73 //   a multiple of getStackFrameSizeAlignment().
74 //---------------------------------------------------------------------------
75
76 int
77 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
78                                                 bool& pos) const
79 {
80   pos = false;                          // static stack area grows downwards
81   return StaticAreaOffsetFromFP;
82 }
83
84 int
85 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
86                                            bool& pos) const
87 {
88   // ensure no more auto vars are added
89   mcInfo.getInfo()->freezeAutomaticVarsArea();
90   
91   pos = false;                          // static stack area grows downwards
92   unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
93   return StaticAreaOffsetFromFP - autoVarsSize; 
94 }
95
96 int
97 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
98                                       bool& pos) const
99 {
100   MachineFunctionInfo *MFI = mcInfo.getInfo();
101   MFI->freezeAutomaticVarsArea();     // ensure no more auto vars are added
102   MFI->freezeSpillsArea();            // ensure no more spill slots are added
103   
104   pos = false;                          // static stack area grows downwards
105   unsigned autoVarsSize = MFI->getAutomaticVarsSize();
106   unsigned spillAreaSize = MFI->getRegSpillsSize();
107   int offset = autoVarsSize + spillAreaSize;
108   return StaticAreaOffsetFromFP - offset;
109 }
110
111 int
112 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
113                                           bool& pos) const
114 {
115   // Dynamic stack area grows downwards starting at top of opt-args area.
116   // The opt-args, required-args, and register-save areas are empty except
117   // during calls and traps, so they are shifted downwards on each
118   // dynamic-size alloca.
119   pos = false;
120   unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
121   if (int extra = optArgsSize % getStackFrameSizeAlignment())
122     optArgsSize += (getStackFrameSizeAlignment() - extra);
123   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
124   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
125   return offset;
126 }
127
128 //---------------------------------------------------------------------------
129 // class UltraSparcMachine 
130 // 
131 // Purpose:
132 //   Primary interface to machine description for the UltraSPARC.
133 //   Primarily just initializes machine-dependent parameters in
134 //   class TargetMachine, and creates machine-dependent subclasses
135 //   for classes such as TargetInstrInfo. 
136 // 
137 //---------------------------------------------------------------------------
138
139 UltraSparc::UltraSparc()
140   : TargetMachine("UltraSparc-Native", false),
141     schedInfo(*this),
142     regInfo(*this),
143     frameInfo(*this),
144     cacheInfo(*this),
145     optInfo(*this) {
146 }
147
148
149 // addPassesToEmitAssembly - This method controls the entire code generation
150 // process for the ultra sparc.
151 //
152 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
153 {
154   // The following 3 passes used to be inserted specially by llc.
155   // Replace malloc and free instructions with library calls.
156   PM.add(createLowerAllocationsPass());
157   
158   // Strip all of the symbols from the bytecode so that it will be smaller...
159   if (!DisableStrip)
160     PM.add(createSymbolStrippingPass());
161
162   // FIXME: implement the switch instruction in the instruction selector.
163   PM.add(createLowerSwitchPass());
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 instsr. 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(getMappingInfoCollector(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(getEmitBytecodeToAsmPass(Out));
219     PM.add(getFunctionInfo(Out)); 
220   }
221
222   return false;
223 }
224
225 // addPassesToJITCompile - This method controls the JIT method of code
226 // generation for the UltraSparc.
227 //
228 bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
229   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
239   // FIXME: implement the switch instruction in the instruction selector.
240   PM.add(createLowerSwitchPass());
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 }