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