Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CodeGenTypes.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
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 is the code that handles AST -> LLVM type lowering.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenTypes.h"
14
#include "CGCXXABI.h"
15
#include "CGCall.h"
16
#include "CGOpenCLRuntime.h"
17
#include "CGRecordLayout.h"
18
#include "TargetInfo.h"
19
#include "clang/AST/ASTContext.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclObjC.h"
22
#include "clang/AST/Expr.h"
23
#include "clang/AST/RecordLayout.h"
24
#include "clang/CodeGen/CGFunctionInfo.h"
25
#include "llvm/IR/DataLayout.h"
26
#include "llvm/IR/DerivedTypes.h"
27
#include "llvm/IR/Module.h"
28
29
using namespace clang;
30
using namespace CodeGen;
31
32
CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
33
34.2k
  : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
34
34.2k
    Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
35
34.2k
    TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
36
34.2k
  SkippedLayout = false;
37
34.2k
  LongDoubleReferenced = false;
38
34.2k
}
39
40
34.2k
CodeGenTypes::~CodeGenTypes() {
41
34.2k
  for (llvm::FoldingSet<CGFunctionInfo>::iterator
42
260k
       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
43
225k
    delete &*I++;
44
34.2k
}
45
46
21.2k
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
47
21.2k
  return CGM.getCodeGenOpts();
48
21.2k
}
49
50
void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
51
                                     llvm::StructType *Ty,
52
121k
                                     StringRef suffix) {
53
121k
  SmallString<256> TypeName;
54
121k
  llvm::raw_svector_ostream OS(TypeName);
55
121k
  OS << RD->getKindName() << '.';
56
57
  // FIXME: We probably want to make more tweaks to the printing policy. For
58
  // example, we should probably enable PrintCanonicalTypes and
59
  // FullyQualifiedNames.
60
121k
  PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
61
121k
  Policy.SuppressInlineNamespace = false;
62
63
  // Name the codegen type after the typedef name
64
  // if there is no tag type name available
65
121k
  if (RD->getIdentifier()) {
66
    // FIXME: We should not have to check for a null decl context here.
67
    // Right now we do it because the implicit Obj-C decls don't have one.
68
114k
    if (RD->getDeclContext())
69
114k
      RD->printQualifiedName(OS, Policy);
70
0
    else
71
0
      RD->printName(OS, Policy);
72
114k
  } else 
if (const TypedefNameDecl *7.38k
TDD7.38k
= RD->getTypedefNameForAnonDecl()) {
73
    // FIXME: We should not have to check for a null decl context here.
74
    // Right now we do it because the implicit Obj-C decls don't have one.
75
1.43k
    if (TDD->getDeclContext())
76
1.43k
      TDD->printQualifiedName(OS, Policy);
77
0
    else
78
0
      TDD->printName(OS);
79
1.43k
  } else
80
5.95k
    OS << "anon";
81
82
121k
  if (!suffix.empty())
83
8.93k
    OS << suffix;
84
85
121k
  Ty->setName(OS.str());
86
121k
}
87
88
/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
89
/// ConvertType in that it is used to convert to the memory representation for
90
/// a type.  For example, the scalar representation for _Bool is i1, but the
91
/// memory representation is usually i8 or i32, depending on the target.
92
3.42M
llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
93
3.42M
  if (T->isConstantMatrixType()) {
94
1.04k
    const Type *Ty = Context.getCanonicalType(T).getTypePtr();
95
1.04k
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
96
1.04k
    return llvm::ArrayType::get(ConvertType(MT->getElementType()),
97
1.04k
                                MT->getNumRows() * MT->getNumColumns());
98
1.04k
  }
99
100
3.42M
  llvm::Type *R = ConvertType(T);
101
102
  // Check for the boolean vector case.
103
3.42M
  if (T->isExtVectorBoolType()) {
104
43
    auto *FixedVT = cast<llvm::FixedVectorType>(R);
105
    // Pad to at least one byte.
106
43
    uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8);
107
43
    return llvm::IntegerType::get(FixedVT->getContext(), BytePadded);
108
43
  }
109
110
  // If this is a bool type, or a bit-precise integer type in a bitfield
111
  // representation, map this integer to the target-specified size.
112
3.42M
  if ((ForBitField && 
T->isBitIntType()146
) ||
113
3.42M
      
(3.42M
!T->isBitIntType()3.42M
&&
R->isIntegerTy(1)3.42M
))
114
25.3k
    return llvm::IntegerType::get(getLLVMContext(),
115
25.3k
                                  (unsigned)Context.getTypeSize(T));
116
117
  // Else, don't map it.
