1 //===-- AArch64TargetMachine.cpp - Define TargetMachine for AArch64 -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
14 #include "AArch64TargetMachine.h"
15 #include "AArch64TargetObjectFile.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/RegAllocRegistry.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Transforms/Scalar.h"
27 EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
28 cl::init(true), cl::Hidden);
30 static cl::opt<bool> EnableMCR("aarch64-mcr",
31 cl::desc("Enable the machine combiner pass"),
32 cl::init(true), cl::Hidden);
35 EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
36 cl::init(true), cl::Hidden);
39 EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
40 " integer instructions"), cl::init(false), cl::Hidden);
43 EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
44 "constant pass"), cl::init(true), cl::Hidden);
47 EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
48 " linker optimization hints (LOH)"), cl::init(true),
52 EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
53 cl::desc("Enable the pass that removes dead"
54 " definitons and replaces stores to"
55 " them with stores to the zero"
60 EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
61 " optimization pass"), cl::init(true), cl::Hidden);
64 EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
65 cl::desc("Run SimplifyCFG after expanding atomic operations"
66 " to make use of cmpxchg flow-based information"),
70 EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
71 cl::desc("Run early if-conversion"),
75 EnableCondOpt("aarch64-condopt",
76 cl::desc("Enable the condition optimizer pass"),
77 cl::init(true), cl::Hidden);
80 EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
81 cl::desc("Work around Cortex-A53 erratum 835769"),
85 EnableGEPOpt("aarch64-gep-opt", cl::Hidden,
86 cl::desc("Enable optimizations on complex GEPs"),
89 extern "C" void LLVMInitializeAArch64Target() {
90 // Register the target.
91 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
92 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
93 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
96 //===----------------------------------------------------------------------===//
97 // AArch64 Lowering public interface.
98 //===----------------------------------------------------------------------===//
99 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
100 if (TT.isOSBinFormatMachO())
101 return make_unique<AArch64_MachoTargetObjectFile>();
103 return make_unique<AArch64_ELFTargetObjectFile>();
106 /// TargetMachine ctor - Create an AArch64 architecture model.
108 AArch64TargetMachine::AArch64TargetMachine(const Target &T, StringRef TT,
109 StringRef CPU, StringRef FS,
110 const TargetOptions &Options,
111 Reloc::Model RM, CodeModel::Model CM,
112 CodeGenOpt::Level OL,
114 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
115 TLOF(createTLOF(Triple(getTargetTriple()))),
116 Subtarget(TT, CPU, FS, *this, LittleEndian), isLittle(LittleEndian) {
120 AArch64TargetMachine::~AArch64TargetMachine() {}
122 const AArch64Subtarget *
123 AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
124 AttributeSet FnAttrs = F.getAttributes();
126 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
128 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
130 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
131 ? CPUAttr.getValueAsString().str()
133 std::string FS = !FSAttr.hasAttribute(Attribute::None)
134 ? FSAttr.getValueAsString().str()
137 auto &I = SubtargetMap[CPU + FS];
139 // This needs to be done before we create a new subtarget since any
140 // creation will depend on the TM and the code generation flags on the
141 // function that reside in TargetOptions.
142 resetTargetOptions(F);
143 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this, isLittle);
148 void AArch64leTargetMachine::anchor() { }
150 AArch64leTargetMachine::
151 AArch64leTargetMachine(const Target &T, StringRef TT,
152 StringRef CPU, StringRef FS, const TargetOptions &Options,
153 Reloc::Model RM, CodeModel::Model CM,
154 CodeGenOpt::Level OL)
155 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
157 void AArch64beTargetMachine::anchor() { }
159 AArch64beTargetMachine::
160 AArch64beTargetMachine(const Target &T, StringRef TT,
161 StringRef CPU, StringRef FS, const TargetOptions &Options,
162 Reloc::Model RM, CodeModel::Model CM,
163 CodeGenOpt::Level OL)
164 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
167 /// AArch64 Code Generator Pass Configuration Options.
168 class AArch64PassConfig : public TargetPassConfig {
170 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
171 : TargetPassConfig(TM, PM) {
172 if (TM->getOptLevel() != CodeGenOpt::None)
173 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
176 AArch64TargetMachine &getAArch64TargetMachine() const {
177 return getTM<AArch64TargetMachine>();
180 void addIRPasses() override;
181 bool addPreISel() override;
182 bool addInstSelector() override;
183 bool addILPOpts() override;
184 void addPreRegAlloc() override;
185 void addPostRegAlloc() override;
186 void addPreSched2() override;
187 void addPreEmitPass() override;
191 void AArch64TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
192 // Add first the target-independent BasicTTI pass, then our AArch64 pass. This
193 // allows the AArch64 pass to delegate to the target independent layer when
195 PM.add(createBasicTargetTransformInfoPass(this));
196 PM.add(createAArch64TargetTransformInfoPass(this));
199 TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
200 return new AArch64PassConfig(this, PM);
203 void AArch64PassConfig::addIRPasses() {
204 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
206 addPass(createAtomicExpandPass(TM));
208 // Cmpxchg instructions are often used with a subsequent comparison to
209 // determine whether it succeeded. We can exploit existing control-flow in
210 // ldrex/strex loops to simplify this, but it needs tidying up.
211 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
212 addPass(createCFGSimplificationPass());
214 TargetPassConfig::addIRPasses();
216 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
217 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
218 // and lower a GEP with multiple indices to either arithmetic operations or
219 // multiple GEPs with single index.
220 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
221 // Call EarlyCSE pass to find and remove subexpressions in the lowered
223 addPass(createEarlyCSEPass());
224 // Do loop invariant code motion in case part of the lowered result is
226 addPass(createLICMPass());
230 // Pass Pipeline Configuration
231 bool AArch64PassConfig::addPreISel() {
232 // Run promote constant before global merge, so that the promoted constants
233 // get a chance to be merged
234 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
235 addPass(createAArch64PromoteConstantPass());
236 if (TM->getOptLevel() != CodeGenOpt::None)
237 addPass(createGlobalMergePass(TM));
238 if (TM->getOptLevel() != CodeGenOpt::None)
239 addPass(createAArch64AddressTypePromotionPass());
244 bool AArch64PassConfig::addInstSelector() {
245 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
247 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
248 // references to _TLS_MODULE_BASE_ as possible.
249 if (TM->getSubtarget<AArch64Subtarget>().isTargetELF() &&
250 getOptLevel() != CodeGenOpt::None)
251 addPass(createAArch64CleanupLocalDynamicTLSPass());
256 bool AArch64PassConfig::addILPOpts() {
258 addPass(createAArch64ConditionOptimizerPass());
260 addPass(createAArch64ConditionalCompares());
262 addPass(&MachineCombinerID);
263 if (EnableEarlyIfConversion)
264 addPass(&EarlyIfConverterID);
265 if (EnableStPairSuppress)
266 addPass(createAArch64StorePairSuppressPass());
270 void AArch64PassConfig::addPreRegAlloc() {
271 // Use AdvSIMD scalar instructions whenever profitable.
272 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
273 addPass(createAArch64AdvSIMDScalar());
274 // The AdvSIMD pass may produce copies that can be rewritten to
275 // be register coaleascer friendly.
276 addPass(&PeepholeOptimizerID);
280 void AArch64PassConfig::addPostRegAlloc() {
281 // Change dead register definitions to refer to the zero register.
282 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
283 addPass(createAArch64DeadRegisterDefinitions());
284 if (TM->getOptLevel() != CodeGenOpt::None &&
285 (TM->getSubtarget<AArch64Subtarget>().isCortexA53() ||
286 TM->getSubtarget<AArch64Subtarget>().isCortexA57()) &&
287 usingDefaultRegAlloc())
288 // Improve performance for some FP/SIMD code for A57.
289 addPass(createAArch64A57FPLoadBalancing());
292 void AArch64PassConfig::addPreSched2() {
293 // Expand some pseudo instructions to allow proper scheduling.
294 addPass(createAArch64ExpandPseudoPass());
295 // Use load/store pair instructions when possible.
296 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
297 addPass(createAArch64LoadStoreOptimizationPass());
300 void AArch64PassConfig::addPreEmitPass() {
301 if (EnableA53Fix835769)
302 addPass(createAArch64A53Fix835769());
303 // Relax conditional branch instructions if they're otherwise out of
304 // range of their destination.
305 addPass(createAArch64BranchRelaxation());
306 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
307 TM->getSubtarget<AArch64Subtarget>().isTargetMachO())
308 addPass(createAArch64CollectLOHPass());