Coverage Report

Created: 2021-08-24 07:12

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