Coverage Report

Created: 2021-01-23 06:44

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