blob: 7c24c2a3dfd51f1e53f184c3c03d52251ec76113 [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
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "ARMTargetMachine.h"
Diana Picus22274932016-11-11 08:27:37 +000014#include "ARM.h"
15#include "ARMCallLowering.h"
16#include "ARMFrameLowering.h"
17#include "ARMInstructionSelector.h"
18#include "ARMLegalizerInfo.h"
19#include "ARMRegisterBankInfo.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000020#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000021#include "ARMTargetTransformInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000022#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
23#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
24#include "llvm/CodeGen/GlobalISel/Legalizer.h"
25#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Evan Chengad3aac712007-05-16 02:01:49 +000026#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000027#include "llvm/CodeGen/TargetPassConfig.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000028#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Bill Wendling354ff9e2011-09-27 22:14:12 +000030#include "llvm/MC/MCAsmInfo.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000031#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000033#include "llvm/Support/TargetParser.h"
Diana Picus22274932016-11-11 08:27:37 +000034#include "llvm/Support/TargetRegistry.h"
Evan Cheng10043e22007-01-19 07:51:42 +000035#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000036#include "llvm/Transforms/Scalar.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000037using namespace llvm;
38
Evan Chengf066b2f2011-08-25 01:00:36 +000039static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000040DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
41 cl::desc("Inhibit optimization of S->D register accesses on A15"),
42 cl::init(false));
43
Tim Northoverb4ddc082014-05-30 10:09:59 +000044static cl::opt<bool>
45EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
46 cl::desc("Run SimplifyCFG after expanding atomic operations"
47 " to make use of cmpxchg flow-based information"),
48 cl::init(true));
49
Renato Golin4c871392015-03-26 18:38:04 +000050static cl::opt<bool>
51EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
52 cl::desc("Enable ARM load/store optimization pass"),
53 cl::init(true));
54
Ahmed Bougachab96444e2015-04-11 00:06:36 +000055// FIXME: Unify control over GlobalMerge.
56static cl::opt<cl::boolOrDefault>
57EnableGlobalMerge("arm-global-merge", cl::Hidden,
58 cl::desc("Enable the global merge pass"));
59
Jim Grosbachf24f9d92009-08-11 15:33:49 +000060extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000061 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000062 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
63 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
64 RegisterTargetMachine<ThumbLETargetMachine> A(getTheThumbLETarget());
65 RegisterTargetMachine<ThumbBETargetMachine> B(getTheThumbBETarget());
Matthias Braun8f456fb2016-07-16 02:24:10 +000066
67 PassRegistry &Registry = *PassRegistry::getPassRegistry();
Diana Picus22274932016-11-11 08:27:37 +000068 initializeGlobalISel(Registry);
Matthias Braun8f456fb2016-07-16 02:24:10 +000069 initializeARMLoadStoreOptPass(Registry);
70 initializeARMPreAllocLoadStoreOptPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000071}
Douglas Gregor1b731d52009-06-16 20:12:29 +000072
Aditya Nandakumara2719322014-11-13 09:26:31 +000073static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
74 if (TT.isOSBinFormatMachO())
75 return make_unique<TargetLoweringObjectFileMachO>();
76 if (TT.isOSWindows())
77 return make_unique<TargetLoweringObjectFileCOFF>();
78 return make_unique<ARMElfTargetObjectFile>();
79}
80
Eric Christopher661f2d12014-12-18 02:20:58 +000081static ARMBaseTargetMachine::ARMABI
82computeTargetABI(const Triple &TT, StringRef CPU,
83 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +000084 if (Options.MCOptions.getABIName() == "aapcs16")
85 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
86 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000087 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +000088 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000089 return ARMBaseTargetMachine::ARM_ABI_APCS;
90
Eric Christopher6e30cd92015-01-14 00:50:31 +000091 assert(Options.MCOptions.getABIName().empty() &&
92 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +000093
94 ARMBaseTargetMachine::ARMABI TargetABI =
95 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
96
Zijiao Ma53d55f42016-08-17 02:08:28 +000097 unsigned ArchKind = llvm::ARM::parseCPUArch(CPU);
98 StringRef ArchName = llvm::ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +000099 // FIXME: This is duplicated code from the front end and should be unified.
100 if (TT.isOSBinFormatMachO()) {
101 if (TT.getEnvironment() == llvm::Triple::EABI ||
Daniel Sandersfbdab432015-07-06 16:33:18 +0000102 (TT.getOS() == llvm::Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
Zijiao Ma53d55f42016-08-17 02:08:28 +0000103 llvm::ARM::parseArchProfile(ArchName) == llvm::ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +0000104 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +0000105 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +0000106 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +0000107 } else {
108 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
109 }
110 } else if (TT.isOSWindows()) {
111 // FIXME: this is invalid for WindowsCE
112 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
113 } else {
114 // Select the default based on the platform.
115 switch (TT.getEnvironment()) {
116 case llvm::Triple::Android:
117 case llvm::Triple::GNUEABI:
118 case llvm::Triple::GNUEABIHF:
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000119 case llvm::Triple::MuslEABI:
120 case llvm::Triple::MuslEABIHF:
Eric Christopher661f2d12014-12-18 02:20:58 +0000121 case llvm::Triple::EABIHF:
122 case llvm::Triple::EABI:
123 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
124 break;
125 case llvm::Triple::GNU:
126 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
127 break;
128 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000129 if (TT.isOSNetBSD())
130 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000131 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000132 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000133 break;
134 }
135 }
136
137 return TargetABI;
138}
139
Daniel Sandersed64d622015-06-11 15:34:59 +0000140static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000141 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000142 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000143 auto ABI = computeTargetABI(TT, CPU, Options);
Eric Christopher8b770652015-01-26 19:03:15 +0000144 std::string Ret = "";
145
146 if (isLittle)
147 // Little endian.
148 Ret += "e";
149 else
150 // Big endian.
151 Ret += "E";
152
Daniel Sandersed64d622015-06-11 15:34:59 +0000153 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000154
155 // Pointers are 32 bits and aligned to 32 bits.
156 Ret += "-p:32:32";
157
158 // ABIs other than APCS have 64 bit integers with natural alignment.
159 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
160 Ret += "-i64:64";
161
162 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
163 // bits, others to 64 bits. We always try to align to 64 bits.
164 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
165 Ret += "-f64:32:64";
166
167 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
168 // to 64. We always ty to give them natural alignment.
169 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
170 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000171 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000172 Ret += "-v128:64:128";
173
174 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
175 // particular hardware support on 32-bit ARM).
176 Ret += "-a:0:32";
177
178 // Integer registers are 32 bits.
179 Ret += "-n32";
180
181 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
182 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000183 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000184 Ret += "-S128";
185 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
186 Ret += "-S64";
187 else
188 Ret += "-S32";
189
190 return Ret;
191}
192
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000193static Reloc::Model getEffectiveRelocModel(const Triple &TT,
194 Optional<Reloc::Model> RM) {
195 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000196 // Default relocation model on Darwin is PIC.
197 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000198
Oliver Stannard8331aae2016-08-08 15:28:31 +0000199 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
200 assert(TT.isOSBinFormatELF() &&
201 "ROPI/RWPI currently only supported for ELF");
202
Renato Golin9be886292016-05-28 04:47:13 +0000203 // DynamicNoPIC is only used on darwin.
204 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
205 return Reloc::Static;
206
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000207 return *RM;
208}
209
Rafael Espindola38af4d62016-05-18 16:00:24 +0000210/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000211///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000212ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000213 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000214 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000215 Optional<Reloc::Model> RM,
216 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000217 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000218 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000219 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
220 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000221 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000222 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000223 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000224
225 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000226 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000227 this->Options.FloatABIType =
228 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000229
230 // Default to triple-appropriate EABI
231 if (Options.EABIVersion == EABI::Default ||
232 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000233 // musl is compatible with glibc with regard to EABI version
234 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000235 this->Options.EABIVersion = EABI::GNU;
236 else
237 this->Options.EABIVersion = EABI::EABI5;
238 }
Evan Cheng66cff402008-10-30 16:10:54 +0000239}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000240
Reid Kleckner357600e2014-11-20 23:37:18 +0000241ARMBaseTargetMachine::~ARMBaseTargetMachine() {}
242
Diana Picus22274932016-11-11 08:27:37 +0000243#ifdef LLVM_BUILD_GLOBAL_ISEL
244namespace {
245struct ARMGISelActualAccessor : public GISelAccessor {
246 std::unique_ptr<CallLowering> CallLoweringInfo;
247 std::unique_ptr<InstructionSelector> InstSelector;
248 std::unique_ptr<LegalizerInfo> Legalizer;
249 std::unique_ptr<RegisterBankInfo> RegBankInfo;
250 const CallLowering *getCallLowering() const override {
251 return CallLoweringInfo.get();
252 }
253 const InstructionSelector *getInstructionSelector() const override {
254 return InstSelector.get();
255 }
256 const class LegalizerInfo *getLegalizerInfo() const override {
257 return Legalizer.get();
258 }
259 const RegisterBankInfo *getRegBankInfo() const override {
260 return RegBankInfo.get();
261 }
262};
263} // End anonymous namespace.
264#endif
265
Eric Christopher3faf2f12014-10-06 06:45:36 +0000266const ARMSubtarget *
267ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000268 Attribute CPUAttr = F.getFnAttribute("target-cpu");
269 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000270
271 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
272 ? CPUAttr.getValueAsString().str()
273 : TargetCPU;
274 std::string FS = !FSAttr.hasAttribute(Attribute::None)
275 ? FSAttr.getValueAsString().str()
276 : TargetFS;
277
278 // FIXME: This is related to the code below to reset the target options,
279 // we need to know whether or not the soft float flag is set on the
280 // function before we can generate a subtarget. We also need to use
281 // it as a key for the subtarget since that can be the only difference
282 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000283 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000284 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
285 // If the soft float attribute is set on the function turn on the soft float
286 // subtarget feature.
287 if (SoftFloat)
288 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000289
Eric Christopher824f42f2015-05-12 01:26:05 +0000290 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000291 if (!I) {
292 // This needs to be done before we create a new subtarget since any
293 // creation will depend on the TM and the code generation flags on the
294 // function that reside in TargetOptions.
295 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000296 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Diana Picus22274932016-11-11 08:27:37 +0000297
298#ifndef LLVM_BUILD_GLOBAL_ISEL
Diana Picus90f0a842016-11-15 15:38:15 +0000299 GISelAccessor *GISel = new GISelAccessor();
Diana Picus22274932016-11-11 08:27:37 +0000300#else
Diana Picus90f0a842016-11-15 15:38:15 +0000301 ARMGISelActualAccessor *GISel = new ARMGISelActualAccessor();
302 GISel->CallLoweringInfo.reset(new ARMCallLowering(*I->getTargetLowering()));
303 GISel->Legalizer.reset(new ARMLegalizerInfo());
Diana Picus22274932016-11-11 08:27:37 +0000304
Diana Picus90f0a842016-11-15 15:38:15 +0000305 auto *RBI = new ARMRegisterBankInfo(*I->getRegisterInfo());
Diana Picus22274932016-11-11 08:27:37 +0000306
Diana Picus90f0a842016-11-15 15:38:15 +0000307 // FIXME: At this point, we can't rely on Subtarget having RBI.
308 // It's awkward to mix passing RBI and the Subtarget; should we pass
309 // TII/TRI as well?
Diana Picus895c6aa2016-11-15 16:42:10 +0000310 GISel->InstSelector.reset(new ARMInstructionSelector(*I, *RBI));
Diana Picus22274932016-11-11 08:27:37 +0000311
Diana Picus90f0a842016-11-15 15:38:15 +0000312 GISel->RegBankInfo.reset(RBI);
Diana Picus22274932016-11-11 08:27:37 +0000313#endif
Diana Picus90f0a842016-11-15 15:38:15 +0000314 I->setGISelAccessor(*GISel);
315 }
Eric Christopher3faf2f12014-10-06 06:45:36 +0000316 return I.get();
317}
318
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000319TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000320 return TargetIRAnalysis([this](const Function &F) {
321 return TargetTransformInfo(ARMTTIImpl(this, F));
322 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000323}
324
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000325void ARMTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000326
Daniel Sanders3e5de882015-06-11 19:41:26 +0000327ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT,
328 StringRef CPU, StringRef FS,
329 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000330 Optional<Reloc::Model> RM,
331 CodeModel::Model CM, CodeGenOpt::Level OL,
332 bool isLittle)
Eric Christopher80b24ef2014-06-26 19:30:02 +0000333 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000334 initAsmInfo();
Evan Cheng5190f092010-08-11 07:17:46 +0000335 if (!Subtarget.hasARMOps())
336 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
337 "support ARM mode execution!");
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000338}
339
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000340void ARMLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000341
Daniel Sanders3e5de882015-06-11 19:41:26 +0000342ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000343 StringRef CPU, StringRef FS,
344 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000345 Optional<Reloc::Model> RM,
346 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000347 CodeGenOpt::Level OL)
348 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000349
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000350void ARMBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000351
Daniel Sanders3e5de882015-06-11 19:41:26 +0000352ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000353 StringRef CPU, StringRef FS,
354 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000355 Optional<Reloc::Model> RM,
356 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000357 CodeGenOpt::Level OL)
358 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000359
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000360void ThumbTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000361
Daniel Sanders3e5de882015-06-11 19:41:26 +0000362ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000363 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000364 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000365 Optional<Reloc::Model> RM,
366 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000367 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000368 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000369 initAsmInfo();
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000370}
371
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000372void ThumbLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000373
Daniel Sanders3e5de882015-06-11 19:41:26 +0000374ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000375 StringRef CPU, StringRef FS,
376 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000377 Optional<Reloc::Model> RM,
378 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000379 CodeGenOpt::Level OL)
380 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000381
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000382void ThumbBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000383
Daniel Sanders3e5de882015-06-11 19:41:26 +0000384ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000385 StringRef CPU, StringRef FS,
386 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000387 Optional<Reloc::Model> RM,
388 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000389 CodeGenOpt::Level OL)
390 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000391
Andrew Trickccb67362012-02-03 05:12:41 +0000392namespace {
393/// ARM Code Generator Pass Configuration Options.
394class ARMPassConfig : public TargetPassConfig {
395public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000396 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
397 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000398
399 ARMBaseTargetMachine &getARMTargetMachine() const {
400 return getTM<ARMBaseTargetMachine>();
401 }
402
Tim Northoverb4ddc082014-05-30 10:09:59 +0000403 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000404 bool addPreISel() override;
405 bool addInstSelector() override;
Diana Picus22274932016-11-11 08:27:37 +0000406#ifdef LLVM_BUILD_GLOBAL_ISEL
407 bool addIRTranslator() override;
408 bool addLegalizeMachineIR() override;
409 bool addRegBankSelect() override;
410 bool addGlobalInstructionSelect() override;
411#endif
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000412 void addPreRegAlloc() override;
413 void addPreSched2() override;
414 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000415};
416} // namespace
417
Andrew Trickf8ea1082012-02-04 02:56:59 +0000418TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
419 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000420}
421
Tim Northoverb4ddc082014-05-30 10:09:59 +0000422void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000423 if (TM->Options.ThreadModel == ThreadModel::Single)
424 addPass(createLowerAtomicPass());
425 else
Robin Morisset59c23cd2014-08-21 21:50:01 +0000426 addPass(createAtomicExpandPass(TM));
Tim Northoverc882eb02014-04-03 11:44:58 +0000427
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000428 // Cmpxchg instructions are often used with a subsequent comparison to
429 // determine whether it succeeded. We can exploit existing control-flow in
430 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000431 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
432 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
433 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
434 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
435 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000436
437 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000438
439 // Match interleaved memory accesses to ldN/stN intrinsics.
440 if (TM->getOptLevel() != CodeGenOpt::None)
441 addPass(createInterleavedAccessPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000442}
443
444bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000445 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000446 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000447 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000448 // FIXME: This is using the thumb1 only constant value for
449 // maximal global offset for merging globals. We may want
450 // to look into using the old value for non-thumb1 code of
451 // 4095 based on the TargetMachine, but this starts to become
452 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000453 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
454 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000455 // Merging of extern globals is enabled by default on non-Mach-O as we
456 // expect it to be generally either beneficial or harmless. On Mach-O it
457 // is disabled as we emit the .subsections_via_symbols directive which
458 // means that merging extern globals is not safe.
459 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
460 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
461 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000462 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000463
464 return false;
465}
466
Andrew Trickccb67362012-02-03 05:12:41 +0000467bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000468 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000469 return false;
470}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000471
Diana Picus22274932016-11-11 08:27:37 +0000472#ifdef LLVM_BUILD_GLOBAL_ISEL
473bool ARMPassConfig::addIRTranslator() {
474 addPass(new IRTranslator());
475 return false;
476}
477
478bool ARMPassConfig::addLegalizeMachineIR() {
479 addPass(new Legalizer());
480 return false;
481}
482
483bool ARMPassConfig::addRegBankSelect() {
484 addPass(new RegBankSelect());
485 return false;
486}
487
488bool ARMPassConfig::addGlobalInstructionSelect() {
489 addPass(new InstructionSelect());
490 return false;
491}
492#endif
493
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000494void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000495 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000496 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000497
498 if (EnableARMLoadStoreOpt)
499 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
500
501 if (!DisableA15SDOptimization)
502 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000503 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000504}
505
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000506void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000507 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000508 if (EnableARMLoadStoreOpt)
509 addPass(createARMLoadStoreOptimizationPass());
510
Eric Christopher7e70aba2015-03-07 00:12:22 +0000511 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass));
Eric Christopher7ae11c62010-11-11 20:50:14 +0000512 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000513
Evan Cheng207b2462009-11-06 23:52:48 +0000514 // Expand some pseudo instructions into multiple instructions to allow
515 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000516 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000517
Evan Chengecb29082011-11-16 08:38:26 +0000518 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000519 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000520 addPass(createThumb2SizeReductionPass([this](const Function &F) {
521 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
522 }));
523
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000524 addPass(createIfConverter([](const MachineFunction &MF) {
525 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000526 }));
Renato Golin4c871392015-03-26 18:38:04 +0000527 }
Eric Christopher63b44882015-03-05 00:23:40 +0000528 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000529}
530
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000531void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000532 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000533
Eric Christopher63b44882015-03-05 00:23:40 +0000534 // Constant island pass work on unbundled instructions.
Matthias Braun8b38ffa2016-10-24 23:23:02 +0000535 addPass(createUnpackMachineBundles([](const MachineFunction &MF) {
536 return MF.getSubtarget<ARMSubtarget>().isThumb2();
Akira Hatanaka4a616192015-06-08 18:50:43 +0000537 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000538
Davide Italiano141b28912015-05-20 21:40:38 +0000539 // Don't optimize barriers at -O0.
540 if (getOptLevel() != CodeGenOpt::None)
541 addPass(createARMOptimizeBarriersPass());
542
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000543 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000544}