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" 140b57cec5SDimitry Andric #include "AArch64MacroFusion.h" 150b57cec5SDimitry Andric #include "AArch64Subtarget.h" 160b57cec5SDimitry Andric #include "AArch64TargetObjectFile.h" 170b57cec5SDimitry Andric #include "AArch64TargetTransformInfo.h" 180b57cec5SDimitry Andric #include "MCTargetDesc/AArch64MCTargetDesc.h" 190b57cec5SDimitry Andric #include "TargetInfo/AArch64TargetInfo.h" 200b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 210b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 220b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/CSEConfigBase.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/IRTranslator.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/InstructionSelect.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Legalizer.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/Localizer.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 320b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 330b57cec5SDimitry Andric #include "llvm/IR/Function.h" 340b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 350b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 360b57cec5SDimitry Andric #include "llvm/Pass.h" 370b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 380b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 390b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 400b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 410b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 420b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h" 430b57cec5SDimitry Andric #include <memory> 440b57cec5SDimitry Andric #include <string> 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric using namespace llvm; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp", 490b57cec5SDimitry Andric cl::desc("Enable the CCMP formation pass"), 500b57cec5SDimitry Andric cl::init(true), cl::Hidden); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric static cl::opt<bool> 530b57cec5SDimitry Andric EnableCondBrTuning("aarch64-enable-cond-br-tune", 540b57cec5SDimitry Andric cl::desc("Enable the conditional branch tuning pass"), 550b57cec5SDimitry Andric cl::init(true), cl::Hidden); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric static cl::opt<bool> EnableMCR("aarch64-enable-mcr", 580b57cec5SDimitry Andric cl::desc("Enable the machine combiner pass"), 590b57cec5SDimitry Andric cl::init(true), cl::Hidden); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress", 620b57cec5SDimitry Andric cl::desc("Suppress STP for AArch64"), 630b57cec5SDimitry Andric cl::init(true), cl::Hidden); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric static cl::opt<bool> EnableAdvSIMDScalar( 660b57cec5SDimitry Andric "aarch64-enable-simd-scalar", 670b57cec5SDimitry Andric cl::desc("Enable use of AdvSIMD scalar integer instructions"), 680b57cec5SDimitry Andric cl::init(false), cl::Hidden); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric static cl::opt<bool> 710b57cec5SDimitry Andric EnablePromoteConstant("aarch64-enable-promote-const", 720b57cec5SDimitry Andric cl::desc("Enable the promote constant pass"), 730b57cec5SDimitry Andric cl::init(true), cl::Hidden); 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric static cl::opt<bool> EnableCollectLOH( 760b57cec5SDimitry Andric "aarch64-enable-collect-loh", 770b57cec5SDimitry Andric cl::desc("Enable the pass that emits the linker optimization hints (LOH)"), 780b57cec5SDimitry Andric cl::init(true), cl::Hidden); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric static cl::opt<bool> 810b57cec5SDimitry Andric EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden, 820b57cec5SDimitry Andric cl::desc("Enable the pass that removes dead" 830b57cec5SDimitry Andric " definitons and replaces stores to" 840b57cec5SDimitry Andric " them with stores to the zero" 850b57cec5SDimitry Andric " register"), 860b57cec5SDimitry Andric cl::init(true)); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric static cl::opt<bool> EnableRedundantCopyElimination( 890b57cec5SDimitry Andric "aarch64-enable-copyelim", 900b57cec5SDimitry Andric cl::desc("Enable the redundant copy elimination pass"), cl::init(true), 910b57cec5SDimitry Andric cl::Hidden); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt", 940b57cec5SDimitry Andric cl::desc("Enable the load/store pair" 950b57cec5SDimitry Andric " optimization pass"), 960b57cec5SDimitry Andric cl::init(true), cl::Hidden); 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric static cl::opt<bool> EnableAtomicTidy( 990b57cec5SDimitry Andric "aarch64-enable-atomic-cfg-tidy", cl::Hidden, 1000b57cec5SDimitry Andric cl::desc("Run SimplifyCFG after expanding atomic operations" 1010b57cec5SDimitry Andric " to make use of cmpxchg flow-based information"), 1020b57cec5SDimitry Andric cl::init(true)); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric static cl::opt<bool> 1050b57cec5SDimitry Andric EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden, 1060b57cec5SDimitry Andric cl::desc("Run early if-conversion"), 1070b57cec5SDimitry Andric cl::init(true)); 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric static cl::opt<bool> 1100b57cec5SDimitry Andric EnableCondOpt("aarch64-enable-condopt", 1110b57cec5SDimitry Andric cl::desc("Enable the condition optimizer pass"), 1120b57cec5SDimitry Andric cl::init(true), cl::Hidden); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric static cl::opt<bool> 1150b57cec5SDimitry Andric EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden, 1160b57cec5SDimitry Andric cl::desc("Work around Cortex-A53 erratum 835769"), 1170b57cec5SDimitry Andric cl::init(false)); 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 1470b57cec5SDimitry Andric static cl::opt<bool> EnableFalkorHWPFFix("aarch64-enable-falkor-hwpf-fix", 1480b57cec5SDimitry Andric cl::init(true), cl::Hidden); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric static cl::opt<bool> 1510b57cec5SDimitry Andric EnableBranchTargets("aarch64-enable-branch-targets", cl::Hidden, 1520b57cec5SDimitry Andric cl::desc("Enable the AAcrh64 branch target pass"), 1530b57cec5SDimitry Andric cl::init(true)); 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric extern "C" void LLVMInitializeAArch64Target() { 1560b57cec5SDimitry Andric // Register the target. 1570b57cec5SDimitry Andric RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget()); 1580b57cec5SDimitry Andric RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget()); 1590b57cec5SDimitry Andric RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target()); 160*8bcb0991SDimitry Andric RegisterTargetMachine<AArch64leTargetMachine> W(getTheARM64_32Target()); 161*8bcb0991SDimitry Andric RegisterTargetMachine<AArch64leTargetMachine> V(getTheAArch64_32Target()); 1620b57cec5SDimitry Andric auto PR = PassRegistry::getPassRegistry(); 1630b57cec5SDimitry Andric initializeGlobalISel(*PR); 1640b57cec5SDimitry Andric initializeAArch64A53Fix835769Pass(*PR); 1650b57cec5SDimitry Andric initializeAArch64A57FPLoadBalancingPass(*PR); 1660b57cec5SDimitry Andric initializeAArch64AdvSIMDScalarPass(*PR); 1670b57cec5SDimitry Andric initializeAArch64BranchTargetsPass(*PR); 1680b57cec5SDimitry Andric initializeAArch64CollectLOHPass(*PR); 1690b57cec5SDimitry Andric initializeAArch64CompressJumpTablesPass(*PR); 1700b57cec5SDimitry Andric initializeAArch64ConditionalComparesPass(*PR); 1710b57cec5SDimitry Andric initializeAArch64ConditionOptimizerPass(*PR); 1720b57cec5SDimitry Andric initializeAArch64DeadRegisterDefinitionsPass(*PR); 1730b57cec5SDimitry Andric initializeAArch64ExpandPseudoPass(*PR); 1740b57cec5SDimitry Andric initializeAArch64LoadStoreOptPass(*PR); 1750b57cec5SDimitry Andric initializeAArch64SIMDInstrOptPass(*PR); 1760b57cec5SDimitry Andric initializeAArch64PreLegalizerCombinerPass(*PR); 1770b57cec5SDimitry Andric initializeAArch64PromoteConstantPass(*PR); 1780b57cec5SDimitry Andric initializeAArch64RedundantCopyEliminationPass(*PR); 1790b57cec5SDimitry Andric initializeAArch64StorePairSuppressPass(*PR); 1800b57cec5SDimitry Andric initializeFalkorHWPFFixPass(*PR); 1810b57cec5SDimitry Andric initializeFalkorMarkStridedAccessesLegacyPass(*PR); 1820b57cec5SDimitry Andric initializeLDTLSCleanupPass(*PR); 1830b57cec5SDimitry Andric initializeAArch64SpeculationHardeningPass(*PR); 1840b57cec5SDimitry Andric initializeAArch64StackTaggingPass(*PR); 185*8bcb0991SDimitry Andric initializeAArch64StackTaggingPreRAPass(*PR); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1890b57cec5SDimitry Andric // AArch64 Lowering public interface. 1900b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1910b57cec5SDimitry Andric static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { 1920b57cec5SDimitry Andric if (TT.isOSBinFormatMachO()) 193*8bcb0991SDimitry Andric return std::make_unique<AArch64_MachoTargetObjectFile>(); 1940b57cec5SDimitry Andric if (TT.isOSBinFormatCOFF()) 195*8bcb0991SDimitry Andric return std::make_unique<AArch64_COFFTargetObjectFile>(); 1960b57cec5SDimitry Andric 197*8bcb0991SDimitry Andric return std::make_unique<AArch64_ELFTargetObjectFile>(); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Helper function to build a DataLayout string 2010b57cec5SDimitry Andric static std::string computeDataLayout(const Triple &TT, 2020b57cec5SDimitry Andric const MCTargetOptions &Options, 2030b57cec5SDimitry Andric bool LittleEndian) { 2040b57cec5SDimitry Andric if (Options.getABIName() == "ilp32") 2050b57cec5SDimitry Andric return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128"; 206*8bcb0991SDimitry Andric if (TT.isOSBinFormatMachO()) { 207*8bcb0991SDimitry Andric if (TT.getArch() == Triple::aarch64_32) 208*8bcb0991SDimitry Andric return "e-m:o-p:32:32-i64:64-i128:128-n32:64-S128"; 2090b57cec5SDimitry Andric return "e-m:o-i64:64-i128:128-n32:64-S128"; 210*8bcb0991SDimitry Andric } 2110b57cec5SDimitry Andric if (TT.isOSBinFormatCOFF()) 2120b57cec5SDimitry Andric return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128"; 2130b57cec5SDimitry Andric if (LittleEndian) 2140b57cec5SDimitry Andric return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"; 2150b57cec5SDimitry Andric return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric static Reloc::Model getEffectiveRelocModel(const Triple &TT, 2190b57cec5SDimitry Andric Optional<Reloc::Model> RM) { 2200b57cec5SDimitry Andric // AArch64 Darwin and Windows are always PIC. 2210b57cec5SDimitry Andric if (TT.isOSDarwin() || TT.isOSWindows()) 2220b57cec5SDimitry Andric return Reloc::PIC_; 2230b57cec5SDimitry Andric // On ELF platforms the default static relocation model has a smart enough 2240b57cec5SDimitry Andric // linker to cope with referencing external symbols defined in a shared 2250b57cec5SDimitry Andric // library. Hence DynamicNoPIC doesn't need to be promoted to PIC. 2260b57cec5SDimitry Andric if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC) 2270b57cec5SDimitry Andric return Reloc::Static; 2280b57cec5SDimitry Andric return *RM; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric static CodeModel::Model 2320b57cec5SDimitry Andric getEffectiveAArch64CodeModel(const Triple &TT, Optional<CodeModel::Model> CM, 2330b57cec5SDimitry Andric bool JIT) { 2340b57cec5SDimitry Andric if (CM) { 2350b57cec5SDimitry Andric if (*CM != CodeModel::Small && *CM != CodeModel::Tiny && 2360b57cec5SDimitry Andric *CM != CodeModel::Large) { 2370b57cec5SDimitry Andric if (!TT.isOSFuchsia()) 2380b57cec5SDimitry Andric report_fatal_error( 2390b57cec5SDimitry Andric "Only small, tiny and large code models are allowed on AArch64"); 2400b57cec5SDimitry Andric else if (*CM != CodeModel::Kernel) 2410b57cec5SDimitry Andric report_fatal_error("Only small, tiny, kernel, and large code models " 2420b57cec5SDimitry Andric "are allowed on AArch64"); 2430b57cec5SDimitry Andric } else if (*CM == CodeModel::Tiny && !TT.isOSBinFormatELF()) 2440b57cec5SDimitry Andric report_fatal_error("tiny code model is only supported on ELF"); 2450b57cec5SDimitry Andric return *CM; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric // The default MCJIT memory managers make no guarantees about where they can 2480b57cec5SDimitry Andric // find an executable page; JITed code needs to be able to refer to globals 2490b57cec5SDimitry Andric // no matter how far away they are. 2500b57cec5SDimitry Andric if (JIT) 2510b57cec5SDimitry Andric return CodeModel::Large; 2520b57cec5SDimitry Andric return CodeModel::Small; 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric /// Create an AArch64 architecture model. 2560b57cec5SDimitry Andric /// 2570b57cec5SDimitry Andric AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT, 2580b57cec5SDimitry Andric StringRef CPU, StringRef FS, 2590b57cec5SDimitry Andric const TargetOptions &Options, 2600b57cec5SDimitry Andric Optional<Reloc::Model> RM, 2610b57cec5SDimitry Andric Optional<CodeModel::Model> CM, 2620b57cec5SDimitry Andric CodeGenOpt::Level OL, bool JIT, 2630b57cec5SDimitry Andric bool LittleEndian) 2640b57cec5SDimitry Andric : LLVMTargetMachine(T, 2650b57cec5SDimitry Andric computeDataLayout(TT, Options.MCOptions, LittleEndian), 2660b57cec5SDimitry Andric TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM), 2670b57cec5SDimitry Andric getEffectiveAArch64CodeModel(TT, CM, JIT), OL), 2680b57cec5SDimitry Andric TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) { 2690b57cec5SDimitry Andric initAsmInfo(); 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric if (TT.isOSBinFormatMachO()) { 2720b57cec5SDimitry Andric this->Options.TrapUnreachable = true; 2730b57cec5SDimitry Andric this->Options.NoTrapAfterNoreturn = true; 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric if (getMCAsmInfo()->usesWindowsCFI()) { 2770b57cec5SDimitry Andric // Unwinding can get confused if the last instruction in an 2780b57cec5SDimitry Andric // exception-handling region (function, funclet, try block, etc.) 2790b57cec5SDimitry Andric // is a call. 2800b57cec5SDimitry Andric // 2810b57cec5SDimitry Andric // FIXME: We could elide the trap if the next instruction would be in 2820b57cec5SDimitry Andric // the same region anyway. 2830b57cec5SDimitry Andric this->Options.TrapUnreachable = true; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 286*8bcb0991SDimitry Andric // Enable GlobalISel at or below EnableGlobalISelAt0, unless this is 287*8bcb0991SDimitry Andric // MachO/CodeModel::Large, which GlobalISel does not support. 288*8bcb0991SDimitry Andric if (getOptLevel() <= EnableGlobalISelAtO && 289*8bcb0991SDimitry Andric TT.getArch() != Triple::aarch64_32 && 290*8bcb0991SDimitry Andric !(getCodeModel() == CodeModel::Large && TT.isOSBinFormatMachO())) { 2910b57cec5SDimitry Andric setGlobalISel(true); 2920b57cec5SDimitry Andric setGlobalISelAbort(GlobalISelAbortMode::Disable); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // AArch64 supports the MachineOutliner. 2960b57cec5SDimitry Andric setMachineOutliner(true); 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric // AArch64 supports default outlining behaviour. 2990b57cec5SDimitry Andric setSupportsDefaultOutlining(true); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric AArch64TargetMachine::~AArch64TargetMachine() = default; 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric const AArch64Subtarget * 3050b57cec5SDimitry Andric AArch64TargetMachine::getSubtargetImpl(const Function &F) const { 3060b57cec5SDimitry Andric Attribute CPUAttr = F.getFnAttribute("target-cpu"); 3070b57cec5SDimitry Andric Attribute FSAttr = F.getFnAttribute("target-features"); 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 3100b57cec5SDimitry Andric ? CPUAttr.getValueAsString().str() 3110b57cec5SDimitry Andric : TargetCPU; 3120b57cec5SDimitry Andric std::string FS = !FSAttr.hasAttribute(Attribute::None) 3130b57cec5SDimitry Andric ? FSAttr.getValueAsString().str() 3140b57cec5SDimitry Andric : TargetFS; 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric auto &I = SubtargetMap[CPU + FS]; 3170b57cec5SDimitry Andric if (!I) { 3180b57cec5SDimitry Andric // This needs to be done before we create a new subtarget since any 3190b57cec5SDimitry Andric // creation will depend on the TM and the code generation flags on the 3200b57cec5SDimitry Andric // function that reside in TargetOptions. 3210b57cec5SDimitry Andric resetTargetOptions(F); 322*8bcb0991SDimitry Andric I = std::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this, 3230b57cec5SDimitry Andric isLittle); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric return I.get(); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric void AArch64leTargetMachine::anchor() { } 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric AArch64leTargetMachine::AArch64leTargetMachine( 3310b57cec5SDimitry Andric const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 3320b57cec5SDimitry Andric const TargetOptions &Options, Optional<Reloc::Model> RM, 3330b57cec5SDimitry Andric Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) 3340b57cec5SDimitry Andric : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {} 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric void AArch64beTargetMachine::anchor() { } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric AArch64beTargetMachine::AArch64beTargetMachine( 3390b57cec5SDimitry Andric const Target &T, const Triple &TT, StringRef CPU, StringRef FS, 3400b57cec5SDimitry Andric const TargetOptions &Options, Optional<Reloc::Model> RM, 3410b57cec5SDimitry Andric Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) 3420b57cec5SDimitry Andric : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {} 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric namespace { 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric /// AArch64 Code Generator Pass Configuration Options. 3470b57cec5SDimitry Andric class AArch64PassConfig : public TargetPassConfig { 3480b57cec5SDimitry Andric public: 3490b57cec5SDimitry Andric AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM) 3500b57cec5SDimitry Andric : TargetPassConfig(TM, PM) { 3510b57cec5SDimitry Andric if (TM.getOptLevel() != CodeGenOpt::None) 3520b57cec5SDimitry Andric substitutePass(&PostRASchedulerID, &PostMachineSchedulerID); 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric AArch64TargetMachine &getAArch64TargetMachine() const { 3560b57cec5SDimitry Andric return getTM<AArch64TargetMachine>(); 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric ScheduleDAGInstrs * 3600b57cec5SDimitry Andric createMachineScheduler(MachineSchedContext *C) const override { 3610b57cec5SDimitry Andric const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>(); 3620b57cec5SDimitry Andric ScheduleDAGMILive *DAG = createGenericSchedLive(C); 3630b57cec5SDimitry Andric DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI)); 3640b57cec5SDimitry Andric DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI)); 3650b57cec5SDimitry Andric if (ST.hasFusion()) 3660b57cec5SDimitry Andric DAG->addMutation(createAArch64MacroFusionDAGMutation()); 3670b57cec5SDimitry Andric return DAG; 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric ScheduleDAGInstrs * 3710b57cec5SDimitry Andric createPostMachineScheduler(MachineSchedContext *C) const override { 3720b57cec5SDimitry Andric const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>(); 3730b57cec5SDimitry Andric if (ST.hasFusion()) { 3740b57cec5SDimitry Andric // Run the Macro Fusion after RA again since literals are expanded from 3750b57cec5SDimitry Andric // pseudos then (v. addPreSched2()). 3760b57cec5SDimitry Andric ScheduleDAGMI *DAG = createGenericSchedPostRA(C); 3770b57cec5SDimitry Andric DAG->addMutation(createAArch64MacroFusionDAGMutation()); 3780b57cec5SDimitry Andric return DAG; 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric return nullptr; 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric void addIRPasses() override; 3850b57cec5SDimitry Andric bool addPreISel() override; 3860b57cec5SDimitry Andric bool addInstSelector() override; 3870b57cec5SDimitry Andric bool addIRTranslator() override; 3880b57cec5SDimitry Andric void addPreLegalizeMachineIR() override; 3890b57cec5SDimitry Andric bool addLegalizeMachineIR() override; 3900b57cec5SDimitry Andric bool addRegBankSelect() override; 3910b57cec5SDimitry Andric void addPreGlobalInstructionSelect() override; 3920b57cec5SDimitry Andric bool addGlobalInstructionSelect() override; 3930b57cec5SDimitry Andric bool addILPOpts() override; 3940b57cec5SDimitry Andric void addPreRegAlloc() override; 3950b57cec5SDimitry Andric void addPostRegAlloc() override; 3960b57cec5SDimitry Andric void addPreSched2() override; 3970b57cec5SDimitry Andric void addPreEmitPass() override; 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> getCSEConfig() const override; 4000b57cec5SDimitry Andric }; 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric } // end anonymous namespace 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric TargetTransformInfo 4050b57cec5SDimitry Andric AArch64TargetMachine::getTargetTransformInfo(const Function &F) { 4060b57cec5SDimitry Andric return TargetTransformInfo(AArch64TTIImpl(this, F)); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) { 4100b57cec5SDimitry Andric return new AArch64PassConfig(*this, PM); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric std::unique_ptr<CSEConfigBase> AArch64PassConfig::getCSEConfig() const { 4140b57cec5SDimitry Andric return getStandardCSEConfigForOpt(TM->getOptLevel()); 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric void AArch64PassConfig::addIRPasses() { 4180b57cec5SDimitry Andric // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg 4190b57cec5SDimitry Andric // ourselves. 4200b57cec5SDimitry Andric addPass(createAtomicExpandPass()); 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric // Cmpxchg instructions are often used with a subsequent comparison to 4230b57cec5SDimitry Andric // determine whether it succeeded. We can exploit existing control-flow in 4240b57cec5SDimitry Andric // ldrex/strex loops to simplify this, but it needs tidying up. 4250b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy) 4260b57cec5SDimitry Andric addPass(createCFGSimplificationPass(1, true, true, false, true)); 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric // Run LoopDataPrefetch 4290b57cec5SDimitry Andric // 4300b57cec5SDimitry Andric // Run this before LSR to remove the multiplies involved in computing the 4310b57cec5SDimitry Andric // pointer values N iterations ahead. 4320b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None) { 4330b57cec5SDimitry Andric if (EnableLoopDataPrefetch) 4340b57cec5SDimitry Andric addPass(createLoopDataPrefetchPass()); 4350b57cec5SDimitry Andric if (EnableFalkorHWPFFix) 4360b57cec5SDimitry Andric addPass(createFalkorMarkStridedAccessesPass()); 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric TargetPassConfig::addIRPasses(); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric // Match interleaved memory accesses to ldN/stN intrinsics. 4420b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None) { 4430b57cec5SDimitry Andric addPass(createInterleavedLoadCombinePass()); 4440b57cec5SDimitry Andric addPass(createInterleavedAccessPass()); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) { 4480b57cec5SDimitry Andric // Call SeparateConstOffsetFromGEP pass to extract constants within indices 4490b57cec5SDimitry Andric // and lower a GEP with multiple indices to either arithmetic operations or 4500b57cec5SDimitry Andric // multiple GEPs with single index. 4510b57cec5SDimitry Andric addPass(createSeparateConstOffsetFromGEPPass(true)); 4520b57cec5SDimitry Andric // Call EarlyCSE pass to find and remove subexpressions in the lowered 4530b57cec5SDimitry Andric // result. 4540b57cec5SDimitry Andric addPass(createEarlyCSEPass()); 4550b57cec5SDimitry Andric // Do loop invariant code motion in case part of the lowered result is 4560b57cec5SDimitry Andric // invariant. 4570b57cec5SDimitry Andric addPass(createLICMPass()); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric 460*8bcb0991SDimitry Andric addPass(createAArch64StackTaggingPass(/* MergeInit = */ TM->getOptLevel() != 461*8bcb0991SDimitry Andric CodeGenOpt::None)); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric // Pass Pipeline Configuration 4650b57cec5SDimitry Andric bool AArch64PassConfig::addPreISel() { 4660b57cec5SDimitry Andric // Run promote constant before global merge, so that the promoted constants 4670b57cec5SDimitry Andric // get a chance to be merged 4680b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant) 4690b57cec5SDimitry Andric addPass(createAArch64PromoteConstantPass()); 4700b57cec5SDimitry Andric // FIXME: On AArch64, this depends on the type. 4710b57cec5SDimitry Andric // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes(). 4720b57cec5SDimitry Andric // and the offset has to be a multiple of the related size in bytes. 4730b57cec5SDimitry Andric if ((TM->getOptLevel() != CodeGenOpt::None && 4740b57cec5SDimitry Andric EnableGlobalMerge == cl::BOU_UNSET) || 4750b57cec5SDimitry Andric EnableGlobalMerge == cl::BOU_TRUE) { 4760b57cec5SDimitry Andric bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) && 4770b57cec5SDimitry Andric (EnableGlobalMerge == cl::BOU_UNSET); 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric // Merging of extern globals is enabled by default on non-Mach-O as we 4800b57cec5SDimitry Andric // expect it to be generally either beneficial or harmless. On Mach-O it 4810b57cec5SDimitry Andric // is disabled as we emit the .subsections_via_symbols directive which 4820b57cec5SDimitry Andric // means that merging extern globals is not safe. 4830b57cec5SDimitry Andric bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO(); 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric // FIXME: extern global merging is only enabled when we optimise for size 4860b57cec5SDimitry Andric // because there are some regressions with it also enabled for performance. 4870b57cec5SDimitry Andric if (!OnlyOptimizeForSize) 4880b57cec5SDimitry Andric MergeExternalByDefault = false; 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize, 4910b57cec5SDimitry Andric MergeExternalByDefault)); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric return false; 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric bool AArch64PassConfig::addInstSelector() { 4980b57cec5SDimitry Andric addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel())); 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many 5010b57cec5SDimitry Andric // references to _TLS_MODULE_BASE_ as possible. 5020b57cec5SDimitry Andric if (TM->getTargetTriple().isOSBinFormatELF() && 5030b57cec5SDimitry Andric getOptLevel() != CodeGenOpt::None) 5040b57cec5SDimitry Andric addPass(createAArch64CleanupLocalDynamicTLSPass()); 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric return false; 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric bool AArch64PassConfig::addIRTranslator() { 5100b57cec5SDimitry Andric addPass(new IRTranslator()); 5110b57cec5SDimitry Andric return false; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric void AArch64PassConfig::addPreLegalizeMachineIR() { 515*8bcb0991SDimitry Andric bool IsOptNone = getOptLevel() == CodeGenOpt::None; 516*8bcb0991SDimitry Andric addPass(createAArch64PreLegalizeCombiner(IsOptNone)); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric bool AArch64PassConfig::addLegalizeMachineIR() { 5200b57cec5SDimitry Andric addPass(new Legalizer()); 5210b57cec5SDimitry Andric return false; 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric bool AArch64PassConfig::addRegBankSelect() { 5250b57cec5SDimitry Andric addPass(new RegBankSelect()); 5260b57cec5SDimitry Andric return false; 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric void AArch64PassConfig::addPreGlobalInstructionSelect() { 5300b57cec5SDimitry Andric addPass(new Localizer()); 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric bool AArch64PassConfig::addGlobalInstructionSelect() { 5340b57cec5SDimitry Andric addPass(new InstructionSelect()); 5350b57cec5SDimitry Andric return false; 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric bool AArch64PassConfig::addILPOpts() { 5390b57cec5SDimitry Andric if (EnableCondOpt) 5400b57cec5SDimitry Andric addPass(createAArch64ConditionOptimizerPass()); 5410b57cec5SDimitry Andric if (EnableCCMP) 5420b57cec5SDimitry Andric addPass(createAArch64ConditionalCompares()); 5430b57cec5SDimitry Andric if (EnableMCR) 5440b57cec5SDimitry Andric addPass(&MachineCombinerID); 5450b57cec5SDimitry Andric if (EnableCondBrTuning) 5460b57cec5SDimitry Andric addPass(createAArch64CondBrTuning()); 5470b57cec5SDimitry Andric if (EnableEarlyIfConversion) 5480b57cec5SDimitry Andric addPass(&EarlyIfConverterID); 5490b57cec5SDimitry Andric if (EnableStPairSuppress) 5500b57cec5SDimitry Andric addPass(createAArch64StorePairSuppressPass()); 5510b57cec5SDimitry Andric addPass(createAArch64SIMDInstrOptPass()); 552*8bcb0991SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None) 553*8bcb0991SDimitry Andric addPass(createAArch64StackTaggingPreRAPass()); 5540b57cec5SDimitry Andric return true; 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric void AArch64PassConfig::addPreRegAlloc() { 5580b57cec5SDimitry Andric // Change dead register definitions to refer to the zero register. 5590b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination) 5600b57cec5SDimitry Andric addPass(createAArch64DeadRegisterDefinitions()); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric // Use AdvSIMD scalar instructions whenever profitable. 5630b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) { 5640b57cec5SDimitry Andric addPass(createAArch64AdvSIMDScalar()); 5650b57cec5SDimitry Andric // The AdvSIMD pass may produce copies that can be rewritten to 5660b57cec5SDimitry Andric // be register coaleascer friendly. 5670b57cec5SDimitry Andric addPass(&PeepholeOptimizerID); 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric void AArch64PassConfig::addPostRegAlloc() { 5720b57cec5SDimitry Andric // Remove redundant copy instructions. 5730b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination) 5740b57cec5SDimitry Andric addPass(createAArch64RedundantCopyEliminationPass()); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc()) 5770b57cec5SDimitry Andric // Improve performance for some FP/SIMD code for A57. 5780b57cec5SDimitry Andric addPass(createAArch64A57FPLoadBalancing()); 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric void AArch64PassConfig::addPreSched2() { 5820b57cec5SDimitry Andric // Expand some pseudo instructions to allow proper scheduling. 5830b57cec5SDimitry Andric addPass(createAArch64ExpandPseudoPass()); 5840b57cec5SDimitry Andric // Use load/store pair instructions when possible. 5850b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None) { 5860b57cec5SDimitry Andric if (EnableLoadStoreOpt) 5870b57cec5SDimitry Andric addPass(createAArch64LoadStoreOptimizationPass()); 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric // The AArch64SpeculationHardeningPass destroys dominator tree and natural 5910b57cec5SDimitry Andric // loop info, which is needed for the FalkorHWPFFixPass and also later on. 5920b57cec5SDimitry Andric // Therefore, run the AArch64SpeculationHardeningPass before the 5930b57cec5SDimitry Andric // FalkorHWPFFixPass to avoid recomputing dominator tree and natural loop 5940b57cec5SDimitry Andric // info. 5950b57cec5SDimitry Andric addPass(createAArch64SpeculationHardeningPass()); 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None) { 5980b57cec5SDimitry Andric if (EnableFalkorHWPFFix) 5990b57cec5SDimitry Andric addPass(createFalkorHWPFFixPass()); 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric } 6020b57cec5SDimitry Andric 6030b57cec5SDimitry Andric void AArch64PassConfig::addPreEmitPass() { 6040b57cec5SDimitry Andric // Machine Block Placement might have created new opportunities when run 6050b57cec5SDimitry Andric // at O3, where the Tail Duplication Threshold is set to 4 instructions. 6060b57cec5SDimitry Andric // Run the load/store optimizer once more. 6070b57cec5SDimitry Andric if (TM->getOptLevel() >= CodeGenOpt::Aggressive && EnableLoadStoreOpt) 6080b57cec5SDimitry Andric addPass(createAArch64LoadStoreOptimizationPass()); 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric if (EnableA53Fix835769) 6110b57cec5SDimitry Andric addPass(createAArch64A53Fix835769()); 6120b57cec5SDimitry Andric // Relax conditional branch instructions if they're otherwise out of 6130b57cec5SDimitry Andric // range of their destination. 6140b57cec5SDimitry Andric if (BranchRelaxation) 6150b57cec5SDimitry Andric addPass(&BranchRelaxationPassID); 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric if (EnableBranchTargets) 6180b57cec5SDimitry Andric addPass(createAArch64BranchTargetsPass()); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableCompressJumpTables) 6210b57cec5SDimitry Andric addPass(createAArch64CompressJumpTablesPass()); 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH && 6240b57cec5SDimitry Andric TM->getTargetTriple().isOSBinFormatMachO()) 6250b57cec5SDimitry Andric addPass(createAArch64CollectLOHPass()); 6260b57cec5SDimitry Andric } 627