Coverage Report

Created: 2021-09-21 08:58

/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
using namespace clang;
29
using namespace CodeGen;
30
31
CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
32
  : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
33
    Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
34
46.8k
    TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
35
46.8k
  SkippedLayout = false;
36
46.8k
}
37
38
46.8k
CodeGenTypes::~CodeGenTypes() {
39
46.8k
  for (llvm::FoldingSet<CGFunctionInfo>::iterator
40
335k
       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
41
289k
    delete &*I++;
42
46.8k
}
43
44
20.3k
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
45
20.3k
  return CGM.getCodeGenOpts();
46
20.3k
}
47
48
void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
49
                                     llvm::StructType *Ty,
50
152k
                                     StringRef suffix) {
51
152k
  SmallString<256> TypeName;
52
152k
  llvm::raw_svector_ostream OS(TypeName);
53
152k
  OS << RD->getKindName() << '.';
54
55
  // FIXME: We probably want to make more tweaks to the printing policy. For
56
  // example, we should probably enable PrintCanonicalTypes and
57
  // FullyQualifiedNames.
58
152k
  PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
59
152k
  Policy.SuppressInlineNamespace = false;
60
61
  // Name the codegen type after the typedef name
62
  // if there is no tag type name available
63
152k
  if (RD->getIdentifier()) {
64
    // FIXME: We should not have to check for a null decl context here.
65
    // Right now we do it because the implicit Obj-C decls don't have one.
66
144k
    if (RD->getDeclContext())
67
144k
      RD->printQualifiedName(OS, Policy);
68
0
    else
69
0
      RD->printName(OS);
70
144k
  } else 
if (const TypedefNameDecl *7.63k
TDD7.63k
= RD->getTypedefNameForAnonDecl()) {
71
    // FIXME: We should not have to check for a null decl context here.
72
    // Right now we do it because the implicit Obj-C decls don't have one.
73
1.96k
    if (TDD->getDeclContext())
74
1.96k
      TDD->printQualifiedName(OS, Policy);
75
0
    else
76
0
      TDD->printName(OS);
77
1.96k
  } else
78
5.66k
    OS << "anon";
79
80
152k
  if (!suffix.empty())
81
8.40k
    OS << suffix;
82
83
152k
  Ty->setName(OS.str());
84
152k
}
85
86
/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
87
/// ConvertType in that it is used to convert to the memory representation for
88
/// a type.  For example, the scalar representation for _Bool is i1, but the
89
/// memory representation is usually i8 or i32, depending on the target.
90
4.15M
llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
91
4.15M
  if (T->isConstantMatrixType()) {
92
724
    const Type *Ty = Context.getCanonicalType(T).getTypePtr();
93
724
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
94
724
    return llvm::ArrayType::get(ConvertType(MT->getElementType()),
95
724
                                MT->getNumRows() * MT->getNumColumns());
96
724
  }
97
98
4.15M
  llvm::Type *R = ConvertType(T);
99
100
  // If this is a bool type, or an ExtIntType in a bitfield representation,
101
  // map this integer to the target-specified size.
102
4.15M
  if ((ForBitField && 
T->isExtIntType()117
) ||
103
4.15M
      
(4.15M
!T->isExtIntType()4.15M
&&
R->isIntegerTy(1)4.15M
))
104
15.3k
    return llvm::IntegerType::get(getLLVMContext(),
105
15.3k
                                  (unsigned)Context.getTypeSize(T));
106
107
  // Else, don't map it.
108
4.13M
  return R;
109
4.15M
}
110
111
/// isRecordLayoutComplete - Return true if the specified type is already
112
/// completely laid out.
113
600k
bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
114
600k
  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
115
600k
  RecordDeclTypes.find(Ty);
116
600k
  return I != RecordDeclTypes.end() && 
!I->second->isOpaque()35.7k
;
117
600k
}
118
119
static bool
120
isSafeToConvert(QualType T, CodeGenTypes &CGT,
121
                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
122
123
124
/// isSafeToConvert - Return true if it is safe to convert the specified record
125
/// decl to IR and lay it out, false if doing so would cause us to get into a
126
/// recursive compilation mess.
127
static bool
128
isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
129
602k
                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
130
  // If we have already checked this type (maybe the same type is used by-value
131
  // multiple times in multiple structure fields, don't check again.
132
602k
  if (!AlreadyChecked.insert(RD).second)
133
1.69k
    return true;
134
135
600k
  const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
136
137
  // If this type is already laid out, converting it is a noop.
138
600k
  if (CGT.isRecordLayoutComplete(Key)) 
return true2.47k
;
139
140
  // If this type is currently being laid out, we can't recursively compile it.
141
598k
  if (CGT.isRecordBeingLaidOut(Key))
142
653
    return false;
143
144
  // If this type would require laying out bases that are currently being laid
145
  // out, don't do it.  This includes virtual base classes which get laid out
146
  // when a class is translated, even though they aren't embedded by-value into
147
  // the class.
148
597k
  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
149
596k
    for (const auto &I : CRD->bases())
150
551k
      if (!isSafeToConvert(I.getType()->castAs<RecordType>()->getDecl(), CGT,
151
551k
                           AlreadyChecked))
152
23
        return false;
153
596k
  }
