blob: 4225794d5492579e15d4623cc5e6dda0cb29bee4 [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 "ARMInstructionSelector.h"
16#include "ARMLegalizerInfo.h"
17#include "ARMRegisterBankInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000018#include "ARMSubtarget.h"
19#include "ARMTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000020#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000021#include "ARMTargetTransformInfo.h"
Eugene Zelenko342257e2017-01-31 00:56:17 +000022#include "MCTargetDesc/ARMMCTargetDesc.h"
23#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Triple.h"
27#include "llvm/Analysis/TargetTransformInfo.h"
28#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
Jim Grosbachf24f9d92009-08-11 15:33:49 +000079extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000080 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000081 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
82 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
83 RegisterTargetMachine<ThumbLETargetMachine> A(getTheThumbLETarget());
84 RegisterTargetMachine<ThumbBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000085
86 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000087 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000088 initializeARMLoadStoreOptPass(Registry);
89 initializeARMPreAllocLoadStoreOptPass(Registry);
James Molloy9b3b8992017-02-13 14:07:25 +000090 initializeARMConstantIslandsPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000091}
Douglas Gregor1b731d52009-06-16 20:12:29 +000092
Aditya Nandakumara2719322014-11-13 09:26:31 +000093static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
94 if (TT.isOSBinFormatMachO())
Eugene Zelenko342257e2017-01-31 00:56:17 +000095 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000096 if (TT.isOSWindows())
Eugene Zelenko342257e2017-01-31 00:56:17 +000097 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
98 return llvm::make_unique<ARMElfTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000099}
100
Eric Christopher661f2d12014-12-18 02:20:58 +0000101static ARMBaseTargetMachine::ARMABI
102computeTargetABI(const Triple &TT, StringRef CPU,
103 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000104 if (Options.MCOptions.getABIName() == "aapcs16")
105 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
106 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000107 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +0000108 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +0000109 return ARMBaseTargetMachine::ARM_ABI_APCS;
110
Eric Christopher6e30cd92015-01-14 00:50:31 +0000111 assert(Options.MCOptions.getABIName().empty() &&
112 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +0000113
114 ARMBaseTargetMachine::ARMABI TargetABI =
115 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
116
Eugene Zelenko342257e2017-01-31 00:56:17 +0000117 unsigned ArchKind = ARM::parseCPUArch(CPU);
118 StringRef ArchName = ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +0000119 // FIXME: This is duplicated code from the front end and should be unified.
120 if (TT.isOSBinFormatMachO()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000121 if (TT.getEnvironment() == Triple::EABI ||
122 (TT.getOS() == Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
123 ARM::parseArchProfile(ArchName) == ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +0000124 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +0000125 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000126 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +0000127 } else {
128 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
129 }
130 } else if (TT.isOSWindows()) {
131 // FIXME: this is invalid for WindowsCE
132 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
133 } else {
134 // Select the default based on the platform.
135 switch (TT.getEnvironment()) {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000136 case Triple::Android:
137 case Triple::GNUEABI:
138 case Triple::GNUEABIHF:
139 case Triple::MuslEABI:
140 case Triple::MuslEABIHF:
141 case Triple::EABIHF:
142 case Triple::EABI:
Eric Christopher661f2d12014-12-18 02:20:58 +0000143 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
144 break;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000145 case Triple::GNU:
Eric Christopher661f2d12014-12-18 02:20:58 +0000146 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
147 break;
148 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000149 if (TT.isOSNetBSD())
150 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000151 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000152 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000153 break;
154 }
155 }
156
157 return TargetABI;
158}
159
Daniel Sandersed64d622015-06-11 15:34:59 +0000160static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000161 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000162 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000163 auto ABI = computeTargetABI(TT, CPU, Options);
Eugene Zelenko342257e2017-01-31 00:56:17 +0000164 std::string Ret;
Eric Christopher8b770652015-01-26 19:03:15 +0000165
166 if (isLittle)
167 // Little endian.
168 Ret += "e";
169 else
170 // Big endian.
171 Ret += "E";
172
Daniel Sandersed64d622015-06-11 15:34:59 +0000173 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000174
175 // Pointers are 32 bits and aligned to 32 bits.
176 Ret += "-p:32:32";
177
178 // ABIs other than APCS have 64 bit integers with natural alignment.
179 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
180 Ret += "-i64:64";
181
182 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
183 // bits, others to 64 bits. We always try to align to 64 bits.
184 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
185 Ret += "-f64:32:64";
186
187 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
188 // to 64. We always ty to give them natural alignment.
189 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
190 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000191 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000192 Ret += "-v128:64:128";
193
194 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
195 // particular hardware support on 32-bit ARM).
196 Ret += "-a:0:32";
197
198 // Integer registers are 32 bits.
199 Ret += "-n32";
200
201 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
202 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000203 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000204 Ret += "-S128";
205 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
206 Ret += "-S64";
207 else
208 Ret += "-S32";
209
210 return Ret;
211}
212
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000213static Reloc::Model getEffectiveRelocModel(const Triple &TT,
214 Optional<Reloc::Model> RM) {
215 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000216 // Default relocation model on Darwin is PIC.
217 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000218
Oliver Stannard8331aae2016-08-08 15:28:31 +0000219 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
220 assert(TT.isOSBinFormatELF() &&
221 "ROPI/RWPI currently only supported for ELF");
222
Renato Golin9be886292016-05-28 04:47:13 +0000223 // DynamicNoPIC is only used on darwin.
224 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
225 return Reloc::Static;
226
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000227 return *RM;
228}
229
Rafael Espindola38af4d62016-05-18 16:00:24 +0000230/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000231///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000232ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000233 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000234 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000235 Optional<Reloc::Model> RM,
236 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000237 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000238 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000239 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
240 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000241 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000242 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000243 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000244
245 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000246 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000247 this->Options.FloatABIType =
248 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000249
250 // Default to triple-appropriate EABI
251 if (Options.EABIVersion == EABI::Default ||
252 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000253 // musl is compatible with glibc with regard to EABI version
254 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000255 this->Options.EABIVersion = EABI::GNU;
256 else
257 this->Options.EABIVersion = EABI::EABI5;
258 }
Evan Cheng66cff402008-10-30 16:10:54 +0000259}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000260
Eugene Zelenko342257e2017-01-31 00:56:17 +0000261ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000262
Diana Picus22274932016-11-11 08:27:37 +0000263#ifdef LLVM_BUILD_GLOBAL_ISEL
264namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000265
Diana Picus22274932016-11-11 08:27:37 +0000266struct ARMGISelActualAccessor : public GISelAccessor {
267 std::unique_ptr<CallLowering> CallLoweringInfo;
268 std::unique_ptr<InstructionSelector> InstSelector;
269 std::unique_ptr<LegalizerInfo> Legalizer;
270 std::unique_ptr<RegisterBankInfo> RegBankInfo;
Eugene Zelenko342257e2017-01-31 00:56:17 +0000271
Diana Picus22274932016-11-11 08:27:37 +0000272 const CallLowering *getCallLowering() const override {
273 return CallLoweringInfo.get();
274 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000275
Diana Picus22274932016-11-11 08:27:37 +0000276 const InstructionSelector *getInstructionSelector() const override {
277 return InstSelector.get();
278 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000279
Ahmed Bougacha52286032016-12-15 18:45:30 +0000280 const LegalizerInfo *getLegalizerInfo() const override {
Diana Picus22274932016-11-11 08:27:37 +0000281 return Legalizer.get();
282 }
Eugene Zelenko342257e2017-01-31 00:56:17 +0000283
Diana Picus22274932016-11-11 08:27:37 +0000284 const RegisterBankInfo *getRegBankInfo() const override {
285 return RegBankInfo.get();
286 }
287};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000288
289} // end anonymous namespace
Diana Picus22274932016-11-11 08:27:37 +0000290#endif
291
Eric Christopher3faf2f12014-10-06 06:45:36 +0000292const ARMSubtarget *
293ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000294 Attribute CPUAttr = F.getFnAttribute("target-cpu");
295 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000296
297 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
298 ? CPUAttr.getValueAsString().str()
299 : TargetCPU;
300 std::string FS = !FSAttr.hasAttribute(Attribute::None)
301 ? FSAttr.getValueAsString().str()
302 : TargetFS;
303
304 // FIXME: This is related to the code below to reset the target options,
305 // we need to know whether or not the soft float flag is set on the
306 // function before we can generate a subtarget. We also need to use
307 // it as a key for the subtarget since that can be the only difference
308 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000309 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000310 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
311 // If the soft float attribute is set on the function turn on the soft float
312 // subtarget feature.
313 if (SoftFloat)
314 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000315
Eric Christopher824f42f2015-05-12 01:26:05 +0000316 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000317 if (!I) {
318 // This needs to be done before we create a new subtarget since any
319 // creation will depend on the TM and the code generation flags on the
320 // function that reside in TargetOptions.
321 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000322 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000323
324#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000325 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000326#else
Diana Picus90f0a842016-11-15 15:38:15 +0000327 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
328 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
Diana Picus7cab0782017-02-17 11:25:17 +0000329 GISel->Legalizer.reset(new ARMLegalizerInfo(*I));
Diana Picus22274932016-11-11 08:27:37 +0000330
Diana Picus90f0a842016-11-15 15:38:15 +0000331 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000332
Diana Picus90f0a842016-11-15 15:38:15 +0000333 // FIXME: At this point, we can't rely on Subtarget having RBI.
334 // It's awkward to mix passing RBI and the Subtarget; should we pass
335 // TII/TRI as well?
Diana Picus895c6aa2016-11-15 16:42:10 +0000336 GISel->InstSelector.reset(new ARMInstructionSelector(*I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000337
Diana Picus90f0a842016-11-15 15:38:15 +0000338 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000339#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000340 I->setGISelAccessor(*GISel);
341 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000342 return I.get();
343}
344
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000345TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000346 return TargetIRAnalysis([this](const Function &F) {
347 return TargetTransformInfo(ARMTTIImpl(this, F));
348 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000349}
350
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000351void ARMTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000352
Daniel Sanders3e5de882015-06-11 19:41:26 +0000353ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT,
354 StringRef CPU, StringRef FS,
355 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000356 Optional<Reloc::Model> RM,
357 CodeModel::Model CM, CodeGenOpt::Level OL,
358 bool isLittle)
Eric Christopher80b24ef2014-06-26 19:30:02 +0000359 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000360 initAsmInfo();
Evan Cheng5190f092010-08-11 07:17:46 +0000361 if (!Subtarget.hasARMOps())
362 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
363 "support ARM mode execution!");
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000364}
365
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000366void ARMLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000367
Daniel Sanders3e5de882015-06-11 19:41:26 +0000368ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000369 StringRef CPU, StringRef FS,
370 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000371 Optional<Reloc::Model> RM,
372 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000373 CodeGenOpt::Level OL)
374 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000375
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000376void ARMBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000377
Daniel Sanders3e5de882015-06-11 19:41:26 +0000378ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000379 StringRef CPU, StringRef FS,
380 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000381 Optional<Reloc::Model> RM,
382 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000383 CodeGenOpt::Level OL)
384 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000385
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000386void ThumbTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000387
Daniel Sanders3e5de882015-06-11 19:41:26 +0000388ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000389 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000390 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000391 Optional<Reloc::Model> RM,
392 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000393 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000394 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000395 initAsmInfo();
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000396}
397
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000398void ThumbLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000399
Daniel Sanders3e5de882015-06-11 19:41:26 +0000400ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000401 StringRef CPU, StringRef FS,
402 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000403 Optional<Reloc::Model> RM,
404 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000405 CodeGenOpt::Level OL)
406 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000407
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000408void ThumbBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000409
Daniel Sanders3e5de882015-06-11 19:41:26 +0000410ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000411 StringRef CPU, StringRef FS,
412 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000413 Optional<Reloc::Model> RM,
414 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000415 CodeGenOpt::Level OL)
416 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000417
Andrew Trickccb67362012-02-03 05:12:41 +0000418namespace {
Eugene Zelenko342257e2017-01-31 00:56:17 +0000419
Andrew Trickccb67362012-02-03 05:12:41 +0000420/// ARM Code Generator Pass Configuration Options.
421class ARMPassConfig : public TargetPassConfig {
422public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000423 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
424 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000425
426 ARMBaseTargetMachine &getARMTargetMachine() const {
427 return getTM<ARMBaseTargetMachine>();
428 }
429
Tim Northoverb4ddc082014-05-30 10:09:59 +0000430 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000431 bool addPreISel() override;
432 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000433#ifdef LLVM_BUILD_GLOBAL_ISEL
434 bool addIRTranslator() override;
435 bool addLegalizeMachineIR() override;
436 bool addRegBankSelect() override;
437 bool addGlobalInstructionSelect() override;
438#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000439 void addPreRegAlloc() override;
440 void addPreSched2() override;
441 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000442};
Eugene Zelenko342257e2017-01-31 00:56:17 +0000443
444} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000445
Andrew Trickf8ea1082012-02-04 02:56:59 +0000446TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
447 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000448}
449
Tim Northoverb4ddc082014-05-30 10:09:59 +0000450void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000451 if (TM->Options.ThreadModel == ThreadModel::Single)
452 addPass(createLowerAtomicPass());
453 else
Robin Morisset59c23cd2014-08-21 21:50:01 +0000454 addPass(createAtomicExpandPass(TM));
Tim Northoverc882eb02014-04-03 11:44:58 +0000455
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000456 // Cmpxchg instructions are often used with a subsequent comparison to
457 // determine whether it succeeded. We can exploit existing control-flow in
458 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000459 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
460 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
461 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
462 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
463 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000464
465 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000466
467 // Match interleaved memory accesses to ldN/stN intrinsics.
468 if (TM->getOptLevel() != CodeGenOpt::None)
469 addPass(createInterleavedAccessPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000470}
471
472bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000473 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000474 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000475 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000476 // FIXME: This is using the thumb1 only constant value for
477 // maximal global offset for merging globals. We may want
478 // to look into using the old value for non-thumb1 code of
479 // 4095 based on the TargetMachine, but this starts to become
480 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000481 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
482 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000483 // Merging of extern globals is enabled by default on non-Mach-O as we
484 // expect it to be generally either beneficial or harmless. On Mach-O it
485 // is disabled as we emit the .subsections_via_symbols directive which
486 // means that merging extern globals is not safe.
487 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
488 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
489 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000490 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000491
492 return false;
493}
494
Andrew Trickccb67362012-02-03 05:12:41 +0000495bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000496 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000497 return false;
498}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000499
Diana Picus22274932016-11-11 08:27:37 +0000500#ifdef LLVM_BUILD_GLOBAL_ISEL
501bool ARMPassConfig::addIRTranslator() {
502 addPass(new IRTranslator());
503 return false;
504}
505
506bool ARMPassConfig::addLegalizeMachineIR() {
507 addPass(new Legalizer());
508 return false;
509}
510
511bool ARMPassConfig::addRegBankSelect() {
512 addPass(new RegBankSelect());
513 return false;
514}
515
516bool ARMPassConfig::addGlobalInstructionSelect() {
517 addPass(new InstructionSelect());
518 return false;
519}
520#endif
521
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000522void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000523 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000524 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000525
526 if (EnableARMLoadStoreOpt)
527 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
528
529 if (!DisableA15SDOptimization)
530 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000531 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000532}
533
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000534void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000535 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000536 if (EnableARMLoadStoreOpt)
537 addPass(createARMLoadStoreOptimizationPass());
538
Eric Christopher7e70aba2015-03-07 00:12:22 +0000539 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass));
Eric Christopher7ae11c62010-11-11 20:50:14 +0000540 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000541
Evan Cheng207b2462009-11-06 23:52:48 +0000542 // Expand some pseudo instructions into multiple instructions to allow
543 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000544 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000545
Evan Chengecb29082011-11-16 08:38:26 +0000546 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000547 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000548 addPass(createThumb2SizeReductionPass([this](const Function &F) {
549 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
550 }));
551
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000552 addPass(createIfConverter([](const MachineFunction &MF) {
553 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000554 }));
Renato Golin4c871392015-03-26 18:38:04 +0000555 }
Eric Christopher63b44882015-03-05 00:23:40 +0000556 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000557}
558
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000559void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000560 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000561
Eric Christopher63b44882015-03-05 00:23:40 +0000562 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000563 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
564 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000565 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000566
Davide Italiano141b28912015-05-20 21:40:38 +0000567 // Don't optimize barriers at -O0.
568 if (getOptLevel() != CodeGenOpt::None)
569 addPass(createARMOptimizeBarriersPass());
570
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000571 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000572}