blob: 120d71381c675ef5141b6f6c934eb072c6a123ff [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
Quentin Colombet7a43edd2017-05-27 01:34:07 +000013#include "AArch64TargetMachine.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000014#include "AArch64.h"
Evandro Menezes94edf022017-02-01 02:54:34 +000015#include "AArch64MacroFusion.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000016#include "AArch64Subtarget.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000017#include "AArch64TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000018#include "AArch64TargetTransformInfo.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000019#include "MCTargetDesc/AArch64MCTargetDesc.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Triple.h"
22#include "llvm/Analysis/TargetTransformInfo.h"
Quentin Colombet846219a2016-04-07 21:24:40 +000023#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000024#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Tim Northover69fa84a2016-10-14 22:18:18 +000025#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Quentin Colombet7a43edd2017-05-27 01:34:07 +000026#include "llvm/CodeGen/GlobalISel/Localizer.h"
Quentin Colombetd4131812016-04-07 20:27:33 +000027#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000028#include "llvm/CodeGen/MachineScheduler.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000029#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000030#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000031#include "llvm/IR/Attributes.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000032#include "llvm/IR/Function.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000033#include "llvm/MC/MCTargetOptions.h"
34#include "llvm/Pass.h"
35#include "llvm/Support/CodeGen.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000039#include "llvm/Target/TargetOptions.h"
40#include "llvm/Transforms/Scalar.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000041#include <memory>
42#include <string>
43
Tim Northover3b0846e2014-05-24 12:50:23 +000044using namespace llvm;
45
Diana Picus850043b2016-08-01 05:56:57 +000046static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
47 cl::desc("Enable the CCMP formation pass"),
48 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000049
Chad Rosier6db9ff62017-06-23 19:20:12 +000050static cl::opt<bool>
51 EnableCondBrTuning("aarch64-enable-cond-br-tune",
52 cl::desc("Enable the conditional branch tuning pass"),
53 cl::init(true), cl::Hidden);
54
Diana Picus850043b2016-08-01 05:56:57 +000055static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000056 cl::desc("Enable the machine combiner pass"),
57 cl::init(true), cl::Hidden);
58
Diana Picus850043b2016-08-01 05:56:57 +000059static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
60 cl::desc("Suppress STP for AArch64"),
61 cl::init(true), cl::Hidden);
62
63static cl::opt<bool> EnableAdvSIMDScalar(
64 "aarch64-enable-simd-scalar",
65 cl::desc("Enable use of AdvSIMD scalar integer instructions"),
66 cl::init(false), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000067
68static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000069 EnablePromoteConstant("aarch64-enable-promote-const",
70 cl::desc("Enable the promote constant pass"),
71 cl::init(true), cl::Hidden);
72
73static cl::opt<bool> EnableCollectLOH(
74 "aarch64-enable-collect-loh",
75 cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
76 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000077
78static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000079 EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
80 cl::desc("Enable the pass that removes dead"
81 " definitons and replaces stores to"
82 " them with stores to the zero"
83 " register"),
84 cl::init(true));
Tim Northover3b0846e2014-05-24 12:50:23 +000085
Diana Picus850043b2016-08-01 05:56:57 +000086static cl::opt<bool> EnableRedundantCopyElimination(
87 "aarch64-enable-copyelim",
88 cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
89 cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000090
Diana Picus850043b2016-08-01 05:56:57 +000091static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
92 cl::desc("Enable the load/store pair"
93 " optimization pass"),
94 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000095
Diana Picus850043b2016-08-01 05:56:57 +000096static cl::opt<bool> EnableAtomicTidy(
97 "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
98 cl::desc("Run SimplifyCFG after expanding atomic operations"
99 " to make use of cmpxchg flow-based information"),
100 cl::init(true));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000101
James Molloy99917942014-08-06 13:31:32 +0000102static cl::opt<bool>
103EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
104 cl::desc("Run early if-conversion"),
105 cl::init(true));
106
Jiangning Liu1a486da2014-09-05 02:55:24 +0000107static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000108 EnableCondOpt("aarch64-enable-condopt",
109 cl::desc("Enable the condition optimizer pass"),
110 cl::init(true), cl::Hidden);
Jiangning Liu1a486da2014-09-05 02:55:24 +0000111
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +0000112static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +0000113EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
114 cl::desc("Work around Cortex-A53 erratum 835769"),
115 cl::init(false));
116
Hao Liufd46bea2014-11-19 06:39:53 +0000117static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000118 EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
119 cl::desc("Enable optimizations on complex GEPs"),
120 cl::init(false));
121
122static cl::opt<bool>
123 BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
124 cl::desc("Relax out of range conditional branches"));
Hao Liufd46bea2014-11-19 06:39:53 +0000125
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000126// FIXME: Unify control over GlobalMerge.
127static cl::opt<cl::boolOrDefault>
Diana Picus850043b2016-08-01 05:56:57 +0000128 EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
129 cl::desc("Enable the global merge pass"));
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000130
Adam Nemet53e758f2016-03-18 00:27:29 +0000131static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000132 EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
Adam Nemet53e758f2016-03-18 00:27:29 +0000133 cl::desc("Enable the loop data prefetch pass"),
Adam Nemetfb8fbba52016-03-30 00:21:29 +0000134 cl::init(true));
Adam Nemet53e758f2016-03-18 00:27:29 +0000135
Ahmed Bougacha120ae222017-03-01 23:33:08 +0000136static cl::opt<int> EnableGlobalISelAtO(
137 "aarch64-enable-global-isel-at-O", cl::Hidden,
138 cl::desc("Enable GlobalISel at or below an opt level (-1 to disable)"),
Amara Emerson854d10d2018-01-02 16:30:47 +0000139 cl::init(0));
Ahmed Bougacha120ae222017-03-01 23:33:08 +0000140
Geoff Berryb1e87142017-07-14 21:44:12 +0000141static cl::opt<bool> EnableFalkorHWPFFix("aarch64-enable-falkor-hwpf-fix",
142 cl::init(true), cl::Hidden);
143
Tim Northover3b0846e2014-05-24 12:50:23 +0000144extern "C" void LLVMInitializeAArch64Target() {
145 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +0000146 RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
147 RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
148 RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
Tim Northover5dad9df2016-04-01 23:14:52 +0000149 auto PR = PassRegistry::getPassRegistry();
150 initializeGlobalISel(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000151 initializeAArch64A53Fix835769Pass(*PR);
152 initializeAArch64A57FPLoadBalancingPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000153 initializeAArch64AdvSIMDScalarPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000154 initializeAArch64CollectLOHPass(*PR);
155 initializeAArch64ConditionalComparesPass(*PR);
156 initializeAArch64ConditionOptimizerPass(*PR);
157 initializeAArch64DeadRegisterDefinitionsPass(*PR);
Tim Northover5dad9df2016-04-01 23:14:52 +0000158 initializeAArch64ExpandPseudoPass(*PR);
Geoff Berry24c81e82016-07-20 21:45:58 +0000159 initializeAArch64LoadStoreOptPass(*PR);
Abderrazek Zaafrani2c80e4c2017-12-08 00:58:49 +0000160 initializeAArch64SIMDInstrOptPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000161 initializeAArch64PromoteConstantPass(*PR);
162 initializeAArch64RedundantCopyEliminationPass(*PR);
163 initializeAArch64StorePairSuppressPass(*PR);
Geoff Berry9962fae2017-07-18 16:14:22 +0000164 initializeFalkorHWPFFixPass(*PR);
Geoff Berryb1e87142017-07-14 21:44:12 +0000165 initializeFalkorMarkStridedAccessesLegacyPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000166 initializeLDTLSCleanupPass(*PR);
Tim Northover3b0846e2014-05-24 12:50:23 +0000167}
168
Aditya Nandakumara2719322014-11-13 09:26:31 +0000169//===----------------------------------------------------------------------===//
170// AArch64 Lowering public interface.
171//===----------------------------------------------------------------------===//
172static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
173 if (TT.isOSBinFormatMachO())
Eugene Zelenko049b0172017-01-06 00:30:53 +0000174 return llvm::make_unique<AArch64_MachoTargetObjectFile>();
Mandeep Singh Grang0c721722017-06-27 23:58:19 +0000175 if (TT.isOSBinFormatCOFF())
176 return llvm::make_unique<AArch64_COFFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000177
Eugene Zelenko049b0172017-01-06 00:30:53 +0000178 return llvm::make_unique<AArch64_ELFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000179}
180
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000181// Helper function to build a DataLayout string
Joel Jones504bf332016-10-24 13:37:13 +0000182static std::string computeDataLayout(const Triple &TT,
183 const MCTargetOptions &Options,
184 bool LittleEndian) {
185 if (Options.getABIName() == "ilp32")
186 return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
Daniel Sandersed64d622015-06-11 15:34:59 +0000187 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000188 return "e-m:o-i64:64-i128:128-n32:64-S128";
Mandeep Singh Grang0c721722017-06-27 23:58:19 +0000189 if (TT.isOSBinFormatCOFF())
Mandeep Singh Grang6d6f2fa2017-07-17 21:25:19 +0000190 return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000191 if (LittleEndian)
Chad Rosier112d0e92016-07-07 20:02:18 +0000192 return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
193 return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000194}
195
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000196static Reloc::Model getEffectiveRelocModel(const Triple &TT,
197 Optional<Reloc::Model> RM) {
198 // AArch64 Darwin is always PIC.
199 if (TT.isOSDarwin())
200 return Reloc::PIC_;
201 // On ELF platforms the default static relocation model has a smart enough
202 // linker to cope with referencing external symbols defined in a shared
203 // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
204 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
205 return Reloc::Static;
206 return *RM;
207}
208
Rafael Espindola79e238a2017-08-03 02:16:21 +0000209static CodeModel::Model getEffectiveCodeModel(const Triple &TT,
210 Optional<CodeModel::Model> CM,
211 bool JIT) {
212 if (CM) {
213 if (*CM != CodeModel::Small && *CM != CodeModel::Large) {
214 if (!TT.isOSFuchsia())
215 report_fatal_error(
216 "Only small and large code models are allowed on AArch64");
217 else if (CM != CodeModel::Kernel)
218 report_fatal_error(
219 "Only small, kernel, and large code models are allowed on AArch64");
220 }
221 return *CM;
222 }
223 // The default MCJIT memory managers make no guarantees about where they can
224 // find an executable page; JITed code needs to be able to refer to globals
225 // no matter how far away they are.
226 if (JIT)
227 return CodeModel::Large;
228 return CodeModel::Small;
229}
230
Rafael Espindola38af4d62016-05-18 16:00:24 +0000231/// Create an AArch64 architecture model.
Tim Northover3b0846e2014-05-24 12:50:23 +0000232///
Rafael Espindola79e238a2017-08-03 02:16:21 +0000233AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
234 StringRef CPU, StringRef FS,
235 const TargetOptions &Options,
236 Optional<Reloc::Model> RM,
237 Optional<CodeModel::Model> CM,
238 CodeGenOpt::Level OL, bool JIT,
239 bool LittleEndian)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000240 : LLVMTargetMachine(T,
241 computeDataLayout(TT, Options.MCOptions, LittleEndian),
242 TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
243 getEffectiveCodeModel(TT, CM, JIT), OL),
Rafael Espindola79e238a2017-08-03 02:16:21 +0000244 TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000245 initAsmInfo();
Volkan Kelesa79b0622018-01-17 22:34:21 +0000246
Matthias Braunda5e7e12018-06-28 17:00:45 +0000247 if (TT.isOSBinFormatMachO()) {
Tim Northover271d3d22018-04-13 22:25:20 +0000248 this->Options.TrapUnreachable = true;
Matthias Braunda5e7e12018-06-28 17:00:45 +0000249 this->Options.NoTrapAfterNoreturn = true;
250 }
Tim Northover271d3d22018-04-13 22:25:20 +0000251
Volkan Kelesa79b0622018-01-17 22:34:21 +0000252 // Enable GlobalISel at or below EnableGlobalISelAt0.
253 if (getOptLevel() <= EnableGlobalISelAtO)
254 setGlobalISel(true);
Jessica Paquettedafa1982018-06-28 17:45:43 +0000255
256 // AArch64 supports the MachineOutliner.
257 setMachineOutliner(true);
Jessica Paquettef90edbe2018-07-27 20:18:27 +0000258
259 // AArch64 supports default outlining behaviour.
260 setSupportsDefaultOutlining(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000261}
262
Eugene Zelenko049b0172017-01-06 00:30:53 +0000263AArch64TargetMachine::~AArch64TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000264
Eric Christopher3faf2f12014-10-06 06:45:36 +0000265const AArch64Subtarget *
266AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000267 Attribute CPUAttr = F.getFnAttribute("target-cpu");
268 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000269
270 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
271 ? CPUAttr.getValueAsString().str()
272 : TargetCPU;
273 std::string FS = !FSAttr.hasAttribute(Attribute::None)
274 ? FSAttr.getValueAsString().str()
275 : TargetFS;
276
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000277 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000278 if (!I) {
279 // This needs to be done before we create a new subtarget since any
280 // creation will depend on the TM and the code generation flags on the
281 // function that reside in TargetOptions.
282 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000283 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000284 isLittle);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000285 }
286 return I.get();
287}
288
Tim Northover3b0846e2014-05-24 12:50:23 +0000289void AArch64leTargetMachine::anchor() { }
290
Daniel Sanders3e5de882015-06-11 19:41:26 +0000291AArch64leTargetMachine::AArch64leTargetMachine(
292 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000293 const TargetOptions &Options, Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000294 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
295 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000296
297void AArch64beTargetMachine::anchor() { }
298
Daniel Sanders3e5de882015-06-11 19:41:26 +0000299AArch64beTargetMachine::AArch64beTargetMachine(
300 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000301 const TargetOptions &Options, Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000302 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
303 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000304
305namespace {
Eugene Zelenko049b0172017-01-06 00:30:53 +0000306
Tim Northover3b0846e2014-05-24 12:50:23 +0000307/// AArch64 Code Generator Pass Configuration Options.
308class AArch64PassConfig : public TargetPassConfig {
309public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000310 AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000311 : TargetPassConfig(TM, PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000312 if (TM.getOptLevel() != CodeGenOpt::None)
Chad Rosier347ed4e2014-09-12 22:17:28 +0000313 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000314 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000315
316 AArch64TargetMachine &getAArch64TargetMachine() const {
317 return getTM<AArch64TargetMachine>();
318 }
319
Matthias Braun115efcd2016-11-28 20:11:54 +0000320 ScheduleDAGInstrs *
321 createMachineScheduler(MachineSchedContext *C) const override {
Florian Hahn15be1ac2017-07-12 21:41:28 +0000322 const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
Matthias Braun115efcd2016-11-28 20:11:54 +0000323 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
324 DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
325 DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
Florian Hahn15be1ac2017-07-12 21:41:28 +0000326 if (ST.hasFusion())
327 DAG->addMutation(createAArch64MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000328 return DAG;
329 }
330
Evandro Menezes455382e2017-02-01 02:54:42 +0000331 ScheduleDAGInstrs *
332 createPostMachineScheduler(MachineSchedContext *C) const override {
333 const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
Florian Hahnf934add2017-07-12 20:53:22 +0000334 if (ST.hasFusion()) {
Evandro Menezes455382e2017-02-01 02:54:42 +0000335 // Run the Macro Fusion after RA again since literals are expanded from
336 // pseudos then (v. addPreSched2()).
337 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
338 DAG->addMutation(createAArch64MacroFusionDAGMutation());
339 return DAG;
340 }
341
342 return nullptr;
343 }
344
Tim Northoverb4ddc082014-05-30 10:09:59 +0000345 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000346 bool addPreISel() override;
347 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000348 bool addIRTranslator() override;
Tim Northover33b07d62016-07-22 20:03:43 +0000349 bool addLegalizeMachineIR() override;
Quentin Colombetd4131812016-04-07 20:27:33 +0000350 bool addRegBankSelect() override;
Quentin Colombet7a43edd2017-05-27 01:34:07 +0000351 void addPreGlobalInstructionSelect() override;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000352 bool addGlobalInstructionSelect() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000353 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000354 void addPreRegAlloc() override;
355 void addPostRegAlloc() override;
356 void addPreSched2() override;
357 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000358};
Eugene Zelenko049b0172017-01-06 00:30:53 +0000359
360} // end anonymous namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000361
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000362TargetTransformInfo
363AArch64TargetMachine::getTargetTransformInfo(const Function &F) {
364 return TargetTransformInfo(AArch64TTIImpl(this, F));
Tim Northover3b0846e2014-05-24 12:50:23 +0000365}
366
367TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000368 return new AArch64PassConfig(*this, PM);
Tim Northover3b0846e2014-05-24 12:50:23 +0000369}
370
Tim Northoverb4ddc082014-05-30 10:09:59 +0000371void AArch64PassConfig::addIRPasses() {
372 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
373 // ourselves.
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000374 addPass(createAtomicExpandPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000375
376 // Cmpxchg instructions are often used with a subsequent comparison to
377 // determine whether it succeeded. We can exploit existing control-flow in
378 // ldrex/strex loops to simplify this, but it needs tidying up.
379 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000380 addPass(createCFGSimplificationPass(1, true, true, false, true));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000381
Junmo Park384d3762016-07-06 23:18:58 +0000382 // Run LoopDataPrefetch
Adam Nemet53e758f2016-03-18 00:27:29 +0000383 //
384 // Run this before LSR to remove the multiplies involved in computing the
385 // pointer values N iterations ahead.
Geoff Berryb1e87142017-07-14 21:44:12 +0000386 if (TM->getOptLevel() != CodeGenOpt::None) {
387 if (EnableLoopDataPrefetch)
388 addPass(createLoopDataPrefetchPass());
389 if (EnableFalkorHWPFFix)
390 addPass(createFalkorMarkStridedAccessesPass());
391 }
Adam Nemet53e758f2016-03-18 00:27:29 +0000392
Tim Northoverb4ddc082014-05-30 10:09:59 +0000393 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000394
Hao Liu7ec8ee32015-06-26 02:32:07 +0000395 // Match interleaved memory accesses to ldN/stN intrinsics.
396 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000397 addPass(createInterleavedAccessPass());
Hao Liu7ec8ee32015-06-26 02:32:07 +0000398
Hao Liufd46bea2014-11-19 06:39:53 +0000399 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
400 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
401 // and lower a GEP with multiple indices to either arithmetic operations or
402 // multiple GEPs with single index.
David Blaikie8ad9a972018-03-28 22:28:50 +0000403 addPass(createSeparateConstOffsetFromGEPPass(true));
Hao Liufd46bea2014-11-19 06:39:53 +0000404 // Call EarlyCSE pass to find and remove subexpressions in the lowered
405 // result.
406 addPass(createEarlyCSEPass());
407 // Do loop invariant code motion in case part of the lowered result is
408 // invariant.
409 addPass(createLICMPass());
410 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000411}
412
Tim Northover3b0846e2014-05-24 12:50:23 +0000413// Pass Pipeline Configuration
414bool AArch64PassConfig::addPreISel() {
415 // Run promote constant before global merge, so that the promoted constants
416 // get a chance to be merged
417 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
418 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000419 // FIXME: On AArch64, this depends on the type.
420 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
421 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000422 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000423 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000424 EnableGlobalMerge == cl::BOU_TRUE) {
425 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
426 (EnableGlobalMerge == cl::BOU_UNSET);
427 addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
428 }
429
Tim Northover3b0846e2014-05-24 12:50:23 +0000430 return false;
431}
432
433bool AArch64PassConfig::addInstSelector() {
434 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
435
436 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
437 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000438 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000439 getOptLevel() != CodeGenOpt::None)
440 addPass(createAArch64CleanupLocalDynamicTLSPass());
441
442 return false;
443}
444
Quentin Colombetd96f4952016-02-11 19:35:06 +0000445bool AArch64PassConfig::addIRTranslator() {
446 addPass(new IRTranslator());
447 return false;
448}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000449
Tim Northover33b07d62016-07-22 20:03:43 +0000450bool AArch64PassConfig::addLegalizeMachineIR() {
Tim Northover69fa84a2016-10-14 22:18:18 +0000451 addPass(new Legalizer());
Tim Northover33b07d62016-07-22 20:03:43 +0000452 return false;
453}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000454
Quentin Colombetd4131812016-04-07 20:27:33 +0000455bool AArch64PassConfig::addRegBankSelect() {
456 addPass(new RegBankSelect());
457 return false;
458}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000459
Quentin Colombet7a43edd2017-05-27 01:34:07 +0000460void AArch64PassConfig::addPreGlobalInstructionSelect() {
461 // Workaround the deficiency of the fast register allocator.
462 if (TM->getOptLevel() == CodeGenOpt::None)
463 addPass(new Localizer());
464}
465
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000466bool AArch64PassConfig::addGlobalInstructionSelect() {
467 addPass(new InstructionSelect());
468 return false;
469}
Quentin Colombetd96f4952016-02-11 19:35:06 +0000470
Tim Northover3b0846e2014-05-24 12:50:23 +0000471bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000472 if (EnableCondOpt)
473 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000474 if (EnableCCMP)
475 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000476 if (EnableMCR)
477 addPass(&MachineCombinerID);
Chad Rosier6db9ff62017-06-23 19:20:12 +0000478 if (EnableCondBrTuning)
479 addPass(createAArch64CondBrTuning());
James Molloy99917942014-08-06 13:31:32 +0000480 if (EnableEarlyIfConversion)
481 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000482 if (EnableStPairSuppress)
483 addPass(createAArch64StorePairSuppressPass());
Abderrazek Zaafrani2c80e4c2017-12-08 00:58:49 +0000484 addPass(createAArch64SIMDInstrOptPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000485 return true;
486}
487
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000488void AArch64PassConfig::addPreRegAlloc() {
Matthias Braun3d51cf02016-11-16 03:38:27 +0000489 // Change dead register definitions to refer to the zero register.
490 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
491 addPass(createAArch64DeadRegisterDefinitions());
492
Tim Northover3b0846e2014-05-24 12:50:23 +0000493 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000494 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000495 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000496 // The AdvSIMD pass may produce copies that can be rewritten to
497 // be register coaleascer friendly.
498 addPass(&PeepholeOptimizerID);
499 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000500}
501
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000502void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000503 // Remove redundant copy instructions.
504 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
505 addPass(createAArch64RedundantCopyEliminationPass());
506
Eric Christopher6f1e5682015-03-03 23:22:40 +0000507 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000508 // Improve performance for some FP/SIMD code for A57.
509 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000510}
511
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000512void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000513 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000514 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000515 // Use load/store pair instructions when possible.
Geoff Berry9962fae2017-07-18 16:14:22 +0000516 if (TM->getOptLevel() != CodeGenOpt::None) {
517 if (EnableLoadStoreOpt)
518 addPass(createAArch64LoadStoreOptimizationPass());
519 if (EnableFalkorHWPFFix)
520 addPass(createFalkorHWPFFixPass());
521 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000522}
523
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000524void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000525 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000526 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000527 // Relax conditional branch instructions if they're otherwise out of
528 // range of their destination.
Diana Picus850043b2016-08-01 05:56:57 +0000529 if (BranchRelaxation)
Matt Arsenault36919a42016-10-06 15:38:53 +0000530 addPass(&BranchRelaxationPassID);
531
Tim Northover3b0846e2014-05-24 12:50:23 +0000532 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000533 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000534 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000535}