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