fbe0b9bc4e269f5b6f1cd80959e320bcb865422d
[oota-llvm.git] / lib / Target / AArch64 / AArch64TargetMachine.cpp
1 //===-- 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"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/CodeGen/RegAllocRegistry.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Transforms/Scalar.h"
23 using namespace llvm;
24
25 static cl::opt<bool>
26 EnableCCMP("aarch64-ccmp", cl::desc("Enable the CCMP formation pass"),
27            cl::init(true), cl::Hidden);
28
29 static cl::opt<bool> EnableMCR("aarch64-mcr",
30                                cl::desc("Enable the machine combiner pass"),
31                                cl::init(true), cl::Hidden);
32
33 static cl::opt<bool>
34 EnableStPairSuppress("aarch64-stp-suppress", cl::desc("Suppress STP for AArch64"),
35                      cl::init(true), cl::Hidden);
36
37 static cl::opt<bool>
38 EnableAdvSIMDScalar("aarch64-simd-scalar", cl::desc("Enable use of AdvSIMD scalar"
39                     " integer instructions"), cl::init(false), cl::Hidden);
40
41 static cl::opt<bool>
42 EnablePromoteConstant("aarch64-promote-const", cl::desc("Enable the promote "
43                       "constant pass"), cl::init(true), cl::Hidden);
44
45 static cl::opt<bool>
46 EnableCollectLOH("aarch64-collect-loh", cl::desc("Enable the pass that emits the"
47                  " linker optimization hints (LOH)"), cl::init(true),
48                  cl::Hidden);
49
50 static cl::opt<bool>
51 EnableDeadRegisterElimination("aarch64-dead-def-elimination", cl::Hidden,
52                               cl::desc("Enable the pass that removes dead"
53                                        " definitons and replaces stores to"
54                                        " them with stores to the zero"
55                                        " register"),
56                               cl::init(true));
57
58 static cl::opt<bool>
59 EnableLoadStoreOpt("aarch64-load-store-opt", cl::desc("Enable the load/store pair"
60                    " optimization pass"), cl::init(true), cl::Hidden);
61
62 static cl::opt<bool>
63 EnableAtomicTidy("aarch64-atomic-cfg-tidy", cl::Hidden,
64                  cl::desc("Run SimplifyCFG after expanding atomic operations"
65                           " to make use of cmpxchg flow-based information"),
66                  cl::init(true));
67
68 static cl::opt<bool>
69 EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
70                         cl::desc("Run early if-conversion"),
71                         cl::init(true));
72
73 static cl::opt<bool>
74 EnableCondOpt("aarch64-condopt",
75               cl::desc("Enable the condition optimizer pass"),
76               cl::init(true), cl::Hidden);
77
78 static cl::opt<bool>
79 EnablePBQP("aarch64-pbqp", cl::Hidden,
80            cl::desc("Use PBQP register allocator (experimental)"),
81            cl::init(false));
82
83 static cl::opt<bool>
84 EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
85                 cl::desc("Work around Cortex-A53 erratum 835769"),
86                 cl::init(false));
87
88 extern "C" void LLVMInitializeAArch64Target() {
89   // Register the target.
90   RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
91   RegisterTargetMachine<AArch64beTargetMachine> Y(TheAArch64beTarget);
92   RegisterTargetMachine<AArch64leTargetMachine> Z(TheARM64Target);
93 }
94
95 /// TargetMachine ctor - Create an AArch64 architecture model.
96 ///
97 AArch64TargetMachine::AArch64TargetMachine(const Target &T, StringRef TT,
98                                            StringRef CPU, StringRef FS,
99                                            const TargetOptions &Options,
100                                            Reloc::Model RM, CodeModel::Model CM,
101                                            CodeGenOpt::Level OL,
102                                            bool LittleEndian)
103     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
104       Subtarget(TT, CPU, FS, *this, LittleEndian), isLittle(LittleEndian),
105       usingPBQP(false) {
106   initAsmInfo();
107
108   if (EnablePBQP && Subtarget.isCortexA57() && OL != CodeGenOpt::None) {
109     usingPBQP = true;
110     RegisterRegAlloc::setDefault(createDefaultPBQPRegisterAllocator);
111   }
112 }
113
114 const AArch64Subtarget *
115 AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
116   AttributeSet FnAttrs = F.getAttributes();
117   Attribute CPUAttr =
118       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
119   Attribute FSAttr =
120       FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
121
122   std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
123                         ? CPUAttr.getValueAsString().str()
124                         : TargetCPU;
125   std::string FS = !FSAttr.hasAttribute(Attribute::None)
126                        ? FSAttr.getValueAsString().str()
127                        : TargetFS;
128
129   auto &I = SubtargetMap[CPU + FS];
130   if (!I) {
131     // This needs to be done before we create a new subtarget since any
132     // creation will depend on the TM and the code generation flags on the
133     // function that reside in TargetOptions.
134     resetTargetOptions(F);
135     I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this, isLittle);
136   }
137   return I.get();
138 }
139
140 void AArch64leTargetMachine::anchor() { }
141
142 AArch64leTargetMachine::
143 AArch64leTargetMachine(const Target &T, StringRef TT,
144                        StringRef CPU, StringRef FS, const TargetOptions &Options,
145                        Reloc::Model RM, CodeModel::Model CM,
146                        CodeGenOpt::Level OL)
147   : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
148
149 void AArch64beTargetMachine::anchor() { }
150
151 AArch64beTargetMachine::
152 AArch64beTargetMachine(const Target &T, StringRef TT,
153                        StringRef CPU, StringRef FS, const TargetOptions &Options,
154                        Reloc::Model RM, CodeModel::Model CM,
155                        CodeGenOpt::Level OL)
156   : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
157
158 namespace {
159 /// AArch64 Code Generator Pass Configuration Options.
160 class AArch64PassConfig : public TargetPassConfig {
161 public:
162   AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
163       : TargetPassConfig(TM, PM) {
164     if (TM->getOptLevel() != CodeGenOpt::None)
165       substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
166   }
167
168   AArch64TargetMachine &getAArch64TargetMachine() const {
169     return getTM<AArch64TargetMachine>();
170   }
171
172   void addIRPasses()  override;
173   bool addPreISel() override;
174   bool addInstSelector() override;
175   bool addILPOpts() override;
176   bool addPreRegAlloc() override;
177   bool addPostRegAlloc() override;
178   bool addPreSched2() override;
179   bool addPreEmitPass() override;
180 };
181 } // namespace
182
183 void AArch64TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
184   // Add first the target-independent BasicTTI pass, then our AArch64 pass. This
185   // allows the AArch64 pass to delegate to the target independent layer when
186   // appropriate.
187   PM.add(createBasicTargetTransformInfoPass(this));
188   PM.add(createAArch64TargetTransformInfoPass(this));
189 }
190
191 TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
192   return new AArch64PassConfig(this, PM);
193 }
194
195 void AArch64PassConfig::addIRPasses() {
196   // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
197   // ourselves.
198   addPass(createAtomicExpandPass(TM));
199
200   // Cmpxchg instructions are often used with a subsequent comparison to
201   // determine whether it succeeded. We can exploit existing control-flow in
202   // ldrex/strex loops to simplify this, but it needs tidying up.
203   if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
204     addPass(createCFGSimplificationPass());
205
206   TargetPassConfig::addIRPasses();
207 }
208
209 // Pass Pipeline Configuration
210 bool AArch64PassConfig::addPreISel() {
211   // Run promote constant before global merge, so that the promoted constants
212   // get a chance to be merged
213   if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
214     addPass(createAArch64PromoteConstantPass());
215   if (TM->getOptLevel() != CodeGenOpt::None)
216     addPass(createGlobalMergePass(TM));
217   if (TM->getOptLevel() != CodeGenOpt::None)
218     addPass(createAArch64AddressTypePromotionPass());
219
220   return false;
221 }
222
223 bool AArch64PassConfig::addInstSelector() {
224   addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
225
226   // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
227   // references to _TLS_MODULE_BASE_ as possible.
228   if (TM->getSubtarget<AArch64Subtarget>().isTargetELF() &&
229       getOptLevel() != CodeGenOpt::None)
230     addPass(createAArch64CleanupLocalDynamicTLSPass());
231
232   return false;
233 }
234
235 bool AArch64PassConfig::addILPOpts() {
236   if (EnableCondOpt)
237     addPass(createAArch64ConditionOptimizerPass());
238   if (EnableCCMP)
239     addPass(createAArch64ConditionalCompares());
240   if (EnableMCR)
241     addPass(&MachineCombinerID);
242   if (EnableEarlyIfConversion)
243     addPass(&EarlyIfConverterID);
244   if (EnableStPairSuppress)
245     addPass(createAArch64StorePairSuppressPass());
246   return true;
247 }
248
249 bool AArch64PassConfig::addPreRegAlloc() {
250   // Use AdvSIMD scalar instructions whenever profitable.
251   if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
252     addPass(createAArch64AdvSIMDScalar());
253     // The AdvSIMD pass may produce copies that can be rewritten to
254     // be register coaleascer friendly.
255     addPass(&PeepholeOptimizerID);
256   }
257   return true;
258 }
259
260 bool AArch64PassConfig::addPostRegAlloc() {
261   // Change dead register definitions to refer to the zero register.
262   if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
263     addPass(createAArch64DeadRegisterDefinitions());
264   if (TM->getOptLevel() != CodeGenOpt::None &&
265       TM->getSubtarget<AArch64Subtarget>().isCortexA57() &&
266       !static_cast<const AArch64TargetMachine *>(TM)->isPBQPUsed())
267     // Improve performance for some FP/SIMD code for A57.
268     addPass(createAArch64A57FPLoadBalancing());
269   return true;
270 }
271
272 bool AArch64PassConfig::addPreSched2() {
273   // Expand some pseudo instructions to allow proper scheduling.
274   addPass(createAArch64ExpandPseudoPass());
275   // Use load/store pair instructions when possible.
276   if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
277     addPass(createAArch64LoadStoreOptimizationPass());
278   return true;
279 }
280
281 bool AArch64PassConfig::addPreEmitPass() {
282   if (EnableA53Fix835769)
283     addPass(createAArch64A53Fix835769());
284   // Relax conditional branch instructions if they're otherwise out of
285   // range of their destination.
286   addPass(createAArch64BranchRelaxation());
287   if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
288       TM->getSubtarget<AArch64Subtarget>().isTargetMachO())
289     addPass(createAArch64CollectLOHPass());
290   return true;
291 }