blob: 9a191e42247758d55dfc4b81473b4774207edddf [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"
Eugene Zelenko342257e2017-01-31 00:56:17 +000014#include "ARMSubtarget.h"
Florian Hahnb489e562017-06-22 09:39:36 +000015#include "ARMMacroFusion.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000016#include "ARMTargetMachine.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"
Matthias Braune6ff30b2017-03-18 05:08:58 +000025#include "llvm/CodeGen/ExecutionDepsFix.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"
Matthias Braun31d19d42016-05-10 03:21:59 +000037#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000038#include "llvm/IR/Attributes.h"
39#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000040#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000041#include "llvm/Pass.h"
42#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000043#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000044#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000045#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000046#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000047#include "llvm/Target/TargetLoweringObjectFile.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 {
78 void initializeARMExecutionDepsFixPass(PassRegistry&);
79}
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);
Matthias Braune6ff30b2017-03-18 05:08:58 +000093 initializeARMExecutionDepsFixPass(Registry);
Eli Friedman06d0ee72017-09-05 22:45:23 +000094 initializeARMExpandPseudoPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000095}
Douglas Gregor1b731d52009-06-16 20:12:29 +000096
Aditya Nandakumara2719322014-11-13 09:26:31 +000097static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
98 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +000099 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000100 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000101 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
102 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000103}
104
Eric Christopher661f2d12014-12-18 02:20:58 +0000105static ARMBaseTargetMachine::ARMABI
106computeTargetABI(const Triple &TT, StringRef CPU,
107 const TargetOptions &Options) {
Eric Christopheree837a52017-06-30 00:03:54 +0000108 StringRef ABIName = Options.MCOptions.getABIName();
109
110 if (ABIName.empty())
111 ABIName = ARM::computeDefaultTargetABI(TT, CPU);
112
113 if (ABIName == "aapcs16")
Tim Northovere0ccdc62015-10-28 22:46:43 +0000114 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopheree837a52017-06-30 00:03:54 +0000115 else if (ABIName.startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000116 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopheree837a52017-06-30 00:03:54 +0000117 else if (ABIName.startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000118 return ARMBaseTargetMachine::ARM_ABI_APCS;
119
Eric Christopheree837a52017-06-30 00:03:54 +0000120 llvm_unreachable("Unhandled/unknown ABI Name!");
121 return ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
Eric Christopher661f2d12014-12-18 02:20:58 +0000122}
123
Daniel Sandersed64d622015-06-11 15:34:59 +0000124static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000125 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000126 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000127 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000128 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000129
130 if (isLittle)
131 // Little endian.
132 Ret += "e";
133 else
134 // Big endian.
135 Ret += "E";
136
Daniel Sandersed64d622015-06-11 15:34:59 +0000137 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000138
139 // Pointers are 32 bits and aligned to 32 bits.
140 Ret += "-p:32:32";
141
142 // ABIs other than APCS have 64 bit integers with natural alignment.
143 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
144 Ret += "-i64:64";
145
146 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
147 // bits, others to 64 bits. We always try to align to 64 bits.
148 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
149 Ret += "-f64:32:64";
150
151 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
152 // to 64. We always ty to give them natural alignment.
153 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
154 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000155 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000156 Ret += "-v128:64:128";
157
158 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
159 // particular hardware support on 32-bit ARM).
160 Ret += "-a:0:32";
161
162 // Integer registers are 32 bits.
163 Ret += "-n32";
164
165 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
166 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000167 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000168 Ret += "-S128";
169 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
170 Ret += "-S64";
171 else
172 Ret += "-S32";
173
174 return Ret;
175}
176
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000177static Reloc::Model getEffectiveRelocModel(const Triple &TT,
178 Optional<Reloc::Model> RM) {
179 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000180 // Default relocation model on Darwin is PIC.
181 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000182
Oliver Stannard8331aae2016-08-08 15:28:31 +0000183 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
184 assert(TT.isOSBinFormatELF() &&
185 "ROPI/RWPI currently only supported for ELF");
186
Renato Golin9be886292016-05-28 04:47:13 +0000187 // DynamicNoPIC is only used on darwin.
188 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
189 return Reloc::Static;
190
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000191 return *RM;
192}
193
Rafael Espindola79e238a2017-08-03 02:16:21 +0000194static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
195 if (CM)
196 return *CM;
197 return CodeModel::Small;
198}
199
Rafael Espindola38af4d62016-05-18 16:00:24 +0000200/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000201///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000202ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000203 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000204 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000205 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000206 Optional<CodeModel::Model> CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000207 CodeGenOpt::Level OL, bool isLittle)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000208 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
209 CPU, FS, Options, getEffectiveRelocModel(TT, RM),
210 getEffectiveCodeModel(CM), OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000211 TargetABI(computeTargetABI(TT, CPU, Options)),
Eric Christopher3df231a2017-07-01 03:41:53 +0000212 TLOF(createTLOF(getTargetTriple())), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000213
214 // Default to triple-appropriate float ABI
Eric Christopher3df231a2017-07-01 03:41:53 +0000215 if (Options.FloatABIType == FloatABI::Default) {
216 if (TargetTriple.getEnvironment() == Triple::GNUEABIHF ||
217 TargetTriple.getEnvironment() == Triple::MuslEABIHF ||
218 TargetTriple.getEnvironment() == Triple::EABIHF ||
219 TargetTriple.isOSWindows() ||
220 TargetABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
221 this->Options.FloatABIType = FloatABI::Hard;
222 else
223 this->Options.FloatABIType = FloatABI::Soft;
224 }
Renato Golin6d435f12015-11-09 12:40:30 +0000225
226 // Default to triple-appropriate EABI
227 if (Options.EABIVersion == EABI::Default ||
228 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000229 // musl is compatible with glibc with regard to EABI version
Eric Christopher3df231a2017-07-01 03:41:53 +0000230 if ((TargetTriple.getEnvironment() == Triple::GNUEABI ||
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000231 TargetTriple.getEnvironment() == Triple::GNUEABIHF ||
232 TargetTriple.getEnvironment() == Triple::MuslEABI ||
233 TargetTriple.getEnvironment() == Triple::MuslEABIHF) &&
234 !(TargetTriple.isOSWindows() || TargetTriple.isOSDarwin()))
Renato Golin6d435f12015-11-09 12:40:30 +0000235 this->Options.EABIVersion = EABI::GNU;
236 else
237 this->Options.EABIVersion = EABI::EABI5;
238 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000239
240 initAsmInfo();
Evan Cheng66cff402008-10-30 16:10:54 +0000241}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000242
Eugene Zelenko342257e2017-01-31 00:56:17 +0000243ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000244
Eric Christopher3faf2f12014-10-06 06:45:36 +0000245const ARMSubtarget *
246ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000247 Attribute CPUAttr = F.getFnAttribute("target-cpu");
248 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000249
250 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
251 ? CPUAttr.getValueAsString().str()
252 : TargetCPU;
253 std::string FS = !FSAttr.hasAttribute(Attribute::None)
254 ? FSAttr.getValueAsString().str()
255 : TargetFS;
256
257 // FIXME: This is related to the code below to reset the target options,
258 // we need to know whether or not the soft float flag is set on the
259 // function before we can generate a subtarget. We also need to use
260 // it as a key for the subtarget since that can be the only difference
261 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000262 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000263 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
264 // If the soft float attribute is set on the function turn on the soft float
265 // subtarget feature.
266 if (SoftFloat)
267 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000268
Eric Christopher824f42f2015-05-12 01:26:05 +0000269 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000270 if (!I) {
271 // This needs to be done before we create a new subtarget since any
272 // creation will depend on the TM and the code generation flags on the
273 // function that reside in TargetOptions.
274 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000275 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000276
277 if (!I->isThumb() && !I->hasARMOps())
278 F.getContext().emitError("Function '" + F.getName() + "' uses ARM "
279 "instructions, but the target does not support ARM mode execution.");
Diana Picus90f0a842016-11-15 15:38:15 +0000280 }
Florian Hahnd68bc7a2017-08-09 15:39:10 +0000281
Eric Christopher3faf2f12014-10-06 06:45:36 +0000282 return I.get();
283}
284
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000285TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000286 return TargetIRAnalysis([this](const Function &F) {
287 return TargetTransformInfo(ARMTTIImpl(this, F));
288 });
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)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000313 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000314
315 ARMBaseTargetMachine &getARMTargetMachine() const {
316 return getTM<ARMBaseTargetMachine>();
317 }
318
Javed Absar9e1ff862017-06-09 14:07:21 +0000319 ScheduleDAGInstrs *
320 createMachineScheduler(MachineSchedContext *C) const override {
321 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
322 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000323 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
324 if (ST.hasFusion())
325 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000326 return DAG;
327 }
328
329 ScheduleDAGInstrs *
330 createPostMachineScheduler(MachineSchedContext *C) const override {
331 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
332 // add DAG Mutations here.
Florian Hahnb489e562017-06-22 09:39:36 +0000333 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
334 if (ST.hasFusion())
335 DAG->addMutation(createARMMacroFusionDAGMutation());
Javed Absar9e1ff862017-06-09 14:07:21 +0000336 return DAG;
337 }
338
Tim Northoverb4ddc082014-05-30 10:09:59 +0000339 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000340 bool addPreISel() override;
341 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000342 bool addIRTranslator() override;
343 bool addLegalizeMachineIR() override;
344 bool addRegBankSelect() override;
345 bool addGlobalInstructionSelect() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000346 void addPreRegAlloc() override;
347 void addPreSched2() override;
348 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000349};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000350
Matthias Braune6ff30b2017-03-18 05:08:58 +0000351class ARMExecutionDepsFix : public ExecutionDepsFix {
352public:
353 static char ID;
354 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
355 StringRef getPassName() const override {
356 return "ARM Execution Dependency Fix";
357 }
358};
359char ARMExecutionDepsFix::ID;
360
Eugene Zelenko342257e2017-01-31 00:56:17 +0000361} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000362
Matthias Braune6ff30b2017-03-18 05:08:58 +0000363INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
364 "ARM Execution Dependency Fix", false, false)
365
Andrew Trickf8ea1082012-02-04 02:56:59 +0000366TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000367 return new ARMPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000368}
369
Tim Northoverb4ddc082014-05-30 10:09:59 +0000370void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000371 if (TM->Options.ThreadModel == ThreadModel::Single)
372 addPass(createLowerAtomicPass());
373 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000374 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000375
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000376 // Cmpxchg instructions are often used with a subsequent comparison to
377 // determine whether it succeeded. We can exploit existing control-flow in
378 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000379 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
380 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
381 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
382 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
383 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000384
385 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000386
387 // Match interleaved memory accesses to ldN/stN intrinsics.
388 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000389 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000390}
391
392bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000393 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000394 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000395 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000396 // FIXME: This is using the thumb1 only constant value for
397 // maximal global offset for merging globals. We may want
398 // to look into using the old value for non-thumb1 code of
399 // 4095 based on the TargetMachine, but this starts to become
400 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000401 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
402 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000403 // Merging of extern globals is enabled by default on non-Mach-O as we
404 // expect it to be generally either beneficial or harmless. On Mach-O it
405 // is disabled as we emit the .subsections_via_symbols directive which
406 // means that merging extern globals is not safe.
407 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
408 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
409 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000410 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000411
412 return false;
413}
414
Andrew Trickccb67362012-02-03 05:12:41 +0000415bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000416 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000417 return false;
418}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000419
Diana Picus22274932016-11-11 08:27:37 +0000420bool ARMPassConfig::addIRTranslator() {
421 addPass(new IRTranslator());
422 return false;
423}
424
425bool ARMPassConfig::addLegalizeMachineIR() {
426 addPass(new Legalizer());
427 return false;
428}
429
430bool ARMPassConfig::addRegBankSelect() {
431 addPass(new RegBankSelect());
432 return false;
433}
434
435bool ARMPassConfig::addGlobalInstructionSelect() {
436 addPass(new InstructionSelect());
437 return false;
438}
Diana Picus22274932016-11-11 08:27:37 +0000439
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000440void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000441 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000442 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000443
444 if (EnableARMLoadStoreOpt)
445 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
446
447 if (!DisableA15SDOptimization)
448 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000449 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000450}
451
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000452void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000453 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000454 if (EnableARMLoadStoreOpt)
455 addPass(createARMLoadStoreOptimizationPass());
456
Matthias Braune6ff30b2017-03-18 05:08:58 +0000457 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000458 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000459
Evan Cheng207b2462009-11-06 23:52:48 +0000460 // Expand some pseudo instructions into multiple instructions to allow
461 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000462 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000463
Evan Chengecb29082011-11-16 08:38:26 +0000464 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000465 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000466 addPass(createThumb2SizeReductionPass([this](const Function &F) {
467 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
468 }));
469
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000470 addPass(createIfConverter([](const MachineFunction &MF) {
471 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000472 }));
Renato Golin4c871392015-03-26 18:38:04 +0000473 }
Eric Christopher63b44882015-03-05 00:23:40 +0000474 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000475}
476
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000477void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000478 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000479
Eric Christopher63b44882015-03-05 00:23:40 +0000480 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000481 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
482 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000483 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000484
Davide Italiano141b28912015-05-20 21:40:38 +0000485 // Don't optimize barriers at -O0.
486 if (getOptLevel() != CodeGenOpt::None)
487 addPass(createARMOptimizeBarriersPass());
488
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000489 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000490}