blob: 433a1915a1a63c02939acc94aaa5c3dea16d5252 [file] [log] [blame]
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
Eugene Zelenko342257e2017-01-31 00:56:17 +000012#include "ARMTargetMachine.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000013#include "ARM.h"
14#include "ARMMacroFusion.h"
15#include "ARMSubtarget.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000016#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000018#include "MCTargetDesc/ARMMCTargetDesc.h"
19#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Triple.h"
23#include "llvm/Analysis/TargetTransformInfo.h"
Marina Yatsina3d8efa42018-01-22 10:06:33 +000024#include "llvm/CodeGen/ExecutionDomainFix.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000025#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Diana Picus22274932016-11-11 08:27:37 +000026#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
27#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000028#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Diana Picus22274932016-11-11 08:27:37 +000029#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000030#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000031#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000032#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
33#include "llvm/CodeGen/MachineFunction.h"
Javed Absar9e1ff862017-06-09 14:07:21 +000034#include "llvm/CodeGen/MachineScheduler.h"
Evan Chengad3aac712007-05-16 02:01:49 +000035#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000036#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000037#include "llvm/IR/Attributes.h"
38#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000039#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000040#include "llvm/Pass.h"
41#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000042#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000043#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000044#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000045#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000046#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng10043e22007-01-19 07:51:42 +000047#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000048#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000049#include <cassert>
50#include <memory>
51#include <string>
52
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000053using namespace llvm;
54
Evan Chengf066b2f2011-08-25 01:00:36 +000055static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000056DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
57 cl::desc("Inhibit optimization of S->D register accesses on A15"),
58 cl::init(false));
59
Tim Northoverb4ddc082014-05-30 10:09:59 +000060static cl::opt<bool>
61EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
62 cl::desc("Run SimplifyCFG after expanding atomic operations"
63 " to make use of cmpxchg flow-based information"),
64 cl::init(true));
65
Renato Golin4c871392015-03-26 18:38:04 +000066static cl::opt<bool>
67EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
68 cl::desc("Enable ARM load/store optimization pass"),
69 cl::init(true));
70
Ahmed Bougachab96444e2015-04-11 00:06:36 +000071// FIXME: Unify control over GlobalMerge.
72static cl::opt<cl::boolOrDefault>
73EnableGlobalMerge("arm-global-merge", cl::Hidden,
74 cl::desc("Enable the global merge pass"));
75
Matthias Braune6ff30b2017-03-18 05:08:58 +000076namespace llvm {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000077 void initializeARMExecutionDomainFixPass(PassRegistry&);
Matthias Braune6ff30b2017-03-18 05:08:58 +000078}
79
Jim Grosbachf24f9d92009-08-11 15:33:49 +000080extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000081 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000082 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000083 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
Mehdi Aminif42454b2016-10-09 23:00:34 +000084 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000085 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000086
87 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000088 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000089 initializeARMLoadStoreOptPass(Registry);
90 initializeARMPreAllocLoadStoreOptPass(Registry);
Sjoerd Meijerc89ca552018-06-28 12:55:29 +000091 initializeARMParallelDSPPass(Registry);
Sam Parker3828c6f2018-07-23 12:27:47 +000092 initializeARMCodeGenPreparePass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000093 initializeARMConstantIslandsPass(Registry);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000094 initializeARMExecutionDomainFixPass(Registry);
Eli Friedman06d0ee72017-09-05 22:45:23 +000095 initializeARMExpandPseudoPass(Registry);
David Green110844d2017-12-19 12:19:08 +000096 initializeThumb2SizeReducePass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000097}
Douglas Gregor1b731d52009-06-16 20:12:29 +000098
Aditya Nandakumara2719322014-11-13 09:26:31 +000099static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
100 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000101 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000102 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000103 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
104 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000105}
106
Eric Christopher661f2d12014-12-18 02:20:58 +0000107static ARMBaseTargetMachine::ARMABI
108computeTargetABI(const Triple &TT, StringRef CPU,
109 const TargetOptions &Options) {
Eric Christopheree837a52017-06-30 00:03:54 +0000110 StringRef ABIName = Options.MCOptions.getABIName();
111
112 if (ABIName.empty())
113 ABIName = ARM::computeDefaultTargetABI(TT, CPU);
114
115 if (ABIName == "aapcs16")
Tim Northovere0ccdc62015-10-28 22:46:43 +0000116 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopheree837a52017-06-30 00:03:54 +0000117 else if (ABIName.startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000118 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopheree837a52017-06-30 00:03:54 +0000119 else if (ABIName.startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000120 return ARMBaseTargetMachine::ARM_ABI_APCS;
121
Eric Christopheree837a52017-06-30 00:03:54 +0000122 llvm_unreachable("Unhandled/unknown ABI Name!");
123 return ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
Eric Christopher661f2d12014-12-18 02:20:58 +0000124}
125
Daniel Sandersed64d622015-06-11 15:34:59 +0000126static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000127 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000128 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000129 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000130 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000131
132 if (isLittle)
133 // Little endian.
134 Ret += "e";
135 else
136 // Big endian.
137 Ret += "E";
138
Daniel Sandersed64d622015-06-11 15:34:59 +0000139 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000140
141 // Pointers are 32 bits and aligned to 32 bits.
142 Ret += "-p:32:32";
143
144 // ABIs other than APCS have 64 bit integers with natural alignment.
145 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
146 Ret += "-i64:64";
147
148 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
149 // bits, others to 64 bits. We always try to align to 64 bits.
150 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
151 Ret += "-f64:32:64";
152
153 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
154 // to 64. We always ty to give them natural alignment.
155 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
156 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000157 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000158 Ret += "-v128:64:128";
159
160 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
161 // particular hardware support on 32-bit ARM).
162 Ret += "-a:0:32";
163
164 // Integer registers are 32 bits.
165 Ret += "-n32";
166
167 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
168 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000169 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000170 Ret += "-S128";
171 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
172 Ret += "-S64";
173 else
174 Ret += "-S32";
175
176 return Ret;
177}
178
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000179static Reloc::Model getEffectiveRelocModel(const Triple &TT,
180 Optional<Reloc::Model> RM) {
181 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000182 // Default relocation model on Darwin is PIC.
183 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000184
Oliver Stannard8331aae2016-08-08 15:28:31 +0000185 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
186 assert(TT.isOSBinFormatELF() &&
187 "ROPI/RWPI currently only supported for ELF");
188
Renato Golin9be886292016-05-28 04:47:13 +0000189 // DynamicNoPIC is only used on darwin.
190 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
191 return Reloc::Static;
192
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000193 return *RM;
194}
195
Rafael Espindola38af4d62016-05-18 16:00:24 +0000196/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000197///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000198ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000199 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000200 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000201 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000202 Optional<CodeModel::Model> CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000203 CodeGenOpt::Level OL, bool isLittle)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000204 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
205 CPU, FS, Options, getEffectiveRelocModel(TT, RM),
David Greenca29c272018-12-07 12:10:23 +0000206 getEffectiveCodeModel(CM, CodeModel::Small), OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000207 TargetABI(computeTargetABI(TT, CPU, Options)),
Eric Christopher3df231a2017-07-01 03:41:53 +0000208 TLOF(createTLOF(getTargetTriple())), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000209
210 // Default to triple-appropriate float ABI
Eric Christopher3df231a2017-07-01 03:41:53 +0000211 if (Options.FloatABIType == FloatABI::Default) {
Tim Northover097a3e32018-07-18 12:36:25 +0000212 if (isTargetHardFloat())
Eric Christopher3df231a2017-07-01 03:41:53 +0000213 this->Options.FloatABIType = FloatABI::Hard;
214 else
215 this->Options.FloatABIType = FloatABI::Soft;
216 }
Renato Golin6d435f12015-11-09 12:40:30 +0000217
218 // Default to triple-appropriate EABI
219 if (Options.EABIVersion == EABI::Default ||
220 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000221 // musl is compatible with glibc with regard to EABI version
Eric Christopher3df231a2017-07-01 03:41:53 +0000222 if ((TargetTriple.getEnvironment() == Triple::GNUEABI ||
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000223 TargetTriple.getEnvironment() == Triple::GNUEABIHF ||
224 TargetTriple.getEnvironment() == Triple::MuslEABI ||
225 TargetTriple.getEnvironment() == Triple::MuslEABIHF) &&
226 !(TargetTriple.isOSWindows() || TargetTriple.isOSDarwin()))
Renato Golin6d435f12015-11-09 12:40:30 +0000227 this->Options.EABIVersion = EABI::GNU;
228 else
229 this->Options.EABIVersion = EABI::EABI5;
230 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000231
Matthias Braunda5e7e12018-06-28 17:00:45 +0000232 if (TT.isOSBinFormatMachO()) {
Tim Northover271d3d22018-04-13 22:25:20 +0000233 this->Options.TrapUnreachable = true;
Matthias Braunda5e7e12018-06-28 17:00:45 +0000234 this->Options.NoTrapAfterNoreturn = true;
235 }
Tim Northover271d3d22018-04-13 22:25:20 +0000236
Florian Hahnd211fe72017-05-24 10:18:57 +0000237 initAsmInfo();
Evan Cheng66cff402008-10-30 16:10:54 +0000238}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000239
Eugene Zelenko342257e2017-01-31 00:56:17 +0000240ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000241
Eric Christopher3faf2f12014-10-06 06:45:36 +0000242const ARMSubtarget *
243ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000244 Attribute CPUAttr = F.getFnAttribute("target-cpu");
245 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000246
247 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
248 ? CPUAttr.getValueAsString().str()
249 : TargetCPU;
250 std::string FS = !FSAttr.hasAttribute(Attribute::None)
251 ? FSAttr.getValueAsString().str()
252 : TargetFS;
253
254 // FIXME: This is related to the code below to reset the target options,
255 // we need to know whether or not the soft float flag is set on the
256 // function before we can generate a subtarget. We also need to use
257 // it as a key for the subtarget since that can be the only difference
258 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000259 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000260 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
261 // If the soft float attribute is set on the function turn on the soft float
262 // subtarget feature.
263 if (SoftFloat)
264 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000265
Eric Christopher824f42f2015-05-12 01:26:05 +0000266 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000267 if (!I) {
268 // This needs to be done before we create a new subtarget since any
269 // creation will depend on the TM and the code generation flags on the
270 // function that reside in TargetOptions.
271 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000272 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000273
274 if (!I->isThumb() && !I->hasARMOps())
275 F.getContext().emitError("Function '" + F.getName() + "' uses ARM "
276 "instructions, but the target does not support ARM mode execution.");
Diana Picus90f0a842016-11-15 15:38:15 +0000277 }
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000278
Eric Christopher3faf2f12014-10-06 06:45:36 +0000279 return I.get();
280}
281
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000282TargetTransformInfo
283ARMBaseTargetMachine::getTargetTransformInfo(const Function &F) {
284 return TargetTransformInfo(ARMTTIImpl(this, F));
Chandler Carruth664e3542013-01-07 01:37:14 +0000285}
286
Daniel Sanders3e5de882015-06-11 19:41:26 +0000287ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000288 StringRef CPU, StringRef FS,
289 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000290 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000291 Optional<CodeModel::Model> CM,
292 CodeGenOpt::Level OL, bool JIT)
Florian Hahnd211fe72017-05-24 10:18:57 +0000293 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000294
Daniel Sanders3e5de882015-06-11 19:41:26 +0000295ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000296 StringRef CPU, StringRef FS,
297 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000298 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000299 Optional<CodeModel::Model> CM,
300 CodeGenOpt::Level OL, bool JIT)
Florian Hahnd211fe72017-05-24 10:18:57 +0000301 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000302
Andrew Trickccb67362012-02-03 05:12:41 +0000303namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000304
Andrew Trickccb67362012-02-03 05:12:41 +0000305/// ARM Code Generator Pass Configuration Options.
306class ARMPassConfig : public TargetPassConfig {
307public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000308 ARMPassConfig(ARMBaseTargetMachine &TM, PassManagerBase &PM)
Eugene Leviant27b226f2017-10-20 14:29:17 +0000309 : TargetPassConfig(TM, PM) {
310 if (TM.getOptLevel() != CodeGenOpt::None) {
311 ARMGenSubtargetInfo STI(TM.getTargetTriple(), TM.getTargetCPU(),
312 TM.getTargetFeatureString());
313 if (STI.hasFeature(ARM::FeatureUseMISched))
314 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
315 }
316 }
Andrew Trickccb67362012-02-03 05:12:41 +0000317
318 ARMBaseTargetMachine &getARMTargetMachine() const {
319 return getTM<ARMBaseTargetMachine>();
320 }
321
Javed Absar9e1ff862017-06-09 14:07:21 +0000322 ScheduleDAGInstrs *
323 createMachineScheduler(MachineSchedContext *C) const override {
324 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
325 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000326 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
327 if (ST.hasFusion())
328 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000329 return DAG;
330 }
331
332 ScheduleDAGInstrs *
333 createPostMachineScheduler(MachineSchedContext *C) const override {
334 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
335 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000336 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
337 if (ST.hasFusion())
338 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000339 return DAG;
340 }
341
Tim Northoverb4ddc082014-05-30 10:09:59 +0000342 void addIRPasses() override;
Sam Parker3828c6f2018-07-23 12:27:47 +0000343 void addCodeGenPrepare() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000344 bool addPreISel() override;
345 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000346 bool addIRTranslator() override;
347 bool addLegalizeMachineIR() override;
348 bool addRegBankSelect() override;
349 bool addGlobalInstructionSelect() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000350 void addPreRegAlloc() override;
351 void addPreSched2() override;
352 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000353};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000354
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000355class ARMExecutionDomainFix : public ExecutionDomainFix {
Matthias Braune6ff30b2017-03-18 05:08:58 +0000356public:
357 static char ID;
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000358 ARMExecutionDomainFix() : ExecutionDomainFix(ID, ARM::DPRRegClass) {}
Matthias Braune6ff30b2017-03-18 05:08:58 +0000359 StringRef getPassName() const override {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000360 return "ARM Execution Domain Fix";
Matthias Braune6ff30b2017-03-18 05:08:58 +0000361 }
362};
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000363char ARMExecutionDomainFix::ID;
Matthias Braune6ff30b2017-03-18 05:08:58 +0000364
Eugene Zelenko342257e2017-01-31 00:56:17 +0000365} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000366
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000367INITIALIZE_PASS_BEGIN(ARMExecutionDomainFix, "arm-execution-domain-fix",
368 "ARM Execution Domain Fix", false, false)
369INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
370INITIALIZE_PASS_END(ARMExecutionDomainFix, "arm-execution-domain-fix",
371 "ARM Execution Domain Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000372
Andrew Trickf8ea1082012-02-04 02:56:59 +0000373TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000374 return new ARMPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000375}
376
Tim Northoverb4ddc082014-05-30 10:09:59 +0000377void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000378 if (TM->Options.ThreadModel == ThreadModel::Single)
379 addPass(createLowerAtomicPass());
380 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000381 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000382
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000383 // Cmpxchg instructions are often used with a subsequent comparison to
384 // determine whether it succeeded. We can exploit existing control-flow in
385 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000386 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
Sanjay Patelb0491732017-10-28 18:43:07 +0000387 addPass(createCFGSimplificationPass(
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000388 1, false, false, true, true, [this](const Function &F) {
Sanjay Patelb0491732017-10-28 18:43:07 +0000389 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
390 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
391 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000392
393 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000394
395 // Match interleaved memory accesses to ldN/stN intrinsics.
396 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000397 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000398}
399
Sam Parker3828c6f2018-07-23 12:27:47 +0000400void ARMPassConfig::addCodeGenPrepare() {
401 if (getOptLevel() != CodeGenOpt::None)
402 addPass(createARMCodeGenPreparePass());
403 TargetPassConfig::addCodeGenPrepare();
404}
405
Tim Northoverb4ddc082014-05-30 10:09:59 +0000406bool ARMPassConfig::addPreISel() {
Sjoerd Meijerc89ca552018-06-28 12:55:29 +0000407 if (getOptLevel() != CodeGenOpt::None)
408 addPass(createARMParallelDSPPass());
409
Ahmed Bougacha82076412015-06-04 20:39:23 +0000410 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000411 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000412 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000413 // FIXME: This is using the thumb1 only constant value for
414 // maximal global offset for merging globals. We may want
415 // to look into using the old value for non-thumb1 code of
416 // 4095 based on the TargetMachine, but this starts to become
417 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000418 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
419 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000420 // Merging of extern globals is enabled by default on non-Mach-O as we
421 // expect it to be generally either beneficial or harmless. On Mach-O it
422 // is disabled as we emit the .subsections_via_symbols directive which
423 // means that merging extern globals is not safe.
424 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
425 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
426 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000427 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000428
429 return false;
430}
431
Andrew Trickccb67362012-02-03 05:12:41 +0000432bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000433 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000434 return false;
435}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000436
Diana Picus22274932016-11-11 08:27:37 +0000437bool ARMPassConfig::addIRTranslator() {
438 addPass(new IRTranslator());
439 return false;
440}
441
442bool ARMPassConfig::addLegalizeMachineIR() {
443 addPass(new Legalizer());
444 return false;
445}
446
447bool ARMPassConfig::addRegBankSelect() {
448 addPass(new RegBankSelect());
449 return false;
450}
451
452bool ARMPassConfig::addGlobalInstructionSelect() {
453 addPass(new InstructionSelect());
454 return false;
455}
Diana Picus22274932016-11-11 08:27:37 +0000456
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000457void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000458 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000459 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000460
461 if (EnableARMLoadStoreOpt)
462 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
463
464 if (!DisableA15SDOptimization)
465 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000466 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000467}
468
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000469void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000470 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000471 if (EnableARMLoadStoreOpt)
472 addPass(createARMLoadStoreOptimizationPass());
473
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000474 addPass(new ARMExecutionDomainFix());
Marina Yatsina0bf841a2018-01-22 10:06:50 +0000475 addPass(createBreakFalseDeps());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000476 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000477
Evan Cheng207b2462009-11-06 23:52:48 +0000478 // Expand some pseudo instructions into multiple instructions to allow
479 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000480 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000481
Evan Chengecb29082011-11-16 08:38:26 +0000482 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000483 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000484 addPass(createThumb2SizeReductionPass([this](const Function &F) {
485 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
486 }));
487
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000488 addPass(createIfConverter([](const MachineFunction &MF) {
489 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000490 }));
Renato Golin4c871392015-03-26 18:38:04 +0000491 }
Eric Christopher63b44882015-03-05 00:23:40 +0000492 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000493}
494
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000495void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000496 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000497
Eric Christopher63b44882015-03-05 00:23:40 +0000498 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000499 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
500 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000501 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000502
Davide Italiano141b28912015-05-20 21:40:38 +0000503 // Don't optimize barriers at -O0.
504 if (getOptLevel() != CodeGenOpt::None)
505 addPass(createARMOptimizeBarriersPass());
506
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000507 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000508}