1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the X86 specific subclass of TargetMachine.
12 //===----------------------------------------------------------------------===//
14 #include "X86TargetAsmInfo.h"
15 #include "X86TargetMachine.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"
25 /// X86TargetMachineModule - Note that this is used on hosts that cannot link
26 /// in a library unless there are references into the library. In particular,
27 /// it seems that it is not possible to get things to work on Win32 without
28 /// this. Though it is unused, do not remove it.
29 extern "C" int X86TargetMachineModule;
30 int X86TargetMachineModule = 0;
32 // Register the target.
33 static RegisterTarget<X86_32TargetMachine>
34 X("x86", " 32-bit X86: Pentium-Pro and above");
35 static RegisterTarget<X86_64TargetMachine>
36 Y("x86-64", " 64-bit X86: EM64T and AMD64");
38 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
39 if (Subtarget.isFlavorIntel())
40 return new X86WinTargetAsmInfo(*this);
42 switch (Subtarget.TargetType) {
43 case X86Subtarget::isDarwin:
44 return new X86DarwinTargetAsmInfo(*this);
45 case X86Subtarget::isELF:
46 return new X86ELFTargetAsmInfo(*this);
47 case X86Subtarget::isMingw:
48 case X86Subtarget::isCygwin:
49 return new X86COFFTargetAsmInfo(*this);
50 case X86Subtarget::isWindows:
51 return new X86WinTargetAsmInfo(*this);
53 return new X86TargetAsmInfo(*this);
57 unsigned X86_32TargetMachine::getJITMatchQuality() {
58 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
64 unsigned X86_64TargetMachine::getJITMatchQuality() {
65 #if defined(__x86_64__) || defined(_M_AMD64)
71 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
72 // We strongly match "i[3-9]86-*".
73 std::string TT = M.getTargetTriple();
74 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
75 TT[4] == '-' && TT[1] - '3' < 6)
77 // If the target triple is something non-X86, we don't match.
78 if (!TT.empty()) return 0;
80 if (M.getEndianness() == Module::LittleEndian &&
81 M.getPointerSize() == Module::Pointer32)
82 return 10; // Weak match
83 else if (M.getEndianness() != Module::AnyEndianness ||
84 M.getPointerSize() != Module::AnyPointerSize)
85 return 0; // Match for some other target
87 return getJITMatchQuality()/2;
90 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
91 // We strongly match "x86_64-*".
92 std::string TT = M.getTargetTriple();
93 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
94 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
97 // We strongly match "amd64-*".
98 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
99 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
102 // If the target triple is something non-X86-64, we don't match.
103 if (!TT.empty()) return 0;
105 if (M.getEndianness() == Module::LittleEndian &&
106 M.getPointerSize() == Module::Pointer64)
107 return 10; // Weak match
108 else if (M.getEndianness() != Module::AnyEndianness ||
109 M.getPointerSize() != Module::AnyPointerSize)
110 return 0; // Match for some other target
112 return getJITMatchQuality()/2;
115 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
116 : X86TargetMachine(M, FS, false) {
120 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
121 : X86TargetMachine(M, FS, true) {
124 /// X86TargetMachine ctor - Create an ILP32 architecture model
126 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
128 : Subtarget(M, FS, is64Bit),
129 DataLayout(Subtarget.getDataLayout()),
130 FrameInfo(TargetFrameInfo::StackGrowsDown,
131 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
132 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
133 DefRelocModel = getRelocationModel();
134 // FIXME: Correctly select PIC model for Win64 stuff
135 if (getRelocationModel() == Reloc::Default) {
136 if (Subtarget.isTargetDarwin() ||
137 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
138 setRelocationModel(Reloc::DynamicNoPIC);
140 setRelocationModel(Reloc::Static);
142 if (Subtarget.is64Bit()) {
143 // No DynamicNoPIC support under X86-64.
144 if (getRelocationModel() == Reloc::DynamicNoPIC)
145 setRelocationModel(Reloc::PIC_);
146 // Default X86-64 code model is small.
147 if (getCodeModel() == CodeModel::Default)
148 setCodeModel(CodeModel::Small);
151 if (Subtarget.isTargetCygMing())
152 Subtarget.setPICStyle(PICStyle::WinPIC);
153 else if (Subtarget.isTargetDarwin()) {
154 if (Subtarget.is64Bit())
155 Subtarget.setPICStyle(PICStyle::RIPRel);
157 Subtarget.setPICStyle(PICStyle::Stub);
158 } else if (Subtarget.isTargetELF()) {
159 if (Subtarget.is64Bit())
160 Subtarget.setPICStyle(PICStyle::RIPRel);
162 Subtarget.setPICStyle(PICStyle::GOT);
166 //===----------------------------------------------------------------------===//
167 // Pass Pipeline Configuration
168 //===----------------------------------------------------------------------===//
170 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
171 // Install an instruction selector.
172 PM.add(createX86ISelDag(*this, Fast));
176 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
177 // Calculate and set max stack object alignment early, so we can decide
178 // whether we will need stack realignment (and thus FP).
179 PM.add(createX86MaxStackAlignmentCalculatorPass());
180 return false; // -print-machineinstr shouldn't print after this.
183 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
184 PM.add(createX86FloatingPointStackifierPass());
185 return true; // -print-machineinstr should print after this.
188 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
190 PM.add(createX86CodePrinterPass(Out, *this));
194 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
195 bool DumpAsm, MachineCodeEmitter &MCE) {
196 // FIXME: Move this to TargetJITInfo!
197 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
198 if (DefRelocModel == Reloc::Default &&
199 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
200 setRelocationModel(Reloc::Static);
202 // 64-bit JIT places everything in the same buffer except external functions.
203 // On Darwin, use small code model but hack the call instruction for
204 // externals. Elsewhere, do not assume globals are in the lower 4G.
205 if (Subtarget.is64Bit()) {
206 if (Subtarget.isTargetDarwin())
207 setCodeModel(CodeModel::Small);
209 setCodeModel(CodeModel::Large);
212 PM.add(createX86CodeEmitterPass(*this, MCE));
214 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
219 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
220 bool DumpAsm, MachineCodeEmitter &MCE) {
221 PM.add(createX86CodeEmitterPass(*this, MCE));
223 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));