154
155
  // If this type would require laying out members that are currently being laid
156
  // out, don't do it.
157
597k
  for (const auto *I : RD->fields())
158
74.2k
    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
159
5
      return false;
160
161
  // If there are no problems, lets do it.
162
597k
  return true;
163
597k
}
164
165
/// isSafeToConvert - Return true if it is safe to convert this field type,
166
/// which requires the structure elements contained by-value to all be
167
/// recursively safe to convert.
168
static bool
169
isSafeToConvert(QualType T, CodeGenTypes &CGT,
170
80.0k
                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
171
  // Strip off atomic type sugar.
172
80.0k
  if (const auto *AT = T->getAs<AtomicType>())
173
639
    T = AT->getValueType();
174
175
  // If this is a record, check it.
176
80.0k
  if (const auto *RT = T->getAs<RecordType>())
177
17.5k
    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
178
179
  // If this is an array, check the elements, which are embedded inline.
180
62.5k
  if (const auto *AT = CGT.getContext().getAsArrayType(T))
181
5.84k
    return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
182
183
  // Otherwise, there is no concern about transforming this.  We only care about
184
  // things that are contained by-value in a structure that can have another
185
  // structure as a member.
186
56.7k
  return true;
187
62.5k
}
188
189
190
/// isSafeToConvert - Return true if it is safe to convert the specified record
191
/// decl to IR and lay it out, false if doing so would cause us to get into a
192
/// recursive compilation mess.
193
149k
static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
194
  // If no structs are being laid out, we can certainly do this one.
195
149k
  if (CGT.noRecordsBeingLaidOut()) 
return true115k
;
196
197
33.2k
  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
198
33.2k
  return isSafeToConvert(RD, CGT, AlreadyChecked);
199
149k
}
200
201
/// isFuncParamTypeConvertible - Return true if the specified type in a
202
/// function parameter or result position can be converted to an IR type at this
203
/// point.  This boils down to being whether it is complete, as well as whether
204
/// we've temporarily deferred expanding the type because we're in a recursive
205
/// context.
206
166k
bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
207
  // Some ABIs cannot have their member pointers represented in IR unless
208
  // certain circumstances have been reached.
209
166k
  if (const auto *MPT = Ty->getAs<MemberPointerType>())
210
57
    return getCXXABI().isMemberPointerConvertible(MPT);
211
212
  // If this isn't a tagged type, we can convert it!
213
166k
  const TagType *TT = Ty->getAs<TagType>();
214
166k
  if (!TT) 
return true159k
;
215
216
  // Incomplete types cannot be converted.
217
6.83k
  if (TT->isIncompleteType())
218
82
    return false;
219
220
  // If this is an enum, then it is always safe to convert.
221
6.75k
  const RecordType *RT = dyn_cast<RecordType>(TT);
222
6.75k
  if (!RT) 
return true545
;
223
224
  // Otherwise, we have to be careful.  If it is a struct that we're in the
225
  // process of expanding, then we can't convert the function type.  That's ok
226
  // though because we must be in a pointer context under the struct, so we can
227
  // just convert it to a dummy type.
228
  //
229
  // We decide this by checking whether ConvertRecordDeclType returns us an
230
  // opaque type for a struct that we know is defined.
231
6.20k
  return isSafeToConvert(RT->getDecl(), *this);
232
6.75k
}
233
234
235
/// Code to verify a given function type is complete, i.e. the return type
236
/// and all of the parameter types are complete.  Also check to see if we are in
237
/// a RS_StructPointer context, and if so whether any struct types have been
238
/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
239
/// that cannot be converted to an IR type.
240
69.7k
bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
241
69.7k
  if (!isFuncParamTypeConvertible(FT->getReturnType()))
242
17
    return false;
243
244
69.6k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
245
165k
    
for (unsigned i = 0, e = FPT->getNumParams(); 69.0k
i != e;
i++96.6k
)
246
96.6k
      if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
247
70
        return false;
248
249
69.6k
  return true;
250
69.6k
}
251
252
/// UpdateCompletedType - When we find the full definition for a TagDecl,
253
/// replace the 'opaque' type we previously made for it if applicable.
254
2.12M
void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
255
  // If this is an enum being completed, then we flush all non-struct types from
256
  // the cache.  This allows function types and other things that may be derived
257
  // from the enum to be recomputed.
258
2.12M
  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
259
    // Only flush the cache if we've actually already converted this type.
