blob: 4b24af8a58c823dbfc36b294e2a9e179e9b36798 [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
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000013#include "ARM.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "ARMFrameLowering.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "ARMTargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000016#include "ARMTargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "ARMTargetTransformInfo.h"
Evan Chengad3aac712007-05-16 02:01:49 +000018#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000019#include "llvm/CodeGen/TargetPassConfig.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000020#include "llvm/IR/Function.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000021#include "llvm/IR/LegacyPassManager.h"
Bill Wendling354ff9e2011-09-27 22:14:12 +000022#include "llvm/MC/MCAsmInfo.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000023#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000024#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Zijiao Ma53d55f42016-08-17 02:08:28 +000026#include "llvm/Support/TargetParser.h"
Evan Cheng10043e22007-01-19 07:51:42 +000027#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000028#include "llvm/Transforms/Scalar.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000029using namespace llvm;
30
Evan Chengf066b2f2011-08-25 01:00:36 +000031static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000032DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
33 cl::desc("Inhibit optimization of S->D register accesses on A15"),
34 cl::init(false));
35
Tim Northoverb4ddc082014-05-30 10:09:59 +000036static cl::opt<bool>
37EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
38 cl::desc("Run SimplifyCFG after expanding atomic operations"
39 " to make use of cmpxchg flow-based information"),
40 cl::init(true));
41
Renato Golin4c871392015-03-26 18:38:04 +000042static cl::opt<bool>
43EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
44 cl::desc("Enable ARM load/store optimization pass"),
45 cl::init(true));
46
Ahmed Bougachab96444e2015-04-11 00:06:36 +000047// FIXME: Unify control over GlobalMerge.
48static cl::opt<cl::boolOrDefault>
49EnableGlobalMerge("arm-global-merge", cl::Hidden,
50 cl::desc("Enable the global merge pass"));
51
Jim Grosbachf24f9d92009-08-11 15:33:49 +000052extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000053 // Register the target.
Christian Pirkerdc9ff752014-04-01 15:19:30 +000054 RegisterTargetMachine<ARMLETargetMachine> X(TheARMLETarget);
55 RegisterTargetMachine<ARMBETargetMachine> Y(TheARMBETarget);
56 RegisterTargetMachine<ThumbLETargetMachine> A(TheThumbLETarget);
57 RegisterTargetMachine<ThumbBETargetMachine> B(TheThumbBETarget);
Matthias Braun8f456fb2016-07-16 02:24:10 +000058
59 PassRegistry &Registry = *PassRegistry::getPassRegistry();
60 initializeARMLoadStoreOptPass(Registry);
61 initializeARMPreAllocLoadStoreOptPass(Registry);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000062}
Douglas Gregor1b731d52009-06-16 20:12:29 +000063
Aditya Nandakumara2719322014-11-13 09:26:31 +000064static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
65 if (TT.isOSBinFormatMachO())
66 return make_unique<TargetLoweringObjectFileMachO>();
67 if (TT.isOSWindows())
68 return make_unique<TargetLoweringObjectFileCOFF>();
69 return make_unique<ARMElfTargetObjectFile>();
70}
71
Eric Christopher661f2d12014-12-18 02:20:58 +000072static ARMBaseTargetMachine::ARMABI
73computeTargetABI(const Triple &TT, StringRef CPU,
74 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +000075 if (Options.MCOptions.getABIName() == "aapcs16")
76 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
77 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000078 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +000079 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000080 return ARMBaseTargetMachine::ARM_ABI_APCS;
81
Eric Christopher6e30cd92015-01-14 00:50:31 +000082 assert(Options.MCOptions.getABIName().empty() &&
83 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +000084
85 ARMBaseTargetMachine::ARMABI TargetABI =
86 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
87
Zijiao Ma53d55f42016-08-17 02:08:28 +000088 unsigned ArchKind = llvm::ARM::parseCPUArch(CPU);
89 StringRef ArchName = llvm::ARM::getArchName(ArchKind);
Eric Christopher661f2d12014-12-18 02:20:58 +000090 // FIXME: This is duplicated code from the front end and should be unified.
91 if (TT.isOSBinFormatMachO()) {
92 if (TT.getEnvironment() == llvm::Triple::EABI ||
Daniel Sandersfbdab432015-07-06 16:33:18 +000093 (TT.getOS() == llvm::Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
Zijiao Ma53d55f42016-08-17 02:08:28 +000094 llvm::ARM::parseArchProfile(ArchName) == llvm::ARM::PK_M) {
Eric Christopher661f2d12014-12-18 02:20:58 +000095 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +000096 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +000097 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +000098 } else {
99 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
100 }
101 } else if (TT.isOSWindows()) {
102 // FIXME: this is invalid for WindowsCE
103 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
104 } else {
105 // Select the default based on the platform.
106 switch (TT.getEnvironment()) {
107 case llvm::Triple::Android:
108 case llvm::Triple::GNUEABI:
109 case llvm::Triple::GNUEABIHF:
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000110 case llvm::Triple::MuslEABI:
111 case llvm::Triple::MuslEABIHF:
Eric Christopher661f2d12014-12-18 02:20:58 +0000112 case llvm::Triple::EABIHF:
113 case llvm::Triple::EABI:
114 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
115 break;
116 case llvm::Triple::GNU:
117 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
118 break;
119 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000120 if (TT.isOSNetBSD())
121 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000122 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000123 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000124 break;
125 }
126 }
127
128 return TargetABI;
129}
130
Daniel Sandersed64d622015-06-11 15:34:59 +0000131static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000132 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000133 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000134 auto ABI = computeTargetABI(TT, CPU, Options);
Eric Christopher8b770652015-01-26 19:03:15 +0000135 std::string Ret = "";
136
137 if (isLittle)
138 // Little endian.
139 Ret += "e";
140 else
141 // Big endian.
142 Ret += "E";
143
Daniel Sandersed64d622015-06-11 15:34:59 +0000144 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000145
146 // Pointers are 32 bits and aligned to 32 bits.
147 Ret += "-p:32:32";
148
149 // ABIs other than APCS have 64 bit integers with natural alignment.
150 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
151 Ret += "-i64:64";
152
153 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
154 // bits, others to 64 bits. We always try to align to 64 bits.
155 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
156 Ret += "-f64:32:64";
157
158 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
159 // to 64. We always ty to give them natural alignment.
160 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
161 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000162 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000163 Ret += "-v128:64:128";
164
165 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
166 // particular hardware support on 32-bit ARM).
167 Ret += "-a:0:32";
168
169 // Integer registers are 32 bits.
170 Ret += "-n32";
171
172 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
173 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000174 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000175 Ret += "-S128";
176 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
177 Ret += "-S64";
178 else
179 Ret += "-S32";
180
181 return Ret;
182}
183
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000184static Reloc::Model getEffectiveRelocModel(const Triple &TT,
185 Optional<Reloc::Model> RM) {
186 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000187 // Default relocation model on Darwin is PIC.
188 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000189
Oliver Stannard8331aae2016-08-08 15:28:31 +0000190 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
191 assert(TT.isOSBinFormatELF() &&
192 "ROPI/RWPI currently only supported for ELF");
193
Renato Golin9be886292016-05-28 04:47:13 +0000194 // DynamicNoPIC is only used on darwin.
195 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
196 return Reloc::Static;
197
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000198 return *RM;
199}
200
Rafael Espindola38af4d62016-05-18 16:00:24 +0000201/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000202///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000203ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000204 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000205 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000206 Optional<Reloc::Model> RM,
207 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000208 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000209 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000210 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
211 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000212 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000213 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000214 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000215
216 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000217 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000218 this->Options.FloatABIType =
219 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000220
221 // Default to triple-appropriate EABI
222 if (Options.EABIVersion == EABI::Default ||
223 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000224 // musl is compatible with glibc with regard to EABI version
225 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000226 this->Options.EABIVersion = EABI::GNU;
227 else
228 this->Options.EABIVersion = EABI::EABI5;
229 }
Evan Cheng66cff402008-10-30 16:10:54 +0000230}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000231
Reid Kleckner357600e2014-11-20 23:37:18 +0000232ARMBaseTargetMachine::~ARMBaseTargetMachine() {}
233
Eric Christopher3faf2f12014-10-06 06:45:36 +0000234const ARMSubtarget *
235ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000236 Attribute CPUAttr = F.getFnAttribute("target-cpu");
237 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000238
239 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
240 ? CPUAttr.getValueAsString().str()
241 : TargetCPU;
242 std::string FS = !FSAttr.hasAttribute(Attribute::None)
243 ? FSAttr.getValueAsString().str()
244 : TargetFS;
245
246 // FIXME: This is related to the code below to reset the target options,
247 // we need to know whether or not the soft float flag is set on the
248 // function before we can generate a subtarget. We also need to use
249 // it as a key for the subtarget since that can be the only difference
250 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000251 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000252 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
253 // If the soft float attribute is set on the function turn on the soft float
254 // subtarget feature.
255 if (SoftFloat)
256 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000257
Eric Christopher824f42f2015-05-12 01:26:05 +0000258 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000259 if (!I) {
260 // This needs to be done before we create a new subtarget since any
261 // creation will depend on the TM and the code generation flags on the
262 // function that reside in TargetOptions.
263 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000264 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000265 }
266 return I.get();
267}
268
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000269TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000270 return TargetIRAnalysis([this](const Function &F) {
271 return TargetTransformInfo(ARMTTIImpl(this, F));
272 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000273}
274
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000275void ARMTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000276
Daniel Sanders3e5de882015-06-11 19:41:26 +0000277ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT,
278 StringRef CPU, StringRef FS,
279 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000280 Optional<Reloc::Model> RM,
281 CodeModel::Model CM, CodeGenOpt::Level OL,
282 bool isLittle)
Eric Christopher80b24ef2014-06-26 19:30:02 +0000283 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000284 initAsmInfo();
Evan Cheng5190f092010-08-11 07:17:46 +0000285 if (!Subtarget.hasARMOps())
286 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
287 "support ARM mode execution!");
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000288}
289
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000290void ARMLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000291
Daniel Sanders3e5de882015-06-11 19:41:26 +0000292ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000293 StringRef CPU, StringRef FS,
294 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000295 Optional<Reloc::Model> RM,
296 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000297 CodeGenOpt::Level OL)
298 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000299
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000300void ARMBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000301
Daniel Sanders3e5de882015-06-11 19:41:26 +0000302ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000303 StringRef CPU, StringRef FS,
304 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000305 Optional<Reloc::Model> RM,
306 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000307 CodeGenOpt::Level OL)
308 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000309
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000310void ThumbTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000311
Daniel Sanders3e5de882015-06-11 19:41:26 +0000312ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000313 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000314 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000315 Optional<Reloc::Model> RM,
316 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000317 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000318 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000319 initAsmInfo();
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000320}
321
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000322void ThumbLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000323
Daniel Sanders3e5de882015-06-11 19:41:26 +0000324ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000325 StringRef CPU, StringRef FS,
326 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000327 Optional<Reloc::Model> RM,
328 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000329 CodeGenOpt::Level OL)
330 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000331
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000332void ThumbBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000333
Daniel Sanders3e5de882015-06-11 19:41:26 +0000334ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000335 StringRef CPU, StringRef FS,
336 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000337 Optional<Reloc::Model> RM,
338 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000339 CodeGenOpt::Level OL)
340 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000341
Andrew Trickccb67362012-02-03 05:12:41 +0000342namespace {
343/// ARM Code Generator Pass Configuration Options.
344class ARMPassConfig : public TargetPassConfig {
345public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000346 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
347 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000348
349 ARMBaseTargetMachine &getARMTargetMachine() const {
350 return getTM<ARMBaseTargetMachine>();
351 }
352
Tim Northoverb4ddc082014-05-30 10:09:59 +0000353 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000354 bool addPreISel() override;
355 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000356 void addPreRegAlloc() override;
357 void addPreSched2() override;
358 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000359};
360} // namespace
361
Andrew Trickf8ea1082012-02-04 02:56:59 +0000362TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
363 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000364}
365
Tim Northoverb4ddc082014-05-30 10:09:59 +0000366void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000367 if (TM->Options.ThreadModel == ThreadModel::Single)
368 addPass(createLowerAtomicPass());
369 else
Robin Morisset59c23cd2014-08-21 21:50:01 +0000370 addPass(createAtomicExpandPass(TM));
Tim Northoverc882eb02014-04-03 11:44:58 +0000371
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000372 // Cmpxchg instructions are often used with a subsequent comparison to
373 // determine whether it succeeded. We can exploit existing control-flow in
374 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000375 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
376 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
377 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
378 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
379 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000380
381 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000382
383 // Match interleaved memory accesses to ldN/stN intrinsics.
384 if (TM->getOptLevel() != CodeGenOpt::None)
385 addPass(createInterleavedAccessPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000386}
387
388bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000389 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000390 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000391 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000392 // FIXME: This is using the thumb1 only constant value for
393 // maximal global offset for merging globals. We may want
394 // to look into using the old value for non-thumb1 code of
395 // 4095 based on the TargetMachine, but this starts to become
396 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000397 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
398 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000399 // Merging of extern globals is enabled by default on non-Mach-O as we
400 // expect it to be generally either beneficial or harmless. On Mach-O it
401 // is disabled as we emit the .subsections_via_symbols directive which
402 // means that merging extern globals is not safe.
403 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
404 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
405 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000406 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000407
408 return false;
409}
410
Andrew Trickccb67362012-02-03 05:12:41 +0000411bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000412 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000413 return false;
414}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000415
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000416void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000417 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000418 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000419
420 if (EnableARMLoadStoreOpt)
421 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
422
423 if (!DisableA15SDOptimization)
424 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000425 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000426}
427
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000428void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000429 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000430 if (EnableARMLoadStoreOpt)
431 addPass(createARMLoadStoreOptimizationPass());
432
Eric Christopher7e70aba2015-03-07 00:12:22 +0000433 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass));
Eric Christopher7ae11c62010-11-11 20:50:14 +0000434 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000435
Evan Cheng207b2462009-11-06 23:52:48 +0000436 // Expand some pseudo instructions into multiple instructions to allow
437 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000438 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000439
Evan Chengecb29082011-11-16 08:38:26 +0000440 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000441 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000442 addPass(createThumb2SizeReductionPass([this](const Function &F) {
443 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
444 }));
445
446 addPass(createIfConverter([this](const Function &F) {
447 return !this->TM->getSubtarget<ARMSubtarget>(F).isThumb1Only();
448 }));
Renato Golin4c871392015-03-26 18:38:04 +0000449 }
Eric Christopher63b44882015-03-05 00:23:40 +0000450 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000451}
452
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000453void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000454 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000455
Eric Christopher63b44882015-03-05 00:23:40 +0000456 // Constant island pass work on unbundled instructions.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000457 addPass(createUnpackMachineBundles([this](const Function &F) {
458 return this->TM->getSubtarget<ARMSubtarget>(F).isThumb2();
459 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000460
Davide Italiano141b28912015-05-20 21:40:38 +0000461 // Don't optimize barriers at -O0.
462 if (getOptLevel() != CodeGenOpt::None)
463 addPass(createARMOptimizeBarriersPass());
464
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000465 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000466}