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"
2614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include <clang/AST/Mangle.h>
2793d557bc1867b7d7b102f87290194b4be7932c92John McCall#include <clang/AST/Type.h>
280502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall#include <llvm/Intrinsics.h>
290bab0cdab751248ca389a5592bcb70eac5d39260John McCall#include <llvm/Target/TargetData.h>
3093d557bc1867b7d7b102f87290194b4be7932c92John McCall#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
954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<CanQualType> &ArgTys);
994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys);
1044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
111285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos  StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
112285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos
113e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1176ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
1181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
119e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
120e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   llvm::Value *allocPtr,
121e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
1225cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
1233030eb82593097502469a8b3fc26112c79c75605John McCall  void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
1240f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth                       llvm::GlobalVariable *DeclPtr, bool PerformInit);
12520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  void registerGlobalDtor(CodeGenFunction &CGF, llvm::Constant *dtor,
12620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                          llvm::Constant *addr);
1279ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis
1289ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis  void EmitVTables(const CXXRecordDecl *Class);
1293a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis};
130ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
131ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI {
132ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic:
133babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
1344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
1364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<CanQualType> &ArgTys);
1394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys);
1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
153e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits getArrayCookieSizeImpl(QualType elementType);
1541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1576ec278d1a354517e20f13a877481453ee7940c78John McCall                                     const CXXNewExpr *expr,
1581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
159e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
160e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                   CharUnits cookieSize);
1614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallprivate:
1634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// \brief Returns true if the given instance method is one of the
1644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// kinds that the ARM ABI says returns 'this'.
1654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  static bool HasThisReturn(GlobalDecl GD) {
1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
1684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall            (isa<CXXConstructorDecl>(MD)));
1694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
170ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall};
1713a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1723a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
173071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
1743a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  return new ItaniumCXXABI(CGM);
1753a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1763a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
177ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallCodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
178ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return new ARMCXXABI(CGM);
179ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}
180ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
1819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *
1820bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
1830bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
1840bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return getPtrDiffTy();
1857650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner  return llvm::StructType::get(getPtrDiffTy(), getPtrDiffTy(), NULL);
186875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
187875ab10245d3bf37252dd822aa1616bb0a391095John McCall
188babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form:
189babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
190babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
191babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI:
192babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
193babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj)
194babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr - 1)
195babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
196babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI:
197babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.adj & 1) is nonzero
198babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj >> 1)
199babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr)
200babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions
201babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned.
202babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
203babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points
204babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied.
205babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
206babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of
207babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call.
20893d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value *
20993d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
21093d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
21193d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
21293d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT) {
21393d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGBuilderTy &Builder = CGF.Builder;
21493d557bc1867b7d7b102f87290194b4be7932c92John McCall
21593d557bc1867b7d7b102f87290194b4be7932c92John McCall  const FunctionProtoType *FPT =
21693d557bc1867b7d7b102f87290194b4be7932c92John McCall    MPT->getPointeeType()->getAs<FunctionProtoType>();
21793d557bc1867b7d7b102f87290194b4be7932c92John McCall  const CXXRecordDecl *RD =
21893d557bc1867b7d7b102f87290194b4be7932c92John McCall    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
21993d557bc1867b7d7b102f87290194b4be7932c92John McCall
2202acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
221de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CGM.getTypes().GetFunctionType(
222de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      CGM.getTypes().arrangeCXXMethodType(RD, FPT));
22393d557bc1867b7d7b102f87290194b4be7932c92John McCall
2242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *ptrdiff = getPtrDiffTy();
225babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
22693d557bc1867b7d7b102f87290194b4be7932c92John McCall
227babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
228babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
229babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
230babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
231d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Extract memptr.adj, which is in the second field.
232d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
233babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
234babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // Compute the true adjustment.
235babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Adj = RawAdj;
236babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
237babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
23893d557bc1867b7d7b102f87290194b4be7932c92John McCall
23993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the adjustment and cast back to the original struct type
24093d557bc1867b7d7b102f87290194b4be7932c92John McCall  // for consistency.
241babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
242babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
243babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
24493d557bc1867b7d7b102f87290194b4be7932c92John McCall
24593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the function pointer.
246d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
24793d557bc1867b7d7b102f87290194b4be7932c92John McCall
24893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // If the LSB in the function pointer is 1, the function pointer points to
24993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // a virtual function.
250babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *IsVirtual;
251babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
252babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
253babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  else
254babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
25693d557bc1867b7d7b102f87290194b4be7932c92John McCall  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
25793d557bc1867b7d7b102f87290194b4be7932c92John McCall
25893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the virtual path, the adjustment left 'This' pointing to the
25993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // vtable of the correct base subobject.  The "function pointer" is an
260babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // offset within the vtable (+1 for the virtual flag on non-ARM).
26193d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnVirtual);
26293d557bc1867b7d7b102f87290194b4be7932c92John McCall
26393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Cast the adjusted this to a pointer to vtable pointer and load.
2642acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *VTableTy = Builder.getInt8PtrTy();
26593d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
266babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
26793d557bc1867b7d7b102f87290194b4be7932c92John McCall
26893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the offset.
269babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VTableOffset = FnAsInt;
270babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
271babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateGEP(VTable, VTableOffset);
27293d557bc1867b7d7b102f87290194b4be7932c92John McCall
27393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the virtual function to call.
27493d557bc1867b7d7b102f87290194b4be7932c92John McCall  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
275babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
27693d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBranch(FnEnd);
27793d557bc1867b7d7b102f87290194b4be7932c92John McCall
27893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the non-virtual path, the function pointer is actually a
27993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // function pointer.
28093d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnNonVirtual);
28193d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *NonVirtualFn =
282babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
28393d557bc1867b7d7b102f87290194b4be7932c92John McCall
28493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // We're done.
28593d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnEnd);
286bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
28793d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(VirtualFn, FnVirtual);
28893d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
28993d557bc1867b7d7b102f87290194b4be7932c92John McCall  return Callee;
29093d557bc1867b7d7b102f87290194b4be7932c92John McCall}
2913023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
2926c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a
2936c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object.
2946c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
2956c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *Base,
2966c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *MemPtr,
2976c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                           const MemberPointerType *MPT) {
2986c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  assert(MemPtr->getType() == getPtrDiffTy());
2996c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3006c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  CGBuilderTy &Builder = CGF.Builder;
3016c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3026c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
3036c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3046c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast to char*.
3056c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
3066c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3076c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Apply the offset, which we assume is non-null.
3086c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
3096c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3106c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast the address to the appropriate pointer type, adopting the
3116c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // address space of the base pointer.
3122acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *PType
313eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor    = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
3146c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  return Builder.CreateBitCast(Addr, PType);
3156c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall}
3166c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
3184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// conversion.
3194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall///
3204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall/// Bitcast conversions are always a no-op under Itanium.
3210bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3220bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram:
3230bab0cdab751248ca389a5592bcb70eac5d39260John McCall///         <-- offset -->          <-- adjustment -->
3240bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   |--------------------------|----------------------|--------------------|
3250bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   ^Derived address point     ^Base address point    ^Member address point
3260bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3270bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer,
3280bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has
3290bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased;  and conversely, when converting a derived MP to a base MP
3300bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point
3310bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased.
3320bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3330bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from
3340bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here.
3350bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3360bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base
3370bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base.
3380bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member
3390bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers.
340d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value *
3410bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3420bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
3434d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Value *src) {
3442de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3454d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
3464d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
3474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
3494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
3504d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Use constant emission if we can.
3524d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isa<llvm::Constant>(src))
3534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
3544d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3554d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
3564d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
3573023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3583023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  CGBuilderTy &Builder = CGF.Builder;
3594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
3604d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
3624d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
3633023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // For member data pointers, this is just a matter of adding the
3654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // offset if the source is non-null.
3664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
3674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *dst;
3684d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
3694d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWSub(src, adj, "adj");
3704d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
3714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      dst = Builder.CreateNSWAdd(src, adj, "adj");
3723023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // Null check.
3744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
3754d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    return Builder.CreateSelect(isNull, src, dst);
3774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
3783023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // The this-adjustment is left-shifted by 1 on ARM.
3804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (IsARM) {
3814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
3824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
3834d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
3844d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  }
3853023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3864d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
3874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Value *dstAdj;
3884d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
3894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
3903023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  else
3914d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
3924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3934d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return Builder.CreateInsertValue(src, dstAdj, 1);
3944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall}
3954d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
3964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallllvm::Constant *
3974d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCallItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
3984d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall                                           llvm::Constant *src) {
3994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
4004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall         E->getCastKind() == CK_ReinterpretMemberPointer);
4023023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // Under Itanium, reinterprets don't require any additional processing.
4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  // If the adjustment is trivial, we don't need to do anything.
4074d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *adj = getMemberPointerAdjustment(E);
4084d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (!adj) return src;
4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall
4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  const MemberPointerType *destTy =
4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    E->getType()->castAs<MemberPointerType>();
414875ab10245d3bf37252dd822aa1616bb0a391095John McCall
4150bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member data pointers, this is just a matter of adding the
4160bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // offset if the source is non-null.
4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (destTy->isMemberDataPointer()) {
4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    // null maps to null.
4194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (src->isAllOnesValue()) return src;
4200bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4214d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    if (isDerivedToBase)
4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWSub(src, adj);
4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    else
4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall      return llvm::ConstantExpr::getNSWAdd(src, adj);
4250bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
4260bab0cdab751248ca389a5592bcb70eac5d39260John McCall
427d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // The this-adjustment is left-shifted by 1 on ARM.
428d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (IsARM) {
4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
4304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    offset <<= 1;
4314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    adj = llvm::ConstantInt::get(adj->getType(), offset);
432d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
433d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  llvm::Constant *dstAdj;
4364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  if (isDerivedToBase)
4374d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
438d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  else
4394d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall    dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
440d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall  return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
4423023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall}
443cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
444cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant *
4450bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
4462acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ptrdiff_t = getPtrDiffTy();
4470bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4480bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4490bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A NULL pointer is represented as -1.
4500bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
4510bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
452d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
453d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
454d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Values[2] = { Zero, Zero };
455c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(Values);
456cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
457cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
4585808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallllvm::Constant *
4595808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCallItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
4605808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall                                     CharUnits offset) {
4610bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4620bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A pointer to data member is an offset from the base address of
4630bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   the class object containing it, represented as a ptrdiff_t
4645808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall  return llvm::ConstantInt::get(getPtrDiffTy(), offset.getQuantity());
4650bab0cdab751248ca389a5592bcb70eac5d39260John McCall}
4660bab0cdab751248ca389a5592bcb70eac5d39260John McCall
467755d8497e39071aa24acc173ff07083e3256b8f8John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
4682d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return BuildMemberPointer(MD, CharUnits::Zero());
4692d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
4702d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
4712d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
4722d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                  CharUnits ThisAdjustment) {
473d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  assert(MD->isInstance() && "Member function must not be static!");
474d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  MD = MD->getCanonicalDecl();
475875ab10245d3bf37252dd822aa1616bb0a391095John McCall
476d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  CodeGenTypes &Types = CGM.getTypes();
4772acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *ptrdiff_t = getPtrDiffTy();
478875ab10245d3bf37252dd822aa1616bb0a391095John McCall
479d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Get the function pointer (or index if this is a virtual function).
480d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *MemPtr[2];
481d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (MD->isVirtual()) {
4821d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne    uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
483875ab10245d3bf37252dd822aa1616bb0a391095John McCall
4841246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    const ASTContext &Context = getContext();
4851246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    CharUnits PointerWidth =
486bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
4871246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck    uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
488d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
489d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (IsARM) {
490d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // ARM C++ ABI 3.2.1:
491d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   This ABI specifies that adj contains twice the this
492d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   adjustment, plus 1 if the member function is virtual. The
493d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   least significant bit of adj then makes exactly the same
494d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   discrimination as the least significant bit of ptr does for
495d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   Itanium.
496d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
4972d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
4982d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         2 * ThisAdjustment.getQuantity() + 1);
499d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
500d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // Itanium C++ ABI 2.3:
501d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   For a virtual function, [the pointer field] is 1 plus the
502d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   virtual table offset (in bytes) of the function,
503d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   represented as a ptrdiff_t.
504d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
5052d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t,
5062d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                         ThisAdjustment.getQuantity());
507d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  } else {
509755d8497e39071aa24acc173ff07083e3256b8f8John McCall    const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
5102acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *Ty;
511755d8497e39071aa24acc173ff07083e3256b8f8John McCall    // Check whether the function has a computable LLVM signature.
512f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    if (Types.isFuncTypeConvertible(FPT)) {
513755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // The function has a computable LLVM signature; use the correct type.
514de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
515d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
516755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
517755d8497e39071aa24acc173ff07083e3256b8f8John McCall      // function type is incomplete.
518755d8497e39071aa24acc173ff07083e3256b8f8John McCall      Ty = ptrdiff_t;
519d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
520755d8497e39071aa24acc173ff07083e3256b8f8John McCall    llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
521d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
522379b5155b4566f63679e1da6b0ceb5fdfa2aec6dJohn McCall    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, ptrdiff_t);
5232d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, (IsARM ? 2 : 1) *
5242d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                       ThisAdjustment.getQuantity());
525d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
526d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
527c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner  return llvm::ConstantStruct::getAnon(MemPtr);
528875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
529875ab10245d3bf37252dd822aa1616bb0a391095John McCall
5302d6a5670465cb3f1d811695a9f23e372508240d2Richard Smithllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
5312d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith                                                 QualType MPType) {
5322d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
5332d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const ValueDecl *MPD = MP.getMemberPointerDecl();
5342d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (!MPD)
5352d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return EmitNullMemberPointer(MPT);
5362d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5372d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  // Compute the this-adjustment.
5382d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  CharUnits ThisAdjustment = CharUnits::Zero();
5392d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
5402d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  bool DerivedMember = MP.isMemberPointerToDerivedMember();
5412d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
5422d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
5432d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    const CXXRecordDecl *Base = RD;
5442d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    const CXXRecordDecl *Derived = Path[I];
5452d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    if (DerivedMember)
5462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      std::swap(Base, Derived);
5472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    ThisAdjustment +=
5482d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith      getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
5492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    RD = Path[I];
5502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  }
5512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (DerivedMember)
5522d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    ThisAdjustment = -ThisAdjustment;
5532d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5542d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
5552d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    return BuildMemberPointer(MD, ThisAdjustment);
5562d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
5572d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  CharUnits FieldOffset =
5582d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith    getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
5592d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith  return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
5602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith}
5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith
562e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are
563e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null.
564e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall///
565e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated.
566e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
5670bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
5680bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
5690bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
5700bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
5710bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality) {
572e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
573e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
574e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::ICmpInst::Predicate Eq;
575e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Instruction::BinaryOps And, Or;
576e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (Inequality) {
577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_NE;
578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::Or;
579e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::And;
580e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  } else {
581e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_EQ;
582e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::And;
583e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::Or;
584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
5860bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Member data pointers are easy because there's a unique null
5870bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // value, so it just comes down to bitwise equality.
5880bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
5890bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmp(Eq, L, R);
5900bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5910bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member function pointers, the tautologies are more complex.
5920bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The Itanium tautology is:
593de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
5940bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The ARM tautology is:
595de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr &&
596de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                  (L.adj == R.adj ||
597de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
5980bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The inequality tautologies have exactly the same structure, except
5990bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // applying De Morgan's laws.
6000bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6010bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
6020bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall
604e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.ptr == R.ptr.  This must always be
605e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true for equality to hold.
606e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
607e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
608e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition, together with the assumption that L.ptr == R.ptr,
609e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // tests whether the pointers are both null.  ARM imposes an extra
610e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // condition.
611e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
612e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
613e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
614e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.adj == R.adj.  If this isn't
615e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true, the pointers are unequal unless they're both null.
616d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
617d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Null member function pointers on ARM clear the low bit of Adj,
621e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // so the zero condition has to check that neither low bit is set.
622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    // Compute (l.adj | r.adj) & 1 and test it against zero.
626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
628e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
629e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                                                      "cmp.or.adj");
630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Tie together all our conditions.
634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  Result = Builder.CreateBinOp(And, PtrEq, Result,
636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                               Inequality ? "memptr.ne" : "memptr.eq");
637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
6410bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
6420bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *MemPtr,
6430bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT) {
644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
6450bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6460bab0cdab751248ca389a5592bcb70eac5d39260John McCall  /// For member data pointers, this is just a check against -1.
6470bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer()) {
6480bab0cdab751248ca389a5592bcb70eac5d39260John McCall    assert(MemPtr->getType() == getPtrDiffTy());
6490bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *NegativeOne =
6500bab0cdab751248ca389a5592bcb70eac5d39260John McCall      llvm::Constant::getAllOnesValue(MemPtr->getType());
6510bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
6520bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
653e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
654db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // In Itanium, a member function pointer is not null if 'ptr' is not null.
655d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
656e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
657e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
658e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
659e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
660db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // On ARM, a member function pointer is also non-null if the low bit of 'adj'
661db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar  // (the virtual bit) is set.
662e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
663e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
664d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
666db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
667db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar                                                  "memptr.isvirtual");
668db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar    Result = Builder.CreateOr(Result, IsVirtual);
669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
672e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
673875ab10245d3bf37252dd822aa1616bb0a391095John McCall
674f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data
675f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset.
676f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
677f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return MPT->getPointeeType()->isFunctionType();
678cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
6794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a
6814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
6824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
6834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CXXCtorType Type,
6844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CanQualType &ResTy,
6855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
6869cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
6874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // 'this' is already there.
6894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
6924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
6934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
6944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
6964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
6974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CXXCtorType Type,
6984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CanQualType &ResTy,
6995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
7014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ResTy = ArgTys[0];
7024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a
7054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CXXDtorType 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 == Dtor_Base && Dtor->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 McCall/// for non-deleting destructors.
7214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CXXDtorType Type,
7234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CanQualType &ResTy,
7245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                SmallVectorImpl<CanQualType> &ArgTys) {
7254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
7264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type != Dtor_Deleting)
7284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ResTy = ArgTys[0];
7294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7304c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                QualType &ResTy,
7334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                FunctionArgList &Params) {
7344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Create the 'this' variable.
7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  BuildThisParam(CGF, Params);
7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
7384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(MD->isInstance());
7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need a VTT parameter as well.
7414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
7429cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    ASTContext &Context = getContext();
7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    // FIXME: avoid the fake decl
7454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    QualType T = Context.getPointerType(Context.VoidPtrTy);
7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ImplicitParamDecl *VTTDecl
7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
7484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                  &Context.Idents.get("vtt"), T);
749d26bc76c98006609002d9930f8840490e88ac5b5John McCall    Params.push_back(VTTDecl);
7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTDecl(CGF) = VTTDecl;
7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            QualType &ResTy,
7564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            FunctionArgList &Params) {
7574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Return 'this' from certain constructors and destructors.
7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
761d26bc76c98006609002d9930f8840490e88ac5b5John McCall    ResTy = Params[0]->getType();
7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'this' slot.
7664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  EmitThisParam(CGF);
7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'vtt' slot if needed.
7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (getVTTDecl(CGF)) {
7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTValue(CGF)
7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                               "vtt");
7734c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the return slot to 'this' at the start of the
7804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// function.
7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
782cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman    CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                    RValue RV, QualType ResultType) {
7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Destructor thunks in the ARM ABI have indeterminate results.
7912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *T =
7924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  RValue Undef = RValue::get(llvm::UndefValue::get(T));
7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
7971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/
7981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
799e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
800e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The array cookie is a size_t; pad that up to the element alignment.
801e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The cookie is actually right-justified in that space.
802e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
803e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                  CGM.getContext().getTypeAlignInChars(elementType));
8041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
8071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NewPtr,
8081e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NumElements,
8096ec278d1a354517e20f13a877481453ee7940c78John McCall                                                  const CXXNewExpr *expr,
8101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  QualType ElementType) {
811e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
8121e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
8141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8159cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
8161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  QualType SizeTy = Ctx.getSizeType();
8171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
8181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The size of the cookie.
8201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize =
8211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
822e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(CookieSize == getArrayCookieSizeImpl(ElementType));
8231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Compute an offset to the cookie.
8251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
8261e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieOffset = CookieSize - SizeSize;
8271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!CookieOffset.isZero())
8281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
8291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                 CookieOffset.getQuantity());
8301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Write the number of elements into the appropriate slot.
8321e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr
8331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    = CGF.Builder.CreateBitCast(CookiePtr,
8341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                CGF.ConvertType(SizeTy)->getPointerTo(AS));
8351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, NumElementsPtr);
8361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
8381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
8391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
8401e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
8411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
843e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
844e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                llvm::Value *allocPtr,
845e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                                CharUnits cookieSize) {
846e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The element size is right-justified in the cookie.
847e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr = allocPtr;
848e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  CharUnits numElementsOffset =
849e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
850e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  if (!numElementsOffset.isZero())
851e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    numElementsPtr =
852e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall      CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
853e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                             numElementsOffset.getQuantity());
8541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
855e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace();
856e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
857e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
858e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
8591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
861e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallCharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // On ARM, the cookie is always:
8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   struct array_cookie {
8641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_size; // element_size != 0
8651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_count;
8661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   };
8671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // TODO: what should we do if the allocated type actually wants
8681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // greater alignment?
869e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes);
8701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
8731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NewPtr,
8741e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NumElements,
8756ec278d1a354517e20f13a877481453ee7940c78John McCall                                              const CXXNewExpr *expr,
8761e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              QualType ElementType) {
877e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  assert(requiresArrayCookie(expr));
8781e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8791e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // NewPtr is a char*.
8801e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
8821e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8839cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
8841e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
8852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::IntegerType *SizeTy =
8861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
8871e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8881e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The cookie is always at the start of the buffer.
8891e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The first element is the element size.
8921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
8931e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                          Ctx.getTypeSizeInChars(ElementType).getQuantity());
8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(ElementSize, CookiePtr);
8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The second element is the element count.
8981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
8991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, CookiePtr);
9001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
9021e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize = 2 * SizeSize;
9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
9051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
9061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
908e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallllvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
909e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            llvm::Value *allocPtr,
910e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall                                            CharUnits cookieSize) {
911e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // The number of elements is at offset sizeof(size_t) relative to
912e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  // the allocated pointer.
913e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  llvm::Value *numElementsPtr
914e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
9151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
916e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace();
917e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  numElementsPtr =
918e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
919e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall  return CGF.Builder.CreateLoad(numElementsPtr);
9201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9225cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/*********************** Static local initialization **************************/
9235cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9245cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
9259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
9265cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // int __cxa_guard_acquire(__guard *guard_object);
9272acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
929da549e8995c447542d5631b8b67fcc3a9582797aJay Foad                            GuardPtrTy, /*isVarArg=*/false);
9305cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
931e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
932e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky                                   llvm::Attribute::NoUnwind);
9335cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9345cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9355cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
9369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                         llvm::PointerType *GuardPtrTy) {
9375cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_release(__guard *guard_object);
9382acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9398b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
9405cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
941e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
942e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky                                   llvm::Attribute::NoUnwind);
9435cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9445cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallstatic llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
9469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       llvm::PointerType *GuardPtrTy) {
9475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // void __cxa_guard_abort(__guard *guard_object);
9482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::FunctionType *FTy =
9498b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
9505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
951e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
952e76872e81ea32fbc863d8d7ef6eadc91a8f8673bNick Lewycky                                   llvm::Attribute::NoUnwind);
9535cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9545cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9555cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCallnamespace {
9565cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  struct CallGuardAbort : EHScopeStack::Cleanup {
9575cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::GlobalVariable *Guard;
9580f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
9595cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
960ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall    void Emit(CodeGenFunction &CGF, Flags flags) {
9615cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall      CGF.Builder.CreateCall(getGuardAbortFn(CGF.CGM, Guard->getType()), Guard)
9625cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall        ->setDoesNotThrow();
9635cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    }
9645cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  };
9655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
9665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
9675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// The ARM code here follows the Itanium code closely enough that we
9685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall/// just special-case it at particular places.
9693030eb82593097502469a8b3fc26112c79c75605John McCallvoid ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
9703030eb82593097502469a8b3fc26112c79c75605John McCall                                    const VarDecl &D,
971355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    llvm::GlobalVariable *var,
972355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                    bool shouldPerformInit) {
9735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGBuilderTy &Builder = CGF.Builder;
9743030eb82593097502469a8b3fc26112c79c75605John McCall
9753030eb82593097502469a8b3fc26112c79c75605John McCall  // We only need to use thread-safe statics for local variables;
9763030eb82593097502469a8b3fc26112c79c75605John McCall  // global initialization is always single-threaded.
9770502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  bool threadsafe =
9784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    (getContext().getLangOpts().ThreadsafeStatics && D.isLocalVarDecl());
979173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson
980173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // If we have a global variable with internal linkage and thread-safe statics
981173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  // are disabled, we can just let the guard variable be of type i8.
982355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
983355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
984355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::IntegerType *guardTy;
9850502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (useInt8GuardVariable) {
986355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guardTy = CGF.Int8Ty;
9870502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  } else {
988173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson    // Guard variables are 64 bits in the generic ABI and 32 bits on ARM.
989355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guardTy = (IsARM ? CGF.Int32Ty : CGF.Int64Ty);
990355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  }
991355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
992355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
993355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // Create the guard variable if we don't already have it (as we
994355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  // might if we're double-emitting this function body).
995355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
996355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  if (!guard) {
997355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Mangle the name for the guard.
998355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    SmallString<256> guardName;
999355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    {
1000355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      llvm::raw_svector_ostream out(guardName);
1001355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      getMangleContext().mangleItaniumGuardVariable(&D, out);
1002355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      out.flush();
1003355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    }
1004355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1005355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Create the guard variable with a zero-initializer.
1006355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    // Just absorb linkage and visibility from the guarded variable.
1007355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1008355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     false, var->getLinkage(),
1009355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     llvm::ConstantInt::get(guardTy, 0),
1010355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall                                     guardName.str());
1011355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    guard->setVisibility(var->getVisibility());
1012355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall
1013355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGM.setStaticLocalDeclGuardAddress(&D, guard);
1014173d51286bcaff4b6b76eebf6542d3b1311142e2Anders Carlsson  }
10155cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10165cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Test whether the variable has completed initialization.
1017355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  llvm::Value *isInitialized;
10185cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10195cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // ARM C++ ABI 3.2.3.1:
10205cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   To support the potential use of initialization guard variables
10215cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   as semaphores that are the target of ARM SWP and LDREX/STREX
10225cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   synchronizing instructions we define a static initialization
10235cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   guard variable to be a 4-byte aligned, 4- byte word with the
10245cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   following inline access protocol.
10255cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     #define INITIALIZED 1
10265cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
10275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if (__cxa_guard_acquire(&obj_guard))
10285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ...
10295cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
10300502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (IsARM && !useInt8GuardVariable) {
1031355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    llvm::Value *V = Builder.CreateLoad(guard);
10325cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    V = Builder.CreateAnd(V, Builder.getInt32(1));
1033355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
10345cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10355cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Itanium C++ ABI 3.3.2:
10365cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //   The following is pseudo-code showing how these functions can be used:
10375cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     if (obj_guard.first_byte == 0) {
10385cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       if ( __cxa_guard_acquire (&obj_guard) ) {
10395cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         try {
10405cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //           ... initialize the object ...;
10415cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         } catch (...) {
10425cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            __cxa_guard_abort (&obj_guard);
10435cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //            throw;
10445cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         }
10455cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         ... queue object destructor with __cxa_atexit() ...;
10465cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //         __cxa_guard_release (&obj_guard);
10475cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //       }
10485cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  //     }
10495cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
10505cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Load the first byte of the guard variable.
10510f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    llvm::LoadInst *LI =
1052355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
10530f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth    LI->setAlignment(1);
1054eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1055eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // Itanium ABI:
1056eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   An implementation supporting thread-safety on multiprocessor
1057eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   systems must also guarantee that references to the initialized
1058eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //   object do not occur before the load of the initialization flag.
1059eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    //
1060eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    // In LLVM, we do this by marking the load Acquire.
1061eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman    if (threadsafe)
10620f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth      LI->setAtomic(llvm::Acquire);
1063eb43f4a8f133c2bc510ae136a556e92b68a6ff44Eli Friedman
1064355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
10655cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
10665cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
10685cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
10695cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10705cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Check if the first byte of the guard variable is zero.
1071355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
10725cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10735cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(InitCheckBlock);
10745cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10755cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Variables used when coping with thread-safe statics and exceptions.
10760502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
10775cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_acquire.
10785cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::Value *V
1079355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall      = Builder.CreateCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
10805cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10815cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
10825cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10835cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
10845cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall                         InitBlock, EndBlock);
10855cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10865cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_abort along the exceptional edge.
1087355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
10885cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10895cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.EmitBlock(InitBlock);
10905cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
10915cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10925cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  // Emit the initializer and add a global destructor if appropriate.
1093355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall  CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
10945cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10950502a224984a26087ea4d64e8e5d2dd4dca432f6John McCall  if (threadsafe) {
10965cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Pop the guard-abort cleanup if we pushed one.
10975cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    CGF.PopCleanupBlock();
10985cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
10995cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall    // Call __cxa_guard_release.  This cannot throw.
1100355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    Builder.CreateCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
11015cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  } else {
1102355bba72ca52c4a70ca3c3802412c03a6ec31f24John McCall    Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
11035cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  }
11045cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
11055cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall  CGF.EmitBlock(EndBlock);
11065cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall}
110720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
110820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor using __cxa_atexit.
110920bb175cb8ae5844034828db094fb948c0e3454aJohn McCallstatic void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
111020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                        llvm::Constant *dtor,
111120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                        llvm::Constant *addr) {
111220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // We're assuming that the destructor function is something we can
111320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // reasonably call with the default CC.  Go ahead and cast it to the
111420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // right prototype.
111520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *dtorTy =
111620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
111720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
111820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
111920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
112020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::FunctionType *atexitTy =
112120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::FunctionType::get(CGF.IntTy, paramTys, false);
112220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
112320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Fetch the actual function.
112420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Constant *atexit =
112520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    CGF.CGM.CreateRuntimeFunction(atexitTy, "__cxa_atexit");
112620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
112720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    fn->setDoesNotThrow();
112820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
112920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Create a variable that binds the atexit to this shared object.
113020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Constant *handle =
113120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
113220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
113320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  llvm::Value *args[] = {
113420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(dtor, dtorTy),
113520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
113620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    handle
113720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  };
113820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  CGF.Builder.CreateCall(atexit, args)->setDoesNotThrow();
113920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
114020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
114120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall/// Register a global destructor as best as we know how.
114220bb175cb8ae5844034828db094fb948c0e3454aJohn McCallvoid ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
114320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *dtor,
114420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall                                       llvm::Constant *addr) {
114520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // Use __cxa_atexit if available.
114620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (CGM.getCodeGenOpts().CXAAtExit) {
114720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr);
114820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  }
114920bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
115020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // In Apple kexts, we want to add a global destructor entry.
115120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  // FIXME: shouldn't this be guarded by some variable?
115220bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  if (CGM.getContext().getLangOpts().AppleKext) {
115320bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    // Generate a global destructor entry.
115420bb175cb8ae5844034828db094fb948c0e3454aJohn McCall    return CGM.AddCXXDtorEntry(dtor, addr);
115520bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  }
115620bb175cb8ae5844034828db094fb948c0e3454aJohn McCall
115720bb175cb8ae5844034828db094fb948c0e3454aJohn McCall  CGF.registerGlobalDtorWithAtExit(dtor, addr);
115820bb175cb8ae5844034828db094fb948c0e3454aJohn McCall}
11599ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis
11609ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis/// Generate and emit virtual tables for the given class.
11619ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davisvoid ItaniumCXXABI::EmitVTables(const CXXRecordDecl *Class) {
11629ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis  CGM.getVTables().GenerateClassData(CGM.getVTableLinkage(Class), Class);
11639ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis}
1164