Turn off the SparcV9MachineCodeDestructionPass for now, because it's buggy
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9TargetMachine.cpp
1 //===-- SparcV9.cpp - General implementation file for the SparcV9 Target ------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // Primary interface to machine description for the UltraSPARC.  Primarily just
11 // initializes machine-dependent parameters in class TargetMachine, and creates
12 // machine-dependent subclasses for classes such as TargetInstrInfo.
13 // 
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Function.h"
17 #include "llvm/IntrinsicLowering.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Assembly/PrintModulePass.h"
20 #include "llvm/CodeGen/InstrSelection.h"
21 #include "llvm/CodeGen/InstrScheduling.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionInfo.h"
24 #include "llvm/CodeGen/MachineCodeForInstruction.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/Target/TargetMachineImpls.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "MappingInfo.h" 
29 #include "SparcV9Internals.h"
30 #include "SparcV9TargetMachine.h"
31 #include "Support/CommandLine.h"
32
33 using namespace llvm;
34
35 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
36 // Build the MachineInstruction Description Array...
37 const TargetInstrDescriptor llvm::SparcV9MachineInstrDesc[] = {
38 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
39           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS)             \
40   { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE,             \
41           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0,          \
42           ImplicitRegUseList, ImplicitRegUseList },
43 #include "SparcV9Instr.def"
44 };
45
46 //---------------------------------------------------------------------------
47 // Command line options to control choice of code generation passes.
48 //---------------------------------------------------------------------------
49
50 namespace {
51   cl::opt<bool> DisableSched("disable-sched",
52                              cl::desc("Disable local scheduling pass"));
53
54   cl::opt<bool> DisablePeephole("disable-peephole",
55                                 cl::desc("Disable peephole optimization pass"));
56
57   cl::opt<bool> EmitMappingInfo("enable-maps",
58                  cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
59
60   cl::opt<bool> DisableStrip("disable-strip",
61                       cl::desc("Do not strip the LLVM bytecode in executable"));
62 }
63
64 //===---------------------------------------------------------------------===//
65 // Code generation/destruction passes
66 //===---------------------------------------------------------------------===//
67
68 namespace {
69   class ConstructMachineFunction : public FunctionPass {
70     TargetMachine &Target;
71   public:
72     ConstructMachineFunction(TargetMachine &T) : Target(T) {}
73     
74     const char *getPassName() const {
75       return "ConstructMachineFunction";
76     }
77     
78     bool runOnFunction(Function &F) {
79       MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
80       return false;
81     }
82   };
83
84   struct DestroyMachineFunction : public FunctionPass {
85     const char *getPassName() const { return "DestroyMachineFunction"; }
86     
87     static void freeMachineCode(Instruction &I) {
88       MachineCodeForInstruction::destroy(&I);
89     }
90     
91     bool runOnFunction(Function &F) {
92       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
93         for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
94           MachineCodeForInstruction::get(I).dropAllReferences();
95       
96       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
97         for_each(FI->begin(), FI->end(), freeMachineCode);
98       
99       MachineFunction::destruct(&F);
100       return false;
101     }
102   };
103   
104   FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
105     return new ConstructMachineFunction(Target);
106   }
107 }
108
109 FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
110   return new DestroyMachineFunction();
111 }
112
113
114 SparcV9TargetMachine::SparcV9TargetMachine(IntrinsicLowering *il)
115   : TargetMachine("UltraSparcV9-Native", il, false),
116     schedInfo(*this),
117     regInfo(*this),
118     frameInfo(*this),
119     cacheInfo(*this),
120     jitInfo(*this) {
121 }
122
123 /// addPassesToEmitAssembly - This method controls the entire code generation
124 /// process for the ultra sparc.
125 ///
126 bool
127 SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
128 {
129   // The following 3 passes used to be inserted specially by llc.
130   // Replace malloc and free instructions with library calls.
131   PM.add(createLowerAllocationsPass());
132   
133   // Strip all of the symbols from the bytecode so that it will be smaller...
134   if (!DisableStrip)
135     PM.add(createSymbolStrippingPass());
136
137   // FIXME: implement the switch instruction in the instruction selector.
138   PM.add(createLowerSwitchPass());
139
140   // FIXME: implement the invoke/unwind instructions!
141   PM.add(createLowerInvokePass());
142   
143   // decompose multi-dimensional array references into single-dim refs
144   PM.add(createDecomposeMultiDimRefsPass());
145   
146   // Construct and initialize the MachineFunction object for this fn.
147   PM.add(createMachineCodeConstructionPass(*this));
148
149   //Insert empty stackslots in the stack frame of each function
150   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
151   PM.add(createStackSlotsPass(*this));
152
153   // Specialize LLVM code for this target machine and then
154   // run basic dataflow optimizations on LLVM code.
155   PM.add(createPreSelectionPass(*this));
156   PM.add(createReassociatePass());
157   PM.add(createLICMPass());
158   PM.add(createGCSEPass());
159
160   PM.add(createInstructionSelectionPass(*this));
161
162   if (!DisableSched)
163     PM.add(createInstructionSchedulingWithSSAPass(*this));
164
165   PM.add(getRegisterAllocator(*this));
166   PM.add(createPrologEpilogInsertionPass());
167
168   if (!DisablePeephole)
169     PM.add(createPeepholeOptsPass(*this));
170
171   if (EmitMappingInfo)
172     PM.add(getMappingInfoAsmPrinterPass(Out));  
173
174   // Output assembly language to the .s file.  Assembly emission is split into
175   // two parts: Function output and Global value output.  This is because
176   // function output is pipelined with all of the rest of code generation stuff,
177   // allowing machine code representations for functions to be free'd after the
178   // function has been emitted.
179   PM.add(createAsmPrinterPass(Out, *this));
180
181   // FIXME: this pass crashes if added; there is a double deletion going on
182   // somewhere inside it. This is caught when running the SparcV9 code generator
183   // on X86, but is typically ignored when running natively.
184   // Free machine-code IR which is no longer needed:
185   // PM.add(createSparcV9MachineCodeDestructionPass());
186
187   // Emit bytecode to the assembly file into its special section next
188   if (EmitMappingInfo)
189     PM.add(createBytecodeAsmPrinterPass(Out));
190
191   return false;
192 }
193
194 /// addPassesToJITCompile - This method controls the JIT method of code
195 /// generation for the UltraSparcV9.
196 ///
197 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
198   const TargetData &TD = TM.getTargetData();
199
200   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
201                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
202
203   // Replace malloc and free instructions with library calls.
204   // Do this after tracing until lli implements these lib calls.
205   // For now, it will emulate malloc and free internally.
206   PM.add(createLowerAllocationsPass());
207
208   // FIXME: implement the switch instruction in the instruction selector.
209   PM.add(createLowerSwitchPass());
210
211   // FIXME: implement the invoke/unwind instructions!
212   PM.add(createLowerInvokePass());
213
214   // decompose multi-dimensional array references into single-dim refs
215   PM.add(createDecomposeMultiDimRefsPass());
216   
217   // Construct and initialize the MachineFunction object for this fn.
218   PM.add(createMachineCodeConstructionPass(TM));
219
220   // Specialize LLVM code for this target machine and then
221   // run basic dataflow optimizations on LLVM code.
222   PM.add(createPreSelectionPass(TM));
223   PM.add(createReassociatePass());
224   // FIXME: these passes crash the FunctionPassManager when being added...
225   //PM.add(createLICMPass());
226   //PM.add(createGCSEPass());
227
228   PM.add(createInstructionSelectionPass(TM));
229
230   PM.add(getRegisterAllocator(TM));
231   PM.add(createPrologEpilogInsertionPass());
232
233   if (!DisablePeephole)
234     PM.add(createPeepholeOptsPass(TM));
235 }
236
237 /// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
238 /// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
239 ///
240 TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
241                                                 IntrinsicLowering *IL) {
242   return new SparcV9TargetMachine(IL);
243 }