260
445k
    if (TypeCache.count(ED->getTypeForDecl())) {
261
      // Okay, we formed some types based on this.  We speculated that the enum
262
      // would be lowered to i32, so we only need to flush the cache if this
263
      // didn't happen.
264
1
      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
265
0
        TypeCache.clear();
266
1
    }
267
    // If necessary, provide the full definition of a type only used with a
268
    // declaration so far.
269
445k
    if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
270
437k
      DI->completeType(ED);
271
445k
    return;
272
445k
  }
273
274
  // If we completed a RecordDecl that we previously used and converted to an
275
  // anonymous type, then go ahead and complete it now.
276
1.68M
  const RecordDecl *RD = cast<RecordDecl>(TD);
277
1.68M
  if (RD->isDependentType()) 
return352k
;
278
279
  // Only complete it if we converted it already.  If we haven't converted it
280
  // yet, we'll just do it lazily.
281
1.32M
  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
282
658
    ConvertRecordDeclType(RD);
283
284
  // If necessary, provide the full definition of a type only used with a
285
  // declaration so far.
286
1.32M
  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
287
1.20M
    DI->completeType(RD);
288
1.32M
}
289
290
476
void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
291
476
  QualType T = Context.getRecordType(RD);
292
476
  T = Context.getCanonicalType(T);
293
294
476
  const Type *Ty = T.getTypePtr();
295
476
  if (RecordsWithOpaqueMemberPointers.count(Ty)) {
296
8
    TypeCache.clear();
297
8
    RecordsWithOpaqueMemberPointers.clear();
298
8
  }
299
476
}
300
301
static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
302
                                    const llvm::fltSemantics &format,
303
12.9k
                                    bool UseNativeHalf = false) {
304
12.9k
  if (&format == &llvm::APFloat::IEEEhalf()) {
305
850
    if (UseNativeHalf)
306
847
      return llvm::Type::getHalfTy(VMContext);
307
3
    else
308
3
      return llvm::Type::getInt16Ty(VMContext);
309
850
  }
310
12.1k
  if (&format == &llvm::APFloat::BFloat())
311
296
    return llvm::Type::getBFloatTy(VMContext);
312
11.8k
  if (&format == &llvm::APFloat::IEEEsingle())
313
5.93k
    return llvm::Type::getFloatTy(VMContext);
314
5.89k
  if (&format == &llvm::APFloat::IEEEdouble())
315
5.45k
    return llvm::Type::getDoubleTy(VMContext);
316
436
  if (&format == &llvm::APFloat::IEEEquad())
317
96
    return llvm::Type::getFP128Ty(VMContext);
318
340
  if (&format == &llvm::APFloat::PPCDoubleDouble())
319
51
    return llvm::Type::getPPC_FP128Ty(VMContext);
320
289
  if (&format == &llvm::APFloat::x87DoubleExtended())
321
289
    return llvm::Type::getX86_FP80Ty(VMContext);
322
0
  llvm_unreachable("Unknown float format!");
323
0
}
324
325
62.2k
llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
326
62.2k
  assert(QFT.isCanonical());
327
0
  const Type *Ty = QFT.getTypePtr();
328
62.2k
  const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
329
  // First, check whether we can build the full function type.  If the
330
  // function type depends on an incomplete type (e.g. a struct or enum), we
331
  // cannot lower the function type.
332
62.2k
  if (!isFuncTypeConvertible(FT)) {
333
    // This function's type depends on an incomplete tag type.
334
335
    // Force conversion of all the relevant record types, to make sure
336
    // we re-convert the FunctionType when appropriate.
337
23
    if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
338
8
      ConvertRecordDeclType(RT->getDecl());
339
23
    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
340
39
      
for (unsigned i = 0, e = FPT->getNumParams(); 21
i != e;
i++18
)
341
18
        if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
342
12
          ConvertRecordDeclType(RT->getDecl());
343
344
23
    SkippedLayout = true;
345
346
    // Return a placeholder type.
347
23
    return llvm::StructType::get(getLLVMContext());
348
23
  }
349
350
  // While we're converting the parameter types for a function, we don't want
351
  // to recursively convert any pointed-to structs.  Converting directly-used
352
  // structs is ok though.
353
62.2k
  if (!RecordsBeingLaidOut.insert(Ty).second) {
354
1
    SkippedLayout = true;
355
1
    return llvm::StructType::get(getLLVMContext());
356
1
  }
357
358
  // The function type can be built; call the appropriate routines to
359
  // build it.
360
62.2k
  const CGFunctionInfo *FI;
