1 //===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This provides C++ code generation targeting the Itanium C++ ABI. The class 11 // in this file generates structures that follow the Itanium C++ ABI, which is 12 // documented at: 13 // http://www.codesourcery.com/public/cxx-abi/abi.html 14 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html 15 // 16 // It also supports the closely-related ARM ABI, documented at: 17 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "CGCXXABI.h" 22 #include "CGCleanup.h" 23 #include "CGRecordLayout.h" 24 #include "CGVTables.h" 25 #include "CodeGenFunction.h" 26 #include "CodeGenModule.h" 27 #include "TargetInfo.h" 28 #include "clang/CodeGen/ConstantInitBuilder.h" 29 #include "clang/AST/Mangle.h" 30 #include "clang/AST/Type.h" 31 #include "clang/AST/StmtCXX.h" 32 #include "llvm/IR/CallSite.h" 33 #include "llvm/IR/DataLayout.h" 34 #include "llvm/IR/Instructions.h" 35 #include "llvm/IR/Intrinsics.h" 36 #include "llvm/IR/Value.h" 37 38 using namespace clang; 39 using namespace CodeGen; 40 41 namespace { 42 class ItaniumCXXABI : public CodeGen::CGCXXABI { 43 /// VTables - All the vtables which have been defined. 44 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; 45 46 protected: 47 bool UseARMMethodPtrABI; 48 bool UseARMGuardVarABI; 49 bool Use32BitVTableOffsetABI; 50 51 ItaniumMangleContext &getMangleContext() { 52 return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext()); 53 } 54 55 public: 56 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, 57 bool UseARMMethodPtrABI = false, 58 bool UseARMGuardVarABI = false) : 59 CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI), 60 UseARMGuardVarABI(UseARMGuardVarABI), 61 Use32BitVTableOffsetABI(false) { } 62 63 bool classifyReturnType(CGFunctionInfo &FI) const override; 64 65 bool passClassIndirect(const CXXRecordDecl *RD) const { 66 // Clang <= 4 used the pre-C++11 rule, which ignores move operations. 67 // The PS4 platform ABI follows the behavior of Clang 3.2. 68 if (CGM.getCodeGenOpts().getClangABICompat() <= 69 CodeGenOptions::ClangABI::Ver4 || 70 CGM.getTriple().getOS() == llvm::Triple::PS4) 71 return RD->hasNonTrivialDestructor() || 72 RD->hasNonTrivialCopyConstructor(); 73 return !canCopyArgument(RD); 74 } 75 76 RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override { 77 // If C++ prohibits us from making a copy, pass by address. 78 if (passClassIndirect(RD)) 79 return RAA_Indirect; 80 return RAA_Default; 81 } 82 83 bool isThisCompleteObject(GlobalDecl GD) const override { 84 // The Itanium ABI has separate complete-object vs. base-object 85 // variants of both constructors and destructors. 86 if (isa<CXXDestructorDecl>(GD.getDecl())) { 87 switch (GD.getDtorType()) { 88 case Dtor_Complete: 89 case Dtor_Deleting: 90 return true; 91 92 case Dtor_Base: 93 return false; 94 95 case Dtor_Comdat: 96 llvm_unreachable("emitting dtor comdat as function?"); 97 } 98 llvm_unreachable("bad dtor kind"); 99 } 100 if (isa<CXXConstructorDecl>(GD.getDecl())) { 101 switch (GD.getCtorType()) { 102 case Ctor_Complete: 103 return true; 104 105 case Ctor_Base: 106 return false; 107 108 case Ctor_CopyingClosure: 109 case Ctor_DefaultClosure: 110 llvm_unreachable("closure ctors in Itanium ABI?"); 111 112 case Ctor_Comdat: 113 llvm_unreachable("emitting ctor comdat as function?"); 114 } 115 llvm_unreachable("bad dtor kind"); 116 } 117 118 // No other kinds. 119 return false; 120 } 121 122 bool isZeroInitializable(const MemberPointerType *MPT) override; 123 124 llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; 125 126 CGCallee 127 EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 128 const Expr *E, 129 Address This, 130 llvm::Value *&ThisPtrForCall, 131 llvm::Value *MemFnPtr, 132 const MemberPointerType *MPT) override; 133 134 llvm::Value * 135 EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, 136 Address Base, 137 llvm::Value *MemPtr, 138 const MemberPointerType *MPT) override; 139 140 llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 141 const CastExpr *E, 142 llvm::Value *Src) override; 143 llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 144 llvm::Constant *Src) override; 145 146 llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; 147 148 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override; 149 llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 150 CharUnits offset) override; 151 llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; 152 llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, 153 CharUnits ThisAdjustment); 154 155 llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, 156 llvm::Value *L, llvm::Value *R, 157 const MemberPointerType *MPT, 158 bool Inequality) override; 159 160 llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 161 llvm::Value *Addr, 162 const MemberPointerType *MPT) override; 163 164 void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, 165 Address Ptr, QualType ElementType, 166 const CXXDestructorDecl *Dtor) override; 167 168 /// Itanium says that an _Unwind_Exception has to be "double-word" 169 /// aligned (and thus the end of it is also so-aligned), meaning 16 170 /// bytes. Of course, that was written for the actual Itanium, 171 /// which is a 64-bit platform. Classically, the ABI doesn't really 172 /// specify the alignment on other platforms, but in practice 173 /// libUnwind declares the struct with __attribute__((aligned)), so 174 /// we assume that alignment here. (It's generally 16 bytes, but 175 /// some targets overwrite it.) 176 CharUnits getAlignmentOfExnObject() { 177 auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned(); 178 return CGM.getContext().toCharUnitsFromBits(align); 179 } 180 181 void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; 182 void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override; 183 184 void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override; 185 186 llvm::CallInst * 187 emitTerminateForUnexpectedException(CodeGenFunction &CGF, 188 llvm::Value *Exn) override; 189 190 void EmitFundamentalRTTIDescriptor(QualType Type, bool DLLExport); 191 void EmitFundamentalRTTIDescriptors(bool DLLExport); 192 llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; 193 CatchTypeInfo 194 getAddrOfCXXCatchHandlerType(QualType Ty, 195 QualType CatchHandlerType) override { 196 return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0}; 197 } 198 199 bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; 200 void EmitBadTypeidCall(CodeGenFunction &CGF) override; 201 llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, 202 Address ThisPtr, 203 llvm::Type *StdTypeInfoPtrTy) override; 204 205 bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 206 QualType SrcRecordTy) override; 207 208 llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value, 209 QualType SrcRecordTy, QualType DestTy, 210 QualType DestRecordTy, 211 llvm::BasicBlock *CastEnd) override; 212 213 llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value, 214 QualType SrcRecordTy, 215 QualType DestTy) override; 216 217 bool EmitBadCastCall(CodeGenFunction &CGF) override; 218 219 llvm::Value * 220 GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This, 221 const CXXRecordDecl *ClassDecl, 222 const CXXRecordDecl *BaseClassDecl) override; 223 224 void EmitCXXConstructors(const CXXConstructorDecl *D) override; 225 226 AddedStructorArgs 227 buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 228 SmallVectorImpl<CanQualType> &ArgTys) override; 229 230 bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, 231 CXXDtorType DT) const override { 232 // Itanium does not emit any destructor variant as an inline thunk. 233 // Delegating may occur as an optimization, but all variants are either 234 // emitted with external linkage or as linkonce if they are inline and used. 235 return false; 236 } 237 238 void EmitCXXDestructors(const CXXDestructorDecl *D) override; 239 240 void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, 241 FunctionArgList &Params) override; 242 243 void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; 244 245 AddedStructorArgs 246 addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, 247 CXXCtorType Type, bool ForVirtualBase, 248 bool Delegating, CallArgList &Args) override; 249 250 void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, 251 CXXDtorType Type, bool ForVirtualBase, 252 bool Delegating, Address This) override; 253 254 void emitVTableDefinitions(CodeGenVTables &CGVT, 255 const CXXRecordDecl *RD) override; 256 257 bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF, 258 CodeGenFunction::VPtr Vptr) override; 259 260 bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override { 261 return true; 262 } 263 264 llvm::Constant * 265 getVTableAddressPoint(BaseSubobject Base, 266 const CXXRecordDecl *VTableClass) override; 267 268 llvm::Value *getVTableAddressPointInStructor( 269 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, 270 BaseSubobject Base, const CXXRecordDecl *NearestVBase) override; 271 272 llvm::Value *getVTableAddressPointInStructorWithVTT( 273 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, 274 BaseSubobject Base, const CXXRecordDecl *NearestVBase); 275 276 llvm::Constant * 277 getVTableAddressPointForConstExpr(BaseSubobject Base, 278 const CXXRecordDecl *VTableClass) override; 279 280 llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, 281 CharUnits VPtrOffset) override; 282 283 CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, 284 Address This, llvm::Type *Ty, 285 SourceLocation Loc) override; 286 287 llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, 288 const CXXDestructorDecl *Dtor, 289 CXXDtorType DtorType, 290 Address This, 291 const CXXMemberCallExpr *CE) override; 292 293 void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; 294 295 bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override; 296 297 void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD, 298 bool ReturnAdjustment) override { 299 // Allow inlining of thunks by emitting them with available_externally 300 // linkage together with vtables when needed. 301 if (ForVTable && !Thunk->hasLocalLinkage()) 302 Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); 303 304 // Propagate dllexport storage, to enable the linker to generate import 305 // thunks as necessary (e.g. when a parent class has a key function and a 306 // child class doesn't, and the construction vtable for the parent in the 307 // child needs to reference the parent's thunks). 308 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 309 if (MD->hasAttr<DLLExportAttr>()) 310 Thunk->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 311 } 312 313 llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This, 314 const ThisAdjustment &TA) override; 315 316 llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret, 317 const ReturnAdjustment &RA) override; 318 319 size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, 320 FunctionArgList &Args) const override { 321 assert(!Args.empty() && "expected the arglist to not be empty!"); 322 return Args.size() - 1; 323 } 324 325 StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; } 326 StringRef GetDeletedVirtualCallName() override 327 { return "__cxa_deleted_virtual"; } 328 329 CharUnits getArrayCookieSizeImpl(QualType elementType) override; 330 Address InitializeArrayCookie(CodeGenFunction &CGF, 331 Address NewPtr, 332 llvm::Value *NumElements, 333 const CXXNewExpr *expr, 334 QualType ElementType) override; 335 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 336 Address allocPtr, 337 CharUnits cookieSize) override; 338 339 void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 340 llvm::GlobalVariable *DeclPtr, 341 bool PerformInit) override; 342 void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 343 llvm::Constant *dtor, llvm::Constant *addr) override; 344 345 llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD, 346 llvm::Value *Val); 347 void EmitThreadLocalInitFuncs( 348 CodeGenModule &CGM, 349 ArrayRef<const VarDecl *> CXXThreadLocals, 350 ArrayRef<llvm::Function *> CXXThreadLocalInits, 351 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) override; 352 353 bool usesThreadWrapperFunction() const override { return true; } 354 LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, 355 QualType LValType) override; 356 357 bool NeedsVTTParameter(GlobalDecl GD) override; 358 359 /**************************** RTTI Uniqueness ******************************/ 360 361 protected: 362 /// Returns true if the ABI requires RTTI type_info objects to be unique 363 /// across a program. 364 virtual bool shouldRTTIBeUnique() const { return true; } 365 366 public: 367 /// What sort of unique-RTTI behavior should we use? 368 enum RTTIUniquenessKind { 369 /// We are guaranteeing, or need to guarantee, that the RTTI string 370 /// is unique. 371 RUK_Unique, 372 373 /// We are not guaranteeing uniqueness for the RTTI string, so we 374 /// can demote to hidden visibility but must use string comparisons. 375 RUK_NonUniqueHidden, 376 377 /// We are not guaranteeing uniqueness for the RTTI string, so we 378 /// have to use string comparisons, but we also have to emit it with 379 /// non-hidden visibility. 380 RUK_NonUniqueVisible 381 }; 382 383 /// Return the required visibility status for the given type and linkage in 384 /// the current ABI. 385 RTTIUniquenessKind 386 classifyRTTIUniqueness(QualType CanTy, 387 llvm::GlobalValue::LinkageTypes Linkage) const; 388 friend class ItaniumRTTIBuilder; 389 390 void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; 391 392 private: 393 bool hasAnyUnusedVirtualInlineFunction(const CXXRecordDecl *RD) const { 394 const auto &VtableLayout = 395 CGM.getItaniumVTableContext().getVTableLayout(RD); 396 397 for (const auto &VtableComponent : VtableLayout.vtable_components()) { 398 // Skip empty slot. 399 if (!VtableComponent.isUsedFunctionPointerKind()) 400 continue; 401 402 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl(); 403 if (!Method->getCanonicalDecl()->isInlined()) 404 continue; 405 406 StringRef Name = CGM.getMangledName(VtableComponent.getGlobalDecl()); 407 auto *Entry = CGM.GetGlobalValue(Name); 408 // This checks if virtual inline function has already been emitted. 409 // Note that it is possible that this inline function would be emitted 410 // after trying to emit vtable speculatively. Because of this we do 411 // an extra pass after emitting all deferred vtables to find and emit 412 // these vtables opportunistically. 413 if (!Entry || Entry->isDeclaration()) 414 return true; 415 } 416 return false; 417 } 418 419 bool isVTableHidden(const CXXRecordDecl *RD) const { 420 const auto &VtableLayout = 421 CGM.getItaniumVTableContext().getVTableLayout(RD); 422 423 for (const auto &VtableComponent : VtableLayout.vtable_components()) { 424 if (VtableComponent.isRTTIKind()) { 425 const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl(); 426 if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility) 427 return true; 428 } else if (VtableComponent.isUsedFunctionPointerKind()) { 429 const CXXMethodDecl *Method = VtableComponent.getFunctionDecl(); 430 if (Method->getVisibility() == Visibility::HiddenVisibility && 431 !Method->isDefined()) 432 return true; 433 } 434 } 435 return false; 436 } 437 }; 438 439 class ARMCXXABI : public ItaniumCXXABI { 440 public: 441 ARMCXXABI(CodeGen::CodeGenModule &CGM) : 442 ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, 443 /* UseARMGuardVarABI = */ true) {} 444 445 bool HasThisReturn(GlobalDecl GD) const override { 446 return (isa<CXXConstructorDecl>(GD.getDecl()) || ( 447 isa<CXXDestructorDecl>(GD.getDecl()) && 448 GD.getDtorType() != Dtor_Deleting)); 449 } 450 451 void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, 452 QualType ResTy) override; 453 454 CharUnits getArrayCookieSizeImpl(QualType elementType) override; 455 Address InitializeArrayCookie(CodeGenFunction &CGF, 456 Address NewPtr, 457 llvm::Value *NumElements, 458 const CXXNewExpr *expr, 459 QualType ElementType) override; 460 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr, 461 CharUnits cookieSize) override; 462 }; 463 464 class iOS64CXXABI : public ARMCXXABI { 465 public: 466 iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) { 467 Use32BitVTableOffsetABI = true; 468 } 469 470 // ARM64 libraries are prepared for non-unique RTTI. 471 bool shouldRTTIBeUnique() const override { return false; } 472 }; 473 474 class WebAssemblyCXXABI final : public ItaniumCXXABI { 475 public: 476 explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM) 477 : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true, 478 /*UseARMGuardVarABI=*/true) {} 479 480 private: 481 bool HasThisReturn(GlobalDecl GD) const override { 482 return isa<CXXConstructorDecl>(GD.getDecl()) || 483 (isa<CXXDestructorDecl>(GD.getDecl()) && 484 GD.getDtorType() != Dtor_Deleting); 485 } 486 bool canCallMismatchedFunctionType() const override { return false; } 487 }; 488 } 489 490 CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { 491 switch (CGM.getTarget().getCXXABI().getKind()) { 492 // For IR-generation purposes, there's no significant difference 493 // between the ARM and iOS ABIs. 494 case TargetCXXABI::GenericARM: 495 case TargetCXXABI::iOS: 496 case TargetCXXABI::WatchOS: 497 return new ARMCXXABI(CGM); 498 499 case TargetCXXABI::iOS64: 500 return new iOS64CXXABI(CGM); 501 502 // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't 503 // include the other 32-bit ARM oddities: constructor/destructor return values 504 // and array cookies. 505 case TargetCXXABI::GenericAArch64: 506 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, 507 /* UseARMGuardVarABI = */ true); 508 509 case TargetCXXABI::GenericMIPS: 510 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true); 511 512 case TargetCXXABI::WebAssembly: 513 return new WebAssemblyCXXABI(CGM); 514 515 case TargetCXXABI::GenericItanium: 516 if (CGM.getContext().getTargetInfo().getTriple().getArch() 517 == llvm::Triple::le32) { 518 // For PNaCl, use ARM-style method pointers so that PNaCl code 519 // does not assume anything about the alignment of function 520 // pointers. 521 return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, 522 /* UseARMGuardVarABI = */ false); 523 } 524 return new ItaniumCXXABI(CGM); 525 526 case TargetCXXABI::Microsoft: 527 llvm_unreachable("Microsoft ABI is not Itanium-based"); 528 } 529 llvm_unreachable("bad ABI kind"); 530 } 531 532 llvm::Type * 533 ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 534 if (MPT->isMemberDataPointer()) 535 return CGM.PtrDiffTy; 536 return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy); 537 } 538 539 /// In the Itanium and ARM ABIs, method pointers have the form: 540 /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; 541 /// 542 /// In the Itanium ABI: 543 /// - method pointers are virtual if (memptr.ptr & 1) is nonzero 544 /// - the this-adjustment is (memptr.adj) 545 /// - the virtual offset is (memptr.ptr - 1) 546 /// 547 /// In the ARM ABI: 548 /// - method pointers are virtual if (memptr.adj & 1) is nonzero 549 /// - the this-adjustment is (memptr.adj >> 1) 550 /// - the virtual offset is (memptr.ptr) 551 /// ARM uses 'adj' for the virtual flag because Thumb functions 552 /// may be only single-byte aligned. 553 /// 554 /// If the member is virtual, the adjusted 'this' pointer points 555 /// to a vtable pointer from which the virtual offset is applied. 556 /// 557 /// If the member is non-virtual, memptr.ptr is the address of 558 /// the function to call. 559 CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( 560 CodeGenFunction &CGF, const Expr *E, Address ThisAddr, 561 llvm::Value *&ThisPtrForCall, 562 llvm::Value *MemFnPtr, const MemberPointerType *MPT) { 563 CGBuilderTy &Builder = CGF.Builder; 564 565 const FunctionProtoType *FPT = 566 MPT->getPointeeType()->getAs<FunctionProtoType>(); 567 const CXXRecordDecl *RD = 568 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 569 570 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType( 571 CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr)); 572 573 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); 574 575 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); 576 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); 577 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); 578 579 // Extract memptr.adj, which is in the second field. 580 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); 581 582 // Compute the true adjustment. 583 llvm::Value *Adj = RawAdj; 584 if (UseARMMethodPtrABI) 585 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); 586 587 // Apply the adjustment and cast back to the original struct type 588 // for consistency. 589 llvm::Value *This = ThisAddr.getPointer(); 590 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 591 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); 592 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 593 ThisPtrForCall = This; 594 595 // Load the function pointer. 596 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); 597 598 // If the LSB in the function pointer is 1, the function pointer points to 599 // a virtual function. 600 llvm::Value *IsVirtual; 601 if (UseARMMethodPtrABI) 602 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); 603 else 604 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); 605 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); 606 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 607 608 // In the virtual path, the adjustment left 'This' pointing to the 609 // vtable of the correct base subobject. The "function pointer" is an 610 // offset within the vtable (+1 for the virtual flag on non-ARM). 611 CGF.EmitBlock(FnVirtual); 612 613 // Cast the adjusted this to a pointer to vtable pointer and load. 614 llvm::Type *VTableTy = Builder.getInt8PtrTy(); 615 CharUnits VTablePtrAlign = 616 CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD, 617 CGF.getPointerAlign()); 618 llvm::Value *VTable = 619 CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD); 620 621 // Apply the offset. 622 // On ARM64, to reserve extra space in virtual member function pointers, 623 // we only pay attention to the low 32 bits of the offset. 624 llvm::Value *VTableOffset = FnAsInt; 625 if (!UseARMMethodPtrABI) 626 VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); 627 if (Use32BitVTableOffsetABI) { 628 VTableOffset = Builder.CreateTrunc(VTableOffset, CGF.Int32Ty); 629 VTableOffset = Builder.CreateZExt(VTableOffset, CGM.PtrDiffTy); 630 } 631 VTable = Builder.CreateGEP(VTable, VTableOffset); 632 633 // Load the virtual function to call. 634 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); 635 llvm::Value *VirtualFn = 636 Builder.CreateAlignedLoad(VTable, CGF.getPointerAlign(), 637 "memptr.virtualfn"); 638 CGF.EmitBranch(FnEnd); 639 640 // In the non-virtual path, the function pointer is actually a 641 // function pointer. 642 CGF.EmitBlock(FnNonVirtual); 643 llvm::Value *NonVirtualFn = 644 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); 645 646 // We're done. 647 CGF.EmitBlock(FnEnd); 648 llvm::PHINode *CalleePtr = Builder.CreatePHI(FTy->getPointerTo(), 2); 649 CalleePtr->addIncoming(VirtualFn, FnVirtual); 650 CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual); 651 652 CGCallee Callee(FPT, CalleePtr); 653 return Callee; 654 } 655 656 /// Compute an l-value by applying the given pointer-to-member to a 657 /// base object. 658 llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( 659 CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr, 660 const MemberPointerType *MPT) { 661 assert(MemPtr->getType() == CGM.PtrDiffTy); 662 663 CGBuilderTy &Builder = CGF.Builder; 664 665 // Cast to char*. 666 Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty); 667 668 // Apply the offset, which we assume is non-null. 669 llvm::Value *Addr = 670 Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset"); 671 672 // Cast the address to the appropriate pointer type, adopting the 673 // address space of the base pointer. 674 llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType()) 675 ->getPointerTo(Base.getAddressSpace()); 676 return Builder.CreateBitCast(Addr, PType); 677 } 678 679 /// Perform a bitcast, derived-to-base, or base-to-derived member pointer 680 /// conversion. 681 /// 682 /// Bitcast conversions are always a no-op under Itanium. 683 /// 684 /// Obligatory offset/adjustment diagram: 685 /// <-- offset --> <-- adjustment --> 686 /// |--------------------------|----------------------|--------------------| 687 /// ^Derived address point ^Base address point ^Member address point 688 /// 689 /// So when converting a base member pointer to a derived member pointer, 690 /// we add the offset to the adjustment because the address point has 691 /// decreased; and conversely, when converting a derived MP to a base MP 692 /// we subtract the offset from the adjustment because the address point 693 /// has increased. 694 /// 695 /// The standard forbids (at compile time) conversion to and from 696 /// virtual bases, which is why we don't have to consider them here. 697 /// 698 /// The standard forbids (at run time) casting a derived MP to a base 699 /// MP when the derived MP does not point to a member of the base. 700 /// This is why -1 is a reasonable choice for null data member 701 /// pointers. 702 llvm::Value * 703 ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 704 const CastExpr *E, 705 llvm::Value *src) { 706 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 707 E->getCastKind() == CK_BaseToDerivedMemberPointer || 708 E->getCastKind() == CK_ReinterpretMemberPointer); 709 710 // Under Itanium, reinterprets don't require any additional processing. 711 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 712 713 // Use constant emission if we can. 714 if (isa<llvm::Constant>(src)) 715 return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); 716 717 llvm::Constant *adj = getMemberPointerAdjustment(E); 718 if (!adj) return src; 719 720 CGBuilderTy &Builder = CGF.Builder; 721 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 722 723 const MemberPointerType *destTy = 724 E->getType()->castAs<MemberPointerType>(); 725 726 // For member data pointers, this is just a matter of adding the 727 // offset if the source is non-null. 728 if (destTy->isMemberDataPointer()) { 729 llvm::Value *dst; 730 if (isDerivedToBase) 731 dst = Builder.CreateNSWSub(src, adj, "adj"); 732 else 733 dst = Builder.CreateNSWAdd(src, adj, "adj"); 734 735 // Null check. 736 llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); 737 llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); 738 return Builder.CreateSelect(isNull, src, dst); 739 } 740 741 // The this-adjustment is left-shifted by 1 on ARM. 742 if (UseARMMethodPtrABI) { 743 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 744 offset <<= 1; 745 adj = llvm::ConstantInt::get(adj->getType(), offset); 746 } 747 748 llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); 749 llvm::Value *dstAdj; 750 if (isDerivedToBase) 751 dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); 752 else 753 dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); 754 755 return Builder.CreateInsertValue(src, dstAdj, 1); 756 } 757 758 llvm::Constant * 759 ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, 760 llvm::Constant *src) { 761 assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 762 E->getCastKind() == CK_BaseToDerivedMemberPointer || 763 E->getCastKind() == CK_ReinterpretMemberPointer); 764 765 // Under Itanium, reinterprets don't require any additional processing. 766 if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 767 768 // If the adjustment is trivial, we don't need to do anything. 769 llvm::Constant *adj = getMemberPointerAdjustment(E); 770 if (!adj) return src; 771 772 bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 773 774 const MemberPointerType *destTy = 775 E->getType()->castAs<MemberPointerType>(); 776 777 // For member data pointers, this is just a matter of adding the 778 // offset if the source is non-null. 779 if (destTy->isMemberDataPointer()) { 780 // null maps to null. 781 if (src->isAllOnesValue()) return src; 782 783 if (isDerivedToBase) 784 return llvm::ConstantExpr::getNSWSub(src, adj); 785 else 786 return llvm::ConstantExpr::getNSWAdd(src, adj); 787 } 788 789 // The this-adjustment is left-shifted by 1 on ARM. 790 if (UseARMMethodPtrABI) { 791 uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 792 offset <<= 1; 793 adj = llvm::ConstantInt::get(adj->getType(), offset); 794 } 795 796 llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); 797 llvm::Constant *dstAdj; 798 if (isDerivedToBase) 799 dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); 800 else 801 dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); 802 803 return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); 804 } 805 806 llvm::Constant * 807 ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 808 // Itanium C++ ABI 2.3: 809 // A NULL pointer is represented as -1. 810 if (MPT->isMemberDataPointer()) 811 return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); 812 813 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); 814 llvm::Constant *Values[2] = { Zero, Zero }; 815 return llvm::ConstantStruct::getAnon(Values); 816 } 817 818 llvm::Constant * 819 ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 820 CharUnits offset) { 821 // Itanium C++ ABI 2.3: 822 // A pointer to data member is an offset from the base address of 823 // the class object containing it, represented as a ptrdiff_t 824 return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); 825 } 826 827 llvm::Constant * 828 ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { 829 return BuildMemberPointer(MD, CharUnits::Zero()); 830 } 831 832 llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, 833 CharUnits ThisAdjustment) { 834 assert(MD->isInstance() && "Member function must not be static!"); 835 MD = MD->getCanonicalDecl(); 836 837 CodeGenTypes &Types = CGM.getTypes(); 838 839 // Get the function pointer (or index if this is a virtual function). 840 llvm::Constant *MemPtr[2]; 841 if (MD->isVirtual()) { 842 uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); 843 844 const ASTContext &Context = getContext(); 845 CharUnits PointerWidth = 846 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 847 uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); 848 849 if (UseARMMethodPtrABI) { 850 // ARM C++ ABI 3.2.1: 851 // This ABI specifies that adj contains twice the this 852 // adjustment, plus 1 if the member function is virtual. The 853 // least significant bit of adj then makes exactly the same 854 // discrimination as the least significant bit of ptr does for 855 // Itanium. 856 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); 857 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 858 2 * ThisAdjustment.getQuantity() + 1); 859 } else { 860 // Itanium C++ ABI 2.3: 861 // For a virtual function, [the pointer field] is 1 plus the 862 // virtual table offset (in bytes) of the function, 863 // represented as a ptrdiff_t. 864 MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); 865 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 866 ThisAdjustment.getQuantity()); 867 } 868 } else { 869 const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 870 llvm::Type *Ty; 871 // Check whether the function has a computable LLVM signature. 872 if (Types.isFuncTypeConvertible(FPT)) { 873 // The function has a computable LLVM signature; use the correct type. 874 Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 875 } else { 876 // Use an arbitrary non-function type to tell GetAddrOfFunction that the 877 // function type is incomplete. 878 Ty = CGM.PtrDiffTy; 879 } 880 llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); 881 882 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); 883 MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 884 (UseARMMethodPtrABI ? 2 : 1) * 885 ThisAdjustment.getQuantity()); 886 } 887 888 return llvm::ConstantStruct::getAnon(MemPtr); 889 } 890 891 llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, 892 QualType MPType) { 893 const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 894 const ValueDecl *MPD = MP.getMemberPointerDecl(); 895 if (!MPD) 896 return EmitNullMemberPointer(MPT); 897 898 CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); 899 900 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 901 return BuildMemberPointer(MD, ThisAdjustment); 902 903 CharUnits FieldOffset = 904 getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 905 return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 906 } 907 908 /// The comparison algorithm is pretty easy: the member pointers are 909 /// the same if they're either bitwise identical *or* both null. 910 /// 911 /// ARM is different here only because null-ness is more complicated. 912 llvm::Value * 913 ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 914 llvm::Value *L, 915 llvm::Value *R, 916 const MemberPointerType *MPT, 917 bool Inequality) { 918 CGBuilderTy &Builder = CGF.Builder; 919 920 llvm::ICmpInst::Predicate Eq; 921 llvm::Instruction::BinaryOps And, Or; 922 if (Inequality) { 923 Eq = llvm::ICmpInst::ICMP_NE; 924 And = llvm::Instruction::Or; 925 Or = llvm::Instruction::And; 926 } else { 927 Eq = llvm::ICmpInst::ICMP_EQ; 928 And = llvm::Instruction::And; 929 Or = llvm::Instruction::Or; 930 } 931 932 // Member data pointers are easy because there's a unique null 933 // value, so it just comes down to bitwise equality. 934 if (MPT->isMemberDataPointer()) 935 return Builder.CreateICmp(Eq, L, R); 936 937 // For member function pointers, the tautologies are more complex. 938 // The Itanium tautology is: 939 // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) 940 // The ARM tautology is: 941 // (L == R) <==> (L.ptr == R.ptr && 942 // (L.adj == R.adj || 943 // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) 944 // The inequality tautologies have exactly the same structure, except 945 // applying De Morgan's laws. 946 947 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); 948 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); 949 950 // This condition tests whether L.ptr == R.ptr. This must always be 951 // true for equality to hold. 952 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); 953 954 // This condition, together with the assumption that L.ptr == R.ptr, 955 // tests whether the pointers are both null. ARM imposes an extra 956 // condition. 957 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); 958 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); 959 960 // This condition tests whether L.adj == R.adj. If this isn't 961 // true, the pointers are unequal unless they're both null. 962 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); 963 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); 964 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); 965 966 // Null member function pointers on ARM clear the low bit of Adj, 967 // so the zero condition has to check that neither low bit is set. 968 if (UseARMMethodPtrABI) { 969 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); 970 971 // Compute (l.adj | r.adj) & 1 and test it against zero. 972 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); 973 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); 974 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, 975 "cmp.or.adj"); 976 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); 977 } 978 979 // Tie together all our conditions. 980 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); 981 Result = Builder.CreateBinOp(And, PtrEq, Result, 982 Inequality ? "memptr.ne" : "memptr.eq"); 983 return Result; 984 } 985 986 llvm::Value * 987 ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 988 llvm::Value *MemPtr, 989 const MemberPointerType *MPT) { 990 CGBuilderTy &Builder = CGF.Builder; 991 992 /// For member data pointers, this is just a check against -1. 993 if (MPT->isMemberDataPointer()) { 994 assert(MemPtr->getType() == CGM.PtrDiffTy); 995 llvm::Value *NegativeOne = 996 llvm::Constant::getAllOnesValue(MemPtr->getType()); 997 return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); 998 } 999 1000 // In Itanium, a member function pointer is not null if 'ptr' is not null. 1001 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); 1002 1003 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); 1004 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); 1005 1006 // On ARM, a member function pointer is also non-null if the low bit of 'adj' 1007 // (the virtual bit) is set. 1008 if (UseARMMethodPtrABI) { 1009 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); 1010 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); 1011 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); 1012 llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, 1013 "memptr.isvirtual"); 1014 Result = Builder.CreateOr(Result, IsVirtual); 1015 } 1016 1017 return Result; 1018 } 1019 1020 bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const { 1021 const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); 1022 if (!RD) 1023 return false; 1024 1025 // If C++ prohibits us from making a copy, return by address. 1026 if (passClassIndirect(RD)) { 1027 auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType()); 1028 FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false); 1029 return true; 1030 } 1031 return false; 1032 } 1033 1034 /// The Itanium ABI requires non-zero initialization only for data 1035 /// member pointers, for which '0' is a valid offset. 1036 bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 1037 return MPT->isMemberFunctionPointer(); 1038 } 1039 1040 /// The Itanium ABI always places an offset to the complete object 1041 /// at entry -2 in the vtable. 1042 void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, 1043 const CXXDeleteExpr *DE, 1044 Address Ptr, 1045 QualType ElementType, 1046 const CXXDestructorDecl *Dtor) { 1047 bool UseGlobalDelete = DE->isGlobalDelete(); 1048 if (UseGlobalDelete) { 1049 // Derive the complete-object pointer, which is what we need 1050 // to pass to the deallocation function. 1051 1052 // Grab the vtable pointer as an intptr_t*. 1053 auto *ClassDecl = 1054 cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl()); 1055 llvm::Value *VTable = 1056 CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl); 1057 1058 // Track back to entry -2 and pull out the offset there. 1059 llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64( 1060 VTable, -2, "complete-offset.ptr"); 1061 llvm::Value *Offset = 1062 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign()); 1063 1064 // Apply the offset. 1065 llvm::Value *CompletePtr = 1066 CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy); 1067 CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset); 1068 1069 // If we're supposed to call the global delete, make sure we do so 1070 // even if the destructor throws. 1071 CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr, 1072 ElementType); 1073 } 1074 1075 // FIXME: Provide a source location here even though there's no 1076 // CXXMemberCallExpr for dtor call. 1077 CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; 1078 EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); 1079 1080 if (UseGlobalDelete) 1081 CGF.PopCleanupBlock(); 1082 } 1083 1084 void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { 1085 // void __cxa_rethrow(); 1086 1087 llvm::FunctionType *FTy = 1088 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); 1089 1090 llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); 1091 1092 if (isNoReturn) 1093 CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None); 1094 else 1095 CGF.EmitRuntimeCallOrInvoke(Fn); 1096 } 1097 1098 static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) { 1099 // void *__cxa_allocate_exception(size_t thrown_size); 1100 1101 llvm::FunctionType *FTy = 1102 llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false); 1103 1104 return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); 1105 } 1106 1107 static llvm::Constant *getThrowFn(CodeGenModule &CGM) { 1108 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, 1109 // void (*dest) (void *)); 1110 1111 llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy }; 1112 llvm::FunctionType *FTy = 1113 llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false); 1114 1115 return CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); 1116 } 1117 1118 void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) { 1119 QualType ThrowType = E->getSubExpr()->getType(); 1120 // Now allocate the exception object. 1121 llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType()); 1122 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); 1123 1124 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM); 1125 llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall( 1126 AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception"); 1127 1128 CharUnits ExnAlign = getAlignmentOfExnObject(); 1129 CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign)); 1130 1131 // Now throw the exception. 1132 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, 1133 /*ForEH=*/true); 1134 1135 // The address of the destructor. If the exception type has a 1136 // trivial destructor (or isn't a record), we just pass null. 1137 llvm::Constant *Dtor = nullptr; 1138 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) { 1139 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); 1140 if (!Record->hasTrivialDestructor()) { 1141 CXXDestructorDecl *DtorD = Record->getDestructor(); 1142 Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete); 1143 Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy); 1144 } 1145 } 1146 if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy); 1147 1148 llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor }; 1149 CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args); 1150 } 1151 1152 static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) { 1153 // void *__dynamic_cast(const void *sub, 1154 // const abi::__class_type_info *src, 1155 // const abi::__class_type_info *dst, 1156 // std::ptrdiff_t src2dst_offset); 1157 1158 llvm::Type *Int8PtrTy = CGF.Int8PtrTy; 1159 llvm::Type *PtrDiffTy = 1160 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1161 1162 llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; 1163 1164 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); 1165 1166 // Mark the function as nounwind readonly. 1167 llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, 1168 llvm::Attribute::ReadOnly }; 1169 llvm::AttributeList Attrs = llvm::AttributeList::get( 1170 CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs); 1171 1172 return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); 1173 } 1174 1175 static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { 1176 // void __cxa_bad_cast(); 1177 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 1178 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); 1179 } 1180 1181 /// \brief Compute the src2dst_offset hint as described in the 1182 /// Itanium C++ ABI [2.9.7] 1183 static CharUnits computeOffsetHint(ASTContext &Context, 1184 const CXXRecordDecl *Src, 1185 const CXXRecordDecl *Dst) { 1186 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, 1187 /*DetectVirtual=*/false); 1188 1189 // If Dst is not derived from Src we can skip the whole computation below and 1190 // return that Src is not a public base of Dst. Record all inheritance paths. 1191 if (!Dst->isDerivedFrom(Src, Paths)) 1192 return CharUnits::fromQuantity(-2ULL); 1193 1194 unsigned NumPublicPaths = 0; 1195 CharUnits Offset; 1196 1197 // Now walk all possible inheritance paths. 1198 for (const CXXBasePath &Path : Paths) { 1199 if (Path.Access != AS_public) // Ignore non-public inheritance. 1200 continue; 1201 1202 ++NumPublicPaths; 1203 1204 for (const CXXBasePathElement &PathElement : Path) { 1205 // If the path contains a virtual base class we can't give any hint. 1206 // -1: no hint. 1207 if (PathElement.Base->isVirtual()) 1208 return CharUnits::fromQuantity(-1ULL); 1209 1210 if (NumPublicPaths > 1) // Won't use offsets, skip computation. 1211 continue; 1212 1213 // Accumulate the base class offsets. 1214 const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class); 1215 Offset += L.getBaseClassOffset( 1216 PathElement.Base->getType()->getAsCXXRecordDecl()); 1217 } 1218 } 1219 1220 // -2: Src is not a public base of Dst. 1221 if (NumPublicPaths == 0) 1222 return CharUnits::fromQuantity(-2ULL); 1223 1224 // -3: Src is a multiple public base type but never a virtual base type. 1225 if (NumPublicPaths > 1) 1226 return CharUnits::fromQuantity(-3ULL); 1227 1228 // Otherwise, the Src type is a unique public nonvirtual base type of Dst. 1229 // Return the offset of Src from the origin of Dst. 1230 return Offset; 1231 } 1232 1233 static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { 1234 // void __cxa_bad_typeid(); 1235 llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); 1236 1237 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); 1238 } 1239 1240 bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref, 1241 QualType SrcRecordTy) { 1242 return IsDeref; 1243 } 1244 1245 void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { 1246 llvm::Value *Fn = getBadTypeidFn(CGF); 1247 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1248 CGF.Builder.CreateUnreachable(); 1249 } 1250 1251 llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF, 1252 QualType SrcRecordTy, 1253 Address ThisPtr, 1254 llvm::Type *StdTypeInfoPtrTy) { 1255 auto *ClassDecl = 1256 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl()); 1257 llvm::Value *Value = 1258 CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl); 1259 1260 // Load the type info. 1261 Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); 1262 return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign()); 1263 } 1264 1265 bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, 1266 QualType SrcRecordTy) { 1267 return SrcIsPtr; 1268 } 1269 1270 llvm::Value *ItaniumCXXABI::EmitDynamicCastCall( 1271 CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy, 1272 QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { 1273 llvm::Type *PtrDiffLTy = 1274 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1275 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1276 1277 llvm::Value *SrcRTTI = 1278 CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); 1279 llvm::Value *DestRTTI = 1280 CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); 1281 1282 // Compute the offset hint. 1283 const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); 1284 const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); 1285 llvm::Value *OffsetHint = llvm::ConstantInt::get( 1286 PtrDiffLTy, 1287 computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); 1288 1289 // Emit the call to __dynamic_cast. 1290 llvm::Value *Value = ThisAddr.getPointer(); 1291 Value = CGF.EmitCastToVoidPtr(Value); 1292 1293 llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; 1294 Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); 1295 Value = CGF.Builder.CreateBitCast(Value, DestLTy); 1296 1297 /// C++ [expr.dynamic.cast]p9: 1298 /// A failed cast to reference type throws std::bad_cast 1299 if (DestTy->isReferenceType()) { 1300 llvm::BasicBlock *BadCastBlock = 1301 CGF.createBasicBlock("dynamic_cast.bad_cast"); 1302 1303 llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); 1304 CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); 1305 1306 CGF.EmitBlock(BadCastBlock); 1307 EmitBadCastCall(CGF); 1308 } 1309 1310 return Value; 1311 } 1312 1313 llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, 1314 Address ThisAddr, 1315 QualType SrcRecordTy, 1316 QualType DestTy) { 1317 llvm::Type *PtrDiffLTy = 1318 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1319 llvm::Type *DestLTy = CGF.ConvertType(DestTy); 1320 1321 auto *ClassDecl = 1322 cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl()); 1323 // Get the vtable pointer. 1324 llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(), 1325 ClassDecl); 1326 1327 // Get the offset-to-top from the vtable. 1328 llvm::Value *OffsetToTop = 1329 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); 1330 OffsetToTop = 1331 CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(), 1332 "offset.to.top"); 1333 1334 // Finally, add the offset to the pointer. 1335 llvm::Value *Value = ThisAddr.getPointer(); 1336 Value = CGF.EmitCastToVoidPtr(Value); 1337 Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); 1338 1339 return CGF.Builder.CreateBitCast(Value, DestLTy); 1340 } 1341 1342 bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { 1343 llvm::Value *Fn = getBadCastFn(CGF); 1344 CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); 1345 CGF.Builder.CreateUnreachable(); 1346 return true; 1347 } 1348 1349 llvm::Value * 1350 ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, 1351 Address This, 1352 const CXXRecordDecl *ClassDecl, 1353 const CXXRecordDecl *BaseClassDecl) { 1354 llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl); 1355 CharUnits VBaseOffsetOffset = 1356 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, 1357 BaseClassDecl); 1358 1359 llvm::Value *VBaseOffsetPtr = 1360 CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), 1361 "vbase.offset.ptr"); 1362 VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, 1363 CGM.PtrDiffTy->getPointerTo()); 1364 1365 llvm::Value *VBaseOffset = 1366 CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(), 1367 "vbase.offset"); 1368 1369 return VBaseOffset; 1370 } 1371 1372 void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { 1373 // Just make sure we're in sync with TargetCXXABI. 1374 assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); 1375 1376 // The constructor used for constructing this as a base class; 1377 // ignores virtual bases. 1378 CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); 1379 1380 // The constructor used for constructing this as a complete class; 1381 // constructs the virtual bases, then calls the base constructor. 1382 if (!D->getParent()->isAbstract()) { 1383 // We don't need to emit the complete ctor if the class is abstract. 1384 CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); 1385 } 1386 } 1387 1388 CGCXXABI::AddedStructorArgs 1389 ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, 1390 SmallVectorImpl<CanQualType> &ArgTys) { 1391 ASTContext &Context = getContext(); 1392 1393 // All parameters are already in place except VTT, which goes after 'this'. 1394 // These are Clang types, so we don't need to worry about sret yet. 1395 1396 // Check if we need to add a VTT parameter (which has type void **). 1397 if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) { 1398 ArgTys.insert(ArgTys.begin() + 1, 1399 Context.getPointerType(Context.VoidPtrTy)); 1400 return AddedStructorArgs::prefix(1); 1401 } 1402 return AddedStructorArgs{}; 1403 } 1404 1405 void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { 1406 // The destructor used for destructing this as a base class; ignores 1407 // virtual bases. 1408 CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); 1409 1410 // The destructor used for destructing this as a most-derived class; 1411 // call the base destructor and then destructs any virtual bases. 1412 CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); 1413 1414 // The destructor in a virtual table is always a 'deleting' 1415 // destructor, which calls the complete destructor and then uses the 1416 // appropriate operator delete. 1417 if (D->isVirtual()) 1418 CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 1419 } 1420 1421 void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, 1422 QualType &ResTy, 1423 FunctionArgList &Params) { 1424 const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 1425 assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); 1426 1427 // Check if we need a VTT parameter as well. 1428 if (NeedsVTTParameter(CGF.CurGD)) { 1429 ASTContext &Context = getContext(); 1430 1431 // FIXME: avoid the fake decl 1432 QualType T = Context.getPointerType(Context.VoidPtrTy); 1433 auto *VTTDecl = ImplicitParamDecl::Create( 1434 Context, /*DC=*/nullptr, MD->getLocation(), &Context.Idents.get("vtt"), 1435 T, ImplicitParamDecl::CXXVTT); 1436 Params.insert(Params.begin() + 1, VTTDecl); 1437 getStructorImplicitParamDecl(CGF) = VTTDecl; 1438 } 1439 } 1440 1441 void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 1442 // Naked functions have no prolog. 1443 if (CGF.CurFuncDecl && CGF.CurFuncDecl->hasAttr<NakedAttr>()) 1444 return; 1445 1446 /// Initialize the 'this' slot. In the Itanium C++ ABI, no prologue 1447 /// adjustments are required, becuase they are all handled by thunks. 1448 setCXXABIThisValue(CGF, loadIncomingCXXThis(CGF)); 1449 1450 /// Initialize the 'vtt' slot if needed. 1451 if (getStructorImplicitParamDecl(CGF)) { 1452 getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( 1453 CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); 1454 } 1455 1456 /// If this is a function that the ABI specifies returns 'this', initialize 1457 /// the return slot to 'this' at the start of the function. 1458 /// 1459 /// Unlike the setting of return types, this is done within the ABI 1460 /// implementation instead of by clients of CGCXXABI because: 1461 /// 1) getThisValue is currently protected 1462 /// 2) in theory, an ABI could implement 'this' returns some other way; 1463 /// HasThisReturn only specifies a contract, not the implementation 1464 if (HasThisReturn(CGF.CurGD)) 1465 CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 1466 } 1467 1468 CGCXXABI::AddedStructorArgs ItaniumCXXABI::addImplicitConstructorArgs( 1469 CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, 1470 bool ForVirtualBase, bool Delegating, CallArgList &Args) { 1471 if (!NeedsVTTParameter(GlobalDecl(D, Type))) 1472 return AddedStructorArgs{}; 1473 1474 // Insert the implicit 'vtt' argument as the second argument. 1475 llvm::Value *VTT = 1476 CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); 1477 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1478 Args.insert(Args.begin() + 1, 1479 CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); 1480 return AddedStructorArgs::prefix(1); // Added one arg. 1481 } 1482 1483 void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, 1484 const CXXDestructorDecl *DD, 1485 CXXDtorType Type, bool ForVirtualBase, 1486 bool Delegating, Address This) { 1487 GlobalDecl GD(DD, Type); 1488 llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); 1489 QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 1490 1491 CGCallee Callee; 1492 if (getContext().getLangOpts().AppleKext && 1493 Type != Dtor_Base && DD->isVirtual()) 1494 Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); 1495 else 1496 Callee = 1497 CGCallee::forDirect(CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)), 1498 DD); 1499 1500 CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), 1501 This.getPointer(), VTT, VTTTy, 1502 nullptr, nullptr); 1503 } 1504 1505 void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, 1506 const CXXRecordDecl *RD) { 1507 llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); 1508 if (VTable->hasInitializer()) 1509 return; 1510 1511 ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); 1512 const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); 1513 llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); 1514 llvm::Constant *RTTI = 1515 CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD)); 1516 1517 // Create and set the initializer. 1518 ConstantInitBuilder Builder(CGM); 1519 auto Components = Builder.beginStruct(); 1520 CGVT.createVTableInitializer(Components, VTLayout, RTTI); 1521 Components.finishAndSetAsInitializer(VTable); 1522 1523 // Set the correct linkage. 1524 VTable->setLinkage(Linkage); 1525 1526 if (CGM.supportsCOMDAT() && VTable->isWeakForLinker()) 1527 VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName())); 1528 1529 // Set the right visibility. 1530 CGM.setGlobalVisibility(VTable, RD, ForDefinition); 1531 1532 // Use pointer alignment for the vtable. Otherwise we would align them based 1533 // on the size of the initializer which doesn't make sense as only single 1534 // values are read. 1535 unsigned PAlign = CGM.getTarget().getPointerAlign(0); 1536 VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity()); 1537 1538 // If this is the magic class __cxxabiv1::__fundamental_type_info, 1539 // we will emit the typeinfo for the fundamental types. This is the 1540 // same behaviour as GCC. 1541 const DeclContext *DC = RD->getDeclContext(); 1542 if (RD->getIdentifier() && 1543 RD->getIdentifier()->isStr("__fundamental_type_info") && 1544 isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && 1545 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && 1546 DC->getParent()->isTranslationUnit()) 1547 EmitFundamentalRTTIDescriptors(RD->hasAttr<DLLExportAttr>()); 1548 1549 if (!VTable->isDeclarationForLinker()) 1550 CGM.EmitVTableTypeMetadata(VTable, VTLayout); 1551 } 1552 1553 bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField( 1554 CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) { 1555 if (Vptr.NearestVBase == nullptr) 1556 return false; 1557 return NeedsVTTParameter(CGF.CurGD); 1558 } 1559 1560 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( 1561 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1562 const CXXRecordDecl *NearestVBase) { 1563 1564 if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && 1565 NeedsVTTParameter(CGF.CurGD)) { 1566 return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base, 1567 NearestVBase); 1568 } 1569 return getVTableAddressPoint(Base, VTableClass); 1570 } 1571 1572 llvm::Constant * 1573 ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base, 1574 const CXXRecordDecl *VTableClass) { 1575 llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits()); 1576 1577 // Find the appropriate vtable within the vtable group, and the address point 1578 // within that vtable. 1579 VTableLayout::AddressPointLocation AddressPoint = 1580 CGM.getItaniumVTableContext() 1581 .getVTableLayout(VTableClass) 1582 .getAddressPoint(Base); 1583 llvm::Value *Indices[] = { 1584 llvm::ConstantInt::get(CGM.Int32Ty, 0), 1585 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex), 1586 llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex), 1587 }; 1588 1589 return llvm::ConstantExpr::getGetElementPtr(VTable->getValueType(), VTable, 1590 Indices, /*InBounds=*/true, 1591 /*InRangeIndex=*/1); 1592 } 1593 1594 llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT( 1595 CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, 1596 const CXXRecordDecl *NearestVBase) { 1597 assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && 1598 NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT"); 1599 1600 // Get the secondary vpointer index. 1601 uint64_t VirtualPointerIndex = 1602 CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); 1603 1604 /// Load the VTT. 1605 llvm::Value *VTT = CGF.LoadCXXVTT(); 1606 if (VirtualPointerIndex) 1607 VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); 1608 1609 // And load the address point from the VTT. 1610 return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign()); 1611 } 1612 1613 llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( 1614 BaseSubobject Base, const CXXRecordDecl *VTableClass) { 1615 return getVTableAddressPoint(Base, VTableClass); 1616 } 1617 1618 llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, 1619 CharUnits VPtrOffset) { 1620 assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); 1621 1622 llvm::GlobalVariable *&VTable = VTables[RD]; 1623 if (VTable) 1624 return VTable; 1625 1626 // Queue up this vtable for possible deferred emission. 1627 CGM.addDeferredVTable(RD); 1628 1629 SmallString<256> Name; 1630 llvm::raw_svector_ostream Out(Name); 1631 getMangleContext().mangleCXXVTable(RD, Out); 1632 1633 const VTableLayout &VTLayout = 1634 CGM.getItaniumVTableContext().getVTableLayout(RD); 1635 llvm::Type *VTableType = CGM.getVTables().getVTableType(VTLayout); 1636 1637 VTable = CGM.CreateOrReplaceCXXRuntimeVariable( 1638 Name, VTableType, llvm::GlobalValue::ExternalLinkage); 1639 VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 1640 CGM.setGlobalVisibility(VTable, RD, NotForDefinition); 1641 1642 if (RD->hasAttr<DLLImportAttr>()) 1643 VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 1644 else if (RD->hasAttr<DLLExportAttr>()) 1645 VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 1646 1647 return VTable; 1648 } 1649 1650 CGCallee ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, 1651 GlobalDecl GD, 1652 Address This, 1653 llvm::Type *Ty, 1654 SourceLocation Loc) { 1655 GD = GD.getCanonicalDecl(); 1656 Ty = Ty->getPointerTo()->getPointerTo(); 1657 auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl()); 1658 llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent()); 1659 1660 uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); 1661 llvm::Value *VFunc; 1662 if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) { 1663 VFunc = CGF.EmitVTableTypeCheckedLoad( 1664 MethodDecl->getParent(), VTable, 1665 VTableIndex * CGM.getContext().getTargetInfo().getPointerWidth(0) / 8); 1666 } else { 1667 CGF.EmitTypeMetadataCodeForVCall(MethodDecl->getParent(), VTable, Loc); 1668 1669 llvm::Value *VFuncPtr = 1670 CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); 1671 auto *VFuncLoad = 1672 CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); 1673 1674 // Add !invariant.load md to virtual function load to indicate that 1675 // function didn't change inside vtable. 1676 // It's safe to add it without -fstrict-vtable-pointers, but it would not 1677 // help in devirtualization because it will only matter if we will have 2 1678 // the same virtual function loads from the same vtable load, which won't 1679 // happen without enabled devirtualization with -fstrict-vtable-pointers. 1680 if (CGM.getCodeGenOpts().OptimizationLevel > 0 && 1681 CGM.getCodeGenOpts().StrictVTablePointers) 1682 VFuncLoad->setMetadata( 1683 llvm::LLVMContext::MD_invariant_load, 1684 llvm::MDNode::get(CGM.getLLVMContext(), 1685 llvm::ArrayRef<llvm::Metadata *>())); 1686 VFunc = VFuncLoad; 1687 } 1688 1689 CGCallee Callee(MethodDecl, VFunc); 1690 return Callee; 1691 } 1692 1693 llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( 1694 CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, 1695 Address This, const CXXMemberCallExpr *CE) { 1696 assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); 1697 assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 1698 1699 const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( 1700 Dtor, getFromDtorType(DtorType)); 1701 llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 1702 CGCallee Callee = 1703 getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty, 1704 CE ? CE->getLocStart() : SourceLocation()); 1705 1706 CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), 1707 This.getPointer(), /*ImplicitParam=*/nullptr, 1708 QualType(), CE, nullptr); 1709 return nullptr; 1710 } 1711 1712 void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { 1713 CodeGenVTables &VTables = CGM.getVTables(); 1714 llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); 1715 VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); 1716 } 1717 1718 bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const { 1719 // We don't emit available_externally vtables if we are in -fapple-kext mode 1720 // because kext mode does not permit devirtualization. 1721 if (CGM.getLangOpts().AppleKext) 1722 return false; 1723 1724 // If we don't have any not emitted inline virtual function, and if vtable is 1725 // not hidden, then we are safe to emit available_externally copy of vtable. 1726 // FIXME we can still emit a copy of the vtable if we 1727 // can emit definition of the inline functions. 1728 return !hasAnyUnusedVirtualInlineFunction(RD) && !isVTableHidden(RD); 1729 } 1730 static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, 1731 Address InitialPtr, 1732 int64_t NonVirtualAdjustment, 1733 int64_t VirtualAdjustment, 1734 bool IsReturnAdjustment) { 1735 if (!NonVirtualAdjustment && !VirtualAdjustment) 1736 return InitialPtr.getPointer(); 1737 1738 Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty); 1739 1740 // In a base-to-derived cast, the non-virtual adjustment is applied first. 1741 if (NonVirtualAdjustment && !IsReturnAdjustment) { 1742 V = CGF.Builder.CreateConstInBoundsByteGEP(V, 1743 CharUnits::fromQuantity(NonVirtualAdjustment)); 1744 } 1745 1746 // Perform the virtual adjustment if we have one. 1747 llvm::Value *ResultPtr; 1748 if (VirtualAdjustment) { 1749 llvm::Type *PtrDiffTy = 1750 CGF.ConvertType(CGF.getContext().getPointerDiffType()); 1751 1752 Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy); 1753 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); 1754 1755 llvm::Value *OffsetPtr = 1756 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 1757 1758 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); 1759 1760 // Load the adjustment offset from the vtable. 1761 llvm::Value *Offset = 1762 CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign()); 1763 1764 // Adjust our pointer. 1765 ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset); 1766 } else { 1767 ResultPtr = V.getPointer(); 1768 } 1769 1770 // In a derived-to-base conversion, the non-virtual adjustment is 1771 // applied second. 1772 if (NonVirtualAdjustment && IsReturnAdjustment) { 1773 ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr, 1774 NonVirtualAdjustment); 1775 } 1776 1777 // Cast back to the original type. 1778 return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType()); 1779 } 1780 1781 llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, 1782 Address This, 1783 const ThisAdjustment &TA) { 1784 return performTypeAdjustment(CGF, This, TA.NonVirtual, 1785 TA.Virtual.Itanium.VCallOffsetOffset, 1786 /*IsReturnAdjustment=*/false); 1787 } 1788 1789 llvm::Value * 1790 ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret, 1791 const ReturnAdjustment &RA) { 1792 return performTypeAdjustment(CGF, Ret, RA.NonVirtual, 1793 RA.Virtual.Itanium.VBaseOffsetOffset, 1794 /*IsReturnAdjustment=*/true); 1795 } 1796 1797 void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 1798 RValue RV, QualType ResultType) { 1799 if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) 1800 return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); 1801 1802 // Destructor thunks in the ARM ABI have indeterminate results. 1803 llvm::Type *T = CGF.ReturnValue.getElementType(); 1804 RValue Undef = RValue::get(llvm::UndefValue::get(T)); 1805 return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); 1806 } 1807 1808 /************************** Array allocation cookies **************************/ 1809 1810 CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1811 // The array cookie is a size_t; pad that up to the element alignment. 1812 // The cookie is actually right-justified in that space. 1813 return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), 1814 CGM.getContext().getTypeAlignInChars(elementType)); 1815 } 1816 1817 Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1818 Address NewPtr, 1819 llvm::Value *NumElements, 1820 const CXXNewExpr *expr, 1821 QualType ElementType) { 1822 assert(requiresArrayCookie(expr)); 1823 1824 unsigned AS = NewPtr.getAddressSpace(); 1825 1826 ASTContext &Ctx = getContext(); 1827 CharUnits SizeSize = CGF.getSizeSize(); 1828 1829 // The size of the cookie. 1830 CharUnits CookieSize = 1831 std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); 1832 assert(CookieSize == getArrayCookieSizeImpl(ElementType)); 1833 1834 // Compute an offset to the cookie. 1835 Address CookiePtr = NewPtr; 1836 CharUnits CookieOffset = CookieSize - SizeSize; 1837 if (!CookieOffset.isZero()) 1838 CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset); 1839 1840 // Write the number of elements into the appropriate slot. 1841 Address NumElementsPtr = 1842 CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy); 1843 llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr); 1844 1845 // Handle the array cookie specially in ASan. 1846 if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 && 1847 expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) { 1848 // The store to the CookiePtr does not need to be instrumented. 1849 CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); 1850 llvm::FunctionType *FTy = 1851 llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false); 1852 llvm::Constant *F = 1853 CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie"); 1854 CGF.Builder.CreateCall(F, NumElementsPtr.getPointer()); 1855 } 1856 1857 // Finally, compute a pointer to the actual data buffer by skipping 1858 // over the cookie completely. 1859 return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize); 1860 } 1861 1862 llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1863 Address allocPtr, 1864 CharUnits cookieSize) { 1865 // The element size is right-justified in the cookie. 1866 Address numElementsPtr = allocPtr; 1867 CharUnits numElementsOffset = cookieSize - CGF.getSizeSize(); 1868 if (!numElementsOffset.isZero()) 1869 numElementsPtr = 1870 CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset); 1871 1872 unsigned AS = allocPtr.getAddressSpace(); 1873 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy); 1874 if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0) 1875 return CGF.Builder.CreateLoad(numElementsPtr); 1876 // In asan mode emit a function call instead of a regular load and let the 1877 // run-time deal with it: if the shadow is properly poisoned return the 1878 // cookie, otherwise return 0 to avoid an infinite loop calling DTORs. 1879 // We can't simply ignore this load using nosanitize metadata because 1880 // the metadata may be lost. 1881 llvm::FunctionType *FTy = 1882 llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false); 1883 llvm::Constant *F = 1884 CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie"); 1885 return CGF.Builder.CreateCall(F, numElementsPtr.getPointer()); 1886 } 1887 1888 CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { 1889 // ARM says that the cookie is always: 1890 // struct array_cookie { 1891 // std::size_t element_size; // element_size != 0 1892 // std::size_t element_count; 1893 // }; 1894 // But the base ABI doesn't give anything an alignment greater than 1895 // 8, so we can dismiss this as typical ABI-author blindness to 1896 // actual language complexity and round up to the element alignment. 1897 return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), 1898 CGM.getContext().getTypeAlignInChars(elementType)); 1899 } 1900 1901 Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 1902 Address newPtr, 1903 llvm::Value *numElements, 1904 const CXXNewExpr *expr, 1905 QualType elementType) { 1906 assert(requiresArrayCookie(expr)); 1907 1908 // The cookie is always at the start of the buffer. 1909 Address cookie = newPtr; 1910 1911 // The first element is the element size. 1912 cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy); 1913 llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, 1914 getContext().getTypeSizeInChars(elementType).getQuantity()); 1915 CGF.Builder.CreateStore(elementSize, cookie); 1916 1917 // The second element is the element count. 1918 cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize()); 1919 CGF.Builder.CreateStore(numElements, cookie); 1920 1921 // Finally, compute a pointer to the actual data buffer by skipping 1922 // over the cookie completely. 1923 CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); 1924 return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize); 1925 } 1926 1927 llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 1928 Address allocPtr, 1929 CharUnits cookieSize) { 1930 // The number of elements is at offset sizeof(size_t) relative to 1931 // the allocated pointer. 1932 Address numElementsPtr 1933 = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize()); 1934 1935 numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy); 1936 return CGF.Builder.CreateLoad(numElementsPtr); 1937 } 1938 1939 /*********************** Static local initialization **************************/ 1940 1941 static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, 1942 llvm::PointerType *GuardPtrTy) { 1943 // int __cxa_guard_acquire(__guard *guard_object); 1944 llvm::FunctionType *FTy = 1945 llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), 1946 GuardPtrTy, /*isVarArg=*/false); 1947 return CGM.CreateRuntimeFunction( 1948 FTy, "__cxa_guard_acquire", 1949 llvm::AttributeList::get(CGM.getLLVMContext(), 1950 llvm::AttributeList::FunctionIndex, 1951 llvm::Attribute::NoUnwind)); 1952 } 1953 1954 static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, 1955 llvm::PointerType *GuardPtrTy) { 1956 // void __cxa_guard_release(__guard *guard_object); 1957 llvm::FunctionType *FTy = 1958 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1959 return CGM.CreateRuntimeFunction( 1960 FTy, "__cxa_guard_release", 1961 llvm::AttributeList::get(CGM.getLLVMContext(), 1962 llvm::AttributeList::FunctionIndex, 1963 llvm::Attribute::NoUnwind)); 1964 } 1965 1966 static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, 1967 llvm::PointerType *GuardPtrTy) { 1968 // void __cxa_guard_abort(__guard *guard_object); 1969 llvm::FunctionType *FTy = 1970 llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1971 return CGM.CreateRuntimeFunction( 1972 FTy, "__cxa_guard_abort", 1973 llvm::AttributeList::get(CGM.getLLVMContext(), 1974 llvm::AttributeList::FunctionIndex, 1975 llvm::Attribute::NoUnwind)); 1976 } 1977 1978 namespace { 1979 struct CallGuardAbort final : EHScopeStack::Cleanup { 1980 llvm::GlobalVariable *Guard; 1981 CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} 1982 1983 void Emit(CodeGenFunction &CGF, Flags flags) override { 1984 CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), 1985 Guard); 1986 } 1987 }; 1988 } 1989 1990 /// The ARM code here follows the Itanium code closely enough that we 1991 /// just special-case it at particular places. 1992 void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 1993 const VarDecl &D, 1994 llvm::GlobalVariable *var, 1995 bool shouldPerformInit) { 1996 CGBuilderTy &Builder = CGF.Builder; 1997 1998 // Inline variables that weren't instantiated from variable templates have 1999 // partially-ordered initialization within their translation unit. 2000 bool NonTemplateInline = 2001 D.isInline() && 2002 !isTemplateInstantiation(D.getTemplateSpecializationKind()); 2003 2004 // We only need to use thread-safe statics for local non-TLS variables and 2005 // inline variables; other global initialization is always single-threaded 2006 // or (through lazy dynamic loading in multiple threads) unsequenced. 2007 bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && 2008 (D.isLocalVarDecl() || NonTemplateInline) && 2009 !D.getTLSKind(); 2010 2011 // If we have a global variable with internal linkage and thread-safe statics 2012 // are disabled, we can just let the guard variable be of type i8. 2013 bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); 2014 2015 llvm::IntegerType *guardTy; 2016 CharUnits guardAlignment; 2017 if (useInt8GuardVariable) { 2018 guardTy = CGF.Int8Ty; 2019 guardAlignment = CharUnits::One(); 2020 } else { 2021 // Guard variables are 64 bits in the generic ABI and size width on ARM 2022 // (i.e. 32-bit on AArch32, 64-bit on AArch64). 2023 if (UseARMGuardVarABI) { 2024 guardTy = CGF.SizeTy; 2025 guardAlignment = CGF.getSizeAlign(); 2026 } else { 2027 guardTy = CGF.Int64Ty; 2028 guardAlignment = CharUnits::fromQuantity( 2029 CGM.getDataLayout().getABITypeAlignment(guardTy)); 2030 } 2031 } 2032 llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); 2033 2034 // Create the guard variable if we don't already have it (as we 2035 // might if we're double-emitting this function body). 2036 llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); 2037 if (!guard) { 2038 // Mangle the name for the guard. 2039 SmallString<256> guardName; 2040 { 2041 llvm::raw_svector_ostream out(guardName); 2042 getMangleContext().mangleStaticGuardVariable(&D, out); 2043 } 2044 2045 // Create the guard variable with a zero-initializer. 2046 // Just absorb linkage and visibility from the guarded variable. 2047 guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, 2048 false, var->getLinkage(), 2049 llvm::ConstantInt::get(guardTy, 0), 2050 guardName.str()); 2051 guard->setVisibility(var->getVisibility()); 2052 // If the variable is thread-local, so is its guard variable. 2053 guard->setThreadLocalMode(var->getThreadLocalMode()); 2054 guard->setAlignment(guardAlignment.getQuantity()); 2055 2056 // The ABI says: "It is suggested that it be emitted in the same COMDAT 2057 // group as the associated data object." In practice, this doesn't work for 2058 // non-ELF and non-Wasm object formats, so only do it for ELF and Wasm. 2059 llvm::Comdat *C = var->getComdat(); 2060 if (!D.isLocalVarDecl() && C && 2061 (CGM.getTarget().getTriple().isOSBinFormatELF() || 2062 CGM.getTarget().getTriple().isOSBinFormatWasm())) { 2063 guard->setComdat(C); 2064 // An inline variable's guard function is run from the per-TU 2065 // initialization function, not via a dedicated global ctor function, so 2066 // we can't put it in a comdat. 2067 if (!NonTemplateInline) 2068 CGF.CurFn->setComdat(C); 2069 } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) { 2070 guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName())); 2071 } 2072 2073 CGM.setStaticLocalDeclGuardAddress(&D, guard); 2074 } 2075 2076 Address guardAddr = Address(guard, guardAlignment); 2077 2078 // Test whether the variable has completed initialization. 2079 // 2080 // Itanium C++ ABI 3.3.2: 2081 // The following is pseudo-code showing how these functions can be used: 2082 // if (obj_guard.first_byte == 0) { 2083 // if ( __cxa_guard_acquire (&obj_guard) ) { 2084 // try { 2085 // ... initialize the object ...; 2086 // } catch (...) { 2087 // __cxa_guard_abort (&obj_guard); 2088 // throw; 2089 // } 2090 // ... queue object destructor with __cxa_atexit() ...; 2091 // __cxa_guard_release (&obj_guard); 2092 // } 2093 // } 2094 2095 // Load the first byte of the guard variable. 2096 llvm::LoadInst *LI = 2097 Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty)); 2098 2099 // Itanium ABI: 2100 // An implementation supporting thread-safety on multiprocessor 2101 // systems must also guarantee that references to the initialized 2102 // object do not occur before the load of the initialization flag. 2103 // 2104 // In LLVM, we do this by marking the load Acquire. 2105 if (threadsafe) 2106 LI->setAtomic(llvm::AtomicOrdering::Acquire); 2107 2108 // For ARM, we should only check the first bit, rather than the entire byte: 2109 // 2110 // ARM C++ ABI 3.2.3.1: 2111 // To support the potential use of initialization guard variables 2112 // as semaphores that are the target of ARM SWP and LDREX/STREX 2113 // synchronizing instructions we define a static initialization 2114 // guard variable to be a 4-byte aligned, 4-byte word with the 2115 // following inline access protocol. 2116 // #define INITIALIZED 1 2117 // if ((obj_guard & INITIALIZED) != INITIALIZED) { 2118 // if (__cxa_guard_acquire(&obj_guard)) 2119 // ... 2120 // } 2121 // 2122 // and similarly for ARM64: 2123 // 2124 // ARM64 C++ ABI 3.2.2: 2125 // This ABI instead only specifies the value bit 0 of the static guard 2126 // variable; all other bits are platform defined. Bit 0 shall be 0 when the 2127 // variable is not initialized and 1 when it is. 2128 llvm::Value *V = 2129 (UseARMGuardVarABI && !useInt8GuardVariable) 2130 ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1)) 2131 : LI; 2132 llvm::Value *NeedsInit = Builder.CreateIsNull(V, "guard.uninitialized"); 2133 2134 llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); 2135 llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 2136 2137 // Check if the first byte of the guard variable is zero. 2138 CGF.EmitCXXGuardedInitBranch(NeedsInit, InitCheckBlock, EndBlock, 2139 CodeGenFunction::GuardKind::VariableGuard, &D); 2140 2141 CGF.EmitBlock(InitCheckBlock); 2142 2143 // Variables used when coping with thread-safe statics and exceptions. 2144 if (threadsafe) { 2145 // Call __cxa_guard_acquire. 2146 llvm::Value *V 2147 = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); 2148 2149 llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 2150 2151 Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), 2152 InitBlock, EndBlock); 2153 2154 // Call __cxa_guard_abort along the exceptional edge. 2155 CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); 2156 2157 CGF.EmitBlock(InitBlock); 2158 } 2159 2160 // Emit the initializer and add a global destructor if appropriate. 2161 CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); 2162 2163 if (threadsafe) { 2164 // Pop the guard-abort cleanup if we pushed one. 2165 CGF.PopCleanupBlock(); 2166 2167 // Call __cxa_guard_release. This cannot throw. 2168 CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), 2169 guardAddr.getPointer()); 2170 } else { 2171 Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr); 2172 } 2173 2174 CGF.EmitBlock(EndBlock); 2175 } 2176 2177 /// Register a global destructor using __cxa_atexit. 2178 static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, 2179 llvm::Constant *dtor, 2180 llvm::Constant *addr, 2181 bool TLS) { 2182 const char *Name = "__cxa_atexit"; 2183 if (TLS) { 2184 const llvm::Triple &T = CGF.getTarget().getTriple(); 2185 Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit"; 2186 } 2187 2188 // We're assuming that the destructor function is something we can 2189 // reasonably call with the default CC. Go ahead and cast it to the 2190 // right prototype. 2191 llvm::Type *dtorTy = 2192 llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); 2193 2194 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); 2195 llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; 2196 llvm::FunctionType *atexitTy = 2197 llvm::FunctionType::get(CGF.IntTy, paramTys, false); 2198 2199 // Fetch the actual function. 2200 llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); 2201 if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) 2202 fn->setDoesNotThrow(); 2203 2204 // Create a variable that binds the atexit to this shared object. 2205 llvm::Constant *handle = 2206 CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); 2207 auto *GV = cast<llvm::GlobalValue>(handle->stripPointerCasts()); 2208 GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 2209 2210 llvm::Value *args[] = { 2211 llvm::ConstantExpr::getBitCast(dtor, dtorTy), 2212 llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), 2213 handle 2214 }; 2215 CGF.EmitNounwindRuntimeCall(atexit, args); 2216 } 2217 2218 /// Register a global destructor as best as we know how. 2219 void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, 2220 const VarDecl &D, 2221 llvm::Constant *dtor, 2222 llvm::Constant *addr) { 2223 // Use __cxa_atexit if available. 2224 if (CGM.getCodeGenOpts().CXAAtExit) 2225 return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); 2226 2227 if (D.getTLSKind()) 2228 CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); 2229 2230 // In Apple kexts, we want to add a global destructor entry. 2231 // FIXME: shouldn't this be guarded by some variable? 2232 if (CGM.getLangOpts().AppleKext) { 2233 // Generate a global destructor entry. 2234 return CGM.AddCXXDtorEntry(dtor, addr); 2235 } 2236 2237 CGF.registerGlobalDtorWithAtExit(D, dtor, addr); 2238 } 2239 2240 static bool isThreadWrapperReplaceable(const VarDecl *VD, 2241 CodeGen::CodeGenModule &CGM) { 2242 assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!"); 2243 // Darwin prefers to have references to thread local variables to go through 2244 // the thread wrapper instead of directly referencing the backing variable. 2245 return VD->getTLSKind() == VarDecl::TLS_Dynamic && 2246 CGM.getTarget().getTriple().isOSDarwin(); 2247 } 2248 2249 /// Get the appropriate linkage for the wrapper function. This is essentially 2250 /// the weak form of the variable's linkage; every translation unit which needs 2251 /// the wrapper emits a copy, and we want the linker to merge them. 2252 static llvm::GlobalValue::LinkageTypes 2253 getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { 2254 llvm::GlobalValue::LinkageTypes VarLinkage = 2255 CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false); 2256 2257 // For internal linkage variables, we don't need an external or weak wrapper. 2258 if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) 2259 return VarLinkage; 2260 2261 // If the thread wrapper is replaceable, give it appropriate linkage. 2262 if (isThreadWrapperReplaceable(VD, CGM)) 2263 if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) && 2264 !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage)) 2265 return VarLinkage; 2266 return llvm::GlobalValue::WeakODRLinkage; 2267 } 2268 2269 llvm::Function * 2270 ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, 2271 llvm::Value *Val) { 2272 // Mangle the name for the thread_local wrapper function. 2273 SmallString<256> WrapperName; 2274 { 2275 llvm::raw_svector_ostream Out(WrapperName); 2276 getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); 2277 } 2278 2279 // FIXME: If VD is a definition, we should regenerate the function attributes 2280 // before returning. 2281 if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName)) 2282 return cast<llvm::Function>(V); 2283 2284 QualType RetQT = VD->getType(); 2285 if (RetQT->isReferenceType()) 2286 RetQT = RetQT.getNonReferenceType(); 2287 2288 const CGFunctionInfo &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration( 2289 getContext().getPointerType(RetQT), FunctionArgList()); 2290 2291 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FI); 2292 llvm::Function *Wrapper = 2293 llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM), 2294 WrapperName.str(), &CGM.getModule()); 2295 2296 CGM.SetLLVMFunctionAttributes(nullptr, FI, Wrapper); 2297 2298 if (VD->hasDefinition()) 2299 CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Wrapper); 2300 2301 // Always resolve references to the wrapper at link time. 2302 if (!Wrapper->hasLocalLinkage() && !(isThreadWrapperReplaceable(VD, CGM) && 2303 !llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) && 2304 !llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage()))) 2305 Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); 2306 2307 if (isThreadWrapperReplaceable(VD, CGM)) { 2308 Wrapper->setCallingConv(llvm::CallingConv::CXX_FAST_TLS); 2309 Wrapper->addFnAttr(llvm::Attribute::NoUnwind); 2310 } 2311 return Wrapper; 2312 } 2313 2314 void ItaniumCXXABI::EmitThreadLocalInitFuncs( 2315 CodeGenModule &CGM, ArrayRef<const VarDecl *> CXXThreadLocals, 2316 ArrayRef<llvm::Function *> CXXThreadLocalInits, 2317 ArrayRef<const VarDecl *> CXXThreadLocalInitVars) { 2318 llvm::Function *InitFunc = nullptr; 2319 2320 // Separate initializers into those with ordered (or partially-ordered) 2321 // initialization and those with unordered initialization. 2322 llvm::SmallVector<llvm::Function *, 8> OrderedInits; 2323 llvm::SmallDenseMap<const VarDecl *, llvm::Function *> UnorderedInits; 2324 for (unsigned I = 0; I != CXXThreadLocalInits.size(); ++I) { 2325 if (isTemplateInstantiation( 2326 CXXThreadLocalInitVars[I]->getTemplateSpecializationKind())) 2327 UnorderedInits[CXXThreadLocalInitVars[I]->getCanonicalDecl()] = 2328 CXXThreadLocalInits[I]; 2329 else 2330 OrderedInits.push_back(CXXThreadLocalInits[I]); 2331 } 2332 2333 if (!OrderedInits.empty()) { 2334 // Generate a guarded initialization function. 2335 llvm::FunctionType *FTy = 2336 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); 2337 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); 2338 InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI, 2339 SourceLocation(), 2340 /*TLS=*/true); 2341 llvm::GlobalVariable *Guard = new llvm::GlobalVariable( 2342 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, 2343 llvm::GlobalVariable::InternalLinkage, 2344 llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard"); 2345 Guard->setThreadLocal(true); 2346 2347 CharUnits GuardAlign = CharUnits::One(); 2348 Guard->setAlignment(GuardAlign.getQuantity()); 2349 2350 CodeGenFunction(CGM).GenerateCXXGlobalInitFunc(InitFunc, OrderedInits, 2351 Address(Guard, GuardAlign)); 2352 // On Darwin platforms, use CXX_FAST_TLS calling convention. 2353 if (CGM.getTarget().getTriple().isOSDarwin()) { 2354 InitFunc->setCallingConv(llvm::CallingConv::CXX_FAST_TLS); 2355 InitFunc->addFnAttr(llvm::Attribute::NoUnwind); 2356 } 2357 } 2358 2359 // Emit thread wrappers. 2360 for (const VarDecl *VD : CXXThreadLocals) { 2361 llvm::GlobalVariable *Var = 2362 cast<llvm::GlobalVariable>(CGM.GetGlobalValue(CGM.getMangledName(VD))); 2363 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); 2364 2365 // Some targets require that all access to thread local variables go through 2366 // the thread wrapper. This means that we cannot attempt to create a thread 2367 // wrapper or a thread helper. 2368 if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) { 2369 Wrapper->setLinkage(llvm::Function::ExternalLinkage); 2370 continue; 2371 } 2372 2373 // Mangle the name for the thread_local initialization function. 2374 SmallString<256> InitFnName; 2375 { 2376 llvm::raw_svector_ostream Out(InitFnName); 2377 getMangleContext().mangleItaniumThreadLocalInit(VD, Out); 2378 } 2379 2380 // If we have a definition for the variable, emit the initialization 2381 // function as an alias to the global Init function (if any). Otherwise, 2382 // produce a declaration of the initialization function. 2383 llvm::GlobalValue *Init = nullptr; 2384 bool InitIsInitFunc = false; 2385 if (VD->hasDefinition()) { 2386 InitIsInitFunc = true; 2387 llvm::Function *InitFuncToUse = InitFunc; 2388 if (isTemplateInstantiation(VD->getTemplateSpecializationKind())) 2389 InitFuncToUse = UnorderedInits.lookup(VD->getCanonicalDecl()); 2390 if (InitFuncToUse) 2391 Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(), 2392 InitFuncToUse); 2393 } else { 2394 // Emit a weak global function referring to the initialization function. 2395 // This function will not exist if the TU defining the thread_local 2396 // variable in question does not need any dynamic initialization for 2397 // its thread_local variables. 2398 llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); 2399 Init = llvm::Function::Create(FnTy, 2400 llvm::GlobalVariable::ExternalWeakLinkage, 2401 InitFnName.str(), &CGM.getModule()); 2402 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); 2403 CGM.SetLLVMFunctionAttributes(nullptr, FI, cast<llvm::Function>(Init)); 2404 } 2405 2406 if (Init) 2407 Init->setVisibility(Var->getVisibility()); 2408 2409 llvm::LLVMContext &Context = CGM.getModule().getContext(); 2410 llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); 2411 CGBuilderTy Builder(CGM, Entry); 2412 if (InitIsInitFunc) { 2413 if (Init) { 2414 llvm::CallInst *CallVal = Builder.CreateCall(Init); 2415 if (isThreadWrapperReplaceable(VD, CGM)) 2416 CallVal->setCallingConv(llvm::CallingConv::CXX_FAST_TLS); 2417 } 2418 } else { 2419 // Don't know whether we have an init function. Call it if it exists. 2420 llvm::Value *Have = Builder.CreateIsNotNull(Init); 2421 llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2422 llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 2423 Builder.CreateCondBr(Have, InitBB, ExitBB); 2424 2425 Builder.SetInsertPoint(InitBB); 2426 Builder.CreateCall(Init); 2427 Builder.CreateBr(ExitBB); 2428 2429 Builder.SetInsertPoint(ExitBB); 2430 } 2431 2432 // For a reference, the result of the wrapper function is a pointer to 2433 // the referenced object. 2434 llvm::Value *Val = Var; 2435 if (VD->getType()->isReferenceType()) { 2436 CharUnits Align = CGM.getContext().getDeclAlign(VD); 2437 Val = Builder.CreateAlignedLoad(Val, Align); 2438 } 2439 if (Val->getType() != Wrapper->getReturnType()) 2440 Val = Builder.CreatePointerBitCastOrAddrSpaceCast( 2441 Val, Wrapper->getReturnType(), ""); 2442 Builder.CreateRet(Val); 2443 } 2444 } 2445 2446 LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, 2447 const VarDecl *VD, 2448 QualType LValType) { 2449 llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD); 2450 llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val); 2451 2452 llvm::CallInst *CallVal = CGF.Builder.CreateCall(Wrapper); 2453 CallVal->setCallingConv(Wrapper->getCallingConv()); 2454 2455 LValue LV; 2456 if (VD->getType()->isReferenceType()) 2457 LV = CGF.MakeNaturalAlignAddrLValue(CallVal, LValType); 2458 else 2459 LV = CGF.MakeAddrLValue(CallVal, LValType, 2460 CGF.getContext().getDeclAlign(VD)); 2461 // FIXME: need setObjCGCLValueClass? 2462 return LV; 2463 } 2464 2465 /// Return whether the given global decl needs a VTT parameter, which it does 2466 /// if it's a base constructor or destructor with virtual bases. 2467 bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { 2468 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 2469 2470 // We don't have any virtual bases, just return early. 2471 if (!MD->getParent()->getNumVBases()) 2472 return false; 2473 2474 // Check if we have a base constructor. 2475 if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) 2476 return true; 2477 2478 // Check if we have a base destructor. 2479 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 2480 return true; 2481 2482 return false; 2483 } 2484 2485 namespace { 2486 class ItaniumRTTIBuilder { 2487 CodeGenModule &CGM; // Per-module state. 2488 llvm::LLVMContext &VMContext; 2489 const ItaniumCXXABI &CXXABI; // Per-module state. 2490 2491 /// Fields - The fields of the RTTI descriptor currently being built. 2492 SmallVector<llvm::Constant *, 16> Fields; 2493 2494 /// GetAddrOfTypeName - Returns the mangled type name of the given type. 2495 llvm::GlobalVariable * 2496 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage); 2497 2498 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI 2499 /// descriptor of the given type. 2500 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); 2501 2502 /// BuildVTablePointer - Build the vtable pointer for the given type. 2503 void BuildVTablePointer(const Type *Ty); 2504 2505 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 2506 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. 2507 void BuildSIClassTypeInfo(const CXXRecordDecl *RD); 2508 2509 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 2510 /// classes with bases that do not satisfy the abi::__si_class_type_info 2511 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 2512 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); 2513 2514 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used 2515 /// for pointer types. 2516 void BuildPointerTypeInfo(QualType PointeeTy); 2517 2518 /// BuildObjCObjectTypeInfo - Build the appropriate kind of 2519 /// type_info for an object type. 2520 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); 2521 2522 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 2523 /// struct, used for member pointer types. 2524 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); 2525 2526 public: 2527 ItaniumRTTIBuilder(const ItaniumCXXABI &ABI) 2528 : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {} 2529 2530 // Pointer type info flags. 2531 enum { 2532 /// PTI_Const - Type has const qualifier. 2533 PTI_Const = 0x1, 2534 2535 /// PTI_Volatile - Type has volatile qualifier. 2536 PTI_Volatile = 0x2, 2537 2538 /// PTI_Restrict - Type has restrict qualifier. 2539 PTI_Restrict = 0x4, 2540 2541 /// PTI_Incomplete - Type is incomplete. 2542 PTI_Incomplete = 0x8, 2543 2544 /// PTI_ContainingClassIncomplete - Containing class is incomplete. 2545 /// (in pointer to member). 2546 PTI_ContainingClassIncomplete = 0x10, 2547 2548 /// PTI_TransactionSafe - Pointee is transaction_safe function (C++ TM TS). 2549 //PTI_TransactionSafe = 0x20, 2550 2551 /// PTI_Noexcept - Pointee is noexcept function (C++1z). 2552 PTI_Noexcept = 0x40, 2553 }; 2554 2555 // VMI type info flags. 2556 enum { 2557 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. 2558 VMI_NonDiamondRepeat = 0x1, 2559 2560 /// VMI_DiamondShaped - Class is diamond shaped. 2561 VMI_DiamondShaped = 0x2 2562 }; 2563 2564 // Base class type info flags. 2565 enum { 2566 /// BCTI_Virtual - Base class is virtual. 2567 BCTI_Virtual = 0x1, 2568 2569 /// BCTI_Public - Base class is public. 2570 BCTI_Public = 0x2 2571 }; 2572 2573 /// BuildTypeInfo - Build the RTTI type info struct for the given type. 2574 /// 2575 /// \param Force - true to force the creation of this RTTI value 2576 /// \param DLLExport - true to mark the RTTI value as DLLExport 2577 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false, 2578 bool DLLExport = false); 2579 }; 2580 } 2581 2582 llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName( 2583 QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) { 2584 SmallString<256> Name; 2585 llvm::raw_svector_ostream Out(Name); 2586 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out); 2587 2588 // We know that the mangled name of the type starts at index 4 of the 2589 // mangled name of the typename, so we can just index into it in order to 2590 // get the mangled name of the type. 2591 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, 2592 Name.substr(4)); 2593 2594 llvm::GlobalVariable *GV = 2595 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage); 2596 2597 GV->setInitializer(Init); 2598 2599 return GV; 2600 } 2601 2602 llvm::Constant * 2603 ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { 2604 // Mangle the RTTI name. 2605 SmallString<256> Name; 2606 llvm::raw_svector_ostream Out(Name); 2607 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 2608 2609 // Look for an existing global. 2610 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); 2611 2612 if (!GV) { 2613 // Create a new global variable. 2614 // Note for the future: If we would ever like to do deferred emission of 2615 // RTTI, check if emitting vtables opportunistically need any adjustment. 2616 2617 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, 2618 /*Constant=*/true, 2619 llvm::GlobalValue::ExternalLinkage, nullptr, 2620 Name); 2621 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2622 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2623 if (RD->hasAttr<DLLImportAttr>()) 2624 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 2625 } 2626 } 2627 2628 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 2629 } 2630 2631 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type 2632 /// info for that type is defined in the standard library. 2633 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { 2634 // Itanium C++ ABI 2.9.2: 2635 // Basic type information (e.g. for "int", "bool", etc.) will be kept in 2636 // the run-time support library. Specifically, the run-time support 2637 // library should contain type_info objects for the types X, X* and 2638 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char, 2639 // unsigned char, signed char, short, unsigned short, int, unsigned int, 2640 // long, unsigned long, long long, unsigned long long, float, double, 2641 // long double, char16_t, char32_t, and the IEEE 754r decimal and 2642 // half-precision floating point types. 2643 // 2644 // GCC also emits RTTI for __int128. 2645 // FIXME: We do not emit RTTI information for decimal types here. 2646 2647 // Types added here must also be added to EmitFundamentalRTTIDescriptors. 2648 switch (Ty->getKind()) { 2649 case BuiltinType::Void: 2650 case BuiltinType::NullPtr: 2651 case BuiltinType::Bool: 2652 case BuiltinType::WChar_S: 2653 case BuiltinType::WChar_U: 2654 case BuiltinType::Char_U: 2655 case BuiltinType::Char_S: 2656 case BuiltinType::UChar: 2657 case BuiltinType::SChar: 2658 case BuiltinType::Short: 2659 case BuiltinType::UShort: 2660 case BuiltinType::Int: 2661 case BuiltinType::UInt: 2662 case BuiltinType::Long: 2663 case BuiltinType::ULong: 2664 case BuiltinType::LongLong: 2665 case BuiltinType::ULongLong: 2666 case BuiltinType::Half: 2667 case BuiltinType::Float: 2668 case BuiltinType::Double: 2669 case BuiltinType::LongDouble: 2670 case BuiltinType::Float16: 2671 case BuiltinType::Float128: 2672 case BuiltinType::Char16: 2673 case BuiltinType::Char32: 2674 case BuiltinType::Int128: 2675 case BuiltinType::UInt128: 2676 return true; 2677 2678 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 2679 case BuiltinType::Id: 2680 #include "clang/Basic/OpenCLImageTypes.def" 2681 case BuiltinType::OCLSampler: 2682 case BuiltinType::OCLEvent: 2683 case BuiltinType::OCLClkEvent: 2684 case BuiltinType::OCLQueue: 2685 case BuiltinType::OCLReserveID: 2686 return false; 2687 2688 case BuiltinType::Dependent: 2689 #define BUILTIN_TYPE(Id, SingletonId) 2690 #define PLACEHOLDER_TYPE(Id, SingletonId) \ 2691 case BuiltinType::Id: 2692 #include "clang/AST/BuiltinTypes.def" 2693 llvm_unreachable("asking for RRTI for a placeholder type!"); 2694 2695 case BuiltinType::ObjCId: 2696 case BuiltinType::ObjCClass: 2697 case BuiltinType::ObjCSel: 2698 llvm_unreachable("FIXME: Objective-C types are unsupported!"); 2699 } 2700 2701 llvm_unreachable("Invalid BuiltinType Kind!"); 2702 } 2703 2704 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { 2705 QualType PointeeTy = PointerTy->getPointeeType(); 2706 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); 2707 if (!BuiltinTy) 2708 return false; 2709 2710 // Check the qualifiers. 2711 Qualifiers Quals = PointeeTy.getQualifiers(); 2712 Quals.removeConst(); 2713 2714 if (!Quals.empty()) 2715 return false; 2716 2717 return TypeInfoIsInStandardLibrary(BuiltinTy); 2718 } 2719 2720 /// IsStandardLibraryRTTIDescriptor - Returns whether the type 2721 /// information for the given type exists in the standard library. 2722 static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { 2723 // Type info for builtin types is defined in the standard library. 2724 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) 2725 return TypeInfoIsInStandardLibrary(BuiltinTy); 2726 2727 // Type info for some pointer types to builtin types is defined in the 2728 // standard library. 2729 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2730 return TypeInfoIsInStandardLibrary(PointerTy); 2731 2732 return false; 2733 } 2734 2735 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for 2736 /// the given type exists somewhere else, and that we should not emit the type 2737 /// information in this translation unit. Assumes that it is not a 2738 /// standard-library type. 2739 static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, 2740 QualType Ty) { 2741 ASTContext &Context = CGM.getContext(); 2742 2743 // If RTTI is disabled, assume it might be disabled in the 2744 // translation unit that defines any potential key function, too. 2745 if (!Context.getLangOpts().RTTI) return false; 2746 2747 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2748 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); 2749 if (!RD->hasDefinition()) 2750 return false; 2751 2752 if (!RD->isDynamicClass()) 2753 return false; 2754 2755 // FIXME: this may need to be reconsidered if the key function 2756 // changes. 2757 // N.B. We must always emit the RTTI data ourselves if there exists a key 2758 // function. 2759 bool IsDLLImport = RD->hasAttr<DLLImportAttr>(); 2760 if (CGM.getVTables().isVTableExternal(RD)) 2761 return IsDLLImport && !CGM.getTriple().isWindowsItaniumEnvironment() 2762 ? false 2763 : true; 2764 2765 if (IsDLLImport) 2766 return true; 2767 } 2768 2769 return false; 2770 } 2771 2772 /// IsIncompleteClassType - Returns whether the given record type is incomplete. 2773 static bool IsIncompleteClassType(const RecordType *RecordTy) { 2774 return !RecordTy->getDecl()->isCompleteDefinition(); 2775 } 2776 2777 /// ContainsIncompleteClassType - Returns whether the given type contains an 2778 /// incomplete class type. This is true if 2779 /// 2780 /// * The given type is an incomplete class type. 2781 /// * The given type is a pointer type whose pointee type contains an 2782 /// incomplete class type. 2783 /// * The given type is a member pointer type whose class is an incomplete 2784 /// class type. 2785 /// * The given type is a member pointer type whoise pointee type contains an 2786 /// incomplete class type. 2787 /// is an indirect or direct pointer to an incomplete class type. 2788 static bool ContainsIncompleteClassType(QualType Ty) { 2789 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { 2790 if (IsIncompleteClassType(RecordTy)) 2791 return true; 2792 } 2793 2794 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) 2795 return ContainsIncompleteClassType(PointerTy->getPointeeType()); 2796 2797 if (const MemberPointerType *MemberPointerTy = 2798 dyn_cast<MemberPointerType>(Ty)) { 2799 // Check if the class type is incomplete. 2800 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); 2801 if (IsIncompleteClassType(ClassType)) 2802 return true; 2803 2804 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); 2805 } 2806 2807 return false; 2808 } 2809 2810 // CanUseSingleInheritance - Return whether the given record decl has a "single, 2811 // public, non-virtual base at offset zero (i.e. the derived class is dynamic 2812 // iff the base is)", according to Itanium C++ ABI, 2.95p6b. 2813 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { 2814 // Check the number of bases. 2815 if (RD->getNumBases() != 1) 2816 return false; 2817 2818 // Get the base. 2819 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); 2820 2821 // Check that the base is not virtual. 2822 if (Base->isVirtual()) 2823 return false; 2824 2825 // Check that the base is public. 2826 if (Base->getAccessSpecifier() != AS_public) 2827 return false; 2828 2829 // Check that the class is dynamic iff the base is. 2830 const CXXRecordDecl *BaseDecl = 2831 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2832 if (!BaseDecl->isEmpty() && 2833 BaseDecl->isDynamicClass() != RD->isDynamicClass()) 2834 return false; 2835 2836 return true; 2837 } 2838 2839 void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) { 2840 // abi::__class_type_info. 2841 static const char * const ClassTypeInfo = 2842 "_ZTVN10__cxxabiv117__class_type_infoE"; 2843 // abi::__si_class_type_info. 2844 static const char * const SIClassTypeInfo = 2845 "_ZTVN10__cxxabiv120__si_class_type_infoE"; 2846 // abi::__vmi_class_type_info. 2847 static const char * const VMIClassTypeInfo = 2848 "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; 2849 2850 const char *VTableName = nullptr; 2851 2852 switch (Ty->getTypeClass()) { 2853 #define TYPE(Class, Base) 2854 #define ABSTRACT_TYPE(Class, Base) 2855 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 2856 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2857 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 2858 #include "clang/AST/TypeNodes.def" 2859 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 2860 2861 case Type::LValueReference: 2862 case Type::RValueReference: 2863 llvm_unreachable("References shouldn't get here"); 2864 2865 case Type::Auto: 2866 case Type::DeducedTemplateSpecialization: 2867 llvm_unreachable("Undeduced type shouldn't get here"); 2868 2869 case Type::Pipe: 2870 llvm_unreachable("Pipe types shouldn't get here"); 2871 2872 case Type::Builtin: 2873 // GCC treats vector and complex types as fundamental types. 2874 case Type::Vector: 2875 case Type::ExtVector: 2876 case Type::Complex: 2877 case Type::Atomic: 2878 // FIXME: GCC treats block pointers as fundamental types?! 2879 case Type::BlockPointer: 2880 // abi::__fundamental_type_info. 2881 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; 2882 break; 2883 2884 case Type::ConstantArray: 2885 case Type::IncompleteArray: 2886 case Type::VariableArray: 2887 // abi::__array_type_info. 2888 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; 2889 break; 2890 2891 case Type::FunctionNoProto: 2892 case Type::FunctionProto: 2893 // abi::__function_type_info. 2894 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; 2895 break; 2896 2897 case Type::Enum: 2898 // abi::__enum_type_info. 2899 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; 2900 break; 2901 2902 case Type::Record: { 2903 const CXXRecordDecl *RD = 2904 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 2905 2906 if (!RD->hasDefinition() || !RD->getNumBases()) { 2907 VTableName = ClassTypeInfo; 2908 } else if (CanUseSingleInheritance(RD)) { 2909 VTableName = SIClassTypeInfo; 2910 } else { 2911 VTableName = VMIClassTypeInfo; 2912 } 2913 2914 break; 2915 } 2916 2917 case Type::ObjCObject: 2918 // Ignore protocol qualifiers. 2919 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); 2920 2921 // Handle id and Class. 2922 if (isa<BuiltinType>(Ty)) { 2923 VTableName = ClassTypeInfo; 2924 break; 2925 } 2926 2927 assert(isa<ObjCInterfaceType>(Ty)); 2928 // Fall through. 2929 2930 case Type::ObjCInterface: 2931 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { 2932 VTableName = SIClassTypeInfo; 2933 } else { 2934 VTableName = ClassTypeInfo; 2935 } 2936 break; 2937 2938 case Type::ObjCObjectPointer: 2939 case Type::Pointer: 2940 // abi::__pointer_type_info. 2941 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; 2942 break; 2943 2944 case Type::MemberPointer: 2945 // abi::__pointer_to_member_type_info. 2946 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; 2947 break; 2948 } 2949 2950 llvm::Constant *VTable = 2951 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy); 2952 2953 llvm::Type *PtrDiffTy = 2954 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); 2955 2956 // The vtable address point is 2. 2957 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); 2958 VTable = 2959 llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two); 2960 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy); 2961 2962 Fields.push_back(VTable); 2963 } 2964 2965 /// \brief Return the linkage that the type info and type info name constants 2966 /// should have for the given type. 2967 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, 2968 QualType Ty) { 2969 // Itanium C++ ABI 2.9.5p7: 2970 // In addition, it and all of the intermediate abi::__pointer_type_info 2971 // structs in the chain down to the abi::__class_type_info for the 2972 // incomplete class type must be prevented from resolving to the 2973 // corresponding type_info structs for the complete class type, possibly 2974 // by making them local static objects. Finally, a dummy class RTTI is 2975 // generated for the incomplete type that will not resolve to the final 2976 // complete class RTTI (because the latter need not exist), possibly by 2977 // making it a local static object. 2978 if (ContainsIncompleteClassType(Ty)) 2979 return llvm::GlobalValue::InternalLinkage; 2980 2981 switch (Ty->getLinkage()) { 2982 case NoLinkage: 2983 case InternalLinkage: 2984 case UniqueExternalLinkage: 2985 return llvm::GlobalValue::InternalLinkage; 2986 2987 case VisibleNoLinkage: 2988 case ModuleInternalLinkage: 2989 case ModuleLinkage: 2990 case ExternalLinkage: 2991 // RTTI is not enabled, which means that this type info struct is going 2992 // to be used for exception handling. Give it linkonce_odr linkage. 2993 if (!CGM.getLangOpts().RTTI) 2994 return llvm::GlobalValue::LinkOnceODRLinkage; 2995 2996 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { 2997 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); 2998 if (RD->hasAttr<WeakAttr>()) 2999 return llvm::GlobalValue::WeakODRLinkage; 3000 if (CGM.getTriple().isWindowsItaniumEnvironment()) 3001 if (RD->hasAttr<DLLImportAttr>() && 3002 ShouldUseExternalRTTIDescriptor(CGM, Ty)) 3003 return llvm::GlobalValue::ExternalLinkage; 3004 // MinGW always uses LinkOnceODRLinkage for type info. 3005 if (RD->isDynamicClass() && 3006 !CGM.getContext() 3007 .getTargetInfo() 3008 .getTriple() 3009 .isWindowsGNUEnvironment()) 3010 return CGM.getVTableLinkage(RD); 3011 } 3012 3013 return llvm::GlobalValue::LinkOnceODRLinkage; 3014 } 3015 3016 llvm_unreachable("Invalid linkage!"); 3017 } 3018 3019 llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force, 3020 bool DLLExport) { 3021 // We want to operate on the canonical type. 3022 Ty = Ty.getCanonicalType(); 3023 3024 // Check if we've already emitted an RTTI descriptor for this type. 3025 SmallString<256> Name; 3026 llvm::raw_svector_ostream Out(Name); 3027 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); 3028 3029 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); 3030 if (OldGV && !OldGV->isDeclaration()) { 3031 assert(!OldGV->hasAvailableExternallyLinkage() && 3032 "available_externally typeinfos not yet implemented"); 3033 3034 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy); 3035 } 3036 3037 // Check if there is already an external RTTI descriptor for this type. 3038 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); 3039 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty))) 3040 return GetAddrOfExternalRTTIDescriptor(Ty); 3041 3042 // Emit the standard library with external linkage. 3043 llvm::GlobalVariable::LinkageTypes Linkage; 3044 if (IsStdLib) 3045 Linkage = llvm::GlobalValue::ExternalLinkage; 3046 else 3047 Linkage = getTypeInfoLinkage(CGM, Ty); 3048 3049 // Add the vtable pointer. 3050 BuildVTablePointer(cast<Type>(Ty)); 3051 3052 // And the name. 3053 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage); 3054 llvm::Constant *TypeNameField; 3055 3056 // If we're supposed to demote the visibility, be sure to set a flag 3057 // to use a string comparison for type_info comparisons. 3058 ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness = 3059 CXXABI.classifyRTTIUniqueness(Ty, Linkage); 3060 if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) { 3061 // The flag is the sign bit, which on ARM64 is defined to be clear 3062 // for global pointers. This is very ARM64-specific. 3063 TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty); 3064 llvm::Constant *flag = 3065 llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63); 3066 TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag); 3067 TypeNameField = 3068 llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy); 3069 } else { 3070 TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy); 3071 } 3072 Fields.push_back(TypeNameField); 3073 3074 switch (Ty->getTypeClass()) { 3075 #define TYPE(Class, Base) 3076 #define ABSTRACT_TYPE(Class, Base) 3077 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 3078 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 3079 #define DEPENDENT_TYPE(Class, Base) case Type::Class: 3080 #include "clang/AST/TypeNodes.def" 3081 llvm_unreachable("Non-canonical and dependent types shouldn't get here"); 3082 3083 // GCC treats vector types as fundamental types. 3084 case Type::Builtin: 3085 case Type::Vector: 3086 case Type::ExtVector: 3087 case Type::Complex: 3088 case Type::BlockPointer: 3089 // Itanium C++ ABI 2.9.5p4: 3090 // abi::__fundamental_type_info adds no data members to std::type_info. 3091 break; 3092 3093 case Type::LValueReference: 3094 case Type::RValueReference: 3095 llvm_unreachable("References shouldn't get here"); 3096 3097 case Type::Auto: 3098 case Type::DeducedTemplateSpecialization: 3099 llvm_unreachable("Undeduced type shouldn't get here"); 3100 3101 case Type::Pipe: 3102 llvm_unreachable("Pipe type shouldn't get here"); 3103 3104 case Type::ConstantArray: 3105 case Type::IncompleteArray: 3106 case Type::VariableArray: 3107 // Itanium C++ ABI 2.9.5p5: 3108 // abi::__array_type_info adds no data members to std::type_info. 3109 break; 3110 3111 case Type::FunctionNoProto: 3112 case Type::FunctionProto: 3113 // Itanium C++ ABI 2.9.5p5: 3114 // abi::__function_type_info adds no data members to std::type_info. 3115 break; 3116 3117 case Type::Enum: 3118 // Itanium C++ ABI 2.9.5p5: 3119 // abi::__enum_type_info adds no data members to std::type_info. 3120 break; 3121 3122 case Type::Record: { 3123 const CXXRecordDecl *RD = 3124 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); 3125 if (!RD->hasDefinition() || !RD->getNumBases()) { 3126 // We don't need to emit any fields. 3127 break; 3128 } 3129 3130 if (CanUseSingleInheritance(RD)) 3131 BuildSIClassTypeInfo(RD); 3132 else 3133 BuildVMIClassTypeInfo(RD); 3134 3135 break; 3136 } 3137 3138 case Type::ObjCObject: 3139 case Type::ObjCInterface: 3140 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); 3141 break; 3142 3143 case Type::ObjCObjectPointer: 3144 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 3145 break; 3146 3147 case Type::Pointer: 3148 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); 3149 break; 3150 3151 case Type::MemberPointer: 3152 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); 3153 break; 3154 3155 case Type::Atomic: 3156 // No fields, at least for the moment. 3157 break; 3158 } 3159 3160 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields); 3161 3162 llvm::Module &M = CGM.getModule(); 3163 llvm::GlobalVariable *GV = 3164 new llvm::GlobalVariable(M, Init->getType(), 3165 /*Constant=*/true, Linkage, Init, Name); 3166 3167 // If there's already an old global variable, replace it with the new one. 3168 if (OldGV) { 3169 GV->takeName(OldGV); 3170 llvm::Constant *NewPtr = 3171 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 3172 OldGV->replaceAllUsesWith(NewPtr); 3173 OldGV->eraseFromParent(); 3174 } 3175 3176 if (CGM.supportsCOMDAT() && GV->isWeakForLinker()) 3177 GV->setComdat(M.getOrInsertComdat(GV->getName())); 3178 3179 // The Itanium ABI specifies that type_info objects must be globally 3180 // unique, with one exception: if the type is an incomplete class 3181 // type or a (possibly indirect) pointer to one. That exception 3182 // affects the general case of comparing type_info objects produced 3183 // by the typeid operator, which is why the comparison operators on 3184 // std::type_info generally use the type_info name pointers instead 3185 // of the object addresses. However, the language's built-in uses 3186 // of RTTI generally require class types to be complete, even when 3187 // manipulating pointers to those class types. This allows the 3188 // implementation of dynamic_cast to rely on address equality tests, 3189 // which is much faster. 3190 3191 // All of this is to say that it's important that both the type_info 3192 // object and the type_info name be uniqued when weakly emitted. 3193 3194 // Give the type_info object and name the formal visibility of the 3195 // type itself. 3196 llvm::GlobalValue::VisibilityTypes llvmVisibility; 3197 if (llvm::GlobalValue::isLocalLinkage(Linkage)) 3198 // If the linkage is local, only default visibility makes sense. 3199 llvmVisibility = llvm::GlobalValue::DefaultVisibility; 3200 else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden) 3201 llvmVisibility = llvm::GlobalValue::HiddenVisibility; 3202 else 3203 llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility()); 3204 3205 TypeName->setVisibility(llvmVisibility); 3206 GV->setVisibility(llvmVisibility); 3207 3208 if (CGM.getTriple().isWindowsItaniumEnvironment()) { 3209 auto RD = Ty->getAsCXXRecordDecl(); 3210 if (DLLExport || (RD && RD->hasAttr<DLLExportAttr>())) { 3211 TypeName->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 3212 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 3213 } else if (RD && RD->hasAttr<DLLImportAttr>() && 3214 ShouldUseExternalRTTIDescriptor(CGM, Ty)) { 3215 TypeName->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 3216 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 3217 3218 // Because the typename and the typeinfo are DLL import, convert them to 3219 // declarations rather than definitions. The initializers still need to 3220 // be constructed to calculate the type for the declarations. 3221 TypeName->setInitializer(nullptr); 3222 GV->setInitializer(nullptr); 3223 } 3224 } 3225 3226 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); 3227 } 3228 3229 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info 3230 /// for the given Objective-C object type. 3231 void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { 3232 // Drop qualifiers. 3233 const Type *T = OT->getBaseType().getTypePtr(); 3234 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); 3235 3236 // The builtin types are abi::__class_type_infos and don't require 3237 // extra fields. 3238 if (isa<BuiltinType>(T)) return; 3239 3240 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); 3241 ObjCInterfaceDecl *Super = Class->getSuperClass(); 3242 3243 // Root classes are also __class_type_info. 3244 if (!Super) return; 3245 3246 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); 3247 3248 // Everything else is single inheritance. 3249 llvm::Constant *BaseTypeInfo = 3250 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy); 3251 Fields.push_back(BaseTypeInfo); 3252 } 3253 3254 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single 3255 /// inheritance, according to the Itanium C++ ABI, 2.95p6b. 3256 void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { 3257 // Itanium C++ ABI 2.9.5p6b: 3258 // It adds to abi::__class_type_info a single member pointing to the 3259 // type_info structure for the base type, 3260 llvm::Constant *BaseTypeInfo = 3261 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType()); 3262 Fields.push_back(BaseTypeInfo); 3263 } 3264 3265 namespace { 3266 /// SeenBases - Contains virtual and non-virtual bases seen when traversing 3267 /// a class hierarchy. 3268 struct SeenBases { 3269 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; 3270 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; 3271 }; 3272 } 3273 3274 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in 3275 /// abi::__vmi_class_type_info. 3276 /// 3277 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, 3278 SeenBases &Bases) { 3279 3280 unsigned Flags = 0; 3281 3282 const CXXRecordDecl *BaseDecl = 3283 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 3284 3285 if (Base->isVirtual()) { 3286 // Mark the virtual base as seen. 3287 if (!Bases.VirtualBases.insert(BaseDecl).second) { 3288 // If this virtual base has been seen before, then the class is diamond 3289 // shaped. 3290 Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped; 3291 } else { 3292 if (Bases.NonVirtualBases.count(BaseDecl)) 3293 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 3294 } 3295 } else { 3296 // Mark the non-virtual base as seen. 3297 if (!Bases.NonVirtualBases.insert(BaseDecl).second) { 3298 // If this non-virtual base has been seen before, then the class has non- 3299 // diamond shaped repeated inheritance. 3300 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 3301 } else { 3302 if (Bases.VirtualBases.count(BaseDecl)) 3303 Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; 3304 } 3305 } 3306 3307 // Walk all bases. 3308 for (const auto &I : BaseDecl->bases()) 3309 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 3310 3311 return Flags; 3312 } 3313 3314 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { 3315 unsigned Flags = 0; 3316 SeenBases Bases; 3317 3318 // Walk all bases. 3319 for (const auto &I : RD->bases()) 3320 Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); 3321 3322 return Flags; 3323 } 3324 3325 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for 3326 /// classes with bases that do not satisfy the abi::__si_class_type_info 3327 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. 3328 void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { 3329 llvm::Type *UnsignedIntLTy = 3330 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 3331 3332 // Itanium C++ ABI 2.9.5p6c: 3333 // __flags is a word with flags describing details about the class 3334 // structure, which may be referenced by using the __flags_masks 3335 // enumeration. These flags refer to both direct and indirect bases. 3336 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); 3337 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 3338 3339 // Itanium C++ ABI 2.9.5p6c: 3340 // __base_count is a word with the number of direct proper base class 3341 // descriptions that follow. 3342 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); 3343 3344 if (!RD->getNumBases()) 3345 return; 3346 3347 // Now add the base class descriptions. 3348 3349 // Itanium C++ ABI 2.9.5p6c: 3350 // __base_info[] is an array of base class descriptions -- one for every 3351 // direct proper base. Each description is of the type: 3352 // 3353 // struct abi::__base_class_type_info { 3354 // public: 3355 // const __class_type_info *__base_type; 3356 // long __offset_flags; 3357 // 3358 // enum __offset_flags_masks { 3359 // __virtual_mask = 0x1, 3360 // __public_mask = 0x2, 3361 // __offset_shift = 8 3362 // }; 3363 // }; 3364 3365 // If we're in mingw and 'long' isn't wide enough for a pointer, use 'long 3366 // long' instead of 'long' for __offset_flags. libstdc++abi uses long long on 3367 // LLP64 platforms. 3368 // FIXME: Consider updating libc++abi to match, and extend this logic to all 3369 // LLP64 platforms. 3370 QualType OffsetFlagsTy = CGM.getContext().LongTy; 3371 const TargetInfo &TI = CGM.getContext().getTargetInfo(); 3372 if (TI.getTriple().isOSCygMing() && TI.getPointerWidth(0) > TI.getLongWidth()) 3373 OffsetFlagsTy = CGM.getContext().LongLongTy; 3374 llvm::Type *OffsetFlagsLTy = 3375 CGM.getTypes().ConvertType(OffsetFlagsTy); 3376 3377 for (const auto &Base : RD->bases()) { 3378 // The __base_type member points to the RTTI for the base type. 3379 Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType())); 3380 3381 const CXXRecordDecl *BaseDecl = 3382 cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); 3383 3384 int64_t OffsetFlags = 0; 3385 3386 // All but the lower 8 bits of __offset_flags are a signed offset. 3387 // For a non-virtual base, this is the offset in the object of the base 3388 // subobject. For a virtual base, this is the offset in the virtual table of 3389 // the virtual base offset for the virtual base referenced (negative). 3390 CharUnits Offset; 3391 if (Base.isVirtual()) 3392 Offset = 3393 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl); 3394 else { 3395 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 3396 Offset = Layout.getBaseClassOffset(BaseDecl); 3397 }; 3398 3399 OffsetFlags = uint64_t(Offset.getQuantity()) << 8; 3400 3401 // The low-order byte of __offset_flags contains flags, as given by the 3402 // masks from the enumeration __offset_flags_masks. 3403 if (Base.isVirtual()) 3404 OffsetFlags |= BCTI_Virtual; 3405 if (Base.getAccessSpecifier() == AS_public) 3406 OffsetFlags |= BCTI_Public; 3407 3408 Fields.push_back(llvm::ConstantInt::get(OffsetFlagsLTy, OffsetFlags)); 3409 } 3410 } 3411 3412 /// Compute the flags for a __pbase_type_info, and remove the corresponding 3413 /// pieces from \p Type. 3414 static unsigned extractPBaseFlags(ASTContext &Ctx, QualType &Type) { 3415 unsigned Flags = 0; 3416 3417 if (Type.isConstQualified()) 3418 Flags |= ItaniumRTTIBuilder::PTI_Const; 3419 if (Type.isVolatileQualified()) 3420 Flags |= ItaniumRTTIBuilder::PTI_Volatile; 3421 if (Type.isRestrictQualified()) 3422 Flags |= ItaniumRTTIBuilder::PTI_Restrict; 3423 Type = Type.getUnqualifiedType(); 3424 3425 // Itanium C++ ABI 2.9.5p7: 3426 // When the abi::__pbase_type_info is for a direct or indirect pointer to an 3427 // incomplete class type, the incomplete target type flag is set. 3428 if (ContainsIncompleteClassType(Type)) 3429 Flags |= ItaniumRTTIBuilder::PTI_Incomplete; 3430 3431 if (auto *Proto = Type->getAs<FunctionProtoType>()) { 3432 if (Proto->isNothrow(Ctx)) { 3433 Flags |= ItaniumRTTIBuilder::PTI_Noexcept; 3434 Type = Ctx.getFunctionType( 3435 Proto->getReturnType(), Proto->getParamTypes(), 3436 Proto->getExtProtoInfo().withExceptionSpec(EST_None)); 3437 } 3438 } 3439 3440 return Flags; 3441 } 3442 3443 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, 3444 /// used for pointer types. 3445 void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { 3446 // Itanium C++ ABI 2.9.5p7: 3447 // __flags is a flag word describing the cv-qualification and other 3448 // attributes of the type pointed to 3449 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy); 3450 3451 llvm::Type *UnsignedIntLTy = 3452 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 3453 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 3454 3455 // Itanium C++ ABI 2.9.5p7: 3456 // __pointee is a pointer to the std::type_info derivation for the 3457 // unqualified type being pointed to. 3458 llvm::Constant *PointeeTypeInfo = 3459 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy); 3460 Fields.push_back(PointeeTypeInfo); 3461 } 3462 3463 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 3464 /// struct, used for member pointer types. 3465 void 3466 ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { 3467 QualType PointeeTy = Ty->getPointeeType(); 3468 3469 // Itanium C++ ABI 2.9.5p7: 3470 // __flags is a flag word describing the cv-qualification and other 3471 // attributes of the type pointed to. 3472 unsigned Flags = extractPBaseFlags(CGM.getContext(), PointeeTy); 3473 3474 const RecordType *ClassType = cast<RecordType>(Ty->getClass()); 3475 if (IsIncompleteClassType(ClassType)) 3476 Flags |= PTI_ContainingClassIncomplete; 3477 3478 llvm::Type *UnsignedIntLTy = 3479 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); 3480 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); 3481 3482 // Itanium C++ ABI 2.9.5p7: 3483 // __pointee is a pointer to the std::type_info derivation for the 3484 // unqualified type being pointed to. 3485 llvm::Constant *PointeeTypeInfo = 3486 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(PointeeTy); 3487 Fields.push_back(PointeeTypeInfo); 3488 3489 // Itanium C++ ABI 2.9.5p9: 3490 // __context is a pointer to an abi::__class_type_info corresponding to the 3491 // class type containing the member pointed to 3492 // (e.g., the "A" in "int A::*"). 3493 Fields.push_back( 3494 ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0))); 3495 } 3496 3497 llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) { 3498 return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty); 3499 } 3500 3501 void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type, 3502 bool DLLExport) { 3503 QualType PointerType = getContext().getPointerType(Type); 3504 QualType PointerTypeConst = getContext().getPointerType(Type.withConst()); 3505 ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, /*Force=*/true, DLLExport); 3506 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, /*Force=*/true, 3507 DLLExport); 3508 ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, /*Force=*/true, 3509 DLLExport); 3510 } 3511 3512 void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(bool DLLExport) { 3513 // Types added here must also be added to TypeInfoIsInStandardLibrary. 3514 QualType FundamentalTypes[] = { 3515 getContext().VoidTy, getContext().NullPtrTy, 3516 getContext().BoolTy, getContext().WCharTy, 3517 getContext().CharTy, getContext().UnsignedCharTy, 3518 getContext().SignedCharTy, getContext().ShortTy, 3519 getContext().UnsignedShortTy, getContext().IntTy, 3520 getContext().UnsignedIntTy, getContext().LongTy, 3521 getContext().UnsignedLongTy, getContext().LongLongTy, 3522 getContext().UnsignedLongLongTy, getContext().Int128Ty, 3523 getContext().UnsignedInt128Ty, getContext().HalfTy, 3524 getContext().FloatTy, getContext().DoubleTy, 3525 getContext().LongDoubleTy, getContext().Float128Ty, 3526 getContext().Char16Ty, getContext().Char32Ty 3527 }; 3528 for (const QualType &FundamentalType : FundamentalTypes) 3529 EmitFundamentalRTTIDescriptor(FundamentalType, DLLExport); 3530 } 3531 3532 /// What sort of uniqueness rules should we use for the RTTI for the 3533 /// given type? 3534 ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness( 3535 QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const { 3536 if (shouldRTTIBeUnique()) 3537 return RUK_Unique; 3538 3539 // It's only necessary for linkonce_odr or weak_odr linkage. 3540 if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage && 3541 Linkage != llvm::GlobalValue::WeakODRLinkage) 3542 return RUK_Unique; 3543 3544 // It's only necessary with default visibility. 3545 if (CanTy->getVisibility() != DefaultVisibility) 3546 return RUK_Unique; 3547 3548 // If we're not required to publish this symbol, hide it. 3549 if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage) 3550 return RUK_NonUniqueHidden; 3551 3552 // If we're required to publish this symbol, as we might be under an 3553 // explicit instantiation, leave it with default visibility but 3554 // enable string-comparisons. 3555 assert(Linkage == llvm::GlobalValue::WeakODRLinkage); 3556 return RUK_NonUniqueVisible; 3557 } 3558 3559 // Find out how to codegen the complete destructor and constructor 3560 namespace { 3561 enum class StructorCodegen { Emit, RAUW, Alias, COMDAT }; 3562 } 3563 static StructorCodegen getCodegenToUse(CodeGenModule &CGM, 3564 const CXXMethodDecl *MD) { 3565 if (!CGM.getCodeGenOpts().CXXCtorDtorAliases) 3566 return StructorCodegen::Emit; 3567 3568 // The complete and base structors are not equivalent if there are any virtual 3569 // bases, so emit separate functions. 3570 if (MD->getParent()->getNumVBases()) 3571 return StructorCodegen::Emit; 3572 3573 GlobalDecl AliasDecl; 3574 if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { 3575 AliasDecl = GlobalDecl(DD, Dtor_Complete); 3576 } else { 3577 const auto *CD = cast<CXXConstructorDecl>(MD); 3578 AliasDecl = GlobalDecl(CD, Ctor_Complete); 3579 } 3580 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3581 3582 if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) 3583 return StructorCodegen::RAUW; 3584 3585 // FIXME: Should we allow available_externally aliases? 3586 if (!llvm::GlobalAlias::isValidLinkage(Linkage)) 3587 return StructorCodegen::RAUW; 3588 3589 if (llvm::GlobalValue::isWeakForLinker(Linkage)) { 3590 // Only ELF and wasm support COMDATs with arbitrary names (C5/D5). 3591 if (CGM.getTarget().getTriple().isOSBinFormatELF() || 3592 CGM.getTarget().getTriple().isOSBinFormatWasm()) 3593 return StructorCodegen::COMDAT; 3594 return StructorCodegen::Emit; 3595 } 3596 3597 return StructorCodegen::Alias; 3598 } 3599 3600 static void emitConstructorDestructorAlias(CodeGenModule &CGM, 3601 GlobalDecl AliasDecl, 3602 GlobalDecl TargetDecl) { 3603 llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); 3604 3605 StringRef MangledName = CGM.getMangledName(AliasDecl); 3606 llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName); 3607 if (Entry && !Entry->isDeclaration()) 3608 return; 3609 3610 auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl)); 3611 3612 // Create the alias with no name. 3613 auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee); 3614 3615 // Switch any previous uses to the alias. 3616 if (Entry) { 3617 assert(Entry->getType() == Aliasee->getType() && 3618 "declaration exists with different type"); 3619 Alias->takeName(Entry); 3620 Entry->replaceAllUsesWith(Alias); 3621 Entry->eraseFromParent(); 3622 } else { 3623 Alias->setName(MangledName); 3624 } 3625 3626 // Finally, set up the alias with its proper name and attributes. 3627 CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); 3628 } 3629 3630 void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD, 3631 StructorType Type) { 3632 auto *CD = dyn_cast<CXXConstructorDecl>(MD); 3633 const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD); 3634 3635 StructorCodegen CGType = getCodegenToUse(CGM, MD); 3636 3637 if (Type == StructorType::Complete) { 3638 GlobalDecl CompleteDecl; 3639 GlobalDecl BaseDecl; 3640 if (CD) { 3641 CompleteDecl = GlobalDecl(CD, Ctor_Complete); 3642 BaseDecl = GlobalDecl(CD, Ctor_Base); 3643 } else { 3644 CompleteDecl = GlobalDecl(DD, Dtor_Complete); 3645 BaseDecl = GlobalDecl(DD, Dtor_Base); 3646 } 3647 3648 if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) { 3649 emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl); 3650 return; 3651 } 3652 3653 if (CGType == StructorCodegen::RAUW) { 3654 StringRef MangledName = CGM.getMangledName(CompleteDecl); 3655 auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl); 3656 CGM.addReplacement(MangledName, Aliasee); 3657 return; 3658 } 3659 } 3660 3661 // The base destructor is equivalent to the base destructor of its 3662 // base class if there is exactly one non-virtual base class with a 3663 // non-trivial destructor, there are no fields with a non-trivial 3664 // destructor, and the body of the destructor is trivial. 3665 if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT && 3666 !CGM.TryEmitBaseDestructorAsAlias(DD)) 3667 return; 3668 3669 // FIXME: The deleting destructor is equivalent to the selected operator 3670 // delete if: 3671 // * either the delete is a destroying operator delete or the destructor 3672 // would be trivial if it weren't virtual, 3673 // * the conversion from the 'this' parameter to the first parameter of the 3674 // destructor is equivalent to a bitcast, 3675 // * the destructor does not have an implicit "this" return, and 3676 // * the operator delete has the same calling convention and IR function type 3677 // as the destructor. 3678 // In such cases we should try to emit the deleting dtor as an alias to the 3679 // selected 'operator delete'. 3680 3681 llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type); 3682 3683 if (CGType == StructorCodegen::COMDAT) { 3684 SmallString<256> Buffer; 3685 llvm::raw_svector_ostream Out(Buffer); 3686 if (DD) 3687 getMangleContext().mangleCXXDtorComdat(DD, Out); 3688 else 3689 getMangleContext().mangleCXXCtorComdat(CD, Out); 3690 llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str()); 3691 Fn->setComdat(C); 3692 } else { 3693 CGM.maybeSetTrivialComdat(*MD, *Fn); 3694 } 3695 } 3696 3697 static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) { 3698 // void *__cxa_begin_catch(void*); 3699 llvm::FunctionType *FTy = llvm::FunctionType::get( 3700 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 3701 3702 return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); 3703 } 3704 3705 static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) { 3706 // void __cxa_end_catch(); 3707 llvm::FunctionType *FTy = 3708 llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); 3709 3710 return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); 3711 } 3712 3713 static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) { 3714 // void *__cxa_get_exception_ptr(void*); 3715 llvm::FunctionType *FTy = llvm::FunctionType::get( 3716 CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 3717 3718 return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr"); 3719 } 3720 3721 namespace { 3722 /// A cleanup to call __cxa_end_catch. In many cases, the caught 3723 /// exception type lets us state definitively that the thrown exception 3724 /// type does not have a destructor. In particular: 3725 /// - Catch-alls tell us nothing, so we have to conservatively 3726 /// assume that the thrown exception might have a destructor. 3727 /// - Catches by reference behave according to their base types. 3728 /// - Catches of non-record types will only trigger for exceptions 3729 /// of non-record types, which never have destructors. 3730 /// - Catches of record types can trigger for arbitrary subclasses 3731 /// of the caught type, so we have to assume the actual thrown 3732 /// exception type might have a throwing destructor, even if the 3733 /// caught type's destructor is trivial or nothrow. 3734 struct CallEndCatch final : EHScopeStack::Cleanup { 3735 CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {} 3736 bool MightThrow; 3737 3738 void Emit(CodeGenFunction &CGF, Flags flags) override { 3739 if (!MightThrow) { 3740 CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM)); 3741 return; 3742 } 3743 3744 CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM)); 3745 } 3746 }; 3747 } 3748 3749 /// Emits a call to __cxa_begin_catch and enters a cleanup to call 3750 /// __cxa_end_catch. 3751 /// 3752 /// \param EndMightThrow - true if __cxa_end_catch might throw 3753 static llvm::Value *CallBeginCatch(CodeGenFunction &CGF, 3754 llvm::Value *Exn, 3755 bool EndMightThrow) { 3756 llvm::CallInst *call = 3757 CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn); 3758 3759 CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow); 3760 3761 return call; 3762 } 3763 3764 /// A "special initializer" callback for initializing a catch 3765 /// parameter during catch initialization. 3766 static void InitCatchParam(CodeGenFunction &CGF, 3767 const VarDecl &CatchParam, 3768 Address ParamAddr, 3769 SourceLocation Loc) { 3770 // Load the exception from where the landing pad saved it. 3771 llvm::Value *Exn = CGF.getExceptionFromSlot(); 3772 3773 CanQualType CatchType = 3774 CGF.CGM.getContext().getCanonicalType(CatchParam.getType()); 3775 llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType); 3776 3777 // If we're catching by reference, we can just cast the object 3778 // pointer to the appropriate pointer. 3779 if (isa<ReferenceType>(CatchType)) { 3780 QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType(); 3781 bool EndCatchMightThrow = CaughtType->isRecordType(); 3782 3783 // __cxa_begin_catch returns the adjusted object pointer. 3784 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow); 3785 3786 // We have no way to tell the personality function that we're 3787 // catching by reference, so if we're catching a pointer, 3788 // __cxa_begin_catch will actually return that pointer by value. 3789 if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) { 3790 QualType PointeeType = PT->getPointeeType(); 3791 3792 // When catching by reference, generally we should just ignore 3793 // this by-value pointer and use the exception object instead. 3794 if (!PointeeType->isRecordType()) { 3795 3796 // Exn points to the struct _Unwind_Exception header, which 3797 // we have to skip past in order to reach the exception data. 3798 unsigned HeaderSize = 3799 CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException(); 3800 AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize); 3801 3802 // However, if we're catching a pointer-to-record type that won't 3803 // work, because the personality function might have adjusted 3804 // the pointer. There's actually no way for us to fully satisfy 3805 // the language/ABI contract here: we can't use Exn because it 3806 // might have the wrong adjustment, but we can't use the by-value 3807 // pointer because it's off by a level of abstraction. 3808 // 3809 // The current solution is to dump the adjusted pointer into an 3810 // alloca, which breaks language semantics (because changing the 3811 // pointer doesn't change the exception) but at least works. 3812 // The better solution would be to filter out non-exact matches 3813 // and rethrow them, but this is tricky because the rethrow 3814 // really needs to be catchable by other sites at this landing 3815 // pad. The best solution is to fix the personality function. 3816 } else { 3817 // Pull the pointer for the reference type off. 3818 llvm::Type *PtrTy = 3819 cast<llvm::PointerType>(LLVMCatchTy)->getElementType(); 3820 3821 // Create the temporary and write the adjusted pointer into it. 3822 Address ExnPtrTmp = 3823 CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp"); 3824 llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 3825 CGF.Builder.CreateStore(Casted, ExnPtrTmp); 3826 3827 // Bind the reference to the temporary. 3828 AdjustedExn = ExnPtrTmp.getPointer(); 3829 } 3830 } 3831 3832 llvm::Value *ExnCast = 3833 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref"); 3834 CGF.Builder.CreateStore(ExnCast, ParamAddr); 3835 return; 3836 } 3837 3838 // Scalars and complexes. 3839 TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType); 3840 if (TEK != TEK_Aggregate) { 3841 llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false); 3842 3843 // If the catch type is a pointer type, __cxa_begin_catch returns 3844 // the pointer by value. 3845 if (CatchType->hasPointerRepresentation()) { 3846 llvm::Value *CastExn = 3847 CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted"); 3848 3849 switch (CatchType.getQualifiers().getObjCLifetime()) { 3850 case Qualifiers::OCL_Strong: 3851 CastExn = CGF.EmitARCRetainNonBlock(CastExn); 3852 // fallthrough 3853 3854 case Qualifiers::OCL_None: 3855 case Qualifiers::OCL_ExplicitNone: 3856 case Qualifiers::OCL_Autoreleasing: 3857 CGF.Builder.CreateStore(CastExn, ParamAddr); 3858 return; 3859 3860 case Qualifiers::OCL_Weak: 3861 CGF.EmitARCInitWeak(ParamAddr, CastExn); 3862 return; 3863 } 3864 llvm_unreachable("bad ownership qualifier!"); 3865 } 3866 3867 // Otherwise, it returns a pointer into the exception object. 3868 3869 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok 3870 llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); 3871 3872 LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType); 3873 LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType); 3874 switch (TEK) { 3875 case TEK_Complex: 3876 CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV, 3877 /*init*/ true); 3878 return; 3879 case TEK_Scalar: { 3880 llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc); 3881 CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true); 3882 return; 3883 } 3884 case TEK_Aggregate: 3885 llvm_unreachable("evaluation kind filtered out!"); 3886 } 3887 llvm_unreachable("bad evaluation kind"); 3888 } 3889 3890 assert(isa<RecordType>(CatchType) && "unexpected catch type!"); 3891 auto catchRD = CatchType->getAsCXXRecordDecl(); 3892 CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD); 3893 3894 llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok 3895 3896 // Check for a copy expression. If we don't have a copy expression, 3897 // that means a trivial copy is okay. 3898 const Expr *copyExpr = CatchParam.getInit(); 3899 if (!copyExpr) { 3900 llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true); 3901 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy), 3902 caughtExnAlignment); 3903 CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType); 3904 return; 3905 } 3906 3907 // We have to call __cxa_get_exception_ptr to get the adjusted 3908 // pointer before copying. 3909 llvm::CallInst *rawAdjustedExn = 3910 CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn); 3911 3912 // Cast that to the appropriate type. 3913 Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy), 3914 caughtExnAlignment); 3915 3916 // The copy expression is defined in terms of an OpaqueValueExpr. 3917 // Find it and map it to the adjusted expression. 3918 CodeGenFunction::OpaqueValueMapping 3919 opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr), 3920 CGF.MakeAddrLValue(adjustedExn, CatchParam.getType())); 3921 3922 // Call the copy ctor in a terminate scope. 3923 CGF.EHStack.pushTerminate(); 3924 3925 // Perform the copy construction. 3926 CGF.EmitAggExpr(copyExpr, 3927 AggValueSlot::forAddr(ParamAddr, Qualifiers(), 3928 AggValueSlot::IsNotDestructed, 3929 AggValueSlot::DoesNotNeedGCBarriers, 3930 AggValueSlot::IsNotAliased)); 3931 3932 // Leave the terminate scope. 3933 CGF.EHStack.popTerminate(); 3934 3935 // Undo the opaque value mapping. 3936 opaque.pop(); 3937 3938 // Finally we can call __cxa_begin_catch. 3939 CallBeginCatch(CGF, Exn, true); 3940 } 3941 3942 /// Begins a catch statement by initializing the catch variable and 3943 /// calling __cxa_begin_catch. 3944 void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF, 3945 const CXXCatchStmt *S) { 3946 // We have to be very careful with the ordering of cleanups here: 3947 // C++ [except.throw]p4: 3948 // The destruction [of the exception temporary] occurs 3949 // immediately after the destruction of the object declared in 3950 // the exception-declaration in the handler. 3951 // 3952 // So the precise ordering is: 3953 // 1. Construct catch variable. 3954 // 2. __cxa_begin_catch 3955 // 3. Enter __cxa_end_catch cleanup 3956 // 4. Enter dtor cleanup 3957 // 3958 // We do this by using a slightly abnormal initialization process. 3959 // Delegation sequence: 3960 // - ExitCXXTryStmt opens a RunCleanupsScope 3961 // - EmitAutoVarAlloca creates the variable and debug info 3962 // - InitCatchParam initializes the variable from the exception 3963 // - CallBeginCatch calls __cxa_begin_catch 3964 // - CallBeginCatch enters the __cxa_end_catch cleanup 3965 // - EmitAutoVarCleanups enters the variable destructor cleanup 3966 // - EmitCXXTryStmt emits the code for the catch body 3967 // - EmitCXXTryStmt close the RunCleanupsScope 3968 3969 VarDecl *CatchParam = S->getExceptionDecl(); 3970 if (!CatchParam) { 3971 llvm::Value *Exn = CGF.getExceptionFromSlot(); 3972 CallBeginCatch(CGF, Exn, true); 3973 return; 3974 } 3975 3976 // Emit the local. 3977 CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam); 3978 InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart()); 3979 CGF.EmitAutoVarCleanups(var); 3980 } 3981 3982 /// Get or define the following function: 3983 /// void @__clang_call_terminate(i8* %exn) nounwind noreturn 3984 /// This code is used only in C++. 3985 static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) { 3986 llvm::FunctionType *fnTy = 3987 llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); 3988 llvm::Constant *fnRef = CGM.CreateRuntimeFunction( 3989 fnTy, "__clang_call_terminate", llvm::AttributeList(), /*Local=*/true); 3990 3991 llvm::Function *fn = dyn_cast<llvm::Function>(fnRef); 3992 if (fn && fn->empty()) { 3993 fn->setDoesNotThrow(); 3994 fn->setDoesNotReturn(); 3995 3996 // What we really want is to massively penalize inlining without 3997 // forbidding it completely. The difference between that and 3998 // 'noinline' is negligible. 3999 fn->addFnAttr(llvm::Attribute::NoInline); 4000 4001 // Allow this function to be shared across translation units, but 4002 // we don't want it to turn into an exported symbol. 4003 fn->setLinkage(llvm::Function::LinkOnceODRLinkage); 4004 fn->setVisibility(llvm::Function::HiddenVisibility); 4005 if (CGM.supportsCOMDAT()) 4006 fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName())); 4007 4008 // Set up the function. 4009 llvm::BasicBlock *entry = 4010 llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn); 4011 CGBuilderTy builder(CGM, entry); 4012 4013 // Pull the exception pointer out of the parameter list. 4014 llvm::Value *exn = &*fn->arg_begin(); 4015 4016 // Call __cxa_begin_catch(exn). 4017 llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn); 4018 catchCall->setDoesNotThrow(); 4019 catchCall->setCallingConv(CGM.getRuntimeCC()); 4020 4021 // Call std::terminate(). 4022 llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn()); 4023 termCall->setDoesNotThrow(); 4024 termCall->setDoesNotReturn(); 4025 termCall->setCallingConv(CGM.getRuntimeCC()); 4026 4027 // std::terminate cannot return. 4028 builder.CreateUnreachable(); 4029 } 4030 4031 return fnRef; 4032 } 4033 4034 llvm::CallInst * 4035 ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF, 4036 llvm::Value *Exn) { 4037 // In C++, we want to call __cxa_begin_catch() before terminating. 4038 if (Exn) { 4039 assert(CGF.CGM.getLangOpts().CPlusPlus); 4040 return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn); 4041 } 4042 return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn()); 4043 } 4044