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