blob: f87640f224bf85c852175edf2a49b05ef12186a0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- 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"
24using namespace llvm;
25
26/// X86TargetMachineModule - Note that this is used on hosts that cannot link
27/// in a library unless there are references into the library. In particular,
28/// it seems that it is not possible to get things to work on Win32 without
29/// this. Though it is unused, do not remove it.
30extern "C" int X86TargetMachineModule;
31int X86TargetMachineModule = 0;
32
33namespace {
34 // Register the target.
35 RegisterTarget<X86_32TargetMachine>
36 X("x86", " 32-bit X86: Pentium-Pro and above");
37 RegisterTarget<X86_64TargetMachine>
38 Y("x86-64", " 64-bit X86: EM64T and AMD64");
39}
40
41const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
42 return new X86TargetAsmInfo(*this);
43}
44
45unsigned X86_32TargetMachine::getJITMatchQuality() {
46#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
47 return 10;
48#endif
49 return 0;
50}
51
52unsigned X86_64TargetMachine::getJITMatchQuality() {
53#if defined(__x86_64__)
54 return 10;
55#endif
56 return 0;
57}
58
59unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
60 // We strongly match "i[3-9]86-*".
61 std::string TT = M.getTargetTriple();
62 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
63 TT[4] == '-' && TT[1] - '3' < 6)
64 return 20;
65 // If the target triple is something non-X86, we don't match.
66 if (!TT.empty()) return 0;
67
68 if (M.getEndianness() == Module::LittleEndian &&
69 M.getPointerSize() == Module::Pointer32)
70 return 10; // Weak match
71 else if (M.getEndianness() != Module::AnyEndianness ||
72 M.getPointerSize() != Module::AnyPointerSize)
73 return 0; // Match for some other target
74
75 return getJITMatchQuality()/2;
76}
77
78unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
79 // We strongly match "x86_64-*".
80 std::string TT = M.getTargetTriple();
81 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
82 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
83 return 20;
84
85 // We strongly match "amd64-*".
86 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
87 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
88 return 20;
89
90 // If the target triple is something non-X86-64, we don't match.
91 if (!TT.empty()) return 0;
92
93 if (M.getEndianness() == Module::LittleEndian &&
94 M.getPointerSize() == Module::Pointer64)
95 return 10; // Weak match
96 else if (M.getEndianness() != Module::AnyEndianness ||
97 M.getPointerSize() != Module::AnyPointerSize)
98 return 0; // Match for some other target
99
100 return getJITMatchQuality()/2;
101}
102
103X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
104 : X86TargetMachine(M, FS, false) {
105}
106
107
108X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
109 : X86TargetMachine(M, FS, true) {
110}
111
112/// X86TargetMachine ctor - Create an ILP32 architecture model
113///
114X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
115 bool is64Bit)
116 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000117 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 FrameInfo(TargetFrameInfo::StackGrowsDown,
119 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
120 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
121 if (getRelocationModel() == Reloc::Default)
122 if (Subtarget.isTargetDarwin() || Subtarget.isTargetCygMing())
123 setRelocationModel(Reloc::DynamicNoPIC);
124 else
125 setRelocationModel(Reloc::Static);
126 if (Subtarget.is64Bit()) {
127 // No DynamicNoPIC support under X86-64.
128 if (getRelocationModel() == Reloc::DynamicNoPIC)
129 setRelocationModel(Reloc::PIC_);
130 // Default X86-64 code model is small.
131 if (getCodeModel() == CodeModel::Default)
132 setCodeModel(CodeModel::Small);
133 }
134
135 if (Subtarget.isTargetCygMing())
136 Subtarget.setPICStyle(PICStyle::WinPIC);
137 else if (Subtarget.isTargetDarwin())
138 if (Subtarget.is64Bit())
139 Subtarget.setPICStyle(PICStyle::RIPRel);
140 else
141 Subtarget.setPICStyle(PICStyle::Stub);
142 else if (Subtarget.isTargetELF())
143 if (Subtarget.is64Bit())
144 Subtarget.setPICStyle(PICStyle::RIPRel);
145 else
146 Subtarget.setPICStyle(PICStyle::GOT);
147}
148
149//===----------------------------------------------------------------------===//
150// Pass Pipeline Configuration
151//===----------------------------------------------------------------------===//
152
153bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
154 // Install an instruction selector.
155 PM.add(createX86ISelDag(*this, Fast));
156 return false;
157}
158
159bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
160 PM.add(createX86FloatingPointStackifierPass());
161 return true; // -print-machineinstr should print after this.
162}
163
164bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
165 std::ostream &Out) {
166 PM.add(createX86CodePrinterPass(Out, *this));
167 return false;
168}
169
170bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000171 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 // FIXME: Move this to TargetJITInfo!
Evan Cheng704f18f2007-12-22 09:14:34 +0000173 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 Subtarget.setPICStyle(PICStyle::None);
175
176 // JIT cannot ensure globals are placed in the lower 4G of address.
177 if (Subtarget.is64Bit())
178 setCodeModel(CodeModel::Large);
179
180 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000181 if (DumpAsm)
182 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return false;
185}
186
187bool X86TargetMachine::addSimpleCodeEmitter(FunctionPassManager &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000188 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000190 if (DumpAsm)
191 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 return false;
193}