1 //===--- TargetInfo.cpp - Information about Target machine ----------------===//
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 the TargetInfo and TargetInfoImpl interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/TargetInfo.h"
14 #include "clang/Basic/AddressSpaces.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/TargetParser.h"
22 #include <cstdlib>
23 using namespace clang;
24 
25 static const LangASMap DefaultAddrSpaceMap = {0};
26 
27 // TargetInfo Constructor.
28 TargetInfo::TargetInfo(const llvm::Triple &T) : TargetOpts(), Triple(T) {
29   // Set defaults.  Defaults are set for a 32-bit RISC platform, like PPC or
30   // SPARC.  These should be overridden by concrete targets as needed.
31   BigEndian = !T.isLittleEndian();
32   TLSSupported = true;
33   VLASupported = true;
34   NoAsmVariants = false;
35   HasLegalHalfType = false;
36   HasFloat128 = false;
37   HasFloat16 = false;
38   HasBFloat16 = false;
39   HasStrictFP = false;
40   PointerWidth = PointerAlign = 32;
41   BoolWidth = BoolAlign = 8;
42   IntWidth = IntAlign = 32;
43   LongWidth = LongAlign = 32;
44   LongLongWidth = LongLongAlign = 64;
45 
46   // Fixed point default bit widths
47   ShortAccumWidth = ShortAccumAlign = 16;
48   AccumWidth = AccumAlign = 32;
49   LongAccumWidth = LongAccumAlign = 64;
50   ShortFractWidth = ShortFractAlign = 8;
51   FractWidth = FractAlign = 16;
52   LongFractWidth = LongFractAlign = 32;
53 
54   // Fixed point default integral and fractional bit sizes
55   // We give the _Accum 1 fewer fractional bits than their corresponding _Fract
56   // types by default to have the same number of fractional bits between _Accum
57   // and _Fract types.
58   PaddingOnUnsignedFixedPoint = false;
59   ShortAccumScale = 7;
60   AccumScale = 15;
61   LongAccumScale = 31;
62 
63   SuitableAlign = 64;
64   DefaultAlignForAttributeAligned = 128;
65   MinGlobalAlign = 0;
66   // From the glibc documentation, on GNU systems, malloc guarantees 16-byte
67   // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See
68   // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html.
69   // This alignment guarantee also applies to Windows and Android. On Darwin,
70   // the alignment is 16 bytes on both 64-bit and 32-bit systems.
71   if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid())
72     NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0;
73   else if (T.isOSDarwin())
74     NewAlign = 128;
75   else
76     NewAlign = 0; // Infer from basic type alignment.
77   HalfWidth = 16;
78   HalfAlign = 16;
79   FloatWidth = 32;
80   FloatAlign = 32;
81   DoubleWidth = 64;
82   DoubleAlign = 64;
83   LongDoubleWidth = 64;
84   LongDoubleAlign = 64;
85   Float128Align = 128;
86   LargeArrayMinWidth = 0;
87   LargeArrayAlign = 0;
88   MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0;
89   MaxVectorAlign = 0;
90   MaxTLSAlign = 0;
91   SimdDefaultAlign = 0;
92   SizeType = UnsignedLong;
93   PtrDiffType = SignedLong;
94   IntMaxType = SignedLongLong;
95   IntPtrType = SignedLong;
96   WCharType = SignedInt;
97   WIntType = SignedInt;
98   Char16Type = UnsignedShort;
99   Char32Type = UnsignedInt;
100   Int64Type = SignedLongLong;
101   Int16Type = SignedShort;
102   SigAtomicType = SignedInt;
103   ProcessIDType = SignedInt;
104   UseSignedCharForObjCBool = true;
105   UseBitFieldTypeAlignment = true;
106   UseZeroLengthBitfieldAlignment = false;
107   UseLeadingZeroLengthBitfield = true;
108   UseExplicitBitFieldAlignment = true;
109   ZeroLengthBitfieldBoundary = 0;
110   MaxAlignedAttribute = 0;
111   HalfFormat = &llvm::APFloat::IEEEhalf();
112   FloatFormat = &llvm::APFloat::IEEEsingle();
113   DoubleFormat = &llvm::APFloat::IEEEdouble();
114   LongDoubleFormat = &llvm::APFloat::IEEEdouble();
115   Float128Format = &llvm::APFloat::IEEEquad();
116   MCountName = "mcount";
117   UserLabelPrefix = "_";
118   RegParmMax = 0;
119   SSERegParmMax = 0;
120   HasAlignMac68kSupport = false;
121   HasBuiltinMSVaList = false;
122   IsRenderScriptTarget = false;
123   HasAArch64SVETypes = false;
124   HasRISCVVTypes = false;
125   AllowAMDGPUUnsafeFPAtomics = false;
126   ARMCDECoprocMask = 0;
127 
128   // Default to no types using fpret.
129   RealTypeUsesObjCFPRet = 0;
130 
131   // Default to not using fp2ret for __Complex long double
132   ComplexLongDoubleUsesFP2Ret = false;
133 
134   // Set the C++ ABI based on the triple.
135   TheCXXABI.set(Triple.isKnownWindowsMSVCEnvironment()
136                     ? TargetCXXABI::Microsoft
137                     : TargetCXXABI::GenericItanium);
138 
139   // Default to an empty address space map.
140   AddrSpaceMap = &DefaultAddrSpaceMap;
141   UseAddrSpaceMapMangling = false;
142 
143   // Default to an unknown platform name.
144   PlatformName = "unknown";
145   PlatformMinVersion = VersionTuple();
146 
147   MaxOpenCLWorkGroupSize = 1024;
148 }
149 
150 // Out of line virtual dtor for TargetInfo.
151 TargetInfo::~TargetInfo() {}
152 
153 void TargetInfo::resetDataLayout(StringRef DL, const char *ULP) {
154   DataLayoutString = DL.str();
155   UserLabelPrefix = ULP;
156 }
157 
158 bool
159 TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
160   Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch";
161   return false;
162 }
163 
164 bool
165 TargetInfo::checkCFProtectionReturnSupported(DiagnosticsEngine &Diags) const {
166   Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=return";
167   return false;
168 }
169 
170 /// getTypeName - Return the user string for the specified integer type enum.
171 /// For example, SignedShort -> "short".
172 const char *TargetInfo::getTypeName(IntType T) {
173   switch (T) {
174   default: llvm_unreachable("not an integer!");
175   case SignedChar:       return "signed char";
176   case UnsignedChar:     return "unsigned char";
177   case SignedShort:      return "short";
178   case UnsignedShort:    return "unsigned short";
179   case SignedInt:        return "int";
180   case UnsignedInt:      return "unsigned int";
181   case SignedLong:       return "long int";
182   case UnsignedLong:     return "long unsigned int";
183   case SignedLongLong:   return "long long int";
184   case UnsignedLongLong: return "long long unsigned int";
185   }
186 }
187 
188 /// getTypeConstantSuffix - Return the constant suffix for the specified
189 /// integer type enum. For example, SignedLong -> "L".
190 const char *TargetInfo::getTypeConstantSuffix(IntType T) const {
191   switch (T) {
192   default: llvm_unreachable("not an integer!");
193   case SignedChar:
194   case SignedShort:
195   case SignedInt:        return "";
196   case SignedLong:       return "L";
197   case SignedLongLong:   return "LL";
198   case UnsignedChar:
199     if (getCharWidth() < getIntWidth())
200       return "";
201     LLVM_FALLTHROUGH;
202   case UnsignedShort:
203     if (getShortWidth() < getIntWidth())
204       return "";
205     LLVM_FALLTHROUGH;
206   case UnsignedInt:      return "U";
207   case UnsignedLong:     return "UL";
208   case UnsignedLongLong: return "ULL";
209   }
210 }
211 
212 /// getTypeFormatModifier - Return the printf format modifier for the
213 /// specified integer type enum. For example, SignedLong -> "l".
214 
215 const char *TargetInfo::getTypeFormatModifier(IntType T) {
216   switch (T) {
217   default: llvm_unreachable("not an integer!");
218   case SignedChar:
219   case UnsignedChar:     return "hh";
220   case SignedShort:
221   case UnsignedShort:    return "h";
222   case SignedInt:
223   case UnsignedInt:      return "";
224   case SignedLong:
225   case UnsignedLong:     return "l";
226   case SignedLongLong:
227   case UnsignedLongLong: return "ll";
228   }
229 }
230 
231 /// getTypeWidth - Return the width (in bits) of the specified integer type
232 /// enum. For example, SignedInt -> getIntWidth().
233 unsigned TargetInfo::getTypeWidth(IntType T) const {
234   switch (T) {
235   default: llvm_unreachable("not an integer!");
236   case SignedChar:
237   case UnsignedChar:     return getCharWidth();
238   case SignedShort:
239   case UnsignedShort:    return getShortWidth();
240   case SignedInt:
241   case UnsignedInt:      return getIntWidth();
242   case SignedLong:
243   case UnsignedLong:     return getLongWidth();
244   case SignedLongLong:
245   case UnsignedLongLong: return getLongLongWidth();
246   };
247 }
248 
249 TargetInfo::IntType TargetInfo::getIntTypeByWidth(
250     unsigned BitWidth, bool IsSigned) const {
251   if (getCharWidth() == BitWidth)
252     return IsSigned ? SignedChar : UnsignedChar;
253   if (getShortWidth() == BitWidth)
254     return IsSigned ? SignedShort : UnsignedShort;
255   if (getIntWidth() == BitWidth)
256     return IsSigned ? SignedInt : UnsignedInt;
257   if (getLongWidth() == BitWidth)
258     return IsSigned ? SignedLong : UnsignedLong;
259   if (getLongLongWidth() == BitWidth)
260     return IsSigned ? SignedLongLong : UnsignedLongLong;
261   return NoInt;
262 }
263 
264 TargetInfo::IntType TargetInfo::getLeastIntTypeByWidth(unsigned BitWidth,
265                                                        bool IsSigned) const {
266   if (getCharWidth() >= BitWidth)
267     return IsSigned ? SignedChar : UnsignedChar;
268   if (getShortWidth() >= BitWidth)
269     return IsSigned ? SignedShort : UnsignedShort;
270   if (getIntWidth() >= BitWidth)
271     return IsSigned ? SignedInt : UnsignedInt;
272   if (getLongWidth() >= BitWidth)
273     return IsSigned ? SignedLong : UnsignedLong;
274   if (getLongLongWidth() >= BitWidth)
275     return IsSigned ? SignedLongLong : UnsignedLongLong;
276   return NoInt;
277 }
278 
279 TargetInfo::RealType TargetInfo::getRealTypeByWidth(unsigned BitWidth,
280                                                     bool ExplicitIEEE) const {
281   if (getFloatWidth() == BitWidth)
282     return Float;
283   if (getDoubleWidth() == BitWidth)
284     return Double;
285 
286   switch (BitWidth) {
287   case 96:
288     if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
289       return LongDouble;
290     break;
291   case 128:
292     // The caller explicitly asked for an IEEE compliant type but we still
293     // have to check if the target supports it.
294     if (ExplicitIEEE)
295       return hasFloat128Type() ? Float128 : NoFloat;
296     if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
297         &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
298       return LongDouble;
299     if (hasFloat128Type())
300       return Float128;
301     break;
302   }
303 
304   return NoFloat;
305 }
306 
307 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
308 /// enum. For example, SignedInt -> getIntAlign().
309 unsigned TargetInfo::getTypeAlign(IntType T) const {
310   switch (T) {
311   default: llvm_unreachable("not an integer!");
312   case SignedChar:
313   case UnsignedChar:     return getCharAlign();
314   case SignedShort:
315   case UnsignedShort:    return getShortAlign();
316   case SignedInt:
317   case UnsignedInt:      return getIntAlign();
318   case SignedLong:
319   case UnsignedLong:     return getLongAlign();
320   case SignedLongLong:
321   case UnsignedLongLong: return getLongLongAlign();
322   };
323 }
324 
325 /// isTypeSigned - Return whether an integer types is signed. Returns true if
326 /// the type is signed; false otherwise.
327 bool TargetInfo::isTypeSigned(IntType T) {
328   switch (T) {
329   default: llvm_unreachable("not an integer!");
330   case SignedChar:
331   case SignedShort:
332   case SignedInt:
333   case SignedLong:
334   case SignedLongLong:
335     return true;
336   case UnsignedChar:
337   case UnsignedShort:
338   case UnsignedInt:
339   case UnsignedLong:
340   case UnsignedLongLong:
341     return false;
342   };
343 }
344 
345 /// adjust - Set forced language options.
346 /// Apply changes to the target information with respect to certain
347 /// language options which change the target configuration and adjust
348 /// the language based on the target options where applicable.
349 void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
350   if (Opts.NoBitFieldTypeAlign)
351     UseBitFieldTypeAlignment = false;
352 
353   switch (Opts.WCharSize) {
354   default: llvm_unreachable("invalid wchar_t width");
355   case 0: break;
356   case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
357   case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
358   case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
359   }
360 
361   if (Opts.AlignDouble) {
362     DoubleAlign = LongLongAlign = 64;
363     LongDoubleAlign = 64;
364   }
365 
366   if (Opts.OpenCL) {
367     // OpenCL C requires specific widths for types, irrespective of
368     // what these normally are for the target.
369     // We also define long long and long double here, although the
370     // OpenCL standard only mentions these as "reserved".
371     IntWidth = IntAlign = 32;
372     LongWidth = LongAlign = 64;
373     LongLongWidth = LongLongAlign = 128;
374     HalfWidth = HalfAlign = 16;
375     FloatWidth = FloatAlign = 32;
376 
377     // Embedded 32-bit targets (OpenCL EP) might have double C type
378     // defined as float. Let's not override this as it might lead
379     // to generating illegal code that uses 64bit doubles.
380     if (DoubleWidth != FloatWidth) {
381       DoubleWidth = DoubleAlign = 64;
382       DoubleFormat = &llvm::APFloat::IEEEdouble();
383     }
384     LongDoubleWidth = LongDoubleAlign = 128;
385 
386     unsigned MaxPointerWidth = getMaxPointerWidth();
387     assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
388     bool Is32BitArch = MaxPointerWidth == 32;
389     SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
390     PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
391     IntPtrType = Is32BitArch ? SignedInt : SignedLong;
392 
393     IntMaxType = SignedLongLong;
394     Int64Type = SignedLong;
395 
396     HalfFormat = &llvm::APFloat::IEEEhalf();
397     FloatFormat = &llvm::APFloat::IEEEsingle();
398     LongDoubleFormat = &llvm::APFloat::IEEEquad();
399 
400     // OpenCL C v3.0 s6.7.5 - The generic address space requires support for
401     // OpenCL C 2.0 or OpenCL C 3.0 with the __opencl_c_generic_address_space
402     // feature
403     // OpenCL C v3.0 s6.2.1 - OpenCL pipes require support of OpenCL C 2.0
404     // or later and __opencl_c_pipes feature
405     // FIXME: These language options are also defined in setLangDefaults()
406     // for OpenCL C 2.0 but with no access to target capabilities. Target
407     // should be immutable once created and thus these language options need
408     // to be defined only once.
409     if (Opts.getOpenCLCompatibleVersion() == 300) {
410       const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts();
411       Opts.OpenCLGenericAddressSpace = hasFeatureEnabled(
412           OpenCLFeaturesMap, "__opencl_c_generic_address_space");
413       if (Opts.OpenCLVersion == 300)
414         Opts.OpenCLPipes =
415             hasFeatureEnabled(OpenCLFeaturesMap, "__opencl_c_pipes");
416     }
417   }
418 
419   if (Opts.DoubleSize) {
420     if (Opts.DoubleSize == 32) {
421       DoubleWidth = 32;
422       LongDoubleWidth = 32;
423       DoubleFormat = &llvm::APFloat::IEEEsingle();
424       LongDoubleFormat = &llvm::APFloat::IEEEsingle();
425     } else if (Opts.DoubleSize == 64) {
426       DoubleWidth = 64;
427       LongDoubleWidth = 64;
428       DoubleFormat = &llvm::APFloat::IEEEdouble();
429       LongDoubleFormat = &llvm::APFloat::IEEEdouble();
430     }
431   }
432 
433   if (Opts.LongDoubleSize) {
434     if (Opts.LongDoubleSize == DoubleWidth) {
435       LongDoubleWidth = DoubleWidth;
436       LongDoubleAlign = DoubleAlign;
437       LongDoubleFormat = DoubleFormat;
438     } else if (Opts.LongDoubleSize == 128) {
439       LongDoubleWidth = LongDoubleAlign = 128;
440       LongDoubleFormat = &llvm::APFloat::IEEEquad();
441     }
442   }
443 
444   if (Opts.NewAlignOverride)
445     NewAlign = Opts.NewAlignOverride * getCharWidth();
446 
447   // Each unsigned fixed point type has the same number of fractional bits as
448   // its corresponding signed type.
449   PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
450   CheckFixedPointBits();
451 
452   if (Opts.ProtectParens && !checkArithmeticFenceSupported()) {
453     Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens";
454     Opts.ProtectParens = false;
455   }
456 }
457 
458 bool TargetInfo::initFeatureMap(
459     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
460     const std::vector<std::string> &FeatureVec) const {
461   for (const auto &F : FeatureVec) {
462     StringRef Name = F;
463     // Apply the feature via the target.
464     bool Enabled = Name[0] == '+';
465     setFeatureEnabled(Features, Name.substr(1), Enabled);
466   }
467   return true;
468 }
469 
470 TargetInfo::CallingConvKind
471 TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
472   if (getCXXABI() != TargetCXXABI::Microsoft &&
473       (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
474     return CCK_ClangABI4OrPS4;
475   return CCK_Default;
476 }
477 
478 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
479   switch (TK) {
480   case OCLTK_Image:
481   case OCLTK_Pipe:
482     return LangAS::opencl_global;
483 
484   case OCLTK_Sampler:
485     return LangAS::opencl_constant;
486 
487   default:
488     return LangAS::Default;
489   }
490 }
491 
492 //===----------------------------------------------------------------------===//
493 
494 
495 static StringRef removeGCCRegisterPrefix(StringRef Name) {
496   if (Name[0] == '%' || Name[0] == '#')
497     Name = Name.substr(1);
498 
499   return Name;
500 }
501 
502 /// isValidClobber - Returns whether the passed in string is
503 /// a valid clobber in an inline asm statement. This is used by
504 /// Sema.
505 bool TargetInfo::isValidClobber(StringRef Name) const {
506   return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" ||
507           Name == "unwind");
508 }
509 
510 /// isValidGCCRegisterName - Returns whether the passed in string
511 /// is a valid register name according to GCC. This is used by Sema for
512 /// inline asm statements.
513 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
514   if (Name.empty())
515     return false;
516 
517   // Get rid of any register prefix.
518   Name = removeGCCRegisterPrefix(Name);
519   if (Name.empty())
520     return false;
521 
522   ArrayRef<const char *> Names = getGCCRegNames();
523 
524   // If we have a number it maps to an entry in the register name array.
525   if (isDigit(Name[0])) {
526     unsigned n;
527     if (!Name.getAsInteger(0, n))
528       return n < Names.size();
529   }
530 
531   // Check register names.
532   if (llvm::is_contained(Names, Name))
533     return true;
534 
535   // Check any additional names that we have.
536   for (const AddlRegName &ARN : getGCCAddlRegNames())
537     for (const char *AN : ARN.Names) {
538       if (!AN)
539         break;
540       // Make sure the register that the additional name is for is within
541       // the bounds of the register names from above.
542       if (AN == Name && ARN.RegNum < Names.size())
543         return true;
544     }
545 
546   // Now check aliases.
547   for (const GCCRegAlias &GRA : getGCCRegAliases())
548     for (const char *A : GRA.Aliases) {
549       if (!A)
550         break;
551       if (A == Name)
552         return true;
553     }
554 
555   return false;
556 }
557 
558 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
559                                                    bool ReturnCanonical) const {
560   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
561 
562   // Get rid of any register prefix.
563   Name = removeGCCRegisterPrefix(Name);
564 
565   ArrayRef<const char *> Names = getGCCRegNames();
566 
567   // First, check if we have a number.
568   if (isDigit(Name[0])) {
569     unsigned n;
570     if (!Name.getAsInteger(0, n)) {
571       assert(n < Names.size() && "Out of bounds register number!");
572       return Names[n];
573     }
574   }
575 
576   // Check any additional names that we have.
577   for (const AddlRegName &ARN : getGCCAddlRegNames())
578     for (const char *AN : ARN.Names) {
579       if (!AN)
580         break;
581       // Make sure the register that the additional name is for is within
582       // the bounds of the register names from above.
583       if (AN == Name && ARN.RegNum < Names.size())
584         return ReturnCanonical ? Names[ARN.RegNum] : Name;
585     }
586 
587   // Now check aliases.
588   for (const GCCRegAlias &RA : getGCCRegAliases())
589     for (const char *A : RA.Aliases) {
590       if (!A)
591         break;
592       if (A == Name)
593         return RA.Register;
594     }
595 
596   return Name;
597 }
598 
599 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
600   const char *Name = Info.getConstraintStr().c_str();
601   // An output constraint must start with '=' or '+'
602   if (*Name != '=' && *Name != '+')
603     return false;
604 
605   if (*Name == '+')
606     Info.setIsReadWrite();
607 
608   Name++;
609   while (*Name) {
610     switch (*Name) {
611     default:
612       if (!validateAsmConstraint(Name, Info)) {
613         // FIXME: We temporarily return false
614         // so we can add more constraints as we hit it.
615         // Eventually, an unknown constraint should just be treated as 'g'.
616         return false;
617       }
618       break;
619     case '&': // early clobber.
620       Info.setEarlyClobber();
621       break;
622     case '%': // commutative.
623       // FIXME: Check that there is a another register after this one.
624       break;
625     case 'r': // general register.
626       Info.setAllowsRegister();
627       break;
628     case 'm': // memory operand.
629     case 'o': // offsetable memory operand.
630     case 'V': // non-offsetable memory operand.
631     case '<': // autodecrement memory operand.
632     case '>': // autoincrement memory operand.
633       Info.setAllowsMemory();
634       break;
635     case 'g': // general register, memory operand or immediate integer.
636     case 'X': // any operand.
637       Info.setAllowsRegister();
638       Info.setAllowsMemory();
639       break;
640     case ',': // multiple alternative constraint.  Pass it.
641       // Handle additional optional '=' or '+' modifiers.
642       if (Name[1] == '=' || Name[1] == '+')
643         Name++;
644       break;
645     case '#': // Ignore as constraint.
646       while (Name[1] && Name[1] != ',')
647         Name++;
648       break;
649     case '?': // Disparage slightly code.
650     case '!': // Disparage severely.
651     case '*': // Ignore for choosing register preferences.
652     case 'i': // Ignore i,n,E,F as output constraints (match from the other
653               // chars)
654     case 'n':
655     case 'E':
656     case 'F':
657       break;  // Pass them.
658     }
659 
660     Name++;
661   }
662 
663   // Early clobber with a read-write constraint which doesn't permit registers
664   // is invalid.
665   if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
666     return false;
667 
668   // If a constraint allows neither memory nor register operands it contains
669   // only modifiers. Reject it.
670   return Info.allowsMemory() || Info.allowsRegister();
671 }
672 
673 bool TargetInfo::resolveSymbolicName(const char *&Name,
674                                      ArrayRef<ConstraintInfo> OutputConstraints,
675                                      unsigned &Index) const {
676   assert(*Name == '[' && "Symbolic name did not start with '['");
677   Name++;
678   const char *Start = Name;
679   while (*Name && *Name != ']')
680     Name++;
681 
682   if (!*Name) {
683     // Missing ']'
684     return false;
685   }
686 
687   std::string SymbolicName(Start, Name - Start);
688 
689   for (Index = 0; Index != OutputConstraints.size(); ++Index)
690     if (SymbolicName == OutputConstraints[Index].getName())
691       return true;
692 
693   return false;
694 }
695 
696 bool TargetInfo::validateInputConstraint(
697                               MutableArrayRef<ConstraintInfo> OutputConstraints,
698                               ConstraintInfo &Info) const {
699   const char *Name = Info.ConstraintStr.c_str();
700 
701   if (!*Name)
702     return false;
703 
704   while (*Name) {
705     switch (*Name) {
706     default:
707       // Check if we have a matching constraint
708       if (*Name >= '0' && *Name <= '9') {
709         const char *DigitStart = Name;
710         while (Name[1] >= '0' && Name[1] <= '9')
711           Name++;
712         const char *DigitEnd = Name;
713         unsigned i;
714         if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
715                 .getAsInteger(10, i))
716           return false;
717 
718         // Check if matching constraint is out of bounds.
719         if (i >= OutputConstraints.size()) return false;
720 
721         // A number must refer to an output only operand.
722         if (OutputConstraints[i].isReadWrite())
723           return false;
724 
725         // If the constraint is already tied, it must be tied to the
726         // same operand referenced to by the number.
727         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
728           return false;
729 
730         // The constraint should have the same info as the respective
731         // output constraint.
732         Info.setTiedOperand(i, OutputConstraints[i]);
733       } else if (!validateAsmConstraint(Name, Info)) {
734         // FIXME: This error return is in place temporarily so we can
735         // add more constraints as we hit it.  Eventually, an unknown
736         // constraint should just be treated as 'g'.
737         return false;
738       }
739       break;
740     case '[': {
741       unsigned Index = 0;
742       if (!resolveSymbolicName(Name, OutputConstraints, Index))
743         return false;
744 
745       // If the constraint is already tied, it must be tied to the
746       // same operand referenced to by the number.
747       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
748         return false;
749 
750       // A number must refer to an output only operand.
751       if (OutputConstraints[Index].isReadWrite())
752         return false;
753 
754       Info.setTiedOperand(Index, OutputConstraints[Index]);
755       break;
756     }
757     case '%': // commutative
758       // FIXME: Fail if % is used with the last operand.
759       break;
760     case 'i': // immediate integer.
761       break;
762     case 'n': // immediate integer with a known value.
763       Info.setRequiresImmediate();
764       break;
765     case 'I':  // Various constant constraints with target-specific meanings.
766     case 'J':
767     case 'K':
768     case 'L':
769     case 'M':
770     case 'N':
771     case 'O':
772     case 'P':
773       if (!validateAsmConstraint(Name, Info))
774         return false;
775       break;
776     case 'r': // general register.
777       Info.setAllowsRegister();
778       break;
779     case 'm': // memory operand.
780     case 'o': // offsettable memory operand.
781     case 'V': // non-offsettable memory operand.
782     case '<': // autodecrement memory operand.
783     case '>': // autoincrement memory operand.
784       Info.setAllowsMemory();
785       break;
786     case 'g': // general register, memory operand or immediate integer.
787     case 'X': // any operand.
788       Info.setAllowsRegister();
789       Info.setAllowsMemory();
790       break;
791     case 'E': // immediate floating point.
792     case 'F': // immediate floating point.
793     case 'p': // address operand.
794       break;
795     case ',': // multiple alternative constraint.  Ignore comma.
796       break;
797     case '#': // Ignore as constraint.
798       while (Name[1] && Name[1] != ',')
799         Name++;
800       break;
801     case '?': // Disparage slightly code.
802     case '!': // Disparage severely.
803     case '*': // Ignore for choosing register preferences.
804       break;  // Pass them.
805     }
806 
807     Name++;
808   }
809 
810   return true;
811 }
812 
813 void TargetInfo::CheckFixedPointBits() const {
814   // Check that the number of fractional and integral bits (and maybe sign) can
815   // fit into the bits given for a fixed point type.
816   assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
817   assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
818   assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
819   assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
820          ShortAccumWidth);
821   assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
822   assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
823          LongAccumWidth);
824 
825   assert(getShortFractScale() + 1 <= ShortFractWidth);
826   assert(getFractScale() + 1 <= FractWidth);
827   assert(getLongFractScale() + 1 <= LongFractWidth);
828   assert(getUnsignedShortFractScale() <= ShortFractWidth);
829   assert(getUnsignedFractScale() <= FractWidth);
830   assert(getUnsignedLongFractScale() <= LongFractWidth);
831 
832   // Each unsigned fract type has either the same number of fractional bits
833   // as, or one more fractional bit than, its corresponding signed fract type.
834   assert(getShortFractScale() == getUnsignedShortFractScale() ||
835          getShortFractScale() == getUnsignedShortFractScale() - 1);
836   assert(getFractScale() == getUnsignedFractScale() ||
837          getFractScale() == getUnsignedFractScale() - 1);
838   assert(getLongFractScale() == getUnsignedLongFractScale() ||
839          getLongFractScale() == getUnsignedLongFractScale() - 1);
840 
841   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
842   // fractional bits is nondecreasing for each of the following sets of
843   // fixed-point types:
844   // - signed fract types
845   // - unsigned fract types
846   // - signed accum types
847   // - unsigned accum types.
848   assert(getLongFractScale() >= getFractScale() &&
849          getFractScale() >= getShortFractScale());
850   assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
851          getUnsignedFractScale() >= getUnsignedShortFractScale());
852   assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
853   assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
854          getUnsignedAccumScale() >= getUnsignedShortAccumScale());
855 
856   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
857   // integral bits is nondecreasing for each of the following sets of
858   // fixed-point types:
859   // - signed accum types
860   // - unsigned accum types
861   assert(getLongAccumIBits() >= getAccumIBits() &&
862          getAccumIBits() >= getShortAccumIBits());
863   assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
864          getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
865 
866   // Each signed accum type has at least as many integral bits as its
867   // corresponding unsigned accum type.
868   assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
869   assert(getAccumIBits() >= getUnsignedAccumIBits());
870   assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
871 }
872 
873 void TargetInfo::copyAuxTarget(const TargetInfo *Aux) {
874   auto *Target = static_cast<TransferrableTargetInfo*>(this);
875   auto *Src = static_cast<const TransferrableTargetInfo*>(Aux);
876   *Target = *Src;
877 }
878