blob: fdf9bdfba3a10fd4f30f4109074c2f8ed85f8456 [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 {
Anton Korobeynikovf700e682008-07-09 13:20:48 +000039 if (Subtarget.isFlavorIntel())
40 return new X86WinTargetAsmInfo(*this);
41 else
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);
52 default:
53 return new X86TargetAsmInfo(*this);
54 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055}
56
57unsigned X86_32TargetMachine::getJITMatchQuality() {
58#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
59 return 10;
60#endif
61 return 0;
62}
63
64unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov57348662008-03-23 13:43:47 +000065#if defined(__x86_64__) || defined(_M_AMD64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return 10;
67#endif
68 return 0;
69}
70
71unsigned 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)
76 return 20;
77 // If the target triple is something non-X86, we don't match.
78 if (!TT.empty()) return 0;
79
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
86
87 return getJITMatchQuality()/2;
88}
89
90unsigned 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] == '-')
95 return 20;
96
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] == '-')
100 return 20;
101
102 // If the target triple is something non-X86-64, we don't match.
103 if (!TT.empty()) return 0;
104
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
111
112 return getJITMatchQuality()/2;
113}
114
115X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
116 : X86TargetMachine(M, FS, false) {
117}
118
119
120X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
121 : X86TargetMachine(M, FS, true) {
122}
123
124/// X86TargetMachine ctor - Create an ILP32 architecture model
125///
126X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
127 bool is64Bit)
128 : Subtarget(M, FS, is64Bit),
Dale Johannesend4c671c2007-08-06 21:48:35 +0000129 DataLayout(Subtarget.getDataLayout()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 FrameInfo(TargetFrameInfo::StackGrowsDown,
131 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
132 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000133 DefRelocModel = getRelocationModel();
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000134 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000135 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikov0ab9fa82008-03-23 13:41:18 +0000136 if (Subtarget.isTargetDarwin() ||
137 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setRelocationModel(Reloc::DynamicNoPIC);
139 else
140 setRelocationModel(Reloc::Static);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000141 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 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);
149 }
150
151 if (Subtarget.isTargetCygMing())
152 Subtarget.setPICStyle(PICStyle::WinPIC);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000153 else if (Subtarget.isTargetDarwin()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 if (Subtarget.is64Bit())
155 Subtarget.setPICStyle(PICStyle::RIPRel);
156 else
157 Subtarget.setPICStyle(PICStyle::Stub);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000158 } else if (Subtarget.isTargetELF()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 if (Subtarget.is64Bit())
160 Subtarget.setPICStyle(PICStyle::RIPRel);
161 else
162 Subtarget.setPICStyle(PICStyle::GOT);
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000163 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164}
165
166//===----------------------------------------------------------------------===//
167// Pass Pipeline Configuration
168//===----------------------------------------------------------------------===//
169
Dan Gohmane34aa772008-03-11 22:29:46 +0000170bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // Install an instruction selector.
172 PM.add(createX86ISelDag(*this, Fast));
173 return false;
174}
175
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000176bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
Anton Korobeynikov8c912892008-04-23 18:23:30 +0000177 // Calculate and set max stack object alignment early, so we can decide
178 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov194ae6c2008-04-23 18:23:05 +0000179 PM.add(createX86MaxStackAlignmentCalculatorPass());
180 return false; // -print-machineinstr shouldn't print after this.
181}
182
Dan Gohmane34aa772008-03-11 22:29:46 +0000183bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 PM.add(createX86FloatingPointStackifierPass());
185 return true; // -print-machineinstr should print after this.
186}
187
Dan Gohmane34aa772008-03-11 22:29:46 +0000188bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 std::ostream &Out) {
190 PM.add(createX86CodePrinterPass(Out, *this));
191 return false;
192}
193
Dan Gohmane34aa772008-03-11 22:29:46 +0000194bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000195 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // FIXME: Move this to TargetJITInfo!
Dale Johannesen58c6d512008-08-12 21:02:08 +0000197 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
198 if (DefRelocModel == Reloc::Default &&
199 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit()))
Evan Cheng8ee6bab2007-12-22 09:40:20 +0000200 setRelocationModel(Reloc::Static);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
Dale Johannesenc501c082008-08-11 23:46:25 +0000202 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen58c6d512008-08-12 21:02:08 +0000203 // 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);
208 else
209 setCodeModel(CodeModel::Large);
210 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
212 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000213 if (DumpAsm)
214 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
215
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 return false;
217}
218
Dan Gohmane34aa772008-03-11 22:29:46 +0000219bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng77547212007-07-20 21:56:13 +0000220 bool DumpAsm, MachineCodeEmitter &MCE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng77547212007-07-20 21:56:13 +0000222 if (DumpAsm)
223 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return false;
225}