blob: 1c5f2a2a61aa2d2d4afcc88cf03ef0512a2774f8 [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"
Evandro Menezes94edf022017-02-01 02:54:34 +000018#include "X86MacroFusion.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000021#include "X86TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000022#include "X86TargetTransformInfo.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000023#include "llvm/ADT/Optional.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000024#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/ADT/SmallString.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000026#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000029#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000030#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000031#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000032#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000033#include "llvm/CodeGen/GlobalISel/Legalizer.h"
34#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000035#include "llvm/CodeGen/MachineScheduler.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000036#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000037#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000038#include "llvm/IR/Attributes.h"
39#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000040#include "llvm/IR/Function.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000041#include "llvm/Pass.h"
42#include "llvm/Support/CodeGen.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000043#include "llvm/Support/CommandLine.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000044#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000046#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetOptions.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000048#include <memory>
49#include <string>
50
Chris Lattner833c3c22003-12-20 01:22:19 +000051using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000052
Sanjay Patel08829ba2015-06-10 20:32:21 +000053static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
54 cl::desc("Enable the machine combiner pass"),
55 cl::init(true), cl::Hidden);
56
David Majnemer0ad363e2015-08-18 19:07:12 +000057namespace llvm {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000058
David Majnemer0ad363e2015-08-18 19:07:12 +000059void initializeWinEHStatePassPass(PassRegistry &);
Lama Saba2ea271b2017-05-18 08:11:50 +000060void initializeFixupLEAPassPass(PassRegistry &);
Amjad Aboud8ef85a02017-10-02 21:46:37 +000061void initializeX86CmovConverterPassPass(PassRegistry &);
Matthias Braune6ff30b2017-03-18 05:08:58 +000062void initializeX86ExecutionDepsFixPass(PassRegistry &);
Guy Blank92d5ce32017-10-22 11:43:08 +000063void initializeX86DomainReassignmentPass(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);
Lama Saba2ea271b2017-05-18 08:11:50 +000077 initializeFixupLEAPassPass(PR);
Amjad Aboud8ef85a02017-10-02 21:46:37 +000078 initializeX86CmovConverterPassPass(PR);
Matthias Braune6ff30b2017-03-18 05:08:58 +000079 initializeX86ExecutionDepsFixPass(PR);
Guy Blank92d5ce32017-10-22 11:43:08 +000080 initializeX86DomainReassignmentPass(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>();
Davide Italiano9b8e3d32017-06-21 20:36:32 +000094 if (TT.isOSSolaris())
95 return llvm::make_unique<X86SolarisTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000096 if (TT.isOSFuchsia())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000097 return llvm::make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000098 if (TT.isOSBinFormatELF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000099 return llvm::make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +0000100 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000101 return llvm::make_unique<X86WindowsTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000102 if (TT.isOSBinFormatCOFF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000103 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000104 llvm_unreachable("unknown subtarget type");
105}
106
Eric Christopher8b770652015-01-26 19:03:15 +0000107static std::string computeDataLayout(const Triple &TT) {
108 // X86 is little endian
109 std::string Ret = "e";
110
111 Ret += DataLayout::getManglingComponent(TT);
112 // X86 and x32 have 32 bit pointers.
113 if ((TT.isArch64Bit() &&
114 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
115 !TT.isArch64Bit())
116 Ret += "-p:32:32";
117
118 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
119 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
120 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000121 else if (TT.isOSIAMCU())
122 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +0000123 else
124 Ret += "-f64:32:64";
125
126 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000127 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +0000128 ; // No f80
129 else if (TT.isArch64Bit() || TT.isOSDarwin())
130 Ret += "-f80:128";
131 else
132 Ret += "-f80:32";
133
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000134 if (TT.isOSIAMCU())
135 Ret += "-f128:32";
136
Eric Christopher8b770652015-01-26 19:03:15 +0000137 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
138 if (TT.isArch64Bit())
139 Ret += "-n8:16:32:64";
140 else
141 Ret += "-n8:16:32";
142
143 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000144 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000145 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000146 else
147 Ret += "-S128";
148
149 return Ret;
150}
151
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000152static Reloc::Model getEffectiveRelocModel(const Triple &TT,
153 Optional<Reloc::Model> RM) {
154 bool is64Bit = TT.getArch() == Triple::x86_64;
155 if (!RM.hasValue()) {
156 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
157 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
158 // use static relocation model by default.
159 if (TT.isOSDarwin()) {
160 if (is64Bit)
161 return Reloc::PIC_;
162 return Reloc::DynamicNoPIC;
163 }
164 if (TT.isOSWindows() && is64Bit)
165 return Reloc::PIC_;
166 return Reloc::Static;
167 }
168
169 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
170 // is defined as a model for code which may be used in static or dynamic
171 // executables but not necessarily a shared library. On X86-32 we just
172 // compile in -static mode, in x86-64 we use PIC.
173 if (*RM == Reloc::DynamicNoPIC) {
174 if (is64Bit)
175 return Reloc::PIC_;
176 if (!TT.isOSDarwin())
177 return Reloc::Static;
178 }
179
180 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
181 // the Mach-O file format doesn't support it.
182 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
183 return Reloc::PIC_;
184
185 return *RM;
186}
187
Rafael Espindola79e238a2017-08-03 02:16:21 +0000188static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
189 bool JIT, bool Is64Bit) {
190 if (CM)
191 return *CM;
192 if (JIT)
193 return Is64Bit ? CodeModel::Large : CodeModel::Small;
194 return CodeModel::Small;
195}
196
Rafael Espindola38af4d62016-05-18 16:00:24 +0000197/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000198///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000199X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
200 StringRef CPU, StringRef FS,
201 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000202 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000203 Optional<CodeModel::Model> CM,
204 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000205 : LLVMTargetMachine(
Rafael Espindola79e238a2017-08-03 02:16:21 +0000206 T, computeDataLayout(TT), TT, CPU, FS, Options,
207 getEffectiveRelocModel(TT, RM),
208 getEffectiveCodeModel(CM, JIT, TT.getArch() == Triple::x86_64), OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000209 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000210 // Windows stack unwinder gets confused when execution flow "falls through"
211 // after a call to 'noreturn' function.
212 // To prevent that, we emit a trap for 'unreachable' IR instructions.
213 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000214 // On PS4, the "return address" of a 'noreturn' call must still be within
215 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000216 // The check here for 64-bit windows is a bit icky, but as we're unlikely
217 // to ever want to mix 32 and 64-bit windows code in a single module
218 // this should be fine.
219 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000220 this->Options.TrapUnreachable = true;
221
David Woodhouse1c3996a2014-01-08 00:08:50 +0000222 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000223}
Chris Lattner02a3d832002-10-29 22:37:54 +0000224
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000225X86TargetMachine::~X86TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000226
Eric Christopher3faf2f12014-10-06 06:45:36 +0000227const X86Subtarget *
228X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000229 Attribute CPUAttr = F.getFnAttribute("target-cpu");
230 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000231
David Majnemer498f2fd2016-05-20 20:41:24 +0000232 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
233 ? CPUAttr.getValueAsString()
234 : (StringRef)TargetCPU;
235 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
236 ? FSAttr.getValueAsString()
237 : (StringRef)TargetFS;
238
239 SmallString<512> Key;
240 Key.reserve(CPU.size() + FS.size());
241 Key += CPU;
242 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000243
244 // FIXME: This is related to the code below to reset the target options,
245 // we need to know whether or not the soft float flag is set on the
246 // function before we can generate a subtarget. We also need to use
247 // it as a key for the subtarget since that can be the only difference
248 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000249 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000250 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
251 // If the soft float attribute is set on the function turn on the soft float
252 // subtarget feature.
253 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000254 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000255
David Majnemer498f2fd2016-05-20 20:41:24 +0000256 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000257
258 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000259 if (!I) {
260 // This needs to be done before we create a new subtarget since any
261 // creation will depend on the TM and the code generation flags on the
262 // function that reside in TargetOptions.
263 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000264 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000265 Options.StackAlignmentOverride);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000266 }
267 return I.get();
268}
269
Chris Lattner12e97302006-09-04 04:14:57 +0000270//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000271// Command line options for x86
272//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000273static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000274UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000275 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000276 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000277
278//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000279// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000280//===----------------------------------------------------------------------===//
281
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000282TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000283 return TargetIRAnalysis([this](const Function &F) {
284 return TargetTransformInfo(X86TTIImpl(this, F));
285 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000286}
287
Chandler Carruth664e3542013-01-07 01:37:14 +0000288//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000289// Pass Pipeline Configuration
290//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000291
Andrew Trickccb67362012-02-03 05:12:41 +0000292namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000293
Andrew Trickccb67362012-02-03 05:12:41 +0000294/// X86 Code Generator Pass Configuration Options.
295class X86PassConfig : public TargetPassConfig {
296public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000297 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000298 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000299
300 X86TargetMachine &getX86TargetMachine() const {
301 return getTM<X86TargetMachine>();
302 }
303
Matthias Braun115efcd2016-11-28 20:11:54 +0000304 ScheduleDAGInstrs *
305 createMachineScheduler(MachineSchedContext *C) const override {
306 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
Evandro Menezes94edf022017-02-01 02:54:34 +0000307 DAG->addMutation(createX86MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000308 return DAG;
309 }
310
Tim Northover277066a2014-07-01 18:53:31 +0000311 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000312 bool addInstSelector() override;
Zvi Rackover76dbf262016-11-15 06:34:33 +0000313 bool addIRTranslator() override;
314 bool addLegalizeMachineIR() override;
315 bool addRegBankSelect() override;
316 bool addGlobalInstructionSelect() override;
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000317 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000318 bool addPreISel() override;
Guy Blank92d5ce32017-10-22 11:43:08 +0000319 void addMachineSSAOptimization() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000320 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000321 void addPostRegAlloc() override;
322 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000323 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000324};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000325
Matthias Braune6ff30b2017-03-18 05:08:58 +0000326class X86ExecutionDepsFix : public ExecutionDepsFix {
327public:
328 static char ID;
329 X86ExecutionDepsFix() : ExecutionDepsFix(ID, X86::VR128XRegClass) {}
330 StringRef getPassName() const override {
331 return "X86 Execution Dependency Fix";
332 }
333};
334char X86ExecutionDepsFix::ID;
335
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000336} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000337
Matthias Braune6ff30b2017-03-18 05:08:58 +0000338INITIALIZE_PASS(X86ExecutionDepsFix, "x86-execution-deps-fix",
Davide Italiano200e5e12017-03-21 19:17:23 +0000339 "X86 Execution Dependency Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000340
Andrew Trickf8ea1082012-02-04 02:56:59 +0000341TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000342 return new X86PassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000343}
344
Tim Northover277066a2014-07-01 18:53:31 +0000345void X86PassConfig::addIRPasses() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000346 addPass(createAtomicExpandPass());
Tim Northover277066a2014-07-01 18:53:31 +0000347
348 TargetPassConfig::addIRPasses();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000349
350 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000351 addPass(createInterleavedAccessPass());
Tim Northover277066a2014-07-01 18:53:31 +0000352}
353
Andrew Trickccb67362012-02-03 05:12:41 +0000354bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000355 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000356 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000357
Hans Wennborg789acfb2012-06-01 16:27:21 +0000358 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000359 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000360 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000361 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000362
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000363 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000364 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000365}
366
Zvi Rackover76dbf262016-11-15 06:34:33 +0000367bool X86PassConfig::addIRTranslator() {
368 addPass(new IRTranslator());
369 return false;
370}
371
372bool X86PassConfig::addLegalizeMachineIR() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000373 addPass(new Legalizer());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000374 return false;
375}
376
377bool X86PassConfig::addRegBankSelect() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000378 addPass(new RegBankSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000379 return false;
380}
381
382bool X86PassConfig::addGlobalInstructionSelect() {
Igor Bregerf7359d82017-02-22 12:25:09 +0000383 addPass(new InstructionSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000384 return false;
385}
Zvi Rackover76dbf262016-11-15 06:34:33 +0000386
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000387bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000388 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000389 if (EnableMachineCombinerPass)
390 addPass(&MachineCombinerID);
Amjad Aboud4563c062017-07-16 17:39:56 +0000391 addPass(createX86CmovConverterPass());
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000392 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000393}
394
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000395bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000396 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000397 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000398 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000399 addPass(createX86WinEHStatePass());
400 return true;
401}
402
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000403void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000404 if (getOptLevel() != CodeGenOpt::None) {
Dehao Chen6b737dd2017-05-31 23:25:25 +0000405 addPass(&LiveRangeShrinkID);
Nico Weberead8f8f2016-07-14 15:07:44 +0000406 addPass(createX86FixupSetCC());
407 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000408 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000409 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000410
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000411 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000412}
Guy Blank92d5ce32017-10-22 11:43:08 +0000413void X86PassConfig::addMachineSSAOptimization() {
414 addPass(createX86DomainReassignmentPass());
415 TargetPassConfig::addMachineSSAOptimization();
416}
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000417
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000418void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000419 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000420}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000421
Quentin Colombet494eb602015-05-22 18:10:47 +0000422void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
423
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000424void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000425 if (getOptLevel() != CodeGenOpt::None)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000426 addPass(new X86ExecutionDepsFix());
Rafael Espindola01c73612014-12-11 20:03:57 +0000427
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000428 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000429 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000430
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000431 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000432 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000433 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000434 addPass(createX86FixupLEAs());
Gadi Haber19c4fc52016-12-28 10:12:48 +0000435 addPass(createX86EvexToVexInsts());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000436 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000437}