Coverage Report

Created: 2022-01-18 06:27

/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
35.0k
    TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
35
35.0k
  SkippedLayout = false;
36
35.0k
}
37
38
35.0k
CodeGenTypes::~CodeGenTypes() {
39
35.0k
  for (llvm::FoldingSet<CGFunctionInfo>::iterator
40
273k
       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
41
238k
    delete &*I++;
42
35.0k
}
43
44
18.5k
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
45
18.5k
  return CGM.getCodeGenOpts();
46
18.5k
}
47
48
void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
49
                                     llvm::StructType *Ty,
50
146k
                                     StringRef suffix) {
51
146k
  SmallString<256> TypeName;
52
146k
  llvm::raw_svector_ostream OS(TypeName);
53
146k
  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
146k
  PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
59
146k
  Policy.SuppressInlineNamespace = false;
60
61
  // Name the codegen type after the typedef name
62
  // if there is no tag type name available
63
146k
  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
139k
    if (RD->getDeclContext())
67
139k
      RD->printQualifiedName(OS, Policy);
68
0
    else
69
0
      RD->printName(OS);
70
139k
  } else 
if (const TypedefNameDecl *6.70k
TDD6.70k
= 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.79k
    if (TDD->getDeclContext())
74
1.79k
      TDD->printQualifiedName(OS, Policy);
75
0
    else
76
0
      TDD->printName(OS);
77
1.79k
  } else
78
4.90k
    OS << "anon";
79
80
146k
  if (!suffix.empty())
81
10.3k
    OS << suffix;
82
83
146k
  Ty->setName(OS.str());
84
146k
}
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.04M
llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool ForBitField) {
91
4.04M
  if (T->isConstantMatrixType()) {
92
1.05k
    const Type *Ty = Context.getCanonicalType(T).getTypePtr();
93
1.05k
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
94
1.05k
    return llvm::ArrayType::get(ConvertType(MT->getElementType()),
95
1.05k
                                MT->getNumRows() * MT->getNumColumns());
96
1.05k
  }
97
98
4.03M
  llvm::Type *R = ConvertType(T);
99
100
  // If this is a bool type, or a bit-precise integer type in a bitfield
101
  // representation, map this integer to the target-specified size.
102
4.03M
  if ((ForBitField && 
T->isBitIntType()141
) ||
103
4.03M
      
(4.03M
!T->isBitIntType()4.03M
&&
R->isIntegerTy(1)4.03M
))
104
25.7k
    return llvm::IntegerType::get(getLLVMContext(),
105
25.7k
                                  (unsigned)Context.getTypeSize(T));
106
107
  // Else, don't map it.
108
4.01M
  return R;
109
4.03M
}
110
111
/// isRecordLayoutComplete - Return true if the specified type is already
112
/// completely laid out.
113
647k
bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
114
647k
  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
115
647k
  RecordDeclTypes.find(Ty);
116
647k
  return I != RecordDeclTypes.end() && 
!I->second->isOpaque()47.6k
;
117
647k
}
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
648k
                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
648k
  if (!AlreadyChecked.insert(RD).second)
133
923
    return true;
134
135
647k
  const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
136
137
  // If this type is already laid out, converting it is a noop.
138
647k
  if (CGT.isRecordLayoutComplete(Key)) 
return true2.48k
;
139
140
  // If this type is currently being laid out, we can't recursively compile it.
141
644k
  if (CGT.isRecordBeingLaidOut(Key))
142
686
    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
644k
  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
149
642k
    for (const auto &I : CRD->bases())
150
583k
      if (!isSafeToConvert(I.getType()->castAs<RecordType>()->getDecl(), CGT,
151
583k
                           AlreadyChecked))
152
23
        return false;
153
642k
  }
154
155
  // If this type would require laying out members that are currently being laid
156
  // out, don't do it.
157
644k
  for (const auto *I : RD->fields())
158
85.4k
    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
