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