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