159
3
      return false;
160
161
  // If there are no problems, lets do it.
162
644k
  return true;
163
644k
}
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
91.3k
                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
171
  // Strip off atomic type sugar.
172
91.3k
  if (const auto *AT = T->getAs<AtomicType>())
173
643
    T = AT->getValueType();
174
175
  // If this is a record, check it.
176
91.3k
  if (const auto *RT = T->getAs<RecordType>())
177
19.9k
    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
178
179
  // If this is an array, check the elements, which are embedded inline.
180
71.3k
  if (const auto *AT = CGT.getContext().getAsArrayType(T))
181
5.93k
    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
65.4k
  return true;
187
71.3k
}
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
141k
static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
194
  // If no structs are being laid out, we can certainly do this one.
195
141k
  if (CGT.noRecordsBeingLaidOut()) 
return true96.6k
;
196
197
45.1k
  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
198
45.1k
  return isSafeToConvert(RD, CGT, AlreadyChecked);
199
141k
}
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
153k
bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
207
  // Some ABIs cannot have their member pointers represented in IR unless
208
  // certain circumstances have been reached.
209
153k
  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
153k
  const TagType *TT = Ty->getAs<TagType>();
214
153k
  if (!TT) 
return true146k
;
215
216
  // Incomplete types cannot be converted.
217
6.93k
  if (TT->isIncompleteType())
218
82
    return false;
219
220
  // If this is an enum, then it is always safe to convert.
221
6.85k
  const RecordType *RT = dyn_cast<RecordType>(TT);
222
6.85k
  if (!RT) 
return true542
;
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.30k
  return isSafeToConvert(RT->getDecl(), *this);
232
6.85k
}
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
65.6k
bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
241
65.6k
  if (!isFuncParamTypeConvertible(FT->getReturnType()))
242
17
    return false;
243
244
65.6k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
245
152k
    
for (unsigned i = 0, e = FPT->getNumParams(); 65.0k
i != e;
i++87.5k
)
246
87.5k
      if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
247
70
        return false;
248
249
65.5k
  return true;
250
65.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.14M
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.14M
  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
259
    // Only flush the cache if we've actually already converted this type.
260
444k
    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
444k
    if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
270
437k
      DI->completeType(ED);
271
444k
    return;
272
444k
  }
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.69M
  const RecordDecl *RD = cast<RecordDecl>(TD);
277
1.69M
  if (RD->isDependentType()) 
return357k
;
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.34M
  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
282
667
    ConvertRecordDeclType(RD);
283
284
  // If necessary, provide the full definition of a type only used with a
285
  // declaration so far.
286
1.34M
  if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
287
1.21M
    DI->completeType(RD);
