ItaniumCXXABI.cpp revision b80a16eadd0dacabfc1c32412e243ccb99dd664d
1642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
2642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
3693dfad9c1b121cf079a3082866daa2225df1797Petr Machata//                     The LLVM Compiler Infrastructure
4642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
5642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// This file is distributed under the University of Illinois Open Source
6642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// License. See LICENSE.TXT for details.
7642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
8642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//===----------------------------------------------------------------------===//
9642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
10642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// This provides C++ code generation targeting the Itanium C++ ABI.  The class
11642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// in this file generates structures that follow the Itanium C++ ABI, which is
12642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// documented at:
13642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//  http://www.codesourcery.com/public/cxx-abi/abi.html
14642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//  http://www.codesourcery.com/public/cxx-abi/abi-eh.html
15642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
16642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// It also supports the closely-related ARM ABI, documented at:
17642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//
19642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata//===----------------------------------------------------------------------===//
20642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
21642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata#include "CGCXXABI.h"
22642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata#include "CGRecordLayout.h"
23642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata#include "CGVTables.h"
24642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata#include "CodeGenFunction.h"
25bac2da505ee174b7fb984b975c5938f88f0dbab2Petr Machata#include "CodeGenModule.h"
26bac2da505ee174b7fb984b975c5938f88f0dbab2Petr Machata#include "clang/AST/Mangle.h"
27ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata#include "clang/AST/Type.h"
28ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata#include "llvm/IR/DataLayout.h"
29ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata#include "llvm/IR/Intrinsics.h"
306dfc544b6aa6703d2292be34470aebf874d78452Petr Machata#include "llvm/IR/Value.h"
316dfc544b6aa6703d2292be34470aebf874d78452Petr Machata
326dfc544b6aa6703d2292be34470aebf874d78452Petr Machatausing namespace clang;
336dfc544b6aa6703d2292be34470aebf874d78452Petr Machatausing namespace CodeGen;
346dfc544b6aa6703d2292be34470aebf874d78452Petr Machata
356dfc544b6aa6703d2292be34470aebf874d78452Petr Machatanamespace {
36ba1664b062414481d0f37d06bb01a19874c8d481Petr Machataclass ItaniumCXXABI : public CodeGen::CGCXXABI {
37ba1664b062414481d0f37d06bb01a19874c8d481Petr Machataprotected:
38642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  bool IsARM;
39642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
40642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machatapublic:
41642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
42642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    CGCXXABI(CGM), IsARM(IsARM) { }
43642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
44642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  bool isReturnTypeIndirect(const CXXRecordDecl *RD) const {
45642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    // Structures with either a non-trivial destructor or a non-trivial
46642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    // copy constructor are always indirect.
47642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor();
48642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  }
49642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
50642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const {
51642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    // Structures with either a non-trivial destructor or a non-trivial
52642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    // copy constructor are always indirect.
53642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    if (!RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor())
54642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata      return RAA_Indirect;
55642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    return RAA_Default;
56642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  }
57642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
58642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  bool isZeroInitializable(const MemberPointerType *MPT);
59642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
60642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
61642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
62642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
63642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                               llvm::Value *&This,
64642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                               llvm::Value *MemFnPtr,
65642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                               const MemberPointerType *MPT);
66642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
67ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata  llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF,
68642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                            llvm::Value *Base,
69642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                            llvm::Value *MemPtr,
70642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                            const MemberPointerType *MPT);
71642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
72642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
73929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                           const CastExpr *E,
74642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                           llvm::Value *Src);
75642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *EmitMemberPointerConversion(const CastExpr *E,
76642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                              llvm::Constant *Src);
77642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
78642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
79642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
80642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
81642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT,
82642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                        CharUnits offset);
83642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT);
84642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD,
85642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                     CharUnits ThisAdjustment);
86642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
87642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
88642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                           llvm::Value *L,
89929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                           llvm::Value *R,
90642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                           const MemberPointerType *MPT,
91642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                           bool Inequality);
92642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
93642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
94642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                          llvm::Value *Addr,
95ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata                                          const MemberPointerType *MPT);
96bac2da505ee174b7fb984b975c5938f88f0dbab2Petr Machata
97642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF,
98929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                      llvm::Value *ptr,
99642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                      QualType type);
100642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
101929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
102642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                 CXXCtorType T,
103642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                 CanQualType &ResTy,
104929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                 SmallVectorImpl<CanQualType> &ArgTys);
105642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
106642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
107642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                CXXDtorType T,
108929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                CanQualType &ResTy,
109642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                SmallVectorImpl<CanQualType> &ArgTys);
110642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
111929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
112642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   QualType &ResTy,
113642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   FunctionArgList &Params);
114929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata
115642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
116642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
117642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *EmitConstructorCall(CodeGenFunction &CGF,
118642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                           const CXXConstructorDecl *D,
119642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                           CXXCtorType Type, bool ForVirtualBase,
120642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                           bool Delegating,
121929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                           llvm::Value *This,
122642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                           CallExpr::const_arg_iterator ArgBeg,
123642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                           CallExpr::const_arg_iterator ArgEnd);
124642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
125642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  RValue EmitVirtualDestructorCall(CodeGenFunction &CGF,
126642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   const CXXDestructorDecl *Dtor,
127642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   CXXDtorType DtorType,
128642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   SourceLocation CallLoc,
129642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   ReturnValueSlot ReturnValue,
130642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   llvm::Value *This);
131642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
132642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; }
133642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; }
134642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
135929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  CharUnits getArrayCookieSizeImpl(QualType elementType);
136642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
137642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                     llvm::Value *NewPtr,
138642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                     llvm::Value *NumElements,
139929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                     const CXXNewExpr *expr,
140642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                     QualType ElementType);
141642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
142642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   llvm::Value *allocPtr,
143642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   CharUnits cookieSize);
144642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
145929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D,
146642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                       llvm::GlobalVariable *DeclPtr, bool PerformInit);
147057caa59fc0063bd73568b0ae19bbf668b572737Petr Machata  void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
148057caa59fc0063bd73568b0ae19bbf668b572737Petr Machata                          llvm::Constant *dtor, llvm::Constant *addr);
149057caa59fc0063bd73568b0ae19bbf668b572737Petr Machata
150057caa59fc0063bd73568b0ae19bbf668b572737Petr Machata  llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD,
151642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                                llvm::GlobalVariable *Var);
152642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void EmitThreadLocalInitFuncs(
153642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata      llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
154642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata      llvm::Function *InitFunc);
155642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
156642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                    const DeclRefExpr *DRE);
157642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata};
158642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
159642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machataclass ARMCXXABI : public ItaniumCXXABI {
160642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machatapublic:
161642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
162642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
163642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
164642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                 CXXCtorType T,
165642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                 CanQualType &ResTy,
166642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                 SmallVectorImpl<CanQualType> &ArgTys);
167642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
168642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void BuildDestructorSignature(const CXXDestructorDecl *Dtor,
169642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                CXXDtorType T,
170642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                CanQualType &ResTy,
171642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                SmallVectorImpl<CanQualType> &ArgTys);
172642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
173642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void BuildInstanceFunctionParams(CodeGenFunction &CGF,
174929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata                                   QualType &ResTy,
175642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata                                   FunctionArgList &Params);
176642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
177642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void EmitInstanceFunctionProlog(CodeGenFunction &CGF);
178642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
179642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy);
180642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
181929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  CharUnits getArrayCookieSizeImpl(QualType elementType);
182642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
183311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                     llvm::Value *NewPtr,
184311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                     llvm::Value *NumElements,
185311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                     const CXXNewExpr *expr,
186311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                     QualType ElementType);
187311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr,
188311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                   CharUnits cookieSize);
189642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
190ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata  /// \brief Returns true if the given instance method is one of the
191ba1664b062414481d0f37d06bb01a19874c8d481Petr Machata  /// kinds that the ARM ABI says returns 'this'.
192929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  bool HasThisReturn(GlobalDecl GD) const {
193642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(GD.getDecl());
194642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    if (!MD) return false;
195642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    return ((isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Deleting) ||
196642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata            (isa<CXXConstructorDecl>(MD)));
197642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  }
198642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata};
199642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata}
200642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
201929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr MachataCodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
202642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  switch (CGM.getTarget().getCXXABI().getKind()) {
203311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // For IR-generation purposes, there's no significant difference
204311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // between the ARM and iOS ABIs.
205311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  case TargetCXXABI::GenericARM:
206da69ed0785ff0a9790f8edadb73fe21013aa118bPetr Machata  case TargetCXXABI::iOS:
207da69ed0785ff0a9790f8edadb73fe21013aa118bPetr Machata    return new ARMCXXABI(CGM);
208929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata
209311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
210311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // include the other 32-bit ARM oddities: constructor/destructor return values
211311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // and array cookies.
212311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  case TargetCXXABI::GenericAArch64:
213311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    return  new ItaniumCXXABI(CGM, /*IsARM = */ true);
214311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
215311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  case TargetCXXABI::GenericItanium:
216311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    return new ItaniumCXXABI(CGM);
217311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
218311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  case TargetCXXABI::Microsoft:
219311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    llvm_unreachable("Microsoft ABI is not Itanium-based");
220311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  }
221311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm_unreachable("bad ABI kind");
222311358a1cac22781ecffe697a808bd43eac73bcePetr Machata}
223311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
224311358a1cac22781ecffe697a808bd43eac73bcePetr Machatallvm::Type *
225311358a1cac22781ecffe697a808bd43eac73bcePetr MachataItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
226311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  if (MPT->isMemberDataPointer())
227642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    return CGM.PtrDiffTy;
228642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, NULL);
229642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata}
2309f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata
2319f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata/// In the Itanium and ARM ABIs, method pointers have the form:
2329f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
2339f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///
2349f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata/// In the Itanium ABI:
2359f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
2369f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///  - the this-adjustment is (memptr.adj)
2379f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///  - the virtual offset is (memptr.ptr - 1)
2389f819d5747dc2b8e0f7ac54b38dc321115de6ddaPetr Machata///
239311358a1cac22781ecffe697a808bd43eac73bcePetr Machata/// In the ARM ABI:
240311358a1cac22781ecffe697a808bd43eac73bcePetr Machata///  - method pointers are virtual if (memptr.adj & 1) is nonzero
241311358a1cac22781ecffe697a808bd43eac73bcePetr Machata///  - the this-adjustment is (memptr.adj >> 1)
242311358a1cac22781ecffe697a808bd43eac73bcePetr Machata///  - the virtual offset is (memptr.ptr)
243311358a1cac22781ecffe697a808bd43eac73bcePetr Machata/// ARM uses 'adj' for the virtual flag because Thumb functions
244929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata/// may be only single-byte aligned.
245642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata///
246642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata/// If the member is virtual, the adjusted 'this' pointer points
247642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata/// to a vtable pointer from which the virtual offset is applied.
2488fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata///
2498fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata/// If the member is non-virtual, memptr.ptr is the address of
2508fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata/// the function to call.
2518fdd09b028426f92df614d6ebe5c56d99877febfPetr Machatallvm::Value *
2528fdd09b028426f92df614d6ebe5c56d99877febfPetr MachataItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
2538fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata                                               llvm::Value *&This,
2548fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata                                               llvm::Value *MemFnPtr,
2558fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata                                               const MemberPointerType *MPT) {
256311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  CGBuilderTy &Builder = CGF.Builder;
257311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
2587287166e8fd5949ffcf8eb1f3d378b5ea538915ePetr Machata  const FunctionProtoType *FPT =
2597287166e8fd5949ffcf8eb1f3d378b5ea538915ePetr Machata    MPT->getPointeeType()->getAs<FunctionProtoType>();
2607287166e8fd5949ffcf8eb1f3d378b5ea538915ePetr Machata  const CXXRecordDecl *RD =
261642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
2627287166e8fd5949ffcf8eb1f3d378b5ea538915ePetr Machata
263642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::FunctionType *FTy =
2648fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata    CGM.getTypes().GetFunctionType(
2658fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata      CGM.getTypes().arrangeCXXMethodType(RD, FPT));
2668fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata
2678fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1);
2688fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata
2698fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
2708fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
2718fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
2728fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata
2738fdd09b028426f92df614d6ebe5c56d99877febfPetr Machata  // Extract memptr.adj, which is in the second field.
274311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
275311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
276311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // Compute the true adjustment.
277311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm::Value *Adj = RawAdj;
278311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  if (IsARM)
279642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
280642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata
281642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  // Apply the adjustment and cast back to the original struct type
282642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  // for consistency.
283642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
28423124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
28523124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
28623124cc5c33c6b7a547eb1008505f60590f67593Petr Machata
28723124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  // Load the function pointer.
28823124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
28923124cc5c33c6b7a547eb1008505f60590f67593Petr Machata
29023124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  // If the LSB in the function pointer is 1, the function pointer points to
29123124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  // a virtual function.
29223124cc5c33c6b7a547eb1008505f60590f67593Petr Machata  llvm::Value *IsVirtual;
293311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  if (IsARM)
294311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
295311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  else
296311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
297311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
298311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
299929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata
300929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  // In the virtual path, the adjustment left 'This' pointing to the
301929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  // vtable of the correct base subobject.  The "function pointer" is an
302929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata  // offset within the vtable (+1 for the virtual flag on non-ARM).
303642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  CGF.EmitBlock(FnVirtual);
304b5920d12ceb3dec3d359d8b2972fd22a30c72a66Edgar E. Iglesias
305b5920d12ceb3dec3d359d8b2972fd22a30c72a66Edgar E. Iglesias  // Cast the adjusted this to a pointer to vtable pointer and load.
306b5920d12ceb3dec3d359d8b2972fd22a30c72a66Edgar E. Iglesias  llvm::Type *VTableTy = Builder.getInt8PtrTy();
307552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
308552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
309552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata
310552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata  // Apply the offset.
311552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata  llvm::Value *VTableOffset = FnAsInt;
312673ff510953b65b844a58478aa434120f457c014Petr Machata  if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
313552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata  VTable = Builder.CreateGEP(VTable, VTableOffset);
314552d75e2a226782cc9ecf966e6e343af8f51031fPetr Machata
315b5920d12ceb3dec3d359d8b2972fd22a30c72a66Edgar E. Iglesias  // Load the virtual function to call.
316311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
317ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
318ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata  CGF.EmitBranch(FnEnd);
319ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata
320311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // In the non-virtual path, the function pointer is actually a
321642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata  // function pointer.
322311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  CGF.EmitBlock(FnNonVirtual);
323311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm::Value *NonVirtualFn =
324311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
325311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
326311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // We're done.
327311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  CGF.EmitBlock(FnEnd);
328311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2);
329311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  Callee->addIncoming(VirtualFn, FnVirtual);
330ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
331ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata  return Callee;
332ade3b9798fbc62becbe1b4854f7a2d106498167aPetr Machata}
333311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
334929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata/// Compute an l-value by applying the given pointer-to-member to a
335311358a1cac22781ecffe697a808bd43eac73bcePetr Machata/// base object.
336311358a1cac22781ecffe697a808bd43eac73bcePetr Machatallvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF,
337311358a1cac22781ecffe697a808bd43eac73bcePetr Machata                                                         llvm::Value *Base,
338a186b0e5c469156b9af66e88dca12d208418195cPetr Machata                                                         llvm::Value *MemPtr,
339a186b0e5c469156b9af66e88dca12d208418195cPetr Machata                                           const MemberPointerType *MPT) {
340a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  assert(MemPtr->getType() == CGM.PtrDiffTy);
341a186b0e5c469156b9af66e88dca12d208418195cPetr Machata
342a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  CGBuilderTy &Builder = CGF.Builder;
343a186b0e5c469156b9af66e88dca12d208418195cPetr Machata
344a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  unsigned AS = Base->getType()->getPointerAddressSpace();
345a186b0e5c469156b9af66e88dca12d208418195cPetr Machata
346a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  // Cast to char*.
347a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS));
348a186b0e5c469156b9af66e88dca12d208418195cPetr Machata
349a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  // Apply the offset, which we assume is non-null.
350a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset");
351a186b0e5c469156b9af66e88dca12d208418195cPetr Machata
352a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  // Cast the address to the appropriate pointer type, adopting the
353a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  // address space of the base pointer.
354a186b0e5c469156b9af66e88dca12d208418195cPetr Machata  llvm::Type *PType
355311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS);
356311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  return Builder.CreateBitCast(Addr, PType);
3579af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata}
358929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata
359642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata/// Perform a bitcast, derived-to-base, or base-to-derived member pointer
3603a1806db0ecaa4364e51b4822d66d05f4acd87a9Edgar E. Iglesias/// conversion.
3613a1806db0ecaa4364e51b4822d66d05f4acd87a9Edgar E. Iglesias///
362929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata/// Bitcast conversions are always a no-op under Itanium.
3633a1806db0ecaa4364e51b4822d66d05f4acd87a9Edgar E. Iglesias///
36411c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias/// Obligatory offset/adjustment diagram:
36511c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias///         <-- offset -->          <-- adjustment -->
36611c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias///   |--------------------------|----------------------|--------------------|
36711c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias///   ^Derived address point     ^Base address point    ^Member address point
36811c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias///
36911c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias/// So when converting a base member pointer to a derived member pointer,
37011c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias/// we add the offset to the adjustment because the address point has
37111c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias/// decreased;  and conversely, when converting a derived MP to a base MP
372929bd57ca202fd2f2e8485ebf65d683e664f67b5Petr Machata/// we subtract the offset from the adjustment because the address point
37311c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias/// has increased.
37411c3c3e28461886bb16c69c745b1fefa946c4d9fEdgar E. Iglesias///
3759af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// The standard forbids (at compile time) conversion to and from
3769af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// virtual bases, which is why we don't have to consider them here.
3779af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata///
3789af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// The standard forbids (at run time) casting a derived MP to a base
3799af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// MP when the derived MP does not point to a member of the base.
3809af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// This is why -1 is a reasonable choice for null data member
3819af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata/// pointers.
3829af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machatallvm::Value *
3839af3f3ab7a8c44d94f944f15ece10109e6492930Petr MachataItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
3849af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata                                           const CastExpr *E,
3859af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata                                           llvm::Value *src) {
3869af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
3879af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
3889af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata         E->getCastKind() == CK_ReinterpretMemberPointer);
3899af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata
3909af3f3ab7a8c44d94f944f15ece10109e6492930Petr Machata  // Under Itanium, reinterprets don't require any additional processing.
391aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
392aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata
393aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  // Use constant emission if we can.
394aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  if (isa<llvm::Constant>(src))
395aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata    return EmitMemberPointerConversion(E, cast<llvm::Constant>(src));
396aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata
397aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  llvm::Constant *adj = getMemberPointerAdjustment(E);
398aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  if (!adj) return src;
399aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata
400aa3db6b1234da0e542ba7782849cf200d0d91c1cPetr Machata  CGBuilderTy &Builder = CGF.Builder;
401364753a1cfe46998946a42badd9099591a00325aPetr Machata  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
402311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
403311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  const MemberPointerType *destTy =
404311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    E->getType()->castAs<MemberPointerType>();
405311358a1cac22781ecffe697a808bd43eac73bcePetr Machata
406311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // For member data pointers, this is just a matter of adding the
407311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  // offset if the source is non-null.
408311358a1cac22781ecffe697a808bd43eac73bcePetr Machata  if (destTy->isMemberDataPointer()) {
409311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    llvm::Value *dst;
410311358a1cac22781ecffe697a808bd43eac73bcePetr Machata    if (isDerivedToBase)
411311358a1cac22781ecffe697a808bd43eac73bcePetr Machata      dst = Builder.CreateNSWSub(src, adj, "adj");
412693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    else
413693dfad9c1b121cf079a3082866daa2225df1797Petr Machata      dst = Builder.CreateNSWAdd(src, adj, "adj");
414693dfad9c1b121cf079a3082866daa2225df1797Petr Machata
415693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    // Null check.
416693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType());
417693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull");
418693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    return Builder.CreateSelect(isNull, src, dst);
419693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  }
420693dfad9c1b121cf079a3082866daa2225df1797Petr Machata
421693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  // The this-adjustment is left-shifted by 1 on ARM.
422693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  if (IsARM) {
423693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
424693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    offset <<= 1;
425693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    adj = llvm::ConstantInt::get(adj->getType(), offset);
426693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  }
427693dfad9c1b121cf079a3082866daa2225df1797Petr Machata
428693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj");
429693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  llvm::Value *dstAdj;
430693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  if (isDerivedToBase)
431693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj");
432693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  else
433693dfad9c1b121cf079a3082866daa2225df1797Petr Machata    dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj");
434693dfad9c1b121cf079a3082866daa2225df1797Petr Machata
435693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  return Builder.CreateInsertValue(src, dstAdj, 1);
436693dfad9c1b121cf079a3082866daa2225df1797Petr Machata}
437693dfad9c1b121cf079a3082866daa2225df1797Petr Machata
438693dfad9c1b121cf079a3082866daa2225df1797Petr Machatallvm::Constant *
439693dfad9c1b121cf079a3082866daa2225df1797Petr MachataItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E,
440693dfad9c1b121cf079a3082866daa2225df1797Petr Machata                                           llvm::Constant *src) {
441693dfad9c1b121cf079a3082866daa2225df1797Petr Machata  assert(E->getCastKind() == CK_DerivedToBaseMemberPointer ||
442642626096a694c6af279d25d2b1b2fba5b10ddfbPetr Machata         E->getCastKind() == CK_BaseToDerivedMemberPointer ||
443         E->getCastKind() == CK_ReinterpretMemberPointer);
444
445  // Under Itanium, reinterprets don't require any additional processing.
446  if (E->getCastKind() == CK_ReinterpretMemberPointer) return src;
447
448  // If the adjustment is trivial, we don't need to do anything.
449  llvm::Constant *adj = getMemberPointerAdjustment(E);
450  if (!adj) return src;
451
452  bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer);
453
454  const MemberPointerType *destTy =
455    E->getType()->castAs<MemberPointerType>();
456
457  // For member data pointers, this is just a matter of adding the
458  // offset if the source is non-null.
459  if (destTy->isMemberDataPointer()) {
460    // null maps to null.
461    if (src->isAllOnesValue()) return src;
462
463    if (isDerivedToBase)
464      return llvm::ConstantExpr::getNSWSub(src, adj);
465    else
466      return llvm::ConstantExpr::getNSWAdd(src, adj);
467  }
468
469  // The this-adjustment is left-shifted by 1 on ARM.
470  if (IsARM) {
471    uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue();
472    offset <<= 1;
473    adj = llvm::ConstantInt::get(adj->getType(), offset);
474  }
475
476  llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1);
477  llvm::Constant *dstAdj;
478  if (isDerivedToBase)
479    dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj);
480  else
481    dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj);
482
483  return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1);
484}
485
486llvm::Constant *
487ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
488  // Itanium C++ ABI 2.3:
489  //   A NULL pointer is represented as -1.
490  if (MPT->isMemberDataPointer())
491    return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true);
492
493  llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0);
494  llvm::Constant *Values[2] = { Zero, Zero };
495  return llvm::ConstantStruct::getAnon(Values);
496}
497
498llvm::Constant *
499ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT,
500                                     CharUnits offset) {
501  // Itanium C++ ABI 2.3:
502  //   A pointer to data member is an offset from the base address of
503  //   the class object containing it, represented as a ptrdiff_t
504  return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity());
505}
506
507llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
508  return BuildMemberPointer(MD, CharUnits::Zero());
509}
510
511llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD,
512                                                  CharUnits ThisAdjustment) {
513  assert(MD->isInstance() && "Member function must not be static!");
514  MD = MD->getCanonicalDecl();
515
516  CodeGenTypes &Types = CGM.getTypes();
517
518  // Get the function pointer (or index if this is a virtual function).
519  llvm::Constant *MemPtr[2];
520  if (MD->isVirtual()) {
521    uint64_t Index = CGM.getVTableContext().getMethodVTableIndex(MD);
522
523    const ASTContext &Context = getContext();
524    CharUnits PointerWidth =
525      Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
526    uint64_t VTableOffset = (Index * PointerWidth.getQuantity());
527
528    if (IsARM) {
529      // ARM C++ ABI 3.2.1:
530      //   This ABI specifies that adj contains twice the this
531      //   adjustment, plus 1 if the member function is virtual. The
532      //   least significant bit of adj then makes exactly the same
533      //   discrimination as the least significant bit of ptr does for
534      //   Itanium.
535      MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset);
536      MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
537                                         2 * ThisAdjustment.getQuantity() + 1);
538    } else {
539      // Itanium C++ ABI 2.3:
540      //   For a virtual function, [the pointer field] is 1 plus the
541      //   virtual table offset (in bytes) of the function,
542      //   represented as a ptrdiff_t.
543      MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1);
544      MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy,
545                                         ThisAdjustment.getQuantity());
546    }
547  } else {
548    const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
549    llvm::Type *Ty;
550    // Check whether the function has a computable LLVM signature.
551    if (Types.isFuncTypeConvertible(FPT)) {
552      // The function has a computable LLVM signature; use the correct type.
553      Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD));
554    } else {
555      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
556      // function type is incomplete.
557      Ty = CGM.PtrDiffTy;
558    }
559    llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty);
560
561    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy);
562    MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, (IsARM ? 2 : 1) *
563                                       ThisAdjustment.getQuantity());
564  }
565
566  return llvm::ConstantStruct::getAnon(MemPtr);
567}
568
569llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP,
570                                                 QualType MPType) {
571  const MemberPointerType *MPT = MPType->castAs<MemberPointerType>();
572  const ValueDecl *MPD = MP.getMemberPointerDecl();
573  if (!MPD)
574    return EmitNullMemberPointer(MPT);
575
576  // Compute the this-adjustment.
577  CharUnits ThisAdjustment = CharUnits::Zero();
578  ArrayRef<const CXXRecordDecl*> Path = MP.getMemberPointerPath();
579  bool DerivedMember = MP.isMemberPointerToDerivedMember();
580  const CXXRecordDecl *RD = cast<CXXRecordDecl>(MPD->getDeclContext());
581  for (unsigned I = 0, N = Path.size(); I != N; ++I) {
582    const CXXRecordDecl *Base = RD;
583    const CXXRecordDecl *Derived = Path[I];
584    if (DerivedMember)
585      std::swap(Base, Derived);
586    ThisAdjustment +=
587      getContext().getASTRecordLayout(Derived).getBaseClassOffset(Base);
588    RD = Path[I];
589  }
590  if (DerivedMember)
591    ThisAdjustment = -ThisAdjustment;
592
593  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD))
594    return BuildMemberPointer(MD, ThisAdjustment);
595
596  CharUnits FieldOffset =
597    getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD));
598  return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset);
599}
600
601/// The comparison algorithm is pretty easy: the member pointers are
602/// the same if they're either bitwise identical *or* both null.
603///
604/// ARM is different here only because null-ness is more complicated.
605llvm::Value *
606ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
607                                           llvm::Value *L,
608                                           llvm::Value *R,
609                                           const MemberPointerType *MPT,
610                                           bool Inequality) {
611  CGBuilderTy &Builder = CGF.Builder;
612
613  llvm::ICmpInst::Predicate Eq;
614  llvm::Instruction::BinaryOps And, Or;
615  if (Inequality) {
616    Eq = llvm::ICmpInst::ICMP_NE;
617    And = llvm::Instruction::Or;
618    Or = llvm::Instruction::And;
619  } else {
620    Eq = llvm::ICmpInst::ICMP_EQ;
621    And = llvm::Instruction::And;
622    Or = llvm::Instruction::Or;
623  }
624
625  // Member data pointers are easy because there's a unique null
626  // value, so it just comes down to bitwise equality.
627  if (MPT->isMemberDataPointer())
628    return Builder.CreateICmp(Eq, L, R);
629
630  // For member function pointers, the tautologies are more complex.
631  // The Itanium tautology is:
632  //   (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj))
633  // The ARM tautology is:
634  //   (L == R) <==> (L.ptr == R.ptr &&
635  //                  (L.adj == R.adj ||
636  //                   (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0)))
637  // The inequality tautologies have exactly the same structure, except
638  // applying De Morgan's laws.
639
640  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
641  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
642
643  // This condition tests whether L.ptr == R.ptr.  This must always be
644  // true for equality to hold.
645  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
646
647  // This condition, together with the assumption that L.ptr == R.ptr,
648  // tests whether the pointers are both null.  ARM imposes an extra
649  // condition.
650  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
651  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
652
653  // This condition tests whether L.adj == R.adj.  If this isn't
654  // true, the pointers are unequal unless they're both null.
655  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
656  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
657  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
658
659  // Null member function pointers on ARM clear the low bit of Adj,
660  // so the zero condition has to check that neither low bit is set.
661  if (IsARM) {
662    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
663
664    // Compute (l.adj | r.adj) & 1 and test it against zero.
665    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
666    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
667    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
668                                                      "cmp.or.adj");
669    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
670  }
671
672  // Tie together all our conditions.
673  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
674  Result = Builder.CreateBinOp(And, PtrEq, Result,
675                               Inequality ? "memptr.ne" : "memptr.eq");
676  return Result;
677}
678
679llvm::Value *
680ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
681                                          llvm::Value *MemPtr,
682                                          const MemberPointerType *MPT) {
683  CGBuilderTy &Builder = CGF.Builder;
684
685  /// For member data pointers, this is just a check against -1.
686  if (MPT->isMemberDataPointer()) {
687    assert(MemPtr->getType() == CGM.PtrDiffTy);
688    llvm::Value *NegativeOne =
689      llvm::Constant::getAllOnesValue(MemPtr->getType());
690    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
691  }
692
693  // In Itanium, a member function pointer is not null if 'ptr' is not null.
694  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
695
696  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
697  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
698
699  // On ARM, a member function pointer is also non-null if the low bit of 'adj'
700  // (the virtual bit) is set.
701  if (IsARM) {
702    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
703    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
704    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
705    llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero,
706                                                  "memptr.isvirtual");
707    Result = Builder.CreateOr(Result, IsVirtual);
708  }
709
710  return Result;
711}
712
713/// The Itanium ABI requires non-zero initialization only for data
714/// member pointers, for which '0' is a valid offset.
715bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
716  return MPT->getPointeeType()->isFunctionType();
717}
718
719/// The Itanium ABI always places an offset to the complete object
720/// at entry -2 in the vtable.
721llvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF,
722                                                   llvm::Value *ptr,
723                                                   QualType type) {
724  // Grab the vtable pointer as an intptr_t*.
725  llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo());
726
727  // Track back to entry -2 and pull out the offset there.
728  llvm::Value *offsetPtr =
729    CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr");
730  llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr);
731  offset->setAlignment(CGF.PointerAlignInBytes);
732
733  // Apply the offset.
734  ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
735  return CGF.Builder.CreateInBoundsGEP(ptr, offset);
736}
737
738/// The generic ABI passes 'this', plus a VTT if it's initializing a
739/// base subobject.
740void ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
741                                              CXXCtorType Type,
742                                              CanQualType &ResTy,
743                                SmallVectorImpl<CanQualType> &ArgTys) {
744  ASTContext &Context = getContext();
745
746  // 'this' is already there.
747
748  // Check if we need to add a VTT parameter (which has type void **).
749  if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0)
750    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
751}
752
753/// The ARM ABI does the same as the Itanium ABI, but returns 'this'.
754void ARMCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor,
755                                          CXXCtorType Type,
756                                          CanQualType &ResTy,
757                                SmallVectorImpl<CanQualType> &ArgTys) {
758  ItaniumCXXABI::BuildConstructorSignature(Ctor, Type, ResTy, ArgTys);
759  ResTy = ArgTys[0];
760}
761
762/// The generic ABI passes 'this', plus a VTT if it's destroying a
763/// base subobject.
764void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
765                                             CXXDtorType Type,
766                                             CanQualType &ResTy,
767                                SmallVectorImpl<CanQualType> &ArgTys) {
768  ASTContext &Context = getContext();
769
770  // 'this' is already there.
771
772  // Check if we need to add a VTT parameter (which has type void **).
773  if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0)
774    ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy));
775}
776
777/// The ARM ABI does the same as the Itanium ABI, but returns 'this'
778/// for non-deleting destructors.
779void ARMCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor,
780                                         CXXDtorType Type,
781                                         CanQualType &ResTy,
782                                SmallVectorImpl<CanQualType> &ArgTys) {
783  ItaniumCXXABI::BuildDestructorSignature(Dtor, Type, ResTy, ArgTys);
784
785  if (Type != Dtor_Deleting)
786    ResTy = ArgTys[0];
787}
788
789void ItaniumCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
790                                                QualType &ResTy,
791                                                FunctionArgList &Params) {
792  /// Create the 'this' variable.
793  BuildThisParam(CGF, Params);
794
795  const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl());
796  assert(MD->isInstance());
797
798  // Check if we need a VTT parameter as well.
799  if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
800    ASTContext &Context = getContext();
801
802    // FIXME: avoid the fake decl
803    QualType T = Context.getPointerType(Context.VoidPtrTy);
804    ImplicitParamDecl *VTTDecl
805      = ImplicitParamDecl::Create(Context, 0, MD->getLocation(),
806                                  &Context.Idents.get("vtt"), T);
807    Params.push_back(VTTDecl);
808    getVTTDecl(CGF) = VTTDecl;
809  }
810}
811
812void ARMCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF,
813                                            QualType &ResTy,
814                                            FunctionArgList &Params) {
815  ItaniumCXXABI::BuildInstanceFunctionParams(CGF, ResTy, Params);
816
817  // Return 'this' from certain constructors and destructors.
818  if (HasThisReturn(CGF.CurGD))
819    ResTy = Params[0]->getType();
820}
821
822void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
823  /// Initialize the 'this' slot.
824  EmitThisParam(CGF);
825
826  /// Initialize the 'vtt' slot if needed.
827  if (getVTTDecl(CGF)) {
828    getVTTValue(CGF)
829      = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(getVTTDecl(CGF)),
830                               "vtt");
831  }
832}
833
834void ARMCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) {
835  ItaniumCXXABI::EmitInstanceFunctionProlog(CGF);
836
837  /// Initialize the return slot to 'this' at the start of the
838  /// function.
839  if (HasThisReturn(CGF.CurGD))
840    CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue);
841}
842
843llvm::Value *ItaniumCXXABI::EmitConstructorCall(CodeGenFunction &CGF,
844                                        const CXXConstructorDecl *D,
845                                        CXXCtorType Type, bool ForVirtualBase,
846                                        bool Delegating,
847                                        llvm::Value *This,
848                                        CallExpr::const_arg_iterator ArgBeg,
849                                        CallExpr::const_arg_iterator ArgEnd) {
850  llvm::Value *VTT = CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase,
851                                         Delegating);
852  QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy);
853  llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
854
855  // FIXME: Provide a source location here.
856  CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), This,
857                        VTT, VTTTy, ArgBeg, ArgEnd);
858  return Callee;
859}
860
861RValue ItaniumCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF,
862                                                const CXXDestructorDecl *Dtor,
863                                                CXXDtorType DtorType,
864                                                SourceLocation CallLoc,
865                                                ReturnValueSlot ReturnValue,
866                                                llvm::Value *This) {
867  assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete);
868
869  const CGFunctionInfo *FInfo
870    = &CGM.getTypes().arrangeCXXDestructor(Dtor, DtorType);
871  llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo);
872  llvm::Value *Callee = CGF.BuildVirtualCall(Dtor, DtorType, This, Ty);
873
874  return CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValue, This,
875                               /*ImplicitParam=*/0, QualType(), 0, 0);
876}
877
878void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF,
879                                    RValue RV, QualType ResultType) {
880  if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl()))
881    return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType);
882
883  // Destructor thunks in the ARM ABI have indeterminate results.
884  llvm::Type *T =
885    cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType();
886  RValue Undef = RValue::get(llvm::UndefValue::get(T));
887  return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType);
888}
889
890/************************** Array allocation cookies **************************/
891
892CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) {
893  // The array cookie is a size_t; pad that up to the element alignment.
894  // The cookie is actually right-justified in that space.
895  return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes),
896                  CGM.getContext().getTypeAlignInChars(elementType));
897}
898
899llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
900                                                  llvm::Value *NewPtr,
901                                                  llvm::Value *NumElements,
902                                                  const CXXNewExpr *expr,
903                                                  QualType ElementType) {
904  assert(requiresArrayCookie(expr));
905
906  unsigned AS = NewPtr->getType()->getPointerAddressSpace();
907
908  ASTContext &Ctx = getContext();
909  QualType SizeTy = Ctx.getSizeType();
910  CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy);
911
912  // The size of the cookie.
913  CharUnits CookieSize =
914    std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType));
915  assert(CookieSize == getArrayCookieSizeImpl(ElementType));
916
917  // Compute an offset to the cookie.
918  llvm::Value *CookiePtr = NewPtr;
919  CharUnits CookieOffset = CookieSize - SizeSize;
920  if (!CookieOffset.isZero())
921    CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr,
922                                                 CookieOffset.getQuantity());
923
924  // Write the number of elements into the appropriate slot.
925  llvm::Value *NumElementsPtr
926    = CGF.Builder.CreateBitCast(CookiePtr,
927                                CGF.ConvertType(SizeTy)->getPointerTo(AS));
928  CGF.Builder.CreateStore(NumElements, NumElementsPtr);
929
930  // Finally, compute a pointer to the actual data buffer by skipping
931  // over the cookie completely.
932  return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr,
933                                                CookieSize.getQuantity());
934}
935
936llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
937                                                llvm::Value *allocPtr,
938                                                CharUnits cookieSize) {
939  // The element size is right-justified in the cookie.
940  llvm::Value *numElementsPtr = allocPtr;
941  CharUnits numElementsOffset =
942    cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes);
943  if (!numElementsOffset.isZero())
944    numElementsPtr =
945      CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr,
946                                             numElementsOffset.getQuantity());
947
948  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
949  numElementsPtr =
950    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
951  return CGF.Builder.CreateLoad(numElementsPtr);
952}
953
954CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) {
955  // ARM says that the cookie is always:
956  //   struct array_cookie {
957  //     std::size_t element_size; // element_size != 0
958  //     std::size_t element_count;
959  //   };
960  // But the base ABI doesn't give anything an alignment greater than
961  // 8, so we can dismiss this as typical ABI-author blindness to
962  // actual language complexity and round up to the element alignment.
963  return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes),
964                  CGM.getContext().getTypeAlignInChars(elementType));
965}
966
967llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
968                                              llvm::Value *newPtr,
969                                              llvm::Value *numElements,
970                                              const CXXNewExpr *expr,
971                                              QualType elementType) {
972  assert(requiresArrayCookie(expr));
973
974  // NewPtr is a char*, but we generalize to arbitrary addrspaces.
975  unsigned AS = newPtr->getType()->getPointerAddressSpace();
976
977  // The cookie is always at the start of the buffer.
978  llvm::Value *cookie = newPtr;
979
980  // The first element is the element size.
981  cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS));
982  llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy,
983                 getContext().getTypeSizeInChars(elementType).getQuantity());
984  CGF.Builder.CreateStore(elementSize, cookie);
985
986  // The second element is the element count.
987  cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1);
988  CGF.Builder.CreateStore(numElements, cookie);
989
990  // Finally, compute a pointer to the actual data buffer by skipping
991  // over the cookie completely.
992  CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType);
993  return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
994                                                cookieSize.getQuantity());
995}
996
997llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
998                                            llvm::Value *allocPtr,
999                                            CharUnits cookieSize) {
1000  // The number of elements is at offset sizeof(size_t) relative to
1001  // the allocated pointer.
1002  llvm::Value *numElementsPtr
1003    = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes);
1004
1005  unsigned AS = allocPtr->getType()->getPointerAddressSpace();
1006  numElementsPtr =
1007    CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS));
1008  return CGF.Builder.CreateLoad(numElementsPtr);
1009}
1010
1011/*********************** Static local initialization **************************/
1012
1013static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM,
1014                                         llvm::PointerType *GuardPtrTy) {
1015  // int __cxa_guard_acquire(__guard *guard_object);
1016  llvm::FunctionType *FTy =
1017    llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy),
1018                            GuardPtrTy, /*isVarArg=*/false);
1019  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire",
1020                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1021                                              llvm::AttributeSet::FunctionIndex,
1022                                                 llvm::Attribute::NoUnwind));
1023}
1024
1025static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM,
1026                                         llvm::PointerType *GuardPtrTy) {
1027  // void __cxa_guard_release(__guard *guard_object);
1028  llvm::FunctionType *FTy =
1029    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1030  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release",
1031                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1032                                              llvm::AttributeSet::FunctionIndex,
1033                                                 llvm::Attribute::NoUnwind));
1034}
1035
1036static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM,
1037                                       llvm::PointerType *GuardPtrTy) {
1038  // void __cxa_guard_abort(__guard *guard_object);
1039  llvm::FunctionType *FTy =
1040    llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false);
1041  return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort",
1042                                   llvm::AttributeSet::get(CGM.getLLVMContext(),
1043                                              llvm::AttributeSet::FunctionIndex,
1044                                                 llvm::Attribute::NoUnwind));
1045}
1046
1047namespace {
1048  struct CallGuardAbort : EHScopeStack::Cleanup {
1049    llvm::GlobalVariable *Guard;
1050    CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {}
1051
1052    void Emit(CodeGenFunction &CGF, Flags flags) {
1053      CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()),
1054                                  Guard);
1055    }
1056  };
1057}
1058
1059/// The ARM code here follows the Itanium code closely enough that we
1060/// just special-case it at particular places.
1061void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF,
1062                                    const VarDecl &D,
1063                                    llvm::GlobalVariable *var,
1064                                    bool shouldPerformInit) {
1065  CGBuilderTy &Builder = CGF.Builder;
1066
1067  // We only need to use thread-safe statics for local non-TLS variables;
1068  // global initialization is always single-threaded.
1069  bool threadsafe = getContext().getLangOpts().ThreadsafeStatics &&
1070                    D.isLocalVarDecl() && !D.getTLSKind();
1071
1072  // If we have a global variable with internal linkage and thread-safe statics
1073  // are disabled, we can just let the guard variable be of type i8.
1074  bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage();
1075
1076  llvm::IntegerType *guardTy;
1077  if (useInt8GuardVariable) {
1078    guardTy = CGF.Int8Ty;
1079  } else {
1080    // Guard variables are 64 bits in the generic ABI and size width on ARM
1081    // (i.e. 32-bit on AArch32, 64-bit on AArch64).
1082    guardTy = (IsARM ? CGF.SizeTy : CGF.Int64Ty);
1083  }
1084  llvm::PointerType *guardPtrTy = guardTy->getPointerTo();
1085
1086  // Create the guard variable if we don't already have it (as we
1087  // might if we're double-emitting this function body).
1088  llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D);
1089  if (!guard) {
1090    // Mangle the name for the guard.
1091    SmallString<256> guardName;
1092    {
1093      llvm::raw_svector_ostream out(guardName);
1094      getMangleContext().mangleItaniumGuardVariable(&D, out);
1095      out.flush();
1096    }
1097
1098    // Create the guard variable with a zero-initializer.
1099    // Just absorb linkage and visibility from the guarded variable.
1100    guard = new llvm::GlobalVariable(CGM.getModule(), guardTy,
1101                                     false, var->getLinkage(),
1102                                     llvm::ConstantInt::get(guardTy, 0),
1103                                     guardName.str());
1104    guard->setVisibility(var->getVisibility());
1105    // If the variable is thread-local, so is its guard variable.
1106    guard->setThreadLocalMode(var->getThreadLocalMode());
1107
1108    CGM.setStaticLocalDeclGuardAddress(&D, guard);
1109  }
1110
1111  // Test whether the variable has completed initialization.
1112  llvm::Value *isInitialized;
1113
1114  // ARM C++ ABI 3.2.3.1:
1115  //   To support the potential use of initialization guard variables
1116  //   as semaphores that are the target of ARM SWP and LDREX/STREX
1117  //   synchronizing instructions we define a static initialization
1118  //   guard variable to be a 4-byte aligned, 4- byte word with the
1119  //   following inline access protocol.
1120  //     #define INITIALIZED 1
1121  //     if ((obj_guard & INITIALIZED) != INITIALIZED) {
1122  //       if (__cxa_guard_acquire(&obj_guard))
1123  //         ...
1124  //     }
1125  if (IsARM && !useInt8GuardVariable) {
1126    llvm::Value *V = Builder.CreateLoad(guard);
1127    llvm::Value *Test1 = llvm::ConstantInt::get(guardTy, 1);
1128    V = Builder.CreateAnd(V, Test1);
1129    isInitialized = Builder.CreateIsNull(V, "guard.uninitialized");
1130
1131  // Itanium C++ ABI 3.3.2:
1132  //   The following is pseudo-code showing how these functions can be used:
1133  //     if (obj_guard.first_byte == 0) {
1134  //       if ( __cxa_guard_acquire (&obj_guard) ) {
1135  //         try {
1136  //           ... initialize the object ...;
1137  //         } catch (...) {
1138  //            __cxa_guard_abort (&obj_guard);
1139  //            throw;
1140  //         }
1141  //         ... queue object destructor with __cxa_atexit() ...;
1142  //         __cxa_guard_release (&obj_guard);
1143  //       }
1144  //     }
1145  } else {
1146    // Load the first byte of the guard variable.
1147    llvm::LoadInst *LI =
1148      Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy));
1149    LI->setAlignment(1);
1150
1151    // Itanium ABI:
1152    //   An implementation supporting thread-safety on multiprocessor
1153    //   systems must also guarantee that references to the initialized
1154    //   object do not occur before the load of the initialization flag.
1155    //
1156    // In LLVM, we do this by marking the load Acquire.
1157    if (threadsafe)
1158      LI->setAtomic(llvm::Acquire);
1159
1160    isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized");
1161  }
1162
1163  llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check");
1164  llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end");
1165
1166  // Check if the first byte of the guard variable is zero.
1167  Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock);
1168
1169  CGF.EmitBlock(InitCheckBlock);
1170
1171  // Variables used when coping with thread-safe statics and exceptions.
1172  if (threadsafe) {
1173    // Call __cxa_guard_acquire.
1174    llvm::Value *V
1175      = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard);
1176
1177    llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init");
1178
1179    Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"),
1180                         InitBlock, EndBlock);
1181
1182    // Call __cxa_guard_abort along the exceptional edge.
1183    CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard);
1184
1185    CGF.EmitBlock(InitBlock);
1186  }
1187
1188  // Emit the initializer and add a global destructor if appropriate.
1189  CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit);
1190
1191  if (threadsafe) {
1192    // Pop the guard-abort cleanup if we pushed one.
1193    CGF.PopCleanupBlock();
1194
1195    // Call __cxa_guard_release.  This cannot throw.
1196    CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard);
1197  } else {
1198    Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard);
1199  }
1200
1201  CGF.EmitBlock(EndBlock);
1202}
1203
1204/// Register a global destructor using __cxa_atexit.
1205static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,
1206                                        llvm::Constant *dtor,
1207                                        llvm::Constant *addr,
1208                                        bool TLS) {
1209  const char *Name = TLS ? "__cxa_thread_atexit" : "__cxa_atexit";
1210
1211  // We're assuming that the destructor function is something we can
1212  // reasonably call with the default CC.  Go ahead and cast it to the
1213  // right prototype.
1214  llvm::Type *dtorTy =
1215    llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo();
1216
1217  // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
1218  llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy };
1219  llvm::FunctionType *atexitTy =
1220    llvm::FunctionType::get(CGF.IntTy, paramTys, false);
1221
1222  // Fetch the actual function.
1223  llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name);
1224  if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit))
1225    fn->setDoesNotThrow();
1226
1227  // Create a variable that binds the atexit to this shared object.
1228  llvm::Constant *handle =
1229    CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle");
1230
1231  llvm::Value *args[] = {
1232    llvm::ConstantExpr::getBitCast(dtor, dtorTy),
1233    llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy),
1234    handle
1235  };
1236  CGF.EmitNounwindRuntimeCall(atexit, args);
1237}
1238
1239/// Register a global destructor as best as we know how.
1240void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF,
1241                                       const VarDecl &D,
1242                                       llvm::Constant *dtor,
1243                                       llvm::Constant *addr) {
1244  // Use __cxa_atexit if available.
1245  if (CGM.getCodeGenOpts().CXAAtExit)
1246    return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind());
1247
1248  if (D.getTLSKind())
1249    CGM.ErrorUnsupported(&D, "non-trivial TLS destruction");
1250
1251  // In Apple kexts, we want to add a global destructor entry.
1252  // FIXME: shouldn't this be guarded by some variable?
1253  if (CGM.getLangOpts().AppleKext) {
1254    // Generate a global destructor entry.
1255    return CGM.AddCXXDtorEntry(dtor, addr);
1256  }
1257
1258  CGF.registerGlobalDtorWithAtExit(dtor, addr);
1259}
1260
1261/// Get the appropriate linkage for the wrapper function. This is essentially
1262/// the weak form of the variable's linkage; every translation unit which wneeds
1263/// the wrapper emits a copy, and we want the linker to merge them.
1264static llvm::GlobalValue::LinkageTypes getThreadLocalWrapperLinkage(
1265    llvm::GlobalValue::LinkageTypes VarLinkage) {
1266  if (llvm::GlobalValue::isLinkerPrivateLinkage(VarLinkage))
1267    return llvm::GlobalValue::LinkerPrivateWeakLinkage;
1268  // For internal linkage variables, we don't need an external or weak wrapper.
1269  if (llvm::GlobalValue::isLocalLinkage(VarLinkage))
1270    return VarLinkage;
1271  return llvm::GlobalValue::WeakODRLinkage;
1272}
1273
1274llvm::Function *
1275ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD,
1276                                             llvm::GlobalVariable *Var) {
1277  // Mangle the name for the thread_local wrapper function.
1278  SmallString<256> WrapperName;
1279  {
1280    llvm::raw_svector_ostream Out(WrapperName);
1281    getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out);
1282    Out.flush();
1283  }
1284
1285  if (llvm::Value *V = Var->getParent()->getNamedValue(WrapperName))
1286    return cast<llvm::Function>(V);
1287
1288  llvm::Type *RetTy = Var->getType();
1289  if (VD->getType()->isReferenceType())
1290    RetTy = RetTy->getPointerElementType();
1291
1292  llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false);
1293  llvm::Function *Wrapper = llvm::Function::Create(
1294      FnTy, getThreadLocalWrapperLinkage(Var->getLinkage()), WrapperName.str(),
1295      &CGM.getModule());
1296  // Always resolve references to the wrapper at link time.
1297  Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility);
1298  return Wrapper;
1299}
1300
1301void ItaniumCXXABI::EmitThreadLocalInitFuncs(
1302    llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls,
1303    llvm::Function *InitFunc) {
1304  for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1305    const VarDecl *VD = Decls[I].first;
1306    llvm::GlobalVariable *Var = Decls[I].second;
1307
1308    // Mangle the name for the thread_local initialization function.
1309    SmallString<256> InitFnName;
1310    {
1311      llvm::raw_svector_ostream Out(InitFnName);
1312      getMangleContext().mangleItaniumThreadLocalInit(VD, Out);
1313      Out.flush();
1314    }
1315
1316    // If we have a definition for the variable, emit the initialization
1317    // function as an alias to the global Init function (if any). Otherwise,
1318    // produce a declaration of the initialization function.
1319    llvm::GlobalValue *Init = 0;
1320    bool InitIsInitFunc = false;
1321    if (VD->hasDefinition()) {
1322      InitIsInitFunc = true;
1323      if (InitFunc)
1324        Init =
1325            new llvm::GlobalAlias(InitFunc->getType(), Var->getLinkage(),
1326                                  InitFnName.str(), InitFunc, &CGM.getModule());
1327    } else {
1328      // Emit a weak global function referring to the initialization function.
1329      // This function will not exist if the TU defining the thread_local
1330      // variable in question does not need any dynamic initialization for
1331      // its thread_local variables.
1332      llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false);
1333      Init = llvm::Function::Create(
1334          FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(),
1335          &CGM.getModule());
1336    }
1337
1338    if (Init)
1339      Init->setVisibility(Var->getVisibility());
1340
1341    llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var);
1342    llvm::LLVMContext &Context = CGM.getModule().getContext();
1343    llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper);
1344    CGBuilderTy Builder(Entry);
1345    if (InitIsInitFunc) {
1346      if (Init)
1347        Builder.CreateCall(Init);
1348    } else {
1349      // Don't know whether we have an init function. Call it if it exists.
1350      llvm::Value *Have = Builder.CreateIsNotNull(Init);
1351      llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
1352      llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper);
1353      Builder.CreateCondBr(Have, InitBB, ExitBB);
1354
1355      Builder.SetInsertPoint(InitBB);
1356      Builder.CreateCall(Init);
1357      Builder.CreateBr(ExitBB);
1358
1359      Builder.SetInsertPoint(ExitBB);
1360    }
1361
1362    // For a reference, the result of the wrapper function is a pointer to
1363    // the referenced object.
1364    llvm::Value *Val = Var;
1365    if (VD->getType()->isReferenceType()) {
1366      llvm::LoadInst *LI = Builder.CreateLoad(Val);
1367      LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity());
1368      Val = LI;
1369    }
1370
1371    Builder.CreateRet(Val);
1372  }
1373}
1374
1375LValue ItaniumCXXABI::EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF,
1376                                                 const DeclRefExpr *DRE) {
1377  const VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1378  QualType T = VD->getType();
1379  llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T);
1380  llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty);
1381  llvm::Function *Wrapper =
1382      getOrCreateThreadLocalWrapper(VD, cast<llvm::GlobalVariable>(Val));
1383
1384  Val = CGF.Builder.CreateCall(Wrapper);
1385
1386  LValue LV;
1387  if (VD->getType()->isReferenceType())
1388    LV = CGF.MakeNaturalAlignAddrLValue(Val, T);
1389  else
1390    LV = CGF.MakeAddrLValue(Val, DRE->getType(),
1391                            CGF.getContext().getDeclAlign(VD));
1392  // FIXME: need setObjCGCLValueClass?
1393  return LV;
1394}
1395