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