blob: 15c6c4e7a7d212833a59b6eda52c53fea2e340f5 [file] [log] [blame]
Chris Lattner02a3d832002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00009//
Chris Lattner02a3d832002-10-29 22:37:54 +000010// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetMachine.h"
Chris Lattnera32b4052002-12-24 00:04:01 +000015#include "X86.h"
Chris Lattner524608a2003-04-23 16:24:55 +000016#include "llvm/PassManager.h"
Chris Lattnerd7a85562002-10-30 00:47:49 +000017#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000018#include "llvm/CodeGen/Passes.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000019#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000020#include "llvm/Support/FormattedStream.h"
Chris Lattnerc2726e62004-07-11 04:17:10 +000021#include "llvm/Target/TargetOptions.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000025extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000026 // Register the target.
27 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
28 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000029}
Douglas Gregor1b731d52009-06-16 20:12:29 +000030
Jim Laskeyae92ce82006-09-07 23:39:26 +000031
Evan Cheng2129f592011-07-19 06:37:02 +000032X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
33 StringRef CPU, StringRef FS,
Evan Chengefd9b422011-07-20 07:51:56 +000034 Reloc::Model RM, CodeModel::Model CM)
35 : X86TargetMachine(T, TT, CPU, FS, RM, CM, false),
Rafael Espindola66e08d42010-10-03 18:59:45 +000036 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
Evan Cheng06fdaeb2011-10-14 20:36:23 +000037 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-"
38 "n8:16:32-S128" :
Rafael Espindola66e08d42010-10-03 18:59:45 +000039 (getSubtargetImpl()->isTargetCygMing() ||
40 getSubtargetImpl()->isTargetWindows()) ?
Evan Cheng06fdaeb2011-10-14 20:36:23 +000041 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-"
42 "n8:16:32-S32" :
43 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-"
44 "n8:16:32-S128"),
Rafael Espindola66e08d42010-10-03 18:59:45 +000045 InstrInfo(*this),
46 TSInfo(*this),
47 TLInfo(*this),
48 JITInfo(*this) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +000049}
50
51
Evan Cheng2129f592011-07-19 06:37:02 +000052X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
53 StringRef CPU, StringRef FS,
Evan Chengefd9b422011-07-20 07:51:56 +000054 Reloc::Model RM, CodeModel::Model CM)
55 : X86TargetMachine(T, TT, CPU, FS, RM, CM, true),
Evan Cheng06fdaeb2011-10-14 20:36:23 +000056 DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-"
57 "n8:16:32:64-S128"),
Rafael Espindola66e08d42010-10-03 18:59:45 +000058 InstrInfo(*this),
59 TSInfo(*this),
60 TLInfo(*this),
61 JITInfo(*this) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +000062}
63
Chris Lattner7e3abf12009-07-09 03:32:31 +000064/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000065///
Evan Cheng2129f592011-07-19 06:37:02 +000066X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
67 StringRef CPU, StringRef FS,
Evan Chengefd9b422011-07-20 07:51:56 +000068 Reloc::Model RM, CodeModel::Model CM,
69 bool is64Bit)
70 : LLVMTargetMachine(T, TT, CPU, FS, RM, CM),
Evan Cheng60fc0fc2011-07-08 22:30:25 +000071 Subtarget(TT, CPU, FS, StackAlignmentOverride, is64Bit),
Anton Korobeynikov2f931282011-01-10 12:39:04 +000072 FrameLowering(*this, Subtarget),
Rafael Espindola66e08d42010-10-03 18:59:45 +000073 ELFWriterInfo(is64Bit, true) {
Chris Lattner7e3abf12009-07-09 03:32:31 +000074 // Determine the PICStyle based on the target selected.
75 if (getRelocationModel() == Reloc::Static) {
76 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
77 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000078 } else if (Subtarget.is64Bit()) {
79 // PIC in 64 bit mode is always rip-rel.
80 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner7e3abf12009-07-09 03:32:31 +000081 } else if (Subtarget.isTargetCygMing()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +000082 Subtarget.setPICStyle(PICStyles::None);
83 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000084 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +000085 Subtarget.setPICStyle(PICStyles::StubPIC);
86 else {
87 assert(getRelocationModel() == Reloc::DynamicNoPIC);
88 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
89 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000090 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000091 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000092 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000093
Evan Cheng3a0c5e52011-06-23 17:54:54 +000094 // default to hard float ABI
95 if (FloatABIType == FloatABI::Default)
96 FloatABIType = FloatABI::Hard;
Chris Lattnera1d312c2006-02-03 18:59:39 +000097}
Chris Lattner02a3d832002-10-29 22:37:54 +000098
Chris Lattner12e97302006-09-04 04:14:57 +000099//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000100// Command line options for x86
101//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000102static cl::opt<bool>
103UseVZeroUpper("x86-use-vzeroupper",
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000104 cl::desc("Minimize AVX to SSE transition penalty"),
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000105 cl::init(false));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000106
107//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000108// Pass Pipeline Configuration
109//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000110
Bill Wendling026e5d72009-04-29 23:29:43 +0000111bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
112 CodeGenOpt::Level OptLevel) {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000113 // Install an instruction selector.
Bill Wendling084669a2009-04-29 00:15:41 +0000114 PM.add(createX86ISelDag(*this, OptLevel));
Dan Gohman19145312008-10-25 17:46:52 +0000115
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000116 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
117 if (!Subtarget.is64Bit())
118 PM.add(createGlobalBaseRegPass());
119
Chris Lattner12e97302006-09-04 04:14:57 +0000120 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000121}
122
Bill Wendling026e5d72009-04-29 23:29:43 +0000123bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
124 CodeGenOpt::Level OptLevel) {
Jim Grosbach4dac8902010-04-06 20:26:37 +0000125 PM.add(createX86MaxStackAlignmentHeuristicPass());
Anton Korobeynikov26590112008-04-23 18:23:05 +0000126 return false; // -print-machineinstr shouldn't print after this.
127}
128
Bill Wendling026e5d72009-04-29 23:29:43 +0000129bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
130 CodeGenOpt::Level OptLevel) {
Chris Lattner962d5be2003-01-13 00:51:23 +0000131 PM.add(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000132 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000133}
134
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000135bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
136 CodeGenOpt::Level OptLevel) {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000137 bool ShouldPrint = false;
138 if (OptLevel != CodeGenOpt::None &&
139 (Subtarget.hasSSE2() || Subtarget.hasAVX())) {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000140 PM.add(createExecutionDependencyFixPass(&X86::VR128RegClass));
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000141 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000142 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000143
144 if (Subtarget.hasAVX() && UseVZeroUpper) {
145 PM.add(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000146 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000147 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000148
149 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000150}
151
Bill Wendling026e5d72009-04-29 23:29:43 +0000152bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
153 CodeGenOpt::Level OptLevel,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000154 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000155 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000156
157 return false;
158}