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