xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===-- AArch64TargetMachine.cpp - Define TargetMachine for AArch64 -------===//
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 //
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "AArch64TargetMachine.h"
130b57cec5SDimitry Andric #include "AArch64.h"
14*5ffd83dbSDimitry Andric #include "AArch64MachineFunctionInfo.h"
150b57cec5SDimitry Andric #include "AArch64MacroFusion.h"
160b57cec5SDimitry Andric #include "AArch64Subtarget.h"
170b57cec5SDimitry Andric #include "AArch64TargetObjectFile.h"
180b57cec5SDimitry Andric #include "AArch64TargetTransformInfo.h"
190b57cec5SDimitry Andric #include "MCTargetDesc/AArch64MCTargetDesc.h"
200b57cec5SDimitry Andric #include "TargetInfo/AArch64TargetInfo.h"
210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
220b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
230b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/CSEConfigBase.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
30*5ffd83dbSDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
340b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
350b57cec5SDimitry Andric #include "llvm/IR/Function.h"
36480093f4SDimitry Andric #include "llvm/InitializePasses.h"
370b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
390b57cec5SDimitry Andric #include "llvm/Pass.h"
400b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
410b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
420b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
430b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
440b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
45480093f4SDimitry Andric #include "llvm/Transforms/CFGuard.h"
460b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
470b57cec5SDimitry Andric #include <memory>
480b57cec5SDimitry Andric #include <string>
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric using namespace llvm;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
530b57cec5SDimitry Andric                                 cl::desc("Enable the CCMP formation pass"),
540b57cec5SDimitry Andric                                 cl::init(true), cl::Hidden);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric static cl::opt<bool>
570b57cec5SDimitry Andric     EnableCondBrTuning("aarch64-enable-cond-br-tune",
580b57cec5SDimitry Andric                        cl::desc("Enable the conditional branch tuning pass"),
590b57cec5SDimitry Andric                        cl::init(true), cl::Hidden);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
620b57cec5SDimitry Andric                                cl::desc("Enable the machine combiner pass"),
630b57cec5SDimitry Andric                                cl::init(true), cl::Hidden);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
660b57cec5SDimitry Andric                                           cl::desc("Suppress STP for AArch64"),
670b57cec5SDimitry Andric                                           cl::init(true), cl::Hidden);
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric static cl::opt<bool> EnableAdvSIMDScalar(
700b57cec5SDimitry Andric     "aarch64-enable-simd-scalar",
710b57cec5SDimitry Andric     cl::desc("Enable use of AdvSIMD scalar integer instructions"),
720b57cec5SDimitry Andric     cl::init(false), cl::Hidden);
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric static cl::opt<bool>
750b57cec5SDimitry Andric     EnablePromoteConstant("aarch64-enable-promote-const",
760b57cec5SDimitry Andric                           cl::desc("Enable the promote constant pass"),
770b57cec5SDimitry Andric                           cl::init(true), cl::Hidden);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric static cl::opt<bool> EnableCollectLOH(
800b57cec5SDimitry Andric     "aarch64-enable-collect-loh",
810b57cec5SDimitry Andric     cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
820b57cec5SDimitry Andric     cl::init(true), cl::Hidden);
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric static cl::opt<bool>
850b57cec5SDimitry Andric     EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
860b57cec5SDimitry Andric                                   cl::desc("Enable the pass that removes dead"
870b57cec5SDimitry Andric                                            " definitons and replaces stores to"
880b57cec5SDimitry Andric                                            " them with stores to the zero"
890b57cec5SDimitry Andric                                            " register"),
900b57cec5SDimitry Andric                                   cl::init(true));
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric static cl::opt<bool> EnableRedundantCopyElimination(
930b57cec5SDimitry Andric     "aarch64-enable-copyelim",
940b57cec5SDimitry Andric     cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
950b57cec5SDimitry Andric     cl::Hidden);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
980b57cec5SDimitry Andric                                         cl::desc("Enable the load/store pair"
990b57cec5SDimitry Andric                                                  " optimization pass"),
1000b57cec5SDimitry Andric                                         cl::init(true), cl::Hidden);
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric static cl::opt<bool> EnableAtomicTidy(
1030b57cec5SDimitry Andric     "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
1040b57cec5SDimitry Andric     cl::desc("Run SimplifyCFG after expanding atomic operations"
1050b57cec5SDimitry Andric              " to make use of cmpxchg flow-based information"),
1060b57cec5SDimitry Andric     cl::init(true));
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric static cl::opt<bool>
1090b57cec5SDimitry Andric EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
1100b57cec5SDimitry Andric                         cl::desc("Run early if-conversion"),
1110b57cec5SDimitry Andric                         cl::init(true));
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric static cl::opt<bool>
1140b57cec5SDimitry Andric     EnableCondOpt("aarch64-enable-condopt",
1150b57cec5SDimitry Andric                   cl::desc("Enable the condition optimizer pass"),
1160b57cec5SDimitry Andric                   cl::init(true), cl::Hidden);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric static cl::opt<bool>
1190b57cec5SDimitry Andric EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
1200b57cec5SDimitry Andric                 cl::desc("Work around Cortex-A53 erratum 835769"),
1210b57cec5SDimitry Andric                 cl::init(false));
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric static cl::opt<bool>
1240b57cec5SDimitry Andric     EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
1250b57cec5SDimitry Andric                  cl::desc("Enable optimizations on complex GEPs"),
1260b57cec5SDimitry Andric                  cl::init(false));
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric static cl::opt<bool>
1290b57cec5SDimitry Andric     BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
1300b57cec5SDimitry Andric                      cl::desc("Relax out of range conditional branches"));
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric static cl::opt<bool> EnableCompressJumpTables(
1330b57cec5SDimitry Andric     "aarch64-enable-compress-jump-tables", cl::Hidden, cl::init(true),
1340b57cec5SDimitry Andric     cl::desc("Use smallest entry possible for jump tables"));
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric // FIXME: Unify control over GlobalMerge.
1370b57cec5SDimitry Andric static cl::opt<cl::boolOrDefault>
1380b57cec5SDimitry Andric     EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
1390b57cec5SDimitry Andric                       cl::desc("Enable the global merge pass"));
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric static cl::opt<bool>
1420b57cec5SDimitry Andric     EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
1430b57cec5SDimitry Andric                            cl::desc("Enable the loop data prefetch pass"),
1440b57cec5SDimitry Andric                            cl::init(true));
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric static cl::opt<int> EnableGlobalISelAtO(
1470b57cec5SDimitry Andric     "aarch64-enable-global-isel-at-O", cl::Hidden,
1480b57cec5SDimitry Andric     cl::desc("Enable GlobalISel at or below an opt level (-1 to disable)"),
1490b57cec5SDimitry Andric     cl::init(0));
1500b57cec5SDimitry Andric 
151*5ffd83dbSDimitry Andric static cl::opt<bool> EnableSVEIntrinsicOpts(
152*5ffd83dbSDimitry Andric     "aarch64-sve-intrinsic-opts", cl::Hidden,
153*5ffd83dbSDimitry Andric     cl::desc("Enable SVE intrinsic opts"),
154*5ffd83dbSDimitry Andric     cl::init(true));
155*5ffd83dbSDimitry Andric 
1560b57cec5SDimitry Andric static cl::opt<bool> EnableFalkorHWPFFix("aarch64-enable-falkor-hwpf-fix",
1570b57cec5SDimitry Andric                                          cl::init(true), cl::Hidden);
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric static cl::opt<bool>
1600b57cec5SDimitry Andric     EnableBranchTargets("aarch64-enable-branch-targets", cl::Hidden,
1610b57cec5SDimitry Andric                         cl::desc("Enable the AAcrh64 branch target pass"),
1620b57cec5SDimitry Andric                         cl::init(true));
1630b57cec5SDimitry Andric 
164480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Target() {
1650b57cec5SDimitry Andric   // Register the target.
1660b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
1670b57cec5SDimitry Andric   RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
1680b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
1698bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> W(getTheARM64_32Target());
1708bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> V(getTheAArch64_32Target());
1710b57cec5SDimitry Andric   auto PR = PassRegistry::getPassRegistry();
1720b57cec5SDimitry Andric   initializeGlobalISel(*PR);
1730b57cec5SDimitry Andric   initializeAArch64A53Fix835769Pass(*PR);
1740b57cec5SDimitry Andric   initializeAArch64A57FPLoadBalancingPass(*PR);
1750b57cec5SDimitry Andric   initializeAArch64AdvSIMDScalarPass(*PR);
1760b57cec5SDimitry Andric   initializeAArch64BranchTargetsPass(*PR);
1770b57cec5SDimitry Andric   initializeAArch64CollectLOHPass(*PR);
1780b57cec5SDimitry Andric   initializeAArch64CompressJumpTablesPass(*PR);
1790b57cec5SDimitry Andric   initializeAArch64ConditionalComparesPass(*PR);
1800b57cec5SDimitry Andric   initializeAArch64ConditionOptimizerPass(*PR);
1810b57cec5SDimitry Andric   initializeAArch64DeadRegisterDefinitionsPass(*PR);
1820b57cec5SDimitry Andric   initializeAArch64ExpandPseudoPass(*PR);
1830b57cec5SDimitry Andric   initializeAArch64LoadStoreOptPass(*PR);
1840b57cec5SDimitry Andric   initializeAArch64SIMDInstrOptPass(*PR);
1850b57cec5SDimitry Andric   initializeAArch64PreLegalizerCombinerPass(*PR);
186*5ffd83dbSDimitry Andric   initializeAArch64PostLegalizerCombinerPass(*PR);
1870b57cec5SDimitry Andric   initializeAArch64PromoteConstantPass(*PR);
1880b57cec5SDimitry Andric   initializeAArch64RedundantCopyEliminationPass(*PR);
1890b57cec5SDimitry Andric   initializeAArch64StorePairSuppressPass(*PR);
1900b57cec5SDimitry Andric   initializeFalkorHWPFFixPass(*PR);
1910b57cec5SDimitry Andric   initializeFalkorMarkStridedAccessesLegacyPass(*PR);
1920b57cec5SDimitry Andric   initializeLDTLSCleanupPass(*PR);
193*5ffd83dbSDimitry Andric   initializeSVEIntrinsicOptsPass(*PR);
1940b57cec5SDimitry Andric   initializeAArch64SpeculationHardeningPass(*PR);
195*5ffd83dbSDimitry Andric   initializeAArch64SLSHardeningPass(*PR);
1960b57cec5SDimitry Andric   initializeAArch64StackTaggingPass(*PR);
1978bcb0991SDimitry Andric   initializeAArch64StackTaggingPreRAPass(*PR);
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2010b57cec5SDimitry Andric // AArch64 Lowering public interface.
2020b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2030b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
2040b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO())
2058bcb0991SDimitry Andric     return std::make_unique<AArch64_MachoTargetObjectFile>();
2060b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2078bcb0991SDimitry Andric     return std::make_unique<AArch64_COFFTargetObjectFile>();
2080b57cec5SDimitry Andric 
2098bcb0991SDimitry Andric   return std::make_unique<AArch64_ELFTargetObjectFile>();
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric // Helper function to build a DataLayout string
2130b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT,
2140b57cec5SDimitry Andric                                      const MCTargetOptions &Options,
2150b57cec5SDimitry Andric                                      bool LittleEndian) {
2160b57cec5SDimitry Andric   if (Options.getABIName() == "ilp32")
2170b57cec5SDimitry Andric     return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
2188bcb0991SDimitry Andric   if (TT.isOSBinFormatMachO()) {
2198bcb0991SDimitry Andric     if (TT.getArch() == Triple::aarch64_32)
2208bcb0991SDimitry Andric       return "e-m:o-p:32:32-i64:64-i128:128-n32:64-S128";
2210b57cec5SDimitry Andric     return "e-m:o-i64:64-i128:128-n32:64-S128";
2228bcb0991SDimitry Andric   }
2230b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2240b57cec5SDimitry Andric     return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
2250b57cec5SDimitry Andric   if (LittleEndian)
2260b57cec5SDimitry Andric     return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
2270b57cec5SDimitry Andric   return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT,
2310b57cec5SDimitry Andric                                            Optional<Reloc::Model> RM) {
2320b57cec5SDimitry Andric   // AArch64 Darwin and Windows are always PIC.
2330b57cec5SDimitry Andric   if (TT.isOSDarwin() || TT.isOSWindows())
2340b57cec5SDimitry Andric     return Reloc::PIC_;
2350b57cec5SDimitry Andric   // On ELF platforms the default static relocation model has a smart enough
2360b57cec5SDimitry Andric   // linker to cope with referencing external symbols defined in a shared
2370b57cec5SDimitry Andric   // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
2380b57cec5SDimitry Andric   if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
2390b57cec5SDimitry Andric     return Reloc::Static;
2400b57cec5SDimitry Andric   return *RM;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric static CodeModel::Model
2440b57cec5SDimitry Andric getEffectiveAArch64CodeModel(const Triple &TT, Optional<CodeModel::Model> CM,
2450b57cec5SDimitry Andric                              bool JIT) {
2460b57cec5SDimitry Andric   if (CM) {
2470b57cec5SDimitry Andric     if (*CM != CodeModel::Small && *CM != CodeModel::Tiny &&
2480b57cec5SDimitry Andric         *CM != CodeModel::Large) {
2490b57cec5SDimitry Andric       report_fatal_error(
2500b57cec5SDimitry Andric           "Only small, tiny and large code models are allowed on AArch64");
2510b57cec5SDimitry Andric     } else if (*CM == CodeModel::Tiny && !TT.isOSBinFormatELF())
2520b57cec5SDimitry Andric       report_fatal_error("tiny code model is only supported on ELF");
2530b57cec5SDimitry Andric     return *CM;
2540b57cec5SDimitry Andric   }
2550b57cec5SDimitry Andric   // The default MCJIT memory managers make no guarantees about where they can
2560b57cec5SDimitry Andric   // find an executable page; JITed code needs to be able to refer to globals
2570b57cec5SDimitry Andric   // no matter how far away they are.
258480093f4SDimitry Andric   // We should set the CodeModel::Small for Windows ARM64 in JIT mode,
259480093f4SDimitry Andric   // since with large code model LLVM generating 4 MOV instructions, and
260480093f4SDimitry Andric   // Windows doesn't support relocating these long branch (4 MOVs).
261480093f4SDimitry Andric   if (JIT && !TT.isOSWindows())
2620b57cec5SDimitry Andric     return CodeModel::Large;
2630b57cec5SDimitry Andric   return CodeModel::Small;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric /// Create an AArch64 architecture model.
2670b57cec5SDimitry Andric ///
2680b57cec5SDimitry Andric AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
2690b57cec5SDimitry Andric                                            StringRef CPU, StringRef FS,
2700b57cec5SDimitry Andric                                            const TargetOptions &Options,
2710b57cec5SDimitry Andric                                            Optional<Reloc::Model> RM,
2720b57cec5SDimitry Andric                                            Optional<CodeModel::Model> CM,
2730b57cec5SDimitry Andric                                            CodeGenOpt::Level OL, bool JIT,
2740b57cec5SDimitry Andric                                            bool LittleEndian)
2750b57cec5SDimitry Andric     : LLVMTargetMachine(T,
2760b57cec5SDimitry Andric                         computeDataLayout(TT, Options.MCOptions, LittleEndian),
2770b57cec5SDimitry Andric                         TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
2780b57cec5SDimitry Andric                         getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
2790b57cec5SDimitry Andric       TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
2800b57cec5SDimitry Andric   initAsmInfo();
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO()) {
2830b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
2840b57cec5SDimitry Andric     this->Options.NoTrapAfterNoreturn = true;
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   if (getMCAsmInfo()->usesWindowsCFI()) {
2880b57cec5SDimitry Andric     // Unwinding can get confused if the last instruction in an
2890b57cec5SDimitry Andric     // exception-handling region (function, funclet, try block, etc.)
2900b57cec5SDimitry Andric     // is a call.
2910b57cec5SDimitry Andric     //
2920b57cec5SDimitry Andric     // FIXME: We could elide the trap if the next instruction would be in
2930b57cec5SDimitry Andric     // the same region anyway.
2940b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
2950b57cec5SDimitry Andric   }
2960b57cec5SDimitry Andric 
297480093f4SDimitry Andric   if (this->Options.TLSSize == 0) // default
298480093f4SDimitry Andric     this->Options.TLSSize = 24;
299480093f4SDimitry Andric   if ((getCodeModel() == CodeModel::Small ||
300480093f4SDimitry Andric        getCodeModel() == CodeModel::Kernel) &&
301480093f4SDimitry Andric       this->Options.TLSSize > 32)
302480093f4SDimitry Andric     // for the small (and kernel) code model, the maximum TLS size is 4GiB
303480093f4SDimitry Andric     this->Options.TLSSize = 32;
304480093f4SDimitry Andric   else if (getCodeModel() == CodeModel::Tiny && this->Options.TLSSize > 24)
305480093f4SDimitry Andric     // for the tiny code model, the maximum TLS size is 1MiB (< 16MiB)
306480093f4SDimitry Andric     this->Options.TLSSize = 24;
307480093f4SDimitry Andric 
3088bcb0991SDimitry Andric   // Enable GlobalISel at or below EnableGlobalISelAt0, unless this is
3098bcb0991SDimitry Andric   // MachO/CodeModel::Large, which GlobalISel does not support.
3108bcb0991SDimitry Andric   if (getOptLevel() <= EnableGlobalISelAtO &&
3118bcb0991SDimitry Andric       TT.getArch() != Triple::aarch64_32 &&
3128bcb0991SDimitry Andric       !(getCodeModel() == CodeModel::Large && TT.isOSBinFormatMachO())) {
3130b57cec5SDimitry Andric     setGlobalISel(true);
3140b57cec5SDimitry Andric     setGlobalISelAbort(GlobalISelAbortMode::Disable);
3150b57cec5SDimitry Andric   }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // AArch64 supports the MachineOutliner.
3180b57cec5SDimitry Andric   setMachineOutliner(true);
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   // AArch64 supports default outlining behaviour.
3210b57cec5SDimitry Andric   setSupportsDefaultOutlining(true);
322*5ffd83dbSDimitry Andric 
323*5ffd83dbSDimitry Andric   // AArch64 supports the debug entry values.
324*5ffd83dbSDimitry Andric   setSupportsDebugEntryValues(true);
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric AArch64TargetMachine::~AArch64TargetMachine() = default;
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric const AArch64Subtarget *
3300b57cec5SDimitry Andric AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
3310b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
3320b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
3350b57cec5SDimitry Andric                         ? CPUAttr.getValueAsString().str()
3360b57cec5SDimitry Andric                         : TargetCPU;
3370b57cec5SDimitry Andric   std::string FS = !FSAttr.hasAttribute(Attribute::None)
3380b57cec5SDimitry Andric                        ? FSAttr.getValueAsString().str()
3390b57cec5SDimitry Andric                        : TargetFS;
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   auto &I = SubtargetMap[CPU + FS];
3420b57cec5SDimitry Andric   if (!I) {
3430b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
3440b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
3450b57cec5SDimitry Andric     // function that reside in TargetOptions.
3460b57cec5SDimitry Andric     resetTargetOptions(F);
3478bcb0991SDimitry Andric     I = std::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
3480b57cec5SDimitry Andric                                             isLittle);
3490b57cec5SDimitry Andric   }
3500b57cec5SDimitry Andric   return I.get();
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric void AArch64leTargetMachine::anchor() { }
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric AArch64leTargetMachine::AArch64leTargetMachine(
3560b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
3570b57cec5SDimitry Andric     const TargetOptions &Options, Optional<Reloc::Model> RM,
3580b57cec5SDimitry Andric     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
3590b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric void AArch64beTargetMachine::anchor() { }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric AArch64beTargetMachine::AArch64beTargetMachine(
3640b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
3650b57cec5SDimitry Andric     const TargetOptions &Options, Optional<Reloc::Model> RM,
3660b57cec5SDimitry Andric     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
3670b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric namespace {
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric /// AArch64 Code Generator Pass Configuration Options.
3720b57cec5SDimitry Andric class AArch64PassConfig : public TargetPassConfig {
3730b57cec5SDimitry Andric public:
3740b57cec5SDimitry Andric   AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM)
3750b57cec5SDimitry Andric       : TargetPassConfig(TM, PM) {
3760b57cec5SDimitry Andric     if (TM.getOptLevel() != CodeGenOpt::None)
3770b57cec5SDimitry Andric       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
3780b57cec5SDimitry Andric   }
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   AArch64TargetMachine &getAArch64TargetMachine() const {
3810b57cec5SDimitry Andric     return getTM<AArch64TargetMachine>();
3820b57cec5SDimitry Andric   }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   ScheduleDAGInstrs *
3850b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
3860b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
3870b57cec5SDimitry Andric     ScheduleDAGMILive *DAG = createGenericSchedLive(C);
3880b57cec5SDimitry Andric     DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
3890b57cec5SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
3900b57cec5SDimitry Andric     if (ST.hasFusion())
3910b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
3920b57cec5SDimitry Andric     return DAG;
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   ScheduleDAGInstrs *
3960b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
3970b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
3980b57cec5SDimitry Andric     if (ST.hasFusion()) {
3990b57cec5SDimitry Andric       // Run the Macro Fusion after RA again since literals are expanded from
4000b57cec5SDimitry Andric       // pseudos then (v. addPreSched2()).
4010b57cec5SDimitry Andric       ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
4020b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
4030b57cec5SDimitry Andric       return DAG;
4040b57cec5SDimitry Andric     }
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric     return nullptr;
4070b57cec5SDimitry Andric   }
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   void addIRPasses()  override;
4100b57cec5SDimitry Andric   bool addPreISel() override;
4110b57cec5SDimitry Andric   bool addInstSelector() override;
4120b57cec5SDimitry Andric   bool addIRTranslator() override;
4130b57cec5SDimitry Andric   void addPreLegalizeMachineIR() override;
4140b57cec5SDimitry Andric   bool addLegalizeMachineIR() override;
415*5ffd83dbSDimitry Andric   void addPreRegBankSelect() override;
4160b57cec5SDimitry Andric   bool addRegBankSelect() override;
4170b57cec5SDimitry Andric   void addPreGlobalInstructionSelect() override;
4180b57cec5SDimitry Andric   bool addGlobalInstructionSelect() override;
4190b57cec5SDimitry Andric   bool addILPOpts() override;
4200b57cec5SDimitry Andric   void addPreRegAlloc() override;
4210b57cec5SDimitry Andric   void addPostRegAlloc() override;
4220b57cec5SDimitry Andric   void addPreSched2() override;
4230b57cec5SDimitry Andric   void addPreEmitPass() override;
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
4260b57cec5SDimitry Andric };
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric } // end anonymous namespace
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric TargetTransformInfo
4310b57cec5SDimitry Andric AArch64TargetMachine::getTargetTransformInfo(const Function &F) {
4320b57cec5SDimitry Andric   return TargetTransformInfo(AArch64TTIImpl(this, F));
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
4360b57cec5SDimitry Andric   return new AArch64PassConfig(*this, PM);
4370b57cec5SDimitry Andric }
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> AArch64PassConfig::getCSEConfig() const {
4400b57cec5SDimitry Andric   return getStandardCSEConfigForOpt(TM->getOptLevel());
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric void AArch64PassConfig::addIRPasses() {
4440b57cec5SDimitry Andric   // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
4450b57cec5SDimitry Andric   // ourselves.
4460b57cec5SDimitry Andric   addPass(createAtomicExpandPass());
4470b57cec5SDimitry Andric 
448*5ffd83dbSDimitry Andric   // Expand any SVE vector library calls that we can't code generate directly.
449*5ffd83dbSDimitry Andric   if (EnableSVEIntrinsicOpts && TM->getOptLevel() == CodeGenOpt::Aggressive)
450*5ffd83dbSDimitry Andric     addPass(createSVEIntrinsicOptsPass());
451*5ffd83dbSDimitry Andric 
4520b57cec5SDimitry Andric   // Cmpxchg instructions are often used with a subsequent comparison to
4530b57cec5SDimitry Andric   // determine whether it succeeded. We can exploit existing control-flow in
4540b57cec5SDimitry Andric   // ldrex/strex loops to simplify this, but it needs tidying up.
4550b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
4560b57cec5SDimitry Andric     addPass(createCFGSimplificationPass(1, true, true, false, true));
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric   // Run LoopDataPrefetch
4590b57cec5SDimitry Andric   //
4600b57cec5SDimitry Andric   // Run this before LSR to remove the multiplies involved in computing the
4610b57cec5SDimitry Andric   // pointer values N iterations ahead.
4620b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
4630b57cec5SDimitry Andric     if (EnableLoopDataPrefetch)
4640b57cec5SDimitry Andric       addPass(createLoopDataPrefetchPass());
4650b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
4660b57cec5SDimitry Andric       addPass(createFalkorMarkStridedAccessesPass());
4670b57cec5SDimitry Andric   }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
4700b57cec5SDimitry Andric 
471*5ffd83dbSDimitry Andric   addPass(createAArch64StackTaggingPass(
472*5ffd83dbSDimitry Andric       /*IsOptNone=*/TM->getOptLevel() == CodeGenOpt::None));
473*5ffd83dbSDimitry Andric 
4740b57cec5SDimitry Andric   // Match interleaved memory accesses to ldN/stN intrinsics.
4750b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
4760b57cec5SDimitry Andric     addPass(createInterleavedLoadCombinePass());
4770b57cec5SDimitry Andric     addPass(createInterleavedAccessPass());
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
4810b57cec5SDimitry Andric     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
4820b57cec5SDimitry Andric     // and lower a GEP with multiple indices to either arithmetic operations or
4830b57cec5SDimitry Andric     // multiple GEPs with single index.
4840b57cec5SDimitry Andric     addPass(createSeparateConstOffsetFromGEPPass(true));
4850b57cec5SDimitry Andric     // Call EarlyCSE pass to find and remove subexpressions in the lowered
4860b57cec5SDimitry Andric     // result.
4870b57cec5SDimitry Andric     addPass(createEarlyCSEPass());
4880b57cec5SDimitry Andric     // Do loop invariant code motion in case part of the lowered result is
4890b57cec5SDimitry Andric     // invariant.
4900b57cec5SDimitry Andric     addPass(createLICMPass());
4910b57cec5SDimitry Andric   }
4920b57cec5SDimitry Andric 
493480093f4SDimitry Andric   // Add Control Flow Guard checks.
494480093f4SDimitry Andric   if (TM->getTargetTriple().isOSWindows())
495480093f4SDimitry Andric     addPass(createCFGuardCheckPass());
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric // Pass Pipeline Configuration
4990b57cec5SDimitry Andric bool AArch64PassConfig::addPreISel() {
5000b57cec5SDimitry Andric   // Run promote constant before global merge, so that the promoted constants
5010b57cec5SDimitry Andric   // get a chance to be merged
5020b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
5030b57cec5SDimitry Andric     addPass(createAArch64PromoteConstantPass());
5040b57cec5SDimitry Andric   // FIXME: On AArch64, this depends on the type.
5050b57cec5SDimitry Andric   // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
5060b57cec5SDimitry Andric   // and the offset has to be a multiple of the related size in bytes.
5070b57cec5SDimitry Andric   if ((TM->getOptLevel() != CodeGenOpt::None &&
5080b57cec5SDimitry Andric        EnableGlobalMerge == cl::BOU_UNSET) ||
5090b57cec5SDimitry Andric       EnableGlobalMerge == cl::BOU_TRUE) {
5100b57cec5SDimitry Andric     bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
5110b57cec5SDimitry Andric                                (EnableGlobalMerge == cl::BOU_UNSET);
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric     // Merging of extern globals is enabled by default on non-Mach-O as we
5140b57cec5SDimitry Andric     // expect it to be generally either beneficial or harmless. On Mach-O it
5150b57cec5SDimitry Andric     // is disabled as we emit the .subsections_via_symbols directive which
5160b57cec5SDimitry Andric     // means that merging extern globals is not safe.
5170b57cec5SDimitry Andric     bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric     // FIXME: extern global merging is only enabled when we optimise for size
5200b57cec5SDimitry Andric     // because there are some regressions with it also enabled for performance.
5210b57cec5SDimitry Andric     if (!OnlyOptimizeForSize)
5220b57cec5SDimitry Andric       MergeExternalByDefault = false;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric     addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize,
5250b57cec5SDimitry Andric                                   MergeExternalByDefault));
5260b57cec5SDimitry Andric   }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric   return false;
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric bool AArch64PassConfig::addInstSelector() {
5320b57cec5SDimitry Andric   addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
5350b57cec5SDimitry Andric   // references to _TLS_MODULE_BASE_ as possible.
5360b57cec5SDimitry Andric   if (TM->getTargetTriple().isOSBinFormatELF() &&
5370b57cec5SDimitry Andric       getOptLevel() != CodeGenOpt::None)
5380b57cec5SDimitry Andric     addPass(createAArch64CleanupLocalDynamicTLSPass());
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   return false;
5410b57cec5SDimitry Andric }
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric bool AArch64PassConfig::addIRTranslator() {
5440b57cec5SDimitry Andric   addPass(new IRTranslator());
5450b57cec5SDimitry Andric   return false;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric void AArch64PassConfig::addPreLegalizeMachineIR() {
5498bcb0991SDimitry Andric   bool IsOptNone = getOptLevel() == CodeGenOpt::None;
5508bcb0991SDimitry Andric   addPass(createAArch64PreLegalizeCombiner(IsOptNone));
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric bool AArch64PassConfig::addLegalizeMachineIR() {
5540b57cec5SDimitry Andric   addPass(new Legalizer());
5550b57cec5SDimitry Andric   return false;
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric 
558*5ffd83dbSDimitry Andric void AArch64PassConfig::addPreRegBankSelect() {
559*5ffd83dbSDimitry Andric   // For now we don't add this to the pipeline for -O0. We could do in future
560*5ffd83dbSDimitry Andric   // if we split the combines into separate O0/opt groupings.
561*5ffd83dbSDimitry Andric   bool IsOptNone = getOptLevel() == CodeGenOpt::None;
562*5ffd83dbSDimitry Andric   if (!IsOptNone)
563*5ffd83dbSDimitry Andric     addPass(createAArch64PostLegalizeCombiner(IsOptNone));
564*5ffd83dbSDimitry Andric }
565*5ffd83dbSDimitry Andric 
5660b57cec5SDimitry Andric bool AArch64PassConfig::addRegBankSelect() {
5670b57cec5SDimitry Andric   addPass(new RegBankSelect());
5680b57cec5SDimitry Andric   return false;
5690b57cec5SDimitry Andric }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric void AArch64PassConfig::addPreGlobalInstructionSelect() {
5720b57cec5SDimitry Andric   addPass(new Localizer());
5730b57cec5SDimitry Andric }
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric bool AArch64PassConfig::addGlobalInstructionSelect() {
5760b57cec5SDimitry Andric   addPass(new InstructionSelect());
5770b57cec5SDimitry Andric   return false;
5780b57cec5SDimitry Andric }
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric bool AArch64PassConfig::addILPOpts() {
5810b57cec5SDimitry Andric   if (EnableCondOpt)
5820b57cec5SDimitry Andric     addPass(createAArch64ConditionOptimizerPass());
5830b57cec5SDimitry Andric   if (EnableCCMP)
5840b57cec5SDimitry Andric     addPass(createAArch64ConditionalCompares());
5850b57cec5SDimitry Andric   if (EnableMCR)
5860b57cec5SDimitry Andric     addPass(&MachineCombinerID);
5870b57cec5SDimitry Andric   if (EnableCondBrTuning)
5880b57cec5SDimitry Andric     addPass(createAArch64CondBrTuning());
5890b57cec5SDimitry Andric   if (EnableEarlyIfConversion)
5900b57cec5SDimitry Andric     addPass(&EarlyIfConverterID);
5910b57cec5SDimitry Andric   if (EnableStPairSuppress)
5920b57cec5SDimitry Andric     addPass(createAArch64StorePairSuppressPass());
5930b57cec5SDimitry Andric   addPass(createAArch64SIMDInstrOptPass());
5948bcb0991SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
5958bcb0991SDimitry Andric     addPass(createAArch64StackTaggingPreRAPass());
5960b57cec5SDimitry Andric   return true;
5970b57cec5SDimitry Andric }
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric void AArch64PassConfig::addPreRegAlloc() {
6000b57cec5SDimitry Andric   // Change dead register definitions to refer to the zero register.
6010b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
6020b57cec5SDimitry Andric     addPass(createAArch64DeadRegisterDefinitions());
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric   // Use AdvSIMD scalar instructions whenever profitable.
6050b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
6060b57cec5SDimitry Andric     addPass(createAArch64AdvSIMDScalar());
6070b57cec5SDimitry Andric     // The AdvSIMD pass may produce copies that can be rewritten to
608480093f4SDimitry Andric     // be register coalescer friendly.
6090b57cec5SDimitry Andric     addPass(&PeepholeOptimizerID);
6100b57cec5SDimitry Andric   }
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric void AArch64PassConfig::addPostRegAlloc() {
6140b57cec5SDimitry Andric   // Remove redundant copy instructions.
6150b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
6160b57cec5SDimitry Andric     addPass(createAArch64RedundantCopyEliminationPass());
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
6190b57cec5SDimitry Andric     // Improve performance for some FP/SIMD code for A57.
6200b57cec5SDimitry Andric     addPass(createAArch64A57FPLoadBalancing());
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric void AArch64PassConfig::addPreSched2() {
6240b57cec5SDimitry Andric   // Expand some pseudo instructions to allow proper scheduling.
6250b57cec5SDimitry Andric   addPass(createAArch64ExpandPseudoPass());
6260b57cec5SDimitry Andric   // Use load/store pair instructions when possible.
6270b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
6280b57cec5SDimitry Andric     if (EnableLoadStoreOpt)
6290b57cec5SDimitry Andric       addPass(createAArch64LoadStoreOptimizationPass());
6300b57cec5SDimitry Andric   }
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   // The AArch64SpeculationHardeningPass destroys dominator tree and natural
6330b57cec5SDimitry Andric   // loop info, which is needed for the FalkorHWPFFixPass and also later on.
6340b57cec5SDimitry Andric   // Therefore, run the AArch64SpeculationHardeningPass before the
6350b57cec5SDimitry Andric   // FalkorHWPFFixPass to avoid recomputing dominator tree and natural loop
6360b57cec5SDimitry Andric   // info.
6370b57cec5SDimitry Andric   addPass(createAArch64SpeculationHardeningPass());
6380b57cec5SDimitry Andric 
639*5ffd83dbSDimitry Andric   addPass(createAArch64IndirectThunks());
640*5ffd83dbSDimitry Andric   addPass(createAArch64SLSHardeningPass());
641*5ffd83dbSDimitry Andric 
6420b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
6430b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
6440b57cec5SDimitry Andric       addPass(createFalkorHWPFFixPass());
6450b57cec5SDimitry Andric   }
6460b57cec5SDimitry Andric }
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric void AArch64PassConfig::addPreEmitPass() {
6490b57cec5SDimitry Andric   // Machine Block Placement might have created new opportunities when run
6500b57cec5SDimitry Andric   // at O3, where the Tail Duplication Threshold is set to 4 instructions.
6510b57cec5SDimitry Andric   // Run the load/store optimizer once more.
6520b57cec5SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Aggressive && EnableLoadStoreOpt)
6530b57cec5SDimitry Andric     addPass(createAArch64LoadStoreOptimizationPass());
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric   if (EnableA53Fix835769)
6560b57cec5SDimitry Andric     addPass(createAArch64A53Fix835769());
657480093f4SDimitry Andric 
658480093f4SDimitry Andric   if (EnableBranchTargets)
659480093f4SDimitry Andric     addPass(createAArch64BranchTargetsPass());
660480093f4SDimitry Andric 
6610b57cec5SDimitry Andric   // Relax conditional branch instructions if they're otherwise out of
6620b57cec5SDimitry Andric   // range of their destination.
6630b57cec5SDimitry Andric   if (BranchRelaxation)
6640b57cec5SDimitry Andric     addPass(&BranchRelaxationPassID);
6650b57cec5SDimitry Andric 
666480093f4SDimitry Andric   // Identify valid longjmp targets for Windows Control Flow Guard.
667480093f4SDimitry Andric   if (TM->getTargetTriple().isOSWindows())
668480093f4SDimitry Andric     addPass(createCFGuardLongjmpPass());
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCompressJumpTables)
6710b57cec5SDimitry Andric     addPass(createAArch64CompressJumpTablesPass());
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
6740b57cec5SDimitry Andric       TM->getTargetTriple().isOSBinFormatMachO())
6750b57cec5SDimitry Andric     addPass(createAArch64CollectLOHPass());
676*5ffd83dbSDimitry Andric 
677*5ffd83dbSDimitry Andric   // SVE bundles move prefixes with destructive operations.
678*5ffd83dbSDimitry Andric   addPass(createUnpackMachineBundles(nullptr));
679*5ffd83dbSDimitry Andric }
680*5ffd83dbSDimitry Andric 
681*5ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
682*5ffd83dbSDimitry Andric AArch64TargetMachine::createDefaultFuncInfoYAML() const {
683*5ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo();
684*5ffd83dbSDimitry Andric }
685*5ffd83dbSDimitry Andric 
686*5ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
687*5ffd83dbSDimitry Andric AArch64TargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
688*5ffd83dbSDimitry Andric   const auto *MFI = MF.getInfo<AArch64FunctionInfo>();
689*5ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo(*MFI);
690*5ffd83dbSDimitry Andric }
691*5ffd83dbSDimitry Andric 
692*5ffd83dbSDimitry Andric bool AArch64TargetMachine::parseMachineFunctionInfo(
693*5ffd83dbSDimitry Andric     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
694*5ffd83dbSDimitry Andric     SMDiagnostic &Error, SMRange &SourceRange) const {
695*5ffd83dbSDimitry Andric   const auto &YamlMFI =
696*5ffd83dbSDimitry Andric       reinterpret_cast<const yaml::AArch64FunctionInfo &>(MFI);
697*5ffd83dbSDimitry Andric   MachineFunction &MF = PFS.MF;
698*5ffd83dbSDimitry Andric   MF.getInfo<AArch64FunctionInfo>()->initializeBaseYamlFields(YamlMFI);
699*5ffd83dbSDimitry Andric   return false;
7000b57cec5SDimitry Andric }
701