Coverage Report

Created: 2019-07-24 05:18

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