blob: a66f5277f243cf7eb12361a496e6d50f4b5dee61 [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);
Daniel Sanders9659bfd2018-10-01 18:56:47 +0000161 initializeAArch64PreLegalizerCombinerPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000162 initializeAArch64PromoteConstantPass(*PR);
163 initializeAArch64RedundantCopyEliminationPass(*PR);
164 initializeAArch64StorePairSuppressPass(*PR);
Geoff Berry9962fae2017-07-18 16:14:22 +0000165 initializeFalkorHWPFFixPass(*PR);
Geoff Berryb1e87142017-07-14 21:44:12 +0000166 initializeFalkorMarkStridedAccessesLegacyPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000167 initializeLDTLSCleanupPass(*PR);
Tim Northover3b0846e2014-05-24 12:50:23 +0000168}
169
Aditya Nandakumara2719322014-11-13 09:26:31 +0000170//===----------------------------------------------------------------------===//
171// AArch64 Lowering public interface.
172//===----------------------------------------------------------------------===//
173static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
174 if (TT.isOSBinFormatMachO())
Eugene Zelenko049b0172017-01-06 00:30:53 +0000175 return llvm::make_unique<AArch64_MachoTargetObjectFile>();
Mandeep Singh Grang0c721722017-06-27 23:58:19 +0000176 if (TT.isOSBinFormatCOFF())
177 return llvm::make_unique<AArch64_COFFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000178
Eugene Zelenko049b0172017-01-06 00:30:53 +0000179 return llvm::make_unique<AArch64_ELFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000180}
181
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000182// Helper function to build a DataLayout string
Joel Jones504bf332016-10-24 13:37:13 +0000183static std::string computeDataLayout(const Triple &TT,
184 const MCTargetOptions &Options,
185 bool LittleEndian) {
186 if (Options.getABIName() == "ilp32")
187 return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
Daniel Sandersed64d622015-06-11 15:34:59 +0000188 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000189 return "e-m:o-i64:64-i128:128-n32:64-S128";
Mandeep Singh Grang0c721722017-06-27 23:58:19 +0000190 if (TT.isOSBinFormatCOFF())
Mandeep Singh Grang6d6f2fa2017-07-17 21:25:19 +0000191 return "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000192 if (LittleEndian)
Chad Rosier112d0e92016-07-07 20:02:18 +0000193 return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
194 return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000195}
196
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000197static Reloc::Model getEffectiveRelocModel(const Triple &TT,
198 Optional<Reloc::Model> RM) {
199 // AArch64 Darwin is always PIC.
200 if (TT.isOSDarwin())
201 return Reloc::PIC_;
202 // On ELF platforms the default static relocation model has a smart enough
203 // linker to cope with referencing external symbols defined in a shared
204 // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
205 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
206 return Reloc::Static;
207 return *RM;
208}
209
Rafael Espindola79e238a2017-08-03 02:16:21 +0000210static CodeModel::Model getEffectiveCodeModel(const Triple &TT,
211 Optional<CodeModel::Model> CM,
212 bool JIT) {
213 if (CM) {
David Green9dd1d452018-08-22 11:31:39 +0000214 if (*CM != CodeModel::Small && *CM != CodeModel::Tiny &&
215 *CM != CodeModel::Large) {
Rafael Espindola79e238a2017-08-03 02:16:21 +0000216 if (!TT.isOSFuchsia())
217 report_fatal_error(
David Green9dd1d452018-08-22 11:31:39 +0000218 "Only small, tiny and large code models are allowed on AArch64");
219 else if (*CM != CodeModel::Kernel)
220 report_fatal_error("Only small, tiny, kernel, and large code models "
221 "are allowed on AArch64");
222 } else if (*CM == CodeModel::Tiny && !TT.isOSBinFormatELF())
223 report_fatal_error("tiny code model is only supported on ELF");
Rafael Espindola79e238a2017-08-03 02:16:21 +0000224 return *CM;
225 }
226 // The default MCJIT memory managers make no guarantees about where they can
227 // find an executable page; JITed code needs to be able to refer to globals
228 // no matter how far away they are.
229 if (JIT)
230 return CodeModel::Large;
231 return CodeModel::Small;
232}
233
Rafael Espindola38af4d62016-05-18 16:00:24 +0000234/// Create an AArch64 architecture model.
Tim Northover3b0846e2014-05-24 12:50:23 +0000235///
Rafael Espindola79e238a2017-08-03 02:16:21 +0000236AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
237 StringRef CPU, StringRef FS,
238 const TargetOptions &Options,
239 Optional<Reloc::Model> RM,
240 Optional<CodeModel::Model> CM,
241 CodeGenOpt::Level OL, bool JIT,
242 bool LittleEndian)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000243 : LLVMTargetMachine(T,
244 computeDataLayout(TT, Options.MCOptions, LittleEndian),
245 TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM),
246 getEffectiveCodeModel(TT, CM, JIT), OL),
Rafael Espindola79e238a2017-08-03 02:16:21 +0000247 TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000248 initAsmInfo();
Volkan Kelesa79b0622018-01-17 22:34:21 +0000249
Matthias Braunda5e7e12018-06-28 17:00:45 +0000250 if (TT.isOSBinFormatMachO()) {
Tim Northover271d3d22018-04-13 22:25:20 +0000251 this->Options.TrapUnreachable = true;
Matthias Braunda5e7e12018-06-28 17:00:45 +0000252 this->Options.NoTrapAfterNoreturn = true;
253 }
Tim Northover271d3d22018-04-13 22:25:20 +0000254
Volkan Kelesa79b0622018-01-17 22:34:21 +0000255 // Enable GlobalISel at or below EnableGlobalISelAt0.
256 if (getOptLevel() <= EnableGlobalISelAtO)
257 setGlobalISel(true);
Jessica Paquettedafa1982018-06-28 17:45:43 +0000258
259 // AArch64 supports the MachineOutliner.
260 setMachineOutliner(true);
Jessica Paquettef90edbe2018-07-27 20:18:27 +0000261
262 // AArch64 supports default outlining behaviour.
263 setSupportsDefaultOutlining(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000264}
265
Eugene Zelenko049b0172017-01-06 00:30:53 +0000266AArch64TargetMachine::~AArch64TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000267
Eric Christopher3faf2f12014-10-06 06:45:36 +0000268const AArch64Subtarget *
269AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000270 Attribute CPUAttr = F.getFnAttribute("target-cpu");
271 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000272
273 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
274 ? CPUAttr.getValueAsString().str()
275 : TargetCPU;
276 std::string FS = !FSAttr.hasAttribute(Attribute::None)
277 ? FSAttr.getValueAsString().str()
278 : TargetFS;
279
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000280 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000281 if (!I) {
282 // This needs to be done before we create a new subtarget since any
283 // creation will depend on the TM and the code generation flags on the
284 // function that reside in TargetOptions.
285 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000286 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000287 isLittle);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000288 }
289 return I.get();
290}
291
Tim Northover3b0846e2014-05-24 12:50:23 +0000292void AArch64leTargetMachine::anchor() { }
293
Daniel Sanders3e5de882015-06-11 19:41:26 +0000294AArch64leTargetMachine::AArch64leTargetMachine(
295 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000296 const TargetOptions &Options, Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000297 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
298 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000299
300void AArch64beTargetMachine::anchor() { }
301
Daniel Sanders3e5de882015-06-11 19:41:26 +0000302AArch64beTargetMachine::AArch64beTargetMachine(
303 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000304 const TargetOptions &Options, Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000305 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT)
306 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000307
308namespace {
Eugene Zelenko049b0172017-01-06 00:30:53 +0000309
Tim Northover3b0846e2014-05-24 12:50:23 +0000310/// AArch64 Code Generator Pass Configuration Options.
311class AArch64PassConfig : public TargetPassConfig {
312public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000313 AArch64PassConfig(AArch64TargetMachine &TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000314 : TargetPassConfig(TM, PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000315 if (TM.getOptLevel() != CodeGenOpt::None)
Chad Rosier347ed4e2014-09-12 22:17:28 +0000316 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000317 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000318
319 AArch64TargetMachine &getAArch64TargetMachine() const {
320 return getTM<AArch64TargetMachine>();
321 }
322
Matthias Braun115efcd2016-11-28 20:11:54 +0000323 ScheduleDAGInstrs *
324 createMachineScheduler(MachineSchedContext *C) const override {
Florian Hahn15be1ac2017-07-12 21:41:28 +0000325 const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
Matthias Braun115efcd2016-11-28 20:11:54 +0000326 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
327 DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
328 DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
Florian Hahn15be1ac2017-07-12 21:41:28 +0000329 if (ST.hasFusion())
330 DAG->addMutation(createAArch64MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000331 return DAG;
332 }
333
Evandro Menezes455382e2017-02-01 02:54:42 +0000334 ScheduleDAGInstrs *
335 createPostMachineScheduler(MachineSchedContext *C) const override {
336 const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
Florian Hahnf934add2017-07-12 20:53:22 +0000337 if (ST.hasFusion()) {
Evandro Menezes455382e2017-02-01 02:54:42 +0000338 // Run the Macro Fusion after RA again since literals are expanded from
339 // pseudos then (v. addPreSched2()).
340 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
341 DAG->addMutation(createAArch64MacroFusionDAGMutation());
342 return DAG;
343 }
344
345 return nullptr;
346 }
347
Tim Northoverb4ddc082014-05-30 10:09:59 +0000348 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000349 bool addPreISel() override;
350 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000351 bool addIRTranslator() override;
Daniel Sanders9659bfd2018-10-01 18:56:47 +0000352 void addPreLegalizeMachineIR() override;
Tim Northover33b07d62016-07-22 20:03:43 +0000353 bool addLegalizeMachineIR() override;
Quentin Colombetd4131812016-04-07 20:27:33 +0000354 bool addRegBankSelect() override;
Quentin Colombet7a43edd2017-05-27 01:34:07 +0000355 void addPreGlobalInstructionSelect() override;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000356 bool addGlobalInstructionSelect() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000357 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000358 void addPreRegAlloc() override;
359 void addPostRegAlloc() override;
360 void addPreSched2() override;
361 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000362};
Eugene Zelenko049b0172017-01-06 00:30:53 +0000363
364} // end anonymous namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000365
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000366TargetTransformInfo
367AArch64TargetMachine::getTargetTransformInfo(const Function &F) {
368 return TargetTransformInfo(AArch64TTIImpl(this, F));
Tim Northover3b0846e2014-05-24 12:50:23 +0000369}
370
371TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000372 return new AArch64PassConfig(*this, PM);
Tim Northover3b0846e2014-05-24 12:50:23 +0000373}
374
Tim Northoverb4ddc082014-05-30 10:09:59 +0000375void AArch64PassConfig::addIRPasses() {
376 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
377 // ourselves.
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000378 addPass(createAtomicExpandPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000379
380 // Cmpxchg instructions are often used with a subsequent comparison to
381 // determine whether it succeeded. We can exploit existing control-flow in
382 // ldrex/strex loops to simplify this, but it needs tidying up.
383 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000384 addPass(createCFGSimplificationPass(1, true, true, false, true));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000385
Junmo Park384d3762016-07-06 23:18:58 +0000386 // Run LoopDataPrefetch
Adam Nemet53e758f2016-03-18 00:27:29 +0000387 //
388 // Run this before LSR to remove the multiplies involved in computing the
389 // pointer values N iterations ahead.
Geoff Berryb1e87142017-07-14 21:44:12 +0000390 if (TM->getOptLevel() != CodeGenOpt::None) {
391 if (EnableLoopDataPrefetch)
392 addPass(createLoopDataPrefetchPass());
393 if (EnableFalkorHWPFFix)
394 addPass(createFalkorMarkStridedAccessesPass());
395 }
Adam Nemet53e758f2016-03-18 00:27:29 +0000396
Tim Northoverb4ddc082014-05-30 10:09:59 +0000397 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000398
Hao Liu7ec8ee32015-06-26 02:32:07 +0000399 // Match interleaved memory accesses to ldN/stN intrinsics.
400 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000401 addPass(createInterleavedAccessPass());
Hao Liu7ec8ee32015-06-26 02:32:07 +0000402
Hao Liufd46bea2014-11-19 06:39:53 +0000403 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
404 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
405 // and lower a GEP with multiple indices to either arithmetic operations or
406 // multiple GEPs with single index.
David Blaikie8ad9a972018-03-28 22:28:50 +0000407 addPass(createSeparateConstOffsetFromGEPPass(true));
Hao Liufd46bea2014-11-19 06:39:53 +0000408 // Call EarlyCSE pass to find and remove subexpressions in the lowered
409 // result.
410 addPass(createEarlyCSEPass());
411 // Do loop invariant code motion in case part of the lowered result is
412 // invariant.
413 addPass(createLICMPass());
414 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000415}
416
Tim Northover3b0846e2014-05-24 12:50:23 +0000417// Pass Pipeline Configuration
418bool AArch64PassConfig::addPreISel() {
419 // Run promote constant before global merge, so that the promoted constants
420 // get a chance to be merged
421 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
422 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000423 // FIXME: On AArch64, this depends on the type.
424 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
425 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000426 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000427 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000428 EnableGlobalMerge == cl::BOU_TRUE) {
429 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
430 (EnableGlobalMerge == cl::BOU_UNSET);
431 addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
432 }
433
Tim Northover3b0846e2014-05-24 12:50:23 +0000434 return false;
435}
436
437bool AArch64PassConfig::addInstSelector() {
438 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
439
440 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
441 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000442 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000443 getOptLevel() != CodeGenOpt::None)
444 addPass(createAArch64CleanupLocalDynamicTLSPass());
445
446 return false;
447}
448
Quentin Colombetd96f4952016-02-11 19:35:06 +0000449bool AArch64PassConfig::addIRTranslator() {
450 addPass(new IRTranslator());
451 return false;
452}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000453
Daniel Sanders9659bfd2018-10-01 18:56:47 +0000454void AArch64PassConfig::addPreLegalizeMachineIR() {
455 addPass(createAArch64PreLegalizeCombiner());
456}
457
Tim Northover33b07d62016-07-22 20:03:43 +0000458bool AArch64PassConfig::addLegalizeMachineIR() {
Tim Northover69fa84a2016-10-14 22:18:18 +0000459 addPass(new Legalizer());
Tim Northover33b07d62016-07-22 20:03:43 +0000460 return false;
461}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000462
Quentin Colombetd4131812016-04-07 20:27:33 +0000463bool AArch64PassConfig::addRegBankSelect() {
464 addPass(new RegBankSelect());
465 return false;
466}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000467
Quentin Colombet7a43edd2017-05-27 01:34:07 +0000468void AArch64PassConfig::addPreGlobalInstructionSelect() {
469 // Workaround the deficiency of the fast register allocator.
470 if (TM->getOptLevel() == CodeGenOpt::None)
471 addPass(new Localizer());
472}
473
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000474bool AArch64PassConfig::addGlobalInstructionSelect() {
475 addPass(new InstructionSelect());
476 return false;
477}
Quentin Colombetd96f4952016-02-11 19:35:06 +0000478
Tim Northover3b0846e2014-05-24 12:50:23 +0000479bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000480 if (EnableCondOpt)
481 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000482 if (EnableCCMP)
483 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000484 if (EnableMCR)
485 addPass(&MachineCombinerID);
Chad Rosier6db9ff62017-06-23 19:20:12 +0000486 if (EnableCondBrTuning)
487 addPass(createAArch64CondBrTuning());
James Molloy99917942014-08-06 13:31:32 +0000488 if (EnableEarlyIfConversion)
489 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000490 if (EnableStPairSuppress)
491 addPass(createAArch64StorePairSuppressPass());
Abderrazek Zaafrani2c80e4c2017-12-08 00:58:49 +0000492 addPass(createAArch64SIMDInstrOptPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000493 return true;
494}
495
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000496void AArch64PassConfig::addPreRegAlloc() {
Matthias Braun3d51cf02016-11-16 03:38:27 +0000497 // Change dead register definitions to refer to the zero register.
498 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
499 addPass(createAArch64DeadRegisterDefinitions());
500
Tim Northover3b0846e2014-05-24 12:50:23 +0000501 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000502 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000503 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000504 // The AdvSIMD pass may produce copies that can be rewritten to
505 // be register coaleascer friendly.
506 addPass(&PeepholeOptimizerID);
507 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000508}
509
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000510void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000511 // Remove redundant copy instructions.
512 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
513 addPass(createAArch64RedundantCopyEliminationPass());
514
Eric Christopher6f1e5682015-03-03 23:22:40 +0000515 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000516 // Improve performance for some FP/SIMD code for A57.
517 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000518}
519
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000520void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000521 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000522 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000523 // Use load/store pair instructions when possible.
Geoff Berry9962fae2017-07-18 16:14:22 +0000524 if (TM->getOptLevel() != CodeGenOpt::None) {
525 if (EnableLoadStoreOpt)
526 addPass(createAArch64LoadStoreOptimizationPass());
527 if (EnableFalkorHWPFFix)
528 addPass(createFalkorHWPFFixPass());
529 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000530}
531
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000532void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000533 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000534 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000535 // Relax conditional branch instructions if they're otherwise out of
536 // range of their destination.
Diana Picus850043b2016-08-01 05:56:57 +0000537 if (BranchRelaxation)
Matt Arsenault36919a42016-10-06 15:38:53 +0000538 addPass(&BranchRelaxationPassID);
539
Tim Northover3b0846e2014-05-24 12:50:23 +0000540 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000541 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000542 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000543}