xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
145ffd83dbSDimitry Andric #include "AArch64MachineFunctionInfo.h"
1581ad6265SDimitry Andric #include "AArch64MachineScheduler.h"
160b57cec5SDimitry Andric #include "AArch64MacroFusion.h"
170b57cec5SDimitry Andric #include "AArch64Subtarget.h"
180b57cec5SDimitry Andric #include "AArch64TargetObjectFile.h"
190b57cec5SDimitry Andric #include "AArch64TargetTransformInfo.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/AArch64MCTargetDesc.h"
210b57cec5SDimitry Andric #include "TargetInfo/AArch64TargetInfo.h"
220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
230b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
240b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
2581ad6265SDimitry Andric #include "llvm/CodeGen/CFIFixup.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/CSEConfigBase.h"
2781ad6265SDimitry Andric #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h"
31349cc55cSDimitry Andric #include "llvm/CodeGen/GlobalISel/LoadStoreOpt.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
345ffd83dbSDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
3781ad6265SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
390b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
400b57cec5SDimitry Andric #include "llvm/IR/Function.h"
41480093f4SDimitry Andric #include "llvm/InitializePasses.h"
420b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
430b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.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/Target/TargetLoweringObjectFile.h"
490b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
50480093f4SDimitry Andric #include "llvm/Transforms/CFGuard.h"
510b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
520b57cec5SDimitry Andric #include <memory>
53*bdd1243dSDimitry Andric #include <optional>
540b57cec5SDimitry Andric #include <string>
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric using namespace llvm;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
590b57cec5SDimitry Andric                                 cl::desc("Enable the CCMP formation pass"),
600b57cec5SDimitry Andric                                 cl::init(true), cl::Hidden);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static cl::opt<bool>
630b57cec5SDimitry Andric     EnableCondBrTuning("aarch64-enable-cond-br-tune",
640b57cec5SDimitry Andric                        cl::desc("Enable the conditional branch tuning pass"),
650b57cec5SDimitry Andric                        cl::init(true), cl::Hidden);
660b57cec5SDimitry Andric 
6781ad6265SDimitry Andric static cl::opt<bool> EnableAArch64CopyPropagation(
6881ad6265SDimitry Andric     "aarch64-enable-copy-propagation",
6981ad6265SDimitry Andric     cl::desc("Enable the copy propagation with AArch64 copy instr"),
7081ad6265SDimitry Andric     cl::init(true), cl::Hidden);
7181ad6265SDimitry Andric 
720b57cec5SDimitry Andric static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
730b57cec5SDimitry Andric                                cl::desc("Enable the machine combiner pass"),
740b57cec5SDimitry Andric                                cl::init(true), cl::Hidden);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
770b57cec5SDimitry Andric                                           cl::desc("Suppress STP for AArch64"),
780b57cec5SDimitry Andric                                           cl::init(true), cl::Hidden);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric static cl::opt<bool> EnableAdvSIMDScalar(
810b57cec5SDimitry Andric     "aarch64-enable-simd-scalar",
820b57cec5SDimitry Andric     cl::desc("Enable use of AdvSIMD scalar integer instructions"),
830b57cec5SDimitry Andric     cl::init(false), cl::Hidden);
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric static cl::opt<bool>
860b57cec5SDimitry Andric     EnablePromoteConstant("aarch64-enable-promote-const",
870b57cec5SDimitry Andric                           cl::desc("Enable the promote constant pass"),
880b57cec5SDimitry Andric                           cl::init(true), cl::Hidden);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric static cl::opt<bool> EnableCollectLOH(
910b57cec5SDimitry Andric     "aarch64-enable-collect-loh",
920b57cec5SDimitry Andric     cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
930b57cec5SDimitry Andric     cl::init(true), cl::Hidden);
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric static cl::opt<bool>
960b57cec5SDimitry Andric     EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
970b57cec5SDimitry Andric                                   cl::desc("Enable the pass that removes dead"
980b57cec5SDimitry Andric                                            " definitons and replaces stores to"
990b57cec5SDimitry Andric                                            " them with stores to the zero"
1000b57cec5SDimitry Andric                                            " register"),
1010b57cec5SDimitry Andric                                   cl::init(true));
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric static cl::opt<bool> EnableRedundantCopyElimination(
1040b57cec5SDimitry Andric     "aarch64-enable-copyelim",
1050b57cec5SDimitry Andric     cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
1060b57cec5SDimitry Andric     cl::Hidden);
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
1090b57cec5SDimitry Andric                                         cl::desc("Enable the load/store pair"
1100b57cec5SDimitry Andric                                                  " optimization pass"),
1110b57cec5SDimitry Andric                                         cl::init(true), cl::Hidden);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric static cl::opt<bool> EnableAtomicTidy(
1140b57cec5SDimitry Andric     "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
1150b57cec5SDimitry Andric     cl::desc("Run SimplifyCFG after expanding atomic operations"
1160b57cec5SDimitry Andric              " to make use of cmpxchg flow-based information"),
1170b57cec5SDimitry Andric     cl::init(true));
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric static cl::opt<bool>
1200b57cec5SDimitry Andric EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
1210b57cec5SDimitry Andric                         cl::desc("Run early if-conversion"),
1220b57cec5SDimitry Andric                         cl::init(true));
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric static cl::opt<bool>
1250b57cec5SDimitry Andric     EnableCondOpt("aarch64-enable-condopt",
1260b57cec5SDimitry Andric                   cl::desc("Enable the condition optimizer pass"),
1270b57cec5SDimitry Andric                   cl::init(true), cl::Hidden);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric static cl::opt<bool>
1300b57cec5SDimitry Andric     EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
1310b57cec5SDimitry Andric                  cl::desc("Enable optimizations on complex GEPs"),
132*bdd1243dSDimitry Andric                  cl::init(false));
133*bdd1243dSDimitry Andric 
134*bdd1243dSDimitry Andric static cl::opt<bool>
135*bdd1243dSDimitry Andric     EnableSelectOpt("aarch64-select-opt", cl::Hidden,
136*bdd1243dSDimitry Andric                     cl::desc("Enable select to branch optimizations"),
137fcaf7f86SDimitry Andric                     cl::init(true));
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric static cl::opt<bool>
1400b57cec5SDimitry Andric     BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
1410b57cec5SDimitry Andric                      cl::desc("Relax out of range conditional branches"));
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric static cl::opt<bool> EnableCompressJumpTables(
1440b57cec5SDimitry Andric     "aarch64-enable-compress-jump-tables", cl::Hidden, cl::init(true),
1450b57cec5SDimitry Andric     cl::desc("Use smallest entry possible for jump tables"));
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric // FIXME: Unify control over GlobalMerge.
1480b57cec5SDimitry Andric static cl::opt<cl::boolOrDefault>
1490b57cec5SDimitry Andric     EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
1500b57cec5SDimitry Andric                       cl::desc("Enable the global merge pass"));
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric static cl::opt<bool>
1530b57cec5SDimitry Andric     EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
1540b57cec5SDimitry Andric                            cl::desc("Enable the loop data prefetch pass"),
1550b57cec5SDimitry Andric                            cl::init(true));
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric static cl::opt<int> EnableGlobalISelAtO(
1580b57cec5SDimitry Andric     "aarch64-enable-global-isel-at-O", cl::Hidden,
1590b57cec5SDimitry Andric     cl::desc("Enable GlobalISel at or below an opt level (-1 to disable)"),
1600b57cec5SDimitry Andric     cl::init(0));
1610b57cec5SDimitry Andric 
162e8d8bef9SDimitry Andric static cl::opt<bool>
163e8d8bef9SDimitry Andric     EnableSVEIntrinsicOpts("aarch64-enable-sve-intrinsic-opts", cl::Hidden,
1645ffd83dbSDimitry Andric                            cl::desc("Enable SVE intrinsic opts"),
1655ffd83dbSDimitry Andric                            cl::init(true));
1665ffd83dbSDimitry Andric 
1670b57cec5SDimitry Andric static cl::opt<bool> EnableFalkorHWPFFix("aarch64-enable-falkor-hwpf-fix",
1680b57cec5SDimitry Andric                                          cl::init(true), cl::Hidden);
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric static cl::opt<bool>
1710b57cec5SDimitry Andric     EnableBranchTargets("aarch64-enable-branch-targets", cl::Hidden,
172fe6060f1SDimitry Andric                         cl::desc("Enable the AArch64 branch target pass"),
1730b57cec5SDimitry Andric                         cl::init(true));
1740b57cec5SDimitry Andric 
175fe6060f1SDimitry Andric static cl::opt<unsigned> SVEVectorBitsMaxOpt(
176fe6060f1SDimitry Andric     "aarch64-sve-vector-bits-max",
177fe6060f1SDimitry Andric     cl::desc("Assume SVE vector registers are at most this big, "
178fe6060f1SDimitry Andric              "with zero meaning no maximum size is assumed."),
179fe6060f1SDimitry Andric     cl::init(0), cl::Hidden);
180fe6060f1SDimitry Andric 
181fe6060f1SDimitry Andric static cl::opt<unsigned> SVEVectorBitsMinOpt(
182fe6060f1SDimitry Andric     "aarch64-sve-vector-bits-min",
183fe6060f1SDimitry Andric     cl::desc("Assume SVE vector registers are at least this big, "
184fe6060f1SDimitry Andric              "with zero meaning no minimum size is assumed."),
185fe6060f1SDimitry Andric     cl::init(0), cl::Hidden);
186fe6060f1SDimitry Andric 
187fe6060f1SDimitry Andric extern cl::opt<bool> EnableHomogeneousPrologEpilog;
188fe6060f1SDimitry Andric 
189349cc55cSDimitry Andric static cl::opt<bool> EnableGISelLoadStoreOptPreLegal(
190349cc55cSDimitry Andric     "aarch64-enable-gisel-ldst-prelegal",
191349cc55cSDimitry Andric     cl::desc("Enable GlobalISel's pre-legalizer load/store optimization pass"),
192349cc55cSDimitry Andric     cl::init(true), cl::Hidden);
193349cc55cSDimitry Andric 
194349cc55cSDimitry Andric static cl::opt<bool> EnableGISelLoadStoreOptPostLegal(
195349cc55cSDimitry Andric     "aarch64-enable-gisel-ldst-postlegal",
196349cc55cSDimitry Andric     cl::desc("Enable GlobalISel's post-legalizer load/store optimization pass"),
197349cc55cSDimitry Andric     cl::init(false), cl::Hidden);
198349cc55cSDimitry Andric 
199480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Target() {
2000b57cec5SDimitry Andric   // Register the target.
2010b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
2020b57cec5SDimitry Andric   RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
2030b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
2048bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> W(getTheARM64_32Target());
2058bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> V(getTheAArch64_32Target());
2060b57cec5SDimitry Andric   auto PR = PassRegistry::getPassRegistry();
2070b57cec5SDimitry Andric   initializeGlobalISel(*PR);
2080b57cec5SDimitry Andric   initializeAArch64A53Fix835769Pass(*PR);
2090b57cec5SDimitry Andric   initializeAArch64A57FPLoadBalancingPass(*PR);
2100b57cec5SDimitry Andric   initializeAArch64AdvSIMDScalarPass(*PR);
2110b57cec5SDimitry Andric   initializeAArch64BranchTargetsPass(*PR);
2120b57cec5SDimitry Andric   initializeAArch64CollectLOHPass(*PR);
2130b57cec5SDimitry Andric   initializeAArch64CompressJumpTablesPass(*PR);
2140b57cec5SDimitry Andric   initializeAArch64ConditionalComparesPass(*PR);
2150b57cec5SDimitry Andric   initializeAArch64ConditionOptimizerPass(*PR);
2160b57cec5SDimitry Andric   initializeAArch64DeadRegisterDefinitionsPass(*PR);
2170b57cec5SDimitry Andric   initializeAArch64ExpandPseudoPass(*PR);
218*bdd1243dSDimitry Andric   initializeAArch64KCFIPass(*PR);
2190b57cec5SDimitry Andric   initializeAArch64LoadStoreOptPass(*PR);
220349cc55cSDimitry Andric   initializeAArch64MIPeepholeOptPass(*PR);
2210b57cec5SDimitry Andric   initializeAArch64SIMDInstrOptPass(*PR);
222fe6060f1SDimitry Andric   initializeAArch64O0PreLegalizerCombinerPass(*PR);
2230b57cec5SDimitry Andric   initializeAArch64PreLegalizerCombinerPass(*PR);
2245ffd83dbSDimitry Andric   initializeAArch64PostLegalizerCombinerPass(*PR);
225e8d8bef9SDimitry Andric   initializeAArch64PostLegalizerLoweringPass(*PR);
226e8d8bef9SDimitry Andric   initializeAArch64PostSelectOptimizePass(*PR);
2270b57cec5SDimitry Andric   initializeAArch64PromoteConstantPass(*PR);
2280b57cec5SDimitry Andric   initializeAArch64RedundantCopyEliminationPass(*PR);
2290b57cec5SDimitry Andric   initializeAArch64StorePairSuppressPass(*PR);
2300b57cec5SDimitry Andric   initializeFalkorHWPFFixPass(*PR);
2310b57cec5SDimitry Andric   initializeFalkorMarkStridedAccessesLegacyPass(*PR);
2320b57cec5SDimitry Andric   initializeLDTLSCleanupPass(*PR);
233*bdd1243dSDimitry Andric   initializeSMEABIPass(*PR);
2345ffd83dbSDimitry Andric   initializeSVEIntrinsicOptsPass(*PR);
2350b57cec5SDimitry Andric   initializeAArch64SpeculationHardeningPass(*PR);
2365ffd83dbSDimitry Andric   initializeAArch64SLSHardeningPass(*PR);
2370b57cec5SDimitry Andric   initializeAArch64StackTaggingPass(*PR);
2388bcb0991SDimitry Andric   initializeAArch64StackTaggingPreRAPass(*PR);
239fe6060f1SDimitry Andric   initializeAArch64LowerHomogeneousPrologEpilogPass(*PR);
240*bdd1243dSDimitry Andric   initializeAArch64DAGToDAGISelPass(*PR);
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2440b57cec5SDimitry Andric // AArch64 Lowering public interface.
2450b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2460b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
2470b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO())
2488bcb0991SDimitry Andric     return std::make_unique<AArch64_MachoTargetObjectFile>();
2490b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2508bcb0991SDimitry Andric     return std::make_unique<AArch64_COFFTargetObjectFile>();
2510b57cec5SDimitry Andric 
2528bcb0991SDimitry Andric   return std::make_unique<AArch64_ELFTargetObjectFile>();
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric // Helper function to build a DataLayout string
2560b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT,
2570b57cec5SDimitry Andric                                      const MCTargetOptions &Options,
2580b57cec5SDimitry Andric                                      bool LittleEndian) {
2598bcb0991SDimitry Andric   if (TT.isOSBinFormatMachO()) {
2608bcb0991SDimitry Andric     if (TT.getArch() == Triple::aarch64_32)
2618bcb0991SDimitry Andric       return "e-m:o-p:32:32-i64:64-i128:128-n32:64-S128";
2620b57cec5SDimitry Andric     return "e-m:o-i64:64-i128:128-n32:64-S128";
2638bcb0991SDimitry Andric   }
2640b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2650b57cec5SDimitry Andric     return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
266e8d8bef9SDimitry Andric   std::string Endian = LittleEndian ? "e" : "E";
267e8d8bef9SDimitry Andric   std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
268e8d8bef9SDimitry Andric   return Endian + "-m:e" + Ptr32 +
269e8d8bef9SDimitry Andric          "-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
270e8d8bef9SDimitry Andric }
271e8d8bef9SDimitry Andric 
272e8d8bef9SDimitry Andric static StringRef computeDefaultCPU(const Triple &TT, StringRef CPU) {
273e8d8bef9SDimitry Andric   if (CPU.empty() && TT.isArm64e())
274e8d8bef9SDimitry Andric     return "apple-a12";
275e8d8bef9SDimitry Andric   return CPU;
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT,
279*bdd1243dSDimitry Andric                                            std::optional<Reloc::Model> RM) {
2800b57cec5SDimitry Andric   // AArch64 Darwin and Windows are always PIC.
2810b57cec5SDimitry Andric   if (TT.isOSDarwin() || TT.isOSWindows())
2820b57cec5SDimitry Andric     return Reloc::PIC_;
2830b57cec5SDimitry Andric   // On ELF platforms the default static relocation model has a smart enough
2840b57cec5SDimitry Andric   // linker to cope with referencing external symbols defined in a shared
2850b57cec5SDimitry Andric   // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
28681ad6265SDimitry Andric   if (!RM || *RM == Reloc::DynamicNoPIC)
2870b57cec5SDimitry Andric     return Reloc::Static;
2880b57cec5SDimitry Andric   return *RM;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric static CodeModel::Model
292*bdd1243dSDimitry Andric getEffectiveAArch64CodeModel(const Triple &TT,
293*bdd1243dSDimitry Andric                              std::optional<CodeModel::Model> CM, bool JIT) {
2940b57cec5SDimitry Andric   if (CM) {
2950b57cec5SDimitry Andric     if (*CM != CodeModel::Small && *CM != CodeModel::Tiny &&
2960b57cec5SDimitry Andric         *CM != CodeModel::Large) {
2970b57cec5SDimitry Andric       report_fatal_error(
2980b57cec5SDimitry Andric           "Only small, tiny and large code models are allowed on AArch64");
2990b57cec5SDimitry Andric     } else if (*CM == CodeModel::Tiny && !TT.isOSBinFormatELF())
3000b57cec5SDimitry Andric       report_fatal_error("tiny code model is only supported on ELF");
3010b57cec5SDimitry Andric     return *CM;
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric   // The default MCJIT memory managers make no guarantees about where they can
3040b57cec5SDimitry Andric   // find an executable page; JITed code needs to be able to refer to globals
3050b57cec5SDimitry Andric   // no matter how far away they are.
306480093f4SDimitry Andric   // We should set the CodeModel::Small for Windows ARM64 in JIT mode,
307480093f4SDimitry Andric   // since with large code model LLVM generating 4 MOV instructions, and
308480093f4SDimitry Andric   // Windows doesn't support relocating these long branch (4 MOVs).
309480093f4SDimitry Andric   if (JIT && !TT.isOSWindows())
3100b57cec5SDimitry Andric     return CodeModel::Large;
3110b57cec5SDimitry Andric   return CodeModel::Small;
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric /// Create an AArch64 architecture model.
3150b57cec5SDimitry Andric ///
3160b57cec5SDimitry Andric AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
3170b57cec5SDimitry Andric                                            StringRef CPU, StringRef FS,
3180b57cec5SDimitry Andric                                            const TargetOptions &Options,
319*bdd1243dSDimitry Andric                                            std::optional<Reloc::Model> RM,
320*bdd1243dSDimitry Andric                                            std::optional<CodeModel::Model> CM,
3210b57cec5SDimitry Andric                                            CodeGenOpt::Level OL, bool JIT,
3220b57cec5SDimitry Andric                                            bool LittleEndian)
3230b57cec5SDimitry Andric     : LLVMTargetMachine(T,
3240b57cec5SDimitry Andric                         computeDataLayout(TT, Options.MCOptions, LittleEndian),
325e8d8bef9SDimitry Andric                         TT, computeDefaultCPU(TT, CPU), FS, Options,
326e8d8bef9SDimitry Andric                         getEffectiveRelocModel(TT, RM),
3270b57cec5SDimitry Andric                         getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
3280b57cec5SDimitry Andric       TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
3290b57cec5SDimitry Andric   initAsmInfo();
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO()) {
3320b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
3330b57cec5SDimitry Andric     this->Options.NoTrapAfterNoreturn = true;
3340b57cec5SDimitry Andric   }
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   if (getMCAsmInfo()->usesWindowsCFI()) {
3370b57cec5SDimitry Andric     // Unwinding can get confused if the last instruction in an
3380b57cec5SDimitry Andric     // exception-handling region (function, funclet, try block, etc.)
3390b57cec5SDimitry Andric     // is a call.
3400b57cec5SDimitry Andric     //
3410b57cec5SDimitry Andric     // FIXME: We could elide the trap if the next instruction would be in
3420b57cec5SDimitry Andric     // the same region anyway.
3430b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric 
346480093f4SDimitry Andric   if (this->Options.TLSSize == 0) // default
347480093f4SDimitry Andric     this->Options.TLSSize = 24;
348480093f4SDimitry Andric   if ((getCodeModel() == CodeModel::Small ||
349480093f4SDimitry Andric        getCodeModel() == CodeModel::Kernel) &&
350480093f4SDimitry Andric       this->Options.TLSSize > 32)
351480093f4SDimitry Andric     // for the small (and kernel) code model, the maximum TLS size is 4GiB
352480093f4SDimitry Andric     this->Options.TLSSize = 32;
353480093f4SDimitry Andric   else if (getCodeModel() == CodeModel::Tiny && this->Options.TLSSize > 24)
354480093f4SDimitry Andric     // for the tiny code model, the maximum TLS size is 1MiB (< 16MiB)
355480093f4SDimitry Andric     this->Options.TLSSize = 24;
356480093f4SDimitry Andric 
3578bcb0991SDimitry Andric   // Enable GlobalISel at or below EnableGlobalISelAt0, unless this is
3588bcb0991SDimitry Andric   // MachO/CodeModel::Large, which GlobalISel does not support.
3598bcb0991SDimitry Andric   if (getOptLevel() <= EnableGlobalISelAtO &&
3608bcb0991SDimitry Andric       TT.getArch() != Triple::aarch64_32 &&
361e8d8bef9SDimitry Andric       TT.getEnvironment() != Triple::GNUILP32 &&
3628bcb0991SDimitry Andric       !(getCodeModel() == CodeModel::Large && TT.isOSBinFormatMachO())) {
3630b57cec5SDimitry Andric     setGlobalISel(true);
3640b57cec5SDimitry Andric     setGlobalISelAbort(GlobalISelAbortMode::Disable);
3650b57cec5SDimitry Andric   }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   // AArch64 supports the MachineOutliner.
3680b57cec5SDimitry Andric   setMachineOutliner(true);
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   // AArch64 supports default outlining behaviour.
3710b57cec5SDimitry Andric   setSupportsDefaultOutlining(true);
3725ffd83dbSDimitry Andric 
3735ffd83dbSDimitry Andric   // AArch64 supports the debug entry values.
3745ffd83dbSDimitry Andric   setSupportsDebugEntryValues(true);
37581ad6265SDimitry Andric 
37681ad6265SDimitry Andric   // AArch64 supports fixing up the DWARF unwind information.
37781ad6265SDimitry Andric   if (!getMCAsmInfo()->usesWindowsCFI())
37881ad6265SDimitry Andric     setCFIFixup(true);
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric AArch64TargetMachine::~AArch64TargetMachine() = default;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric const AArch64Subtarget *
3840b57cec5SDimitry Andric AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
3850b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
386349cc55cSDimitry Andric   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
3870b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
3880b57cec5SDimitry Andric 
389*bdd1243dSDimitry Andric   StringRef CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString() : TargetCPU;
390*bdd1243dSDimitry Andric   StringRef TuneCPU = TuneAttr.isValid() ? TuneAttr.getValueAsString() : CPU;
391*bdd1243dSDimitry Andric   StringRef FS = FSAttr.isValid() ? FSAttr.getValueAsString() : TargetFS;
3920b57cec5SDimitry Andric 
393*bdd1243dSDimitry Andric   bool StreamingSVEModeDisabled =
394*bdd1243dSDimitry Andric       !F.hasFnAttribute("aarch64_pstate_sm_enabled") &&
395*bdd1243dSDimitry Andric       !F.hasFnAttribute("aarch64_pstate_sm_compatible") &&
396*bdd1243dSDimitry Andric       !F.hasFnAttribute("aarch64_pstate_sm_body");
397fe6060f1SDimitry Andric 
398fe6060f1SDimitry Andric   unsigned MinSVEVectorSize = 0;
399fe6060f1SDimitry Andric   unsigned MaxSVEVectorSize = 0;
400fe6060f1SDimitry Andric   Attribute VScaleRangeAttr = F.getFnAttribute(Attribute::VScaleRange);
401fe6060f1SDimitry Andric   if (VScaleRangeAttr.isValid()) {
402*bdd1243dSDimitry Andric     std::optional<unsigned> VScaleMax = VScaleRangeAttr.getVScaleRangeMax();
4030eae32dcSDimitry Andric     MinSVEVectorSize = VScaleRangeAttr.getVScaleRangeMin() * 128;
40481ad6265SDimitry Andric     MaxSVEVectorSize = VScaleMax ? *VScaleMax * 128 : 0;
405fe6060f1SDimitry Andric   } else {
406fe6060f1SDimitry Andric     MinSVEVectorSize = SVEVectorBitsMinOpt;
407fe6060f1SDimitry Andric     MaxSVEVectorSize = SVEVectorBitsMaxOpt;
408fe6060f1SDimitry Andric   }
409fe6060f1SDimitry Andric 
410fe6060f1SDimitry Andric   assert(MinSVEVectorSize % 128 == 0 &&
411fe6060f1SDimitry Andric          "SVE requires vector length in multiples of 128!");
412fe6060f1SDimitry Andric   assert(MaxSVEVectorSize % 128 == 0 &&
413fe6060f1SDimitry Andric          "SVE requires vector length in multiples of 128!");
414fe6060f1SDimitry Andric   assert((MaxSVEVectorSize >= MinSVEVectorSize || MaxSVEVectorSize == 0) &&
415fe6060f1SDimitry Andric          "Minimum SVE vector size should not be larger than its maximum!");
416fe6060f1SDimitry Andric 
417fe6060f1SDimitry Andric   // Sanitize user input in case of no asserts
418fe6060f1SDimitry Andric   if (MaxSVEVectorSize == 0)
419fe6060f1SDimitry Andric     MinSVEVectorSize = (MinSVEVectorSize / 128) * 128;
420fe6060f1SDimitry Andric   else {
421fe6060f1SDimitry Andric     MinSVEVectorSize =
422fe6060f1SDimitry Andric         (std::min(MinSVEVectorSize, MaxSVEVectorSize) / 128) * 128;
423fe6060f1SDimitry Andric     MaxSVEVectorSize =
424fe6060f1SDimitry Andric         (std::max(MinSVEVectorSize, MaxSVEVectorSize) / 128) * 128;
425fe6060f1SDimitry Andric   }
426fe6060f1SDimitry Andric 
427*bdd1243dSDimitry Andric   SmallString<512> Key;
428*bdd1243dSDimitry Andric   raw_svector_ostream(Key) << "SVEMin" << MinSVEVectorSize << "SVEMax"
429*bdd1243dSDimitry Andric                            << MaxSVEVectorSize << "StreamingSVEModeDisabled="
430*bdd1243dSDimitry Andric                            << StreamingSVEModeDisabled << CPU << TuneCPU << FS;
431fe6060f1SDimitry Andric 
432fe6060f1SDimitry Andric   auto &I = SubtargetMap[Key];
4330b57cec5SDimitry Andric   if (!I) {
4340b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
4350b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
4360b57cec5SDimitry Andric     // function that reside in TargetOptions.
4370b57cec5SDimitry Andric     resetTargetOptions(F);
438*bdd1243dSDimitry Andric     I = std::make_unique<AArch64Subtarget>(
439*bdd1243dSDimitry Andric         TargetTriple, CPU, TuneCPU, FS, *this, isLittle, MinSVEVectorSize,
440*bdd1243dSDimitry Andric         MaxSVEVectorSize, StreamingSVEModeDisabled);
4410b57cec5SDimitry Andric   }
4420b57cec5SDimitry Andric   return I.get();
4430b57cec5SDimitry Andric }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric void AArch64leTargetMachine::anchor() { }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric AArch64leTargetMachine::AArch64leTargetMachine(
4480b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
449*bdd1243dSDimitry Andric     const TargetOptions &Options, std::optional<Reloc::Model> RM,
450*bdd1243dSDimitry Andric     std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
4510b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric void AArch64beTargetMachine::anchor() { }
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric AArch64beTargetMachine::AArch64beTargetMachine(
4560b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
457*bdd1243dSDimitry Andric     const TargetOptions &Options, std::optional<Reloc::Model> RM,
458*bdd1243dSDimitry Andric     std::optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
4590b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric namespace {
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric /// AArch64 Code Generator Pass Configuration Options.
4640b57cec5SDimitry Andric class AArch64PassConfig : public TargetPassConfig {
4650b57cec5SDimitry Andric public:
4660b57cec5SDimitry Andric   AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM)
4670b57cec5SDimitry Andric       : TargetPassConfig(TM, PM) {
4680b57cec5SDimitry Andric     if (TM.getOptLevel() != CodeGenOpt::None)
4690b57cec5SDimitry Andric       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   AArch64TargetMachine &getAArch64TargetMachine() const {
4730b57cec5SDimitry Andric     return getTM<AArch64TargetMachine>();
4740b57cec5SDimitry Andric   }
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric   ScheduleDAGInstrs *
4770b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
4780b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
4790b57cec5SDimitry Andric     ScheduleDAGMILive *DAG = createGenericSchedLive(C);
4800b57cec5SDimitry Andric     DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
4810b57cec5SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
4820b57cec5SDimitry Andric     if (ST.hasFusion())
4830b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
4840b57cec5SDimitry Andric     return DAG;
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   ScheduleDAGInstrs *
4880b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
4890b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
49081ad6265SDimitry Andric     ScheduleDAGMI *DAG =
49181ad6265SDimitry Andric         new ScheduleDAGMI(C, std::make_unique<AArch64PostRASchedStrategy>(C),
49281ad6265SDimitry Andric                           /* RemoveKillFlags=*/true);
4930b57cec5SDimitry Andric     if (ST.hasFusion()) {
4940b57cec5SDimitry Andric       // Run the Macro Fusion after RA again since literals are expanded from
4950b57cec5SDimitry Andric       // pseudos then (v. addPreSched2()).
4960b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
4970b57cec5SDimitry Andric       return DAG;
4980b57cec5SDimitry Andric     }
4990b57cec5SDimitry Andric 
50081ad6265SDimitry Andric     return DAG;
5010b57cec5SDimitry Andric   }
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   void addIRPasses()  override;
5040b57cec5SDimitry Andric   bool addPreISel() override;
505349cc55cSDimitry Andric   void addCodeGenPrepare() override;
5060b57cec5SDimitry Andric   bool addInstSelector() override;
5070b57cec5SDimitry Andric   bool addIRTranslator() override;
5080b57cec5SDimitry Andric   void addPreLegalizeMachineIR() override;
5090b57cec5SDimitry Andric   bool addLegalizeMachineIR() override;
5105ffd83dbSDimitry Andric   void addPreRegBankSelect() override;
5110b57cec5SDimitry Andric   bool addRegBankSelect() override;
5120b57cec5SDimitry Andric   void addPreGlobalInstructionSelect() override;
5130b57cec5SDimitry Andric   bool addGlobalInstructionSelect() override;
514349cc55cSDimitry Andric   void addMachineSSAOptimization() override;
5150b57cec5SDimitry Andric   bool addILPOpts() override;
5160b57cec5SDimitry Andric   void addPreRegAlloc() override;
5170b57cec5SDimitry Andric   void addPostRegAlloc() override;
5180b57cec5SDimitry Andric   void addPreSched2() override;
5190b57cec5SDimitry Andric   void addPreEmitPass() override;
520fe6060f1SDimitry Andric   void addPreEmitPass2() override;
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
5230b57cec5SDimitry Andric };
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric } // end anonymous namespace
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric TargetTransformInfo
52881ad6265SDimitry Andric AArch64TargetMachine::getTargetTransformInfo(const Function &F) const {
5290b57cec5SDimitry Andric   return TargetTransformInfo(AArch64TTIImpl(this, F));
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
5330b57cec5SDimitry Andric   return new AArch64PassConfig(*this, PM);
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> AArch64PassConfig::getCSEConfig() const {
5370b57cec5SDimitry Andric   return getStandardCSEConfigForOpt(TM->getOptLevel());
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric void AArch64PassConfig::addIRPasses() {
5410b57cec5SDimitry Andric   // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
5420b57cec5SDimitry Andric   // ourselves.
5430b57cec5SDimitry Andric   addPass(createAtomicExpandPass());
5440b57cec5SDimitry Andric 
5455ffd83dbSDimitry Andric   // Expand any SVE vector library calls that we can't code generate directly.
5465ffd83dbSDimitry Andric   if (EnableSVEIntrinsicOpts && TM->getOptLevel() == CodeGenOpt::Aggressive)
5475ffd83dbSDimitry Andric     addPass(createSVEIntrinsicOptsPass());
5485ffd83dbSDimitry Andric 
5490b57cec5SDimitry Andric   // Cmpxchg instructions are often used with a subsequent comparison to
5500b57cec5SDimitry Andric   // determine whether it succeeded. We can exploit existing control-flow in
5510b57cec5SDimitry Andric   // ldrex/strex loops to simplify this, but it needs tidying up.
5520b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
553e8d8bef9SDimitry Andric     addPass(createCFGSimplificationPass(SimplifyCFGOptions()
554e8d8bef9SDimitry Andric                                             .forwardSwitchCondToPhi(true)
555fb03ea46SDimitry Andric                                             .convertSwitchRangeToICmp(true)
556e8d8bef9SDimitry Andric                                             .convertSwitchToLookupTable(true)
557e8d8bef9SDimitry Andric                                             .needCanonicalLoops(false)
558e8d8bef9SDimitry Andric                                             .hoistCommonInsts(true)
559e8d8bef9SDimitry Andric                                             .sinkCommonInsts(true)));
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   // Run LoopDataPrefetch
5620b57cec5SDimitry Andric   //
5630b57cec5SDimitry Andric   // Run this before LSR to remove the multiplies involved in computing the
5640b57cec5SDimitry Andric   // pointer values N iterations ahead.
5650b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
5660b57cec5SDimitry Andric     if (EnableLoopDataPrefetch)
5670b57cec5SDimitry Andric       addPass(createLoopDataPrefetchPass());
5680b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
5690b57cec5SDimitry Andric       addPass(createFalkorMarkStridedAccessesPass());
5700b57cec5SDimitry Andric   }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
5730b57cec5SDimitry Andric     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
5740b57cec5SDimitry Andric     // and lower a GEP with multiple indices to either arithmetic operations or
5750b57cec5SDimitry Andric     // multiple GEPs with single index.
5760b57cec5SDimitry Andric     addPass(createSeparateConstOffsetFromGEPPass(true));
5770b57cec5SDimitry Andric     // Call EarlyCSE pass to find and remove subexpressions in the lowered
5780b57cec5SDimitry Andric     // result.
5790b57cec5SDimitry Andric     addPass(createEarlyCSEPass());
5800b57cec5SDimitry Andric     // Do loop invariant code motion in case part of the lowered result is
5810b57cec5SDimitry Andric     // invariant.
5820b57cec5SDimitry Andric     addPass(createLICMPass());
5830b57cec5SDimitry Andric   }
5840b57cec5SDimitry Andric 
585fcaf7f86SDimitry Andric   TargetPassConfig::addIRPasses();
586fcaf7f86SDimitry Andric 
587*bdd1243dSDimitry Andric   if (getOptLevel() == CodeGenOpt::Aggressive && EnableSelectOpt)
588*bdd1243dSDimitry Andric     addPass(createSelectOptimizePass());
589*bdd1243dSDimitry Andric 
590fcaf7f86SDimitry Andric   addPass(createAArch64StackTaggingPass(
591fcaf7f86SDimitry Andric       /*IsOptNone=*/TM->getOptLevel() == CodeGenOpt::None));
592fcaf7f86SDimitry Andric 
593*bdd1243dSDimitry Andric   // Match complex arithmetic patterns
594*bdd1243dSDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Default)
595*bdd1243dSDimitry Andric     addPass(createComplexDeinterleavingPass(TM));
596*bdd1243dSDimitry Andric 
597fcaf7f86SDimitry Andric   // Match interleaved memory accesses to ldN/stN intrinsics.
598fcaf7f86SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
599fcaf7f86SDimitry Andric     addPass(createInterleavedLoadCombinePass());
600fcaf7f86SDimitry Andric     addPass(createInterleavedAccessPass());
601fcaf7f86SDimitry Andric   }
602fcaf7f86SDimitry Andric 
603*bdd1243dSDimitry Andric   // Expand any functions marked with SME attributes which require special
604*bdd1243dSDimitry Andric   // changes for the calling convention or that require the lazy-saving
605*bdd1243dSDimitry Andric   // mechanism specified in the SME ABI.
606*bdd1243dSDimitry Andric   addPass(createSMEABIPass());
607*bdd1243dSDimitry Andric 
608480093f4SDimitry Andric   // Add Control Flow Guard checks.
609480093f4SDimitry Andric   if (TM->getTargetTriple().isOSWindows())
610480093f4SDimitry Andric     addPass(createCFGuardCheckPass());
61181ad6265SDimitry Andric 
61281ad6265SDimitry Andric   if (TM->Options.JMCInstrument)
61381ad6265SDimitry Andric     addPass(createJMCInstrumenterPass());
6140b57cec5SDimitry Andric }
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric // Pass Pipeline Configuration
6170b57cec5SDimitry Andric bool AArch64PassConfig::addPreISel() {
6180b57cec5SDimitry Andric   // Run promote constant before global merge, so that the promoted constants
6190b57cec5SDimitry Andric   // get a chance to be merged
6200b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
6210b57cec5SDimitry Andric     addPass(createAArch64PromoteConstantPass());
6220b57cec5SDimitry Andric   // FIXME: On AArch64, this depends on the type.
6230b57cec5SDimitry Andric   // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
6240b57cec5SDimitry Andric   // and the offset has to be a multiple of the related size in bytes.
6250b57cec5SDimitry Andric   if ((TM->getOptLevel() != CodeGenOpt::None &&
6260b57cec5SDimitry Andric        EnableGlobalMerge == cl::BOU_UNSET) ||
6270b57cec5SDimitry Andric       EnableGlobalMerge == cl::BOU_TRUE) {
6280b57cec5SDimitry Andric     bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
6290b57cec5SDimitry Andric                                (EnableGlobalMerge == cl::BOU_UNSET);
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric     // Merging of extern globals is enabled by default on non-Mach-O as we
6320b57cec5SDimitry Andric     // expect it to be generally either beneficial or harmless. On Mach-O it
6330b57cec5SDimitry Andric     // is disabled as we emit the .subsections_via_symbols directive which
6340b57cec5SDimitry Andric     // means that merging extern globals is not safe.
6350b57cec5SDimitry Andric     bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric     // FIXME: extern global merging is only enabled when we optimise for size
6380b57cec5SDimitry Andric     // because there are some regressions with it also enabled for performance.
6390b57cec5SDimitry Andric     if (!OnlyOptimizeForSize)
6400b57cec5SDimitry Andric       MergeExternalByDefault = false;
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric     addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize,
6430b57cec5SDimitry Andric                                   MergeExternalByDefault));
6440b57cec5SDimitry Andric   }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   return false;
6470b57cec5SDimitry Andric }
6480b57cec5SDimitry Andric 
649349cc55cSDimitry Andric void AArch64PassConfig::addCodeGenPrepare() {
650349cc55cSDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
651*bdd1243dSDimitry Andric     addPass(createTypePromotionLegacyPass());
652349cc55cSDimitry Andric   TargetPassConfig::addCodeGenPrepare();
653349cc55cSDimitry Andric }
654349cc55cSDimitry Andric 
6550b57cec5SDimitry Andric bool AArch64PassConfig::addInstSelector() {
6560b57cec5SDimitry Andric   addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric   // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
6590b57cec5SDimitry Andric   // references to _TLS_MODULE_BASE_ as possible.
6600b57cec5SDimitry Andric   if (TM->getTargetTriple().isOSBinFormatELF() &&
6610b57cec5SDimitry Andric       getOptLevel() != CodeGenOpt::None)
6620b57cec5SDimitry Andric     addPass(createAArch64CleanupLocalDynamicTLSPass());
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   return false;
6650b57cec5SDimitry Andric }
6660b57cec5SDimitry Andric 
6670b57cec5SDimitry Andric bool AArch64PassConfig::addIRTranslator() {
668e8d8bef9SDimitry Andric   addPass(new IRTranslator(getOptLevel()));
6690b57cec5SDimitry Andric   return false;
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric void AArch64PassConfig::addPreLegalizeMachineIR() {
673fe6060f1SDimitry Andric   if (getOptLevel() == CodeGenOpt::None)
674fe6060f1SDimitry Andric     addPass(createAArch64O0PreLegalizerCombiner());
675349cc55cSDimitry Andric   else {
676fe6060f1SDimitry Andric     addPass(createAArch64PreLegalizerCombiner());
677349cc55cSDimitry Andric     if (EnableGISelLoadStoreOptPreLegal)
678349cc55cSDimitry Andric       addPass(new LoadStoreOpt());
679349cc55cSDimitry Andric   }
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric bool AArch64PassConfig::addLegalizeMachineIR() {
6830b57cec5SDimitry Andric   addPass(new Legalizer());
6840b57cec5SDimitry Andric   return false;
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
6875ffd83dbSDimitry Andric void AArch64PassConfig::addPreRegBankSelect() {
6885ffd83dbSDimitry Andric   bool IsOptNone = getOptLevel() == CodeGenOpt::None;
689349cc55cSDimitry Andric   if (!IsOptNone) {
690e8d8bef9SDimitry Andric     addPass(createAArch64PostLegalizerCombiner(IsOptNone));
691349cc55cSDimitry Andric     if (EnableGISelLoadStoreOptPostLegal)
692349cc55cSDimitry Andric       addPass(new LoadStoreOpt());
693349cc55cSDimitry Andric   }
694e8d8bef9SDimitry Andric   addPass(createAArch64PostLegalizerLowering());
6955ffd83dbSDimitry Andric }
6965ffd83dbSDimitry Andric 
6970b57cec5SDimitry Andric bool AArch64PassConfig::addRegBankSelect() {
6980b57cec5SDimitry Andric   addPass(new RegBankSelect());
6990b57cec5SDimitry Andric   return false;
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric void AArch64PassConfig::addPreGlobalInstructionSelect() {
7030b57cec5SDimitry Andric   addPass(new Localizer());
7040b57cec5SDimitry Andric }
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric bool AArch64PassConfig::addGlobalInstructionSelect() {
707fe6060f1SDimitry Andric   addPass(new InstructionSelect(getOptLevel()));
708e8d8bef9SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
709e8d8bef9SDimitry Andric     addPass(createAArch64PostSelectOptimize());
7100b57cec5SDimitry Andric   return false;
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric 
713349cc55cSDimitry Andric void AArch64PassConfig::addMachineSSAOptimization() {
714349cc55cSDimitry Andric   // Run default MachineSSAOptimization first.
715349cc55cSDimitry Andric   TargetPassConfig::addMachineSSAOptimization();
716349cc55cSDimitry Andric 
717349cc55cSDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
718349cc55cSDimitry Andric     addPass(createAArch64MIPeepholeOptPass());
719349cc55cSDimitry Andric }
720349cc55cSDimitry Andric 
7210b57cec5SDimitry Andric bool AArch64PassConfig::addILPOpts() {
7220b57cec5SDimitry Andric   if (EnableCondOpt)
7230b57cec5SDimitry Andric     addPass(createAArch64ConditionOptimizerPass());
7240b57cec5SDimitry Andric   if (EnableCCMP)
7250b57cec5SDimitry Andric     addPass(createAArch64ConditionalCompares());
7260b57cec5SDimitry Andric   if (EnableMCR)
7270b57cec5SDimitry Andric     addPass(&MachineCombinerID);
7280b57cec5SDimitry Andric   if (EnableCondBrTuning)
7290b57cec5SDimitry Andric     addPass(createAArch64CondBrTuning());
7300b57cec5SDimitry Andric   if (EnableEarlyIfConversion)
7310b57cec5SDimitry Andric     addPass(&EarlyIfConverterID);
7320b57cec5SDimitry Andric   if (EnableStPairSuppress)
7330b57cec5SDimitry Andric     addPass(createAArch64StorePairSuppressPass());
7340b57cec5SDimitry Andric   addPass(createAArch64SIMDInstrOptPass());
7358bcb0991SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
7368bcb0991SDimitry Andric     addPass(createAArch64StackTaggingPreRAPass());
7370b57cec5SDimitry Andric   return true;
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric void AArch64PassConfig::addPreRegAlloc() {
7410b57cec5SDimitry Andric   // Change dead register definitions to refer to the zero register.
7420b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
7430b57cec5SDimitry Andric     addPass(createAArch64DeadRegisterDefinitions());
7440b57cec5SDimitry Andric 
7450b57cec5SDimitry Andric   // Use AdvSIMD scalar instructions whenever profitable.
7460b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
7470b57cec5SDimitry Andric     addPass(createAArch64AdvSIMDScalar());
7480b57cec5SDimitry Andric     // The AdvSIMD pass may produce copies that can be rewritten to
749480093f4SDimitry Andric     // be register coalescer friendly.
7500b57cec5SDimitry Andric     addPass(&PeepholeOptimizerID);
7510b57cec5SDimitry Andric   }
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric void AArch64PassConfig::addPostRegAlloc() {
7550b57cec5SDimitry Andric   // Remove redundant copy instructions.
7560b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
7570b57cec5SDimitry Andric     addPass(createAArch64RedundantCopyEliminationPass());
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
7600b57cec5SDimitry Andric     // Improve performance for some FP/SIMD code for A57.
7610b57cec5SDimitry Andric     addPass(createAArch64A57FPLoadBalancing());
7620b57cec5SDimitry Andric }
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric void AArch64PassConfig::addPreSched2() {
765fe6060f1SDimitry Andric   // Lower homogeneous frame instructions
766fe6060f1SDimitry Andric   if (EnableHomogeneousPrologEpilog)
767fe6060f1SDimitry Andric     addPass(createAArch64LowerHomogeneousPrologEpilogPass());
7680b57cec5SDimitry Andric   // Expand some pseudo instructions to allow proper scheduling.
7690b57cec5SDimitry Andric   addPass(createAArch64ExpandPseudoPass());
7700b57cec5SDimitry Andric   // Use load/store pair instructions when possible.
7710b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
7720b57cec5SDimitry Andric     if (EnableLoadStoreOpt)
7730b57cec5SDimitry Andric       addPass(createAArch64LoadStoreOptimizationPass());
7740b57cec5SDimitry Andric   }
775*bdd1243dSDimitry Andric   // Emit KCFI checks for indirect calls.
776*bdd1243dSDimitry Andric   addPass(createAArch64KCFIPass());
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric   // The AArch64SpeculationHardeningPass destroys dominator tree and natural
7790b57cec5SDimitry Andric   // loop info, which is needed for the FalkorHWPFFixPass and also later on.
7800b57cec5SDimitry Andric   // Therefore, run the AArch64SpeculationHardeningPass before the
7810b57cec5SDimitry Andric   // FalkorHWPFFixPass to avoid recomputing dominator tree and natural loop
7820b57cec5SDimitry Andric   // info.
7830b57cec5SDimitry Andric   addPass(createAArch64SpeculationHardeningPass());
7840b57cec5SDimitry Andric 
7855ffd83dbSDimitry Andric   addPass(createAArch64IndirectThunks());
7865ffd83dbSDimitry Andric   addPass(createAArch64SLSHardeningPass());
7875ffd83dbSDimitry Andric 
7880b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
7890b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
7900b57cec5SDimitry Andric       addPass(createFalkorHWPFFixPass());
7910b57cec5SDimitry Andric   }
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric void AArch64PassConfig::addPreEmitPass() {
7950b57cec5SDimitry Andric   // Machine Block Placement might have created new opportunities when run
7960b57cec5SDimitry Andric   // at O3, where the Tail Duplication Threshold is set to 4 instructions.
7970b57cec5SDimitry Andric   // Run the load/store optimizer once more.
7980b57cec5SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Aggressive && EnableLoadStoreOpt)
7990b57cec5SDimitry Andric     addPass(createAArch64LoadStoreOptimizationPass());
8000b57cec5SDimitry Andric 
80181ad6265SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Aggressive &&
80281ad6265SDimitry Andric       EnableAArch64CopyPropagation)
80381ad6265SDimitry Andric     addPass(createMachineCopyPropagationPass(true));
80481ad6265SDimitry Andric 
8050b57cec5SDimitry Andric   addPass(createAArch64A53Fix835769());
806480093f4SDimitry Andric 
807480093f4SDimitry Andric   if (EnableBranchTargets)
808480093f4SDimitry Andric     addPass(createAArch64BranchTargetsPass());
809480093f4SDimitry Andric 
8100b57cec5SDimitry Andric   // Relax conditional branch instructions if they're otherwise out of
8110b57cec5SDimitry Andric   // range of their destination.
8120b57cec5SDimitry Andric   if (BranchRelaxation)
8130b57cec5SDimitry Andric     addPass(&BranchRelaxationPassID);
8140b57cec5SDimitry Andric 
815fe6060f1SDimitry Andric   if (TM->getTargetTriple().isOSWindows()) {
816480093f4SDimitry Andric     // Identify valid longjmp targets for Windows Control Flow Guard.
817480093f4SDimitry Andric     addPass(createCFGuardLongjmpPass());
818fe6060f1SDimitry Andric     // Identify valid eh continuation targets for Windows EHCont Guard.
819fe6060f1SDimitry Andric     addPass(createEHContGuardCatchretPass());
820fe6060f1SDimitry Andric   }
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCompressJumpTables)
8230b57cec5SDimitry Andric     addPass(createAArch64CompressJumpTablesPass());
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
8260b57cec5SDimitry Andric       TM->getTargetTriple().isOSBinFormatMachO())
8270b57cec5SDimitry Andric     addPass(createAArch64CollectLOHPass());
828fe6060f1SDimitry Andric }
8295ffd83dbSDimitry Andric 
830fe6060f1SDimitry Andric void AArch64PassConfig::addPreEmitPass2() {
831fe6060f1SDimitry Andric   // SVE bundles move prefixes with destructive operations. BLR_RVMARKER pseudo
832fe6060f1SDimitry Andric   // instructions are lowered to bundles as well.
8335ffd83dbSDimitry Andric   addPass(createUnpackMachineBundles(nullptr));
8345ffd83dbSDimitry Andric }
8355ffd83dbSDimitry Andric 
836*bdd1243dSDimitry Andric MachineFunctionInfo *AArch64TargetMachine::createMachineFunctionInfo(
837*bdd1243dSDimitry Andric     BumpPtrAllocator &Allocator, const Function &F,
838*bdd1243dSDimitry Andric     const TargetSubtargetInfo *STI) const {
839*bdd1243dSDimitry Andric   return AArch64FunctionInfo::create<AArch64FunctionInfo>(
840*bdd1243dSDimitry Andric       Allocator, F, static_cast<const AArch64Subtarget *>(STI));
841*bdd1243dSDimitry Andric }
842*bdd1243dSDimitry Andric 
8435ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
8445ffd83dbSDimitry Andric AArch64TargetMachine::createDefaultFuncInfoYAML() const {
8455ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo();
8465ffd83dbSDimitry Andric }
8475ffd83dbSDimitry Andric 
8485ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
8495ffd83dbSDimitry Andric AArch64TargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
8505ffd83dbSDimitry Andric   const auto *MFI = MF.getInfo<AArch64FunctionInfo>();
8515ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo(*MFI);
8525ffd83dbSDimitry Andric }
8535ffd83dbSDimitry Andric 
8545ffd83dbSDimitry Andric bool AArch64TargetMachine::parseMachineFunctionInfo(
8555ffd83dbSDimitry Andric     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
8565ffd83dbSDimitry Andric     SMDiagnostic &Error, SMRange &SourceRange) const {
85781ad6265SDimitry Andric   const auto &YamlMFI = static_cast<const yaml::AArch64FunctionInfo &>(MFI);
8585ffd83dbSDimitry Andric   MachineFunction &MF = PFS.MF;
8595ffd83dbSDimitry Andric   MF.getInfo<AArch64FunctionInfo>()->initializeBaseYamlFields(YamlMFI);
8605ffd83dbSDimitry Andric   return false;
8610b57cec5SDimitry Andric }
862