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:
3821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  bool UseARMMethodPtrABI;
3921fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  bool UseARMGuardVarABI;
400bab0cdab751248ca389a5592bcb70eac5d39260John McCall
413a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davispublic:
4221fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  ItaniumCXXABI(CodeGen::CodeGenModule &CGM,
4321fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                bool UseARMMethodPtrABI = false,
4421fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                bool UseARMGuardVarABI = false) :
4521fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI),
4621fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    UseARMGuardVarABI(UseARMGuardVarABI) { }
4793d557bc1867b7d7b102f87290194b4be7932c92John McCall
48ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  bool isReturnTypeIndirect(const CXXRecordDecl *RD) const {
49ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    // Structures with either a non-trivial destructor or a non-trivial
50ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    // copy constructor are always indirect.
51ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
52ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  }
53ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
54ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const {
55ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    // Structures with either a non-trivial destructor or a non-trivial
56ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    // copy constructor are always indirect.
57ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    if (!RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor())
58ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov      return RAA_Indirect;
59ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov    return RAA_Default;
60ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov  }
61ed23bdf69dd63e4fd01c02b12a13d1e6cbff9c2fTimur Iskhodzhanov
62f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  bool isZeroInitializable(const MemberPointerType *MPT);
63cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
650bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6693d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
6793d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
6893d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
6993d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT);
703023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
716c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
726c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *Base,
736c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *MemPtr,
746c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            const MemberPointerType *MPT);
756c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
760bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
770bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
780bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *Src);
794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                              llvm::Constant *Src);
81cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
820bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
83cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
84755d8497e39071aa24acc173ff07083e3256b8f8John McCall  llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
855808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
865808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall                                        CharUnits offset);
872d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
882d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
892d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                     CharUnits ThisAdjustment);
90875ab10245d3bf37252dd822aa1616bb0a391095John McCall
910bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
920bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
930bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
940bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
950bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality);
96e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
970bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
980bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *Addr,
990bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT);
1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
101ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
102ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                      llvm::Value *ptr,
103ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                      QualType type);
104ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
105b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF,
106b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         llvm::Value *This,
107b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         const CXXRecordDecl *ClassDecl,
108b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         const CXXRecordDecl *BaseClassDecl);
109b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner
1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<CanQualType> &ArgTys);
1144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
115bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  void EmitCXXConstructors(const CXXConstructorDecl *D);
116bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov
1174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys);
1214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
122a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor,
123a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner                              CXXDtorType DT) const {
124a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner    // Itanium does not emit any destructor variant as an inline thunk.
125a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner    // Delegating may occur as an optimization, but all variants are either
126a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner    // emitted with external linkage or as linkonce if they are inline and used.
127a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner    return false;
128a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  }
129a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner
130a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  void EmitCXXDestructors(const CXXDestructorDecl *D);
131a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner
1324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1383b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  void EmitConstructorCall(CodeGenFunction &CGF,
1393b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                           const CXXConstructorDecl *D, CXXCtorType Type,
1403b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                           bool ForVirtualBase, bool Delegating,
1414444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin                           llvm::Value *This,
1424444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin                           CallExpr::const_arg_iterator ArgBeg,
1434444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin                           CallExpr::const_arg_iterator ArgEnd);
1444444dbbb96ba55ff8a05a1918627f609a387db9fStephen Lin
1453b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  void EmitVirtualDestructorCall(CodeGenFunction &CGF,
1463b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                 const CXXDestructorDecl *Dtor,
1473b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                 CXXDtorType DtorType, SourceLocation CallLoc,
1483b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                 llvm::Value *This);
1490f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov
1509063302a82423cb83f002257a416741850739a70Reid Kleckner  void EmitVirtualInheritanceTables(llvm::GlobalVariable::LinkageTypes Linkage,
1519063302a82423cb83f002257a416741850739a70Reid Kleckner                                    const CXXRecordDecl *RD);
1529063302a82423cb83f002257a416741850739a70Reid Kleckner
153285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos  StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
1542eb9a959d24ad757a82ecab61f343635ad67749aDavid Blaikie  StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
155285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos
156e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1606ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
1611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
162e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
163e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   llvm::Value *allocPtr,
164e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
1655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
1663030eb82593097502469a8b3fc26112c79c75605John McCall  void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
1670f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth                       llvm::GlobalVariable *DeclPtr, bool PerformInit);
16804e517650569598e847c2ab609672e6df93effe5Richard Smith  void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
16904e517650569598e847c2ab609672e6df93effe5Richard Smith                          llvm::Constant *dtor, llvm::Constant *addr);
170b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
171b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
172b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                                                llvm::GlobalVariable *Var);
173b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  void EmitThreadLocalInitFuncs(
174b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
175b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::Function *InitFunc);
176b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
177b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                                    const DeclRefExpr *DRE);
178e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
179e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  bool NeedsVTTParameter(GlobalDecl GD);
1803a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis};
181ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
182ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI {
183ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic:
18421fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  ARMCXXABI(CodeGen::CodeGenModule &CGM) :
18521fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
18621fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                  /* UseARMGuardVarABI = */ true) {}
1874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1883b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  bool HasThisReturn(GlobalDecl GD) const {
1893b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin    return (isa<CXXConstructorDecl>(GD.getDecl()) || (
1903b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin              isa<CXXDestructorDecl>(GD.getDecl()) &&
1913b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin              GD.getDtorType() != Dtor_Deleting));
1923b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  }
1934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
1954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
196e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
2006ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
2011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
202e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
203e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
204ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall};
2053a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
2063a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
207071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
20864aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall  switch (CGM.getTarget().getCXXABI().getKind()) {
20996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  // For IR-generation purposes, there's no significant difference
21096fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  // between the ARM and iOS ABIs.
21196fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  case TargetCXXABI::GenericARM:
21296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  case TargetCXXABI::iOS:
21396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall    return new ARMCXXABI(CGM);
21496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall
215c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
216c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // include the other 32-bit ARM oddities: constructor/destructor return values
217c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // and array cookies.
218c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetCXXABI::GenericAArch64:
21921fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
22021fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                             /* UseARMGuardVarABI = */ true);
221c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
22296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  case TargetCXXABI::GenericItanium:
22321fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    if (CGM.getContext().getTargetInfo().getTriple().getArch()
22421fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn        == llvm::Triple::le32) {
22521fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn      // For PNaCl, use ARM-style method pointers so that PNaCl code
22621fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn      // does not assume anything about the alignment of function
22721fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn      // pointers.
22821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn      return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true,
22921fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                               /* UseARMGuardVarABI = */ false);
23021fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    }
23196fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall    return new ItaniumCXXABI(CGM);
23296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall
23396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  case TargetCXXABI::Microsoft:
23496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall    llvm_unreachable("Microsoft ABI is not Itanium-based");
23596fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  }
23696fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall  llvm_unreachable("bad ABI kind");
237ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}
238ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
2399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *
2400bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
2410bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
24292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner    return CGM.PtrDiffTy;
24392e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner  return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, NULL);
244875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
245875ab10245d3bf37252dd822aa1616bb0a391095John McCall
246babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form:
247babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
248babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
249babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI:
250babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
251babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj)
252babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr - 1)
253babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
254babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI:
255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.adj & 1) is nonzero
256babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj >> 1)
257babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr)
258babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions
259babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned.
260babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
261babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points
262babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied.
263babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
264babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of
265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call.
26693d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value *
26793d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
26893d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
26993d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
27093d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT) {
27193d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGBuilderTy &Builder = CGF.Builder;
27293d557bc1867b7d7b102f87290194b4be7932c92John McCall
27393d557bc1867b7d7b102f87290194b4be7932c92John McCall  const FunctionProtoType *FPT =
27493d557bc1867b7d7b102f87290194b4be7932c92John McCall    MPT->getPointeeType()->getAs<FunctionProtoType>();
27593d557bc1867b7d7b102f87290194b4be7932c92John McCall  const CXXRecordDecl *RD =
27693d557bc1867b7d7b102f87290194b4be7932c92John McCall    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
27793d557bc1867b7d7b102f87290194b4be7932c92John McCall
2782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
279de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CGM.getTypes().GetFunctionType(
280de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      CGM.getTypes().arrangeCXXMethodType(RD, FPT));
28193d557bc1867b7d7b102f87290194b4be7932c92John McCall
28292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
28393d557bc1867b7d7b102f87290194b4be7932c92John McCall
284babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
285babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
286babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
287babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
288d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Extract memptr.adj, which is in the second field.
289d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
290babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
291babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // Compute the true adjustment.
292babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Adj = RawAdj;
29321fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI)
294babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
29593d557bc1867b7d7b102f87290194b4be7932c92John McCall
29693d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the adjustment and cast back to the original struct type
29793d557bc1867b7d7b102f87290194b4be7932c92John McCall  // for consistency.
298babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
299babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
300babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
30193d557bc1867b7d7b102f87290194b4be7932c92John McCall
30293d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the function pointer.
303d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
30493d557bc1867b7d7b102f87290194b4be7932c92John McCall
30593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // If the LSB in the function pointer is 1, the function pointer points to
30693d557bc1867b7d7b102f87290194b4be7932c92John McCall  // a virtual function.
307babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *IsVirtual;
30821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI)
309babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
310babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  else
311babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
312babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
31393d557bc1867b7d7b102f87290194b4be7932c92John McCall  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
31493d557bc1867b7d7b102f87290194b4be7932c92John McCall
31593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the virtual path, the adjustment left 'This' pointing to the
31693d557bc1867b7d7b102f87290194b4be7932c92John McCall  // vtable of the correct base subobject.  The "function pointer" is an
317babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // offset within the vtable (+1 for the virtual flag on non-ARM).
31893d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnVirtual);
31993d557bc1867b7d7b102f87290194b4be7932c92John McCall
32093d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Cast the adjusted this to a pointer to vtable pointer and load.
3212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *VTableTy = Builder.getInt8PtrTy();
32293d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
323babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
32493d557bc1867b7d7b102f87290194b4be7932c92John McCall
32593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the offset.
326babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VTableOffset = FnAsInt;
32721fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (!UseARMMethodPtrABI)
32821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
329babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateGEP(VTable, VTableOffset);
33093d557bc1867b7d7b102f87290194b4be7932c92John McCall
33193d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the virtual function to call.
33293d557bc1867b7d7b102f87290194b4be7932c92John McCall  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
333babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
33493d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBranch(FnEnd);
33593d557bc1867b7d7b102f87290194b4be7932c92John McCall
33693d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the non-virtual path, the function pointer is actually a
33793d557bc1867b7d7b102f87290194b4be7932c92John McCall  // function pointer.
33893d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnNonVirtual);
33993d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *NonVirtualFn =
340babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
34193d557bc1867b7d7b102f87290194b4be7932c92John McCall
34293d557bc1867b7d7b102f87290194b4be7932c92John McCall  // We're done.
34393d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnEnd);
344bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
34593d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(VirtualFn, FnVirtual);
34693d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
34793d557bc1867b7d7b102f87290194b4be7932c92John McCall  return Callee;
34893d557bc1867b7d7b102f87290194b4be7932c92John McCall}
3493023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3506c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a
3516c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object.
3526c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
3536c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *Base,
3546c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *MemPtr,
3556c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                           const MemberPointerType *MPT) {
35692e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner  assert(MemPtr->getType() == CGM.PtrDiffTy);
3576c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3586c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  CGBuilderTy &Builder = CGF.Builder;
3596c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
360956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = Base->getType()->getPointerAddressSpace();
3616c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3626c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast to char*.
3636c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
3646c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3656c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Apply the offset, which we assume is non-null.
3666c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
3676c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3686c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast the address to the appropriate pointer type, adopting the
3696c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // address space of the base pointer.
3702acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *PType
371eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor    = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
3726c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  return Builder.CreateBitCast(Addr, PType);
3736c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall}
3746c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// conversion.
3774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall///
3784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Bitcast conversions are always a no-op under Itanium.
3790bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3800bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram:
3810bab0cdab751248ca389a5592bcb70eac5d39260John McCall///         <-- offset -->          <-- adjustment -->
3820bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   |--------------------------|----------------------|--------------------|
3830bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   ^Derived address point     ^Base address point    ^Member address point
3840bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3850bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer,
3860bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has
3870bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased;  and conversely, when converting a derived MP to a base MP
3880bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point
3890bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased.
3900bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3910bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from
3920bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here.
3930bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3940bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base
3950bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base.
3960bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member
3970bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers.
398d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value *
3990bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
4000bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Value *src) {
4022de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
4074d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
4084d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Use constant emission if we can.
4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isa<llvm::Constant>(src))
4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
4144d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
4153023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4163023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  CGBuilderTy &Builder = CGF.Builder;
4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
4204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
4213023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // For member data pointers, this is just a matter of adding the
4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // offset if the source is non-null.
4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
4254d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *dst;
4264d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
4274d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWSub(src, adj, "adj");
4284d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWAdd(src, adj, "adj");
4303023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // Null check.
4324d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
4334d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return Builder.CreateSelect(isNull, src, dst);
4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
4363023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4374d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // The this-adjustment is left-shifted by 1 on ARM.
43821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI) {
4394d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
4404d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
4424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
4433023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4444d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
4454d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *dstAdj;
4464d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
4474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
4483023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  else
4494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
4504d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return Builder.CreateInsertValue(src, dstAdj, 1);
4524d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall}
4534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4544d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallllvm::Constant *
4554d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
4564d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Constant *src) {
4574d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
4584d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
4594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
4603023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
4624d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
4634d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // If the adjustment is trivial, we don't need to do anything.
4654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
4664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
4674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4684d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
4694d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4704d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
4714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
472875ab10245d3bf37252dd822aa1616bb0a391095John McCall
4730bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member data pointers, this is just a matter of adding the
4740bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // offset if the source is non-null.
4754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
4764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // null maps to null.
4774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (src->isAllOnesValue()) return src;
4780bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
4804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWSub(src, adj);
4814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
4824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWAdd(src, adj);
4830bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
4840bab0cdab751248ca389a5592bcb70eac5d39260John McCall
485d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // The this-adjustment is left-shifted by 1 on ARM.
48621fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI) {
4874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
4884d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
4894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
490d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
491d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
4934d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *dstAdj;
4944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
4954d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
496d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  else
4974d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
498d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
5003023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall}
501cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
502cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant *
5030bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
5040bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
5050bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A NULL pointer is represented as -1.
5060bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
50792e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner    return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
50992e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner  llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
510d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Values[2] = { Zero, Zero };
511c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(Values);
512cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
513cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
5145808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallllvm::Constant *
5155808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
5165808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall                                     CharUnits offset) {
5170bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
5180bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A pointer to data member is an offset from the base address of
5190bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   the class object containing it, represented as a ptrdiff_t
52092e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner  return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
5210bab0cdab751248ca389a5592bcb70eac5d39260John McCall}
5220bab0cdab751248ca389a5592bcb70eac5d39260John McCall
523755d8497e39071aa24acc173ff07083e3256b8f8John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
5242d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return BuildMemberPointer(MD, CharUnits::Zero());
5252d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
5262d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5272d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
5282d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                  CharUnits ThisAdjustment) {
529d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  assert(MD->isInstance() && "Member function must not be static!");
530d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  MD = MD->getCanonicalDecl();
531875ab10245d3bf37252dd822aa1616bb0a391095John McCall
532d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  CodeGenTypes &Types = CGM.getTypes();
533875ab10245d3bf37252dd822aa1616bb0a391095John McCall
534d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Get the function pointer (or index if this is a virtual function).
535d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *MemPtr[2];
536d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (MD->isVirtual()) {
5371d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne    uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
538875ab10245d3bf37252dd822aa1616bb0a391095John McCall
5391246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    const ASTContext &Context = getContext();
5401246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    CharUnits PointerWidth =
541bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
5421246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
543d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
54421fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    if (UseARMMethodPtrABI) {
545d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // ARM C++ ABI 3.2.1:
546d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   This ABI specifies that adj contains twice the this
547d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   adjustment, plus 1 if the member function is virtual. The
548d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   least significant bit of adj then makes exactly the same
549d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   discrimination as the least significant bit of ptr does for
550d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   Itanium.
55192e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner      MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
55292e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner      MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
5532d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         2 * ThisAdjustment.getQuantity() + 1);
554d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
555d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // Itanium C++ ABI 2.3:
556d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   For a virtual function, [the pointer field] is 1 plus the
557d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   virtual table offset (in bytes) of the function,
558d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   represented as a ptrdiff_t.
55992e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner      MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
56092e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner      MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         ThisAdjustment.getQuantity());
562d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
563d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  } else {
564755d8497e39071aa24acc173ff07083e3256b8f8John McCall    const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
5652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *Ty;
566755d8497e39071aa24acc173ff07083e3256b8f8John McCall    // Check whether the function has a computable LLVM signature.
567f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    if (Types.isFuncTypeConvertible(FPT)) {
568755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // The function has a computable LLVM signature; use the correct type.
569de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
570d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
571755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
572755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // function type is incomplete.
57392e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner      Ty = CGM.PtrDiffTy;
574d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
575755d8497e39071aa24acc173ff07083e3256b8f8John McCall    llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
576d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
57792e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
57821fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
57921fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn                                       (UseARMMethodPtrABI ? 2 : 1) *
5802d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                       ThisAdjustment.getQuantity());
581d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
582d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
583c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(MemPtr);
584875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
585875ab10245d3bf37252dd822aa1616bb0a391095John McCall
5862d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
5872d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                 QualType MPType) {
5882d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
5892d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const ValueDecl *MPD = MP.getMemberPointerDecl();
5902d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (!MPD)
5912d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return EmitNullMemberPointer(MPT);
5922d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
593f632730ffb0b8ca531a35e737b29cc9f9774ca1dReid Kleckner  CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP);
5942d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5952d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
5962d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return BuildMemberPointer(MD, ThisAdjustment);
5972d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5982d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  CharUnits FieldOffset =
5992d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
6002d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
6012d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
6022d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
603e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are
604e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null.
605e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall///
606e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated.
607e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
6080bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
6090bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
6100bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
6110bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
6120bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality) {
613e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
614e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
615e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::ICmpInst::Predicate Eq;
616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Instruction::BinaryOps And, Or;
617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (Inequality) {
618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_NE;
619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::Or;
620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::And;
621e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  } else {
622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_EQ;
623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::And;
624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::Or;
625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
6270bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Member data pointers are easy because there's a unique null
6280bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // value, so it just comes down to bitwise equality.
6290bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
6300bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmp(Eq, L, R);
6310bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6320bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member function pointers, the tautologies are more complex.
6330bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The Itanium tautology is:
634de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
6350bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The ARM tautology is:
636de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr &&
637de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                  (L.adj == R.adj ||
638de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
6390bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The inequality tautologies have exactly the same structure, except
6400bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // applying De Morgan's laws.
6410bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6420bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
6430bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
6440bab0cdab751248ca389a5592bcb70eac5d39260John McCall
645e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.ptr == R.ptr.  This must always be
646e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true for equality to hold.
647e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
648e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
649e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition, together with the assumption that L.ptr == R.ptr,
650e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // tests whether the pointers are both null.  ARM imposes an extra
651e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // condition.
652e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
653e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
654e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
655e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.adj == R.adj.  If this isn't
656e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true, the pointers are unequal unless they're both null.
657d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
658d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
659e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
660e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
661e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Null member function pointers on ARM clear the low bit of Adj,
662e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // so the zero condition has to check that neither low bit is set.
66321fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI) {
664e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
666e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    // Compute (l.adj | r.adj) & 1 and test it against zero.
667e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                                                      "cmp.or.adj");
671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
672e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
673e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Tie together all our conditions.
675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
676e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  Result = Builder.CreateBinOp(And, PtrEq, Result,
677e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                               Inequality ? "memptr.ne" : "memptr.eq");
678e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
679e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
680e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
681e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
6820bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
6830bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *MemPtr,
6840bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT) {
685e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
6860bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6870bab0cdab751248ca389a5592bcb70eac5d39260John McCall  /// For member data pointers, this is just a check against -1.
6880bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer()) {
68992e44d911c748f2ef0d578bbf7b0703fb2ed4d9cReid Kleckner    assert(MemPtr->getType() == CGM.PtrDiffTy);
6900bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *NegativeOne =
6910bab0cdab751248ca389a5592bcb70eac5d39260John McCall      llvm::Constant::getAllOnesValue(MemPtr->getType());
6920bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
6930bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
694e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
695db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // In Itanium, a member function pointer is not null if 'ptr' is not null.
696d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
697e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
698e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
699e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
700e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
701db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // On ARM, a member function pointer is also non-null if the low bit of 'adj'
702db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // (the virtual bit) is set.
70321fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMMethodPtrABI) {
704e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
705d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
706e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
707db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
708db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar                                                  "memptr.isvirtual");
709db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    Result = Builder.CreateOr(Result, IsVirtual);
710e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
711e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
712e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
713e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
714875ab10245d3bf37252dd822aa1616bb0a391095John McCall
715f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data
716f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset.
717f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
718f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return MPT->getPointeeType()->isFunctionType();
719cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
721ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// The Itanium ABI always places an offset to the complete object
722ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// at entry -2 in the vtable.
723ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCallllvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
724ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                                   llvm::Value *ptr,
725ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                                   QualType type) {
726ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Grab the vtable pointer as an intptr_t*.
727ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
728ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
729ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Track back to entry -2 and pull out the offset there.
730ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *offsetPtr =
731ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall    CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
732ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
733ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  offset->setAlignment(CGF.PointerAlignInBytes);
734ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
735ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Apply the offset.
736ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
737ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  return CGF.Builder.CreateInBoundsGEP(ptr, offset);
738ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall}
739ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
740b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Klecknerllvm::Value *
741b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid KlecknerItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF,
742b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         llvm::Value *This,
743b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         const CXXRecordDecl *ClassDecl,
744b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                         const CXXRecordDecl *BaseClassDecl) {
745b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy);
746b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  CharUnits VBaseOffsetOffset =
747b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner    CGM.getVTableContext().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl);
748b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner
749b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  llvm::Value *VBaseOffsetPtr =
750b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner    CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
751b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                   "vbase.offset.ptr");
752b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr,
753b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner                                             CGM.PtrDiffTy->getPointerTo());
754b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner
755b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  llvm::Value *VBaseOffset =
756b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner    CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
757b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner
758b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner  return VBaseOffset;
759b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner}
760b0f533e716ae5a21ca5682ea235a68082fd5ed28Reid Kleckner
7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a
7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CXXCtorType Type,
7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CanQualType &ResTy,
7665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7679cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7693b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  // 'this' parameter is already there, as well as 'this' return if
7703b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  // HasThisReturn(GlobalDecl(Ctor, Type)) is true
7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
7734c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
777bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanovvoid ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) {
778bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  // Just make sure we're in sync with TargetCXXABI.
779bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  assert(CGM.getTarget().getCXXABI().hasConstructorVariants());
780bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov
781bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  // The constructor used for constructing this as a complete class;
782bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  // constucts the virtual bases, then calls the base constructor.
783bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  if (!D->getParent()->isAbstract()) {
784bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov    // We don't need to emit the complete ctor if the class is abstract.
785bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov    CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete));
786bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  }
787bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov
788bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  // The constructor used for constructing this as a base class;
789bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  // ignores virtual bases.
790bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov  CGM.EmitGlobal(GlobalDecl(D, Ctor_Base));
791bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov}
792bb1b797d2e432293563747bd9704b22cf0787061Timur Iskhodzhanov
7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a
7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CXXDtorType Type,
7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CanQualType &ResTy,
7985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7999cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
8004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8013b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  // 'this' parameter is already there, as well as 'this' return if
8023b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  // HasThisReturn(GlobalDecl(Dtor, Type)) is true
8034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
8054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
8064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
8074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
8084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
809a4130baad9d10b7feabb7e003da53424e986d269Reid Klecknervoid ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) {
810a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // The destructor in a virtual table is always a 'deleting'
811a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // destructor, which calls the complete destructor and then uses the
812a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // appropriate operator delete.
813a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  if (D->isVirtual())
814a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner    CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting));
815a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner
816a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // The destructor used for destructing this as a most-derived class;
817a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // call the base destructor and then destructs any virtual bases.
818a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete));
819a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner
820a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // The destructor used for destructing this as a base class; ignores
821a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  // virtual bases.
822a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner  CGM.EmitGlobal(GlobalDecl(D, Dtor_Base));
823a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner}
824a4130baad9d10b7feabb7e003da53424e986d269Reid Kleckner
8254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
8264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                QualType &ResTy,
8274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                FunctionArgList &Params) {
8284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Create the 'this' variable.
8294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  BuildThisParam(CGF, Params);
8304c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
8324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(MD->isInstance());
8334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need a VTT parameter as well.
835e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  if (NeedsVTTParameter(CGF.CurGD)) {
8369cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    ASTContext &Context = getContext();
8374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    // FIXME: avoid the fake decl
8394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    QualType T = Context.getPointerType(Context.VoidPtrTy);
8404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ImplicitParamDecl *VTTDecl
8414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
8424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                  &Context.Idents.get("vtt"), T);
843d26bc76c98006609002d9930f8840490e88ac5b5John McCall    Params.push_back(VTTDecl);
8444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTDecl(CGF) = VTTDecl;
8454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
8464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
8474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
8494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'this' slot.
8504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  EmitThisParam(CGF);
8514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'vtt' slot if needed.
8534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (getVTTDecl(CGF)) {
8544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTValue(CGF)
8554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
8564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                               "vtt");
8574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
8584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8593b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// If this is a function that the ABI specifies returns 'this', initialize
8603b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// the return slot to 'this' at the start of the function.
8613b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  ///
8623b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// Unlike the setting of return types, this is done within the ABI
8633b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// implementation instead of by clients of CGCXXABI because:
8643b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// 1) getThisValue is currently protected
8653b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  /// 2) in theory, an ABI could implement 'this' returns some other way;
8663b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  ///    HasThisReturn only specifies a contract, not the implementation
8674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
868cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
8694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
8704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8713b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Linvoid ItaniumCXXABI::EmitConstructorCall(CodeGenFunction &CGF,
8721d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                                        const CXXConstructorDecl *D,
8733b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                        CXXCtorType Type,
8743b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                        bool ForVirtualBase, bool Delegating,
8751d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                                        llvm::Value *This,
8761d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                                        CallExpr::const_arg_iterator ArgBeg,
8771d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                                        CallExpr::const_arg_iterator ArgEnd) {
8781d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  llvm::Value *VTT = CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase,
8791d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov                                         Delegating);
8801d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
8811d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
8821d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov
8831d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov  // FIXME: Provide a source location here.
8843b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(),
8853b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                        This, VTT, VTTTy, ArgBeg, ArgEnd);
8861d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov}
8871d4fff5551c2347010b955b4337a2aa7d65a050eTimur Iskhodzhanov
8883b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Linvoid ItaniumCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF,
8893b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                              const CXXDestructorDecl *Dtor,
8903b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                              CXXDtorType DtorType,
8913b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                              SourceLocation CallLoc,
8923b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                                              llvm::Value *This) {
8930f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov  assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
8940f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov
8950f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov  const CGFunctionInfo *FInfo
8960f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov    = &CGM.getTypes().arrangeCXXDestructor(Dtor, DtorType);
8970f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov  llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
8986e007f9fa0b9ebe5898025010c5ebc7d82decfa7Timur Iskhodzhanov  llvm::Value *Callee
8996e007f9fa0b9ebe5898025010c5ebc7d82decfa7Timur Iskhodzhanov    = CGF.BuildVirtualCall(GlobalDecl(Dtor, DtorType), This, Ty);
9000f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov
9013b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin  CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This,
9023b50e8d78c34fc57e25781015a2cb0536ca54f89Stephen Lin                        /*ImplicitParam=*/0, QualType(), 0, 0);
9030f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov}
9040f9827f5d6248d7008063768eb5f2c3e6ba83e94Timur Iskhodzhanov
9059063302a82423cb83f002257a416741850739a70Reid Klecknervoid ItaniumCXXABI::EmitVirtualInheritanceTables(
9069063302a82423cb83f002257a416741850739a70Reid Kleckner    llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD) {
9079063302a82423cb83f002257a416741850739a70Reid Kleckner  CodeGenVTables &VTables = CGM.getVTables();
9089063302a82423cb83f002257a416741850739a70Reid Kleckner  llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD);
9099063302a82423cb83f002257a416741850739a70Reid Kleckner  VTables.EmitVTTDefinition(VTT, Linkage, RD);
9109063302a82423cb83f002257a416741850739a70Reid Kleckner}
9119063302a82423cb83f002257a416741850739a70Reid Kleckner
9124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
9134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                    RValue RV, QualType ResultType) {
9144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
9154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
9164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
9174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Destructor thunks in the ARM ABI have indeterminate results.
9182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *T =
9194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
9204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  RValue Undef = RValue::get(llvm::UndefValue::get(T));
9214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
9224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
9231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/
9251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
926e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
927e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The array cookie is a size_t; pad that up to the element alignment.
928e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The cookie is actually right-justified in that space.
929e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
930e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                  CGM.getContext().getTypeAlignInChars(elementType));
9311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9321e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
9341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NewPtr,
9351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NumElements,
9366ec278d1a354517e20f13a877481453ee7940c78John McCall                                                  const CXXNewExpr *expr,
9371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  QualType ElementType) {
938e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
9391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
940956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = NewPtr->getType()->getPointerAddressSpace();
9411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9429cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
9431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  QualType SizeTy = Ctx.getSizeType();
9441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
9451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9461e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The size of the cookie.
9471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize =
9481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
949e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(CookieSize == getArrayCookieSizeImpl(ElementType));
9501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Compute an offset to the cookie.
9521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
9531e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieOffset = CookieSize - SizeSize;
9541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!CookieOffset.isZero())
9551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
9561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                 CookieOffset.getQuantity());
9571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Write the number of elements into the appropriate slot.
9591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr
9601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    = CGF.Builder.CreateBitCast(CookiePtr,
9611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                CGF.ConvertType(SizeTy)->getPointerTo(AS));
9621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, NumElementsPtr);
9631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
9651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
9661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
9671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
9681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
970e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
971e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                llvm::Value *allocPtr,
972e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                CharUnits cookieSize) {
973e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The element size is right-justified in the cookie.
974e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr = allocPtr;
975e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits numElementsOffset =
976e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
977e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  if (!numElementsOffset.isZero())
978e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    numElementsPtr =
979e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall      CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
980e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                             numElementsOffset.getQuantity());
9811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
982956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
983e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
984e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
985e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
9861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9871e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
988e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
989f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  // ARM says that the cookie is always:
9901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   struct array_cookie {
9911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_size; // element_size != 0
9921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_count;
9931e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   };
994f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  // But the base ABI doesn't give anything an alignment greater than
995f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  // 8, so we can dismiss this as typical ABI-author blindness to
996f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  // actual language complexity and round up to the element alignment.
997f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
998f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                  CGM.getContext().getTypeAlignInChars(elementType));
9991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
10001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
1002f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                                              llvm::Value *newPtr,
1003f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                                              llvm::Value *numElements,
10046ec278d1a354517e20f13a877481453ee7940c78John McCall                                              const CXXNewExpr *expr,
1005f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                                              QualType elementType) {
1006e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
10071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1008f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  // NewPtr is a char*, but we generalize to arbitrary addrspaces.
1009f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  unsigned AS = newPtr->getType()->getPointerAddressSpace();
10101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The cookie is always at the start of the buffer.
1012f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  llvm::Value *cookie = newPtr;
10131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The first element is the element size.
1015f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS));
1016f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
1017f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                 getContext().getTypeSizeInChars(elementType).getQuantity());
1018f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  CGF.Builder.CreateStore(elementSize, cookie);
10191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The second element is the element count.
1021f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1);
1022f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  CGF.Builder.CreateStore(numElements, cookie);
10231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
10251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
1026f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
1027f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
1028f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall                                                cookieSize.getQuantity());
10291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
10301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1031e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
1032e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            llvm::Value *allocPtr,
1033e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            CharUnits cookieSize) {
1034e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The number of elements is at offset sizeof(size_t) relative to
1035e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // the allocated pointer.
1036e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr
1037e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
10381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1039956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1040e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
1041e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1042e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
10431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
10441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/*********************** Static local initialization **************************/
10465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
10489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
10495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // int __cxa_guard_acquire(__guard *guard_object);
10502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
10515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1052da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                            GuardPtrTy, /*isVarArg=*/false);
1053e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1054c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1055c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                              llvm::AttributeSet::FunctionIndex,
105672390b39c545426023ec104afe8706395d732badBill Wendling                                                 llvm::Attribute::NoUnwind));
10575cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
10585cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
10609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
10615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_release(__guard *guard_object);
10622acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
10638b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1064e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1065c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1066c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                              llvm::AttributeSet::FunctionIndex,
106772390b39c545426023ec104afe8706395d732badBill Wendling                                                 llvm::Attribute::NoUnwind));
10685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
10695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
10719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       llvm::PointerType *GuardPtrTy) {
10725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_abort(__guard *guard_object);
10732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
10748b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1075e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1076c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1077c4c62fd78a4728c9e4d4df14911a2ced9bdd2031Bill Wendling                                              llvm::AttributeSet::FunctionIndex,
107872390b39c545426023ec104afe8706395d732badBill Wendling                                                 llvm::Attribute::NoUnwind));
10795cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
10805cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10815cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallnamespace {
10825cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  struct CallGuardAbort : EHScopeStack::Cleanup {
10835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::GlobalVariable *Guard;
10840f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
10855cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
1086ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
1087bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1088bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                  Guard);
10895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    }
10905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  };
10915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
10925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10935cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// The ARM code here follows the Itanium code closely enough that we
10945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// just special-case it at particular places.
10953030eb82593097502469a8b3fc26112c79c75605John McCallvoid ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
10963030eb82593097502469a8b3fc26112c79c75605John McCall                                    const VarDecl &D,
1097355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    llvm::GlobalVariable *var,
1098355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    bool shouldPerformInit) {
10995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGBuilderTy &Builder = CGF.Builder;
11003030eb82593097502469a8b3fc26112c79c75605John McCall
110104e517650569598e847c2ab609672e6df93effe5Richard Smith  // We only need to use thread-safe statics for local non-TLS variables;
11023030eb82593097502469a8b3fc26112c79c75605John McCall  // global initialization is always single-threaded.
110304e517650569598e847c2ab609672e6df93effe5Richard Smith  bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
110404e517650569598e847c2ab609672e6df93effe5Richard Smith                    D.isLocalVarDecl() && !D.getTLSKind();
1105173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson
1106173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // If we have a global variable with internal linkage and thread-safe statics
1107173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // are disabled, we can just let the guard variable be of type i8.
1108355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1109355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1110355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::IntegerType *guardTy;
11110502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (useInt8GuardVariable) {
1112355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guardTy = CGF.Int8Ty;
11130502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  } else {
1114c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // Guard variables are 64 bits in the generic ABI and size width on ARM
1115c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // (i.e. 32-bit on AArch32, 64-bit on AArch64).
111621fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn    guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty);
1117355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  }
1118355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1119355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1120355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // Create the guard variable if we don't already have it (as we
1121355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // might if we're double-emitting this function body).
1122355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1123355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  if (!guard) {
1124355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Mangle the name for the guard.
1125355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    SmallString<256> guardName;
1126355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    {
1127355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      llvm::raw_svector_ostream out(guardName);
1128355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      getMangleContext().mangleItaniumGuardVariable(&D, out);
1129355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      out.flush();
1130355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    }
1131355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1132355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Create the guard variable with a zero-initializer.
1133355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Just absorb linkage and visibility from the guarded variable.
1134355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1135355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     false, var->getLinkage(),
1136355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     llvm::ConstantInt::get(guardTy, 0),
1137355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     guardName.str());
1138355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard->setVisibility(var->getVisibility());
113904e517650569598e847c2ab609672e6df93effe5Richard Smith    // If the variable is thread-local, so is its guard variable.
114004e517650569598e847c2ab609672e6df93effe5Richard Smith    guard->setThreadLocalMode(var->getThreadLocalMode());
1141355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1142355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGM.setStaticLocalDeclGuardAddress(&D, guard);
1143173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  }
11445cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Test whether the variable has completed initialization.
1146355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::Value *isInitialized;
11475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // ARM C++ ABI 3.2.3.1:
11495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   To support the potential use of initialization guard variables
11505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   as semaphores that are the target of ARM SWP and LDREX/STREX
11515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   synchronizing instructions we define a static initialization
11525cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   guard variable to be a 4-byte aligned, 4- byte word with the
11535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   following inline access protocol.
11545cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     #define INITIALIZED 1
11555cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
11565cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if (__cxa_guard_acquire(&obj_guard))
11575cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ...
11585cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
115921fe45076c371e9a9f27e15c4e068e77a185fe62Mark Seaborn  if (UseARMGuardVarABI && !useInt8GuardVariable) {
1160355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    llvm::Value *V = Builder.CreateLoad(guard);
1161c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    llvm::Value *Test1 = llvm::ConstantInt::get(guardTy, 1);
1162c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    V = Builder.CreateAnd(V, Test1);
1163355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
11645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Itanium C++ ABI 3.3.2:
11665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   The following is pseudo-code showing how these functions can be used:
11675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if (obj_guard.first_byte == 0) {
11685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if ( __cxa_guard_acquire (&obj_guard) ) {
11695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         try {
11705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //           ... initialize the object ...;
11715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         } catch (...) {
11725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            __cxa_guard_abort (&obj_guard);
11735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            throw;
11745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         }
11755cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ... queue object destructor with __cxa_atexit() ...;
11765cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         __cxa_guard_release (&obj_guard);
11775cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       }
11785cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
11795cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
11805cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Load the first byte of the guard variable.
11810f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    llvm::LoadInst *LI =
1182355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
11830f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    LI->setAlignment(1);
1184eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1185eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // Itanium ABI:
1186eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   An implementation supporting thread-safety on multiprocessor
1187eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   systems must also guarantee that references to the initialized
1188eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   object do not occur before the load of the initialization flag.
1189eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //
1190eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // In LLVM, we do this by marking the load Acquire.
1191eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    if (threadsafe)
11920f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth      LI->setAtomic(llvm::Acquire);
1193eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1194355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
11955cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
11965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
11985cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
11995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12005cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Check if the first byte of the guard variable is zero.
1201355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
12025cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12035cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(InitCheckBlock);
12045cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12055cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Variables used when coping with thread-safe statics and exceptions.
12060502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
12075cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_acquire.
12085cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::Value *V
1209bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
12105cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12115cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
12125cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12135cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
12145cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall                         InitBlock, EndBlock);
12155cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12165cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_abort along the exceptional edge.
1217355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
12185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12195cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.EmitBlock(InitBlock);
12205cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
12215cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12225cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Emit the initializer and add a global destructor if appropriate.
1223355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
12245cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12250502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
12265cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Pop the guard-abort cleanup if we pushed one.
12275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.PopCleanupBlock();
12285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12295cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_release.  This cannot throw.
1230bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
12315cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
1232355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
12335cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
12345cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
12355cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(EndBlock);
12365cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
123720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
123820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor using __cxa_atexit.
123920bb175cb8ae5844034828db094fb948c0e3454aJohn McCallstatic void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
124020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                        llvm::Constant *dtor,
124104e517650569598e847c2ab609672e6df93effe5Richard Smith                                        llvm::Constant *addr,
124204e517650569598e847c2ab609672e6df93effe5Richard Smith                                        bool TLS) {
12434e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling  const char *Name = "__cxa_atexit";
12444e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling  if (TLS) {
12454e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling    const llvm::Triple &T = CGF.getTarget().getTriple();
12464e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling    Name = T.isMacOSX() ?  "_tlv_atexit" : "__cxa_thread_atexit";
12474e3b54b4acb4dd926ca50d7f06c8265d1d24ba79Bill Wendling  }
124804e517650569598e847c2ab609672e6df93effe5Richard Smith
124920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // We're assuming that the destructor function is something we can
125020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // reasonably call with the default CC.  Go ahead and cast it to the
125120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // right prototype.
125220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *dtorTy =
125320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
125420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
125520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
125620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
125720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::FunctionType *atexitTy =
125820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.IntTy, paramTys, false);
125920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
126020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Fetch the actual function.
126104e517650569598e847c2ab609672e6df93effe5Richard Smith  llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
126220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
126320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    fn->setDoesNotThrow();
126420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
126520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Create a variable that binds the atexit to this shared object.
126620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Constant *handle =
126720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
126820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
126920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Value *args[] = {
127020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(dtor, dtorTy),
127120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
127220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    handle
127320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  };
1274bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  CGF.EmitNounwindRuntimeCall(atexit, args);
127520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
127620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
127720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor as best as we know how.
127820bb175cb8ae5844034828db094fb948c0e3454aJohn McCallvoid ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
127904e517650569598e847c2ab609672e6df93effe5Richard Smith                                       const VarDecl &D,
128020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *dtor,
128120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *addr) {
128220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Use __cxa_atexit if available.
128304e517650569598e847c2ab609672e6df93effe5Richard Smith  if (CGM.getCodeGenOpts().CXAAtExit)
128404e517650569598e847c2ab609672e6df93effe5Richard Smith    return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
128504e517650569598e847c2ab609672e6df93effe5Richard Smith
128604e517650569598e847c2ab609672e6df93effe5Richard Smith  if (D.getTLSKind())
128704e517650569598e847c2ab609672e6df93effe5Richard Smith    CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
128820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
128920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // In Apple kexts, we want to add a global destructor entry.
129020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // FIXME: shouldn't this be guarded by some variable?
12917edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (CGM.getLangOpts().AppleKext) {
129220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    // Generate a global destructor entry.
129320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    return CGM.AddCXXDtorEntry(dtor, addr);
129420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  }
129520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
129620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  CGF.registerGlobalDtorWithAtExit(dtor, addr);
129720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
1298b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1299b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// Get the appropriate linkage for the wrapper function. This is essentially
1300b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// the weak form of the variable's linkage; every translation unit which wneeds
1301b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith/// the wrapper emits a copy, and we want the linker to merge them.
1302b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithstatic llvm::GlobalValue::LinkageTypes getThreadLocalWrapperLinkage(
1303b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::GlobalValue::LinkageTypes VarLinkage) {
1304b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  if (llvm::GlobalValue::isLinkerPrivateLinkage(VarLinkage))
1305b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    return llvm::GlobalValue::LinkerPrivateWeakLinkage;
1306b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  // For internal linkage variables, we don't need an external or weak wrapper.
1307b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
1308b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    return VarLinkage;
1309b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  return llvm::GlobalValue::WeakODRLinkage;
1310b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith}
1311b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1312b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithllvm::Function *
1313b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard SmithItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
1314b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                                             llvm::GlobalVariable *Var) {
1315b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  // Mangle the name for the thread_local wrapper function.
1316b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  SmallString<256> WrapperName;
1317b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  {
1318b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::raw_svector_ostream Out(WrapperName);
1319b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
1320b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    Out.flush();
1321b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  }
1322b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1323b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  if (llvm::Value *V = Var->getParent()->getNamedValue(WrapperName))
1324b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    return cast<llvm::Function>(V);
1325b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1326b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Type *RetTy = Var->getType();
1327b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  if (VD->getType()->isReferenceType())
1328b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    RetTy = RetTy->getPointerElementType();
1329b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1330b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false);
1331b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Function *Wrapper = llvm::Function::Create(
1332b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      FnTy, getThreadLocalWrapperLinkage(Var->getLinkage()), WrapperName.str(),
1333b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      &CGM.getModule());
1334b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  // Always resolve references to the wrapper at link time.
1335b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
1336b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  return Wrapper;
1337b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith}
1338b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1339b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smithvoid ItaniumCXXABI::EmitThreadLocalInitFuncs(
1340b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
1341b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::Function *InitFunc) {
1342b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1343b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    const VarDecl *VD = Decls[I].first;
1344b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::GlobalVariable *Var = Decls[I].second;
1345b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1346b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // Mangle the name for the thread_local initialization function.
1347b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    SmallString<256> InitFnName;
1348b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    {
1349b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::raw_svector_ostream Out(InitFnName);
1350b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
1351b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Out.flush();
1352b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    }
1353b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1354b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // If we have a definition for the variable, emit the initialization
1355b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // function as an alias to the global Init function (if any). Otherwise,
1356b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // produce a declaration of the initialization function.
1357b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::GlobalValue *Init = 0;
1358b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    bool InitIsInitFunc = false;
1359b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    if (VD->hasDefinition()) {
1360b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      InitIsInitFunc = true;
1361b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      if (InitFunc)
1362b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith        Init =
1363b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith            new llvm::GlobalAlias(InitFunc->getType(), Var->getLinkage(),
1364b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                                  InitFnName.str(), InitFunc, &CGM.getModule());
1365b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    } else {
1366b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      // Emit a weak global function referring to the initialization function.
1367b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      // This function will not exist if the TU defining the thread_local
1368b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      // variable in question does not need any dynamic initialization for
1369b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      // its thread_local variables.
1370b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
1371b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Init = llvm::Function::Create(
1372b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith          FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(),
1373b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith          &CGM.getModule());
1374b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    }
1375b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1376b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    if (Init)
1377b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Init->setVisibility(Var->getVisibility());
1378b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1379b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
1380b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::LLVMContext &Context = CGM.getModule().getContext();
1381b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
1382b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    CGBuilderTy Builder(Entry);
1383b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    if (InitIsInitFunc) {
1384b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      if (Init)
1385b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith        Builder.CreateCall(Init);
1386b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    } else {
1387b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      // Don't know whether we have an init function. Call it if it exists.
1388b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::Value *Have = Builder.CreateIsNotNull(Init);
1389b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
1390b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
1391b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Builder.CreateCondBr(Have, InitBB, ExitBB);
1392b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1393b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Builder.SetInsertPoint(InitBB);
1394b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Builder.CreateCall(Init);
1395b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Builder.CreateBr(ExitBB);
1396b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1397b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Builder.SetInsertPoint(ExitBB);
1398b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    }
1399b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1400b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // For a reference, the result of the wrapper function is a pointer to
1401b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    // the referenced object.
1402b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    llvm::Value *Val = Var;
1403b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    if (VD->getType()->isReferenceType()) {
1404b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      llvm::LoadInst *LI = Builder.CreateLoad(Val);
1405b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity());
1406b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      Val = LI;
1407b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    }
1408b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1409b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    Builder.CreateRet(Val);
1410b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  }
1411b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith}
1412b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1413b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard SmithLValue ItaniumCXXABI::EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
1414b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                                                 const DeclRefExpr *DRE) {
1415b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  const VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1416b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  QualType T = VD->getType();
1417b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T);
1418b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty);
1419b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  llvm::Function *Wrapper =
1420b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith      getOrCreateThreadLocalWrapper(VD, cast<llvm::GlobalVariable>(Val));
1421b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1422b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  Val = CGF.Builder.CreateCall(Wrapper);
1423b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith
1424b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  LValue LV;
1425b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  if (VD->getType()->isReferenceType())
1426b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    LV = CGF.MakeNaturalAlignAddrLValue(Val, T);
1427b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  else
1428b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith    LV = CGF.MakeAddrLValue(Val, DRE->getType(),
1429b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith                            CGF.getContext().getDeclAlign(VD));
1430b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  // FIXME: need setObjCGCLValueClass?
1431b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith  return LV;
1432b80a16eadd0dacabfc1c32412e243ccb99dd664dRichard Smith}
1433e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
1434e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne/// Return whether the given global decl needs a VTT parameter, which it does
1435e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne/// if it's a base constructor or destructor with virtual bases.
1436e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbournebool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) {
1437e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1438e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
1439e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  // We don't have any virtual bases, just return early.
1440e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  if (!MD->getParent()->getNumVBases())
1441e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne    return false;
1442e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
1443e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  // Check if we have a base constructor.
1444e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base)
1445e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne    return true;
1446e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
1447e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  // Check if we have a base destructor.
1448e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
1449e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne    return true;
1450e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne
1451e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne  return false;
1452e1e35f761970fd662696b803a839c1f4a56f61b2Peter Collingbourne}
1453