xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/X86/X86TargetMachine.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the X86 specific subclass of TargetMachine.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "X86TargetMachine.h"
140b57cec5SDimitry Andric #include "MCTargetDesc/X86MCTargetDesc.h"
150b57cec5SDimitry Andric #include "TargetInfo/X86TargetInfo.h"
160b57cec5SDimitry Andric #include "X86.h"
17bdd1243dSDimitry Andric #include "X86MachineFunctionInfo.h"
180b57cec5SDimitry Andric #include "X86MacroFusion.h"
190b57cec5SDimitry Andric #include "X86Subtarget.h"
200b57cec5SDimitry Andric #include "X86TargetObjectFile.h"
210b57cec5SDimitry Andric #include "X86TargetTransformInfo.h"
220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/ExecutionDomainFix.h"
2781ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/CallLowering.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
3181ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
34*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
35*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MIRYamlMapping.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
3881ad6265SDimitry Andric #include "llvm/CodeGen/RegAllocRegistry.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
400b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
410b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
420b57cec5SDimitry Andric #include "llvm/IR/Function.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
44349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
450b57cec5SDimitry Andric #include "llvm/Pass.h"
460b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
470b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
480b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
490b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
500b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
5106c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
52480093f4SDimitry Andric #include "llvm/Transforms/CFGuard.h"
530b57cec5SDimitry Andric #include <memory>
54bdd1243dSDimitry Andric #include <optional>
550b57cec5SDimitry Andric #include <string>
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric using namespace llvm;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
600b57cec5SDimitry Andric                                cl::desc("Enable the machine combiner pass"),
610b57cec5SDimitry Andric                                cl::init(true), cl::Hidden);
620b57cec5SDimitry Andric 
6381ad6265SDimitry Andric static cl::opt<bool>
6481ad6265SDimitry Andric     EnableTileRAPass("x86-tile-ra",
6581ad6265SDimitry Andric                      cl::desc("Enable the tile register allocation pass"),
6681ad6265SDimitry Andric                      cl::init(true), cl::Hidden);
6781ad6265SDimitry Andric 
68480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86Target() {
690b57cec5SDimitry Andric   // Register the target.
700b57cec5SDimitry Andric   RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target());
710b57cec5SDimitry Andric   RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target());
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   PassRegistry &PR = *PassRegistry::getPassRegistry();
74fe6060f1SDimitry Andric   initializeX86LowerAMXIntrinsicsLegacyPassPass(PR);
75e8d8bef9SDimitry Andric   initializeX86LowerAMXTypeLegacyPassPass(PR);
7681ad6265SDimitry Andric   initializeX86PreTileConfigPass(PR);
770b57cec5SDimitry Andric   initializeGlobalISel(PR);
780b57cec5SDimitry Andric   initializeWinEHStatePassPass(PR);
790b57cec5SDimitry Andric   initializeFixupBWInstPassPass(PR);
801db9f3b2SDimitry Andric   initializeCompressEVEXPassPass(PR);
810b57cec5SDimitry Andric   initializeFixupLEAPassPass(PR);
820b57cec5SDimitry Andric   initializeFPSPass(PR);
835ffd83dbSDimitry Andric   initializeX86FixupSetCCPassPass(PR);
840b57cec5SDimitry Andric   initializeX86CallFrameOptimizationPass(PR);
850b57cec5SDimitry Andric   initializeX86CmovConverterPassPass(PR);
86e8d8bef9SDimitry Andric   initializeX86TileConfigPass(PR);
8781ad6265SDimitry Andric   initializeX86FastPreTileConfigPass(PR);
88fe6060f1SDimitry Andric   initializeX86FastTileConfigPass(PR);
8906c3fb27SDimitry Andric   initializeKCFIPass(PR);
90fe6060f1SDimitry Andric   initializeX86LowerTileCopyPass(PR);
910b57cec5SDimitry Andric   initializeX86ExpandPseudoPass(PR);
920b57cec5SDimitry Andric   initializeX86ExecutionDomainFixPass(PR);
930b57cec5SDimitry Andric   initializeX86DomainReassignmentPass(PR);
940b57cec5SDimitry Andric   initializeX86AvoidSFBPassPass(PR);
955ffd83dbSDimitry Andric   initializeX86AvoidTrailingCallPassPass(PR);
960b57cec5SDimitry Andric   initializeX86SpeculativeLoadHardeningPassPass(PR);
975ffd83dbSDimitry Andric   initializeX86SpeculativeExecutionSideEffectSuppressionPass(PR);
980b57cec5SDimitry Andric   initializeX86FlagsCopyLoweringPassPass(PR);
990946e70aSDimitry Andric   initializeX86LoadValueInjectionLoadHardeningPassPass(PR);
1000946e70aSDimitry Andric   initializeX86LoadValueInjectionRetHardeningPassPass(PR);
1018bcb0991SDimitry Andric   initializeX86OptimizeLEAPassPass(PR);
1025ffd83dbSDimitry Andric   initializeX86PartialReductionPass(PR);
103e8d8bef9SDimitry Andric   initializePseudoProbeInserterPass(PR);
104753f127fSDimitry Andric   initializeX86ReturnThunksPass(PR);
105*0fca6ea1SDimitry Andric   initializeX86DAGToDAGISelLegacyPass(PR);
10606c3fb27SDimitry Andric   initializeX86ArgumentStackSlotPassPass(PR);
107*0fca6ea1SDimitry Andric   initializeX86FixupInstTuningPassPass(PR);
108*0fca6ea1SDimitry Andric   initializeX86FixupVectorConstantsPassPass(PR);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
1120b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO()) {
1130b57cec5SDimitry Andric     if (TT.getArch() == Triple::x86_64)
1148bcb0991SDimitry Andric       return std::make_unique<X86_64MachoTargetObjectFile>();
1158bcb0991SDimitry Andric     return std::make_unique<TargetLoweringObjectFileMachO>();
1160b57cec5SDimitry Andric   }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
1198bcb0991SDimitry Andric     return std::make_unique<TargetLoweringObjectFileCOFF>();
1207a6dacacSDimitry Andric 
1217a6dacacSDimitry Andric   if (TT.getArch() == Triple::x86_64)
1227a6dacacSDimitry Andric     return std::make_unique<X86_64ELFTargetObjectFile>();
1235ffd83dbSDimitry Andric   return std::make_unique<X86ELFTargetObjectFile>();
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT) {
1270b57cec5SDimitry Andric   // X86 is little endian
1280b57cec5SDimitry Andric   std::string Ret = "e";
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   Ret += DataLayout::getManglingComponent(TT);
1310b57cec5SDimitry Andric   // X86 and x32 have 32 bit pointers.
132fe6060f1SDimitry Andric   if (!TT.isArch64Bit() || TT.isX32() || TT.isOSNaCl())
1330b57cec5SDimitry Andric     Ret += "-p:32:32";
1340b57cec5SDimitry Andric 
1358bcb0991SDimitry Andric   // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
1368bcb0991SDimitry Andric   Ret += "-p270:32:32-p271:32:32-p272:64:64";
1378bcb0991SDimitry Andric 
1380b57cec5SDimitry Andric   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
1395f757f3fSDimitry Andric   // 128 bit integers are not specified in the 32-bit ABIs but are used
1405f757f3fSDimitry Andric   // internally for lowering f128, so we match the alignment to that.
1410b57cec5SDimitry Andric   if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
1425f757f3fSDimitry Andric     Ret += "-i64:64-i128:128";
1430b57cec5SDimitry Andric   else if (TT.isOSIAMCU())
1440b57cec5SDimitry Andric     Ret += "-i64:32-f64:32";
1450b57cec5SDimitry Andric   else
1465f757f3fSDimitry Andric     Ret += "-i128:128-f64:32:64";
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // Some ABIs align long double to 128 bits, others to 32.
1490b57cec5SDimitry Andric   if (TT.isOSNaCl() || TT.isOSIAMCU())
1500b57cec5SDimitry Andric     ; // No f80
15104eeddc0SDimitry Andric   else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
1520b57cec5SDimitry Andric     Ret += "-f80:128";
1530b57cec5SDimitry Andric   else
1540b57cec5SDimitry Andric     Ret += "-f80:32";
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   if (TT.isOSIAMCU())
1570b57cec5SDimitry Andric     Ret += "-f128:32";
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
1600b57cec5SDimitry Andric   if (TT.isArch64Bit())
1610b57cec5SDimitry Andric     Ret += "-n8:16:32:64";
1620b57cec5SDimitry Andric   else
1630b57cec5SDimitry Andric     Ret += "-n8:16:32";
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
1660b57cec5SDimitry Andric   if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
1670b57cec5SDimitry Andric     Ret += "-a:0:32-S32";
1680b57cec5SDimitry Andric   else
1690b57cec5SDimitry Andric     Ret += "-S128";
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   return Ret;
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
174bdd1243dSDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT, bool JIT,
175bdd1243dSDimitry Andric                                            std::optional<Reloc::Model> RM) {
1760b57cec5SDimitry Andric   bool is64Bit = TT.getArch() == Triple::x86_64;
17781ad6265SDimitry Andric   if (!RM) {
1780b57cec5SDimitry Andric     // JIT codegen should use static relocations by default, since it's
1790b57cec5SDimitry Andric     // typically executed in process and not relocatable.
1800b57cec5SDimitry Andric     if (JIT)
1810b57cec5SDimitry Andric       return Reloc::Static;
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric     // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
1840b57cec5SDimitry Andric     // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
1850b57cec5SDimitry Andric     // use static relocation model by default.
1860b57cec5SDimitry Andric     if (TT.isOSDarwin()) {
1870b57cec5SDimitry Andric       if (is64Bit)
1880b57cec5SDimitry Andric         return Reloc::PIC_;
1890b57cec5SDimitry Andric       return Reloc::DynamicNoPIC;
1900b57cec5SDimitry Andric     }
1910b57cec5SDimitry Andric     if (TT.isOSWindows() && is64Bit)
1920b57cec5SDimitry Andric       return Reloc::PIC_;
1930b57cec5SDimitry Andric     return Reloc::Static;
1940b57cec5SDimitry Andric   }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
1970b57cec5SDimitry Andric   // is defined as a model for code which may be used in static or dynamic
1980b57cec5SDimitry Andric   // executables but not necessarily a shared library. On X86-32 we just
1990b57cec5SDimitry Andric   // compile in -static mode, in x86-64 we use PIC.
2000b57cec5SDimitry Andric   if (*RM == Reloc::DynamicNoPIC) {
2010b57cec5SDimitry Andric     if (is64Bit)
2020b57cec5SDimitry Andric       return Reloc::PIC_;
2030b57cec5SDimitry Andric     if (!TT.isOSDarwin())
2040b57cec5SDimitry Andric       return Reloc::Static;
2050b57cec5SDimitry Andric   }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   // If we are on Darwin, disallow static relocation model in X86-64 mode, since
2080b57cec5SDimitry Andric   // the Mach-O file format doesn't support it.
2090b57cec5SDimitry Andric   if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
2100b57cec5SDimitry Andric     return Reloc::PIC_;
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   return *RM;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
215bdd1243dSDimitry Andric static CodeModel::Model
216*0fca6ea1SDimitry Andric getEffectiveX86CodeModel(const Triple &TT, std::optional<CodeModel::Model> CM,
217*0fca6ea1SDimitry Andric                          bool JIT) {
218*0fca6ea1SDimitry Andric   bool Is64Bit = TT.getArch() == Triple::x86_64;
2190b57cec5SDimitry Andric   if (CM) {
2200b57cec5SDimitry Andric     if (*CM == CodeModel::Tiny)
2210b57cec5SDimitry Andric       report_fatal_error("Target does not support the tiny CodeModel", false);
2220b57cec5SDimitry Andric     return *CM;
2230b57cec5SDimitry Andric   }
2240b57cec5SDimitry Andric   if (JIT)
2250b57cec5SDimitry Andric     return Is64Bit ? CodeModel::Large : CodeModel::Small;
2260b57cec5SDimitry Andric   return CodeModel::Small;
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric /// Create an X86 target.
2300b57cec5SDimitry Andric ///
2310b57cec5SDimitry Andric X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
2320b57cec5SDimitry Andric                                    StringRef CPU, StringRef FS,
2330b57cec5SDimitry Andric                                    const TargetOptions &Options,
234bdd1243dSDimitry Andric                                    std::optional<Reloc::Model> RM,
235bdd1243dSDimitry Andric                                    std::optional<CodeModel::Model> CM,
2365f757f3fSDimitry Andric                                    CodeGenOptLevel OL, bool JIT)
2370b57cec5SDimitry Andric     : LLVMTargetMachine(
2380b57cec5SDimitry Andric           T, computeDataLayout(TT), TT, CPU, FS, Options,
2390b57cec5SDimitry Andric           getEffectiveRelocModel(TT, JIT, RM),
240*0fca6ea1SDimitry Andric           getEffectiveX86CodeModel(TT, CM, JIT),
2410b57cec5SDimitry Andric           OL),
242d65cd7a5SDimitry Andric       TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) {
24381ad6265SDimitry Andric   // On PS4/PS5, the "return address" of a 'noreturn' call must still be within
2440b57cec5SDimitry Andric   // the calling function, and TrapUnreachable is an easy way to get that.
24581ad6265SDimitry Andric   if (TT.isPS() || TT.isOSBinFormatMachO()) {
2460b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
2470b57cec5SDimitry Andric     this->Options.NoTrapAfterNoreturn = TT.isOSBinFormatMachO();
2480b57cec5SDimitry Andric   }
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   setMachineOutliner(true);
2510b57cec5SDimitry Andric 
2525ffd83dbSDimitry Andric   // x86 supports the debug entry values.
2535ffd83dbSDimitry Andric   setSupportsDebugEntryValues(true);
2545ffd83dbSDimitry Andric 
2550b57cec5SDimitry Andric   initAsmInfo();
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric X86TargetMachine::~X86TargetMachine() = default;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric const X86Subtarget *
2610b57cec5SDimitry Andric X86TargetMachine::getSubtargetImpl(const Function &F) const {
2620b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
263e8d8bef9SDimitry Andric   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
2640b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
2650b57cec5SDimitry Andric 
266e8d8bef9SDimitry Andric   StringRef CPU =
267e8d8bef9SDimitry Andric       CPUAttr.isValid() ? CPUAttr.getValueAsString() : (StringRef)TargetCPU;
268fcaf7f86SDimitry Andric   // "x86-64" is a default target setting for many front ends. In these cases,
269fcaf7f86SDimitry Andric   // they actually request for "generic" tuning unless the "tune-cpu" was
270fcaf7f86SDimitry Andric   // specified.
271fcaf7f86SDimitry Andric   StringRef TuneCPU = TuneAttr.isValid() ? TuneAttr.getValueAsString()
272fcaf7f86SDimitry Andric                       : CPU == "x86-64"  ? "generic"
273fcaf7f86SDimitry Andric                                          : (StringRef)CPU;
274e8d8bef9SDimitry Andric   StringRef FS =
275e8d8bef9SDimitry Andric       FSAttr.isValid() ? FSAttr.getValueAsString() : (StringRef)TargetFS;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   SmallString<512> Key;
278e8d8bef9SDimitry Andric   // The additions here are ordered so that the definitely short strings are
279e8d8bef9SDimitry Andric   // added first so we won't exceed the small size. We append the
280e8d8bef9SDimitry Andric   // much longer FS string at the end so that we only heap allocate at most
281e8d8bef9SDimitry Andric   // one time.
282e8d8bef9SDimitry Andric 
283e8d8bef9SDimitry Andric   // Extract prefer-vector-width attribute.
284e8d8bef9SDimitry Andric   unsigned PreferVectorWidthOverride = 0;
285e8d8bef9SDimitry Andric   Attribute PreferVecWidthAttr = F.getFnAttribute("prefer-vector-width");
286e8d8bef9SDimitry Andric   if (PreferVecWidthAttr.isValid()) {
287e8d8bef9SDimitry Andric     StringRef Val = PreferVecWidthAttr.getValueAsString();
288e8d8bef9SDimitry Andric     unsigned Width;
289e8d8bef9SDimitry Andric     if (!Val.getAsInteger(0, Width)) {
290fe6060f1SDimitry Andric       Key += 'p';
291e8d8bef9SDimitry Andric       Key += Val;
292e8d8bef9SDimitry Andric       PreferVectorWidthOverride = Width;
293e8d8bef9SDimitry Andric     }
294e8d8bef9SDimitry Andric   }
295e8d8bef9SDimitry Andric 
296e8d8bef9SDimitry Andric   // Extract min-legal-vector-width attribute.
297e8d8bef9SDimitry Andric   unsigned RequiredVectorWidth = UINT32_MAX;
298e8d8bef9SDimitry Andric   Attribute MinLegalVecWidthAttr = F.getFnAttribute("min-legal-vector-width");
299e8d8bef9SDimitry Andric   if (MinLegalVecWidthAttr.isValid()) {
300e8d8bef9SDimitry Andric     StringRef Val = MinLegalVecWidthAttr.getValueAsString();
301e8d8bef9SDimitry Andric     unsigned Width;
302e8d8bef9SDimitry Andric     if (!Val.getAsInteger(0, Width)) {
303fe6060f1SDimitry Andric       Key += 'm';
304e8d8bef9SDimitry Andric       Key += Val;
305e8d8bef9SDimitry Andric       RequiredVectorWidth = Width;
306e8d8bef9SDimitry Andric     }
307e8d8bef9SDimitry Andric   }
308e8d8bef9SDimitry Andric 
309e8d8bef9SDimitry Andric   // Add CPU to the Key.
3100b57cec5SDimitry Andric   Key += CPU;
311e8d8bef9SDimitry Andric 
312e8d8bef9SDimitry Andric   // Add tune CPU to the Key.
313e8d8bef9SDimitry Andric   Key += TuneCPU;
314e8d8bef9SDimitry Andric 
315e8d8bef9SDimitry Andric   // Keep track of the start of the feature portion of the string.
316e8d8bef9SDimitry Andric   unsigned FSStart = Key.size();
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // FIXME: This is related to the code below to reset the target options,
3190b57cec5SDimitry Andric   // we need to know whether or not the soft float flag is set on the
3200b57cec5SDimitry Andric   // function before we can generate a subtarget. We also need to use
3210b57cec5SDimitry Andric   // it as a key for the subtarget since that can be the only difference
3220b57cec5SDimitry Andric   // between two functions.
323fe6060f1SDimitry Andric   bool SoftFloat = F.getFnAttribute("use-soft-float").getValueAsBool();
3240b57cec5SDimitry Andric   // If the soft float attribute is set on the function turn on the soft float
3250b57cec5SDimitry Andric   // subtarget feature.
3260b57cec5SDimitry Andric   if (SoftFloat)
327e8d8bef9SDimitry Andric     Key += FS.empty() ? "+soft-float" : "+soft-float,";
3280b57cec5SDimitry Andric 
329e8d8bef9SDimitry Andric   Key += FS;
3300b57cec5SDimitry Andric 
331e8d8bef9SDimitry Andric   // We may have added +soft-float to the features so move the StringRef to
332e8d8bef9SDimitry Andric   // point to the full string in the Key.
333e8d8bef9SDimitry Andric   FS = Key.substr(FSStart);
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   auto &I = SubtargetMap[Key];
3360b57cec5SDimitry Andric   if (!I) {
3370b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
3380b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
3390b57cec5SDimitry Andric     // function that reside in TargetOptions.
3400b57cec5SDimitry Andric     resetTargetOptions(F);
3418bcb0991SDimitry Andric     I = std::make_unique<X86Subtarget>(
342e8d8bef9SDimitry Andric         TargetTriple, CPU, TuneCPU, FS, *this,
343fe6060f1SDimitry Andric         MaybeAlign(F.getParent()->getOverrideStackAlignment()),
344fe6060f1SDimitry Andric         PreferVectorWidthOverride, RequiredVectorWidth);
3450b57cec5SDimitry Andric   }
3460b57cec5SDimitry Andric   return I.get();
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
349*0fca6ea1SDimitry Andric yaml::MachineFunctionInfo *X86TargetMachine::createDefaultFuncInfoYAML() const {
350*0fca6ea1SDimitry Andric   return new yaml::X86MachineFunctionInfo();
351*0fca6ea1SDimitry Andric }
352*0fca6ea1SDimitry Andric 
353*0fca6ea1SDimitry Andric yaml::MachineFunctionInfo *
354*0fca6ea1SDimitry Andric X86TargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
355*0fca6ea1SDimitry Andric   const auto *MFI = MF.getInfo<X86MachineFunctionInfo>();
356*0fca6ea1SDimitry Andric   return new yaml::X86MachineFunctionInfo(*MFI);
357*0fca6ea1SDimitry Andric }
358*0fca6ea1SDimitry Andric 
359*0fca6ea1SDimitry Andric bool X86TargetMachine::parseMachineFunctionInfo(
360*0fca6ea1SDimitry Andric     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
361*0fca6ea1SDimitry Andric     SMDiagnostic &Error, SMRange &SourceRange) const {
362*0fca6ea1SDimitry Andric   const auto &YamlMFI = static_cast<const yaml::X86MachineFunctionInfo &>(MFI);
363*0fca6ea1SDimitry Andric   PFS.MF.getInfo<X86MachineFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
364*0fca6ea1SDimitry Andric   return false;
365*0fca6ea1SDimitry Andric }
366*0fca6ea1SDimitry Andric 
367e8d8bef9SDimitry Andric bool X86TargetMachine::isNoopAddrSpaceCast(unsigned SrcAS,
368e8d8bef9SDimitry Andric                                            unsigned DestAS) const {
369e8d8bef9SDimitry Andric   assert(SrcAS != DestAS && "Expected different address spaces!");
370e8d8bef9SDimitry Andric   if (getPointerSize(SrcAS) != getPointerSize(DestAS))
371e8d8bef9SDimitry Andric     return false;
372e8d8bef9SDimitry Andric   return SrcAS < 256 && DestAS < 256;
373e8d8bef9SDimitry Andric }
374e8d8bef9SDimitry Andric 
3750b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3760b57cec5SDimitry Andric // X86 TTI query.
3770b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric TargetTransformInfo
38081ad6265SDimitry Andric X86TargetMachine::getTargetTransformInfo(const Function &F) const {
3810b57cec5SDimitry Andric   return TargetTransformInfo(X86TTIImpl(this, F));
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3850b57cec5SDimitry Andric // Pass Pipeline Configuration
3860b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric namespace {
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric /// X86 Code Generator Pass Configuration Options.
3910b57cec5SDimitry Andric class X86PassConfig : public TargetPassConfig {
3920b57cec5SDimitry Andric public:
3930b57cec5SDimitry Andric   X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM)
3940b57cec5SDimitry Andric     : TargetPassConfig(TM, PM) {}
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   X86TargetMachine &getX86TargetMachine() const {
3970b57cec5SDimitry Andric     return getTM<X86TargetMachine>();
3980b57cec5SDimitry Andric   }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric   ScheduleDAGInstrs *
4010b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
4020b57cec5SDimitry Andric     ScheduleDAGMILive *DAG = createGenericSchedLive(C);
4030b57cec5SDimitry Andric     DAG->addMutation(createX86MacroFusionDAGMutation());
4040b57cec5SDimitry Andric     return DAG;
4050b57cec5SDimitry Andric   }
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   ScheduleDAGInstrs *
4080b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
4090b57cec5SDimitry Andric     ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
4100b57cec5SDimitry Andric     DAG->addMutation(createX86MacroFusionDAGMutation());
4110b57cec5SDimitry Andric     return DAG;
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   void addIRPasses() override;
4150b57cec5SDimitry Andric   bool addInstSelector() override;
4160b57cec5SDimitry Andric   bool addIRTranslator() override;
4170b57cec5SDimitry Andric   bool addLegalizeMachineIR() override;
4180b57cec5SDimitry Andric   bool addRegBankSelect() override;
4190b57cec5SDimitry Andric   bool addGlobalInstructionSelect() override;
4200b57cec5SDimitry Andric   bool addILPOpts() override;
4210b57cec5SDimitry Andric   bool addPreISel() override;
4220b57cec5SDimitry Andric   void addMachineSSAOptimization() override;
4230b57cec5SDimitry Andric   void addPreRegAlloc() override;
424fe6060f1SDimitry Andric   bool addPostFastRegAllocRewrite() override;
4250b57cec5SDimitry Andric   void addPostRegAlloc() override;
4260b57cec5SDimitry Andric   void addPreEmitPass() override;
4270b57cec5SDimitry Andric   void addPreEmitPass2() override;
4280b57cec5SDimitry Andric   void addPreSched2() override;
42981ad6265SDimitry Andric   bool addRegAssignAndRewriteOptimized() override;
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric   std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
4320b57cec5SDimitry Andric };
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric class X86ExecutionDomainFix : public ExecutionDomainFix {
4350b57cec5SDimitry Andric public:
4360b57cec5SDimitry Andric   static char ID;
4370b57cec5SDimitry Andric   X86ExecutionDomainFix() : ExecutionDomainFix(ID, X86::VR128XRegClass) {}
4380b57cec5SDimitry Andric   StringRef getPassName() const override {
4390b57cec5SDimitry Andric     return "X86 Execution Dependency Fix";
4400b57cec5SDimitry Andric   }
4410b57cec5SDimitry Andric };
4420b57cec5SDimitry Andric char X86ExecutionDomainFix::ID;
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric } // end anonymous namespace
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(X86ExecutionDomainFix, "x86-execution-domain-fix",
4470b57cec5SDimitry Andric   "X86 Execution Domain Fix", false, false)
4480b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
4490b57cec5SDimitry Andric INITIALIZE_PASS_END(X86ExecutionDomainFix, "x86-execution-domain-fix",
4500b57cec5SDimitry Andric   "X86 Execution Domain Fix", false, false)
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
4530b57cec5SDimitry Andric   return new X86PassConfig(*this, PM);
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric 
456bdd1243dSDimitry Andric MachineFunctionInfo *X86TargetMachine::createMachineFunctionInfo(
457bdd1243dSDimitry Andric     BumpPtrAllocator &Allocator, const Function &F,
458bdd1243dSDimitry Andric     const TargetSubtargetInfo *STI) const {
459bdd1243dSDimitry Andric   return X86MachineFunctionInfo::create<X86MachineFunctionInfo>(Allocator, F,
460bdd1243dSDimitry Andric                                                                 STI);
461bdd1243dSDimitry Andric }
462bdd1243dSDimitry Andric 
4630b57cec5SDimitry Andric void X86PassConfig::addIRPasses() {
464*0fca6ea1SDimitry Andric   addPass(createAtomicExpandLegacyPass());
465fe6060f1SDimitry Andric 
466fe6060f1SDimitry Andric   // We add both pass anyway and when these two passes run, we skip the pass
467fe6060f1SDimitry Andric   // based on the option level and option attribute.
468fe6060f1SDimitry Andric   addPass(createX86LowerAMXIntrinsicsPass());
469e8d8bef9SDimitry Andric   addPass(createX86LowerAMXTypePass());
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
4720b57cec5SDimitry Andric 
4735f757f3fSDimitry Andric   if (TM->getOptLevel() != CodeGenOptLevel::None) {
4740b57cec5SDimitry Andric     addPass(createInterleavedAccessPass());
4755ffd83dbSDimitry Andric     addPass(createX86PartialReductionPass());
4765ffd83dbSDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   // Add passes that handle indirect branch removal and insertion of a retpoline
4790b57cec5SDimitry Andric   // thunk. These will be a no-op unless a function subtarget has the retpoline
4800b57cec5SDimitry Andric   // feature enabled.
4810b57cec5SDimitry Andric   addPass(createIndirectBrExpandPass());
482480093f4SDimitry Andric 
483480093f4SDimitry Andric   // Add Control Flow Guard checks.
484480093f4SDimitry Andric   const Triple &TT = TM->getTargetTriple();
485480093f4SDimitry Andric   if (TT.isOSWindows()) {
486480093f4SDimitry Andric     if (TT.getArch() == Triple::x86_64) {
487480093f4SDimitry Andric       addPass(createCFGuardDispatchPass());
488480093f4SDimitry Andric     } else {
489480093f4SDimitry Andric       addPass(createCFGuardCheckPass());
490480093f4SDimitry Andric     }
491480093f4SDimitry Andric   }
49281ad6265SDimitry Andric 
49381ad6265SDimitry Andric   if (TM->Options.JMCInstrument)
49481ad6265SDimitry Andric     addPass(createJMCInstrumenterPass());
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric bool X86PassConfig::addInstSelector() {
4980b57cec5SDimitry Andric   // Install an instruction selector.
4990b57cec5SDimitry Andric   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   // For ELF, cleanup any local-dynamic TLS accesses.
5020b57cec5SDimitry Andric   if (TM->getTargetTriple().isOSBinFormatELF() &&
5035f757f3fSDimitry Andric       getOptLevel() != CodeGenOptLevel::None)
5040b57cec5SDimitry Andric     addPass(createCleanupLocalDynamicTLSPass());
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   addPass(createX86GlobalBaseRegPass());
50706c3fb27SDimitry Andric   addPass(createX86ArgumentStackSlotPass());
5080b57cec5SDimitry Andric   return false;
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric bool X86PassConfig::addIRTranslator() {
512e8d8bef9SDimitry Andric   addPass(new IRTranslator(getOptLevel()));
5130b57cec5SDimitry Andric   return false;
5140b57cec5SDimitry Andric }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric bool X86PassConfig::addLegalizeMachineIR() {
5170b57cec5SDimitry Andric   addPass(new Legalizer());
5180b57cec5SDimitry Andric   return false;
5190b57cec5SDimitry Andric }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric bool X86PassConfig::addRegBankSelect() {
5220b57cec5SDimitry Andric   addPass(new RegBankSelect());
5230b57cec5SDimitry Andric   return false;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric bool X86PassConfig::addGlobalInstructionSelect() {
527fe6060f1SDimitry Andric   addPass(new InstructionSelect(getOptLevel()));
528*0fca6ea1SDimitry Andric   // Add GlobalBaseReg in case there is no SelectionDAG passes afterwards
529*0fca6ea1SDimitry Andric   if (isGlobalISelAbortEnabled())
530*0fca6ea1SDimitry Andric     addPass(createX86GlobalBaseRegPass());
5310b57cec5SDimitry Andric   return false;
5320b57cec5SDimitry Andric }
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric bool X86PassConfig::addILPOpts() {
5350b57cec5SDimitry Andric   addPass(&EarlyIfConverterID);
5360b57cec5SDimitry Andric   if (EnableMachineCombinerPass)
5370b57cec5SDimitry Andric     addPass(&MachineCombinerID);
5380b57cec5SDimitry Andric   addPass(createX86CmovConverterPass());
5390b57cec5SDimitry Andric   return true;
5400b57cec5SDimitry Andric }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric bool X86PassConfig::addPreISel() {
5430b57cec5SDimitry Andric   // Only add this pass for 32-bit x86 Windows.
5440b57cec5SDimitry Andric   const Triple &TT = TM->getTargetTriple();
5450b57cec5SDimitry Andric   if (TT.isOSWindows() && TT.getArch() == Triple::x86)
5460b57cec5SDimitry Andric     addPass(createX86WinEHStatePass());
5470b57cec5SDimitry Andric   return true;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric void X86PassConfig::addPreRegAlloc() {
5515f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None) {
5520b57cec5SDimitry Andric     addPass(&LiveRangeShrinkID);
553*0fca6ea1SDimitry Andric     addPass(createX86WinFixupBufferSecurityCheckPass());
5540b57cec5SDimitry Andric     addPass(createX86FixupSetCC());
5550b57cec5SDimitry Andric     addPass(createX86OptimizeLEAs());
5560b57cec5SDimitry Andric     addPass(createX86CallFrameOptimization());
5570b57cec5SDimitry Andric     addPass(createX86AvoidStoreForwardingBlocks());
5580b57cec5SDimitry Andric   }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   addPass(createX86SpeculativeLoadHardeningPass());
5610b57cec5SDimitry Andric   addPass(createX86FlagsCopyLoweringPass());
562349cc55cSDimitry Andric   addPass(createX86DynAllocaExpander());
563e8d8bef9SDimitry Andric 
5645f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
565e8d8bef9SDimitry Andric     addPass(createX86PreTileConfigPass());
56681ad6265SDimitry Andric   else
56781ad6265SDimitry Andric     addPass(createX86FastPreTileConfigPass());
568e8d8bef9SDimitry Andric }
569e8d8bef9SDimitry Andric 
5700b57cec5SDimitry Andric void X86PassConfig::addMachineSSAOptimization() {
5710b57cec5SDimitry Andric   addPass(createX86DomainReassignmentPass());
5720b57cec5SDimitry Andric   TargetPassConfig::addMachineSSAOptimization();
5730b57cec5SDimitry Andric }
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric void X86PassConfig::addPostRegAlloc() {
576fe6060f1SDimitry Andric   addPass(createX86LowerTileCopyPass());
5770b57cec5SDimitry Andric   addPass(createX86FloatingPointStackifierPass());
5785ffd83dbSDimitry Andric   // When -O0 is enabled, the Load Value Injection Hardening pass will fall back
5795ffd83dbSDimitry Andric   // to using the Speculative Execution Side Effect Suppression pass for
5805ffd83dbSDimitry Andric   // mitigation. This is to prevent slow downs due to
5815ffd83dbSDimitry Andric   // analyses needed by the LVIHardening pass when compiling at -O0.
5825f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None)
5830946e70aSDimitry Andric     addPass(createX86LoadValueInjectionLoadHardeningPass());
5840b57cec5SDimitry Andric }
5850b57cec5SDimitry Andric 
586bdd1243dSDimitry Andric void X86PassConfig::addPreSched2() {
587bdd1243dSDimitry Andric   addPass(createX86ExpandPseudoPass());
58806c3fb27SDimitry Andric   addPass(createKCFIPass());
589bdd1243dSDimitry Andric }
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric void X86PassConfig::addPreEmitPass() {
5925f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None) {
5930b57cec5SDimitry Andric     addPass(new X86ExecutionDomainFix());
5940b57cec5SDimitry Andric     addPass(createBreakFalseDeps());
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric 
5970b57cec5SDimitry Andric   addPass(createX86IndirectBranchTrackingPass());
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric   addPass(createX86IssueVZeroUpperPass());
6000b57cec5SDimitry Andric 
6015f757f3fSDimitry Andric   if (getOptLevel() != CodeGenOptLevel::None) {
6020b57cec5SDimitry Andric     addPass(createX86FixupBWInsts());
6030b57cec5SDimitry Andric     addPass(createX86PadShortFunctions());
6040b57cec5SDimitry Andric     addPass(createX86FixupLEAs());
60506c3fb27SDimitry Andric     addPass(createX86FixupInstTuning());
60606c3fb27SDimitry Andric     addPass(createX86FixupVectorConstants());
6070b57cec5SDimitry Andric   }
6081db9f3b2SDimitry Andric   addPass(createX86CompressEVEXPass());
6090b57cec5SDimitry Andric   addPass(createX86DiscriminateMemOpsPass());
6100b57cec5SDimitry Andric   addPass(createX86InsertPrefetchPass());
6115ffd83dbSDimitry Andric   addPass(createX86InsertX87waitPass());
6120b57cec5SDimitry Andric }
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric void X86PassConfig::addPreEmitPass2() {
6158bcb0991SDimitry Andric   const Triple &TT = TM->getTargetTriple();
6168bcb0991SDimitry Andric   const MCAsmInfo *MAI = TM->getMCAsmInfo();
6178bcb0991SDimitry Andric 
6185ffd83dbSDimitry Andric   // The X86 Speculative Execution Pass must run after all control
6195ffd83dbSDimitry Andric   // flow graph modifying passes. As a result it was listed to run right before
6205ffd83dbSDimitry Andric   // the X86 Retpoline Thunks pass. The reason it must run after control flow
6215ffd83dbSDimitry Andric   // graph modifications is that the model of LFENCE in LLVM has to be updated
6225ffd83dbSDimitry Andric   // (FIXME: https://bugs.llvm.org/show_bug.cgi?id=45167). Currently the
6235ffd83dbSDimitry Andric   // placement of this pass was hand checked to ensure that the subsequent
6245ffd83dbSDimitry Andric   // passes don't move the code around the LFENCEs in a way that will hurt the
6255ffd83dbSDimitry Andric   // correctness of this pass. This placement has been shown to work based on
6265ffd83dbSDimitry Andric   // hand inspection of the codegen output.
6275ffd83dbSDimitry Andric   addPass(createX86SpeculativeExecutionSideEffectSuppression());
6280946e70aSDimitry Andric   addPass(createX86IndirectThunksPass());
629753f127fSDimitry Andric   addPass(createX86ReturnThunksPass());
6308bcb0991SDimitry Andric 
6318bcb0991SDimitry Andric   // Insert extra int3 instructions after trailing call instructions to avoid
6328bcb0991SDimitry Andric   // issues in the unwinder.
6338bcb0991SDimitry Andric   if (TT.isOSWindows() && TT.getArch() == Triple::x86_64)
6348bcb0991SDimitry Andric     addPass(createX86AvoidTrailingCallPass());
6358bcb0991SDimitry Andric 
6360b57cec5SDimitry Andric   // Verify basic block incoming and outgoing cfa offset and register values and
6370b57cec5SDimitry Andric   // correct CFA calculation rule where needed by inserting appropriate CFI
6380b57cec5SDimitry Andric   // instructions.
6390b57cec5SDimitry Andric   if (!TT.isOSDarwin() &&
6400b57cec5SDimitry Andric       (!TT.isOSWindows() ||
6410b57cec5SDimitry Andric        MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI))
6420b57cec5SDimitry Andric     addPass(createCFIInstrInserter());
643fe6060f1SDimitry Andric 
644fe6060f1SDimitry Andric   if (TT.isOSWindows()) {
645480093f4SDimitry Andric     // Identify valid longjmp targets for Windows Control Flow Guard.
646480093f4SDimitry Andric     addPass(createCFGuardLongjmpPass());
647fe6060f1SDimitry Andric     // Identify valid eh continuation targets for Windows EHCont Guard.
648fe6060f1SDimitry Andric     addPass(createEHContGuardCatchretPass());
649fe6060f1SDimitry Andric   }
6500946e70aSDimitry Andric   addPass(createX86LoadValueInjectionRetHardeningPass());
651349cc55cSDimitry Andric 
652349cc55cSDimitry Andric   // Insert pseudo probe annotation for callsite profiling
653349cc55cSDimitry Andric   addPass(createPseudoProbeInserter());
6540eae32dcSDimitry Andric 
655bdd1243dSDimitry Andric   // KCFI indirect call checks are lowered to a bundle, and on Darwin platforms,
656bdd1243dSDimitry Andric   // also CALL_RVMARKER.
657bdd1243dSDimitry Andric   addPass(createUnpackMachineBundles([&TT](const MachineFunction &MF) {
658bdd1243dSDimitry Andric     // Only run bundle expansion if the module uses kcfi, or there are relevant
659bdd1243dSDimitry Andric     // ObjC runtime functions present in the module.
6600eae32dcSDimitry Andric     const Function &F = MF.getFunction();
6610eae32dcSDimitry Andric     const Module *M = F.getParent();
662bdd1243dSDimitry Andric     return M->getModuleFlag("kcfi") ||
663bdd1243dSDimitry Andric            (TT.isOSDarwin() &&
664bdd1243dSDimitry Andric             (M->getFunction("objc_retainAutoreleasedReturnValue") ||
665bdd1243dSDimitry Andric              M->getFunction("objc_unsafeClaimAutoreleasedReturnValue")));
6660eae32dcSDimitry Andric   }));
6670b57cec5SDimitry Andric }
6680b57cec5SDimitry Andric 
669fe6060f1SDimitry Andric bool X86PassConfig::addPostFastRegAllocRewrite() {
670fe6060f1SDimitry Andric   addPass(createX86FastTileConfigPass());
671fe6060f1SDimitry Andric   return true;
672fe6060f1SDimitry Andric }
673fe6060f1SDimitry Andric 
6740b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> X86PassConfig::getCSEConfig() const {
6750b57cec5SDimitry Andric   return getStandardCSEConfigForOpt(TM->getOptLevel());
6760b57cec5SDimitry Andric }
67781ad6265SDimitry Andric 
67881ad6265SDimitry Andric static bool onlyAllocateTileRegisters(const TargetRegisterInfo &TRI,
679*0fca6ea1SDimitry Andric                                       const MachineRegisterInfo &MRI,
680*0fca6ea1SDimitry Andric                                       const Register Reg) {
681*0fca6ea1SDimitry Andric   const TargetRegisterClass *RC = MRI.getRegClass(Reg);
682*0fca6ea1SDimitry Andric   return static_cast<const X86RegisterInfo &>(TRI).isTileRegisterClass(RC);
68381ad6265SDimitry Andric }
68481ad6265SDimitry Andric 
68581ad6265SDimitry Andric bool X86PassConfig::addRegAssignAndRewriteOptimized() {
68681ad6265SDimitry Andric   // Don't support tile RA when RA is specified by command line "-regalloc".
68781ad6265SDimitry Andric   if (!isCustomizedRegAlloc() && EnableTileRAPass) {
68881ad6265SDimitry Andric     // Allocate tile register first.
68981ad6265SDimitry Andric     addPass(createGreedyRegisterAllocator(onlyAllocateTileRegisters));
69081ad6265SDimitry Andric     addPass(createX86TileConfigPass());
69181ad6265SDimitry Andric   }
69281ad6265SDimitry Andric   return TargetPassConfig::addRegAssignAndRewriteOptimized();
69381ad6265SDimitry Andric }
694