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 return new X86TargetAsmInfo(*this);
42 unsigned X86_32TargetMachine::getJITMatchQuality() {
43 #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
49 unsigned X86_64TargetMachine::getJITMatchQuality() {
50 #if defined(__x86_64__) || defined(_M_AMD64)
56 unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
57 // We strongly match "i[3-9]86-*".
58 std::string TT = M.getTargetTriple();
59 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
60 TT[4] == '-' && TT[1] - '3' < 6)
62 // If the target triple is something non-X86, we don't match.
63 if (!TT.empty()) return 0;
65 if (M.getEndianness() == Module::LittleEndian &&
66 M.getPointerSize() == Module::Pointer32)
67 return 10; // Weak match
68 else if (M.getEndianness() != Module::AnyEndianness ||
69 M.getPointerSize() != Module::AnyPointerSize)
70 return 0; // Match for some other target
72 return getJITMatchQuality()/2;
75 unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
76 // We strongly match "x86_64-*".
77 std::string TT = M.getTargetTriple();
78 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
79 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
82 // We strongly match "amd64-*".
83 if (TT.size() >= 6 && TT[0] == 'a' && TT[1] == 'm' && TT[2] == 'd' &&
84 TT[3] == '6' && TT[4] == '4' && TT[5] == '-')
87 // If the target triple is something non-X86-64, we don't match.
88 if (!TT.empty()) return 0;
90 if (M.getEndianness() == Module::LittleEndian &&
91 M.getPointerSize() == Module::Pointer64)
92 return 10; // Weak match
93 else if (M.getEndianness() != Module::AnyEndianness ||
94 M.getPointerSize() != Module::AnyPointerSize)
95 return 0; // Match for some other target
97 return getJITMatchQuality()/2;
100 X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
101 : X86TargetMachine(M, FS, false) {
105 X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
106 : X86TargetMachine(M, FS, true) {
109 /// X86TargetMachine ctor - Create an ILP32 architecture model
111 X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
113 : Subtarget(M, FS, is64Bit),
114 DataLayout(Subtarget.getDataLayout()),
115 FrameInfo(TargetFrameInfo::StackGrowsDown,
116 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
117 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
118 DefRelocModel = getRelocationModel();
119 // FIXME: Correctly select PIC model for Win64 stuff
120 if (getRelocationModel() == Reloc::Default) {
121 if (Subtarget.isTargetDarwin() ||
122 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
123 setRelocationModel(Reloc::DynamicNoPIC);
125 setRelocationModel(Reloc::Static);
127 if (Subtarget.is64Bit()) {
128 // No DynamicNoPIC support under X86-64.
129 if (getRelocationModel() == Reloc::DynamicNoPIC)
130 setRelocationModel(Reloc::PIC_);
131 // Default X86-64 code model is small.
132 if (getCodeModel() == CodeModel::Default)
133 setCodeModel(CodeModel::Small);
136 if (Subtarget.isTargetCygMing())
137 Subtarget.setPICStyle(PICStyle::WinPIC);
138 else if (Subtarget.isTargetDarwin()) {
139 if (Subtarget.is64Bit())
140 Subtarget.setPICStyle(PICStyle::RIPRel);
142 Subtarget.setPICStyle(PICStyle::Stub);
143 } else if (Subtarget.isTargetELF()) {
144 if (Subtarget.is64Bit())
145 Subtarget.setPICStyle(PICStyle::RIPRel);
147 Subtarget.setPICStyle(PICStyle::GOT);
151 //===----------------------------------------------------------------------===//
152 // Pass Pipeline Configuration
153 //===----------------------------------------------------------------------===//
155 bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
156 // Install an instruction selector.
157 PM.add(createX86ISelDag(*this, Fast));
161 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
162 // Calculate and set max stack object alignment early, so we can decide
163 // whether we will need stack realignment (and thus FP).
164 PM.add(createX86MaxStackAlignmentCalculatorPass());
165 return false; // -print-machineinstr shouldn't print after this.
168 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
169 PM.add(createX86FloatingPointStackifierPass());
170 return true; // -print-machineinstr should print after this.
173 bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
175 PM.add(createX86CodePrinterPass(Out, *this));
179 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
180 bool DumpAsm, MachineCodeEmitter &MCE) {
181 // FIXME: Move this to TargetJITInfo!
182 if (DefRelocModel == Reloc::Default)
183 setRelocationModel(Reloc::Static);
185 // JIT cannot ensure globals are placed in the lower 4G of address.
186 if (Subtarget.is64Bit())
187 setCodeModel(CodeModel::Large);
189 PM.add(createX86CodeEmitterPass(*this, MCE));
191 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
196 bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
197 bool DumpAsm, MachineCodeEmitter &MCE) {
198 PM.add(createX86CodeEmitterPass(*this, MCE));
200 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));