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