b4fc79abcaeaddc4ede5f4f286f75c846e47c959
[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 "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.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 <iostream>
25 using namespace llvm;
26
27 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
28 /// in a library unless there are references into the library.  In particular,
29 /// it seems that it is not possible to get things to work on Win32 without
30 /// this.  Though it is unused, do not remove it.
31 extern "C" int X86TargetMachineModule;
32 int X86TargetMachineModule = 0;
33
34 namespace {
35   // Register the target.
36   RegisterTarget<X86_32TargetMachine>
37   X("x86",    "  32-bit X86: Pentium-Pro and above");
38   RegisterTarget<X86_64TargetMachine>
39   Y("x86-64", "  64-bit X86: EM64T and AMD64");
40 }
41
42 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
43   return new X86TargetAsmInfo(*this);
44 }
45
46 unsigned X86_32TargetMachine::getJITMatchQuality() {
47 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
48   return 10;
49 #endif
50   return 0;
51 }
52
53 unsigned X86_64TargetMachine::getJITMatchQuality() {
54 #if defined(__x86_64__)
55   return 10;
56 #endif
57   return 0;
58 }
59
60 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
61   // We strongly match "i[3-9]86-*".
62   std::string TT = M.getTargetTriple();
63   if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
64       TT[4] == '-' && TT[1] - '3' < 6)
65     return 20;
66
67   if (M.getEndianness()  == Module::LittleEndian &&
68       M.getPointerSize() == Module::Pointer32)
69     return 10;                                   // Weak match
70   else if (M.getEndianness() != Module::AnyEndianness ||
71            M.getPointerSize() != Module::AnyPointerSize)
72     return 0;                                    // Match for some other target
73
74   return getJITMatchQuality()/2;
75 }
76
77 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
78   // We strongly match "x86_64-*".
79   std::string TT = M.getTargetTriple();
80   if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
81       TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
82     return 20;
83
84   if (M.getEndianness()  == Module::LittleEndian &&
85       M.getPointerSize() == Module::Pointer64)
86     return 10;                                   // Weak match
87   else if (M.getEndianness() != Module::AnyEndianness ||
88            M.getPointerSize() != Module::AnyPointerSize)
89     return 0;                                    // Match for some other target
90
91   return getJITMatchQuality()/2;
92 }
93
94 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS) 
95   : X86TargetMachine(M, FS, false) {
96 }
97
98
99 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
100   : X86TargetMachine(M, FS, true) {
101 }
102
103 /// X86TargetMachine ctor - Create an ILP32 architecture model
104 ///
105 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS, bool is64Bit)
106   : Subtarget(M, FS, is64Bit),
107     DataLayout(Subtarget.is64Bit() ?
108                std::string("e-p:64:64-d:32-l:32") :
109                std::string("e-p:32:32-d:32-l:32")),
110     FrameInfo(TargetFrameInfo::StackGrowsDown,
111               Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
112     InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
113   if (getRelocationModel() == Reloc::Default)
114     if (Subtarget.isTargetDarwin())
115       setRelocationModel(Reloc::DynamicNoPIC);
116     else
117       setRelocationModel(Reloc::PIC_);
118   if (Subtarget.is64Bit()) {
119     // No DynamicNoPIC support under X86-64.
120     if (getRelocationModel() == Reloc::DynamicNoPIC)
121       setRelocationModel(Reloc::PIC_);
122     // Default X86-64 code model is small.
123     if (getCodeModel() == CodeModel::Default)
124       setCodeModel(CodeModel::Small);
125   }
126 }
127
128 //===----------------------------------------------------------------------===//
129 // Pass Pipeline Configuration
130 //===----------------------------------------------------------------------===//
131
132 bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
133   // Install an instruction selector.
134   PM.add(createX86ISelDag(*this, Fast));
135   return false;
136 }
137
138 bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
139   PM.add(createX86FloatingPointStackifierPass());
140   return true;  // -print-machineinstr should print after this.
141 }
142
143 bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast, 
144                                           std::ostream &Out) {
145   PM.add(createX86CodePrinterPass(Out, *this));
146   return false;
147 }
148
149 bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
150                                        std::ostream &Out) {
151   if (Subtarget.isTargetELF()) {
152     addX86ELFObjectWriterPass(PM, Out, *this);
153     return false;
154   }
155   return true;
156 }
157
158 bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
159                                       MachineCodeEmitter &MCE) {
160   // FIXME: Move this to TargetJITInfo!
161   setRelocationModel(Reloc::Static);
162   
163   PM.add(createX86CodeEmitterPass(*this, MCE));
164   return false;
165 }