ItaniumCXXABI.cpp revision 9cb2cee212d708220c52249ceac4cdd9f2b8aeb0
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//
103a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// This provides C++ code generation targetting 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"
2393d557bc1867b7d7b102f87290194b4be7932c92John McCall#include "CodeGenFunction.h"
243a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CodeGenModule.h"
253a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "Mangle.h"
2693d557bc1867b7d7b102f87290194b4be7932c92John McCall#include <clang/AST/Type.h>
270bab0cdab751248ca389a5592bcb70eac5d39260John McCall#include <llvm/Target/TargetData.h>
2893d557bc1867b7d7b102f87290194b4be7932c92John McCall#include <llvm/Value.h>
293a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
303a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisusing namespace clang;
3193d557bc1867b7d7b102f87290194b4be7932c92John McCallusing namespace CodeGen;
323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
333a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisnamespace {
34071cc7deffad608165b1ddd5263e8bf181861520Charles Davisclass ItaniumCXXABI : public CodeGen::CGCXXABI {
350bab0cdab751248ca389a5592bcb70eac5d39260John McCallprivate:
360bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::IntegerType *PtrDiffTy;
3793d557bc1867b7d7b102f87290194b4be7932c92John McCallprotected:
383a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  CodeGen::MangleContext MangleCtx;
39babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  bool IsARM;
400bab0cdab751248ca389a5592bcb70eac5d39260John McCall
410bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // It's a little silly for us to cache this.
420bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::IntegerType *getPtrDiffTy() {
430bab0cdab751248ca389a5592bcb70eac5d39260John McCall    if (!PtrDiffTy) {
449cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall      QualType T = getContext().getPointerDiffType();
450bab0cdab751248ca389a5592bcb70eac5d39260John McCall      const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
460bab0cdab751248ca389a5592bcb70eac5d39260John McCall      PtrDiffTy = cast<llvm::IntegerType>(Ty);
470bab0cdab751248ca389a5592bcb70eac5d39260John McCall    }
480bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return PtrDiffTy;
490bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
500bab0cdab751248ca389a5592bcb70eac5d39260John McCall
511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  bool NeedsArrayCookie(QualType ElementType);
521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
533a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davispublic:
54babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
559cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(getContext(), CGM.getDiags()),
560bab0cdab751248ca389a5592bcb70eac5d39260John McCall    IsARM(IsARM) { }
573a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
583a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  CodeGen::MangleContext &getMangleContext() {
593a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis    return MangleCtx;
603a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  }
6193d557bc1867b7d7b102f87290194b4be7932c92John McCall
62f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  bool isZeroInitializable(const MemberPointerType *MPT);
63cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
640bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
650bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6693d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
6793d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
6893d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
6993d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT);
703023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
716c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
726c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *Base,
736c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            llvm::Value *MemPtr,
746c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                            const MemberPointerType *MPT);
756c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
760bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
770bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
780bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *Src);
79cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
800bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
810bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                              const CastExpr *E);
82cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
830bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
84cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
850bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
860bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
87875ab10245d3bf37252dd822aa1616bb0a391095John McCall
880bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
890bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
900bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
910bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
920bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality);
93e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
940bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
950bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *Addr,
960bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT);
974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 llvm::SmallVectorImpl<CanQualType> &ArgTys);
1024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys);
1074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits GetArrayCookieSize(QualType ElementType);
1151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
1191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
1201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                       QualType ElementType, llvm::Value *&NumElements,
1211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                       llvm::Value *&AllocPtr, CharUnits &CookieSize);
1223a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis};
123ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
124ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallclass ARMCXXABI : public ItaniumCXXABI {
125ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallpublic:
126babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
1274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
1294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CXXCtorType T,
1304c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 CanQualType &ResTy,
1314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                 llvm::SmallVectorImpl<CanQualType> &ArgTys);
1324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
1344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CXXDtorType T,
1354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                CanQualType &ResTy,
1364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys);
1374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
1394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   QualType &ResTy,
1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                   FunctionArgList &Params);
1414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
1434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1461e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits GetArrayCookieSize(QualType ElementType);
1471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
1481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NewPtr,
1491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     llvm::Value *NumElements,
1501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                     QualType ElementType);
1511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  void ReadArrayCookie(CodeGenFunction &CGF, llvm::Value *Ptr,
1521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                       QualType ElementType, llvm::Value *&NumElements,
1531e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                       llvm::Value *&AllocPtr, CharUnits &CookieSize);
1544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallprivate:
1564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// \brief Returns true if the given instance method is one of the
1574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// kinds that the ARM ABI says returns 'this'.
1584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  static bool HasThisReturn(GlobalDecl GD) {
1594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
1614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall            (isa<CXXConstructorDecl>(MD)));
1624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
163ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall};
1643a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1653a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
166071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
1673a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis  return new ItaniumCXXABI(CGM);
1683a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis}
1693a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis
170ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallCodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
171ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return new ARMCXXABI(CGM);
172ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall}
173ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
1740bab0cdab751248ca389a5592bcb70eac5d39260John McCallconst llvm::Type *
1750bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
1760bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
1770bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return getPtrDiffTy();
1780bab0cdab751248ca389a5592bcb70eac5d39260John McCall  else
1790bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return llvm::StructType::get(CGM.getLLVMContext(),
1800bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                 getPtrDiffTy(), getPtrDiffTy(), NULL);
181875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
182875ab10245d3bf37252dd822aa1616bb0a391095John McCall
183babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium and ARM ABIs, method pointers have the form:
184babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
185babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
186babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the Itanium ABI:
187babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
188babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj)
189babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr - 1)
190babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
191babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// In the ARM ABI:
192babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - method pointers are virtual if (memptr.adj & 1) is nonzero
193babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the this-adjustment is (memptr.adj >> 1)
194babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///  - the virtual offset is (memptr.ptr)
195babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// ARM uses 'adj' for the virtual flag because Thumb functions
196babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// may be only single-byte aligned.
197babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
198babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is virtual, the adjusted 'this' pointer points
199babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// to a vtable pointer from which the virtual offset is applied.
200babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall///
201babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// If the member is non-virtual, memptr.ptr is the address of
202babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall/// the function to call.
20393d557bc1867b7d7b102f87290194b4be7932c92John McCallllvm::Value *
20493d557bc1867b7d7b102f87290194b4be7932c92John McCallItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
20593d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *&This,
20693d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               llvm::Value *MemFnPtr,
20793d557bc1867b7d7b102f87290194b4be7932c92John McCall                                               const MemberPointerType *MPT) {
20893d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGBuilderTy &Builder = CGF.Builder;
20993d557bc1867b7d7b102f87290194b4be7932c92John McCall
21093d557bc1867b7d7b102f87290194b4be7932c92John McCall  const FunctionProtoType *FPT =
21193d557bc1867b7d7b102f87290194b4be7932c92John McCall    MPT->getPointeeType()->getAs<FunctionProtoType>();
21293d557bc1867b7d7b102f87290194b4be7932c92John McCall  const CXXRecordDecl *RD =
21393d557bc1867b7d7b102f87290194b4be7932c92John McCall    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
21493d557bc1867b7d7b102f87290194b4be7932c92John McCall
21593d557bc1867b7d7b102f87290194b4be7932c92John McCall  const llvm::FunctionType *FTy =
21693d557bc1867b7d7b102f87290194b4be7932c92John McCall    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
21793d557bc1867b7d7b102f87290194b4be7932c92John McCall                                   FPT->isVariadic());
21893d557bc1867b7d7b102f87290194b4be7932c92John McCall
2190bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::IntegerType *ptrdiff = getPtrDiffTy();
220babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
22193d557bc1867b7d7b102f87290194b4be7932c92John McCall
222babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
223babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
224babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
225babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
226d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Extract memptr.adj, which is in the second field.
227d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
228babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall
229babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // Compute the true adjustment.
230babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Adj = RawAdj;
231babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
232babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
23393d557bc1867b7d7b102f87290194b4be7932c92John McCall
23493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the adjustment and cast back to the original struct type
23593d557bc1867b7d7b102f87290194b4be7932c92John McCall  // for consistency.
236babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
237babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
238babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
23993d557bc1867b7d7b102f87290194b4be7932c92John McCall
24093d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the function pointer.
241d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
24293d557bc1867b7d7b102f87290194b4be7932c92John McCall
24393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // If the LSB in the function pointer is 1, the function pointer points to
24493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // a virtual function.
245babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *IsVirtual;
246babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (IsARM)
247babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
248babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  else
249babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
250babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
25193d557bc1867b7d7b102f87290194b4be7932c92John McCall  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
25293d557bc1867b7d7b102f87290194b4be7932c92John McCall
25393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the virtual path, the adjustment left 'This' pointing to the
25493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // vtable of the correct base subobject.  The "function pointer" is an
255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  // offset within the vtable (+1 for the virtual flag on non-ARM).
25693d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnVirtual);
25793d557bc1867b7d7b102f87290194b4be7932c92John McCall
25893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Cast the adjusted this to a pointer to vtable pointer and load.
25993d557bc1867b7d7b102f87290194b4be7932c92John McCall  const llvm::Type *VTableTy = Builder.getInt8PtrTy();
26093d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
261babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
26293d557bc1867b7d7b102f87290194b4be7932c92John McCall
26393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Apply the offset.
264babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VTableOffset = FnAsInt;
265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
266babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  VTable = Builder.CreateGEP(VTable, VTableOffset);
26793d557bc1867b7d7b102f87290194b4be7932c92John McCall
26893d557bc1867b7d7b102f87290194b4be7932c92John McCall  // Load the virtual function to call.
26993d557bc1867b7d7b102f87290194b4be7932c92John McCall  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
270babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
27193d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBranch(FnEnd);
27293d557bc1867b7d7b102f87290194b4be7932c92John McCall
27393d557bc1867b7d7b102f87290194b4be7932c92John McCall  // In the non-virtual path, the function pointer is actually a
27493d557bc1867b7d7b102f87290194b4be7932c92John McCall  // function pointer.
27593d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnNonVirtual);
27693d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::Value *NonVirtualFn =
277babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
27893d557bc1867b7d7b102f87290194b4be7932c92John McCall
27993d557bc1867b7d7b102f87290194b4be7932c92John McCall  // We're done.
28093d557bc1867b7d7b102f87290194b4be7932c92John McCall  CGF.EmitBlock(FnEnd);
28193d557bc1867b7d7b102f87290194b4be7932c92John McCall  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
28293d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->reserveOperandSpace(2);
28393d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(VirtualFn, FnVirtual);
28493d557bc1867b7d7b102f87290194b4be7932c92John McCall  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
28593d557bc1867b7d7b102f87290194b4be7932c92John McCall  return Callee;
28693d557bc1867b7d7b102f87290194b4be7932c92John McCall}
2873023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
2886c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// Compute an l-value by applying the given pointer-to-member to a
2896c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall/// base object.
2906c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCallllvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
2916c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *Base,
2926c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                                         llvm::Value *MemPtr,
2936c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall                                           const MemberPointerType *MPT) {
2946c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  assert(MemPtr->getType() == getPtrDiffTy());
2956c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
2966c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  CGBuilderTy &Builder = CGF.Builder;
2976c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
2986c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  unsigned AS = cast<llvm::PointerType>(Base->getType())->getAddressSpace();
2996c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3006c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast to char*.
3016c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
3026c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3036c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Apply the offset, which we assume is non-null.
3046c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
3056c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3066c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // Cast the address to the appropriate pointer type, adopting the
3076c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  // address space of the base pointer.
3086c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  const llvm::Type *PType
3096c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall    = CGF.ConvertType(MPT->getPointeeType())->getPointerTo(AS);
3106c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall  return Builder.CreateBitCast(Addr, PType);
3116c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall}
3126c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall
3133023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall/// Perform a derived-to-base or base-to-derived member pointer conversion.
3140bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3150bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// Obligatory offset/adjustment diagram:
3160bab0cdab751248ca389a5592bcb70eac5d39260John McCall///         <-- offset -->          <-- adjustment -->
3170bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   |--------------------------|----------------------|--------------------|
3180bab0cdab751248ca389a5592bcb70eac5d39260John McCall///   ^Derived address point     ^Base address point    ^Member address point
3190bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3200bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// So when converting a base member pointer to a derived member pointer,
3210bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we add the offset to the adjustment because the address point has
3220bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// decreased;  and conversely, when converting a derived MP to a base MP
3230bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// we subtract the offset from the adjustment because the address point
3240bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// has increased.
3250bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3260bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at compile time) conversion to and from
3270bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// virtual bases, which is why we don't have to consider them here.
3280bab0cdab751248ca389a5592bcb70eac5d39260John McCall///
3290bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// The standard forbids (at run time) casting a derived MP to a base
3300bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// MP when the derived MP does not point to a member of the base.
3310bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// This is why -1 is a reasonable choice for null data member
3320bab0cdab751248ca389a5592bcb70eac5d39260John McCall/// pointers.
333d608cdb7c044365cf4e8764ade1e11e99c176078John McCallllvm::Value *
3340bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3350bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E,
3360bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *Src) {
3372de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3382de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall         E->getCastKind() == CK_BaseToDerivedMemberPointer);
3393023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
340d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isa<llvm::Constant>(Src))
3410bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
342d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
3433023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  CGBuilderTy &Builder = CGF.Builder;
3443023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3453023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  const MemberPointerType *SrcTy =
3463023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall    E->getSubExpr()->getType()->getAs<MemberPointerType>();
3473023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
3483023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3493023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
3503023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
3513023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3523023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  bool DerivedToBase =
3532de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    E->getCastKind() == CK_DerivedToBaseMemberPointer;
3543023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
3553023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  const CXXRecordDecl *BaseDecl, *DerivedDecl;
3563023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  if (DerivedToBase)
3573023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall    DerivedDecl = SrcDecl, BaseDecl = DestDecl;
3583023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall  else
3593023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall    BaseDecl = SrcDecl, DerivedDecl = DestDecl;
3603023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall
361d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Adj =
362d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
363d608cdb7c044365cf4e8764ade1e11e99c176078John McCall                                         E->path_begin(),
364d608cdb7c044365cf4e8764ade1e11e99c176078John McCall                                         E->path_end());
365d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!Adj) return Src;
366875ab10245d3bf37252dd822aa1616bb0a391095John McCall
3670bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member data pointers, this is just a matter of adding the
3680bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // offset if the source is non-null.
3690bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (SrcTy->isMemberDataPointer()) {
3700bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *Dst;
3710bab0cdab751248ca389a5592bcb70eac5d39260John McCall    if (DerivedToBase)
3720bab0cdab751248ca389a5592bcb70eac5d39260John McCall      Dst = Builder.CreateNSWSub(Src, Adj, "adj");
3730bab0cdab751248ca389a5592bcb70eac5d39260John McCall    else
3740bab0cdab751248ca389a5592bcb70eac5d39260John McCall      Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
3750bab0cdab751248ca389a5592bcb70eac5d39260John McCall
3760bab0cdab751248ca389a5592bcb70eac5d39260John McCall    // Null check.
3770bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
3780bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
3790bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateSelect(IsNull, Src, Dst);
3800bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
3810bab0cdab751248ca389a5592bcb70eac5d39260John McCall
382d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // The this-adjustment is left-shifted by 1 on ARM.
383d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (IsARM) {
384d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
385d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    Offset <<= 1;
386d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
387d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
388d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
389e14add4a275318e7a9cafd3a01f79fb15a5a08bcJohn McCall  llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
390d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *DstAdj;
391d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (DerivedToBase)
3920bab0cdab751248ca389a5592bcb70eac5d39260John McCall    DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
393d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  else
3940bab0cdab751248ca389a5592bcb70eac5d39260John McCall    DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
395d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
396e14add4a275318e7a9cafd3a01f79fb15a5a08bcJohn McCall  return Builder.CreateInsertValue(Src, DstAdj, 1);
3973023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall}
398cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
399cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant *
4000bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
4010bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const CastExpr *E) {
402cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  const MemberPointerType *SrcTy =
403cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall    E->getSubExpr()->getType()->getAs<MemberPointerType>();
404cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  const MemberPointerType *DestTy =
405cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall    E->getType()->getAs<MemberPointerType>();
406cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
407cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  bool DerivedToBase =
4082de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall    E->getCastKind() == CK_DerivedToBaseMemberPointer;
409cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
410cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  const CXXRecordDecl *DerivedDecl;
411cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  if (DerivedToBase)
412cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall    DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
413cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  else
414cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall    DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
415cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
416cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  // Calculate the offset to the base class.
417cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  llvm::Constant *Offset =
418cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall    CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
419cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall                                     E->path_begin(),
420cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall                                     E->path_end());
421cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  // If there's no offset, we're done.
422cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  if (!Offset) return C;
423cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
4240bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // If the source is a member data pointer, we have to do a null
4250bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // check and then add the offset.  In the common case, we can fold
4260bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // away the offset.
4270bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (SrcTy->isMemberDataPointer()) {
4280bab0cdab751248ca389a5592bcb70eac5d39260John McCall    assert(C->getType() == getPtrDiffTy());
4290bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4300bab0cdab751248ca389a5592bcb70eac5d39260John McCall    // If it's a constant int, just create a new constant int.
4310bab0cdab751248ca389a5592bcb70eac5d39260John McCall    if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
4320bab0cdab751248ca389a5592bcb70eac5d39260John McCall      int64_t Src = CI->getSExtValue();
4330bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4340bab0cdab751248ca389a5592bcb70eac5d39260John McCall      // Null converts to null.
4350bab0cdab751248ca389a5592bcb70eac5d39260John McCall      if (Src == -1) return CI;
4360bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4370bab0cdab751248ca389a5592bcb70eac5d39260John McCall      // Otherwise, just add the offset.
4380bab0cdab751248ca389a5592bcb70eac5d39260John McCall      int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
4390bab0cdab751248ca389a5592bcb70eac5d39260John McCall      int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
4400bab0cdab751248ca389a5592bcb70eac5d39260John McCall      return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
4410bab0cdab751248ca389a5592bcb70eac5d39260John McCall    }
4420bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4430bab0cdab751248ca389a5592bcb70eac5d39260John McCall    // Otherwise, we have to form a constant select expression.
4440bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
4450bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4460bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Constant *IsNull =
4470bab0cdab751248ca389a5592bcb70eac5d39260John McCall      llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
4480bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4490bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Constant *Dst;
4500bab0cdab751248ca389a5592bcb70eac5d39260John McCall    if (DerivedToBase)
4510bab0cdab751248ca389a5592bcb70eac5d39260John McCall      Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
4520bab0cdab751248ca389a5592bcb70eac5d39260John McCall    else
4530bab0cdab751248ca389a5592bcb70eac5d39260John McCall      Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
4540bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4550bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
4560bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
4570bab0cdab751248ca389a5592bcb70eac5d39260John McCall
458875ab10245d3bf37252dd822aa1616bb0a391095John McCall  // The this-adjustment is left-shifted by 1 on ARM.
459875ab10245d3bf37252dd822aa1616bb0a391095John McCall  if (IsARM) {
4600bab0cdab751248ca389a5592bcb70eac5d39260John McCall    int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
461875ab10245d3bf37252dd822aa1616bb0a391095John McCall    OffsetV <<= 1;
462875ab10245d3bf37252dd822aa1616bb0a391095John McCall    Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
463875ab10245d3bf37252dd822aa1616bb0a391095John McCall  }
464875ab10245d3bf37252dd822aa1616bb0a391095John McCall
465cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
466cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
4670bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
4680bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (DerivedToBase)
4690bab0cdab751248ca389a5592bcb70eac5d39260John McCall    Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
4700bab0cdab751248ca389a5592bcb70eac5d39260John McCall  else
4710bab0cdab751248ca389a5592bcb70eac5d39260John McCall    Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
4720bab0cdab751248ca389a5592bcb70eac5d39260John McCall
473cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall  return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
474cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall                                   /*Packed=*/false);
475cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
476cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
477cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
478cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCallllvm::Constant *
4790bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
4800bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::Type *ptrdiff_t = getPtrDiffTy();
4810bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4820bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4830bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A NULL pointer is represented as -1.
4840bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
4850bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
486d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
487d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
488d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *Values[2] = { Zero, Zero };
489d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
490d608cdb7c044365cf4e8764ade1e11e99c176078John McCall                                   /*Packed=*/false);
491cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
492cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall
4930bab0cdab751248ca389a5592bcb70eac5d39260John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
4940bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Itanium C++ ABI 2.3:
4950bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   A pointer to data member is an offset from the base address of
4960bab0cdab751248ca389a5592bcb70eac5d39260John McCall  //   the class object containing it, represented as a ptrdiff_t
4970bab0cdab751248ca389a5592bcb70eac5d39260John McCall
4989cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  QualType ClassType = getContext().getTypeDeclType(FD->getParent());
4990bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::StructType *ClassLTy =
5000bab0cdab751248ca389a5592bcb70eac5d39260John McCall    cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType));
5010bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5020bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
5030bab0cdab751248ca389a5592bcb70eac5d39260John McCall  unsigned FieldNo = RL.getLLVMFieldNo(FD);
5040bab0cdab751248ca389a5592bcb70eac5d39260John McCall  uint64_t Offset =
5050bab0cdab751248ca389a5592bcb70eac5d39260John McCall    CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
5060bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5070bab0cdab751248ca389a5592bcb70eac5d39260John McCall  return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
5080bab0cdab751248ca389a5592bcb70eac5d39260John McCall}
5090bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5100bab0cdab751248ca389a5592bcb70eac5d39260John McCallllvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
511d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  assert(MD->isInstance() && "Member function must not be static!");
512d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  MD = MD->getCanonicalDecl();
513875ab10245d3bf37252dd822aa1616bb0a391095John McCall
514d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  CodeGenTypes &Types = CGM.getTypes();
5150bab0cdab751248ca389a5592bcb70eac5d39260John McCall  const llvm::Type *ptrdiff_t = getPtrDiffTy();
516875ab10245d3bf37252dd822aa1616bb0a391095John McCall
517d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  // Get the function pointer (or index if this is a virtual function).
518d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Constant *MemPtr[2];
519d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (MD->isVirtual()) {
520d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
521875ab10245d3bf37252dd822aa1616bb0a391095John McCall
522d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    // FIXME: We shouldn't use / 8 here.
523d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    uint64_t PointerWidthInBytes =
5249cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall      getContext().Target.getPointerWidth(0) / 8;
525d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    uint64_t VTableOffset = (Index * PointerWidthInBytes);
526d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
527d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (IsARM) {
528d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // ARM C++ ABI 3.2.1:
529d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   This ABI specifies that adj contains twice the this
530d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   adjustment, plus 1 if the member function is virtual. The
531d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   least significant bit of adj then makes exactly the same
532d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   discrimination as the least significant bit of ptr does for
533d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   Itanium.
534d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
535d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
536d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
537d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // Itanium C++ ABI 2.3:
538d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   For a virtual function, [the pointer field] is 1 plus the
539d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   virtual table offset (in bytes) of the function,
540d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      //   represented as a ptrdiff_t.
541d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
542d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
543d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
544d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  } else {
545d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
546d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    const llvm::Type *Ty;
547d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    // Check whether the function has a computable LLVM signature.
548d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
549d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // The function has a computable LLVM signature; use the correct type.
550d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
551d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    } else {
552d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
553d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      // function type is incomplete.
554d608cdb7c044365cf4e8764ade1e11e99c176078John McCall      Ty = ptrdiff_t;
555d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    }
556d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
557d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
558d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
559d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
560d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  }
561d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
562d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  return llvm::ConstantStruct::get(CGM.getLLVMContext(),
563d608cdb7c044365cf4e8764ade1e11e99c176078John McCall                                   MemPtr, 2, /*Packed=*/false);
564875ab10245d3bf37252dd822aa1616bb0a391095John McCall}
565875ab10245d3bf37252dd822aa1616bb0a391095John McCall
566e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// The comparison algorithm is pretty easy: the member pointers are
567e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// the same if they're either bitwise identical *or* both null.
568e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall///
569e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall/// ARM is different here only because null-ness is more complicated.
570e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
5710bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
5720bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *L,
5730bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           llvm::Value *R,
5740bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           const MemberPointerType *MPT,
5750bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                           bool Inequality) {
576e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::ICmpInst::Predicate Eq;
579e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Instruction::BinaryOps And, Or;
580e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (Inequality) {
581e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_NE;
582e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::Or;
583e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::And;
584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  } else {
585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Eq = llvm::ICmpInst::ICMP_EQ;
586e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    And = llvm::Instruction::And;
587e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Or = llvm::Instruction::Or;
588e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
589e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
5900bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // Member data pointers are easy because there's a unique null
5910bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // value, so it just comes down to bitwise equality.
5920bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer())
5930bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmp(Eq, L, R);
5940bab0cdab751248ca389a5592bcb70eac5d39260John McCall
5950bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // For member function pointers, the tautologies are more complex.
5960bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The Itanium tautology is:
597de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
5980bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The ARM tautology is:
599de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //   (L == R) <==> (L.ptr == R.ptr &&
600de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                  (L.adj == R.adj ||
601de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall  //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
6020bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // The inequality tautologies have exactly the same structure, except
6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall  // applying De Morgan's laws.
6040bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6050bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
6060bab0cdab751248ca389a5592bcb70eac5d39260John McCall  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
6070bab0cdab751248ca389a5592bcb70eac5d39260John McCall
608e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.ptr == R.ptr.  This must always be
609e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true for equality to hold.
610e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
611e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
612e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition, together with the assumption that L.ptr == R.ptr,
613e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // tests whether the pointers are both null.  ARM imposes an extra
614e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // condition.
615e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // This condition tests whether L.adj == R.adj.  If this isn't
619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // true, the pointers are unequal unless they're both null.
620d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
621d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Null member function pointers on ARM clear the low bit of Adj,
625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // so the zero condition has to check that neither low bit is set.
626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
628e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
629e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    // Compute (l.adj | r.adj) & 1 and test it against zero.
630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                                                      "cmp.or.adj");
634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // Tie together all our conditions.
638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  Result = Builder.CreateBinOp(And, PtrEq, Result,
640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                               Inequality ? "memptr.ne" : "memptr.eq");
641e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
642e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
643e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCallllvm::Value *
6450bab0cdab751248ca389a5592bcb70eac5d39260John McCallItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
6460bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          llvm::Value *MemPtr,
6470bab0cdab751248ca389a5592bcb70eac5d39260John McCall                                          const MemberPointerType *MPT) {
648e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  CGBuilderTy &Builder = CGF.Builder;
6490bab0cdab751248ca389a5592bcb70eac5d39260John McCall
6500bab0cdab751248ca389a5592bcb70eac5d39260John McCall  /// For member data pointers, this is just a check against -1.
6510bab0cdab751248ca389a5592bcb70eac5d39260John McCall  if (MPT->isMemberDataPointer()) {
6520bab0cdab751248ca389a5592bcb70eac5d39260John McCall    assert(MemPtr->getType() == getPtrDiffTy());
6530bab0cdab751248ca389a5592bcb70eac5d39260John McCall    llvm::Value *NegativeOne =
6540bab0cdab751248ca389a5592bcb70eac5d39260John McCall      llvm::Constant::getAllOnesValue(MemPtr->getType());
6550bab0cdab751248ca389a5592bcb70eac5d39260John McCall    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
6560bab0cdab751248ca389a5592bcb70eac5d39260John McCall  }
657e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
658e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // In Itanium, a member function pointer is null if 'ptr' is null.
659d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
660e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
661e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
662e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
663e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
664e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  // In ARM, it's that, plus the low bit of 'adj' must be zero.
665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  if (IsARM) {
666e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
667d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall                                                     "memptr.notvirtual");
671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall    Result = Builder.CreateAnd(Result, IsNotVirtual);
672e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  }
673e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall
674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall  return Result;
675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall}
676875ab10245d3bf37252dd822aa1616bb0a391095John McCall
677f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// The Itanium ABI requires non-zero initialization only for data
678f16aa103d3afd42fbca2ab346f191bf745cec092John McCall/// member pointers, for which '0' is a valid offset.
679f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
680f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return MPT->getPointeeType()->isFunctionType();
681cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall}
6824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's initializing a
6844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
6854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
6864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CXXCtorType Type,
6874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                              CanQualType &ResTy,
6884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys) {
6899cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
6904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // 'this' is already there.
6924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
6944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
6954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
6964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
6974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
6984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
6994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
7004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CXXCtorType Type,
7014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                          CanQualType &ResTy,
7024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys) {
7034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
7044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ResTy = ArgTys[0];
7054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The generic ABI passes 'this', plus a VTT if it's destroying a
7084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// base subobject.
7094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CXXDtorType Type,
7114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                             CanQualType &ResTy,
7124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys) {
7139cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Context = getContext();
7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // 'this' is already there.
7164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need to add a VTT parameter (which has type void **).
7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
7234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall/// for non-deleting destructors.
7244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
7254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CXXDtorType Type,
7264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                         CanQualType &ResTy,
7274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                llvm::SmallVectorImpl<CanQualType> &ArgTys) {
7284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
7294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7304c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (Type != Dtor_Deleting)
7314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ResTy = ArgTys[0];
7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                QualType &ResTy,
7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                                FunctionArgList &Params) {
7374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Create the 'this' variable.
7384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  BuildThisParam(CGF, Params);
7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
7414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(MD->isInstance());
7424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Check if we need a VTT parameter as well.
7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
7459cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    ASTContext &Context = getContext();
7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    // FIXME: avoid the fake decl
7484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    QualType T = Context.getPointerType(Context.VoidPtrTy);
7494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ImplicitParamDecl *VTTDecl
7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                  &Context.Idents.get("vtt"), T);
7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    Params.push_back(std::make_pair(VTTDecl, VTTDecl->getType()));
7534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTDecl(CGF) = VTTDecl;
7544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            QualType &ResTy,
7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                            FunctionArgList &Params) {
7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Return 'this' from certain constructors and destructors.
7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ResTy = Params[0].second;
7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'this' slot.
7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  EmitThisParam(CGF);
7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the 'vtt' slot if needed.
7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (getVTTDecl(CGF)) {
7734c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    getVTTValue(CGF)
7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                               "vtt");
7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
7804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// Initialize the return slot to 'this' at the start of the
7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  /// function.
7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (HasThisReturn(CGF.CurGD))
7854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    CGF.Builder.CreateStore(CGF.LoadCXXThis(), CGF.ReturnValue);
7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallvoid ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall                                    RValue RV, QualType ResultType) {
7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
7914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
7924c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Destructor thunks in the ARM ABI have indeterminate results.
7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const llvm::Type *T =
7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  RValue Undef = RValue::get(llvm::UndefValue::get(T));
7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
7984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall}
7991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall/************************** Array allocation cookies **************************/
8011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8021e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallbool ItaniumCXXABI::NeedsArrayCookie(QualType ElementType) {
8039cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ElementType = getContext().getBaseElementType(ElementType);
8041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const RecordType *RT = ElementType->getAs<RecordType>();
8051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!RT) return false;
8061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
8081e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8091e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // If the class has a non-trivial destructor, it always needs a cookie.
8101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!RD->hasTrivialDestructor()) return true;
8111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8121e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // If the class's usual deallocation function takes two arguments,
8131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // it needs a cookie.  Otherwise we don't need a cookie.
8141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const CXXMethodDecl *UsualDeallocationFunction = 0;
8151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Usual deallocation functions of this form are always found on the
8171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // class.
8181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //
8191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // FIXME: what exactly is this code supposed to do if there's an
8201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // ambiguity?  That's possible with using declarations.
8211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  DeclarationName OpName =
8229cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    getContext().DeclarationNames.getCXXOperatorName(OO_Array_Delete);
8231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  DeclContext::lookup_const_iterator Op, OpEnd;
8241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  for (llvm::tie(Op, OpEnd) = RD->lookup(OpName); Op != OpEnd; ++Op) {
8251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    const CXXMethodDecl *Delete =
8261e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall      cast<CXXMethodDecl>((*Op)->getUnderlyingDecl());
8271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    if (Delete->isUsualDeallocationFunction()) {
8291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall      UsualDeallocationFunction = Delete;
8301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall      break;
8311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    }
8321e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  }
8331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // No usual deallocation function, we don't need a cookie.
8351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!UsualDeallocationFunction)
8361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return false;
8371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The usual deallocation function doesn't take a size_t argument,
8391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // so we don't need a cookie.
8401e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (UsualDeallocationFunction->getNumParams() == 1)
8411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return false;
8421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  assert(UsualDeallocationFunction->getNumParams() == 2 &&
8441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall         "Unexpected deallocation function type!");
8451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return true;
8461e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallCharUnits ItaniumCXXABI::GetArrayCookieSize(QualType ElementType) {
8491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!NeedsArrayCookie(ElementType))
8501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return CharUnits::Zero();
8511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Padding is the maximum of sizeof(size_t) and alignof(ElementType)
8539cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
8541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
8551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                  Ctx.getTypeAlignInChars(ElementType));
8561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
8591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NewPtr,
8601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  llvm::Value *NumElements,
8611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                  QualType ElementType) {
8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  assert(NeedsArrayCookie(ElementType));
8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
8651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8669cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
8671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  QualType SizeTy = Ctx.getSizeType();
8681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
8691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The size of the cookie.
8711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize =
8721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
8731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8741e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Compute an offset to the cookie.
8751e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
8761e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieOffset = CookieSize - SizeSize;
8771e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!CookieOffset.isZero())
8781e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
8791e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                 CookieOffset.getQuantity());
8801e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Write the number of elements into the appropriate slot.
8821e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr
8831e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    = CGF.Builder.CreateBitCast(CookiePtr,
8841e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                CGF.ConvertType(SizeTy)->getPointerTo(AS));
8851e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, NumElementsPtr);
8861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8871e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
8881e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
8891e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
8921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
8931e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallvoid ItaniumCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                    llvm::Value *Ptr,
8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                    QualType ElementType,
8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                    llvm::Value *&NumElements,
8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                    llvm::Value *&AllocPtr,
8981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                    CharUnits &CookieSize) {
8991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Derive a char* in the same address space as the pointer.
9001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
9011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
9021e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // If we don't need an array cookie, bail out early.
9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!NeedsArrayCookie(ElementType)) {
9051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
9061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    NumElements = 0;
9071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookieSize = CharUnits::Zero();
9081e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return;
9091e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  }
9101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9119cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  QualType SizeTy = getContext().getSizeType();
9129cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
9131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
9141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookieSize
9169cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall    = std::max(SizeSize, getContext().getTypeAlignInChars(ElementType));
9171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits NumElementsOffset = CookieSize - SizeSize;
9191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Compute the allocated pointer.
9211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
9221e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
9231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                    -CookieSize.getQuantity());
9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9251e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr = AllocPtr;
9261e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!NumElementsOffset.isZero())
9271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    NumElementsPtr =
9281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall      CGF.Builder.CreateConstInBoundsGEP1_64(NumElementsPtr,
9291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                             NumElementsOffset.getQuantity());
9301e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  NumElementsPtr =
9311e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
9321e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
9331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallCharUnits ARMCXXABI::GetArrayCookieSize(QualType ElementType) {
9361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!NeedsArrayCookie(ElementType))
9371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return CharUnits::Zero();
9381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // On ARM, the cookie is always:
9401e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   struct array_cookie {
9411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_size; // element_size != 0
9421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //     std::size_t element_count;
9431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  //   };
9441e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // TODO: what should we do if the allocated type actually wants
9451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // greater alignment?
9461e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return getContext().getTypeSizeInChars(getContext().getSizeType()) * 2;
9471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallllvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
9501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NewPtr,
9511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              llvm::Value *NumElements,
9521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                              QualType ElementType) {
9531e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  assert(NeedsArrayCookie(ElementType));
9541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // NewPtr is a char*.
9561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(NewPtr->getType())->getAddressSpace();
9581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9599cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  ASTContext &Ctx = getContext();
9601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits SizeSize = Ctx.getTypeSizeInChars(Ctx.getSizeType());
9611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const llvm::IntegerType *SizeTy =
9621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    cast<llvm::IntegerType>(CGF.ConvertType(Ctx.getSizeType()));
9631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The cookie is always at the start of the buffer.
9651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *CookiePtr = NewPtr;
9661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The first element is the element size.
9681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateBitCast(CookiePtr, SizeTy->getPointerTo(AS));
9691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *ElementSize = llvm::ConstantInt::get(SizeTy,
9701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                          Ctx.getTypeSizeInChars(ElementType).getQuantity());
9711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(ElementSize, CookiePtr);
9721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The second element is the element count.
9741e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_32(CookiePtr, 1);
9751e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CGF.Builder.CreateStore(NumElements, CookiePtr);
9761e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9771e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Finally, compute a pointer to the actual data buffer by skipping
9781e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // over the cookie completely.
9791e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits CookieSize = 2 * SizeSize;
9801e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
9811e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                                CookieSize.getQuantity());
9821e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
9831e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9841e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallvoid ARMCXXABI::ReadArrayCookie(CodeGenFunction &CGF,
9851e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                llvm::Value *Ptr,
9861e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                QualType ElementType,
9871e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                llvm::Value *&NumElements,
9881e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                llvm::Value *&AllocPtr,
9891e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                CharUnits &CookieSize) {
9901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // Derive a char* in the same address space as the pointer.
9911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  unsigned AS = cast<llvm::PointerType>(Ptr->getType())->getAddressSpace();
9921e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const llvm::Type *CharPtrTy = CGF.Builder.getInt8Ty()->getPointerTo(AS);
9931e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
9941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // If we don't need an array cookie, bail out early.
9951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  if (!NeedsArrayCookie(ElementType)) {
9961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
9971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    NumElements = 0;
9981e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CookieSize = CharUnits::Zero();
9991e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    return;
10001e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  }
10011e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10029cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  QualType SizeTy = getContext().getSizeType();
10039cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall  CharUnits SizeSize = getContext().getTypeSizeInChars(SizeTy);
10041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  const llvm::Type *SizeLTy = CGF.ConvertType(SizeTy);
10051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10061e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The cookie size is always 2 * sizeof(size_t).
10071e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CookieSize = 2 * SizeSize;
10081e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CharUnits NumElementsOffset = CookieSize - SizeSize;
10091e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10101e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The allocated pointer is the input ptr, minus that amount.
10111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  AllocPtr = CGF.Builder.CreateBitCast(Ptr, CharPtrTy);
10121e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  AllocPtr = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
10131e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                               -CookieSize.getQuantity());
10141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
10151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  // The number of elements is at offset sizeof(size_t) relative to that.
10161e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  llvm::Value *NumElementsPtr
10171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    = CGF.Builder.CreateConstInBoundsGEP1_64(AllocPtr,
10181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall                                             SizeSize.getQuantity());
10191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  NumElementsPtr =
10201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall    CGF.Builder.CreateBitCast(NumElementsPtr, SizeLTy->getPointerTo(AS));
10211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  NumElements = CGF.Builder.CreateLoad(NumElementsPtr);
10221e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall}
10231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall
1024