blob: 78fe430c7f6d2241395e338c6ccb6ff89f5c19f1 [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 Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/PassManager.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
40 if (TT.isOSLinux())
41 return make_unique<X86LinuxTargetObjectFile>();
42 if (TT.isOSBinFormatELF())
43 return make_unique<TargetLoweringObjectFileELF>();
44 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())
84 Ret += "-S32";
85 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)
Eric Christopher66f676e2014-06-05 22:10:58 +000097 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +000098 TLOF(createTLOF(Triple(getTargetTriple()))),
Eric Christopher8b770652015-01-26 19:03:15 +000099 DL(computeDataLayout(Triple(TT))),
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 {
119 AttributeSet FnAttrs = F.getAttributes();
120 Attribute CPUAttr =
121 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
122 Attribute FSAttr =
123 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
124
125 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
126 ? CPUAttr.getValueAsString().str()
127 : TargetCPU;
128 std::string FS = !FSAttr.hasAttribute(Attribute::None)
129 ? FSAttr.getValueAsString().str()
130 : TargetFS;
131
132 // FIXME: This is related to the code below to reset the target options,
133 // we need to know whether or not the soft float flag is set on the
134 // function before we can generate a subtarget. We also need to use
135 // it as a key for the subtarget since that can be the only difference
136 // between two functions.
137 Attribute SFAttr =
138 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
139 bool SoftFloat = !SFAttr.hasAttribute(Attribute::None)
140 ? SFAttr.getValueAsString() == "true"
141 : Options.UseSoftFloat;
142
143 auto &I = SubtargetMap[CPU + FS + (SoftFloat ? "use-soft-float=true"
144 : "use-soft-float=false")];
145 if (!I) {
146 // This needs to be done before we create a new subtarget since any
147 // creation will depend on the TM and the code generation flags on the
148 // function that reside in TargetOptions.
149 resetTargetOptions(F);
150 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
151 Options.StackAlignmentOverride);
152 }
153 return I.get();
154}
155
Chris Lattner12e97302006-09-04 04:14:57 +0000156//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000157// Command line options for x86
158//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000159static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000160UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000161 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000162 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000163
164//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000165// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000166//===----------------------------------------------------------------------===//
167
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000168TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
169 return TargetIRAnalysis(
170 [this](Function &F) { return TargetTransformInfo(X86TTIImpl(this, F)); });
Chandler Carruth664e3542013-01-07 01:37:14 +0000171}
172
173
174//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000175// Pass Pipeline Configuration
176//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000177
Andrew Trickccb67362012-02-03 05:12:41 +0000178namespace {
179/// X86 Code Generator Pass Configuration Options.
180class X86PassConfig : public TargetPassConfig {
181public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000182 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
183 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000184
185 X86TargetMachine &getX86TargetMachine() const {
186 return getTM<X86TargetMachine>();
187 }
188
189 const X86Subtarget &getX86Subtarget() const {
190 return *getX86TargetMachine().getSubtargetImpl();
191 }
192
Tim Northover277066a2014-07-01 18:53:31 +0000193 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000194 bool addInstSelector() override;
195 bool addILPOpts() override;
Michael Kupersteinbd571862015-02-01 11:44:44 +0000196 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000197 void addPostRegAlloc() override;
198 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000199};
200} // namespace
201
Andrew Trickf8ea1082012-02-04 02:56:59 +0000202TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000203 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000204}
205
Tim Northover277066a2014-07-01 18:53:31 +0000206void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000207 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000208
209 TargetPassConfig::addIRPasses();
210}
211
Andrew Trickccb67362012-02-03 05:12:41 +0000212bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000213 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000214 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000215
Hans Wennborg789acfb2012-06-01 16:27:21 +0000216 // For ELF, cleanup any local-dynamic TLS accesses.
217 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000218 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000219
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000220 addPass(createX86GlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000221
Chris Lattner12e97302006-09-04 04:14:57 +0000222 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000223}
224
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000225bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000226 addPass(&EarlyIfConverterID);
227 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000228}
229
Michael Kupersteinbd571862015-02-01 11:44:44 +0000230void X86PassConfig::addPreRegAlloc() {
231 addPass(createX86CallFrameOptimization());
232}
233
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000234void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000235 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000236}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000237
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000238void X86PassConfig::addPreEmitPass() {
239 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2())
Matthias Braunb2f23882014-12-11 23:18:03 +0000240 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000241
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000242 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000243 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000244
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000245 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000246 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000247 addPass(createX86FixupLEAs());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000248 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000249}