blob: c0506cfda61299b4c743adaaa0a58bd60a2a204e [file] [log] [blame]
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00001//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
Diana Picus22274932016-11-11 08:27:37 +000013#include "ARM.h"
14#include "ARMCallLowering.h"
Diana Picus22274932016-11-11 08:27:37 +000015#include "ARMLegalizerInfo.h"
Aaron Ballman32346472017-05-09 15:12:03 +000016#ifdef LLVM_BUILD_GLOBAL_ISEL
Diana Picus22274932016-11-11 08:27:37 +000017#include "ARMRegisterBankInfo.h"
Aaron Ballman06297e82017-05-09 14:59:48 +000018#endif
Eugene Zelenko342257e2017-01-31 00:56:17 +000019#include "ARMSubtarget.h"
20#include "ARMTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000021#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000022#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000023#include "MCTargetDesc/ARMMCTargetDesc.h"
24#include "llvm/ADT/Optional.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000029#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000030#include "llvm/CodeGen/GlobalISel/CallLowering.h"
31#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
Diana Picus22274932016-11-11 08:27:37 +000032#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
33#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000034#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Diana Picus22274932016-11-11 08:27:37 +000035#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000036#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000037#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000038#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
39#include "llvm/CodeGen/MachineFunction.h"
Javed Absar9e1ff862017-06-09 14:07:21 +000040#include "llvm/CodeGen/MachineScheduler.h"
Evan Chengad3aac712007-05-16 02:01:49 +000041#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000042#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000043#include "llvm/IR/Attributes.h"
44#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000045#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000046#include "llvm/Pass.h"
47#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000048#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000049#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000050#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000051#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000052#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng10043e22007-01-19 07:51:42 +000053#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000054#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000055#include <cassert>
56#include <memory>
57#include <string>
58
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000059using namespace llvm;
60
Evan Chengf066b2f2011-08-25 01:00:36 +000061static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000062DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
63 cl::desc("Inhibit optimization of S->D register accesses on A15"),
64 cl::init(false));
65
Tim Northoverb4ddc082014-05-30 10:09:59 +000066static cl::opt<bool>
67EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
68 cl::desc("Run SimplifyCFG after expanding atomic operations"
69 " to make use of cmpxchg flow-based information"),
70 cl::init(true));
71
Renato Golin4c871392015-03-26 18:38:04 +000072static cl::opt<bool>
73EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
74 cl::desc("Enable ARM load/store optimization pass"),
75 cl::init(true));
76
Ahmed Bougachab96444e2015-04-11 00:06:36 +000077// FIXME: Unify control over GlobalMerge.
78static cl::opt<cl::boolOrDefault>
79EnableGlobalMerge("arm-global-merge", cl::Hidden,
80 cl::desc("Enable the global merge pass"));
81
Matthias Braune6ff30b2017-03-18 05:08:58 +000082namespace llvm {
83 void initializeARMExecutionDepsFixPass(PassRegistry&);
84}
85
Jim Grosbachf24f9d92009-08-11 15:33:49 +000086extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000087 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000088 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000089 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
Mehdi Aminif42454b2016-10-09 23:00:34 +000090 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000091 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000092
93 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000094 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000095 initializeARMLoadStoreOptPass(Registry);
96 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000097 initializeARMConstantIslandsPass(Registry);
Matthias Braune6ff30b2017-03-18 05:08:58 +000098 initializeARMExecutionDepsFixPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000099}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000100
Aditya Nandakumara2719322014-11-13 09:26:31 +0000101static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
102 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000103 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000104 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000105 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
106 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000107}
108
Eric Christopher661f2d12014-12-18 02:20:58 +0000109static ARMBaseTargetMachine::ARMABI
110computeTargetABI(const Triple &TT, StringRef CPU,
111 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000112 if (Options.MCOptions.getABIName() == "aapcs16")
113 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
114 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000115 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +0000116 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000117 return ARMBaseTargetMachine::ARM_ABI_APCS;
118
Eric Christopher6e30cd92015-01-14 00:50:31 +0000119 assert(Options.MCOptions.getABIName().empty() &&
120 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +0000121
122 ARMBaseTargetMachine::ARMABI TargetABI =
123 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
124
Eugene Zelenko342257e2017-01-31 00:56:17 +0000125 unsigned ArchKind = ARM::parseCPUArch(CPU);
126 StringRef ArchName = ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +0000127 // FIXME: This is duplicated code from the front end and should be unified.
128 if (TT.isOSBinFormatMachO()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000129 if (TT.getEnvironment() == Triple::EABI ||
130 (TT.getOS() == Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
131 ARM::parseArchProfile(ArchName) == ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +0000132 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +0000133 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000134 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +0000135 } else {
136 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
137 }
138 } else if (TT.isOSWindows()) {
139 // FIXME: this is invalid for WindowsCE
140 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
141 } else {
142 // Select the default based on the platform.
143 switch (TT.getEnvironment()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000144 case Triple::Android:
145 case Triple::GNUEABI:
146 case Triple::GNUEABIHF:
147 case Triple::MuslEABI:
148 case Triple::MuslEABIHF:
149 case Triple::EABIHF:
150 case Triple::EABI:
Eric Christopher661f2d12014-12-18 02:20:58 +0000151 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
152 break;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000153 case Triple::GNU:
Eric Christopher661f2d12014-12-18 02:20:58 +0000154 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
155 break;
156 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000157 if (TT.isOSNetBSD())
158 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000159 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000160 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000161 break;
162 }
163 }
164
165 return TargetABI;
166}
167
Daniel Sandersed64d622015-06-11 15:34:59 +0000168static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000169 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000170 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000171 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000172 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000173
174 if (isLittle)
175 // Little endian.
176 Ret += "e";
177 else
178 // Big endian.
179 Ret += "E";
180
Daniel Sandersed64d622015-06-11 15:34:59 +0000181 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000182
183 // Pointers are 32 bits and aligned to 32 bits.
184 Ret += "-p:32:32";
185
186 // ABIs other than APCS have 64 bit integers with natural alignment.
187 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
188 Ret += "-i64:64";
189
190 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
191 // bits, others to 64 bits. We always try to align to 64 bits.
192 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
193 Ret += "-f64:32:64";
194
195 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
196 // to 64. We always ty to give them natural alignment.
197 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
198 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000199 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000200 Ret += "-v128:64:128";
201
202 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
203 // particular hardware support on 32-bit ARM).
204 Ret += "-a:0:32";
205
206 // Integer registers are 32 bits.
207 Ret += "-n32";
208
209 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
210 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000211 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000212 Ret += "-S128";
213 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
214 Ret += "-S64";
215 else
216 Ret += "-S32";
217
218 return Ret;
219}
220
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000221static Reloc::Model getEffectiveRelocModel(const Triple &TT,
222 Optional<Reloc::Model> RM) {
223 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000224 // Default relocation model on Darwin is PIC.
225 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000226
Oliver Stannard8331aae2016-08-08 15:28:31 +0000227 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
228 assert(TT.isOSBinFormatELF() &&
229 "ROPI/RWPI currently only supported for ELF");
230
Renato Golin9be886292016-05-28 04:47:13 +0000231 // DynamicNoPIC is only used on darwin.
232 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
233 return Reloc::Static;
234
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000235 return *RM;
236}
237
Rafael Espindola38af4d62016-05-18 16:00:24 +0000238/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000239///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000240ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000241 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000242 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000243 Optional<Reloc::Model> RM,
244 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000245 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000246 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000247 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
248 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000249 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000250 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000251 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000252
253 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000254 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000255 this->Options.FloatABIType =
256 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000257
258 // Default to triple-appropriate EABI
259 if (Options.EABIVersion == EABI::Default ||
260 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000261 // musl is compatible with glibc with regard to EABI version
262 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000263 this->Options.EABIVersion = EABI::GNU;
264 else
265 this->Options.EABIVersion = EABI::EABI5;
266 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000267
268 initAsmInfo();
269 if (!Subtarget.isThumb() && !Subtarget.hasARMOps())
270 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
271 "support ARM mode execution!");
Evan Cheng66cff402008-10-30 16:10:54 +0000272}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000273
Eugene Zelenko342257e2017-01-31 00:56:17 +0000274ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000275
Diana Picus22274932016-11-11 08:27:37 +0000276#ifdef LLVM_BUILD_GLOBAL_ISEL
277namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000278
Diana Picus22274932016-11-11 08:27:37 +0000279struct ARMGISelActualAccessor : public GISelAccessor {
280 std::unique_ptr<CallLowering> CallLoweringInfo;
281 std::unique_ptr<InstructionSelector> InstSelector;
282 std::unique_ptr<LegalizerInfo> Legalizer;
283 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000284
Diana Picus22274932016-11-11 08:27:37 +0000285 const CallLowering *getCallLowering() const override {
286 return CallLoweringInfo.get();
287 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000288
Diana Picus22274932016-11-11 08:27:37 +0000289 const InstructionSelector *getInstructionSelector() const override {
290 return InstSelector.get();
291 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000292
Ahmed Bougacha52286032016-12-15 18:45:30 +0000293 const LegalizerInfo *getLegalizerInfo() const override {
Diana Picus22274932016-11-11 08:27:37 +0000294 return Legalizer.get();
295 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000296
Diana Picus22274932016-11-11 08:27:37 +0000297 const RegisterBankInfo *getRegBankInfo() const override {
298 return RegBankInfo.get();
299 }
300};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000301
302} // end anonymous namespace
Diana Picus22274932016-11-11 08:27:37 +0000303#endif
304
Eric Christopher3faf2f12014-10-06 06:45:36 +0000305const ARMSubtarget *
306ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000307 Attribute CPUAttr = F.getFnAttribute("target-cpu");
308 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000309
310 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
311 ? CPUAttr.getValueAsString().str()
312 : TargetCPU;
313 std::string FS = !FSAttr.hasAttribute(Attribute::None)
314 ? FSAttr.getValueAsString().str()
315 : TargetFS;
316
317 // FIXME: This is related to the code below to reset the target options,
318 // we need to know whether or not the soft float flag is set on the
319 // function before we can generate a subtarget. We also need to use
320 // it as a key for the subtarget since that can be the only difference
321 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000322 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000323 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
324 // If the soft float attribute is set on the function turn on the soft float
325 // subtarget feature.
326 if (SoftFloat)
327 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000328
Eric Christopher824f42f2015-05-12 01:26:05 +0000329 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000330 if (!I) {
331 // This needs to be done before we create a new subtarget since any
332 // creation will depend on the TM and the code generation flags on the
333 // function that reside in TargetOptions.
334 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000335 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000336
337#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000338 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000339#else
Diana Picus90f0a842016-11-15 15:38:15 +0000340 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
341 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
Diana Picus7cab0782017-02-17 11:25:17 +0000342 GISel->Legalizer.reset(new ARMLegalizerInfo(*I));
Diana Picus22274932016-11-11 08:27:37 +0000343
Diana Picus90f0a842016-11-15 15:38:15 +0000344 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000345
Diana Picus90f0a842016-11-15 15:38:15 +0000346 // FIXME: At this point, we can't rely on Subtarget having RBI.
347 // It's awkward to mix passing RBI and the Subtarget; should we pass
348 // TII/TRI as well?
Diana Picus8abcbbb2017-05-02 09:40:49 +0000349 GISel->InstSelector.reset(createARMInstructionSelector(*this, *I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000350
Diana Picus90f0a842016-11-15 15:38:15 +0000351 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000352#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000353 I->setGISelAccessor(*GISel);
354 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000355 return I.get();
356}
357
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000358TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000359 return TargetIRAnalysis([this](const Function &F) {
360 return TargetTransformInfo(ARMTTIImpl(this, F));
361 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000362}
363
Christian Pirker2a111602014-03-28 14:35:30 +0000364
Daniel Sanders3e5de882015-06-11 19:41:26 +0000365ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000366 StringRef CPU, StringRef FS,
367 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000368 Optional<Reloc::Model> RM,
369 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000370 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000371 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000372
Daniel Sanders3e5de882015-06-11 19:41:26 +0000373ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000374 StringRef CPU, StringRef FS,
375 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000376 Optional<Reloc::Model> RM,
377 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000378 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000379 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000380
Andrew Trickccb67362012-02-03 05:12:41 +0000381namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000382
Andrew Trickccb67362012-02-03 05:12:41 +0000383/// ARM Code Generator Pass Configuration Options.
384class ARMPassConfig : public TargetPassConfig {
385public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000386 ARMPassConfig(ARMBaseTargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000387 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000388
389 ARMBaseTargetMachine &getARMTargetMachine() const {
390 return getTM<ARMBaseTargetMachine>();
391 }
392
Javed Absar9e1ff862017-06-09 14:07:21 +0000393 ScheduleDAGInstrs *
394 createMachineScheduler(MachineSchedContext *C) const override {
395 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
396 // add DAG Mutations here.
397 return DAG;
398 }
399
400 ScheduleDAGInstrs *
401 createPostMachineScheduler(MachineSchedContext *C) const override {
402 ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
403 // add DAG Mutations here.
404 return DAG;
405 }
406
Tim Northoverb4ddc082014-05-30 10:09:59 +0000407 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000408 bool addPreISel() override;
409 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000410#ifdef LLVM_BUILD_GLOBAL_ISEL
411 bool addIRTranslator() override;
412 bool addLegalizeMachineIR() override;
413 bool addRegBankSelect() override;
414 bool addGlobalInstructionSelect() override;
415#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000416 void addPreRegAlloc() override;
417 void addPreSched2() override;
418 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000419};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000420
Matthias Braune6ff30b2017-03-18 05:08:58 +0000421class ARMExecutionDepsFix : public ExecutionDepsFix {
422public:
423 static char ID;
424 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
425 StringRef getPassName() const override {
426 return "ARM Execution Dependency Fix";
427 }
428};
429char ARMExecutionDepsFix::ID;
430
Eugene Zelenko342257e2017-01-31 00:56:17 +0000431} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000432
Matthias Braune6ff30b2017-03-18 05:08:58 +0000433INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
434 "ARM Execution Dependency Fix", false, false)
435
Andrew Trickf8ea1082012-02-04 02:56:59 +0000436TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000437 return new ARMPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000438}
439
Tim Northoverb4ddc082014-05-30 10:09:59 +0000440void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000441 if (TM->Options.ThreadModel == ThreadModel::Single)
442 addPass(createLowerAtomicPass());
443 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000444 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000445
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000446 // Cmpxchg instructions are often used with a subsequent comparison to
447 // determine whether it succeeded. We can exploit existing control-flow in
448 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000449 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
450 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
451 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
452 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
453 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000454
455 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000456
457 // Match interleaved memory accesses to ldN/stN intrinsics.
458 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000459 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000460}
461
462bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000463 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000464 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000465 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000466 // FIXME: This is using the thumb1 only constant value for
467 // maximal global offset for merging globals. We may want
468 // to look into using the old value for non-thumb1 code of
469 // 4095 based on the TargetMachine, but this starts to become
470 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000471 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
472 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000473 // Merging of extern globals is enabled by default on non-Mach-O as we
474 // expect it to be generally either beneficial or harmless. On Mach-O it
475 // is disabled as we emit the .subsections_via_symbols directive which
476 // means that merging extern globals is not safe.
477 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
478 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
479 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000480 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000481
482 return false;
483}
484
Andrew Trickccb67362012-02-03 05:12:41 +0000485bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000486 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000487 return false;
488}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000489
Diana Picus22274932016-11-11 08:27:37 +0000490#ifdef LLVM_BUILD_GLOBAL_ISEL
491bool ARMPassConfig::addIRTranslator() {
492 addPass(new IRTranslator());
493 return false;
494}
495
496bool ARMPassConfig::addLegalizeMachineIR() {
497 addPass(new Legalizer());
498 return false;
499}
500
501bool ARMPassConfig::addRegBankSelect() {
502 addPass(new RegBankSelect());
503 return false;
504}
505
506bool ARMPassConfig::addGlobalInstructionSelect() {
507 addPass(new InstructionSelect());
508 return false;
509}
510#endif
511
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000512void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000513 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000514 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000515
516 if (EnableARMLoadStoreOpt)
517 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
518
519 if (!DisableA15SDOptimization)
520 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000521 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000522}
523
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000524void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000525 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000526 if (EnableARMLoadStoreOpt)
527 addPass(createARMLoadStoreOptimizationPass());
528
Matthias Braune6ff30b2017-03-18 05:08:58 +0000529 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000530 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000531
Evan Cheng207b2462009-11-06 23:52:48 +0000532 // Expand some pseudo instructions into multiple instructions to allow
533 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000534 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000535
Evan Chengecb29082011-11-16 08:38:26 +0000536 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000537 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000538 addPass(createThumb2SizeReductionPass([this](const Function &F) {
539 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
540 }));
541
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000542 addPass(createIfConverter([](const MachineFunction &MF) {
543 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000544 }));
Renato Golin4c871392015-03-26 18:38:04 +0000545 }
Eric Christopher63b44882015-03-05 00:23:40 +0000546 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000547}
548
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000549void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000550 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000551
Eric Christopher63b44882015-03-05 00:23:40 +0000552 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000553 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
554 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000555 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000556
Davide Italiano141b28912015-05-20 21:40:38 +0000557 // Don't optimize barriers at -O0.
558 if (getOptLevel() != CodeGenOpt::None)
559 addPass(createARMOptimizeBarriersPass());
560
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000561 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000562}