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