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