blob: 8e10ee9b225d671c93e06b1c5b08e8f984c6c547 [file] [log] [blame]
Chris Lattnerb4f68ed2002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00009//
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000010// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000014#include "X86TargetAsmInfo.h"
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000015#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000016#include "X86.h"
Chris Lattnerbb144a82003-08-24 19:49:48 +000017#include "llvm/Module.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000018#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000020#include "llvm/CodeGen/Passes.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000022#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Jeff Cohen1c32f792005-01-03 16:34:19 +000025/// 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 Gohman844731a2008-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");
Chris Lattner439a27a2002-12-16 16:15:51 +000037
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000038const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-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 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000055}
56
Evan Cheng25ab6902006-09-08 06:48:29 +000057unsigned X86_32TargetMachine::getJITMatchQuality() {
Chris Lattner7d0974b2004-10-18 15:54:17 +000058#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerd36c9702004-07-11 02:48:49 +000059 return 10;
Chris Lattnerd36c9702004-07-11 02:48:49 +000060#endif
Evan Cheng25ab6902006-09-08 06:48:29 +000061 return 0;
Chris Lattnerd36c9702004-07-11 02:48:49 +000062}
63
Evan Cheng25ab6902006-09-08 06:48:29 +000064unsigned X86_64TargetMachine::getJITMatchQuality() {
Anton Korobeynikov8c278292008-03-23 13:43:47 +000065#if defined(__x86_64__) || defined(_M_AMD64)
Evan Cheng25ab6902006-09-08 06:48:29 +000066 return 10;
67#endif
68 return 0;
69}
70
71unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000072 // 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;
Chris Lattner87bdba62007-07-09 17:25:29 +000077 // If the target triple is something non-X86, we don't match.
78 if (!TT.empty()) return 0;
Chris Lattner3ea78c42004-12-12 17:40:28 +000079
Chris Lattnerd36c9702004-07-11 02:48:49 +000080 if (M.getEndianness() == Module::LittleEndian &&
81 M.getPointerSize() == Module::Pointer32)
Chris Lattner3ea78c42004-12-12 17:40:28 +000082 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000083 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}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000089
Evan Cheng25ab6902006-09-08 06:48:29 +000090unsigned 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
Chris Lattner21847f42006-12-19 19:40:09 +000097 // 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
Chris Lattner87bdba62007-07-09 17:25:29 +0000102 // If the target triple is something non-X86-64, we don't match.
103 if (!TT.empty()) return 0;
104
Evan Cheng25ab6902006-09-08 06:48:29 +0000105 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
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000124/// X86TargetMachine ctor - Create an ILP32 architecture model
125///
Evan Chengcdc69442007-02-23 03:03:16 +0000126X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS,
127 bool is64Bit)
Evan Cheng25ab6902006-09-08 06:48:29 +0000128 : Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +0000129 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +0000130 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Evan Chengaa3c1412006-05-30 21:45:53 +0000132 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000133 DefRelocModel = getRelocationModel();
Anton Korobeynikovc15b81b2008-03-23 13:41:18 +0000134 // FIXME: Correctly select PIC model for Win64 stuff
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000135 if (getRelocationModel() == Reloc::Default) {
Anton Korobeynikovc15b81b2008-03-23 13:41:18 +0000136 if (Subtarget.isTargetDarwin() ||
137 (Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64()))
Evan Cheng4c1aa862006-02-22 20:19:42 +0000138 setRelocationModel(Reloc::DynamicNoPIC);
139 else
Evan Cheng2c312ad2006-12-04 18:07:10 +0000140 setRelocationModel(Reloc::Static);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000141 }
Evan Cheng25ab6902006-09-08 06:48:29 +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 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000150
Evan Chengae19abc2007-01-18 22:27:12 +0000151 if (Subtarget.isTargetCygMing())
152 Subtarget.setPICStyle(PICStyle::WinPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000153 else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000154 if (Subtarget.is64Bit())
155 Subtarget.setPICStyle(PICStyle::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000156 else
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000157 Subtarget.setPICStyle(PICStyle::Stub);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000158 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000159 if (Subtarget.is64Bit())
160 Subtarget.setPICStyle(PICStyle::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000161 else
Evan Chengae19abc2007-01-18 22:27:12 +0000162 Subtarget.setPICStyle(PICStyle::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000163 }
Chris Lattner4efab052006-02-03 18:59:39 +0000164}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000165
Chris Lattner1911fd42006-09-04 04:14:57 +0000166//===----------------------------------------------------------------------===//
167// Pass Pipeline Configuration
168//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000169
Dan Gohmanbfae8312008-03-11 22:29:46 +0000170bool X86TargetMachine::addInstSelector(PassManagerBase &PM, bool Fast) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000171 // Install an instruction selector.
Evan Chenge50794a2006-08-29 18:28:33 +0000172 PM.add(createX86ISelDag(*this, Fast));
Chris Lattner1911fd42006-09-04 04:14:57 +0000173 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000174}
175
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000176bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM, bool Fast) {
Anton Korobeynikovd52bdaf2008-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 Korobeynikov856914f2008-04-23 18:23:05 +0000179 PM.add(createX86MaxStackAlignmentCalculatorPass());
180 return false; // -print-machineinstr shouldn't print after this.
181}
182
Dan Gohmanbfae8312008-03-11 22:29:46 +0000183bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM, bool Fast) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000184 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000185 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000186}
187
Dan Gohmanbfae8312008-03-11 22:29:46 +0000188bool X86TargetMachine::addAssemblyEmitter(PassManagerBase &PM, bool Fast,
Chris Lattner1911fd42006-09-04 04:14:57 +0000189 std::ostream &Out) {
190 PM.add(createX86CodePrinterPass(Out, *this));
191 return false;
192}
193
Dan Gohmanbfae8312008-03-11 22:29:46 +0000194bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng8bd60352007-07-20 21:56:13 +0000195 bool DumpAsm, MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000196 // FIXME: Move this to TargetJITInfo!
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000197 // Do not override 64-bit setting made in X86TargetMachine().
198 if (DefRelocModel == Reloc::Default && !Subtarget.is64Bit())
Evan Chengaabe38b2007-12-22 09:40:20 +0000199 setRelocationModel(Reloc::Static);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000200
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000201 // 64-bit JIT places everything in the same buffer except external functions.
202 // Use small code model but hack the call instruction for externals.
Evan Cheng28b514392006-12-05 19:50:18 +0000203 if (Subtarget.is64Bit())
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000204 setCodeModel(CodeModel::Small);
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000205
Evan Cheng55fc2802006-07-25 20:40:54 +0000206 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000207 if (DumpAsm)
208 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
209
Chris Lattner81b6ed72005-07-11 05:17:48 +0000210 return false;
211}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000212
Dan Gohmanbfae8312008-03-11 22:29:46 +0000213bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM, bool Fast,
Evan Cheng8bd60352007-07-20 21:56:13 +0000214 bool DumpAsm, MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000215 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000216 if (DumpAsm)
217 PM.add(createX86CodePrinterPass(*cerr.stream(), *this));
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000218 return false;
219}