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