blob: b84abc795d9a175fa2b274b72f287eff84e47f08 [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"
Quentin Colombetc17f7442016-04-06 17:26:03 +000017#include "AArch64RegisterBankInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000018#include "AArch64TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000019#include "AArch64TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000020#include "AArch64TargetTransformInfo.h"
Quentin Colombet846219a2016-04-07 21:24:40 +000021#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000022#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Tim Northover69fa84a2016-10-14 22:18:18 +000023#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Quentin Colombetd4131812016-04-07 20:27:33 +000024#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000025#include "llvm/CodeGen/MachineScheduler.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000026#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000027#include "llvm/CodeGen/RegAllocRegistry.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000028#include "llvm/CodeGen/TargetPassConfig.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000029#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000030#include "llvm/IR/LegacyPassManager.h"
Quentin Colombetf574ab22016-03-08 01:45:36 +000031#include "llvm/InitializePasses.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/TargetRegistry.h"
34#include "llvm/Target/TargetOptions.h"
35#include "llvm/Transforms/Scalar.h"
36using namespace llvm;
37
Diana Picus850043b2016-08-01 05:56:57 +000038static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
39 cl::desc("Enable the CCMP formation pass"),
40 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000041
Diana Picus850043b2016-08-01 05:56:57 +000042static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000043 cl::desc("Enable the machine combiner pass"),
44 cl::init(true), cl::Hidden);
45
Diana Picus850043b2016-08-01 05:56:57 +000046static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
47 cl::desc("Suppress STP for AArch64"),
48 cl::init(true), cl::Hidden);
49
50static cl::opt<bool> EnableAdvSIMDScalar(
51 "aarch64-enable-simd-scalar",
52 cl::desc("Enable use of AdvSIMD scalar integer instructions"),
53 cl::init(false), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000054
55static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000056 EnablePromoteConstant("aarch64-enable-promote-const",
57 cl::desc("Enable the promote constant pass"),
58 cl::init(true), cl::Hidden);
59
60static cl::opt<bool> EnableCollectLOH(
61 "aarch64-enable-collect-loh",
62 cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
63 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000064
65static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000066 EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
67 cl::desc("Enable the pass that removes dead"
68 " definitons and replaces stores to"
69 " them with stores to the zero"
70 " register"),
71 cl::init(true));
Tim Northover3b0846e2014-05-24 12:50:23 +000072
Diana Picus850043b2016-08-01 05:56:57 +000073static cl::opt<bool> EnableRedundantCopyElimination(
74 "aarch64-enable-copyelim",
75 cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
76 cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000077
Diana Picus850043b2016-08-01 05:56:57 +000078static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
79 cl::desc("Enable the load/store pair"
80 " optimization pass"),
81 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000082
Diana Picus850043b2016-08-01 05:56:57 +000083static cl::opt<bool> EnableAtomicTidy(
84 "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
85 cl::desc("Run SimplifyCFG after expanding atomic operations"
86 " to make use of cmpxchg flow-based information"),
87 cl::init(true));
Tim Northoverb4ddc082014-05-30 10:09:59 +000088
James Molloy99917942014-08-06 13:31:32 +000089static cl::opt<bool>
90EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
91 cl::desc("Run early if-conversion"),
92 cl::init(true));
93
Jiangning Liu1a486da2014-09-05 02:55:24 +000094static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000095 EnableCondOpt("aarch64-enable-condopt",
96 cl::desc("Enable the condition optimizer pass"),
97 cl::init(true), cl::Hidden);
Jiangning Liu1a486da2014-09-05 02:55:24 +000098
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000099static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +0000100EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
101 cl::desc("Work around Cortex-A53 erratum 835769"),
102 cl::init(false));
103
Hao Liufd46bea2014-11-19 06:39:53 +0000104static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000105 EnableAddressTypePromotion("aarch64-enable-type-promotion", cl::Hidden,
106 cl::desc("Enable the type promotion pass"),
107 cl::init(true));
108
109static cl::opt<bool>
110 EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
111 cl::desc("Enable optimizations on complex GEPs"),
112 cl::init(false));
113
114static cl::opt<bool>
115 BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
116 cl::desc("Relax out of range conditional branches"));
Hao Liufd46bea2014-11-19 06:39:53 +0000117
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000118// FIXME: Unify control over GlobalMerge.
119static cl::opt<cl::boolOrDefault>
Diana Picus850043b2016-08-01 05:56:57 +0000120 EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
121 cl::desc("Enable the global merge pass"));
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000122
Adam Nemet53e758f2016-03-18 00:27:29 +0000123static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000124 EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
Adam Nemet53e758f2016-03-18 00:27:29 +0000125 cl::desc("Enable the loop data prefetch pass"),
Adam Nemetfb8fbba52016-03-30 00:21:29 +0000126 cl::init(true));
Adam Nemet53e758f2016-03-18 00:27:29 +0000127
Tim Northover3b0846e2014-05-24 12:50:23 +0000128extern "C" void LLVMInitializeAArch64Target() {
129 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +0000130 RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
131 RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
132 RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
Tim Northover5dad9df2016-04-01 23:14:52 +0000133 auto PR = PassRegistry::getPassRegistry();
134 initializeGlobalISel(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000135 initializeAArch64A53Fix835769Pass(*PR);
136 initializeAArch64A57FPLoadBalancingPass(*PR);
137 initializeAArch64AddressTypePromotionPass(*PR);
138 initializeAArch64AdvSIMDScalarPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000139 initializeAArch64CollectLOHPass(*PR);
140 initializeAArch64ConditionalComparesPass(*PR);
141 initializeAArch64ConditionOptimizerPass(*PR);
142 initializeAArch64DeadRegisterDefinitionsPass(*PR);
Tim Northover5dad9df2016-04-01 23:14:52 +0000143 initializeAArch64ExpandPseudoPass(*PR);
Geoff Berry24c81e82016-07-20 21:45:58 +0000144 initializeAArch64LoadStoreOptPass(*PR);
Sebastian Popeb65d722016-10-08 12:30:07 +0000145 initializeAArch64VectorByElementOptPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000146 initializeAArch64PromoteConstantPass(*PR);
147 initializeAArch64RedundantCopyEliminationPass(*PR);
148 initializeAArch64StorePairSuppressPass(*PR);
149 initializeLDTLSCleanupPass(*PR);
Tim Northover3b0846e2014-05-24 12:50:23 +0000150}
151
Aditya Nandakumara2719322014-11-13 09:26:31 +0000152//===----------------------------------------------------------------------===//
153// AArch64 Lowering public interface.
154//===----------------------------------------------------------------------===//
155static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
156 if (TT.isOSBinFormatMachO())
157 return make_unique<AArch64_MachoTargetObjectFile>();
158
159 return make_unique<AArch64_ELFTargetObjectFile>();
160}
161
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000162// Helper function to build a DataLayout string
Joel Jones504bf332016-10-24 13:37:13 +0000163static std::string computeDataLayout(const Triple &TT,
164 const MCTargetOptions &Options,
165 bool LittleEndian) {
166 if (Options.getABIName() == "ilp32")
167 return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
Daniel Sandersed64d622015-06-11 15:34:59 +0000168 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000169 return "e-m:o-i64:64-i128:128-n32:64-S128";
170 if (LittleEndian)
Chad Rosier112d0e92016-07-07 20:02:18 +0000171 return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
172 return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000173}
174
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000175static Reloc::Model getEffectiveRelocModel(const Triple &TT,
176 Optional<Reloc::Model> RM) {
177 // AArch64 Darwin is always PIC.
178 if (TT.isOSDarwin())
179 return Reloc::PIC_;
180 // On ELF platforms the default static relocation model has a smart enough
181 // linker to cope with referencing external symbols defined in a shared
182 // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
183 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
184 return Reloc::Static;
185 return *RM;
186}
187
Rafael Espindola38af4d62016-05-18 16:00:24 +0000188/// Create an AArch64 architecture model.
Tim Northover3b0846e2014-05-24 12:50:23 +0000189///
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000190AArch64TargetMachine::AArch64TargetMachine(
191 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
192 const TargetOptions &Options, Optional<Reloc::Model> RM,
193 CodeModel::Model CM, CodeGenOpt::Level OL, bool LittleEndian)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000194 // This nested ternary is horrible, but DL needs to be properly
Eric Christopher63ea0402015-03-12 18:23:01 +0000195 // initialized before TLInfo is constructed.
Joel Jones504bf332016-10-24 13:37:13 +0000196 : LLVMTargetMachine(T, computeDataLayout(TT, Options.MCOptions,
197 LittleEndian),
198 TT, CPU, FS, Options,
199 getEffectiveRelocModel(TT, RM), CM, OL),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000200 TLOF(createTLOF(getTargetTriple())),
Evandro Menezesba4926e2016-09-20 19:02:06 +0000201 isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000202 initAsmInfo();
203}
204
Reid Kleckner357600e2014-11-20 23:37:18 +0000205AArch64TargetMachine::~AArch64TargetMachine() {}
206
Quentin Colombetc17f7442016-04-06 17:26:03 +0000207#ifdef LLVM_BUILD_GLOBAL_ISEL
208namespace {
Tom Stellardcef0fe42016-04-14 17:45:38 +0000209struct AArch64GISelActualAccessor : public GISelAccessor {
Quentin Colombetc17f7442016-04-06 17:26:03 +0000210 std::unique_ptr<CallLowering> CallLoweringInfo;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000211 std::unique_ptr<InstructionSelector> InstSelector;
Tim Northover69fa84a2016-10-14 22:18:18 +0000212 std::unique_ptr<LegalizerInfo> Legalizer;
Quentin Colombetc17f7442016-04-06 17:26:03 +0000213 std::unique_ptr<RegisterBankInfo> RegBankInfo;
214 const CallLowering *getCallLowering() const override {
215 return CallLoweringInfo.get();
216 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000217 const InstructionSelector *getInstructionSelector() const override {
218 return InstSelector.get();
219 }
Tim Northover69fa84a2016-10-14 22:18:18 +0000220 const class LegalizerInfo *getLegalizerInfo() const override {
Chandler Carruth488cb132016-07-23 07:50:05 +0000221 return Legalizer.get();
Tim Northover33b07d62016-07-22 20:03:43 +0000222 }
Quentin Colombetc17f7442016-04-06 17:26:03 +0000223 const RegisterBankInfo *getRegBankInfo() const override {
224 return RegBankInfo.get();
225 }
226};
227} // End anonymous namespace.
228#endif
229
Eric Christopher3faf2f12014-10-06 06:45:36 +0000230const AArch64Subtarget *
231AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000232 Attribute CPUAttr = F.getFnAttribute("target-cpu");
233 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000234
235 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
236 ? CPUAttr.getValueAsString().str()
237 : TargetCPU;
238 std::string FS = !FSAttr.hasAttribute(Attribute::None)
239 ? FSAttr.getValueAsString().str()
240 : TargetFS;
241
242 auto &I = SubtargetMap[CPU + FS];
243 if (!I) {
244 // This needs to be done before we create a new subtarget since any
245 // creation will depend on the TM and the code generation flags on the
246 // function that reside in TargetOptions.
247 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000248 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
Evandro Menezesba4926e2016-09-20 19:02:06 +0000249 isLittle);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000250#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picusbda72762016-11-14 10:25:43 +0000251 GISelAccessor *GISel = new GISelAccessor();
Quentin Colombetc17f7442016-04-06 17:26:03 +0000252#else
Tom Stellardcef0fe42016-04-14 17:45:38 +0000253 AArch64GISelActualAccessor *GISel =
Quentin Colombetc17f7442016-04-06 17:26:03 +0000254 new AArch64GISelActualAccessor();
Tom Stellardcef0fe42016-04-14 17:45:38 +0000255 GISel->CallLoweringInfo.reset(
Quentin Colombetc17f7442016-04-06 17:26:03 +0000256 new AArch64CallLowering(*I->getTargetLowering()));
Tim Northover69fa84a2016-10-14 22:18:18 +0000257 GISel->Legalizer.reset(new AArch64LegalizerInfo());
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000258
259 auto *RBI = new AArch64RegisterBankInfo(*I->getRegisterInfo());
260
261 // FIXME: At this point, we can't rely on Subtarget having RBI.
262 // It's awkward to mix passing RBI and the Subtarget; should we pass
263 // TII/TRI as well?
Tim Northoverbdf16242016-10-10 21:50:00 +0000264 GISel->InstSelector.reset(new AArch64InstructionSelector(*this, *I, *RBI));
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000265
266 GISel->RegBankInfo.reset(RBI);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000267#endif
Tom Stellardcef0fe42016-04-14 17:45:38 +0000268 I->setGISelAccessor(*GISel);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000269 }
270 return I.get();
271}
272
Tim Northover3b0846e2014-05-24 12:50:23 +0000273void AArch64leTargetMachine::anchor() { }
274
Daniel Sanders3e5de882015-06-11 19:41:26 +0000275AArch64leTargetMachine::AArch64leTargetMachine(
276 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000277 const TargetOptions &Options, Optional<Reloc::Model> RM,
278 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000279 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000280
281void AArch64beTargetMachine::anchor() { }
282
Daniel Sanders3e5de882015-06-11 19:41:26 +0000283AArch64beTargetMachine::AArch64beTargetMachine(
284 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000285 const TargetOptions &Options, Optional<Reloc::Model> RM,
286 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000287 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000288
289namespace {
290/// AArch64 Code Generator Pass Configuration Options.
291class AArch64PassConfig : public TargetPassConfig {
292public:
293 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000294 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000295 if (TM->getOptLevel() != CodeGenOpt::None)
296 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000297 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000298
299 AArch64TargetMachine &getAArch64TargetMachine() const {
300 return getTM<AArch64TargetMachine>();
301 }
302
Matthias Braun115efcd2016-11-28 20:11:54 +0000303 ScheduleDAGInstrs *
304 createMachineScheduler(MachineSchedContext *C) const override {
305 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
306 DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
307 DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
308 DAG->addMutation(createMacroFusionDAGMutation(DAG->TII));
309 return DAG;
310 }
311
Tim Northoverb4ddc082014-05-30 10:09:59 +0000312 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000313 bool addPreISel() override;
314 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000315#ifdef LLVM_BUILD_GLOBAL_ISEL
316 bool addIRTranslator() override;
Tim Northover33b07d62016-07-22 20:03:43 +0000317 bool addLegalizeMachineIR() override;
Quentin Colombetd4131812016-04-07 20:27:33 +0000318 bool addRegBankSelect() override;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000319 bool addGlobalInstructionSelect() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000320#endif
Tim Northover3b0846e2014-05-24 12:50:23 +0000321 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000322 void addPreRegAlloc() override;
323 void addPostRegAlloc() override;
324 void addPreSched2() override;
325 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000326};
327} // namespace
328
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000329TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000330 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000331 return TargetTransformInfo(AArch64TTIImpl(this, F));
332 });
Tim Northover3b0846e2014-05-24 12:50:23 +0000333}
334
335TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
336 return new AArch64PassConfig(this, PM);
337}
338
Tim Northoverb4ddc082014-05-30 10:09:59 +0000339void AArch64PassConfig::addIRPasses() {
340 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
341 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000342 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000343
344 // Cmpxchg instructions are often used with a subsequent comparison to
345 // determine whether it succeeded. We can exploit existing control-flow in
346 // ldrex/strex loops to simplify this, but it needs tidying up.
347 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
348 addPass(createCFGSimplificationPass());
349
Junmo Park384d3762016-07-06 23:18:58 +0000350 // Run LoopDataPrefetch
Adam Nemet53e758f2016-03-18 00:27:29 +0000351 //
352 // Run this before LSR to remove the multiplies involved in computing the
353 // pointer values N iterations ahead.
354 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoopDataPrefetch)
355 addPass(createLoopDataPrefetchPass());
356
Tim Northoverb4ddc082014-05-30 10:09:59 +0000357 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000358
Hao Liu7ec8ee32015-06-26 02:32:07 +0000359 // Match interleaved memory accesses to ldN/stN intrinsics.
360 if (TM->getOptLevel() != CodeGenOpt::None)
361 addPass(createInterleavedAccessPass(TM));
362
Hao Liufd46bea2014-11-19 06:39:53 +0000363 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
364 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
365 // and lower a GEP with multiple indices to either arithmetic operations or
366 // multiple GEPs with single index.
367 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
368 // Call EarlyCSE pass to find and remove subexpressions in the lowered
369 // result.
370 addPass(createEarlyCSEPass());
371 // Do loop invariant code motion in case part of the lowered result is
372 // invariant.
373 addPass(createLICMPass());
374 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000375}
376
Tim Northover3b0846e2014-05-24 12:50:23 +0000377// Pass Pipeline Configuration
378bool AArch64PassConfig::addPreISel() {
379 // Run promote constant before global merge, so that the promoted constants
380 // get a chance to be merged
381 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
382 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000383 // FIXME: On AArch64, this depends on the type.
384 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
385 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000386 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000387 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000388 EnableGlobalMerge == cl::BOU_TRUE) {
389 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
390 (EnableGlobalMerge == cl::BOU_UNSET);
391 addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
392 }
393
Diana Picus850043b2016-08-01 05:56:57 +0000394 if (TM->getOptLevel() != CodeGenOpt::None && EnableAddressTypePromotion)
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000395 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000396
Tim Northover3b0846e2014-05-24 12:50:23 +0000397 return false;
398}
399
400bool AArch64PassConfig::addInstSelector() {
401 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
402
403 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
404 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000405 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000406 getOptLevel() != CodeGenOpt::None)
407 addPass(createAArch64CleanupLocalDynamicTLSPass());
408
409 return false;
410}
411
Quentin Colombetd96f4952016-02-11 19:35:06 +0000412#ifdef LLVM_BUILD_GLOBAL_ISEL
413bool AArch64PassConfig::addIRTranslator() {
414 addPass(new IRTranslator());
415 return false;
416}
Tim Northover33b07d62016-07-22 20:03:43 +0000417bool AArch64PassConfig::addLegalizeMachineIR() {
Tim Northover69fa84a2016-10-14 22:18:18 +0000418 addPass(new Legalizer());
Tim Northover33b07d62016-07-22 20:03:43 +0000419 return false;
420}
Quentin Colombetd4131812016-04-07 20:27:33 +0000421bool AArch64PassConfig::addRegBankSelect() {
422 addPass(new RegBankSelect());
423 return false;
424}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000425bool AArch64PassConfig::addGlobalInstructionSelect() {
426 addPass(new InstructionSelect());
427 return false;
428}
Quentin Colombetd96f4952016-02-11 19:35:06 +0000429#endif
430
Tim Northover3b0846e2014-05-24 12:50:23 +0000431bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000432 if (EnableCondOpt)
433 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000434 if (EnableCCMP)
435 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000436 if (EnableMCR)
437 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000438 if (EnableEarlyIfConversion)
439 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000440 if (EnableStPairSuppress)
441 addPass(createAArch64StorePairSuppressPass());
Sebastian Popeb65d722016-10-08 12:30:07 +0000442 addPass(createAArch64VectorByElementOptPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000443 return true;
444}
445
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000446void AArch64PassConfig::addPreRegAlloc() {
Matthias Braun3d51cf02016-11-16 03:38:27 +0000447 // Change dead register definitions to refer to the zero register.
448 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
449 addPass(createAArch64DeadRegisterDefinitions());
450
Tim Northover3b0846e2014-05-24 12:50:23 +0000451 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000452 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000453 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000454 // The AdvSIMD pass may produce copies that can be rewritten to
455 // be register coaleascer friendly.
456 addPass(&PeepholeOptimizerID);
457 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000458}
459
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000460void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000461 // Remove redundant copy instructions.
462 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
463 addPass(createAArch64RedundantCopyEliminationPass());
464
Eric Christopher6f1e5682015-03-03 23:22:40 +0000465 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000466 // Improve performance for some FP/SIMD code for A57.
467 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000468}
469
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000470void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000471 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000472 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000473 // Use load/store pair instructions when possible.
474 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
475 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000476}
477
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000478void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000479 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000480 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000481 // Relax conditional branch instructions if they're otherwise out of
482 // range of their destination.
Diana Picus850043b2016-08-01 05:56:57 +0000483 if (BranchRelaxation)
Matt Arsenault36919a42016-10-06 15:38:53 +0000484 addPass(&BranchRelaxationPassID);
485
Tim Northover3b0846e2014-05-24 12:50:23 +0000486 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000487 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000488 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000489}