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