361
62.2k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
362
61.5k
    FI = &arrangeFreeFunctionType(
363
61.5k
        CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
364
61.5k
  } else {
365
647
    const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
366
647
    FI = &arrangeFreeFunctionType(
367
647
        CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
368
647
  }
369
370
62.2k
  llvm::Type *ResultType = nullptr;
371
  // If there is something higher level prodding our CGFunctionInfo, then
372
  // don't recurse into it again.
373
62.2k
  if (FunctionsBeingProcessed.count(FI)) {
374
375
4
    ResultType = llvm::StructType::get(getLLVMContext());
376
4
    SkippedLayout = true;
377
62.2k
  } else {
378
379
    // Otherwise, we're good to go, go ahead and convert it.
380
62.2k
    ResultType = GetFunctionType(*FI);
381
62.2k
  }
382
383
62.2k
  RecordsBeingLaidOut.erase(Ty);
384
385
62.2k
  if (SkippedLayout)
386
57
    TypeCache.clear();
387
388
62.2k
  if (RecordsBeingLaidOut.empty())
389
57.7k
    
while (57.6k
!DeferredRecords.empty())
390
102
      ConvertRecordDeclType(DeferredRecords.pop_back_val());
391
62.2k
  return ResultType;
392
62.2k
}
393
394
/// ConvertType - Convert the specified type to its LLVM form.
395
11.4M
llvm::Type *CodeGenTypes::ConvertType(QualType T) {
396
11.4M
  T = Context.getCanonicalType(T);
397
398
11.4M
  const Type *Ty = T.getTypePtr();
399
400
  // For the device-side compilation, CUDA device builtin surface/texture types
401
  // may be represented in different types.
402
11.4M
  if (Context.getLangOpts().CUDAIsDevice) {
403
14.4k
    if (T->isCUDADeviceBuiltinSurfaceType()) {
404
6
      if (auto *Ty = CGM.getTargetCodeGenInfo()
405
6
                         .getCUDADeviceBuiltinSurfaceDeviceType())
406
6
        return Ty;
407
14.4k
    } else if (T->isCUDADeviceBuiltinTextureType()) {
408
12
      if (auto *Ty = CGM.getTargetCodeGenInfo()
409
12
                         .getCUDADeviceBuiltinTextureDeviceType())
410
12
        return Ty;
411
12
    }
412
14.4k
  }
413
414
  // RecordTypes are cached and processed specially.
415
11.4M
  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
416
436k
    return ConvertRecordDeclType(RT->getDecl());
417
418
  // See if type is already cached.
419
11.0M
  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
420
  // If type is found in map then use it. Otherwise, convert type T.
421
11.0M
  if (TCI != TypeCache.end())
422
8.81M
    return TCI->second;
423
424
  // If we don't have it in the cache, convert it now.
425
2.20M
  llvm::Type *ResultType = nullptr;
426
2.20M
  switch (Ty->getTypeClass()) {
427
0
  case Type::Record: // Handled above.
428
0
#define TYPE(Class, Base)
429
0
#define ABSTRACT_TYPE(Class, Base)
430
0
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
431
0
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
432
0
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
433
0
#include "clang/AST/TypeNodes.inc"
434
0
    llvm_unreachable("Non-canonical or dependent types aren't possible.");
435
436
1.71M
  case Type::Builtin: {
437
1.71M
    switch (cast<BuiltinType>(Ty)->getKind()) {
438
32.9k
    case BuiltinType::Void:
439
59.3k
    case BuiltinType::ObjCId:
440
85.7k
    case BuiltinType::ObjCClass:
441
112k
    case BuiltinType::ObjCSel:
442
      // LLVM void type can only be used as the result of a function call.  Just
443
      // map to the same as char.
444
112k
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
445
112k
      break;
446
447
3.80k
    case BuiltinType::Bool:
448
      // Note that we always return bool as i1 for use as a scalar type.
449
3.80k
      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
450
3.80k
      break;
451
452
10.6k
    case BuiltinType::Char_S:
453
10.7k
    case BuiltinType::Char_U:
454
12.9k
    case BuiltinType::SChar:
455
18.8k
    case BuiltinType::UChar:
456
48.4k
    case BuiltinType::Short:
457
50.6k
    case BuiltinType::UShort:
458
74.8k
    case BuiltinType::Int:
459
86.2k
    case BuiltinType::UInt:
460
116k
    case BuiltinType::Long:
461
126k
    case BuiltinType::ULong:
462
128k
    case BuiltinType::LongLong:
463
131k
    case BuiltinType::ULongLong:
464
131k
    case BuiltinType::WChar_S:
465
131k
    case BuiltinType::WChar_U:
466
131k
    case BuiltinType::Char8:
467
131k
    case BuiltinType::Char16:
468
131k
    case BuiltinType::Char32:
469
131k
    case BuiltinType::ShortAccum:
470
131k
    case BuiltinType::Accum:
471
131k
    case BuiltinType::LongAccum:
472
131k
    case BuiltinType::UShortAccum:
473
131k
    case BuiltinType::UAccum:
474
131k
    case BuiltinType::ULongAccum:
475
131k
    case BuiltinType::ShortFract:
476
131k
    case BuiltinType::Fract:
477
131k
    case BuiltinType::LongFract:
478
131k
    case BuiltinType::UShortFract:
479
131k
    case BuiltinType::UFract:
480
131k
    case BuiltinType::ULongFract:
481
131k
    case BuiltinType::SatShortAccum:
482
131k
    case BuiltinType::SatAccum:
483
131k
    case BuiltinType::SatLongAccum:
484
131k
    case BuiltinType::SatUShortAccum:
485
131k
    case BuiltinType::SatUAccum:
486
131k
    case BuiltinType::SatULongAccum:
487
131k
    case BuiltinType::SatShortFract:
488
131k
    case BuiltinType::SatFract:
489
131k
    case BuiltinType::SatLongFract:
490
131k
    case BuiltinType::SatUShortFract:
491
131k
    case BuiltinType::SatUFract:
492
131k
    case BuiltinType::SatULongFract:
493
131k
      ResultType = llvm::IntegerType::get(getLLVMContext(),
494
131k
                                 static_cast<unsigned>(Context.getTypeSize(T)));
495
131k
      break;
496
497
32
    case BuiltinType::Float16:
498
32
      ResultType =
499
32
          getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
500
32
                           /* UseNativeHalf = */ true);
501
32
      break;
502
503
818
    case BuiltinType::Half:
504
      // Half FP can either be storage-only (lowered to i16) or native.
505
818
      ResultType = getTypeForFormat(
506
818
          getLLVMContext(), Context.getFloatTypeSemantics(T),
507
818
          Context.getLangOpts().NativeHalfType ||
508
818
              
!Context.getTargetInfo().useFP16ConversionIntrinsics()776
);
509
818
      break;
510
296
    case BuiltinType::BFloat16:
511
6.22k
    case BuiltinType::Float:
512
11.6k
    case BuiltinType::Double:
513
12.0k
    case BuiltinType::LongDouble:
514
12.1k
    case BuiltinType::Float128:
515
12.1k
    case BuiltinType::Ibm128:
516
12.1k
      ResultType = getTypeForFormat(getLLVMContext(),
517
12.1k
                                    Context.getFloatTypeSemantics(T),
518
12.1k
                                    /* UseNativeHalf = */ false);
519
12.1k
      break;
520
521
672
    case BuiltinType::NullPtr:
522
      // Model std::nullptr_t as i8*
523
672
      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
524
672
      break;
525
526
77
    case BuiltinType::UInt128:
527
175
    case BuiltinType::Int128:
528
175
      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
529
175
      break;
530
531
0
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
532
989
    case BuiltinType::Id:
533
989
#include 
"clang/Basic/OpenCLImageTypes.def"175
534
989
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
535
989
    
case BuiltinType::Id:522
536
989
#include 
"clang/Basic/OpenCLExtensionTypes.def"37
537
522
    case BuiltinType::OCLSampler:
538
57
    case BuiltinType::OCLEvent:
539
62
    case BuiltinType::OCLClkEvent:
540
74
    case BuiltinType::OCLQueue:
541
82
    case BuiltinType::OCLReserveID:
542
82
      ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
543
82
      break;
544
24.5k
    case BuiltinType::SveInt8:
545
47.3k
    case BuiltinType::SveUint8:
546
47.5k
    case BuiltinType::SveInt8x2:
547
47.8k
    case BuiltinType::SveUint8x2:
548
47.9k
    case BuiltinType::SveInt8x3:
549
48.1k
    case BuiltinType::SveUint8x3:
550
48.3k
    case BuiltinType::SveInt8x4:
551
48.5k
    case BuiltinType::SveUint8x4:
552
79.0k
    case BuiltinType::SveInt16:
553
105k
    case BuiltinType::SveUint16:
554
105k
    case BuiltinType::SveInt16x2:
555
106k
    case BuiltinType::SveUint16x2:
556
106k
    case BuiltinType::SveInt16x3:
557
106k
    case BuiltinType::SveUint16x3:
558
106k
    case BuiltinType::SveInt16x4:
559
106k
    case BuiltinType::SveUint16x4:
560
143k
    case BuiltinType::SveInt32:
561
181k
    case BuiltinType::SveUint32:
562
181k
    case BuiltinType::SveInt32x2:
563
181k
    case BuiltinType::SveUint32x2:
564
181k
    case BuiltinType::SveInt32x3:
565
181k
    case BuiltinType::SveUint32x3:
566
182k
    case BuiltinType::SveInt32x4:
567
182k
    case BuiltinType::SveUint32x4:
568
217k
    case BuiltinType::SveInt64:
569
256k
    case BuiltinType::SveUint64:
570
256k
    case BuiltinType::SveInt64x2:
571
256k
    case BuiltinType::SveUint64x2:
572
256k
    case BuiltinType::SveInt64x3:
573
256k
    case BuiltinType::SveUint64x3:
574
257k
    case BuiltinType::SveInt64x4:
575
257k
    case BuiltinType::SveUint64x4:
576
377k
    case BuiltinType::SveBool:
577
397k
    case BuiltinType::SveFloat16:
578
397k
    case BuiltinType::SveFloat16x2:
579
397k
    case BuiltinType::SveFloat16x3:
580
397k
    case BuiltinType::SveFloat16x4:
581
418k
    case BuiltinType::SveFloat32:
582
418k
    case BuiltinType::SveFloat32x2:
583
418k
    case BuiltinType::SveFloat32x3:
584
419k
    case BuiltinType::SveFloat32x4:
585
438k
    case BuiltinType::SveFloat64:
586
438k
    case BuiltinType::SveFloat64x2:
587
438k
    case BuiltinType::SveFloat64x3:
588
438k
    case BuiltinType::SveFloat64x4:
589
443k
    case BuiltinType::SveBFloat16:
590
443k
    case BuiltinType::SveBFloat16x2:
591
443k
    case BuiltinType::SveBFloat16x3:
592
443k
    case BuiltinType::SveBFloat16x4: {
593
443k
      ASTContext::BuiltinVectorTypeInfo Info =
594
443k
          Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
595
443k
      return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
596
443k
                                           Info.EC.getKnownMinValue() *
597
443k
                                               Info.NumVectors);
598
443k
    }
599
0
#define PPC_VECTOR_TYPE(Name, Id, Size) \
600
8
    case BuiltinType::Id: \
601
8
      ResultType = \
602
8
        llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
603
8
      break;
604
443k
#include "clang/Basic/PPCTypes.def"
605
35.2M
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
606
1.01M
#include 
"clang/Basic/RISCVVTypes.def"4
607
1.01M
    {
608
1.01M
      ASTContext::BuiltinVectorTypeInfo Info =
609
1.01M
          Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
610
1.01M
      return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
611
1.01M
                                           Info.EC.getKnownMinValue() *
612
1.01M
                                           Info.NumVectors);
613
34.2M
    }
614
0
   case BuiltinType::Dependent:
615
0
#define BUILTIN_TYPE(Id, SingletonId)
616
0
#define PLACEHOLDER_TYPE(Id, SingletonId) \
617
0
    case BuiltinType::Id:
618
0
#include "clang/AST/BuiltinTypes.def"
619
0
      llvm_unreachable("Unexpected placeholder builtin type!");
620
1.71M
    }