118
3.40M
  return R;
119
3.42M
}
120
121
/// isRecordLayoutComplete - Return true if the specified type is already
122
/// completely laid out.
123
0
bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
124
0
  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
125
0
  RecordDeclTypes.find(Ty);
126
0
  return I != RecordDeclTypes.end() && !I->second->isOpaque();
127
0
}
128
129
/// isFuncParamTypeConvertible - Return true if the specified type in a
130
/// function parameter or result position can be converted to an IR type at this
131
/// point. This boils down to being whether it is complete.
132
119k
bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
133
  // Some ABIs cannot have their member pointers represented in IR unless
134
  // certain circumstances have been reached.
135
119k
  if (const auto *MPT = Ty->getAs<MemberPointerType>())
136
60
    return getCXXABI().isMemberPointerConvertible(MPT);
137
138
  // If this isn't a tagged type, we can convert it!
139
119k
  const TagType *TT = Ty->getAs<TagType>();
140
119k
  if (!TT) 
return true110k
;
141
142
  // Incomplete types cannot be converted.
143
9.19k
  return !TT->isIncompleteType();
144
119k
}
145
146
147
/// Code to verify a given function type is complete, i.e. the return type
148
/// and all of the parameter types are complete.  Also check to see if we are in
149
/// a RS_StructPointer context, and if so whether any struct types have been
150
/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
151
/// that cannot be converted to an IR type.
152
54.4k
bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
153
54.4k
  if (!isFuncParamTypeConvertible(FT->getReturnType()))
154
14
    return false;
155
156
54.4k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
157
118k
    
for (unsigned i = 0, e = FPT->getNumParams(); 54.0k
i != e;
i++64.8k
)
158
64.9k
      if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
159
71
        return false;
160
161
54.3k
  return true;
162
54.4k
}
163
164
/// UpdateCompletedType - When we find the full definition for a TagDecl,
165
/// replace the 'opaque' type we previously made for it if applicable.
166
2.07M
void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
167
  // If this is an enum being completed, then we flush all non-struct types from
168
  // the cache.  This allows function types and other things that may be derived
169
  // from the enum to be recomputed.
170
2.07M
  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
171
    // Only flush the cache if we've actually already converted this type.
172
460k
    if (TypeCache.count(ED->getTypeForDecl())) {
173
      // Okay, we formed some types based on this.  We speculated that the enum
174
      // would be lowered to i32, so we only need to flush the cache if this
175
      // didn't happen.
176
0
      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
177
0
        TypeCache.clear();
178
0
    }
179
    // If necessary, provide the full definition of a type only used with a
180
    // declaration so far.
181
460k
    if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
182
450k
      DI->completeType(ED);
183
460k
    return;
184
460k
  }
185
186
  // If we completed a RecordDecl that we previously used and converted to an
187
  // anonymous type, then go ahead and complete it now.
188
1.61M
  const RecordDecl *RD = cast<RecordDecl>(TD);
189
1.61M
  if (RD->isDependentType()) 
return440k
;
190
191
  // Only complete it if we converted it already.  If we haven't converted it
192
  // yet, we'll just do it lazily.
193
1.17M
  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
194
534
    ConvertRecordDeclType(RD);
195
196
  // If necessary, provide the full definition of a type only used with a
197
  // declaration so far.
198
1.17M
  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
199
1.07M
    DI->completeType(RD);
200
1.17M
}
201
202
544
void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
203
544
  QualType T = Context.getRecordType(RD);
204
544
  T = Context.getCanonicalType(T);
205
206
544
  const Type *Ty = T.getTypePtr();
207
544
  if (RecordsWithOpaqueMemberPointers.count(Ty)) {
208
0
    TypeCache.clear();
209
0
    RecordsWithOpaqueMemberPointers.clear();
210
0
  }
211
544
}
212
213
static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
214
                                    const llvm::fltSemantics &format,
215
11.9k
                                    bool UseNativeHalf = false) {
216
11.9k
  if (&format == &llvm::APFloat::IEEEhalf()) {
217
1.08k
    if (UseNativeHalf)
218
1.08k
      return llvm::Type::getHalfTy(VMContext);
219
3
    else
220
3
      return llvm::Type::getInt16Ty(VMContext);
221
1.08k
  }
222
10.8k
  if (&format == &llvm::APFloat::BFloat())
223
404
    return llvm::Type::getBFloatTy(VMContext);
224
10.4k
  if (&format == &llvm::APFloat::IEEEsingle())
225
4.90k
    return llvm::Type::getFloatTy(VMContext);
226
5.54k
  if (&format == &llvm::APFloat::IEEEdouble())
227
5.07k
    return llvm::Type::getDoubleTy(VMContext);
228
470
  if (&format == &llvm::APFloat::IEEEquad())
229
109
    return llvm::Type::getFP128Ty(VMContext);
230
361
  if (&format == &llvm::APFloat::PPCDoubleDouble())
231
41
    return llvm::Type::getPPC_FP128Ty(VMContext);
232
320
  if (&format == &llvm::APFloat::x87DoubleExtended())
233
320
    return llvm::Type::getX86_FP80Ty(VMContext);
234
0
  llvm_unreachable("Unknown float format!");
235
0
}
236
237
46.9k
llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
238
46.9k
  assert(QFT.isCanonical());
