blob: b4fc79abcaeaddc4ede5f4f286f75c846e47c959 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattner155e68f2003-04-23 16:24:55 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000024#include <iostream>
Chris Lattner1e60a912003-12-20 01:22:19 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Jeff Cohen1c32f792005-01-03 16:34:19 +000027/// X86TargetMachineModule - Note that this is used on hosts that cannot link
28/// in a library unless there are references into the library. In particular,
29/// it seems that it is not possible to get things to work on Win32 without
30/// this. Though it is unused, do not remove it.
31extern "C" int X86TargetMachineModule;
32int X86TargetMachineModule = 0;
33
Chris Lattner439a27a2002-12-16 16:15:51 +000034namespace {
Chris Lattnerd36c9702004-07-11 02:48:49 +000035 // Register the target.
Evan Cheng25ab6902006-09-08 06:48:29 +000036 RegisterTarget<X86_32TargetMachine>
37 X("x86", " 32-bit X86: Pentium-Pro and above");
38 RegisterTarget<X86_64TargetMachine>
39 Y("x86-64", " 64-bit X86: EM64T and AMD64");
Chris Lattner439a27a2002-12-16 16:15:51 +000040}
41
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000042const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
43 return new X86TargetAsmInfo(*this);
44}
45
Evan Cheng25ab6902006-09-08 06:48:29 +000046unsigned X86_32TargetMachine::getJITMatchQuality() {
Chris Lattner7d0974b2004-10-18 15:54:17 +000047#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerd36c9702004-07-11 02:48:49 +000048 return 10;
Chris Lattnerd36c9702004-07-11 02:48:49 +000049#endif
Evan Cheng25ab6902006-09-08 06:48:29 +000050 return 0;
Chris Lattnerd36c9702004-07-11 02:48:49 +000051}
52
Evan Cheng25ab6902006-09-08 06:48:29 +000053unsigned X86_64TargetMachine::getJITMatchQuality() {
54#if defined(__x86_64__)
55 return 10;
56#endif
57 return 0;
58}
59
60unsigned X86_32TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000061 // We strongly match "i[3-9]86-*".
62 std::string TT = M.getTargetTriple();
63 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
64 TT[4] == '-' && TT[1] - '3' < 6)
65 return 20;
66
Chris Lattnerd36c9702004-07-11 02:48:49 +000067 if (M.getEndianness() == Module::LittleEndian &&
68 M.getPointerSize() == Module::Pointer32)
Chris Lattner3ea78c42004-12-12 17:40:28 +000069 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000070 else if (M.getEndianness() != Module::AnyEndianness ||
71 M.getPointerSize() != Module::AnyPointerSize)
72 return 0; // Match for some other target
73
74 return getJITMatchQuality()/2;
75}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000076
Evan Cheng25ab6902006-09-08 06:48:29 +000077unsigned X86_64TargetMachine::getModuleMatchQuality(const Module &M) {
78 // We strongly match "x86_64-*".
79 std::string TT = M.getTargetTriple();
80 if (TT.size() >= 7 && TT[0] == 'x' && TT[1] == '8' && TT[2] == '6' &&
81 TT[3] == '_' && TT[4] == '6' && TT[5] == '4' && TT[6] == '-')
82 return 20;
83
84 if (M.getEndianness() == Module::LittleEndian &&
85 M.getPointerSize() == Module::Pointer64)
86 return 10; // Weak match
87 else if (M.getEndianness() != Module::AnyEndianness ||
88 M.getPointerSize() != Module::AnyPointerSize)
89 return 0; // Match for some other target
90
91 return getJITMatchQuality()/2;
92}
93
94X86_32TargetMachine::X86_32TargetMachine(const Module &M, const std::string &FS)
95 : X86TargetMachine(M, FS, false) {
96}
97
98
99X86_64TargetMachine::X86_64TargetMachine(const Module &M, const std::string &FS)
100 : X86TargetMachine(M, FS, true) {
101}
102
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000103/// X86TargetMachine ctor - Create an ILP32 architecture model
104///
Evan Cheng25ab6902006-09-08 06:48:29 +0000105X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS, bool is64Bit)
106 : Subtarget(M, FS, is64Bit),
107 DataLayout(Subtarget.is64Bit() ?
108 std::string("e-p:64:64-d:32-l:32") :
109 std::string("e-p:32:32-d:32-l:32")),
Nate Begemanfb5792f2005-07-12 01:41:54 +0000110 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Evan Chengaa3c1412006-05-30 21:45:53 +0000112 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000113 if (getRelocationModel() == Reloc::Default)
114 if (Subtarget.isTargetDarwin())
115 setRelocationModel(Reloc::DynamicNoPIC);
116 else
Chris Lattner35d86fe2006-07-26 21:12:04 +0000117 setRelocationModel(Reloc::PIC_);
Evan Cheng25ab6902006-09-08 06:48:29 +0000118 if (Subtarget.is64Bit()) {
119 // No DynamicNoPIC support under X86-64.
120 if (getRelocationModel() == Reloc::DynamicNoPIC)
121 setRelocationModel(Reloc::PIC_);
122 // Default X86-64 code model is small.
123 if (getCodeModel() == CodeModel::Default)
124 setCodeModel(CodeModel::Small);
125 }
Chris Lattner4efab052006-02-03 18:59:39 +0000126}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000127
Chris Lattner1911fd42006-09-04 04:14:57 +0000128//===----------------------------------------------------------------------===//
129// Pass Pipeline Configuration
130//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000131
Chris Lattner1911fd42006-09-04 04:14:57 +0000132bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000133 // Install an instruction selector.
Evan Chenge50794a2006-08-29 18:28:33 +0000134 PM.add(createX86ISelDag(*this, Fast));
Chris Lattner1911fd42006-09-04 04:14:57 +0000135 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000136}
137
Chris Lattner1911fd42006-09-04 04:14:57 +0000138bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000139 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000140 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000141}
142
Chris Lattner1911fd42006-09-04 04:14:57 +0000143bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
144 std::ostream &Out) {
145 PM.add(createX86CodePrinterPass(Out, *this));
146 return false;
147}
148
149bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
150 std::ostream &Out) {
151 if (Subtarget.isTargetELF()) {
152 addX86ELFObjectWriterPass(PM, Out, *this);
153 return false;
154 }
155 return true;
156}
157
158bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
159 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000160 // FIXME: Move this to TargetJITInfo!
161 setRelocationModel(Reloc::Static);
162
Evan Cheng55fc2802006-07-25 20:40:54 +0000163 PM.add(createX86CodeEmitterPass(*this, MCE));
Chris Lattner81b6ed72005-07-11 05:17:48 +0000164 return false;
165}