blob: 9ba286a98d57a084a21a39b695bd497b249c9fcd [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
Eugene Zelenko342257e2017-01-31 00:56:17 +000013#include "ARMTargetMachine.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000014#include "ARM.h"
15#include "ARMMacroFusion.h"
16#include "ARMSubtarget.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000017#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000018#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000019#include "MCTargetDesc/ARMMCTargetDesc.h"
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Triple.h"
24#include "llvm/Analysis/TargetTransformInfo.h"
Marina Yatsina3d8efa42018-01-22 10:06:33 +000025#include "llvm/CodeGen/ExecutionDomainFix.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000026#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Diana Picus22274932016-11-11 08:27:37 +000027#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
28#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000029#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Diana Picus22274932016-11-11 08:27:37 +000030#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000031#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000032#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000033#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
34#include "llvm/CodeGen/MachineFunction.h"
Javed Absar9e1ff862017-06-09 14:07:21 +000035#include "llvm/CodeGen/MachineScheduler.h"
Evan Chengad3aac712007-05-16 02:01:49 +000036#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000037#include "llvm/CodeGen/TargetLoweringObjectFile.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000038#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000039#include "llvm/IR/Attributes.h"
40#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000041#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000042#include "llvm/Pass.h"
43#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000044#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000045#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000046#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000047#include "llvm/Support/TargetRegistry.h"
Evan Cheng10043e22007-01-19 07:51:42 +000048#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000049#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000050#include <cassert>
51#include <memory>
52#include <string>
53
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000054using namespace llvm;
55
Evan Chengf066b2f2011-08-25 01:00:36 +000056static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000057DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
58 cl::desc("Inhibit optimization of S->D register accesses on A15"),
59 cl::init(false));
60
Tim Northoverb4ddc082014-05-30 10:09:59 +000061static cl::opt<bool>
62EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
63 cl::desc("Run SimplifyCFG after expanding atomic operations"
64 " to make use of cmpxchg flow-based information"),
65 cl::init(true));
66
Renato Golin4c871392015-03-26 18:38:04 +000067static cl::opt<bool>
68EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
69 cl::desc("Enable ARM load/store optimization pass"),
70 cl::init(true));
71
Ahmed Bougachab96444e2015-04-11 00:06:36 +000072// FIXME: Unify control over GlobalMerge.
73static cl::opt<cl::boolOrDefault>
74EnableGlobalMerge("arm-global-merge", cl::Hidden,
75 cl::desc("Enable the global merge pass"));
76
Matthias Braune6ff30b2017-03-18 05:08:58 +000077namespace llvm {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000078 void initializeARMExecutionDomainFixPass(PassRegistry&);
Matthias Braune6ff30b2017-03-18 05:08:58 +000079}
80
Jim Grosbachf24f9d92009-08-11 15:33:49 +000081extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000082 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000083 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000084 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
Mehdi Aminif42454b2016-10-09 23:00:34 +000085 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000086 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000087
88 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000089 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000090 initializeARMLoadStoreOptPass(Registry);
91 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000092 initializeARMConstantIslandsPass(Registry);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000093 initializeARMExecutionDomainFixPass(Registry);
Eli Friedman06d0ee72017-09-05 22:45:23 +000094 initializeARMExpandPseudoPass(Registry);
David Green110844d2017-12-19 12:19:08 +000095 initializeThumb2SizeReducePass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000096}
Douglas Gregor1b731d52009-06-16 20:12:29 +000097
Aditya Nandakumara2719322014-11-13 09:26:31 +000098static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
99 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000100 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000101 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000102 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
103 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000104}
105
Eric Christopher661f2d12014-12-18 02:20:58 +0000106static ARMBaseTargetMachine::ARMABI
107computeTargetABI(const Triple &TT, StringRef CPU,
108 const TargetOptions &Options) {
Eric Christopheree837a52017-06-30 00:03:54 +0000109 StringRef ABIName = Options.MCOptions.getABIName();
110
111 if (ABIName.empty())
112 ABIName = ARM::computeDefaultTargetABI(TT, CPU);
113
114 if (ABIName == "aapcs16")
Tim Northovere0ccdc62015-10-28 22:46:43 +0000115 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopheree837a52017-06-30 00:03:54 +0000116 else if (ABIName.startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000117 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopheree837a52017-06-30 00:03:54 +0000118 else if (ABIName.startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000119 return ARMBaseTargetMachine::ARM_ABI_APCS;
120
Eric Christopheree837a52017-06-30 00:03:54 +0000121 llvm_unreachable("Unhandled/unknown ABI Name!");
122 return ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
Eric Christopher661f2d12014-12-18 02:20:58 +0000123}
124
Daniel Sandersed64d622015-06-11 15:34:59 +0000125static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000126 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000127 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000128 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000129 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000130
131 if (isLittle)
132 // Little endian.
133 Ret += "e";
134 else
135 // Big endian.
136 Ret += "E";
137
Daniel Sandersed64d622015-06-11 15:34:59 +0000138 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000139
140 // Pointers are 32 bits and aligned to 32 bits.
141 Ret += "-p:32:32";
142
143 // ABIs other than APCS have 64 bit integers with natural alignment.
144 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
145 Ret += "-i64:64";
146
147 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
148 // bits, others to 64 bits. We always try to align to 64 bits.
149 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
150 Ret += "-f64:32:64";
151
152 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
153 // to 64. We always ty to give them natural alignment.
154 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
155 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000156 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000157 Ret += "-v128:64:128";
158
159 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
160 // particular hardware support on 32-bit ARM).
161 Ret += "-a:0:32";
162
163 // Integer registers are 32 bits.
164 Ret += "-n32";
165
166 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
167 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000168 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000169 Ret += "-S128";
170 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
171 Ret += "-S64";
172 else
173 Ret += "-S32";
174
175 return Ret;
176}
177
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000178static Reloc::Model getEffectiveRelocModel(const Triple &TT,
179 Optional<Reloc::Model> RM) {
180 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000181 // Default relocation model on Darwin is PIC.
182 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000183
Oliver Stannard8331aae2016-08-08 15:28:31 +0000184 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
185 assert(TT.isOSBinFormatELF() &&
186 "ROPI/RWPI currently only supported for ELF");
187
Renato Golin9be886292016-05-28 04:47:13 +0000188 // DynamicNoPIC is only used on darwin.
189 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
190 return Reloc::Static;
191
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000192 return *RM;
193}
194
Rafael Espindola79e238a2017-08-03 02:16:21 +0000195static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
196 if (CM)
197 return *CM;
198 return CodeModel::Small;
199}
200
Rafael Espindola38af4d62016-05-18 16:00:24 +0000201/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000202///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000203ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000204 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000205 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000206 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000207 Optional<CodeModel::Model> CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000208 CodeGenOpt::Level OL, bool isLittle)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000209 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
210 CPU, FS, Options, getEffectiveRelocModel(TT, RM),
211 getEffectiveCodeModel(CM), OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000212 TargetABI(computeTargetABI(TT, CPU, Options)),
Eric Christopher3df231a2017-07-01 03:41:53 +0000213 TLOF(createTLOF(getTargetTriple())), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000214
215 // Default to triple-appropriate float ABI
Eric Christopher3df231a2017-07-01 03:41:53 +0000216 if (Options.FloatABIType == FloatABI::Default) {
217 if (TargetTriple.getEnvironment() == Triple::GNUEABIHF ||
218 TargetTriple.getEnvironment() == Triple::MuslEABIHF ||
219 TargetTriple.getEnvironment() == Triple::EABIHF ||
220 TargetTriple.isOSWindows() ||
221 TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
222 this->Options.FloatABIType = FloatABI::Hard;
223 else
224 this->Options.FloatABIType = FloatABI::Soft;
225 }
Renato Golin6d435f12015-11-09 12:40:30 +0000226
227 // Default to triple-appropriate EABI
228 if (Options.EABIVersion == EABI::Default ||
229 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000230 // musl is compatible with glibc with regard to EABI version
Eric Christopher3df231a2017-07-01 03:41:53 +0000231 if ((TargetTriple.getEnvironment() == Triple::GNUEABI ||
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000232 TargetTriple.getEnvironment() == Triple::GNUEABIHF ||
233 TargetTriple.getEnvironment() == Triple::MuslEABI ||
234 TargetTriple.getEnvironment() == Triple::MuslEABIHF) &&
235 !(TargetTriple.isOSWindows() || TargetTriple.isOSDarwin()))
Renato Golin6d435f12015-11-09 12:40:30 +0000236 this->Options.EABIVersion = EABI::GNU;
237 else
238 this->Options.EABIVersion = EABI::EABI5;
239 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000240
241 initAsmInfo();
Evan Cheng66cff402008-10-30 16:10:54 +0000242}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000243
Eugene Zelenko342257e2017-01-31 00:56:17 +0000244ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000245
Eric Christopher3faf2f12014-10-06 06:45:36 +0000246const ARMSubtarget *
247ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000248 Attribute CPUAttr = F.getFnAttribute("target-cpu");
249 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000250
251 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
252 ? CPUAttr.getValueAsString().str()
253 : TargetCPU;
254 std::string FS = !FSAttr.hasAttribute(Attribute::None)
255 ? FSAttr.getValueAsString().str()
256 : TargetFS;
257
258 // FIXME: This is related to the code below to reset the target options,
259 // we need to know whether or not the soft float flag is set on the
260 // function before we can generate a subtarget. We also need to use
261 // it as a key for the subtarget since that can be the only difference
262 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000263 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000264 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
265 // If the soft float attribute is set on the function turn on the soft float
266 // subtarget feature.
267 if (SoftFloat)
268 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000269
Eric Christopher824f42f2015-05-12 01:26:05 +0000270 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000271 if (!I) {
272 // This needs to be done before we create a new subtarget since any
273 // creation will depend on the TM and the code generation flags on the
274 // function that reside in TargetOptions.
275 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000276 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000277
278 if (!I->isThumb() && !I->hasARMOps())
279 F.getContext().emitError("Function '" + F.getName() + "' uses ARM "
280 "instructions, but the target does not support ARM mode execution.");
Diana Picus90f0a842016-11-15 15:38:15 +0000281 }
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000282
Eric Christopher3faf2f12014-10-06 06:45:36 +0000283 return I.get();
284}
285
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000286TargetTransformInfo
287ARMBaseTargetMachine::getTargetTransformInfo(const Function &F) {
288 return TargetTransformInfo(ARMTTIImpl(this, F));
Chandler Carruth664e3542013-01-07 01:37:14 +0000289}
290
Daniel Sanders3e5de882015-06-11 19:41:26 +0000291ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000292 StringRef CPU, StringRef FS,
293 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000294 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000295 Optional<CodeModel::Model> CM,
296 CodeGenOpt::Level OL, bool JIT)
Florian Hahnd211fe72017-05-24 10:18:57 +0000297 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000298
Daniel Sanders3e5de882015-06-11 19:41:26 +0000299ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000300 StringRef CPU, StringRef FS,
301 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000302 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000303 Optional<CodeModel::Model> CM,
304 CodeGenOpt::Level OL, bool JIT)
Florian Hahnd211fe72017-05-24 10:18:57 +0000305 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000306
Andrew Trickccb67362012-02-03 05:12:41 +0000307namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000308
Andrew Trickccb67362012-02-03 05:12:41 +0000309/// ARM Code Generator Pass Configuration Options.
310class ARMPassConfig : public TargetPassConfig {
311public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000312 ARMPassConfig(ARMBaseTargetMachine &TM, PassManagerBase &PM)
Eugene Leviant27b226f2017-10-20 14:29:17 +0000313 : TargetPassConfig(TM, PM) {
314 if (TM.getOptLevel() != CodeGenOpt::None) {
315 ARMGenSubtargetInfo STI(TM.getTargetTriple(), TM.getTargetCPU(),
316 TM.getTargetFeatureString());
317 if (STI.hasFeature(ARM::FeatureUseMISched))
318 substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
319 }
320 }
Andrew Trickccb67362012-02-03 05:12:41 +0000321
322 ARMBaseTargetMachine &getARMTargetMachine() const {
323 return getTM<ARMBaseTargetMachine>();
324 }
325
Javed Absar9e1ff862017-06-09 14:07:21 +0000326 ScheduleDAGInstrs *
327 createMachineScheduler(MachineSchedContext *C) const override {
328 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
329 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000330 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
331 if (ST.hasFusion())
332 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000333 return DAG;
334 }
335
336 ScheduleDAGInstrs *
337 createPostMachineScheduler(MachineSchedContext *C) const override {
338 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
339 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000340 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
341 if (ST.hasFusion())
342 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000343 return DAG;
344 }
345
Tim Northoverb4ddc082014-05-30 10:09:59 +0000346 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000347 bool addPreISel() override;
348 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000349 bool addIRTranslator() override;
350 bool addLegalizeMachineIR() override;
351 bool addRegBankSelect() override;
352 bool addGlobalInstructionSelect() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000353 void addPreRegAlloc() override;
354 void addPreSched2() override;
355 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000356};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000357
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000358class ARMExecutionDomainFix : public ExecutionDomainFix {
Matthias Braune6ff30b2017-03-18 05:08:58 +0000359public:
360 static char ID;
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000361 ARMExecutionDomainFix() : ExecutionDomainFix(ID, ARM::DPRRegClass) {}
Matthias Braune6ff30b2017-03-18 05:08:58 +0000362 StringRef getPassName() const override {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000363 return "ARM Execution Domain Fix";
Matthias Braune6ff30b2017-03-18 05:08:58 +0000364 }
365};
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000366char ARMExecutionDomainFix::ID;
Matthias Braune6ff30b2017-03-18 05:08:58 +0000367
Eugene Zelenko342257e2017-01-31 00:56:17 +0000368} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000369
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000370INITIALIZE_PASS_BEGIN(ARMExecutionDomainFix, "arm-execution-domain-fix",
371 "ARM Execution Domain Fix", false, false)
372INITIALIZE_PASS_DEPENDENCY(ReachingDefAnalysis)
373INITIALIZE_PASS_END(ARMExecutionDomainFix, "arm-execution-domain-fix",
374 "ARM Execution Domain Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000375
Andrew Trickf8ea1082012-02-04 02:56:59 +0000376TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000377 return new ARMPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000378}
379
Tim Northoverb4ddc082014-05-30 10:09:59 +0000380void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000381 if (TM->Options.ThreadModel == ThreadModel::Single)
382 addPass(createLowerAtomicPass());
383 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000384 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000385
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000386 // Cmpxchg instructions are often used with a subsequent comparison to
387 // determine whether it succeeded. We can exploit existing control-flow in
388 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000389 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
Sanjay Patelb0491732017-10-28 18:43:07 +0000390 addPass(createCFGSimplificationPass(
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000391 1, false, false, true, true, [this](const Function &F) {
Sanjay Patelb0491732017-10-28 18:43:07 +0000392 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
393 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
394 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000395
396 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000397
398 // Match interleaved memory accesses to ldN/stN intrinsics.
399 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000400 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000401}
402
403bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000404 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000405 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000406 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000407 // FIXME: This is using the thumb1 only constant value for
408 // maximal global offset for merging globals. We may want
409 // to look into using the old value for non-thumb1 code of
410 // 4095 based on the TargetMachine, but this starts to become
411 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000412 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
413 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000414 // Merging of extern globals is enabled by default on non-Mach-O as we
415 // expect it to be generally either beneficial or harmless. On Mach-O it
416 // is disabled as we emit the .subsections_via_symbols directive which
417 // means that merging extern globals is not safe.
418 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
419 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
420 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000421 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000422
423 return false;
424}
425
Andrew Trickccb67362012-02-03 05:12:41 +0000426bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000427 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000428 return false;
429}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000430
Diana Picus22274932016-11-11 08:27:37 +0000431bool ARMPassConfig::addIRTranslator() {
432 addPass(new IRTranslator());
433 return false;
434}
435
436bool ARMPassConfig::addLegalizeMachineIR() {
437 addPass(new Legalizer());
438 return false;
439}
440
441bool ARMPassConfig::addRegBankSelect() {
442 addPass(new RegBankSelect());
443 return false;
444}
445
446bool ARMPassConfig::addGlobalInstructionSelect() {
447 addPass(new InstructionSelect());
448 return false;
449}
Diana Picus22274932016-11-11 08:27:37 +0000450
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000451void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000452 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000453 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000454
455 if (EnableARMLoadStoreOpt)
456 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
457
458 if (!DisableA15SDOptimization)
459 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000460 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000461}
462
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000463void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000464 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000465 if (EnableARMLoadStoreOpt)
466 addPass(createARMLoadStoreOptimizationPass());
467
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000468 addPass(new ARMExecutionDomainFix());
Marina Yatsina0bf841a2018-01-22 10:06:50 +0000469 addPass(createBreakFalseDeps());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000470 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000471
Evan Cheng207b2462009-11-06 23:52:48 +0000472 // Expand some pseudo instructions into multiple instructions to allow
473 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000474 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000475
Evan Chengecb29082011-11-16 08:38:26 +0000476 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000477 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000478 addPass(createThumb2SizeReductionPass([this](const Function &F) {
479 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
480 }));
481
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000482 addPass(createIfConverter([](const MachineFunction &MF) {
483 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000484 }));
Renato Golin4c871392015-03-26 18:38:04 +0000485 }
Eric Christopher63b44882015-03-05 00:23:40 +0000486 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000487}
488
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000489void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000490 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000491
Eric Christopher63b44882015-03-05 00:23:40 +0000492 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000493 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
494 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000495 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000496
Davide Italiano141b28912015-05-20 21:40:38 +0000497 // Don't optimize barriers at -O0.
498 if (getOptLevel() != CodeGenOpt::None)
499 addPass(createARMOptimizeBarriersPass());
500
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000501 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000502}