239
46.9k
  const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
240
  // First, check whether we can build the full function type.  If the
241
  // function type depends on an incomplete type (e.g. a struct or enum), we
242
  // cannot lower the function type.
243
46.9k
  if (!isFuncTypeConvertible(FT)) {
244
    // This function's type depends on an incomplete tag type.
245
246
    // Force conversion of all the relevant record types, to make sure
247
    // we re-convert the FunctionType when appropriate.
248
20
    if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
249
4
      ConvertRecordDeclType(RT->getDecl());
250
20
    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
251
37
      
for (unsigned i = 0, e = FPT->getNumParams(); 19
i != e;
i++18
)
252
18
        if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
253
13
          ConvertRecordDeclType(RT->getDecl());
254
255
20
    SkippedLayout = true;
256
257
    // Return a placeholder type.
258
20
    return llvm::StructType::get(getLLVMContext());
259
20
  }
260
261
  // The function type can be built; call the appropriate routines to
262
  // build it.
263
46.9k
  const CGFunctionInfo *FI;
264
46.9k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
265
46.6k
    FI = &arrangeFreeFunctionType(
266
46.6k
        CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
267
46.6k
  } else {
268
335
    const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
269
335
    FI = &arrangeFreeFunctionType(
270
335
        CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
271
335
  }
272
273
46.9k
  llvm::Type *ResultType = nullptr;
274
  // If there is something higher level prodding our CGFunctionInfo, then
275
  // don't recurse into it again.
276
46.9k
  if (FunctionsBeingProcessed.count(FI)) {
277
278
0
    ResultType = llvm::StructType::get(getLLVMContext());
279
0
    SkippedLayout = true;
280
46.9k
  } else {
281
282
    // Otherwise, we're good to go, go ahead and convert it.
283
46.9k
    ResultType = GetFunctionType(*FI);
284
46.9k
  }
285
286
46.9k
  return ResultType;
287
46.9k
}
288
289
/// ConvertType - Convert the specified type to its LLVM form.
290
8.11M
llvm::Type *CodeGenTypes::ConvertType(QualType T) {
291
8.11M
  T = Context.getCanonicalType(T);
292
293
8.11M
  const Type *Ty = T.getTypePtr();
294
295
  // For the device-side compilation, CUDA device builtin surface/texture types
296
  // may be represented in different types.
297
8.11M
  if (Context.getLangOpts().CUDAIsDevice) {
298
38.9k
    if (T->isCUDADeviceBuiltinSurfaceType()) {
299
0
      if (auto *Ty = CGM.getTargetCodeGenInfo()
300
0
                         .getCUDADeviceBuiltinSurfaceDeviceType())
301
0
        return Ty;
302
38.9k
    } else if (T->isCUDADeviceBuiltinTextureType()) {
303
0
      if (auto *Ty = CGM.getTargetCodeGenInfo()
304
0
                         .getCUDADeviceBuiltinTextureDeviceType())
305
0
        return Ty;
306
0
    }
307
38.9k
  }
308
309
  // RecordTypes are cached and processed specially.
310
8.11M
  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
311
717k
    return ConvertRecordDeclType(RT->getDecl());
312
313
7.39M
  llvm::Type *CachedType = nullptr;
314
7.39M
  auto TCI = TypeCache.find(Ty);
315
7.39M
  if (TCI != TypeCache.end())
316
6.43M
    CachedType = TCI->second;
317
    // With expensive checks, check that the type we compute matches the
318
    // cached type.
319
7.39M
#ifndef EXPENSIVE_CHECKS
320
7.39M
  if (CachedType)
321
6.43M
    return CachedType;
322
965k
#endif
323
324
  // If we don't have it in the cache, convert it now.
325
965k
  llvm::Type *ResultType = nullptr;
326
965k
  switch (Ty->getTypeClass()) {
327
0
  case Type::Record: // Handled above.
328
0
#define TYPE(Class, Base)
329
0
#define ABSTRACT_TYPE(Class, Base)
330
0
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
331
0
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
332
0
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
333
0
#include "clang/AST/TypeNodes.inc"
334
0
    llvm_unreachable("Non-canonical or dependent types aren't possible.");
335
336
647k
  case Type::Builtin: {
337
647k
    switch (cast<BuiltinType>(Ty)->getKind()) {
338
465
    case BuiltinType::Void:
339
560
    case BuiltinType::ObjCId:
340
560
    case BuiltinType::ObjCClass:
341
654
    case BuiltinType::ObjCSel:
342
      // LLVM void type can only be used as the result of a function call.  Just
343
      // map to the same as char.
344
654
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
345
654
      break;
346
347
3.22k
    case BuiltinType::Bool:
348
      // Note that we always return bool as i1 for use as a scalar type.
349
3.22k
      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
350
3.22k
      break;
351
352
6.58k
    case BuiltinType::Char_S:
353
6.63k
    case BuiltinType::Char_U:
354
8.55k
    case BuiltinType::SChar:
355
13.3k
    case BuiltinType::UChar:
356
28.8k
    case BuiltinType::Short:
357
30.8k
    case BuiltinType::UShort:
358
52.8k
    case BuiltinType::Int:
359
62.2k
    case BuiltinType::UInt:
360
78.5k
    case BuiltinType::Long:
361
87.1k
    case BuiltinType::ULong:
362
88.9k
    case BuiltinType::LongLong:
363
91.0k
    case BuiltinType::ULongLong:
364
91.0k
    case BuiltinType::WChar_S:
365
91.0k
    case BuiltinType::WChar_U:
366
91.1k
    case BuiltinType::Char8:
367
91.1k
    case BuiltinType::Char16:
368
91.1k
    case BuiltinType::Char32:
369
91.1k
    case BuiltinType::ShortAccum:
370
91.2k
    case BuiltinType::Accum:
371
91.2k
    case BuiltinType::LongAccum:
372
91.2k
    case BuiltinType::UShortAccum:
373
91.2k
    case BuiltinType::UAccum:
374
91.2k
    case BuiltinType::ULongAccum:
375
91.2k
    case BuiltinType::ShortFract:
376
91.3k
    case BuiltinType::Fract:
377
91.3k
    case BuiltinType::LongFract:
378
91.3k
    case BuiltinType::UShortFract:
379
91.3k
    case BuiltinType::UFract:
380
91.3k
    case BuiltinType::ULongFract:
381
91.3k
    case BuiltinType::SatShortAccum:
382
91.4k
    case BuiltinType::SatAccum:
383
91.4k
    case BuiltinType::SatLongAccum:
384
91.4k
    case BuiltinType::SatUShortAccum:
385
91.4k
    case BuiltinType::SatUAccum:
386
91.4k
    case BuiltinType::SatULongAccum:
387
91.4k
    case BuiltinType::SatShortFract:
388
91.5k
    case BuiltinType::SatFract:
389
91.5k
    case BuiltinType::SatLongFract:
390
91.5k
    case BuiltinType::SatUShortFract:
391
91.5k
    case BuiltinType::SatUFract:
392
91.5k
    case BuiltinType::SatULongFract:
393
91.5k
      ResultType = llvm::IntegerType::get(getLLVMContext(),
394
91.5k
                                 static_cast<unsigned>(Context.getTypeSize(T)));
395
91.5k
      break;
396
397
113
    case BuiltinType::Float16:
398
113
      ResultType =
399
113
          getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
400
113
                           /* UseNativeHalf = */ true);
401
113
      break;
402
403
988
    case BuiltinType::Half:
404
      // Half FP can either be storage-only (lowered to i16) or native.
405
988
      ResultType = getTypeForFormat(
406
988
          getLLVMContext(), Context.getFloatTypeSemantics(T),
407
988
          Context.getLangOpts().NativeHalfType ||
408
988
              
!Context.getTargetInfo().useFP16ConversionIntrinsics()913
);
409
988
      break;
410
496
    case BuiltinType::LongDouble:
411
496
      LongDoubleReferenced = true;
412
496
      LLVM_FALLTHROUGH;
413
900
    case BuiltinType::BFloat16:
414
5.78k
    case BuiltinType::Float:
415
10.8k
    case BuiltinType::Double:
416
10.8k
    case BuiltinType::Float128:
417
10.8k
    case BuiltinType::Ibm128:
418
10.8k
      ResultType = getTypeForFormat(getLLVMContext(),
419
10.8k
                                    Context.getFloatTypeSemantics(T),
420
10.8k
                                    /* UseNativeHalf = */ false);
421
10.8k
      break;
422
423
612
    case BuiltinType::NullPtr:
424
      // Model std::nullptr_t as i8*
425
612
      ResultType = llvm::PointerType::getUnqual(getLLVMContext());
426
612
      break;
427
428
78
    case BuiltinType::UInt128:
429
177
    case BuiltinType::Int128:
430
177
      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
431
177
      break;
432
433
0
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
434
1.06k
    case BuiltinType::Id:
435
1.06k
#include 
"clang/Basic/OpenCLImageTypes.def"177
436
1.06k
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
437
1.06k
    
case BuiltinType::Id:546
438
1.06k
#include 
"clang/Basic/OpenCLExtensionTypes.def"39
439
546
    case BuiltinType::OCLSampler:
440
62
    case BuiltinType::OCLEvent:
441
73
    case BuiltinType::OCLClkEvent:
442
94
    case BuiltinType::OCLQueue:
443
102
    case BuiltinType::OCLReserveID:
444
102
      ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
445
102
      break;
446
28.0k
    case BuiltinType::SveInt8:
447
54.1k
    case BuiltinType::SveUint8:
448
55.6k
    case BuiltinType::SveInt8x2:
449
57.2k
    case BuiltinType::SveUint8x2:
450
58.1k
    case BuiltinType::SveInt8x3:
451
59.1k
    case BuiltinType::SveUint8x3:
452
60.6k
    case BuiltinType::SveInt8x4:
453
62.1k
    case BuiltinType::SveUint8x4:
454
96.7k
    case BuiltinType::SveInt16:
455
126k
    case BuiltinType::SveUint16:
456
128k
    case BuiltinType::SveInt16x2:
457
129k
    case BuiltinType::SveUint16x2:
458
130k
    case BuiltinType::SveInt16x3:
459
131k
    case BuiltinType::SveUint16x3:
460
133k
    case BuiltinType::SveInt16x4:
461
134k
    case BuiltinType::SveUint16x4:
462
176k
    case BuiltinType::SveInt32:
463
217k
    case BuiltinType::SveUint32:
464
219k
    case BuiltinType::SveInt32x2:
465
221k
    case BuiltinType::SveUint32x2:
466
222k
    case BuiltinType::SveInt32x3:
467
223k
    case BuiltinType::SveUint32x3:
468
224k
    case BuiltinType::SveInt32x4:
469
226k
    case BuiltinType::SveUint32x4:
470
265k
    case BuiltinType::SveInt64:
471
308k
    case BuiltinType::SveUint64:
472
309k
    case BuiltinType::SveInt64x2:
473
311k
    case BuiltinType::SveUint64x2:
474
312k
    case BuiltinType::SveInt64x3:
475
313k
    case BuiltinType::SveUint64x3:
476
315k
    case BuiltinType::SveInt64x4:
477
317k
    case BuiltinType::SveUint64x4:
478
447k
    case BuiltinType::SveBool:
479
447k
    case BuiltinType::SveBoolx2:
480
447k
    case BuiltinType::SveBoolx4:
481
468k
    case BuiltinType::SveFloat16:
482
470k
    case BuiltinType::SveFloat16x2:
483
471k
    case BuiltinType::SveFloat16x3:
484
472k
    case BuiltinType::SveFloat16x4:
485
495k
    case BuiltinType::SveFloat32:
486
497k
    case BuiltinType::SveFloat32x2:
487
498k
    case BuiltinType::SveFloat32x3:
488
499k
    case BuiltinType::SveFloat32x4:
489
520k
    case BuiltinType::SveFloat64:
490
522k
    case BuiltinType::SveFloat64x2:
491
523k
    case BuiltinType::SveFloat64x3:
492
524k
    case BuiltinType::SveFloat64x4:
493
530k
    case BuiltinType::SveBFloat16:
494
531k
    case BuiltinType::SveBFloat16x2:
495
532k
    case BuiltinType::SveBFloat16x3:
496
533k
    case BuiltinType::SveBFloat16x4: {
497
533k
      ASTContext::BuiltinVectorTypeInfo Info =
498
533k
          Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
499
533k
      return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
500
533k
                                           Info.EC.getKnownMinValue() *
501
533k
                                               Info.NumVectors);
502
532k
    }
503
5.27k
    case BuiltinType::SveCount:
504
5.27k
      return llvm::TargetExtType::get(getLLVMContext(), "aarch64.svcount");
505
0
#define PPC_VECTOR_TYPE(Name, Id, Size) \
506
22
    case BuiltinType::Id: \
507
22
      ResultType = \
508
22
        llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
509
22
      break;
510
5.27k
#include "clang/Basic/PPCTypes.def"
511
13.3k
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
512
13.3k
#include 
"clang/Basic/RISCVVTypes.def"11
513
13.3k
      {
514
13.3k
        ASTContext::BuiltinVectorTypeInfo Info =
515
13.3k
            Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
516
        // Tuple types are expressed as aggregregate types of the same scalable
517
        // vector type (e.g. vint32m1x2_t is two vint32m1_t, which is {<vscale x
518
        // 2 x i32>, <vscale x 2 x i32>}).
519
13.3k
        if (
Info.NumVectors != 148
) {
520
0
          llvm::Type *EltTy = llvm::ScalableVectorType::get(
521
0
              ConvertType(Info.ElementType), Info.EC.getKnownMinValue());
522
0
          llvm::SmallVector<llvm::Type *, 4> EltTys(Info.NumVectors, EltTy);
523
0
          return llvm::StructType::get(getLLVMContext(), EltTys);
524
0
        }
525
48
        return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
526
48
                                             Info.EC.getKnownMinValue() *
527
48
                                                 Info.NumVectors);
528
13.3k
      }
529
0
#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS)                  \
530
3
  case BuiltinType::Id: {                                                      \
531
3
    if (BuiltinType::Id == BuiltinType::WasmExternRef)                         \
532
3
      ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \
533
3
    else                                                                       \
534
3
      
llvm_unreachable0
("Unexpected wasm reference builtin type!"); \
535
3
  } break;
536
48
#include "clang/Basic/WebAssemblyReferenceTypes.def"
537
0
    case BuiltinType::Dependent:
538
0
#define BUILTIN_TYPE(Id, SingletonId)
539
0
#define PLACEHOLDER_TYPE(Id, SingletonId) \
540
0
    case BuiltinType::Id:
541
0
#include "clang/AST/BuiltinTypes.def"
542
0
      llvm_unreachable("Unexpected placeholder builtin type!");
543
647k
    }
