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