Clean up a lot of the code I added yesterday by exposing the IntrinsicLowering
[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/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 "SparcInternals.h"
30 #include "SparcTargetMachine.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::SparcMachineInstrDesc[] = {
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 "SparcInstr.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   cl::opt<bool> DumpInput("dump-input",
64                           cl::desc("Print bytecode before code generation"),
65                           cl::Hidden);
66 }
67
68 //===---------------------------------------------------------------------===//
69 // Code generation/destruction passes
70 //===---------------------------------------------------------------------===//
71
72 namespace {
73   class ConstructMachineFunction : public FunctionPass {
74     TargetMachine &Target;
75   public:
76     ConstructMachineFunction(TargetMachine &T) : Target(T) {}
77     
78     const char *getPassName() const {
79       return "ConstructMachineFunction";
80     }
81     
82     bool runOnFunction(Function &F) {
83       MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
84       return false;
85     }
86   };
87
88   struct DestroyMachineFunction : public FunctionPass {
89     const char *getPassName() const { return "FreeMachineFunction"; }
90     
91     static void freeMachineCode(Instruction &I) {
92       MachineCodeForInstruction::destroy(&I);
93     }
94     
95     bool runOnFunction(Function &F) {
96       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
97         for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
98           MachineCodeForInstruction::get(I).dropAllReferences();
99       
100       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
101         for_each(FI->begin(), FI->end(), freeMachineCode);
102       
103       MachineFunction::destruct(&F);
104       return false;
105     }
106   };
107   
108   FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
109     return new ConstructMachineFunction(Target);
110   }
111 }
112
113 FunctionPass *llvm::createSparcMachineCodeDestructionPass() {
114   return new DestroyMachineFunction();
115 }
116
117
118 SparcTargetMachine::SparcTargetMachine(IntrinsicLowering *il)
119   : TargetMachine("UltraSparc-Native", il, false),
120     schedInfo(*this),
121     regInfo(*this),
122     frameInfo(*this),
123     cacheInfo(*this),
124     jitInfo(*this) {
125 }
126
127 // addPassesToEmitAssembly - This method controls the entire code generation
128 // process for the ultra sparc.
129 //
130 bool
131 SparcTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
132 {
133   // The following 3 passes used to be inserted specially by llc.
134   // Replace malloc and free instructions with library calls.
135   PM.add(createLowerAllocationsPass());
136   
137   // Strip all of the symbols from the bytecode so that it will be smaller...
138   if (!DisableStrip)
139     PM.add(createSymbolStrippingPass());
140
141   // FIXME: implement the switch instruction in the instruction selector.
142   PM.add(createLowerSwitchPass());
143
144   // FIXME: implement the invoke/unwind instructions!
145   PM.add(createLowerInvokePass());
146   
147   // decompose multi-dimensional array references into single-dim refs
148   PM.add(createDecomposeMultiDimRefsPass());
149   
150   // Construct and initialize the MachineFunction object for this fn.
151   PM.add(createMachineCodeConstructionPass(*this));
152
153   //Insert empty stackslots in the stack frame of each function
154   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
155   PM.add(createStackSlotsPass(*this));
156
157   // Specialize LLVM code for this target machine
158   PM.add(createPreSelectionPass(*this));
159   // Run basic dataflow optimizations on LLVM code
160   PM.add(createReassociatePass());
161   PM.add(createLICMPass());
162   PM.add(createGCSEPass());
163
164   // If LLVM dumping after transformations is requested, add it to the pipeline
165   if (DumpInput)
166     PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
167                                  &std::cerr));
168
169   PM.add(createInstructionSelectionPass(*this));
170
171   if (!DisableSched)
172     PM.add(createInstructionSchedulingWithSSAPass(*this));
173
174   PM.add(getRegisterAllocator(*this));
175
176   PM.add(createPrologEpilogInsertionPass());
177
178   if (!DisablePeephole)
179     PM.add(createPeepholeOptsPass(*this));
180
181   if (EmitMappingInfo)
182     PM.add(getMappingInfoAsmPrinterPass(Out));  
183
184   // Output assembly language to the .s file.  Assembly emission is split into
185   // two parts: Function output and Global value output.  This is because
186   // function output is pipelined with all of the rest of code generation stuff,
187   // allowing machine code representations for functions to be free'd after the
188   // function has been emitted.
189   //
190   PM.add(createAsmPrinterPass(Out, *this));
191   PM.add(createSparcMachineCodeDestructionPass()); // Free mem no longer needed
192
193   // Emit bytecode to the assembly file into its special section next
194   if (EmitMappingInfo)
195     PM.add(createBytecodeAsmPrinterPass(Out));
196
197   return false;
198 }
199
200 // addPassesToJITCompile - This method controls the JIT method of code
201 // generation for the UltraSparc.
202 //
203 void SparcJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
204   const TargetData &TD = TM.getTargetData();
205
206   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
207                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
208
209   // Replace malloc and free instructions with library calls.
210   // Do this after tracing until lli implements these lib calls.
211   // For now, it will emulate malloc and free internally.
212   PM.add(createLowerAllocationsPass());
213
214   // FIXME: implement the switch instruction in the instruction selector.
215   PM.add(createLowerSwitchPass());
216
217   // FIXME: implement the invoke/unwind instructions!
218   PM.add(createLowerInvokePass());
219
220   // decompose multi-dimensional array references into single-dim refs
221   PM.add(createDecomposeMultiDimRefsPass());
222   
223   // Construct and initialize the MachineFunction object for this fn.
224   PM.add(createMachineCodeConstructionPass(TM));
225
226   // Specialize LLVM code for this target machine and then
227   // run basic dataflow optimizations on LLVM code.
228   PM.add(createPreSelectionPass(TM));
229   // Run basic dataflow optimizations on LLVM code
230   PM.add(createReassociatePass());
231
232   // FIXME: these passes crash the FunctionPassManager when being added...
233   //PM.add(createLICMPass());
234   //PM.add(createGCSEPass());
235
236   PM.add(createInstructionSelectionPass(TM));
237
238   PM.add(getRegisterAllocator(TM));
239   PM.add(createPrologEpilogInsertionPass());
240
241   if (!DisablePeephole)
242     PM.add(createPeepholeOptsPass(TM));
243 }
244
245 //----------------------------------------------------------------------------
246 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
247 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
248 //----------------------------------------------------------------------------
249
250 TargetMachine *llvm::allocateSparcTargetMachine(const Module &M,
251                                                 IntrinsicLowering *IL) {
252   return new SparcTargetMachine(IL);
253 }