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