288
1.34M
}
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
11.2k
                                    bool UseNativeHalf = false) {
304
11.2k
  if (&format == &llvm::APFloat::IEEEhalf()) {
305
826
    if (UseNativeHalf)
306
824
      return llvm::Type::getHalfTy(VMContext);
307
2
    else
308
2
      return llvm::Type::getInt16Ty(VMContext);
309
826
  }
310
10.4k
  if (&format == &llvm::APFloat::BFloat())
311
311
    return llvm::Type::getBFloatTy(VMContext);
312
10.1k
  if (&format == &llvm::APFloat::IEEEsingle())
313
4.55k
    return llvm::Type::getFloatTy(VMContext);
314
5.55k
  if (&format == &llvm::APFloat::IEEEdouble())
315
5.12k
    return llvm::Type::getDoubleTy(VMContext);
316
423
  if (&format == &llvm::APFloat::IEEEquad())
317
94
    return llvm::Type::getFP128Ty(VMContext);
318
329
  if (&format == &llvm::APFloat::PPCDoubleDouble())
319
40
    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
58.2k
llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
326
58.2k
  assert(QFT.isCanonical());
327
0
  const Type *Ty = QFT.getTypePtr();
328
58.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
58.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
58.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
58.2k
  const CGFunctionInfo *FI;
361
58.2k
  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
362
57.5k
    FI = &arrangeFreeFunctionType(
363
57.5k
        CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
364
57.5k
  } else {
365
637
    const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
366
637
    FI = &arrangeFreeFunctionType(
367
637
        CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
368
637
  }
369
370
58.2k
  llvm::Type *ResultType = nullptr;
371
  // If there is something higher level prodding our CGFunctionInfo, then
372
  // don't recurse into it again.
373
58.2k
  if (FunctionsBeingProcessed.count(FI)) {
374
375
4
    ResultType = llvm::StructType::get(getLLVMContext());
376
4
    SkippedLayout = true;
377
58.2k
  } else {
378
379
    // Otherwise, we're good to go, go ahead and convert it.
380
58.2k
    ResultType = GetFunctionType(*FI);
381
58.2k
  }
382
383
58.2k
  RecordsBeingLaidOut.erase(Ty);
384
385
58.2k
  if (SkippedLayout)
386
57
    TypeCache.clear();
387
388
58.2k
  if (RecordsBeingLaidOut.empty())
389
53.8k
    
while (53.7k
!DeferredRecords.empty())
390
96
      ConvertRecordDeclType(DeferredRecords.pop_back_val());
391
58.2k
  return ResultType;
392
58.2k
}
393
394
/// ConvertType - Convert the specified type to its LLVM form.
395
8.82M
llvm::Type *CodeGenTypes::ConvertType(QualType T) {
396
8.82M
  T = Context.getCanonicalType(T);
397
398
8.82M
  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
8.82M
  if (Context.getLangOpts().CUDAIsDevice) {
403
10.1k
    if (T->isCUDADeviceBuiltinSurfaceType()) {
404
0
      if (auto *Ty = CGM.getTargetCodeGenInfo()
405
0
                         .getCUDADeviceBuiltinSurfaceDeviceType())
406
0
        return Ty;
407
10.1k
    } else if (T->isCUDADeviceBuiltinTextureType()) {
408
0
      if (auto *Ty = CGM.getTargetCodeGenInfo()
409
0
                         .getCUDADeviceBuiltinTextureDeviceType())
410
0
        return Ty;
411
0
    }
412
10.1k
  }
413
414
  // RecordTypes are cached and processed specially.
415
8.82M
  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
416
877k
    return ConvertRecordDeclType(RT->getDecl());
417
418
  // See if type is already cached.
419
7.94M
  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
7.94M
  if (TCI != TypeCache.end())
422
6.88M
    return TCI->second;
423
424
  // If we don't have it in the cache, convert it now.
425
1.06M
  llvm::Type *ResultType = nullptr;
426
1.06M
  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
650k
  case Type::Builtin: {
437
650k
    switch (cast<BuiltinType>(Ty)->getKind()) {
438
22.0k
    case BuiltinType::Void:
439
37.9k
    case BuiltinType::ObjCId:
440
53.8k
    case BuiltinType::ObjCClass:
441
69.8k
    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
69.8k
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
445
69.8k
      break;
446
447
3.13k
    case BuiltinType::Bool:
448
      // Note that we always return bool as i1 for use as a scalar type.
449
3.13k
      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
450
3.13k
      break;
451
452
11.0k
    case BuiltinType::Char_S:
453
11.0k
    case BuiltinType::Char_U:
454
13.0k
    case BuiltinType::SChar:
455
18.6k
    case BuiltinType::UChar:
456
37.5k
    case BuiltinType::Short:
457
39.5k
    case BuiltinType::UShort:
458
62.4k
    case BuiltinType::Int:
459
72.6k
    case BuiltinType::UInt:
460
92.1k
    case BuiltinType::Long:
461
102k
    case BuiltinType::ULong:
462
105k
    case BuiltinType::LongLong:
463
107k
    case BuiltinType::ULongLong:
464
107k
    case BuiltinType::WChar_S:
465
107k
    case BuiltinType::WChar_U:
466
107k
    case BuiltinType::Char8:
467
107k
    case BuiltinType::Char16:
468
107k
    case BuiltinType::Char32:
469
107k
    case BuiltinType::ShortAccum:
470
107k
    case BuiltinType::Accum:
471
107k
    case BuiltinType::LongAccum:
472
107k
    case BuiltinType::UShortAccum:
473
107k
    case BuiltinType::UAccum:
474
107k
    case BuiltinType::ULongAccum:
475
107k
    case BuiltinType::ShortFract:
476
107k
    case BuiltinType::Fract:
477
107k
    case BuiltinType::LongFract:
478
107k
    case BuiltinType::UShortFract:
479
107k
    case BuiltinType::UFract:
480
107k
    case BuiltinType::ULongFract:
481
107k
    case BuiltinType::SatShortAccum:
482
107k
    case BuiltinType::SatAccum:
483
107k
    case BuiltinType::SatLongAccum:
484
107k
    case BuiltinType::SatUShortAccum:
485
107k
    case BuiltinType::SatUAccum:
486
107k
    case BuiltinType::SatULongAccum:
487
107k
    case BuiltinType::SatShortFract:
488
107k
    case BuiltinType::SatFract:
489
107k
    case BuiltinType::SatLongFract:
490
107k
    case BuiltinType::SatUShortFract:
491
107k
    case BuiltinType::SatUFract:
492
107k
    case BuiltinType::SatULongFract:
493
107k
      ResultType = llvm::IntegerType::get(getLLVMContext(),
494
107k
                                 static_cast<unsigned>(Context.getTypeSize(T)));
495
107k
      break;
496
497
17
    case BuiltinType::Float16:
498
17
      ResultType =
499
17
          getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
500
17
                           /* UseNativeHalf = */ true);
501
17
      break;
502
503
809
    case BuiltinType::Half:
504
      // Half FP can either be storage-only (lowered to i16) or native.
505
809
      ResultType = getTypeForFormat(
506
809
          getLLVMContext(), Context.getFloatTypeSemantics(T),
507
809
          Context.getLangOpts().NativeHalfType ||
508
809
              
!Context.getTargetInfo().useFP16ConversionIntrinsics()790
);
509
809
      break;
510
311
    case BuiltinType::BFloat16:
511
4.86k
    case BuiltinType::Float:
512
9.95k
    case BuiltinType::Double:
513
10.3k
    case BuiltinType::LongDouble:
514
10.4k
    case BuiltinType::Float128:
515
10.4k
    case BuiltinType::Ibm128:
516
10.4k
      ResultType = getTypeForFormat(getLLVMContext(),
517
10.4k
                                    Context.getFloatTypeSemantics(T),
518
10.4k
                                    /* UseNativeHalf = */ false);
519
10.4k
      break;
520
521
714
    case BuiltinType::NullPtr:
522
      // Model std::nullptr_t as i8*
523
714
      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
524
714
      break;
525
526
66
    case BuiltinType::UInt128:
527
154
    case BuiltinType::Int128:
528
154
      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
529
154
      break;
530
531
0
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
532
989
    case BuiltinType::Id:
533
989
#include 
"clang/Basic/OpenCLImageTypes.def"154
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
25.3k
    case BuiltinType::SveInt8:
545
49.1k
    case BuiltinType::SveUint8:
546
49.3k
    case BuiltinType::SveInt8x2:
547
49.5k
    case BuiltinType::SveUint8x2:
548
49.7k
    case BuiltinType::SveInt8x3:
549
49.9k
    case BuiltinType::SveUint8x3:
550
50.1k
    case BuiltinType::SveInt8x4:
551
50.3k
    case BuiltinType::SveUint8x4:
552
81.9k
    case BuiltinType::SveInt16:
553
109k
    case BuiltinType::SveUint16:
554
109k
    case BuiltinType::SveInt16x2:
555
109k
    case BuiltinType::SveUint16x2:
556
110k
    case BuiltinType::SveInt16x3:
557
110k
    case BuiltinType::SveUint16x3:
558
110k
    case BuiltinType::SveInt16x4:
559
110k
    case BuiltinType::SveUint16x4:
560
149k
    case BuiltinType::SveInt32:
561
187k
    case BuiltinType::SveUint32:
562
188k
    case BuiltinType::SveInt32x2:
563
188k
    case BuiltinType::SveUint32x2:
564
188k
    case BuiltinType::SveInt32x3:
565
188k
    case BuiltinType::SveUint32x3:
566
188k
    case BuiltinType::SveInt32x4:
567
189k
    case BuiltinType::SveUint32x4:
568
225k
    case BuiltinType::SveInt64:
569
265k
    case BuiltinType::SveUint64:
570
265k
    case BuiltinType::SveInt64x2:
571
266k
    case BuiltinType::SveUint64x2:
572
266k
    case BuiltinType::SveInt64x3:
573
266k
    case BuiltinType::SveUint64x3:
574
266k
    case BuiltinType::SveInt64x4:
575
266k
    case BuiltinType::SveUint64x4:
576
388k
    case BuiltinType::SveBool:
577
408k
    case BuiltinType::SveFloat16:
578
408k
    case BuiltinType::SveFloat16x2:
579
408k
    case BuiltinType::SveFloat16x3:
580
408k
    case BuiltinType::SveFloat16x4:
581
430k
    case BuiltinType::SveFloat32:
582
430k
    case BuiltinType::SveFloat32x2:
583
430k
    case BuiltinType::SveFloat32x3:
584
430k
    case BuiltinType::SveFloat32x4:
585
450k
    case BuiltinType::SveFloat64:
586
451k
    case BuiltinType::SveFloat64x2:
587
451k
    case BuiltinType::SveFloat64x3:
588
451k
    case BuiltinType::SveFloat64x4:
589
455k
    case BuiltinType::SveBFloat16:
590
456k
    case BuiltinType::SveBFloat16x2:
591
456k
    case BuiltinType::SveBFloat16x3:
592
456k
    case BuiltinType::SveBFloat16x4: {
593
456k
      ASTContext::BuiltinVectorTypeInfo Info =
594
456k
          Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
595
456k
      return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
596
456k
                                           Info.EC.getKnownMinValue() *
597
456k
                                               Info.NumVectors);
598
456k
    }
599
0
#define PPC_VECTOR_TYPE(Name, Id, Size) \
600
20
    case BuiltinType::Id: \
601
20
      ResultType = \
602
20
        llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
603
20
      break;
604
456k
#include "clang/Basic/PPCTypes.def"
605
2.24k
#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
606
48
#include 
"clang/Basic/RISCVVTypes.def"10
607
48
    {
608
48
      ASTContext::BuiltinVectorTypeInfo Info =
609
48
          Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
610
48
      return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
611
48
                                           Info.EC.getKnownMinValue() *
612
48
                                           Info.NumVectors);
613
2.19k
    }
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
650k
    }
