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