xref: /freebsd/contrib/llvm-project/llvm/lib/TargetParser/ARMTargetParser.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===-- ARMTargetParser - Parser for ARM target features --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a target parser to recognise ARM hardware features
10 // such as FPU/CPU/ARCH/extensions and specific support such as HWDIV.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/TargetParser/ARMTargetParser.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/TargetParser/ARMTargetParserCommon.h"
19 #include "llvm/TargetParser/Triple.h"
20 #include <cctype>
21 
22 using namespace llvm;
23 
24 static StringRef getHWDivSynonym(StringRef HWDiv) {
25   return StringSwitch<StringRef>(HWDiv)
26       .Case("thumb,arm", "arm,thumb")
27       .Default(HWDiv);
28 }
29 
30 // Allows partial match, ex. "v7a" matches "armv7a".
31 ARM::ArchKind ARM::parseArch(StringRef Arch) {
32   Arch = getCanonicalArchName(Arch);
33   StringRef Syn = getArchSynonym(Arch);
34   for (const auto &A : ARMArchNames) {
35     if (A.Name.ends_with(Syn))
36       return A.ID;
37   }
38   return ArchKind::INVALID;
39 }
40 
41 // Version number (ex. v7 = 7).
42 unsigned ARM::parseArchVersion(StringRef Arch) {
43   Arch = getCanonicalArchName(Arch);
44   switch (parseArch(Arch)) {
45   case ArchKind::ARMV4:
46   case ArchKind::ARMV4T:
47     return 4;
48   case ArchKind::ARMV5T:
49   case ArchKind::ARMV5TE:
50   case ArchKind::IWMMXT:
51   case ArchKind::IWMMXT2:
52   case ArchKind::XSCALE:
53   case ArchKind::ARMV5TEJ:
54     return 5;
55   case ArchKind::ARMV6:
56   case ArchKind::ARMV6K:
57   case ArchKind::ARMV6T2:
58   case ArchKind::ARMV6KZ:
59   case ArchKind::ARMV6M:
60     return 6;
61   case ArchKind::ARMV7A:
62   case ArchKind::ARMV7VE:
63   case ArchKind::ARMV7R:
64   case ArchKind::ARMV7M:
65   case ArchKind::ARMV7S:
66   case ArchKind::ARMV7EM:
67   case ArchKind::ARMV7K:
68     return 7;
69   case ArchKind::ARMV8A:
70   case ArchKind::ARMV8_1A:
71   case ArchKind::ARMV8_2A:
72   case ArchKind::ARMV8_3A:
73   case ArchKind::ARMV8_4A:
74   case ArchKind::ARMV8_5A:
75   case ArchKind::ARMV8_6A:
76   case ArchKind::ARMV8_7A:
77   case ArchKind::ARMV8_8A:
78   case ArchKind::ARMV8_9A:
79   case ArchKind::ARMV8R:
80   case ArchKind::ARMV8MBaseline:
81   case ArchKind::ARMV8MMainline:
82   case ArchKind::ARMV8_1MMainline:
83     return 8;
84   case ArchKind::ARMV9A:
85   case ArchKind::ARMV9_1A:
86   case ArchKind::ARMV9_2A:
87   case ArchKind::ARMV9_3A:
88   case ArchKind::ARMV9_4A:
89     return 9;
90   case ArchKind::INVALID:
91     return 0;
92   }
93   llvm_unreachable("Unhandled architecture");
94 }
95 
96 static ARM::ProfileKind getProfileKind(ARM::ArchKind AK) {
97   switch (AK) {
98   case ARM::ArchKind::ARMV6M:
99   case ARM::ArchKind::ARMV7M:
100   case ARM::ArchKind::ARMV7EM:
101   case ARM::ArchKind::ARMV8MMainline:
102   case ARM::ArchKind::ARMV8MBaseline:
103   case ARM::ArchKind::ARMV8_1MMainline:
104     return ARM::ProfileKind::M;
105   case ARM::ArchKind::ARMV7R:
106   case ARM::ArchKind::ARMV8R:
107     return ARM::ProfileKind::R;
108   case ARM::ArchKind::ARMV7A:
109   case ARM::ArchKind::ARMV7VE:
110   case ARM::ArchKind::ARMV7K:
111   case ARM::ArchKind::ARMV8A:
112   case ARM::ArchKind::ARMV8_1A:
113   case ARM::ArchKind::ARMV8_2A:
114   case ARM::ArchKind::ARMV8_3A:
115   case ARM::ArchKind::ARMV8_4A:
116   case ARM::ArchKind::ARMV8_5A:
117   case ARM::ArchKind::ARMV8_6A:
118   case ARM::ArchKind::ARMV8_7A:
119   case ARM::ArchKind::ARMV8_8A:
120   case ARM::ArchKind::ARMV8_9A:
121   case ARM::ArchKind::ARMV9A:
122   case ARM::ArchKind::ARMV9_1A:
123   case ARM::ArchKind::ARMV9_2A:
124   case ARM::ArchKind::ARMV9_3A:
125   case ARM::ArchKind::ARMV9_4A:
126     return ARM::ProfileKind::A;
127   case ARM::ArchKind::ARMV4:
128   case ARM::ArchKind::ARMV4T:
129   case ARM::ArchKind::ARMV5T:
130   case ARM::ArchKind::ARMV5TE:
131   case ARM::ArchKind::ARMV5TEJ:
132   case ARM::ArchKind::ARMV6:
133   case ARM::ArchKind::ARMV6K:
134   case ARM::ArchKind::ARMV6T2:
135   case ARM::ArchKind::ARMV6KZ:
136   case ARM::ArchKind::ARMV7S:
137   case ARM::ArchKind::IWMMXT:
138   case ARM::ArchKind::IWMMXT2:
139   case ARM::ArchKind::XSCALE:
140   case ARM::ArchKind::INVALID:
141     return ARM::ProfileKind::INVALID;
142   }
143   llvm_unreachable("Unhandled architecture");
144 }
145 
146 // Profile A/R/M
147 ARM::ProfileKind ARM::parseArchProfile(StringRef Arch) {
148   Arch = getCanonicalArchName(Arch);
149   return getProfileKind(parseArch(Arch));
150 }
151 
152 bool ARM::getFPUFeatures(ARM::FPUKind FPUKind,
153                          std::vector<StringRef> &Features) {
154 
155   if (FPUKind >= FK_LAST || FPUKind == FK_INVALID)
156     return false;
157 
158   static const struct FPUFeatureNameInfo {
159     const char *PlusName, *MinusName;
160     FPUVersion MinVersion;
161     FPURestriction MaxRestriction;
162   } FPUFeatureInfoList[] = {
163     // We have to specify the + and - versions of the name in full so
164     // that we can return them as static StringRefs.
165     //
166     // Also, the SubtargetFeatures ending in just "sp" are listed here
167     // under FPURestriction::None, which is the only FPURestriction in
168     // which they would be valid (since FPURestriction::SP doesn't
169     // exist).
170     {"+vfp2", "-vfp2", FPUVersion::VFPV2, FPURestriction::D16},
171     {"+vfp2sp", "-vfp2sp", FPUVersion::VFPV2, FPURestriction::SP_D16},
172     {"+vfp3", "-vfp3", FPUVersion::VFPV3, FPURestriction::None},
173     {"+vfp3d16", "-vfp3d16", FPUVersion::VFPV3, FPURestriction::D16},
174     {"+vfp3d16sp", "-vfp3d16sp", FPUVersion::VFPV3, FPURestriction::SP_D16},
175     {"+vfp3sp", "-vfp3sp", FPUVersion::VFPV3, FPURestriction::None},
176     {"+fp16", "-fp16", FPUVersion::VFPV3_FP16, FPURestriction::SP_D16},
177     {"+vfp4", "-vfp4", FPUVersion::VFPV4, FPURestriction::None},
178     {"+vfp4d16", "-vfp4d16", FPUVersion::VFPV4, FPURestriction::D16},
179     {"+vfp4d16sp", "-vfp4d16sp", FPUVersion::VFPV4, FPURestriction::SP_D16},
180     {"+vfp4sp", "-vfp4sp", FPUVersion::VFPV4, FPURestriction::None},
181     {"+fp-armv8", "-fp-armv8", FPUVersion::VFPV5, FPURestriction::None},
182     {"+fp-armv8d16", "-fp-armv8d16", FPUVersion::VFPV5, FPURestriction::D16},
183     {"+fp-armv8d16sp", "-fp-armv8d16sp", FPUVersion::VFPV5, FPURestriction::SP_D16},
184     {"+fp-armv8sp", "-fp-armv8sp", FPUVersion::VFPV5, FPURestriction::None},
185     {"+fullfp16", "-fullfp16", FPUVersion::VFPV5_FULLFP16, FPURestriction::SP_D16},
186     {"+fp64", "-fp64", FPUVersion::VFPV2, FPURestriction::D16},
187     {"+d32", "-d32", FPUVersion::VFPV3, FPURestriction::None},
188   };
189 
190   for (const auto &Info: FPUFeatureInfoList) {
191     if (FPUNames[FPUKind].FPUVer >= Info.MinVersion &&
192         FPUNames[FPUKind].Restriction <= Info.MaxRestriction)
193       Features.push_back(Info.PlusName);
194     else
195       Features.push_back(Info.MinusName);
196   }
197 
198   static const struct NeonFeatureNameInfo {
199     const char *PlusName, *MinusName;
200     NeonSupportLevel MinSupportLevel;
201   } NeonFeatureInfoList[] = {
202       {"+neon", "-neon", NeonSupportLevel::Neon},
203       {"+sha2", "-sha2", NeonSupportLevel::Crypto},
204       {"+aes", "-aes", NeonSupportLevel::Crypto},
205   };
206 
207   for (const auto &Info: NeonFeatureInfoList) {
208     if (FPUNames[FPUKind].NeonSupport >= Info.MinSupportLevel)
209       Features.push_back(Info.PlusName);
210     else
211       Features.push_back(Info.MinusName);
212   }
213 
214   return true;
215 }
216 
217 ARM::FPUKind ARM::parseFPU(StringRef FPU) {
218   StringRef Syn = getFPUSynonym(FPU);
219   for (const auto &F : FPUNames) {
220     if (Syn == F.Name)
221       return F.ID;
222   }
223   return FK_INVALID;
224 }
225 
226 ARM::NeonSupportLevel ARM::getFPUNeonSupportLevel(ARM::FPUKind FPUKind) {
227   if (FPUKind >= FK_LAST)
228     return NeonSupportLevel::None;
229   return FPUNames[FPUKind].NeonSupport;
230 }
231 
232 StringRef ARM::getFPUSynonym(StringRef FPU) {
233   return StringSwitch<StringRef>(FPU)
234       .Cases("fpa", "fpe2", "fpe3", "maverick", "invalid") // Unsupported
235       .Case("vfp2", "vfpv2")
236       .Case("vfp3", "vfpv3")
237       .Case("vfp4", "vfpv4")
238       .Case("vfp3-d16", "vfpv3-d16")
239       .Case("vfp4-d16", "vfpv4-d16")
240       .Cases("fp4-sp-d16", "vfpv4-sp-d16", "fpv4-sp-d16")
241       .Cases("fp4-dp-d16", "fpv4-dp-d16", "vfpv4-d16")
242       .Case("fp5-sp-d16", "fpv5-sp-d16")
243       .Cases("fp5-dp-d16", "fpv5-dp-d16", "fpv5-d16")
244       // FIXME: Clang uses it, but it's bogus, since neon defaults to vfpv3.
245       .Case("neon-vfpv3", "neon")
246       .Default(FPU);
247 }
248 
249 StringRef ARM::getFPUName(ARM::FPUKind FPUKind) {
250   if (FPUKind >= FK_LAST)
251     return StringRef();
252   return FPUNames[FPUKind].Name;
253 }
254 
255 ARM::FPUVersion ARM::getFPUVersion(ARM::FPUKind FPUKind) {
256   if (FPUKind >= FK_LAST)
257     return FPUVersion::NONE;
258   return FPUNames[FPUKind].FPUVer;
259 }
260 
261 ARM::FPURestriction ARM::getFPURestriction(ARM::FPUKind FPUKind) {
262   if (FPUKind >= FK_LAST)
263     return FPURestriction::None;
264   return FPUNames[FPUKind].Restriction;
265 }
266 
267 ARM::FPUKind ARM::getDefaultFPU(StringRef CPU, ARM::ArchKind AK) {
268   if (CPU == "generic")
269     return ARM::ARMArchNames[static_cast<unsigned>(AK)].DefaultFPU;
270 
271   return StringSwitch<ARM::FPUKind>(CPU)
272 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT)           \
273   .Case(NAME, DEFAULT_FPU)
274 #include "llvm/TargetParser/ARMTargetParser.def"
275       .Default(ARM::FK_INVALID);
276 }
277 
278 uint64_t ARM::getDefaultExtensions(StringRef CPU, ARM::ArchKind AK) {
279   if (CPU == "generic")
280     return ARM::ARMArchNames[static_cast<unsigned>(AK)].ArchBaseExtensions;
281 
282   return StringSwitch<uint64_t>(CPU)
283 #define ARM_CPU_NAME(NAME, ID, DEFAULT_FPU, IS_DEFAULT, DEFAULT_EXT)           \
284   .Case(NAME,                                                                  \
285         ARMArchNames[static_cast<unsigned>(ArchKind::ID)].ArchBaseExtensions | \
286             DEFAULT_EXT)
287 #include "llvm/TargetParser/ARMTargetParser.def"
288   .Default(ARM::AEK_INVALID);
289 }
290 
291 bool ARM::getHWDivFeatures(uint64_t HWDivKind,
292                            std::vector<StringRef> &Features) {
293 
294   if (HWDivKind == AEK_INVALID)
295     return false;
296 
297   if (HWDivKind & AEK_HWDIVARM)
298     Features.push_back("+hwdiv-arm");
299   else
300     Features.push_back("-hwdiv-arm");
301 
302   if (HWDivKind & AEK_HWDIVTHUMB)
303     Features.push_back("+hwdiv");
304   else
305     Features.push_back("-hwdiv");
306 
307   return true;
308 }
309 
310 bool ARM::getExtensionFeatures(uint64_t Extensions,
311                                std::vector<StringRef> &Features) {
312 
313   if (Extensions == AEK_INVALID)
314     return false;
315 
316   for (const auto &AE : ARCHExtNames) {
317     if ((Extensions & AE.ID) == AE.ID && !AE.Feature.empty())
318       Features.push_back(AE.Feature);
319     else if (!AE.NegFeature.empty())
320       Features.push_back(AE.NegFeature);
321   }
322 
323   return getHWDivFeatures(Extensions, Features);
324 }
325 
326 StringRef ARM::getArchName(ARM::ArchKind AK) {
327   return ARMArchNames[static_cast<unsigned>(AK)].Name;
328 }
329 
330 StringRef ARM::getCPUAttr(ARM::ArchKind AK) {
331   return ARMArchNames[static_cast<unsigned>(AK)].CPUAttr;
332 }
333 
334 StringRef ARM::getSubArch(ARM::ArchKind AK) {
335   return ARMArchNames[static_cast<unsigned>(AK)].getSubArch();
336 }
337 
338 unsigned ARM::getArchAttr(ARM::ArchKind AK) {
339   return ARMArchNames[static_cast<unsigned>(AK)].ArchAttr;
340 }
341 
342 StringRef ARM::getArchExtName(uint64_t ArchExtKind) {
343   for (const auto &AE : ARCHExtNames) {
344     if (ArchExtKind == AE.ID)
345       return AE.Name;
346   }
347   return StringRef();
348 }
349 
350 static bool stripNegationPrefix(StringRef &Name) {
351   return Name.consume_front("no");
352 }
353 
354 StringRef ARM::getArchExtFeature(StringRef ArchExt) {
355   bool Negated = stripNegationPrefix(ArchExt);
356   for (const auto &AE : ARCHExtNames) {
357     if (!AE.Feature.empty() && ArchExt == AE.Name)
358       return StringRef(Negated ? AE.NegFeature : AE.Feature);
359   }
360 
361   return StringRef();
362 }
363 
364 static ARM::FPUKind findDoublePrecisionFPU(ARM::FPUKind InputFPUKind) {
365   if (InputFPUKind == ARM::FK_INVALID || InputFPUKind == ARM::FK_NONE)
366     return ARM::FK_INVALID;
367 
368   const ARM::FPUName &InputFPU = ARM::FPUNames[InputFPUKind];
369 
370   // If the input FPU already supports double-precision, then there
371   // isn't any different FPU we can return here.
372   if (ARM::isDoublePrecision(InputFPU.Restriction))
373     return InputFPUKind;
374 
375   // Otherwise, look for an FPU entry with all the same fields, except
376   // that it supports double precision.
377   for (const ARM::FPUName &CandidateFPU : ARM::FPUNames) {
378     if (CandidateFPU.FPUVer == InputFPU.FPUVer &&
379         CandidateFPU.NeonSupport == InputFPU.NeonSupport &&
380         ARM::has32Regs(CandidateFPU.Restriction) ==
381             ARM::has32Regs(InputFPU.Restriction) &&
382         ARM::isDoublePrecision(CandidateFPU.Restriction)) {
383       return CandidateFPU.ID;
384     }
385   }
386 
387   // nothing found
388   return ARM::FK_INVALID;
389 }
390 
391 static ARM::FPUKind findSinglePrecisionFPU(ARM::FPUKind InputFPUKind) {
392   if (InputFPUKind == ARM::FK_INVALID || InputFPUKind == ARM::FK_NONE)
393     return ARM::FK_INVALID;
394 
395   const ARM::FPUName &InputFPU = ARM::FPUNames[InputFPUKind];
396 
397   // If the input FPU already is single-precision only, then there
398   // isn't any different FPU we can return here.
399   if (!ARM::isDoublePrecision(InputFPU.Restriction))
400     return InputFPUKind;
401 
402   // Otherwise, look for an FPU entry with all the same fields, except
403   // that it does not support double precision.
404   for (const ARM::FPUName &CandidateFPU : ARM::FPUNames) {
405     if (CandidateFPU.FPUVer == InputFPU.FPUVer &&
406         CandidateFPU.NeonSupport == InputFPU.NeonSupport &&
407         ARM::has32Regs(CandidateFPU.Restriction) ==
408             ARM::has32Regs(InputFPU.Restriction) &&
409         !ARM::isDoublePrecision(CandidateFPU.Restriction)) {
410       return CandidateFPU.ID;
411     }
412   }
413 
414   // nothing found
415   return ARM::FK_INVALID;
416 }
417 
418 bool ARM::appendArchExtFeatures(StringRef CPU, ARM::ArchKind AK,
419                                 StringRef ArchExt,
420                                 std::vector<StringRef> &Features,
421                                 ARM::FPUKind &ArgFPUKind) {
422 
423   size_t StartingNumFeatures = Features.size();
424   const bool Negated = stripNegationPrefix(ArchExt);
425   uint64_t ID = parseArchExt(ArchExt);
426 
427   if (ID == AEK_INVALID)
428     return false;
429 
430   for (const auto &AE : ARCHExtNames) {
431     if (Negated) {
432       if ((AE.ID & ID) == ID && !AE.NegFeature.empty())
433         Features.push_back(AE.NegFeature);
434     } else {
435       if ((AE.ID & ID) == AE.ID && !AE.Feature.empty())
436         Features.push_back(AE.Feature);
437     }
438   }
439 
440   if (CPU == "")
441     CPU = "generic";
442 
443   if (ArchExt == "fp" || ArchExt == "fp.dp") {
444     const ARM::FPUKind DefaultFPU = getDefaultFPU(CPU, AK);
445     ARM::FPUKind FPUKind;
446     if (ArchExt == "fp.dp") {
447       const bool IsDP = ArgFPUKind != ARM::FK_INVALID &&
448                         ArgFPUKind != ARM::FK_NONE &&
449                         isDoublePrecision(getFPURestriction(ArgFPUKind));
450       if (Negated) {
451         /* If there is no FPU selected yet, we still need to set ArgFPUKind, as
452          * leaving it as FK_INVALID, would cause default FPU to be selected
453          * later and that could be double precision one. */
454         if (ArgFPUKind != ARM::FK_INVALID && !IsDP)
455           return true;
456         FPUKind = findSinglePrecisionFPU(DefaultFPU);
457         if (FPUKind == ARM::FK_INVALID)
458           FPUKind = ARM::FK_NONE;
459       } else {
460         if (IsDP)
461           return true;
462         FPUKind = findDoublePrecisionFPU(DefaultFPU);
463         if (FPUKind == ARM::FK_INVALID)
464           return false;
465       }
466     } else if (Negated) {
467       FPUKind = ARM::FK_NONE;
468     } else {
469       FPUKind = DefaultFPU;
470     }
471     ArgFPUKind = FPUKind;
472     return true;
473   }
474   return StartingNumFeatures != Features.size();
475 }
476 
477 ARM::ArchKind ARM::convertV9toV8(ARM::ArchKind AK) {
478   if (getProfileKind(AK) != ProfileKind::A)
479     return ARM::ArchKind::INVALID;
480   if (AK < ARM::ArchKind::ARMV9A || AK > ARM::ArchKind::ARMV9_3A)
481     return ARM::ArchKind::INVALID;
482   unsigned AK_v8 = static_cast<unsigned>(ARM::ArchKind::ARMV8_5A);
483   AK_v8 += static_cast<unsigned>(AK) -
484            static_cast<unsigned>(ARM::ArchKind::ARMV9A);
485   return static_cast<ARM::ArchKind>(AK_v8);
486 }
487 
488 StringRef ARM::getDefaultCPU(StringRef Arch) {
489   ArchKind AK = parseArch(Arch);
490   if (AK == ArchKind::INVALID)
491     return StringRef();
492 
493   // Look for multiple AKs to find the default for pair AK+Name.
494   for (const auto &CPU : CPUNames) {
495     if (CPU.ArchID == AK && CPU.Default)
496       return CPU.Name;
497   }
498 
499   // If we can't find a default then target the architecture instead
500   return "generic";
501 }
502 
503 uint64_t ARM::parseHWDiv(StringRef HWDiv) {
504   StringRef Syn = getHWDivSynonym(HWDiv);
505   for (const auto &D : HWDivNames) {
506     if (Syn == D.Name)
507       return D.ID;
508   }
509   return AEK_INVALID;
510 }
511 
512 uint64_t ARM::parseArchExt(StringRef ArchExt) {
513   for (const auto &A : ARCHExtNames) {
514     if (ArchExt == A.Name)
515       return A.ID;
516   }
517   return AEK_INVALID;
518 }
519 
520 ARM::ArchKind ARM::parseCPUArch(StringRef CPU) {
521   for (const auto &C : CPUNames) {
522     if (CPU == C.Name)
523       return C.ArchID;
524   }
525   return ArchKind::INVALID;
526 }
527 
528 void ARM::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values) {
529   for (const auto &Arch : CPUNames) {
530     if (Arch.ArchID != ArchKind::INVALID)
531       Values.push_back(Arch.Name);
532   }
533 }
534 
535 StringRef ARM::computeDefaultTargetABI(const Triple &TT, StringRef CPU) {
536   StringRef ArchName =
537       CPU.empty() ? TT.getArchName() : getArchName(parseCPUArch(CPU));
538 
539   if (TT.isOSBinFormatMachO()) {
540     if (TT.getEnvironment() == Triple::EABI ||
541         TT.getOS() == Triple::UnknownOS ||
542         parseArchProfile(ArchName) == ProfileKind::M)
543       return "aapcs";
544     if (TT.isWatchABI())
545       return "aapcs16";
546     return "apcs-gnu";
547   } else if (TT.isOSWindows())
548     // FIXME: this is invalid for WindowsCE.
549     return "aapcs";
550 
551   // Select the default based on the platform.
552   switch (TT.getEnvironment()) {
553   case Triple::Android:
554   case Triple::GNUEABI:
555   case Triple::GNUEABIHF:
556   case Triple::MuslEABI:
557   case Triple::MuslEABIHF:
558   case Triple::OpenHOS:
559     return "aapcs-linux";
560   case Triple::EABIHF:
561   case Triple::EABI:
562     return "aapcs";
563   default:
564     if (TT.isOSNetBSD())
565       return "apcs-gnu";
566     if (TT.isOSFreeBSD() || TT.isOSOpenBSD() || TT.isOSHaiku() ||
567         TT.isOHOSFamily())
568       return "aapcs-linux";
569     return "aapcs";
570   }
571 }
572 
573 StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, StringRef MArch) {
574   if (MArch.empty())
575     MArch = Triple.getArchName();
576   MArch = llvm::ARM::getCanonicalArchName(MArch);
577 
578   // Some defaults are forced.
579   switch (Triple.getOS()) {
580   case llvm::Triple::FreeBSD:
581   case llvm::Triple::NetBSD:
582   case llvm::Triple::OpenBSD:
583   case llvm::Triple::Haiku:
584     if (!MArch.empty() && MArch == "v6")
585       return "arm1176jzf-s";
586     if (!MArch.empty() && MArch == "v7")
587       return "cortex-a8";
588     break;
589   case llvm::Triple::Win32:
590     // FIXME: this is invalid for WindowsCE
591     if (llvm::ARM::parseArchVersion(MArch) <= 7)
592       return "cortex-a9";
593     break;
594   case llvm::Triple::IOS:
595   case llvm::Triple::MacOSX:
596   case llvm::Triple::TvOS:
597   case llvm::Triple::WatchOS:
598   case llvm::Triple::DriverKit:
599     if (MArch == "v7k")
600       return "cortex-a7";
601     break;
602   default:
603     break;
604   }
605 
606   if (MArch.empty())
607     return StringRef();
608 
609   StringRef CPU = llvm::ARM::getDefaultCPU(MArch);
610   if (!CPU.empty() && !CPU.equals("invalid"))
611     return CPU;
612 
613   // If no specific architecture version is requested, return the minimum CPU
614   // required by the OS and environment.
615   switch (Triple.getOS()) {
616   case llvm::Triple::Haiku:
617     return "arm1176jzf-s";
618   case llvm::Triple::NetBSD:
619     switch (Triple.getEnvironment()) {
620     case llvm::Triple::EABI:
621     case llvm::Triple::EABIHF:
622     case llvm::Triple::GNUEABI:
623     case llvm::Triple::GNUEABIHF:
624       return "arm926ej-s";
625     default:
626       return "strongarm";
627     }
628   case llvm::Triple::NaCl:
629   case llvm::Triple::OpenBSD:
630     return "cortex-a8";
631   default:
632     switch (Triple.getEnvironment()) {
633     case llvm::Triple::EABIHF:
634     case llvm::Triple::GNUEABIHF:
635     case llvm::Triple::MuslEABIHF:
636       return "arm1176jzf-s";
637     default:
638       return "arm7tdmi";
639     }
640   }
641 
642   llvm_unreachable("invalid arch name");
643 }
644 
645 void ARM::PrintSupportedExtensions(StringMap<StringRef> DescMap) {
646   outs() << "All available -march extensions for ARM\n\n"
647          << "    " << left_justify("Name", 20)
648          << (DescMap.empty() ? "\n" : "Description\n");
649   for (const auto &Ext : ARCHExtNames) {
650     // Extensions without a feature cannot be used with -march.
651     if (!Ext.Feature.empty()) {
652       std::string Description = DescMap[Ext.Name].str();
653       outs() << "    "
654              << format(Description.empty() ? "%s\n" : "%-20s%s\n",
655                        Ext.Name.str().c_str(), Description.c_str());
656     }
657   }
658 }
659