621
193k
    break;
622
650k
  }
623
193k
  case Type::Auto:
624
0
  case Type::DeducedTemplateSpecialization:
625
0
    llvm_unreachable("Unexpected undeduced type!");
626
590
  case Type::Complex: {
627
590
    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
628
590
    ResultType = llvm::StructType::get(EltTy, EltTy);
629
590
    break;
630
0
  }
631
31.7k
  case Type::LValueReference:
632
35.3k
  case Type::RValueReference: {
633
35.3k
    const ReferenceType *RTy = cast<ReferenceType>(Ty);
634
35.3k
    QualType ETy = RTy->getPointeeType();
635
35.3k
    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
636
35.3k
    unsigned AS = Context.getTargetAddressSpace(ETy);
637
35.3k
    ResultType = llvm::PointerType::get(PointeeType, AS);
638
35.3k
    break;
639
31.7k
  }
640
167k
  case Type::Pointer: {
641
167k
    const PointerType *PTy = cast<PointerType>(Ty);
642
167k
    QualType ETy = PTy->getPointeeType();
643
167k
    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
644
167k
    if (PointeeType->isVoidTy())
645
0
      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
646
167k
    unsigned AS = Context.getTargetAddressSpace(ETy);
647
167k
    ResultType = llvm::PointerType::get(PointeeType, AS);
648
167k
    break;
649
31.7k
  }
