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