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