544
108k
    break;
545
647k
  }
546
108k
  case Type::Auto:
547
0
  case Type::DeducedTemplateSpecialization:
548
0
    llvm_unreachable("Unexpected undeduced type!");
549
656
  case Type::Complex: {
550
656
    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
551
656
    ResultType = llvm::StructType::get(EltTy, EltTy);
552
656
    break;
553
0
  }
554
27.5k
  case Type::LValueReference:
555
30.3k
  case Type::RValueReference: {
556
30.3k
    const ReferenceType *RTy = cast<ReferenceType>(Ty);
557
30.3k
    QualType ETy = RTy->getPointeeType();
558
30.3k
    unsigned AS = getTargetAddressSpace(ETy);
559
30.3k
    ResultType = llvm::PointerType::get(getLLVMContext(), AS);
560
30.3k
    break;
561
27.5k
  }
562
143k
  case Type::Pointer: {
563
143k
    const PointerType *PTy = cast<PointerType>(Ty);
564
143k
    QualType ETy = PTy->getPointeeType();
565
143k
    unsigned AS = getTargetAddressSpace(ETy);
566
143k
    ResultType = llvm::PointerType::get(getLLVMContext(), AS);
567
143k
    break;
568
27.5k
  }
569
570
2.90k
  case Type::VariableArray: {
571
2.90k
    const VariableArrayType *A = cast<VariableArrayType>(Ty);
572
2.90k
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
573
2.90k
           "FIXME: We only handle trivial array types so far!");
