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     // FIXME: OpenCLGenericAddressSpace is also defined in setLangDefaults()
404     // for OpenCL C 2.0 but with no access to target capabilities. Target
405     // should be immutable once created and thus this language option needs
406     // to be defined only once.
407     if (Opts.OpenCLVersion >= 300) {
408       const auto &OpenCLFeaturesMap = getSupportedOpenCLOpts();
409       Opts.OpenCLGenericAddressSpace = hasFeatureEnabled(
410           OpenCLFeaturesMap, "__opencl_c_generic_address_space");
411     }
412   }
413 
414   if (Opts.DoubleSize) {
415     if (Opts.DoubleSize == 32) {
416       DoubleWidth = 32;
417       LongDoubleWidth = 32;
418       DoubleFormat = &llvm::APFloat::IEEEsingle();
419       LongDoubleFormat = &llvm::APFloat::IEEEsingle();
420     } else if (Opts.DoubleSize == 64) {
421       DoubleWidth = 64;
422       LongDoubleWidth = 64;
423       DoubleFormat = &llvm::APFloat::IEEEdouble();
424       LongDoubleFormat = &llvm::APFloat::IEEEdouble();
425     }
426   }
427 
428   if (Opts.LongDoubleSize) {
429     if (Opts.LongDoubleSize == DoubleWidth) {
430       LongDoubleWidth = DoubleWidth;
431       LongDoubleAlign = DoubleAlign;
432       LongDoubleFormat = DoubleFormat;
433     } else if (Opts.LongDoubleSize == 128) {
434       LongDoubleWidth = LongDoubleAlign = 128;
435       LongDoubleFormat = &llvm::APFloat::IEEEquad();
436     }
437   }
438 
439   if (Opts.NewAlignOverride)
440     NewAlign = Opts.NewAlignOverride * getCharWidth();
441 
442   // Each unsigned fixed point type has the same number of fractional bits as
443   // its corresponding signed type.
444   PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
445   CheckFixedPointBits();
446 
447   if (Opts.ProtectParens && !checkArithmeticFenceSupported()) {
448     Diags.Report(diag::err_opt_not_valid_on_target) << "-fprotect-parens";
449     Opts.ProtectParens = false;
450   }
451 }
452 
453 bool TargetInfo::initFeatureMap(
454     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
455     const std::vector<std::string> &FeatureVec) const {
456   for (const auto &F : FeatureVec) {
457     StringRef Name = F;
458     // Apply the feature via the target.
459     bool Enabled = Name[0] == '+';
460     setFeatureEnabled(Features, Name.substr(1), Enabled);
461   }
462   return true;
463 }
464 
465 TargetInfo::CallingConvKind
466 TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
467   if (getCXXABI() != TargetCXXABI::Microsoft &&
468       (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
469     return CCK_ClangABI4OrPS4;
470   return CCK_Default;
471 }
472 
473 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
474   switch (TK) {
475   case OCLTK_Image:
476   case OCLTK_Pipe:
477     return LangAS::opencl_global;
478 
479   case OCLTK_Sampler:
480     return LangAS::opencl_constant;
481 
482   default:
483     return LangAS::Default;
484   }
485 }
486 
487 //===----------------------------------------------------------------------===//
488 
489 
490 static StringRef removeGCCRegisterPrefix(StringRef Name) {
491   if (Name[0] == '%' || Name[0] == '#')
492     Name = Name.substr(1);
493 
494   return Name;
495 }
496 
497 /// isValidClobber - Returns whether the passed in string is
498 /// a valid clobber in an inline asm statement. This is used by
499 /// Sema.
500 bool TargetInfo::isValidClobber(StringRef Name) const {
501   return (isValidGCCRegisterName(Name) || Name == "memory" || Name == "cc" ||
502           Name == "unwind");
503 }
504 
505 /// isValidGCCRegisterName - Returns whether the passed in string
506 /// is a valid register name according to GCC. This is used by Sema for
507 /// inline asm statements.
508 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
509   if (Name.empty())
510     return false;
511 
512   // Get rid of any register prefix.
513   Name = removeGCCRegisterPrefix(Name);
514   if (Name.empty())
515     return false;
516 
517   ArrayRef<const char *> Names = getGCCRegNames();
518 
519   // If we have a number it maps to an entry in the register name array.
520   if (isDigit(Name[0])) {
521     unsigned n;
522     if (!Name.getAsInteger(0, n))
523       return n < Names.size();
524   }
525 
526   // Check register names.
527   if (llvm::is_contained(Names, Name))
528     return true;
529 
530   // Check any additional names that we have.
531   for (const AddlRegName &ARN : getGCCAddlRegNames())
532     for (const char *AN : ARN.Names) {
533       if (!AN)
534         break;
535       // Make sure the register that the additional name is for is within
536       // the bounds of the register names from above.
537       if (AN == Name && ARN.RegNum < Names.size())
538         return true;
539     }
540 
541   // Now check aliases.
542   for (const GCCRegAlias &GRA : getGCCRegAliases())
543     for (const char *A : GRA.Aliases) {
544       if (!A)
545         break;
546       if (A == Name)
547         return true;
548     }
549 
550   return false;
551 }
552 
553 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
554                                                    bool ReturnCanonical) const {
555   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
556 
557   // Get rid of any register prefix.
558   Name = removeGCCRegisterPrefix(Name);
559 
560   ArrayRef<const char *> Names = getGCCRegNames();
561 
562   // First, check if we have a number.
563   if (isDigit(Name[0])) {
564     unsigned n;
565     if (!Name.getAsInteger(0, n)) {
566       assert(n < Names.size() && "Out of bounds register number!");
567       return Names[n];
568     }
569   }
570 
571   // Check any additional names that we have.
572   for (const AddlRegName &ARN : getGCCAddlRegNames())
573     for (const char *AN : ARN.Names) {
574       if (!AN)
575         break;
576       // Make sure the register that the additional name is for is within
577       // the bounds of the register names from above.
578       if (AN == Name && ARN.RegNum < Names.size())
579         return ReturnCanonical ? Names[ARN.RegNum] : Name;
580     }
581 
582   // Now check aliases.
583   for (const GCCRegAlias &RA : getGCCRegAliases())
584     for (const char *A : RA.Aliases) {
585       if (!A)
586         break;
587       if (A == Name)
588         return RA.Register;
589     }
590 
591   return Name;
592 }
593 
594 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
595   const char *Name = Info.getConstraintStr().c_str();
596   // An output constraint must start with '=' or '+'
597   if (*Name != '=' && *Name != '+')
598     return false;
599 
600   if (*Name == '+')
601     Info.setIsReadWrite();
602 
603   Name++;
604   while (*Name) {
605     switch (*Name) {
606     default:
607       if (!validateAsmConstraint(Name, Info)) {
608         // FIXME: We temporarily return false
609         // so we can add more constraints as we hit it.
610         // Eventually, an unknown constraint should just be treated as 'g'.
611         return false;
612       }
613       break;
614     case '&': // early clobber.
615       Info.setEarlyClobber();
616       break;
617     case '%': // commutative.
618       // FIXME: Check that there is a another register after this one.
619       break;
620     case 'r': // general register.
621       Info.setAllowsRegister();
622       break;
623     case 'm': // memory operand.
624     case 'o': // offsetable memory operand.
625     case 'V': // non-offsetable memory operand.
626     case '<': // autodecrement memory operand.
627     case '>': // autoincrement memory operand.
628       Info.setAllowsMemory();
629       break;
630     case 'g': // general register, memory operand or immediate integer.
631     case 'X': // any operand.
632       Info.setAllowsRegister();
633       Info.setAllowsMemory();
634       break;
635     case ',': // multiple alternative constraint.  Pass it.
636       // Handle additional optional '=' or '+' modifiers.
637       if (Name[1] == '=' || Name[1] == '+')
638         Name++;
639       break;
640     case '#': // Ignore as constraint.
641       while (Name[1] && Name[1] != ',')
642         Name++;
643       break;
644     case '?': // Disparage slightly code.
645     case '!': // Disparage severely.
646     case '*': // Ignore for choosing register preferences.
647     case 'i': // Ignore i,n,E,F as output constraints (match from the other
648               // chars)
649     case 'n':
650     case 'E':
651     case 'F':
652       break;  // Pass them.
653     }
654 
655     Name++;
656   }
657 
658   // Early clobber with a read-write constraint which doesn't permit registers
659   // is invalid.
660   if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
661     return false;
662 
663   // If a constraint allows neither memory nor register operands it contains
664   // only modifiers. Reject it.
665   return Info.allowsMemory() || Info.allowsRegister();
666 }
667 
668 bool TargetInfo::resolveSymbolicName(const char *&Name,
669                                      ArrayRef<ConstraintInfo> OutputConstraints,
670                                      unsigned &Index) const {
671   assert(*Name == '[' && "Symbolic name did not start with '['");
672   Name++;
673   const char *Start = Name;
674   while (*Name && *Name != ']')
675     Name++;
676 
677   if (!*Name) {
678     // Missing ']'
679     return false;
680   }
681 
682   std::string SymbolicName(Start, Name - Start);
683 
684   for (Index = 0; Index != OutputConstraints.size(); ++Index)
685     if (SymbolicName == OutputConstraints[Index].getName())
686       return true;
687 
688   return false;
689 }
690 
691 bool TargetInfo::validateInputConstraint(
692                               MutableArrayRef<ConstraintInfo> OutputConstraints,
693                               ConstraintInfo &Info) const {
694   const char *Name = Info.ConstraintStr.c_str();
695 
696   if (!*Name)
697     return false;
698 
699   while (*Name) {
700     switch (*Name) {
701     default:
702       // Check if we have a matching constraint
703       if (*Name >= '0' && *Name <= '9') {
704         const char *DigitStart = Name;
705         while (Name[1] >= '0' && Name[1] <= '9')
706           Name++;
707         const char *DigitEnd = Name;
708         unsigned i;
709         if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
710                 .getAsInteger(10, i))
711           return false;
712 
713         // Check if matching constraint is out of bounds.
714         if (i >= OutputConstraints.size()) return false;
715 
716         // A number must refer to an output only operand.
717         if (OutputConstraints[i].isReadWrite())
718           return false;
719 
720         // If the constraint is already tied, it must be tied to the
721         // same operand referenced to by the number.
722         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
723           return false;
724 
725         // The constraint should have the same info as the respective
726         // output constraint.
727         Info.setTiedOperand(i, OutputConstraints[i]);
728       } else if (!validateAsmConstraint(Name, Info)) {
729         // FIXME: This error return is in place temporarily so we can
730         // add more constraints as we hit it.  Eventually, an unknown
731         // constraint should just be treated as 'g'.
732         return false;
733       }
734       break;
735     case '[': {
736       unsigned Index = 0;
737       if (!resolveSymbolicName(Name, OutputConstraints, Index))
738         return false;
739 
740       // If the constraint is already tied, it must be tied to the
741       // same operand referenced to by the number.
742       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
743         return false;
744 
745       // A number must refer to an output only operand.
746       if (OutputConstraints[Index].isReadWrite())
747         return false;
748 
749       Info.setTiedOperand(Index, OutputConstraints[Index]);
750       break;
751     }
752     case '%': // commutative
753       // FIXME: Fail if % is used with the last operand.
754       break;
755     case 'i': // immediate integer.
756       break;
757     case 'n': // immediate integer with a known value.
758       Info.setRequiresImmediate();
759       break;
760     case 'I':  // Various constant constraints with target-specific meanings.
761     case 'J':
762     case 'K':
763     case 'L':
764     case 'M':
765     case 'N':
766     case 'O':
767     case 'P':
768       if (!validateAsmConstraint(Name, Info))
769         return false;
770       break;
771     case 'r': // general register.
772       Info.setAllowsRegister();
773       break;
774     case 'm': // memory operand.
775     case 'o': // offsettable memory operand.
776     case 'V': // non-offsettable memory operand.
777     case '<': // autodecrement memory operand.
778     case '>': // autoincrement memory operand.
779       Info.setAllowsMemory();
780       break;
781     case 'g': // general register, memory operand or immediate integer.
782     case 'X': // any operand.
783       Info.setAllowsRegister();
784       Info.setAllowsMemory();
785       break;
786     case 'E': // immediate floating point.
787     case 'F': // immediate floating point.
788     case 'p': // address operand.
789       break;
790     case ',': // multiple alternative constraint.  Ignore comma.
791       break;
792     case '#': // Ignore as constraint.
793       while (Name[1] && Name[1] != ',')
794         Name++;
795       break;
796     case '?': // Disparage slightly code.
797     case '!': // Disparage severely.
798     case '*': // Ignore for choosing register preferences.
799       break;  // Pass them.
800     }
801 
802     Name++;
803   }
804 
805   return true;
806 }
807 
808 void TargetInfo::CheckFixedPointBits() const {
809   // Check that the number of fractional and integral bits (and maybe sign) can
810   // fit into the bits given for a fixed point type.
811   assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
812   assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
813   assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
814   assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
815          ShortAccumWidth);
816   assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
817   assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
818          LongAccumWidth);
819 
820   assert(getShortFractScale() + 1 <= ShortFractWidth);
821   assert(getFractScale() + 1 <= FractWidth);
822   assert(getLongFractScale() + 1 <= LongFractWidth);
823   assert(getUnsignedShortFractScale() <= ShortFractWidth);
824   assert(getUnsignedFractScale() <= FractWidth);
825   assert(getUnsignedLongFractScale() <= LongFractWidth);
826 
827   // Each unsigned fract type has either the same number of fractional bits
828   // as, or one more fractional bit than, its corresponding signed fract type.
829   assert(getShortFractScale() == getUnsignedShortFractScale() ||
830          getShortFractScale() == getUnsignedShortFractScale() - 1);
831   assert(getFractScale() == getUnsignedFractScale() ||
832          getFractScale() == getUnsignedFractScale() - 1);
833   assert(getLongFractScale() == getUnsignedLongFractScale() ||
834          getLongFractScale() == getUnsignedLongFractScale() - 1);
835 
836   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
837   // fractional bits is nondecreasing for each of the following sets of
838   // fixed-point types:
839   // - signed fract types
840   // - unsigned fract types
841   // - signed accum types
842   // - unsigned accum types.
843   assert(getLongFractScale() >= getFractScale() &&
844          getFractScale() >= getShortFractScale());
845   assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
846          getUnsignedFractScale() >= getUnsignedShortFractScale());
847   assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
848   assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
849          getUnsignedAccumScale() >= getUnsignedShortAccumScale());
850 
851   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
852   // integral bits is nondecreasing for each of the following sets of
853   // fixed-point types:
854   // - signed accum types
855   // - unsigned accum types
856   assert(getLongAccumIBits() >= getAccumIBits() &&
857          getAccumIBits() >= getShortAccumIBits());
858   assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
859          getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
860 
861   // Each signed accum type has at least as many integral bits as its
862   // corresponding unsigned accum type.
863   assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
864   assert(getAccumIBits() >= getUnsignedAccumIBits());
865   assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
866 }
867 
868 void TargetInfo::copyAuxTarget(const TargetInfo *Aux) {
869   auto *Target = static_cast<TransferrableTargetInfo*>(this);
870   auto *Src = static_cast<const TransferrableTargetInfo*>(Aux);
871   *Target = *Src;
872 }
873