blob: e83dd0dd335025d6f5d97d90eb060d8fe511cc24 [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000015#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000016#include "llvm/CodeGen/RegAllocRegistry.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000017#include "llvm/PassManager.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/TargetRegistry.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/Transforms/Scalar.h"
22using namespace llvm;
23
24static cl::opt<bool>
25EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
26 cl::init(true), cl::Hidden);
27
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000028static cl::opt<bool> EnableMCR("aarch64-mcr",
29 cl::desc("Enable the machine combiner pass"),
30 cl::init(true), cl::Hidden);
31
Tim Northover3b0846e2014-05-24 12:50:23 +000032static cl::opt<bool>
33EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
34 cl::init(true), cl::Hidden);
35
36static cl::opt<bool>
37EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
38 " integer instructions"), cl::init(false), cl::Hidden);
39
40static cl::opt<bool>
41EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
42 "constant pass"), cl::init(true), cl::Hidden);
43
44static cl::opt<bool>
45EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
46 " linker optimization hints (LOH)"), cl::init(true),
47 cl::Hidden);
48
49static cl::opt<bool>
50EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
51 cl::desc("Enable the pass that removes dead"
52 " definitons and replaces stores to"
53 " them with stores to the zero"
54 " register"),
55 cl::init(true));
56
57static cl::opt<bool>
58EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
59 " optimization pass"), cl::init(true), cl::Hidden);
60
Tim Northoverb4ddc082014-05-30 10:09:59 +000061static cl::opt<bool>
62EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
63 cl::desc("Run SimplifyCFG after expanding atomic operations"
64 " to make use of cmpxchg flow-based information"),
65 cl::init(true));
66
James Molloy99917942014-08-06 13:31:32 +000067static cl::opt<bool>
68EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
69 cl::desc("Run early if-conversion"),
70 cl::init(true));
71
Jiangning Liu1a486da2014-09-05 02:55:24 +000072static cl::opt<bool>
73EnableCondOpt("aarch64-condopt",
74 cl::desc("Enable the condition optimizer pass"),
75 cl::init(true), cl::Hidden);
76
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000077static cl::opt<bool>
78EnablePBQP("aarch64-pbqp", cl::Hidden,
79 cl::desc("Use PBQP register allocator (experimental)"),
80 cl::init(false));
James Molloy99917942014-08-06 13:31:32 +000081
Tim Northover3b0846e2014-05-24 12:50:23 +000082extern "C" void LLVMInitializeAArch64Target() {
83 // Register the target.
84 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
85 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
Tim Northover35910d72014-07-23 12:58:11 +000086 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
Tim Northover3b0846e2014-05-24 12:50:23 +000087}
88
89/// TargetMachine ctor - Create an AArch64 architecture model.
90///
91AArch64TargetMachine::AArch64TargetMachine(const Target &T, StringRef TT,
92 StringRef CPU, StringRef FS,
93 const TargetOptions &Options,
94 Reloc::Model RM, CodeModel::Model CM,
95 CodeGenOpt::Level OL,
96 bool LittleEndian)
97 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000098 Subtarget(TT, CPU, FS, *this, LittleEndian),
99 usingPBQP(false) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000100 initAsmInfo();
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +0000101
102 if (EnablePBQP && Subtarget.isCortexA57() && OL != CodeGenOpt::None) {
103 usingPBQP = true;
104 RegisterRegAlloc::setDefault(createAArch64A57PBQPRegAlloc);
105 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000106}
107
108void AArch64leTargetMachine::anchor() { }
109
110AArch64leTargetMachine::
111AArch64leTargetMachine(const Target &T, StringRef TT,
112 StringRef CPU, StringRef FS, const TargetOptions &Options,
113 Reloc::Model RM, CodeModel::Model CM,
114 CodeGenOpt::Level OL)
115 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
116
117void AArch64beTargetMachine::anchor() { }
118
119AArch64beTargetMachine::
120AArch64beTargetMachine(const Target &T, StringRef TT,
121 StringRef CPU, StringRef FS, const TargetOptions &Options,
122 Reloc::Model RM, CodeModel::Model CM,
123 CodeGenOpt::Level OL)
124 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
125
126namespace {
127/// AArch64 Code Generator Pass Configuration Options.
128class AArch64PassConfig : public TargetPassConfig {
129public:
130 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000131 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000132 if (TM->getOptLevel() != CodeGenOpt::None)
133 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000134 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000135
136 AArch64TargetMachine &getAArch64TargetMachine() const {
137 return getTM<AArch64TargetMachine>();
138 }
139
Tim Northoverb4ddc082014-05-30 10:09:59 +0000140 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000141 bool addPreISel() override;
142 bool addInstSelector() override;
143 bool addILPOpts() override;
144 bool addPreRegAlloc() override;
145 bool addPostRegAlloc() override;
146 bool addPreSched2() override;
147 bool addPreEmitPass() override;
148};
149} // namespace
150
151void AArch64TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
152 // Add first the target-independent BasicTTI pass, then our AArch64 pass. This
153 // allows the AArch64 pass to delegate to the target independent layer when
154 // appropriate.
155 PM.add(createBasicTargetTransformInfoPass(this));
156 PM.add(createAArch64TargetTransformInfoPass(this));
157}
158
159TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
160 return new AArch64PassConfig(this, PM);
161}
162
Tim Northoverb4ddc082014-05-30 10:09:59 +0000163void AArch64PassConfig::addIRPasses() {
164 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
165 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000166 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000167
168 // Cmpxchg instructions are often used with a subsequent comparison to
169 // determine whether it succeeded. We can exploit existing control-flow in
170 // ldrex/strex loops to simplify this, but it needs tidying up.
171 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
172 addPass(createCFGSimplificationPass());
173
174 TargetPassConfig::addIRPasses();
175}
176
Tim Northover3b0846e2014-05-24 12:50:23 +0000177// Pass Pipeline Configuration
178bool AArch64PassConfig::addPreISel() {
179 // Run promote constant before global merge, so that the promoted constants
180 // get a chance to be merged
181 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
182 addPass(createAArch64PromoteConstantPass());
183 if (TM->getOptLevel() != CodeGenOpt::None)
184 addPass(createGlobalMergePass(TM));
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000185 if (TM->getOptLevel() != CodeGenOpt::None)
186 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000187
Tim Northover3b0846e2014-05-24 12:50:23 +0000188 return false;
189}
190
191bool AArch64PassConfig::addInstSelector() {
192 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
193
194 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
195 // references to _TLS_MODULE_BASE_ as possible.
196 if (TM->getSubtarget<AArch64Subtarget>().isTargetELF() &&
197 getOptLevel() != CodeGenOpt::None)
198 addPass(createAArch64CleanupLocalDynamicTLSPass());
199
200 return false;
201}
202
203bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000204 if (EnableCondOpt)
205 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000206 if (EnableCCMP)
207 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000208 if (EnableMCR)
209 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000210 if (EnableEarlyIfConversion)
211 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000212 if (EnableStPairSuppress)
213 addPass(createAArch64StorePairSuppressPass());
214 return true;
215}
216
217bool AArch64PassConfig::addPreRegAlloc() {
218 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000219 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000220 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000221 // The AdvSIMD pass may produce copies that can be rewritten to
222 // be register coaleascer friendly.
223 addPass(&PeepholeOptimizerID);
224 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000225 return true;
226}
227
228bool AArch64PassConfig::addPostRegAlloc() {
229 // Change dead register definitions to refer to the zero register.
230 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
231 addPass(createAArch64DeadRegisterDefinitions());
James Molloy3feea9c2014-08-08 12:33:21 +0000232 if (TM->getOptLevel() != CodeGenOpt::None &&
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +0000233 TM->getSubtarget<AArch64Subtarget>().isCortexA57() &&
234 !static_cast<const AArch64TargetMachine *>(TM)->isPBQPUsed())
James Molloy3feea9c2014-08-08 12:33:21 +0000235 // Improve performance for some FP/SIMD code for A57.
236 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000237 return true;
238}
239
240bool AArch64PassConfig::addPreSched2() {
241 // Expand some pseudo instructions to allow proper scheduling.
242 addPass(createAArch64ExpandPseudoPass());
243 // Use load/store pair instructions when possible.
244 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
245 addPass(createAArch64LoadStoreOptimizationPass());
246 return true;
247}
248
249bool AArch64PassConfig::addPreEmitPass() {
250 // Relax conditional branch instructions if they're otherwise out of
251 // range of their destination.
252 addPass(createAArch64BranchRelaxation());
253 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
254 TM->getSubtarget<AArch64Subtarget>().isTargetMachO())
255 addPass(createAArch64CollectLOHPass());
256 return true;
257}