Add this back, as its absence introduces assertions, and it seems to work now
[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     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   // The following 3 passes used to be inserted specially by llc.
129   // Replace malloc and free instructions with library calls.
130   PM.add(createLowerAllocationsPass());
131   
132   // Strip all of the symbols from the bytecode so that it will be smaller...
133   if (!DisableStrip)
134     PM.add(createSymbolStrippingPass());
135
136   // FIXME: implement the switch instruction in the instruction selector.
137   PM.add(createLowerSwitchPass());
138
139   // FIXME: implement the invoke/unwind instructions!
140   PM.add(createLowerInvokePass());
141   
142   // decompose multi-dimensional array references into single-dim refs
143   PM.add(createDecomposeMultiDimRefsPass());
144   
145   // Construct and initialize the MachineFunction object for this fn.
146   PM.add(createMachineCodeConstructionPass(*this));
147
148   //Insert empty stackslots in the stack frame of each function
149   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
150   PM.add(createStackSlotsPass(*this));
151
152   // Specialize LLVM code for this target machine and then
153   // run basic dataflow optimizations on LLVM code.
154   PM.add(createPreSelectionPass(*this));
155   PM.add(createReassociatePass());
156   PM.add(createLICMPass());
157   PM.add(createGCSEPass());
158
159   PM.add(createInstructionSelectionPass(*this));
160
161   if (!DisableSched)
162     PM.add(createInstructionSchedulingWithSSAPass(*this));
163
164   PM.add(getRegisterAllocator(*this));
165   PM.add(createPrologEpilogInsertionPass());
166
167   if (!DisablePeephole)
168     PM.add(createPeepholeOptsPass(*this));
169
170   if (EmitMappingInfo)
171     PM.add(getMappingInfoAsmPrinterPass(Out));  
172
173   // Output assembly language to the .s file.  Assembly emission is split into
174   // two parts: Function output and Global value output.  This is because
175   // function output is pipelined with all of the rest of code generation stuff,
176   // allowing machine code representations for functions to be free'd after the
177   // function has been emitted.
178   PM.add(createAsmPrinterPass(Out, *this));
179
180   // Free machine-code IR which is no longer needed:
181   PM.add(createSparcV9MachineCodeDestructionPass());
182
183   // Emit bytecode to the assembly file into its special section next
184   if (EmitMappingInfo)
185     PM.add(createBytecodeAsmPrinterPass(Out));
186
187   return false;
188 }
189
190 /// addPassesToJITCompile - This method controls the JIT method of code
191 /// generation for the UltraSparcV9.
192 ///
193 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
194   const TargetData &TD = TM.getTargetData();
195
196   PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
197                         TD.getPointerAlignment(), TD.getDoubleAlignment()));
198
199   // Replace malloc and free instructions with library calls.
200   // Do this after tracing until lli implements these lib calls.
201   // For now, it will emulate malloc and free internally.
202   PM.add(createLowerAllocationsPass());
203
204   // FIXME: implement the switch instruction in the instruction selector.
205   PM.add(createLowerSwitchPass());
206
207   // FIXME: implement the invoke/unwind instructions!
208   PM.add(createLowerInvokePass());
209
210   // decompose multi-dimensional array references into single-dim refs
211   PM.add(createDecomposeMultiDimRefsPass());
212   
213   // Construct and initialize the MachineFunction object for this fn.
214   PM.add(createMachineCodeConstructionPass(TM));
215
216   // Specialize LLVM code for this target machine and then
217   // run basic dataflow optimizations on LLVM code.
218   PM.add(createPreSelectionPass(TM));
219   PM.add(createReassociatePass());
220   // FIXME: these passes crash the FunctionPassManager when being added...
221   //PM.add(createLICMPass());
222   //PM.add(createGCSEPass());
223
224   PM.add(createInstructionSelectionPass(TM));
225
226   PM.add(getRegisterAllocator(TM));
227   PM.add(createPrologEpilogInsertionPass());
228
229   if (!DisablePeephole)
230     PM.add(createPeepholeOptsPass(TM));
231 }
232
233 /// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
234 /// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
235 ///
236 TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
237                                                 IntrinsicLowering *IL) {
238   return new SparcV9TargetMachine(IL);
239 }