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