Coverage Report

Created: 2020-02-25 14:32

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