blob: ab28a16515d83fa5dff0654135ed407201336a66 [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000018#include "llvm/CodeGen/RegAllocRegistry.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000019#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/Transforms/Scalar.h"
25using namespace llvm;
26
27static cl::opt<bool>
28EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
29 cl::init(true), cl::Hidden);
30
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000031static cl::opt<bool> EnableMCR("aarch64-mcr",
32 cl::desc("Enable the machine combiner pass"),
33 cl::init(true), cl::Hidden);
34
Tim Northover3b0846e2014-05-24 12:50:23 +000035static cl::opt<bool>
36EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
37 cl::init(true), cl::Hidden);
38
39static cl::opt<bool>
40EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
41 " integer instructions"), cl::init(false), cl::Hidden);
42
43static cl::opt<bool>
44EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
45 "constant pass"), cl::init(true), cl::Hidden);
46
47static cl::opt<bool>
48EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
49 " linker optimization hints (LOH)"), cl::init(true),
50 cl::Hidden);
51
52static cl::opt<bool>
53EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
54 cl::desc("Enable the pass that removes dead"
55 " definitons and replaces stores to"
56 " them with stores to the zero"
57 " register"),
58 cl::init(true));
59
60static cl::opt<bool>
61EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
62 " optimization pass"), cl::init(true), cl::Hidden);
63
Tim Northoverb4ddc082014-05-30 10:09:59 +000064static cl::opt<bool>
65EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
66 cl::desc("Run SimplifyCFG after expanding atomic operations"
67 " to make use of cmpxchg flow-based information"),
68 cl::init(true));
69
James Molloy99917942014-08-06 13:31:32 +000070static cl::opt<bool>
71EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
72 cl::desc("Run early if-conversion"),
73 cl::init(true));
74
Jiangning Liu1a486da2014-09-05 02:55:24 +000075static cl::opt<bool>
76EnableCondOpt("aarch64-condopt",
77 cl::desc("Enable the condition optimizer pass"),
78 cl::init(true), cl::Hidden);
79
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000080static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +000081EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
82 cl::desc("Work around Cortex-A53 erratum 835769"),
83 cl::init(false));
84
Hao Liufd46bea2014-11-19 06:39:53 +000085static cl::opt<bool>
86EnableGEPOpt("aarch64-gep-opt", cl::Hidden,
87 cl::desc("Enable optimizations on complex GEPs"),
88 cl::init(true));
89
Ahmed Bougachab96444e2015-04-11 00:06:36 +000090// FIXME: Unify control over GlobalMerge.
91static cl::opt<cl::boolOrDefault>
92EnableGlobalMerge("aarch64-global-merge", cl::Hidden,
93 cl::desc("Enable the global merge pass"));
94
Tim Northover3b0846e2014-05-24 12:50:23 +000095extern "C" void LLVMInitializeAArch64Target() {
96 // Register the target.
97 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
98 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
Tim Northover35910d72014-07-23 12:58:11 +000099 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
Tim Northover3b0846e2014-05-24 12:50:23 +0000100}
101
Aditya Nandakumara2719322014-11-13 09:26:31 +0000102//===----------------------------------------------------------------------===//
103// AArch64 Lowering public interface.
104//===----------------------------------------------------------------------===//
105static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
106 if (TT.isOSBinFormatMachO())
107 return make_unique<AArch64_MachoTargetObjectFile>();
108
109 return make_unique<AArch64_ELFTargetObjectFile>();
110}
111
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000112// Helper function to build a DataLayout string
113static std::string computeDataLayout(StringRef TT, bool LittleEndian) {
114 Triple Triple(TT);
115 if (Triple.isOSBinFormatMachO())
116 return "e-m:o-i64:64-i128:128-n32:64-S128";
117 if (LittleEndian)
118 return "e-m:e-i64:64-i128:128-n32:64-S128";
119 return "E-m:e-i64:64-i128:128-n32:64-S128";
120}
121
Tim Northover3b0846e2014-05-24 12:50:23 +0000122/// TargetMachine ctor - Create an AArch64 architecture model.
123///
124AArch64TargetMachine::AArch64TargetMachine(const Target &T, StringRef TT,
125 StringRef CPU, StringRef FS,
126 const TargetOptions &Options,
127 Reloc::Model RM, CodeModel::Model CM,
128 CodeGenOpt::Level OL,
129 bool LittleEndian)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000130 // This nested ternary is horrible, but DL needs to be properly
Eric Christopher63ea0402015-03-12 18:23:01 +0000131 // initialized before TLInfo is constructed.
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000132 : LLVMTargetMachine(T, computeDataLayout(TT, LittleEndian), TT, CPU, FS,
133 Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000134 TLOF(createTLOF(Triple(getTargetTriple()))),
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000135 isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000136 initAsmInfo();
137}
138
Reid Kleckner357600e2014-11-20 23:37:18 +0000139AArch64TargetMachine::~AArch64TargetMachine() {}
140
Eric Christopher3faf2f12014-10-06 06:45:36 +0000141const AArch64Subtarget *
142AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000143 Attribute CPUAttr = F.getFnAttribute("target-cpu");
144 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000145
146 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
147 ? CPUAttr.getValueAsString().str()
148 : TargetCPU;
149 std::string FS = !FSAttr.hasAttribute(Attribute::None)
150 ? FSAttr.getValueAsString().str()
151 : TargetFS;
152
153 auto &I = SubtargetMap[CPU + FS];
154 if (!I) {
155 // This needs to be done before we create a new subtarget since any
156 // creation will depend on the TM and the code generation flags on the
157 // function that reside in TargetOptions.
158 resetTargetOptions(F);
159 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this, isLittle);
160 }
161 return I.get();
162}
163
Tim Northover3b0846e2014-05-24 12:50:23 +0000164void AArch64leTargetMachine::anchor() { }
165
166AArch64leTargetMachine::
167AArch64leTargetMachine(const Target &T, StringRef TT,
168 StringRef CPU, StringRef FS, const TargetOptions &Options,
169 Reloc::Model RM, CodeModel::Model CM,
170 CodeGenOpt::Level OL)
171 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
172
173void AArch64beTargetMachine::anchor() { }
174
175AArch64beTargetMachine::
176AArch64beTargetMachine(const Target &T, StringRef TT,
177 StringRef CPU, StringRef FS, const TargetOptions &Options,
178 Reloc::Model RM, CodeModel::Model CM,
179 CodeGenOpt::Level OL)
180 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
181
182namespace {
183/// AArch64 Code Generator Pass Configuration Options.
184class AArch64PassConfig : public TargetPassConfig {
185public:
186 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000187 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000188 if (TM->getOptLevel() != CodeGenOpt::None)
189 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000190 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000191
192 AArch64TargetMachine &getAArch64TargetMachine() const {
193 return getTM<AArch64TargetMachine>();
194 }
195
Tim Northoverb4ddc082014-05-30 10:09:59 +0000196 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000197 bool addPreISel() override;
198 bool addInstSelector() override;
199 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000200 void addPreRegAlloc() override;
201 void addPostRegAlloc() override;
202 void addPreSched2() override;
203 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000204};
205} // namespace
206
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000207TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
208 return TargetIRAnalysis([this](Function &F) {
209 return TargetTransformInfo(AArch64TTIImpl(this, F));
210 });
Tim Northover3b0846e2014-05-24 12:50:23 +0000211}
212
213TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
214 return new AArch64PassConfig(this, PM);
215}
216
Tim Northoverb4ddc082014-05-30 10:09:59 +0000217void AArch64PassConfig::addIRPasses() {
218 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
219 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000220 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000221
222 // Cmpxchg instructions are often used with a subsequent comparison to
223 // determine whether it succeeded. We can exploit existing control-flow in
224 // ldrex/strex loops to simplify this, but it needs tidying up.
225 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
226 addPass(createCFGSimplificationPass());
227
228 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000229
230 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
231 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
232 // and lower a GEP with multiple indices to either arithmetic operations or
233 // multiple GEPs with single index.
234 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
235 // Call EarlyCSE pass to find and remove subexpressions in the lowered
236 // result.
237 addPass(createEarlyCSEPass());
238 // Do loop invariant code motion in case part of the lowered result is
239 // invariant.
240 addPass(createLICMPass());
241 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000242}
243
Tim Northover3b0846e2014-05-24 12:50:23 +0000244// Pass Pipeline Configuration
245bool AArch64PassConfig::addPreISel() {
246 // Run promote constant before global merge, so that the promoted constants
247 // get a chance to be merged
248 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
249 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000250 // FIXME: On AArch64, this depends on the type.
251 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
252 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000253 if ((TM->getOptLevel() == CodeGenOpt::Aggressive &&
254 EnableGlobalMerge == cl::BOU_UNSET) ||
255 EnableGlobalMerge == cl::BOU_TRUE)
Eric Christophered47b222015-02-23 19:28:45 +0000256 addPass(createGlobalMergePass(TM, 4095));
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000257 if (TM->getOptLevel() != CodeGenOpt::None)
258 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000259
Tim Northover3b0846e2014-05-24 12:50:23 +0000260 return false;
261}
262
263bool AArch64PassConfig::addInstSelector() {
264 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
265
266 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
267 // references to _TLS_MODULE_BASE_ as possible.
Eric Christopher988ce752015-01-30 01:10:26 +0000268 if (Triple(TM->getTargetTriple()).isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000269 getOptLevel() != CodeGenOpt::None)
270 addPass(createAArch64CleanupLocalDynamicTLSPass());
271
272 return false;
273}
274
275bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000276 if (EnableCondOpt)
277 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000278 if (EnableCCMP)
279 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000280 if (EnableMCR)
281 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000282 if (EnableEarlyIfConversion)
283 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000284 if (EnableStPairSuppress)
285 addPass(createAArch64StorePairSuppressPass());
286 return true;
287}
288
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000289void AArch64PassConfig::addPreRegAlloc() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000290 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000291 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000292 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000293 // The AdvSIMD pass may produce copies that can be rewritten to
294 // be register coaleascer friendly.
295 addPass(&PeepholeOptimizerID);
296 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000297}
298
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000299void AArch64PassConfig::addPostRegAlloc() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000300 // Change dead register definitions to refer to the zero register.
301 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
Matthias Braunb2f23882014-12-11 23:18:03 +0000302 addPass(createAArch64DeadRegisterDefinitions());
Eric Christopher6f1e5682015-03-03 23:22:40 +0000303 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000304 // Improve performance for some FP/SIMD code for A57.
305 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000306}
307
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000308void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000309 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000310 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000311 // Use load/store pair instructions when possible.
312 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
313 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000314}
315
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000316void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000317 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000318 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000319 // Relax conditional branch instructions if they're otherwise out of
320 // range of their destination.
Matthias Braunb2f23882014-12-11 23:18:03 +0000321 addPass(createAArch64BranchRelaxation());
Tim Northover3b0846e2014-05-24 12:50:23 +0000322 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Eric Christopher988ce752015-01-30 01:10:26 +0000323 Triple(TM->getTargetTriple()).isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000324 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000325}