blob: 9fa2481a25eea37be4923750f18ea19cf8f1b1b2 [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 Espindola58873562014-01-03 19:21:54 +000037 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
Rafael Espindola8afbb282013-12-16 17:15:29 +000038 // X86 and x32 have 32 bit pointers.
Rafael Espindola002f8aa2013-12-10 22:05:32 +000039 if (ST.isTarget64BitILP32() || !ST.is64Bit())
40 Ret += "-p:32:32";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000041
Rafael Espindola002f8aa2013-12-10 22:05:32 +000042 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
Rafael Espindoladdb913c2013-12-19 00:44:37 +000043 if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetWindows() ||
44 ST.isTargetNaCl())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000045 Ret += "-i64:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000046 else
Rafael Espindola1caa6932013-12-13 17:56:11 +000047 Ret += "-f64:32:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000048
49 // Some ABIs align long double to 128 bits, others to 32.
Rafael Espindoladdb913c2013-12-19 00:44:37 +000050 if (ST.isTargetNaCl())
51 ; // No f80
52 else if (ST.is64Bit() || ST.isTargetDarwin())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000053 Ret += "-f80:128";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000054 else
Rafael Espindolabccb9d42013-12-16 18:01:51 +000055 Ret += "-f80:32";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000056
Rafael Espindola002f8aa2013-12-10 22:05:32 +000057 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
58 if (ST.is64Bit())
59 Ret += "-n8:16:32:64";
60 else
61 Ret += "-n8:16:32";
62
63 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
64 if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetWindows()))
65 Ret += "-S32";
66 else
67 Ret += "-S128";
68
69 return Ret;
70}
71
Evan Cheng2129f592011-07-19 06:37:02 +000072X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
73 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000074 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000075 Reloc::Model RM, CodeModel::Model CM,
76 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000077 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
Rafael Espindola002f8aa2013-12-10 22:05:32 +000078 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000079 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +000080 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +000081 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +000082 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +000083 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000084}
85
David Blaikiea379b1812011-12-20 02:50:00 +000086void X86_64TargetMachine::anchor() { }
Evan Cheng11b0a5d2006-09-08 06:48:29 +000087
Evan Cheng2129f592011-07-19 06:37:02 +000088X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
89 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000090 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000091 Reloc::Model RM, CodeModel::Model CM,
92 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000093 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
Eli Bendersky597fc122013-01-25 22:07:43 +000094 // The x32 ABI dictates the ILP32 programming model for x64.
Rafael Espindola002f8aa2013-12-10 22:05:32 +000095 DL(computeDataLayout(*getSubtargetImpl())),
Rafael Espindola66e08d42010-10-03 18:59:45 +000096 InstrInfo(*this),
Rafael Espindola66e08d42010-10-03 18:59:45 +000097 TLInfo(*this),
Richard Smith15b1e372012-12-20 04:04:17 +000098 TSInfo(*this),
Chandler Carruth664e3542013-01-07 01:37:14 +000099 JITInfo(*this) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000100 initAsmInfo();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000101}
102
Chris Lattner7e3abf12009-07-09 03:32:31 +0000103/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000104///
Evan Cheng2129f592011-07-19 06:37:02 +0000105X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
106 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000107 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +0000108 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +0000109 CodeGenOpt::Level OL,
Evan Chengefd9b422011-07-20 07:51:56 +0000110 bool is64Bit)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000111 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
112 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000113 FrameLowering(*this, Subtarget),
Andrew Trick8523b162012-02-01 23:20:51 +0000114 InstrItins(Subtarget.getInstrItineraryData()){
Chris Lattner7e3abf12009-07-09 03:32:31 +0000115 // Determine the PICStyle based on the target selected.
116 if (getRelocationModel() == Reloc::Static) {
117 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
118 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000119 } else if (Subtarget.is64Bit()) {
120 // PIC in 64 bit mode is always rip-rel.
121 Subtarget.setPICStyle(PICStyles::RIPRel);
NAKAMURA Takumide8880a2013-08-21 02:37:25 +0000122 } else if (Subtarget.isTargetCOFF()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +0000123 Subtarget.setPICStyle(PICStyles::None);
124 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000125 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +0000126 Subtarget.setPICStyle(PICStyles::StubPIC);
127 else {
128 assert(getRelocationModel() == Reloc::DynamicNoPIC);
129 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
130 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000131 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000132 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000133 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000134
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000135 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000136 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000137 this->Options.FloatABIType = FloatABI::Hard;
Chris Lattnera1d312c2006-02-03 18:59:39 +0000138}
Chris Lattner02a3d832002-10-29 22:37:54 +0000139
Chris Lattner12e97302006-09-04 04:14:57 +0000140//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000141// Command line options for x86
142//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000143static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000144UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000145 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000146 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000147
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000148// Temporary option to control early if-conversion for x86 while adding machine
149// models.
150static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000151X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000152 cl::desc("Enable early if-conversion on X86"));
153
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000154//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +0000155// X86 Analysis Pass Setup
156//===----------------------------------------------------------------------===//
157
158void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
159 // Add first the target-independent BasicTTI pass, then our X86 pass. This
160 // allows the X86 pass to delegate to the target independent layer when
161 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +0000162 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +0000163 PM.add(createX86TargetTransformInfoPass(this));
164}
165
166
167//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000168// Pass Pipeline Configuration
169//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000170
Andrew Trickccb67362012-02-03 05:12:41 +0000171namespace {
172/// X86 Code Generator Pass Configuration Options.
173class X86PassConfig : public TargetPassConfig {
174public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000175 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
176 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000177
178 X86TargetMachine &getX86TargetMachine() const {
179 return getTM<X86TargetMachine>();
180 }
181
182 const X86Subtarget &getX86Subtarget() const {
183 return *getX86TargetMachine().getSubtargetImpl();
184 }
185
186 virtual bool addInstSelector();
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000187 virtual bool addILPOpts();
Andrew Trickccb67362012-02-03 05:12:41 +0000188 virtual bool addPreRegAlloc();
189 virtual bool addPostRegAlloc();
190 virtual bool addPreEmitPass();
191};
192} // namespace
193
Andrew Trickf8ea1082012-02-04 02:56:59 +0000194TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000195 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000196}
197
198bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000199 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000200 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000201
Hans Wennborg789acfb2012-06-01 16:27:21 +0000202 // For ELF, cleanup any local-dynamic TLS accesses.
203 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000204 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000205
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000206 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
Andrew Trickccb67362012-02-03 05:12:41 +0000207 if (!getX86Subtarget().is64Bit())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000208 addPass(createGlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000209
Chris Lattner12e97302006-09-04 04:14:57 +0000210 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000211}
212
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000213bool X86PassConfig::addILPOpts() {
214 if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
215 addPass(&EarlyIfConverterID);
216 return true;
217 }
218 return false;
219}
220
Andrew Trickccb67362012-02-03 05:12:41 +0000221bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000222 return false; // -print-machineinstr shouldn't print after this.
223}
224
Andrew Trickccb67362012-02-03 05:12:41 +0000225bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000226 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000227 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000228}
229
Andrew Trickccb67362012-02-03 05:12:41 +0000230bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000231 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000232 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000233 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000234 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000235 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000236
Andrew Trickccb67362012-02-03 05:12:41 +0000237 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000238 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000239 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000240 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000241
Preston Gurda01daac2013-01-08 18:27:24 +0000242 if (getOptLevel() != CodeGenOpt::None &&
243 getX86Subtarget().padShortFunctions()) {
244 addPass(createX86PadShortFunctions());
245 ShouldPrint = true;
246 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000247 if (getOptLevel() != CodeGenOpt::None &&
248 getX86Subtarget().LEAusesAG()){
249 addPass(createX86FixupLEAs());
250 ShouldPrint = true;
251 }
Preston Gurda01daac2013-01-08 18:27:24 +0000252
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000253 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000254}
255
Bill Wendling026e5d72009-04-29 23:29:43 +0000256bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000257 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000258 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000259
260 return false;
261}