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