650
651
2.92k
  case Type::VariableArray: {
652
2.92k
    const VariableArrayType *A = cast<VariableArrayType>(Ty);
653
2.92k
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
654
2.92k
           "FIXME: We only handle trivial array types so far!");
655
    // VLAs resolve to the innermost element type; this matches
656
    // the return of alloca, and there isn't any obviously better choice.
657
0
    ResultType = ConvertTypeForMem(A->getElementType());
658
2.92k
    break;
659
31.7k
  }
660
126
  case Type::IncompleteArray: {
661
126
    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
662
126
    assert(A->getIndexTypeCVRQualifiers() == 0 &&
663
126
           "FIXME: We only handle trivial array types so far!");
664
    // int X[] -> [0 x int], unless the element type is not sized.  If it is
665
    // unsized (e.g. an incomplete struct) just use [0 x i8].
666
0
    ResultType = ConvertTypeForMem(A->getElementType());
667
126
    if (!ResultType->isSized()) {
668
1
      SkippedLayout = true;
669
1
      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
670
1
    }
671
126
    ResultType = llvm::ArrayType::get(ResultType, 0);
672
126
    break;
673
31.7k
  }
674
57.9k
  case Type::ConstantArray: {
675
57.9k
    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
676
57.9k
    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
677
678
    // Lower arrays of undefined struct type to arrays of i8 just to have a
679
    // concrete type.
680
57.9k
    if (!EltTy->isSized()) {
681
3
      SkippedLayout = true;
682
3
      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
683
3
    }
684
685
57.9k
    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
686
57.9k
    break;
687
31.7k
  }
