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

source code of clang/lib/CodeGen/CodeGenTypes.cpp