blob: 51982b2dab14cd694683f482d0b0ff28c28913d6 [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"
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"
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 {
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)
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
Matthias Braune6ff30b2017-03-18 05:08:58 +0000358class ARMExecutionDepsFix : public ExecutionDepsFix {
359public:
360 static char ID;
361 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
362 StringRef getPassName() const override {
363 return "ARM Execution Dependency Fix";
364 }
365};
366char ARMExecutionDepsFix::ID;
367
Eugene Zelenko342257e2017-01-31 00:56:17 +0000368} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000369
Matthias Braune6ff30b2017-03-18 05:08:58 +0000370INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
371 "ARM Execution Dependency Fix", false, false)
372
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
400bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000401 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000402 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000403 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000404 // FIXME: This is using the thumb1 only constant value for
405 // maximal global offset for merging globals. We may want
406 // to look into using the old value for non-thumb1 code of
407 // 4095 based on the TargetMachine, but this starts to become
408 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000409 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
410 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000411 // Merging of extern globals is enabled by default on non-Mach-O as we
412 // expect it to be generally either beneficial or harmless. On Mach-O it
413 // is disabled as we emit the .subsections_via_symbols directive which
414 // means that merging extern globals is not safe.
415 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
416 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
417 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000418 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000419
420 return false;
421}
422
Andrew Trickccb67362012-02-03 05:12:41 +0000423bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000424 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000425 return false;
426}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000427
Diana Picus22274932016-11-11 08:27:37 +0000428bool ARMPassConfig::addIRTranslator() {
429 addPass(new IRTranslator());
430 return false;
431}
432
433bool ARMPassConfig::addLegalizeMachineIR() {
434 addPass(new Legalizer());
435 return false;
436}
437
438bool ARMPassConfig::addRegBankSelect() {
439 addPass(new RegBankSelect());
440 return false;
441}
442
443bool ARMPassConfig::addGlobalInstructionSelect() {
444 addPass(new InstructionSelect());
445 return false;
446}
Diana Picus22274932016-11-11 08:27:37 +0000447
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000448void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000449 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000450 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000451
452 if (EnableARMLoadStoreOpt)
453 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
454
455 if (!DisableA15SDOptimization)
456 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000457 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000458}
459
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000460void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000461 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000462 if (EnableARMLoadStoreOpt)
463 addPass(createARMLoadStoreOptimizationPass());
464
Matthias Braune6ff30b2017-03-18 05:08:58 +0000465 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000466 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000467
Evan Cheng207b2462009-11-06 23:52:48 +0000468 // Expand some pseudo instructions into multiple instructions to allow
469 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000470 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000471
Evan Chengecb29082011-11-16 08:38:26 +0000472 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000473 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000474 addPass(createThumb2SizeReductionPass([this](const Function &F) {
475 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
476 }));
477
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000478 addPass(createIfConverter([](const MachineFunction &MF) {
479 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000480 }));
Renato Golin4c871392015-03-26 18:38:04 +0000481 }
Eric Christopher63b44882015-03-05 00:23:40 +0000482 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000483}
484
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000485void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000486 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000487
Eric Christopher63b44882015-03-05 00:23:40 +0000488 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000489 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
490 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000491 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000492
Davide Italiano141b28912015-05-20 21:40:38 +0000493 // Don't optimize barriers at -O0.
494 if (getOptLevel() != CodeGenOpt::None)
495 addPass(createARMOptimizeBarriersPass());
496
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000497 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000498}