blob: 445adbd4349b65f9100dfaaf90c6e2b67779ba59 [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
Derek Schuff072f93f2015-03-11 16:16:09 +000053 if (TT.isOSLinux() || TT.isOSNaCl())
54 return make_unique<X86LinuxNaClTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000055 if (TT.isOSBinFormatELF())
Paul Robinson06a8eb82015-03-03 21:01:27 +000056 return make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000057 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Aditya Nandakumara2719322014-11-13 09:26:31 +000058 return make_unique<X86WindowsTargetObjectFile>();
59 if (TT.isOSBinFormatCOFF())
60 return make_unique<TargetLoweringObjectFileCOFF>();
61 llvm_unreachable("unknown subtarget type");
62}
63
Eric Christopher8b770652015-01-26 19:03:15 +000064static std::string computeDataLayout(const Triple &TT) {
65 // X86 is little endian
66 std::string Ret = "e";
67
68 Ret += DataLayout::getManglingComponent(TT);
69 // X86 and x32 have 32 bit pointers.
70 if ((TT.isArch64Bit() &&
71 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
72 !TT.isArch64Bit())
73 Ret += "-p:32:32";
74
75 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
76 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
77 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +000078 else if (TT.isOSIAMCU())
79 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +000080 else
81 Ret += "-f64:32:64";
82
83 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +000084 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +000085 ; // No f80
86 else if (TT.isArch64Bit() || TT.isOSDarwin())
87 Ret += "-f80:128";
88 else
89 Ret += "-f80:32";
90
Andrey Turetskiy2396c382016-02-10 11:57:06 +000091 if (TT.isOSIAMCU())
92 Ret += "-f128:32";
93
Eric Christopher8b770652015-01-26 19:03:15 +000094 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
95 if (TT.isArch64Bit())
96 Ret += "-n8:16:32:64";
97 else
98 Ret += "-n8:16:32";
99
100 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000101 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000102 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000103 else
104 Ret += "-S128";
105
106 return Ret;
107}
108
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000109static Reloc::Model getEffectiveRelocModel(const Triple &TT,
110 Optional<Reloc::Model> RM) {
111 bool is64Bit = TT.getArch() == Triple::x86_64;
112 if (!RM.hasValue()) {
113 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
114 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
115 // use static relocation model by default.
116 if (TT.isOSDarwin()) {
117 if (is64Bit)
118 return Reloc::PIC_;
119 return Reloc::DynamicNoPIC;
120 }
121 if (TT.isOSWindows() && is64Bit)
122 return Reloc::PIC_;
123 return Reloc::Static;
124 }
125
126 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
127 // is defined as a model for code which may be used in static or dynamic
128 // executables but not necessarily a shared library. On X86-32 we just
129 // compile in -static mode, in x86-64 we use PIC.
130 if (*RM == Reloc::DynamicNoPIC) {
131 if (is64Bit)
132 return Reloc::PIC_;
133 if (!TT.isOSDarwin())
134 return Reloc::Static;
135 }
136
137 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
138 // the Mach-O file format doesn't support it.
139 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
140 return Reloc::PIC_;
141
142 return *RM;
143}
144
Rafael Espindola38af4d62016-05-18 16:00:24 +0000145/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000146///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000147X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
148 StringRef CPU, StringRef FS,
149 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000150 Optional<Reloc::Model> RM,
151 CodeModel::Model CM, CodeGenOpt::Level OL)
152 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
153 getEffectiveRelocModel(TT, RM), CM, OL),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000154 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000155 Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000156 // Windows stack unwinder gets confused when execution flow "falls through"
157 // after a call to 'noreturn' function.
158 // To prevent that, we emit a trap for 'unreachable' IR instructions.
159 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000160 // On PS4, the "return address" of a 'noreturn' call must still be within
161 // the calling function, and TrapUnreachable is an easy way to get that.
162 if (Subtarget.isTargetWin64() || Subtarget.isTargetPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000163 this->Options.TrapUnreachable = true;
164
Sanjay Patel09b2c892015-06-22 18:29:44 +0000165 // By default (and when -ffast-math is on), enable estimate codegen for
166 // everything except scalar division. By default, use 1 refinement step for
167 // all operations. Defaults may be overridden by using command-line options.
168 // Scalar division estimates are disabled because they break too much
169 // real-world code. These defaults match GCC behavior.
170 this->Options.Reciprocals.setDefaults("sqrtf", true, 1);
171 this->Options.Reciprocals.setDefaults("divf", false, 1);
172 this->Options.Reciprocals.setDefaults("vec-sqrtf", true, 1);
173 this->Options.Reciprocals.setDefaults("vec-divf", true, 1);
Sanjay Patel667a7e22015-06-04 01:32:35 +0000174
David Woodhouse1c3996a2014-01-08 00:08:50 +0000175 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000176}
Chris Lattner02a3d832002-10-29 22:37:54 +0000177
Reid Kleckner357600e2014-11-20 23:37:18 +0000178X86TargetMachine::~X86TargetMachine() {}
179
Eric Christopher3faf2f12014-10-06 06:45:36 +0000180const X86Subtarget *
181X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000182 Attribute CPUAttr = F.getFnAttribute("target-cpu");
183 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000184
David Majnemer498f2fd2016-05-20 20:41:24 +0000185 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
186 ? CPUAttr.getValueAsString()
187 : (StringRef)TargetCPU;
188 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
189 ? FSAttr.getValueAsString()
190 : (StringRef)TargetFS;
191
192 SmallString<512> Key;
193 Key.reserve(CPU.size() + FS.size());
194 Key += CPU;
195 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000196
197 // FIXME: This is related to the code below to reset the target options,
198 // we need to know whether or not the soft float flag is set on the
199 // function before we can generate a subtarget. We also need to use
200 // it as a key for the subtarget since that can be the only difference
201 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000202 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000203 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
204 // If the soft float attribute is set on the function turn on the soft float
205 // subtarget feature.
206 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000207 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000208
David Majnemer498f2fd2016-05-20 20:41:24 +0000209 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000210
211 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000212 if (!I) {
213 // This needs to be done before we create a new subtarget since any
214 // creation will depend on the TM and the code generation flags on the
215 // function that reside in TargetOptions.
216 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000217 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Eric Christopher3faf2f12014-10-06 06:45:36 +0000218 Options.StackAlignmentOverride);
219 }
220 return I.get();
221}
222
Chris Lattner12e97302006-09-04 04:14:57 +0000223//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000224// Command line options for x86
225//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000226static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000227UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000228 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000229 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000230
231//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000232// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000233//===----------------------------------------------------------------------===//
234
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000235TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000236 return TargetIRAnalysis([this](const Function &F) {
237 return TargetTransformInfo(X86TTIImpl(this, F));
238 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000239}
240
241
242//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000243// Pass Pipeline Configuration
244//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000245
Andrew Trickccb67362012-02-03 05:12:41 +0000246namespace {
247/// X86 Code Generator Pass Configuration Options.
248class X86PassConfig : public TargetPassConfig {
249public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000250 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
251 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000252
253 X86TargetMachine &getX86TargetMachine() const {
254 return getTM<X86TargetMachine>();
255 }
256
Tim Northover277066a2014-07-01 18:53:31 +0000257 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000258 bool addInstSelector() override;
259 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000260 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000261 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000262 void addPostRegAlloc() override;
263 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000264 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000265};
266} // namespace
267
Andrew Trickf8ea1082012-02-04 02:56:59 +0000268TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000269 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000270}
271
Tim Northover277066a2014-07-01 18:53:31 +0000272void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000273 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000274
275 TargetPassConfig::addIRPasses();
276}
277
Andrew Trickccb67362012-02-03 05:12:41 +0000278bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000279 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000280 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000281
Hans Wennborg789acfb2012-06-01 16:27:21 +0000282 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000283 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000284 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000285 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000286
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000287 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000288 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000289}
290
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000291bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000292 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000293 if (EnableMachineCombinerPass)
294 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000295 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000296}
297
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000298bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000299 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000300 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000301 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000302 addPass(createX86WinEHStatePass());
303 return true;
304}
305
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000306void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000307 if (getOptLevel() != CodeGenOpt::None) {
308 addPass(createX86FixupSetCC());
309 addPass(createX86OptimizeLEAs());
310 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000311
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000312 addPass(createX86CallFrameOptimization());
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000313 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000314}
315
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000316void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000317 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000318}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000319
Quentin Colombet494eb602015-05-22 18:10:47 +0000320void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
321
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000322void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000323 if (getOptLevel() != CodeGenOpt::None)
Matthias Braunb2f23882014-12-11 23:18:03 +0000324 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000325
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000326 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000327 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000328
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000329 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000330 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000331 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000332 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000333 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000334}