1 //===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
14 #include "ARMFrameLowering.h"
15 #include "ARMTargetMachine.h"
16 #include "ARMTargetObjectFile.h"
17 #include "ARMTargetTransformInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Transforms/Scalar.h"
30 DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
31 cl::desc("Inhibit optimization of S->D register accesses on A15"),
35 EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
36 cl::desc("Run SimplifyCFG after expanding atomic operations"
37 " to make use of cmpxchg flow-based information"),
41 EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
42 cl::desc("Enable ARM load/store optimization pass"),
45 // FIXME: Unify control over GlobalMerge.
46 static cl::opt<cl::boolOrDefault>
47 EnableGlobalMerge("arm-global-merge", cl::Hidden,
48 cl::desc("Enable the global merge pass"));
50 extern "C" void LLVMInitializeARMTarget() {
51 // Register the target.
52 RegisterTargetMachine<ARMLETargetMachine> X(TheARMLETarget);
53 RegisterTargetMachine<ARMBETargetMachine> Y(TheARMBETarget);
54 RegisterTargetMachine<ThumbLETargetMachine> A(TheThumbLETarget);
55 RegisterTargetMachine<ThumbBETargetMachine> B(TheThumbBETarget);
58 static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
59 if (TT.isOSBinFormatMachO())
60 return make_unique<TargetLoweringObjectFileMachO>();
62 return make_unique<TargetLoweringObjectFileCOFF>();
63 return make_unique<ARMElfTargetObjectFile>();
66 static ARMBaseTargetMachine::ARMABI
67 computeTargetABI(const Triple &TT, StringRef CPU,
68 const TargetOptions &Options) {
69 if (Options.MCOptions.getABIName().startswith("aapcs"))
70 return ARMBaseTargetMachine::ARM_ABI_AAPCS;
71 else if (Options.MCOptions.getABIName().startswith("apcs"))
72 return ARMBaseTargetMachine::ARM_ABI_APCS;
74 assert(Options.MCOptions.getABIName().empty() &&
75 "Unknown target-abi option!");
77 ARMBaseTargetMachine::ARMABI TargetABI =
78 ARMBaseTargetMachine::ARM_ABI_UNKNOWN;
80 // FIXME: This is duplicated code from the front end and should be unified.
81 if (TT.isOSBinFormatMachO()) {
82 if (TT.getEnvironment() == llvm::Triple::EABI ||
83 (TT.getOS() == llvm::Triple::UnknownOS &&
84 TT.getObjectFormat() == llvm::Triple::MachO) ||
85 CPU.startswith("cortex-m")) {
86 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
88 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
90 } else if (TT.isOSWindows()) {
91 // FIXME: this is invalid for WindowsCE
92 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
94 // Select the default based on the platform.
95 switch (TT.getEnvironment()) {
96 case llvm::Triple::Android:
97 case llvm::Triple::GNUEABI:
98 case llvm::Triple::GNUEABIHF:
99 case llvm::Triple::EABIHF:
100 case llvm::Triple::EABI:
101 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
103 case llvm::Triple::GNU:
104 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
107 if (TT.getOS() == llvm::Triple::NetBSD)
108 TargetABI = ARMBaseTargetMachine::ARM_ABI_APCS;
110 TargetABI = ARMBaseTargetMachine::ARM_ABI_AAPCS;
118 static std::string computeDataLayout(StringRef TT, StringRef CPU,
119 const TargetOptions &Options,
121 const Triple Triple(TT);
122 auto ABI = computeTargetABI(Triple, CPU, Options);
123 std::string Ret = "";
132 Ret += DataLayout::getManglingComponent(Triple);
134 // Pointers are 32 bits and aligned to 32 bits.
137 // ABIs other than APCS have 64 bit integers with natural alignment.
138 if (ABI != ARMBaseTargetMachine::ARM_ABI_APCS)
141 // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
142 // bits, others to 64 bits. We always try to align to 64 bits.
143 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
146 // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
147 // to 64. We always ty to give them natural alignment.
148 if (ABI == ARMBaseTargetMachine::ARM_ABI_APCS)
149 Ret += "-v64:32:64-v128:32:128";
151 Ret += "-v128:64:128";
153 // Try to align aggregates to 32 bits (the default is 64 bits, which has no
154 // particular hardware support on 32-bit ARM).
157 // Integer registers are 32 bits.
160 // The stack is 128 bit aligned on NaCl, 64 bit aligned on AAPCS and 32 bit
161 // aligned everywhere else.
162 if (Triple.isOSNaCl())
164 else if (ABI == ARMBaseTargetMachine::ARM_ABI_AAPCS)
172 /// TargetMachine ctor - Create an ARM architecture model.
174 ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, StringRef TT,
175 StringRef CPU, StringRef FS,
176 const TargetOptions &Options,
177 Reloc::Model RM, CodeModel::Model CM,
178 CodeGenOpt::Level OL, bool isLittle)
179 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT,
180 CPU, FS, Options, RM, CM, OL),
181 TargetABI(computeTargetABI(Triple(TT), CPU, Options)),
182 TLOF(createTLOF(Triple(getTargetTriple()))),
183 Subtarget(TT, CPU, FS, *this, isLittle), isLittle(isLittle) {
185 // Default to triple-appropriate float ABI
186 if (Options.FloatABIType == FloatABI::Default)
187 this->Options.FloatABIType =
188 Subtarget.isTargetHardFloat() ? FloatABI::Hard : FloatABI::Soft;
191 ARMBaseTargetMachine::~ARMBaseTargetMachine() {}
194 ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
195 Attribute CPUAttr = F.getFnAttribute("target-cpu");
196 Attribute FSAttr = F.getFnAttribute("target-features");
198 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
199 ? CPUAttr.getValueAsString().str()
201 std::string FS = !FSAttr.hasAttribute(Attribute::None)
202 ? FSAttr.getValueAsString().str()
205 // FIXME: This is related to the code below to reset the target options,
206 // we need to know whether or not the soft float flag is set on the
207 // function before we can generate a subtarget. We also need to use
208 // it as a key for the subtarget since that can be the only difference
209 // between two functions.
211 F.hasFnAttribute("use-soft-float") &&
212 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
213 // If the soft float attribute is set on the function turn on the soft float
214 // subtarget feature.
216 FS += FS.empty() ? "+soft-float" : ",+soft-float";
218 auto &I = SubtargetMap[CPU + FS];
220 // This needs to be done before we create a new subtarget since any
221 // creation will depend on the TM and the code generation flags on the
222 // function that reside in TargetOptions.
223 resetTargetOptions(F);
224 I = llvm::make_unique<ARMSubtarget>(TargetTriple, CPU, FS, *this, isLittle);
229 TargetIRAnalysis ARMBaseTargetMachine::getTargetIRAnalysis() {
230 return TargetIRAnalysis(
231 [this](Function &F) { return TargetTransformInfo(ARMTTIImpl(this, F)); });
235 void ARMTargetMachine::anchor() { }
237 ARMTargetMachine::ARMTargetMachine(const Target &T, StringRef TT, StringRef CPU,
238 StringRef FS, const TargetOptions &Options,
239 Reloc::Model RM, CodeModel::Model CM,
240 CodeGenOpt::Level OL, bool isLittle)
241 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, isLittle) {
243 if (!Subtarget.hasARMOps())
244 report_fatal_error("CPU: '" + Subtarget.getCPUString() + "' does not "
245 "support ARM mode execution!");
248 void ARMLETargetMachine::anchor() { }
250 ARMLETargetMachine::ARMLETargetMachine(const Target &T, StringRef TT,
251 StringRef CPU, StringRef FS,
252 const TargetOptions &Options,
253 Reloc::Model RM, CodeModel::Model CM,
254 CodeGenOpt::Level OL)
255 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
257 void ARMBETargetMachine::anchor() { }
259 ARMBETargetMachine::ARMBETargetMachine(const Target &T, StringRef TT,
260 StringRef CPU, StringRef FS,
261 const TargetOptions &Options,
262 Reloc::Model RM, CodeModel::Model CM,
263 CodeGenOpt::Level OL)
264 : ARMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
266 void ThumbTargetMachine::anchor() { }
268 ThumbTargetMachine::ThumbTargetMachine(const Target &T, StringRef TT,
269 StringRef CPU, StringRef FS,
270 const TargetOptions &Options,
271 Reloc::Model RM, CodeModel::Model CM,
272 CodeGenOpt::Level OL, bool isLittle)
273 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL,
278 void ThumbLETargetMachine::anchor() { }
280 ThumbLETargetMachine::ThumbLETargetMachine(const Target &T, StringRef TT,
281 StringRef CPU, StringRef FS,
282 const TargetOptions &Options,
283 Reloc::Model RM, CodeModel::Model CM,
284 CodeGenOpt::Level OL)
285 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
287 void ThumbBETargetMachine::anchor() { }
289 ThumbBETargetMachine::ThumbBETargetMachine(const Target &T, StringRef TT,
290 StringRef CPU, StringRef FS,
291 const TargetOptions &Options,
292 Reloc::Model RM, CodeModel::Model CM,
293 CodeGenOpt::Level OL)
294 : ThumbTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
297 /// ARM Code Generator Pass Configuration Options.
298 class ARMPassConfig : public TargetPassConfig {
300 ARMPassConfig(ARMBaseTargetMachine *TM, PassManagerBase &PM)
301 : TargetPassConfig(TM, PM) {}
303 ARMBaseTargetMachine &getARMTargetMachine() const {
304 return getTM<ARMBaseTargetMachine>();
307 void addIRPasses() override;
308 bool addPreISel() override;
309 bool addInstSelector() override;
310 void addPreRegAlloc() override;
311 void addPreSched2() override;
312 void addPreEmitPass() override;
316 TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
317 return new ARMPassConfig(this, PM);
320 void ARMPassConfig::addIRPasses() {
321 if (TM->Options.ThreadModel == ThreadModel::Single)
322 addPass(createLowerAtomicPass());
324 addPass(createAtomicExpandPass(TM));
326 // Cmpxchg instructions are often used with a subsequent comparison to
327 // determine whether it succeeded. We can exploit existing control-flow in
328 // ldrex/strex loops to simplify this, but it needs tidying up.
329 if (TM->getOptLevel() != CodeGenOpt::None && EnableAtomicTidy)
330 addPass(createCFGSimplificationPass(-1, [this](const Function &F) {
331 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
332 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
335 TargetPassConfig::addIRPasses();
338 bool ARMPassConfig::addPreISel() {
339 if ((TM->getOptLevel() != CodeGenOpt::None &&
340 EnableGlobalMerge == cl::BOU_UNSET) ||
341 EnableGlobalMerge == cl::BOU_TRUE) {
342 // FIXME: This is using the thumb1 only constant value for
343 // maximal global offset for merging globals. We may want
344 // to look into using the old value for non-thumb1 code of
345 // 4095 based on the TargetMachine, but this starts to become
346 // tricky when doing code gen per function.
347 bool OnlyOptimizeForSize = (TM->getOptLevel() < CodeGenOpt::Aggressive) &&
348 (EnableGlobalMerge == cl::BOU_UNSET);
349 addPass(createGlobalMergePass(TM, 127, OnlyOptimizeForSize));
355 bool ARMPassConfig::addInstSelector() {
356 addPass(createARMISelDag(getARMTargetMachine(), getOptLevel()));
358 if (Triple(TM->getTargetTriple()).isOSBinFormatELF() &&
359 TM->Options.EnableFastISel)
360 addPass(createARMGlobalBaseRegPass());
364 void ARMPassConfig::addPreRegAlloc() {
365 if (getOptLevel() != CodeGenOpt::None) {
366 addPass(createMLxExpansionPass());
368 if (EnableARMLoadStoreOpt)
369 addPass(createARMLoadStoreOptimizationPass(/* pre-register alloc */ true));
371 if (!DisableA15SDOptimization)
372 addPass(createA15SDOptimizerPass());
376 void ARMPassConfig::addPreSched2() {
377 if (getOptLevel() != CodeGenOpt::None) {
378 if (EnableARMLoadStoreOpt)
379 addPass(createARMLoadStoreOptimizationPass());
381 addPass(createExecutionDependencyFixPass(&ARM::DPRRegClass));
384 // Expand some pseudo instructions into multiple instructions to allow
385 // proper scheduling.
386 addPass(createARMExpandPseudoPass());
388 if (getOptLevel() != CodeGenOpt::None) {
389 // in v8, IfConversion depends on Thumb instruction widths
390 addPass(createThumb2SizeReductionPass([this](const Function &F) {
391 return this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
394 addPass(createIfConverter([this](const Function &F) {
395 return !this->TM->getSubtarget<ARMSubtarget>(F).isThumb1Only();
398 addPass(createThumb2ITBlockPass());
401 void ARMPassConfig::addPreEmitPass() {
402 addPass(createThumb2SizeReductionPass());
404 // Constant island pass work on unbundled instructions.
405 addPass(createUnpackMachineBundles([this](const Function &F) {
406 return this->TM->getSubtarget<ARMSubtarget>(F).isThumb2();
409 // Don't optimize barriers at -O0.
410 if (getOptLevel() != CodeGenOpt::None)
411 addPass(createARMOptimizeBarriersPass());
413 addPass(createARMConstantIslandPass());