688
428
  case Type::ExtVector:
689
4.86k
  case Type::Vector: {
690
4.86k
    const VectorType *VT = cast<VectorType>(Ty);
691
4.86k
    ResultType = llvm::FixedVectorType::get(ConvertType(VT->getElementType()),
692
4.86k
                                            VT->getNumElements());
693
4.86k
    break;
694
428
  }
695
83
  case Type::ConstantMatrix: {
696
83
    const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
697
83
    ResultType =
698
83
        llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
699
83
                                   MT->getNumRows() * MT->getNumColumns());
700
83
    break;
701
428
  }
702
639
  case Type::FunctionNoProto:
703
58.2k
  case Type::FunctionProto:
704
58.2k
    ResultType = ConvertFunctionTypeInternal(T);
705
58.2k
    break;
706
32.2k
  case Type::ObjCObject:
707
32.2k
    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
708
32.2k
    break;
709
710
3.80k
  case Type::ObjCInterface: {
711
    // Objective-C interfaces are always opaque (outside of the
712
    // runtime, which can do whatever it likes); we never refine
713
    // these.
714
3.80k
    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
715
3.80k
    if (!T)
716
3.80k
      T = llvm::StructType::create(getLLVMContext());
717
3.80k
    ResultType = T;
718
3.80k
    break;
719
639
  }
720
721
35.9k
  case Type::ObjCObjectPointer: {
722
    // Protocol qualifications do not influence the LLVM type, we just return a
723
    // pointer to the underlying interface type. We don't need to worry about
724
    // recursive conversion.
725
35.9k
    llvm::Type *T =
726
35.9k
      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
727
35.9k
    ResultType = T->getPointerTo();
728
35.9k
    break;
729
639
  }
