blob: 53480eb0ba917f2191ddd14b1e4dbc2b7ae65470 [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"
Aditya Nandakumara2719322014-11-13 09:26:31 +000016#include "X86TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "X86TargetTransformInfo.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000018#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000019#include "llvm/CodeGen/TargetPassConfig.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000020#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000021#include "llvm/IR/LegacyPassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000022#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000023#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000024#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Sanjay Patel08829ba2015-06-10 20:32:21 +000028static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
29 cl::desc("Enable the machine combiner pass"),
30 cl::init(true), cl::Hidden);
31
David Majnemer0ad363e2015-08-18 19:07:12 +000032namespace llvm {
33void initializeWinEHStatePassPass(PassRegistry &);
34}
35
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000036extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000037 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000038 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
39 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
David Majnemer0ad363e2015-08-18 19:07:12 +000040
41 PassRegistry &PR = *PassRegistry::getPassRegistry();
42 initializeWinEHStatePassPass(PR);
Ahmed Bougacha068ac4a2016-05-07 01:11:10 +000043 initializeFixupBWInstPassPass(PR);
Daniel Dunbare8338102009-07-15 20:24:03 +000044}
Douglas Gregor1b731d52009-06-16 20:12:29 +000045
Aditya Nandakumara2719322014-11-13 09:26:31 +000046static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
47 if (TT.isOSBinFormatMachO()) {
48 if (TT.getArch() == Triple::x86_64)
49 return make_unique<X86_64MachoTargetObjectFile>();
50 return make_unique<TargetLoweringObjectFileMachO>();
51 }
52
Davide Italianoa9f85d62016-09-26 22:53:15 +000053 if (TT.isOSFreeBSD())
54 return make_unique<X86FreeBSDTargetObjectFile>();
Derek Schuff072f93f2015-03-11 16:16:09 +000055 if (TT.isOSLinux() || TT.isOSNaCl())
56 return make_unique<X86LinuxNaClTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000057 if (TT.isOSFuchsia())
58 return make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000059 if (TT.isOSBinFormatELF())
Paul Robinson06a8eb82015-03-03 21:01:27 +000060 return make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000061 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Aditya Nandakumara2719322014-11-13 09:26:31 +000062 return make_unique<X86WindowsTargetObjectFile>();
63 if (TT.isOSBinFormatCOFF())
64 return make_unique<TargetLoweringObjectFileCOFF>();
65 llvm_unreachable("unknown subtarget type");
66}
67
Eric Christopher8b770652015-01-26 19:03:15 +000068static std::string computeDataLayout(const Triple &TT) {
69 // X86 is little endian
70 std::string Ret = "e";
71
72 Ret += DataLayout::getManglingComponent(TT);
73 // X86 and x32 have 32 bit pointers.
74 if ((TT.isArch64Bit() &&
75 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
76 !TT.isArch64Bit())
77 Ret += "-p:32:32";
78
79 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
80 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
81 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +000082 else if (TT.isOSIAMCU())
83 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +000084 else
85 Ret += "-f64:32:64";
86
87 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +000088 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +000089 ; // No f80
90 else if (TT.isArch64Bit() || TT.isOSDarwin())
91 Ret += "-f80:128";
92 else
93 Ret += "-f80:32";
94
Andrey Turetskiy2396c382016-02-10 11:57:06 +000095 if (TT.isOSIAMCU())
96 Ret += "-f128:32";
97
Eric Christopher8b770652015-01-26 19:03:15 +000098 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
99 if (TT.isArch64Bit())
100 Ret += "-n8:16:32:64";
101 else
102 Ret += "-n8:16:32";
103
104 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000105 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000106 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000107 else
108 Ret += "-S128";
109
110 return Ret;
111}
112
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000113static Reloc::Model getEffectiveRelocModel(const Triple &TT,
114 Optional<Reloc::Model> RM) {
115 bool is64Bit = TT.getArch() == Triple::x86_64;
116 if (!RM.hasValue()) {
117 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
118 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
119 // use static relocation model by default.
120 if (TT.isOSDarwin()) {
121 if (is64Bit)
122 return Reloc::PIC_;
123 return Reloc::DynamicNoPIC;
124 }
125 if (TT.isOSWindows() && is64Bit)
126 return Reloc::PIC_;
127 return Reloc::Static;
128 }
129
130 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
131 // is defined as a model for code which may be used in static or dynamic
132 // executables but not necessarily a shared library. On X86-32 we just
133 // compile in -static mode, in x86-64 we use PIC.
134 if (*RM == Reloc::DynamicNoPIC) {
135 if (is64Bit)
136 return Reloc::PIC_;
137 if (!TT.isOSDarwin())
138 return Reloc::Static;
139 }
140
141 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
142 // the Mach-O file format doesn't support it.
143 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
144 return Reloc::PIC_;
145
146 return *RM;
147}
148
Rafael Espindola38af4d62016-05-18 16:00:24 +0000149/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000150///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000151X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
152 StringRef CPU, StringRef FS,
153 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000154 Optional<Reloc::Model> RM,
155 CodeModel::Model CM, CodeGenOpt::Level OL)
156 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
157 getEffectiveRelocModel(TT, RM), CM, OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000158 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000159 // Windows stack unwinder gets confused when execution flow "falls through"
160 // after a call to 'noreturn' function.
161 // To prevent that, we emit a trap for 'unreachable' IR instructions.
162 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000163 // On PS4, the "return address" of a 'noreturn' call must still be within
164 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000165 // The check here for 64-bit windows is a bit icky, but as we're unlikely
166 // to ever want to mix 32 and 64-bit windows code in a single module
167 // this should be fine.
168 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000169 this->Options.TrapUnreachable = true;
170
David Woodhouse1c3996a2014-01-08 00:08:50 +0000171 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000172}
Chris Lattner02a3d832002-10-29 22:37:54 +0000173
Reid Kleckner357600e2014-11-20 23:37:18 +0000174X86TargetMachine::~X86TargetMachine() {}
175
Eric Christopher3faf2f12014-10-06 06:45:36 +0000176const X86Subtarget *
177X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000178 Attribute CPUAttr = F.getFnAttribute("target-cpu");
179 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000180
David Majnemer498f2fd2016-05-20 20:41:24 +0000181 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
182 ? CPUAttr.getValueAsString()
183 : (StringRef)TargetCPU;
184 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
185 ? FSAttr.getValueAsString()
186 : (StringRef)TargetFS;
187
188 SmallString<512> Key;
189 Key.reserve(CPU.size() + FS.size());
190 Key += CPU;
191 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000192
193 // FIXME: This is related to the code below to reset the target options,
194 // we need to know whether or not the soft float flag is set on the
195 // function before we can generate a subtarget. We also need to use
196 // it as a key for the subtarget since that can be the only difference
197 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000198 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000199 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
200 // If the soft float attribute is set on the function turn on the soft float
201 // subtarget feature.
202 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000203 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000204
David Majnemer498f2fd2016-05-20 20:41:24 +0000205 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000206
207 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000208 if (!I) {
209 // This needs to be done before we create a new subtarget since any
210 // creation will depend on the TM and the code generation flags on the
211 // function that reside in TargetOptions.
212 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000213 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Eric Christopher3faf2f12014-10-06 06:45:36 +0000214 Options.StackAlignmentOverride);
215 }
216 return I.get();
217}
218
Chris Lattner12e97302006-09-04 04:14:57 +0000219//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000220// Command line options for x86
221//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000222static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000223UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000224 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000225 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000226
227//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000228// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000229//===----------------------------------------------------------------------===//
230
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000231TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000232 return TargetIRAnalysis([this](const Function &F) {
233 return TargetTransformInfo(X86TTIImpl(this, F));
234 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000235}
236
237
238//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000239// Pass Pipeline Configuration
240//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000241
Andrew Trickccb67362012-02-03 05:12:41 +0000242namespace {
243/// X86 Code Generator Pass Configuration Options.
244class X86PassConfig : public TargetPassConfig {
245public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000246 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
247 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000248
249 X86TargetMachine &getX86TargetMachine() const {
250 return getTM<X86TargetMachine>();
251 }
252
Tim Northover277066a2014-07-01 18:53:31 +0000253 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000254 bool addInstSelector() override;
255 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000256 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000257 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000258 void addPostRegAlloc() override;
259 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000260 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000261};
262} // namespace
263
Andrew Trickf8ea1082012-02-04 02:56:59 +0000264TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000265 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000266}
267
Tim Northover277066a2014-07-01 18:53:31 +0000268void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000269 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000270
271 TargetPassConfig::addIRPasses();
272}
273
Andrew Trickccb67362012-02-03 05:12:41 +0000274bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000275 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000276 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000277
Hans Wennborg789acfb2012-06-01 16:27:21 +0000278 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000279 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000280 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000281 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000282
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000283 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000284 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000285}
286
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000287bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000288 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000289 if (EnableMachineCombinerPass)
290 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000291 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000292}
293
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000294bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000295 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000296 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000297 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000298 addPass(createX86WinEHStatePass());
299 return true;
300}
301
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000302void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000303 if (getOptLevel() != CodeGenOpt::None) {
Nico Weberead8f8f2016-07-14 15:07:44 +0000304 addPass(createX86FixupSetCC());
305 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000306 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000307 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000308
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000309 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000310}
311
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000312void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000313 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000314}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000315
Quentin Colombet494eb602015-05-22 18:10:47 +0000316void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
317
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000318void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000319 if (getOptLevel() != CodeGenOpt::None)
Craig Topperf4151be2016-07-22 05:00:52 +0000320 addPass(createExecutionDependencyFixPass(&X86::VR128XRegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000321
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000322 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000323 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000324
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000325 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000326 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000327 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000328 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000329 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000330}