ItaniumCXXABI.cpp revision 3b50e8d78c34fc57e25781015a2cb0536ca54f89
13a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===// 23a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 33a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// The LLVM Compiler Infrastructure 43a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 53a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// This file is distributed under the University of Illinois Open Source 63a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// License. See LICENSE.TXT for details. 73a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 83a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 93a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 10fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// This provides C++ code generation targeting the Itanium C++ ABI. The class 113a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// in this file generates structures that follow the Itanium C++ ABI, which is 123a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// documented at: 133a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// http://www.codesourcery.com/public/cxx-abi/abi.html 143a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// http://www.codesourcery.com/public/cxx-abi/abi-eh.html 15ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// 16ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// It also supports the closely-related ARM ABI, documented at: 17ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf 18ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall// 193a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 203a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 213a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CGCXXABI.h" 220bab0cdab751248ca389a5592bcb70eac5d39260John McCall#include "CGRecordLayout.h" 239ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis#include "CGVTables.h" 2493d557bc1867b7d7b102f87290194b4be7932c92John McCall#include "CodeGenFunction.h" 253a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CodeGenModule.h" 26ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "clang/AST/Mangle.h" 27ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "clang/AST/Type.h" 283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Intrinsics.h" 303b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Value.h" 313a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisusing namespace clang; 3393d557bc1867b7d7b102f87290194b4be7932c92John McCallusing namespace CodeGen; 343a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 353a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisnamespace { 36071cc7deffad608165b1ddd5263e8bf181861520Charles Davisclass ItaniumCXXABI : public CodeGen::CGCXXABI { 3793d557bc1867b7d7b102f87290194b4be7932c92John McCallprotected: 38babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall bool IsARM; 390bab0cdab751248ca389a5592bcb70eac5d39260John McCall 403a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davispublic: 41babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) : 4292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner CGCXXABI(CGM), IsARM(IsARM) { } 4393d557bc1867b7d7b102f87290194b4be7932c92John McCall 44ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov bool isReturnTypeIndirect(const CXXRecordDecl *RD) const { 45ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov // Structures with either a non-trivial destructor or a non-trivial 46ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov // copy constructor are always indirect. 47ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor(); 48ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov } 49ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov 50ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const { 51ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov // Structures with either a non-trivial destructor or a non-trivial 52ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov // copy constructor are always indirect. 53ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov if (!RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) 54ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov return RAA_Indirect; 55ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov return RAA_Default; 56ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov } 57ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov 58f16aa103d3afd42fbca2ab346f191bf745cec092John McCall bool isZeroInitializable(const MemberPointerType *MPT); 59cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); 610bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6293d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 6393d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *&This, 6493d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *MemFnPtr, 6593d557bc1867b7d7b102f87290194b4be7932c92John McCall const MemberPointerType *MPT); 663023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 676c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, 686c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Base, 696c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *MemPtr, 706c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall const MemberPointerType *MPT); 716c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 720bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, 730bab0cdab751248ca389a5592bcb70eac5d39260John McCall const CastExpr *E, 740bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *Src); 754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, 764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *Src); 77cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 780bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); 79cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 80755d8497e39071aa24acc173ff07083e3256b8f8John McCall llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); 815808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, 825808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall CharUnits offset); 832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); 842d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, 852d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits ThisAdjustment); 86875ab10245d3bf37252dd822aa1616bb0a391095John McCall 870bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, 880bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *L, 890bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *R, 900bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT, 910bab0cdab751248ca389a5592bcb70eac5d39260John McCall bool Inequality); 92e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 930bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 940bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *Addr, 950bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT); 964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 97ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, 98ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *ptr, 99ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall QualType type); 100ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 101b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, 102b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *This, 103b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner const CXXRecordDecl *ClassDecl, 104b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner const CXXRecordDecl *BaseClassDecl); 105b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner 1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildConstructorSignature(const CXXConstructorDecl *Ctor, 1074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType T, 1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildDestructorSignature(const CXXDestructorDecl *Dtor, 1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType T, 1134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 1145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys); 1154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void BuildInstanceFunctionParams(CodeGenFunction &CGF, 1174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 1184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params); 1194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void EmitInstanceFunctionProlog(CodeGenFunction &CGF); 1211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 1223b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin void EmitConstructorCall(CodeGenFunction &CGF, 1233b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin const CXXConstructorDecl *D, CXXCtorType Type, 1243b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin bool ForVirtualBase, bool Delegating, 1254444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin llvm::Value *This, 1264444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin CallExpr::const_arg_iterator ArgBeg, 1274444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin CallExpr::const_arg_iterator ArgEnd); 1284444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin 1293b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin void EmitVirtualDestructorCall(CodeGenFunction &CGF, 1303b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin const CXXDestructorDecl *Dtor, 1313b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin CXXDtorType DtorType, SourceLocation CallLoc, 1323b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin llvm::Value *This); 1330f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov 1349063302a82423cb83f002257a416741850739a70Reid Kleckner void EmitVirtualInheritanceTables(llvm::GlobalVariable::LinkageTypes Linkage, 1359063302a82423cb83f002257a416741850739a70Reid Kleckner const CXXRecordDecl *RD); 1369063302a82423cb83f002257a416741850739a70Reid Kleckner 137285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; } 1382eb9a959d24ad757a82ecab61f343635ad67749aDavid Blaikie StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; } 139285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos 140e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits getArrayCookieSizeImpl(QualType elementType); 1411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 1421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 1431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 1446ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 1451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType); 146e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, 147e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 148e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize); 1495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 1503030eb82593097502469a8b3fc26112c79c75605John McCall void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, 1510f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth llvm::GlobalVariable *DeclPtr, bool PerformInit); 15204e517650569598e847c2ab609672e6df93effe5Richard Smith void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, 15304e517650569598e847c2ab609672e6df93effe5Richard Smith llvm::Constant *dtor, llvm::Constant *addr); 154b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 155b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD, 156b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::GlobalVariable *Var); 157b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith void EmitThreadLocalInitFuncs( 158b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, 159b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *InitFunc); 160b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, 161b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith const DeclRefExpr *DRE); 162e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 163e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne bool NeedsVTTParameter(GlobalDecl GD); 1643a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}; 165ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall 166ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI { 167ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic: 168babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {} 1694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1703b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin bool HasThisReturn(GlobalDecl GD) const { 1713b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin return (isa<CXXConstructorDecl>(GD.getDecl()) || ( 1723b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin isa<CXXDestructorDecl>(GD.getDecl()) && 1733b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin GD.getDtorType() != Dtor_Deleting)); 1743b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin } 1754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); 1774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 178e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits getArrayCookieSizeImpl(QualType elementType); 1791e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, 1801e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 1811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 1826ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 1831e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType); 184e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, 185e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize); 186ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}; 1873a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis} 1883a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 189071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { 19064aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall switch (CGM.getTarget().getCXXABI().getKind()) { 19196fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall // For IR-generation purposes, there's no significant difference 19296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall // between the ARM and iOS ABIs. 19396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::GenericARM: 19496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::iOS: 19596fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall return new ARMCXXABI(CGM); 19696fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall 197c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't 198c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover // include the other 32-bit ARM oddities: constructor/destructor return values 199c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover // and array cookies. 200c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover case TargetCXXABI::GenericAArch64: 201c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover return new ItaniumCXXABI(CGM, /*IsARM = */ true); 202c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover 20396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::GenericItanium: 20496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall return new ItaniumCXXABI(CGM); 20596fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall 20696fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case TargetCXXABI::Microsoft: 20796fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall llvm_unreachable("Microsoft ABI is not Itanium-based"); 20896fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall } 20996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall llvm_unreachable("bad ABI kind"); 210ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall} 211ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall 2129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type * 2130bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { 2140bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 21592e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner return CGM.PtrDiffTy; 21692e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, NULL); 217875ab10245d3bf37252dd822aa1616bb0a391095John McCall} 218875ab10245d3bf37252dd822aa1616bb0a391095John McCall 219babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form: 220babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; 221babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 222babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI: 223babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - method pointers are virtual if (memptr.ptr & 1) is nonzero 224babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the this-adjustment is (memptr.adj) 225babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the virtual offset is (memptr.ptr - 1) 226babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 227babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI: 228babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - method pointers are virtual if (memptr.adj & 1) is nonzero 229babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the this-adjustment is (memptr.adj >> 1) 230babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// - the virtual offset is (memptr.ptr) 231babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions 232babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned. 233babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 234babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points 235babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied. 236babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// 237babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of 238babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call. 23993d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value * 24093d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, 24193d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *&This, 24293d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *MemFnPtr, 24393d557bc1867b7d7b102f87290194b4be7932c92John McCall const MemberPointerType *MPT) { 24493d557bc1867b7d7b102f87290194b4be7932c92John McCall CGBuilderTy &Builder = CGF.Builder; 24593d557bc1867b7d7b102f87290194b4be7932c92John McCall 24693d557bc1867b7d7b102f87290194b4be7932c92John McCall const FunctionProtoType *FPT = 24793d557bc1867b7d7b102f87290194b4be7932c92John McCall MPT->getPointeeType()->getAs<FunctionProtoType>(); 24893d557bc1867b7d7b102f87290194b4be7932c92John McCall const CXXRecordDecl *RD = 24993d557bc1867b7d7b102f87290194b4be7932c92John McCall cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 25093d557bc1867b7d7b102f87290194b4be7932c92John McCall 2512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 252de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().GetFunctionType( 253de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeCXXMethodType(RD, FPT)); 25493d557bc1867b7d7b102f87290194b4be7932c92John McCall 25592e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); 25693d557bc1867b7d7b102f87290194b4be7932c92John McCall 257babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); 258babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); 259babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); 260babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 261d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Extract memptr.adj, which is in the second field. 262d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); 263babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 264babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // Compute the true adjustment. 265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *Adj = RawAdj; 266babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (IsARM) 267babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); 26893d557bc1867b7d7b102f87290194b4be7932c92John McCall 26993d557bc1867b7d7b102f87290194b4be7932c92John McCall // Apply the adjustment and cast back to the original struct type 27093d557bc1867b7d7b102f87290194b4be7932c92John McCall // for consistency. 271babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); 272babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); 273babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); 27493d557bc1867b7d7b102f87290194b4be7932c92John McCall 27593d557bc1867b7d7b102f87290194b4be7932c92John McCall // Load the function pointer. 276d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); 27793d557bc1867b7d7b102f87290194b4be7932c92John McCall 27893d557bc1867b7d7b102f87290194b4be7932c92John McCall // If the LSB in the function pointer is 1, the function pointer points to 27993d557bc1867b7d7b102f87290194b4be7932c92John McCall // a virtual function. 280babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *IsVirtual; 281babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (IsARM) 282babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); 283babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall else 284babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); 285babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); 28693d557bc1867b7d7b102f87290194b4be7932c92John McCall Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 28793d557bc1867b7d7b102f87290194b4be7932c92John McCall 28893d557bc1867b7d7b102f87290194b4be7932c92John McCall // In the virtual path, the adjustment left 'This' pointing to the 28993d557bc1867b7d7b102f87290194b4be7932c92John McCall // vtable of the correct base subobject. The "function pointer" is an 290babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // offset within the vtable (+1 for the virtual flag on non-ARM). 29193d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnVirtual); 29293d557bc1867b7d7b102f87290194b4be7932c92John McCall 29393d557bc1867b7d7b102f87290194b4be7932c92John McCall // Cast the adjusted this to a pointer to vtable pointer and load. 2942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *VTableTy = Builder.getInt8PtrTy(); 29593d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); 296babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall VTable = Builder.CreateLoad(VTable, "memptr.vtable"); 29793d557bc1867b7d7b102f87290194b4be7932c92John McCall 29893d557bc1867b7d7b102f87290194b4be7932c92John McCall // Apply the offset. 299babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *VTableOffset = FnAsInt; 300babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); 301babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall VTable = Builder.CreateGEP(VTable, VTableOffset); 30293d557bc1867b7d7b102f87290194b4be7932c92John McCall 30393d557bc1867b7d7b102f87290194b4be7932c92John McCall // Load the virtual function to call. 30493d557bc1867b7d7b102f87290194b4be7932c92John McCall VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); 305babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); 30693d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBranch(FnEnd); 30793d557bc1867b7d7b102f87290194b4be7932c92John McCall 30893d557bc1867b7d7b102f87290194b4be7932c92John McCall // In the non-virtual path, the function pointer is actually a 30993d557bc1867b7d7b102f87290194b4be7932c92John McCall // function pointer. 31093d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnNonVirtual); 31193d557bc1867b7d7b102f87290194b4be7932c92John McCall llvm::Value *NonVirtualFn = 312babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); 31393d557bc1867b7d7b102f87290194b4be7932c92John McCall 31493d557bc1867b7d7b102f87290194b4be7932c92John McCall // We're done. 31593d557bc1867b7d7b102f87290194b4be7932c92John McCall CGF.EmitBlock(FnEnd); 316bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); 31793d557bc1867b7d7b102f87290194b4be7932c92John McCall Callee->addIncoming(VirtualFn, FnVirtual); 31893d557bc1867b7d7b102f87290194b4be7932c92John McCall Callee->addIncoming(NonVirtualFn, FnNonVirtual); 31993d557bc1867b7d7b102f87290194b4be7932c92John McCall return Callee; 32093d557bc1867b7d7b102f87290194b4be7932c92John McCall} 3213023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3226c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a 3236c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object. 3246c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, 3256c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Base, 3266c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *MemPtr, 3276c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall const MemberPointerType *MPT) { 32892e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner assert(MemPtr->getType() == CGM.PtrDiffTy); 3296c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3306c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall CGBuilderTy &Builder = CGF.Builder; 3316c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 332956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = Base->getType()->getPointerAddressSpace(); 3336c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3346c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Cast to char*. 3356c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); 3366c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3376c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Apply the offset, which we assume is non-null. 3386c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); 3396c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3406c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Cast the address to the appropriate pointer type, adopting the 3416c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // address space of the base pointer. 3422acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *PType 343eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); 3446c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return Builder.CreateBitCast(Addr, PType); 3456c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall} 3466c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 3474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Perform a bitcast, derived-to-base, or base-to-derived member pointer 3484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// conversion. 3494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// 3504d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Bitcast conversions are always a no-op under Itanium. 3510bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3520bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram: 3530bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// <-- offset --> <-- adjustment --> 3540bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// |--------------------------|----------------------|--------------------| 3550bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// ^Derived address point ^Base address point ^Member address point 3560bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3570bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer, 3580bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has 3590bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased; and conversely, when converting a derived MP to a base MP 3600bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point 3610bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased. 3620bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3630bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from 3640bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here. 3650bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// 3660bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base 3670bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base. 3680bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member 3690bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers. 370d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value * 3710bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, 3720bab0cdab751248ca389a5592bcb70eac5d39260John McCall const CastExpr *E, 3734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *src) { 3742de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 3754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_BaseToDerivedMemberPointer || 3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_ReinterpretMemberPointer); 3774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Under Itanium, reinterprets don't require any additional processing. 3794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 3804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Use constant emission if we can. 3824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isa<llvm::Constant>(src)) 3834d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); 3844d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3854d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *adj = getMemberPointerAdjustment(E); 3864d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (!adj) return src; 3873023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3883023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall CGBuilderTy &Builder = CGF.Builder; 3894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 3904d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3914d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const MemberPointerType *destTy = 3924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getType()->castAs<MemberPointerType>(); 3933023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 3944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // For member data pointers, this is just a matter of adding the 3954d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // offset if the source is non-null. 3964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (destTy->isMemberDataPointer()) { 3974d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *dst; 3984d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 3994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dst = Builder.CreateNSWSub(src, adj, "adj"); 4004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dst = Builder.CreateNSWAdd(src, adj, "adj"); 4023023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Null check. 4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); 4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); 4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return Builder.CreateSelect(isNull, src, dst); 4074d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4083023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // The this-adjustment is left-shifted by 1 on ARM. 4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (IsARM) { 4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall offset <<= 1; 4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall adj = llvm::ConstantInt::get(adj->getType(), offset); 4144d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4153023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4164d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); 4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Value *dstAdj; 4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); 4203023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall else 4214d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); 4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return Builder.CreateInsertValue(src, dstAdj, 1); 4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall} 4254d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4264d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallllvm::Constant * 4274d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, 4284d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *src) { 4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || 4304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_BaseToDerivedMemberPointer || 4314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getCastKind() == CK_ReinterpretMemberPointer); 4323023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4334d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Under Itanium, reinterprets don't require any additional processing. 4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; 4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // If the adjustment is trivial, we don't need to do anything. 4374d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *adj = getMemberPointerAdjustment(E); 4384d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (!adj) return src; 4394d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4404d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); 4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const MemberPointerType *destTy = 4434d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall E->getType()->castAs<MemberPointerType>(); 444875ab10245d3bf37252dd822aa1616bb0a391095John McCall 4450bab0cdab751248ca389a5592bcb70eac5d39260John McCall // For member data pointers, this is just a matter of adding the 4460bab0cdab751248ca389a5592bcb70eac5d39260John McCall // offset if the source is non-null. 4474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (destTy->isMemberDataPointer()) { 4484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // null maps to null. 4494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (src->isAllOnesValue()) return src; 4500bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4524d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getNSWSub(src, adj); 4534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 4544d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getNSWAdd(src, adj); 4550bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 4560bab0cdab751248ca389a5592bcb70eac5d39260John McCall 457d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // The this-adjustment is left-shifted by 1 on ARM. 458d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (IsARM) { 4594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); 4604d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall offset <<= 1; 4614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall adj = llvm::ConstantInt::get(adj->getType(), offset); 462d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 463d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 4644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); 4654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm::Constant *dstAdj; 4664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isDerivedToBase) 4674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); 468d608cdb7c044365cf4e8764ade1e11e99c176078John McCall else 4694d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); 470d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 4714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); 4723023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall} 473cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 474cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant * 4750bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { 4760bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Itanium C++ ABI 2.3: 4770bab0cdab751248ca389a5592bcb70eac5d39260John McCall // A NULL pointer is represented as -1. 4780bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 47992e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); 480d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 48192e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); 482d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Constant *Values[2] = { Zero, Zero }; 483c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner return llvm::ConstantStruct::getAnon(Values); 484cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall} 485cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall 4865808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallllvm::Constant * 4875808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, 4885808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall CharUnits offset) { 4890bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Itanium C++ ABI 2.3: 4900bab0cdab751248ca389a5592bcb70eac5d39260John McCall // A pointer to data member is an offset from the base address of 4910bab0cdab751248ca389a5592bcb70eac5d39260John McCall // the class object containing it, represented as a ptrdiff_t 49292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); 4930bab0cdab751248ca389a5592bcb70eac5d39260John McCall} 4940bab0cdab751248ca389a5592bcb70eac5d39260John McCall 495755d8497e39071aa24acc173ff07083e3256b8f8John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { 4962d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return BuildMemberPointer(MD, CharUnits::Zero()); 4972d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 4982d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 4992d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, 5002d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits ThisAdjustment) { 501d608cdb7c044365cf4e8764ade1e11e99c176078John McCall assert(MD->isInstance() && "Member function must not be static!"); 502d608cdb7c044365cf4e8764ade1e11e99c176078John McCall MD = MD->getCanonicalDecl(); 503875ab10245d3bf37252dd822aa1616bb0a391095John McCall 504d608cdb7c044365cf4e8764ade1e11e99c176078John McCall CodeGenTypes &Types = CGM.getTypes(); 505875ab10245d3bf37252dd822aa1616bb0a391095John McCall 506d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Get the function pointer (or index if this is a virtual function). 507d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Constant *MemPtr[2]; 508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (MD->isVirtual()) { 5091d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD); 510875ab10245d3bf37252dd822aa1616bb0a391095John McCall 5111246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck const ASTContext &Context = getContext(); 5121246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck CharUnits PointerWidth = 513bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 5141246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); 515d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 516d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (IsARM) { 517d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // ARM C++ ABI 3.2.1: 518d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // This ABI specifies that adj contains twice the this 519d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // adjustment, plus 1 if the member function is virtual. The 520d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // least significant bit of adj then makes exactly the same 521d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // discrimination as the least significant bit of ptr does for 522d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Itanium. 52392e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); 52492e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 5252d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 2 * ThisAdjustment.getQuantity() + 1); 526d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 527d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Itanium C++ ABI 2.3: 528d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // For a virtual function, [the pointer field] is 1 plus the 529d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // virtual table offset (in bytes) of the function, 530d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // represented as a ptrdiff_t. 53192e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); 53292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, 5332d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment.getQuantity()); 534d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 535d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 536755d8497e39071aa24acc173ff07083e3256b8f8John McCall const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); 5372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty; 538755d8497e39071aa24acc173ff07083e3256b8f8John McCall // Check whether the function has a computable LLVM signature. 539f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (Types.isFuncTypeConvertible(FPT)) { 540755d8497e39071aa24acc173ff07083e3256b8f8John McCall // The function has a computable LLVM signature; use the correct type. 541de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); 542d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } else { 543755d8497e39071aa24acc173ff07083e3256b8f8John McCall // Use an arbitrary non-function type to tell GetAddrOfFunction that the 544755d8497e39071aa24acc173ff07083e3256b8f8John McCall // function type is incomplete. 54592e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner Ty = CGM.PtrDiffTy; 546d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 547755d8497e39071aa24acc173ff07083e3256b8f8John McCall llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); 548d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 54992e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); 55092e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, (IsARM ? 2 : 1) * 5512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith ThisAdjustment.getQuantity()); 552d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 553d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 554c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner return llvm::ConstantStruct::getAnon(MemPtr); 555875ab10245d3bf37252dd822aa1616bb0a391095John McCall} 556875ab10245d3bf37252dd822aa1616bb0a391095John McCall 5572d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, 5582d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith QualType MPType) { 5592d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); 5602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith const ValueDecl *MPD = MP.getMemberPointerDecl(); 5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (!MPD) 5622d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return EmitNullMemberPointer(MPT); 5632d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 564f632730ffb0b8ca531a35e737b29cc9f9774ca1dReid Kleckner CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); 5652d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5662d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) 5672d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return BuildMemberPointer(MD, ThisAdjustment); 5682d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5692d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith CharUnits FieldOffset = 5702d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); 5712d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); 5722d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 5732d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 574e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are 575e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null. 576e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// 577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated. 578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value * 5790bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, 5800bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *L, 5810bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *R, 5820bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT, 5830bab0cdab751248ca389a5592bcb70eac5d39260John McCall bool Inequality) { 584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall CGBuilderTy &Builder = CGF.Builder; 585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 586e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::ICmpInst::Predicate Eq; 587e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Instruction::BinaryOps And, Or; 588e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (Inequality) { 589e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Eq = llvm::ICmpInst::ICMP_NE; 590e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall And = llvm::Instruction::Or; 591e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Or = llvm::Instruction::And; 592e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } else { 593e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Eq = llvm::ICmpInst::ICMP_EQ; 594e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall And = llvm::Instruction::And; 595e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Or = llvm::Instruction::Or; 596e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 597e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 5980bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Member data pointers are easy because there's a unique null 5990bab0cdab751248ca389a5592bcb70eac5d39260John McCall // value, so it just comes down to bitwise equality. 6000bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) 6010bab0cdab751248ca389a5592bcb70eac5d39260John McCall return Builder.CreateICmp(Eq, L, R); 6020bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall // For member function pointers, the tautologies are more complex. 6040bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The Itanium tautology is: 605de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) 6060bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The ARM tautology is: 607de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L == R) <==> (L.ptr == R.ptr && 608de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L.adj == R.adj || 609de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) 6100bab0cdab751248ca389a5592bcb70eac5d39260John McCall // The inequality tautologies have exactly the same structure, except 6110bab0cdab751248ca389a5592bcb70eac5d39260John McCall // applying De Morgan's laws. 6120bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6130bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); 6140bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); 6150bab0cdab751248ca389a5592bcb70eac5d39260John McCall 616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition tests whether L.ptr == R.ptr. This must always be 617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // true for equality to hold. 618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); 619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition, together with the assumption that L.ptr == R.ptr, 621e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // tests whether the pointers are both null. ARM imposes an extra 622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // condition. 623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); 624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); 625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This condition tests whether L.adj == R.adj. If this isn't 627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // true, the pointers are unequal unless they're both null. 628d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); 629d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); 630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); 631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Null member function pointers on ARM clear the low bit of Adj, 633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // so the zero condition has to check that neither low bit is set. 634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (IsARM) { 635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); 636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Compute (l.adj | r.adj) & 1 and test it against zero. 638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); 639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); 640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, 641e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall "cmp.or.adj"); 642e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); 643e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 645e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Tie together all our conditions. 646e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); 647e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Result = Builder.CreateBinOp(And, PtrEq, Result, 648e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall Inequality ? "memptr.ne" : "memptr.eq"); 649e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return Result; 650e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall} 651e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 652e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value * 6530bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, 6540bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *MemPtr, 6550bab0cdab751248ca389a5592bcb70eac5d39260John McCall const MemberPointerType *MPT) { 656e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall CGBuilderTy &Builder = CGF.Builder; 6570bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6580bab0cdab751248ca389a5592bcb70eac5d39260John McCall /// For member data pointers, this is just a check against -1. 6590bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (MPT->isMemberDataPointer()) { 66092e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner assert(MemPtr->getType() == CGM.PtrDiffTy); 6610bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Value *NegativeOne = 6620bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm::Constant::getAllOnesValue(MemPtr->getType()); 6630bab0cdab751248ca389a5592bcb70eac5d39260John McCall return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); 6640bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 666db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // In Itanium, a member function pointer is not null if 'ptr' is not null. 667d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); 668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); 670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); 671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 672db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // On ARM, a member function pointer is also non-null if the low bit of 'adj' 673db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // (the virtual bit) is set. 674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (IsARM) { 675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); 676d608cdb7c044365cf4e8764ade1e11e99c176078John McCall llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); 677e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); 678db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, 679db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar "memptr.isvirtual"); 680db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar Result = Builder.CreateOr(Result, IsVirtual); 681e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 682e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 683e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return Result; 684e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall} 685875ab10245d3bf37252dd822aa1616bb0a391095John McCall 686f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data 687f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset. 688f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { 689f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return MPT->getPointeeType()->isFunctionType(); 690cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall} 6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 692ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// The Itanium ABI always places an offset to the complete object 693ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// at entry -2 in the vtable. 694ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCallllvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, 695ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *ptr, 696ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall QualType type) { 697ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Grab the vtable pointer as an intptr_t*. 698ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo()); 699ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 700ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Track back to entry -2 and pull out the offset there. 701ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::Value *offsetPtr = 702ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr"); 703ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr); 704ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall offset->setAlignment(CGF.PointerAlignInBytes); 705ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 706ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Apply the offset. 707ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); 708ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return CGF.Builder.CreateInBoundsGEP(ptr, offset); 709ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall} 710ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 711b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Klecknerllvm::Value * 712b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid KlecknerItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, 713b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *This, 714b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner const CXXRecordDecl *ClassDecl, 715b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner const CXXRecordDecl *BaseClassDecl) { 716b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy); 717b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner CharUnits VBaseOffsetOffset = 718b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner CGM.getVTableContext().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); 719b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner 720b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *VBaseOffsetPtr = 721b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), 722b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner "vbase.offset.ptr"); 723b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, 724b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner CGM.PtrDiffTy->getPointerTo()); 725b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner 726b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner llvm::Value *VBaseOffset = 727b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 728b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner 729b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner return VBaseOffset; 730b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner} 731b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner 7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a 7334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject. 7344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, 7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXCtorType Type, 7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7389cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7403b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin // 'this' parameter is already there, as well as 'this' return if 7413b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin // HasThisReturn(GlobalDecl(Ctor, Type)) is true 7424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need to add a VTT parameter (which has type void **). 7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) 7454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); 7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a 7494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject. 7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, 7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CXXDtorType Type, 7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType &ResTy, 7535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<CanQualType> &ArgTys) { 7549cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7563b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin // 'this' parameter is already there, as well as 'this' return if 7573b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin // HasThisReturn(GlobalDecl(Dtor, Type)) is true 7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need to add a VTT parameter (which has type void **). 7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) 7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); 7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, 7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType &ResTy, 7664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall FunctionArgList &Params) { 7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Create the 'this' variable. 7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall BuildThisParam(CGF, Params); 7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); 7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall assert(MD->isInstance()); 7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7734c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Check if we need a VTT parameter as well. 774e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne if (NeedsVTTParameter(CGF.CurGD)) { 7759cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Context = getContext(); 7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // FIXME: avoid the fake decl 7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall QualType T = Context.getPointerType(Context.VoidPtrTy); 7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ImplicitParamDecl *VTTDecl 7804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), 7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall &Context.Idents.get("vtt"), T); 782d26bc76c98006609002d9930f8840490e88ac5b5John McCall Params.push_back(VTTDecl); 7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall getVTTDecl(CGF) = VTTDecl; 7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { 7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Initialize the 'this' slot. 7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall EmitThisParam(CGF); 7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall /// Initialize the 'vtt' slot if needed. 7924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (getVTTDecl(CGF)) { 7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall getVTTValue(CGF) 7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)), 7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall "vtt"); 7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7983b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// If this is a function that the ABI specifies returns 'this', initialize 7993b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// the return slot to 'this' at the start of the function. 8003b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// 8013b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// Unlike the setting of return types, this is done within the ABI 8023b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// implementation instead of by clients of CGCXXABI because: 8033b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// 1) getThisValue is currently protected 8043b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// 2) in theory, an ABI could implement 'this' returns some other way; 8053b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /// HasThisReturn only specifies a contract, not the implementation 8064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (HasThisReturn(CGF.CurGD)) 807cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); 8084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 8094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8103b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Linvoid ItaniumCXXABI::EmitConstructorCall(CodeGenFunction &CGF, 8111d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov const CXXConstructorDecl *D, 8123b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin CXXCtorType Type, 8133b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin bool ForVirtualBase, bool Delegating, 8141d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov llvm::Value *This, 8151d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov CallExpr::const_arg_iterator ArgBeg, 8161d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov CallExpr::const_arg_iterator ArgEnd) { 8171d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov llvm::Value *VTT = CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, 8181d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov Delegating); 8191d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); 8201d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); 8211d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov 8221d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov // FIXME: Provide a source location here. 8233b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), 8243b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin This, VTT, VTTTy, ArgBeg, ArgEnd); 8251d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov} 8261d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov 8273b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Linvoid ItaniumCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF, 8283b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin const CXXDestructorDecl *Dtor, 8293b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin CXXDtorType DtorType, 8303b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin SourceLocation CallLoc, 8313b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin llvm::Value *This) { 8320f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); 8330f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov 8340f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov const CGFunctionInfo *FInfo 8350f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov = &CGM.getTypes().arrangeCXXDestructor(Dtor, DtorType); 8360f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); 8370f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov llvm::Value *Callee = CGF.BuildVirtualCall(Dtor, DtorType, This, Ty); 8380f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov 8393b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This, 8403b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin /*ImplicitParam=*/0, QualType(), 0, 0); 8410f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov} 8420f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov 8439063302a82423cb83f002257a416741850739a70Reid Klecknervoid ItaniumCXXABI::EmitVirtualInheritanceTables( 8449063302a82423cb83f002257a416741850739a70Reid Kleckner llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD) { 8459063302a82423cb83f002257a416741850739a70Reid Kleckner CodeGenVTables &VTables = CGM.getVTables(); 8469063302a82423cb83f002257a416741850739a70Reid Kleckner llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); 8479063302a82423cb83f002257a416741850739a70Reid Kleckner VTables.EmitVTTDefinition(VTT, Linkage, RD); 8489063302a82423cb83f002257a416741850739a70Reid Kleckner} 8499063302a82423cb83f002257a416741850739a70Reid Kleckner 8504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, 8514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RValue RV, QualType ResultType) { 8524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) 8534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); 8544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Destructor thunks in the ARM ABI have indeterminate results. 8562acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *T = 8574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); 8584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RValue Undef = RValue::get(llvm::UndefValue::get(T)); 8594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); 8604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 8611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/ 8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 864e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { 865e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The array cookie is a size_t; pad that up to the element alignment. 866e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The cookie is actually right-justified in that space. 867e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), 868e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGM.getContext().getTypeAlignInChars(elementType)); 8691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 8701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 8721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NewPtr, 8731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElements, 8746ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 8751e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType ElementType) { 876e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(requiresArrayCookie(expr)); 8771e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 878956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = NewPtr->getType()->getPointerAddressSpace(); 8791e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8809cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall ASTContext &Ctx = getContext(); 8811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall QualType SizeTy = Ctx.getSizeType(); 8821e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); 8831e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8841e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The size of the cookie. 8851e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits CookieSize = 8861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); 887e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(CookieSize == getArrayCookieSizeImpl(ElementType)); 8881e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8891e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Compute an offset to the cookie. 8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *CookiePtr = NewPtr; 8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CharUnits CookieOffset = CookieSize - SizeSize; 8921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (!CookieOffset.isZero()) 8931e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, 8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookieOffset.getQuantity()); 8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Write the number of elements into the appropriate slot. 8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::Value *NumElementsPtr 8981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall = CGF.Builder.CreateBitCast(CookiePtr, 8991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CGF.ConvertType(SizeTy)->getPointerTo(AS)); 9001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CGF.Builder.CreateStore(NumElements, NumElementsPtr); 9011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9021e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Finally, compute a pointer to the actual data buffer by skipping 9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // over the cookie completely. 9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, 9051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall CookieSize.getQuantity()); 9061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 908e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 909e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 910e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize) { 911e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The element size is right-justified in the cookie. 912e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *numElementsPtr = allocPtr; 913e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits numElementsOffset = 914e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); 915e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (!numElementsOffset.isZero()) 916e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 917e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, 918e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsOffset.getQuantity()); 9191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 920956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 921e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 922e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 923e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return CGF.Builder.CreateLoad(numElementsPtr); 9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 926e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { 927f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // ARM says that the cookie is always: 9281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // struct array_cookie { 9291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // std::size_t element_size; // element_size != 0 9301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // std::size_t element_count; 9311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // }; 932f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // But the base ABI doesn't give anything an alignment greater than 933f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // 8, so we can dismiss this as typical ABI-author blindness to 934f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // actual language complexity and round up to the element alignment. 935f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), 936f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGM.getContext().getTypeAlignInChars(elementType)); 9371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, 940f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *newPtr, 941f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *numElements, 9426ec278d1a354517e20f13a877481453ee7940c78John McCall const CXXNewExpr *expr, 943f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall QualType elementType) { 944e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall assert(requiresArrayCookie(expr)); 9451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 946f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // NewPtr is a char*, but we generalize to arbitrary addrspaces. 947f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall unsigned AS = newPtr->getType()->getPointerAddressSpace(); 9481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The cookie is always at the start of the buffer. 950f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *cookie = newPtr; 9511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The first element is the element size. 953f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); 954f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, 955f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall getContext().getTypeSizeInChars(elementType).getQuantity()); 956f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGF.Builder.CreateStore(elementSize, cookie); 9571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // The second element is the element count. 959f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); 960f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CGF.Builder.CreateStore(numElements, cookie); 9611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Finally, compute a pointer to the actual data buffer by skipping 9631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // over the cookie completely. 964f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); 965f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, 966f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall cookieSize.getQuantity()); 9671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 969e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, 970e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *allocPtr, 971e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CharUnits cookieSize) { 972e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // The number of elements is at offset sizeof(size_t) relative to 973e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // the allocated pointer. 974e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::Value *numElementsPtr 975e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); 9761e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 977956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow unsigned AS = allocPtr->getType()->getPointerAddressSpace(); 978e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall numElementsPtr = 979e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); 980e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return CGF.Builder.CreateLoad(numElementsPtr); 9811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall} 9821e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/*********************** Static local initialization **************************/ 9845cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9855cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, 9869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 9875cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // int __cxa_guard_acquire(__guard *guard_object); 9882acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 9895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), 990da549e8995c447542d5631b8b67fcc3a9582797aJay Foad GuardPtrTy, /*isVarArg=*/false); 991e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", 992c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::get(CGM.getLLVMContext(), 993c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::FunctionIndex, 99472390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 9955cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 9965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 9975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, 9989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 9995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // void __cxa_guard_release(__guard *guard_object); 10002acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 10018b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1002e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", 1003c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::get(CGM.getLLVMContext(), 1004c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::FunctionIndex, 100572390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 10065cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 10075cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10085cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, 10099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::PointerType *GuardPtrTy) { 10105cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // void __cxa_guard_abort(__guard *guard_object); 10112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *FTy = 10128b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); 1013e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", 1014c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::get(CGM.getLLVMContext(), 1015c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling llvm::AttributeSet::FunctionIndex, 101672390b39c545426023ec104afe8706395d732badBill Wendling llvm::Attribute::NoUnwind)); 10175cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 10185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10195cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallnamespace { 10205cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall struct CallGuardAbort : EHScopeStack::Cleanup { 10215cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::GlobalVariable *Guard; 10220f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} 10235cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 1024ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 1025bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), 1026bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Guard); 10275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 10285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall }; 10295cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 10305cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10315cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// The ARM code here follows the Itanium code closely enough that we 10325cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// just special-case it at particular places. 10333030eb82593097502469a8b3fc26112c79c75605John McCallvoid ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, 10343030eb82593097502469a8b3fc26112c79c75605John McCall const VarDecl &D, 1035355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::GlobalVariable *var, 1036355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall bool shouldPerformInit) { 10375cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGBuilderTy &Builder = CGF.Builder; 10383030eb82593097502469a8b3fc26112c79c75605John McCall 103904e517650569598e847c2ab609672e6df93effe5Richard Smith // We only need to use thread-safe statics for local non-TLS variables; 10403030eb82593097502469a8b3fc26112c79c75605John McCall // global initialization is always single-threaded. 104104e517650569598e847c2ab609672e6df93effe5Richard Smith bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && 104204e517650569598e847c2ab609672e6df93effe5Richard Smith D.isLocalVarDecl() && !D.getTLSKind(); 1043173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson 1044173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson // If we have a global variable with internal linkage and thread-safe statics 1045173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson // are disabled, we can just let the guard variable be of type i8. 1046355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); 1047355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1048355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::IntegerType *guardTy; 10490502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (useInt8GuardVariable) { 1050355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guardTy = CGF.Int8Ty; 10510502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall } else { 1052c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover // Guard variables are 64 bits in the generic ABI and size width on ARM 1053c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover // (i.e. 32-bit on AArch32, 64-bit on AArch64). 1054c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover guardTy = (IsARM ? CGF.SizeTy : CGF.Int64Ty); 1055355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall } 1056355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); 1057355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1058355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Create the guard variable if we don't already have it (as we 1059355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // might if we're double-emitting this function body). 1060355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); 1061355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall if (!guard) { 1062355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Mangle the name for the guard. 1063355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall SmallString<256> guardName; 1064355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall { 1065355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::raw_svector_ostream out(guardName); 1066355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall getMangleContext().mangleItaniumGuardVariable(&D, out); 1067355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall out.flush(); 1068355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall } 1069355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1070355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Create the guard variable with a zero-initializer. 1071355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall // Just absorb linkage and visibility from the guarded variable. 1072355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, 1073355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall false, var->getLinkage(), 1074355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::ConstantInt::get(guardTy, 0), 1075355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guardName.str()); 1076355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall guard->setVisibility(var->getVisibility()); 107704e517650569598e847c2ab609672e6df93effe5Richard Smith // If the variable is thread-local, so is its guard variable. 107804e517650569598e847c2ab609672e6df93effe5Richard Smith guard->setThreadLocalMode(var->getThreadLocalMode()); 1079355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall 1080355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGM.setStaticLocalDeclGuardAddress(&D, guard); 1081173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson } 10825cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Test whether the variable has completed initialization. 1084355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::Value *isInitialized; 10855cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 10865cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ARM C++ ABI 3.2.3.1: 10875cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // To support the potential use of initialization guard variables 10885cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // as semaphores that are the target of ARM SWP and LDREX/STREX 10895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // synchronizing instructions we define a static initialization 10905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // guard variable to be a 4-byte aligned, 4- byte word with the 10915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // following inline access protocol. 10925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // #define INITIALIZED 1 10935cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if ((obj_guard & INITIALIZED) != INITIALIZED) { 10945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if (__cxa_guard_acquire(&obj_guard)) 10955cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... 10965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 10970502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (IsARM && !useInt8GuardVariable) { 1098355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall llvm::Value *V = Builder.CreateLoad(guard); 1099c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover llvm::Value *Test1 = llvm::ConstantInt::get(guardTy, 1); 1100c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover V = Builder.CreateAnd(V, Test1); 1101355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); 11025cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11035cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Itanium C++ ABI 3.3.2: 11045cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // The following is pseudo-code showing how these functions can be used: 11055cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if (obj_guard.first_byte == 0) { 11065cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // if ( __cxa_guard_acquire (&obj_guard) ) { 11075cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // try { 11085cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... initialize the object ...; 11095cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } catch (...) { 11105cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // __cxa_guard_abort (&obj_guard); 11115cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // throw; 11125cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 11135cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // ... queue object destructor with __cxa_atexit() ...; 11145cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // __cxa_guard_release (&obj_guard); 11155cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 11165cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // } 11175cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } else { 11185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Load the first byte of the guard variable. 11190f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth llvm::LoadInst *LI = 1120355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); 11210f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth LI->setAlignment(1); 1122eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman 1123eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // Itanium ABI: 1124eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // An implementation supporting thread-safety on multiprocessor 1125eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // systems must also guarantee that references to the initialized 1126eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // object do not occur before the load of the initialization flag. 1127eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // 1128eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman // In LLVM, we do this by marking the load Acquire. 1129eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman if (threadsafe) 11300f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth LI->setAtomic(llvm::Acquire); 1131eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman 1132355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized"); 11335cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 11345cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11355cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); 11365cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); 11375cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11385cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Check if the first byte of the guard variable is zero. 1139355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); 11405cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11415cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(InitCheckBlock); 11425cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11435cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Variables used when coping with thread-safe statics and exceptions. 11440502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (threadsafe) { 11455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_acquire. 11465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::Value *V 1147bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); 11485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); 11505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), 11525cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall InitBlock, EndBlock); 11535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11545cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_abort along the exceptional edge. 1155355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); 11565cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11575cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(InitBlock); 11585cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 11595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11605cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Emit the initializer and add a global destructor if appropriate. 1161355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); 11625cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11630502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall if (threadsafe) { 11645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Pop the guard-abort cleanup if we pushed one. 11655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.PopCleanupBlock(); 11665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall // Call __cxa_guard_release. This cannot throw. 1168bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard); 11695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } else { 1170355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); 11715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall } 11725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall 11735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall CGF.EmitBlock(EndBlock); 11745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall} 117520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 117620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor using __cxa_atexit. 117720bb175cb8ae5844034828db094fb948c0e3454aJohn McCallstatic void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, 117820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *dtor, 117904e517650569598e847c2ab609672e6df93effe5Richard Smith llvm::Constant *addr, 118004e517650569598e847c2ab609672e6df93effe5Richard Smith bool TLS) { 11814e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling const char *Name = "__cxa_atexit"; 11824e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling if (TLS) { 11834e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling const llvm::Triple &T = CGF.getTarget().getTriple(); 11844e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit"; 11854e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling } 118604e517650569598e847c2ab609672e6df93effe5Richard Smith 118720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // We're assuming that the destructor function is something we can 118820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // reasonably call with the default CC. Go ahead and cast it to the 118920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // right prototype. 119020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Type *dtorTy = 119120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); 119220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 119320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); 119420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; 119520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType *atexitTy = 119620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::FunctionType::get(CGF.IntTy, paramTys, false); 119720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 119820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Fetch the actual function. 119904e517650569598e847c2ab609672e6df93effe5Richard Smith llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); 120020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) 120120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall fn->setDoesNotThrow(); 120220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 120320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Create a variable that binds the atexit to this shared object. 120420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *handle = 120520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); 120620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 120720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Value *args[] = { 120820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::ConstantExpr::getBitCast(dtor, dtorTy), 120920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), 121020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall handle 121120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall }; 1212bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(atexit, args); 121320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall} 121420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 121520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor as best as we know how. 121620bb175cb8ae5844034828db094fb948c0e3454aJohn McCallvoid ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, 121704e517650569598e847c2ab609672e6df93effe5Richard Smith const VarDecl &D, 121820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *dtor, 121920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall llvm::Constant *addr) { 122020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Use __cxa_atexit if available. 122104e517650569598e847c2ab609672e6df93effe5Richard Smith if (CGM.getCodeGenOpts().CXAAtExit) 122204e517650569598e847c2ab609672e6df93effe5Richard Smith return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); 122304e517650569598e847c2ab609672e6df93effe5Richard Smith 122404e517650569598e847c2ab609672e6df93effe5Richard Smith if (D.getTLSKind()) 122504e517650569598e847c2ab609672e6df93effe5Richard Smith CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); 122620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 122720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // In Apple kexts, we want to add a global destructor entry. 122820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // FIXME: shouldn't this be guarded by some variable? 12297edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith if (CGM.getLangOpts().AppleKext) { 123020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall // Generate a global destructor entry. 123120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall return CGM.AddCXXDtorEntry(dtor, addr); 123220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall } 123320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 123420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall CGF.registerGlobalDtorWithAtExit(dtor, addr); 123520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall} 1236b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1237b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// Get the appropriate linkage for the wrapper function. This is essentially 1238b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// the weak form of the variable's linkage; every translation unit which wneeds 1239b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// the wrapper emits a copy, and we want the linker to merge them. 1240b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithstatic llvm::GlobalValue::LinkageTypes getThreadLocalWrapperLinkage( 1241b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::GlobalValue::LinkageTypes VarLinkage) { 1242b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (llvm::GlobalValue::isLinkerPrivateLinkage(VarLinkage)) 1243b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return llvm::GlobalValue::LinkerPrivateWeakLinkage; 1244b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // For internal linkage variables, we don't need an external or weak wrapper. 1245b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) 1246b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return VarLinkage; 1247b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return llvm::GlobalValue::WeakODRLinkage; 1248b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith} 1249b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1250b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithllvm::Function * 1251b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard SmithItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, 1252b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::GlobalVariable *Var) { 1253b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // Mangle the name for the thread_local wrapper function. 1254b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith SmallString<256> WrapperName; 1255b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith { 1256b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::raw_svector_ostream Out(WrapperName); 1257b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); 1258b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Out.flush(); 1259b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1260b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1261b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (llvm::Value *V = Var->getParent()->getNamedValue(WrapperName)) 1262b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return cast<llvm::Function>(V); 1263b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1264b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Type *RetTy = Var->getType(); 1265b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (VD->getType()->isReferenceType()) 1266b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith RetTy = RetTy->getPointerElementType(); 1267b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1268b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); 1269b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *Wrapper = llvm::Function::Create( 1270b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith FnTy, getThreadLocalWrapperLinkage(Var->getLinkage()), WrapperName.str(), 1271b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith &CGM.getModule()); 1272b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // Always resolve references to the wrapper at link time. 1273b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); 1274b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return Wrapper; 1275b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith} 1276b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1277b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithvoid ItaniumCXXABI::EmitThreadLocalInitFuncs( 1278b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, 1279b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *InitFunc) { 1280b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith for (unsigned I = 0, N = Decls.size(); I != N; ++I) { 1281b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith const VarDecl *VD = Decls[I].first; 1282b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::GlobalVariable *Var = Decls[I].second; 1283b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1284b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // Mangle the name for the thread_local initialization function. 1285b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith SmallString<256> InitFnName; 1286b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith { 1287b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::raw_svector_ostream Out(InitFnName); 1288b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith getMangleContext().mangleItaniumThreadLocalInit(VD, Out); 1289b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Out.flush(); 1290b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1291b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1292b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // If we have a definition for the variable, emit the initialization 1293b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // function as an alias to the global Init function (if any). Otherwise, 1294b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // produce a declaration of the initialization function. 1295b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::GlobalValue *Init = 0; 1296b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith bool InitIsInitFunc = false; 1297b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (VD->hasDefinition()) { 1298b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith InitIsInitFunc = true; 1299b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (InitFunc) 1300b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Init = 1301b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith new llvm::GlobalAlias(InitFunc->getType(), Var->getLinkage(), 1302b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith InitFnName.str(), InitFunc, &CGM.getModule()); 1303b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } else { 1304b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // Emit a weak global function referring to the initialization function. 1305b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // This function will not exist if the TU defining the thread_local 1306b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // variable in question does not need any dynamic initialization for 1307b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // its thread_local variables. 1308b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); 1309b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Init = llvm::Function::Create( 1310b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), 1311b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith &CGM.getModule()); 1312b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1313b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1314b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (Init) 1315b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Init->setVisibility(Var->getVisibility()); 1316b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1317b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); 1318b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::LLVMContext &Context = CGM.getModule().getContext(); 1319b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); 1320b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith CGBuilderTy Builder(Entry); 1321b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (InitIsInitFunc) { 1322b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (Init) 1323b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.CreateCall(Init); 1324b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } else { 1325b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // Don't know whether we have an init function. Call it if it exists. 1326b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Value *Have = Builder.CreateIsNotNull(Init); 1327b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 1328b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); 1329b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.CreateCondBr(Have, InitBB, ExitBB); 1330b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1331b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.SetInsertPoint(InitBB); 1332b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.CreateCall(Init); 1333b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.CreateBr(ExitBB); 1334b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1335b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.SetInsertPoint(ExitBB); 1336b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1337b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1338b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // For a reference, the result of the wrapper function is a pointer to 1339b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // the referenced object. 1340b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Value *Val = Var; 1341b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (VD->getType()->isReferenceType()) { 1342b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::LoadInst *LI = Builder.CreateLoad(Val); 1343b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity()); 1344b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Val = LI; 1345b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1346b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1347b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Builder.CreateRet(Val); 1348b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith } 1349b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith} 1350b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1351b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard SmithLValue ItaniumCXXABI::EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, 1352b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith const DeclRefExpr *DRE) { 1353b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith const VarDecl *VD = cast<VarDecl>(DRE->getDecl()); 1354b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith QualType T = VD->getType(); 1355b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); 1356b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); 1357b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith llvm::Function *Wrapper = 1358b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith getOrCreateThreadLocalWrapper(VD, cast<llvm::GlobalVariable>(Val)); 1359b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1360b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith Val = CGF.Builder.CreateCall(Wrapper); 1361b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith 1362b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith LValue LV; 1363b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith if (VD->getType()->isReferenceType()) 1364b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith LV = CGF.MakeNaturalAlignAddrLValue(Val, T); 1365b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith else 1366b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith LV = CGF.MakeAddrLValue(Val, DRE->getType(), 1367b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith CGF.getContext().getDeclAlign(VD)); 1368b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith // FIXME: need setObjCGCLValueClass? 1369b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith return LV; 1370b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith} 1371e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 1372e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne/// Return whether the given global decl needs a VTT parameter, which it does 1373e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne/// if it's a base constructor or destructor with virtual bases. 1374e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbournebool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { 1375e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 1376e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 1377e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne // We don't have any virtual bases, just return early. 1378e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne if (!MD->getParent()->getNumVBases()) 1379e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne return false; 1380e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 1381e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne // Check if we have a base constructor. 1382e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) 1383e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne return true; 1384e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 1385e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne // Check if we have a base destructor. 1386e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) 1387e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne return true; 1388e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne 1389e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne return false; 1390e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne} 1391