Put back the separate pass to decompose multi-dimensional references
[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 DumpInput("dump-input",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   // Strip all of the symbols from the bytecode so that it will be smaller...
158   if (!DisableStrip)
159     PM.add(createSymbolStrippingPass());
160
161   // FIXME: implement the switch instruction in the instruction selector.
162   PM.add(createLowerSwitchPass());
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   // Specialize LLVM code for this target machine and then
175   // run basic dataflow optimizations on LLVM code.
176   if (!DisablePreSelect) {
177     PM.add(createPreSelectionPass(*this));
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", &std::cerr));
186
187   PM.add(createInstructionSelectionPass(*this));
188
189   if (!DisableSched)
190     PM.add(createInstructionSchedulingWithSSAPass(*this));
191
192   PM.add(getRegisterAllocator(*this));
193
194   PM.add(getPrologEpilogInsertionPass());
195
196   if (!DisablePeephole)
197     PM.add(createPeepholeOptsPass(*this));
198
199   PM.add(getMappingInfoCollector(Out));  
200
201   // Output assembly language to the .s file.  Assembly emission is split into
202   // two parts: Function output and Global value output.  This is because
203   // function output is pipelined with all of the rest of code generation stuff,
204   // allowing machine code representations for functions to be free'd after the
205   // function has been emitted.
206   //
207   PM.add(getFunctionAsmPrinterPass(Out));
208   PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
209
210   // Emit Module level assembly after all of the functions have been processed.
211   PM.add(getModuleAsmPrinterPass(Out));
212
213   // Emit bytecode to the assembly file into its special section next
214   PM.add(getEmitBytecodeToAsmPass(Out));
215   PM.add(getFunctionInfo(Out)); 
216   return false;
217 }
218
219 // addPassesToJITCompile - This method controls the JIT method of code
220 // generation for the UltraSparc.
221 //
222 bool UltraSparc::addPassesToJITCompile(PassManager &PM) {
223   const TargetData &TD = getTargetData();
224
225   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
226                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
227
228   // Replace malloc and free instructions with library calls.
229   // Do this after tracing until lli implements these lib calls.
230   // For now, it will emulate malloc and free internally.
231   PM.add(createLowerAllocationsPass());
232
233   // FIXME: implement the switch instruction in the instruction selector.
234   PM.add(createLowerSwitchPass());
235
236   // Construct and initialize the MachineFunction object for this fn.
237   PM.add(createMachineCodeConstructionPass(*this));
238
239   PM.add(createInstructionSelectionPass(*this));
240
241   // new pass: convert Value* in MachineOperand to an unsigned register
242   // this brings it in line with what the X86 JIT's RegisterAllocator expects
243   //PM.add(createAddRegNumToValuesPass());
244
245   PM.add(getRegisterAllocator(*this));
246   PM.add(getPrologEpilogInsertionPass());
247
248   if (!DisablePeephole)
249     PM.add(createPeepholeOptsPass(*this));
250
251   return false; // success!
252 }