621
261k
    break;
622
1.71M
  }
623
261k
  case Type::Auto:
624
0
  case Type::DeducedTemplateSpecialization:
625
0
    llvm_unreachable("Unexpected undeduced type!");
626
661
  case Type::Complex: {
627
661
    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
628
661
    ResultType = llvm::StructType::get(EltTy, EltTy);
629
661
    break;
630
0
  }
631
32.2k
  case Type::LValueReference:
632
35.8k
  case Type::RValueReference: {
633
35.8k
    const ReferenceType *RTy = cast<ReferenceType>(Ty);
634
35.8k
    QualType ETy = RTy->getPointeeType();
635
35.8k
    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
636
35.8k
    unsigned AS = Context.getTargetAddressSpace(ETy);
637
35.8k
    ResultType = llvm::PointerType::get(PointeeType, AS);
638
35.8k
    break;
639
32.2k
  }
640
193k
  case Type::Pointer: {
641
193k
    const PointerType *PTy = cast<PointerType>(Ty);
642
193k
    QualType ETy = PTy->getPointeeType();
643
193k
    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
644
193k
    if (PointeeType->isVoidTy())
645
0
      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
646
647
193k
    unsigned AS = PointeeType->isFunctionTy()
648
193k
                      ? 
getDataLayout().getProgramAddressSpace()7.45k
649
193k
                      : 
Context.getTargetAddressSpace(ETy)186k
;
650
651
193k
    ResultType = llvm::PointerType::get(PointeeType, AS);
652
193k
    break;
653
32.2k
  }