574
    // VLAs resolve to the innermost element type; this matches
575
    // the return of alloca, and there isn't any obviously better choice.
576
2.90k
    ResultType = ConvertTypeForMem(A->getElementType());
577
2.90k
    break;
578
2.90k
  }
579
148
  case Type::IncompleteArray: {
580
148
    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
581
148
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
582
148
           "FIXME: We only handle trivial array types so far!");
583
    // int X[] -> [0 x int], unless the element type is not sized.  If it is
584
    // unsized (e.g. an incomplete struct) just use [0 x i8].
585
148
    ResultType = ConvertTypeForMem(A->getElementType());
586
148
    if (!ResultType->isSized()) {
587
1
      SkippedLayout = true;
588
1
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
589
1
    }
590
148
    ResultType = llvm::ArrayType::get(ResultType, 0);
591
148
    break;
592
148
  }
593
46.8k
  case Type::ConstantArray: {
594
46.8k
    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
595
46.8k
    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
596
597
    // Lower arrays of undefined struct type to arrays of i8 just to have a
598
    // concrete type.
599
46.8k
    if (!EltTy->isSized()) {
600
2
      SkippedLayout = true;
601
2
      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
602
2
    }
603
604
46.8k
    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
605
46.8k
    break;
606
148
  }
607
1.22k
  case Type::ExtVector:
