blob: 83af172c7f0721f4b863bc05d9a79b88643497f3 [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
14#include "X86TargetMachine.h"
Chris Lattner5bcd95c2002-12-24 00:04:01 +000015#include "X86.h"
Chris Lattnerbb144a82003-08-24 19:49:48 +000016#include "llvm/Module.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000017#include "llvm/PassManager.h"
Chris Lattner3dffa792002-10-30 00:47:49 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd91d86f2003-01-13 00:51:23 +000019#include "llvm/CodeGen/Passes.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000021#include "llvm/Target/TargetMachineRegistry.h"
Chris Lattner155e68f2003-04-23 16:24:55 +000022#include "llvm/Transforms/Scalar.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000023#include <iostream>
Chris Lattner1e60a912003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Jeff Cohen1c32f792005-01-03 16:34:19 +000026/// X86TargetMachineModule - Note that this is used on hosts that cannot link
27/// in a library unless there are references into the library. In particular,
28/// it seems that it is not possible to get things to work on Win32 without
29/// this. Though it is unused, do not remove it.
30extern "C" int X86TargetMachineModule;
31int X86TargetMachineModule = 0;
32
Chris Lattner439a27a2002-12-16 16:15:51 +000033namespace {
Chris Lattnerd36c9702004-07-11 02:48:49 +000034 // Register the target.
Chris Lattner71d24aa2004-07-11 03:27:42 +000035 RegisterTarget<X86TargetMachine> X("x86", " IA-32 (Pentium and above)");
Chris Lattner439a27a2002-12-16 16:15:51 +000036}
37
Chris Lattnerd36c9702004-07-11 02:48:49 +000038unsigned X86TargetMachine::getJITMatchQuality() {
Chris Lattner7d0974b2004-10-18 15:54:17 +000039#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)
Chris Lattnerd36c9702004-07-11 02:48:49 +000040 return 10;
41#else
42 return 0;
43#endif
44}
45
46unsigned X86TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000047 // We strongly match "i[3-9]86-*".
48 std::string TT = M.getTargetTriple();
49 if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
50 TT[4] == '-' && TT[1] - '3' < 6)
51 return 20;
52
Chris Lattnerd36c9702004-07-11 02:48:49 +000053 if (M.getEndianness() == Module::LittleEndian &&
54 M.getPointerSize() == Module::Pointer32)
Chris Lattner3ea78c42004-12-12 17:40:28 +000055 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000056 else if (M.getEndianness() != Module::AnyEndianness ||
57 M.getPointerSize() != Module::AnyPointerSize)
58 return 0; // Match for some other target
59
60 return getJITMatchQuality()/2;
61}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000062
63/// X86TargetMachine ctor - Create an ILP32 architecture model
64///
Chris Lattnerbc641b92006-03-23 05:43:16 +000065X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS)
Chris Lattnerc4fa3862006-09-03 18:44:02 +000066 : Subtarget(M, FS), DataLayout("e-p:32:32-d:32-l:32"),
Nate Begemanfb5792f2005-07-12 01:41:54 +000067 FrameInfo(TargetFrameInfo::StackGrowsDown,
68 Subtarget.getStackAlignment(), -4),
Evan Chengaa3c1412006-05-30 21:45:53 +000069 InstrInfo(*this), JITInfo(*this), TLInfo(*this) {
Evan Cheng4c1aa862006-02-22 20:19:42 +000070 if (getRelocationModel() == Reloc::Default)
71 if (Subtarget.isTargetDarwin())
72 setRelocationModel(Reloc::DynamicNoPIC);
73 else
Chris Lattner35d86fe2006-07-26 21:12:04 +000074 setRelocationModel(Reloc::PIC_);
Chris Lattner4efab052006-02-03 18:59:39 +000075}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000076
Chris Lattner1911fd42006-09-04 04:14:57 +000077//===----------------------------------------------------------------------===//
78// Pass Pipeline Configuration
79//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +000080
Chris Lattner1911fd42006-09-04 04:14:57 +000081bool X86TargetMachine::addInstSelector(FunctionPassManager &PM, bool Fast) {
Nate Begeman73bfa712005-08-18 23:53:15 +000082 // Install an instruction selector.
Evan Chenge50794a2006-08-29 18:28:33 +000083 PM.add(createX86ISelDag(*this, Fast));
Chris Lattner1911fd42006-09-04 04:14:57 +000084 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +000085}
86
Chris Lattner1911fd42006-09-04 04:14:57 +000087bool X86TargetMachine::addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +000088 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +000089 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000090}
91
Chris Lattner1911fd42006-09-04 04:14:57 +000092bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
93 std::ostream &Out) {
94 PM.add(createX86CodePrinterPass(Out, *this));
95 return false;
96}
97
98bool X86TargetMachine::addObjectWriter(FunctionPassManager &PM, bool Fast,
99 std::ostream &Out) {
100 if (Subtarget.isTargetELF()) {
101 addX86ELFObjectWriterPass(PM, Out, *this);
102 return false;
103 }
104 return true;
105}
106
107bool X86TargetMachine::addCodeEmitter(FunctionPassManager &PM, bool Fast,
108 MachineCodeEmitter &MCE) {
Evan Cheng55fc2802006-07-25 20:40:54 +0000109 PM.add(createX86CodeEmitterPass(*this, MCE));
Chris Lattner81b6ed72005-07-11 05:17:48 +0000110 return false;
111}