654
655
2.92k
  case Type::VariableArray: {
656
2.92k
    const VariableArrayType *A = cast<VariableArrayType>(Ty);
657
2.92k
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
658
2.92k
           "FIXME: We only handle trivial array types so far!");
659
    // VLAs resolve to the innermost element type; this matches
660
    // the return of alloca, and there isn't any obviously better choice.
661
0
    ResultType = ConvertTypeForMem(A->getElementType());
662
2.92k
    break;
663
32.2k
  }
664
215
  case Type::IncompleteArray: {
665
215
    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
666
215
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
667
215
           "FIXME: We only handle trivial array types so far!");
668
    // int X[] -> [0 x int], unless the element type is not sized.  If it is
669
    // unsized (e.g. an incomplete struct) just use [0 x i8].
670
0
    ResultType = ConvertTypeForMem(A->getElementType());
671
215
    if (!ResultType->isSized()) {
672
1
      SkippedLayout = true;
673
1
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
674
1
    }
675
215
    ResultType = llvm::ArrayType::get(ResultType, 0);
676
215
    break;
677
32.2k
  }
678
58.5k
  case Type::ConstantArray: {
679
58.5k
    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
680
58.5k
    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
681
682
    // Lower arrays of undefined struct type to arrays of i8 just to have a
683
    // concrete type.
684
58.5k
    if (!EltTy->isSized()) {
685
3
      SkippedLayout = true;
686
3
      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
687
3
    }
688
689
58.5k
    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
690
58.5k
    break;
691
32.2k
  }
