blob: 21fbdc7be54388fb61411a022e787b30ac6f73d9 [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
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000014#include "MCTargetDesc/X86MCTargetDesc.h"
Chris Lattnera32b4052002-12-24 00:04:01 +000015#include "X86.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000016#include "X86CallLowering.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000017#include "X86LegalizerInfo.h"
Igor Breger66779992017-02-10 07:33:14 +000018#ifdef LLVM_BUILD_GLOBAL_ISEL
Igor Bregerb4442f32017-02-10 07:05:56 +000019#include "X86RegisterBankInfo.h"
Igor Breger66779992017-02-10 07:33:14 +000020#endif
Evandro Menezes94edf022017-02-01 02:54:34 +000021#include "X86MacroFusion.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000022#include "X86Subtarget.h"
23#include "X86TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000024#include "X86TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000025#include "X86TargetTransformInfo.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000026#include "llvm/ADT/Optional.h"
27#include "llvm/ADT/SmallString.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/Triple.h"
31#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000032#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000033#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000034#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
35#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000036#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000037#include "llvm/CodeGen/GlobalISel/Legalizer.h"
38#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000039#include "llvm/CodeGen/MachineScheduler.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000040#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000041#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000042#include "llvm/IR/Attributes.h"
43#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000044#include "llvm/IR/Function.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000045#include "llvm/Pass.h"
46#include "llvm/Support/CodeGen.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000047#include "llvm/Support/CommandLine.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000049#include "llvm/Support/TargetRegistry.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000050#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000051#include "llvm/Target/TargetOptions.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000052#include <memory>
53#include <string>
54
Chris Lattner833c3c22003-12-20 01:22:19 +000055using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000056
Sanjay Patel08829ba2015-06-10 20:32:21 +000057static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
58 cl::desc("Enable the machine combiner pass"),
59 cl::init(true), cl::Hidden);
60
David Majnemer0ad363e2015-08-18 19:07:12 +000061namespace llvm {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000062
David Majnemer0ad363e2015-08-18 19:07:12 +000063void initializeWinEHStatePassPass(PassRegistry &);
Lama Saba2ea271b2017-05-18 08:11:50 +000064void initializeFixupLEAPassPass(PassRegistry &);
Matthias Braune6ff30b2017-03-18 05:08:58 +000065void initializeX86ExecutionDepsFixPass(PassRegistry &);
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000066
67} // end namespace llvm
David Majnemer0ad363e2015-08-18 19:07:12 +000068
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000069extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000070 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000071 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target());
72 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target());
David Majnemer0ad363e2015-08-18 19:07:12 +000073
74 PassRegistry &PR = *PassRegistry::getPassRegistry();
Zvi Rackover76dbf262016-11-15 06:34:33 +000075 initializeGlobalISel(PR);
David Majnemer0ad363e2015-08-18 19:07:12 +000076 initializeWinEHStatePassPass(PR);
Ahmed Bougacha068ac4a2016-05-07 01:11:10 +000077 initializeFixupBWInstPassPass(PR);
Gadi Haber19c4fc52016-12-28 10:12:48 +000078 initializeEvexToVexInstPassPass(PR);
Lama Saba2ea271b2017-05-18 08:11:50 +000079 initializeFixupLEAPassPass(PR);
Matthias Braune6ff30b2017-03-18 05:08:58 +000080 initializeX86ExecutionDepsFixPass(PR);
Daniel Dunbare8338102009-07-15 20:24:03 +000081}
Douglas Gregor1b731d52009-06-16 20:12:29 +000082
Aditya Nandakumara2719322014-11-13 09:26:31 +000083static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
84 if (TT.isOSBinFormatMachO()) {
85 if (TT.getArch() == Triple::x86_64)
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000086 return llvm::make_unique<X86_64MachoTargetObjectFile>();
87 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000088 }
89
Davide Italianoa9f85d62016-09-26 22:53:15 +000090 if (TT.isOSFreeBSD())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000091 return llvm::make_unique<X86FreeBSDTargetObjectFile>();
Nikolai Bozhenovb7bf3862017-05-09 10:14:03 +000092 if (TT.isOSLinux() || TT.isOSNaCl() || TT.isOSIAMCU())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000093 return llvm::make_unique<X86LinuxNaClTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000094 if (TT.isOSFuchsia())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000095 return llvm::make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000096 if (TT.isOSBinFormatELF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000097 return llvm::make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000098 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000099 return llvm::make_unique<X86WindowsTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000100 if (TT.isOSBinFormatCOFF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000101 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000102 llvm_unreachable("unknown subtarget type");
103}
104
Eric Christopher8b770652015-01-26 19:03:15 +0000105static std::string computeDataLayout(const Triple &TT) {
106 // X86 is little endian
107 std::string Ret = "e";
108
109 Ret += DataLayout::getManglingComponent(TT);
110 // X86 and x32 have 32 bit pointers.
111 if ((TT.isArch64Bit() &&
112 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
113 !TT.isArch64Bit())
114 Ret += "-p:32:32";
115
116 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
117 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
118 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000119 else if (TT.isOSIAMCU())
120 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +0000121 else
122 Ret += "-f64:32:64";
123
124 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000125 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +0000126 ; // No f80
127 else if (TT.isArch64Bit() || TT.isOSDarwin())
128 Ret += "-f80:128";
129 else
130 Ret += "-f80:32";
131
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000132 if (TT.isOSIAMCU())
133 Ret += "-f128:32";
134
Eric Christopher8b770652015-01-26 19:03:15 +0000135 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
136 if (TT.isArch64Bit())
137 Ret += "-n8:16:32:64";
138 else
139 Ret += "-n8:16:32";
140
141 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000142 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000143 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000144 else
145 Ret += "-S128";
146
147 return Ret;
148}
149
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000150static Reloc::Model getEffectiveRelocModel(const Triple &TT,
151 Optional<Reloc::Model> RM) {
152 bool is64Bit = TT.getArch() == Triple::x86_64;
153 if (!RM.hasValue()) {
154 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
155 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
156 // use static relocation model by default.
157 if (TT.isOSDarwin()) {
158 if (is64Bit)
159 return Reloc::PIC_;
160 return Reloc::DynamicNoPIC;
161 }
162 if (TT.isOSWindows() && is64Bit)
163 return Reloc::PIC_;
164 return Reloc::Static;
165 }
166
167 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
168 // is defined as a model for code which may be used in static or dynamic
169 // executables but not necessarily a shared library. On X86-32 we just
170 // compile in -static mode, in x86-64 we use PIC.
171 if (*RM == Reloc::DynamicNoPIC) {
172 if (is64Bit)
173 return Reloc::PIC_;
174 if (!TT.isOSDarwin())
175 return Reloc::Static;
176 }
177
178 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
179 // the Mach-O file format doesn't support it.
180 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
181 return Reloc::PIC_;
182
183 return *RM;
184}
185
Rafael Espindola38af4d62016-05-18 16:00:24 +0000186/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000187///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000188X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
189 StringRef CPU, StringRef FS,
190 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000191 Optional<Reloc::Model> RM,
192 CodeModel::Model CM, CodeGenOpt::Level OL)
193 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
194 getEffectiveRelocModel(TT, RM), CM, OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000195 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000196 // Windows stack unwinder gets confused when execution flow "falls through"
197 // after a call to 'noreturn' function.
198 // To prevent that, we emit a trap for 'unreachable' IR instructions.
199 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000200 // On PS4, the "return address" of a 'noreturn' call must still be within
201 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000202 // The check here for 64-bit windows is a bit icky, but as we're unlikely
203 // to ever want to mix 32 and 64-bit windows code in a single module
204 // this should be fine.
205 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000206 this->Options.TrapUnreachable = true;
207
David Woodhouse1c3996a2014-01-08 00:08:50 +0000208 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000209}
Chris Lattner02a3d832002-10-29 22:37:54 +0000210
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000211X86TargetMachine::~X86TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000212
Zvi Rackover76dbf262016-11-15 06:34:33 +0000213#ifdef LLVM_BUILD_GLOBAL_ISEL
214namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000215
Zvi Rackover76dbf262016-11-15 06:34:33 +0000216struct X86GISelActualAccessor : public GISelAccessor {
Igor Bregerb4442f32017-02-10 07:05:56 +0000217 std::unique_ptr<CallLowering> CallLoweringInfo;
218 std::unique_ptr<LegalizerInfo> Legalizer;
219 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Igor Bregerf7359d82017-02-22 12:25:09 +0000220 std::unique_ptr<InstructionSelector> InstSelector;
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000221
Zvi Rackover76dbf262016-11-15 06:34:33 +0000222 const CallLowering *getCallLowering() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000223 return CallLoweringInfo.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000224 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000225
Zvi Rackover76dbf262016-11-15 06:34:33 +0000226 const InstructionSelector *getInstructionSelector() const override {
Igor Bregerf7359d82017-02-22 12:25:09 +0000227 return InstSelector.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000228 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000229
Ahmed Bougacha52286032016-12-15 18:45:30 +0000230 const LegalizerInfo *getLegalizerInfo() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000231 return Legalizer.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000232 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000233
Zvi Rackover76dbf262016-11-15 06:34:33 +0000234 const RegisterBankInfo *getRegBankInfo() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000235 return RegBankInfo.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000236 }
237};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000238
239} // end anonymous namespace
Zvi Rackover76dbf262016-11-15 06:34:33 +0000240#endif
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000241
Eric Christopher3faf2f12014-10-06 06:45:36 +0000242const X86Subtarget *
243X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000244 Attribute CPUAttr = F.getFnAttribute("target-cpu");
245 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000246
David Majnemer498f2fd2016-05-20 20:41:24 +0000247 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
248 ? CPUAttr.getValueAsString()
249 : (StringRef)TargetCPU;
250 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
251 ? FSAttr.getValueAsString()
252 : (StringRef)TargetFS;
253
254 SmallString<512> Key;
255 Key.reserve(CPU.size() + FS.size());
256 Key += CPU;
257 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000258
259 // FIXME: This is related to the code below to reset the target options,
260 // we need to know whether or not the soft float flag is set on the
261 // function before we can generate a subtarget. We also need to use
262 // it as a key for the subtarget since that can be the only difference
263 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000264 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000265 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
266 // If the soft float attribute is set on the function turn on the soft float
267 // subtarget feature.
268 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000269 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000270
David Majnemer498f2fd2016-05-20 20:41:24 +0000271 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000272
273 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000274 if (!I) {
275 // This needs to be done before we create a new subtarget since any
276 // creation will depend on the TM and the code generation flags on the
277 // function that reside in TargetOptions.
278 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000279 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000280 Options.StackAlignmentOverride);
Zvi Rackover76dbf262016-11-15 06:34:33 +0000281#ifndef LLVM_BUILD_GLOBAL_ISEL
282 GISelAccessor *GISel = new GISelAccessor();
283#else
Igor Bregerb4442f32017-02-10 07:05:56 +0000284 X86GISelActualAccessor *GISel = new X86GISelActualAccessor();
285
286 GISel->CallLoweringInfo.reset(new X86CallLowering(*I->getTargetLowering()));
Igor Breger531a2032017-03-26 08:11:12 +0000287 GISel->Legalizer.reset(new X86LegalizerInfo(*I, *this));
Igor Bregerb4442f32017-02-10 07:05:56 +0000288
289 auto *RBI = new X86RegisterBankInfo(*I->getRegisterInfo());
290 GISel->RegBankInfo.reset(RBI);
Daniel Sanderse9fdba32017-04-29 17:30:09 +0000291 GISel->InstSelector.reset(createX86InstructionSelector(
292 *this, *I, *RBI));
Zvi Rackover76dbf262016-11-15 06:34:33 +0000293#endif
294 I->setGISelAccessor(*GISel);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000295 }
296 return I.get();
297}
298
Chris Lattner12e97302006-09-04 04:14:57 +0000299//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000300// Command line options for x86
301//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000302static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000303UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000304 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000305 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000306
307//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000308// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000309//===----------------------------------------------------------------------===//
310
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000311TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000312 return TargetIRAnalysis([this](const Function &F) {
313 return TargetTransformInfo(X86TTIImpl(this, F));
314 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000315}
316
Chandler Carruth664e3542013-01-07 01:37:14 +0000317//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000318// Pass Pipeline Configuration
319//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000320
Andrew Trickccb67362012-02-03 05:12:41 +0000321namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000322
Andrew Trickccb67362012-02-03 05:12:41 +0000323/// X86 Code Generator Pass Configuration Options.
324class X86PassConfig : public TargetPassConfig {
325public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000326 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000327 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000328
329 X86TargetMachine &getX86TargetMachine() const {
330 return getTM<X86TargetMachine>();
331 }
332
Matthias Braun115efcd2016-11-28 20:11:54 +0000333 ScheduleDAGInstrs *
334 createMachineScheduler(MachineSchedContext *C) const override {
335 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
Evandro Menezes94edf022017-02-01 02:54:34 +0000336 DAG->addMutation(createX86MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000337 return DAG;
338 }
339
Tim Northover277066a2014-07-01 18:53:31 +0000340 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000341 bool addInstSelector() override;
Zvi Rackover76dbf262016-11-15 06:34:33 +0000342#ifdef LLVM_BUILD_GLOBAL_ISEL
343 bool addIRTranslator() override;
344 bool addLegalizeMachineIR() override;
345 bool addRegBankSelect() override;
346 bool addGlobalInstructionSelect() override;
347#endif
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000348 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000349 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000350 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000351 void addPostRegAlloc() override;
352 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000353 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000354};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000355
Matthias Braune6ff30b2017-03-18 05:08:58 +0000356class X86ExecutionDepsFix : public ExecutionDepsFix {
357public:
358 static char ID;
359 X86ExecutionDepsFix() : ExecutionDepsFix(ID, X86::VR128XRegClass) {}
360 StringRef getPassName() const override {
361 return "X86 Execution Dependency Fix";
362 }
363};
364char X86ExecutionDepsFix::ID;
365
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000366} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000367
Matthias Braune6ff30b2017-03-18 05:08:58 +0000368INITIALIZE_PASS(X86ExecutionDepsFix, "x86-execution-deps-fix",
Davide Italiano200e5e12017-03-21 19:17:23 +0000369 "X86 Execution Dependency Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000370
Andrew Trickf8ea1082012-02-04 02:56:59 +0000371TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000372 return new X86PassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000373}
374
Tim Northover277066a2014-07-01 18:53:31 +0000375void X86PassConfig::addIRPasses() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000376 addPass(createAtomicExpandPass());
Tim Northover277066a2014-07-01 18:53:31 +0000377
378 TargetPassConfig::addIRPasses();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000379
380 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000381 addPass(createInterleavedAccessPass());
Tim Northover277066a2014-07-01 18:53:31 +0000382}
383
Andrew Trickccb67362012-02-03 05:12:41 +0000384bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000385 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000386 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000387
Hans Wennborg789acfb2012-06-01 16:27:21 +0000388 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000389 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000390 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000391 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000392
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000393 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000394 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000395}
396
Zvi Rackover76dbf262016-11-15 06:34:33 +0000397#ifdef LLVM_BUILD_GLOBAL_ISEL
398bool X86PassConfig::addIRTranslator() {
399 addPass(new IRTranslator());
400 return false;
401}
402
403bool X86PassConfig::addLegalizeMachineIR() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000404 addPass(new Legalizer());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000405 return false;
406}
407
408bool X86PassConfig::addRegBankSelect() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000409 addPass(new RegBankSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000410 return false;
411}
412
413bool X86PassConfig::addGlobalInstructionSelect() {
Igor Bregerf7359d82017-02-22 12:25:09 +0000414 addPass(new InstructionSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000415 return false;
416}
417#endif
418
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000419bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000420 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000421 if (EnableMachineCombinerPass)
422 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000423 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000424}
425
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000426bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000427 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000428 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000429 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000430 addPass(createX86WinEHStatePass());
431 return true;
432}
433
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000434void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000435 if (getOptLevel() != CodeGenOpt::None) {
Nico Weberead8f8f2016-07-14 15:07:44 +0000436 addPass(createX86FixupSetCC());
437 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000438 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000439 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000440
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000441 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000442}
443
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000444void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000445 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000446}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000447
Quentin Colombet494eb602015-05-22 18:10:47 +0000448void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
449
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000450void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000451 if (getOptLevel() != CodeGenOpt::None)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000452 addPass(new X86ExecutionDepsFix());
Rafael Espindola01c73612014-12-11 20:03:57 +0000453
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000454 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000455 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000456
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000457 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000458 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000459 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000460 addPass(createX86FixupLEAs());
Gadi Haber19c4fc52016-12-28 10:12:48 +0000461 addPass(createX86EvexToVexInsts());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000462 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000463}