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"
23 #include "llvm/Transforms/Scalar.h"
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.
30 extern "C" int X86TargetMachineModule;
31 int X86TargetMachineModule = 0;
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");
41 const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
42 return new X86TargetAsmInfo(*this);
45 unsigned X86_32TargetMachine::getJITMatchQuality() {
46 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
52 unsigned X86_64TargetMachine::getJITMatchQuality() {
53 #if defined(__x86_64__) || defined(_M_AMD64)
59 unsigned 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)
65 // If the target triple is something non-X86, we don't match.
66 if (!TT.empty()) return 0;
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
75 return getJITMatchQuality()/2;
78 unsigned 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] == '-')
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] == '-')
90 // If the target triple is something non-X86-64, we don't match.
91 if (!TT.empty()) return 0;
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
100 return getJITMatchQuality()/2;
103 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
104 : X86TargetMachine(M, FS, false) {
108 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
109 : X86TargetMachine(M, FS, true) {
112 /// X86TargetMachine ctor - Create an ILP32 architecture model
114 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
116 : Subtarget(M, FS, is64Bit),
117 DataLayout(Subtarget.getDataLayout()),
118 FrameInfo(TargetFrameInfo::StackGrowsDown,
119 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
120 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
121 DefRelocModel = getRelocationModel();
122 // FIXME: Correctly select PIC model for Win64 stuff
123 if (getRelocationModel() == Reloc::Default) {
124 if (Subtarget.isTargetDarwin() ||
125 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
126 setRelocationModel(Reloc::DynamicNoPIC);
128 setRelocationModel(Reloc::Static);
130 if (Subtarget.is64Bit()) {
131 // No DynamicNoPIC support under X86-64.
132 if (getRelocationModel() == Reloc::DynamicNoPIC)
133 setRelocationModel(Reloc::PIC_);
134 // Default X86-64 code model is small.
135 if (getCodeModel() == CodeModel::Default)
136 setCodeModel(CodeModel::Small);
139 if (Subtarget.isTargetCygMing())
140 Subtarget.setPICStyle(PICStyle::WinPIC);
141 else if (Subtarget.isTargetDarwin()) {
142 if (Subtarget.is64Bit())
143 Subtarget.setPICStyle(PICStyle::RIPRel);
145 Subtarget.setPICStyle(PICStyle::Stub);
146 } else if (Subtarget.isTargetELF()) {
147 if (Subtarget.is64Bit())
148 Subtarget.setPICStyle(PICStyle::RIPRel);
150 Subtarget.setPICStyle(PICStyle::GOT);
154 //===----------------------------------------------------------------------===//
155 // Pass Pipeline Configuration
156 //===----------------------------------------------------------------------===//
158 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
159 // Install an instruction selector.
160 PM.add(createX86ISelDag(*this, Fast));
164 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
165 // Calculate and set max stack object alignment early, so we can decide
166 // whether we will need stack realignment (and thus FP).
167 PM.add(createX86MaxStackAlignmentCalculatorPass());
168 return false; // -print-machineinstr shouldn't print after this.
171 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
172 PM.add(createX86FloatingPointStackifierPass());
173 return true; // -print-machineinstr should print after this.
176 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
178 PM.add(createX86CodePrinterPass(Out, *this));
182 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
183 bool DumpAsm, MachineCodeEmitter &MCE) {
184 // FIXME: Move this to TargetJITInfo!
185 if (DefRelocModel == Reloc::Default) {
186 setRelocationModel(Reloc::Static);
187 Subtarget.setPICStyle(PICStyle::None);
190 // JIT cannot ensure globals are placed in the lower 4G of address.
191 if (Subtarget.is64Bit())
192 setCodeModel(CodeModel::Large);
194 PM.add(createX86CodeEmitterPass(*this, MCE));
196 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
201 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
202 bool DumpAsm, MachineCodeEmitter &MCE) {
203 PM.add(createX86CodeEmitterPass(*this, MCE));
205 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));