608
6.29k
  case Type::Vector: {
609
6.29k
    const auto *VT = cast<VectorType>(Ty);
610
    // An ext_vector_type of Bool is really a vector of bits.
611
6.29k
    llvm::Type *IRElemTy = VT->isExtVectorBoolType()
612
6.29k
                               ? 
llvm::Type::getInt1Ty(getLLVMContext())27
613
6.29k
                               : 
ConvertType(VT->getElementType())6.26k
;
614
6.29k
    ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
615
6.29k
    break;
616
1.22k
  }
617
91
  case Type::ConstantMatrix: {
618
91
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
619
91
    ResultType =
620
91
        llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
621
91
                                   MT->getNumRows() * MT->getNumColumns());
622
91
    break;
623
1.22k
  }
624
336
  case Type::FunctionNoProto:
625
46.9k
  case Type::FunctionProto:
626
46.9k
    ResultType = ConvertFunctionTypeInternal(T);
627
46.9k
    break;
628
99
  case Type::ObjCObject:
629
99
    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
630
99
    break;
631
632
132
  case Type::ObjCInterface: {
633
    // Objective-C interfaces are always opaque (outside of the
634
    // runtime, which can do whatever it likes); we never refine
635
    // these.
636
132
    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
637
132
    if (!T)
638
132
      T = llvm::StructType::create(getLLVMContext());
639
132
    ResultType = T;
640
132
    break;
641
336
  }
