blob: 4eb244a13c158f022ccfd315fecc3f766a20f80c [file] [log] [blame]
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
Diana Picus22274932016-11-11 08:27:37 +000013#include "ARM.h"
14#include "ARMCallLowering.h"
Diana Picus22274932016-11-11 08:27:37 +000015#include "ARMLegalizerInfo.h"
Aaron Ballman32346472017-05-09 15:12:03 +000016#ifdef LLVM_BUILD_GLOBAL_ISEL
Diana Picus22274932016-11-11 08:27:37 +000017#include "ARMRegisterBankInfo.h"
Aaron Ballman06297e82017-05-09 14:59:48 +000018#endif
Eugene Zelenko342257e2017-01-31 00:56:17 +000019#include "ARMSubtarget.h"
Florian Hahnb489e562017-06-22 09:39:36 +000020#include "ARMMacroFusion.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000021#include "ARMTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000022#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000023#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000024#include "MCTargetDesc/ARMMCTargetDesc.h"
25#include "llvm/ADT/Optional.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/Triple.h"
29#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000030#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000031#include "llvm/CodeGen/GlobalISel/CallLowering.h"
32#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
Diana Picus22274932016-11-11 08:27:37 +000033#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
34#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000035#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Diana Picus22274932016-11-11 08:27:37 +000036#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000037#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000038#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000039#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
40#include "llvm/CodeGen/MachineFunction.h"
Javed Absar9e1ff862017-06-09 14:07:21 +000041#include "llvm/CodeGen/MachineScheduler.h"
Evan Chengad3aac712007-05-16 02:01:49 +000042#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000043#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000044#include "llvm/IR/Attributes.h"
45#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000046#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000047#include "llvm/Pass.h"
48#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000049#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000050#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000051#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000052#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000053#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng10043e22007-01-19 07:51:42 +000054#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000055#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000056#include <cassert>
57#include <memory>
58#include <string>
59
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000060using namespace llvm;
61
Evan Chengf066b2f2011-08-25 01:00:36 +000062static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000063DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
64 cl::desc("Inhibit optimization of S->D register accesses on A15"),
65 cl::init(false));
66
Tim Northoverb4ddc082014-05-30 10:09:59 +000067static cl::opt<bool>
68EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
69 cl::desc("Run SimplifyCFG after expanding atomic operations"
70 " to make use of cmpxchg flow-based information"),
71 cl::init(true));
72
Renato Golin4c871392015-03-26 18:38:04 +000073static cl::opt<bool>
74EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
75 cl::desc("Enable ARM load/store optimization pass"),
76 cl::init(true));
77
Ahmed Bougachab96444e2015-04-11 00:06:36 +000078// FIXME: Unify control over GlobalMerge.
79static cl::opt<cl::boolOrDefault>
80EnableGlobalMerge("arm-global-merge", cl::Hidden,
81 cl::desc("Enable the global merge pass"));
82
Matthias Braune6ff30b2017-03-18 05:08:58 +000083namespace llvm {
84 void initializeARMExecutionDepsFixPass(PassRegistry&);
85}
86
Jim Grosbachf24f9d92009-08-11 15:33:49 +000087extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000088 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000089 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000090 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
Mehdi Aminif42454b2016-10-09 23:00:34 +000091 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000092 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000093
94 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000095 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000096 initializeARMLoadStoreOptPass(Registry);
97 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000098 initializeARMConstantIslandsPass(Registry);
Matthias Braune6ff30b2017-03-18 05:08:58 +000099 initializeARMExecutionDepsFixPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000100}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000101
Aditya Nandakumara2719322014-11-13 09:26:31 +0000102static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
103 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000104 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000105 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000106 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
107 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000108}
109
Eric Christopher661f2d12014-12-18 02:20:58 +0000110static ARMBaseTargetMachine::ARMABI
111computeTargetABI(const Triple &TT, StringRef CPU,
112 const TargetOptions &Options) {
Eric Christopheree837a52017-06-30 00:03:54 +0000113 StringRef ABIName = Options.MCOptions.getABIName();
114
115 if (ABIName.empty())
116 ABIName = ARM::computeDefaultTargetABI(TT, CPU);
117
118 if (ABIName == "aapcs16")
Tim Northovere0ccdc62015-10-28 22:46:43 +0000119 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopheree837a52017-06-30 00:03:54 +0000120 else if (ABIName.startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000121 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopheree837a52017-06-30 00:03:54 +0000122 else if (ABIName.startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000123 return ARMBaseTargetMachine::ARM_ABI_APCS;
124
Eric Christopheree837a52017-06-30 00:03:54 +0000125 llvm_unreachable("Unhandled/unknown ABI Name!");
126 return ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
Eric Christopher661f2d12014-12-18 02:20:58 +0000127}
128
Daniel Sandersed64d622015-06-11 15:34:59 +0000129static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000130 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000131 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000132 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000133 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000134
135 if (isLittle)
136 // Little endian.
137 Ret += "e";
138 else
139 // Big endian.
140 Ret += "E";
141
Daniel Sandersed64d622015-06-11 15:34:59 +0000142 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000143
144 // Pointers are 32 bits and aligned to 32 bits.
145 Ret += "-p:32:32";
146
147 // ABIs other than APCS have 64 bit integers with natural alignment.
148 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
149 Ret += "-i64:64";
150
151 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
152 // bits, others to 64 bits. We always try to align to 64 bits.
153 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
154 Ret += "-f64:32:64";
155
156 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
157 // to 64. We always ty to give them natural alignment.
158 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
159 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000160 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000161 Ret += "-v128:64:128";
162
163 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
164 // particular hardware support on 32-bit ARM).
165 Ret += "-a:0:32";
166
167 // Integer registers are 32 bits.
168 Ret += "-n32";
169
170 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
171 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000172 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000173 Ret += "-S128";
174 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
175 Ret += "-S64";
176 else
177 Ret += "-S32";
178
179 return Ret;
180}
181
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000182static Reloc::Model getEffectiveRelocModel(const Triple &TT,
183 Optional<Reloc::Model> RM) {
184 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000185 // Default relocation model on Darwin is PIC.
186 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000187
Oliver Stannard8331aae2016-08-08 15:28:31 +0000188 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
189 assert(TT.isOSBinFormatELF() &&
190 "ROPI/RWPI currently only supported for ELF");
191
Renato Golin9be886292016-05-28 04:47:13 +0000192 // DynamicNoPIC is only used on darwin.
193 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
194 return Reloc::Static;
195
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000196 return *RM;
197}
198
Rafael Espindola38af4d62016-05-18 16:00:24 +0000199/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000200///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000201ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000202 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000203 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000204 Optional<Reloc::Model> RM,
205 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000206 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000207 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000208 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
209 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000210 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000211 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000212 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000213
214 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000215 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000216 this->Options.FloatABIType =
217 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000218
219 // Default to triple-appropriate EABI
220 if (Options.EABIVersion == EABI::Default ||
221 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000222 // musl is compatible with glibc with regard to EABI version
223 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000224 this->Options.EABIVersion = EABI::GNU;
225 else
226 this->Options.EABIVersion = EABI::EABI5;
227 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000228
229 initAsmInfo();
230 if (!Subtarget.isThumb() && !Subtarget.hasARMOps())
231 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
232 "support ARM mode execution!");
Evan Cheng66cff402008-10-30 16:10:54 +0000233}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000234
Eugene Zelenko342257e2017-01-31 00:56:17 +0000235ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000236
Diana Picus22274932016-11-11 08:27:37 +0000237#ifdef LLVM_BUILD_GLOBAL_ISEL
238namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000239
Diana Picus22274932016-11-11 08:27:37 +0000240struct ARMGISelActualAccessor : public GISelAccessor {
241 std::unique_ptr<CallLowering> CallLoweringInfo;
242 std::unique_ptr<InstructionSelector> InstSelector;
243 std::unique_ptr<LegalizerInfo> Legalizer;
244 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000245
Diana Picus22274932016-11-11 08:27:37 +0000246 const CallLowering *getCallLowering() const override {
247 return CallLoweringInfo.get();
248 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000249
Diana Picus22274932016-11-11 08:27:37 +0000250 const InstructionSelector *getInstructionSelector() const override {
251 return InstSelector.get();
252 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000253
Ahmed Bougacha52286032016-12-15 18:45:30 +0000254 const LegalizerInfo *getLegalizerInfo() const override {
Diana Picus22274932016-11-11 08:27:37 +0000255 return Legalizer.get();
256 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000257
Diana Picus22274932016-11-11 08:27:37 +0000258 const RegisterBankInfo *getRegBankInfo() const override {
259 return RegBankInfo.get();
260 }
261};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000262
263} // end anonymous namespace
Diana Picus22274932016-11-11 08:27:37 +0000264#endif
265
Eric Christopher3faf2f12014-10-06 06:45:36 +0000266const ARMSubtarget *
267ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000268 Attribute CPUAttr = F.getFnAttribute("target-cpu");
269 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000270
271 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
272 ? CPUAttr.getValueAsString().str()
273 : TargetCPU;
274 std::string FS = !FSAttr.hasAttribute(Attribute::None)
275 ? FSAttr.getValueAsString().str()
276 : TargetFS;
277
278 // FIXME: This is related to the code below to reset the target options,
279 // we need to know whether or not the soft float flag is set on the
280 // function before we can generate a subtarget. We also need to use
281 // it as a key for the subtarget since that can be the only difference
282 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000283 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000284 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
285 // If the soft float attribute is set on the function turn on the soft float
286 // subtarget feature.
287 if (SoftFloat)
288 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000289
Eric Christopher824f42f2015-05-12 01:26:05 +0000290 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000291 if (!I) {
292 // This needs to be done before we create a new subtarget since any
293 // creation will depend on the TM and the code generation flags on the
294 // function that reside in TargetOptions.
295 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000296 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000297
298#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000299 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000300#else
Diana Picus90f0a842016-11-15 15:38:15 +0000301 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
302 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
Diana Picus7cab0782017-02-17 11:25:17 +0000303 GISel->Legalizer.reset(new ARMLegalizerInfo(*I));
Diana Picus22274932016-11-11 08:27:37 +0000304
Diana Picus90f0a842016-11-15 15:38:15 +0000305 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000306
Diana Picus90f0a842016-11-15 15:38:15 +0000307 // FIXME: At this point, we can't rely on Subtarget having RBI.
308 // It's awkward to mix passing RBI and the Subtarget; should we pass
309 // TII/TRI as well?
Diana Picus8abcbbb2017-05-02 09:40:49 +0000310 GISel->InstSelector.reset(createARMInstructionSelector(*this, *I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000311
Diana Picus90f0a842016-11-15 15:38:15 +0000312 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000313#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000314 I->setGISelAccessor(*GISel);
315 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000316 return I.get();
317}
318
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000319TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000320 return TargetIRAnalysis([this](const Function &F) {
321 return TargetTransformInfo(ARMTTIImpl(this, F));
322 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000323}
324
Christian Pirker2a111602014-03-28 14:35:30 +0000325
Daniel Sanders3e5de882015-06-11 19:41:26 +0000326ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000327 StringRef CPU, StringRef FS,
328 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000329 Optional<Reloc::Model> RM,
330 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000331 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000332 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000333
Daniel Sanders3e5de882015-06-11 19:41:26 +0000334ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000335 StringRef CPU, StringRef FS,
336 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000337 Optional<Reloc::Model> RM,
338 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000339 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000340 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000341
Andrew Trickccb67362012-02-03 05:12:41 +0000342namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000343
Andrew Trickccb67362012-02-03 05:12:41 +0000344/// ARM Code Generator Pass Configuration Options.
345class ARMPassConfig : public TargetPassConfig {
346public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000347 ARMPassConfig(ARMBaseTargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000348 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000349
350 ARMBaseTargetMachine &getARMTargetMachine() const {
351 return getTM<ARMBaseTargetMachine>();
352 }
353
Javed Absar9e1ff862017-06-09 14:07:21 +0000354 ScheduleDAGInstrs *
355 createMachineScheduler(MachineSchedContext *C) const override {
356 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
357 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000358 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
359 if (ST.hasFusion())
360 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000361 return DAG;
362 }
363
364 ScheduleDAGInstrs *
365 createPostMachineScheduler(MachineSchedContext *C) const override {
366 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
367 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000368 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
369 if (ST.hasFusion())
370 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000371 return DAG;
372 }
373
Tim Northoverb4ddc082014-05-30 10:09:59 +0000374 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000375 bool addPreISel() override;
376 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000377#ifdef LLVM_BUILD_GLOBAL_ISEL
378 bool addIRTranslator() override;
379 bool addLegalizeMachineIR() override;
380 bool addRegBankSelect() override;
381 bool addGlobalInstructionSelect() override;
382#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000383 void addPreRegAlloc() override;
384 void addPreSched2() override;
385 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000386};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000387
Matthias Braune6ff30b2017-03-18 05:08:58 +0000388class ARMExecutionDepsFix : public ExecutionDepsFix {
389public:
390 static char ID;
391 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
392 StringRef getPassName() const override {
393 return "ARM Execution Dependency Fix";
394 }
395};
396char ARMExecutionDepsFix::ID;
397
Eugene Zelenko342257e2017-01-31 00:56:17 +0000398} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000399
Matthias Braune6ff30b2017-03-18 05:08:58 +0000400INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
401 "ARM Execution Dependency Fix", false, false)
402
Andrew Trickf8ea1082012-02-04 02:56:59 +0000403TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000404 return new ARMPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000405}
406
Tim Northoverb4ddc082014-05-30 10:09:59 +0000407void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000408 if (TM->Options.ThreadModel == ThreadModel::Single)
409 addPass(createLowerAtomicPass());
410 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000411 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000412
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000413 // Cmpxchg instructions are often used with a subsequent comparison to
414 // determine whether it succeeded. We can exploit existing control-flow in
415 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000416 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
417 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
418 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
419 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
420 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000421
422 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000423
424 // Match interleaved memory accesses to ldN/stN intrinsics.
425 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000426 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000427}
428
429bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000430 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000431 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000432 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000433 // FIXME: This is using the thumb1 only constant value for
434 // maximal global offset for merging globals. We may want
435 // to look into using the old value for non-thumb1 code of
436 // 4095 based on the TargetMachine, but this starts to become
437 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000438 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
439 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000440 // Merging of extern globals is enabled by default on non-Mach-O as we
441 // expect it to be generally either beneficial or harmless. On Mach-O it
442 // is disabled as we emit the .subsections_via_symbols directive which
443 // means that merging extern globals is not safe.
444 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
445 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
446 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000447 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000448
449 return false;
450}
451
Andrew Trickccb67362012-02-03 05:12:41 +0000452bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000453 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000454 return false;
455}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000456
Diana Picus22274932016-11-11 08:27:37 +0000457#ifdef LLVM_BUILD_GLOBAL_ISEL
458bool ARMPassConfig::addIRTranslator() {
459 addPass(new IRTranslator());
460 return false;
461}
462
463bool ARMPassConfig::addLegalizeMachineIR() {
464 addPass(new Legalizer());
465 return false;
466}
467
468bool ARMPassConfig::addRegBankSelect() {
469 addPass(new RegBankSelect());
470 return false;
471}
472
473bool ARMPassConfig::addGlobalInstructionSelect() {
474 addPass(new InstructionSelect());
475 return false;
476}
477#endif
478
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000479void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000480 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000481 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000482
483 if (EnableARMLoadStoreOpt)
484 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
485
486 if (!DisableA15SDOptimization)
487 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000488 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000489}
490
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000491void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000492 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000493 if (EnableARMLoadStoreOpt)
494 addPass(createARMLoadStoreOptimizationPass());
495
Matthias Braune6ff30b2017-03-18 05:08:58 +0000496 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000497 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000498
Evan Cheng207b2462009-11-06 23:52:48 +0000499 // Expand some pseudo instructions into multiple instructions to allow
500 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000501 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000502
Evan Chengecb29082011-11-16 08:38:26 +0000503 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000504 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000505 addPass(createThumb2SizeReductionPass([this](const Function &F) {
506 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
507 }));
508
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000509 addPass(createIfConverter([](const MachineFunction &MF) {
510 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000511 }));
Renato Golin4c871392015-03-26 18:38:04 +0000512 }
Eric Christopher63b44882015-03-05 00:23:40 +0000513 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000514}
515
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000516void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000517 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000518
Eric Christopher63b44882015-03-05 00:23:40 +0000519 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000520 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
521 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000522 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000523
Davide Italiano141b28912015-05-20 21:40:38 +0000524 // Don't optimize barriers at -O0.
525 if (getOptLevel() != CodeGenOpt::None)
526 addPass(createARMOptimizeBarriersPass());
527
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000528 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000529}