692
1.02k
  case Type::ExtVector:
693
6.44k
  case Type::Vector: {
694
6.44k
    const VectorType *VT = cast<VectorType>(Ty);
695
6.44k
    ResultType = llvm::FixedVectorType::get(ConvertType(VT->getElementType()),
696
6.44k
                                            VT->getNumElements());
697
6.44k
    break;
698
1.02k
  }
699
71
  case Type::ConstantMatrix: {
700
71
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
701
71
    ResultType =
702
71
        llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
703
71
                                   MT->getNumRows() * MT->getNumColumns());
704
71
    break;
705
1.02k
  }
706
649
  case Type::FunctionNoProto:
707
62.2k
  case Type::FunctionProto:
708
62.2k
    ResultType = ConvertFunctionTypeInternal(T);
709
62.2k
    break;
710
53.1k
  case Type::ObjCObject:
711
53.1k
    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
712
53.1k
    break;
713
714
3.78k
  case Type::ObjCInterface: {
715
    // Objective-C interfaces are always opaque (outside of the
716
    // runtime, which can do whatever it likes); we never refine
717
    // these.
718
3.78k
    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
719
3.78k
    if (!T)
720
3.78k
      T = llvm::StructType::create(getLLVMContext());
721
3.78k
    ResultType = T;
722
3.78k
    break;
723
649
  }
724
725
56.8k
  case Type::ObjCObjectPointer: {
726
    // Protocol qualifications do not influence the LLVM type, we just return a
727
    // pointer to the underlying interface type. We don't need to worry about
728
    // recursive conversion.
729
56.8k
    llvm::Type *T =
730
56.8k
      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
731
56.8k
    ResultType = T->getPointerTo();
732
56.8k
    break;
733
649
  }
734
735
11.0k
  case Type::Enum: {
736
11.0k
    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
737
11.0k
    if (ED->isCompleteDefinition() || 
ED->isFixed()94
)
738
11.0k
      return ConvertType(ED->getIntegerType());
739
    // Return a placeholder 'i32' type.  This can be changed later when the
740
    // type is defined (see UpdateCompletedType), but is likely to be the
741
    // "right" answer.
742
10
    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
743
10
    break;
744
11.0k
  }
745
746
621
  case Type::BlockPointer: {
747
621
    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
748
621
    llvm::Type *PointeeType = CGM.getLangOpts().OpenCL
749
621
                                  ? 
CGM.getGenericBlockLiteralType()45
750
621
                                  : 
ConvertTypeForMem(FTy)576
;
751
621
    unsigned AS = Context.getTargetAddressSpace(FTy);
752
621
    ResultType = llvm::PointerType::get(PointeeType, AS);
753
621
    break;
754
11.0k
  }
755
756
843
  case Type::MemberPointer: {
757
843
    auto *MPTy = cast<MemberPointerType>(Ty);
758
843
    if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
759
21
      RecordsWithOpaqueMemberPointers.insert(MPTy->getClass());
760
21
      ResultType = llvm::StructType::create(getLLVMContext());
761
822
    } else {
762
822
      ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
763
822
    }
764
843
    break;
765
11.0k
  }
766
767
340
  case Type::Atomic: {
768
340
    QualType valueType = cast<AtomicType>(Ty)->getValueType();
769
340
    ResultType = ConvertTypeForMem(valueType);
770
771
    // Pad out to the inflated size if necessary.
772
340
    uint64_t valueSize = Context.getTypeSize(valueType);
773
340
    uint64_t atomicSize = Context.getTypeSize(Ty);
774
340
    if (valueSize != atomicSize) {
775
10
      assert(valueSize < atomicSize);
776
0
      llvm::Type *elts[] = {
777
10
        ResultType,
778
10
        llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
779
10
      };
780
10
      ResultType = llvm::StructType::get(getLLVMContext(),
781
10
                                         llvm::makeArrayRef(elts));
782
10
    }
783
0
    break;
784
11.0k
  }
785
108
  case Type::Pipe: {
786
108
    ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
787
108
    break;
788
11.0k
  }
789
392
  case Type::ExtInt: {
790
392
    const auto &EIT = cast<ExtIntType>(Ty);
791
392
    ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
792
392
    break;
793
11.0k
  }
794
2.20M
  }
795
796
738k
  assert(ResultType && "Didn't convert a type?");
