Added command line option for linear scan allocator
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
2 // 
3 // This file defines the X86 specific subclass of TargetMachine.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "X86TargetMachine.h"
8 #include "X86.h"
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Target/TargetMachineImpls.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/Passes.h"
14 #include "llvm/Transforms/Scalar.h"
15 #include "Support/CommandLine.h"
16 #include "Support/Statistic.h"
17
18 namespace {
19   cl::opt<RegAllocName>
20   RegAlloc("regalloc",
21            cl::desc("Register allocator to use:"), cl::Prefix,
22            cl::values(clEnumVal(simple, "simple register allocator)"),
23                       clEnumVal(local, "local register allocator"),
24                       clEnumVal(linearscan, "linear scan global register allocator")),
25            cl::init(local));
26
27   cl::opt<bool> NoLocalRA("disable-local-ra",
28                           cl::desc("Use Simple RA instead of Local RegAlloc"));
29   cl::opt<bool> PrintCode("print-machineinstrs",
30                           cl::desc("Print generated machine code"));
31   cl::opt<bool> NoPatternISel("disable-pattern-isel", cl::init(true),
32                         cl::desc("Use the 'simple' X86 instruction selector"));
33 }
34
35 // allocateX86TargetMachine - Allocate and return a subclass of TargetMachine
36 // that implements the X86 backend.
37 //
38 TargetMachine *allocateX86TargetMachine(const Module &M) {
39   return new X86TargetMachine(M);
40 }
41
42
43 /// X86TargetMachine ctor - Create an ILP32 architecture model
44 ///
45 X86TargetMachine::X86TargetMachine(const Module &M)
46   : TargetMachine("X86", 
47                   M.getEndianness() != Module::BigEndian,
48                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
49                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
50                   M.getPointerSize() != Module::Pointer64 ? 4 : 8,
51                   4, M.getPointerSize() != Module::Pointer64 ? 4 : 8),
52     FrameInfo(TargetFrameInfo::StackGrowsDown, 8/*16 for SSE*/, 4) {
53 }
54
55
56 // addPassesToEmitAssembly - We currently use all of the same passes as the JIT
57 // does to emit statically compiled machine code.
58 bool X86TargetMachine::addPassesToEmitAssembly(PassManager &PM,
59                                                std::ostream &Out) {
60   // FIXME: Implement the switch instruction in the instruction selector!
61   PM.add(createLowerSwitchPass());
62
63   if (NoPatternISel)
64     PM.add(createX86SimpleInstructionSelector(*this));
65   else
66     PM.add(createX86PatternInstructionSelector(*this));
67
68   // TODO: optional optimizations go here
69
70   // FIXME: Add SSA based peephole optimizer here.
71
72   // Print the instruction selected machine code...
73   if (PrintCode)
74     PM.add(createMachineFunctionPrinterPass());
75
76   // Perform register allocation to convert to a concrete x86 representation
77   if (NoLocalRA)
78     PM.add(createSimpleRegisterAllocator());
79   else
80     PM.add(createLocalRegisterAllocator());
81
82   if (PrintCode)
83     PM.add(createMachineFunctionPrinterPass());
84
85   PM.add(createX86FloatingPointStackifierPass());
86
87   if (PrintCode)
88     PM.add(createMachineFunctionPrinterPass());
89
90   // Insert prolog/epilog code.  Eliminate abstract frame index references...
91   PM.add(createPrologEpilogCodeInserter());
92
93   PM.add(createX86PeepholeOptimizerPass());
94
95   if (PrintCode)  // Print the register-allocated code
96     PM.add(createX86CodePrinterPass(std::cerr, *this));
97
98   PM.add(createX86CodePrinterPass(Out, *this));
99   return false; // success!
100 }
101
102 /// addPassesToJITCompile - Add passes to the specified pass manager to
103 /// implement a fast dynamic compiler for this target.  Return true if this is
104 /// not supported for this target.
105 ///
106 bool X86TargetMachine::addPassesToJITCompile(FunctionPassManager &PM) {
107   // FIXME: Implement the switch instruction in the instruction selector!
108   PM.add(createLowerSwitchPass());
109
110   if (NoPatternISel)
111     PM.add(createX86SimpleInstructionSelector(*this));
112   else
113     PM.add(createX86PatternInstructionSelector(*this));
114
115   // TODO: optional optimizations go here
116
117   // FIXME: Add SSA based peephole optimizer here.
118
119   // Print the instruction selected machine code...
120   if (PrintCode)
121     PM.add(createMachineFunctionPrinterPass());
122
123   // Perform register allocation to convert to a concrete x86 representation
124   switch (RegAlloc) {
125   case simple:
126     PM.add(createSimpleRegisterAllocator());
127     break;
128   case local:
129     PM.add(createLocalRegisterAllocator());
130     break;
131   case linearscan:
132     PM.add(createLinearScanRegisterAllocator());
133     break;
134   default:
135     assert(0 && "no register allocator selected");
136   }
137
138   if (PrintCode)
139     PM.add(createMachineFunctionPrinterPass());
140
141   PM.add(createX86FloatingPointStackifierPass());
142
143   if (PrintCode)
144     PM.add(createMachineFunctionPrinterPass());
145
146   // Insert prolog/epilog code.  Eliminate abstract frame index references...
147   PM.add(createPrologEpilogCodeInserter());
148
149   PM.add(createX86PeepholeOptimizerPass());
150
151   if (PrintCode)  // Print the register-allocated code
152     PM.add(createX86CodePrinterPass(std::cerr, *this));
153   return false; // success!
154 }
155