blob: 4675a4ec03d0e404ca93e71e97b4421485e2f1bc [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
Rafael Espindola8afbb282013-12-16 17:15:29 +000037 // X86 and x32 have 32 bit pointers.
Rafael Espindola002f8aa2013-12-10 22:05:32 +000038 if (ST.isTarget64BitILP32() || !ST.is64Bit())
39 Ret += "-p:32:32";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000040
41 // Objects on the stack ore aligned to 64 bits.
42 // FIXME: of any size?
43 if (ST.is64Bit())
44 Ret += "-s:64";
45
46 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
47 if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetWindows())
Rafael Espindola1caa6932013-12-13 17:56:11 +000048 Ret += "-i64:64:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000049 else
Rafael Espindola1caa6932013-12-13 17:56:11 +000050 Ret += "-f64:32:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000051
52 // Some ABIs align long double to 128 bits, others to 32.
53 if (ST.is64Bit() || ST.isTargetDarwin())
54 Ret += "-f80:128:128";
55 else
56 Ret += "-f80:32:32";
57
Rafael Espindola002f8aa2013-12-10 22:05:32 +000058 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
59 if (ST.is64Bit())
60 Ret += "-n8:16:32:64";
61 else
62 Ret += "-n8:16:32";
63
64 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
65 if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetWindows()))
66 Ret += "-S32";
67 else
68 Ret += "-S128";
69
70 return Ret;
71}
72
Evan Cheng2129f592011-07-19 06:37:02 +000073X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
74 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000075 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000076 Reloc::Model RM, CodeModel::Model CM,
77 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000078 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
Rafael Espindola002f8aa2013-12-10 22:05:32 +000079 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000080 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +000081 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +000082 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +000083 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +000084 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000085}
86
David Blaikiea379b1812011-12-20 02:50:00 +000087void X86_64TargetMachine::anchor() { }
Evan Cheng11b0a5d2006-09-08 06:48:29 +000088
Evan Cheng2129f592011-07-19 06:37:02 +000089X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
90 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000091 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000092 Reloc::Model RM, CodeModel::Model CM,
93 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000094 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
Eli Bendersky597fc122013-01-25 22:07:43 +000095 // The x32 ABI dictates the ILP32 programming model for x64.
Rafael Espindola002f8aa2013-12-10 22:05:32 +000096 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000097 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +000098 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +000099 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +0000100 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000101 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000102}
103
Chris Lattner7e3abf12009-07-09 03:32:31 +0000104/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000105///
Evan Cheng2129f592011-07-19 06:37:02 +0000106X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
107 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000108 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000109 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000110 CodeGenOpt::Level OL,
Evan Chengefd9b422011-07-20 07:51:56 +0000111 bool is64Bit)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000112 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
113 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000114 FrameLowering(*this, Subtarget),
Andrew Trick8523b162012-02-01 23:20:51 +0000115 InstrItins(Subtarget.getInstrItineraryData()){
Chris Lattner7e3abf12009-07-09 03:32:31 +0000116 // Determine the PICStyle based on the target selected.
117 if (getRelocationModel() == Reloc::Static) {
118 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
119 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000120 } else if (Subtarget.is64Bit()) {
121 // PIC in 64 bit mode is always rip-rel.
122 Subtarget.setPICStyle(PICStyles::RIPRel);
NAKAMURA Takumide8880a2013-08-21 02:37:25 +0000123 } else if (Subtarget.isTargetCOFF()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +0000124 Subtarget.setPICStyle(PICStyles::None);
125 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000126 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +0000127 Subtarget.setPICStyle(PICStyles::StubPIC);
128 else {
129 assert(getRelocationModel() == Reloc::DynamicNoPIC);
130 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
131 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000132 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000133 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000134 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000135
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000136 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000137 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000138 this->Options.FloatABIType = FloatABI::Hard;
Chris Lattnera1d312c2006-02-03 18:59:39 +0000139}
Chris Lattner02a3d832002-10-29 22:37:54 +0000140
Chris Lattner12e97302006-09-04 04:14:57 +0000141//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000142// Command line options for x86
143//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000144static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000145UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000146 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000147 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000148
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000149// Temporary option to control early if-conversion for x86 while adding machine
150// models.
151static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000152X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000153 cl::desc("Enable early if-conversion on X86"));
154
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000155//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +0000156// X86 Analysis Pass Setup
157//===----------------------------------------------------------------------===//
158
159void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
160 // Add first the target-independent BasicTTI pass, then our X86 pass. This
161 // allows the X86 pass to delegate to the target independent layer when
162 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +0000163 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +0000164 PM.add(createX86TargetTransformInfoPass(this));
165}
166
167
168//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000169// Pass Pipeline Configuration
170//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000171
Andrew Trickccb67362012-02-03 05:12:41 +0000172namespace {
173/// X86 Code Generator Pass Configuration Options.
174class X86PassConfig : public TargetPassConfig {
175public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000176 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
177 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000178
179 X86TargetMachine &getX86TargetMachine() const {
180 return getTM<X86TargetMachine>();
181 }
182
183 const X86Subtarget &getX86Subtarget() const {
184 return *getX86TargetMachine().getSubtargetImpl();
185 }
186
187 virtual bool addInstSelector();
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000188 virtual bool addILPOpts();
Andrew Trickccb67362012-02-03 05:12:41 +0000189 virtual bool addPreRegAlloc();
190 virtual bool addPostRegAlloc();
191 virtual bool addPreEmitPass();
192};
193} // namespace
194
Andrew Trickf8ea1082012-02-04 02:56:59 +0000195TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000196 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000197}
198
199bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000200 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000201 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000202
Hans Wennborg789acfb2012-06-01 16:27:21 +0000203 // For ELF, cleanup any local-dynamic TLS accesses.
204 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000205 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000206
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000207 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
Andrew Trickccb67362012-02-03 05:12:41 +0000208 if (!getX86Subtarget().is64Bit())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000209 addPass(createGlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000210
Chris Lattner12e97302006-09-04 04:14:57 +0000211 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000212}
213
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000214bool X86PassConfig::addILPOpts() {
215 if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
216 addPass(&EarlyIfConverterID);
217 return true;
218 }
219 return false;
220}
221
Andrew Trickccb67362012-02-03 05:12:41 +0000222bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000223 return false; // -print-machineinstr shouldn't print after this.
224}
225
Andrew Trickccb67362012-02-03 05:12:41 +0000226bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000227 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000228 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000229}
230
Andrew Trickccb67362012-02-03 05:12:41 +0000231bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000232 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000233 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000234 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000235 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000236 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000237
Andrew Trickccb67362012-02-03 05:12:41 +0000238 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000239 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000240 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000241 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000242
Preston Gurda01daac2013-01-08 18:27:24 +0000243 if (getOptLevel() != CodeGenOpt::None &&
244 getX86Subtarget().padShortFunctions()) {
245 addPass(createX86PadShortFunctions());
246 ShouldPrint = true;
247 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000248 if (getOptLevel() != CodeGenOpt::None &&
249 getX86Subtarget().LEAusesAG()){
250 addPass(createX86FixupLEAs());
251 ShouldPrint = true;
252 }
Preston Gurda01daac2013-01-08 18:27:24 +0000253
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000254 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000255}
256
Bill Wendling026e5d72009-04-29 23:29:43 +0000257bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000258 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000259 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000260
261 return false;
262}