blob: 807b462b90b0968cd709b0a6a5eadebebbf5bc3e [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.
Chris Lattner71d24aa2004-07-11 03:27:42 +000036 RegisterTarget<X86TargetMachine> X("x86", " IA-32 (Pentium and above)");
Chris Lattner439a27a2002-12-16 16:15:51 +000037}
38
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000039const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
40 return new X86TargetAsmInfo(*this);
41}
42
Chris Lattnerd36c9702004-07-11 02:48:49 +000043unsigned X86TargetMachine::getJITMatchQuality() {
Chris Lattner7d0974b2004-10-18 15:54:17 +000044#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerd36c9702004-07-11 02:48:49 +000045 return 10;
46#else
47 return 0;
48#endif
49}
50
51unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000052 // We strongly match "i[3-9]86-*".
53 std::string TT = M.getTargetTriple();
54 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
55 TT[4] == '-' && TT[1] - '3' < 6)
56 return 20;
57
Chris Lattnerd36c9702004-07-11 02:48:49 +000058 if (M.getEndianness() == Module::LittleEndian &&
59 M.getPointerSize() == Module::Pointer32)
Chris Lattner3ea78c42004-12-12 17:40:28 +000060 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000061 else if (M.getEndianness() != Module::AnyEndianness ||
62 M.getPointerSize() != Module::AnyPointerSize)
63 return 0; // Match for some other target
64
65 return getJITMatchQuality()/2;
66}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000067
68/// X86TargetMachine ctor - Create an ILP32 architecture model
69///
Chris Lattnerbc641b92006-03-23 05:43:16 +000070X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS)
Chris Lattnerc4fa3862006-09-03 18:44:02 +000071 : Subtarget(M, FS), DataLayout("e-p:32:32-d:32-l:32"),
Nate Begemanfb5792f2005-07-12 01:41:54 +000072 FrameInfo(TargetFrameInfo::StackGrowsDown,
73 Subtarget.getStackAlignment(), -4),
Evan Chengaa3c1412006-05-30 21:45:53 +000074 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000075 if (getRelocationModel() == Reloc::Default)
76 if (Subtarget.isTargetDarwin())
77 setRelocationModel(Reloc::DynamicNoPIC);
78 else
Chris Lattner35d86fe2006-07-26 21:12:04 +000079 setRelocationModel(Reloc::PIC_);
Chris Lattner4efab052006-02-03 18:59:39 +000080}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000081
Chris Lattner1911fd42006-09-04 04:14:57 +000082//===----------------------------------------------------------------------===//
83// Pass Pipeline Configuration
84//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000085
Chris Lattner1911fd42006-09-04 04:14:57 +000086bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
Nate Begeman73bfa712005-08-18 23:53:15 +000087 // Install an instruction selector.
Evan Chenge50794a2006-08-29 18:28:33 +000088 PM.add(createX86ISelDag(*this, Fast));
Chris Lattner1911fd42006-09-04 04:14:57 +000089 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +000090}
91
Chris Lattner1911fd42006-09-04 04:14:57 +000092bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +000093 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +000094 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000095}
96
Chris Lattner1911fd42006-09-04 04:14:57 +000097bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
98 std::ostream &Out) {
99 PM.add(createX86CodePrinterPass(Out, *this));
100 return false;
101}
102
103bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
104 std::ostream &Out) {
105 if (Subtarget.isTargetELF()) {
106 addX86ELFObjectWriterPass(PM, Out, *this);
107 return false;
108 }
109 return true;
110}
111
112bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
113 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000114 // FIXME: Move this to TargetJITInfo!
115 setRelocationModel(Reloc::Static);
116
Evan Cheng55fc2802006-07-25 20:40:54 +0000117 PM.add(createX86CodeEmitterPass(*this, MCE));
Chris Lattner81b6ed72005-07-11 05:17:48 +0000118 return false;
119}