blob: cdc469c7b86cae7a6dea01f9dc41a663a3b7dd14 [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"
Tim Northover3b0846e2014-05-24 12:50:23 +000025#include "llvm/CodeGen/Passes.h"
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000026#include "llvm/CodeGen/RegAllocRegistry.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000027#include "llvm/CodeGen/TargetPassConfig.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000028#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Quentin Colombetf574ab22016-03-08 01:45:36 +000030#include "llvm/InitializePasses.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/TargetRegistry.h"
33#include "llvm/Target/TargetOptions.h"
34#include "llvm/Transforms/Scalar.h"
35using namespace llvm;
36
Diana Picus850043b2016-08-01 05:56:57 +000037static cl::opt<bool> EnableCCMP("aarch64-enable-ccmp",
38 cl::desc("Enable the CCMP formation pass"),
39 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000040
Diana Picus850043b2016-08-01 05:56:57 +000041static cl::opt<bool> EnableMCR("aarch64-enable-mcr",
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +000042 cl::desc("Enable the machine combiner pass"),
43 cl::init(true), cl::Hidden);
44
Diana Picus850043b2016-08-01 05:56:57 +000045static cl::opt<bool> EnableStPairSuppress("aarch64-enable-stp-suppress",
46 cl::desc("Suppress STP for AArch64"),
47 cl::init(true), cl::Hidden);
48
49static cl::opt<bool> EnableAdvSIMDScalar(
50 "aarch64-enable-simd-scalar",
51 cl::desc("Enable use of AdvSIMD scalar integer instructions"),
52 cl::init(false), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000053
54static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000055 EnablePromoteConstant("aarch64-enable-promote-const",
56 cl::desc("Enable the promote constant pass"),
57 cl::init(true), cl::Hidden);
58
59static cl::opt<bool> EnableCollectLOH(
60 "aarch64-enable-collect-loh",
61 cl::desc("Enable the pass that emits the linker optimization hints (LOH)"),
62 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000063
64static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000065 EnableDeadRegisterElimination("aarch64-enable-dead-defs", cl::Hidden,
66 cl::desc("Enable the pass that removes dead"
67 " definitons and replaces stores to"
68 " them with stores to the zero"
69 " register"),
70 cl::init(true));
Tim Northover3b0846e2014-05-24 12:50:23 +000071
Diana Picus850043b2016-08-01 05:56:57 +000072static cl::opt<bool> EnableRedundantCopyElimination(
73 "aarch64-enable-copyelim",
74 cl::desc("Enable the redundant copy elimination pass"), cl::init(true),
75 cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000076
Diana Picus850043b2016-08-01 05:56:57 +000077static cl::opt<bool> EnableLoadStoreOpt("aarch64-enable-ldst-opt",
78 cl::desc("Enable the load/store pair"
79 " optimization pass"),
80 cl::init(true), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000081
Diana Picus850043b2016-08-01 05:56:57 +000082static cl::opt<bool> EnableAtomicTidy(
83 "aarch64-enable-atomic-cfg-tidy", cl::Hidden,
84 cl::desc("Run SimplifyCFG after expanding atomic operations"
85 " to make use of cmpxchg flow-based information"),
86 cl::init(true));
Tim Northoverb4ddc082014-05-30 10:09:59 +000087
James Molloy99917942014-08-06 13:31:32 +000088static cl::opt<bool>
89EnableEarlyIfConversion("aarch64-enable-early-ifcvt", cl::Hidden,
90 cl::desc("Run early if-conversion"),
91 cl::init(true));
92
Jiangning Liu1a486da2014-09-05 02:55:24 +000093static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +000094 EnableCondOpt("aarch64-enable-condopt",
95 cl::desc("Enable the condition optimizer pass"),
96 cl::init(true), cl::Hidden);
Jiangning Liu1a486da2014-09-05 02:55:24 +000097
Arnaud A. de Grandmaisonc75dbbb2014-09-10 14:06:10 +000098static cl::opt<bool>
Bradley Smithf2a801d2014-10-13 10:12:35 +000099EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
100 cl::desc("Work around Cortex-A53 erratum 835769"),
101 cl::init(false));
102
Hao Liufd46bea2014-11-19 06:39:53 +0000103static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000104 EnableAddressTypePromotion("aarch64-enable-type-promotion", cl::Hidden,
105 cl::desc("Enable the type promotion pass"),
106 cl::init(true));
107
108static cl::opt<bool>
109 EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
110 cl::desc("Enable optimizations on complex GEPs"),
111 cl::init(false));
112
113static cl::opt<bool>
114 BranchRelaxation("aarch64-enable-branch-relax", cl::Hidden, cl::init(true),
115 cl::desc("Relax out of range conditional branches"));
Hao Liufd46bea2014-11-19 06:39:53 +0000116
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000117// FIXME: Unify control over GlobalMerge.
118static cl::opt<cl::boolOrDefault>
Diana Picus850043b2016-08-01 05:56:57 +0000119 EnableGlobalMerge("aarch64-enable-global-merge", cl::Hidden,
120 cl::desc("Enable the global merge pass"));
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000121
Adam Nemet53e758f2016-03-18 00:27:29 +0000122static cl::opt<bool>
Diana Picus850043b2016-08-01 05:56:57 +0000123 EnableLoopDataPrefetch("aarch64-enable-loop-data-prefetch", cl::Hidden,
Adam Nemet53e758f2016-03-18 00:27:29 +0000124 cl::desc("Enable the loop data prefetch pass"),
Adam Nemetfb8fbba52016-03-30 00:21:29 +0000125 cl::init(true));
Adam Nemet53e758f2016-03-18 00:27:29 +0000126
Tim Northover3b0846e2014-05-24 12:50:23 +0000127extern "C" void LLVMInitializeAArch64Target() {
128 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +0000129 RegisterTargetMachine<AArch64leTargetMachine> X(getTheAArch64leTarget());
130 RegisterTargetMachine<AArch64beTargetMachine> Y(getTheAArch64beTarget());
131 RegisterTargetMachine<AArch64leTargetMachine> Z(getTheARM64Target());
Tim Northover5dad9df2016-04-01 23:14:52 +0000132 auto PR = PassRegistry::getPassRegistry();
133 initializeGlobalISel(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000134 initializeAArch64A53Fix835769Pass(*PR);
135 initializeAArch64A57FPLoadBalancingPass(*PR);
136 initializeAArch64AddressTypePromotionPass(*PR);
137 initializeAArch64AdvSIMDScalarPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000138 initializeAArch64CollectLOHPass(*PR);
139 initializeAArch64ConditionalComparesPass(*PR);
140 initializeAArch64ConditionOptimizerPass(*PR);
141 initializeAArch64DeadRegisterDefinitionsPass(*PR);
Tim Northover5dad9df2016-04-01 23:14:52 +0000142 initializeAArch64ExpandPseudoPass(*PR);
Geoff Berry24c81e82016-07-20 21:45:58 +0000143 initializeAArch64LoadStoreOptPass(*PR);
Sebastian Popeb65d722016-10-08 12:30:07 +0000144 initializeAArch64VectorByElementOptPass(*PR);
Diana Picus850043b2016-08-01 05:56:57 +0000145 initializeAArch64PromoteConstantPass(*PR);
146 initializeAArch64RedundantCopyEliminationPass(*PR);
147 initializeAArch64StorePairSuppressPass(*PR);
148 initializeLDTLSCleanupPass(*PR);
Tim Northover3b0846e2014-05-24 12:50:23 +0000149}
150
Aditya Nandakumara2719322014-11-13 09:26:31 +0000151//===----------------------------------------------------------------------===//
152// AArch64 Lowering public interface.
153//===----------------------------------------------------------------------===//
154static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
155 if (TT.isOSBinFormatMachO())
156 return make_unique<AArch64_MachoTargetObjectFile>();
157
158 return make_unique<AArch64_ELFTargetObjectFile>();
159}
160
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000161// Helper function to build a DataLayout string
Joel Jones504bf332016-10-24 13:37:13 +0000162static std::string computeDataLayout(const Triple &TT,
163 const MCTargetOptions &Options,
164 bool LittleEndian) {
165 if (Options.getABIName() == "ilp32")
166 return "e-m:e-p:32:32-i8:8-i16:16-i64:64-S128";
Daniel Sandersed64d622015-06-11 15:34:59 +0000167 if (TT.isOSBinFormatMachO())
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000168 return "e-m:o-i64:64-i128:128-n32:64-S128";
169 if (LittleEndian)
Chad Rosier112d0e92016-07-07 20:02:18 +0000170 return "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
171 return "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128";
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000172}
173
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000174static Reloc::Model getEffectiveRelocModel(const Triple &TT,
175 Optional<Reloc::Model> RM) {
176 // AArch64 Darwin is always PIC.
177 if (TT.isOSDarwin())
178 return Reloc::PIC_;
179 // On ELF platforms the default static relocation model has a smart enough
180 // linker to cope with referencing external symbols defined in a shared
181 // library. Hence DynamicNoPIC doesn't need to be promoted to PIC.
182 if (!RM.hasValue() || *RM == Reloc::DynamicNoPIC)
183 return Reloc::Static;
184 return *RM;
185}
186
Rafael Espindola38af4d62016-05-18 16:00:24 +0000187/// Create an AArch64 architecture model.
Tim Northover3b0846e2014-05-24 12:50:23 +0000188///
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000189AArch64TargetMachine::AArch64TargetMachine(
190 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
191 const TargetOptions &Options, Optional<Reloc::Model> RM,
192 CodeModel::Model CM, CodeGenOpt::Level OL, bool LittleEndian)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000193 // This nested ternary is horrible, but DL needs to be properly
Eric Christopher63ea0402015-03-12 18:23:01 +0000194 // initialized before TLInfo is constructed.
Joel Jones504bf332016-10-24 13:37:13 +0000195 : LLVMTargetMachine(T, computeDataLayout(TT, Options.MCOptions,
196 LittleEndian),
197 TT, CPU, FS, Options,
198 getEffectiveRelocModel(TT, RM), CM, OL),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000199 TLOF(createTLOF(getTargetTriple())),
Evandro Menezesba4926e2016-09-20 19:02:06 +0000200 isLittle(LittleEndian) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000201 initAsmInfo();
202}
203
Reid Kleckner357600e2014-11-20 23:37:18 +0000204AArch64TargetMachine::~AArch64TargetMachine() {}
205
Quentin Colombetc17f7442016-04-06 17:26:03 +0000206#ifdef LLVM_BUILD_GLOBAL_ISEL
207namespace {
Tom Stellardcef0fe42016-04-14 17:45:38 +0000208struct AArch64GISelActualAccessor : public GISelAccessor {
Quentin Colombetc17f7442016-04-06 17:26:03 +0000209 std::unique_ptr<CallLowering> CallLoweringInfo;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000210 std::unique_ptr<InstructionSelector> InstSelector;
Tim Northover69fa84a2016-10-14 22:18:18 +0000211 std::unique_ptr<LegalizerInfo> Legalizer;
Quentin Colombetc17f7442016-04-06 17:26:03 +0000212 std::unique_ptr<RegisterBankInfo> RegBankInfo;
213 const CallLowering *getCallLowering() const override {
214 return CallLoweringInfo.get();
215 }
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000216 const InstructionSelector *getInstructionSelector() const override {
217 return InstSelector.get();
218 }
Tim Northover69fa84a2016-10-14 22:18:18 +0000219 const class LegalizerInfo *getLegalizerInfo() const override {
Chandler Carruth488cb132016-07-23 07:50:05 +0000220 return Legalizer.get();
Tim Northover33b07d62016-07-22 20:03:43 +0000221 }
Quentin Colombetc17f7442016-04-06 17:26:03 +0000222 const RegisterBankInfo *getRegBankInfo() const override {
223 return RegBankInfo.get();
224 }
225};
226} // End anonymous namespace.
227#endif
228
Eric Christopher3faf2f12014-10-06 06:45:36 +0000229const AArch64Subtarget *
230AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith003bb7d2015-02-14 02:09:06 +0000231 Attribute CPUAttr = F.getFnAttribute("target-cpu");
232 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000233
234 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
235 ? CPUAttr.getValueAsString().str()
236 : TargetCPU;
237 std::string FS = !FSAttr.hasAttribute(Attribute::None)
238 ? FSAttr.getValueAsString().str()
239 : TargetFS;
240
241 auto &I = SubtargetMap[CPU + FS];
242 if (!I) {
243 // This needs to be done before we create a new subtarget since any
244 // creation will depend on the TM and the code generation flags on the
245 // function that reside in TargetOptions.
246 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000247 I = llvm::make_unique<AArch64Subtarget>(TargetTriple, CPU, FS, *this,
Evandro Menezesba4926e2016-09-20 19:02:06 +0000248 isLittle);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000249#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picusbda72762016-11-14 10:25:43 +0000250 GISelAccessor *GISel = new GISelAccessor();
Quentin Colombetc17f7442016-04-06 17:26:03 +0000251#else
Tom Stellardcef0fe42016-04-14 17:45:38 +0000252 AArch64GISelActualAccessor *GISel =
Quentin Colombetc17f7442016-04-06 17:26:03 +0000253 new AArch64GISelActualAccessor();
Tom Stellardcef0fe42016-04-14 17:45:38 +0000254 GISel->CallLoweringInfo.reset(
Quentin Colombetc17f7442016-04-06 17:26:03 +0000255 new AArch64CallLowering(*I->getTargetLowering()));
Tim Northover69fa84a2016-10-14 22:18:18 +0000256 GISel->Legalizer.reset(new AArch64LegalizerInfo());
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000257
258 auto *RBI = new AArch64RegisterBankInfo(*I->getRegisterInfo());
259
260 // FIXME: At this point, we can't rely on Subtarget having RBI.
261 // It's awkward to mix passing RBI and the Subtarget; should we pass
262 // TII/TRI as well?
Tim Northoverbdf16242016-10-10 21:50:00 +0000263 GISel->InstSelector.reset(new AArch64InstructionSelector(*this, *I, *RBI));
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000264
265 GISel->RegBankInfo.reset(RBI);
Quentin Colombetc17f7442016-04-06 17:26:03 +0000266#endif
Tom Stellardcef0fe42016-04-14 17:45:38 +0000267 I->setGISelAccessor(*GISel);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000268 }
269 return I.get();
270}
271
Tim Northover3b0846e2014-05-24 12:50:23 +0000272void AArch64leTargetMachine::anchor() { }
273
Daniel Sanders3e5de882015-06-11 19:41:26 +0000274AArch64leTargetMachine::AArch64leTargetMachine(
275 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000276 const TargetOptions &Options, Optional<Reloc::Model> RM,
277 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000278 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000279
280void AArch64beTargetMachine::anchor() { }
281
Daniel Sanders3e5de882015-06-11 19:41:26 +0000282AArch64beTargetMachine::AArch64beTargetMachine(
283 const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000284 const TargetOptions &Options, Optional<Reloc::Model> RM,
285 CodeModel::Model CM, CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000286 : AArch64TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Tim Northover3b0846e2014-05-24 12:50:23 +0000287
288namespace {
289/// AArch64 Code Generator Pass Configuration Options.
290class AArch64PassConfig : public TargetPassConfig {
291public:
292 AArch64PassConfig(AArch64TargetMachine *TM, PassManagerBase &PM)
Chad Rosier486e0872014-09-12 17:40:39 +0000293 : TargetPassConfig(TM, PM) {
Chad Rosier347ed4e2014-09-12 22:17:28 +0000294 if (TM->getOptLevel() != CodeGenOpt::None)
295 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
Chad Rosier486e0872014-09-12 17:40:39 +0000296 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000297
298 AArch64TargetMachine &getAArch64TargetMachine() const {
299 return getTM<AArch64TargetMachine>();
300 }
301
Tim Northoverb4ddc082014-05-30 10:09:59 +0000302 void addIRPasses() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000303 bool addPreISel() override;
304 bool addInstSelector() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000305#ifdef LLVM_BUILD_GLOBAL_ISEL
306 bool addIRTranslator() override;
Tim Northover33b07d62016-07-22 20:03:43 +0000307 bool addLegalizeMachineIR() override;
Quentin Colombetd4131812016-04-07 20:27:33 +0000308 bool addRegBankSelect() override;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000309 bool addGlobalInstructionSelect() override;
Quentin Colombetd96f4952016-02-11 19:35:06 +0000310#endif
Tim Northover3b0846e2014-05-24 12:50:23 +0000311 bool addILPOpts() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000312 void addPreRegAlloc() override;
313 void addPostRegAlloc() override;
314 void addPreSched2() override;
315 void addPreEmitPass() override;
Tim Northover3b0846e2014-05-24 12:50:23 +0000316};
317} // namespace
318
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000319TargetIRAnalysis AArch64TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000320 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000321 return TargetTransformInfo(AArch64TTIImpl(this, F));
322 });
Tim Northover3b0846e2014-05-24 12:50:23 +0000323}
324
325TargetPassConfig *AArch64TargetMachine::createPassConfig(PassManagerBase &PM) {
326 return new AArch64PassConfig(this, PM);
327}
328
Tim Northoverb4ddc082014-05-30 10:09:59 +0000329void AArch64PassConfig::addIRPasses() {
330 // Always expand atomic operations, we don't deal with atomicrmw or cmpxchg
331 // ourselves.
Robin Morisset59c23cd2014-08-21 21:50:01 +0000332 addPass(createAtomicExpandPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000333
334 // Cmpxchg instructions are often used with a subsequent comparison to
335 // determine whether it succeeded. We can exploit existing control-flow in
336 // ldrex/strex loops to simplify this, but it needs tidying up.
337 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
338 addPass(createCFGSimplificationPass());
339
Junmo Park384d3762016-07-06 23:18:58 +0000340 // Run LoopDataPrefetch
Adam Nemet53e758f2016-03-18 00:27:29 +0000341 //
342 // Run this before LSR to remove the multiplies involved in computing the
343 // pointer values N iterations ahead.
344 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoopDataPrefetch)
345 addPass(createLoopDataPrefetchPass());
346
Tim Northoverb4ddc082014-05-30 10:09:59 +0000347 TargetPassConfig::addIRPasses();
Hao Liufd46bea2014-11-19 06:39:53 +0000348
Hao Liu7ec8ee32015-06-26 02:32:07 +0000349 // Match interleaved memory accesses to ldN/stN intrinsics.
350 if (TM->getOptLevel() != CodeGenOpt::None)
351 addPass(createInterleavedAccessPass(TM));
352
Hao Liufd46bea2014-11-19 06:39:53 +0000353 if (TM->getOptLevel() == CodeGenOpt::Aggressive && EnableGEPOpt) {
354 // Call SeparateConstOffsetFromGEP pass to extract constants within indices
355 // and lower a GEP with multiple indices to either arithmetic operations or
356 // multiple GEPs with single index.
357 addPass(createSeparateConstOffsetFromGEPPass(TM, true));
358 // Call EarlyCSE pass to find and remove subexpressions in the lowered
359 // result.
360 addPass(createEarlyCSEPass());
361 // Do loop invariant code motion in case part of the lowered result is
362 // invariant.
363 addPass(createLICMPass());
364 }
Tim Northoverb4ddc082014-05-30 10:09:59 +0000365}
366
Tim Northover3b0846e2014-05-24 12:50:23 +0000367// Pass Pipeline Configuration
368bool AArch64PassConfig::addPreISel() {
369 // Run promote constant before global merge, so that the promoted constants
370 // get a chance to be merged
371 if (TM->getOptLevel() != CodeGenOpt::None && EnablePromoteConstant)
372 addPass(createAArch64PromoteConstantPass());
Eric Christophered47b222015-02-23 19:28:45 +0000373 // FIXME: On AArch64, this depends on the type.
374 // Basically, the addressable offsets are up to 4095 * Ty.getSizeInBytes().
375 // and the offset has to be a multiple of the related size in bytes.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000376 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000377 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000378 EnableGlobalMerge == cl::BOU_TRUE) {
379 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
380 (EnableGlobalMerge == cl::BOU_UNSET);
381 addPass(createGlobalMergePass(TM, 4095, OnlyOptimizeForSize));
382 }
383
Diana Picus850043b2016-08-01 05:56:57 +0000384 if (TM->getOptLevel() != CodeGenOpt::None && EnableAddressTypePromotion)
Duncan P. N. Exon Smithde588702014-07-02 18:17:40 +0000385 addPass(createAArch64AddressTypePromotionPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000386
Tim Northover3b0846e2014-05-24 12:50:23 +0000387 return false;
388}
389
390bool AArch64PassConfig::addInstSelector() {
391 addPass(createAArch64ISelDag(getAArch64TargetMachine(), getOptLevel()));
392
393 // For ELF, cleanup any local-dynamic TLS accesses (i.e. combine as many
394 // references to _TLS_MODULE_BASE_ as possible.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000395 if (TM->getTargetTriple().isOSBinFormatELF() &&
Tim Northover3b0846e2014-05-24 12:50:23 +0000396 getOptLevel() != CodeGenOpt::None)
397 addPass(createAArch64CleanupLocalDynamicTLSPass());
398
399 return false;
400}
401
Quentin Colombetd96f4952016-02-11 19:35:06 +0000402#ifdef LLVM_BUILD_GLOBAL_ISEL
403bool AArch64PassConfig::addIRTranslator() {
404 addPass(new IRTranslator());
405 return false;
406}
Tim Northover33b07d62016-07-22 20:03:43 +0000407bool AArch64PassConfig::addLegalizeMachineIR() {
Tim Northover69fa84a2016-10-14 22:18:18 +0000408 addPass(new Legalizer());
Tim Northover33b07d62016-07-22 20:03:43 +0000409 return false;
410}
Quentin Colombetd4131812016-04-07 20:27:33 +0000411bool AArch64PassConfig::addRegBankSelect() {
412 addPass(new RegBankSelect());
413 return false;
414}
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000415bool AArch64PassConfig::addGlobalInstructionSelect() {
416 addPass(new InstructionSelect());
417 return false;
418}
Quentin Colombetd96f4952016-02-11 19:35:06 +0000419#endif
420
Tim Northover3b0846e2014-05-24 12:50:23 +0000421bool AArch64PassConfig::addILPOpts() {
Jiangning Liu1a486da2014-09-05 02:55:24 +0000422 if (EnableCondOpt)
423 addPass(createAArch64ConditionOptimizerPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000424 if (EnableCCMP)
425 addPass(createAArch64ConditionalCompares());
Gerolf Hoflehner97c383b2014-08-07 21:40:58 +0000426 if (EnableMCR)
427 addPass(&MachineCombinerID);
James Molloy99917942014-08-06 13:31:32 +0000428 if (EnableEarlyIfConversion)
429 addPass(&EarlyIfConverterID);
Tim Northover3b0846e2014-05-24 12:50:23 +0000430 if (EnableStPairSuppress)
431 addPass(createAArch64StorePairSuppressPass());
Sebastian Popeb65d722016-10-08 12:30:07 +0000432 addPass(createAArch64VectorByElementOptPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000433 return true;
434}
435
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000436void AArch64PassConfig::addPreRegAlloc() {
Matthias Braun3d51cf02016-11-16 03:38:27 +0000437 // Change dead register definitions to refer to the zero register.
438 if (TM->getOptLevel() != CodeGenOpt::None && EnableDeadRegisterElimination)
439 addPass(createAArch64DeadRegisterDefinitions());
440
Tim Northover3b0846e2014-05-24 12:50:23 +0000441 // Use AdvSIMD scalar instructions whenever profitable.
Quentin Colombet0c740d42014-08-21 18:10:07 +0000442 if (TM->getOptLevel() != CodeGenOpt::None && EnableAdvSIMDScalar) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000443 addPass(createAArch64AdvSIMDScalar());
Quentin Colombet0c740d42014-08-21 18:10:07 +0000444 // The AdvSIMD pass may produce copies that can be rewritten to
445 // be register coaleascer friendly.
446 addPass(&PeepholeOptimizerID);
447 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000448}
449
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000450void AArch64PassConfig::addPostRegAlloc() {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000451 // Remove redundant copy instructions.
452 if (TM->getOptLevel() != CodeGenOpt::None && EnableRedundantCopyElimination)
453 addPass(createAArch64RedundantCopyEliminationPass());
454
Eric Christopher6f1e5682015-03-03 23:22:40 +0000455 if (TM->getOptLevel() != CodeGenOpt::None && usingDefaultRegAlloc())
James Molloy3feea9c2014-08-08 12:33:21 +0000456 // Improve performance for some FP/SIMD code for A57.
457 addPass(createAArch64A57FPLoadBalancing());
Tim Northover3b0846e2014-05-24 12:50:23 +0000458}
459
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000460void AArch64PassConfig::addPreSched2() {
Tim Northover3b0846e2014-05-24 12:50:23 +0000461 // Expand some pseudo instructions to allow proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000462 addPass(createAArch64ExpandPseudoPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000463 // Use load/store pair instructions when possible.
464 if (TM->getOptLevel() != CodeGenOpt::None && EnableLoadStoreOpt)
465 addPass(createAArch64LoadStoreOptimizationPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000466}
467
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000468void AArch64PassConfig::addPreEmitPass() {
Bradley Smithf2a801d2014-10-13 10:12:35 +0000469 if (EnableA53Fix835769)
Matthias Braunb2f23882014-12-11 23:18:03 +0000470 addPass(createAArch64A53Fix835769());
Tim Northover3b0846e2014-05-24 12:50:23 +0000471 // Relax conditional branch instructions if they're otherwise out of
472 // range of their destination.
Diana Picus850043b2016-08-01 05:56:57 +0000473 if (BranchRelaxation)
Matt Arsenault36919a42016-10-06 15:38:53 +0000474 addPass(&BranchRelaxationPassID);
475
Tim Northover3b0846e2014-05-24 12:50:23 +0000476 if (TM->getOptLevel() != CodeGenOpt::None && EnableCollectLOH &&
Daniel Sandersc81f4502015-06-16 15:44:21 +0000477 TM->getTargetTriple().isOSBinFormatMachO())
Tim Northover3b0846e2014-05-24 12:50:23 +0000478 addPass(createAArch64CollectLOHPass());
Tim Northover3b0846e2014-05-24 12:50:23 +0000479}