blob: ab20eafec4b37144eac978539e9657020fbc6efb [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"
Evan Cheng10043e22007-01-19 07:51:42 +000026#include "llvm/Target/TargetOptions.h"
Devang Patel76c85632011-10-17 17:17:43 +000027#include "llvm/Transforms/Scalar.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000028using namespace llvm;
29
Evan Chengf066b2f2011-08-25 01:00:36 +000030static cl::opt<bool>
Silviu Baranga82dd6ac2013-03-15 18:28:25 +000031DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
32 cl::desc("Inhibit optimization of S->D register accesses on A15"),
33 cl::init(false));
34
Tim Northoverb4ddc082014-05-30 10:09:59 +000035static cl::opt<bool>
36EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
37 cl::desc("Run SimplifyCFG after expanding atomic operations"
38 " to make use of cmpxchg flow-based information"),
39 cl::init(true));
40
Renato Golin4c871392015-03-26 18:38:04 +000041static cl::opt<bool>
42EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
43 cl::desc("Enable ARM load/store optimization pass"),
44 cl::init(true));
45
Ahmed Bougachab96444e2015-04-11 00:06:36 +000046// FIXME: Unify control over GlobalMerge.
47static cl::opt<cl::boolOrDefault>
48EnableGlobalMerge("arm-global-merge", cl::Hidden,
49 cl::desc("Enable the global merge pass"));
50
Jim Grosbachf24f9d92009-08-11 15:33:49 +000051extern "C" void LLVMInitializeARMTarget() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000052 // Register the target.
Christian Pirkerdc9ff752014-04-01 15:19:30 +000053 RegisterTargetMachine<ARMLETargetMachine> X(TheARMLETarget);
54 RegisterTargetMachine<ARMBETargetMachine> Y(TheARMBETarget);
55 RegisterTargetMachine<ThumbLETargetMachine> A(TheThumbLETarget);
56 RegisterTargetMachine<ThumbBETargetMachine> B(TheThumbBETarget);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000057}
Douglas Gregor1b731d52009-06-16 20:12:29 +000058
Aditya Nandakumara2719322014-11-13 09:26:31 +000059static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
60 if (TT.isOSBinFormatMachO())
61 return make_unique<TargetLoweringObjectFileMachO>();
62 if (TT.isOSWindows())
63 return make_unique<TargetLoweringObjectFileCOFF>();
64 return make_unique<ARMElfTargetObjectFile>();
65}
66
Eric Christopher661f2d12014-12-18 02:20:58 +000067static ARMBaseTargetMachine::ARMABI
68computeTargetABI(const Triple &TT, StringRef CPU,
69 const TargetOptions &Options) {
Tim Northovere0ccdc62015-10-28 22:46:43 +000070 if (Options.MCOptions.getABIName() == "aapcs16")
71 return ARMBaseTargetMachine::ARM_ABI_AAPCS16;
72 else if (Options.MCOptions.getABIName().startswith("aapcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000073 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher6e30cd92015-01-14 00:50:31 +000074 else if (Options.MCOptions.getABIName().startswith("apcs"))
Eric Christopher661f2d12014-12-18 02:20:58 +000075 return ARMBaseTargetMachine::ARM_ABI_APCS;
76
Eric Christopher6e30cd92015-01-14 00:50:31 +000077 assert(Options.MCOptions.getABIName().empty() &&
78 "Unknown target-abi option!");
Eric Christopher661f2d12014-12-18 02:20:58 +000079
80 ARMBaseTargetMachine::ARMABI TargetABI =
81 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
82
83 // FIXME: This is duplicated code from the front end and should be unified.
84 if (TT.isOSBinFormatMachO()) {
85 if (TT.getEnvironment() == llvm::Triple::EABI ||
Daniel Sandersfbdab432015-07-06 16:33:18 +000086 (TT.getOS() == llvm::Triple::UnknownOS && TT.isOSBinFormatMachO()) ||
Eric Christopher661f2d12014-12-18 02:20:58 +000087 CPU.startswith("cortex-m")) {
88 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Tim Northover042a6c12016-01-27 19:32:29 +000089 } else if (TT.isWatchABI()) {
Tim Northovere0ccdc62015-10-28 22:46:43 +000090 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS16;
Eric Christopher661f2d12014-12-18 02:20:58 +000091 } else {
92 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
93 }
94 } else if (TT.isOSWindows()) {
95 // FIXME: this is invalid for WindowsCE
96 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
97 } else {
98 // Select the default based on the platform.
99 switch (TT.getEnvironment()) {
100 case llvm::Triple::Android:
101 case llvm::Triple::GNUEABI:
102 case llvm::Triple::GNUEABIHF:
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000103 case llvm::Triple::MuslEABI:
104 case llvm::Triple::MuslEABIHF:
Eric Christopher661f2d12014-12-18 02:20:58 +0000105 case llvm::Triple::EABIHF:
106 case llvm::Triple::EABI:
107 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
108 break;
109 case llvm::Triple::GNU:
110 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
111 break;
112 default:
Daniel Sandersfbdab432015-07-06 16:33:18 +0000113 if (TT.isOSNetBSD())
114 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000115 else
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000116 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
Eric Christopher661f2d12014-12-18 02:20:58 +0000117 break;
118 }
119 }
120
121 return TargetABI;
122}
123
Daniel Sandersed64d622015-06-11 15:34:59 +0000124static std::string computeDataLayout(const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000125 const TargetOptions &Options,
Eric Christopher8b770652015-01-26 19:03:15 +0000126 bool isLittle) {
Daniel Sandersed64d622015-06-11 15:34:59 +0000127 auto ABI = computeTargetABI(TT, CPU, Options);
Eric Christopher8b770652015-01-26 19:03:15 +0000128 std::string Ret = "";
129
130 if (isLittle)
131 // Little endian.
132 Ret += "e";
133 else
134 // Big endian.
135 Ret += "E";
136
Daniel Sandersed64d622015-06-11 15:34:59 +0000137 Ret += DataLayout::getManglingComponent(TT);
Eric Christopher8b770652015-01-26 19:03:15 +0000138
139 // Pointers are 32 bits and aligned to 32 bits.
140 Ret += "-p:32:32";
141
142 // ABIs other than APCS have 64 bit integers with natural alignment.
143 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
144 Ret += "-i64:64";
145
146 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
147 // bits, others to 64 bits. We always try to align to 64 bits.
148 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
149 Ret += "-f64:32:64";
150
151 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
152 // to 64. We always ty to give them natural alignment.
153 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
154 Ret += "-v64:32:64-v128:32:128";
Tim Northovere0ccdc62015-10-28 22:46:43 +0000155 else if (ABI != ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000156 Ret += "-v128:64:128";
157
158 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
159 // particular hardware support on 32-bit ARM).
160 Ret += "-a:0:32";
161
162 // Integer registers are 32 bits.
163 Ret += "-n32";
164
165 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
166 // aligned everywhere else.
Tim Northovere0ccdc62015-10-28 22:46:43 +0000167 if (TT.isOSNaCl() || ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS16)
Eric Christopher8b770652015-01-26 19:03:15 +0000168 Ret += "-S128";
169 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
170 Ret += "-S64";
171 else
172 Ret += "-S32";
173
174 return Ret;
175}
176
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000177static Reloc::Model getEffectiveRelocModel(const Triple &TT,
178 Optional<Reloc::Model> RM) {
179 if (!RM.hasValue())
Rafael Espindolafe796dc2016-05-28 10:41:15 +0000180 // Default relocation model on Darwin is PIC.
181 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
Renato Golin9be886292016-05-28 04:47:13 +0000182
183 // DynamicNoPIC is only used on darwin.
184 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
185 return Reloc::Static;
186
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000187 return *RM;
188}
189
Rafael Espindola38af4d62016-05-18 16:00:24 +0000190/// Create an ARM architecture model.
Evan Cheng9f830142007-02-23 03:14:31 +0000191///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000192ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000193 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000194 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000195 Optional<Reloc::Model> RM,
196 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000197 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000198 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000199 CPU, FS, Options, getEffectiveRelocModel(TT, RM), CM,
200 OL),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000201 TargetABI(computeTargetABI(TT, CPU, Options)),
Daniel Sandersc81f4502015-06-16 15:44:21 +0000202 TLOF(createTLOF(getTargetTriple())),
Daniel Sanders3e5de882015-06-11 19:41:26 +0000203 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
Tim Northoverf1c31b92013-12-18 14:18:36 +0000204
205 // Default to triple-appropriate float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000206 if (Options.FloatABIType == FloatABI::Default)
Tim Northover44594ad2013-12-18 09:27:33 +0000207 this->Options.FloatABIType =
208 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
Renato Golin6d435f12015-11-09 12:40:30 +0000209
210 // Default to triple-appropriate EABI
211 if (Options.EABIVersion == EABI::Default ||
212 Options.EABIVersion == EABI::Unknown) {
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000213 // musl is compatible with glibc with regard to EABI version
214 if (Subtarget.isTargetGNUAEABI() || Subtarget.isTargetMuslAEABI())
Renato Golin6d435f12015-11-09 12:40:30 +0000215 this->Options.EABIVersion = EABI::GNU;
216 else
217 this->Options.EABIVersion = EABI::EABI5;
218 }
Evan Cheng66cff402008-10-30 16:10:54 +0000219}
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000220
Reid Kleckner357600e2014-11-20 23:37:18 +0000221ARMBaseTargetMachine::~ARMBaseTargetMachine() {}
222
Eric Christopher3faf2f12014-10-06 06:45:36 +0000223const ARMSubtarget *
224ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +0000225 Attribute CPUAttr = F.getFnAttribute("target-cpu");
226 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000227
228 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
229 ? CPUAttr.getValueAsString().str()
230 : TargetCPU;
231 std::string FS = !FSAttr.hasAttribute(Attribute::None)
232 ? FSAttr.getValueAsString().str()
233 : TargetFS;
234
235 // FIXME: This is related to the code below to reset the target options,
236 // we need to know whether or not the soft float flag is set on the
237 // function before we can generate a subtarget. We also need to use
238 // it as a key for the subtarget since that can be the only difference
239 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000240 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000241 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
242 // If the soft float attribute is set on the function turn on the soft float
243 // subtarget feature.
244 if (SoftFloat)
245 FS += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000246
Eric Christopher824f42f2015-05-12 01:26:05 +0000247 auto &I = SubtargetMap[CPU + FS];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000248 if (!I) {
249 // This needs to be done before we create a new subtarget since any
250 // creation will depend on the TM and the code generation flags on the
251 // function that reside in TargetOptions.
252 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000253 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000254 }
255 return I.get();
256}
257
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000258TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000259 return TargetIRAnalysis([this](const Function &F) {
260 return TargetTransformInfo(ARMTTIImpl(this, F));
261 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000262}
263
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000264void ARMTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000265
Daniel Sanders3e5de882015-06-11 19:41:26 +0000266ARMTargetMachine::ARMTargetMachine(const Target &T, const Triple &TT,
267 StringRef CPU, StringRef FS,
268 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000269 Optional<Reloc::Model> RM,
270 CodeModel::Model CM, CodeGenOpt::Level OL,
271 bool isLittle)
Eric Christopher80b24ef2014-06-26 19:30:02 +0000272 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000273 initAsmInfo();
Evan Cheng5190f092010-08-11 07:17:46 +0000274 if (!Subtarget.hasARMOps())
275 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
276 "support ARM mode execution!");
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000277}
278
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000279void ARMLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000280
Daniel Sanders3e5de882015-06-11 19:41:26 +0000281ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000282 StringRef CPU, StringRef FS,
283 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000284 Optional<Reloc::Model> RM,
285 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000286 CodeGenOpt::Level OL)
287 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000288
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000289void ARMBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000290
Daniel Sanders3e5de882015-06-11 19:41:26 +0000291ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000292 StringRef CPU, StringRef FS,
293 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000294 Optional<Reloc::Model> RM,
295 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000296 CodeGenOpt::Level OL)
297 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000298
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000299void ThumbTargetMachine::anchor() {}
David Blaikiea379b1812011-12-20 02:50:00 +0000300
Daniel Sanders3e5de882015-06-11 19:41:26 +0000301ThumbTargetMachine::ThumbTargetMachine(const Target &T, const Triple &TT,
Evan Cheng2129f592011-07-19 06:37:02 +0000302 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000303 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000304 Optional<Reloc::Model> RM,
305 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000306 CodeGenOpt::Level OL, bool isLittle)
Daniel Sanders3e5de882015-06-11 19:41:26 +0000307 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
Rafael Espindola227144c2013-05-13 01:16:13 +0000308 initAsmInfo();
Anton Korobeynikov99152f32009-06-26 21:28:53 +0000309}
310
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000311void ThumbLETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000312
Daniel Sanders3e5de882015-06-11 19:41:26 +0000313ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000314 StringRef CPU, StringRef FS,
315 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000316 Optional<Reloc::Model> RM,
317 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000318 CodeGenOpt::Level OL)
319 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000320
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000321void ThumbBETargetMachine::anchor() {}
Christian Pirker2a111602014-03-28 14:35:30 +0000322
Daniel Sanders3e5de882015-06-11 19:41:26 +0000323ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, const Triple &TT,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000324 StringRef CPU, StringRef FS,
325 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000326 Optional<Reloc::Model> RM,
327 CodeModel::Model CM,
Eric Christopher80b24ef2014-06-26 19:30:02 +0000328 CodeGenOpt::Level OL)
329 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
Christian Pirker2a111602014-03-28 14:35:30 +0000330
Andrew Trickccb67362012-02-03 05:12:41 +0000331namespace {
332/// ARM Code Generator Pass Configuration Options.
333class ARMPassConfig : public TargetPassConfig {
334public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000335 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
336 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000337
338 ARMBaseTargetMachine &getARMTargetMachine() const {
339 return getTM<ARMBaseTargetMachine>();
340 }
341
Tim Northoverb4ddc082014-05-30 10:09:59 +0000342 void addIRPasses() override;
Craig Topper6bc27bf2014-03-10 02:09:33 +0000343 bool addPreISel() override;
344 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000345 void addPreRegAlloc() override;
346 void addPreSched2() override;
347 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000348};
349} // namespace
350
Andrew Trickf8ea1082012-02-04 02:56:59 +0000351TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
352 return new ARMPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000353}
354
Tim Northoverb4ddc082014-05-30 10:09:59 +0000355void ARMPassConfig::addIRPasses() {
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000356 if (TM->Options.ThreadModel == ThreadModel::Single)
357 addPass(createLowerAtomicPass());
358 else
Robin Morisset59c23cd2014-08-21 21:50:01 +0000359 addPass(createAtomicExpandPass(TM));
Tim Northoverc882eb02014-04-03 11:44:58 +0000360
Eric Christopherc40e5ed2014-06-19 21:03:04 +0000361 // Cmpxchg instructions are often used with a subsequent comparison to
362 // determine whether it succeeded. We can exploit existing control-flow in
363 // ldrex/strex loops to simplify this, but it needs tidying up.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000364 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
365 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
366 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
367 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
368 }));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000369
370 TargetPassConfig::addIRPasses();
Hao Liu2cd34bb2015-06-26 02:45:36 +0000371
372 // Match interleaved memory accesses to ldN/stN intrinsics.
373 if (TM->getOptLevel() != CodeGenOpt::None)
374 addPass(createInterleavedAccessPass(TM));
Tim Northoverb4ddc082014-05-30 10:09:59 +0000375}
376
377bool ARMPassConfig::addPreISel() {
Ahmed Bougacha82076412015-06-04 20:39:23 +0000378 if ((TM->getOptLevel() != CodeGenOpt::None &&
Ahmed Bougachab96444e2015-04-11 00:06:36 +0000379 EnableGlobalMerge == cl::BOU_UNSET) ||
Ahmed Bougacha82076412015-06-04 20:39:23 +0000380 EnableGlobalMerge == cl::BOU_TRUE) {
Eric Christophered47b222015-02-23 19:28:45 +0000381 // FIXME: This is using the thumb1 only constant value for
382 // maximal global offset for merging globals. We may want
383 // to look into using the old value for non-thumb1 code of
384 // 4095 based on the TargetMachine, but this starts to become
385 // tricky when doing code gen per function.
Ahmed Bougacha82076412015-06-04 20:39:23 +0000386 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
387 (EnableGlobalMerge == cl::BOU_UNSET);
John Brawnf3324cf2015-08-03 12:13:33 +0000388 // Merging of extern globals is enabled by default on non-Mach-O as we
389 // expect it to be generally either beneficial or harmless. On Mach-O it
390 // is disabled as we emit the .subsections_via_symbols directive which
391 // means that merging extern globals is not safe.
392 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
393 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize,
394 MergeExternalByDefault));
Ahmed Bougacha82076412015-06-04 20:39:23 +0000395 }
Anton Korobeynikov19edda02010-07-24 21:52:08 +0000396
397 return false;
398}
399
Andrew Trickccb67362012-02-03 05:12:41 +0000400bool ARMPassConfig::addInstSelector() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000401 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
Chris Lattner12e97302006-09-04 04:14:57 +0000402 return false;
403}
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000404
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000405void ARMPassConfig::addPreRegAlloc() {
Renato Golin4c871392015-03-26 18:38:04 +0000406 if (getOptLevel() != CodeGenOpt::None) {
Matthias Braunb2f23882014-12-11 23:18:03 +0000407 addPass(createMLxExpansionPass());
Renato Golin4c871392015-03-26 18:38:04 +0000408
409 if (EnableARMLoadStoreOpt)
410 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
411
412 if (!DisableA15SDOptimization)
413 addPass(createA15SDOptimizerPass());
Silviu Baranga82dd6ac2013-03-15 18:28:25 +0000414 }
Evan Cheng185c9ef2009-06-13 09:12:55 +0000415}
416
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000417void ARMPassConfig::addPreSched2() {
Evan Chengecb29082011-11-16 08:38:26 +0000418 if (getOptLevel() != CodeGenOpt::None) {
Renato Golin4c871392015-03-26 18:38:04 +0000419 if (EnableARMLoadStoreOpt)
420 addPass(createARMLoadStoreOptimizationPass());
421
Eric Christopher7e70aba2015-03-07 00:12:22 +0000422 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass));
Eric Christopher7ae11c62010-11-11 20:50:14 +0000423 }
Evan Chengce5a8ca2009-09-30 08:53:01 +0000424
Evan Cheng207b2462009-11-06 23:52:48 +0000425 // Expand some pseudo instructions into multiple instructions to allow
426 // proper scheduling.
Matthias Braunb2f23882014-12-11 23:18:03 +0000427 addPass(createARMExpandPseudoPass());
Evan Cheng207b2462009-11-06 23:52:48 +0000428
Evan Chengecb29082011-11-16 08:38:26 +0000429 if (getOptLevel() != CodeGenOpt::None) {
Eric Christopher63b44882015-03-05 00:23:40 +0000430 // in v8, IfConversion depends on Thumb instruction widths
Akira Hatanaka4a616192015-06-08 18:50:43 +0000431 addPass(createThumb2SizeReductionPass([this](const Function &F) {
432 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
433 }));
434
435 addPass(createIfConverter([this](const Function &F) {
436 return !this->TM->getSubtarget<ARMSubtarget>(F).isThumb1Only();
437 }));
Renato Golin4c871392015-03-26 18:38:04 +0000438 }
Eric Christopher63b44882015-03-05 00:23:40 +0000439 addPass(createThumb2ITBlockPass());
Evan Chengce5a8ca2009-09-30 08:53:01 +0000440}
441
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000442void ARMPassConfig::addPreEmitPass() {
Eric Christopher63b44882015-03-05 00:23:40 +0000443 addPass(createThumb2SizeReductionPass());
Evan Cheng7fae11b2011-12-14 02:11:42 +0000444
Eric Christopher63b44882015-03-05 00:23:40 +0000445 // Constant island pass work on unbundled instructions.
Akira Hatanaka4a616192015-06-08 18:50:43 +0000446 addPass(createUnpackMachineBundles([this](const Function &F) {
447 return this->TM->getSubtarget<ARMSubtarget>(F).isThumb2();
448 }));
Evan Cheng0f9cce72009-07-10 01:54:42 +0000449
Davide Italiano141b28912015-05-20 21:40:38 +0000450 // Don't optimize barriers at -O0.
451 if (getOptLevel() != CodeGenOpt::None)
452 addPass(createARMOptimizeBarriersPass());
453
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000454 addPass(createARMConstantIslandPass());
Rafael Espindolaf7d4a992006-09-19 15:49:25 +0000455}