642
643
28.3k
  case Type::ObjCObjectPointer:
644
28.3k
    ResultType = llvm::PointerType::getUnqual(getLLVMContext());
645
28.3k
    break;
646
647
9.92k
  case Type::Enum: {
648
9.92k
    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
649
9.92k
    if (ED->isCompleteDefinition() || 
ED->isFixed()183
)
650
9.92k
      return ConvertType(ED->getIntegerType());
651
    // Return a placeholder 'i32' type.  This can be changed later when the
652
    // type is defined (see UpdateCompletedType), but is likely to be the
653
    // "right" answer.
654
1
    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
655
1
    break;
656
9.92k
  }
657
658
614
  case Type::BlockPointer: {
659
    // Block pointers lower to function type. For function type,
660
    // getTargetAddressSpace() returns default address space for
661
    // function pointer i.e. program address space. Therefore, for block
662
    // pointers, it is important to pass the pointee AST address space when
663
    // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
664
    // address space for data pointers and not function pointers.
665
614
    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
666
614
    unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace());
667
614
    ResultType = llvm::PointerType::get(getLLVMContext(), AS);
668
614
    break;
669
9.92k
  }
670
671
975
  case Type::MemberPointer: {
672
975
    auto *MPTy = cast<MemberPointerType>(Ty);
673
975
    if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
674
5
      auto *C = MPTy->getClass();
675
5
      auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr});
676
5
      if (Insertion.second)
677
5
        Insertion.first->second = llvm::StructType::create(getLLVMContext());
678
5
      ResultType = Insertion.first->second;
679
970
    } else {
680
970
      ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
681
970
    }
682
975
    break;
683
9.92k
  }
684
685
369
  case Type::Atomic: {
686
369
    QualType valueType = cast<AtomicType>(Ty)->getValueType();
687
369
    ResultType = ConvertTypeForMem(valueType);
688
689
    // Pad out to the inflated size if necessary.
690
369
    uint64_t valueSize = Context.getTypeSize(valueType);
691
369
    uint64_t atomicSize = Context.getTypeSize(Ty);
692
369
    if (valueSize != atomicSize) {
693
10
      assert(valueSize < atomicSize);
694
10
      llvm::Type *elts[] = {
695
10
        ResultType,
696
10
        llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
697
10
      };
698
10
      ResultType =
699
10
          llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts));
700
10
    }
701
369
    break;
702
369
  }
703
369
  case Type::Pipe: {
704
112
    ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
705
112
    break;
706
369
  }
707
437
  case Type::BitInt: {
708
437
    const auto &EIT = cast<BitIntType>(Ty);
709
437
    ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
710
437
    break;
711
369
  }
712
965k
  }
713
714
417k
  assert(ResultType && "Didn't convert a type?");
715
417k
  assert((!CachedType || CachedType == ResultType) &&
716
417k
         "Cached type doesn't match computed type");
717
718
417k
  TypeCache[Ty] = ResultType;
