Coverage Report

Created: 2024-01-17 10:31

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