blob: 274808839cda9524476099ee20e2c72282fc63f6 [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 Lattnerd7a85562002-10-30 00:47:49 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000017#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/PassManager.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"
Evan Cheng2bb40352011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetOptions.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
David Blaikiea379b1812011-12-20 02:50:00 +000031void X86_32TargetMachine::anchor() { }
Jim Laskeyae92ce82006-09-07 23:39:26 +000032
Rafael Espindola002f8aa2013-12-10 22:05:32 +000033static std::string computeDataLayout(const X86Subtarget &ST) {
34 // X86 is little endian
35 std::string Ret = "e";
36
37 // X86 and x32 have 32 bit pointers, x86-64 has 64 bit pointers
38 if (ST.isTarget64BitILP32() || !ST.is64Bit())
39 Ret += "-p:32:32";
40 else
41 Ret += "-p:64:64";
42
43 // Objects on the stack ore aligned to 64 bits.
44 // FIXME: of any size?
45 if (ST.is64Bit())
46 Ret += "-s:64";
47
48 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
49 if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetWindows())
Rafael Espindola1caa6932013-12-13 17:56:11 +000050 Ret += "-i64:64:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000051 else
Rafael Espindola1caa6932013-12-13 17:56:11 +000052 Ret += "-f64:32:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000053
54 // Some ABIs align long double to 128 bits, others to 32.
55 if (ST.is64Bit() || ST.isTargetDarwin())
56 Ret += "-f80:128:128";
57 else
58 Ret += "-f80:32:32";
59
Rafael Espindola002f8aa2013-12-10 22:05:32 +000060 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
61 if (ST.is64Bit())
62 Ret += "-n8:16:32:64";
63 else
64 Ret += "-n8:16:32";
65
66 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
67 if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetWindows()))
68 Ret += "-S32";
69 else
70 Ret += "-S128";
71
72 return Ret;
73}
74
Evan Cheng2129f592011-07-19 06:37:02 +000075X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
76 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000077 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000078 Reloc::Model RM, CodeModel::Model CM,
79 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000080 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
Rafael Espindola002f8aa2013-12-10 22:05:32 +000081 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000082 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +000083 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +000084 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +000085 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +000086 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000087}
88
David Blaikiea379b1812011-12-20 02:50:00 +000089void X86_64TargetMachine::anchor() { }
Evan Cheng11b0a5d2006-09-08 06:48:29 +000090
Evan Cheng2129f592011-07-19 06:37:02 +000091X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
92 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000093 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000094 Reloc::Model RM, CodeModel::Model CM,
95 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000096 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
Eli Bendersky597fc122013-01-25 22:07:43 +000097 // The x32 ABI dictates the ILP32 programming model for x64.
Rafael Espindola002f8aa2013-12-10 22:05:32 +000098 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000099 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +0000100 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +0000101 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +0000102 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000103 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000104}
105
Chris Lattner7e3abf12009-07-09 03:32:31 +0000106/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000107///
Evan Cheng2129f592011-07-19 06:37:02 +0000108X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
109 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000110 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000111 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000112 CodeGenOpt::Level OL,
Evan Chengefd9b422011-07-20 07:51:56 +0000113 bool is64Bit)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000114 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
115 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000116 FrameLowering(*this, Subtarget),
Andrew Trick8523b162012-02-01 23:20:51 +0000117 InstrItins(Subtarget.getInstrItineraryData()){
Chris Lattner7e3abf12009-07-09 03:32:31 +0000118 // Determine the PICStyle based on the target selected.
119 if (getRelocationModel() == Reloc::Static) {
120 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
121 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000122 } else if (Subtarget.is64Bit()) {
123 // PIC in 64 bit mode is always rip-rel.
124 Subtarget.setPICStyle(PICStyles::RIPRel);
NAKAMURA Takumide8880a2013-08-21 02:37:25 +0000125 } else if (Subtarget.isTargetCOFF()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +0000126 Subtarget.setPICStyle(PICStyles::None);
127 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000128 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +0000129 Subtarget.setPICStyle(PICStyles::StubPIC);
130 else {
131 assert(getRelocationModel() == Reloc::DynamicNoPIC);
132 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
133 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000134 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000135 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000136 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000137
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000138 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000139 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000140 this->Options.FloatABIType = FloatABI::Hard;
Chris Lattnera1d312c2006-02-03 18:59:39 +0000141}
Chris Lattner02a3d832002-10-29 22:37:54 +0000142
Chris Lattner12e97302006-09-04 04:14:57 +0000143//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000144// Command line options for x86
145//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000146static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000147UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000148 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000149 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000150
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000151// Temporary option to control early if-conversion for x86 while adding machine
152// models.
153static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000154X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000155 cl::desc("Enable early if-conversion on X86"));
156
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000157//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +0000158// X86 Analysis Pass Setup
159//===----------------------------------------------------------------------===//
160
161void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
162 // Add first the target-independent BasicTTI pass, then our X86 pass. This
163 // allows the X86 pass to delegate to the target independent layer when
164 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +0000165 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +0000166 PM.add(createX86TargetTransformInfoPass(this));
167}
168
169
170//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000171// Pass Pipeline Configuration
172//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000173
Andrew Trickccb67362012-02-03 05:12:41 +0000174namespace {
175/// X86 Code Generator Pass Configuration Options.
176class X86PassConfig : public TargetPassConfig {
177public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000178 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
179 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000180
181 X86TargetMachine &getX86TargetMachine() const {
182 return getTM<X86TargetMachine>();
183 }
184
185 const X86Subtarget &getX86Subtarget() const {
186 return *getX86TargetMachine().getSubtargetImpl();
187 }
188
189 virtual bool addInstSelector();
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000190 virtual bool addILPOpts();
Andrew Trickccb67362012-02-03 05:12:41 +0000191 virtual bool addPreRegAlloc();
192 virtual bool addPostRegAlloc();
193 virtual bool addPreEmitPass();
194};
195} // namespace
196
Andrew Trickf8ea1082012-02-04 02:56:59 +0000197TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000198 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000199}
200
201bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000202 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000203 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000204
Hans Wennborg789acfb2012-06-01 16:27:21 +0000205 // For ELF, cleanup any local-dynamic TLS accesses.
206 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000207 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000208
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000209 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
Andrew Trickccb67362012-02-03 05:12:41 +0000210 if (!getX86Subtarget().is64Bit())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000211 addPass(createGlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000212
Chris Lattner12e97302006-09-04 04:14:57 +0000213 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000214}
215
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000216bool X86PassConfig::addILPOpts() {
217 if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
218 addPass(&EarlyIfConverterID);
219 return true;
220 }
221 return false;
222}
223
Andrew Trickccb67362012-02-03 05:12:41 +0000224bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000225 return false; // -print-machineinstr shouldn't print after this.
226}
227
Andrew Trickccb67362012-02-03 05:12:41 +0000228bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000229 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000230 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000231}
232
Andrew Trickccb67362012-02-03 05:12:41 +0000233bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000234 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000235 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000236 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000237 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000238 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000239
Andrew Trickccb67362012-02-03 05:12:41 +0000240 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000241 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000242 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000243 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000244
Preston Gurda01daac2013-01-08 18:27:24 +0000245 if (getOptLevel() != CodeGenOpt::None &&
246 getX86Subtarget().padShortFunctions()) {
247 addPass(createX86PadShortFunctions());
248 ShouldPrint = true;
249 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000250 if (getOptLevel() != CodeGenOpt::None &&
251 getX86Subtarget().LEAusesAG()){
252 addPass(createX86FixupLEAs());
253 ShouldPrint = true;
254 }
Preston Gurda01daac2013-01-08 18:27:24 +0000255
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000256 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000257}
258
Bill Wendling026e5d72009-04-29 23:29:43 +0000259bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000260 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000261 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000262
263 return false;
264}