blob: 7e597851fcec8054d38ded4a9eb50c1e6f1cd696 [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//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
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.
29extern "C" int X86TargetMachineModule;
30int X86TargetMachineModule = 0;
31
Dan Gohman089efff2008-05-13 00:00:25 +000032// Register the target.
33static RegisterTarget<X86_32TargetMachine>
34X("x86", " 32-bit X86: Pentium-Pro and above");
35static RegisterTarget<X86_64TargetMachine>
36Y("x86-64", " 64-bit X86: EM64T and AMD64");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037
38const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
39 return new X86TargetAsmInfo(*this);
40}
41
42unsigned X86_32TargetMachine::getJITMatchQuality() {
43#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
44 return 10;
45#endif
46 return 0;
47}
48
49unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000050#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 return 10;
52#endif
53 return 0;
54}
55
56unsigned 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)
61 return 20;
62 // If the target triple is something non-X86, we don't match.
63 if (!TT.empty()) return 0;
64
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
71
72 return getJITMatchQuality()/2;
73}
74
75unsigned 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] == '-')
80 return 20;
81
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] == '-')
85 return 20;
86
87 // If the target triple is something non-X86-64, we don't match.
88 if (!TT.empty()) return 0;
89
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
96
97 return getJITMatchQuality()/2;
98}
99
100X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
101 : X86TargetMachine(M, FS, false) {
102}
103
104
105X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
106 : X86TargetMachine(M, FS, true) {
107}
108
109/// X86TargetMachine ctor - Create an ILP32 architecture model
110///
111X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
112 bool is64Bit)
113 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000114 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 FrameInfo(TargetFrameInfo::StackGrowsDown,
116 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
117 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000118 DefRelocModel = getRelocationModel();
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000119 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000120 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000121 if (Subtarget.isTargetDarwin() ||
122 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setRelocationModel(Reloc::DynamicNoPIC);
124 else
125 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 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);
134 }
135
136 if (Subtarget.isTargetCygMing())
137 Subtarget.setPICStyle(PICStyle::WinPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000138 else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 if (Subtarget.is64Bit())
140 Subtarget.setPICStyle(PICStyle::RIPRel);
141 else
142 Subtarget.setPICStyle(PICStyle::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000143 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 if (Subtarget.is64Bit())
145 Subtarget.setPICStyle(PICStyle::RIPRel);
146 else
147 Subtarget.setPICStyle(PICStyle::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000148 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149}
150
151//===----------------------------------------------------------------------===//
152// Pass Pipeline Configuration
153//===----------------------------------------------------------------------===//
154
Dan Gohmane34aa772008-03-11 22:29:46 +0000155bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 // Install an instruction selector.
157 PM.add(createX86ISelDag(*this, Fast));
158 return false;
159}
160
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000161bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000162 // Calculate and set max stack object alignment early, so we can decide
163 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000164 PM.add(createX86MaxStackAlignmentCalculatorPass());
165 return false; // -print-machineinstr shouldn't print after this.
166}
167
Dan Gohmane34aa772008-03-11 22:29:46 +0000168bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 PM.add(createX86FloatingPointStackifierPass());
170 return true; // -print-machineinstr should print after this.
171}
172
Dan Gohmane34aa772008-03-11 22:29:46 +0000173bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 std::ostream &Out) {
175 PM.add(createX86CodePrinterPass(Out, *this));
176 return false;
177}
178
Dan Gohmane34aa772008-03-11 22:29:46 +0000179bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000180 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // FIXME: Move this to TargetJITInfo!
Evan Chenge4cf5fe2008-05-22 23:55:24 +0000182 if (DefRelocModel == Reloc::Default)
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000183 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184
185 // JIT cannot ensure globals are placed in the lower 4G of address.
186 if (Subtarget.is64Bit())
187 setCodeModel(CodeModel::Large);
188
189 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000190 if (DumpAsm)
191 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 return false;
194}
195
Dan Gohmane34aa772008-03-11 22:29:46 +0000196bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000197 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000199 if (DumpAsm)
200 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 return false;
202}