blob: 5234f85f24e2557bd687e5f60f44248a157298c4 [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"
Eric Christopher3faf2f12014-10-06 06:45:36 +000019#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000021#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000022#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000023#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Sanjay Patel08829ba2015-06-10 20:32:21 +000027static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
28 cl::desc("Enable the machine combiner pass"),
29 cl::init(true), cl::Hidden);
30
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000031extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000032 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000033 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
34 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000035}
Douglas Gregor1b731d52009-06-16 20:12:29 +000036
Aditya Nandakumara2719322014-11-13 09:26:31 +000037static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
38 if (TT.isOSBinFormatMachO()) {
39 if (TT.getArch() == Triple::x86_64)
40 return make_unique<X86_64MachoTargetObjectFile>();
41 return make_unique<TargetLoweringObjectFileMachO>();
42 }
43
Derek Schuff072f93f2015-03-11 16:16:09 +000044 if (TT.isOSLinux() || TT.isOSNaCl())
45 return make_unique<X86LinuxNaClTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000046 if (TT.isOSBinFormatELF())
Paul Robinson06a8eb82015-03-03 21:01:27 +000047 return make_unique<X86ELFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000048 if (TT.isKnownWindowsMSVCEnvironment())
49 return make_unique<X86WindowsTargetObjectFile>();
50 if (TT.isOSBinFormatCOFF())
51 return make_unique<TargetLoweringObjectFileCOFF>();
52 llvm_unreachable("unknown subtarget type");
53}
54
Eric Christopher8b770652015-01-26 19:03:15 +000055static std::string computeDataLayout(const Triple &TT) {
56 // X86 is little endian
57 std::string Ret = "e";
58
59 Ret += DataLayout::getManglingComponent(TT);
60 // X86 and x32 have 32 bit pointers.
61 if ((TT.isArch64Bit() &&
62 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
63 !TT.isArch64Bit())
64 Ret += "-p:32:32";
65
66 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
67 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
68 Ret += "-i64:64";
69 else
70 Ret += "-f64:32:64";
71
72 // Some ABIs align long double to 128 bits, others to 32.
73 if (TT.isOSNaCl())
74 ; // No f80
75 else if (TT.isArch64Bit() || TT.isOSDarwin())
76 Ret += "-f80:128";
77 else
78 Ret += "-f80:32";
79
80 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
81 if (TT.isArch64Bit())
82 Ret += "-n8:16:32:64";
83 else
84 Ret += "-n8:16:32";
85
86 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
87 if (!TT.isArch64Bit() && TT.isOSWindows())
Reid Kleckner60d52322015-04-30 22:11:59 +000088 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +000089 else
90 Ret += "-S128";
91
92 return Ret;
93}
94
Chris Lattner7e3abf12009-07-09 03:32:31 +000095/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000096///
Eric Christopher66f676e2014-06-05 22:10:58 +000097X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
98 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000099 Reloc::Model RM, CodeModel::Model CM,
David Woodhouse1c3996a2014-01-08 00:08:50 +0000100 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000101 : LLVMTargetMachine(T, computeDataLayout(Triple(TT)), TT, CPU, FS, Options,
102 RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000103 TLOF(createTLOF(Triple(getTargetTriple()))),
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000104 Subtarget(Triple(TT), CPU, FS, *this, Options.StackAlignmentOverride) {
Yaron Kerend7ba46b2014-04-19 13:47:43 +0000105 // Windows stack unwinder gets confused when execution flow "falls through"
106 // after a call to 'noreturn' function.
107 // To prevent that, we emit a trap for 'unreachable' IR instructions.
108 // (which on X86, happens to be the 'ud2' instruction)
109 if (Subtarget.isTargetWin64())
110 this->Options.TrapUnreachable = true;
111
Sanjay Patel667a7e22015-06-04 01:32:35 +0000112 // TODO: By default, all reciprocal estimate operations are off because
113 // that matches the behavior before TargetRecip was added (except for btver2
114 // which used subtarget features to enable this type of codegen).
115 // We should change this to match GCC behavior where everything but
116 // scalar division estimates are turned on by default with -ffast-math.
117 this->Options.Reciprocals.setDefaults("all", false, 1);
118
David Woodhouse1c3996a2014-01-08 00:08:50 +0000119 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000120}
Chris Lattner02a3d832002-10-29 22:37:54 +0000121
Reid Kleckner357600e2014-11-20 23:37:18 +0000122X86TargetMachine::~X86TargetMachine() {}
123
Eric Christopher3faf2f12014-10-06 06:45:36 +0000124const X86Subtarget *
125X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000126 Attribute CPUAttr = F.getFnAttribute("target-cpu");
127 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000128
129 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
130 ? CPUAttr.getValueAsString().str()
131 : TargetCPU;
132 std::string FS = !FSAttr.hasAttribute(Attribute::None)
133 ? FSAttr.getValueAsString().str()
134 : TargetFS;
135
136 // FIXME: This is related to the code below to reset the target options,
137 // we need to know whether or not the soft float flag is set on the
138 // function before we can generate a subtarget. We also need to use
139 // it as a key for the subtarget since that can be the only difference
140 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000141 bool SoftFloat =
142 F.hasFnAttribute("use-soft-float") &&
143 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
144 // If the soft float attribute is set on the function turn on the soft float
145 // subtarget feature.
146 if (SoftFloat)
147 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000148
Eric Christopher824f42f2015-05-12 01:26:05 +0000149 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000150 if (!I) {
151 // This needs to be done before we create a new subtarget since any
152 // creation will depend on the TM and the code generation flags on the
153 // function that reside in TargetOptions.
154 resetTargetOptions(F);
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000155 I = llvm::make_unique<X86Subtarget>(Triple(TargetTriple), CPU, FS, *this,
Eric Christopher3faf2f12014-10-06 06:45:36 +0000156 Options.StackAlignmentOverride);
157 }
158 return I.get();
159}
160
Chris Lattner12e97302006-09-04 04:14:57 +0000161//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000162// Command line options for x86
163//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000164static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000165UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000166 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000167 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000168
169//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000170// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000171//===----------------------------------------------------------------------===//
172
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000173TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
174 return TargetIRAnalysis(
175 [this](Function &F) { return TargetTransformInfo(X86TTIImpl(this, F)); });
Chandler Carruth664e3542013-01-07 01:37:14 +0000176}
177
178
179//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000180// Pass Pipeline Configuration
181//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000182
Andrew Trickccb67362012-02-03 05:12:41 +0000183namespace {
184/// X86 Code Generator Pass Configuration Options.
185class X86PassConfig : public TargetPassConfig {
186public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000187 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
188 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000189
190 X86TargetMachine &getX86TargetMachine() const {
191 return getTM<X86TargetMachine>();
192 }
193
Tim Northover277066a2014-07-01 18:53:31 +0000194 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000195 bool addInstSelector() override;
196 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000197 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000198 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000199 void addPostRegAlloc() override;
200 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000201 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000202};
203} // namespace
204
Andrew Trickf8ea1082012-02-04 02:56:59 +0000205TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000206 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000207}
208
Tim Northover277066a2014-07-01 18:53:31 +0000209void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000210 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000211
212 TargetPassConfig::addIRPasses();
213}
214
Andrew Trickccb67362012-02-03 05:12:41 +0000215bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000216 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000217 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000218
Hans Wennborg789acfb2012-06-01 16:27:21 +0000219 // For ELF, cleanup any local-dynamic TLS accesses.
Eric Christopher24f3f652015-02-05 19:27:04 +0000220 if (Triple(TM->getTargetTriple()).isOSBinFormatELF() &&
221 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000222 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000223
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000224 addPass(createX86GlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000225
Chris Lattner12e97302006-09-04 04:14:57 +0000226 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000227}
228
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000229bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000230 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000231 if (EnableMachineCombinerPass)
232 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000233 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000234}
235
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000236bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000237 // Only add this pass for 32-bit x86 Windows.
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000238 Triple TT(TM->getTargetTriple());
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000239 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000240 addPass(createX86WinEHStatePass());
241 return true;
242}
243
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000244void X86PassConfig::addPreRegAlloc() {
245 addPass(createX86CallFrameOptimization());
246}
247
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000248void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000249 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000250}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000251
Quentin Colombet494eb602015-05-22 18:10:47 +0000252void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
253
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000254void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000255 if (getOptLevel() != CodeGenOpt::None)
Matthias Braunb2f23882014-12-11 23:18:03 +0000256 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000257
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000258 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000259 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000260
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000261 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000262 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000263 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000264 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000265}