]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Driver/ToolChains/Arch/ARM.cpp
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / lib / Driver / ToolChains / Arch / ARM.cpp
1 //===--- ARM.cpp - ARM (not AArch64) Helpers for Tools ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ARM.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/TargetParser.h"
17
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang;
21 using namespace llvm::opt;
22
23 // Get SubArch (vN).
24 int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) {
25   llvm::StringRef Arch = Triple.getArchName();
26   return llvm::ARM::parseArchVersion(Arch);
27 }
28
29 // True if M-profile.
30 bool arm::isARMMProfile(const llvm::Triple &Triple) {
31   llvm::StringRef Arch = Triple.getArchName();
32   return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::M;
33 }
34
35 // Get Arch/CPU from args.
36 void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
37                                 llvm::StringRef &CPU, bool FromAs) {
38   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
39     CPU = A->getValue();
40   if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
41     Arch = A->getValue();
42   if (!FromAs)
43     return;
44
45   for (const Arg *A :
46        Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
47     StringRef Value = A->getValue();
48     if (Value.startswith("-mcpu="))
49       CPU = Value.substr(6);
50     if (Value.startswith("-march="))
51       Arch = Value.substr(7);
52   }
53 }
54
55 // Handle -mhwdiv=.
56 // FIXME: Use ARMTargetParser.
57 static void getARMHWDivFeatures(const Driver &D, const Arg *A,
58                                 const ArgList &Args, StringRef HWDiv,
59                                 std::vector<StringRef> &Features) {
60   unsigned HWDivID = llvm::ARM::parseHWDiv(HWDiv);
61   if (!llvm::ARM::getHWDivFeatures(HWDivID, Features))
62     D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
63 }
64
65 // Handle -mfpu=.
66 static void getARMFPUFeatures(const Driver &D, const Arg *A,
67                               const ArgList &Args, StringRef FPU,
68                               std::vector<StringRef> &Features) {
69   unsigned FPUID = llvm::ARM::parseFPU(FPU);
70   if (!llvm::ARM::getFPUFeatures(FPUID, Features))
71     D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
72 }
73
74 // Decode ARM features from string like +[no]featureA+[no]featureB+...
75 static bool DecodeARMFeatures(const Driver &D, StringRef text,
76                               std::vector<StringRef> &Features) {
77   SmallVector<StringRef, 8> Split;
78   text.split(Split, StringRef("+"), -1, false);
79
80   for (StringRef Feature : Split) {
81     StringRef FeatureName = llvm::ARM::getArchExtFeature(Feature);
82     if (!FeatureName.empty())
83       Features.push_back(FeatureName);
84     else
85       return false;
86   }
87   return true;
88 }
89
90 static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU,
91                                      std::vector<StringRef> &Features) {
92   if (CPU != "generic") {
93     llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
94     unsigned Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind);
95     llvm::ARM::getExtensionFeatures(Extension, Features);
96   }
97 }
98
99 // Check if -march is valid by checking if it can be canonicalised and parsed.
100 // getARMArch is used here instead of just checking the -march value in order
101 // to handle -march=native correctly.
102 static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
103                              llvm::StringRef ArchName,
104                              std::vector<StringRef> &Features,
105                              const llvm::Triple &Triple) {
106   std::pair<StringRef, StringRef> Split = ArchName.split("+");
107
108   std::string MArch = arm::getARMArch(ArchName, Triple);
109   if (llvm::ARM::parseArch(MArch) == llvm::ARM::ArchKind::INVALID ||
110       (Split.second.size() && !DecodeARMFeatures(D, Split.second, Features)))
111     D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
112 }
113
114 // Check -mcpu=. Needs ArchName to handle -mcpu=generic.
115 static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
116                             llvm::StringRef CPUName, llvm::StringRef ArchName,
117                             std::vector<StringRef> &Features,
118                             const llvm::Triple &Triple) {
119   std::pair<StringRef, StringRef> Split = CPUName.split("+");
120
121   std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
122   if (arm::getLLVMArchSuffixForARM(CPU, ArchName, Triple).empty() ||
123       (Split.second.size() && !DecodeARMFeatures(D, Split.second, Features)))
124     D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
125 }
126
127 bool arm::useAAPCSForMachO(const llvm::Triple &T) {
128   // The backend is hardwired to assume AAPCS for M-class processors, ensure
129   // the frontend matches that.
130   return T.getEnvironment() == llvm::Triple::EABI ||
131          T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T);
132 }
133
134 // Select mode for reading thread pointer (-mtp=soft/cp15).
135 arm::ReadTPMode arm::getReadTPMode(const ToolChain &TC, const ArgList &Args) {
136   if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
137     const Driver &D = TC.getDriver();
138     arm::ReadTPMode ThreadPointer =
139         llvm::StringSwitch<arm::ReadTPMode>(A->getValue())
140             .Case("cp15", ReadTPMode::Cp15)
141             .Case("soft", ReadTPMode::Soft)
142             .Default(ReadTPMode::Invalid);
143     if (ThreadPointer != ReadTPMode::Invalid)
144       return ThreadPointer;
145     if (StringRef(A->getValue()).empty())
146       D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args);
147     else
148       D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
149     return ReadTPMode::Invalid;
150   }
151   return ReadTPMode::Soft;
152 }
153
154 // Select the float ABI as determined by -msoft-float, -mhard-float, and
155 // -mfloat-abi=.
156 arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
157   const Driver &D = TC.getDriver();
158   const llvm::Triple &Triple = TC.getEffectiveTriple();
159   auto SubArch = getARMSubArchVersionNumber(Triple);
160   arm::FloatABI ABI = FloatABI::Invalid;
161   if (Arg *A =
162           Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
163                           options::OPT_mfloat_abi_EQ)) {
164     if (A->getOption().matches(options::OPT_msoft_float)) {
165       ABI = FloatABI::Soft;
166     } else if (A->getOption().matches(options::OPT_mhard_float)) {
167       ABI = FloatABI::Hard;
168     } else {
169       ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue())
170                 .Case("soft", FloatABI::Soft)
171                 .Case("softfp", FloatABI::SoftFP)
172                 .Case("hard", FloatABI::Hard)
173                 .Default(FloatABI::Invalid);
174       if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
175         D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
176         ABI = FloatABI::Soft;
177       }
178     }
179
180     // It is incorrect to select hard float ABI on MachO platforms if the ABI is
181     // "apcs-gnu".
182     if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple) &&
183         ABI == FloatABI::Hard) {
184       D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args)
185                                                        << Triple.getArchName();
186     }
187   }
188
189   // If unspecified, choose the default based on the platform.
190   if (ABI == FloatABI::Invalid) {
191     switch (Triple.getOS()) {
192     case llvm::Triple::Darwin:
193     case llvm::Triple::MacOSX:
194     case llvm::Triple::IOS:
195     case llvm::Triple::TvOS: {
196       // Darwin defaults to "softfp" for v6 and v7.
197       ABI = (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
198       ABI = Triple.isWatchABI() ? FloatABI::Hard : ABI;
199       break;
200     }
201     case llvm::Triple::WatchOS:
202       ABI = FloatABI::Hard;
203       break;
204
205     // FIXME: this is invalid for WindowsCE
206     case llvm::Triple::Win32:
207       ABI = FloatABI::Hard;
208       break;
209
210     case llvm::Triple::NetBSD:
211       switch (Triple.getEnvironment()) {
212       case llvm::Triple::EABIHF:
213       case llvm::Triple::GNUEABIHF:
214         ABI = FloatABI::Hard;
215         break;
216       default:
217         ABI = FloatABI::Soft;
218         break;
219       }
220       break;
221
222     case llvm::Triple::FreeBSD:
223       switch (Triple.getEnvironment()) {
224       case llvm::Triple::GNUEABIHF:
225         ABI = FloatABI::Hard;
226         break;
227       default:
228         // FreeBSD defaults to soft float
229         ABI = FloatABI::Soft;
230         break;
231       }
232       break;
233
234     case llvm::Triple::OpenBSD:
235       ABI = FloatABI::SoftFP;
236       break;
237
238     default:
239       switch (Triple.getEnvironment()) {
240       case llvm::Triple::GNUEABIHF:
241       case llvm::Triple::MuslEABIHF:
242       case llvm::Triple::EABIHF:
243         ABI = FloatABI::Hard;
244         break;
245       case llvm::Triple::GNUEABI:
246       case llvm::Triple::MuslEABI:
247       case llvm::Triple::EABI:
248         // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
249         ABI = FloatABI::SoftFP;
250         break;
251       case llvm::Triple::Android:
252         ABI = (SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
253         break;
254       default:
255         // Assume "soft", but warn the user we are guessing.
256         if (Triple.isOSBinFormatMachO() &&
257             Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em)
258           ABI = FloatABI::Hard;
259         else
260           ABI = FloatABI::Soft;
261
262         if (Triple.getOS() != llvm::Triple::UnknownOS ||
263             !Triple.isOSBinFormatMachO())
264           D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
265         break;
266       }
267     }
268   }
269
270   assert(ABI != FloatABI::Invalid && "must select an ABI");
271   return ABI;
272 }
273
274 void arm::getARMTargetFeatures(const ToolChain &TC,
275                                const llvm::Triple &Triple,
276                                const ArgList &Args,
277                                ArgStringList &CmdArgs,
278                                std::vector<StringRef> &Features,
279                                bool ForAS) {
280   const Driver &D = TC.getDriver();
281
282   bool KernelOrKext =
283       Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
284   arm::FloatABI ABI = arm::getARMFloatABI(TC, Args);
285   arm::ReadTPMode ThreadPointer = arm::getReadTPMode(TC, Args);
286   const Arg *WaCPU = nullptr, *WaFPU = nullptr;
287   const Arg *WaHDiv = nullptr, *WaArch = nullptr;
288
289   if (!ForAS) {
290     // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
291     // yet (it uses the -mfloat-abi and -msoft-float options), and it is
292     // stripped out by the ARM target. We should probably pass this a new
293     // -target-option, which is handled by the -cc1/-cc1as invocation.
294     //
295     // FIXME2:  For consistency, it would be ideal if we set up the target
296     // machine state the same when using the frontend or the assembler. We don't
297     // currently do that for the assembler, we pass the options directly to the
298     // backend and never even instantiate the frontend TargetInfo. If we did,
299     // and used its handleTargetFeatures hook, then we could ensure the
300     // assembler and the frontend behave the same.
301
302     // Use software floating point operations?
303     if (ABI == arm::FloatABI::Soft)
304       Features.push_back("+soft-float");
305
306     // Use software floating point argument passing?
307     if (ABI != arm::FloatABI::Hard)
308       Features.push_back("+soft-float-abi");
309   } else {
310     // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down
311     // to the assembler correctly.
312     for (const Arg *A :
313          Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
314       StringRef Value = A->getValue();
315       if (Value.startswith("-mfpu=")) {
316         WaFPU = A;
317       } else if (Value.startswith("-mcpu=")) {
318         WaCPU = A;
319       } else if (Value.startswith("-mhwdiv=")) {
320         WaHDiv = A;
321       } else if (Value.startswith("-march=")) {
322         WaArch = A;
323       }
324     }
325   }
326
327   if (ThreadPointer == arm::ReadTPMode::Cp15)
328     Features.push_back("+read-tp-hard");
329
330   // Check -march. ClangAs gives preference to -Wa,-march=.
331   const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
332   StringRef ArchName;
333   if (WaArch) {
334     if (ArchArg)
335       D.Diag(clang::diag::warn_drv_unused_argument)
336           << ArchArg->getAsString(Args);
337     ArchName = StringRef(WaArch->getValue()).substr(7);
338     checkARMArchName(D, WaArch, Args, ArchName, Features, Triple);
339     // FIXME: Set Arch.
340     D.Diag(clang::diag::warn_drv_unused_argument) << WaArch->getAsString(Args);
341   } else if (ArchArg) {
342     ArchName = ArchArg->getValue();
343     checkARMArchName(D, ArchArg, Args, ArchName, Features, Triple);
344   }
345
346   // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=.
347   const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
348   StringRef CPUName;
349   if (WaCPU) {
350     if (CPUArg)
351       D.Diag(clang::diag::warn_drv_unused_argument)
352           << CPUArg->getAsString(Args);
353     CPUName = StringRef(WaCPU->getValue()).substr(6);
354     checkARMCPUName(D, WaCPU, Args, CPUName, ArchName, Features, Triple);
355   } else if (CPUArg) {
356     CPUName = CPUArg->getValue();
357     checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, Features, Triple);
358   }
359
360   // Add CPU features for generic CPUs
361   if (CPUName == "native") {
362     llvm::StringMap<bool> HostFeatures;
363     if (llvm::sys::getHostCPUFeatures(HostFeatures))
364       for (auto &F : HostFeatures)
365         Features.push_back(
366             Args.MakeArgString((F.second ? "+" : "-") + F.first()));
367   } else if (!CPUName.empty()) {
368     DecodeARMFeaturesFromCPU(D, CPUName, Features);
369   }
370
371   // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=.
372   const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
373   if (WaFPU) {
374     if (FPUArg)
375       D.Diag(clang::diag::warn_drv_unused_argument)
376           << FPUArg->getAsString(Args);
377     getARMFPUFeatures(D, WaFPU, Args, StringRef(WaFPU->getValue()).substr(6),
378                       Features);
379   } else if (FPUArg) {
380     getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
381   }
382
383   // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=.
384   const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
385   if (WaHDiv) {
386     if (HDivArg)
387       D.Diag(clang::diag::warn_drv_unused_argument)
388           << HDivArg->getAsString(Args);
389     getARMHWDivFeatures(D, WaHDiv, Args,
390                         StringRef(WaHDiv->getValue()).substr(8), Features);
391   } else if (HDivArg)
392     getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
393
394   // Setting -msoft-float/-mfloat-abi=soft effectively disables the FPU (GCC
395   // ignores the -mfpu options in this case).
396   // Note that the ABI can also be set implicitly by the target selected.
397   if (ABI == arm::FloatABI::Soft) {
398     llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features);
399
400     // Disable hardware FP features which have been enabled.
401     // FIXME: Disabling vfp2 and neon should be enough as all the other
402     //        features are dependent on these 2 features in LLVM. However
403     //        there is currently no easy way to test this in clang, so for
404     //        now just be explicit and disable all known dependent features
405     //        as well.
406     for (std::string Feature : {"vfp2", "vfp3", "vfp4", "fp-armv8", "fullfp16",
407                                 "neon", "crypto", "dotprod"})
408       if (std::find(std::begin(Features), std::end(Features), "+" + Feature) != std::end(Features))
409         Features.push_back(Args.MakeArgString("-" + Feature));
410   }
411
412   // En/disable crc code generation.
413   if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
414     if (A->getOption().matches(options::OPT_mcrc))
415       Features.push_back("+crc");
416     else
417       Features.push_back("-crc");
418   }
419
420   // Look for the last occurrence of -mlong-calls or -mno-long-calls. If
421   // neither options are specified, see if we are compiling for kernel/kext and
422   // decide whether to pass "+long-calls" based on the OS and its version.
423   if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
424                                options::OPT_mno_long_calls)) {
425     if (A->getOption().matches(options::OPT_mlong_calls))
426       Features.push_back("+long-calls");
427   } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) &&
428              !Triple.isWatchOS()) {
429       Features.push_back("+long-calls");
430   }
431
432   // Generate execute-only output (no data access to code sections).
433   // This only makes sense for the compiler, not for the assembler.
434   if (!ForAS) {
435     // Supported only on ARMv6T2 and ARMv7 and above.
436     // Cannot be combined with -mno-movt or -mlong-calls
437     if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) {
438       if (A->getOption().matches(options::OPT_mexecute_only)) {
439         if (getARMSubArchVersionNumber(Triple) < 7 &&
440             llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T2)
441               D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName();
442         else if (Arg *B = Args.getLastArg(options::OPT_mno_movt))
443           D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
444         // Long calls create constant pool entries and have not yet been fixed up
445         // to play nicely with execute-only. Hence, they cannot be used in
446         // execute-only code for now
447         else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) {
448           if (B->getOption().matches(options::OPT_mlong_calls))
449             D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
450         }
451         Features.push_back("+execute-only");
452       }
453     }
454   }
455
456   // Kernel code has more strict alignment requirements.
457   if (KernelOrKext)
458     Features.push_back("+strict-align");
459   else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
460                                     options::OPT_munaligned_access)) {
461     if (A->getOption().matches(options::OPT_munaligned_access)) {
462       // No v6M core supports unaligned memory access (v6M ARM ARM A3.2).
463       if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
464         D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
465       // v8M Baseline follows on from v6M, so doesn't support unaligned memory
466       // access either.
467       else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline)
468         D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base";
469     } else
470       Features.push_back("+strict-align");
471   } else {
472     // Assume pre-ARMv6 doesn't support unaligned accesses.
473     //
474     // ARMv6 may or may not support unaligned accesses depending on the
475     // SCTLR.U bit, which is architecture-specific. We assume ARMv6
476     // Darwin and NetBSD targets support unaligned accesses, and others don't.
477     //
478     // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
479     // which raises an alignment fault on unaligned accesses. Linux
480     // defaults this bit to 0 and handles it as a system-wide (not
481     // per-process) setting. It is therefore safe to assume that ARMv7+
482     // Linux targets support unaligned accesses. The same goes for NaCl.
483     //
484     // The above behavior is consistent with GCC.
485     int VersionNum = getARMSubArchVersionNumber(Triple);
486     if (Triple.isOSDarwin() || Triple.isOSNetBSD()) {
487       if (VersionNum < 6 ||
488           Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
489         Features.push_back("+strict-align");
490     } else if (Triple.isOSLinux() || Triple.isOSNaCl()) {
491       if (VersionNum < 7)
492         Features.push_back("+strict-align");
493     } else
494       Features.push_back("+strict-align");
495   }
496
497   // llvm does not support reserving registers in general. There is support
498   // for reserving r9 on ARM though (defined as a platform-specific register
499   // in ARM EABI).
500   if (Args.hasArg(options::OPT_ffixed_r9))
501     Features.push_back("+reserve-r9");
502
503   // The kext linker doesn't know how to deal with movw/movt.
504   if (KernelOrKext || Args.hasArg(options::OPT_mno_movt))
505     Features.push_back("+no-movt");
506
507   if (Args.hasArg(options::OPT_mno_neg_immediates))
508     Features.push_back("+no-neg-immediates");
509 }
510
511 const std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
512   std::string MArch;
513   if (!Arch.empty())
514     MArch = Arch;
515   else
516     MArch = Triple.getArchName();
517   MArch = StringRef(MArch).split("+").first.lower();
518
519   // Handle -march=native.
520   if (MArch == "native") {
521     std::string CPU = llvm::sys::getHostCPUName();
522     if (CPU != "generic") {
523       // Translate the native cpu into the architecture suffix for that CPU.
524       StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
525       // If there is no valid architecture suffix for this CPU we don't know how
526       // to handle it, so return no architecture.
527       if (Suffix.empty())
528         MArch = "";
529       else
530         MArch = std::string("arm") + Suffix.str();
531     }
532   }
533
534   return MArch;
535 }
536
537 /// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
538 StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
539   std::string MArch = getARMArch(Arch, Triple);
540   // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch
541   // here means an -march=native that we can't handle, so instead return no CPU.
542   if (MArch.empty())
543     return StringRef();
544
545   // We need to return an empty string here on invalid MArch values as the
546   // various places that call this function can't cope with a null result.
547   return Triple.getARMCPUForArch(MArch);
548 }
549
550 /// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
551 std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
552                                  const llvm::Triple &Triple) {
553   // FIXME: Warn on inconsistent use of -mcpu and -march.
554   // If we have -mcpu=, use that.
555   if (!CPU.empty()) {
556     std::string MCPU = StringRef(CPU).split("+").first.lower();
557     // Handle -mcpu=native.
558     if (MCPU == "native")
559       return llvm::sys::getHostCPUName();
560     else
561       return MCPU;
562   }
563
564   return getARMCPUForMArch(Arch, Triple);
565 }
566
567 /// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
568 /// CPU  (or Arch, if CPU is generic).
569 // FIXME: This is redundant with -mcpu, why does LLVM use this.
570 StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch,
571                                        const llvm::Triple &Triple) {
572   llvm::ARM::ArchKind ArchKind;
573   if (CPU == "generic") {
574     std::string ARMArch = tools::arm::getARMArch(Arch, Triple);
575     ArchKind = llvm::ARM::parseArch(ARMArch);
576     if (ArchKind == llvm::ARM::ArchKind::INVALID)
577       // In case of generic Arch, i.e. "arm",
578       // extract arch from default cpu of the Triple
579       ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch));
580   } else {
581     // FIXME: horrible hack to get around the fact that Cortex-A7 is only an
582     // armv7k triple if it's actually been specified via "-arch armv7k".
583     ArchKind = (Arch == "armv7k" || Arch == "thumbv7k")
584                           ? llvm::ARM::ArchKind::ARMV7K
585                           : llvm::ARM::parseCPUArch(CPU);
586   }
587   if (ArchKind == llvm::ARM::ArchKind::INVALID)
588     return "";
589   return llvm::ARM::getSubArch(ArchKind);
590 }
591
592 void arm::appendEBLinkFlags(const ArgList &Args, ArgStringList &CmdArgs,
593                             const llvm::Triple &Triple) {
594   if (Args.hasArg(options::OPT_r))
595     return;
596
597   // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker
598   // to generate BE-8 executables.
599   if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple))
600     CmdArgs.push_back("--be8");
601 }