blob: 9e72da29f26fa3be91744ac0d0c98dff4cefbb56 [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 Bregerf7359d82017-02-22 12:25:09 +000018#include "X86InstructionSelector.h"
Igor Breger66779992017-02-10 07:33:14 +000019#ifdef LLVM_BUILD_GLOBAL_ISEL
Igor Bregerb4442f32017-02-10 07:05:56 +000020#include "X86RegisterBankInfo.h"
Igor Breger66779992017-02-10 07:33:14 +000021#endif
Evandro Menezes94edf022017-02-01 02:54:34 +000022#include "X86MacroFusion.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000023#include "X86Subtarget.h"
24#include "X86TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000025#include "X86TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000026#include "X86TargetTransformInfo.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000027#include "llvm/ADT/Optional.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/Triple.h"
32#include "llvm/Analysis/TargetTransformInfo.h"
33#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"
Igor Bregerb4442f32017-02-10 07:05:56 +000036#include "llvm/CodeGen/GlobalISel/Legalizer.h"
37#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Igor Bregerf7359d82017-02-22 12:25:09 +000038#include "llvm/CodeGen/GlobalISel/InstructionSelect.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 &);
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000064
65} // end namespace llvm
David Majnemer0ad363e2015-08-18 19:07:12 +000066
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000067extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000068 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000069 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target());
70 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target());
David Majnemer0ad363e2015-08-18 19:07:12 +000071
72 PassRegistry &PR = *PassRegistry::getPassRegistry();
Zvi Rackover76dbf262016-11-15 06:34:33 +000073 initializeGlobalISel(PR);
David Majnemer0ad363e2015-08-18 19:07:12 +000074 initializeWinEHStatePassPass(PR);
Ahmed Bougacha068ac4a2016-05-07 01:11:10 +000075 initializeFixupBWInstPassPass(PR);
Gadi Haber19c4fc52016-12-28 10:12:48 +000076 initializeEvexToVexInstPassPass(PR);
Daniel Dunbare8338102009-07-15 20:24:03 +000077}
Douglas Gregor1b731d52009-06-16 20:12:29 +000078
Aditya Nandakumara2719322014-11-13 09:26:31 +000079static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
80 if (TT.isOSBinFormatMachO()) {
81 if (TT.getArch() == Triple::x86_64)
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000082 return llvm::make_unique<X86_64MachoTargetObjectFile>();
83 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000084 }
85
Davide Italianoa9f85d62016-09-26 22:53:15 +000086 if (TT.isOSFreeBSD())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000087 return llvm::make_unique<X86FreeBSDTargetObjectFile>();
Derek Schuff072f93f2015-03-11 16:16:09 +000088 if (TT.isOSLinux() || TT.isOSNaCl())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000089 return llvm::make_unique<X86LinuxNaClTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000090 if (TT.isOSFuchsia())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000091 return llvm::make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000092 if (TT.isOSBinFormatELF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000093 return llvm::make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000094 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000095 return llvm::make_unique<X86WindowsTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000096 if (TT.isOSBinFormatCOFF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000097 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000098 llvm_unreachable("unknown subtarget type");
99}
100
Eric Christopher8b770652015-01-26 19:03:15 +0000101static std::string computeDataLayout(const Triple &TT) {
102 // X86 is little endian
103 std::string Ret = "e";
104
105 Ret += DataLayout::getManglingComponent(TT);
106 // X86 and x32 have 32 bit pointers.
107 if ((TT.isArch64Bit() &&
108 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
109 !TT.isArch64Bit())
110 Ret += "-p:32:32";
111
112 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
113 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
114 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000115 else if (TT.isOSIAMCU())
116 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +0000117 else
118 Ret += "-f64:32:64";
119
120 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000121 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +0000122 ; // No f80
123 else if (TT.isArch64Bit() || TT.isOSDarwin())
124 Ret += "-f80:128";
125 else
126 Ret += "-f80:32";
127
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000128 if (TT.isOSIAMCU())
129 Ret += "-f128:32";
130
Eric Christopher8b770652015-01-26 19:03:15 +0000131 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
132 if (TT.isArch64Bit())
133 Ret += "-n8:16:32:64";
134 else
135 Ret += "-n8:16:32";
136
137 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000138 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000139 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000140 else
141 Ret += "-S128";
142
143 return Ret;
144}
145
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000146static Reloc::Model getEffectiveRelocModel(const Triple &TT,
147 Optional<Reloc::Model> RM) {
148 bool is64Bit = TT.getArch() == Triple::x86_64;
149 if (!RM.hasValue()) {
150 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
151 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
152 // use static relocation model by default.
153 if (TT.isOSDarwin()) {
154 if (is64Bit)
155 return Reloc::PIC_;
156 return Reloc::DynamicNoPIC;
157 }
158 if (TT.isOSWindows() && is64Bit)
159 return Reloc::PIC_;
160 return Reloc::Static;
161 }
162
163 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
164 // is defined as a model for code which may be used in static or dynamic
165 // executables but not necessarily a shared library. On X86-32 we just
166 // compile in -static mode, in x86-64 we use PIC.
167 if (*RM == Reloc::DynamicNoPIC) {
168 if (is64Bit)
169 return Reloc::PIC_;
170 if (!TT.isOSDarwin())
171 return Reloc::Static;
172 }
173
174 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
175 // the Mach-O file format doesn't support it.
176 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
177 return Reloc::PIC_;
178
179 return *RM;
180}
181
Rafael Espindola38af4d62016-05-18 16:00:24 +0000182/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000183///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000184X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
185 StringRef CPU, StringRef FS,
186 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000187 Optional<Reloc::Model> RM,
188 CodeModel::Model CM, CodeGenOpt::Level OL)
189 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
190 getEffectiveRelocModel(TT, RM), CM, OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000191 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000192 // Windows stack unwinder gets confused when execution flow "falls through"
193 // after a call to 'noreturn' function.
194 // To prevent that, we emit a trap for 'unreachable' IR instructions.
195 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000196 // On PS4, the "return address" of a 'noreturn' call must still be within
197 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000198 // The check here for 64-bit windows is a bit icky, but as we're unlikely
199 // to ever want to mix 32 and 64-bit windows code in a single module
200 // this should be fine.
201 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000202 this->Options.TrapUnreachable = true;
203
David Woodhouse1c3996a2014-01-08 00:08:50 +0000204 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000205}
Chris Lattner02a3d832002-10-29 22:37:54 +0000206
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000207X86TargetMachine::~X86TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000208
Zvi Rackover76dbf262016-11-15 06:34:33 +0000209#ifdef LLVM_BUILD_GLOBAL_ISEL
210namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000211
Zvi Rackover76dbf262016-11-15 06:34:33 +0000212struct X86GISelActualAccessor : public GISelAccessor {
Igor Bregerb4442f32017-02-10 07:05:56 +0000213 std::unique_ptr<CallLowering> CallLoweringInfo;
214 std::unique_ptr<LegalizerInfo> Legalizer;
215 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Igor Bregerf7359d82017-02-22 12:25:09 +0000216 std::unique_ptr<InstructionSelector> InstSelector;
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000217
Zvi Rackover76dbf262016-11-15 06:34:33 +0000218 const CallLowering *getCallLowering() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000219 return CallLoweringInfo.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000220 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000221
Zvi Rackover76dbf262016-11-15 06:34:33 +0000222 const InstructionSelector *getInstructionSelector() const override {
Igor Bregerf7359d82017-02-22 12:25:09 +0000223 return InstSelector.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000224 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000225
Ahmed Bougacha52286032016-12-15 18:45:30 +0000226 const LegalizerInfo *getLegalizerInfo() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000227 return Legalizer.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000228 }
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000229
Zvi Rackover76dbf262016-11-15 06:34:33 +0000230 const RegisterBankInfo *getRegBankInfo() const override {
Igor Bregerb4442f32017-02-10 07:05:56 +0000231 return RegBankInfo.get();
Zvi Rackover76dbf262016-11-15 06:34:33 +0000232 }
233};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000234
235} // end anonymous namespace
Zvi Rackover76dbf262016-11-15 06:34:33 +0000236#endif
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000237
Eric Christopher3faf2f12014-10-06 06:45:36 +0000238const X86Subtarget *
239X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000240 Attribute CPUAttr = F.getFnAttribute("target-cpu");
241 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000242
David Majnemer498f2fd2016-05-20 20:41:24 +0000243 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
244 ? CPUAttr.getValueAsString()
245 : (StringRef)TargetCPU;
246 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
247 ? FSAttr.getValueAsString()
248 : (StringRef)TargetFS;
249
250 SmallString<512> Key;
251 Key.reserve(CPU.size() + FS.size());
252 Key += CPU;
253 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000254
255 // FIXME: This is related to the code below to reset the target options,
256 // we need to know whether or not the soft float flag is set on the
257 // function before we can generate a subtarget. We also need to use
258 // it as a key for the subtarget since that can be the only difference
259 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000260 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000261 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
262 // If the soft float attribute is set on the function turn on the soft float
263 // subtarget feature.
264 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000265 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000266
David Majnemer498f2fd2016-05-20 20:41:24 +0000267 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000268
269 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000270 if (!I) {
271 // This needs to be done before we create a new subtarget since any
272 // creation will depend on the TM and the code generation flags on the
273 // function that reside in TargetOptions.
274 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000275 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Eric Christopher3faf2f12014-10-06 06:45:36 +0000276 Options.StackAlignmentOverride);
Zvi Rackover76dbf262016-11-15 06:34:33 +0000277#ifndef LLVM_BUILD_GLOBAL_ISEL
278 GISelAccessor *GISel = new GISelAccessor();
279#else
Igor Bregerb4442f32017-02-10 07:05:56 +0000280 X86GISelActualAccessor *GISel = new X86GISelActualAccessor();
281
282 GISel->CallLoweringInfo.reset(new X86CallLowering(*I->getTargetLowering()));
283 GISel->Legalizer.reset(new X86LegalizerInfo(*I));
284
285 auto *RBI = new X86RegisterBankInfo(*I->getRegisterInfo());
286 GISel->RegBankInfo.reset(RBI);
Benjamin Kramer5a7e0f82017-02-22 12:59:47 +0000287 GISel->InstSelector.reset(new X86InstructionSelector(*I, *RBI));
Igor Bregerb4442f32017-02-10 07:05:56 +0000288
Zvi Rackover76dbf262016-11-15 06:34:33 +0000289#endif
290 I->setGISelAccessor(*GISel);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000291 }
292 return I.get();
293}
294
Chris Lattner12e97302006-09-04 04:14:57 +0000295//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000296// Command line options for x86
297//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000298static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000299UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000300 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000301 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000302
303//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000304// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000305//===----------------------------------------------------------------------===//
306
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000307TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000308 return TargetIRAnalysis([this](const Function &F) {
309 return TargetTransformInfo(X86TTIImpl(this, F));
310 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000311}
312
Chandler Carruth664e3542013-01-07 01:37:14 +0000313//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000314// Pass Pipeline Configuration
315//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000316
Andrew Trickccb67362012-02-03 05:12:41 +0000317namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000318
Andrew Trickccb67362012-02-03 05:12:41 +0000319/// X86 Code Generator Pass Configuration Options.
320class X86PassConfig : public TargetPassConfig {
321public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000322 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
323 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000324
325 X86TargetMachine &getX86TargetMachine() const {
326 return getTM<X86TargetMachine>();
327 }
328
Matthias Braun115efcd2016-11-28 20:11:54 +0000329 ScheduleDAGInstrs *
330 createMachineScheduler(MachineSchedContext *C) const override {
331 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
Evandro Menezes94edf022017-02-01 02:54:34 +0000332 DAG->addMutation(createX86MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000333 return DAG;
334 }
335
Tim Northover277066a2014-07-01 18:53:31 +0000336 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000337 bool addInstSelector() override;
Zvi Rackover76dbf262016-11-15 06:34:33 +0000338#ifdef LLVM_BUILD_GLOBAL_ISEL
339 bool addIRTranslator() override;
340 bool addLegalizeMachineIR() override;
341 bool addRegBankSelect() override;
342 bool addGlobalInstructionSelect() override;
343#endif
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000344 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000345 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000346 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000347 void addPostRegAlloc() override;
348 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000349 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000350};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000351
352} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000353
Andrew Trickf8ea1082012-02-04 02:56:59 +0000354TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000355 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000356}
357
Tim Northover277066a2014-07-01 18:53:31 +0000358void X86PassConfig::addIRPasses() {
Robin Morisset25c8e312014-09-17 00:06:58 +0000359 addPass(createAtomicExpandPass(&getX86TargetMachine()));
Tim Northover277066a2014-07-01 18:53:31 +0000360
361 TargetPassConfig::addIRPasses();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000362
363 if (TM->getOptLevel() != CodeGenOpt::None)
364 addPass(createInterleavedAccessPass(TM));
Tim Northover277066a2014-07-01 18:53:31 +0000365}
366
Andrew Trickccb67362012-02-03 05:12:41 +0000367bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000368 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000369 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000370
Hans Wennborg789acfb2012-06-01 16:27:21 +0000371 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000372 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000373 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000374 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000375
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000376 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000377 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000378}
379
Zvi Rackover76dbf262016-11-15 06:34:33 +0000380#ifdef LLVM_BUILD_GLOBAL_ISEL
381bool X86PassConfig::addIRTranslator() {
382 addPass(new IRTranslator());
383 return false;
384}
385
386bool X86PassConfig::addLegalizeMachineIR() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000387 addPass(new Legalizer());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000388 return false;
389}
390
391bool X86PassConfig::addRegBankSelect() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000392 addPass(new RegBankSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000393 return false;
394}
395
396bool X86PassConfig::addGlobalInstructionSelect() {
Igor Bregerf7359d82017-02-22 12:25:09 +0000397 addPass(new InstructionSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000398 return false;
399}
400#endif
401
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000402bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000403 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000404 if (EnableMachineCombinerPass)
405 addPass(&MachineCombinerID);
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000406 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000407}
408
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000409bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000410 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000411 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000412 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000413 addPass(createX86WinEHStatePass());
414 return true;
415}
416
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000417void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000418 if (getOptLevel() != CodeGenOpt::None) {
Nico Weberead8f8f2016-07-14 15:07:44 +0000419 addPass(createX86FixupSetCC());
420 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000421 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000422 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000423
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000424 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000425}
426
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000427void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000428 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000429}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000430
Quentin Colombet494eb602015-05-22 18:10:47 +0000431void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
432
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000433void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000434 if (getOptLevel() != CodeGenOpt::None)
Matthias Braune9f82092017-03-18 05:05:40 +0000435 addPass(createExecutionDepsFixPass(&X86::VR128XRegClass));
Rafael Espindola01c73612014-12-11 20:03:57 +0000436
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000437 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000438 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000439
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000440 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000441 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000442 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000443 addPass(createX86FixupLEAs());
Gadi Haber19c4fc52016-12-28 10:12:48 +0000444 addPass(createX86EvexToVexInsts());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000445 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000446}