blob: cb5ab0dd50200e337a8c3531c0a7882829f839b6 [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 Lattner962d5be2003-01-13 00:51:23 +000016#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/PassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000018#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000019#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000020#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000024extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000025 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000026 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
27 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000028}
Douglas Gregor1b731d52009-06-16 20:12:29 +000029
David Woodhouse1c3996a2014-01-08 00:08:50 +000030void X86TargetMachine::anchor() { }
Jim Laskeyae92ce82006-09-07 23:39:26 +000031
Rafael Espindola002f8aa2013-12-10 22:05:32 +000032static std::string computeDataLayout(const X86Subtarget &ST) {
33 // X86 is little endian
34 std::string Ret = "e";
35
Rafael Espindola58873562014-01-03 19:21:54 +000036 Ret += DataLayout::getManglingComponent(ST.getTargetTriple());
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
Rafael Espindola002f8aa2013-12-10 22:05:32 +000041 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
Yaron Keren136fe7d2014-04-01 18:15:34 +000042 if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC() ||
Rafael Espindoladdb913c2013-12-19 00:44:37 +000043 ST.isTargetNaCl())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000044 Ret += "-i64:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000045 else
Rafael Espindola1caa6932013-12-13 17:56:11 +000046 Ret += "-f64:32:64";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000047
48 // Some ABIs align long double to 128 bits, others to 32.
Rafael Espindoladdb913c2013-12-19 00:44:37 +000049 if (ST.isTargetNaCl())
50 ; // No f80
51 else if (ST.is64Bit() || ST.isTargetDarwin())
Rafael Espindolabccb9d42013-12-16 18:01:51 +000052 Ret += "-f80:128";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000053 else
Rafael Espindolabccb9d42013-12-16 18:01:51 +000054 Ret += "-f80:32";
Rafael Espindola002f8aa2013-12-10 22:05:32 +000055
Rafael Espindola002f8aa2013-12-10 22:05:32 +000056 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
57 if (ST.is64Bit())
58 Ret += "-n8:16:32:64";
59 else
60 Ret += "-n8:16:32";
61
62 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Yaron Keren136fe7d2014-04-01 18:15:34 +000063 if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetKnownWindowsMSVC()))
Rafael Espindola002f8aa2013-12-10 22:05:32 +000064 Ret += "-S32";
65 else
66 Ret += "-S128";
67
68 return Ret;
69}
70
Chris Lattner7e3abf12009-07-09 03:32:31 +000071/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000072///
Evan Cheng2129f592011-07-19 06:37:02 +000073X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
74 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000075 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000076 Reloc::Model RM, CodeModel::Model CM,
David Woodhouse1c3996a2014-01-08 00:08:50 +000077 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000078 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
David Woodhouse1c3996a2014-01-08 00:08:50 +000079 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride),
Anton Korobeynikov2f931282011-01-10 12:39:04 +000080 FrameLowering(*this, Subtarget),
David Woodhouse1c3996a2014-01-08 00:08:50 +000081 InstrItins(Subtarget.getInstrItineraryData()),
82 DL(computeDataLayout(*getSubtargetImpl())),
83 InstrInfo(*this),
84 TLInfo(*this),
85 TSInfo(*this),
86 JITInfo(*this) {
Chris Lattner7e3abf12009-07-09 03:32:31 +000087 // Determine the PICStyle based on the target selected.
88 if (getRelocationModel() == Reloc::Static) {
89 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
90 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000091 } else if (Subtarget.is64Bit()) {
92 // PIC in 64 bit mode is always rip-rel.
93 Subtarget.setPICStyle(PICStyles::RIPRel);
NAKAMURA Takumide8880a2013-08-21 02:37:25 +000094 } else if (Subtarget.isTargetCOFF()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +000095 Subtarget.setPICStyle(PICStyles::None);
96 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000097 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +000098 Subtarget.setPICStyle(PICStyles::StubPIC);
99 else {
100 assert(getRelocationModel() == Reloc::DynamicNoPIC);
101 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
102 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000103 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000104 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000105 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000106
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000107 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000108 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000109 this->Options.FloatABIType = FloatABI::Hard;
David Woodhouse1c3996a2014-01-08 00:08:50 +0000110
Yaron Kerend7ba46b2014-04-19 13:47:43 +0000111 // Windows stack unwinder gets confused when execution flow "falls through"
112 // after a call to 'noreturn' function.
113 // To prevent that, we emit a trap for 'unreachable' IR instructions.
114 // (which on X86, happens to be the 'ud2' instruction)
115 if (Subtarget.isTargetWin64())
116 this->Options.TrapUnreachable = true;
117
David Woodhouse1c3996a2014-01-08 00:08:50 +0000118 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000119}
Chris Lattner02a3d832002-10-29 22:37:54 +0000120
Chris Lattner12e97302006-09-04 04:14:57 +0000121//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000122// Command line options for x86
123//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000124static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000125UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000126 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000127 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000128
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000129// Temporary option to control early if-conversion for x86 while adding machine
130// models.
131static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000132X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000133 cl::desc("Enable early if-conversion on X86"));
134
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000135//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +0000136// X86 Analysis Pass Setup
137//===----------------------------------------------------------------------===//
138
139void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
140 // Add first the target-independent BasicTTI pass, then our X86 pass. This
141 // allows the X86 pass to delegate to the target independent layer when
142 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +0000143 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +0000144 PM.add(createX86TargetTransformInfoPass(this));
145}
146
147
148//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000149// Pass Pipeline Configuration
150//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000151
Andrew Trickccb67362012-02-03 05:12:41 +0000152namespace {
153/// X86 Code Generator Pass Configuration Options.
154class X86PassConfig : public TargetPassConfig {
155public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000156 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
157 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000158
159 X86TargetMachine &getX86TargetMachine() const {
160 return getTM<X86TargetMachine>();
161 }
162
163 const X86Subtarget &getX86Subtarget() const {
164 return *getX86TargetMachine().getSubtargetImpl();
165 }
166
Craig Topper2d9361e2014-03-09 07:44:38 +0000167 bool addInstSelector() override;
168 bool addILPOpts() override;
169 bool addPreRegAlloc() override;
170 bool addPostRegAlloc() override;
171 bool addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000172};
173} // namespace
174
Andrew Trickf8ea1082012-02-04 02:56:59 +0000175TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000176 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000177}
178
179bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000180 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000181 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000182
Hans Wennborg789acfb2012-06-01 16:27:21 +0000183 // For ELF, cleanup any local-dynamic TLS accesses.
184 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000185 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000186
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000187 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
Andrew Trickccb67362012-02-03 05:12:41 +0000188 if (!getX86Subtarget().is64Bit())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000189 addPass(createGlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000190
Chris Lattner12e97302006-09-04 04:14:57 +0000191 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000192}
193
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000194bool X86PassConfig::addILPOpts() {
195 if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
196 addPass(&EarlyIfConverterID);
197 return true;
198 }
199 return false;
200}
201
Andrew Trickccb67362012-02-03 05:12:41 +0000202bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000203 return false; // -print-machineinstr shouldn't print after this.
204}
205
Andrew Trickccb67362012-02-03 05:12:41 +0000206bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000207 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000208 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000209}
210
Andrew Trickccb67362012-02-03 05:12:41 +0000211bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000212 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000213 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000214 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000215 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000216 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000217
Andrew Trickccb67362012-02-03 05:12:41 +0000218 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000219 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000220 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000221 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000222
Preston Gurda01daac2013-01-08 18:27:24 +0000223 if (getOptLevel() != CodeGenOpt::None &&
224 getX86Subtarget().padShortFunctions()) {
225 addPass(createX86PadShortFunctions());
226 ShouldPrint = true;
227 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000228 if (getOptLevel() != CodeGenOpt::None &&
229 getX86Subtarget().LEAusesAG()){
230 addPass(createX86FixupLEAs());
231 ShouldPrint = true;
232 }
Preston Gurda01daac2013-01-08 18:27:24 +0000233
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000234 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000235}
236
Bill Wendling026e5d72009-04-29 23:29:43 +0000237bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000238 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000239 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000240
241 return false;
242}