blob: d4f19d2abd806eb7650e1c145844f3734769560e [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000016#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000017#include "llvm/CodeGen/RegAllocRegistry.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000018#include "llvm/IR/Function.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000019#include "llvm/PassManager.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/TargetRegistry.h"
22#include "llvm/Target/TargetOptions.h"
23#include "llvm/Transforms/Scalar.h"
24using namespace llvm;
25
26static cl::opt<bool>
27EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
28 cl::init(true), cl::Hidden);
29
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000030static cl::opt<bool> EnableMCR("aarch64-mcr",
31 cl::desc("Enable the machine combiner pass"),
32 cl::init(true), cl::Hidden);
33
Tim Northover3b0846e2014-05-24 12:50:23 +000034static cl::opt<bool>
35EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
36 cl::init(true), cl::Hidden);
37
38static cl::opt<bool>
39EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
40 " integer instructions"), cl::init(false), cl::Hidden);
41
42static cl::opt<bool>
43EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
44 "constant pass"), cl::init(true), cl::Hidden);
45
46static cl::opt<bool>
47EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
48 " linker optimization hints (LOH)"), cl::init(true),
49 cl::Hidden);
50
51static cl::opt<bool>
52EnableDeadRegisterElimination("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"
56 " register"),
57 cl::init(true));
58
59static cl::opt<bool>
60EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
61 " optimization pass"), cl::init(true), cl::Hidden);
62
Tim Northoverb4ddc082014-05-30 10:09:59 +000063static cl::opt<bool>
64EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
65 cl::desc("Run SimplifyCFG after expanding atomic operations"
66 " to make use of cmpxchg flow-based information"),
67 cl::init(true));
68
James Molloy99917942014-08-06 13:31:32 +000069static cl::opt<bool>
70EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
71 cl::desc("Run early if-conversion"),
72 cl::init(true));
73
Jiangning Liu1a486da2014-09-05 02:55:24 +000074static cl::opt<bool>
75EnableCondOpt("aarch64-condopt",
76 cl::desc("Enable the condition optimizer pass"),
77 cl::init(true), cl::Hidden);
78
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000079static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +000080EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
81 cl::desc("Work around Cortex-A53 erratum 835769"),
82 cl::init(false));
83
Hao Liufd46bea2014-11-19 06:39:53 +000084static cl::opt<bool>
85EnableGEPOpt("aarch64-gep-opt", cl::Hidden,
86 cl::desc("Enable optimizations on complex GEPs"),
87 cl::init(true));
88
Tim Northover3b0846e2014-05-24 12:50:23 +000089extern "C" void LLVMInitializeAArch64Target() {
90 // Register the target.
91 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
92 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
Tim Northover35910d72014-07-23 12:58:11 +000093 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
Tim Northover3b0846e2014-05-24 12:50:23 +000094}
95
Aditya Nandakumara2719322014-11-13 09:26:31 +000096//===----------------------------------------------------------------------===//
97// AArch64 Lowering public interface.
98//===----------------------------------------------------------------------===//
99static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
100 if (TT.isOSBinFormatMachO())
101 return make_unique<AArch64_MachoTargetObjectFile>();
102
103 return make_unique<AArch64_ELFTargetObjectFile>();
104}
105
Tim Northover3b0846e2014-05-24 12:50:23 +0000106/// TargetMachine ctor - Create an AArch64 architecture model.
107///
108AArch64TargetMachine::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,
113 bool LittleEndian)
114 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Aditya Nandakumara2719322014-11-13 09:26:31 +0000115 TLOF(createTLOF(Triple(getTargetTriple()))),
Arnaud A. de Grandmaisona61262f2014-10-21 20:47:22 +0000116 Subtarget(TT, CPU, FS, *this, LittleEndian), isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000117 initAsmInfo();
118}
119
Reid Kleckner357600e2014-11-20 23:37:18 +0000120AArch64TargetMachine::~AArch64TargetMachine() {}
121
Eric Christopher3faf2f12014-10-06 06:45:36 +0000122const AArch64Subtarget *
123AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
124 AttributeSet FnAttrs = F.getAttributes();
125 Attribute CPUAttr =
126 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
127 Attribute FSAttr =
128 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
129
130 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
131 ? CPUAttr.getValueAsString().str()
132 : TargetCPU;
133 std::string FS = !FSAttr.hasAttribute(Attribute::None)
134 ? FSAttr.getValueAsString().str()
135 : TargetFS;
136
137 auto &I = SubtargetMap[CPU + FS];
138 if (!I) {
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);
144 }
145 return I.get();
146}
147
Tim Northover3b0846e2014-05-24 12:50:23 +0000148void AArch64leTargetMachine::anchor() { }
149
150AArch64leTargetMachine::
151AArch64leTargetMachine(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) {}
156
157void AArch64beTargetMachine::anchor() { }
158
159AArch64beTargetMachine::
160AArch64beTargetMachine(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) {}
165
166namespace {
167/// AArch64 Code Generator Pass Configuration Options.
168class AArch64PassConfig : public TargetPassConfig {
169public:
170 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000171 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000172 if (TM->getOptLevel() != CodeGenOpt::None)
173 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000174 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000175
176 AArch64TargetMachine &getAArch64TargetMachine() const {
177 return getTM<AArch64TargetMachine>();
178 }
179
Tim Northoverb4ddc082014-05-30 10:09:59 +0000180 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000181 bool addPreISel() override;
182 bool addInstSelector() override;
183 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000184 void addPreRegAlloc() override;
185 void addPostRegAlloc() override;
186 void addPreSched2() override;
187 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000188};
189} // namespace
190
191void 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
194 // appropriate.
195 PM.add(createBasicTargetTransformInfoPass(this));
196 PM.add(createAArch64TargetTransformInfoPass(this));
197}
198
199TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
200 return new AArch64PassConfig(this, PM);
201}
202
Tim Northoverb4ddc082014-05-30 10:09:59 +0000203void AArch64PassConfig::addIRPasses() {
204 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
205 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000206 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000207
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());
213
214 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000215
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
222 // result.
223 addPass(createEarlyCSEPass());
224 // Do loop invariant code motion in case part of the lowered result is
225 // invariant.
226 addPass(createLICMPass());
227 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000228}
229
Tim Northover3b0846e2014-05-24 12:50:23 +0000230// Pass Pipeline Configuration
231bool 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));
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000238 if (TM->getOptLevel() != CodeGenOpt::None)
239 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000240
Tim Northover3b0846e2014-05-24 12:50:23 +0000241 return false;
242}
243
244bool AArch64PassConfig::addInstSelector() {
245 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
246
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());
252
253 return false;
254}
255
256bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000257 if (EnableCondOpt)
258 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000259 if (EnableCCMP)
260 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000261 if (EnableMCR)
262 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000263 if (EnableEarlyIfConversion)
264 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000265 if (EnableStPairSuppress)
266 addPass(createAArch64StorePairSuppressPass());
267 return true;
268}
269
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000270void AArch64PassConfig::addPreRegAlloc() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000271 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000272 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000273 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000274 // The AdvSIMD pass may produce copies that can be rewritten to
275 // be register coaleascer friendly.
276 addPass(&PeepholeOptimizerID);
277 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000278}
279
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000280void AArch64PassConfig::addPostRegAlloc() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000281 // Change dead register definitions to refer to the zero register.
282 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
Matthias Braunb2f23882014-12-11 23:18:03 +0000283 addPass(createAArch64DeadRegisterDefinitions());
James Molloy3feea9c2014-08-08 12:33:21 +0000284 if (TM->getOptLevel() != CodeGenOpt::None &&
Tim Northover00917892014-10-28 01:24:32 +0000285 (TM->getSubtarget<AArch64Subtarget>().isCortexA53() ||
286 TM->getSubtarget<AArch64Subtarget>().isCortexA57()) &&
Arnaud A. de Grandmaisona61262f2014-10-21 20:47:22 +0000287 usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000288 // Improve performance for some FP/SIMD code for A57.
289 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000290}
291
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000292void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000293 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000294 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000295 // Use load/store pair instructions when possible.
296 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
297 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000298}
299
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000300void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000301 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000302 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000303 // Relax conditional branch instructions if they're otherwise out of
304 // range of their destination.
Matthias Braunb2f23882014-12-11 23:18:03 +0000305 addPass(createAArch64BranchRelaxation());
Tim Northover3b0846e2014-05-24 12:50:23 +0000306 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
307 TM->getSubtarget<AArch64Subtarget>().isTargetMachO())
308 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000309}