blob: e04fe1b5a97ae2076cd50629841b03280cc7b509 [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"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000016#include "llvm/PassManager.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/TargetRegistry.h"
19#include "llvm/Target/TargetOptions.h"
20#include "llvm/Transforms/Scalar.h"
21using namespace llvm;
22
23static cl::opt<bool>
24EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
25 cl::init(true), cl::Hidden);
26
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000027static cl::opt<bool> EnableMCR("aarch64-mcr",
28 cl::desc("Enable the machine combiner pass"),
29 cl::init(true), cl::Hidden);
30
Tim Northover3b0846e2014-05-24 12:50:23 +000031static cl::opt<bool>
32EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
33 cl::init(true), cl::Hidden);
34
35static cl::opt<bool>
36EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
37 " integer instructions"), cl::init(false), cl::Hidden);
38
39static cl::opt<bool>
40EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
41 "constant pass"), cl::init(true), cl::Hidden);
42
43static cl::opt<bool>
44EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
45 " linker optimization hints (LOH)"), cl::init(true),
46 cl::Hidden);
47
48static cl::opt<bool>
49EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
50 cl::desc("Enable the pass that removes dead"
51 " definitons and replaces stores to"
52 " them with stores to the zero"
53 " register"),
54 cl::init(true));
55
56static cl::opt<bool>
57EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
58 " optimization pass"), cl::init(true), cl::Hidden);
59
Tim Northoverb4ddc082014-05-30 10:09:59 +000060static cl::opt<bool>
61EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
62 cl::desc("Run SimplifyCFG after expanding atomic operations"
63 " to make use of cmpxchg flow-based information"),
64 cl::init(true));
65
James Molloy99917942014-08-06 13:31:32 +000066static cl::opt<bool>
67EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
68 cl::desc("Run early if-conversion"),
69 cl::init(true));
70
71
Tim Northover3b0846e2014-05-24 12:50:23 +000072extern "C" void LLVMInitializeAArch64Target() {
73 // Register the target.
74 RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
75 RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
Tim Northover35910d72014-07-23 12:58:11 +000076 RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
Tim Northover3b0846e2014-05-24 12:50:23 +000077}
78
79/// TargetMachine ctor - Create an AArch64 architecture model.
80///
81AArch64TargetMachine::AArch64TargetMachine(const Target &T, StringRef TT,
82 StringRef CPU, StringRef FS,
83 const TargetOptions &Options,
84 Reloc::Model RM, CodeModel::Model CM,
85 CodeGenOpt::Level OL,
86 bool LittleEndian)
87 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Eric Christopher841da852014-06-10 23:26:45 +000088 Subtarget(TT, CPU, FS, *this, LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +000089 initAsmInfo();
90}
91
92void AArch64leTargetMachine::anchor() { }
93
94AArch64leTargetMachine::
95AArch64leTargetMachine(const Target &T, StringRef TT,
96 StringRef CPU, StringRef FS, const TargetOptions &Options,
97 Reloc::Model RM, CodeModel::Model CM,
98 CodeGenOpt::Level OL)
99 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
100
101void AArch64beTargetMachine::anchor() { }
102
103AArch64beTargetMachine::
104AArch64beTargetMachine(const Target &T, StringRef TT,
105 StringRef CPU, StringRef FS, const TargetOptions &Options,
106 Reloc::Model RM, CodeModel::Model CM,
107 CodeGenOpt::Level OL)
108 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
109
110namespace {
111/// AArch64 Code Generator Pass Configuration Options.
112class AArch64PassConfig : public TargetPassConfig {
113public:
114 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
115 : TargetPassConfig(TM, PM) {}
116
117 AArch64TargetMachine &getAArch64TargetMachine() const {
118 return getTM<AArch64TargetMachine>();
119 }
120
Tim Northoverb4ddc082014-05-30 10:09:59 +0000121 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000122 bool addPreISel() override;
123 bool addInstSelector() override;
124 bool addILPOpts() override;
125 bool addPreRegAlloc() override;
126 bool addPostRegAlloc() override;
127 bool addPreSched2() override;
128 bool addPreEmitPass() override;
129};
130} // namespace
131
132void AArch64TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
133 // Add first the target-independent BasicTTI pass, then our AArch64 pass. This
134 // allows the AArch64 pass to delegate to the target independent layer when
135 // appropriate.
136 PM.add(createBasicTargetTransformInfoPass(this));
137 PM.add(createAArch64TargetTransformInfoPass(this));
138}
139
140TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
141 return new AArch64PassConfig(this, PM);
142}
143
Tim Northoverb4ddc082014-05-30 10:09:59 +0000144void AArch64PassConfig::addIRPasses() {
145 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
146 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000147 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000148
149 // Cmpxchg instructions are often used with a subsequent comparison to
150 // determine whether it succeeded. We can exploit existing control-flow in
151 // ldrex/strex loops to simplify this, but it needs tidying up.
152 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
153 addPass(createCFGSimplificationPass());
154
155 TargetPassConfig::addIRPasses();
156}
157
Tim Northover3b0846e2014-05-24 12:50:23 +0000158// Pass Pipeline Configuration
159bool AArch64PassConfig::addPreISel() {
160 // Run promote constant before global merge, so that the promoted constants
161 // get a chance to be merged
162 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
163 addPass(createAArch64PromoteConstantPass());
164 if (TM->getOptLevel() != CodeGenOpt::None)
165 addPass(createGlobalMergePass(TM));
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000166 if (TM->getOptLevel() != CodeGenOpt::None)
167 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000168
Tim Northover3b0846e2014-05-24 12:50:23 +0000169 return false;
170}
171
172bool AArch64PassConfig::addInstSelector() {
173 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
174
175 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
176 // references to _TLS_MODULE_BASE_ as possible.
177 if (TM->getSubtarget<AArch64Subtarget>().isTargetELF() &&
178 getOptLevel() != CodeGenOpt::None)
179 addPass(createAArch64CleanupLocalDynamicTLSPass());
180
181 return false;
182}
183
184bool AArch64PassConfig::addILPOpts() {
185 if (EnableCCMP)
186 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000187 if (EnableMCR)
188 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000189 if (EnableEarlyIfConversion)
190 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000191 if (EnableStPairSuppress)
192 addPass(createAArch64StorePairSuppressPass());
193 return true;
194}
195
196bool AArch64PassConfig::addPreRegAlloc() {
197 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000198 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000199 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000200 // The AdvSIMD pass may produce copies that can be rewritten to
201 // be register coaleascer friendly.
202 addPass(&PeepholeOptimizerID);
203 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000204 return true;
205}
206
207bool AArch64PassConfig::addPostRegAlloc() {
208 // Change dead register definitions to refer to the zero register.
209 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
210 addPass(createAArch64DeadRegisterDefinitions());
James Molloy3feea9c2014-08-08 12:33:21 +0000211 if (TM->getOptLevel() != CodeGenOpt::None &&
212 TM->getSubtarget<AArch64Subtarget>().isCortexA57())
213 // Improve performance for some FP/SIMD code for A57.
214 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000215 return true;
216}
217
218bool AArch64PassConfig::addPreSched2() {
219 // Expand some pseudo instructions to allow proper scheduling.
220 addPass(createAArch64ExpandPseudoPass());
221 // Use load/store pair instructions when possible.
222 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
223 addPass(createAArch64LoadStoreOptimizationPass());
224 return true;
225}
226
227bool AArch64PassConfig::addPreEmitPass() {
228 // Relax conditional branch instructions if they're otherwise out of
229 // range of their destination.
230 addPass(createAArch64BranchRelaxation());
231 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
232 TM->getSubtarget<AArch64Subtarget>().isTargetMachO())
233 addPass(createAArch64CollectLOHPass());
234 return true;
235}