xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp (revision fb03ea46eb853b2d128828e9d82882125bcc1657)
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"
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"
28349cc55cSDimitry Andric #include "llvm/CodeGen/GlobalISel/LoadStoreOpt.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
315ffd83dbSDimitry Andric #include "llvm/CodeGen/MIRParser/MIParser.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
350b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
360b57cec5SDimitry Andric #include "llvm/IR/Function.h"
37480093f4SDimitry Andric #include "llvm/InitializePasses.h"
380b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
390b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
40349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
410b57cec5SDimitry Andric #include "llvm/Pass.h"
420b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
430b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
440b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
450b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
46480093f4SDimitry Andric #include "llvm/Transforms/CFGuard.h"
470b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
480b57cec5SDimitry Andric #include <memory>
490b57cec5SDimitry Andric #include <string>
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric using namespace llvm;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
540b57cec5SDimitry Andric                                 cl::desc("Enable the CCMP formation pass"),
550b57cec5SDimitry Andric                                 cl::init(true), cl::Hidden);
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric static cl::opt<bool>
580b57cec5SDimitry Andric     EnableCondBrTuning("aarch64-enable-cond-br-tune",
590b57cec5SDimitry Andric                        cl::desc("Enable the conditional branch tuning pass"),
600b57cec5SDimitry Andric                        cl::init(true), cl::Hidden);
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
630b57cec5SDimitry Andric                                cl::desc("Enable the machine combiner pass"),
640b57cec5SDimitry Andric                                cl::init(true), cl::Hidden);
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
670b57cec5SDimitry Andric                                           cl::desc("Suppress STP for AArch64"),
680b57cec5SDimitry Andric                                           cl::init(true), cl::Hidden);
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric static cl::opt<bool> EnableAdvSIMDScalar(
710b57cec5SDimitry Andric     "aarch64-enable-simd-scalar",
720b57cec5SDimitry Andric     cl::desc("Enable use of AdvSIMD scalar integer instructions"),
730b57cec5SDimitry Andric     cl::init(false), cl::Hidden);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric static cl::opt<bool>
760b57cec5SDimitry Andric     EnablePromoteConstant("aarch64-enable-promote-const",
770b57cec5SDimitry Andric                           cl::desc("Enable the promote constant pass"),
780b57cec5SDimitry Andric                           cl::init(true), cl::Hidden);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric static cl::opt<bool> EnableCollectLOH(
810b57cec5SDimitry Andric     "aarch64-enable-collect-loh",
820b57cec5SDimitry Andric     cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
830b57cec5SDimitry Andric     cl::init(true), cl::Hidden);
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric static cl::opt<bool>
860b57cec5SDimitry Andric     EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
870b57cec5SDimitry Andric                                   cl::desc("Enable the pass that removes dead"
880b57cec5SDimitry Andric                                            " definitons and replaces stores to"
890b57cec5SDimitry Andric                                            " them with stores to the zero"
900b57cec5SDimitry Andric                                            " register"),
910b57cec5SDimitry Andric                                   cl::init(true));
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric static cl::opt<bool> EnableRedundantCopyElimination(
940b57cec5SDimitry Andric     "aarch64-enable-copyelim",
950b57cec5SDimitry Andric     cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
960b57cec5SDimitry Andric     cl::Hidden);
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
990b57cec5SDimitry Andric                                         cl::desc("Enable the load/store pair"
1000b57cec5SDimitry Andric                                                  " optimization pass"),
1010b57cec5SDimitry Andric                                         cl::init(true), cl::Hidden);
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric static cl::opt<bool> EnableAtomicTidy(
1040b57cec5SDimitry Andric     "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
1050b57cec5SDimitry Andric     cl::desc("Run SimplifyCFG after expanding atomic operations"
1060b57cec5SDimitry Andric              " to make use of cmpxchg flow-based information"),
1070b57cec5SDimitry Andric     cl::init(true));
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric static cl::opt<bool>
1100b57cec5SDimitry Andric EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
1110b57cec5SDimitry Andric                         cl::desc("Run early if-conversion"),
1120b57cec5SDimitry Andric                         cl::init(true));
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric static cl::opt<bool>
1150b57cec5SDimitry Andric     EnableCondOpt("aarch64-enable-condopt",
1160b57cec5SDimitry Andric                   cl::desc("Enable the condition optimizer pass"),
1170b57cec5SDimitry Andric                   cl::init(true), cl::Hidden);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric static cl::opt<bool>
1200b57cec5SDimitry Andric     EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
1210b57cec5SDimitry Andric                  cl::desc("Enable optimizations on complex GEPs"),
1220b57cec5SDimitry Andric                  cl::init(false));
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric static cl::opt<bool>
1250b57cec5SDimitry Andric     BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
1260b57cec5SDimitry Andric                      cl::desc("Relax out of range conditional branches"));
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric static cl::opt<bool> EnableCompressJumpTables(
1290b57cec5SDimitry Andric     "aarch64-enable-compress-jump-tables", cl::Hidden, cl::init(true),
1300b57cec5SDimitry Andric     cl::desc("Use smallest entry possible for jump tables"));
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric // FIXME: Unify control over GlobalMerge.
1330b57cec5SDimitry Andric static cl::opt<cl::boolOrDefault>
1340b57cec5SDimitry Andric     EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
1350b57cec5SDimitry Andric                       cl::desc("Enable the global merge pass"));
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric static cl::opt<bool>
1380b57cec5SDimitry Andric     EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
1390b57cec5SDimitry Andric                            cl::desc("Enable the loop data prefetch pass"),
1400b57cec5SDimitry Andric                            cl::init(true));
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric static cl::opt<int> EnableGlobalISelAtO(
1430b57cec5SDimitry Andric     "aarch64-enable-global-isel-at-O", cl::Hidden,
1440b57cec5SDimitry Andric     cl::desc("Enable GlobalISel at or below an opt level (-1 to disable)"),
1450b57cec5SDimitry Andric     cl::init(0));
1460b57cec5SDimitry Andric 
147e8d8bef9SDimitry Andric static cl::opt<bool>
148e8d8bef9SDimitry Andric     EnableSVEIntrinsicOpts("aarch64-enable-sve-intrinsic-opts", cl::Hidden,
1495ffd83dbSDimitry Andric                            cl::desc("Enable SVE intrinsic opts"),
1505ffd83dbSDimitry Andric                            cl::init(true));
1515ffd83dbSDimitry Andric 
1520b57cec5SDimitry Andric static cl::opt<bool> EnableFalkorHWPFFix("aarch64-enable-falkor-hwpf-fix",
1530b57cec5SDimitry Andric                                          cl::init(true), cl::Hidden);
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric static cl::opt<bool>
1560b57cec5SDimitry Andric     EnableBranchTargets("aarch64-enable-branch-targets", cl::Hidden,
157fe6060f1SDimitry Andric                         cl::desc("Enable the AArch64 branch target pass"),
1580b57cec5SDimitry Andric                         cl::init(true));
1590b57cec5SDimitry Andric 
160fe6060f1SDimitry Andric static cl::opt<unsigned> SVEVectorBitsMaxOpt(
161fe6060f1SDimitry Andric     "aarch64-sve-vector-bits-max",
162fe6060f1SDimitry Andric     cl::desc("Assume SVE vector registers are at most this big, "
163fe6060f1SDimitry Andric              "with zero meaning no maximum size is assumed."),
164fe6060f1SDimitry Andric     cl::init(0), cl::Hidden);
165fe6060f1SDimitry Andric 
166fe6060f1SDimitry Andric static cl::opt<unsigned> SVEVectorBitsMinOpt(
167fe6060f1SDimitry Andric     "aarch64-sve-vector-bits-min",
168fe6060f1SDimitry Andric     cl::desc("Assume SVE vector registers are at least this big, "
169fe6060f1SDimitry Andric              "with zero meaning no minimum size is assumed."),
170fe6060f1SDimitry Andric     cl::init(0), cl::Hidden);
171fe6060f1SDimitry Andric 
172fe6060f1SDimitry Andric extern cl::opt<bool> EnableHomogeneousPrologEpilog;
173fe6060f1SDimitry Andric 
174349cc55cSDimitry Andric static cl::opt<bool> EnableGISelLoadStoreOptPreLegal(
175349cc55cSDimitry Andric     "aarch64-enable-gisel-ldst-prelegal",
176349cc55cSDimitry Andric     cl::desc("Enable GlobalISel's pre-legalizer load/store optimization pass"),
177349cc55cSDimitry Andric     cl::init(true), cl::Hidden);
178349cc55cSDimitry Andric 
179349cc55cSDimitry Andric static cl::opt<bool> EnableGISelLoadStoreOptPostLegal(
180349cc55cSDimitry Andric     "aarch64-enable-gisel-ldst-postlegal",
181349cc55cSDimitry Andric     cl::desc("Enable GlobalISel's post-legalizer load/store optimization pass"),
182349cc55cSDimitry Andric     cl::init(false), cl::Hidden);
183349cc55cSDimitry Andric 
184480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Target() {
1850b57cec5SDimitry Andric   // Register the target.
1860b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
1870b57cec5SDimitry Andric   RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
1880b57cec5SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
1898bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> W(getTheARM64_32Target());
1908bcb0991SDimitry Andric   RegisterTargetMachine<AArch64leTargetMachine> V(getTheAArch64_32Target());
1910b57cec5SDimitry Andric   auto PR = PassRegistry::getPassRegistry();
1920b57cec5SDimitry Andric   initializeGlobalISel(*PR);
1930b57cec5SDimitry Andric   initializeAArch64A53Fix835769Pass(*PR);
1940b57cec5SDimitry Andric   initializeAArch64A57FPLoadBalancingPass(*PR);
1950b57cec5SDimitry Andric   initializeAArch64AdvSIMDScalarPass(*PR);
1960b57cec5SDimitry Andric   initializeAArch64BranchTargetsPass(*PR);
1970b57cec5SDimitry Andric   initializeAArch64CollectLOHPass(*PR);
1980b57cec5SDimitry Andric   initializeAArch64CompressJumpTablesPass(*PR);
1990b57cec5SDimitry Andric   initializeAArch64ConditionalComparesPass(*PR);
2000b57cec5SDimitry Andric   initializeAArch64ConditionOptimizerPass(*PR);
2010b57cec5SDimitry Andric   initializeAArch64DeadRegisterDefinitionsPass(*PR);
2020b57cec5SDimitry Andric   initializeAArch64ExpandPseudoPass(*PR);
2030b57cec5SDimitry Andric   initializeAArch64LoadStoreOptPass(*PR);
204349cc55cSDimitry Andric   initializeAArch64MIPeepholeOptPass(*PR);
2050b57cec5SDimitry Andric   initializeAArch64SIMDInstrOptPass(*PR);
206fe6060f1SDimitry Andric   initializeAArch64O0PreLegalizerCombinerPass(*PR);
2070b57cec5SDimitry Andric   initializeAArch64PreLegalizerCombinerPass(*PR);
2085ffd83dbSDimitry Andric   initializeAArch64PostLegalizerCombinerPass(*PR);
209e8d8bef9SDimitry Andric   initializeAArch64PostLegalizerLoweringPass(*PR);
210e8d8bef9SDimitry Andric   initializeAArch64PostSelectOptimizePass(*PR);
2110b57cec5SDimitry Andric   initializeAArch64PromoteConstantPass(*PR);
2120b57cec5SDimitry Andric   initializeAArch64RedundantCopyEliminationPass(*PR);
2130b57cec5SDimitry Andric   initializeAArch64StorePairSuppressPass(*PR);
2140b57cec5SDimitry Andric   initializeFalkorHWPFFixPass(*PR);
2150b57cec5SDimitry Andric   initializeFalkorMarkStridedAccessesLegacyPass(*PR);
2160b57cec5SDimitry Andric   initializeLDTLSCleanupPass(*PR);
2175ffd83dbSDimitry Andric   initializeSVEIntrinsicOptsPass(*PR);
2180b57cec5SDimitry Andric   initializeAArch64SpeculationHardeningPass(*PR);
2195ffd83dbSDimitry Andric   initializeAArch64SLSHardeningPass(*PR);
2200b57cec5SDimitry Andric   initializeAArch64StackTaggingPass(*PR);
2218bcb0991SDimitry Andric   initializeAArch64StackTaggingPreRAPass(*PR);
222fe6060f1SDimitry Andric   initializeAArch64LowerHomogeneousPrologEpilogPass(*PR);
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2260b57cec5SDimitry Andric // AArch64 Lowering public interface.
2270b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2280b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
2290b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO())
2308bcb0991SDimitry Andric     return std::make_unique<AArch64_MachoTargetObjectFile>();
2310b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2328bcb0991SDimitry Andric     return std::make_unique<AArch64_COFFTargetObjectFile>();
2330b57cec5SDimitry Andric 
2348bcb0991SDimitry Andric   return std::make_unique<AArch64_ELFTargetObjectFile>();
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric // Helper function to build a DataLayout string
2380b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT,
2390b57cec5SDimitry Andric                                      const MCTargetOptions &Options,
2400b57cec5SDimitry Andric                                      bool LittleEndian) {
2418bcb0991SDimitry Andric   if (TT.isOSBinFormatMachO()) {
2428bcb0991SDimitry Andric     if (TT.getArch() == Triple::aarch64_32)
2438bcb0991SDimitry Andric       return "e-m:o-p:32:32-i64:64-i128:128-n32:64-S128";
2440b57cec5SDimitry Andric     return "e-m:o-i64:64-i128:128-n32:64-S128";
2458bcb0991SDimitry Andric   }
2460b57cec5SDimitry Andric   if (TT.isOSBinFormatCOFF())
2470b57cec5SDimitry Andric     return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
248e8d8bef9SDimitry Andric   std::string Endian = LittleEndian ? "e" : "E";
249e8d8bef9SDimitry Andric   std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
250e8d8bef9SDimitry Andric   return Endian + "-m:e" + Ptr32 +
251e8d8bef9SDimitry Andric          "-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
252e8d8bef9SDimitry Andric }
253e8d8bef9SDimitry Andric 
254e8d8bef9SDimitry Andric static StringRef computeDefaultCPU(const Triple &TT, StringRef CPU) {
255e8d8bef9SDimitry Andric   if (CPU.empty() && TT.isArm64e())
256e8d8bef9SDimitry Andric     return "apple-a12";
257e8d8bef9SDimitry Andric   return CPU;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT,
2610b57cec5SDimitry Andric                                            Optional<Reloc::Model> RM) {
2620b57cec5SDimitry Andric   // AArch64 Darwin and Windows are always PIC.
2630b57cec5SDimitry Andric   if (TT.isOSDarwin() || TT.isOSWindows())
2640b57cec5SDimitry Andric     return Reloc::PIC_;
2650b57cec5SDimitry Andric   // On ELF platforms the default static relocation model has a smart enough
2660b57cec5SDimitry Andric   // linker to cope with referencing external symbols defined in a shared
2670b57cec5SDimitry Andric   // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
2680b57cec5SDimitry Andric   if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
2690b57cec5SDimitry Andric     return Reloc::Static;
2700b57cec5SDimitry Andric   return *RM;
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric static CodeModel::Model
2740b57cec5SDimitry Andric getEffectiveAArch64CodeModel(const Triple &TT, Optional<CodeModel::Model> CM,
2750b57cec5SDimitry Andric                              bool JIT) {
2760b57cec5SDimitry Andric   if (CM) {
2770b57cec5SDimitry Andric     if (*CM != CodeModel::Small && *CM != CodeModel::Tiny &&
2780b57cec5SDimitry Andric         *CM != CodeModel::Large) {
2790b57cec5SDimitry Andric       report_fatal_error(
2800b57cec5SDimitry Andric           "Only small, tiny and large code models are allowed on AArch64");
2810b57cec5SDimitry Andric     } else if (*CM == CodeModel::Tiny && !TT.isOSBinFormatELF())
2820b57cec5SDimitry Andric       report_fatal_error("tiny code model is only supported on ELF");
2830b57cec5SDimitry Andric     return *CM;
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric   // The default MCJIT memory managers make no guarantees about where they can
2860b57cec5SDimitry Andric   // find an executable page; JITed code needs to be able to refer to globals
2870b57cec5SDimitry Andric   // no matter how far away they are.
288480093f4SDimitry Andric   // We should set the CodeModel::Small for Windows ARM64 in JIT mode,
289480093f4SDimitry Andric   // since with large code model LLVM generating 4 MOV instructions, and
290480093f4SDimitry Andric   // Windows doesn't support relocating these long branch (4 MOVs).
291480093f4SDimitry Andric   if (JIT && !TT.isOSWindows())
2920b57cec5SDimitry Andric     return CodeModel::Large;
2930b57cec5SDimitry Andric   return CodeModel::Small;
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric /// Create an AArch64 architecture model.
2970b57cec5SDimitry Andric ///
2980b57cec5SDimitry Andric AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
2990b57cec5SDimitry Andric                                            StringRef CPU, StringRef FS,
3000b57cec5SDimitry Andric                                            const TargetOptions &Options,
3010b57cec5SDimitry Andric                                            Optional<Reloc::Model> RM,
3020b57cec5SDimitry Andric                                            Optional<CodeModel::Model> CM,
3030b57cec5SDimitry Andric                                            CodeGenOpt::Level OL, bool JIT,
3040b57cec5SDimitry Andric                                            bool LittleEndian)
3050b57cec5SDimitry Andric     : LLVMTargetMachine(T,
3060b57cec5SDimitry Andric                         computeDataLayout(TT, Options.MCOptions, LittleEndian),
307e8d8bef9SDimitry Andric                         TT, computeDefaultCPU(TT, CPU), FS, Options,
308e8d8bef9SDimitry Andric                         getEffectiveRelocModel(TT, RM),
3090b57cec5SDimitry Andric                         getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
3100b57cec5SDimitry Andric       TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
3110b57cec5SDimitry Andric   initAsmInfo();
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   if (TT.isOSBinFormatMachO()) {
3140b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
3150b57cec5SDimitry Andric     this->Options.NoTrapAfterNoreturn = true;
3160b57cec5SDimitry Andric   }
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   if (getMCAsmInfo()->usesWindowsCFI()) {
3190b57cec5SDimitry Andric     // Unwinding can get confused if the last instruction in an
3200b57cec5SDimitry Andric     // exception-handling region (function, funclet, try block, etc.)
3210b57cec5SDimitry Andric     // is a call.
3220b57cec5SDimitry Andric     //
3230b57cec5SDimitry Andric     // FIXME: We could elide the trap if the next instruction would be in
3240b57cec5SDimitry Andric     // the same region anyway.
3250b57cec5SDimitry Andric     this->Options.TrapUnreachable = true;
3260b57cec5SDimitry Andric   }
3270b57cec5SDimitry Andric 
328480093f4SDimitry Andric   if (this->Options.TLSSize == 0) // default
329480093f4SDimitry Andric     this->Options.TLSSize = 24;
330480093f4SDimitry Andric   if ((getCodeModel() == CodeModel::Small ||
331480093f4SDimitry Andric        getCodeModel() == CodeModel::Kernel) &&
332480093f4SDimitry Andric       this->Options.TLSSize > 32)
333480093f4SDimitry Andric     // for the small (and kernel) code model, the maximum TLS size is 4GiB
334480093f4SDimitry Andric     this->Options.TLSSize = 32;
335480093f4SDimitry Andric   else if (getCodeModel() == CodeModel::Tiny && this->Options.TLSSize > 24)
336480093f4SDimitry Andric     // for the tiny code model, the maximum TLS size is 1MiB (< 16MiB)
337480093f4SDimitry Andric     this->Options.TLSSize = 24;
338480093f4SDimitry Andric 
3398bcb0991SDimitry Andric   // Enable GlobalISel at or below EnableGlobalISelAt0, unless this is
3408bcb0991SDimitry Andric   // MachO/CodeModel::Large, which GlobalISel does not support.
3418bcb0991SDimitry Andric   if (getOptLevel() <= EnableGlobalISelAtO &&
3428bcb0991SDimitry Andric       TT.getArch() != Triple::aarch64_32 &&
343e8d8bef9SDimitry Andric       TT.getEnvironment() != Triple::GNUILP32 &&
3448bcb0991SDimitry Andric       !(getCodeModel() == CodeModel::Large && TT.isOSBinFormatMachO())) {
3450b57cec5SDimitry Andric     setGlobalISel(true);
3460b57cec5SDimitry Andric     setGlobalISelAbort(GlobalISelAbortMode::Disable);
3470b57cec5SDimitry Andric   }
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   // AArch64 supports the MachineOutliner.
3500b57cec5SDimitry Andric   setMachineOutliner(true);
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   // AArch64 supports default outlining behaviour.
3530b57cec5SDimitry Andric   setSupportsDefaultOutlining(true);
3545ffd83dbSDimitry Andric 
3555ffd83dbSDimitry Andric   // AArch64 supports the debug entry values.
3565ffd83dbSDimitry Andric   setSupportsDebugEntryValues(true);
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric AArch64TargetMachine::~AArch64TargetMachine() = default;
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric const AArch64Subtarget *
3620b57cec5SDimitry Andric AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
3630b57cec5SDimitry Andric   Attribute CPUAttr = F.getFnAttribute("target-cpu");
364349cc55cSDimitry Andric   Attribute TuneAttr = F.getFnAttribute("tune-cpu");
3650b57cec5SDimitry Andric   Attribute FSAttr = F.getFnAttribute("target-features");
3660b57cec5SDimitry Andric 
367e8d8bef9SDimitry Andric   std::string CPU =
368e8d8bef9SDimitry Andric       CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
369349cc55cSDimitry Andric   std::string TuneCPU =
370349cc55cSDimitry Andric       TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
371e8d8bef9SDimitry Andric   std::string FS =
372e8d8bef9SDimitry Andric       FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
3730b57cec5SDimitry Andric 
374fe6060f1SDimitry Andric   SmallString<512> Key;
375fe6060f1SDimitry Andric 
376fe6060f1SDimitry Andric   unsigned MinSVEVectorSize = 0;
377fe6060f1SDimitry Andric   unsigned MaxSVEVectorSize = 0;
378fe6060f1SDimitry Andric   Attribute VScaleRangeAttr = F.getFnAttribute(Attribute::VScaleRange);
379fe6060f1SDimitry Andric   if (VScaleRangeAttr.isValid()) {
3800eae32dcSDimitry Andric     Optional<unsigned> VScaleMax = VScaleRangeAttr.getVScaleRangeMax();
3810eae32dcSDimitry Andric     MinSVEVectorSize = VScaleRangeAttr.getVScaleRangeMin() * 128;
3820eae32dcSDimitry Andric     MaxSVEVectorSize = VScaleMax ? VScaleMax.getValue() * 128 : 0;
383fe6060f1SDimitry Andric   } else {
384fe6060f1SDimitry Andric     MinSVEVectorSize = SVEVectorBitsMinOpt;
385fe6060f1SDimitry Andric     MaxSVEVectorSize = SVEVectorBitsMaxOpt;
386fe6060f1SDimitry Andric   }
387fe6060f1SDimitry Andric 
388fe6060f1SDimitry Andric   assert(MinSVEVectorSize % 128 == 0 &&
389fe6060f1SDimitry Andric          "SVE requires vector length in multiples of 128!");
390fe6060f1SDimitry Andric   assert(MaxSVEVectorSize % 128 == 0 &&
391fe6060f1SDimitry Andric          "SVE requires vector length in multiples of 128!");
392fe6060f1SDimitry Andric   assert((MaxSVEVectorSize >= MinSVEVectorSize || MaxSVEVectorSize == 0) &&
393fe6060f1SDimitry Andric          "Minimum SVE vector size should not be larger than its maximum!");
394fe6060f1SDimitry Andric 
395fe6060f1SDimitry Andric   // Sanitize user input in case of no asserts
396fe6060f1SDimitry Andric   if (MaxSVEVectorSize == 0)
397fe6060f1SDimitry Andric     MinSVEVectorSize = (MinSVEVectorSize / 128) * 128;
398fe6060f1SDimitry Andric   else {
399fe6060f1SDimitry Andric     MinSVEVectorSize =
400fe6060f1SDimitry Andric         (std::min(MinSVEVectorSize, MaxSVEVectorSize) / 128) * 128;
401fe6060f1SDimitry Andric     MaxSVEVectorSize =
402fe6060f1SDimitry Andric         (std::max(MinSVEVectorSize, MaxSVEVectorSize) / 128) * 128;
403fe6060f1SDimitry Andric   }
404fe6060f1SDimitry Andric 
405fe6060f1SDimitry Andric   Key += "SVEMin";
406fe6060f1SDimitry Andric   Key += std::to_string(MinSVEVectorSize);
407fe6060f1SDimitry Andric   Key += "SVEMax";
408fe6060f1SDimitry Andric   Key += std::to_string(MaxSVEVectorSize);
409fe6060f1SDimitry Andric   Key += CPU;
410349cc55cSDimitry Andric   Key += TuneCPU;
411fe6060f1SDimitry Andric   Key += FS;
412fe6060f1SDimitry Andric 
413fe6060f1SDimitry Andric   auto &I = SubtargetMap[Key];
4140b57cec5SDimitry Andric   if (!I) {
4150b57cec5SDimitry Andric     // This needs to be done before we create a new subtarget since any
4160b57cec5SDimitry Andric     // creation will depend on the TM and the code generation flags on the
4170b57cec5SDimitry Andric     // function that reside in TargetOptions.
4180b57cec5SDimitry Andric     resetTargetOptions(F);
419349cc55cSDimitry Andric     I = std::make_unique<AArch64Subtarget>(TargetTriple, CPU, TuneCPU, FS,
420349cc55cSDimitry Andric                                            *this, isLittle, MinSVEVectorSize,
421fe6060f1SDimitry Andric                                            MaxSVEVectorSize);
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric   return I.get();
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric void AArch64leTargetMachine::anchor() { }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric AArch64leTargetMachine::AArch64leTargetMachine(
4290b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
4300b57cec5SDimitry Andric     const TargetOptions &Options, Optional<Reloc::Model> RM,
4310b57cec5SDimitry Andric     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
4320b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric void AArch64beTargetMachine::anchor() { }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric AArch64beTargetMachine::AArch64beTargetMachine(
4370b57cec5SDimitry Andric     const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
4380b57cec5SDimitry Andric     const TargetOptions &Options, Optional<Reloc::Model> RM,
4390b57cec5SDimitry Andric     Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
4400b57cec5SDimitry Andric     : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric namespace {
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric /// AArch64 Code Generator Pass Configuration Options.
4450b57cec5SDimitry Andric class AArch64PassConfig : public TargetPassConfig {
4460b57cec5SDimitry Andric public:
4470b57cec5SDimitry Andric   AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM)
4480b57cec5SDimitry Andric       : TargetPassConfig(TM, PM) {
4490b57cec5SDimitry Andric     if (TM.getOptLevel() != CodeGenOpt::None)
4500b57cec5SDimitry Andric       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   AArch64TargetMachine &getAArch64TargetMachine() const {
4540b57cec5SDimitry Andric     return getTM<AArch64TargetMachine>();
4550b57cec5SDimitry Andric   }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   ScheduleDAGInstrs *
4580b57cec5SDimitry Andric   createMachineScheduler(MachineSchedContext *C) const override {
4590b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
4600b57cec5SDimitry Andric     ScheduleDAGMILive *DAG = createGenericSchedLive(C);
4610b57cec5SDimitry Andric     DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
4620b57cec5SDimitry Andric     DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
4630b57cec5SDimitry Andric     if (ST.hasFusion())
4640b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
4650b57cec5SDimitry Andric     return DAG;
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   ScheduleDAGInstrs *
4690b57cec5SDimitry Andric   createPostMachineScheduler(MachineSchedContext *C) const override {
4700b57cec5SDimitry Andric     const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
4710b57cec5SDimitry Andric     if (ST.hasFusion()) {
4720b57cec5SDimitry Andric       // Run the Macro Fusion after RA again since literals are expanded from
4730b57cec5SDimitry Andric       // pseudos then (v. addPreSched2()).
4740b57cec5SDimitry Andric       ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
4750b57cec5SDimitry Andric       DAG->addMutation(createAArch64MacroFusionDAGMutation());
4760b57cec5SDimitry Andric       return DAG;
4770b57cec5SDimitry Andric     }
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric     return nullptr;
4800b57cec5SDimitry Andric   }
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric   void addIRPasses()  override;
4830b57cec5SDimitry Andric   bool addPreISel() override;
484349cc55cSDimitry Andric   void addCodeGenPrepare() override;
4850b57cec5SDimitry Andric   bool addInstSelector() override;
4860b57cec5SDimitry Andric   bool addIRTranslator() override;
4870b57cec5SDimitry Andric   void addPreLegalizeMachineIR() override;
4880b57cec5SDimitry Andric   bool addLegalizeMachineIR() override;
4895ffd83dbSDimitry Andric   void addPreRegBankSelect() override;
4900b57cec5SDimitry Andric   bool addRegBankSelect() override;
4910b57cec5SDimitry Andric   void addPreGlobalInstructionSelect() override;
4920b57cec5SDimitry Andric   bool addGlobalInstructionSelect() override;
493349cc55cSDimitry Andric   void addMachineSSAOptimization() override;
4940b57cec5SDimitry Andric   bool addILPOpts() override;
4950b57cec5SDimitry Andric   void addPreRegAlloc() override;
4960b57cec5SDimitry Andric   void addPostRegAlloc() override;
4970b57cec5SDimitry Andric   void addPreSched2() override;
4980b57cec5SDimitry Andric   void addPreEmitPass() override;
499fe6060f1SDimitry Andric   void addPreEmitPass2() override;
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
5020b57cec5SDimitry Andric };
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric } // end anonymous namespace
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric TargetTransformInfo
5070b57cec5SDimitry Andric AArch64TargetMachine::getTargetTransformInfo(const Function &F) {
5080b57cec5SDimitry Andric   return TargetTransformInfo(AArch64TTIImpl(this, F));
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
5120b57cec5SDimitry Andric   return new AArch64PassConfig(*this, PM);
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> AArch64PassConfig::getCSEConfig() const {
5160b57cec5SDimitry Andric   return getStandardCSEConfigForOpt(TM->getOptLevel());
5170b57cec5SDimitry Andric }
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric void AArch64PassConfig::addIRPasses() {
5200b57cec5SDimitry Andric   // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
5210b57cec5SDimitry Andric   // ourselves.
5220b57cec5SDimitry Andric   addPass(createAtomicExpandPass());
5230b57cec5SDimitry Andric 
5245ffd83dbSDimitry Andric   // Expand any SVE vector library calls that we can't code generate directly.
5255ffd83dbSDimitry Andric   if (EnableSVEIntrinsicOpts && TM->getOptLevel() == CodeGenOpt::Aggressive)
5265ffd83dbSDimitry Andric     addPass(createSVEIntrinsicOptsPass());
5275ffd83dbSDimitry Andric 
5280b57cec5SDimitry Andric   // Cmpxchg instructions are often used with a subsequent comparison to
5290b57cec5SDimitry Andric   // determine whether it succeeded. We can exploit existing control-flow in
5300b57cec5SDimitry Andric   // ldrex/strex loops to simplify this, but it needs tidying up.
5310b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
532e8d8bef9SDimitry Andric     addPass(createCFGSimplificationPass(SimplifyCFGOptions()
533e8d8bef9SDimitry Andric                                             .forwardSwitchCondToPhi(true)
534*fb03ea46SDimitry Andric                                             .convertSwitchRangeToICmp(true)
535e8d8bef9SDimitry Andric                                             .convertSwitchToLookupTable(true)
536e8d8bef9SDimitry Andric                                             .needCanonicalLoops(false)
537e8d8bef9SDimitry Andric                                             .hoistCommonInsts(true)
538e8d8bef9SDimitry Andric                                             .sinkCommonInsts(true)));
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   // Run LoopDataPrefetch
5410b57cec5SDimitry Andric   //
5420b57cec5SDimitry Andric   // Run this before LSR to remove the multiplies involved in computing the
5430b57cec5SDimitry Andric   // pointer values N iterations ahead.
5440b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
5450b57cec5SDimitry Andric     if (EnableLoopDataPrefetch)
5460b57cec5SDimitry Andric       addPass(createLoopDataPrefetchPass());
5470b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
5480b57cec5SDimitry Andric       addPass(createFalkorMarkStridedAccessesPass());
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   TargetPassConfig::addIRPasses();
5520b57cec5SDimitry Andric 
5535ffd83dbSDimitry Andric   addPass(createAArch64StackTaggingPass(
5545ffd83dbSDimitry Andric       /*IsOptNone=*/TM->getOptLevel() == CodeGenOpt::None));
5555ffd83dbSDimitry Andric 
5560b57cec5SDimitry Andric   // Match interleaved memory accesses to ldN/stN intrinsics.
5570b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
5580b57cec5SDimitry Andric     addPass(createInterleavedLoadCombinePass());
5590b57cec5SDimitry Andric     addPass(createInterleavedAccessPass());
5600b57cec5SDimitry Andric   }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
5630b57cec5SDimitry Andric     // Call SeparateConstOffsetFromGEP pass to extract constants within indices
5640b57cec5SDimitry Andric     // and lower a GEP with multiple indices to either arithmetic operations or
5650b57cec5SDimitry Andric     // multiple GEPs with single index.
5660b57cec5SDimitry Andric     addPass(createSeparateConstOffsetFromGEPPass(true));
5670b57cec5SDimitry Andric     // Call EarlyCSE pass to find and remove subexpressions in the lowered
5680b57cec5SDimitry Andric     // result.
5690b57cec5SDimitry Andric     addPass(createEarlyCSEPass());
5700b57cec5SDimitry Andric     // Do loop invariant code motion in case part of the lowered result is
5710b57cec5SDimitry Andric     // invariant.
5720b57cec5SDimitry Andric     addPass(createLICMPass());
5730b57cec5SDimitry Andric   }
5740b57cec5SDimitry Andric 
575480093f4SDimitry Andric   // Add Control Flow Guard checks.
576480093f4SDimitry Andric   if (TM->getTargetTriple().isOSWindows())
577480093f4SDimitry Andric     addPass(createCFGuardCheckPass());
5780b57cec5SDimitry Andric }
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric // Pass Pipeline Configuration
5810b57cec5SDimitry Andric bool AArch64PassConfig::addPreISel() {
5820b57cec5SDimitry Andric   // Run promote constant before global merge, so that the promoted constants
5830b57cec5SDimitry Andric   // get a chance to be merged
5840b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
5850b57cec5SDimitry Andric     addPass(createAArch64PromoteConstantPass());
5860b57cec5SDimitry Andric   // FIXME: On AArch64, this depends on the type.
5870b57cec5SDimitry Andric   // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
5880b57cec5SDimitry Andric   // and the offset has to be a multiple of the related size in bytes.
5890b57cec5SDimitry Andric   if ((TM->getOptLevel() != CodeGenOpt::None &&
5900b57cec5SDimitry Andric        EnableGlobalMerge == cl::BOU_UNSET) ||
5910b57cec5SDimitry Andric       EnableGlobalMerge == cl::BOU_TRUE) {
5920b57cec5SDimitry Andric     bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
5930b57cec5SDimitry Andric                                (EnableGlobalMerge == cl::BOU_UNSET);
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric     // Merging of extern globals is enabled by default on non-Mach-O as we
5960b57cec5SDimitry Andric     // expect it to be generally either beneficial or harmless. On Mach-O it
5970b57cec5SDimitry Andric     // is disabled as we emit the .subsections_via_symbols directive which
5980b57cec5SDimitry Andric     // means that merging extern globals is not safe.
5990b57cec5SDimitry Andric     bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric     // FIXME: extern global merging is only enabled when we optimise for size
6020b57cec5SDimitry Andric     // because there are some regressions with it also enabled for performance.
6030b57cec5SDimitry Andric     if (!OnlyOptimizeForSize)
6040b57cec5SDimitry Andric       MergeExternalByDefault = false;
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric     addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize,
6070b57cec5SDimitry Andric                                   MergeExternalByDefault));
6080b57cec5SDimitry Andric   }
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   return false;
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric 
613349cc55cSDimitry Andric void AArch64PassConfig::addCodeGenPrepare() {
614349cc55cSDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
615349cc55cSDimitry Andric     addPass(createTypePromotionPass());
616349cc55cSDimitry Andric   TargetPassConfig::addCodeGenPrepare();
617349cc55cSDimitry Andric }
618349cc55cSDimitry Andric 
6190b57cec5SDimitry Andric bool AArch64PassConfig::addInstSelector() {
6200b57cec5SDimitry Andric   addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
6230b57cec5SDimitry Andric   // references to _TLS_MODULE_BASE_ as possible.
6240b57cec5SDimitry Andric   if (TM->getTargetTriple().isOSBinFormatELF() &&
6250b57cec5SDimitry Andric       getOptLevel() != CodeGenOpt::None)
6260b57cec5SDimitry Andric     addPass(createAArch64CleanupLocalDynamicTLSPass());
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   return false;
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric bool AArch64PassConfig::addIRTranslator() {
632e8d8bef9SDimitry Andric   addPass(new IRTranslator(getOptLevel()));
6330b57cec5SDimitry Andric   return false;
6340b57cec5SDimitry Andric }
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric void AArch64PassConfig::addPreLegalizeMachineIR() {
637fe6060f1SDimitry Andric   if (getOptLevel() == CodeGenOpt::None)
638fe6060f1SDimitry Andric     addPass(createAArch64O0PreLegalizerCombiner());
639349cc55cSDimitry Andric   else {
640fe6060f1SDimitry Andric     addPass(createAArch64PreLegalizerCombiner());
641349cc55cSDimitry Andric     if (EnableGISelLoadStoreOptPreLegal)
642349cc55cSDimitry Andric       addPass(new LoadStoreOpt());
643349cc55cSDimitry Andric   }
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric bool AArch64PassConfig::addLegalizeMachineIR() {
6470b57cec5SDimitry Andric   addPass(new Legalizer());
6480b57cec5SDimitry Andric   return false;
6490b57cec5SDimitry Andric }
6500b57cec5SDimitry Andric 
6515ffd83dbSDimitry Andric void AArch64PassConfig::addPreRegBankSelect() {
6525ffd83dbSDimitry Andric   bool IsOptNone = getOptLevel() == CodeGenOpt::None;
653349cc55cSDimitry Andric   if (!IsOptNone) {
654e8d8bef9SDimitry Andric     addPass(createAArch64PostLegalizerCombiner(IsOptNone));
655349cc55cSDimitry Andric     if (EnableGISelLoadStoreOptPostLegal)
656349cc55cSDimitry Andric       addPass(new LoadStoreOpt());
657349cc55cSDimitry Andric   }
658e8d8bef9SDimitry Andric   addPass(createAArch64PostLegalizerLowering());
6595ffd83dbSDimitry Andric }
6605ffd83dbSDimitry Andric 
6610b57cec5SDimitry Andric bool AArch64PassConfig::addRegBankSelect() {
6620b57cec5SDimitry Andric   addPass(new RegBankSelect());
6630b57cec5SDimitry Andric   return false;
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric void AArch64PassConfig::addPreGlobalInstructionSelect() {
6670b57cec5SDimitry Andric   addPass(new Localizer());
6680b57cec5SDimitry Andric }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric bool AArch64PassConfig::addGlobalInstructionSelect() {
671fe6060f1SDimitry Andric   addPass(new InstructionSelect(getOptLevel()));
672e8d8bef9SDimitry Andric   if (getOptLevel() != CodeGenOpt::None)
673e8d8bef9SDimitry Andric     addPass(createAArch64PostSelectOptimize());
6740b57cec5SDimitry Andric   return false;
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric 
677349cc55cSDimitry Andric void AArch64PassConfig::addMachineSSAOptimization() {
678349cc55cSDimitry Andric   // Run default MachineSSAOptimization first.
679349cc55cSDimitry Andric   TargetPassConfig::addMachineSSAOptimization();
680349cc55cSDimitry Andric 
681349cc55cSDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
682349cc55cSDimitry Andric     addPass(createAArch64MIPeepholeOptPass());
683349cc55cSDimitry Andric }
684349cc55cSDimitry Andric 
6850b57cec5SDimitry Andric bool AArch64PassConfig::addILPOpts() {
6860b57cec5SDimitry Andric   if (EnableCondOpt)
6870b57cec5SDimitry Andric     addPass(createAArch64ConditionOptimizerPass());
6880b57cec5SDimitry Andric   if (EnableCCMP)
6890b57cec5SDimitry Andric     addPass(createAArch64ConditionalCompares());
6900b57cec5SDimitry Andric   if (EnableMCR)
6910b57cec5SDimitry Andric     addPass(&MachineCombinerID);
6920b57cec5SDimitry Andric   if (EnableCondBrTuning)
6930b57cec5SDimitry Andric     addPass(createAArch64CondBrTuning());
6940b57cec5SDimitry Andric   if (EnableEarlyIfConversion)
6950b57cec5SDimitry Andric     addPass(&EarlyIfConverterID);
6960b57cec5SDimitry Andric   if (EnableStPairSuppress)
6970b57cec5SDimitry Andric     addPass(createAArch64StorePairSuppressPass());
6980b57cec5SDimitry Andric   addPass(createAArch64SIMDInstrOptPass());
6998bcb0991SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None)
7008bcb0991SDimitry Andric     addPass(createAArch64StackTaggingPreRAPass());
7010b57cec5SDimitry Andric   return true;
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric void AArch64PassConfig::addPreRegAlloc() {
7050b57cec5SDimitry Andric   // Change dead register definitions to refer to the zero register.
7060b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
7070b57cec5SDimitry Andric     addPass(createAArch64DeadRegisterDefinitions());
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric   // Use AdvSIMD scalar instructions whenever profitable.
7100b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
7110b57cec5SDimitry Andric     addPass(createAArch64AdvSIMDScalar());
7120b57cec5SDimitry Andric     // The AdvSIMD pass may produce copies that can be rewritten to
713480093f4SDimitry Andric     // be register coalescer friendly.
7140b57cec5SDimitry Andric     addPass(&PeepholeOptimizerID);
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric void AArch64PassConfig::addPostRegAlloc() {
7190b57cec5SDimitry Andric   // Remove redundant copy instructions.
7200b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
7210b57cec5SDimitry Andric     addPass(createAArch64RedundantCopyEliminationPass());
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
7240b57cec5SDimitry Andric     // Improve performance for some FP/SIMD code for A57.
7250b57cec5SDimitry Andric     addPass(createAArch64A57FPLoadBalancing());
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric void AArch64PassConfig::addPreSched2() {
729fe6060f1SDimitry Andric   // Lower homogeneous frame instructions
730fe6060f1SDimitry Andric   if (EnableHomogeneousPrologEpilog)
731fe6060f1SDimitry Andric     addPass(createAArch64LowerHomogeneousPrologEpilogPass());
7320b57cec5SDimitry Andric   // Expand some pseudo instructions to allow proper scheduling.
7330b57cec5SDimitry Andric   addPass(createAArch64ExpandPseudoPass());
7340b57cec5SDimitry Andric   // Use load/store pair instructions when possible.
7350b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
7360b57cec5SDimitry Andric     if (EnableLoadStoreOpt)
7370b57cec5SDimitry Andric       addPass(createAArch64LoadStoreOptimizationPass());
7380b57cec5SDimitry Andric   }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   // The AArch64SpeculationHardeningPass destroys dominator tree and natural
7410b57cec5SDimitry Andric   // loop info, which is needed for the FalkorHWPFFixPass and also later on.
7420b57cec5SDimitry Andric   // Therefore, run the AArch64SpeculationHardeningPass before the
7430b57cec5SDimitry Andric   // FalkorHWPFFixPass to avoid recomputing dominator tree and natural loop
7440b57cec5SDimitry Andric   // info.
7450b57cec5SDimitry Andric   addPass(createAArch64SpeculationHardeningPass());
7460b57cec5SDimitry Andric 
7475ffd83dbSDimitry Andric   addPass(createAArch64IndirectThunks());
7485ffd83dbSDimitry Andric   addPass(createAArch64SLSHardeningPass());
7495ffd83dbSDimitry Andric 
7500b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None) {
7510b57cec5SDimitry Andric     if (EnableFalkorHWPFFix)
7520b57cec5SDimitry Andric       addPass(createFalkorHWPFFixPass());
7530b57cec5SDimitry Andric   }
7540b57cec5SDimitry Andric }
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric void AArch64PassConfig::addPreEmitPass() {
7570b57cec5SDimitry Andric   // Machine Block Placement might have created new opportunities when run
7580b57cec5SDimitry Andric   // at O3, where the Tail Duplication Threshold is set to 4 instructions.
7590b57cec5SDimitry Andric   // Run the load/store optimizer once more.
7600b57cec5SDimitry Andric   if (TM->getOptLevel() >= CodeGenOpt::Aggressive && EnableLoadStoreOpt)
7610b57cec5SDimitry Andric     addPass(createAArch64LoadStoreOptimizationPass());
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric   addPass(createAArch64A53Fix835769());
764480093f4SDimitry Andric 
765480093f4SDimitry Andric   if (EnableBranchTargets)
766480093f4SDimitry Andric     addPass(createAArch64BranchTargetsPass());
767480093f4SDimitry Andric 
7680b57cec5SDimitry Andric   // Relax conditional branch instructions if they're otherwise out of
7690b57cec5SDimitry Andric   // range of their destination.
7700b57cec5SDimitry Andric   if (BranchRelaxation)
7710b57cec5SDimitry Andric     addPass(&BranchRelaxationPassID);
7720b57cec5SDimitry Andric 
773fe6060f1SDimitry Andric   if (TM->getTargetTriple().isOSWindows()) {
774480093f4SDimitry Andric     // Identify valid longjmp targets for Windows Control Flow Guard.
775480093f4SDimitry Andric     addPass(createCFGuardLongjmpPass());
776fe6060f1SDimitry Andric     // Identify valid eh continuation targets for Windows EHCont Guard.
777fe6060f1SDimitry Andric     addPass(createEHContGuardCatchretPass());
778fe6060f1SDimitry Andric   }
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCompressJumpTables)
7810b57cec5SDimitry Andric     addPass(createAArch64CompressJumpTablesPass());
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
7840b57cec5SDimitry Andric       TM->getTargetTriple().isOSBinFormatMachO())
7850b57cec5SDimitry Andric     addPass(createAArch64CollectLOHPass());
786fe6060f1SDimitry Andric }
7875ffd83dbSDimitry Andric 
788fe6060f1SDimitry Andric void AArch64PassConfig::addPreEmitPass2() {
789fe6060f1SDimitry Andric   // SVE bundles move prefixes with destructive operations. BLR_RVMARKER pseudo
790fe6060f1SDimitry Andric   // instructions are lowered to bundles as well.
7915ffd83dbSDimitry Andric   addPass(createUnpackMachineBundles(nullptr));
7925ffd83dbSDimitry Andric }
7935ffd83dbSDimitry Andric 
7945ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
7955ffd83dbSDimitry Andric AArch64TargetMachine::createDefaultFuncInfoYAML() const {
7965ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo();
7975ffd83dbSDimitry Andric }
7985ffd83dbSDimitry Andric 
7995ffd83dbSDimitry Andric yaml::MachineFunctionInfo *
8005ffd83dbSDimitry Andric AArch64TargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
8015ffd83dbSDimitry Andric   const auto *MFI = MF.getInfo<AArch64FunctionInfo>();
8025ffd83dbSDimitry Andric   return new yaml::AArch64FunctionInfo(*MFI);
8035ffd83dbSDimitry Andric }
8045ffd83dbSDimitry Andric 
8055ffd83dbSDimitry Andric bool AArch64TargetMachine::parseMachineFunctionInfo(
8065ffd83dbSDimitry Andric     const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
8075ffd83dbSDimitry Andric     SMDiagnostic &Error, SMRange &SourceRange) const {
8085ffd83dbSDimitry Andric   const auto &YamlMFI =
8095ffd83dbSDimitry Andric       reinterpret_cast<const yaml::AArch64FunctionInfo &>(MFI);
8105ffd83dbSDimitry Andric   MachineFunction &MF = PFS.MF;
8115ffd83dbSDimitry Andric   MF.getInfo<AArch64FunctionInfo>()->initializeBaseYamlFields(YamlMFI);
8125ffd83dbSDimitry Andric   return false;
8130b57cec5SDimitry Andric }
814