clang 22.0.0git
CodeGenTypes.cpp
Go to the documentation of this file.
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 "CGDebugInfo.h"
17#include "CGHLSLRuntime.h"
18#include "CGOpenCLRuntime.h"
19#include "CGRecordLayout.h"
20#include "TargetInfo.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/Expr.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Module.h"
30
31using namespace clang;
32using namespace CodeGen;
33
35 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
36 Target(cgm.getTarget()) {
37 SkippedLayout = false;
38 LongDoubleReferenced = false;
39}
40
42 for (llvm::FoldingSet<CGFunctionInfo>::iterator
43 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
44 delete &*I++;
45}
46
48
50 return CGM.getCodeGenOpts();
51}
52
54 llvm::StructType *Ty,
55 StringRef suffix) {
57 llvm::raw_svector_ostream OS(TypeName);
58 OS << RD->getKindName() << '.';
59
60 // FIXME: We probably want to make more tweaks to the printing policy. For
61 // example, we should probably enable PrintCanonicalTypes and
62 // FullyQualifiedNames.
66
67 // Name the codegen type after the typedef name
68 // if there is no tag type name available
69 if (RD->getIdentifier()) {
70 // FIXME: We should not have to check for a null decl context here.
71 // Right now we do it because the implicit Obj-C decls don't have one.
72 if (RD->getDeclContext())
73 RD->printQualifiedName(OS, Policy);
74 else
75 RD->printName(OS, Policy);
76 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
77 // FIXME: We should not have to check for a null decl context here.
78 // Right now we do it because the implicit Obj-C decls don't have one.
79 if (TDD->getDeclContext())
80 TDD->printQualifiedName(OS, Policy);
81 else
82 TDD->printName(OS);
83 } else
84 OS << "anon";
85
86 if (!suffix.empty())
87 OS << suffix;
88
89 Ty->setName(OS.str());
90}
91
92/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
93/// ConvertType in that it is used to convert to the memory representation for
94/// a type. For example, the scalar representation for _Bool is i1, but the
95/// memory representation is usually i8 or i32, depending on the target.
96///
97/// We generally assume that the alloc size of this type under the LLVM
98/// data layout is the same as the size of the AST type. The alignment
99/// does not have to match: Clang should always use explicit alignments
100/// and packed structs as necessary to produce the layout it needs.
101/// But the size does need to be exactly right or else things like struct
102/// layout will break.
104 if (T->isConstantMatrixType()) {
105 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
106 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
107 return llvm::ArrayType::get(ConvertType(MT->getElementType()),
108 MT->getNumRows() * MT->getNumColumns());
109 }
110
111 llvm::Type *R = ConvertType(T);
112
113 // Check for the boolean vector case.
114 if (T->isExtVectorBoolType()) {
115 auto *FixedVT = cast<llvm::FixedVectorType>(R);
116
117 if (Context.getLangOpts().HLSL) {
118 llvm::Type *IRElemTy = ConvertTypeForMem(Context.BoolTy);
119 return llvm::FixedVectorType::get(IRElemTy, FixedVT->getNumElements());
120 }
121
122 // Pad to at least one byte.
123 uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8);
124 return llvm::IntegerType::get(FixedVT->getContext(), BytePadded);
125 }
126
127 // If T is _Bool or a _BitInt type, ConvertType will produce an IR type
128 // with the exact semantic bit-width of the AST type; for example,
129 // _BitInt(17) will turn into i17. In memory, however, we need to store
130 // such values extended to their full storage size as decided by AST
131 // layout; this is an ABI requirement. Ideally, we would always use an
132 // integer type that's just the bit-size of the AST type; for example, if
133 // sizeof(_BitInt(17)) == 4, _BitInt(17) would turn into i32. That is what's
134 // returned by convertTypeForLoadStore. However, that type does not
135 // always satisfy the size requirement on memory representation types
136 // describe above. For example, a 32-bit platform might reasonably set
137 // sizeof(_BitInt(65)) == 12, but i96 is likely to have to have an alloc size
138 // of 16 bytes in the LLVM data layout. In these cases, we simply return
139 // a byte array of the appropriate size.
140 if (T->isBitIntType()) {
142 return llvm::ArrayType::get(CGM.Int8Ty,
143 Context.getTypeSizeInChars(T).getQuantity());
144 return llvm::IntegerType::get(getLLVMContext(),
145 (unsigned)Context.getTypeSize(T));
146 }
147
148 if (R->isIntegerTy(1))
149 return llvm::IntegerType::get(getLLVMContext(),
150 (unsigned)Context.getTypeSize(T));
151
152 // Else, don't map it.
153 return R;
154}
155
157 llvm::Type *LLVMTy) {
158 if (!LLVMTy)
159 LLVMTy = ConvertType(ASTTy);
160
161 CharUnits ASTSize = Context.getTypeSizeInChars(ASTTy);
162 CharUnits LLVMSize =
164 return ASTSize != LLVMSize;
165}
166
168 llvm::Type *LLVMTy) {
169 if (!LLVMTy)
170 LLVMTy = ConvertType(T);
171
172 if (T->isBitIntType())
173 return llvm::Type::getIntNTy(
175
176 if (LLVMTy->isIntegerTy(1))
177 return llvm::IntegerType::get(getLLVMContext(),
178 (unsigned)Context.getTypeSize(T));
179
180 if (T->isExtVectorBoolType())
181 return ConvertTypeForMem(T);
182
183 return LLVMTy;
184}
185
186/// isRecordLayoutComplete - Return true if the specified type is already
187/// completely laid out.
189 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
190 RecordDeclTypes.find(Ty);
191 return I != RecordDeclTypes.end() && !I->second->isOpaque();
192}
193
194/// isFuncParamTypeConvertible - Return true if the specified type in a
195/// function parameter or result position can be converted to an IR type at this
196/// point. This boils down to being whether it is complete.
198 // Some ABIs cannot have their member pointers represented in IR unless
199 // certain circumstances have been reached.
200 if (const auto *MPT = Ty->getAs<MemberPointerType>())
202
203 // If this isn't a tagged type, we can convert it!
204 const TagType *TT = Ty->getAs<TagType>();
205 if (!TT) return true;
206
207 // Incomplete types cannot be converted.
208 return !TT->isIncompleteType();
209}
210
211
212/// Code to verify a given function type is complete, i.e. the return type
213/// and all of the parameter types are complete. Also check to see if we are in
214/// a RS_StructPointer context, and if so whether any struct types have been
215/// pended. If so, we don't want to ask the ABI lowering code to handle a type
216/// that cannot be converted to an IR type.
219 return false;
220
221 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
222 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
223 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
224 return false;
225
226 return true;
227}
228
229/// UpdateCompletedType - When we find the full definition for a TagDecl,
230/// replace the 'opaque' type we previously made for it if applicable.
233 // If this is an enum being completed, then we flush all non-struct types from
234 // the cache. This allows function types and other things that may be derived
235 // from the enum to be recomputed.
236 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
237 // Only flush the cache if we've actually already converted this type.
238 if (TypeCache.count(T->getTypePtr())) {
239 // Okay, we formed some types based on this. We speculated that the enum
240 // would be lowered to i32, so we only need to flush the cache if this
241 // didn't happen.
242 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
243 TypeCache.clear();
244 }
245 // If necessary, provide the full definition of a type only used with a
246 // declaration so far.
247 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
248 DI->completeType(ED);
249 return;
250 }
251
252 // If we completed a RecordDecl that we previously used and converted to an
253 // anonymous type, then go ahead and complete it now.
254 const RecordDecl *RD = cast<RecordDecl>(TD);
255 if (RD->isDependentType()) return;
256
257 // Only complete it if we converted it already. If we haven't converted it
258 // yet, we'll just do it lazily.
259 if (RecordDeclTypes.count(T.getTypePtr()))
261
262 // If necessary, provide the full definition of a type only used with a
263 // declaration so far.
264 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
265 DI->completeType(RD);
266}
267
269 CanQualType T = Context.getCanonicalTagType(RD);
270 T = Context.getCanonicalType(T);
271
272 const Type *Ty = T.getTypePtr();
273 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
274 TypeCache.clear();
275 RecordsWithOpaqueMemberPointers.clear();
276 }
277}
278
279static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
280 const llvm::fltSemantics &format,
281 bool UseNativeHalf = false) {
282 if (&format == &llvm::APFloat::IEEEhalf()) {
283 if (UseNativeHalf)
284 return llvm::Type::getHalfTy(VMContext);
285 else
286 return llvm::Type::getInt16Ty(VMContext);
287 }
288 if (&format == &llvm::APFloat::BFloat())
289 return llvm::Type::getBFloatTy(VMContext);
290 if (&format == &llvm::APFloat::IEEEsingle())
291 return llvm::Type::getFloatTy(VMContext);
292 if (&format == &llvm::APFloat::IEEEdouble())
293 return llvm::Type::getDoubleTy(VMContext);
294 if (&format == &llvm::APFloat::IEEEquad())
295 return llvm::Type::getFP128Ty(VMContext);
296 if (&format == &llvm::APFloat::PPCDoubleDouble())
297 return llvm::Type::getPPC_FP128Ty(VMContext);
298 if (&format == &llvm::APFloat::x87DoubleExtended())
299 return llvm::Type::getX86_FP80Ty(VMContext);
300 llvm_unreachable("Unknown float format!");
301}
302
303llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
304 assert(QFT.isCanonical());
305 const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
306 // First, check whether we can build the full function type. If the
307 // function type depends on an incomplete type (e.g. a struct or enum), we
308 // cannot lower the function type.
309 if (!isFuncTypeConvertible(FT)) {
310 // This function's type depends on an incomplete tag type.
311
312 // Force conversion of all the relevant record types, to make sure
313 // we re-convert the FunctionType when appropriate.
314 if (const auto *RD = FT->getReturnType()->getAsRecordDecl())
316 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
317 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
318 if (const auto *RD = FPT->getParamType(i)->getAsRecordDecl())
320
321 SkippedLayout = true;
322
323 // Return a placeholder type.
324 return llvm::StructType::get(getLLVMContext());
325 }
326
327 // The function type can be built; call the appropriate routines to
328 // build it.
329 const CGFunctionInfo *FI;
330 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
333 } else {
334 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
337 }
338
339 llvm::Type *ResultType = nullptr;
340 // If there is something higher level prodding our CGFunctionInfo, then
341 // don't recurse into it again.
342 if (FunctionsBeingProcessed.count(FI)) {
343
344 ResultType = llvm::StructType::get(getLLVMContext());
345 SkippedLayout = true;
346 } else {
347
348 // Otherwise, we're good to go, go ahead and convert it.
349 ResultType = GetFunctionType(*FI);
350 }
351
352 return ResultType;
353}
354
355/// ConvertType - Convert the specified type to its LLVM form.
357 T = Context.getCanonicalType(T);
358
359 const Type *Ty = T.getTypePtr();
360
361 // For the device-side compilation, CUDA device builtin surface/texture types
362 // may be represented in different types.
363 if (Context.getLangOpts().CUDAIsDevice) {
365 if (auto *Ty = CGM.getTargetCodeGenInfo()
367 return Ty;
368 } else if (T->isCUDADeviceBuiltinTextureType()) {
369 if (auto *Ty = CGM.getTargetCodeGenInfo()
371 return Ty;
372 }
373 }
374
375 // RecordTypes are cached and processed specially.
376 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
377 return ConvertRecordDeclType(RT->getOriginalDecl()->getDefinitionOrSelf());
378
379 llvm::Type *CachedType = nullptr;
380 auto TCI = TypeCache.find(Ty);
381 if (TCI != TypeCache.end())
382 CachedType = TCI->second;
383 // With expensive checks, check that the type we compute matches the
384 // cached type.
385#ifndef EXPENSIVE_CHECKS
386 if (CachedType)
387 return CachedType;
388#endif
389
390 // If we don't have it in the cache, convert it now.
391 llvm::Type *ResultType = nullptr;
392 switch (Ty->getTypeClass()) {
393 case Type::Record: // Handled above.
394#define TYPE(Class, Base)
395#define ABSTRACT_TYPE(Class, Base)
396#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
397#define DEPENDENT_TYPE(Class, Base) case Type::Class:
398#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
399#include "clang/AST/TypeNodes.inc"
400 llvm_unreachable("Non-canonical or dependent types aren't possible.");
401
402 case Type::Builtin: {
403 switch (cast<BuiltinType>(Ty)->getKind()) {
404 case BuiltinType::Void:
405 case BuiltinType::ObjCId:
406 case BuiltinType::ObjCClass:
407 case BuiltinType::ObjCSel:
408 // LLVM void type can only be used as the result of a function call. Just
409 // map to the same as char.
410 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
411 break;
412
413 case BuiltinType::Bool:
414 // Note that we always return bool as i1 for use as a scalar type.
415 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
416 break;
417
418 case BuiltinType::Char_S:
419 case BuiltinType::Char_U:
420 case BuiltinType::SChar:
421 case BuiltinType::UChar:
422 case BuiltinType::Short:
423 case BuiltinType::UShort:
424 case BuiltinType::Int:
425 case BuiltinType::UInt:
426 case BuiltinType::Long:
427 case BuiltinType::ULong:
428 case BuiltinType::LongLong:
429 case BuiltinType::ULongLong:
430 case BuiltinType::WChar_S:
431 case BuiltinType::WChar_U:
432 case BuiltinType::Char8:
433 case BuiltinType::Char16:
434 case BuiltinType::Char32:
435 case BuiltinType::ShortAccum:
436 case BuiltinType::Accum:
437 case BuiltinType::LongAccum:
438 case BuiltinType::UShortAccum:
439 case BuiltinType::UAccum:
440 case BuiltinType::ULongAccum:
441 case BuiltinType::ShortFract:
442 case BuiltinType::Fract:
443 case BuiltinType::LongFract:
444 case BuiltinType::UShortFract:
445 case BuiltinType::UFract:
446 case BuiltinType::ULongFract:
447 case BuiltinType::SatShortAccum:
448 case BuiltinType::SatAccum:
449 case BuiltinType::SatLongAccum:
450 case BuiltinType::SatUShortAccum:
451 case BuiltinType::SatUAccum:
452 case BuiltinType::SatULongAccum:
453 case BuiltinType::SatShortFract:
454 case BuiltinType::SatFract:
455 case BuiltinType::SatLongFract:
456 case BuiltinType::SatUShortFract:
457 case BuiltinType::SatUFract:
458 case BuiltinType::SatULongFract:
459 ResultType = llvm::IntegerType::get(getLLVMContext(),
460 static_cast<unsigned>(Context.getTypeSize(T)));
461 break;
462
463 case BuiltinType::Float16:
464 ResultType =
466 /* UseNativeHalf = */ true);
467 break;
468
469 case BuiltinType::Half:
470 // Half FP can either be storage-only (lowered to i16) or native.
471 ResultType = getTypeForFormat(
473 Context.getLangOpts().NativeHalfType ||
475 break;
476 case BuiltinType::LongDouble:
477 LongDoubleReferenced = true;
478 [[fallthrough]];
479 case BuiltinType::BFloat16:
480 case BuiltinType::Float:
481 case BuiltinType::Double:
482 case BuiltinType::Float128:
483 case BuiltinType::Ibm128:
484 ResultType = getTypeForFormat(getLLVMContext(),
485 Context.getFloatTypeSemantics(T),
486 /* UseNativeHalf = */ false);
487 break;
488
489 case BuiltinType::NullPtr:
490 // Model std::nullptr_t as i8*
491 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
492 break;
493
494 case BuiltinType::UInt128:
495 case BuiltinType::Int128:
496 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
497 break;
498
499#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
500 case BuiltinType::Id:
501#include "clang/Basic/OpenCLImageTypes.def"
502#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
503 case BuiltinType::Id:
504#include "clang/Basic/OpenCLExtensionTypes.def"
505 case BuiltinType::OCLSampler:
506 case BuiltinType::OCLEvent:
507 case BuiltinType::OCLClkEvent:
508 case BuiltinType::OCLQueue:
509 case BuiltinType::OCLReserveID:
510 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
511 break;
512#define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \
513 case BuiltinType::Id:
514#define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \
515 case BuiltinType::Id:
516#include "clang/Basic/AArch64ACLETypes.def"
517 {
519 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
520 // The `__mfp8` type maps to `<1 x i8>` which can't be used to build
521 // a <N x i8> vector type, hence bypass the call to `ConvertType` for
522 // the element type and create the vector type directly.
523 auto *EltTy = Info.ElementType->isMFloat8Type()
524 ? llvm::Type::getInt8Ty(getLLVMContext())
525 : ConvertType(Info.ElementType);
526 auto *VTy = llvm::VectorType::get(EltTy, Info.EC);
527 switch (Info.NumVectors) {
528 default:
529 llvm_unreachable("Expected 1, 2, 3 or 4 vectors!");
530 case 1:
531 return VTy;
532 case 2:
533 return llvm::StructType::get(VTy, VTy);
534 case 3:
535 return llvm::StructType::get(VTy, VTy, VTy);
536 case 4:
537 return llvm::StructType::get(VTy, VTy, VTy, VTy);
538 }
539 }
540 case BuiltinType::SveCount:
541 return llvm::TargetExtType::get(getLLVMContext(), "aarch64.svcount");
542 case BuiltinType::MFloat8:
543 return llvm::VectorType::get(llvm::Type::getInt8Ty(getLLVMContext()), 1,
544 false);
545#define PPC_VECTOR_TYPE(Name, Id, Size) \
546 case BuiltinType::Id: \
547 ResultType = \
548 llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
549 break;
550#include "clang/Basic/PPCTypes.def"
551#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
552#include "clang/Basic/RISCVVTypes.def"
553 {
555 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
556 if (Info.NumVectors != 1) {
557 unsigned I8EltCount =
558 Info.EC.getKnownMinValue() *
559 ConvertType(Info.ElementType)->getScalarSizeInBits() / 8;
560 return llvm::TargetExtType::get(
561 getLLVMContext(), "riscv.vector.tuple",
562 llvm::ScalableVectorType::get(
563 llvm::Type::getInt8Ty(getLLVMContext()), I8EltCount),
564 Info.NumVectors);
565 }
566 return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
567 Info.EC.getKnownMinValue());
568 }
569#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \
570 case BuiltinType::Id: { \
571 if (BuiltinType::Id == BuiltinType::WasmExternRef) \
572 ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \
573 else \
574 llvm_unreachable("Unexpected wasm reference builtin type!"); \
575 } break;
576#include "clang/Basic/WebAssemblyReferenceTypes.def"
577#define AMDGPU_OPAQUE_PTR_TYPE(Name, Id, SingletonId, Width, Align, AS) \
578 case BuiltinType::Id: \
579 return llvm::PointerType::get(getLLVMContext(), AS);
580#define AMDGPU_NAMED_BARRIER_TYPE(Name, Id, SingletonId, Width, Align, Scope) \
581 case BuiltinType::Id: \
582 return llvm::TargetExtType::get(getLLVMContext(), "amdgcn.named.barrier", \
583 {}, {Scope});
584#include "clang/Basic/AMDGPUTypes.def"
585#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
586#include "clang/Basic/HLSLIntangibleTypes.def"
587 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(Ty);
588 break;
589 case BuiltinType::Dependent:
590#define BUILTIN_TYPE(Id, SingletonId)
591#define PLACEHOLDER_TYPE(Id, SingletonId) \
592 case BuiltinType::Id:
593#include "clang/AST/BuiltinTypes.def"
594 llvm_unreachable("Unexpected placeholder builtin type!");
595 }
596 break;
597 }
598 case Type::Auto:
599 case Type::DeducedTemplateSpecialization:
600 llvm_unreachable("Unexpected undeduced type!");
601 case Type::Complex: {
602 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
603 ResultType = llvm::StructType::get(EltTy, EltTy);
604 break;
605 }
606 case Type::LValueReference:
607 case Type::RValueReference: {
608 const ReferenceType *RTy = cast<ReferenceType>(Ty);
609 QualType ETy = RTy->getPointeeType();
610 unsigned AS = getTargetAddressSpace(ETy);
611 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
612 break;
613 }
614 case Type::Pointer: {
615 const PointerType *PTy = cast<PointerType>(Ty);
616 QualType ETy = PTy->getPointeeType();
617 unsigned AS = getTargetAddressSpace(ETy);
618 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
619 break;
620 }
621
622 case Type::VariableArray: {
623 const VariableArrayType *A = cast<VariableArrayType>(Ty);
624 assert(A->getIndexTypeCVRQualifiers() == 0 &&
625 "FIXME: We only handle trivial array types so far!");
626 // VLAs resolve to the innermost element type; this matches
627 // the return of alloca, and there isn't any obviously better choice.
628 ResultType = ConvertTypeForMem(A->getElementType());
629 break;
630 }
631 case Type::IncompleteArray: {
632 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
633 assert(A->getIndexTypeCVRQualifiers() == 0 &&
634 "FIXME: We only handle trivial array types so far!");
635 // int X[] -> [0 x int], unless the element type is not sized. If it is
636 // unsized (e.g. an incomplete struct) just use [0 x i8].
637 ResultType = ConvertTypeForMem(A->getElementType());
638 if (!ResultType->isSized()) {
639 SkippedLayout = true;
640 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
641 }
642 ResultType = llvm::ArrayType::get(ResultType, 0);
643 break;
644 }
645 case Type::ArrayParameter:
646 case Type::ConstantArray: {
647 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
648 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
649
650 // Lower arrays of undefined struct type to arrays of i8 just to have a
651 // concrete type.
652 if (!EltTy->isSized()) {
653 SkippedLayout = true;
654 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
655 }
656
657 ResultType = llvm::ArrayType::get(EltTy, A->getZExtSize());
658 break;
659 }
660 case Type::ExtVector:
661 case Type::Vector: {
662 const auto *VT = cast<VectorType>(Ty);
663 // An ext_vector_type of Bool is really a vector of bits.
664 llvm::Type *IRElemTy = VT->isPackedVectorBoolType(Context)
665 ? llvm::Type::getInt1Ty(getLLVMContext())
666 : VT->getElementType()->isMFloat8Type()
667 ? llvm::Type::getInt8Ty(getLLVMContext())
668 : ConvertType(VT->getElementType());
669 ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
670 break;
671 }
672 case Type::ConstantMatrix: {
673 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Ty);
674 ResultType =
675 llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
676 MT->getNumRows() * MT->getNumColumns());
677 break;
678 }
679 case Type::FunctionNoProto:
680 case Type::FunctionProto:
681 ResultType = ConvertFunctionTypeInternal(T);
682 break;
683 case Type::ObjCObject:
684 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
685 break;
686
687 case Type::ObjCInterface: {
688 // Objective-C interfaces are always opaque (outside of the
689 // runtime, which can do whatever it likes); we never refine
690 // these.
691 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
692 if (!T)
693 T = llvm::StructType::create(getLLVMContext());
694 ResultType = T;
695 break;
696 }
697
698 case Type::ObjCObjectPointer:
699 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
700 break;
701
702 case Type::Enum: {
703 const auto *ED = Ty->castAsEnumDecl();
704 if (ED->isCompleteDefinition() || ED->isFixed())
705 return ConvertType(ED->getIntegerType());
706 // Return a placeholder 'i32' type. This can be changed later when the
707 // type is defined (see UpdateCompletedType), but is likely to be the
708 // "right" answer.
709 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
710 break;
711 }
712
713 case Type::BlockPointer: {
714 // Block pointers lower to function type. For function type,
715 // getTargetAddressSpace() returns default address space for
716 // function pointer i.e. program address space. Therefore, for block
717 // pointers, it is important to pass the pointee AST address space when
718 // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
719 // address space for data pointers and not function pointers.
720 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
721 unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace());
722 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
723 break;
724 }
725
726 case Type::MemberPointer: {
727 auto *MPTy = cast<MemberPointerType>(Ty);
728 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
730 MPTy->getMostRecentCXXRecordDecl());
731 auto Insertion =
732 RecordsWithOpaqueMemberPointers.try_emplace(T.getTypePtr());
733 if (Insertion.second)
734 Insertion.first->second = llvm::StructType::create(getLLVMContext());
735 ResultType = Insertion.first->second;
736 } else {
737 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
738 }
739 break;
740 }
741
742 case Type::Atomic: {
743 QualType valueType = cast<AtomicType>(Ty)->getValueType();
744 ResultType = ConvertTypeForMem(valueType);
745
746 // Pad out to the inflated size if necessary.
747 uint64_t valueSize = Context.getTypeSize(valueType);
748 uint64_t atomicSize = Context.getTypeSize(Ty);
749 if (valueSize != atomicSize) {
750 assert(valueSize < atomicSize);
751 llvm::Type *elts[] = {
752 ResultType,
753 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
754 };
755 ResultType =
756 llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts));
757 }
758 break;
759 }
760 case Type::Pipe: {
761 ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
762 break;
763 }
764 case Type::BitInt: {
765 const auto &EIT = cast<BitIntType>(Ty);
766 ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
767 break;
768 }
769 case Type::HLSLAttributedResource:
770 case Type::HLSLInlineSpirv:
771 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(Ty);
772 break;
773 }
774
775 assert(ResultType && "Didn't convert a type?");
776 assert((!CachedType || CachedType == ResultType) &&
777 "Cached type doesn't match computed type");
778
779 TypeCache[Ty] = ResultType;
780 return ResultType;
781}
782
784 return isPaddedAtomicType(type->castAs<AtomicType>());
785}
786
788 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
789}
790
791/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
793 // TagDecl's are not necessarily unique, instead use the (clang)
794 // type connected to the decl.
795 const Type *Key = Context.getCanonicalTagType(RD).getTypePtr();
796
797 llvm::StructType *&Entry = RecordDeclTypes[Key];
798
799 // If we don't have a StructType at all yet, create the forward declaration.
800 if (!Entry) {
801 Entry = llvm::StructType::create(getLLVMContext());
802 addRecordTypeName(RD, Entry, "");
803 }
804 llvm::StructType *Ty = Entry;
805
806 // If this is still a forward declaration, or the LLVM type is already
807 // complete, there's nothing more to do.
808 RD = RD->getDefinition();
809 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
810 return Ty;
811
812 // Force conversion of non-virtual base classes recursively.
813 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
814 for (const auto &I : CRD->bases()) {
815 if (I.isVirtual()) continue;
816 ConvertRecordDeclType(I.getType()->castAsRecordDecl());
817 }
818 }
819
820 // Layout fields.
821 std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
822 CGRecordLayouts[Key] = std::move(Layout);
823
824 // If this struct blocked a FunctionType conversion, then recompute whatever
825 // was derived from that.
826 // FIXME: This is hugely overconservative.
827 if (SkippedLayout)
828 TypeCache.clear();
829
830 return Ty;
831}
832
833/// getCGRecordLayout - Return record layout info for the given record decl.
834const CGRecordLayout &
836 const Type *Key = Context.getCanonicalTagType(RD).getTypePtr();
837
838 auto I = CGRecordLayouts.find(Key);
839 if (I != CGRecordLayouts.end())
840 return *I->second;
841 // Compute the type information.
843
844 // Now try again.
845 I = CGRecordLayouts.find(Key);
846
847 assert(I != CGRecordLayouts.end() &&
848 "Unable to find record layout information for type");
849 return *I->second;
850}
851
853 assert((T->isAnyPointerType() || T->isBlockPointerType() ||
854 T->isNullPtrType()) &&
855 "Invalid type");
856 return isZeroInitializable(T);
857}
858
860 if (T->getAs<PointerType>() || T->isNullPtrType())
861 return Context.getTargetNullPointerValue(T) == 0;
862
863 if (const auto *AT = Context.getAsArrayType(T)) {
864 if (isa<IncompleteArrayType>(AT))
865 return true;
866 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
867 if (Context.getConstantArrayElementCount(CAT) == 0)
868 return true;
869 T = Context.getBaseElementType(T);
870 }
871
872 // Records are non-zero-initializable if they contain any
873 // non-zero-initializable subobjects.
874 if (const auto *RD = T->getAsRecordDecl())
875 return isZeroInitializable(RD);
876
877 // We have to ask the ABI about member pointers.
878 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
879 return getCXXABI().isZeroInitializable(MPT);
880
881 // HLSL Inline SPIR-V types are non-zero-initializable.
883 return false;
884
885 // Everything else is okay.
886 return true;
887}
888
891}
892
894 // Return the address space for the type. If the type is a
895 // function type without an address space qualifier, the
896 // program address space is used. Otherwise, the target picks
897 // the best address space based on the type information
898 return T->isFunctionType() && !T.hasAddressSpace()
899 ? getDataLayout().getProgramAddressSpace()
900 : getContext().getTargetAddressSpace(T.getAddressSpace());
901}
Defines the clang::ASTContext interface.
Expr * E
static llvm::Type * getTypeForFormat(llvm::LLVMContext &VMContext, const llvm::fltSemantics &format, bool UseNativeHalf=false)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:1192
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Target Target
Definition: MachO.h:51
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
BuiltinVectorTypeInfo getBuiltinVectorTypeInfo(const BuiltinType *VecTy) const
Returns the element type, element count and number of vectors (in case of tuple) for a builtin vector...
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
uint64_t getTargetNullPointerValue(QualType QT) const
Get target-dependent integer value for null pointer which is used for constant folding.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2851
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
CanQualType BoolTy
Definition: ASTContext.h:1223
const clang::PrintingPolicy & getPrintingPolicy() const
Definition: ASTContext.h:793
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2625
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:859
uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const
Return number of constant array elements.
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
QualType getElementType() const
Definition: TypeBase.h:3750
unsigned getIndexTypeCVRQualifiers() const
Definition: TypeBase.h:3760
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:84
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Implements C++ ABI-specific code generation functions.
Definition: CGCXXABI.h:43
virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const
Return whether or not a member pointers type is convertible to an IR type.
Definition: CGCXXABI.h:213
virtual llvm::Type * ConvertMemberPointerType(const MemberPointerType *MPT)
Find the LLVM type used to represent the given member pointer type.
Definition: CGCXXABI.cpp:43
virtual bool isZeroInitializable(const MemberPointerType *MPT)
Return true if the given member pointer can be zero-initialized (in the C++ sense) with an LLVM zeroi...
Definition: CGCXXABI.cpp:121
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:59
CGFunctionInfo - Class to encapsulate the information about a function definition.
llvm::Type * convertHLSLSpecificType(const Type *T, SmallVector< int32_t > *Packoffsets=nullptr)
virtual llvm::Type * getPipeType(const PipeType *T, StringRef Name, llvm::Type *&PipeTy)
virtual llvm::Type * convertOpenCLSpecificType(const Type *T)
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
bool isZeroInitializable() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer.
This class organizes the cross-function state that is used while generating LLVM code.
CGHLSLRuntime & getHLSLRuntime()
Return a reference to the configured HLSL runtime.
CGDebugInfo * getModuleDebugInfo()
bool isPaddedAtomicType(QualType type)
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
CGCXXABI & getCXXABI() const
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
const CodeGenOptions & getCodeGenOpts() const
CodeGenTypes(CodeGenModule &cgm)
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
CGCXXABI & getCXXABI() const
bool isPointerZeroInitializable(QualType T)
Check if the pointer type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
const CodeGenOptions & getCodeGenOpts() const
ASTContext & getContext() const
Definition: CodeGenTypes.h:103
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition: CGCall.cpp:249
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1702
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
llvm::Type * convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy=nullptr)
Given that T is a scalar type, return the IR type that should be used for load and store operations.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
unsigned getTargetAddressSpace(QualType T) const
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
bool isRecordLayoutComplete(const Type *Ty) const
isRecordLayoutComplete - Return true if the specified type is already completely laid out.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
CodeGenModule & getCGM() const
Definition: CodeGenTypes.h:102
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenTypes.h:106
bool typeRequiresSplitIntoByteArray(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
Check whether the given type needs to be laid out in memory using an opaque byte-array type because i...
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenTypes.h:99
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
virtual llvm::Type * getCUDADeviceBuiltinSurfaceDeviceType() const
Return the device-side type for the CUDA device builtin surface type.
Definition: TargetInfo.h:408
virtual llvm::Type * getCUDADeviceBuiltinTextureDeviceType() const
Return the device-side type for the CUDA device builtin texture type.
Definition: TargetInfo.h:413
Represents the canonical version of C arrays with a specified constant size.
Definition: TypeBase.h:3776
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition: TypeBase.h:3852
Represents a concrete matrix type with constant number of rows and columns.
Definition: TypeBase.h:4389
unsigned getNumColumns() const
Returns the number of columns in the matrix.
Definition: TypeBase.h:4410
unsigned getNumRows() const
Returns the number of rows in the matrix.
Definition: TypeBase.h:4407
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:524
DeclContext * getDeclContext()
Definition: DeclBase.h:448
Represents an enum.
Definition: Decl.h:4004
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition: TypeBase.h:4860
Represents a prototype with parameter type info, e.g.
Definition: TypeBase.h:5282
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: TypeBase.h:4478
QualType getReturnType() const
Definition: TypeBase.h:4818
Represents an arbitrary, user-specified SPIR-V type instruction.
Definition: TypeBase.h:6860
Represents a C array with an unspecified size.
Definition: TypeBase.h:3925
QualType getElementType() const
Returns type of the elements being stored in the matrix.
Definition: TypeBase.h:4367
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: TypeBase.h:3669
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:294
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
Definition: Decl.cpp:1687
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: TypeBase.h:3346
QualType getPointeeType() const
Definition: TypeBase.h:3356
A (possibly-)qualified type.
Definition: TypeBase.h:937
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: TypeBase.h:8343
LangAS getAddressSpace() const
Return the address space of this type.
Definition: TypeBase.h:8469
bool isCanonical() const
Definition: TypeBase.h:8400
Represents a struct/union/class.
Definition: Decl.h:4309
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4493
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
Base for LValueReferenceType and RValueReferenceType.
Definition: TypeBase.h:3589
QualType getPointeeType() const
Definition: TypeBase.h:3607
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3714
StringRef getKindName() const
Definition: Decl.h:3904
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3809
TypedefNameDecl * getTypedefNameForAnonDecl() const
Definition: Decl.h:3945
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override
Pretty-print the unqualified name of this declaration.
Definition: Decl.cpp:4904
bool isDependentType() const
Whether this declaration declares a type that is dependent, i.e., a type that somehow depends on temp...
Definition: Decl.h:3854
virtual bool useFP16ConversionIntrinsics() const
Check whether llvm intrinsics such as llvm.convert.to.fp16 should be used to convert to and from __fp...
Definition: TargetInfo.h:1015
The base class of the type hierarchy.
Definition: TypeBase.h:1833
bool isBlockPointerType() const
Definition: TypeBase.h:8600
bool isMFloat8Type() const
Definition: TypeBase.h:8961
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.h:41
bool isConstantMatrixType() const
Definition: TypeBase.h:8741
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition: Type.cpp:5379
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:752
bool isExtVectorBoolType() const
Definition: TypeBase.h:8727
bool isBitIntType() const
Definition: TypeBase.h:8845
EnumDecl * castAsEnumDecl() const
Definition: Type.h:59
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition: Type.cpp:5388
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2440
bool isFunctionType() const
Definition: TypeBase.h:8576
bool isAnyPointerType() const
Definition: TypeBase.h:8588
TypeClass getTypeClass() const
Definition: TypeBase.h:2403
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
bool isNullPtrType() const
Definition: TypeBase.h:8973
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3559
Represents a C array with a specified size that is not an integer-constant-expression.
Definition: TypeBase.h:3982
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
unsigned SuppressInlineNamespace
Suppress printing parts of scope specifiers that correspond to inline namespaces.