Refactor things a bit to allow the ELF code emitter to run the X86 machine code emitter
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
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 // This file defines the X86 specific subclass of TargetMachine.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetMachine.h"
15 #include "X86.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/IntrinsicLowering.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Target/TargetMachineRegistry.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/ADT/Statistic.h"
26 using namespace llvm;
27
28 X86VectorEnum llvm::X86Vector = NoSSE;
29 bool llvm::X86ScalarSSE = false;
30
31 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
32 /// in a library unless there are references into the library.  In particular,
33 /// it seems that it is not possible to get things to work on Win32 without
34 /// this.  Though it is unused, do not remove it.
35 extern "C" int X86TargetMachineModule;
36 int X86TargetMachineModule = 0;
37
38 namespace {
39   cl::opt<bool> NoSSAPeephole("disable-ssa-peephole", cl::init(true),
40                         cl::desc("Disable the ssa-based peephole optimizer "
41                                  "(defaults to disabled)"));
42   cl::opt<bool> DisableOutput("disable-x86-llc-output", cl::Hidden,
43                               cl::desc("Disable the X86 asm printer, for use "
44                                        "when profiling the code generator."));
45   cl::opt<bool, true> EnableSSEFP("enable-sse-scalar-fp",
46                 cl::desc("Perform FP math in SSE regs instead of the FP stack"),
47                 cl::location(X86ScalarSSE),
48                 cl::init(false));
49
50   // FIXME: This should eventually be handled with target triples and
51   // subtarget support!
52   cl::opt<X86VectorEnum, true>
53   SSEArg(
54     cl::desc("Enable SSE support in the X86 target:"),
55     cl::values(
56        clEnumValN(SSE,  "sse", "  Enable SSE support"),
57        clEnumValN(SSE2, "sse2", "  Enable SSE and SSE2 support"),
58        clEnumValN(SSE3, "sse3", "  Enable SSE, SSE2, and SSE3 support"),
59        clEnumValEnd),
60     cl::location(X86Vector), cl::init(NoSSE));
61
62   // Register the target.
63   RegisterTarget<X86TargetMachine> X("x86", "  IA-32 (Pentium and above)");
64 }
65
66 unsigned X86TargetMachine::getJITMatchQuality() {
67 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
68   return 10;
69 #else
70   return 0;
71 #endif
72 }
73
74 unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) {
75   // We strongly match "i[3-9]86-*".
76   std::string TT = M.getTargetTriple();
77   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
78       TT[4] == '-' && TT[1] - '3' < 6)
79     return 20;
80
81   if (M.getEndianness()  == Module::LittleEndian &&
82       M.getPointerSize() == Module::Pointer32)
83     return 10;                                   // Weak match
84   else if (M.getEndianness() != Module::AnyEndianness ||
85            M.getPointerSize() != Module::AnyPointerSize)
86     return 0;                                    // Match for some other target
87
88   return getJITMatchQuality()/2;
89 }
90
91 /// X86TargetMachine ctor - Create an ILP32 architecture model
92 ///
93 X86TargetMachine::X86TargetMachine(const Module &M, IntrinsicLowering *IL)
94   : TargetMachine("X86", IL, true, 4, 4, 4, 4, 4),
95     FrameInfo(TargetFrameInfo::StackGrowsDown, 8, -4),
96     JITInfo(*this) {
97   // Scalar SSE FP requires at least SSE2
98   X86ScalarSSE &= X86Vector >= SSE2;
99 }
100
101
102 // addPassesToEmitFile - We currently use all of the same passes as the JIT
103 // does to emit statically compiled machine code.
104 bool X86TargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out,
105                                            CodeGenFileType FileType) {
106   if (FileType != TargetMachine::AssemblyFile && 
107       FileType != TargetMachine::ObjectFile) return true;
108
109   // FIXME: Implement efficient support for garbage collection intrinsics.
110   PM.add(createLowerGCPass());
111
112   // FIXME: Implement the invoke/unwind instructions!
113   PM.add(createLowerInvokePass());
114
115   // FIXME: Implement the switch instruction in the instruction selector!
116   PM.add(createLowerSwitchPass());
117
118   // Make sure that no unreachable blocks are instruction selected.
119   PM.add(createUnreachableBlockEliminationPass());
120
121   // Default to pattern ISel
122   if (PatternISelTriState == 0)
123     PM.add(createX86SimpleInstructionSelector(*this));
124   else
125     PM.add(createX86PatternInstructionSelector(*this));
126
127   // Run optional SSA-based machine code optimizations next...
128   if (!NoSSAPeephole)
129     PM.add(createX86SSAPeepholeOptimizerPass());
130
131   // Print the instruction selected machine code...
132   if (PrintMachineCode)
133     PM.add(createMachineFunctionPrinterPass(&std::cerr));
134
135   // Perform register allocation to convert to a concrete x86 representation
136   PM.add(createRegisterAllocator());
137
138   if (PrintMachineCode)
139     PM.add(createMachineFunctionPrinterPass(&std::cerr));
140
141   PM.add(createX86FloatingPointStackifierPass());
142
143   if (PrintMachineCode)
144     PM.add(createMachineFunctionPrinterPass(&std::cerr));
145
146   // Insert prolog/epilog code.  Eliminate abstract frame index references...
147   PM.add(createPrologEpilogCodeInserter());
148
149   PM.add(createX86PeepholeOptimizerPass());
150
151   if (PrintMachineCode)  // Print the register-allocated code
152     PM.add(createX86CodePrinterPass(std::cerr, *this));
153
154   if (!DisableOutput)
155     switch (FileType) {
156     default:
157       assert(0 && "Unexpected filetype here!");
158     case TargetMachine::AssemblyFile:
159       PM.add(createX86CodePrinterPass(Out, *this));
160       break;
161     case TargetMachine::ObjectFile:
162       // FIXME: We only support emission of ELF files for now, this should check
163       // the target triple and decide on the format to write (e.g. COFF on
164       // win32).
165       addX86ELFObjectWriterPass(PM, Out, *this);
166       break;
167     }
168
169   // Delete machine code for this function
170   PM.add(createMachineCodeDeleter());
171
172   return false; // success!
173 }
174
175 /// addPassesToJITCompile - Add passes to the specified pass manager to
176 /// implement a fast dynamic compiler for this target.  Return true if this is
177 /// not supported for this target.
178 ///
179 void X86JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
180   // FIXME: Implement efficient support for garbage collection intrinsics.
181   PM.add(createLowerGCPass());
182
183   // FIXME: Implement the invoke/unwind instructions!
184   PM.add(createLowerInvokePass());
185
186   // FIXME: Implement the switch instruction in the instruction selector!
187   PM.add(createLowerSwitchPass());
188
189   // Make sure that no unreachable blocks are instruction selected.
190   PM.add(createUnreachableBlockEliminationPass());
191
192   // Default to pattern ISel
193   if (PatternISelTriState == 0)
194     PM.add(createX86SimpleInstructionSelector(TM));
195   else
196     PM.add(createX86PatternInstructionSelector(TM));
197
198   // Run optional SSA-based machine code optimizations next...
199   if (!NoSSAPeephole)
200     PM.add(createX86SSAPeepholeOptimizerPass());
201
202   // FIXME: Add SSA based peephole optimizer here.
203
204   // Print the instruction selected machine code...
205   if (PrintMachineCode)
206     PM.add(createMachineFunctionPrinterPass(&std::cerr));
207
208   // Perform register allocation to convert to a concrete x86 representation
209   PM.add(createRegisterAllocator());
210
211   if (PrintMachineCode)
212     PM.add(createMachineFunctionPrinterPass(&std::cerr));
213
214   PM.add(createX86FloatingPointStackifierPass());
215
216   if (PrintMachineCode)
217     PM.add(createMachineFunctionPrinterPass(&std::cerr));
218
219   // Insert prolog/epilog code.  Eliminate abstract frame index references...
220   PM.add(createPrologEpilogCodeInserter());
221
222   PM.add(createX86PeepholeOptimizerPass());
223
224   if (PrintMachineCode)  // Print the register-allocated code
225     PM.add(createX86CodePrinterPass(std::cerr, TM));
226 }
227
228 bool X86TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
229                                                   MachineCodeEmitter &MCE) {
230   PM.add(createX86CodeEmitterPass(MCE));
231   // Delete machine code for this function
232   PM.add(createMachineCodeDeleter());
233   return false;
234 }