ItaniumCXXABI.cpp revision 7edf9e38b91917b661277601c0e448eef0eb2b56
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"
28ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "llvm/Intrinsics.h"
2925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow#include "llvm/DataLayout.h"
30ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper#include "llvm/Value.h"
313a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisusing namespace clang;
3393d557bc1867b7d7b102f87290194b4be7932c92John McCallusing namespace CodeGen;
343a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
353a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisnamespace {
36071cc7deffad608165b1ddd5263e8bf181861520Charles Davisclass ItaniumCXXABI : public CodeGen::CGCXXABI {
370bab0cdab751248ca389a5592bcb70eac5d39260John McCallprivate:
389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::IntegerType *PtrDiffTy;
3993d557bc1867b7d7b102f87290194b4be7932c92John McCallprotected:
40babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  bool IsARM;
410bab0cdab751248ca389a5592bcb70eac5d39260John McCall
420bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // It's a little silly for us to cache this.
439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::IntegerType *getPtrDiffTy() {
440bab0cdab751248ca389a5592bcb70eac5d39260John McCall    if (!PtrDiffTy) {
459cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall      QualType T = getContext().getPointerDiffType();
469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      llvm::Type *Ty = CGM.getTypes().ConvertType(T);
470bab0cdab751248ca389a5592bcb70eac5d39260John McCall      PtrDiffTy = cast<llvm::IntegerType>(Ty);
480bab0cdab751248ca389a5592bcb70eac5d39260John McCall    }
490bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return PtrDiffTy;
500bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
510bab0cdab751248ca389a5592bcb70eac5d39260John McCall
523a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davispublic:
53babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
5414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    CGCXXABI(CGM), PtrDiffTy(0), IsARM(IsARM) { }
5593d557bc1867b7d7b102f87290194b4be7932c92John McCall
56f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  bool isZeroInitializable(const MemberPointerType *MPT);
57cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
590bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6093d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
6193d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
6293d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
6393d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT);
643023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
656c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
666c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *Base,
676c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *MemPtr,
686c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            const MemberPointerType *MPT);
696c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
700bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
710bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
720bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *Src);
734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                              llvm::Constant *Src);
75cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
760bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
77cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
78755d8497e39071aa24acc173ff07083e3256b8f8John McCall  llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
795808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
805808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall                                        CharUnits offset);
812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
822d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                     CharUnits ThisAdjustment);
84875ab10245d3bf37252dd822aa1616bb0a391095John McCall
850bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
860bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
870bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
880bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
890bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality);
90e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
910bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
920bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *Addr,
930bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT);
944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
95ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
96ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                      llvm::Value *ptr,
97ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                      QualType type);
98ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<CanQualType> &ArgTys);
1034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys);
1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
115285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos  StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
1162eb9a959d24ad757a82ecab61f343635ad67749aDavid Blaikie  StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
117285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos
118e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1226ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
1231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
124e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
125e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   llvm::Value *allocPtr,
126e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
1275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
1283030eb82593097502469a8b3fc26112c79c75605John McCall  void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
1290f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth                       llvm::GlobalVariable *DeclPtr, bool PerformInit);
13020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
13120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                          llvm::Constant *addr);
1329ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis
1339ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis  void EmitVTables(const CXXRecordDecl *Class);
1343a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis};
135ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
136ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI {
137ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic:
138babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
1394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
1414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<CanQualType> &ArgTys);
1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys);
1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
1574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
158e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1626ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
1631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
164e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
165e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallprivate:
1684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// \brief Returns true if the given instance method is one of the
1694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// kinds that the ARM ABI says returns 'this'.
1704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  static bool HasThisReturn(GlobalDecl GD) {
1714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
1734c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall            (isa<CXXConstructorDecl>(MD)));
1744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
175ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall};
1763a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1773a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
178071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
1793a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  return new ItaniumCXXABI(CGM);
1803a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1813a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
182ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallCodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
183ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return new ARMCXXABI(CGM);
184ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}
185ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
1869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *
1870bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
1880bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
1890bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return getPtrDiffTy();
1907650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
191875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
192875ab10245d3bf37252dd822aa1616bb0a391095John McCall
193babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form:
194babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
195babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
196babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI:
197babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
198babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj)
199babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr - 1)
200babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
201babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI:
202babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.adj & 1) is nonzero
203babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj >> 1)
204babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr)
205babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions
206babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned.
207babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
208babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points
209babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied.
210babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
211babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of
212babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call.
21393d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value *
21493d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
21593d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
21693d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
21793d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT) {
21893d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGBuilderTy &Builder = CGF.Builder;
21993d557bc1867b7d7b102f87290194b4be7932c92John McCall
22093d557bc1867b7d7b102f87290194b4be7932c92John McCall  const FunctionProtoType *FPT =
22193d557bc1867b7d7b102f87290194b4be7932c92John McCall    MPT->getPointeeType()->getAs<FunctionProtoType>();
22293d557bc1867b7d7b102f87290194b4be7932c92John McCall  const CXXRecordDecl *RD =
22393d557bc1867b7d7b102f87290194b4be7932c92John McCall    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
22493d557bc1867b7d7b102f87290194b4be7932c92John McCall
2252acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
226de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CGM.getTypes().GetFunctionType(
227de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      CGM.getTypes().arrangeCXXMethodType(RD, FPT));
22893d557bc1867b7d7b102f87290194b4be7932c92John McCall
2292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *ptrdiff = getPtrDiffTy();
230babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
23193d557bc1867b7d7b102f87290194b4be7932c92John McCall
232babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
233babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
234babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
235babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
236d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Extract memptr.adj, which is in the second field.
237d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
238babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
239babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // Compute the true adjustment.
240babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Adj = RawAdj;
241babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
242babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
24393d557bc1867b7d7b102f87290194b4be7932c92John McCall
24493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the adjustment and cast back to the original struct type
24593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // for consistency.
246babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
247babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
248babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
24993d557bc1867b7d7b102f87290194b4be7932c92John McCall
25093d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the function pointer.
251d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
25293d557bc1867b7d7b102f87290194b4be7932c92John McCall
25393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // If the LSB in the function pointer is 1, the function pointer points to
25493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // a virtual function.
255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *IsVirtual;
256babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
257babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
258babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  else
259babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
260babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
26193d557bc1867b7d7b102f87290194b4be7932c92John McCall  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
26293d557bc1867b7d7b102f87290194b4be7932c92John McCall
26393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the virtual path, the adjustment left 'This' pointing to the
26493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // vtable of the correct base subobject.  The "function pointer" is an
265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // offset within the vtable (+1 for the virtual flag on non-ARM).
26693d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnVirtual);
26793d557bc1867b7d7b102f87290194b4be7932c92John McCall
26893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Cast the adjusted this to a pointer to vtable pointer and load.
2692acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *VTableTy = Builder.getInt8PtrTy();
27093d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
271babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
27293d557bc1867b7d7b102f87290194b4be7932c92John McCall
27393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the offset.
274babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VTableOffset = FnAsInt;
275babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
276babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateGEP(VTable, VTableOffset);
27793d557bc1867b7d7b102f87290194b4be7932c92John McCall
27893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the virtual function to call.
27993d557bc1867b7d7b102f87290194b4be7932c92John McCall  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
280babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
28193d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBranch(FnEnd);
28293d557bc1867b7d7b102f87290194b4be7932c92John McCall
28393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the non-virtual path, the function pointer is actually a
28493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // function pointer.
28593d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnNonVirtual);
28693d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *NonVirtualFn =
287babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
28893d557bc1867b7d7b102f87290194b4be7932c92John McCall
28993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // We're done.
29093d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnEnd);
291bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
29293d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(VirtualFn, FnVirtual);
29393d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
29493d557bc1867b7d7b102f87290194b4be7932c92John McCall  return Callee;
29593d557bc1867b7d7b102f87290194b4be7932c92John McCall}
2963023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
2976c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a
2986c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object.
2996c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
3006c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *Base,
3016c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *MemPtr,
3026c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                           const MemberPointerType *MPT) {
3036c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  assert(MemPtr->getType() == getPtrDiffTy());
3046c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3056c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  CGBuilderTy &Builder = CGF.Builder;
3066c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
307956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = Base->getType()->getPointerAddressSpace();
3086c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3096c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast to char*.
3106c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
3116c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3126c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Apply the offset, which we assume is non-null.
3136c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
3146c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3156c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast the address to the appropriate pointer type, adopting the
3166c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // address space of the base pointer.
3172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *PType
318eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor    = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
3196c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  return Builder.CreateBitCast(Addr, PType);
3206c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall}
3216c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
3234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// conversion.
3244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall///
3254d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Bitcast conversions are always a no-op under Itanium.
3260bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3270bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram:
3280bab0cdab751248ca389a5592bcb70eac5d39260John McCall///         <-- offset -->          <-- adjustment -->
3290bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   |--------------------------|----------------------|--------------------|
3300bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   ^Derived address point     ^Base address point    ^Member address point
3310bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3320bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer,
3330bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has
3340bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased;  and conversely, when converting a derived MP to a base MP
3350bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point
3360bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased.
3370bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3380bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from
3390bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here.
3400bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3410bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base
3420bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base.
3430bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member
3440bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers.
345d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value *
3460bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3470bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
3484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Value *src) {
3492de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3504d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
3514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
3524d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
3544d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
3554d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3564d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Use constant emission if we can.
3574d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isa<llvm::Constant>(src))
3584d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
3594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3604d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
3614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
3623023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3633023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  CGBuilderTy &Builder = CGF.Builder;
3644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
3654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
3674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
3683023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3694d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // For member data pointers, this is just a matter of adding the
3704d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // offset if the source is non-null.
3714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
3724d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *dst;
3734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
3744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWSub(src, adj, "adj");
3754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWAdd(src, adj, "adj");
3773023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // Null check.
3794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
3804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
3814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return Builder.CreateSelect(isNull, src, dst);
3824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
3833023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3844d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // The this-adjustment is left-shifted by 1 on ARM.
3854d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (IsARM) {
3864d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
3874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
3884d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
3894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
3903023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3914d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
3924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *dstAdj;
3934d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
3944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
3953023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  else
3964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
3974d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3984d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return Builder.CreateInsertValue(src, dstAdj, 1);
3994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall}
4004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallllvm::Constant *
4024d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Constant *src) {
4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
4073023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4084d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // If the adjustment is trivial, we don't need to do anything.
4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
4144d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4154d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
4164d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
419875ab10245d3bf37252dd822aa1616bb0a391095John McCall
4200bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member data pointers, this is just a matter of adding the
4210bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // offset if the source is non-null.
4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // null maps to null.
4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (src->isAllOnesValue()) return src;
4250bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4264d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
4274d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWSub(src, adj);
4284d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWAdd(src, adj);
4300bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
4310bab0cdab751248ca389a5592bcb70eac5d39260John McCall
432d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // The this-adjustment is left-shifted by 1 on ARM.
433d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (IsARM) {
4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
4364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
437d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
438d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4394d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
4404d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *dstAdj;
4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
4424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
443d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  else
4444d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
445d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4464d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
4473023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall}
448cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
449cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant *
4500bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
4512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ptrdiff_t = getPtrDiffTy();
4520bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4530bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4540bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A NULL pointer is represented as -1.
4550bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
4560bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
457d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
458d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
459d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Values[2] = { Zero, Zero };
460c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(Values);
461cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
462cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
4635808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallllvm::Constant *
4645808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
4655808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall                                     CharUnits offset) {
4660bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4670bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A pointer to data member is an offset from the base address of
4680bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   the class object containing it, represented as a ptrdiff_t
4695808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
4700bab0cdab751248ca389a5592bcb70eac5d39260John McCall}
4710bab0cdab751248ca389a5592bcb70eac5d39260John McCall
472755d8497e39071aa24acc173ff07083e3256b8f8John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
4732d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return BuildMemberPointer(MD, CharUnits::Zero());
4742d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
4752d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
4762d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
4772d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                  CharUnits ThisAdjustment) {
478d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  assert(MD->isInstance() && "Member function must not be static!");
479d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  MD = MD->getCanonicalDecl();
480875ab10245d3bf37252dd822aa1616bb0a391095John McCall
481d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  CodeGenTypes &Types = CGM.getTypes();
4822acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ptrdiff_t = getPtrDiffTy();
483875ab10245d3bf37252dd822aa1616bb0a391095John McCall
484d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Get the function pointer (or index if this is a virtual function).
485d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *MemPtr[2];
486d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (MD->isVirtual()) {
4871d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne    uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
488875ab10245d3bf37252dd822aa1616bb0a391095John McCall
4891246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    const ASTContext &Context = getContext();
4901246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    CharUnits PointerWidth =
491bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
4921246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
493d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
494d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (IsARM) {
495d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // ARM C++ ABI 3.2.1:
496d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   This ABI specifies that adj contains twice the this
497d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   adjustment, plus 1 if the member function is virtual. The
498d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   least significant bit of adj then makes exactly the same
499d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   discrimination as the least significant bit of ptr does for
500d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   Itanium.
501d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
5022d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
5032d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         2 * ThisAdjustment.getQuantity() + 1);
504d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
505d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // Itanium C++ ABI 2.3:
506d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   For a virtual function, [the pointer field] is 1 plus the
507d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   virtual table offset (in bytes) of the function,
508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   represented as a ptrdiff_t.
509d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
5102d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
5112d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         ThisAdjustment.getQuantity());
512d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
513d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  } else {
514755d8497e39071aa24acc173ff07083e3256b8f8John McCall    const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
5152acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *Ty;
516755d8497e39071aa24acc173ff07083e3256b8f8John McCall    // Check whether the function has a computable LLVM signature.
517f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    if (Types.isFuncTypeConvertible(FPT)) {
518755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // The function has a computable LLVM signature; use the correct type.
519de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
520d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
521755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
522755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // function type is incomplete.
523755d8497e39071aa24acc173ff07083e3256b8f8John McCall      Ty = ptrdiff_t;
524d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
525755d8497e39071aa24acc173ff07083e3256b8f8John McCall    llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
526d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
527379b5155b4566f63679e1da6b0ceb5fdfa2aec6dJohn McCall    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
5282d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
5292d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                       ThisAdjustment.getQuantity());
530d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
531d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
532c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(MemPtr);
533875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
534875ab10245d3bf37252dd822aa1616bb0a391095John McCall
5352d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
5362d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                 QualType MPType) {
5372d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
5382d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const ValueDecl *MPD = MP.getMemberPointerDecl();
5392d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (!MPD)
5402d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return EmitNullMemberPointer(MPT);
5412d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5422d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // Compute the this-adjustment.
5432d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  CharUnits ThisAdjustment = CharUnits::Zero();
5442d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
5452d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  bool DerivedMember = MP.isMemberPointerToDerivedMember();
5462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
5472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
5482d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    const CXXRecordDecl *Base = RD;
5492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    const CXXRecordDecl *Derived = Path[I];
5502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    if (DerivedMember)
5512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      std::swap(Base, Derived);
5522d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    ThisAdjustment +=
5532d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
5542d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    RD = Path[I];
5552d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  }
5562d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (DerivedMember)
5572d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    ThisAdjustment = -ThisAdjustment;
5582d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5592d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
5602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return BuildMemberPointer(MD, ThisAdjustment);
5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5622d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  CharUnits FieldOffset =
5632d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
5642d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
5652d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
5662d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
567e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are
568e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null.
569e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall///
570e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated.
571e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
5720bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
5730bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
5740bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
5750bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
5760bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality) {
577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
579e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::ICmpInst::Predicate Eq;
580e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Instruction::BinaryOps And, Or;
581e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (Inequality) {
582e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_NE;
583e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::Or;
584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::And;
585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  } else {
586e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_EQ;
587e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::And;
588e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::Or;
589e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
590e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
5910bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Member data pointers are easy because there's a unique null
5920bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // value, so it just comes down to bitwise equality.
5930bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
5940bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmp(Eq, L, R);
5950bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5960bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member function pointers, the tautologies are more complex.
5970bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The Itanium tautology is:
598de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
5990bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The ARM tautology is:
600de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr &&
601de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                  (L.adj == R.adj ||
602de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The inequality tautologies have exactly the same structure, except
6040bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // applying De Morgan's laws.
6050bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6060bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
6070bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
6080bab0cdab751248ca389a5592bcb70eac5d39260John McCall
609e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.ptr == R.ptr.  This must always be
610e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true for equality to hold.
611e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
612e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
613e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition, together with the assumption that L.ptr == R.ptr,
614e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // tests whether the pointers are both null.  ARM imposes an extra
615e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // condition.
616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.adj == R.adj.  If this isn't
620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true, the pointers are unequal unless they're both null.
621d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
622d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Null member function pointers on ARM clear the low bit of Adj,
626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // so the zero condition has to check that neither low bit is set.
627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
628e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
629e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    // Compute (l.adj | r.adj) & 1 and test it against zero.
631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                                                      "cmp.or.adj");
635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Tie together all our conditions.
639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  Result = Builder.CreateBinOp(And, PtrEq, Result,
641e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                               Inequality ? "memptr.ne" : "memptr.eq");
642e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
643e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
645e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
6460bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
6470bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *MemPtr,
6480bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT) {
649e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
6500bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6510bab0cdab751248ca389a5592bcb70eac5d39260John McCall  /// For member data pointers, this is just a check against -1.
6520bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer()) {
6530bab0cdab751248ca389a5592bcb70eac5d39260John McCall    assert(MemPtr->getType() == getPtrDiffTy());
6540bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *NegativeOne =
6550bab0cdab751248ca389a5592bcb70eac5d39260John McCall      llvm::Constant::getAllOnesValue(MemPtr->getType());
6560bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
6570bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
658e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
659db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // In Itanium, a member function pointer is not null if 'ptr' is not null.
660d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
661e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
662e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
663e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
664e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
665db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // On ARM, a member function pointer is also non-null if the low bit of 'adj'
666db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // (the virtual bit) is set.
667e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
669d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
671db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
672db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar                                                  "memptr.isvirtual");
673db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    Result = Builder.CreateOr(Result, IsVirtual);
674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
676e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
677e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
678875ab10245d3bf37252dd822aa1616bb0a391095John McCall
679f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data
680f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset.
681f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
682f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return MPT->getPointeeType()->isFunctionType();
683cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
6844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
685ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// The Itanium ABI always places an offset to the complete object
686ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall/// at entry -2 in the vtable.
687ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCallllvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
688ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                                   llvm::Value *ptr,
689ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall                                                   QualType type) {
690ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Grab the vtable pointer as an intptr_t*.
691ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
692ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
693ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Track back to entry -2 and pull out the offset there.
694ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::Value *offsetPtr =
695ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall    CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
696ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
697ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  offset->setAlignment(CGF.PointerAlignInBytes);
698ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
699ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  // Apply the offset.
700ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
701ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall  return CGF.Builder.CreateInBoundsGEP(ptr, offset);
702ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall}
703ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall
7044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a
7054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
7074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CXXCtorType Type,
7084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CanQualType &ResTy,
7095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7109cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
7114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // 'this' is already there.
7134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
7164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
7174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
7214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CXXCtorType Type,
7224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CanQualType &ResTy,
7235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
7254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ResTy = ArgTys[0];
7264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a
7294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7304c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CXXDtorType Type,
7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CanQualType &ResTy,
7335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7349cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // 'this' is already there.
7374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
7404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
7414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// for non-deleting destructors.
7454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CXXDtorType Type,
7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CanQualType &ResTy,
7485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type != Dtor_Deleting)
7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ResTy = ArgTys[0];
7534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                QualType &ResTy,
7574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                FunctionArgList &Params) {
7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Create the 'this' variable.
7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  BuildThisParam(CGF, Params);
7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(MD->isInstance());
7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need a VTT parameter as well.
7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
7669cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    ASTContext &Context = getContext();
7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    // FIXME: avoid the fake decl
7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    QualType T = Context.getPointerType(Context.VoidPtrTy);
7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ImplicitParamDecl *VTTDecl
7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                  &Context.Idents.get("vtt"), T);
773d26bc76c98006609002d9930f8840490e88ac5b5John McCall    Params.push_back(VTTDecl);
7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTDecl(CGF) = VTTDecl;
7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            QualType &ResTy,
7804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            FunctionArgList &Params) {
7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
7824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Return 'this' from certain constructors and destructors.
7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
785d26bc76c98006609002d9930f8840490e88ac5b5John McCall    ResTy = Params[0]->getType();
7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'this' slot.
7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  EmitThisParam(CGF);
7914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'vtt' slot if needed.
7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (getVTTDecl(CGF)) {
7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTValue(CGF)
7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                               "vtt");
7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
8014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
8024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the return slot to 'this' at the start of the
8044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// function.
8054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
806cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
8074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
8084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
8104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                    RValue RV, QualType ResultType) {
8114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
8124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
8134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
8144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Destructor thunks in the ARM ABI have indeterminate results.
8152acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *T =
8164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
8174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  RValue Undef = RValue::get(llvm::UndefValue::get(T));
8184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
8194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
8201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/
8221e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
823e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
824e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The array cookie is a size_t; pad that up to the element alignment.
825e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The cookie is actually right-justified in that space.
826e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
827e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                  CGM.getContext().getTypeAlignInChars(elementType));
8281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
8311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NewPtr,
8321e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NumElements,
8336ec278d1a354517e20f13a877481453ee7940c78John McCall                                                  const CXXNewExpr *expr,
8341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  QualType ElementType) {
835e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
8361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
837956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = NewPtr->getType()->getPointerAddressSpace();
8381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8399cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
8401e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  QualType SizeTy = Ctx.getSizeType();
8411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
8421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The size of the cookie.
8441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize =
8451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
846e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(CookieSize == getArrayCookieSizeImpl(ElementType));
8471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Compute an offset to the cookie.
8491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
8501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieOffset = CookieSize - SizeSize;
8511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!CookieOffset.isZero())
8521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
8531e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                 CookieOffset.getQuantity());
8541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Write the number of elements into the appropriate slot.
8561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr
8571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    = CGF.Builder.CreateBitCast(CookiePtr,
8581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                CGF.ConvertType(SizeTy)->getPointerTo(AS));
8591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, NumElementsPtr);
8601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
8641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
8651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
867e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
868e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                llvm::Value *allocPtr,
869e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                CharUnits cookieSize) {
870e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The element size is right-justified in the cookie.
871e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr = allocPtr;
872e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits numElementsOffset =
873e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
874e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  if (!numElementsOffset.isZero())
875e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    numElementsPtr =
876e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall      CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
877e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                             numElementsOffset.getQuantity());
8781e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
879956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
880e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
881e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
882e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
8831e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8841e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
885e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
8861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // On ARM, the cookie is always:
8871e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   struct array_cookie {
8881e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_size; // element_size != 0
8891e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_count;
8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   };
8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // TODO: what should we do if the allocated type actually wants
8921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // greater alignment?
893e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes);
8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NewPtr,
8981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NumElements,
8996ec278d1a354517e20f13a877481453ee7940c78John McCall                                              const CXXNewExpr *expr,
9001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              QualType ElementType) {
901e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
9021e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // NewPtr is a char*.
9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
905956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = NewPtr->getType()->getPointerAddressSpace();
9061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9079cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
9081e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
9092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *SizeTy =
9101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
9111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9121e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The cookie is always at the start of the buffer.
9131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
9141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The first element is the element size.
9161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
9171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
9181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                          Ctx.getTypeSizeInChars(ElementType).getQuantity());
9191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(ElementSize, CookiePtr);
9201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The second element is the element count.
9221e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
9231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, CookiePtr);
9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
9261e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
9271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize = 2 * SizeSize;
9281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
9291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
9301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
932e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
933e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            llvm::Value *allocPtr,
934e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            CharUnits cookieSize) {
935e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The number of elements is at offset sizeof(size_t) relative to
936e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // the allocated pointer.
937e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr
938e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
9391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
940956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
941e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
942e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
943e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
9441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/*********************** Static local initialization **************************/
9475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
9499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
9505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // int __cxa_guard_acquire(__guard *guard_object);
9512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9525cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
953da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                            GuardPtrTy, /*isVarArg=*/false);
954e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
95550e6b18f99c45b31e6216ab221f6b3911b24fa1fBill Wendling                                   llvm::Attributes::get(CGM.getLLVMContext(),
956a6375560645177168099f1a1d96be8fa4718aa8eBill Wendling                                                 llvm::Attributes::NoUnwind));
9575cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9585cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
9609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
9615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_release(__guard *guard_object);
9622acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9638b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
964e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
96550e6b18f99c45b31e6216ab221f6b3911b24fa1fBill Wendling                                   llvm::Attributes::get(CGM.getLLVMContext(),
966a6375560645177168099f1a1d96be8fa4718aa8eBill Wendling                                                 llvm::Attributes::NoUnwind));
9675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
9709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       llvm::PointerType *GuardPtrTy) {
9715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_abort(__guard *guard_object);
9722acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9738b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
974e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
97550e6b18f99c45b31e6216ab221f6b3911b24fa1fBill Wendling                                   llvm::Attributes::get(CGM.getLLVMContext(),
976a6375560645177168099f1a1d96be8fa4718aa8eBill Wendling                                                 llvm::Attributes::NoUnwind));
9775cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9785cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9795cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallnamespace {
9805cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  struct CallGuardAbort : EHScopeStack::Cleanup {
9815cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::GlobalVariable *Guard;
9820f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
9835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
984ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
9855cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall      CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
9865cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall        ->setDoesNotThrow();
9875cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    }
9885cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  };
9895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// The ARM code here follows the Itanium code closely enough that we
9925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// just special-case it at particular places.
9933030eb82593097502469a8b3fc26112c79c75605John McCallvoid ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
9943030eb82593097502469a8b3fc26112c79c75605John McCall                                    const VarDecl &D,
995355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    llvm::GlobalVariable *var,
996355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    bool shouldPerformInit) {
9975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGBuilderTy &Builder = CGF.Builder;
9983030eb82593097502469a8b3fc26112c79c75605John McCall
9993030eb82593097502469a8b3fc26112c79c75605John McCall  // We only need to use thread-safe statics for local variables;
10003030eb82593097502469a8b3fc26112c79c75605John McCall  // global initialization is always single-threaded.
10010502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  bool threadsafe =
10024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
1003173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson
1004173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // If we have a global variable with internal linkage and thread-safe statics
1005173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // are disabled, we can just let the guard variable be of type i8.
1006355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1007355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1008355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::IntegerType *guardTy;
10090502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (useInt8GuardVariable) {
1010355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guardTy = CGF.Int8Ty;
10110502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  } else {
1012173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson    // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
1013355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
1014355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  }
1015355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1016355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1017355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // Create the guard variable if we don't already have it (as we
1018355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // might if we're double-emitting this function body).
1019355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1020355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  if (!guard) {
1021355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Mangle the name for the guard.
1022355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    SmallString<256> guardName;
1023355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    {
1024355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      llvm::raw_svector_ostream out(guardName);
1025355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      getMangleContext().mangleItaniumGuardVariable(&D, out);
1026355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      out.flush();
1027355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    }
1028355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1029355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Create the guard variable with a zero-initializer.
1030355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Just absorb linkage and visibility from the guarded variable.
1031355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1032355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     false, var->getLinkage(),
1033355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     llvm::ConstantInt::get(guardTy, 0),
1034355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     guardName.str());
1035355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard->setVisibility(var->getVisibility());
1036355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1037355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGM.setStaticLocalDeclGuardAddress(&D, guard);
1038173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  }
10395cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10405cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Test whether the variable has completed initialization.
1041355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::Value *isInitialized;
10425cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10435cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // ARM C++ ABI 3.2.3.1:
10445cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   To support the potential use of initialization guard variables
10455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   as semaphores that are the target of ARM SWP and LDREX/STREX
10465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   synchronizing instructions we define a static initialization
10475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   guard variable to be a 4-byte aligned, 4- byte word with the
10485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   following inline access protocol.
10495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     #define INITIALIZED 1
10505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
10515cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if (__cxa_guard_acquire(&obj_guard))
10525cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ...
10535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
10540502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (IsARM && !useInt8GuardVariable) {
1055355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    llvm::Value *V = Builder.CreateLoad(guard);
10565cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    V = Builder.CreateAnd(V, Builder.getInt32(1));
1057355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
10585cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Itanium C++ ABI 3.3.2:
10605cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   The following is pseudo-code showing how these functions can be used:
10615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if (obj_guard.first_byte == 0) {
10625cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if ( __cxa_guard_acquire (&obj_guard) ) {
10635cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         try {
10645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //           ... initialize the object ...;
10655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         } catch (...) {
10665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            __cxa_guard_abort (&obj_guard);
10675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            throw;
10685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         }
10695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ... queue object destructor with __cxa_atexit() ...;
10705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         __cxa_guard_release (&obj_guard);
10715cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       }
10725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
10735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
10745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Load the first byte of the guard variable.
10750f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    llvm::LoadInst *LI =
1076355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
10770f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    LI->setAlignment(1);
1078eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1079eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // Itanium ABI:
1080eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   An implementation supporting thread-safety on multiprocessor
1081eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   systems must also guarantee that references to the initialized
1082eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   object do not occur before the load of the initialization flag.
1083eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //
1084eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // In LLVM, we do this by marking the load Acquire.
1085eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    if (threadsafe)
10860f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth      LI->setAtomic(llvm::Acquire);
1087eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1088355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
10895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
10905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
10925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
10935cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Check if the first byte of the guard variable is zero.
1095355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
10965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(InitCheckBlock);
10985cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Variables used when coping with thread-safe statics and exceptions.
11000502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
11015cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_acquire.
11025cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::Value *V
1103355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
11045cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11055cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
11065cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11075cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
11085cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall                         InitBlock, EndBlock);
11095cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11105cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_abort along the exceptional edge.
1111355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
11125cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11135cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.EmitBlock(InitBlock);
11145cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
11155cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11165cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Emit the initializer and add a global destructor if appropriate.
1117355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
11185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11190502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
11205cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Pop the guard-abort cleanup if we pushed one.
11215cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.PopCleanupBlock();
11225cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11235cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_release.  This cannot throw.
1124355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
11255cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
1126355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
11275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
11285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11295cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(EndBlock);
11305cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
113120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
113220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor using __cxa_atexit.
113320bb175cb8ae5844034828db094fb948c0e3454aJohn McCallstatic void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
113420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                        llvm::Constant *dtor,
113520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                        llvm::Constant *addr) {
113620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // We're assuming that the destructor function is something we can
113720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // reasonably call with the default CC.  Go ahead and cast it to the
113820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // right prototype.
113920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *dtorTy =
114020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
114120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
114220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
114320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
114420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::FunctionType *atexitTy =
114520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.IntTy, paramTys, false);
114620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
114720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Fetch the actual function.
114820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Constant *atexit =
114920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
115020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
115120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    fn->setDoesNotThrow();
115220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
115320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Create a variable that binds the atexit to this shared object.
115420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Constant *handle =
115520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
115620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
115720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Value *args[] = {
115820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(dtor, dtorTy),
115920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
116020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    handle
116120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  };
116220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow();
116320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
116420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
116520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor as best as we know how.
116620bb175cb8ae5844034828db094fb948c0e3454aJohn McCallvoid ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
116720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *dtor,
116820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *addr) {
116920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Use __cxa_atexit if available.
117020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (CGM.getCodeGenOpts().CXAAtExit) {
117120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
117220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  }
117320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
117420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // In Apple kexts, we want to add a global destructor entry.
117520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // FIXME: shouldn't this be guarded by some variable?
11767edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith  if (CGM.getLangOpts().AppleKext) {
117720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    // Generate a global destructor entry.
117820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    return CGM.AddCXXDtorEntry(dtor, addr);
117920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  }
118020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
118120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  CGF.registerGlobalDtorWithAtExit(dtor, addr);
118220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
11839ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis
11849ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis/// Generate and emit virtual tables for the given class.
11859ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davisvoid ItaniumCXXABI::EmitVTables(const CXXRecordDecl *Class) {
11869ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis  CGM.getVTables().GenerateClassData(CGM.getVTableLinkage(Class), Class);
11879ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis}
1188