blob: d09f3ecbaa2825a1e816f2cb9492234c85f18847 [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"
16#include "ARMRegisterBankInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000017#include "ARMSubtarget.h"
18#include "ARMTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000019#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000020#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000021#include "MCTargetDesc/ARMMCTargetDesc.h"
22#include "llvm/ADT/Optional.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Triple.h"
26#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000027#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000028#include "llvm/CodeGen/GlobalISel/CallLowering.h"
29#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
Diana Picus22274932016-11-11 08:27:37 +000030#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
31#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000032#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
Diana Picus22274932016-11-11 08:27:37 +000033#include "llvm/CodeGen/GlobalISel/Legalizer.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000034#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000035#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000036#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
37#include "llvm/CodeGen/MachineFunction.h"
Evan Chengad3aac712007-05-16 02:01:49 +000038#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000039#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000040#include "llvm/IR/Attributes.h"
41#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000042#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000043#include "llvm/Pass.h"
44#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000045#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000046#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000047#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000048#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000049#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng10043e22007-01-19 07:51:42 +000050#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000051#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000052#include <cassert>
53#include <memory>
54#include <string>
55
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000056using namespace llvm;
57
Evan Chengf066b2f2011-08-25 01:00:36 +000058static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000059DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
60 cl::desc("Inhibit optimization of S->D register accesses on A15"),
61 cl::init(false));
62
Tim Northoverb4ddc082014-05-30 10:09:59 +000063static cl::opt<bool>
64EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
65 cl::desc("Run SimplifyCFG after expanding atomic operations"
66 " to make use of cmpxchg flow-based information"),
67 cl::init(true));
68
Renato Golin4c871392015-03-26 18:38:04 +000069static cl::opt<bool>
70EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
71 cl::desc("Enable ARM load/store optimization pass"),
72 cl::init(true));
73
Ahmed Bougachab96444e2015-04-11 00:06:36 +000074// FIXME: Unify control over GlobalMerge.
75static cl::opt<cl::boolOrDefault>
76EnableGlobalMerge("arm-global-merge", cl::Hidden,
77 cl::desc("Enable the global merge pass"));
78
Matthias Braune6ff30b2017-03-18 05:08:58 +000079namespace llvm {
80 void initializeARMExecutionDepsFixPass(PassRegistry&);
81}
82
Jim Grosbachf24f9d92009-08-11 15:33:49 +000083extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000084 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000085 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
86 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
87 RegisterTargetMachine<ThumbLETargetMachine> A(getTheThumbLETarget());
88 RegisterTargetMachine<ThumbBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000089
90 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000091 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000092 initializeARMLoadStoreOptPass(Registry);
93 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000094 initializeARMConstantIslandsPass(Registry);
Matthias Braune6ff30b2017-03-18 05:08:58 +000095 initializeARMExecutionDepsFixPass(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) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000109 if (Options.MCOptions.getABIName() == "aapcs16")
110 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
111 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000112 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +0000113 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000114 return ARMBaseTargetMachine::ARM_ABI_APCS;
115
Eric Christopher6e30cd92015-01-14 00:50:31 +0000116 assert(Options.MCOptions.getABIName().empty() &&
117 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +0000118
119 ARMBaseTargetMachine::ARMABI TargetABI =
120 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
121
Eugene Zelenko342257e2017-01-31 00:56:17 +0000122 unsigned ArchKind = ARM::parseCPUArch(CPU);
123 StringRef ArchName = ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +0000124 // FIXME: This is duplicated code from the front end and should be unified.
125 if (TT.isOSBinFormatMachO()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000126 if (TT.getEnvironment() == Triple::EABI ||
127 (TT.getOS() == Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
128 ARM::parseArchProfile(ArchName) == ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +0000129 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +0000130 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000131 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +0000132 } else {
133 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
134 }
135 } else if (TT.isOSWindows()) {
136 // FIXME: this is invalid for WindowsCE
137 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
138 } else {
139 // Select the default based on the platform.
140 switch (TT.getEnvironment()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000141 case Triple::Android:
142 case Triple::GNUEABI:
143 case Triple::GNUEABIHF:
144 case Triple::MuslEABI:
145 case Triple::MuslEABIHF:
146 case Triple::EABIHF:
147 case Triple::EABI:
Eric Christopher661f2d12014-12-18 02:20:58 +0000148 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
149 break;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000150 case Triple::GNU:
Eric Christopher661f2d12014-12-18 02:20:58 +0000151 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
152 break;
153 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000154 if (TT.isOSNetBSD())
155 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000156 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000157 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000158 break;
159 }
160 }
161
162 return TargetABI;
163}
164
Daniel Sandersed64d622015-06-11 15:34:59 +0000165static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000166 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000167 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000168 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000169 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000170
171 if (isLittle)
172 // Little endian.
173 Ret += "e";
174 else
175 // Big endian.
176 Ret += "E";
177
Daniel Sandersed64d622015-06-11 15:34:59 +0000178 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000179
180 // Pointers are 32 bits and aligned to 32 bits.
181 Ret += "-p:32:32";
182
183 // ABIs other than APCS have 64 bit integers with natural alignment.
184 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
185 Ret += "-i64:64";
186
187 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
188 // bits, others to 64 bits. We always try to align to 64 bits.
189 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
190 Ret += "-f64:32:64";
191
192 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
193 // to 64. We always ty to give them natural alignment.
194 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
195 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000196 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000197 Ret += "-v128:64:128";
198
199 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
200 // particular hardware support on 32-bit ARM).
201 Ret += "-a:0:32";
202
203 // Integer registers are 32 bits.
204 Ret += "-n32";
205
206 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
207 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000208 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000209 Ret += "-S128";
210 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
211 Ret += "-S64";
212 else
213 Ret += "-S32";
214
215 return Ret;
216}
217
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000218static Reloc::Model getEffectiveRelocModel(const Triple &TT,
219 Optional<Reloc::Model> RM) {
220 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000221 // Default relocation model on Darwin is PIC.
222 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000223
Oliver Stannard8331aae2016-08-08 15:28:31 +0000224 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
225 assert(TT.isOSBinFormatELF() &&
226 "ROPI/RWPI currently only supported for ELF");
227
Renato Golin9be886292016-05-28 04:47:13 +0000228 // DynamicNoPIC is only used on darwin.
229 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
230 return Reloc::Static;
231
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000232 return *RM;
233}
234
Rafael Espindola38af4d62016-05-18 16:00:24 +0000235/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000236///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000237ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000238 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000239 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000240 Optional<Reloc::Model> RM,
241 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000242 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000243 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000244 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
245 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000246 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000247 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000248 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000249
250 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000251 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000252 this->Options.FloatABIType =
253 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000254
255 // Default to triple-appropriate EABI
256 if (Options.EABIVersion == EABI::Default ||
257 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000258 // musl is compatible with glibc with regard to EABI version
259 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000260 this->Options.EABIVersion = EABI::GNU;
261 else
262 this->Options.EABIVersion = EABI::EABI5;
263 }
Evan Cheng66cff402008-10-30 16:10:54 +0000264}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000265
Eugene Zelenko342257e2017-01-31 00:56:17 +0000266ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000267
Diana Picus22274932016-11-11 08:27:37 +0000268#ifdef LLVM_BUILD_GLOBAL_ISEL
269namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000270
Diana Picus22274932016-11-11 08:27:37 +0000271struct ARMGISelActualAccessor : public GISelAccessor {
272 std::unique_ptr<CallLowering> CallLoweringInfo;
273 std::unique_ptr<InstructionSelector> InstSelector;
274 std::unique_ptr<LegalizerInfo> Legalizer;
275 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000276
Diana Picus22274932016-11-11 08:27:37 +0000277 const CallLowering *getCallLowering() const override {
278 return CallLoweringInfo.get();
279 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000280
Diana Picus22274932016-11-11 08:27:37 +0000281 const InstructionSelector *getInstructionSelector() const override {
282 return InstSelector.get();
283 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000284
Ahmed Bougacha52286032016-12-15 18:45:30 +0000285 const LegalizerInfo *getLegalizerInfo() const override {
Diana Picus22274932016-11-11 08:27:37 +0000286 return Legalizer.get();
287 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000288
Diana Picus22274932016-11-11 08:27:37 +0000289 const RegisterBankInfo *getRegBankInfo() const override {
290 return RegBankInfo.get();
291 }
292};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000293
294} // end anonymous namespace
Diana Picus22274932016-11-11 08:27:37 +0000295#endif
296
Eric Christopher3faf2f12014-10-06 06:45:36 +0000297const ARMSubtarget *
298ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000299 Attribute CPUAttr = F.getFnAttribute("target-cpu");
300 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000301
302 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
303 ? CPUAttr.getValueAsString().str()
304 : TargetCPU;
305 std::string FS = !FSAttr.hasAttribute(Attribute::None)
306 ? FSAttr.getValueAsString().str()
307 : TargetFS;
308
309 // FIXME: This is related to the code below to reset the target options,
310 // we need to know whether or not the soft float flag is set on the
311 // function before we can generate a subtarget. We also need to use
312 // it as a key for the subtarget since that can be the only difference
313 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000314 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000315 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
316 // If the soft float attribute is set on the function turn on the soft float
317 // subtarget feature.
318 if (SoftFloat)
319 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000320
Eric Christopher824f42f2015-05-12 01:26:05 +0000321 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000322 if (!I) {
323 // This needs to be done before we create a new subtarget since any
324 // creation will depend on the TM and the code generation flags on the
325 // function that reside in TargetOptions.
326 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000327 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000328
329#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000330 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000331#else
Diana Picus90f0a842016-11-15 15:38:15 +0000332 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
333 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
Diana Picus7cab0782017-02-17 11:25:17 +0000334 GISel->Legalizer.reset(new ARMLegalizerInfo(*I));
Diana Picus22274932016-11-11 08:27:37 +0000335
Diana Picus90f0a842016-11-15 15:38:15 +0000336 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000337
Diana Picus90f0a842016-11-15 15:38:15 +0000338 // FIXME: At this point, we can't rely on Subtarget having RBI.
339 // It's awkward to mix passing RBI and the Subtarget; should we pass
340 // TII/TRI as well?
Diana Picus8abcbbb2017-05-02 09:40:49 +0000341 GISel->InstSelector.reset(createARMInstructionSelector(*this, *I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000342
Diana Picus90f0a842016-11-15 15:38:15 +0000343 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000344#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000345 I->setGISelAccessor(*GISel);
346 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000347 return I.get();
348}
349
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000350TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000351 return TargetIRAnalysis([this](const Function &F) {
352 return TargetTransformInfo(ARMTTIImpl(this, F));
353 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000354}
355
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000356void ARMTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000357
Daniel Sanders3e5de882015-06-11 19:41:26 +0000358ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT,
359 StringRef CPU, StringRef FS,
360 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000361 Optional<Reloc::Model> RM,
362 CodeModel::Model CM, CodeGenOpt::Level OL,
363 bool isLittle)
Eric Christopher80b24ef2014-06-26 19:30:02 +0000364 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000365 initAsmInfo();
Evan Cheng5190f092010-08-11 07:17:46 +0000366 if (!Subtarget.hasARMOps())
367 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
368 "support ARM mode execution!");
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000369}
370
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000371void ARMLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000372
Daniel Sanders3e5de882015-06-11 19:41:26 +0000373ARMLETargetMachine::ARMLETargetMachine(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)
379 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000380
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000381void ARMBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000382
Daniel Sanders3e5de882015-06-11 19:41:26 +0000383ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000384 StringRef CPU, StringRef FS,
385 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000386 Optional<Reloc::Model> RM,
387 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000388 CodeGenOpt::Level OL)
389 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000390
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000391void ThumbTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000392
Daniel Sanders3e5de882015-06-11 19:41:26 +0000393ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000394 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000395 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000396 Optional<Reloc::Model> RM,
397 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000398 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000399 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000400 initAsmInfo();
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000401}
402
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000403void ThumbLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000404
Daniel Sanders3e5de882015-06-11 19:41:26 +0000405ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000406 StringRef CPU, StringRef FS,
407 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000408 Optional<Reloc::Model> RM,
409 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000410 CodeGenOpt::Level OL)
411 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000412
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000413void ThumbBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000414
Daniel Sanders3e5de882015-06-11 19:41:26 +0000415ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000416 StringRef CPU, StringRef FS,
417 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000418 Optional<Reloc::Model> RM,
419 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000420 CodeGenOpt::Level OL)
421 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000422
Andrew Trickccb67362012-02-03 05:12:41 +0000423namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000424
Andrew Trickccb67362012-02-03 05:12:41 +0000425/// ARM Code Generator Pass Configuration Options.
426class ARMPassConfig : public TargetPassConfig {
427public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000428 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
429 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000430
431 ARMBaseTargetMachine &getARMTargetMachine() const {
432 return getTM<ARMBaseTargetMachine>();
433 }
434
Tim Northoverb4ddc082014-05-30 10:09:59 +0000435 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000436 bool addPreISel() override;
437 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000438#ifdef LLVM_BUILD_GLOBAL_ISEL
439 bool addIRTranslator() override;
440 bool addLegalizeMachineIR() override;
441 bool addRegBankSelect() override;
442 bool addGlobalInstructionSelect() override;
443#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000444 void addPreRegAlloc() override;
445 void addPreSched2() override;
446 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000447};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000448
Matthias Braune6ff30b2017-03-18 05:08:58 +0000449class ARMExecutionDepsFix : public ExecutionDepsFix {
450public:
451 static char ID;
452 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
453 StringRef getPassName() const override {
454 return "ARM Execution Dependency Fix";
455 }
456};
457char ARMExecutionDepsFix::ID;
458
Eugene Zelenko342257e2017-01-31 00:56:17 +0000459} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000460
Matthias Braune6ff30b2017-03-18 05:08:58 +0000461INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
462 "ARM Execution Dependency Fix", false, false)
463
Andrew Trickf8ea1082012-02-04 02:56:59 +0000464TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
465 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000466}
467
Tim Northoverb4ddc082014-05-30 10:09:59 +0000468void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000469 if (TM->Options.ThreadModel == ThreadModel::Single)
470 addPass(createLowerAtomicPass());
471 else
Robin Morisset59c23cd2014-08-21 21:50:01 +0000472 addPass(createAtomicExpandPass(TM));
Tim Northoverc882eb02014-04-03 11:44:58 +0000473
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000474 // Cmpxchg instructions are often used with a subsequent comparison to
475 // determine whether it succeeded. We can exploit existing control-flow in
476 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000477 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
478 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
479 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
480 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
481 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000482
483 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000484
485 // Match interleaved memory accesses to ldN/stN intrinsics.
486 if (TM->getOptLevel() != CodeGenOpt::None)
487 addPass(createInterleavedAccessPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000488}
489
490bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000491 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000492 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000493 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000494 // FIXME: This is using the thumb1 only constant value for
495 // maximal global offset for merging globals. We may want
496 // to look into using the old value for non-thumb1 code of
497 // 4095 based on the TargetMachine, but this starts to become
498 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000499 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
500 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000501 // Merging of extern globals is enabled by default on non-Mach-O as we
502 // expect it to be generally either beneficial or harmless. On Mach-O it
503 // is disabled as we emit the .subsections_via_symbols directive which
504 // means that merging extern globals is not safe.
505 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
506 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
507 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000508 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000509
510 return false;
511}
512
Andrew Trickccb67362012-02-03 05:12:41 +0000513bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000514 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000515 return false;
516}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000517
Diana Picus22274932016-11-11 08:27:37 +0000518#ifdef LLVM_BUILD_GLOBAL_ISEL
519bool ARMPassConfig::addIRTranslator() {
520 addPass(new IRTranslator());
521 return false;
522}
523
524bool ARMPassConfig::addLegalizeMachineIR() {
525 addPass(new Legalizer());
526 return false;
527}
528
529bool ARMPassConfig::addRegBankSelect() {
530 addPass(new RegBankSelect());
531 return false;
532}
533
534bool ARMPassConfig::addGlobalInstructionSelect() {
535 addPass(new InstructionSelect());
536 return false;
537}
538#endif
539
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000540void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000541 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000542 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000543
544 if (EnableARMLoadStoreOpt)
545 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
546
547 if (!DisableA15SDOptimization)
548 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000549 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000550}
551
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000552void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000553 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000554 if (EnableARMLoadStoreOpt)
555 addPass(createARMLoadStoreOptimizationPass());
556
Matthias Braune6ff30b2017-03-18 05:08:58 +0000557 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000558 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000559
Evan Cheng207b2462009-11-06 23:52:48 +0000560 // Expand some pseudo instructions into multiple instructions to allow
561 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000562 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000563
Evan Chengecb29082011-11-16 08:38:26 +0000564 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000565 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000566 addPass(createThumb2SizeReductionPass([this](const Function &F) {
567 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
568 }));
569
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000570 addPass(createIfConverter([](const MachineFunction &MF) {
571 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000572 }));
Renato Golin4c871392015-03-26 18:38:04 +0000573 }
Eric Christopher63b44882015-03-05 00:23:40 +0000574 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000575}
576
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000577void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000578 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000579
Eric Christopher63b44882015-03-05 00:23:40 +0000580 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000581 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
582 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000583 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000584
Davide Italiano141b28912015-05-20 21:40:38 +0000585 // Don't optimize barriers at -O0.
586 if (getOptLevel() != CodeGenOpt::None)
587 addPass(createARMOptimizeBarriersPass());
588
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000589 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000590}