Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 1 | //===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Itanium C++ ABI. The class |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 11 | // in this file generates structures that follow the Itanium C++ ABI, which is |
| 12 | // documented at: |
| 13 | // http://www.codesourcery.com/public/cxx-abi/abi.html |
| 14 | // http://www.codesourcery.com/public/cxx-abi/abi-eh.html |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 15 | // |
| 16 | // It also supports the closely-related ARM ABI, documented at: |
| 17 | // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf |
| 18 | // |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "CGCXXABI.h" |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 22 | #include "CGRecordLayout.h" |
Charles Davis | a325a6e | 2012-06-23 23:44:00 +0000 | [diff] [blame] | 23 | #include "CGVTables.h" |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 24 | #include "CodeGenFunction.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 25 | #include "CodeGenModule.h" |
Craig Topper | c9ee1d0 | 2012-09-15 18:47:51 +0000 | [diff] [blame] | 26 | #include "clang/AST/Mangle.h" |
| 27 | #include "clang/AST/Type.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/Intrinsics.h" |
| 30 | #include "llvm/IR/Value.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace clang; |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 33 | using namespace CodeGen; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 34 | |
| 35 | namespace { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 36 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 37 | /// VTables - All the vtables which have been defined. |
| 38 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; |
| 39 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 40 | protected: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 41 | bool UseARMMethodPtrABI; |
| 42 | bool UseARMGuardVarABI; |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 43 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 44 | ItaniumMangleContext &getMangleContext() { |
| 45 | return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 46 | } |
| 47 | |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 48 | public: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 49 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, |
| 50 | bool UseARMMethodPtrABI = false, |
| 51 | bool UseARMGuardVarABI = false) : |
| 52 | CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI), |
| 53 | UseARMGuardVarABI(UseARMGuardVarABI) { } |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 54 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 55 | bool isReturnTypeIndirect(const CXXRecordDecl *RD) const { |
| 56 | // Structures with either a non-trivial destructor or a non-trivial |
| 57 | // copy constructor are always indirect. |
| 58 | return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor(); |
| 59 | } |
| 60 | |
| 61 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const { |
| 62 | // Structures with either a non-trivial destructor or a non-trivial |
| 63 | // copy constructor are always indirect. |
| 64 | if (!RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) |
| 65 | return RAA_Indirect; |
| 66 | return RAA_Default; |
| 67 | } |
| 68 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 69 | bool isZeroInitializable(const MemberPointerType *MPT); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 70 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 71 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 72 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 73 | llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 74 | const Expr *E, |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 75 | llvm::Value *&This, |
| 76 | llvm::Value *MemFnPtr, |
| 77 | const MemberPointerType *MPT); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 78 | |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 79 | llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 80 | llvm::Value *Base, |
| 81 | llvm::Value *MemPtr, |
| 82 | const MemberPointerType *MPT); |
| 83 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 84 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 85 | const CastExpr *E, |
| 86 | llvm::Value *Src); |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 87 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
| 88 | llvm::Constant *Src); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 89 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 90 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 91 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 92 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 93 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
| 94 | CharUnits offset); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 95 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); |
| 96 | llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, |
| 97 | CharUnits ThisAdjustment); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 98 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 99 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 100 | llvm::Value *L, |
| 101 | llvm::Value *R, |
| 102 | const MemberPointerType *MPT, |
| 103 | bool Inequality); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 104 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 105 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 106 | llvm::Value *Addr, |
| 107 | const MemberPointerType *MPT); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 108 | |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 109 | llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, |
| 110 | llvm::Value *ptr, |
| 111 | QualType type); |
| 112 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 113 | llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 114 | llvm::Value *This, |
| 115 | const CXXRecordDecl *ClassDecl, |
| 116 | const CXXRecordDecl *BaseClassDecl); |
| 117 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 118 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 119 | CXXCtorType T, |
| 120 | CanQualType &ResTy, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 121 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 122 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 123 | void EmitCXXConstructors(const CXXConstructorDecl *D); |
| 124 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 125 | void BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 126 | CXXDtorType T, |
| 127 | CanQualType &ResTy, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | SmallVectorImpl<CanQualType> &ArgTys); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 129 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 130 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
| 131 | CXXDtorType DT) const { |
| 132 | // Itanium does not emit any destructor variant as an inline thunk. |
| 133 | // Delegating may occur as an optimization, but all variants are either |
| 134 | // emitted with external linkage or as linkonce if they are inline and used. |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | void EmitCXXDestructors(const CXXDestructorDecl *D); |
| 139 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 140 | void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
| 141 | FunctionArgList &Params); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 142 | |
| 143 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 144 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 145 | unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, |
| 146 | const CXXConstructorDecl *D, |
| 147 | CXXCtorType Type, bool ForVirtualBase, |
| 148 | bool Delegating, CallArgList &Args); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 149 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 150 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 151 | CXXDtorType Type, bool ForVirtualBase, |
| 152 | bool Delegating, llvm::Value *This); |
| 153 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 154 | void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD); |
| 155 | |
| 156 | llvm::Value *getVTableAddressPointInStructor( |
| 157 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 158 | BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
| 159 | bool &NeedsVirtualOffset); |
| 160 | |
| 161 | llvm::Constant * |
| 162 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
| 163 | const CXXRecordDecl *VTableClass); |
| 164 | |
| 165 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
| 166 | CharUnits VPtrOffset); |
| 167 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 168 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
| 169 | llvm::Value *This, llvm::Type *Ty); |
| 170 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 171 | void EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 172 | const CXXDestructorDecl *Dtor, |
| 173 | CXXDtorType DtorType, SourceLocation CallLoc, |
| 174 | llvm::Value *This); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 175 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 176 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 177 | |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 178 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) { |
| 179 | // Allow inlining of thunks by emitting them with available_externally |
| 180 | // linkage together with vtables when needed. |
| 181 | if (ForVTable) |
| 182 | Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 183 | } |
| 184 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 185 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, |
| 186 | const ThisAdjustment &TA); |
| 187 | |
| 188 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 189 | const ReturnAdjustment &RA); |
| 190 | |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 191 | StringRef GetPureVirtualCallName() { return "__cxa_pure_virtual"; } |
David Blaikie | eb7d598 | 2012-10-16 22:56:05 +0000 | [diff] [blame] | 192 | StringRef GetDeletedVirtualCallName() { return "__cxa_deleted_virtual"; } |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 193 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 194 | CharUnits getArrayCookieSizeImpl(QualType elementType); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 195 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 196 | llvm::Value *NewPtr, |
| 197 | llvm::Value *NumElements, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 198 | const CXXNewExpr *expr, |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 199 | QualType ElementType); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 200 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 201 | llvm::Value *allocPtr, |
| 202 | CharUnits cookieSize); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 203 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 204 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 205 | llvm::GlobalVariable *DeclPtr, bool PerformInit); |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 206 | void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
| 207 | llvm::Constant *dtor, llvm::Constant *addr); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 208 | |
| 209 | llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD, |
| 210 | llvm::GlobalVariable *Var); |
| 211 | void EmitThreadLocalInitFuncs( |
| 212 | llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, |
| 213 | llvm::Function *InitFunc); |
| 214 | LValue EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, |
| 215 | const DeclRefExpr *DRE); |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 216 | |
| 217 | bool NeedsVTTParameter(GlobalDecl GD); |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 218 | }; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 219 | |
| 220 | class ARMCXXABI : public ItaniumCXXABI { |
| 221 | public: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 222 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : |
| 223 | ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 224 | /* UseARMGuardVarABI = */ true) {} |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 225 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 226 | bool HasThisReturn(GlobalDecl GD) const { |
| 227 | return (isa<CXXConstructorDecl>(GD.getDecl()) || ( |
| 228 | isa<CXXDestructorDecl>(GD.getDecl()) && |
| 229 | GD.getDtorType() != Dtor_Deleting)); |
| 230 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 231 | |
| 232 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, QualType ResTy); |
| 233 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 234 | CharUnits getArrayCookieSizeImpl(QualType elementType); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 235 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 236 | llvm::Value *NewPtr, |
| 237 | llvm::Value *NumElements, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 238 | const CXXNewExpr *expr, |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 239 | QualType ElementType); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 240 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, |
| 241 | CharUnits cookieSize); |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 242 | }; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 245 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 246 | switch (CGM.getTarget().getCXXABI().getKind()) { |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 247 | // For IR-generation purposes, there's no significant difference |
| 248 | // between the ARM and iOS ABIs. |
| 249 | case TargetCXXABI::GenericARM: |
| 250 | case TargetCXXABI::iOS: |
| 251 | return new ARMCXXABI(CGM); |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 252 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 253 | // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't |
| 254 | // include the other 32-bit ARM oddities: constructor/destructor return values |
| 255 | // and array cookies. |
| 256 | case TargetCXXABI::GenericAArch64: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 257 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 258 | /* UseARMGuardVarABI = */ true); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 259 | |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 260 | case TargetCXXABI::GenericItanium: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 261 | if (CGM.getContext().getTargetInfo().getTriple().getArch() |
| 262 | == llvm::Triple::le32) { |
| 263 | // For PNaCl, use ARM-style method pointers so that PNaCl code |
| 264 | // does not assume anything about the alignment of function |
| 265 | // pointers. |
| 266 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 267 | /* UseARMGuardVarABI = */ false); |
| 268 | } |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 269 | return new ItaniumCXXABI(CGM); |
| 270 | |
| 271 | case TargetCXXABI::Microsoft: |
| 272 | llvm_unreachable("Microsoft ABI is not Itanium-based"); |
| 273 | } |
| 274 | llvm_unreachable("bad ABI kind"); |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 277 | llvm::Type * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 278 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 279 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 280 | return CGM.PtrDiffTy; |
| 281 | return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, NULL); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 282 | } |
| 283 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 284 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 285 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 286 | /// |
| 287 | /// In the Itanium ABI: |
| 288 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 289 | /// - the this-adjustment is (memptr.adj) |
| 290 | /// - the virtual offset is (memptr.ptr - 1) |
| 291 | /// |
| 292 | /// In the ARM ABI: |
| 293 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 294 | /// - the this-adjustment is (memptr.adj >> 1) |
| 295 | /// - the virtual offset is (memptr.ptr) |
| 296 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 297 | /// may be only single-byte aligned. |
| 298 | /// |
| 299 | /// If the member is virtual, the adjusted 'this' pointer points |
| 300 | /// to a vtable pointer from which the virtual offset is applied. |
| 301 | /// |
| 302 | /// If the member is non-virtual, memptr.ptr is the address of |
| 303 | /// the function to call. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 304 | llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( |
| 305 | CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, |
| 306 | llvm::Value *MemFnPtr, const MemberPointerType *MPT) { |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 307 | CGBuilderTy &Builder = CGF.Builder; |
| 308 | |
| 309 | const FunctionProtoType *FPT = |
| 310 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 311 | const CXXRecordDecl *RD = |
| 312 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 313 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 314 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 315 | CGM.getTypes().GetFunctionType( |
| 316 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 317 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 318 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 319 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 320 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 321 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 322 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 323 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 324 | // Extract memptr.adj, which is in the second field. |
| 325 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 326 | |
| 327 | // Compute the true adjustment. |
| 328 | llvm::Value *Adj = RawAdj; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 329 | if (UseARMMethodPtrABI) |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 330 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 331 | |
| 332 | // Apply the adjustment and cast back to the original struct type |
| 333 | // for consistency. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 334 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 335 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 336 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 337 | |
| 338 | // Load the function pointer. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 339 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 340 | |
| 341 | // If the LSB in the function pointer is 1, the function pointer points to |
| 342 | // a virtual function. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 343 | llvm::Value *IsVirtual; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 344 | if (UseARMMethodPtrABI) |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 345 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 346 | else |
| 347 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 348 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 349 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 350 | |
| 351 | // In the virtual path, the adjustment left 'This' pointing to the |
| 352 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 353 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 354 | CGF.EmitBlock(FnVirtual); |
| 355 | |
| 356 | // Cast the adjusted this to a pointer to vtable pointer and load. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 357 | llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 358 | llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo()); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 359 | VTable = Builder.CreateLoad(VTable, "memptr.vtable"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 360 | |
| 361 | // Apply the offset. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 362 | llvm::Value *VTableOffset = FnAsInt; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 363 | if (!UseARMMethodPtrABI) |
| 364 | VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 365 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 366 | |
| 367 | // Load the virtual function to call. |
| 368 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 369 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 370 | CGF.EmitBranch(FnEnd); |
| 371 | |
| 372 | // In the non-virtual path, the function pointer is actually a |
| 373 | // function pointer. |
| 374 | CGF.EmitBlock(FnNonVirtual); |
| 375 | llvm::Value *NonVirtualFn = |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 376 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 377 | |
| 378 | // We're done. |
| 379 | CGF.EmitBlock(FnEnd); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 380 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 381 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 382 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 383 | return Callee; |
| 384 | } |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 385 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 386 | /// Compute an l-value by applying the given pointer-to-member to a |
| 387 | /// base object. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 388 | llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( |
| 389 | CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, |
| 390 | const MemberPointerType *MPT) { |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 391 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 392 | |
| 393 | CGBuilderTy &Builder = CGF.Builder; |
| 394 | |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 395 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 396 | |
| 397 | // Cast to char*. |
| 398 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 399 | |
| 400 | // Apply the offset, which we assume is non-null. |
| 401 | llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); |
| 402 | |
| 403 | // Cast the address to the appropriate pointer type, adopting the |
| 404 | // address space of the base pointer. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 405 | llvm::Type *PType |
Douglas Gregor | 04f3621 | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 406 | = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 407 | return Builder.CreateBitCast(Addr, PType); |
| 408 | } |
| 409 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 410 | /// Perform a bitcast, derived-to-base, or base-to-derived member pointer |
| 411 | /// conversion. |
| 412 | /// |
| 413 | /// Bitcast conversions are always a no-op under Itanium. |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 414 | /// |
| 415 | /// Obligatory offset/adjustment diagram: |
| 416 | /// <-- offset --> <-- adjustment --> |
| 417 | /// |--------------------------|----------------------|--------------------| |
| 418 | /// ^Derived address point ^Base address point ^Member address point |
| 419 | /// |
| 420 | /// So when converting a base member pointer to a derived member pointer, |
| 421 | /// we add the offset to the adjustment because the address point has |
| 422 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 423 | /// we subtract the offset from the adjustment because the address point |
| 424 | /// has increased. |
| 425 | /// |
| 426 | /// The standard forbids (at compile time) conversion to and from |
| 427 | /// virtual bases, which is why we don't have to consider them here. |
| 428 | /// |
| 429 | /// The standard forbids (at run time) casting a derived MP to a base |
| 430 | /// MP when the derived MP does not point to a member of the base. |
| 431 | /// This is why -1 is a reasonable choice for null data member |
| 432 | /// pointers. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 433 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 434 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 435 | const CastExpr *E, |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 436 | llvm::Value *src) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 437 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 438 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 439 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 440 | |
| 441 | // Under Itanium, reinterprets don't require any additional processing. |
| 442 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 443 | |
| 444 | // Use constant emission if we can. |
| 445 | if (isa<llvm::Constant>(src)) |
| 446 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); |
| 447 | |
| 448 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 449 | if (!adj) return src; |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 450 | |
| 451 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 452 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 453 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 454 | const MemberPointerType *destTy = |
| 455 | E->getType()->castAs<MemberPointerType>(); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 456 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 457 | // For member data pointers, this is just a matter of adding the |
| 458 | // offset if the source is non-null. |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 459 | if (destTy->isMemberDataPointer()) { |
| 460 | llvm::Value *dst; |
| 461 | if (isDerivedToBase) |
| 462 | dst = Builder.CreateNSWSub(src, adj, "adj"); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 463 | else |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 464 | dst = Builder.CreateNSWAdd(src, adj, "adj"); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 465 | |
| 466 | // Null check. |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 467 | llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); |
| 468 | llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); |
| 469 | return Builder.CreateSelect(isNull, src, dst); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 470 | } |
| 471 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 472 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 473 | if (UseARMMethodPtrABI) { |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 474 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 475 | offset <<= 1; |
| 476 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 477 | } |
| 478 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 479 | llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); |
| 480 | llvm::Value *dstAdj; |
| 481 | if (isDerivedToBase) |
| 482 | dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 483 | else |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 484 | dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 485 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 486 | return Builder.CreateInsertValue(src, dstAdj, 1); |
| 487 | } |
| 488 | |
| 489 | llvm::Constant * |
| 490 | ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 491 | llvm::Constant *src) { |
| 492 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 493 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 494 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 495 | |
| 496 | // Under Itanium, reinterprets don't require any additional processing. |
| 497 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 498 | |
| 499 | // If the adjustment is trivial, we don't need to do anything. |
| 500 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 501 | if (!adj) return src; |
| 502 | |
| 503 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 504 | |
| 505 | const MemberPointerType *destTy = |
| 506 | E->getType()->castAs<MemberPointerType>(); |
| 507 | |
| 508 | // For member data pointers, this is just a matter of adding the |
| 509 | // offset if the source is non-null. |
| 510 | if (destTy->isMemberDataPointer()) { |
| 511 | // null maps to null. |
| 512 | if (src->isAllOnesValue()) return src; |
| 513 | |
| 514 | if (isDerivedToBase) |
| 515 | return llvm::ConstantExpr::getNSWSub(src, adj); |
| 516 | else |
| 517 | return llvm::ConstantExpr::getNSWAdd(src, adj); |
| 518 | } |
| 519 | |
| 520 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 521 | if (UseARMMethodPtrABI) { |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 522 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 523 | offset <<= 1; |
| 524 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
| 525 | } |
| 526 | |
| 527 | llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); |
| 528 | llvm::Constant *dstAdj; |
| 529 | if (isDerivedToBase) |
| 530 | dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); |
| 531 | else |
| 532 | dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); |
| 533 | |
| 534 | return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 535 | } |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 536 | |
| 537 | llvm::Constant * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 538 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 539 | // Itanium C++ ABI 2.3: |
| 540 | // A NULL pointer is represented as -1. |
| 541 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 542 | return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 543 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 544 | llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 545 | llvm::Constant *Values[2] = { Zero, Zero }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 546 | return llvm::ConstantStruct::getAnon(Values); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 547 | } |
| 548 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 549 | llvm::Constant * |
| 550 | ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 551 | CharUnits offset) { |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 552 | // Itanium C++ ABI 2.3: |
| 553 | // A pointer to data member is an offset from the base address of |
| 554 | // the class object containing it, represented as a ptrdiff_t |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 555 | return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 556 | } |
| 557 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 558 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 559 | return BuildMemberPointer(MD, CharUnits::Zero()); |
| 560 | } |
| 561 | |
| 562 | llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, |
| 563 | CharUnits ThisAdjustment) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 564 | assert(MD->isInstance() && "Member function must not be static!"); |
| 565 | MD = MD->getCanonicalDecl(); |
| 566 | |
| 567 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 568 | |
| 569 | // Get the function pointer (or index if this is a virtual function). |
| 570 | llvm::Constant *MemPtr[2]; |
| 571 | if (MD->isVirtual()) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 572 | uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 573 | |
Ken Dyck | df01628 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 574 | const ASTContext &Context = getContext(); |
| 575 | CharUnits PointerWidth = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 576 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | df01628 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 577 | uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 578 | |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 579 | if (UseARMMethodPtrABI) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 580 | // ARM C++ ABI 3.2.1: |
| 581 | // This ABI specifies that adj contains twice the this |
| 582 | // adjustment, plus 1 if the member function is virtual. The |
| 583 | // least significant bit of adj then makes exactly the same |
| 584 | // discrimination as the least significant bit of ptr does for |
| 585 | // Itanium. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 586 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); |
| 587 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 588 | 2 * ThisAdjustment.getQuantity() + 1); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 589 | } else { |
| 590 | // Itanium C++ ABI 2.3: |
| 591 | // For a virtual function, [the pointer field] is 1 plus the |
| 592 | // virtual table offset (in bytes) of the function, |
| 593 | // represented as a ptrdiff_t. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 594 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); |
| 595 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 596 | ThisAdjustment.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 597 | } |
| 598 | } else { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 599 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 600 | llvm::Type *Ty; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 601 | // Check whether the function has a computable LLVM signature. |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 602 | if (Types.isFuncTypeConvertible(FPT)) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 603 | // The function has a computable LLVM signature; use the correct type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 604 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 605 | } else { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 606 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 607 | // function type is incomplete. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 608 | Ty = CGM.PtrDiffTy; |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 609 | } |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 610 | llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 611 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 612 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 613 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
| 614 | (UseARMMethodPtrABI ? 2 : 1) * |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 615 | ThisAdjustment.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 616 | } |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 617 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 618 | return llvm::ConstantStruct::getAnon(MemPtr); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 621 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, |
| 622 | QualType MPType) { |
| 623 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 624 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 625 | if (!MPD) |
| 626 | return EmitNullMemberPointer(MPT); |
| 627 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 628 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 629 | |
| 630 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 631 | return BuildMemberPointer(MD, ThisAdjustment); |
| 632 | |
| 633 | CharUnits FieldOffset = |
| 634 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 635 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
| 636 | } |
| 637 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 638 | /// The comparison algorithm is pretty easy: the member pointers are |
| 639 | /// the same if they're either bitwise identical *or* both null. |
| 640 | /// |
| 641 | /// ARM is different here only because null-ness is more complicated. |
| 642 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 643 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 644 | llvm::Value *L, |
| 645 | llvm::Value *R, |
| 646 | const MemberPointerType *MPT, |
| 647 | bool Inequality) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 648 | CGBuilderTy &Builder = CGF.Builder; |
| 649 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 650 | llvm::ICmpInst::Predicate Eq; |
| 651 | llvm::Instruction::BinaryOps And, Or; |
| 652 | if (Inequality) { |
| 653 | Eq = llvm::ICmpInst::ICMP_NE; |
| 654 | And = llvm::Instruction::Or; |
| 655 | Or = llvm::Instruction::And; |
| 656 | } else { |
| 657 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 658 | And = llvm::Instruction::And; |
| 659 | Or = llvm::Instruction::Or; |
| 660 | } |
| 661 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 662 | // Member data pointers are easy because there's a unique null |
| 663 | // value, so it just comes down to bitwise equality. |
| 664 | if (MPT->isMemberDataPointer()) |
| 665 | return Builder.CreateICmp(Eq, L, R); |
| 666 | |
| 667 | // For member function pointers, the tautologies are more complex. |
| 668 | // The Itanium tautology is: |
John McCall | 61a1488 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 669 | // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 670 | // The ARM tautology is: |
John McCall | 61a1488 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 671 | // (L == R) <==> (L.ptr == R.ptr && |
| 672 | // (L.adj == R.adj || |
| 673 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 674 | // The inequality tautologies have exactly the same structure, except |
| 675 | // applying De Morgan's laws. |
| 676 | |
| 677 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 678 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 679 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 680 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 681 | // true for equality to hold. |
| 682 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 683 | |
| 684 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 685 | // tests whether the pointers are both null. ARM imposes an extra |
| 686 | // condition. |
| 687 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 688 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 689 | |
| 690 | // This condition tests whether L.adj == R.adj. If this isn't |
| 691 | // true, the pointers are unequal unless they're both null. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 692 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 693 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 694 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 695 | |
| 696 | // Null member function pointers on ARM clear the low bit of Adj, |
| 697 | // so the zero condition has to check that neither low bit is set. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 698 | if (UseARMMethodPtrABI) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 699 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 700 | |
| 701 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 702 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 703 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 704 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 705 | "cmp.or.adj"); |
| 706 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 707 | } |
| 708 | |
| 709 | // Tie together all our conditions. |
| 710 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 711 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 712 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 713 | return Result; |
| 714 | } |
| 715 | |
| 716 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 717 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 718 | llvm::Value *MemPtr, |
| 719 | const MemberPointerType *MPT) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 720 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 721 | |
| 722 | /// For member data pointers, this is just a check against -1. |
| 723 | if (MPT->isMemberDataPointer()) { |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 724 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 725 | llvm::Value *NegativeOne = |
| 726 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 727 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 728 | } |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 730 | // In Itanium, a member function pointer is not null if 'ptr' is not null. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 731 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 732 | |
| 733 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 734 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 735 | |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 736 | // On ARM, a member function pointer is also non-null if the low bit of 'adj' |
| 737 | // (the virtual bit) is set. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 738 | if (UseARMMethodPtrABI) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 739 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 740 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 741 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 742 | llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, |
| 743 | "memptr.isvirtual"); |
| 744 | Result = Builder.CreateOr(Result, IsVirtual); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return Result; |
| 748 | } |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 749 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 750 | /// The Itanium ABI requires non-zero initialization only for data |
| 751 | /// member pointers, for which '0' is a valid offset. |
| 752 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 753 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 754 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 755 | |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 756 | /// The Itanium ABI always places an offset to the complete object |
| 757 | /// at entry -2 in the vtable. |
| 758 | llvm::Value *ItaniumCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, |
| 759 | llvm::Value *ptr, |
| 760 | QualType type) { |
| 761 | // Grab the vtable pointer as an intptr_t*. |
| 762 | llvm::Value *vtable = CGF.GetVTablePtr(ptr, CGF.IntPtrTy->getPointerTo()); |
| 763 | |
| 764 | // Track back to entry -2 and pull out the offset there. |
| 765 | llvm::Value *offsetPtr = |
| 766 | CGF.Builder.CreateConstInBoundsGEP1_64(vtable, -2, "complete-offset.ptr"); |
| 767 | llvm::LoadInst *offset = CGF.Builder.CreateLoad(offsetPtr); |
| 768 | offset->setAlignment(CGF.PointerAlignInBytes); |
| 769 | |
| 770 | // Apply the offset. |
| 771 | ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); |
| 772 | return CGF.Builder.CreateInBoundsGEP(ptr, offset); |
| 773 | } |
| 774 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 775 | llvm::Value * |
| 776 | ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 777 | llvm::Value *This, |
| 778 | const CXXRecordDecl *ClassDecl, |
| 779 | const CXXRecordDecl *BaseClassDecl) { |
| 780 | llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy); |
| 781 | CharUnits VBaseOffsetOffset = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 782 | CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, |
| 783 | BaseClassDecl); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 784 | |
| 785 | llvm::Value *VBaseOffsetPtr = |
| 786 | CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), |
| 787 | "vbase.offset.ptr"); |
| 788 | VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, |
| 789 | CGM.PtrDiffTy->getPointerTo()); |
| 790 | |
| 791 | llvm::Value *VBaseOffset = |
| 792 | CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 793 | |
| 794 | return VBaseOffset; |
| 795 | } |
| 796 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 797 | /// The generic ABI passes 'this', plus a VTT if it's initializing a |
| 798 | /// base subobject. |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 799 | void |
| 800 | ItaniumCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 801 | CXXCtorType Type, CanQualType &ResTy, |
| 802 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 803 | ASTContext &Context = getContext(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 804 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 805 | // All parameters are already in place except VTT, which goes after 'this'. |
| 806 | // These are Clang types, so we don't need to worry about sret yet. |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 807 | |
| 808 | // Check if we need to add a VTT parameter (which has type void **). |
| 809 | if (Type == Ctor_Base && Ctor->getParent()->getNumVBases() != 0) |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 810 | ArgTys.insert(ArgTys.begin() + 1, |
| 811 | Context.getPointerType(Context.VoidPtrTy)); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 814 | void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 815 | // Just make sure we're in sync with TargetCXXABI. |
| 816 | assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); |
| 817 | |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 818 | // The constructor used for constructing this as a base class; |
| 819 | // ignores virtual bases. |
| 820 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); |
| 821 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 822 | // The constructor used for constructing this as a complete class; |
| 823 | // constucts the virtual bases, then calls the base constructor. |
| 824 | if (!D->getParent()->isAbstract()) { |
| 825 | // We don't need to emit the complete ctor if the class is abstract. |
| 826 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 827 | } |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 828 | } |
| 829 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 830 | /// The generic ABI passes 'this', plus a VTT if it's destroying a |
| 831 | /// base subobject. |
| 832 | void ItaniumCXXABI::BuildDestructorSignature(const CXXDestructorDecl *Dtor, |
| 833 | CXXDtorType Type, |
| 834 | CanQualType &ResTy, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 835 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 836 | ASTContext &Context = getContext(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 837 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 838 | // 'this' parameter is already there, as well as 'this' return if |
| 839 | // HasThisReturn(GlobalDecl(Dtor, Type)) is true |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 840 | |
| 841 | // Check if we need to add a VTT parameter (which has type void **). |
| 842 | if (Type == Dtor_Base && Dtor->getParent()->getNumVBases() != 0) |
| 843 | ArgTys.push_back(Context.getPointerType(Context.VoidPtrTy)); |
| 844 | } |
| 845 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 846 | void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 847 | // The destructor used for destructing this as a base class; ignores |
| 848 | // virtual bases. |
| 849 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 850 | |
| 851 | // The destructor used for destructing this as a most-derived class; |
| 852 | // call the base destructor and then destructs any virtual bases. |
| 853 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
| 854 | |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 855 | // The destructor in a virtual table is always a 'deleting' |
| 856 | // destructor, which calls the complete destructor and then uses the |
| 857 | // appropriate operator delete. |
| 858 | if (D->isVirtual()) |
| 859 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 862 | void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, |
| 863 | QualType &ResTy, |
| 864 | FunctionArgList &Params) { |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 865 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 866 | assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 867 | |
| 868 | // Check if we need a VTT parameter as well. |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 869 | if (NeedsVTTParameter(CGF.CurGD)) { |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 870 | ASTContext &Context = getContext(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 871 | |
| 872 | // FIXME: avoid the fake decl |
| 873 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 874 | ImplicitParamDecl *VTTDecl |
| 875 | = ImplicitParamDecl::Create(Context, 0, MD->getLocation(), |
| 876 | &Context.Idents.get("vtt"), T); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 877 | Params.insert(Params.begin() + 1, VTTDecl); |
Reid Kleckner | 2af6d73 | 2013-12-13 00:09:59 +0000 | [diff] [blame] | 878 | getStructorImplicitParamDecl(CGF) = VTTDecl; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 882 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 883 | /// Initialize the 'this' slot. |
| 884 | EmitThisParam(CGF); |
| 885 | |
| 886 | /// Initialize the 'vtt' slot if needed. |
Reid Kleckner | 2af6d73 | 2013-12-13 00:09:59 +0000 | [diff] [blame] | 887 | if (getStructorImplicitParamDecl(CGF)) { |
| 888 | getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( |
| 889 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 890 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 891 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 892 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 893 | /// the return slot to 'this' at the start of the function. |
| 894 | /// |
| 895 | /// Unlike the setting of return types, this is done within the ABI |
| 896 | /// implementation instead of by clients of CGCXXABI because: |
| 897 | /// 1) getThisValue is currently protected |
| 898 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 899 | /// HasThisReturn only specifies a contract, not the implementation |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 900 | if (HasThisReturn(CGF.CurGD)) |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 901 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 904 | unsigned ItaniumCXXABI::addImplicitConstructorArgs( |
| 905 | CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, |
| 906 | bool ForVirtualBase, bool Delegating, CallArgList &Args) { |
| 907 | if (!NeedsVTTParameter(GlobalDecl(D, Type))) |
| 908 | return 0; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 909 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 910 | // Insert the implicit 'vtt' argument as the second argument. |
| 911 | llvm::Value *VTT = |
| 912 | CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); |
| 913 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
| 914 | Args.insert(Args.begin() + 1, |
| 915 | CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); |
| 916 | return 1; // Added one arg. |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 920 | const CXXDestructorDecl *DD, |
| 921 | CXXDtorType Type, bool ForVirtualBase, |
| 922 | bool Delegating, llvm::Value *This) { |
| 923 | GlobalDecl GD(DD, Type); |
| 924 | llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); |
| 925 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
| 926 | |
| 927 | llvm::Value *Callee = 0; |
| 928 | if (getContext().getLangOpts().AppleKext) |
| 929 | Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); |
| 930 | |
| 931 | if (!Callee) |
| 932 | Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 933 | |
| 934 | if (DD->isVirtual()) |
| 935 | This = adjustThisArgumentForVirtualCall(CGF, GD, This); |
| 936 | |
| 937 | // FIXME: Provide a source location here. |
| 938 | CGF.EmitCXXMemberCall(DD, SourceLocation(), Callee, ReturnValueSlot(), This, |
| 939 | VTT, VTTTy, 0, 0); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 942 | void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 943 | const CXXRecordDecl *RD) { |
| 944 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); |
| 945 | if (VTable->hasInitializer()) |
| 946 | return; |
| 947 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 948 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 949 | const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); |
| 950 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
| 951 | |
| 952 | // Create and set the initializer. |
| 953 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 954 | RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), |
| 955 | VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks()); |
| 956 | VTable->setInitializer(Init); |
| 957 | |
| 958 | // Set the correct linkage. |
| 959 | VTable->setLinkage(Linkage); |
| 960 | |
| 961 | // Set the right visibility. |
John McCall | 8f80a61 | 2014-02-08 00:41:16 +0000 | [diff] [blame] | 962 | CGM.setGlobalVisibility(VTable, RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 963 | |
| 964 | // If this is the magic class __cxxabiv1::__fundamental_type_info, |
| 965 | // we will emit the typeinfo for the fundamental types. This is the |
| 966 | // same behaviour as GCC. |
| 967 | const DeclContext *DC = RD->getDeclContext(); |
| 968 | if (RD->getIdentifier() && |
| 969 | RD->getIdentifier()->isStr("__fundamental_type_info") && |
| 970 | isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && |
| 971 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && |
| 972 | DC->getParent()->isTranslationUnit()) |
| 973 | CGM.EmitFundamentalRTTIDescriptors(); |
| 974 | } |
| 975 | |
| 976 | llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( |
| 977 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 978 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { |
| 979 | bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD); |
| 980 | NeedsVirtualOffset = (NeedsVTTParam && NearestVBase); |
| 981 | |
| 982 | llvm::Value *VTableAddressPoint; |
| 983 | if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) { |
| 984 | // Get the secondary vpointer index. |
| 985 | uint64_t VirtualPointerIndex = |
| 986 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 987 | |
| 988 | /// Load the VTT. |
| 989 | llvm::Value *VTT = CGF.LoadCXXVTT(); |
| 990 | if (VirtualPointerIndex) |
| 991 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 992 | |
| 993 | // And load the address point from the VTT. |
| 994 | VTableAddressPoint = CGF.Builder.CreateLoad(VTT); |
| 995 | } else { |
| 996 | llvm::Constant *VTable = |
| 997 | CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits()); |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 998 | uint64_t AddressPoint = CGM.getItaniumVTableContext() |
| 999 | .getVTableLayout(VTableClass) |
| 1000 | .getAddressPoint(Base); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1001 | VTableAddressPoint = |
| 1002 | CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
| 1003 | } |
| 1004 | |
| 1005 | return VTableAddressPoint; |
| 1006 | } |
| 1007 | |
| 1008 | llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( |
| 1009 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
| 1010 | llvm::Constant *VTable = getAddrOfVTable(VTableClass, CharUnits()); |
| 1011 | |
| 1012 | // Find the appropriate vtable within the vtable group. |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1013 | uint64_t AddressPoint = CGM.getItaniumVTableContext() |
| 1014 | .getVTableLayout(VTableClass) |
| 1015 | .getAddressPoint(Base); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1016 | llvm::Value *Indices[] = { |
| 1017 | llvm::ConstantInt::get(CGM.Int64Ty, 0), |
| 1018 | llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) |
| 1019 | }; |
| 1020 | |
| 1021 | return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices); |
| 1022 | } |
| 1023 | |
| 1024 | llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 1025 | CharUnits VPtrOffset) { |
| 1026 | assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); |
| 1027 | |
| 1028 | llvm::GlobalVariable *&VTable = VTables[RD]; |
| 1029 | if (VTable) |
| 1030 | return VTable; |
| 1031 | |
| 1032 | // Queue up this v-table for possible deferred emission. |
| 1033 | CGM.addDeferredVTable(RD); |
| 1034 | |
| 1035 | SmallString<256> OutName; |
| 1036 | llvm::raw_svector_ostream Out(OutName); |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1037 | getMangleContext().mangleCXXVTable(RD, Out); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1038 | Out.flush(); |
| 1039 | StringRef Name = OutName.str(); |
| 1040 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1041 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1042 | llvm::ArrayType *ArrayType = llvm::ArrayType::get( |
| 1043 | CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents()); |
| 1044 | |
| 1045 | VTable = CGM.CreateOrReplaceCXXRuntimeVariable( |
| 1046 | Name, ArrayType, llvm::GlobalValue::ExternalLinkage); |
| 1047 | VTable->setUnnamedAddr(true); |
| 1048 | return VTable; |
| 1049 | } |
| 1050 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1051 | llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 1052 | GlobalDecl GD, |
| 1053 | llvm::Value *This, |
| 1054 | llvm::Type *Ty) { |
| 1055 | GD = GD.getCanonicalDecl(); |
| 1056 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 1057 | llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); |
| 1058 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1059 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1060 | llvm::Value *VFuncPtr = |
| 1061 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
| 1062 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 1063 | } |
| 1064 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1065 | void ItaniumCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 1066 | const CXXDestructorDecl *Dtor, |
| 1067 | CXXDtorType DtorType, |
| 1068 | SourceLocation CallLoc, |
| 1069 | llvm::Value *This) { |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1070 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 1071 | |
| 1072 | const CGFunctionInfo *FInfo |
| 1073 | = &CGM.getTypes().arrangeCXXDestructor(Dtor, DtorType); |
| 1074 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1075 | llvm::Value *Callee = |
| 1076 | getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1077 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1078 | CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This, |
| 1079 | /*ImplicitParam=*/0, QualType(), 0, 0); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1082 | void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1083 | CodeGenVTables &VTables = CGM.getVTables(); |
| 1084 | llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1085 | VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1088 | static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, |
| 1089 | llvm::Value *Ptr, |
| 1090 | int64_t NonVirtualAdjustment, |
| 1091 | int64_t VirtualAdjustment, |
| 1092 | bool IsReturnAdjustment) { |
| 1093 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
| 1094 | return Ptr; |
| 1095 | |
| 1096 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
| 1097 | llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 1098 | |
| 1099 | if (NonVirtualAdjustment && !IsReturnAdjustment) { |
| 1100 | // Perform the non-virtual adjustment for a base-to-derived cast. |
| 1101 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 1102 | } |
| 1103 | |
| 1104 | if (VirtualAdjustment) { |
| 1105 | llvm::Type *PtrDiffTy = |
| 1106 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1107 | |
| 1108 | // Perform the virtual adjustment. |
| 1109 | llvm::Value *VTablePtrPtr = |
| 1110 | CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); |
| 1111 | |
| 1112 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 1113 | |
| 1114 | llvm::Value *OffsetPtr = |
| 1115 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 1116 | |
| 1117 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 1118 | |
| 1119 | // Load the adjustment offset from the vtable. |
| 1120 | llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 1121 | |
| 1122 | // Adjust our pointer. |
| 1123 | V = CGF.Builder.CreateInBoundsGEP(V, Offset); |
| 1124 | } |
| 1125 | |
| 1126 | if (NonVirtualAdjustment && IsReturnAdjustment) { |
| 1127 | // Perform the non-virtual adjustment for a derived-to-base cast. |
| 1128 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 1129 | } |
| 1130 | |
| 1131 | // Cast back to the original type. |
| 1132 | return CGF.Builder.CreateBitCast(V, Ptr->getType()); |
| 1133 | } |
| 1134 | |
| 1135 | llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
| 1136 | llvm::Value *This, |
| 1137 | const ThisAdjustment &TA) { |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1138 | return performTypeAdjustment(CGF, This, TA.NonVirtual, |
| 1139 | TA.Virtual.Itanium.VCallOffsetOffset, |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1140 | /*IsReturnAdjustment=*/false); |
| 1141 | } |
| 1142 | |
| 1143 | llvm::Value * |
| 1144 | ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 1145 | const ReturnAdjustment &RA) { |
| 1146 | return performTypeAdjustment(CGF, Ret, RA.NonVirtual, |
| 1147 | RA.Virtual.Itanium.VBaseOffsetOffset, |
| 1148 | /*IsReturnAdjustment=*/true); |
| 1149 | } |
| 1150 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1151 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 1152 | RValue RV, QualType ResultType) { |
| 1153 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 1154 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 1155 | |
| 1156 | // Destructor thunks in the ARM ABI have indeterminate results. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1157 | llvm::Type *T = |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1158 | cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); |
| 1159 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 1160 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 1161 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1162 | |
| 1163 | /************************** Array allocation cookies **************************/ |
| 1164 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1165 | CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
| 1166 | // The array cookie is a size_t; pad that up to the element alignment. |
| 1167 | // The cookie is actually right-justified in that space. |
| 1168 | return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), |
| 1169 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1173 | llvm::Value *NewPtr, |
| 1174 | llvm::Value *NumElements, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1175 | const CXXNewExpr *expr, |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1176 | QualType ElementType) { |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1177 | assert(requiresArrayCookie(expr)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1178 | |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1179 | unsigned AS = NewPtr->getType()->getPointerAddressSpace(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1180 | |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1181 | ASTContext &Ctx = getContext(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1182 | QualType SizeTy = Ctx.getSizeType(); |
| 1183 | CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); |
| 1184 | |
| 1185 | // The size of the cookie. |
| 1186 | CharUnits CookieSize = |
| 1187 | std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1188 | assert(CookieSize == getArrayCookieSizeImpl(ElementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1189 | |
| 1190 | // Compute an offset to the cookie. |
| 1191 | llvm::Value *CookiePtr = NewPtr; |
| 1192 | CharUnits CookieOffset = CookieSize - SizeSize; |
| 1193 | if (!CookieOffset.isZero()) |
| 1194 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, |
| 1195 | CookieOffset.getQuantity()); |
| 1196 | |
| 1197 | // Write the number of elements into the appropriate slot. |
| 1198 | llvm::Value *NumElementsPtr |
| 1199 | = CGF.Builder.CreateBitCast(CookiePtr, |
| 1200 | CGF.ConvertType(SizeTy)->getPointerTo(AS)); |
| 1201 | CGF.Builder.CreateStore(NumElements, NumElementsPtr); |
| 1202 | |
| 1203 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1204 | // over the cookie completely. |
| 1205 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 1206 | CookieSize.getQuantity()); |
| 1207 | } |
| 1208 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1209 | llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1210 | llvm::Value *allocPtr, |
| 1211 | CharUnits cookieSize) { |
| 1212 | // The element size is right-justified in the cookie. |
| 1213 | llvm::Value *numElementsPtr = allocPtr; |
| 1214 | CharUnits numElementsOffset = |
| 1215 | cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); |
| 1216 | if (!numElementsOffset.isZero()) |
| 1217 | numElementsPtr = |
| 1218 | CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, |
| 1219 | numElementsOffset.getQuantity()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1220 | |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1221 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1222 | numElementsPtr = |
| 1223 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1224 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1227 | CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1228 | // ARM says that the cookie is always: |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1229 | // struct array_cookie { |
| 1230 | // std::size_t element_size; // element_size != 0 |
| 1231 | // std::size_t element_count; |
| 1232 | // }; |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1233 | // But the base ABI doesn't give anything an alignment greater than |
| 1234 | // 8, so we can dismiss this as typical ABI-author blindness to |
| 1235 | // actual language complexity and round up to the element alignment. |
| 1236 | return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), |
| 1237 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1241 | llvm::Value *newPtr, |
| 1242 | llvm::Value *numElements, |
John McCall | 284c48f | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1243 | const CXXNewExpr *expr, |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1244 | QualType elementType) { |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1245 | assert(requiresArrayCookie(expr)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1246 | |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1247 | // NewPtr is a char*, but we generalize to arbitrary addrspaces. |
| 1248 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1249 | |
| 1250 | // The cookie is always at the start of the buffer. |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1251 | llvm::Value *cookie = newPtr; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1252 | |
| 1253 | // The first element is the element size. |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1254 | cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); |
| 1255 | llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, |
| 1256 | getContext().getTypeSizeInChars(elementType).getQuantity()); |
| 1257 | CGF.Builder.CreateStore(elementSize, cookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1258 | |
| 1259 | // The second element is the element count. |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1260 | cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); |
| 1261 | CGF.Builder.CreateStore(numElements, cookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1262 | |
| 1263 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1264 | // over the cookie completely. |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1265 | CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); |
| 1266 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 1267 | cookieSize.getQuantity()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1270 | llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1271 | llvm::Value *allocPtr, |
| 1272 | CharUnits cookieSize) { |
| 1273 | // The number of elements is at offset sizeof(size_t) relative to |
| 1274 | // the allocated pointer. |
| 1275 | llvm::Value *numElementsPtr |
| 1276 | = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1277 | |
Micah Villmow | ea2fea2 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1278 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1279 | numElementsPtr = |
| 1280 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1281 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1284 | /*********************** Static local initialization **************************/ |
| 1285 | |
| 1286 | static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1287 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1288 | // int __cxa_guard_acquire(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1289 | llvm::FunctionType *FTy = |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1290 | llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1291 | GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1292 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1293 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1294 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1295 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1299 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1300 | // void __cxa_guard_release(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1301 | llvm::FunctionType *FTy = |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1302 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1303 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1304 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1305 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1306 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1310 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1311 | // void __cxa_guard_abort(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1312 | llvm::FunctionType *FTy = |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1313 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1314 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1315 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1316 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1317 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | namespace { |
| 1321 | struct CallGuardAbort : EHScopeStack::Cleanup { |
| 1322 | llvm::GlobalVariable *Guard; |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1323 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1324 | |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1325 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1326 | CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), |
| 1327 | Guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1328 | } |
| 1329 | }; |
| 1330 | } |
| 1331 | |
| 1332 | /// The ARM code here follows the Itanium code closely enough that we |
| 1333 | /// just special-case it at particular places. |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1334 | void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, |
| 1335 | const VarDecl &D, |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1336 | llvm::GlobalVariable *var, |
| 1337 | bool shouldPerformInit) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1338 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1339 | |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1340 | // We only need to use thread-safe statics for local non-TLS variables; |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1341 | // global initialization is always single-threaded. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1342 | bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && |
| 1343 | D.isLocalVarDecl() && !D.getTLSKind(); |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1344 | |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1345 | // If we have a global variable with internal linkage and thread-safe statics |
| 1346 | // are disabled, we can just let the guard variable be of type i8. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1347 | bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); |
| 1348 | |
| 1349 | llvm::IntegerType *guardTy; |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1350 | if (useInt8GuardVariable) { |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1351 | guardTy = CGF.Int8Ty; |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1352 | } else { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 1353 | // Guard variables are 64 bits in the generic ABI and size width on ARM |
| 1354 | // (i.e. 32-bit on AArch32, 64-bit on AArch64). |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 1355 | guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty); |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1356 | } |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1357 | llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1358 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1359 | // Create the guard variable if we don't already have it (as we |
| 1360 | // might if we're double-emitting this function body). |
| 1361 | llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); |
| 1362 | if (!guard) { |
| 1363 | // Mangle the name for the guard. |
| 1364 | SmallString<256> guardName; |
| 1365 | { |
| 1366 | llvm::raw_svector_ostream out(guardName); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1367 | getMangleContext().mangleStaticGuardVariable(&D, out); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1368 | out.flush(); |
| 1369 | } |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1370 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1371 | // Create the guard variable with a zero-initializer. |
| 1372 | // Just absorb linkage and visibility from the guarded variable. |
| 1373 | guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, |
| 1374 | false, var->getLinkage(), |
| 1375 | llvm::ConstantInt::get(guardTy, 0), |
| 1376 | guardName.str()); |
| 1377 | guard->setVisibility(var->getVisibility()); |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1378 | // If the variable is thread-local, so is its guard variable. |
| 1379 | guard->setThreadLocalMode(var->getThreadLocalMode()); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1380 | |
| 1381 | CGM.setStaticLocalDeclGuardAddress(&D, guard); |
| 1382 | } |
John McCall | 87590e6 | 2012-03-30 07:09:50 +0000 | [diff] [blame] | 1383 | |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1384 | // Test whether the variable has completed initialization. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1385 | llvm::Value *isInitialized; |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1386 | |
| 1387 | // ARM C++ ABI 3.2.3.1: |
| 1388 | // To support the potential use of initialization guard variables |
| 1389 | // as semaphores that are the target of ARM SWP and LDREX/STREX |
| 1390 | // synchronizing instructions we define a static initialization |
| 1391 | // guard variable to be a 4-byte aligned, 4- byte word with the |
| 1392 | // following inline access protocol. |
| 1393 | // #define INITIALIZED 1 |
| 1394 | // if ((obj_guard & INITIALIZED) != INITIALIZED) { |
| 1395 | // if (__cxa_guard_acquire(&obj_guard)) |
| 1396 | // ... |
| 1397 | // } |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 1398 | if (UseARMGuardVarABI && !useInt8GuardVariable) { |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1399 | llvm::Value *V = Builder.CreateLoad(guard); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 1400 | llvm::Value *Test1 = llvm::ConstantInt::get(guardTy, 1); |
| 1401 | V = Builder.CreateAnd(V, Test1); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1402 | isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Itanium C++ ABI 3.3.2: |
| 1405 | // The following is pseudo-code showing how these functions can be used: |
| 1406 | // if (obj_guard.first_byte == 0) { |
| 1407 | // if ( __cxa_guard_acquire (&obj_guard) ) { |
| 1408 | // try { |
| 1409 | // ... initialize the object ...; |
| 1410 | // } catch (...) { |
| 1411 | // __cxa_guard_abort (&obj_guard); |
| 1412 | // throw; |
| 1413 | // } |
| 1414 | // ... queue object destructor with __cxa_atexit() ...; |
| 1415 | // __cxa_guard_release (&obj_guard); |
| 1416 | // } |
| 1417 | // } |
| 1418 | } else { |
| 1419 | // Load the first byte of the guard variable. |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1420 | llvm::LoadInst *LI = |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1421 | Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1422 | LI->setAlignment(1); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1423 | |
Eli Friedman | 84d2812 | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 1424 | // Itanium ABI: |
| 1425 | // An implementation supporting thread-safety on multiprocessor |
| 1426 | // systems must also guarantee that references to the initialized |
| 1427 | // object do not occur before the load of the initialization flag. |
| 1428 | // |
| 1429 | // In LLVM, we do this by marking the load Acquire. |
| 1430 | if (threadsafe) |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1431 | LI->setAtomic(llvm::Acquire); |
Eli Friedman | 84d2812 | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 1432 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1433 | isInitialized = Builder.CreateIsNull(LI, "guard.uninitialized"); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); |
| 1437 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1438 | |
| 1439 | // Check if the first byte of the guard variable is zero. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1440 | Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1441 | |
| 1442 | CGF.EmitBlock(InitCheckBlock); |
| 1443 | |
| 1444 | // Variables used when coping with thread-safe statics and exceptions. |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1445 | if (threadsafe) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1446 | // Call __cxa_guard_acquire. |
| 1447 | llvm::Value *V |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1448 | = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1449 | |
| 1450 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1451 | |
| 1452 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 1453 | InitBlock, EndBlock); |
| 1454 | |
| 1455 | // Call __cxa_guard_abort along the exceptional edge. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1456 | CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1457 | |
| 1458 | CGF.EmitBlock(InitBlock); |
| 1459 | } |
| 1460 | |
| 1461 | // Emit the initializer and add a global destructor if appropriate. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1462 | CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1463 | |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1464 | if (threadsafe) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1465 | // Pop the guard-abort cleanup if we pushed one. |
| 1466 | CGF.PopCleanupBlock(); |
| 1467 | |
| 1468 | // Call __cxa_guard_release. This cannot throw. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1469 | CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1470 | } else { |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1471 | Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | CGF.EmitBlock(EndBlock); |
| 1475 | } |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1476 | |
| 1477 | /// Register a global destructor using __cxa_atexit. |
| 1478 | static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, |
| 1479 | llvm::Constant *dtor, |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1480 | llvm::Constant *addr, |
| 1481 | bool TLS) { |
Bill Wendling | 95cae88 | 2013-05-02 19:18:03 +0000 | [diff] [blame] | 1482 | const char *Name = "__cxa_atexit"; |
| 1483 | if (TLS) { |
| 1484 | const llvm::Triple &T = CGF.getTarget().getTriple(); |
| 1485 | Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit"; |
| 1486 | } |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1487 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1488 | // We're assuming that the destructor function is something we can |
| 1489 | // reasonably call with the default CC. Go ahead and cast it to the |
| 1490 | // right prototype. |
| 1491 | llvm::Type *dtorTy = |
| 1492 | llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); |
| 1493 | |
| 1494 | // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); |
| 1495 | llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; |
| 1496 | llvm::FunctionType *atexitTy = |
| 1497 | llvm::FunctionType::get(CGF.IntTy, paramTys, false); |
| 1498 | |
| 1499 | // Fetch the actual function. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1500 | llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1501 | if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) |
| 1502 | fn->setDoesNotThrow(); |
| 1503 | |
| 1504 | // Create a variable that binds the atexit to this shared object. |
| 1505 | llvm::Constant *handle = |
| 1506 | CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); |
| 1507 | |
| 1508 | llvm::Value *args[] = { |
| 1509 | llvm::ConstantExpr::getBitCast(dtor, dtorTy), |
| 1510 | llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), |
| 1511 | handle |
| 1512 | }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1513 | CGF.EmitNounwindRuntimeCall(atexit, args); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /// Register a global destructor as best as we know how. |
| 1517 | void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1518 | const VarDecl &D, |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1519 | llvm::Constant *dtor, |
| 1520 | llvm::Constant *addr) { |
| 1521 | // Use __cxa_atexit if available. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1522 | if (CGM.getCodeGenOpts().CXAAtExit) |
| 1523 | return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); |
| 1524 | |
| 1525 | if (D.getTLSKind()) |
| 1526 | CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1527 | |
| 1528 | // In Apple kexts, we want to add a global destructor entry. |
| 1529 | // FIXME: shouldn't this be guarded by some variable? |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1530 | if (CGM.getLangOpts().AppleKext) { |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1531 | // Generate a global destructor entry. |
| 1532 | return CGM.AddCXXDtorEntry(dtor, addr); |
| 1533 | } |
| 1534 | |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 1535 | CGF.registerGlobalDtorWithAtExit(D, dtor, addr); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1536 | } |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1537 | |
| 1538 | /// Get the appropriate linkage for the wrapper function. This is essentially |
| 1539 | /// the weak form of the variable's linkage; every translation unit which wneeds |
| 1540 | /// the wrapper emits a copy, and we want the linker to merge them. |
| 1541 | static llvm::GlobalValue::LinkageTypes getThreadLocalWrapperLinkage( |
| 1542 | llvm::GlobalValue::LinkageTypes VarLinkage) { |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1543 | // For internal linkage variables, we don't need an external or weak wrapper. |
| 1544 | if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) |
| 1545 | return VarLinkage; |
| 1546 | return llvm::GlobalValue::WeakODRLinkage; |
| 1547 | } |
| 1548 | |
| 1549 | llvm::Function * |
| 1550 | ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, |
| 1551 | llvm::GlobalVariable *Var) { |
| 1552 | // Mangle the name for the thread_local wrapper function. |
| 1553 | SmallString<256> WrapperName; |
| 1554 | { |
| 1555 | llvm::raw_svector_ostream Out(WrapperName); |
| 1556 | getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); |
| 1557 | Out.flush(); |
| 1558 | } |
| 1559 | |
| 1560 | if (llvm::Value *V = Var->getParent()->getNamedValue(WrapperName)) |
| 1561 | return cast<llvm::Function>(V); |
| 1562 | |
| 1563 | llvm::Type *RetTy = Var->getType(); |
| 1564 | if (VD->getType()->isReferenceType()) |
| 1565 | RetTy = RetTy->getPointerElementType(); |
| 1566 | |
| 1567 | llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); |
| 1568 | llvm::Function *Wrapper = llvm::Function::Create( |
| 1569 | FnTy, getThreadLocalWrapperLinkage(Var->getLinkage()), WrapperName.str(), |
| 1570 | &CGM.getModule()); |
| 1571 | // Always resolve references to the wrapper at link time. |
| 1572 | Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1573 | return Wrapper; |
| 1574 | } |
| 1575 | |
| 1576 | void ItaniumCXXABI::EmitThreadLocalInitFuncs( |
| 1577 | llvm::ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *> > Decls, |
| 1578 | llvm::Function *InitFunc) { |
| 1579 | for (unsigned I = 0, N = Decls.size(); I != N; ++I) { |
| 1580 | const VarDecl *VD = Decls[I].first; |
| 1581 | llvm::GlobalVariable *Var = Decls[I].second; |
| 1582 | |
| 1583 | // Mangle the name for the thread_local initialization function. |
| 1584 | SmallString<256> InitFnName; |
| 1585 | { |
| 1586 | llvm::raw_svector_ostream Out(InitFnName); |
| 1587 | getMangleContext().mangleItaniumThreadLocalInit(VD, Out); |
| 1588 | Out.flush(); |
| 1589 | } |
| 1590 | |
| 1591 | // If we have a definition for the variable, emit the initialization |
| 1592 | // function as an alias to the global Init function (if any). Otherwise, |
| 1593 | // produce a declaration of the initialization function. |
| 1594 | llvm::GlobalValue *Init = 0; |
| 1595 | bool InitIsInitFunc = false; |
| 1596 | if (VD->hasDefinition()) { |
| 1597 | InitIsInitFunc = true; |
| 1598 | if (InitFunc) |
| 1599 | Init = |
| 1600 | new llvm::GlobalAlias(InitFunc->getType(), Var->getLinkage(), |
| 1601 | InitFnName.str(), InitFunc, &CGM.getModule()); |
| 1602 | } else { |
| 1603 | // Emit a weak global function referring to the initialization function. |
| 1604 | // This function will not exist if the TU defining the thread_local |
| 1605 | // variable in question does not need any dynamic initialization for |
| 1606 | // its thread_local variables. |
| 1607 | llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); |
| 1608 | Init = llvm::Function::Create( |
| 1609 | FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), |
| 1610 | &CGM.getModule()); |
| 1611 | } |
| 1612 | |
| 1613 | if (Init) |
| 1614 | Init->setVisibility(Var->getVisibility()); |
| 1615 | |
| 1616 | llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); |
| 1617 | llvm::LLVMContext &Context = CGM.getModule().getContext(); |
| 1618 | llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 1619 | CGBuilderTy Builder(Entry); |
| 1620 | if (InitIsInitFunc) { |
| 1621 | if (Init) |
| 1622 | Builder.CreateCall(Init); |
| 1623 | } else { |
| 1624 | // Don't know whether we have an init function. Call it if it exists. |
| 1625 | llvm::Value *Have = Builder.CreateIsNotNull(Init); |
| 1626 | llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 1627 | llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 1628 | Builder.CreateCondBr(Have, InitBB, ExitBB); |
| 1629 | |
| 1630 | Builder.SetInsertPoint(InitBB); |
| 1631 | Builder.CreateCall(Init); |
| 1632 | Builder.CreateBr(ExitBB); |
| 1633 | |
| 1634 | Builder.SetInsertPoint(ExitBB); |
| 1635 | } |
| 1636 | |
| 1637 | // For a reference, the result of the wrapper function is a pointer to |
| 1638 | // the referenced object. |
| 1639 | llvm::Value *Val = Var; |
| 1640 | if (VD->getType()->isReferenceType()) { |
| 1641 | llvm::LoadInst *LI = Builder.CreateLoad(Val); |
| 1642 | LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity()); |
| 1643 | Val = LI; |
| 1644 | } |
| 1645 | |
| 1646 | Builder.CreateRet(Val); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | LValue ItaniumCXXABI::EmitThreadLocalDeclRefExpr(CodeGenFunction &CGF, |
| 1651 | const DeclRefExpr *DRE) { |
| 1652 | const VarDecl *VD = cast<VarDecl>(DRE->getDecl()); |
| 1653 | QualType T = VD->getType(); |
| 1654 | llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); |
| 1655 | llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); |
| 1656 | llvm::Function *Wrapper = |
| 1657 | getOrCreateThreadLocalWrapper(VD, cast<llvm::GlobalVariable>(Val)); |
| 1658 | |
| 1659 | Val = CGF.Builder.CreateCall(Wrapper); |
| 1660 | |
| 1661 | LValue LV; |
| 1662 | if (VD->getType()->isReferenceType()) |
| 1663 | LV = CGF.MakeNaturalAlignAddrLValue(Val, T); |
| 1664 | else |
| 1665 | LV = CGF.MakeAddrLValue(Val, DRE->getType(), |
| 1666 | CGF.getContext().getDeclAlign(VD)); |
| 1667 | // FIXME: need setObjCGCLValueClass? |
| 1668 | return LV; |
| 1669 | } |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 1670 | |
| 1671 | /// Return whether the given global decl needs a VTT parameter, which it does |
| 1672 | /// if it's a base constructor or destructor with virtual bases. |
| 1673 | bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { |
| 1674 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1675 | |
| 1676 | // We don't have any virtual bases, just return early. |
| 1677 | if (!MD->getParent()->getNumVBases()) |
| 1678 | return false; |
| 1679 | |
| 1680 | // Check if we have a base constructor. |
| 1681 | if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) |
| 1682 | return true; |
| 1683 | |
| 1684 | // Check if we have a base destructor. |
| 1685 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 1686 | return true; |
| 1687 | |
| 1688 | return false; |
| 1689 | } |