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) const {
269   if (getFloatWidth() == BitWidth)
270     return Float;
271   if (getDoubleWidth() == BitWidth)
272     return Double;
273 
274   switch (BitWidth) {
275   case 96:
276     if (&getLongDoubleFormat() == &llvm::APFloat::x87DoubleExtended())
277       return LongDouble;
278     break;
279   case 128:
280     if (&getLongDoubleFormat() == &llvm::APFloat::PPCDoubleDouble() ||
281         &getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
282       return LongDouble;
283     if (hasFloat128Type())
284       return Float128;
285     break;
286   }
287 
288   return NoFloat;
289 }
290 
291 /// getTypeAlign - Return the alignment (in bits) of the specified integer type
292 /// enum. For example, SignedInt -> getIntAlign().
293 unsigned TargetInfo::getTypeAlign(IntType T) const {
294   switch (T) {
295   default: llvm_unreachable("not an integer!");
296   case SignedChar:
297   case UnsignedChar:     return getCharAlign();
298   case SignedShort:
299   case UnsignedShort:    return getShortAlign();
300   case SignedInt:
301   case UnsignedInt:      return getIntAlign();
302   case SignedLong:
303   case UnsignedLong:     return getLongAlign();
304   case SignedLongLong:
305   case UnsignedLongLong: return getLongLongAlign();
306   };
307 }
308 
309 /// isTypeSigned - Return whether an integer types is signed. Returns true if
310 /// the type is signed; false otherwise.
311 bool TargetInfo::isTypeSigned(IntType T) {
312   switch (T) {
313   default: llvm_unreachable("not an integer!");
314   case SignedChar:
315   case SignedShort:
316   case SignedInt:
317   case SignedLong:
318   case SignedLongLong:
319     return true;
320   case UnsignedChar:
321   case UnsignedShort:
322   case UnsignedInt:
323   case UnsignedLong:
324   case UnsignedLongLong:
325     return false;
326   };
327 }
328 
329 /// adjust - Set forced language options.
330 /// Apply changes to the target information with respect to certain
331 /// language options which change the target configuration and adjust
332 /// the language based on the target options where applicable.
333 void TargetInfo::adjust(LangOptions &Opts) {
334   if (Opts.NoBitFieldTypeAlign)
335     UseBitFieldTypeAlignment = false;
336 
337   switch (Opts.WCharSize) {
338   default: llvm_unreachable("invalid wchar_t width");
339   case 0: break;
340   case 1: WCharType = Opts.WCharIsSigned ? SignedChar : UnsignedChar; break;
341   case 2: WCharType = Opts.WCharIsSigned ? SignedShort : UnsignedShort; break;
342   case 4: WCharType = Opts.WCharIsSigned ? SignedInt : UnsignedInt; break;
343   }
344 
345   if (Opts.AlignDouble) {
346     DoubleAlign = LongLongAlign = 64;
347     LongDoubleAlign = 64;
348   }
349 
350   if (Opts.OpenCL) {
351     // OpenCL C requires specific widths for types, irrespective of
352     // what these normally are for the target.
353     // We also define long long and long double here, although the
354     // OpenCL standard only mentions these as "reserved".
355     IntWidth = IntAlign = 32;
356     LongWidth = LongAlign = 64;
357     LongLongWidth = LongLongAlign = 128;
358     HalfWidth = HalfAlign = 16;
359     FloatWidth = FloatAlign = 32;
360 
361     // Embedded 32-bit targets (OpenCL EP) might have double C type
362     // defined as float. Let's not override this as it might lead
363     // to generating illegal code that uses 64bit doubles.
364     if (DoubleWidth != FloatWidth) {
365       DoubleWidth = DoubleAlign = 64;
366       DoubleFormat = &llvm::APFloat::IEEEdouble();
367     }
368     LongDoubleWidth = LongDoubleAlign = 128;
369 
370     unsigned MaxPointerWidth = getMaxPointerWidth();
371     assert(MaxPointerWidth == 32 || MaxPointerWidth == 64);
372     bool Is32BitArch = MaxPointerWidth == 32;
373     SizeType = Is32BitArch ? UnsignedInt : UnsignedLong;
374     PtrDiffType = Is32BitArch ? SignedInt : SignedLong;
375     IntPtrType = Is32BitArch ? SignedInt : SignedLong;
376 
377     IntMaxType = SignedLongLong;
378     Int64Type = SignedLong;
379 
380     HalfFormat = &llvm::APFloat::IEEEhalf();
381     FloatFormat = &llvm::APFloat::IEEEsingle();
382     LongDoubleFormat = &llvm::APFloat::IEEEquad();
383   }
384 
385   if (Opts.DoubleSize) {
386     if (Opts.DoubleSize == 32) {
387       DoubleWidth = 32;
388       LongDoubleWidth = 32;
389       DoubleFormat = &llvm::APFloat::IEEEsingle();
390       LongDoubleFormat = &llvm::APFloat::IEEEsingle();
391     } else if (Opts.DoubleSize == 64) {
392       DoubleWidth = 64;
393       LongDoubleWidth = 64;
394       DoubleFormat = &llvm::APFloat::IEEEdouble();
395       LongDoubleFormat = &llvm::APFloat::IEEEdouble();
396     }
397   }
398 
399   if (Opts.LongDoubleSize) {
400     if (Opts.LongDoubleSize == DoubleWidth) {
401       LongDoubleWidth = DoubleWidth;
402       LongDoubleAlign = DoubleAlign;
403       LongDoubleFormat = DoubleFormat;
404     } else if (Opts.LongDoubleSize == 128) {
405       LongDoubleWidth = LongDoubleAlign = 128;
406       LongDoubleFormat = &llvm::APFloat::IEEEquad();
407     }
408   }
409 
410   if (Opts.NewAlignOverride)
411     NewAlign = Opts.NewAlignOverride * getCharWidth();
412 
413   // Each unsigned fixed point type has the same number of fractional bits as
414   // its corresponding signed type.
415   PaddingOnUnsignedFixedPoint |= Opts.PaddingOnUnsignedFixedPoint;
416   CheckFixedPointBits();
417 }
418 
419 bool TargetInfo::initFeatureMap(
420     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
421     const std::vector<std::string> &FeatureVec) const {
422   for (const auto &F : FeatureVec) {
423     StringRef Name = F;
424     // Apply the feature via the target.
425     bool Enabled = Name[0] == '+';
426     setFeatureEnabled(Features, Name.substr(1), Enabled);
427   }
428   return true;
429 }
430 
431 TargetInfo::CallingConvKind
432 TargetInfo::getCallingConvKind(bool ClangABICompat4) const {
433   if (getCXXABI() != TargetCXXABI::Microsoft &&
434       (ClangABICompat4 || getTriple().getOS() == llvm::Triple::PS4))
435     return CCK_ClangABI4OrPS4;
436   return CCK_Default;
437 }
438 
439 LangAS TargetInfo::getOpenCLTypeAddrSpace(OpenCLTypeKind TK) const {
440   switch (TK) {
441   case OCLTK_Image:
442   case OCLTK_Pipe:
443     return LangAS::opencl_global;
444 
445   case OCLTK_Sampler:
446     return LangAS::opencl_constant;
447 
448   default:
449     return LangAS::Default;
450   }
451 }
452 
453 //===----------------------------------------------------------------------===//
454 
455 
456 static StringRef removeGCCRegisterPrefix(StringRef Name) {
457   if (Name[0] == '%' || Name[0] == '#')
458     Name = Name.substr(1);
459 
460   return Name;
461 }
462 
463 /// isValidClobber - Returns whether the passed in string is
464 /// a valid clobber in an inline asm statement. This is used by
465 /// Sema.
466 bool TargetInfo::isValidClobber(StringRef Name) const {
467   return (isValidGCCRegisterName(Name) ||
468           Name == "memory" || Name == "cc");
469 }
470 
471 /// isValidGCCRegisterName - Returns whether the passed in string
472 /// is a valid register name according to GCC. This is used by Sema for
473 /// inline asm statements.
474 bool TargetInfo::isValidGCCRegisterName(StringRef Name) const {
475   if (Name.empty())
476     return false;
477 
478   // Get rid of any register prefix.
479   Name = removeGCCRegisterPrefix(Name);
480   if (Name.empty())
481     return false;
482 
483   ArrayRef<const char *> Names = getGCCRegNames();
484 
485   // If we have a number it maps to an entry in the register name array.
486   if (isDigit(Name[0])) {
487     unsigned n;
488     if (!Name.getAsInteger(0, n))
489       return n < Names.size();
490   }
491 
492   // Check register names.
493   if (llvm::is_contained(Names, Name))
494     return true;
495 
496   // Check any additional names that we have.
497   for (const AddlRegName &ARN : getGCCAddlRegNames())
498     for (const char *AN : ARN.Names) {
499       if (!AN)
500         break;
501       // Make sure the register that the additional name is for is within
502       // the bounds of the register names from above.
503       if (AN == Name && ARN.RegNum < Names.size())
504         return true;
505     }
506 
507   // Now check aliases.
508   for (const GCCRegAlias &GRA : getGCCRegAliases())
509     for (const char *A : GRA.Aliases) {
510       if (!A)
511         break;
512       if (A == Name)
513         return true;
514     }
515 
516   return false;
517 }
518 
519 StringRef TargetInfo::getNormalizedGCCRegisterName(StringRef Name,
520                                                    bool ReturnCanonical) const {
521   assert(isValidGCCRegisterName(Name) && "Invalid register passed in");
522 
523   // Get rid of any register prefix.
524   Name = removeGCCRegisterPrefix(Name);
525 
526   ArrayRef<const char *> Names = getGCCRegNames();
527 
528   // First, check if we have a number.
529   if (isDigit(Name[0])) {
530     unsigned n;
531     if (!Name.getAsInteger(0, n)) {
532       assert(n < Names.size() && "Out of bounds register number!");
533       return Names[n];
534     }
535   }
536 
537   // Check any additional names that we have.
538   for (const AddlRegName &ARN : getGCCAddlRegNames())
539     for (const char *AN : ARN.Names) {
540       if (!AN)
541         break;
542       // Make sure the register that the additional name is for is within
543       // the bounds of the register names from above.
544       if (AN == Name && ARN.RegNum < Names.size())
545         return ReturnCanonical ? Names[ARN.RegNum] : Name;
546     }
547 
548   // Now check aliases.
549   for (const GCCRegAlias &RA : getGCCRegAliases())
550     for (const char *A : RA.Aliases) {
551       if (!A)
552         break;
553       if (A == Name)
554         return RA.Register;
555     }
556 
557   return Name;
558 }
559 
560 bool TargetInfo::validateOutputConstraint(ConstraintInfo &Info) const {
561   const char *Name = Info.getConstraintStr().c_str();
562   // An output constraint must start with '=' or '+'
563   if (*Name != '=' && *Name != '+')
564     return false;
565 
566   if (*Name == '+')
567     Info.setIsReadWrite();
568 
569   Name++;
570   while (*Name) {
571     switch (*Name) {
572     default:
573       if (!validateAsmConstraint(Name, Info)) {
574         // FIXME: We temporarily return false
575         // so we can add more constraints as we hit it.
576         // Eventually, an unknown constraint should just be treated as 'g'.
577         return false;
578       }
579       break;
580     case '&': // early clobber.
581       Info.setEarlyClobber();
582       break;
583     case '%': // commutative.
584       // FIXME: Check that there is a another register after this one.
585       break;
586     case 'r': // general register.
587       Info.setAllowsRegister();
588       break;
589     case 'm': // memory operand.
590     case 'o': // offsetable memory operand.
591     case 'V': // non-offsetable memory operand.
592     case '<': // autodecrement memory operand.
593     case '>': // autoincrement memory operand.
594       Info.setAllowsMemory();
595       break;
596     case 'g': // general register, memory operand or immediate integer.
597     case 'X': // any operand.
598       Info.setAllowsRegister();
599       Info.setAllowsMemory();
600       break;
601     case ',': // multiple alternative constraint.  Pass it.
602       // Handle additional optional '=' or '+' modifiers.
603       if (Name[1] == '=' || Name[1] == '+')
604         Name++;
605       break;
606     case '#': // Ignore as constraint.
607       while (Name[1] && Name[1] != ',')
608         Name++;
609       break;
610     case '?': // Disparage slightly code.
611     case '!': // Disparage severely.
612     case '*': // Ignore for choosing register preferences.
613     case 'i': // Ignore i,n,E,F as output constraints (match from the other
614               // chars)
615     case 'n':
616     case 'E':
617     case 'F':
618       break;  // Pass them.
619     }
620 
621     Name++;
622   }
623 
624   // Early clobber with a read-write constraint which doesn't permit registers
625   // is invalid.
626   if (Info.earlyClobber() && Info.isReadWrite() && !Info.allowsRegister())
627     return false;
628 
629   // If a constraint allows neither memory nor register operands it contains
630   // only modifiers. Reject it.
631   return Info.allowsMemory() || Info.allowsRegister();
632 }
633 
634 bool TargetInfo::resolveSymbolicName(const char *&Name,
635                                      ArrayRef<ConstraintInfo> OutputConstraints,
636                                      unsigned &Index) const {
637   assert(*Name == '[' && "Symbolic name did not start with '['");
638   Name++;
639   const char *Start = Name;
640   while (*Name && *Name != ']')
641     Name++;
642 
643   if (!*Name) {
644     // Missing ']'
645     return false;
646   }
647 
648   std::string SymbolicName(Start, Name - Start);
649 
650   for (Index = 0; Index != OutputConstraints.size(); ++Index)
651     if (SymbolicName == OutputConstraints[Index].getName())
652       return true;
653 
654   return false;
655 }
656 
657 bool TargetInfo::validateInputConstraint(
658                               MutableArrayRef<ConstraintInfo> OutputConstraints,
659                               ConstraintInfo &Info) const {
660   const char *Name = Info.ConstraintStr.c_str();
661 
662   if (!*Name)
663     return false;
664 
665   while (*Name) {
666     switch (*Name) {
667     default:
668       // Check if we have a matching constraint
669       if (*Name >= '0' && *Name <= '9') {
670         const char *DigitStart = Name;
671         while (Name[1] >= '0' && Name[1] <= '9')
672           Name++;
673         const char *DigitEnd = Name;
674         unsigned i;
675         if (StringRef(DigitStart, DigitEnd - DigitStart + 1)
676                 .getAsInteger(10, i))
677           return false;
678 
679         // Check if matching constraint is out of bounds.
680         if (i >= OutputConstraints.size()) return false;
681 
682         // A number must refer to an output only operand.
683         if (OutputConstraints[i].isReadWrite())
684           return false;
685 
686         // If the constraint is already tied, it must be tied to the
687         // same operand referenced to by the number.
688         if (Info.hasTiedOperand() && Info.getTiedOperand() != i)
689           return false;
690 
691         // The constraint should have the same info as the respective
692         // output constraint.
693         Info.setTiedOperand(i, OutputConstraints[i]);
694       } else if (!validateAsmConstraint(Name, Info)) {
695         // FIXME: This error return is in place temporarily so we can
696         // add more constraints as we hit it.  Eventually, an unknown
697         // constraint should just be treated as 'g'.
698         return false;
699       }
700       break;
701     case '[': {
702       unsigned Index = 0;
703       if (!resolveSymbolicName(Name, OutputConstraints, Index))
704         return false;
705 
706       // If the constraint is already tied, it must be tied to the
707       // same operand referenced to by the number.
708       if (Info.hasTiedOperand() && Info.getTiedOperand() != Index)
709         return false;
710 
711       // A number must refer to an output only operand.
712       if (OutputConstraints[Index].isReadWrite())
713         return false;
714 
715       Info.setTiedOperand(Index, OutputConstraints[Index]);
716       break;
717     }
718     case '%': // commutative
719       // FIXME: Fail if % is used with the last operand.
720       break;
721     case 'i': // immediate integer.
722       break;
723     case 'n': // immediate integer with a known value.
724       Info.setRequiresImmediate();
725       break;
726     case 'I':  // Various constant constraints with target-specific meanings.
727     case 'J':
728     case 'K':
729     case 'L':
730     case 'M':
731     case 'N':
732     case 'O':
733     case 'P':
734       if (!validateAsmConstraint(Name, Info))
735         return false;
736       break;
737     case 'r': // general register.
738       Info.setAllowsRegister();
739       break;
740     case 'm': // memory operand.
741     case 'o': // offsettable memory operand.
742     case 'V': // non-offsettable memory operand.
743     case '<': // autodecrement memory operand.
744     case '>': // autoincrement memory operand.
745       Info.setAllowsMemory();
746       break;
747     case 'g': // general register, memory operand or immediate integer.
748     case 'X': // any operand.
749       Info.setAllowsRegister();
750       Info.setAllowsMemory();
751       break;
752     case 'E': // immediate floating point.
753     case 'F': // immediate floating point.
754     case 'p': // address operand.
755       break;
756     case ',': // multiple alternative constraint.  Ignore comma.
757       break;
758     case '#': // Ignore as constraint.
759       while (Name[1] && Name[1] != ',')
760         Name++;
761       break;
762     case '?': // Disparage slightly code.
763     case '!': // Disparage severely.
764     case '*': // Ignore for choosing register preferences.
765       break;  // Pass them.
766     }
767 
768     Name++;
769   }
770 
771   return true;
772 }
773 
774 void TargetInfo::CheckFixedPointBits() const {
775   // Check that the number of fractional and integral bits (and maybe sign) can
776   // fit into the bits given for a fixed point type.
777   assert(ShortAccumScale + getShortAccumIBits() + 1 <= ShortAccumWidth);
778   assert(AccumScale + getAccumIBits() + 1 <= AccumWidth);
779   assert(LongAccumScale + getLongAccumIBits() + 1 <= LongAccumWidth);
780   assert(getUnsignedShortAccumScale() + getUnsignedShortAccumIBits() <=
781          ShortAccumWidth);
782   assert(getUnsignedAccumScale() + getUnsignedAccumIBits() <= AccumWidth);
783   assert(getUnsignedLongAccumScale() + getUnsignedLongAccumIBits() <=
784          LongAccumWidth);
785 
786   assert(getShortFractScale() + 1 <= ShortFractWidth);
787   assert(getFractScale() + 1 <= FractWidth);
788   assert(getLongFractScale() + 1 <= LongFractWidth);
789   assert(getUnsignedShortFractScale() <= ShortFractWidth);
790   assert(getUnsignedFractScale() <= FractWidth);
791   assert(getUnsignedLongFractScale() <= LongFractWidth);
792 
793   // Each unsigned fract type has either the same number of fractional bits
794   // as, or one more fractional bit than, its corresponding signed fract type.
795   assert(getShortFractScale() == getUnsignedShortFractScale() ||
796          getShortFractScale() == getUnsignedShortFractScale() - 1);
797   assert(getFractScale() == getUnsignedFractScale() ||
798          getFractScale() == getUnsignedFractScale() - 1);
799   assert(getLongFractScale() == getUnsignedLongFractScale() ||
800          getLongFractScale() == getUnsignedLongFractScale() - 1);
801 
802   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
803   // fractional bits is nondecreasing for each of the following sets of
804   // fixed-point types:
805   // - signed fract types
806   // - unsigned fract types
807   // - signed accum types
808   // - unsigned accum types.
809   assert(getLongFractScale() >= getFractScale() &&
810          getFractScale() >= getShortFractScale());
811   assert(getUnsignedLongFractScale() >= getUnsignedFractScale() &&
812          getUnsignedFractScale() >= getUnsignedShortFractScale());
813   assert(LongAccumScale >= AccumScale && AccumScale >= ShortAccumScale);
814   assert(getUnsignedLongAccumScale() >= getUnsignedAccumScale() &&
815          getUnsignedAccumScale() >= getUnsignedShortAccumScale());
816 
817   // When arranged in order of increasing rank (see 6.3.1.3a), the number of
818   // integral bits is nondecreasing for each of the following sets of
819   // fixed-point types:
820   // - signed accum types
821   // - unsigned accum types
822   assert(getLongAccumIBits() >= getAccumIBits() &&
823          getAccumIBits() >= getShortAccumIBits());
824   assert(getUnsignedLongAccumIBits() >= getUnsignedAccumIBits() &&
825          getUnsignedAccumIBits() >= getUnsignedShortAccumIBits());
826 
827   // Each signed accum type has at least as many integral bits as its
828   // corresponding unsigned accum type.
829   assert(getShortAccumIBits() >= getUnsignedShortAccumIBits());
830   assert(getAccumIBits() >= getUnsignedAccumIBits());
831   assert(getLongAccumIBits() >= getUnsignedLongAccumIBits());
832 }
833 
834 void TargetInfo::copyAuxTarget(const TargetInfo *Aux) {
835   auto *Target = static_cast<TransferrableTargetInfo*>(this);
836   auto *Src = static_cast<const TransferrableTargetInfo*>(Aux);
837   *Target = *Src;
838 }
839