blob: a720e6e894d4abf272f25fcb7cce32e7a83deacc [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"
David Greene71847812009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000022#include "llvm/Target/TargetOptions.h"
Daniel Dunbar0c795d62009-07-25 06:49:55 +000023#include "llvm/Target/TargetRegistry.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Daniel Dunbar51b198a2009-07-15 20:24:03 +000026extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +000027 // Register the target.
28 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
29 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbar51b198a2009-07-15 20:24:03 +000030}
Douglas Gregor1555a232009-06-16 20:12:29 +000031
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000032const TargetAsmInfo *X86TargetMachine::createTargetAsmInfo() const {
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000033 if (Subtarget.isFlavorIntel())
34 return new X86WinTargetAsmInfo(*this);
35 else
36 switch (Subtarget.TargetType) {
37 case X86Subtarget::isDarwin:
38 return new X86DarwinTargetAsmInfo(*this);
39 case X86Subtarget::isELF:
40 return new X86ELFTargetAsmInfo(*this);
41 case X86Subtarget::isMingw:
42 case X86Subtarget::isCygwin:
43 return new X86COFFTargetAsmInfo(*this);
44 case X86Subtarget::isWindows:
45 return new X86WinTargetAsmInfo(*this);
46 default:
Anton Korobeynikov32b952a2008-09-25 21:00:33 +000047 return new X86GenericTargetAsmInfo(*this);
Anton Korobeynikov4468b7a2008-07-09 13:20:48 +000048 }
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000049}
50
Daniel Dunbar51b198a2009-07-15 20:24:03 +000051X86_32TargetMachine::X86_32TargetMachine(const Target &T, const Module &M,
52 const std::string &FS)
53 : X86TargetMachine(T, M, FS, false) {
Evan Cheng25ab6902006-09-08 06:48:29 +000054}
55
56
Daniel Dunbar51b198a2009-07-15 20:24:03 +000057X86_64TargetMachine::X86_64TargetMachine(const Target &T, const Module &M,
58 const std::string &FS)
59 : X86TargetMachine(T, M, FS, true) {
Evan Cheng25ab6902006-09-08 06:48:29 +000060}
61
Chris Lattner11348ee2009-07-09 03:32:31 +000062/// X86TargetMachine ctor - Create an X86 target.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000063///
Daniel Dunbar51b198a2009-07-15 20:24:03 +000064X86TargetMachine::X86TargetMachine(const Target &T, const Module &M,
65 const std::string &FS, bool is64Bit)
66 : LLVMTargetMachine(T),
67 Subtarget(M, FS, is64Bit),
Dale Johannesen27f92be2007-08-06 21:48:35 +000068 DataLayout(Subtarget.getDataLayout()),
Nate Begemanfb5792f2005-07-12 01:41:54 +000069 FrameInfo(TargetFrameInfo::StackGrowsDown,
Evan Cheng25ab6902006-09-08 06:48:29 +000070 Subtarget.getStackAlignment(), Subtarget.is64Bit() ? -8 : -4),
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000071 InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
Evan Chengaabe38b2007-12-22 09:40:20 +000072 DefRelocModel = getRelocationModel();
Chris Lattner11348ee2009-07-09 03:32:31 +000073
74 // If no relocation model was picked, default as appropriate for the target.
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000075 if (getRelocationModel() == Reloc::Default) {
Chris Lattner11348ee2009-07-09 03:32:31 +000076 if (!Subtarget.isTargetDarwin())
Evan Cheng2c312ad2006-12-04 18:07:10 +000077 setRelocationModel(Reloc::Static);
Chris Lattner11348ee2009-07-09 03:32:31 +000078 else if (Subtarget.is64Bit())
79 setRelocationModel(Reloc::PIC_);
80 else
81 setRelocationModel(Reloc::DynamicNoPIC);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +000082 }
Dan Gohman6520e202008-10-18 02:06:02 +000083
Chris Lattnere4df7562009-07-09 03:15:51 +000084 assert(getRelocationModel() != Reloc::Default &&
85 "Relocation mode not picked");
86
Chris Lattner11348ee2009-07-09 03:32:31 +000087 // If no code model is picked, default to small.
88 if (getCodeModel() == CodeModel::Default)
89 setCodeModel(CodeModel::Small);
90
Chris Lattnerb2fc55b2009-07-09 03:37:30 +000091 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
Chris Lattner11348ee2009-07-09 03:32:31 +000092 // is defined as a model for code which may be used in static or dynamic
Chris Lattner88e1fd52009-07-09 04:24:46 +000093 // executables but not necessarily a shared library. On X86-32 we just
94 // compile in -static mode, in x86-64 we use PIC.
95 if (getRelocationModel() == Reloc::DynamicNoPIC) {
96 if (is64Bit)
97 setRelocationModel(Reloc::PIC_);
98 else if (!Subtarget.isTargetDarwin())
99 setRelocationModel(Reloc::Static);
100 }
Dan Gohman6520e202008-10-18 02:06:02 +0000101
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000102 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
103 // the Mach-O file format doesn't support it.
104 if (getRelocationModel() == Reloc::Static &&
105 Subtarget.isTargetDarwin() &&
106 is64Bit)
107 setRelocationModel(Reloc::PIC_);
108
Chris Lattner11348ee2009-07-09 03:32:31 +0000109 // Determine the PICStyle based on the target selected.
110 if (getRelocationModel() == Reloc::Static) {
111 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
112 Subtarget.setPICStyle(PICStyles::None);
113 } else if (Subtarget.isTargetCygMing()) {
Chris Lattnere4df7562009-07-09 03:15:51 +0000114 Subtarget.setPICStyle(PICStyles::None);
115 } else if (Subtarget.isTargetDarwin()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000116 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000117 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner8097b652009-07-10 20:58:47 +0000118 else if (getRelocationModel() == Reloc::PIC_)
119 Subtarget.setPICStyle(PICStyles::StubPIC);
120 else {
121 assert(getRelocationModel() == Reloc::DynamicNoPIC);
122 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
123 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000124 } else if (Subtarget.isTargetELF()) {
Evan Chengae19abc2007-01-18 22:27:12 +0000125 if (Subtarget.is64Bit())
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000126 Subtarget.setPICStyle(PICStyles::RIPRel);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000127 else
Duncan Sandsf9a67a82008-11-28 09:29:37 +0000128 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000129 }
Chris Lattnere4df7562009-07-09 03:15:51 +0000130
Chris Lattner11348ee2009-07-09 03:32:31 +0000131 // Finally, if we have "none" as our PIC style, force to static mode.
132 if (Subtarget.getPICStyle() == PICStyles::None)
133 setRelocationModel(Reloc::Static);
Chris Lattner4efab052006-02-03 18:59:39 +0000134}
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000135
Chris Lattner1911fd42006-09-04 04:14:57 +0000136//===----------------------------------------------------------------------===//
137// Pass Pipeline Configuration
138//===----------------------------------------------------------------------===//
Chris Lattnerc9bbfbc2003-08-05 16:34:44 +0000139
Bill Wendling98a366d2009-04-29 23:29:43 +0000140bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
141 CodeGenOpt::Level OptLevel) {
Nate Begeman73bfa712005-08-18 23:53:15 +0000142 // Install an instruction selector.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000143 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman71b7f642008-10-25 17:46:52 +0000144
145 // If we're using Fast-ISel, clean up the mess.
146 if (EnableFastISel)
147 PM.add(createDeadMachineInstructionElimPass());
148
Dan Gohmanbc5cbb82008-11-12 22:55:05 +0000149 // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
150 PM.add(createX87FPRegKillInserterPass());
151
Chris Lattner1911fd42006-09-04 04:14:57 +0000152 return false;
Brian Gaekede3aa4f2003-06-18 21:43:21 +0000153}
154
Bill Wendling98a366d2009-04-29 23:29:43 +0000155bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
156 CodeGenOpt::Level OptLevel) {
Anton Korobeynikovd52bdaf2008-04-23 18:23:30 +0000157 // Calculate and set max stack object alignment early, so we can decide
158 // whether we will need stack realignment (and thus FP).
Anton Korobeynikov856914f2008-04-23 18:23:05 +0000159 PM.add(createX86MaxStackAlignmentCalculatorPass());
160 return false; // -print-machineinstr shouldn't print after this.
161}
162
Bill Wendling98a366d2009-04-29 23:29:43 +0000163bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
164 CodeGenOpt::Level OptLevel) {
Chris Lattnerd91d86f2003-01-13 00:51:23 +0000165 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner1911fd42006-09-04 04:14:57 +0000166 return true; // -print-machineinstr should print after this.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000167}
168
Bill Wendling98a366d2009-04-29 23:29:43 +0000169bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
170 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000171 MachineCodeEmitter &MCE) {
Chris Lattner2130b992006-09-04 18:48:41 +0000172 // FIXME: Move this to TargetJITInfo!
Dale Johannesen5f777192008-08-12 21:02:08 +0000173 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
174 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000175 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Evan Chengaabe38b2007-12-22 09:40:20 +0000176 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000177 Subtarget.setPICStyle(PICStyles::None);
178 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000179
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000180 // 64-bit JIT places everything in the same buffer except external functions.
Dale Johannesen5f777192008-08-12 21:02:08 +0000181 // On Darwin, use small code model but hack the call instruction for
182 // externals. Elsewhere, do not assume globals are in the lower 4G.
183 if (Subtarget.is64Bit()) {
184 if (Subtarget.isTargetDarwin())
185 setCodeModel(CodeModel::Small);
186 else
187 setCodeModel(CodeModel::Large);
188 }
Anton Korobeynikov15fccf12006-12-20 01:03:20 +0000189
Evan Cheng55fc2802006-07-25 20:40:54 +0000190 PM.add(createX86CodeEmitterPass(*this, MCE));
Evan Cheng8bd60352007-07-20 21:56:13 +0000191
Chris Lattner81b6ed72005-07-11 05:17:48 +0000192 return false;
193}
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000194
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000195bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000197 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000198 // FIXME: Move this to TargetJITInfo!
199 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
200 if (DefRelocModel == Reloc::Default &&
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000201 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000202 setRelocationModel(Reloc::Static);
Chris Lattnerb2fc55b2009-07-09 03:37:30 +0000203 Subtarget.setPICStyle(PICStyles::None);
204 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000205
206 // 64-bit JIT places everything in the same buffer except external functions.
207 // On Darwin, use small code model but hack the call instruction for
208 // externals. Elsewhere, do not assume globals are in the lower 4G.
209 if (Subtarget.is64Bit()) {
210 if (Subtarget.isTargetDarwin())
211 setCodeModel(CodeModel::Small);
212 else
213 setCodeModel(CodeModel::Large);
214 }
215
216 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000217
218 return false;
219}
220
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000221bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
222 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000223 ObjectCodeEmitter &OCE) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000224 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000225 return false;
226}
227
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000228bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000229 CodeGenOpt::Level OptLevel,
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000230 MachineCodeEmitter &MCE) {
Bill Wendlingeb1ac332007-02-08 01:39:44 +0000231 PM.add(createX86CodeEmitterPass(*this, MCE));
232 return false;
233}
Dan Gohman97135e12008-09-26 19:15:30 +0000234
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000235bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
236 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000237 JITCodeEmitter &JCE) {
238 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000239 return false;
240}
241
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000242bool X86TargetMachine::addSimpleCodeEmitter(PassManagerBase &PM,
243 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000244 ObjectCodeEmitter &OCE) {
245 PM.add(createX86ObjectCodeEmitterPass(*this, OCE));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000246 return false;
247}