[AMDGPU][AsmParser][NFC] Get rid of custom default operand handlers.
[llvm-project.git] / clang / lib / CodeGen / CodeGenTypes.cpp
blob0ceab41cdd3601151a2efe939284178f46ba4301
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.
11 //===----------------------------------------------------------------------===//
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"
29 using namespace clang;
30 using namespace CodeGen;
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;
39 CodeGenTypes::~CodeGenTypes() {
40 for (llvm::FoldingSet<CGFunctionInfo>::iterator
41 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
42 delete &*I++;
45 const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
46 return CGM.getCodeGenOpts();
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() << '.';
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;
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";
81 if (!suffix.empty())
82 OS << suffix;
84 Ty->setName(OS.str());
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());
99 llvm::Type *R = ConvertType(T);
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);
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));
116 // Else, don't map it.
117 return R;
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();
128 static bool
129 isSafeToConvert(QualType T, CodeGenTypes &CGT,
130 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
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;
144 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
146 // If this type is already laid out, converting it is a noop.
147 if (CGT.isRecordLayoutComplete(Key)) return true;
149 // If this type is currently being laid out, we can't recursively compile it.
150 if (CGT.isRecordBeingLaidOut(Key))
151 return false;
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;
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;
170 // If there are no problems, lets do it.
171 return true;
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();
184 // If this is a record, check it.
185 if (const auto *RT = T->getAs<RecordType>())
186 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
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);
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;
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;
206 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
207 return isSafeToConvert(RD, CGT, AlreadyChecked);
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);
221 // If this isn't a tagged type, we can convert it!
222 const TagType *TT = Ty->getAs<TagType>();
223 if (!TT) return true;
225 // Incomplete types cannot be converted.
226 if (TT->isIncompleteType())
227 return false;
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;
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.
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);
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;
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;
258 return true;
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();
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;
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;
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);
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);
299 void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
300 QualType T = Context.getRecordType(RD);
301 T = Context.getCanonicalType(T);
303 const Type *Ty = T.getTypePtr();
304 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
305 TypeCache.clear();
306 RecordsWithOpaqueMemberPointers.clear();
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);
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!");
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.
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());
353 SkippedLayout = true;
355 // Return a placeholder type.
356 return llvm::StructType::get(getLLVMContext());
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());
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)));
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)) {
384 ResultType = llvm::StructType::get(getLLVMContext());
385 SkippedLayout = true;
386 } else {
388 // Otherwise, we're good to go, go ahead and convert it.
389 ResultType = GetFunctionType(*FI);
392 RecordsBeingLaidOut.erase(Ty);
394 if (RecordsBeingLaidOut.empty())
395 while (!DeferredRecords.empty())
396 ConvertRecordDeclType(DeferredRecords.pop_back_val());
397 return ResultType;
400 /// ConvertType - Convert the specified type to its LLVM form.
401 llvm::Type *CodeGenTypes::ConvertType(QualType T) {
402 T = Context.getCanonicalType(T);
404 const Type *Ty = T.getTypePtr();
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;
420 // RecordTypes are cached and processed specially.
421 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
422 return ConvertRecordDeclType(RT->getDecl());
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
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.");
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;
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;
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;
519 case BuiltinType::Float16:
520 ResultType =
521 getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
522 /* UseNativeHalf = */ true);
523 break;
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;
543 case BuiltinType::NullPtr:
544 // Model std::nullptr_t as i8*
545 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
546 break;
548 case BuiltinType::UInt128:
549 case BuiltinType::Int128:
550 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
551 break;
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);
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"
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);
645 return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
646 Info.EC.getKnownMinValue() *
647 Info.NumVectors);
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!");
664 break;
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;
674 case Type::LValueReference:
675 case Type::RValueReference: {
676 const ReferenceType *RTy = cast<ReferenceType>(Ty);
677 QualType ETy = RTy->getPointeeType();
678 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
679 unsigned AS = getTargetAddressSpace(ETy);
680 ResultType = llvm::PointerType::get(PointeeType, AS);
681 break;
683 case Type::Pointer: {
684 const PointerType *PTy = cast<PointerType>(Ty);
685 QualType ETy = PTy->getPointeeType();
686 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
687 if (PointeeType->isVoidTy())
688 PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
689 unsigned AS = getTargetAddressSpace(ETy);
690 ResultType = llvm::PointerType::get(PointeeType, AS);
691 break;
694 case Type::VariableArray: {
695 const VariableArrayType *A = cast<VariableArrayType>(Ty);
696 assert(A->getIndexTypeCVRQualifiers() == 0 &&
697 "FIXME: We only handle trivial array types so far!");
698 // VLAs resolve to the innermost element type; this matches
699 // the return of alloca, and there isn't any obviously better choice.
700 ResultType = ConvertTypeForMem(A->getElementType());
701 break;
703 case Type::IncompleteArray: {
704 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
705 assert(A->getIndexTypeCVRQualifiers() == 0 &&
706 "FIXME: We only handle trivial array types so far!");
707 // int X[] -> [0 x int], unless the element type is not sized. If it is
708 // unsized (e.g. an incomplete struct) just use [0 x i8].
709 ResultType = ConvertTypeForMem(A->getElementType());
710 if (!ResultType->isSized()) {
711 SkippedLayout = true;
712 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
714 ResultType = llvm::ArrayType::get(ResultType, 0);
715 break;
717 case Type::ConstantArray: {
718 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
719 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
721 // Lower arrays of undefined struct type to arrays of i8 just to have a
722 // concrete type.
723 if (!EltTy->isSized()) {
724 SkippedLayout = true;
725 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
728 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
729 break;
731 case Type::ExtVector:
732 case Type::Vector: {
733 const auto *VT = cast<VectorType>(Ty);
734 // An ext_vector_type of Bool is really a vector of bits.
735 llvm::Type *IRElemTy = VT->isExtVectorBoolType()
736 ? llvm::Type::getInt1Ty(getLLVMContext())
737 : ConvertType(VT->getElementType());
738 ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
739 break;
741 case Type::ConstantMatrix: {
742 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
743 ResultType =
744 llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
745 MT->getNumRows() * MT->getNumColumns());
746 break;
748 case Type::FunctionNoProto:
749 case Type::FunctionProto:
750 ResultType = ConvertFunctionTypeInternal(T);
751 break;
752 case Type::ObjCObject:
753 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
754 break;
756 case Type::ObjCInterface: {
757 // Objective-C interfaces are always opaque (outside of the
758 // runtime, which can do whatever it likes); we never refine
759 // these.
760 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
761 if (!T)
762 T = llvm::StructType::create(getLLVMContext());
763 ResultType = T;
764 break;
767 case Type::ObjCObjectPointer: {
768 // Protocol qualifications do not influence the LLVM type, we just return a
769 // pointer to the underlying interface type. We don't need to worry about
770 // recursive conversion.
771 llvm::Type *T =
772 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
773 ResultType = T->getPointerTo();
774 break;
777 case Type::Enum: {
778 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
779 if (ED->isCompleteDefinition() || ED->isFixed())
780 return ConvertType(ED->getIntegerType());
781 // Return a placeholder 'i32' type. This can be changed later when the
782 // type is defined (see UpdateCompletedType), but is likely to be the
783 // "right" answer.
784 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
785 break;
788 case Type::BlockPointer: {
789 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
790 llvm::Type *PointeeType = CGM.getLangOpts().OpenCL
791 ? CGM.getGenericBlockLiteralType()
792 : ConvertTypeForMem(FTy);
793 // Block pointers lower to function type. For function type,
794 // getTargetAddressSpace() returns default address space for
795 // function pointer i.e. program address space. Therefore, for block
796 // pointers, it is important to pass the pointee AST address space when
797 // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
798 // address space for data pointers and not function pointers.
799 unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace());
800 ResultType = llvm::PointerType::get(PointeeType, AS);
801 break;
804 case Type::MemberPointer: {
805 auto *MPTy = cast<MemberPointerType>(Ty);
806 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
807 auto *C = MPTy->getClass();
808 auto Insertion = RecordsWithOpaqueMemberPointers.insert({C, nullptr});
809 if (Insertion.second)
810 Insertion.first->second = llvm::StructType::create(getLLVMContext());
811 ResultType = Insertion.first->second;
812 } else {
813 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
815 break;
818 case Type::Atomic: {
819 QualType valueType = cast<AtomicType>(Ty)->getValueType();
820 ResultType = ConvertTypeForMem(valueType);
822 // Pad out to the inflated size if necessary.
823 uint64_t valueSize = Context.getTypeSize(valueType);
824 uint64_t atomicSize = Context.getTypeSize(Ty);
825 if (valueSize != atomicSize) {
826 assert(valueSize < atomicSize);
827 llvm::Type *elts[] = {
828 ResultType,
829 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
831 ResultType =
832 llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts));
834 break;
836 case Type::Pipe: {
837 ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
838 break;
840 case Type::BitInt: {
841 const auto &EIT = cast<BitIntType>(Ty);
842 ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
843 break;
847 assert(ResultType && "Didn't convert a type?");
848 assert((!CachedType || CachedType == ResultType) &&
849 "Cached type doesn't match computed type");
851 if (ShouldUseCache)
852 TypeCache[Ty] = ResultType;
853 return ResultType;
856 bool CodeGenModule::isPaddedAtomicType(QualType type) {
857 return isPaddedAtomicType(type->castAs<AtomicType>());
860 bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
861 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
864 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
865 llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
866 // TagDecl's are not necessarily unique, instead use the (clang)
867 // type connected to the decl.
868 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
870 llvm::StructType *&Entry = RecordDeclTypes[Key];
872 // If we don't have a StructType at all yet, create the forward declaration.
873 if (!Entry) {
874 Entry = llvm::StructType::create(getLLVMContext());
875 addRecordTypeName(RD, Entry, "");
877 llvm::StructType *Ty = Entry;
879 // If this is still a forward declaration, or the LLVM type is already
880 // complete, there's nothing more to do.
881 RD = RD->getDefinition();
882 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
883 return Ty;
885 // If converting this type would cause us to infinitely loop, don't do it!
886 if (!isSafeToConvert(RD, *this)) {
887 DeferredRecords.push_back(RD);
888 return Ty;
891 // Okay, this is a definition of a type. Compile the implementation now.
892 bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
893 (void)InsertResult;
894 assert(InsertResult && "Recursively compiling a struct?");
896 // Force conversion of non-virtual base classes recursively.
897 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
898 for (const auto &I : CRD->bases()) {
899 if (I.isVirtual()) continue;
900 ConvertRecordDeclType(I.getType()->castAs<RecordType>()->getDecl());
904 // Layout fields.
905 std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
906 CGRecordLayouts[Key] = std::move(Layout);
908 // We're done laying out this struct.
909 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
910 assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
912 // If this struct blocked a FunctionType conversion, then recompute whatever
913 // was derived from that.
914 // FIXME: This is hugely overconservative.
915 if (SkippedLayout)
916 TypeCache.clear();
918 // If we're done converting the outer-most record, then convert any deferred
919 // structs as well.
920 if (RecordsBeingLaidOut.empty())
921 while (!DeferredRecords.empty())
922 ConvertRecordDeclType(DeferredRecords.pop_back_val());
924 return Ty;
927 /// getCGRecordLayout - Return record layout info for the given record decl.
928 const CGRecordLayout &
929 CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
930 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
932 auto I = CGRecordLayouts.find(Key);
933 if (I != CGRecordLayouts.end())
934 return *I->second;
935 // Compute the type information.
936 ConvertRecordDeclType(RD);
938 // Now try again.
939 I = CGRecordLayouts.find(Key);
941 assert(I != CGRecordLayouts.end() &&
942 "Unable to find record layout information for type");
943 return *I->second;
946 bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
947 assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
948 return isZeroInitializable(T);
951 bool CodeGenTypes::isZeroInitializable(QualType T) {
952 if (T->getAs<PointerType>())
953 return Context.getTargetNullPointerValue(T) == 0;
955 if (const auto *AT = Context.getAsArrayType(T)) {
956 if (isa<IncompleteArrayType>(AT))
957 return true;
958 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
959 if (Context.getConstantArrayElementCount(CAT) == 0)
960 return true;
961 T = Context.getBaseElementType(T);
964 // Records are non-zero-initializable if they contain any
965 // non-zero-initializable subobjects.
966 if (const RecordType *RT = T->getAs<RecordType>()) {
967 const RecordDecl *RD = RT->getDecl();
968 return isZeroInitializable(RD);
971 // We have to ask the ABI about member pointers.
972 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
973 return getCXXABI().isZeroInitializable(MPT);
975 // Everything else is okay.
976 return true;
979 bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
980 return getCGRecordLayout(RD).isZeroInitializable();
983 unsigned CodeGenTypes::getTargetAddressSpace(QualType T) const {
984 // Return the address space for the type. If the type is a
985 // function type without an address space qualifier, the
986 // program address space is used. Otherwise, the target picks
987 // the best address space based on the type information
988 return T->isFunctionType() && !T.hasAddressSpace()
989 ? getDataLayout().getProgramAddressSpace()
990 : getContext().getTargetAddressSpace(T.getAddressSpace());