797
798
0
  TypeCache[Ty] = ResultType;
799
738k
  return ResultType;
800
2.20M
}
801
802
36
bool CodeGenModule::isPaddedAtomicType(QualType type) {
803
36
  return isPaddedAtomicType(type->castAs<AtomicType>());
804
36
}
805
806
36
bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
807
36
  return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
808
36
}
809
810
/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
811
463k
llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
812
  // TagDecl's are not necessarily unique, instead use the (clang)
813
  // type connected to the decl.
814
463k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
815
816
463k
  llvm::StructType *&Entry = RecordDeclTypes[Key];
817
818
  // If we don't have a StructType at all yet, create the forward declaration.
819
463k
  if (!Entry) {
820
143k
    Entry = llvm::StructType::create(getLLVMContext());
821
143k
    addRecordTypeName(RD, Entry, "");
822
143k
  }
823
463k
  llvm::StructType *Ty = Entry;
824
825
  // If this is still a forward declaration, or the LLVM type is already
826
  // complete, there's nothing more to do.
827
463k
  RD = RD->getDefinition();
828
463k
  if (!RD || 
!RD->isCompleteDefinition()461k
||
!Ty->isOpaque()461k
)
829
320k
    return Ty;
830
831
  // If converting this type would cause us to infinitely loop, don't do it!
832
142k
  if (!isSafeToConvert(RD, *this)) {
833
652
    DeferredRecords.push_back(RD);
834
652
    return Ty;
835
652
  }
836
837
  // Okay, this is a definition of a type.  Compile the implementation now.
838
142k
  bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
839
142k
  (void)InsertResult;
840
142k
  assert(InsertResult && "Recursively compiling a struct?");
841
842
  // Force conversion of non-virtual base classes recursively.
843
142k
  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
844
82.3k
    for (const auto &I : CRD->bases()) {
845
19.2k
      if (I.isVirtual()) 
continue872
;
846
18.3k
      ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
847
18.3k
    }
848
82.3k
  }
849
850
  // Layout fields.
851
142k
  std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
852
142k
  CGRecordLayouts[Key] = std::move(Layout);
853
854
  // We're done laying out this struct.
855
142k
  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
856
142k
  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
857
858
  // If this struct blocked a FunctionType conversion, then recompute whatever
859
  // was derived from that.
860
  // FIXME: This is hugely overconservative.
861
142k
  if (SkippedLayout)
862
63
    TypeCache.clear();
863
864
  // If we're done converting the outer-most record, then convert any deferred
865
  // structs as well.
866
142k
  if (RecordsBeingLaidOut.empty())
867
110k
    
while (109k
!DeferredRecords.empty())
868
550
      ConvertRecordDeclType(DeferredRecords.pop_back_val());
869
870
142k
  return Ty;
871
142k
}
872
873
/// getCGRecordLayout - Return record layout info for the given record decl.
874
const CGRecordLayout &
875
289k
CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
876
289k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
877
878
289k
  auto I = CGRecordLayouts.find(Key);
879
289k
  if (I != CGRecordLayouts.end())
880
283k
    return *I->second;
881
  // Compute the type information.
882
6.88k
  ConvertRecordDeclType(RD);
883
884
  // Now try again.
885
6.88k
  I = CGRecordLayouts.find(Key);
886
887
6.88k
  assert(I != CGRecordLayouts.end() &&
888
6.88k
         "Unable to find record layout information for type");
889
0
  return *I->second;
890
289k
}
891
892
18
bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
893
18
  assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
894
0
  return isZeroInitializable(T);
895
18
}
896
897
320k
bool CodeGenTypes::isZeroInitializable(QualType T) {
898
320k
  if (T->getAs<PointerType>())
899
85.7k
    return Context.getTargetNullPointerValue(T) == 0;
900
901
234k
  if (const auto *AT = Context.getAsArrayType(T)) {
902
14.1k
    if (isa<IncompleteArrayType>(AT))
903
59
      return true;
904
14.1k
    if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
905
14.1k
      if (Context.getConstantArrayElementCount(CAT) == 0)
906
2.45k
        return true;
907
11.6k
    T = Context.getBaseElementType(T);
908
11.6k
  }
909
910
  // Records are non-zero-initializable if they contain any
911
  // non-zero-initializable subobjects.
912
232k
  if (const RecordType *RT = T->getAs<RecordType>()) {
913
25.7k
    const RecordDecl *RD = RT->getDecl();
914
25.7k
    return isZeroInitializable(RD);
915
25.7k
  }
916
917
  // We have to ask the ABI about member pointers.
918
206k
  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
919
287
    return getCXXABI().isZeroInitializable(MPT);
920
921
  // Everything else is okay.
922
206k
  return true;
923
206k
}
924
925
37.4k
bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
926
37.4k
  return getCGRecordLayout(RD).isZeroInitializable();
927
37.4k
}