blob: 791cb900a3f4cda72d3e7dcd3d883ac8acbf0ae9 [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- AArch64TargetMachine.cpp - Define TargetMachine for AArch64 -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "AArch64.h"
14#include "AArch64TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000015#include "AArch64TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000016#include "AArch64TargetTransformInfo.h"
Quentin Colombetd96f4952016-02-11 19:35:06 +000017#ifdef LLVM_BUILD_GLOBAL_ISEL
18# include "llvm/CodeGen/GlobalISel/IRTranslator.h"
19#endif
Tim Northover3b0846e2014-05-24 12:50:23 +000020#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000021#include "llvm/CodeGen/RegAllocRegistry.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000022#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000023#include "llvm/IR/LegacyPassManager.h"
Quentin Colombetf574ab22016-03-08 01:45:36 +000024#include "llvm/InitializePasses.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Target/TargetOptions.h"
28#include "llvm/Transforms/Scalar.h"
29using namespace llvm;
30
31static cl::opt<bool>
32EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
33 cl::init(true), cl::Hidden);
34
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000035static cl::opt<bool> EnableMCR("aarch64-mcr",
36 cl::desc("Enable the machine combiner pass"),
37 cl::init(true), cl::Hidden);
38
Tim Northover3b0846e2014-05-24 12:50:23 +000039static cl::opt<bool>
40EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
41 cl::init(true), cl::Hidden);
42
43static cl::opt<bool>
44EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
45 " integer instructions"), cl::init(false), cl::Hidden);
46
47static cl::opt<bool>
48EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
49 "constant pass"), cl::init(true), cl::Hidden);
50
51static cl::opt<bool>
52EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
53 " linker optimization hints (LOH)"), cl::init(true),
54 cl::Hidden);
55
56static cl::opt<bool>
57EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
58 cl::desc("Enable the pass that removes dead"
59 " definitons and replaces stores to"
60 " them with stores to the zero"
61 " register"),
62 cl::init(true));
63
64static cl::opt<bool>
Jun Bum Limb389d9b2016-02-16 20:02:39 +000065EnableRedundantCopyElimination("aarch64-redundant-copy-elim",
66 cl::desc("Enable the redundant copy elimination pass"),
67 cl::init(true), cl::Hidden);
68
69static cl::opt<bool>
Tim Northover3b0846e2014-05-24 12:50:23 +000070EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
71 " optimization pass"), cl::init(true), cl::Hidden);
72
Tim Northoverb4ddc082014-05-30 10:09:59 +000073static cl::opt<bool>
74EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
75 cl::desc("Run SimplifyCFG after expanding atomic operations"
76 " to make use of cmpxchg flow-based information"),
77 cl::init(true));
78
James Molloy99917942014-08-06 13:31:32 +000079static cl::opt<bool>
80EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
81 cl::desc("Run early if-conversion"),
82 cl::init(true));
83
Jiangning Liu1a486da2014-09-05 02:55:24 +000084static cl::opt<bool>
85EnableCondOpt("aarch64-condopt",
86 cl::desc("Enable the condition optimizer pass"),
87 cl::init(true), cl::Hidden);
88
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000089static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +000090EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
91 cl::desc("Work around Cortex-A53 erratum 835769"),
92 cl::init(false));
93
Hao Liufd46bea2014-11-19 06:39:53 +000094static cl::opt<bool>
95EnableGEPOpt("aarch64-gep-opt", cl::Hidden,
96 cl::desc("Enable optimizations on complex GEPs"),
James Molloycd2334e2015-04-22 09:11:38 +000097 cl::init(false));
Hao Liufd46bea2014-11-19 06:39:53 +000098
Ahmed Bougachab96444e2015-04-11 00:06:36 +000099// FIXME: Unify control over GlobalMerge.
100static cl::opt<cl::boolOrDefault>
101EnableGlobalMerge("aarch64-global-merge", cl::Hidden,
102 cl::desc("Enable the global merge pass"));
103
Adam Nemet53e758f2016-03-18 00:27:29 +0000104static cl::opt<bool>
105 EnableLoopDataPrefetch("aarch64-loop-data-prefetch", cl::Hidden,
106 cl::desc("Enable the loop data prefetch pass"),
107 cl::init(false));
108
Tim Northover3b0846e2014-05-24 12:50:23 +0000109extern "C" void LLVMInitializeAArch64Target() {
110 // Register the target.
111 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
112 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
Tim Northover35910d72014-07-23 12:58:11 +0000113 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
Quentin Colombetf574ab22016-03-08 01:45:36 +0000114 initializeGlobalISel(*PassRegistry::getPassRegistry());
Tim Northover3b0846e2014-05-24 12:50:23 +0000115}
116
Aditya Nandakumara2719322014-11-13 09:26:31 +0000117//===----------------------------------------------------------------------===//
118// AArch64 Lowering public interface.
119//===----------------------------------------------------------------------===//
120static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
121 if (TT.isOSBinFormatMachO())
122 return make_unique<AArch64_MachoTargetObjectFile>();
123
124 return make_unique<AArch64_ELFTargetObjectFile>();
125}
126
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000127// Helper function to build a DataLayout string
Daniel Sandersed64d622015-06-11 15:34:59 +0000128static std::string computeDataLayout(const Triple &TT, bool LittleEndian) {
129 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000130 return "e-m:o-i64:64-i128:128-n32:64-S128";
131 if (LittleEndian)
132 return "e-m:e-i64:64-i128:128-n32:64-S128";
133 return "E-m:e-i64:64-i128:128-n32:64-S128";
134}
135
Tim Northover3b0846e2014-05-24 12:50:23 +0000136/// TargetMachine ctor - Create an AArch64 architecture model.
137///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000138AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
Tim Northover3b0846e2014-05-24 12:50:23 +0000139 StringRef CPU, StringRef FS,
140 const TargetOptions &Options,
141 Reloc::Model RM, CodeModel::Model CM,
142 CodeGenOpt::Level OL,
143 bool LittleEndian)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000144 // This nested ternary is horrible, but DL needs to be properly
Eric Christopher63ea0402015-03-12 18:23:01 +0000145 // initialized before TLInfo is constructed.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000146 : LLVMTargetMachine(T, computeDataLayout(TT, LittleEndian), TT, CPU, FS,
147 Options, RM, CM, OL),
148 TLOF(createTLOF(getTargetTriple())),
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000149 isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000150 initAsmInfo();
151}
152
Reid Kleckner357600e2014-11-20 23:37:18 +0000153AArch64TargetMachine::~AArch64TargetMachine() {}
154
Eric Christopher3faf2f12014-10-06 06:45:36 +0000155const AArch64Subtarget *
156AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000157 Attribute CPUAttr = F.getFnAttribute("target-cpu");
158 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000159
160 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
161 ? CPUAttr.getValueAsString().str()
162 : TargetCPU;
163 std::string FS = !FSAttr.hasAttribute(Attribute::None)
164 ? FSAttr.getValueAsString().str()
165 : TargetFS;
166
167 auto &I = SubtargetMap[CPU + FS];
168 if (!I) {
169 // This needs to be done before we create a new subtarget since any
170 // creation will depend on the TM and the code generation flags on the
171 // function that reside in TargetOptions.
172 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000173 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
174 isLittle);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000175 }
176 return I.get();
177}
178
Tim Northover3b0846e2014-05-24 12:50:23 +0000179void AArch64leTargetMachine::anchor() { }
180
Daniel Sanders3e5de882015-06-11 19:41:26 +0000181AArch64leTargetMachine::AArch64leTargetMachine(
182 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
183 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
184 CodeGenOpt::Level OL)
185 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000186
187void AArch64beTargetMachine::anchor() { }
188
Daniel Sanders3e5de882015-06-11 19:41:26 +0000189AArch64beTargetMachine::AArch64beTargetMachine(
190 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
191 const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
192 CodeGenOpt::Level OL)
193 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000194
195namespace {
196/// AArch64 Code Generator Pass Configuration Options.
197class AArch64PassConfig : public TargetPassConfig {
198public:
199 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000200 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000201 if (TM->getOptLevel() != CodeGenOpt::None)
202 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000203 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000204
205 AArch64TargetMachine &getAArch64TargetMachine() const {
206 return getTM<AArch64TargetMachine>();
207 }
208
Tim Northoverb4ddc082014-05-30 10:09:59 +0000209 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000210 bool addPreISel() override;
211 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000212#ifdef LLVM_BUILD_GLOBAL_ISEL
213 bool addIRTranslator() override;
214#endif
Tim Northover3b0846e2014-05-24 12:50:23 +0000215 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000216 void addPreRegAlloc() override;
217 void addPostRegAlloc() override;
218 void addPreSched2() override;
219 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000220};
221} // namespace
222
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000223TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000224 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000225 return TargetTransformInfo(AArch64TTIImpl(this, F));
226 });
Tim Northover3b0846e2014-05-24 12:50:23 +0000227}
228
229TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
230 return new AArch64PassConfig(this, PM);
231}
232
Tim Northoverb4ddc082014-05-30 10:09:59 +0000233void AArch64PassConfig::addIRPasses() {
234 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
235 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000236 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000237
238 // Cmpxchg instructions are often used with a subsequent comparison to
239 // determine whether it succeeded. We can exploit existing control-flow in
240 // ldrex/strex loops to simplify this, but it needs tidying up.
241 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
242 addPass(createCFGSimplificationPass());
243
Adam Nemet53e758f2016-03-18 00:27:29 +0000244 // Run LoopDataPrefetch for Cyclone (the only subtarget that defines a
245 // non-zero getPrefetchDistance).
246 //
247 // Run this before LSR to remove the multiplies involved in computing the
248 // pointer values N iterations ahead.
249 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoopDataPrefetch)
250 addPass(createLoopDataPrefetchPass());
251
Tim Northoverb4ddc082014-05-30 10:09:59 +0000252 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000253
Hao Liu7ec8ee32015-06-26 02:32:07 +0000254 // Match interleaved memory accesses to ldN/stN intrinsics.
255 if (TM->getOptLevel() != CodeGenOpt::None)
256 addPass(createInterleavedAccessPass(TM));
257
Hao Liufd46bea2014-11-19 06:39:53 +0000258 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
259 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
260 // and lower a GEP with multiple indices to either arithmetic operations or
261 // multiple GEPs with single index.
262 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
263 // Call EarlyCSE pass to find and remove subexpressions in the lowered
264 // result.
265 addPass(createEarlyCSEPass());
266 // Do loop invariant code motion in case part of the lowered result is
267 // invariant.
268 addPass(createLICMPass());
269 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000270}
271
Tim Northover3b0846e2014-05-24 12:50:23 +0000272// Pass Pipeline Configuration
273bool AArch64PassConfig::addPreISel() {
274 // Run promote constant before global merge, so that the promoted constants
275 // get a chance to be merged
276 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
277 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000278 // FIXME: On AArch64, this depends on the type.
279 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
280 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000281 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000282 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000283 EnableGlobalMerge == cl::BOU_TRUE) {
284 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
285 (EnableGlobalMerge == cl::BOU_UNSET);
286 addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
287 }
288
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000289 if (TM->getOptLevel() != CodeGenOpt::None)
290 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000291
Tim Northover3b0846e2014-05-24 12:50:23 +0000292 return false;
293}
294
295bool AArch64PassConfig::addInstSelector() {
296 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
297
298 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
299 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000300 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000301 getOptLevel() != CodeGenOpt::None)
302 addPass(createAArch64CleanupLocalDynamicTLSPass());
303
304 return false;
305}
306
Quentin Colombetd96f4952016-02-11 19:35:06 +0000307#ifdef LLVM_BUILD_GLOBAL_ISEL
308bool AArch64PassConfig::addIRTranslator() {
309 addPass(new IRTranslator());
310 return false;
311}
312#endif
313
Tim Northover3b0846e2014-05-24 12:50:23 +0000314bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000315 if (EnableCondOpt)
316 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000317 if (EnableCCMP)
318 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000319 if (EnableMCR)
320 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000321 if (EnableEarlyIfConversion)
322 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000323 if (EnableStPairSuppress)
324 addPass(createAArch64StorePairSuppressPass());
325 return true;
326}
327
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000328void AArch64PassConfig::addPreRegAlloc() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000329 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000330 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000331 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000332 // The AdvSIMD pass may produce copies that can be rewritten to
333 // be register coaleascer friendly.
334 addPass(&PeepholeOptimizerID);
335 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000336}
337
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000338void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000339 // Remove redundant copy instructions.
340 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
341 addPass(createAArch64RedundantCopyEliminationPass());
342
Tim Northover3b0846e2014-05-24 12:50:23 +0000343 // Change dead register definitions to refer to the zero register.
344 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
Matthias Braunb2f23882014-12-11 23:18:03 +0000345 addPass(createAArch64DeadRegisterDefinitions());
Eric Christopher6f1e5682015-03-03 23:22:40 +0000346 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000347 // Improve performance for some FP/SIMD code for A57.
348 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000349}
350
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000351void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000352 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000353 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000354 // Use load/store pair instructions when possible.
355 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
356 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000357}
358
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000359void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000360 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000361 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000362 // Relax conditional branch instructions if they're otherwise out of
363 // range of their destination.
Matthias Braunb2f23882014-12-11 23:18:03 +0000364 addPass(createAArch64BranchRelaxation());
Tim Northover3b0846e2014-05-24 12:50:23 +0000365 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000366 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000367 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000368}