blob: c92f9d88016dda512da4a3c5988bdfb5b51a5b4d [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>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000057 if (TT.isOSBinFormatELF())
Paul Robinson06a8eb82015-03-03 21:01:27 +000058 return make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000059 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Aditya Nandakumara2719322014-11-13 09:26:31 +000060 return make_unique<X86WindowsTargetObjectFile>();
61 if (TT.isOSBinFormatCOFF())
62 return make_unique<TargetLoweringObjectFileCOFF>();
63 llvm_unreachable("unknown subtarget type");
64}
65
Eric Christopher8b770652015-01-26 19:03:15 +000066static std::string computeDataLayout(const Triple &TT) {
67 // X86 is little endian
68 std::string Ret = "e";
69
70 Ret += DataLayout::getManglingComponent(TT);
71 // X86 and x32 have 32 bit pointers.
72 if ((TT.isArch64Bit() &&
73 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
74 !TT.isArch64Bit())
75 Ret += "-p:32:32";
76
77 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
78 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
79 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +000080 else if (TT.isOSIAMCU())
81 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +000082 else
83 Ret += "-f64:32:64";
84
85 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +000086 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +000087 ; // No f80
88 else if (TT.isArch64Bit() || TT.isOSDarwin())
89 Ret += "-f80:128";
90 else
91 Ret += "-f80:32";
92
Andrey Turetskiy2396c382016-02-10 11:57:06 +000093 if (TT.isOSIAMCU())
94 Ret += "-f128:32";
95
Eric Christopher8b770652015-01-26 19:03:15 +000096 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
97 if (TT.isArch64Bit())
98 Ret += "-n8:16:32:64";
99 else
100 Ret += "-n8:16:32";
101
102 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000103 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000104 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000105 else
106 Ret += "-S128";
107
108 return Ret;
109}
110
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000111static Reloc::Model getEffectiveRelocModel(const Triple &TT,
112 Optional<Reloc::Model> RM) {
113 bool is64Bit = TT.getArch() == Triple::x86_64;
114 if (!RM.hasValue()) {
115 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
116 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
117 // use static relocation model by default.
118 if (TT.isOSDarwin()) {
119 if (is64Bit)
120 return Reloc::PIC_;
121 return Reloc::DynamicNoPIC;
122 }
123 if (TT.isOSWindows() && is64Bit)
124 return Reloc::PIC_;
125 return Reloc::Static;
126 }
127
128 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
129 // is defined as a model for code which may be used in static or dynamic
130 // executables but not necessarily a shared library. On X86-32 we just
131 // compile in -static mode, in x86-64 we use PIC.
132 if (*RM == Reloc::DynamicNoPIC) {
133 if (is64Bit)
134 return Reloc::PIC_;
135 if (!TT.isOSDarwin())
136 return Reloc::Static;
137 }
138
139 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
140 // the Mach-O file format doesn't support it.
141 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
142 return Reloc::PIC_;
143
144 return *RM;
145}
146
Rafael Espindola38af4d62016-05-18 16:00:24 +0000147/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000148///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000149X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
150 StringRef CPU, StringRef FS,
151 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000152 Optional<Reloc::Model> RM,
153 CodeModel::Model CM, CodeGenOpt::Level OL)
154 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
155 getEffectiveRelocModel(TT, RM), CM, OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000156 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000157 // Windows stack unwinder gets confused when execution flow "falls through"
158 // after a call to 'noreturn' function.
159 // To prevent that, we emit a trap for 'unreachable' IR instructions.
160 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000161 // On PS4, the "return address" of a 'noreturn' call must still be within
162 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000163 // The check here for 64-bit windows is a bit icky, but as we're unlikely
164 // to ever want to mix 32 and 64-bit windows code in a single module
165 // this should be fine.
166 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000167 this->Options.TrapUnreachable = true;
168
Sanjay Patel09b2c892015-06-22 18:29:44 +0000169 // By default (and when -ffast-math is on), enable estimate codegen for
170 // everything except scalar division. By default, use 1 refinement step for
171 // all operations. Defaults may be overridden by using command-line options.
172 // Scalar division estimates are disabled because they break too much
173 // real-world code. These defaults match GCC behavior.
174 this->Options.Reciprocals.setDefaults("sqrtf", true, 1);
175 this->Options.Reciprocals.setDefaults("divf", false, 1);
176 this->Options.Reciprocals.setDefaults("vec-sqrtf", true, 1);
177 this->Options.Reciprocals.setDefaults("vec-divf", true, 1);
Sanjay Patel667a7e22015-06-04 01:32:35 +0000178
David Woodhouse1c3996a2014-01-08 00:08:50 +0000179 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000180}
Chris Lattner02a3d832002-10-29 22:37:54 +0000181
Reid Kleckner357600e2014-11-20 23:37:18 +0000182X86TargetMachine::~X86TargetMachine() {}
183
Eric Christopher3faf2f12014-10-06 06:45:36 +0000184const X86Subtarget *
185X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000186 Attribute CPUAttr = F.getFnAttribute("target-cpu");
187 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000188
David Majnemer498f2fd2016-05-20 20:41:24 +0000189 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
190 ? CPUAttr.getValueAsString()
191 : (StringRef)TargetCPU;
192 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
193 ? FSAttr.getValueAsString()
194 : (StringRef)TargetFS;
195
196 SmallString<512> Key;
197 Key.reserve(CPU.size() + FS.size());
198 Key += CPU;
199 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000200
201 // FIXME: This is related to the code below to reset the target options,
202 // we need to know whether or not the soft float flag is set on the
203 // function before we can generate a subtarget. We also need to use
204 // it as a key for the subtarget since that can be the only difference
205 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000206 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000207 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
208 // If the soft float attribute is set on the function turn on the soft float
209 // subtarget feature.
210 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000211 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000212
David Majnemer498f2fd2016-05-20 20:41:24 +0000213 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000214
215 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000216 if (!I) {
217 // This needs to be done before we create a new subtarget since any
218 // creation will depend on the TM and the code generation flags on the
219 // function that reside in TargetOptions.
220 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000221 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Eric Christopher3faf2f12014-10-06 06:45:36 +0000222 Options.StackAlignmentOverride);
223 }
224 return I.get();
225}
226
Chris Lattner12e97302006-09-04 04:14:57 +0000227//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000228// Command line options for x86
229//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000230static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000231UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000232 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000233 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000234
235//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000236// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000237//===----------------------------------------------------------------------===//
238
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000239TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000240 return TargetIRAnalysis([this](const Function &F) {
241 return TargetTransformInfo(X86TTIImpl(this, F));
242 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000243}
244
245
246//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000247// Pass Pipeline Configuration
248//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000249
Andrew Trickccb67362012-02-03 05:12:41 +0000250namespace {
251/// X86 Code Generator Pass Configuration Options.
252class X86PassConfig : public TargetPassConfig {
253public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000254 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
255 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000256
257 X86TargetMachine &getX86TargetMachine() const {
258 return getTM<X86TargetMachine>();
259 }
260
Tim Northover277066a2014-07-01 18:53:31 +0000261 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000262 bool addInstSelector() override;
263 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000264 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000265 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000266 void addPostRegAlloc() override;
267 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000268 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000269};
270} // namespace
271
Andrew Trickf8ea1082012-02-04 02:56:59 +0000272TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000273 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000274}
275
Tim Northover277066a2014-07-01 18:53:31 +0000276void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000277 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000278
279 TargetPassConfig::addIRPasses();
280}
281
Andrew Trickccb67362012-02-03 05:12:41 +0000282bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000283 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000284 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000285
Hans Wennborg789acfb2012-06-01 16:27:21 +0000286 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000287 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000288 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000289 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000290
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000291 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000292 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000293}
294
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000295bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000296 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000297 if (EnableMachineCombinerPass)
298 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000299 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000300}
301
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000302bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000303 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000304 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000305 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000306 addPass(createX86WinEHStatePass());
307 return true;
308}
309
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000310void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000311 if (getOptLevel() != CodeGenOpt::None) {
Nico Weberead8f8f2016-07-14 15:07:44 +0000312 addPass(createX86FixupSetCC());
313 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000314 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000315 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000316
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000317 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000318}
319
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000320void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000321 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000322}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000323
Quentin Colombet494eb602015-05-22 18:10:47 +0000324void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
325
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000326void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000327 if (getOptLevel() != CodeGenOpt::None)
Craig Topperf4151be2016-07-22 05:00:52 +0000328 addPass(createExecutionDependencyFixPass(&X86::VR128XRegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000329
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000330 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000331 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000332
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000333 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000334 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000335 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000336 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000337 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000338}