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