blob: d5e7eac37c480f71afa618c7dea014db852ab326 [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
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000027extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000028 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000029 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
30 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000031}
Douglas Gregor1b731d52009-06-16 20:12:29 +000032
Aditya Nandakumara2719322014-11-13 09:26:31 +000033static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
34 if (TT.isOSBinFormatMachO()) {
35 if (TT.getArch() == Triple::x86_64)
36 return make_unique<X86_64MachoTargetObjectFile>();
37 return make_unique<TargetLoweringObjectFileMachO>();
38 }
39
Derek Schuff072f93f2015-03-11 16:16:09 +000040 if (TT.isOSLinux() || TT.isOSNaCl())
41 return make_unique<X86LinuxNaClTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000042 if (TT.isOSBinFormatELF())
Paul Robinson06a8eb82015-03-03 21:01:27 +000043 return make_unique<X86ELFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000044 if (TT.isKnownWindowsMSVCEnvironment())
45 return make_unique<X86WindowsTargetObjectFile>();
46 if (TT.isOSBinFormatCOFF())
47 return make_unique<TargetLoweringObjectFileCOFF>();
48 llvm_unreachable("unknown subtarget type");
49}
50
Eric Christopher8b770652015-01-26 19:03:15 +000051static std::string computeDataLayout(const Triple &TT) {
52 // X86 is little endian
53 std::string Ret = "e";
54
55 Ret += DataLayout::getManglingComponent(TT);
56 // X86 and x32 have 32 bit pointers.
57 if ((TT.isArch64Bit() &&
58 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
59 !TT.isArch64Bit())
60 Ret += "-p:32:32";
61
62 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
63 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
64 Ret += "-i64:64";
65 else
66 Ret += "-f64:32:64";
67
68 // Some ABIs align long double to 128 bits, others to 32.
69 if (TT.isOSNaCl())
70 ; // No f80
71 else if (TT.isArch64Bit() || TT.isOSDarwin())
72 Ret += "-f80:128";
73 else
74 Ret += "-f80:32";
75
76 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
77 if (TT.isArch64Bit())
78 Ret += "-n8:16:32:64";
79 else
80 Ret += "-n8:16:32";
81
82 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
83 if (!TT.isArch64Bit() && TT.isOSWindows())
Reid Kleckner60d52322015-04-30 22:11:59 +000084 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +000085 else
86 Ret += "-S128";
87
88 return Ret;
89}
90
Chris Lattner7e3abf12009-07-09 03:32:31 +000091/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000092///
Eric Christopher66f676e2014-06-05 22:10:58 +000093X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
94 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000095 Reloc::Model RM, CodeModel::Model CM,
David Woodhouse1c3996a2014-01-08 00:08:50 +000096 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000097 : LLVMTargetMachine(T, computeDataLayout(Triple(TT)), TT, CPU, FS, Options,
98 RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +000099 TLOF(createTLOF(Triple(getTargetTriple()))),
Eric Christophera08f30b2014-06-09 17:08:19 +0000100 Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000101 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000102 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000103 this->Options.FloatABIType = FloatABI::Hard;
David Woodhouse1c3996a2014-01-08 00:08:50 +0000104
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
David Woodhouse1c3996a2014-01-08 00:08:50 +0000112 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000113}
Chris Lattner02a3d832002-10-29 22:37:54 +0000114
Reid Kleckner357600e2014-11-20 23:37:18 +0000115X86TargetMachine::~X86TargetMachine() {}
116
Eric Christopher3faf2f12014-10-06 06:45:36 +0000117const X86Subtarget *
118X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000119 Attribute CPUAttr = F.getFnAttribute("target-cpu");
120 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000121
122 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
123 ? CPUAttr.getValueAsString().str()
124 : TargetCPU;
125 std::string FS = !FSAttr.hasAttribute(Attribute::None)
126 ? FSAttr.getValueAsString().str()
127 : TargetFS;
128
129 // FIXME: This is related to the code below to reset the target options,
130 // we need to know whether or not the soft float flag is set on the
131 // function before we can generate a subtarget. We also need to use
132 // it as a key for the subtarget since that can be the only difference
133 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000134 bool SoftFloat =
135 F.hasFnAttribute("use-soft-float") &&
136 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
137 // If the soft float attribute is set on the function turn on the soft float
138 // subtarget feature.
139 if (SoftFloat)
140 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000141
Eric Christopher824f42f2015-05-12 01:26:05 +0000142 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000143 if (!I) {
144 // This needs to be done before we create a new subtarget since any
145 // creation will depend on the TM and the code generation flags on the
146 // function that reside in TargetOptions.
147 resetTargetOptions(F);
148 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
149 Options.StackAlignmentOverride);
150 }
151 return I.get();
152}
153
Chris Lattner12e97302006-09-04 04:14:57 +0000154//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000155// Command line options for x86
156//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000157static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000158UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000159 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000160 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000161
162//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000163// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000164//===----------------------------------------------------------------------===//
165
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000166TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
167 return TargetIRAnalysis(
168 [this](Function &F) { return TargetTransformInfo(X86TTIImpl(this, F)); });
Chandler Carruth664e3542013-01-07 01:37:14 +0000169}
170
171
172//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000173// Pass Pipeline Configuration
174//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000175
Andrew Trickccb67362012-02-03 05:12:41 +0000176namespace {
177/// X86 Code Generator Pass Configuration Options.
178class X86PassConfig : public TargetPassConfig {
179public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000180 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
181 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000182
183 X86TargetMachine &getX86TargetMachine() const {
184 return getTM<X86TargetMachine>();
185 }
186
Tim Northover277066a2014-07-01 18:53:31 +0000187 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000188 bool addInstSelector() override;
189 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000190 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000191 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000192 void addPostRegAlloc() override;
193 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000194};
195} // namespace
196
Andrew Trickf8ea1082012-02-04 02:56:59 +0000197TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000198 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000199}
200
Tim Northover277066a2014-07-01 18:53:31 +0000201void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000202 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000203
204 TargetPassConfig::addIRPasses();
205}
206
Andrew Trickccb67362012-02-03 05:12:41 +0000207bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000208 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000209 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000210
Hans Wennborg789acfb2012-06-01 16:27:21 +0000211 // For ELF, cleanup any local-dynamic TLS accesses.
Eric Christopher24f3f652015-02-05 19:27:04 +0000212 if (Triple(TM->getTargetTriple()).isOSBinFormatELF() &&
213 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000214 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000215
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000216 addPass(createX86GlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000217
Chris Lattner12e97302006-09-04 04:14:57 +0000218 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000219}
220
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000221bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000222 addPass(&EarlyIfConverterID);
223 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000224}
225
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000226bool X86PassConfig::addPreISel() {
227 // Only add this pass for 32-bit x86.
228 Triple TT(TM->getTargetTriple());
229 if (TT.getArch() == Triple::x86)
230 addPass(createX86WinEHStatePass());
231 return true;
232}
233
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000234void X86PassConfig::addPreRegAlloc() {
235 addPass(createX86CallFrameOptimization());
236}
237
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000238void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000239 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000240}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000241
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000242void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000243 if (getOptLevel() != CodeGenOpt::None)
Matthias Braunb2f23882014-12-11 23:18:03 +0000244 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000245
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000246 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000247 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000248
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000249 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000250 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000251 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000252 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000253}