730
731
9.55k
  case Type::Enum: {
732
9.55k
    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
733
9.55k
    if (ED->isCompleteDefinition() || 
ED->isFixed()97
)
734
9.54k
      return ConvertType(ED->getIntegerType());
735
    // Return a placeholder 'i32' type.  This can be changed later when the
736
    // type is defined (see UpdateCompletedType), but is likely to be the
737
    // "right" answer.
738
11
    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
739
11
    break;
740
9.55k
  }
741
742
617
  case Type::BlockPointer: {
743
617
    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
744
617
    llvm::Type *PointeeType = CGM.getLangOpts().OpenCL
745
617
                                  ? 
CGM.getGenericBlockLiteralType()45
746
617
                                  : 
ConvertTypeForMem(FTy)572
;
747
    // Block pointers lower to function type. For function type,
748
    // getTargetAddressSpace() returns default address space for
749
    // function pointer i.e. program address space. Therefore, for block
750
    // pointers, it is important to pass qualifiers when calling
751
    // getTargetAddressSpace(), to ensure that we get the address space
752
    // for data pointers and not function pointers.
753
617
    unsigned AS = Context.getTargetAddressSpace(FTy.getQualifiers());
754
617
    ResultType = llvm::PointerType::get(PointeeType, AS);
755
617
    break;
756
9.55k
  }
757
758
852
  case Type::MemberPointer: {
759
852
    auto *MPTy = cast<MemberPointerType>(Ty);
760
852
    if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
761
21
      RecordsWithOpaqueMemberPointers.insert(MPTy->getClass());
762
21
      ResultType = llvm::StructType::create(getLLVMContext());
763
831
    } else {
764
831
      ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
765
831
    }
766
852
    break;
767
9.55k
  }
768
769
337
  case Type::Atomic: {
770
337
    QualType valueType = cast<AtomicType>(Ty)->getValueType();
771
337
    ResultType = ConvertTypeForMem(valueType);
772
773
    // Pad out to the inflated size if necessary.
774
337
    uint64_t valueSize = Context.getTypeSize(valueType);
775
337
    uint64_t atomicSize = Context.getTypeSize(Ty);
776
337
    if (valueSize != atomicSize) {
777
10
      assert(valueSize < atomicSize);
778
0
      llvm::Type *elts[] = {
779
10
        ResultType,
780
10
        llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
781
10
      };
782
10
      ResultType = llvm::StructType::get(getLLVMContext(),
783
10
                                         llvm::makeArrayRef(elts));
784
10
    }
785
0
    break;
786
9.55k
  }
787
108
  case Type::Pipe: {
788
108
    ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
789
108
    break;
790
9.55k
  }
791
481
  case Type::BitInt: {
792
481
    const auto &EIT = cast<BitIntType>(Ty);
793
481
    ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
794
481
    break;
795
9.55k
  }
796
1.06M
  }
797
798
595k
  assert(ResultType && "Didn't convert a type?");
799
800
0
  TypeCache[Ty] = ResultType;
801
595k
  return ResultType;
802
1.06M
}
803
804
36
bool CodeGenModule::isPaddedAtomicType(QualType type) {
805
36
  return isPaddedAtomicType(type->castAs<AtomicType>());
806
36
}
807
808
36
bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
809
36
  return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
810
36
}
811
812
/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
813
914k
llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
814
  // TagDecl's are not necessarily unique, instead use the (clang)
815
  // type connected to the decl.
816
914k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
817
818
914k
  llvm::StructType *&Entry = RecordDeclTypes[Key];
819
820
  // If we don't have a StructType at all yet, create the forward declaration.
821
914k
  if (!Entry) {
822
136k
    Entry = llvm::StructType::create(getLLVMContext());
823
136k
    addRecordTypeName(RD, Entry, "");
824
136k
  }
825
914k
  llvm::StructType *Ty = Entry;
826
827
  // If this is still a forward declaration, or the LLVM type is already
