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