Add new mapping info pass, when EmitMappingInfo is on.
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9TargetMachine.cpp
1 //===-- SparcV9TargetMachine.cpp - SparcV9 Target Machine Implementation --===//
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     jitInfo(*this) {
120 }
121
122 /// addPassesToEmitAssembly - This method controls the entire code generation
123 /// process for the ultra sparc.
124 ///
125 bool
126 SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
127 {
128   // FIXME: Implement efficient support for garbage collection intrinsics.
129   PM.add(createLowerGCPass());
130
131   // Replace malloc and free instructions with library calls.
132   PM.add(createLowerAllocationsPass());
133   
134   // FIXME: implement the switch instruction in the instruction selector.
135   PM.add(createLowerSwitchPass());
136
137   // FIXME: implement the invoke/unwind instructions!
138   PM.add(createLowerInvokePass());
139   
140   // decompose multi-dimensional array references into single-dim refs
141   PM.add(createDecomposeMultiDimRefsPass());
142
143   // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
144   PM.add(createPreSelectionPass(*this));
145   PM.add(createLowerSelectPass());
146
147   // Run basic LLVM dataflow optimizations, to clean up after pre-selection.
148   PM.add(createReassociatePass());
149   PM.add(createLICMPass());
150   PM.add(createGCSEPass());
151
152   // If the user's trying to read the generated code, they'll need to see the
153   // transformed input.
154   if (PrintMachineCode)
155     PM.add(new PrintModulePass());
156
157   // Construct and initialize the MachineFunction object for this fn.
158   PM.add(createMachineCodeConstructionPass(*this));
159
160   // Insert empty stackslots in the stack frame of each function
161   // so %fp+offset-8 and %fp+offset-16 are empty slots now!
162   PM.add(createStackSlotsPass(*this));
163   
164   PM.add(createInstructionSelectionPass(*this));
165
166   if (!DisableSched)
167     PM.add(createInstructionSchedulingWithSSAPass(*this));
168
169   if (PrintMachineCode)
170     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
171
172   PM.add(getRegisterAllocator(*this));
173
174   if (PrintMachineCode)
175     PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
176
177   PM.add(createPrologEpilogInsertionPass());
178
179   if (!DisablePeephole)
180     PM.add(createPeepholeOptsPass(*this));
181
182   if (EmitMappingInfo) {
183     PM.add(createInternalGlobalMapperPass());
184     PM.add(getMappingInfoAsmPrinterPass(Out));
185   }
186
187   // Output assembly language to the .s file.  Assembly emission is split into
188   // two parts: Function output and Global value output.  This is because
189   // function output is pipelined with all of the rest of code generation stuff,
190   // allowing machine code representations for functions to be free'd after the
191   // function has been emitted.
192   PM.add(createAsmPrinterPass(Out, *this));
193
194   // Free machine-code IR which is no longer needed:
195   PM.add(createSparcV9MachineCodeDestructionPass());
196
197   // Emit bytecode to the assembly file into its special section next
198   if (EmitMappingInfo) {
199     // Strip all of the symbols from the bytecode so that it will be smaller...
200     if (!DisableStrip)
201       PM.add(createSymbolStrippingPass());
202     PM.add(createBytecodeAsmPrinterPass(Out));
203   }
204   
205   return false;
206 }
207
208 /// addPassesToJITCompile - This method controls the JIT method of code
209 /// generation for the UltraSparcV9.
210 ///
211 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
212   // FIXME: Implement efficient support for garbage collection intrinsics.
213   PM.add(createLowerGCPass());
214
215   // Replace malloc and free instructions with library calls.
216   PM.add(createLowerAllocationsPass());
217   
218   // FIXME: implement the switch instruction in the instruction selector.
219   PM.add(createLowerSwitchPass());
220
221   // FIXME: implement the invoke/unwind instructions!
222   PM.add(createLowerInvokePass());
223   
224   // decompose multi-dimensional array references into single-dim refs
225   PM.add(createDecomposeMultiDimRefsPass());
226
227   // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
228   PM.add(createPreSelectionPass(TM));
229   PM.add(createLowerSelectPass());
230
231   // Run basic LLVM dataflow optimizations, to clean up after pre-selection.
232   PM.add(createReassociatePass());
233   // FIXME: these passes crash the FunctionPassManager when being added...
234   //PM.add(createLICMPass());
235   //PM.add(createGCSEPass());
236
237   // Construct and initialize the MachineFunction object for this fn.
238   PM.add(createMachineCodeConstructionPass(TM));
239
240   PM.add(createInstructionSelectionPass(TM));
241
242   if (PrintMachineCode)
243     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
244
245   PM.add(getRegisterAllocator(TM));
246
247   if (PrintMachineCode)
248     PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
249
250   PM.add(createPrologEpilogInsertionPass());
251
252   if (!DisablePeephole)
253     PM.add(createPeepholeOptsPass(TM));
254 }
255
256 /// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
257 /// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
258 ///
259 TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
260                                                 IntrinsicLowering *IL) {
261   return new SparcV9TargetMachine(IL);
262 }