828
  // complete, there's nothing more to do.
829
914k
  RD = RD->getDefinition();
830
914k
  if (!RD || 
!RD->isCompleteDefinition()912k
||
!Ty->isOpaque()912k
)
831
779k
    return Ty;
832
833
  // If converting this type would cause us to infinitely loop, don't do it!
834
135k
  if (!isSafeToConvert(RD, *this)) {
835
685
    DeferredRecords.push_back(RD);
836
685
    return Ty;
837
685
  }
838
839
  // Okay, this is a definition of a type.  Compile the implementation now.
840
134k
  bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
841
134k
  (void)InsertResult;
842
134k
  assert(InsertResult && "Recursively compiling a struct?");
843
844
  // Force conversion of non-virtual base classes recursively.
845
134k
  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
846
96.2k
    for (const auto &I : CRD->bases()) {
847
30.1k
      if (I.isVirtual()) 
continue865
;
848
29.2k
      ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
849
29.2k
    }
850
96.2k
  }
851
852
  // Layout fields.
853
134k
  std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
854
134k
  CGRecordLayouts[Key] = std::move(Layout);
855
856
  // We're done laying out this struct.
857
134k
  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
858
134k
  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
859
860
  // If this struct blocked a FunctionType conversion, then recompute whatever
861
  // was derived from that.
862
  // FIXME: This is hugely overconservative.
863
134k
  if (SkippedLayout)
864
63
    TypeCache.clear();
865
866
  // If we're done converting the outer-most record, then convert any deferred
867
  // structs as well.
868
134k
  if (RecordsBeingLaidOut.empty())
869
90.9k
    
while (90.3k
!DeferredRecords.empty())
870
589
      ConvertRecordDeclType(DeferredRecords.pop_back_val());
871
872
134k
  return Ty;
873
135k
}
874
875
/// getCGRecordLayout - Return record layout info for the given record decl.
876
const CGRecordLayout &
877
300k
CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
878
300k
  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
879
880
300k
  auto I = CGRecordLayouts.find(Key);
881
300k
  if (I != CGRecordLayouts.end())
882
293k
    return *I->second;
883
  // Compute the type information.
884
6.88k
  ConvertRecordDeclType(RD);
885
886
  // Now try again.
887
6.88k
  I = CGRecordLayouts.find(Key);
888
889
6.88k
  assert(I != CGRecordLayouts.end() &&
890
6.88k
         "Unable to find record layout information for type");
891
0
  return *I->second;
892
300k
}
893
894
18
bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
895
18
  assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
896
0
  return isZeroInitializable(T);
897
18
}
898
899
283k
bool CodeGenTypes::isZeroInitializable(QualType T) {
900
283k
  if (T->getAs<PointerType>())
901
68.4k
    return Context.getTargetNullPointerValue(T) == 0;
902
903
215k
  if (const auto *AT = Context.getAsArrayType(T)) {
904
14.6k
    if (isa<IncompleteArrayType>(AT))
905
69
      return true;
906
14.5k
    if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
907
14.5k
      if (Context.getConstantArrayElementCount(CAT) == 0)
908
2.42k
        return true;
909
12.1k
    T = Context.getBaseElementType(T);
910
12.1k
  }
911
912
  // Records are non-zero-initializable if they contain any
913
  // non-zero-initializable subobjects.
914
212k
  if (const RecordType *RT = T->getAs<RecordType>()) {
915
26.1k
    const RecordDecl *RD = RT->getDecl();
916
26.1k
    return isZeroInitializable(RD);
917
26.1k
  }
918
919
  // We have to ask the ABI about member pointers.
920
186k
  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
921
287
    return getCXXABI().isZeroInitializable(MPT);
922
923
  // Everything else is okay.
924
186k
  return true;
925
186k
}
926
927
41.4k
bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
928
41.4k
  return getCGRecordLayout(RD).isZeroInitializable();
929
41.4k
}