clarify that these are v9 options
[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/PassManager.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/CodeGen/InstrScheduling.h"
20 #include "llvm/CodeGen/IntrinsicLowering.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetMachineRegistry.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "MappingInfo.h"
27 #include "MachineFunctionInfo.h"
28 #include "MachineCodeForInstruction.h"
29 #include "SparcV9Internals.h"
30 #include "SparcV9TargetMachine.h"
31 #include "SparcV9BurgISel.h"
32 #include "llvm/Support/CommandLine.h"
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 llvm {
51   bool EmitMappingInfo = false;
52 }
53
54 namespace {
55   cl::opt<bool> DisableSched("disable-sched",
56                              cl::desc("Disable sparcv9 local scheduling pass"));
57
58   cl::opt<bool> DisablePeephole("disable-peephole",
59                                 cl::desc("Disable sparcv9 peephole optimization pass"));
60
61   cl::opt<bool, true> EmitMappingInfoOpt("enable-maps", cl::ReallyHidden,
62                  cl::location(EmitMappingInfo),
63                  cl::init(false),
64                  cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
65
66   cl::opt<bool> EnableModSched("enable-modsched",
67          cl::desc("Enable sparcv9 modulo scheduling pass instead of local scheduling"), cl::Hidden);
68
69   // Register the target.
70   RegisterTarget<SparcV9TargetMachine> X("sparcv9", "  SPARC V9");
71 }
72
73 unsigned SparcV9TargetMachine::getJITMatchQuality() {
74 #if defined(__sparcv9)
75   return 10;
76 #else
77   return 0;
78 #endif
79 }
80
81 unsigned SparcV9TargetMachine::getModuleMatchQuality(const Module &M) {
82   // We strongly match "sparcv9-*".
83   std::string TT = M.getTargetTriple();
84   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "sparcv9-")
85     return 20;
86
87   if (M.getEndianness()  == Module::BigEndian &&
88       M.getPointerSize() == Module::Pointer64)
89     return 10;                                   // Weak match
90   else if (M.getEndianness() != Module::AnyEndianness ||
91            M.getPointerSize() != Module::AnyPointerSize)
92     return 0;                                    // Match for some other target
93
94   return getJITMatchQuality()/2;
95 }
96
97 //===---------------------------------------------------------------------===//
98 // Code generation/destruction passes
99 //===---------------------------------------------------------------------===//
100
101 namespace {
102   class ConstructMachineFunction : public FunctionPass {
103     TargetMachine &Target;
104   public:
105     ConstructMachineFunction(TargetMachine &T) : Target(T) {}
106
107     const char *getPassName() const {
108       return "ConstructMachineFunction";
109     }
110
111     bool runOnFunction(Function &F) {
112       MachineFunction::construct(&F, Target).getInfo<SparcV9FunctionInfo>()->CalculateArgSize();
113       return false;
114     }
115   };
116
117   struct DestroyMachineFunction : public FunctionPass {
118     const char *getPassName() const { return "DestroyMachineFunction"; }
119
120     static void freeMachineCode(Instruction &I) {
121       MachineCodeForInstruction::destroy(&I);
122     }
123
124     bool runOnFunction(Function &F) {
125       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
126         for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
127           MachineCodeForInstruction::get(I).dropAllReferences();
128
129       for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
130         for_each(FI->begin(), FI->end(), freeMachineCode);
131
132       MachineFunction::destruct(&F);
133       return false;
134     }
135   };
136
137   FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
138     return new ConstructMachineFunction(Target);
139   }
140 }
141
142 FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
143   return new DestroyMachineFunction();
144 }
145
146
147 SparcV9TargetMachine::SparcV9TargetMachine(const Module &M,
148                                            IntrinsicLowering *il)
149   : TargetMachine("UltraSparcV9-Native", il, false),
150     schedInfo(*this),
151     regInfo(*this),
152     frameInfo(*this),
153     jitInfo(*this) {
154 }
155
156 /// addPassesToEmitAssembly - This method controls the entire code generation
157 /// process for the ultra sparc.
158 ///
159 bool
160 SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
161 {
162   // FIXME: Implement efficient support for garbage collection intrinsics.
163   PM.add(createLowerGCPass());
164
165   // Replace malloc and free instructions with library calls.
166   PM.add(createLowerAllocationsPass());
167
168   // FIXME: implement the switch instruction in the instruction selector.
169   PM.add(createLowerSwitchPass());
170
171   // FIXME: implement the invoke/unwind instructions!
172   PM.add(createLowerInvokePass());
173
174   // decompose multi-dimensional array references into single-dim refs
175   PM.add(createDecomposeMultiDimRefsPass());
176
177   // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
178   PM.add(createPreSelectionPass(*this));
179   PM.add(createLowerSelectPass());
180
181   // If the user's trying to read the generated code, they'll need to see the
182   // transformed input.
183   if (PrintMachineCode)
184     PM.add(new PrintModulePass());
185
186   // Construct and initialize the MachineFunction object for this fn.
187   PM.add(createMachineCodeConstructionPass(*this));
188
189   // Insert empty stackslots in the stack frame of each function
190   // so %fp+offset-8 and %fp+offset-16 are empty slots now!
191   PM.add(createStackSlotsPass(*this));
192
193   PM.add(createSparcV9BurgInstSelector(*this));
194
195   if (!DisableSched)
196     PM.add(createInstructionSchedulingWithSSAPass(*this));
197
198   if(PrintMachineCode && EnableModSched)
199     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before modulo scheduling:\n"));
200
201   //Use ModuloScheduling if enabled, otherwise use local scheduling if not disabled.
202   if(EnableModSched)
203     PM.add(createModuloSchedulingPass(*this));
204
205   if (PrintMachineCode)
206     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
207
208   PM.add(getRegisterAllocator(*this));
209
210   if (PrintMachineCode)
211     PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
212
213   PM.add(createPrologEpilogInsertionPass());
214
215   if (!DisablePeephole)
216     PM.add(createPeepholeOptsPass(*this));
217
218   if (PrintMachineCode)
219     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
220
221   if (EmitMappingInfo) {
222     PM.add(createInternalGlobalMapperPass());
223     PM.add(getMappingInfoAsmPrinterPass(Out));
224   }
225
226   // Output assembly language to the .s file.  Assembly emission is split into
227   // two parts: Function output and Global value output.  This is because
228   // function output is pipelined with all of the rest of code generation stuff,
229   // allowing machine code representations for functions to be free'd after the
230   // function has been emitted.
231   PM.add(createAsmPrinterPass(Out, *this));
232
233   // Free machine-code IR which is no longer needed:
234   PM.add(createSparcV9MachineCodeDestructionPass());
235
236   // Emit bytecode to the assembly file into its special section next
237   if (EmitMappingInfo)
238     PM.add(createBytecodeAsmPrinterPass(Out));
239
240   return false;
241 }
242
243 /// addPassesToJITCompile - This method controls the JIT method of code
244 /// generation for the UltraSparcV9.
245 ///
246 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
247   // FIXME: Implement efficient support for garbage collection intrinsics.
248   PM.add(createLowerGCPass());
249
250   // Replace malloc and free instructions with library calls.
251   PM.add(createLowerAllocationsPass());
252
253   // FIXME: implement the switch instruction in the instruction selector.
254   PM.add(createLowerSwitchPass());
255
256   // FIXME: implement the invoke/unwind instructions!
257   PM.add(createLowerInvokePass());
258
259   // decompose multi-dimensional array references into single-dim refs
260   PM.add(createDecomposeMultiDimRefsPass());
261
262   // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
263   PM.add(createPreSelectionPass(TM));
264   PM.add(createLowerSelectPass());
265
266   // If the user's trying to read the generated code, they'll need to see the
267   // transformed input.
268   if (PrintMachineCode)
269     PM.add(new PrintFunctionPass());
270
271   // Construct and initialize the MachineFunction object for this fn.
272   PM.add(createMachineCodeConstructionPass(TM));
273
274   PM.add(createSparcV9BurgInstSelector(TM));
275
276   if (PrintMachineCode)
277     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
278
279   PM.add(getRegisterAllocator(TM));
280
281   if (PrintMachineCode)
282     PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
283
284   PM.add(createPrologEpilogInsertionPass());
285
286   if (!DisablePeephole)
287     PM.add(createPeepholeOptsPass(TM));
288
289   if (PrintMachineCode)
290     PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
291 }
292