719
417k
  return ResultType;
720
417k
}
721
722
37
bool CodeGenModule::isPaddedAtomicType(QualType type) {
723
37
  return isPaddedAtomicType(type->castAs<AtomicType>());
724
37
}
725
726
37
bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
727
37
  return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
728
37
}
729
730
/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
731
744k
llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
732
  // TagDecl's are not necessarily unique, instead use the (clang)
733
  // type connected to the decl.
734
744k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
735
736
744k
  llvm::StructType *&Entry = RecordDeclTypes[Key];
737
738
  // If we don't have a StructType at all yet, create the forward declaration.
739
744k
  if (!Entry) {
740
112k
    Entry = llvm::StructType::create(getLLVMContext());
741
112k
    addRecordTypeName(RD, Entry, "");
742
112k
  }
743
744k
  llvm::StructType *Ty = Entry;
744
745
  // If this is still a forward declaration, or the LLVM type is already
746
  // complete, there's nothing more to do.
747
744k
  RD = RD->getDefinition();
748
744k
  if (!RD || 
!RD->isCompleteDefinition()744k
||
!Ty->isOpaque()744k
)
749
632k
    return Ty;
750
751
  // Force conversion of non-virtual base classes recursively.
752
112k
  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
753
80.5k
    for (const auto &I : CRD->bases()) {
754
21.6k
      if (I.isVirtual()) 
continue885
;
755
20.7k
      ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
756
20.7k
    }
757
80.5k
  }
758
759
  // Layout fields.
760
112k
  std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
761
112k
  CGRecordLayouts[Key] = std::move(Layout);
762
763
  // If this struct blocked a FunctionType conversion, then recompute whatever
764
  // was derived from that.
765
  // FIXME: This is hugely overconservative.
766
112k
  if (SkippedLayout)
767
27
    TypeCache.clear();
768
769
112k
  return Ty;
770
744k
}
771
772
/// getCGRecordLayout - Return record layout info for the given record decl.
773
const CGRecordLayout &
774
252k
CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
775
252k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
776
777
252k
  auto I = CGRecordLayouts.find(Key);
778
252k
  if (I != CGRecordLayouts.end())
779
246k
    return *I->second;
780
  // Compute the type information.
781
6.04k
  ConvertRecordDeclType(RD);
782
783
  // Now try again.
784
6.04k
  I = CGRecordLayouts.find(Key);
785
786
6.04k
  assert(I != CGRecordLayouts.end() &&
787
6.04k
         "Unable to find record layout information for type");
788
6.04k
  return *I->second;
789
6.04k
}
790
791
16
bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
792
16
  assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
793
16
  return isZeroInitializable(T);
794
16
}
795
796
232k
bool CodeGenTypes::isZeroInitializable(QualType T) {
797
232k
  if (T->getAs<PointerType>())
798
49.2k
    return Context.getTargetNullPointerValue(T) == 0;
799
800
182k
  if (const auto *AT = Context.getAsArrayType(T)) {
801
13.5k
    if (isa<IncompleteArrayType>(AT))
802
125
      return true;
803
13.4k
    if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
804
13.4k
      if (Context.getConstantArrayElementCount(CAT) == 0)
805
2.23k
        return true;
806
11.2k
    T = Context.getBaseElementType(T);
807
11.2k
  }
808
809
  // Records are non-zero-initializable if they contain any
810
  // non-zero-initializable subobjects.
811
180k
  if (const RecordType *RT = T->getAs<RecordType>()) {
812
25.2k
    const RecordDecl *RD = RT->getDecl();
813
25.2k
    return isZeroInitializable(RD);
814
25.2k
  }
815
816
  // We have to ask the ABI about member pointers.
817
155k
  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
818
310
    return getCXXABI().isZeroInitializable(MPT);
819
820
  // Everything else is okay.
821
155k
  return true;
822
155k
}
823
824
36.6k
bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
825
36.6k
  return getCGRecordLayout(RD).isZeroInitializable();
826
36.6k
}
827
828
1.04M
unsigned CodeGenTypes::getTargetAddressSpace(QualType T) const {
829
  // Return the address space for the type. If the type is a
830
  // function type without an address space qualifier, the
831
  // program address space is used. Otherwise, the target picks
832
  // the best address space based on the type information
833
1.04M
  return T->isFunctionType() && 
!T.hasAddressSpace()20.2k
834
1.04M
             ? 
getDataLayout().getProgramAddressSpace()20.2k
835
1.04M
             : 
getContext().getTargetAddressSpace(T.getAddressSpace())1.02M
;
836
1.04M
}