blob: c4f23c66e4eaba73cb77532e49717b0f86914d5f [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"
Evan Chengad3aac712007-05-16 02:01:49 +000040#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000041#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000042#include "llvm/IR/Attributes.h"
43#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000044#include "llvm/IR/Function.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000045#include "llvm/Pass.h"
46#include "llvm/Support/CodeGen.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000047#include "llvm/Support/CommandLine.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000048#include "llvm/Support/ErrorHandling.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000049#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000050#include "llvm/Support/TargetRegistry.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000051#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng10043e22007-01-19 07:51:42 +000052#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000053#include "llvm/Transforms/Scalar.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000054#include <cassert>
55#include <memory>
56#include <string>
57
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000058using namespace llvm;
59
Evan Chengf066b2f2011-08-25 01:00:36 +000060static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000061DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
62 cl::desc("Inhibit optimization of S->D register accesses on A15"),
63 cl::init(false));
64
Tim Northoverb4ddc082014-05-30 10:09:59 +000065static cl::opt<bool>
66EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
67 cl::desc("Run SimplifyCFG after expanding atomic operations"
68 " to make use of cmpxchg flow-based information"),
69 cl::init(true));
70
Renato Golin4c871392015-03-26 18:38:04 +000071static cl::opt<bool>
72EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
73 cl::desc("Enable ARM load/store optimization pass"),
74 cl::init(true));
75
Ahmed Bougachab96444e2015-04-11 00:06:36 +000076// FIXME: Unify control over GlobalMerge.
77static cl::opt<cl::boolOrDefault>
78EnableGlobalMerge("arm-global-merge", cl::Hidden,
79 cl::desc("Enable the global merge pass"));
80
Matthias Braune6ff30b2017-03-18 05:08:58 +000081namespace llvm {
82 void initializeARMExecutionDepsFixPass(PassRegistry&);
83}
84
Jim Grosbachf24f9d92009-08-11 15:33:49 +000085extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000086 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000087 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000088 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
Mehdi Aminif42454b2016-10-09 23:00:34 +000089 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
Florian Hahnd211fe72017-05-24 10:18:57 +000090 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000091
92 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000093 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000094 initializeARMLoadStoreOptPass(Registry);
95 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000096 initializeARMConstantIslandsPass(Registry);
Matthias Braune6ff30b2017-03-18 05:08:58 +000097 initializeARMExecutionDepsFixPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000098}
Douglas Gregor1b731d52009-06-16 20:12:29 +000099
Aditya Nandakumara2719322014-11-13 09:26:31 +0000100static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
101 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000102 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000103 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +0000104 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
105 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000106}
107
Eric Christopher661f2d12014-12-18 02:20:58 +0000108static ARMBaseTargetMachine::ARMABI
109computeTargetABI(const Triple &TT, StringRef CPU,
110 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000111 if (Options.MCOptions.getABIName() == "aapcs16")
112 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
113 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000114 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +0000115 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000116 return ARMBaseTargetMachine::ARM_ABI_APCS;
117
Eric Christopher6e30cd92015-01-14 00:50:31 +0000118 assert(Options.MCOptions.getABIName().empty() &&
119 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +0000120
121 ARMBaseTargetMachine::ARMABI TargetABI =
122 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
123
Eugene Zelenko342257e2017-01-31 00:56:17 +0000124 unsigned ArchKind = ARM::parseCPUArch(CPU);
125 StringRef ArchName = ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +0000126 // FIXME: This is duplicated code from the front end and should be unified.
127 if (TT.isOSBinFormatMachO()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000128 if (TT.getEnvironment() == Triple::EABI ||
129 (TT.getOS() == Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
130 ARM::parseArchProfile(ArchName) == ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +0000131 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +0000132 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000133 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +0000134 } else {
135 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
136 }
137 } else if (TT.isOSWindows()) {
138 // FIXME: this is invalid for WindowsCE
139 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
140 } else {
141 // Select the default based on the platform.
142 switch (TT.getEnvironment()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000143 case Triple::Android:
144 case Triple::GNUEABI:
145 case Triple::GNUEABIHF:
146 case Triple::MuslEABI:
147 case Triple::MuslEABIHF:
148 case Triple::EABIHF:
149 case Triple::EABI:
Eric Christopher661f2d12014-12-18 02:20:58 +0000150 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
151 break;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000152 case Triple::GNU:
Eric Christopher661f2d12014-12-18 02:20:58 +0000153 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
154 break;
155 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000156 if (TT.isOSNetBSD())
157 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000158 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000159 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000160 break;
161 }
162 }
163
164 return TargetABI;
165}
166
Daniel Sandersed64d622015-06-11 15:34:59 +0000167static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000168 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000169 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000170 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000171 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000172
173 if (isLittle)
174 // Little endian.
175 Ret += "e";
176 else
177 // Big endian.
178 Ret += "E";
179
Daniel Sandersed64d622015-06-11 15:34:59 +0000180 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000181
182 // Pointers are 32 bits and aligned to 32 bits.
183 Ret += "-p:32:32";
184
185 // ABIs other than APCS have 64 bit integers with natural alignment.
186 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
187 Ret += "-i64:64";
188
189 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
190 // bits, others to 64 bits. We always try to align to 64 bits.
191 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
192 Ret += "-f64:32:64";
193
194 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
195 // to 64. We always ty to give them natural alignment.
196 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
197 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000198 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000199 Ret += "-v128:64:128";
200
201 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
202 // particular hardware support on 32-bit ARM).
203 Ret += "-a:0:32";
204
205 // Integer registers are 32 bits.
206 Ret += "-n32";
207
208 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
209 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000210 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000211 Ret += "-S128";
212 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
213 Ret += "-S64";
214 else
215 Ret += "-S32";
216
217 return Ret;
218}
219
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000220static Reloc::Model getEffectiveRelocModel(const Triple &TT,
221 Optional<Reloc::Model> RM) {
222 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000223 // Default relocation model on Darwin is PIC.
224 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000225
Oliver Stannard8331aae2016-08-08 15:28:31 +0000226 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
227 assert(TT.isOSBinFormatELF() &&
228 "ROPI/RWPI currently only supported for ELF");
229
Renato Golin9be886292016-05-28 04:47:13 +0000230 // DynamicNoPIC is only used on darwin.
231 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
232 return Reloc::Static;
233
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000234 return *RM;
235}
236
Rafael Espindola38af4d62016-05-18 16:00:24 +0000237/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000238///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000239ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000240 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000241 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000242 Optional<Reloc::Model> RM,
243 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000244 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000245 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000246 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
247 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000248 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000249 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000250 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000251
252 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000253 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000254 this->Options.FloatABIType =
255 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000256
257 // Default to triple-appropriate EABI
258 if (Options.EABIVersion == EABI::Default ||
259 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000260 // musl is compatible with glibc with regard to EABI version
261 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000262 this->Options.EABIVersion = EABI::GNU;
263 else
264 this->Options.EABIVersion = EABI::EABI5;
265 }
Florian Hahnd211fe72017-05-24 10:18:57 +0000266
267 initAsmInfo();
268 if (!Subtarget.isThumb() && !Subtarget.hasARMOps())
269 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
270 "support ARM mode execution!");
Evan Cheng66cff402008-10-30 16:10:54 +0000271}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000272
Eugene Zelenko342257e2017-01-31 00:56:17 +0000273ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000274
Diana Picus22274932016-11-11 08:27:37 +0000275#ifdef LLVM_BUILD_GLOBAL_ISEL
276namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000277
Diana Picus22274932016-11-11 08:27:37 +0000278struct ARMGISelActualAccessor : public GISelAccessor {
279 std::unique_ptr<CallLowering> CallLoweringInfo;
280 std::unique_ptr<InstructionSelector> InstSelector;
281 std::unique_ptr<LegalizerInfo> Legalizer;
282 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000283
Diana Picus22274932016-11-11 08:27:37 +0000284 const CallLowering *getCallLowering() const override {
285 return CallLoweringInfo.get();
286 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000287
Diana Picus22274932016-11-11 08:27:37 +0000288 const InstructionSelector *getInstructionSelector() const override {
289 return InstSelector.get();
290 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000291
Ahmed Bougacha52286032016-12-15 18:45:30 +0000292 const LegalizerInfo *getLegalizerInfo() const override {
Diana Picus22274932016-11-11 08:27:37 +0000293 return Legalizer.get();
294 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000295
Diana Picus22274932016-11-11 08:27:37 +0000296 const RegisterBankInfo *getRegBankInfo() const override {
297 return RegBankInfo.get();
298 }
299};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000300
301} // end anonymous namespace
Diana Picus22274932016-11-11 08:27:37 +0000302#endif
303
Eric Christopher3faf2f12014-10-06 06:45:36 +0000304const ARMSubtarget *
305ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000306 Attribute CPUAttr = F.getFnAttribute("target-cpu");
307 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000308
309 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
310 ? CPUAttr.getValueAsString().str()
311 : TargetCPU;
312 std::string FS = !FSAttr.hasAttribute(Attribute::None)
313 ? FSAttr.getValueAsString().str()
314 : TargetFS;
315
316 // FIXME: This is related to the code below to reset the target options,
317 // we need to know whether or not the soft float flag is set on the
318 // function before we can generate a subtarget. We also need to use
319 // it as a key for the subtarget since that can be the only difference
320 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000321 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000322 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
323 // If the soft float attribute is set on the function turn on the soft float
324 // subtarget feature.
325 if (SoftFloat)
326 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000327
Eric Christopher824f42f2015-05-12 01:26:05 +0000328 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000329 if (!I) {
330 // This needs to be done before we create a new subtarget since any
331 // creation will depend on the TM and the code generation flags on the
332 // function that reside in TargetOptions.
333 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000334 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000335
336#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000337 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000338#else
Diana Picus90f0a842016-11-15 15:38:15 +0000339 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
340 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
Diana Picus7cab0782017-02-17 11:25:17 +0000341 GISel->Legalizer.reset(new ARMLegalizerInfo(*I));
Diana Picus22274932016-11-11 08:27:37 +0000342
Diana Picus90f0a842016-11-15 15:38:15 +0000343 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000344
Diana Picus90f0a842016-11-15 15:38:15 +0000345 // FIXME: At this point, we can't rely on Subtarget having RBI.
346 // It's awkward to mix passing RBI and the Subtarget; should we pass
347 // TII/TRI as well?
Diana Picus8abcbbb2017-05-02 09:40:49 +0000348 GISel->InstSelector.reset(createARMInstructionSelector(*this, *I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000349
Diana Picus90f0a842016-11-15 15:38:15 +0000350 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000351#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000352 I->setGISelAccessor(*GISel);
353 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000354 return I.get();
355}
356
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000357TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000358 return TargetIRAnalysis([this](const Function &F) {
359 return TargetTransformInfo(ARMTTIImpl(this, F));
360 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000361}
362
Christian Pirker2a111602014-03-28 14:35:30 +0000363
Daniel Sanders3e5de882015-06-11 19:41:26 +0000364ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000365 StringRef CPU, StringRef FS,
366 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000367 Optional<Reloc::Model> RM,
368 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000369 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000370 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000371
Daniel Sanders3e5de882015-06-11 19:41:26 +0000372ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000373 StringRef CPU, StringRef FS,
374 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000375 Optional<Reloc::Model> RM,
376 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000377 CodeGenOpt::Level OL)
Florian Hahnd211fe72017-05-24 10:18:57 +0000378 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000379
Andrew Trickccb67362012-02-03 05:12:41 +0000380namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000381
Andrew Trickccb67362012-02-03 05:12:41 +0000382/// ARM Code Generator Pass Configuration Options.
383class ARMPassConfig : public TargetPassConfig {
384public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000385 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
386 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000387
388 ARMBaseTargetMachine &getARMTargetMachine() const {
389 return getTM<ARMBaseTargetMachine>();
390 }
391
Tim Northoverb4ddc082014-05-30 10:09:59 +0000392 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000393 bool addPreISel() override;
394 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000395#ifdef LLVM_BUILD_GLOBAL_ISEL
396 bool addIRTranslator() override;
397 bool addLegalizeMachineIR() override;
398 bool addRegBankSelect() override;
399 bool addGlobalInstructionSelect() override;
400#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000401 void addPreRegAlloc() override;
402 void addPreSched2() override;
403 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000404};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000405
Matthias Braune6ff30b2017-03-18 05:08:58 +0000406class ARMExecutionDepsFix : public ExecutionDepsFix {
407public:
408 static char ID;
409 ARMExecutionDepsFix() : ExecutionDepsFix(ID, ARM::DPRRegClass) {}
410 StringRef getPassName() const override {
411 return "ARM Execution Dependency Fix";
412 }
413};
414char ARMExecutionDepsFix::ID;
415
Eugene Zelenko342257e2017-01-31 00:56:17 +0000416} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000417
Matthias Braune6ff30b2017-03-18 05:08:58 +0000418INITIALIZE_PASS(ARMExecutionDepsFix, "arm-execution-deps-fix",
419 "ARM Execution Dependency Fix", false, false)
420
Andrew Trickf8ea1082012-02-04 02:56:59 +0000421TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
422 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000423}
424
Tim Northoverb4ddc082014-05-30 10:09:59 +0000425void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000426 if (TM->Options.ThreadModel == ThreadModel::Single)
427 addPass(createLowerAtomicPass());
428 else
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000429 addPass(createAtomicExpandPass());
Tim Northoverc882eb02014-04-03 11:44:58 +0000430
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000431 // Cmpxchg instructions are often used with a subsequent comparison to
432 // determine whether it succeeded. We can exploit existing control-flow in
433 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000434 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
435 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
436 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
437 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
438 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000439
440 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000441
442 // Match interleaved memory accesses to ldN/stN intrinsics.
443 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000444 addPass(createInterleavedAccessPass());
Tim Northoverb4ddc082014-05-30 10:09:59 +0000445}
446
447bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000448 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000449 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000450 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000451 // FIXME: This is using the thumb1 only constant value for
452 // maximal global offset for merging globals. We may want
453 // to look into using the old value for non-thumb1 code of
454 // 4095 based on the TargetMachine, but this starts to become
455 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000456 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
457 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000458 // Merging of extern globals is enabled by default on non-Mach-O as we
459 // expect it to be generally either beneficial or harmless. On Mach-O it
460 // is disabled as we emit the .subsections_via_symbols directive which
461 // means that merging extern globals is not safe.
462 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
463 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
464 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000465 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000466
467 return false;
468}
469
Andrew Trickccb67362012-02-03 05:12:41 +0000470bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000471 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000472 return false;
473}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000474
Diana Picus22274932016-11-11 08:27:37 +0000475#ifdef LLVM_BUILD_GLOBAL_ISEL
476bool ARMPassConfig::addIRTranslator() {
477 addPass(new IRTranslator());
478 return false;
479}
480
481bool ARMPassConfig::addLegalizeMachineIR() {
482 addPass(new Legalizer());
483 return false;
484}
485
486bool ARMPassConfig::addRegBankSelect() {
487 addPass(new RegBankSelect());
488 return false;
489}
490
491bool ARMPassConfig::addGlobalInstructionSelect() {
492 addPass(new InstructionSelect());
493 return false;
494}
495#endif
496
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000497void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000498 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000499 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000500
501 if (EnableARMLoadStoreOpt)
502 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
503
504 if (!DisableA15SDOptimization)
505 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000506 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000507}
508
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000509void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000510 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000511 if (EnableARMLoadStoreOpt)
512 addPass(createARMLoadStoreOptimizationPass());
513
Matthias Braune6ff30b2017-03-18 05:08:58 +0000514 addPass(new ARMExecutionDepsFix());
Eric Christopher7ae11c62010-11-11 20:50:14 +0000515 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000516
Evan Cheng207b2462009-11-06 23:52:48 +0000517 // Expand some pseudo instructions into multiple instructions to allow
518 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000519 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000520
Evan Chengecb29082011-11-16 08:38:26 +0000521 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000522 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000523 addPass(createThumb2SizeReductionPass([this](const Function &F) {
524 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
525 }));
526
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000527 addPass(createIfConverter([](const MachineFunction &MF) {
528 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000529 }));
Renato Golin4c871392015-03-26 18:38:04 +0000530 }
Eric Christopher63b44882015-03-05 00:23:40 +0000531 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000532}
533
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000534void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000535 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000536
Eric Christopher63b44882015-03-05 00:23:40 +0000537 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000538 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
539 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000540 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000541
Davide Italiano141b28912015-05-20 21:40:38 +0000542 // Don't optimize barriers at -O0.
543 if (getOptLevel() != CodeGenOpt::None)
544 addPass(createARMOptimizeBarriersPass());
545
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000546 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000547}