blob: c7a421ba71d865e0a4a71cfe78d9191660756b67 [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
David Blaikieb3bde2e2017-11-17 01:07:10 +000014#include "X86TargetMachine.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000015#include "MCTargetDesc/X86MCTargetDesc.h"
Chris Lattnera32b4052002-12-24 00:04:01 +000016#include "X86.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000017#include "X86CallLowering.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000018#include "X86LegalizerInfo.h"
Evandro Menezes94edf022017-02-01 02:54:34 +000019#include "X86MacroFusion.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000020#include "X86Subtarget.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"
Marina Yatsina3d8efa42018-01-22 10:06:33 +000029#include "llvm/CodeGen/ExecutionDomainFix.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"
David Blaikieb3bde2e2017-11-17 01:07:10 +000037#include "llvm/CodeGen/TargetLoweringObjectFile.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000038#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000039#include "llvm/IR/Attributes.h"
40#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000041#include "llvm/IR/Function.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000042#include "llvm/Pass.h"
43#include "llvm/Support/CodeGen.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000044#include "llvm/Support/CommandLine.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000045#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000046#include "llvm/Support/TargetRegistry.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 &);
Zvi Rackoverc6d0b6c2017-10-24 05:47:07 +000061void initializeX86CallFrameOptimizationPass(PassRegistry &);
Amjad Aboud8ef85a02017-10-02 21:46:37 +000062void initializeX86CmovConverterPassPass(PassRegistry &);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000063void initializeX86ExecutionDomainFixPass(PassRegistry &);
Guy Blank92d5ce32017-10-22 11:43:08 +000064void initializeX86DomainReassignmentPass(PassRegistry &);
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000065
66} // end namespace llvm
David Majnemer0ad363e2015-08-18 19:07:12 +000067
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000068extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000069 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000070 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target());
71 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target());
David Majnemer0ad363e2015-08-18 19:07:12 +000072
73 PassRegistry &PR = *PassRegistry::getPassRegistry();
Zvi Rackover76dbf262016-11-15 06:34:33 +000074 initializeGlobalISel(PR);
David Majnemer0ad363e2015-08-18 19:07:12 +000075 initializeWinEHStatePassPass(PR);
Ahmed Bougacha068ac4a2016-05-07 01:11:10 +000076 initializeFixupBWInstPassPass(PR);
Gadi Haber19c4fc52016-12-28 10:12:48 +000077 initializeEvexToVexInstPassPass(PR);
Lama Saba2ea271b2017-05-18 08:11:50 +000078 initializeFixupLEAPassPass(PR);
Zvi Rackoverc6d0b6c2017-10-24 05:47:07 +000079 initializeX86CallFrameOptimizationPass(PR);
Amjad Aboud8ef85a02017-10-02 21:46:37 +000080 initializeX86CmovConverterPassPass(PR);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000081 initializeX86ExecutionDomainFixPass(PR);
Guy Blank92d5ce32017-10-22 11:43:08 +000082 initializeX86DomainReassignmentPass(PR);
Daniel Dunbare8338102009-07-15 20:24:03 +000083}
Douglas Gregor1b731d52009-06-16 20:12:29 +000084
Aditya Nandakumara2719322014-11-13 09:26:31 +000085static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
86 if (TT.isOSBinFormatMachO()) {
87 if (TT.getArch() == Triple::x86_64)
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000088 return llvm::make_unique<X86_64MachoTargetObjectFile>();
89 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000090 }
91
Davide Italianoa9f85d62016-09-26 22:53:15 +000092 if (TT.isOSFreeBSD())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000093 return llvm::make_unique<X86FreeBSDTargetObjectFile>();
Nikolai Bozhenovb7bf3862017-05-09 10:14:03 +000094 if (TT.isOSLinux() || TT.isOSNaCl() || TT.isOSIAMCU())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000095 return llvm::make_unique<X86LinuxNaClTargetObjectFile>();
Davide Italiano9b8e3d32017-06-21 20:36:32 +000096 if (TT.isOSSolaris())
97 return llvm::make_unique<X86SolarisTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000098 if (TT.isOSFuchsia())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000099 return llvm::make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000100 if (TT.isOSBinFormatELF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000101 return llvm::make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +0000102 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000103 return llvm::make_unique<X86WindowsTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000104 if (TT.isOSBinFormatCOFF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000105 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000106 llvm_unreachable("unknown subtarget type");
107}
108
Eric Christopher8b770652015-01-26 19:03:15 +0000109static std::string computeDataLayout(const Triple &TT) {
110 // X86 is little endian
111 std::string Ret = "e";
112
113 Ret += DataLayout::getManglingComponent(TT);
114 // X86 and x32 have 32 bit pointers.
115 if ((TT.isArch64Bit() &&
116 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
117 !TT.isArch64Bit())
118 Ret += "-p:32:32";
119
120 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
121 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
122 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000123 else if (TT.isOSIAMCU())
124 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +0000125 else
126 Ret += "-f64:32:64";
127
128 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000129 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +0000130 ; // No f80
131 else if (TT.isArch64Bit() || TT.isOSDarwin())
132 Ret += "-f80:128";
133 else
134 Ret += "-f80:32";
135
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000136 if (TT.isOSIAMCU())
137 Ret += "-f128:32";
138
Eric Christopher8b770652015-01-26 19:03:15 +0000139 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
140 if (TT.isArch64Bit())
141 Ret += "-n8:16:32:64";
142 else
143 Ret += "-n8:16:32";
144
145 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000146 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000147 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000148 else
149 Ret += "-S128";
150
151 return Ret;
152}
153
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000154static Reloc::Model getEffectiveRelocModel(const Triple &TT,
155 Optional<Reloc::Model> RM) {
156 bool is64Bit = TT.getArch() == Triple::x86_64;
157 if (!RM.hasValue()) {
158 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
159 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
160 // use static relocation model by default.
161 if (TT.isOSDarwin()) {
162 if (is64Bit)
163 return Reloc::PIC_;
164 return Reloc::DynamicNoPIC;
165 }
166 if (TT.isOSWindows() && is64Bit)
167 return Reloc::PIC_;
168 return Reloc::Static;
169 }
170
171 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
172 // is defined as a model for code which may be used in static or dynamic
173 // executables but not necessarily a shared library. On X86-32 we just
174 // compile in -static mode, in x86-64 we use PIC.
175 if (*RM == Reloc::DynamicNoPIC) {
176 if (is64Bit)
177 return Reloc::PIC_;
178 if (!TT.isOSDarwin())
179 return Reloc::Static;
180 }
181
182 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
183 // the Mach-O file format doesn't support it.
184 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
185 return Reloc::PIC_;
186
187 return *RM;
188}
189
Rafael Espindola79e238a2017-08-03 02:16:21 +0000190static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
191 bool JIT, bool Is64Bit) {
192 if (CM)
193 return *CM;
194 if (JIT)
195 return Is64Bit ? CodeModel::Large : CodeModel::Small;
196 return CodeModel::Small;
197}
198
Rafael Espindola38af4d62016-05-18 16:00:24 +0000199/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000200///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000201X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
202 StringRef CPU, StringRef FS,
203 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000204 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000205 Optional<CodeModel::Model> CM,
206 CodeGenOpt::Level OL, bool JIT)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000207 : LLVMTargetMachine(
Rafael Espindola79e238a2017-08-03 02:16:21 +0000208 T, computeDataLayout(TT), TT, CPU, FS, Options,
209 getEffectiveRelocModel(TT, RM),
210 getEffectiveCodeModel(CM, JIT, TT.getArch() == Triple::x86_64), OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000211 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000212 // Windows stack unwinder gets confused when execution flow "falls through"
213 // after a call to 'noreturn' function.
214 // To prevent that, we emit a trap for 'unreachable' IR instructions.
215 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000216 // On PS4, the "return address" of a 'noreturn' call must still be within
217 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000218 // The check here for 64-bit windows is a bit icky, but as we're unlikely
219 // to ever want to mix 32 and 64-bit windows code in a single module
220 // this should be fine.
221 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000222 this->Options.TrapUnreachable = true;
223
David Woodhouse1c3996a2014-01-08 00:08:50 +0000224 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000225}
Chris Lattner02a3d832002-10-29 22:37:54 +0000226
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000227X86TargetMachine::~X86TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000228
Eric Christopher3faf2f12014-10-06 06:45:36 +0000229const X86Subtarget *
230X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000231 Attribute CPUAttr = F.getFnAttribute("target-cpu");
232 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000233
David Majnemer498f2fd2016-05-20 20:41:24 +0000234 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
235 ? CPUAttr.getValueAsString()
236 : (StringRef)TargetCPU;
237 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
238 ? FSAttr.getValueAsString()
239 : (StringRef)TargetFS;
240
241 SmallString<512> Key;
242 Key.reserve(CPU.size() + FS.size());
243 Key += CPU;
244 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000245
246 // FIXME: This is related to the code below to reset the target options,
247 // we need to know whether or not the soft float flag is set on the
248 // function before we can generate a subtarget. We also need to use
249 // it as a key for the subtarget since that can be the only difference
250 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000251 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000252 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
253 // If the soft float attribute is set on the function turn on the soft float
254 // subtarget feature.
255 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000256 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000257
Craig Topper0d797a32018-01-20 00:26:08 +0000258 // Keep track of the key width after all features are added so we can extract
259 // the feature string out later.
260 unsigned CPUFSWidth = Key.size();
261
262 // Translate vector width function attribute into subtarget features. This
263 // overrides any CPU specific turning parameter
264 unsigned PreferVectorWidthOverride = 0;
265 if (F.hasFnAttribute("prefer-vector-width")) {
266 StringRef Val = F.getFnAttribute("prefer-vector-width").getValueAsString();
267 unsigned Width;
268 if (!Val.getAsInteger(0, Width)) {
269 Key += ",prefer-vector-width=";
270 Key += Val;
271 PreferVectorWidthOverride = Width;
272 }
273 }
274
275 FS = Key.slice(CPU.size(), CPUFSWidth);
David Majnemerca290232016-05-20 18:16:06 +0000276
277 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000278 if (!I) {
279 // This needs to be done before we create a new subtarget since any
280 // creation will depend on the TM and the code generation flags on the
281 // function that reside in TargetOptions.
282 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000283 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Craig Topper0d797a32018-01-20 00:26:08 +0000284 Options.StackAlignmentOverride,
285 PreferVectorWidthOverride);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000286 }
287 return I.get();
288}
289
Chris Lattner12e97302006-09-04 04:14:57 +0000290//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000291// Command line options for x86
292//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000293static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000294UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000295 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000296 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000297
298//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000299// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000300//===----------------------------------------------------------------------===//
301
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000302TargetTransformInfo
303X86TargetMachine::getTargetTransformInfo(const Function &F) {
304 return TargetTransformInfo(X86TTIImpl(this, F));
Chandler Carruth664e3542013-01-07 01:37:14 +0000305}
306
Chandler Carruth664e3542013-01-07 01:37:14 +0000307//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000308// Pass Pipeline Configuration
309//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000310
Andrew Trickccb67362012-02-03 05:12:41 +0000311namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000312
Andrew Trickccb67362012-02-03 05:12:41 +0000313/// X86 Code Generator Pass Configuration Options.
314class X86PassConfig : public TargetPassConfig {
315public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000316 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000317 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000318
319 X86TargetMachine &getX86TargetMachine() const {
320 return getTM<X86TargetMachine>();
321 }
322
Matthias Braun115efcd2016-11-28 20:11:54 +0000323 ScheduleDAGInstrs *
324 createMachineScheduler(MachineSchedContext *C) const override {
325 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
Evandro Menezes94edf022017-02-01 02:54:34 +0000326 DAG->addMutation(createX86MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000327 return DAG;
328 }
329
Tim Northover277066a2014-07-01 18:53:31 +0000330 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000331 bool addInstSelector() override;
Zvi Rackover76dbf262016-11-15 06:34:33 +0000332 bool addIRTranslator() override;
333 bool addLegalizeMachineIR() override;
334 bool addRegBankSelect() override;
335 bool addGlobalInstructionSelect() override;
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000336 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000337 bool addPreISel() override;
Guy Blank92d5ce32017-10-22 11:43:08 +0000338 void addMachineSSAOptimization() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000339 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000340 void addPostRegAlloc() override;
341 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000342 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000343};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000344
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000345class X86ExecutionDomainFix : public ExecutionDomainFix {
Matthias Braune6ff30b2017-03-18 05:08:58 +0000346public:
347 static char ID;
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000348 X86ExecutionDomainFix() : ExecutionDomainFix(ID, X86::VR128XRegClass) {}
Matthias Braune6ff30b2017-03-18 05:08:58 +0000349 StringRef getPassName() const override {
350 return "X86 Execution Dependency Fix";
351 }
352};
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000353char X86ExecutionDomainFix::ID;
Matthias Braune6ff30b2017-03-18 05:08:58 +0000354
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000355} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000356
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000357INITIALIZE_PASS_BEGIN(X86ExecutionDomainFix, "x86-execution-domain-fix",
358 "X86 Execution Domain Fix", false, false)
359INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
360INITIALIZE_PASS_END(X86ExecutionDomainFix, "x86-execution-domain-fix",
361 "X86 Execution Domain Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000362
Andrew Trickf8ea1082012-02-04 02:56:59 +0000363TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000364 return new X86PassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000365}
366
Tim Northover277066a2014-07-01 18:53:31 +0000367void X86PassConfig::addIRPasses() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000368 addPass(createAtomicExpandPass());
Tim Northover277066a2014-07-01 18:53:31 +0000369
370 TargetPassConfig::addIRPasses();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000371
372 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000373 addPass(createInterleavedAccessPass());
Tim Northover277066a2014-07-01 18:53:31 +0000374}
375
Andrew Trickccb67362012-02-03 05:12:41 +0000376bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000377 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000378 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000379
Hans Wennborg789acfb2012-06-01 16:27:21 +0000380 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000381 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000382 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000383 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000384
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000385 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000386 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000387}
388
Zvi Rackover76dbf262016-11-15 06:34:33 +0000389bool X86PassConfig::addIRTranslator() {
390 addPass(new IRTranslator());
391 return false;
392}
393
394bool X86PassConfig::addLegalizeMachineIR() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000395 addPass(new Legalizer());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000396 return false;
397}
398
399bool X86PassConfig::addRegBankSelect() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000400 addPass(new RegBankSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000401 return false;
402}
403
404bool X86PassConfig::addGlobalInstructionSelect() {
Igor Bregerf7359d82017-02-22 12:25:09 +0000405 addPass(new InstructionSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000406 return false;
407}
Zvi Rackover76dbf262016-11-15 06:34:33 +0000408
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000409bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000410 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000411 if (EnableMachineCombinerPass)
412 addPass(&MachineCombinerID);
Amjad Aboud4563c062017-07-16 17:39:56 +0000413 addPass(createX86CmovConverterPass());
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000414 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000415}
416
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000417bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000418 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000419 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000420 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000421 addPass(createX86WinEHStatePass());
422 return true;
423}
424
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000425void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000426 if (getOptLevel() != CodeGenOpt::None) {
Dehao Chen6b737dd2017-05-31 23:25:25 +0000427 addPass(&LiveRangeShrinkID);
Nico Weberead8f8f2016-07-14 15:07:44 +0000428 addPass(createX86FixupSetCC());
429 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000430 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000431 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000432
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000433 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000434}
Guy Blank92d5ce32017-10-22 11:43:08 +0000435void X86PassConfig::addMachineSSAOptimization() {
436 addPass(createX86DomainReassignmentPass());
437 TargetPassConfig::addMachineSSAOptimization();
438}
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000439
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000440void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000441 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000442}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000443
Quentin Colombet494eb602015-05-22 18:10:47 +0000444void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
445
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000446void X86PassConfig::addPreEmitPass() {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000447 if (getOptLevel() != CodeGenOpt::None) {
448 addPass(new X86ExecutionDomainFix());
Marina Yatsina0bf841a2018-01-22 10:06:50 +0000449 addPass(createBreakFalseDeps());
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000450 }
Rafael Espindola01c73612014-12-11 20:03:57 +0000451
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000452 addPass(createX86IndirectBranchTrackingPass());
453
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}