blob: 8802febfc03a2a67f0ae47ff2197cbe585add91b [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"
Chris Lattner962d5be2003-01-13 00:51:23 +000017#include "llvm/CodeGen/Passes.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000018#include "llvm/IR/Function.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/PassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000020#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000021#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000026extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000027 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000028 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
29 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000030}
Douglas Gregor1b731d52009-06-16 20:12:29 +000031
Aditya Nandakumara2719322014-11-13 09:26:31 +000032static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
33 if (TT.isOSBinFormatMachO()) {
34 if (TT.getArch() == Triple::x86_64)
35 return make_unique<X86_64MachoTargetObjectFile>();
36 return make_unique<TargetLoweringObjectFileMachO>();
37 }
38
39 if (TT.isOSLinux())
40 return make_unique<X86LinuxTargetObjectFile>();
41 if (TT.isOSBinFormatELF())
42 return make_unique<TargetLoweringObjectFileELF>();
43 if (TT.isKnownWindowsMSVCEnvironment())
44 return make_unique<X86WindowsTargetObjectFile>();
45 if (TT.isOSBinFormatCOFF())
46 return make_unique<TargetLoweringObjectFileCOFF>();
47 llvm_unreachable("unknown subtarget type");
48}
49
Chris Lattner7e3abf12009-07-09 03:32:31 +000050/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000051///
Eric Christopher66f676e2014-06-05 22:10:58 +000052X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
53 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000054 Reloc::Model RM, CodeModel::Model CM,
David Woodhouse1c3996a2014-01-08 00:08:50 +000055 CodeGenOpt::Level OL)
Eric Christopher66f676e2014-06-05 22:10:58 +000056 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +000057 TLOF(createTLOF(Triple(getTargetTriple()))),
Eric Christophera08f30b2014-06-09 17:08:19 +000058 Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
Evan Cheng3a0c5e52011-06-23 17:54:54 +000059 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +000060 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +000061 this->Options.FloatABIType = FloatABI::Hard;
David Woodhouse1c3996a2014-01-08 00:08:50 +000062
Yaron Kerend7ba46b2014-04-19 13:47:43 +000063 // Windows stack unwinder gets confused when execution flow "falls through"
64 // after a call to 'noreturn' function.
65 // To prevent that, we emit a trap for 'unreachable' IR instructions.
66 // (which on X86, happens to be the 'ud2' instruction)
67 if (Subtarget.isTargetWin64())
68 this->Options.TrapUnreachable = true;
69
David Woodhouse1c3996a2014-01-08 00:08:50 +000070 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +000071}
Chris Lattner02a3d832002-10-29 22:37:54 +000072
Reid Kleckner357600e2014-11-20 23:37:18 +000073X86TargetMachine::~X86TargetMachine() {}
74
Eric Christopher3faf2f12014-10-06 06:45:36 +000075const X86Subtarget *
76X86TargetMachine::getSubtargetImpl(const Function &F) const {
77 AttributeSet FnAttrs = F.getAttributes();
78 Attribute CPUAttr =
79 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
80 Attribute FSAttr =
81 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
82
83 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
84 ? CPUAttr.getValueAsString().str()
85 : TargetCPU;
86 std::string FS = !FSAttr.hasAttribute(Attribute::None)
87 ? FSAttr.getValueAsString().str()
88 : TargetFS;
89
90 // FIXME: This is related to the code below to reset the target options,
91 // we need to know whether or not the soft float flag is set on the
92 // function before we can generate a subtarget. We also need to use
93 // it as a key for the subtarget since that can be the only difference
94 // between two functions.
95 Attribute SFAttr =
96 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
97 bool SoftFloat = !SFAttr.hasAttribute(Attribute::None)
98 ? SFAttr.getValueAsString() == "true"
99 : Options.UseSoftFloat;
100
101 auto &I = SubtargetMap[CPU + FS + (SoftFloat ? "use-soft-float=true"
102 : "use-soft-float=false")];
103 if (!I) {
104 // This needs to be done before we create a new subtarget since any
105 // creation will depend on the TM and the code generation flags on the
106 // function that reside in TargetOptions.
107 resetTargetOptions(F);
108 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
109 Options.StackAlignmentOverride);
110 }
111 return I.get();
112}
113
Chris Lattner12e97302006-09-04 04:14:57 +0000114//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000115// Command line options for x86
116//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000117static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000118UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000119 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000120 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000121
122//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +0000123// X86 Analysis Pass Setup
124//===----------------------------------------------------------------------===//
125
126void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
127 // Add first the target-independent BasicTTI pass, then our X86 pass. This
128 // allows the X86 pass to delegate to the target independent layer when
129 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +0000130 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +0000131 PM.add(createX86TargetTransformInfoPass(this));
132}
133
134
135//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000136// Pass Pipeline Configuration
137//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000138
Andrew Trickccb67362012-02-03 05:12:41 +0000139namespace {
140/// X86 Code Generator Pass Configuration Options.
141class X86PassConfig : public TargetPassConfig {
142public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000143 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
144 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000145
146 X86TargetMachine &getX86TargetMachine() const {
147 return getTM<X86TargetMachine>();
148 }
149
150 const X86Subtarget &getX86Subtarget() const {
151 return *getX86TargetMachine().getSubtargetImpl();
152 }
153
Tim Northover277066a2014-07-01 18:53:31 +0000154 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000155 bool addInstSelector() override;
156 bool addILPOpts() override;
Rafael Espindola01c73612014-12-11 20:03:57 +0000157 bool addPreRegAlloc() override;
158 bool addPostRegAlloc() override;
159 bool addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000160};
161} // namespace
162
Andrew Trickf8ea1082012-02-04 02:56:59 +0000163TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000164 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000165}
166
Tim Northover277066a2014-07-01 18:53:31 +0000167void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000168 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000169
170 TargetPassConfig::addIRPasses();
171}
172
Andrew Trickccb67362012-02-03 05:12:41 +0000173bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000174 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000175 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000176
Hans Wennborg789acfb2012-06-01 16:27:21 +0000177 // For ELF, cleanup any local-dynamic TLS accesses.
178 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000179 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000180
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000181 addPass(createX86GlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000182
Chris Lattner12e97302006-09-04 04:14:57 +0000183 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000184}
185
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000186bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000187 addPass(&EarlyIfConverterID);
188 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000189}
190
Rafael Espindola01c73612014-12-11 20:03:57 +0000191bool X86PassConfig::addPreRegAlloc() {
192 return false; // -print-machineinstr shouldn't print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000193}
194
Rafael Espindola01c73612014-12-11 20:03:57 +0000195bool X86PassConfig::addPostRegAlloc() {
196 addPass(createX86FloatingPointStackifierPass());
197 return true; // -print-machineinstr should print after this.
198}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000199
Rafael Espindola01c73612014-12-11 20:03:57 +0000200bool X86PassConfig::addPreEmitPass() {
201 bool ShouldPrint = false;
202 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
203 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
204 ShouldPrint = true;
205 }
206
207 if (UseVZeroUpper) {
Matthias Braun199aeff2014-12-11 19:42:09 +0000208 addPass(createX86IssueVZeroUpperPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000209 ShouldPrint = true;
210 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000211
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000212 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braun199aeff2014-12-11 19:42:09 +0000213 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000214 addPass(createX86FixupLEAs());
Rafael Espindola01c73612014-12-11 20:03:57 +0000215 ShouldPrint = true;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000216 }
Rafael Espindola01c73612014-12-11 20:03:57 +0000217
218 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000219}