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