1*9880d681SAndroid Build Coastguard Worker //===-- AArch64TargetMachine.cpp - Define TargetMachine for AArch64 -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker //
11*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
12*9880d681SAndroid Build Coastguard Worker
13*9880d681SAndroid Build Coastguard Worker #include "AArch64.h"
14*9880d681SAndroid Build Coastguard Worker #include "AArch64CallLowering.h"
15*9880d681SAndroid Build Coastguard Worker #include "AArch64RegisterBankInfo.h"
16*9880d681SAndroid Build Coastguard Worker #include "AArch64TargetMachine.h"
17*9880d681SAndroid Build Coastguard Worker #include "AArch64TargetObjectFile.h"
18*9880d681SAndroid Build Coastguard Worker #include "AArch64TargetTransformInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocRegistry.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/TargetPassConfig.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/InitializePasses.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/TargetRegistry.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
34*9880d681SAndroid Build Coastguard Worker EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
35*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> EnableMCR("aarch64-mcr",
38*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the machine combiner pass"),
39*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
42*9880d681SAndroid Build Coastguard Worker EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
43*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
46*9880d681SAndroid Build Coastguard Worker EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
47*9880d681SAndroid Build Coastguard Worker " integer instructions"), cl::init(false), cl::Hidden);
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
50*9880d681SAndroid Build Coastguard Worker EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
51*9880d681SAndroid Build Coastguard Worker "constant pass"), cl::init(true), cl::Hidden);
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
54*9880d681SAndroid Build Coastguard Worker EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
55*9880d681SAndroid Build Coastguard Worker " linker optimization hints (LOH)"), cl::init(true),
56*9880d681SAndroid Build Coastguard Worker cl::Hidden);
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
59*9880d681SAndroid Build Coastguard Worker EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
60*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the pass that removes dead"
61*9880d681SAndroid Build Coastguard Worker " definitons and replaces stores to"
62*9880d681SAndroid Build Coastguard Worker " them with stores to the zero"
63*9880d681SAndroid Build Coastguard Worker " register"),
64*9880d681SAndroid Build Coastguard Worker cl::init(true));
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
67*9880d681SAndroid Build Coastguard Worker EnableRedundantCopyElimination("aarch64-redundant-copy-elim",
68*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the redundant copy elimination pass"),
69*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
72*9880d681SAndroid Build Coastguard Worker EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
73*9880d681SAndroid Build Coastguard Worker " optimization pass"), cl::init(true), cl::Hidden);
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
76*9880d681SAndroid Build Coastguard Worker EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
77*9880d681SAndroid Build Coastguard Worker cl::desc("Run SimplifyCFG after expanding atomic operations"
78*9880d681SAndroid Build Coastguard Worker " to make use of cmpxchg flow-based information"),
79*9880d681SAndroid Build Coastguard Worker cl::init(true));
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
82*9880d681SAndroid Build Coastguard Worker EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
83*9880d681SAndroid Build Coastguard Worker cl::desc("Run early if-conversion"),
84*9880d681SAndroid Build Coastguard Worker cl::init(true));
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
87*9880d681SAndroid Build Coastguard Worker EnableCondOpt("aarch64-condopt",
88*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the condition optimizer pass"),
89*9880d681SAndroid Build Coastguard Worker cl::init(true), cl::Hidden);
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
92*9880d681SAndroid Build Coastguard Worker EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
93*9880d681SAndroid Build Coastguard Worker cl::desc("Work around Cortex-A53 erratum 835769"),
94*9880d681SAndroid Build Coastguard Worker cl::init(false));
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
97*9880d681SAndroid Build Coastguard Worker EnableGEPOpt("aarch64-gep-opt", cl::Hidden,
98*9880d681SAndroid Build Coastguard Worker cl::desc("Enable optimizations on complex GEPs"),
99*9880d681SAndroid Build Coastguard Worker cl::init(false));
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker // FIXME: Unify control over GlobalMerge.
102*9880d681SAndroid Build Coastguard Worker static cl::opt<cl::boolOrDefault>
103*9880d681SAndroid Build Coastguard Worker EnableGlobalMerge("aarch64-global-merge", cl::Hidden,
104*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the global merge pass"));
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker static cl::opt<bool>
107*9880d681SAndroid Build Coastguard Worker EnableLoopDataPrefetch("aarch64-loop-data-prefetch", cl::Hidden,
108*9880d681SAndroid Build Coastguard Worker cl::desc("Enable the loop data prefetch pass"),
109*9880d681SAndroid Build Coastguard Worker cl::init(true));
110*9880d681SAndroid Build Coastguard Worker
LLVMInitializeAArch64Target()111*9880d681SAndroid Build Coastguard Worker extern "C" void LLVMInitializeAArch64Target() {
112*9880d681SAndroid Build Coastguard Worker // Register the target.
113*9880d681SAndroid Build Coastguard Worker RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
114*9880d681SAndroid Build Coastguard Worker RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
115*9880d681SAndroid Build Coastguard Worker RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
116*9880d681SAndroid Build Coastguard Worker auto PR = PassRegistry::getPassRegistry();
117*9880d681SAndroid Build Coastguard Worker initializeGlobalISel(*PR);
118*9880d681SAndroid Build Coastguard Worker initializeAArch64ExpandPseudoPass(*PR);
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
122*9880d681SAndroid Build Coastguard Worker // AArch64 Lowering public interface.
123*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
createTLOF(const Triple & TT)124*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
125*9880d681SAndroid Build Coastguard Worker if (TT.isOSBinFormatMachO())
126*9880d681SAndroid Build Coastguard Worker return make_unique<AArch64_MachoTargetObjectFile>();
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker return make_unique<AArch64_ELFTargetObjectFile>();
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker // Helper function to build a DataLayout string
computeDataLayout(const Triple & TT,bool LittleEndian)132*9880d681SAndroid Build Coastguard Worker static std::string computeDataLayout(const Triple &TT, bool LittleEndian) {
133*9880d681SAndroid Build Coastguard Worker if (TT.isOSBinFormatMachO())
134*9880d681SAndroid Build Coastguard Worker return "e-m:o-i64:64-i128:128-n32:64-S128";
135*9880d681SAndroid Build Coastguard Worker if (LittleEndian)
136*9880d681SAndroid Build Coastguard Worker return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
137*9880d681SAndroid Build Coastguard Worker return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Helper function to set up the defaults for reciprocals.
initReciprocals(AArch64TargetMachine & TM,AArch64Subtarget & ST)141*9880d681SAndroid Build Coastguard Worker static void initReciprocals(AArch64TargetMachine& TM, AArch64Subtarget& ST)
142*9880d681SAndroid Build Coastguard Worker {
143*9880d681SAndroid Build Coastguard Worker // For the estimates, convergence is quadratic, so essentially the number of
144*9880d681SAndroid Build Coastguard Worker // digits is doubled after each iteration. ARMv8, the minimum architected
145*9880d681SAndroid Build Coastguard Worker // accuracy of the initial estimate is 2^-8. Therefore, the number of extra
146*9880d681SAndroid Build Coastguard Worker // steps to refine the result for float (23 mantissa bits) and for double
147*9880d681SAndroid Build Coastguard Worker // (52 mantissa bits) are 2 and 3, respectively.
148*9880d681SAndroid Build Coastguard Worker unsigned ExtraStepsF = 2,
149*9880d681SAndroid Build Coastguard Worker ExtraStepsD = ExtraStepsF + 1;
150*9880d681SAndroid Build Coastguard Worker bool UseRsqrt = ST.useRSqrt();
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("sqrtf", UseRsqrt, ExtraStepsF);
153*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("sqrtd", UseRsqrt, ExtraStepsD);
154*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("vec-sqrtf", UseRsqrt, ExtraStepsF);
155*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("vec-sqrtd", UseRsqrt, ExtraStepsD);
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("divf", false, ExtraStepsF);
158*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("divd", false, ExtraStepsD);
159*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("vec-divf", false, ExtraStepsF);
160*9880d681SAndroid Build Coastguard Worker TM.Options.Reciprocals.setDefaults("vec-divd", false, ExtraStepsD);
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
getEffectiveRelocModel(const Triple & TT,Optional<Reloc::Model> RM)163*9880d681SAndroid Build Coastguard Worker static Reloc::Model getEffectiveRelocModel(const Triple &TT,
164*9880d681SAndroid Build Coastguard Worker Optional<Reloc::Model> RM) {
165*9880d681SAndroid Build Coastguard Worker // AArch64 Darwin is always PIC.
166*9880d681SAndroid Build Coastguard Worker if (TT.isOSDarwin())
167*9880d681SAndroid Build Coastguard Worker return Reloc::PIC_;
168*9880d681SAndroid Build Coastguard Worker // On ELF platforms the default static relocation model has a smart enough
169*9880d681SAndroid Build Coastguard Worker // linker to cope with referencing external symbols defined in a shared
170*9880d681SAndroid Build Coastguard Worker // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
171*9880d681SAndroid Build Coastguard Worker if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
172*9880d681SAndroid Build Coastguard Worker return Reloc::Static;
173*9880d681SAndroid Build Coastguard Worker return *RM;
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker
176*9880d681SAndroid Build Coastguard Worker /// Create an AArch64 architecture model.
177*9880d681SAndroid Build Coastguard Worker ///
AArch64TargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL,bool LittleEndian)178*9880d681SAndroid Build Coastguard Worker AArch64TargetMachine::AArch64TargetMachine(
179*9880d681SAndroid Build Coastguard Worker const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
180*9880d681SAndroid Build Coastguard Worker const TargetOptions &Options, Optional<Reloc::Model> RM,
181*9880d681SAndroid Build Coastguard Worker CodeModel::Model CM, CodeGenOpt::Level OL, bool LittleEndian)
182*9880d681SAndroid Build Coastguard Worker // This nested ternary is horrible, but DL needs to be properly
183*9880d681SAndroid Build Coastguard Worker // initialized before TLInfo is constructed.
184*9880d681SAndroid Build Coastguard Worker : LLVMTargetMachine(T, computeDataLayout(TT, LittleEndian), TT, CPU, FS,
185*9880d681SAndroid Build Coastguard Worker Options, getEffectiveRelocModel(TT, RM), CM, OL),
186*9880d681SAndroid Build Coastguard Worker TLOF(createTLOF(getTargetTriple())),
187*9880d681SAndroid Build Coastguard Worker Subtarget(TT, CPU, FS, *this, LittleEndian) {
188*9880d681SAndroid Build Coastguard Worker initReciprocals(*this, Subtarget);
189*9880d681SAndroid Build Coastguard Worker initAsmInfo();
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker
~AArch64TargetMachine()192*9880d681SAndroid Build Coastguard Worker AArch64TargetMachine::~AArch64TargetMachine() {}
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILD_GLOBAL_ISEL
195*9880d681SAndroid Build Coastguard Worker namespace {
196*9880d681SAndroid Build Coastguard Worker struct AArch64GISelActualAccessor : public GISelAccessor {
197*9880d681SAndroid Build Coastguard Worker std::unique_ptr<CallLowering> CallLoweringInfo;
198*9880d681SAndroid Build Coastguard Worker std::unique_ptr<RegisterBankInfo> RegBankInfo;
getCallLowering__anon4cccfc3b0111::AArch64GISelActualAccessor199*9880d681SAndroid Build Coastguard Worker const CallLowering *getCallLowering() const override {
200*9880d681SAndroid Build Coastguard Worker return CallLoweringInfo.get();
201*9880d681SAndroid Build Coastguard Worker }
getRegBankInfo__anon4cccfc3b0111::AArch64GISelActualAccessor202*9880d681SAndroid Build Coastguard Worker const RegisterBankInfo *getRegBankInfo() const override {
203*9880d681SAndroid Build Coastguard Worker return RegBankInfo.get();
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker };
206*9880d681SAndroid Build Coastguard Worker } // End anonymous namespace.
207*9880d681SAndroid Build Coastguard Worker #endif
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker const AArch64Subtarget *
getSubtargetImpl(const Function & F) const210*9880d681SAndroid Build Coastguard Worker AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
211*9880d681SAndroid Build Coastguard Worker Attribute CPUAttr = F.getFnAttribute("target-cpu");
212*9880d681SAndroid Build Coastguard Worker Attribute FSAttr = F.getFnAttribute("target-features");
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
215*9880d681SAndroid Build Coastguard Worker ? CPUAttr.getValueAsString().str()
216*9880d681SAndroid Build Coastguard Worker : TargetCPU;
217*9880d681SAndroid Build Coastguard Worker std::string FS = !FSAttr.hasAttribute(Attribute::None)
218*9880d681SAndroid Build Coastguard Worker ? FSAttr.getValueAsString().str()
219*9880d681SAndroid Build Coastguard Worker : TargetFS;
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker auto &I = SubtargetMap[CPU + FS];
222*9880d681SAndroid Build Coastguard Worker if (!I) {
223*9880d681SAndroid Build Coastguard Worker // This needs to be done before we create a new subtarget since any
224*9880d681SAndroid Build Coastguard Worker // creation will depend on the TM and the code generation flags on the
225*9880d681SAndroid Build Coastguard Worker // function that reside in TargetOptions.
226*9880d681SAndroid Build Coastguard Worker resetTargetOptions(F);
227*9880d681SAndroid Build Coastguard Worker I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
228*9880d681SAndroid Build Coastguard Worker Subtarget.isLittleEndian());
229*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_BUILD_GLOBAL_ISEL
230*9880d681SAndroid Build Coastguard Worker GISelAccessor *GISel = new GISelAccessor();
231*9880d681SAndroid Build Coastguard Worker #else
232*9880d681SAndroid Build Coastguard Worker AArch64GISelActualAccessor *GISel =
233*9880d681SAndroid Build Coastguard Worker new AArch64GISelActualAccessor();
234*9880d681SAndroid Build Coastguard Worker GISel->CallLoweringInfo.reset(
235*9880d681SAndroid Build Coastguard Worker new AArch64CallLowering(*I->getTargetLowering()));
236*9880d681SAndroid Build Coastguard Worker GISel->RegBankInfo.reset(
237*9880d681SAndroid Build Coastguard Worker new AArch64RegisterBankInfo(*I->getRegisterInfo()));
238*9880d681SAndroid Build Coastguard Worker #endif
239*9880d681SAndroid Build Coastguard Worker I->setGISelAccessor(*GISel);
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker return I.get();
242*9880d681SAndroid Build Coastguard Worker }
243*9880d681SAndroid Build Coastguard Worker
anchor()244*9880d681SAndroid Build Coastguard Worker void AArch64leTargetMachine::anchor() { }
245*9880d681SAndroid Build Coastguard Worker
AArch64leTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)246*9880d681SAndroid Build Coastguard Worker AArch64leTargetMachine::AArch64leTargetMachine(
247*9880d681SAndroid Build Coastguard Worker const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
248*9880d681SAndroid Build Coastguard Worker const TargetOptions &Options, Optional<Reloc::Model> RM,
249*9880d681SAndroid Build Coastguard Worker CodeModel::Model CM, CodeGenOpt::Level OL)
250*9880d681SAndroid Build Coastguard Worker : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
251*9880d681SAndroid Build Coastguard Worker
anchor()252*9880d681SAndroid Build Coastguard Worker void AArch64beTargetMachine::anchor() { }
253*9880d681SAndroid Build Coastguard Worker
AArch64beTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)254*9880d681SAndroid Build Coastguard Worker AArch64beTargetMachine::AArch64beTargetMachine(
255*9880d681SAndroid Build Coastguard Worker const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
256*9880d681SAndroid Build Coastguard Worker const TargetOptions &Options, Optional<Reloc::Model> RM,
257*9880d681SAndroid Build Coastguard Worker CodeModel::Model CM, CodeGenOpt::Level OL)
258*9880d681SAndroid Build Coastguard Worker : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker namespace {
261*9880d681SAndroid Build Coastguard Worker /// AArch64 Code Generator Pass Configuration Options.
262*9880d681SAndroid Build Coastguard Worker class AArch64PassConfig : public TargetPassConfig {
263*9880d681SAndroid Build Coastguard Worker public:
AArch64PassConfig(AArch64TargetMachine * TM,PassManagerBase & PM)264*9880d681SAndroid Build Coastguard Worker AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
265*9880d681SAndroid Build Coastguard Worker : TargetPassConfig(TM, PM) {
266*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None)
267*9880d681SAndroid Build Coastguard Worker substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
268*9880d681SAndroid Build Coastguard Worker }
269*9880d681SAndroid Build Coastguard Worker
getAArch64TargetMachine() const270*9880d681SAndroid Build Coastguard Worker AArch64TargetMachine &getAArch64TargetMachine() const {
271*9880d681SAndroid Build Coastguard Worker return getTM<AArch64TargetMachine>();
272*9880d681SAndroid Build Coastguard Worker }
273*9880d681SAndroid Build Coastguard Worker
274*9880d681SAndroid Build Coastguard Worker void addIRPasses() override;
275*9880d681SAndroid Build Coastguard Worker bool addPreISel() override;
276*9880d681SAndroid Build Coastguard Worker bool addInstSelector() override;
277*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILD_GLOBAL_ISEL
278*9880d681SAndroid Build Coastguard Worker bool addIRTranslator() override;
279*9880d681SAndroid Build Coastguard Worker bool addRegBankSelect() override;
280*9880d681SAndroid Build Coastguard Worker #endif
281*9880d681SAndroid Build Coastguard Worker bool addILPOpts() override;
282*9880d681SAndroid Build Coastguard Worker void addPreRegAlloc() override;
283*9880d681SAndroid Build Coastguard Worker void addPostRegAlloc() override;
284*9880d681SAndroid Build Coastguard Worker void addPreSched2() override;
285*9880d681SAndroid Build Coastguard Worker void addPreEmitPass() override;
286*9880d681SAndroid Build Coastguard Worker };
287*9880d681SAndroid Build Coastguard Worker } // namespace
288*9880d681SAndroid Build Coastguard Worker
getTargetIRAnalysis()289*9880d681SAndroid Build Coastguard Worker TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
290*9880d681SAndroid Build Coastguard Worker return TargetIRAnalysis([this](const Function &F) {
291*9880d681SAndroid Build Coastguard Worker return TargetTransformInfo(AArch64TTIImpl(this, F));
292*9880d681SAndroid Build Coastguard Worker });
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
createPassConfig(PassManagerBase & PM)295*9880d681SAndroid Build Coastguard Worker TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
296*9880d681SAndroid Build Coastguard Worker return new AArch64PassConfig(this, PM);
297*9880d681SAndroid Build Coastguard Worker }
298*9880d681SAndroid Build Coastguard Worker
addIRPasses()299*9880d681SAndroid Build Coastguard Worker void AArch64PassConfig::addIRPasses() {
300*9880d681SAndroid Build Coastguard Worker // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
301*9880d681SAndroid Build Coastguard Worker // ourselves.
302*9880d681SAndroid Build Coastguard Worker addPass(createAtomicExpandPass(TM));
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker // Cmpxchg instructions are often used with a subsequent comparison to
305*9880d681SAndroid Build Coastguard Worker // determine whether it succeeded. We can exploit existing control-flow in
306*9880d681SAndroid Build Coastguard Worker // ldrex/strex loops to simplify this, but it needs tidying up.
307*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
308*9880d681SAndroid Build Coastguard Worker addPass(createCFGSimplificationPass());
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker // Run LoopDataPrefetch
311*9880d681SAndroid Build Coastguard Worker //
312*9880d681SAndroid Build Coastguard Worker // Run this before LSR to remove the multiplies involved in computing the
313*9880d681SAndroid Build Coastguard Worker // pointer values N iterations ahead.
314*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableLoopDataPrefetch)
315*9880d681SAndroid Build Coastguard Worker addPass(createLoopDataPrefetchPass());
316*9880d681SAndroid Build Coastguard Worker
317*9880d681SAndroid Build Coastguard Worker TargetPassConfig::addIRPasses();
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker // Match interleaved memory accesses to ldN/stN intrinsics.
320*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None)
321*9880d681SAndroid Build Coastguard Worker addPass(createInterleavedAccessPass(TM));
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
324*9880d681SAndroid Build Coastguard Worker // Call SeparateConstOffsetFromGEP pass to extract constants within indices
325*9880d681SAndroid Build Coastguard Worker // and lower a GEP with multiple indices to either arithmetic operations or
326*9880d681SAndroid Build Coastguard Worker // multiple GEPs with single index.
327*9880d681SAndroid Build Coastguard Worker addPass(createSeparateConstOffsetFromGEPPass(TM, true));
328*9880d681SAndroid Build Coastguard Worker // Call EarlyCSE pass to find and remove subexpressions in the lowered
329*9880d681SAndroid Build Coastguard Worker // result.
330*9880d681SAndroid Build Coastguard Worker addPass(createEarlyCSEPass());
331*9880d681SAndroid Build Coastguard Worker // Do loop invariant code motion in case part of the lowered result is
332*9880d681SAndroid Build Coastguard Worker // invariant.
333*9880d681SAndroid Build Coastguard Worker addPass(createLICMPass());
334*9880d681SAndroid Build Coastguard Worker }
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker // Pass Pipeline Configuration
addPreISel()338*9880d681SAndroid Build Coastguard Worker bool AArch64PassConfig::addPreISel() {
339*9880d681SAndroid Build Coastguard Worker // Run promote constant before global merge, so that the promoted constants
340*9880d681SAndroid Build Coastguard Worker // get a chance to be merged
341*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
342*9880d681SAndroid Build Coastguard Worker addPass(createAArch64PromoteConstantPass());
343*9880d681SAndroid Build Coastguard Worker // FIXME: On AArch64, this depends on the type.
344*9880d681SAndroid Build Coastguard Worker // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
345*9880d681SAndroid Build Coastguard Worker // and the offset has to be a multiple of the related size in bytes.
346*9880d681SAndroid Build Coastguard Worker if ((TM->getOptLevel() != CodeGenOpt::None &&
347*9880d681SAndroid Build Coastguard Worker EnableGlobalMerge == cl::BOU_UNSET) ||
348*9880d681SAndroid Build Coastguard Worker EnableGlobalMerge == cl::BOU_TRUE) {
349*9880d681SAndroid Build Coastguard Worker bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
350*9880d681SAndroid Build Coastguard Worker (EnableGlobalMerge == cl::BOU_UNSET);
351*9880d681SAndroid Build Coastguard Worker addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
352*9880d681SAndroid Build Coastguard Worker }
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None)
355*9880d681SAndroid Build Coastguard Worker addPass(createAArch64AddressTypePromotionPass());
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard Worker return false;
358*9880d681SAndroid Build Coastguard Worker }
359*9880d681SAndroid Build Coastguard Worker
addInstSelector()360*9880d681SAndroid Build Coastguard Worker bool AArch64PassConfig::addInstSelector() {
361*9880d681SAndroid Build Coastguard Worker addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
364*9880d681SAndroid Build Coastguard Worker // references to _TLS_MODULE_BASE_ as possible.
365*9880d681SAndroid Build Coastguard Worker if (TM->getTargetTriple().isOSBinFormatELF() &&
366*9880d681SAndroid Build Coastguard Worker getOptLevel() != CodeGenOpt::None)
367*9880d681SAndroid Build Coastguard Worker addPass(createAArch64CleanupLocalDynamicTLSPass());
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker return false;
370*9880d681SAndroid Build Coastguard Worker }
371*9880d681SAndroid Build Coastguard Worker
372*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_BUILD_GLOBAL_ISEL
addIRTranslator()373*9880d681SAndroid Build Coastguard Worker bool AArch64PassConfig::addIRTranslator() {
374*9880d681SAndroid Build Coastguard Worker addPass(new IRTranslator());
375*9880d681SAndroid Build Coastguard Worker return false;
376*9880d681SAndroid Build Coastguard Worker }
addRegBankSelect()377*9880d681SAndroid Build Coastguard Worker bool AArch64PassConfig::addRegBankSelect() {
378*9880d681SAndroid Build Coastguard Worker addPass(new RegBankSelect());
379*9880d681SAndroid Build Coastguard Worker return false;
380*9880d681SAndroid Build Coastguard Worker }
381*9880d681SAndroid Build Coastguard Worker #endif
382*9880d681SAndroid Build Coastguard Worker
addILPOpts()383*9880d681SAndroid Build Coastguard Worker bool AArch64PassConfig::addILPOpts() {
384*9880d681SAndroid Build Coastguard Worker if (EnableCondOpt)
385*9880d681SAndroid Build Coastguard Worker addPass(createAArch64ConditionOptimizerPass());
386*9880d681SAndroid Build Coastguard Worker if (EnableCCMP)
387*9880d681SAndroid Build Coastguard Worker addPass(createAArch64ConditionalCompares());
388*9880d681SAndroid Build Coastguard Worker if (EnableMCR)
389*9880d681SAndroid Build Coastguard Worker addPass(&MachineCombinerID);
390*9880d681SAndroid Build Coastguard Worker if (EnableEarlyIfConversion)
391*9880d681SAndroid Build Coastguard Worker addPass(&EarlyIfConverterID);
392*9880d681SAndroid Build Coastguard Worker if (EnableStPairSuppress)
393*9880d681SAndroid Build Coastguard Worker addPass(createAArch64StorePairSuppressPass());
394*9880d681SAndroid Build Coastguard Worker return true;
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker
addPreRegAlloc()397*9880d681SAndroid Build Coastguard Worker void AArch64PassConfig::addPreRegAlloc() {
398*9880d681SAndroid Build Coastguard Worker // Use AdvSIMD scalar instructions whenever profitable.
399*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
400*9880d681SAndroid Build Coastguard Worker addPass(createAArch64AdvSIMDScalar());
401*9880d681SAndroid Build Coastguard Worker // The AdvSIMD pass may produce copies that can be rewritten to
402*9880d681SAndroid Build Coastguard Worker // be register coaleascer friendly.
403*9880d681SAndroid Build Coastguard Worker addPass(&PeepholeOptimizerID);
404*9880d681SAndroid Build Coastguard Worker }
405*9880d681SAndroid Build Coastguard Worker }
406*9880d681SAndroid Build Coastguard Worker
addPostRegAlloc()407*9880d681SAndroid Build Coastguard Worker void AArch64PassConfig::addPostRegAlloc() {
408*9880d681SAndroid Build Coastguard Worker // Remove redundant copy instructions.
409*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
410*9880d681SAndroid Build Coastguard Worker addPass(createAArch64RedundantCopyEliminationPass());
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker // Change dead register definitions to refer to the zero register.
413*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
414*9880d681SAndroid Build Coastguard Worker addPass(createAArch64DeadRegisterDefinitions());
415*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
416*9880d681SAndroid Build Coastguard Worker // Improve performance for some FP/SIMD code for A57.
417*9880d681SAndroid Build Coastguard Worker addPass(createAArch64A57FPLoadBalancing());
418*9880d681SAndroid Build Coastguard Worker }
419*9880d681SAndroid Build Coastguard Worker
addPreSched2()420*9880d681SAndroid Build Coastguard Worker void AArch64PassConfig::addPreSched2() {
421*9880d681SAndroid Build Coastguard Worker // Expand some pseudo instructions to allow proper scheduling.
422*9880d681SAndroid Build Coastguard Worker addPass(createAArch64ExpandPseudoPass());
423*9880d681SAndroid Build Coastguard Worker // Use load/store pair instructions when possible.
424*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
425*9880d681SAndroid Build Coastguard Worker addPass(createAArch64LoadStoreOptimizationPass());
426*9880d681SAndroid Build Coastguard Worker }
427*9880d681SAndroid Build Coastguard Worker
addPreEmitPass()428*9880d681SAndroid Build Coastguard Worker void AArch64PassConfig::addPreEmitPass() {
429*9880d681SAndroid Build Coastguard Worker if (EnableA53Fix835769)
430*9880d681SAndroid Build Coastguard Worker addPass(createAArch64A53Fix835769());
431*9880d681SAndroid Build Coastguard Worker // Relax conditional branch instructions if they're otherwise out of
432*9880d681SAndroid Build Coastguard Worker // range of their destination.
433*9880d681SAndroid Build Coastguard Worker addPass(createAArch64BranchRelaxation());
434*9880d681SAndroid Build Coastguard Worker if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
435*9880d681SAndroid Build Coastguard Worker TM->getTargetTriple().isOSBinFormatMachO())
436*9880d681SAndroid Build Coastguard Worker addPass(createAArch64CollectLOHPass());
437*9880d681SAndroid Build Coastguard Worker }
438