blob: 3368c98418791fabee57c966d22cc0732ddb3478 [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"
Quentin Colombetc17f7442016-04-06 17:26:03 +000014#include "AArch64CallLowering.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000015#include "AArch64InstructionSelector.h"
Tim Northover69fa84a2016-10-14 22:18:18 +000016#include "AArch64LegalizerInfo.h"
Evandro Menezes94edf022017-02-01 02:54:34 +000017#include "AArch64MacroFusion.h"
Daniel Sandersd64d50242017-01-19 11:15:55 +000018#ifdef LLVM_BUILD_GLOBAL_ISEL
Quentin Colombetc17f7442016-04-06 17:26:03 +000019#include "AArch64RegisterBankInfo.h"
Daniel Sandersd64d50242017-01-19 11:15:55 +000020#endif
Eugene Zelenko049b0172017-01-06 00:30:53 +000021#include "AArch64Subtarget.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000022#include "AArch64TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000023#include "AArch64TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000024#include "AArch64TargetTransformInfo.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000025#include "MCTargetDesc/AArch64MCTargetDesc.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/Analysis/TargetTransformInfo.h"
29#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
Quentin Colombet846219a2016-04-07 21:24:40 +000030#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000031#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Tim Northover69fa84a2016-10-14 22:18:18 +000032#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Quentin Colombetd4131812016-04-07 20:27:33 +000033#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000034#include "llvm/CodeGen/MachineScheduler.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000035#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000036#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000037#include "llvm/IR/Attributes.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000038#include "llvm/IR/Function.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000039#include "llvm/MC/MCTargetOptions.h"
40#include "llvm/Pass.h"
41#include "llvm/Support/CodeGen.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000044#include "llvm/Target/TargetLoweringObjectFile.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000045#include "llvm/Target/TargetOptions.h"
46#include "llvm/Transforms/Scalar.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000047#include <memory>
48#include <string>
49
Tim Northover3b0846e2014-05-24 12:50:23 +000050using namespace llvm;
51
Diana Picus850043b2016-08-01 05:56:57 +000052static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
53 cl::desc("Enable the CCMP formation pass"),
54 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000055
Diana Picus850043b2016-08-01 05:56:57 +000056static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000057 cl::desc("Enable the machine combiner pass"),
58 cl::init(true), cl::Hidden);
59
Diana Picus850043b2016-08-01 05:56:57 +000060static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
61 cl::desc("Suppress STP for AArch64"),
62 cl::init(true), cl::Hidden);
63
64static cl::opt<bool> EnableAdvSIMDScalar(
65 "aarch64-enable-simd-scalar",
66 cl::desc("Enable use of AdvSIMD scalar integer instructions"),
67 cl::init(false), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000068
69static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000070 EnablePromoteConstant("aarch64-enable-promote-const",
71 cl::desc("Enable the promote constant pass"),
72 cl::init(true), cl::Hidden);
73
74static cl::opt<bool> EnableCollectLOH(
75 "aarch64-enable-collect-loh",
76 cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
77 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000078
79static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000080 EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
81 cl::desc("Enable the pass that removes dead"
82 " definitons and replaces stores to"
83 " them with stores to the zero"
84 " register"),
85 cl::init(true));
Tim Northover3b0846e2014-05-24 12:50:23 +000086
Diana Picus850043b2016-08-01 05:56:57 +000087static cl::opt<bool> EnableRedundantCopyElimination(
88 "aarch64-enable-copyelim",
89 cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
90 cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000091
Diana Picus850043b2016-08-01 05:56:57 +000092static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
93 cl::desc("Enable the load/store pair"
94 " optimization pass"),
95 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000096
Diana Picus850043b2016-08-01 05:56:57 +000097static cl::opt<bool> EnableAtomicTidy(
98 "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
99 cl::desc("Run SimplifyCFG after expanding atomic operations"
100 " to make use of cmpxchg flow-based information"),
101 cl::init(true));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000102
James Molloy99917942014-08-06 13:31:32 +0000103static cl::opt<bool>
104EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
105 cl::desc("Run early if-conversion"),
106 cl::init(true));
107
Jiangning Liu1a486da2014-09-05 02:55:24 +0000108static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000109 EnableCondOpt("aarch64-enable-condopt",
110 cl::desc("Enable the condition optimizer pass"),
111 cl::init(true), cl::Hidden);
Jiangning Liu1a486da2014-09-05 02:55:24 +0000112
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +0000113static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +0000114EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
115 cl::desc("Work around Cortex-A53 erratum 835769"),
116 cl::init(false));
117
Hao Liufd46bea2014-11-19 06:39:53 +0000118static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000119 EnableAddressTypePromotion("aarch64-enable-type-promotion", cl::Hidden,
120 cl::desc("Enable the type promotion pass"),
121 cl::init(true));
122
123static cl::opt<bool>
124 EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
125 cl::desc("Enable optimizations on complex GEPs"),
126 cl::init(false));
127
128static cl::opt<bool>
129 BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
130 cl::desc("Relax out of range conditional branches"));
Hao Liufd46bea2014-11-19 06:39:53 +0000131
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000132// FIXME: Unify control over GlobalMerge.
133static cl::opt<cl::boolOrDefault>
Diana Picus850043b2016-08-01 05:56:57 +0000134 EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
135 cl::desc("Enable the global merge pass"));
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000136
Adam Nemet53e758f2016-03-18 00:27:29 +0000137static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000138 EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
Adam Nemet53e758f2016-03-18 00:27:29 +0000139 cl::desc("Enable the loop data prefetch pass"),
Adam Nemetfb8fbba52016-03-30 00:21:29 +0000140 cl::init(true));
Adam Nemet53e758f2016-03-18 00:27:29 +0000141
Tim Northover3b0846e2014-05-24 12:50:23 +0000142extern "C" void LLVMInitializeAArch64Target() {
143 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +0000144 RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
145 RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
146 RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
Tim Northover5dad9df2016-04-01 23:14:52 +0000147 auto PR = PassRegistry::getPassRegistry();
148 initializeGlobalISel(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000149 initializeAArch64A53Fix835769Pass(*PR);
150 initializeAArch64A57FPLoadBalancingPass(*PR);
151 initializeAArch64AddressTypePromotionPass(*PR);
152 initializeAArch64AdvSIMDScalarPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000153 initializeAArch64CollectLOHPass(*PR);
154 initializeAArch64ConditionalComparesPass(*PR);
155 initializeAArch64ConditionOptimizerPass(*PR);
156 initializeAArch64DeadRegisterDefinitionsPass(*PR);
Tim Northover5dad9df2016-04-01 23:14:52 +0000157 initializeAArch64ExpandPseudoPass(*PR);
Geoff Berry24c81e82016-07-20 21:45:58 +0000158 initializeAArch64LoadStoreOptPass(*PR);
Sebastian Popeb65d722016-10-08 12:30:07 +0000159 initializeAArch64VectorByElementOptPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000160 initializeAArch64PromoteConstantPass(*PR);
161 initializeAArch64RedundantCopyEliminationPass(*PR);
162 initializeAArch64StorePairSuppressPass(*PR);
163 initializeLDTLSCleanupPass(*PR);
Tim Northover3b0846e2014-05-24 12:50:23 +0000164}
165
Aditya Nandakumara2719322014-11-13 09:26:31 +0000166//===----------------------------------------------------------------------===//
167// AArch64 Lowering public interface.
168//===----------------------------------------------------------------------===//
169static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
170 if (TT.isOSBinFormatMachO())
Eugene Zelenko049b0172017-01-06 00:30:53 +0000171 return llvm::make_unique<AArch64_MachoTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000172
Eugene Zelenko049b0172017-01-06 00:30:53 +0000173 return llvm::make_unique<AArch64_ELFTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000174}
175
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000176// Helper function to build a DataLayout string
Joel Jones504bf332016-10-24 13:37:13 +0000177static std::string computeDataLayout(const Triple &TT,
178 const MCTargetOptions &Options,
179 bool LittleEndian) {
180 if (Options.getABIName() == "ilp32")
181 return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
Daniel Sandersed64d622015-06-11 15:34:59 +0000182 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000183 return "e-m:o-i64:64-i128:128-n32:64-S128";
184 if (LittleEndian)
Chad Rosier112d0e92016-07-07 20:02:18 +0000185 return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
186 return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000187}
188
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000189static Reloc::Model getEffectiveRelocModel(const Triple &TT,
190 Optional<Reloc::Model> RM) {
191 // AArch64 Darwin is always PIC.
192 if (TT.isOSDarwin())
193 return Reloc::PIC_;
194 // On ELF platforms the default static relocation model has a smart enough
195 // linker to cope with referencing external symbols defined in a shared
196 // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
197 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
198 return Reloc::Static;
199 return *RM;
200}
201
Rafael Espindola38af4d62016-05-18 16:00:24 +0000202/// Create an AArch64 architecture model.
Tim Northover3b0846e2014-05-24 12:50:23 +0000203///
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000204AArch64TargetMachine::AArch64TargetMachine(
205 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
206 const TargetOptions &Options, Optional<Reloc::Model> RM,
207 CodeModel::Model CM, CodeGenOpt::Level OL, bool LittleEndian)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000208 // This nested ternary is horrible, but DL needs to be properly
Eric Christopher63ea0402015-03-12 18:23:01 +0000209 // initialized before TLInfo is constructed.
Joel Jones504bf332016-10-24 13:37:13 +0000210 : LLVMTargetMachine(T, computeDataLayout(TT, Options.MCOptions,
211 LittleEndian),
212 TT, CPU, FS, Options,
213 getEffectiveRelocModel(TT, RM), CM, OL),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000214 TLOF(createTLOF(getTargetTriple())),
Evandro Menezesba4926e2016-09-20 19:02:06 +0000215 isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000216 initAsmInfo();
217}
218
Eugene Zelenko049b0172017-01-06 00:30:53 +0000219AArch64TargetMachine::~AArch64TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000220
Quentin Colombetc17f7442016-04-06 17:26:03 +0000221#ifdef LLVM_BUILD_GLOBAL_ISEL
222namespace {
Eugene Zelenko049b0172017-01-06 00:30:53 +0000223
Tom Stellardcef0fe42016-04-14 17:45:38 +0000224struct AArch64GISelActualAccessor : public GISelAccessor {
Quentin Colombetc17f7442016-04-06 17:26:03 +0000225 std::unique_ptr<CallLowering> CallLoweringInfo;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000226 std::unique_ptr<InstructionSelector> InstSelector;
Tim Northover69fa84a2016-10-14 22:18:18 +0000227 std::unique_ptr<LegalizerInfo> Legalizer;
Quentin Colombetc17f7442016-04-06 17:26:03 +0000228 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko049b0172017-01-06 00:30:53 +0000229
Quentin Colombetc17f7442016-04-06 17:26:03 +0000230 const CallLowering *getCallLowering() const override {
231 return CallLoweringInfo.get();
232 }
Eugene Zelenko049b0172017-01-06 00:30:53 +0000233
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000234 const InstructionSelector *getInstructionSelector() const override {
235 return InstSelector.get();
236 }
Eugene Zelenko049b0172017-01-06 00:30:53 +0000237
Ahmed Bougacha52286032016-12-15 18:45:30 +0000238 const LegalizerInfo *getLegalizerInfo() const override {
Chandler Carruth488cb132016-07-23 07:50:05 +0000239 return Legalizer.get();
Tim Northover33b07d62016-07-22 20:03:43 +0000240 }
Eugene Zelenko049b0172017-01-06 00:30:53 +0000241
Quentin Colombetc17f7442016-04-06 17:26:03 +0000242 const RegisterBankInfo *getRegBankInfo() const override {
243 return RegBankInfo.get();
244 }
245};
Eugene Zelenko049b0172017-01-06 00:30:53 +0000246
247} // end anonymous namespace
Quentin Colombetc17f7442016-04-06 17:26:03 +0000248#endif
249
Eric Christopher3faf2f12014-10-06 06:45:36 +0000250const AArch64Subtarget *
251AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000252 Attribute CPUAttr = F.getFnAttribute("target-cpu");
253 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000254
255 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
256 ? CPUAttr.getValueAsString().str()
257 : TargetCPU;
258 std::string FS = !FSAttr.hasAttribute(Attribute::None)
259 ? FSAttr.getValueAsString().str()
260 : TargetFS;
261
262 auto &I = SubtargetMap[CPU + FS];
263 if (!I) {
264 // This needs to be done before we create a new subtarget since any
265 // creation will depend on the TM and the code generation flags on the
266 // function that reside in TargetOptions.
267 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000268 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
Evandro Menezesba4926e2016-09-20 19:02:06 +0000269 isLittle);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000270#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picusbda72762016-11-14 10:25:43 +0000271 GISelAccessor *GISel = new GISelAccessor();
Quentin Colombetc17f7442016-04-06 17:26:03 +0000272#else
Tom Stellardcef0fe42016-04-14 17:45:38 +0000273 AArch64GISelActualAccessor *GISel =
Quentin Colombetc17f7442016-04-06 17:26:03 +0000274 new AArch64GISelActualAccessor();
Tom Stellardcef0fe42016-04-14 17:45:38 +0000275 GISel->CallLoweringInfo.reset(
Quentin Colombetc17f7442016-04-06 17:26:03 +0000276 new AArch64CallLowering(*I->getTargetLowering()));
Tim Northover69fa84a2016-10-14 22:18:18 +0000277 GISel->Legalizer.reset(new AArch64LegalizerInfo());
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000278
279 auto *RBI = new AArch64RegisterBankInfo(*I->getRegisterInfo());
280
281 // FIXME: At this point, we can't rely on Subtarget having RBI.
282 // It's awkward to mix passing RBI and the Subtarget; should we pass
283 // TII/TRI as well?
Tim Northoverbdf16242016-10-10 21:50:00 +0000284 GISel->InstSelector.reset(new AArch64InstructionSelector(*this, *I, *RBI));
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000285
286 GISel->RegBankInfo.reset(RBI);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000287#endif
Tom Stellardcef0fe42016-04-14 17:45:38 +0000288 I->setGISelAccessor(*GISel);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000289 }
290 return I.get();
291}
292
Tim Northover3b0846e2014-05-24 12:50:23 +0000293void AArch64leTargetMachine::anchor() { }
294
Daniel Sanders3e5de882015-06-11 19:41:26 +0000295AArch64leTargetMachine::AArch64leTargetMachine(
296 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000297 const TargetOptions &Options, Optional<Reloc::Model> RM,
298 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000299 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000300
301void AArch64beTargetMachine::anchor() { }
302
Daniel Sanders3e5de882015-06-11 19:41:26 +0000303AArch64beTargetMachine::AArch64beTargetMachine(
304 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000305 const TargetOptions &Options, Optional<Reloc::Model> RM,
306 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000307 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000308
309namespace {
Eugene Zelenko049b0172017-01-06 00:30:53 +0000310
Tim Northover3b0846e2014-05-24 12:50:23 +0000311/// AArch64 Code Generator Pass Configuration Options.
312class AArch64PassConfig : public TargetPassConfig {
313public:
314 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000315 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000316 if (TM->getOptLevel() != CodeGenOpt::None)
317 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000318 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000319
320 AArch64TargetMachine &getAArch64TargetMachine() const {
321 return getTM<AArch64TargetMachine>();
322 }
323
Matthias Braun115efcd2016-11-28 20:11:54 +0000324 ScheduleDAGInstrs *
325 createMachineScheduler(MachineSchedContext *C) const override {
326 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
327 DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
328 DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
Evandro Menezes94edf022017-02-01 02:54:34 +0000329 DAG->addMutation(createAArch64MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000330 return DAG;
331 }
332
Evandro Menezes455382e2017-02-01 02:54:42 +0000333 ScheduleDAGInstrs *
334 createPostMachineScheduler(MachineSchedContext *C) const override {
335 const AArch64Subtarget &ST = C->MF->getSubtarget<AArch64Subtarget>();
336 if (ST.hasFuseLiterals()) {
337 // Run the Macro Fusion after RA again since literals are expanded from
338 // pseudos then (v. addPreSched2()).
339 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
340 DAG->addMutation(createAArch64MacroFusionDAGMutation());
341 return DAG;
342 }
343
344 return nullptr;
345 }
346
Tim Northoverb4ddc082014-05-30 10:09:59 +0000347 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000348 bool addPreISel() override;
349 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000350#ifdef LLVM_BUILD_GLOBAL_ISEL
351 bool addIRTranslator() override;
Tim Northover33b07d62016-07-22 20:03:43 +0000352 bool addLegalizeMachineIR() override;
Quentin Colombetd4131812016-04-07 20:27:33 +0000353 bool addRegBankSelect() override;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000354 bool addGlobalInstructionSelect() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000355#endif
Tim Northover3b0846e2014-05-24 12:50:23 +0000356 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000357 void addPreRegAlloc() override;
358 void addPostRegAlloc() override;
359 void addPreSched2() override;
360 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000361};
Eugene Zelenko049b0172017-01-06 00:30:53 +0000362
363} // end anonymous namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000364
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000365TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000366 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000367 return TargetTransformInfo(AArch64TTIImpl(this, F));
368 });
Tim Northover3b0846e2014-05-24 12:50:23 +0000369}
370
371TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
372 return new AArch64PassConfig(this, PM);
373}
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.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000378 addPass(createAtomicExpandPass(TM));
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)
384 addPass(createCFGSimplificationPass());
385
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.
390 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoopDataPrefetch)
391 addPass(createLoopDataPrefetchPass());
392
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)
397 addPass(createInterleavedAccessPass(TM));
398
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.
403 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
404 // 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
Diana Picus850043b2016-08-01 05:56:57 +0000430 if (TM->getOptLevel() != CodeGenOpt::None && EnableAddressTypePromotion)
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000431 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000432
Tim Northover3b0846e2014-05-24 12:50:23 +0000433 return false;
434}
435
436bool AArch64PassConfig::addInstSelector() {
437 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
438
439 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
440 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000441 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000442 getOptLevel() != CodeGenOpt::None)
443 addPass(createAArch64CleanupLocalDynamicTLSPass());
444
445 return false;
446}
447
Quentin Colombetd96f4952016-02-11 19:35:06 +0000448#ifdef LLVM_BUILD_GLOBAL_ISEL
449bool AArch64PassConfig::addIRTranslator() {
450 addPass(new IRTranslator());
451 return false;
452}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000453
Tim Northover33b07d62016-07-22 20:03:43 +0000454bool AArch64PassConfig::addLegalizeMachineIR() {
Tim Northover69fa84a2016-10-14 22:18:18 +0000455 addPass(new Legalizer());
Tim Northover33b07d62016-07-22 20:03:43 +0000456 return false;
457}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000458
Quentin Colombetd4131812016-04-07 20:27:33 +0000459bool AArch64PassConfig::addRegBankSelect() {
460 addPass(new RegBankSelect());
461 return false;
462}
Eugene Zelenko049b0172017-01-06 00:30:53 +0000463
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000464bool AArch64PassConfig::addGlobalInstructionSelect() {
465 addPass(new InstructionSelect());
466 return false;
467}
Quentin Colombetd96f4952016-02-11 19:35:06 +0000468#endif
469
Tim Northover3b0846e2014-05-24 12:50:23 +0000470bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000471 if (EnableCondOpt)
472 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000473 if (EnableCCMP)
474 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000475 if (EnableMCR)
476 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000477 if (EnableEarlyIfConversion)
478 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000479 if (EnableStPairSuppress)
480 addPass(createAArch64StorePairSuppressPass());
Sebastian Popeb65d722016-10-08 12:30:07 +0000481 addPass(createAArch64VectorByElementOptPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000482 return true;
483}
484
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000485void AArch64PassConfig::addPreRegAlloc() {
Matthias Braun3d51cf02016-11-16 03:38:27 +0000486 // Change dead register definitions to refer to the zero register.
487 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
488 addPass(createAArch64DeadRegisterDefinitions());
489
Tim Northover3b0846e2014-05-24 12:50:23 +0000490 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000491 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000492 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000493 // The AdvSIMD pass may produce copies that can be rewritten to
494 // be register coaleascer friendly.
495 addPass(&PeepholeOptimizerID);
496 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000497}
498
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000499void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000500 // Remove redundant copy instructions.
501 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
502 addPass(createAArch64RedundantCopyEliminationPass());
503
Eric Christopher6f1e5682015-03-03 23:22:40 +0000504 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000505 // Improve performance for some FP/SIMD code for A57.
506 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000507}
508
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000509void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000510 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000511 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000512 // Use load/store pair instructions when possible.
513 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
514 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000515}
516
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000517void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000518 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000519 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000520 // Relax conditional branch instructions if they're otherwise out of
521 // range of their destination.
Diana Picus850043b2016-08-01 05:56:57 +0000522 if (BranchRelaxation)
Matt Arsenault36919a42016-10-06 15:38:53 +0000523 addPass(&BranchRelaxationPassID);
524
Tim Northover3b0846e2014-05-24 12:50:23 +0000525 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000526 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000527 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000528}