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