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