Charles Davis | 4e786dd | 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 | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Itanium C++ ABI. The class |
Charles Davis | 4e786dd | 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 | 8635341 | 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 | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "CGCXXABI.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 22 | #include "CGCleanup.h" |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 23 | #include "CGRecordLayout.h" |
Charles Davis | a325a6e | 2012-06-23 23:44:00 +0000 | [diff] [blame] | 24 | #include "CGVTables.h" |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 25 | #include "CodeGenFunction.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 26 | #include "CodeGenModule.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 27 | #include "TargetInfo.h" |
Craig Topper | c9ee1d0 | 2012-09-15 18:47:51 +0000 | [diff] [blame] | 28 | #include "clang/AST/Mangle.h" |
| 29 | #include "clang/AST/Type.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 30 | #include "clang/AST/StmtCXX.h" |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 31 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DataLayout.h" |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Intrinsics.h" |
| 35 | #include "llvm/IR/Value.h" |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace clang; |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 38 | using namespace CodeGen; |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 41 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 42 | /// VTables - All the vtables which have been defined. |
| 43 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; |
| 44 | |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 45 | protected: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 46 | bool UseARMMethodPtrABI; |
| 47 | bool UseARMGuardVarABI; |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 48 | |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 49 | ItaniumMangleContext &getMangleContext() { |
| 50 | return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 51 | } |
| 52 | |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 53 | public: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 54 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, |
| 55 | bool UseARMMethodPtrABI = false, |
| 56 | bool UseARMGuardVarABI = false) : |
| 57 | CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI), |
| 58 | UseARMGuardVarABI(UseARMGuardVarABI) { } |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 59 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 60 | bool classifyReturnType(CGFunctionInfo &FI) const override; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 61 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 62 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override { |
Reid Kleckner | d355ca7 | 2014-05-15 01:26:32 +0000 | [diff] [blame] | 63 | // Structures with either a non-trivial destructor or a non-trivial |
| 64 | // copy constructor are always indirect. |
| 65 | // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared |
| 66 | // special members. |
| 67 | if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 68 | return RAA_Indirect; |
| 69 | return RAA_Default; |
| 70 | } |
| 71 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 72 | bool isThisCompleteObject(GlobalDecl GD) const override { |
| 73 | // The Itanium ABI has separate complete-object vs. base-object |
| 74 | // variants of both constructors and destructors. |
| 75 | if (isa<CXXDestructorDecl>(GD.getDecl())) { |
| 76 | switch (GD.getDtorType()) { |
| 77 | case Dtor_Complete: |
| 78 | case Dtor_Deleting: |
| 79 | return true; |
| 80 | |
| 81 | case Dtor_Base: |
| 82 | return false; |
| 83 | |
| 84 | case Dtor_Comdat: |
| 85 | llvm_unreachable("emitting dtor comdat as function?"); |
| 86 | } |
| 87 | llvm_unreachable("bad dtor kind"); |
| 88 | } |
| 89 | if (isa<CXXConstructorDecl>(GD.getDecl())) { |
| 90 | switch (GD.getCtorType()) { |
| 91 | case Ctor_Complete: |
| 92 | return true; |
| 93 | |
| 94 | case Ctor_Base: |
| 95 | return false; |
| 96 | |
| 97 | case Ctor_CopyingClosure: |
| 98 | case Ctor_DefaultClosure: |
| 99 | llvm_unreachable("closure ctors in Itanium ABI?"); |
| 100 | |
| 101 | case Ctor_Comdat: |
| 102 | llvm_unreachable("emitting ctor comdat as function?"); |
| 103 | } |
| 104 | llvm_unreachable("bad dtor kind"); |
| 105 | } |
| 106 | |
| 107 | // No other kinds. |
| 108 | return false; |
| 109 | } |
| 110 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 111 | bool isZeroInitializable(const MemberPointerType *MPT) override; |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 112 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 113 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 114 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 115 | llvm::Value * |
| 116 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 117 | const Expr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 118 | Address This, |
| 119 | llvm::Value *&ThisPtrForCall, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 120 | llvm::Value *MemFnPtr, |
| 121 | const MemberPointerType *MPT) override; |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 122 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 123 | llvm::Value * |
| 124 | EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 125 | Address Base, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 126 | llvm::Value *MemPtr, |
| 127 | const MemberPointerType *MPT) override; |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 128 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 129 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 130 | const CastExpr *E, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 131 | llvm::Value *Src) override; |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 132 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 133 | llvm::Constant *Src) override; |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 134 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 135 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 136 | |
David Majnemer | e2be95b | 2015-06-23 07:31:01 +0000 | [diff] [blame] | 137 | llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD) override; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 138 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 139 | CharUnits offset) override; |
| 140 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 141 | llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, |
| 142 | CharUnits ThisAdjustment); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 143 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 144 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 145 | llvm::Value *L, llvm::Value *R, |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 146 | const MemberPointerType *MPT, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 147 | bool Inequality) override; |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 148 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 149 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 150 | llvm::Value *Addr, |
| 151 | const MemberPointerType *MPT) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 152 | |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 153 | void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 154 | Address Ptr, QualType ElementType, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 155 | const CXXDestructorDecl *Dtor) override; |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 156 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 157 | /// Itanium says that an _Unwind_Exception has to be "double-word" |
| 158 | /// aligned (and thus the end of it is also so-aligned), meaning 16 |
| 159 | /// bytes. Of course, that was written for the actual Itanium, |
| 160 | /// which is a 64-bit platform. Classically, the ABI doesn't really |
| 161 | /// specify the alignment on other platforms, but in practice |
| 162 | /// libUnwind declares the struct with __attribute__((aligned)), so |
| 163 | /// we assume that alignment here. (It's generally 16 bytes, but |
| 164 | /// some targets overwrite it.) |
| 165 | CharUnits getAlignmentOfExnObject() { |
| 166 | auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned(); |
| 167 | return CGM.getContext().toCharUnitsFromBits(align); |
| 168 | } |
| 169 | |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 170 | void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 171 | void emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) override; |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 172 | |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 173 | void emitBeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *C) override; |
| 174 | |
| 175 | llvm::CallInst * |
| 176 | emitTerminateForUnexpectedException(CodeGenFunction &CGF, |
| 177 | llvm::Value *Exn) override; |
| 178 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 179 | void EmitFundamentalRTTIDescriptor(QualType Type); |
| 180 | void EmitFundamentalRTTIDescriptors(); |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 181 | llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 182 | CatchTypeInfo |
David Majnemer | 37b417f | 2015-03-29 21:55:10 +0000 | [diff] [blame] | 183 | getAddrOfCXXCatchHandlerType(QualType Ty, |
| 184 | QualType CatchHandlerType) override { |
Reid Kleckner | 10aa770 | 2015-09-16 20:15:55 +0000 | [diff] [blame] | 185 | return CatchTypeInfo{getAddrOfRTTIDescriptor(Ty), 0}; |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 186 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 187 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 188 | bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; |
| 189 | void EmitBadTypeidCall(CodeGenFunction &CGF) override; |
| 190 | llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 191 | Address ThisPtr, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 192 | llvm::Type *StdTypeInfoPtrTy) override; |
| 193 | |
| 194 | bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 195 | QualType SrcRecordTy) override; |
| 196 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 197 | llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, Address Value, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 198 | QualType SrcRecordTy, QualType DestTy, |
| 199 | QualType DestRecordTy, |
| 200 | llvm::BasicBlock *CastEnd) override; |
| 201 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 202 | llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, Address Value, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 203 | QualType SrcRecordTy, |
| 204 | QualType DestTy) override; |
| 205 | |
| 206 | bool EmitBadCastCall(CodeGenFunction &CGF) override; |
| 207 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 208 | llvm::Value * |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 209 | GetVirtualBaseClassOffset(CodeGenFunction &CGF, Address This, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 210 | const CXXRecordDecl *ClassDecl, |
| 211 | const CXXRecordDecl *BaseClassDecl) override; |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 212 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 213 | void EmitCXXConstructors(const CXXConstructorDecl *D) override; |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 214 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 215 | void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 216 | SmallVectorImpl<CanQualType> &ArgTys) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 217 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 218 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 219 | CXXDtorType DT) const override { |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 220 | // Itanium does not emit any destructor variant as an inline thunk. |
| 221 | // Delegating may occur as an optimization, but all variants are either |
| 222 | // emitted with external linkage or as linkonce if they are inline and used. |
| 223 | return false; |
| 224 | } |
| 225 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 226 | void EmitCXXDestructors(const CXXDestructorDecl *D) override; |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 227 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 228 | void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 229 | FunctionArgList &Params) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 230 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 231 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 232 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 233 | unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, |
| 234 | const CXXConstructorDecl *D, |
| 235 | CXXCtorType Type, bool ForVirtualBase, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 236 | bool Delegating, |
| 237 | CallArgList &Args) override; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 238 | |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 239 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 240 | CXXDtorType Type, bool ForVirtualBase, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 241 | bool Delegating, Address This) override; |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 242 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 243 | void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 244 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 245 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 246 | bool isVirtualOffsetNeededForVTableField(CodeGenFunction &CGF, |
| 247 | CodeGenFunction::VPtr Vptr) override; |
| 248 | |
| 249 | bool doStructorsInitializeVPtrs(const CXXRecordDecl *VTableClass) override { |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | llvm::Constant * |
| 254 | getVTableAddressPoint(BaseSubobject Base, |
| 255 | const CXXRecordDecl *VTableClass) override; |
| 256 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 257 | llvm::Value *getVTableAddressPointInStructor( |
| 258 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 259 | BaseSubobject Base, const CXXRecordDecl *NearestVBase) override; |
| 260 | |
| 261 | llvm::Value *getVTableAddressPointInStructorWithVTT( |
| 262 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 263 | BaseSubobject Base, const CXXRecordDecl *NearestVBase); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 264 | |
| 265 | llvm::Constant * |
| 266 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 267 | const CXXRecordDecl *VTableClass) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 268 | |
| 269 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 270 | CharUnits VPtrOffset) override; |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 271 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 272 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 273 | Address This, llvm::Type *Ty, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 274 | SourceLocation Loc) override; |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 275 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 276 | llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 277 | const CXXDestructorDecl *Dtor, |
| 278 | CXXDtorType DtorType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 279 | Address This, |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 280 | const CXXMemberCallExpr *CE) override; |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 281 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 282 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 283 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 284 | bool canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const override; |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 285 | |
Hans Wennborg | c94391d | 2014-06-06 20:04:01 +0000 | [diff] [blame] | 286 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD, |
| 287 | bool ReturnAdjustment) override { |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 288 | // Allow inlining of thunks by emitting them with available_externally |
| 289 | // linkage together with vtables when needed. |
Peter Collingbourne | 8fabc1b | 2015-07-01 02:10:26 +0000 | [diff] [blame] | 290 | if (ForVTable && !Thunk->hasLocalLinkage()) |
Timur Iskhodzhanov | ad9d3b8 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 291 | Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 292 | } |
| 293 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 294 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, Address This, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 295 | const ThisAdjustment &TA) override; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 296 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 297 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, Address Ret, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 298 | const ReturnAdjustment &RA) override; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 299 | |
David Majnemer | 196ac33 | 2014-09-11 23:05:02 +0000 | [diff] [blame] | 300 | size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, |
| 301 | FunctionArgList &Args) const override { |
| 302 | assert(!Args.empty() && "expected the arglist to not be empty!"); |
| 303 | return Args.size() - 1; |
| 304 | } |
| 305 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 306 | StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; } |
| 307 | StringRef GetDeletedVirtualCallName() override |
| 308 | { return "__cxa_deleted_virtual"; } |
Joao Matos | 2ce88ef | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 310 | CharUnits getArrayCookieSizeImpl(QualType elementType) override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 311 | Address InitializeArrayCookie(CodeGenFunction &CGF, |
| 312 | Address NewPtr, |
| 313 | llvm::Value *NumElements, |
| 314 | const CXXNewExpr *expr, |
| 315 | QualType ElementType) override; |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 316 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 317 | Address allocPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 318 | CharUnits cookieSize) override; |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 319 | |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 320 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 321 | llvm::GlobalVariable *DeclPtr, |
| 322 | bool PerformInit) override; |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 323 | void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 324 | llvm::Constant *dtor, llvm::Constant *addr) override; |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 325 | |
| 326 | llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD, |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 327 | llvm::Value *Val); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 328 | void EmitThreadLocalInitFuncs( |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 329 | CodeGenModule &CGM, |
| 330 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 331 | CXXThreadLocals, |
| 332 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 333 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override; |
| 334 | |
| 335 | bool usesThreadWrapperFunction() const override { return true; } |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 336 | LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, |
| 337 | QualType LValType) override; |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 338 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 339 | bool NeedsVTTParameter(GlobalDecl GD) override; |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 340 | |
| 341 | /**************************** RTTI Uniqueness ******************************/ |
| 342 | |
| 343 | protected: |
| 344 | /// Returns true if the ABI requires RTTI type_info objects to be unique |
| 345 | /// across a program. |
| 346 | virtual bool shouldRTTIBeUnique() const { return true; } |
| 347 | |
| 348 | public: |
| 349 | /// What sort of unique-RTTI behavior should we use? |
| 350 | enum RTTIUniquenessKind { |
| 351 | /// We are guaranteeing, or need to guarantee, that the RTTI string |
| 352 | /// is unique. |
| 353 | RUK_Unique, |
| 354 | |
| 355 | /// We are not guaranteeing uniqueness for the RTTI string, so we |
| 356 | /// can demote to hidden visibility but must use string comparisons. |
| 357 | RUK_NonUniqueHidden, |
| 358 | |
| 359 | /// We are not guaranteeing uniqueness for the RTTI string, so we |
| 360 | /// have to use string comparisons, but we also have to emit it with |
| 361 | /// non-hidden visibility. |
| 362 | RUK_NonUniqueVisible |
| 363 | }; |
| 364 | |
| 365 | /// Return the required visibility status for the given type and linkage in |
| 366 | /// the current ABI. |
| 367 | RTTIUniquenessKind |
| 368 | classifyRTTIUniqueness(QualType CanTy, |
| 369 | llvm::GlobalValue::LinkageTypes Linkage) const; |
| 370 | friend class ItaniumRTTIBuilder; |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 371 | |
| 372 | void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 373 | |
| 374 | private: |
Piotr Padlewski | 1d02f68 | 2015-08-19 20:09:09 +0000 | [diff] [blame] | 375 | bool hasAnyUsedVirtualInlineFunction(const CXXRecordDecl *RD) const { |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 376 | const auto &VtableLayout = |
| 377 | CGM.getItaniumVTableContext().getVTableLayout(RD); |
| 378 | |
| 379 | for (const auto &VtableComponent : VtableLayout.vtable_components()) { |
Piotr Padlewski | 1d02f68 | 2015-08-19 20:09:09 +0000 | [diff] [blame] | 380 | if (!VtableComponent.isUsedFunctionPointerKind()) |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 381 | continue; |
| 382 | |
Piotr Padlewski | 1d02f68 | 2015-08-19 20:09:09 +0000 | [diff] [blame] | 383 | const CXXMethodDecl *Method = VtableComponent.getFunctionDecl(); |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 384 | if (Method->getCanonicalDecl()->isInlined()) |
| 385 | return true; |
| 386 | } |
| 387 | return false; |
| 388 | } |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 389 | |
| 390 | bool isVTableHidden(const CXXRecordDecl *RD) const { |
| 391 | const auto &VtableLayout = |
| 392 | CGM.getItaniumVTableContext().getVTableLayout(RD); |
| 393 | |
| 394 | for (const auto &VtableComponent : VtableLayout.vtable_components()) { |
| 395 | if (VtableComponent.isRTTIKind()) { |
| 396 | const CXXRecordDecl *RTTIDecl = VtableComponent.getRTTIDecl(); |
| 397 | if (RTTIDecl->getVisibility() == Visibility::HiddenVisibility) |
| 398 | return true; |
| 399 | } else if (VtableComponent.isUsedFunctionPointerKind()) { |
| 400 | const CXXMethodDecl *Method = VtableComponent.getFunctionDecl(); |
| 401 | if (Method->getVisibility() == Visibility::HiddenVisibility && |
| 402 | !Method->isDefined()) |
| 403 | return true; |
| 404 | } |
| 405 | } |
| 406 | return false; |
| 407 | } |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 408 | }; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 409 | |
| 410 | class ARMCXXABI : public ItaniumCXXABI { |
| 411 | public: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 412 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : |
| 413 | ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 414 | /* UseARMGuardVarABI = */ true) {} |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 415 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 416 | bool HasThisReturn(GlobalDecl GD) const override { |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 417 | return (isa<CXXConstructorDecl>(GD.getDecl()) || ( |
| 418 | isa<CXXDestructorDecl>(GD.getDecl()) && |
| 419 | GD.getDtorType() != Dtor_Deleting)); |
| 420 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 421 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 422 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, |
| 423 | QualType ResTy) override; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 424 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 425 | CharUnits getArrayCookieSizeImpl(QualType elementType) override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 426 | Address InitializeArrayCookie(CodeGenFunction &CGF, |
| 427 | Address NewPtr, |
| 428 | llvm::Value *NumElements, |
| 429 | const CXXNewExpr *expr, |
| 430 | QualType ElementType) override; |
| 431 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, Address allocPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 432 | CharUnits cookieSize) override; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 433 | }; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 434 | |
| 435 | class iOS64CXXABI : public ARMCXXABI { |
| 436 | public: |
| 437 | iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {} |
Tim Northover | 65f582f | 2014-03-30 17:32:48 +0000 | [diff] [blame] | 438 | |
| 439 | // ARM64 libraries are prepared for non-unique RTTI. |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 440 | bool shouldRTTIBeUnique() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 441 | }; |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 442 | |
| 443 | class WebAssemblyCXXABI final : public ItaniumCXXABI { |
| 444 | public: |
| 445 | explicit WebAssemblyCXXABI(CodeGen::CodeGenModule &CGM) |
| 446 | : ItaniumCXXABI(CGM, /*UseARMMethodPtrABI=*/true, |
| 447 | /*UseARMGuardVarABI=*/true) {} |
| 448 | |
| 449 | private: |
| 450 | bool HasThisReturn(GlobalDecl GD) const override { |
| 451 | return isa<CXXConstructorDecl>(GD.getDecl()) || |
| 452 | (isa<CXXDestructorDecl>(GD.getDecl()) && |
| 453 | GD.getDtorType() != Dtor_Deleting); |
| 454 | } |
| 455 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 456 | } |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 457 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 458 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 459 | switch (CGM.getTarget().getCXXABI().getKind()) { |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 460 | // For IR-generation purposes, there's no significant difference |
| 461 | // between the ARM and iOS ABIs. |
| 462 | case TargetCXXABI::GenericARM: |
| 463 | case TargetCXXABI::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 464 | case TargetCXXABI::WatchOS: |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 465 | return new ARMCXXABI(CGM); |
Charles Davis | 4e786dd | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 466 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 467 | case TargetCXXABI::iOS64: |
| 468 | return new iOS64CXXABI(CGM); |
| 469 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 470 | // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't |
| 471 | // include the other 32-bit ARM oddities: constructor/destructor return values |
| 472 | // and array cookies. |
| 473 | case TargetCXXABI::GenericAArch64: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 474 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 475 | /* UseARMGuardVarABI = */ true); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 476 | |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 477 | case TargetCXXABI::GenericMIPS: |
| 478 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true); |
| 479 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 480 | case TargetCXXABI::WebAssembly: |
| 481 | return new WebAssemblyCXXABI(CGM); |
| 482 | |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 483 | case TargetCXXABI::GenericItanium: |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 484 | if (CGM.getContext().getTargetInfo().getTriple().getArch() |
| 485 | == llvm::Triple::le32) { |
| 486 | // For PNaCl, use ARM-style method pointers so that PNaCl code |
| 487 | // does not assume anything about the alignment of function |
| 488 | // pointers. |
| 489 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 490 | /* UseARMGuardVarABI = */ false); |
| 491 | } |
John McCall | 5762592 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 492 | return new ItaniumCXXABI(CGM); |
| 493 | |
| 494 | case TargetCXXABI::Microsoft: |
| 495 | llvm_unreachable("Microsoft ABI is not Itanium-based"); |
| 496 | } |
| 497 | llvm_unreachable("bad ABI kind"); |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 500 | llvm::Type * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 501 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 502 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 503 | return CGM.PtrDiffTy; |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 504 | return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 505 | } |
| 506 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 507 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 508 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 509 | /// |
| 510 | /// In the Itanium ABI: |
| 511 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 512 | /// - the this-adjustment is (memptr.adj) |
| 513 | /// - the virtual offset is (memptr.ptr - 1) |
| 514 | /// |
| 515 | /// In the ARM ABI: |
| 516 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 517 | /// - the this-adjustment is (memptr.adj >> 1) |
| 518 | /// - the virtual offset is (memptr.ptr) |
| 519 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 520 | /// may be only single-byte aligned. |
| 521 | /// |
| 522 | /// If the member is virtual, the adjusted 'this' pointer points |
| 523 | /// to a vtable pointer from which the virtual offset is applied. |
| 524 | /// |
| 525 | /// If the member is non-virtual, memptr.ptr is the address of |
| 526 | /// the function to call. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 527 | llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 528 | CodeGenFunction &CGF, const Expr *E, Address ThisAddr, |
| 529 | llvm::Value *&ThisPtrForCall, |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 530 | llvm::Value *MemFnPtr, const MemberPointerType *MPT) { |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 531 | CGBuilderTy &Builder = CGF.Builder; |
| 532 | |
| 533 | const FunctionProtoType *FPT = |
| 534 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 535 | const CXXRecordDecl *RD = |
| 536 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 537 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 538 | llvm::FunctionType *FTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 539 | CGM.getTypes().GetFunctionType( |
| 540 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 541 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 542 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 543 | |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 544 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 545 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 546 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 547 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 548 | // Extract memptr.adj, which is in the second field. |
| 549 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 550 | |
| 551 | // Compute the true adjustment. |
| 552 | llvm::Value *Adj = RawAdj; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 553 | if (UseARMMethodPtrABI) |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 554 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 555 | |
| 556 | // Apply the adjustment and cast back to the original struct type |
| 557 | // for consistency. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 558 | llvm::Value *This = ThisAddr.getPointer(); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 559 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 560 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 561 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 562 | ThisPtrForCall = This; |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 563 | |
| 564 | // Load the function pointer. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 565 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 566 | |
| 567 | // If the LSB in the function pointer is 1, the function pointer points to |
| 568 | // a virtual function. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 569 | llvm::Value *IsVirtual; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 570 | if (UseARMMethodPtrABI) |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 571 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 572 | else |
| 573 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 574 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 575 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 576 | |
| 577 | // In the virtual path, the adjustment left 'This' pointing to the |
| 578 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 579 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 580 | CGF.EmitBlock(FnVirtual); |
| 581 | |
| 582 | // Cast the adjusted this to a pointer to vtable pointer and load. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 583 | llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 584 | CharUnits VTablePtrAlign = |
| 585 | CGF.CGM.getDynamicOffsetAlignment(ThisAddr.getAlignment(), RD, |
| 586 | CGF.getPointerAlign()); |
| 587 | llvm::Value *VTable = |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 588 | CGF.GetVTablePtr(Address(This, VTablePtrAlign), VTableTy, RD); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 589 | |
| 590 | // Apply the offset. |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 591 | llvm::Value *VTableOffset = FnAsInt; |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 592 | if (!UseARMMethodPtrABI) |
| 593 | VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 594 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 595 | |
| 596 | // Load the virtual function to call. |
| 597 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 598 | llvm::Value *VirtualFn = |
| 599 | Builder.CreateAlignedLoad(VTable, CGF.getPointerAlign(), |
| 600 | "memptr.virtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 601 | CGF.EmitBranch(FnEnd); |
| 602 | |
| 603 | // In the non-virtual path, the function pointer is actually a |
| 604 | // function pointer. |
| 605 | CGF.EmitBlock(FnNonVirtual); |
| 606 | llvm::Value *NonVirtualFn = |
John McCall | d9c6c0b | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 607 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 608 | |
| 609 | // We're done. |
| 610 | CGF.EmitBlock(FnEnd); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 611 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); |
John McCall | 475999d | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 612 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 613 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 614 | return Callee; |
| 615 | } |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 616 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 617 | /// Compute an l-value by applying the given pointer-to-member to a |
| 618 | /// base object. |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 619 | llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 620 | CodeGenFunction &CGF, const Expr *E, Address Base, llvm::Value *MemPtr, |
David Majnemer | 2b0d66d | 2014-02-20 23:22:07 +0000 | [diff] [blame] | 621 | const MemberPointerType *MPT) { |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 622 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 623 | |
| 624 | CGBuilderTy &Builder = CGF.Builder; |
| 625 | |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 626 | // Cast to char*. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 627 | Base = Builder.CreateElementBitCast(Base, CGF.Int8Ty); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 628 | |
| 629 | // Apply the offset, which we assume is non-null. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | llvm::Value *Addr = |
| 631 | Builder.CreateInBoundsGEP(Base.getPointer(), MemPtr, "memptr.offset"); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 632 | |
| 633 | // Cast the address to the appropriate pointer type, adopting the |
| 634 | // address space of the base pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 635 | llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType()) |
| 636 | ->getPointerTo(Base.getAddressSpace()); |
John McCall | c134eb5 | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 637 | return Builder.CreateBitCast(Addr, PType); |
| 638 | } |
| 639 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 640 | /// Perform a bitcast, derived-to-base, or base-to-derived member pointer |
| 641 | /// conversion. |
| 642 | /// |
| 643 | /// Bitcast conversions are always a no-op under Itanium. |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 644 | /// |
| 645 | /// Obligatory offset/adjustment diagram: |
| 646 | /// <-- offset --> <-- adjustment --> |
| 647 | /// |--------------------------|----------------------|--------------------| |
| 648 | /// ^Derived address point ^Base address point ^Member address point |
| 649 | /// |
| 650 | /// So when converting a base member pointer to a derived member pointer, |
| 651 | /// we add the offset to the adjustment because the address point has |
| 652 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 653 | /// we subtract the offset from the adjustment because the address point |
| 654 | /// has increased. |
| 655 | /// |
| 656 | /// The standard forbids (at compile time) conversion to and from |
| 657 | /// virtual bases, which is why we don't have to consider them here. |
| 658 | /// |
| 659 | /// The standard forbids (at run time) casting a derived MP to a base |
| 660 | /// MP when the derived MP does not point to a member of the base. |
| 661 | /// This is why -1 is a reasonable choice for null data member |
| 662 | /// pointers. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 663 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 664 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 665 | const CastExpr *E, |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 666 | llvm::Value *src) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 667 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 668 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 669 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 670 | |
| 671 | // Under Itanium, reinterprets don't require any additional processing. |
| 672 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 673 | |
| 674 | // Use constant emission if we can. |
| 675 | if (isa<llvm::Constant>(src)) |
| 676 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); |
| 677 | |
| 678 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 679 | if (!adj) return src; |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 680 | |
| 681 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 682 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 683 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 684 | const MemberPointerType *destTy = |
| 685 | E->getType()->castAs<MemberPointerType>(); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 686 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 687 | // For member data pointers, this is just a matter of adding the |
| 688 | // offset if the source is non-null. |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 689 | if (destTy->isMemberDataPointer()) { |
| 690 | llvm::Value *dst; |
| 691 | if (isDerivedToBase) |
| 692 | dst = Builder.CreateNSWSub(src, adj, "adj"); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 693 | else |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 694 | dst = Builder.CreateNSWAdd(src, adj, "adj"); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 695 | |
| 696 | // Null check. |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 697 | llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); |
| 698 | llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); |
| 699 | return Builder.CreateSelect(isNull, src, dst); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 700 | } |
| 701 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 702 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 703 | if (UseARMMethodPtrABI) { |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 704 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 705 | offset <<= 1; |
| 706 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 707 | } |
| 708 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 709 | llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); |
| 710 | llvm::Value *dstAdj; |
| 711 | if (isDerivedToBase) |
| 712 | dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 713 | else |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 714 | dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 715 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 716 | return Builder.CreateInsertValue(src, dstAdj, 1); |
| 717 | } |
| 718 | |
| 719 | llvm::Constant * |
| 720 | ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 721 | llvm::Constant *src) { |
| 722 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 723 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 724 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 725 | |
| 726 | // Under Itanium, reinterprets don't require any additional processing. |
| 727 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 728 | |
| 729 | // If the adjustment is trivial, we don't need to do anything. |
| 730 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 731 | if (!adj) return src; |
| 732 | |
| 733 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 734 | |
| 735 | const MemberPointerType *destTy = |
| 736 | E->getType()->castAs<MemberPointerType>(); |
| 737 | |
| 738 | // For member data pointers, this is just a matter of adding the |
| 739 | // offset if the source is non-null. |
| 740 | if (destTy->isMemberDataPointer()) { |
| 741 | // null maps to null. |
| 742 | if (src->isAllOnesValue()) return src; |
| 743 | |
| 744 | if (isDerivedToBase) |
| 745 | return llvm::ConstantExpr::getNSWSub(src, adj); |
| 746 | else |
| 747 | return llvm::ConstantExpr::getNSWAdd(src, adj); |
| 748 | } |
| 749 | |
| 750 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 751 | if (UseARMMethodPtrABI) { |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 752 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 753 | offset <<= 1; |
| 754 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
| 755 | } |
| 756 | |
| 757 | llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); |
| 758 | llvm::Constant *dstAdj; |
| 759 | if (isDerivedToBase) |
| 760 | dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); |
| 761 | else |
| 762 | dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); |
| 763 | |
| 764 | return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); |
John McCall | a8bbb82 | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 765 | } |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 766 | |
| 767 | llvm::Constant * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 768 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 769 | // Itanium C++ ABI 2.3: |
| 770 | // A NULL pointer is represented as -1. |
| 771 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 772 | return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 773 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 774 | llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 775 | llvm::Constant *Values[2] = { Zero, Zero }; |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 776 | return llvm::ConstantStruct::getAnon(Values); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 777 | } |
| 778 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 779 | llvm::Constant * |
| 780 | ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 781 | CharUnits offset) { |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 782 | // Itanium C++ ABI 2.3: |
| 783 | // A pointer to data member is an offset from the base address of |
| 784 | // the class object containing it, represented as a ptrdiff_t |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 785 | return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 786 | } |
| 787 | |
David Majnemer | e2be95b | 2015-06-23 07:31:01 +0000 | [diff] [blame] | 788 | llvm::Constant * |
| 789 | ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 790 | return BuildMemberPointer(MD, CharUnits::Zero()); |
| 791 | } |
| 792 | |
| 793 | llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, |
| 794 | CharUnits ThisAdjustment) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 795 | assert(MD->isInstance() && "Member function must not be static!"); |
| 796 | MD = MD->getCanonicalDecl(); |
| 797 | |
| 798 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 799 | |
| 800 | // Get the function pointer (or index if this is a virtual function). |
| 801 | llvm::Constant *MemPtr[2]; |
| 802 | if (MD->isVirtual()) { |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 803 | uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 804 | |
Ken Dyck | df01628 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 805 | const ASTContext &Context = getContext(); |
| 806 | CharUnits PointerWidth = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 807 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | df01628 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 808 | uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 809 | |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 810 | if (UseARMMethodPtrABI) { |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 811 | // ARM C++ ABI 3.2.1: |
| 812 | // This ABI specifies that adj contains twice the this |
| 813 | // adjustment, plus 1 if the member function is virtual. The |
| 814 | // least significant bit of adj then makes exactly the same |
| 815 | // discrimination as the least significant bit of ptr does for |
| 816 | // Itanium. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 817 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); |
| 818 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 819 | 2 * ThisAdjustment.getQuantity() + 1); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 820 | } else { |
| 821 | // Itanium C++ ABI 2.3: |
| 822 | // For a virtual function, [the pointer field] is 1 plus the |
| 823 | // virtual table offset (in bytes) of the function, |
| 824 | // represented as a ptrdiff_t. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 825 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); |
| 826 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 827 | ThisAdjustment.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 828 | } |
| 829 | } else { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 830 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 831 | llvm::Type *Ty; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 832 | // Check whether the function has a computable LLVM signature. |
Chris Lattner | 8806e32 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 833 | if (Types.isFuncTypeConvertible(FPT)) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 834 | // The function has a computable LLVM signature; use the correct type. |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 835 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 836 | } else { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 837 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 838 | // function type is incomplete. |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 839 | Ty = CGM.PtrDiffTy; |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 840 | } |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 841 | llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 842 | |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 843 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 844 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
| 845 | (UseARMMethodPtrABI ? 2 : 1) * |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 846 | ThisAdjustment.getQuantity()); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 847 | } |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 848 | |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 849 | return llvm::ConstantStruct::getAnon(MemPtr); |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 852 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, |
| 853 | QualType MPType) { |
| 854 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 855 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 856 | if (!MPD) |
| 857 | return EmitNullMemberPointer(MPT); |
| 858 | |
Reid Kleckner | 452abac | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 859 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 860 | |
| 861 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 862 | return BuildMemberPointer(MD, ThisAdjustment); |
| 863 | |
| 864 | CharUnits FieldOffset = |
| 865 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 866 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
| 867 | } |
| 868 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 869 | /// The comparison algorithm is pretty easy: the member pointers are |
| 870 | /// the same if they're either bitwise identical *or* both null. |
| 871 | /// |
| 872 | /// ARM is different here only because null-ness is more complicated. |
| 873 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 874 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 875 | llvm::Value *L, |
| 876 | llvm::Value *R, |
| 877 | const MemberPointerType *MPT, |
| 878 | bool Inequality) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 879 | CGBuilderTy &Builder = CGF.Builder; |
| 880 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 881 | llvm::ICmpInst::Predicate Eq; |
| 882 | llvm::Instruction::BinaryOps And, Or; |
| 883 | if (Inequality) { |
| 884 | Eq = llvm::ICmpInst::ICMP_NE; |
| 885 | And = llvm::Instruction::Or; |
| 886 | Or = llvm::Instruction::And; |
| 887 | } else { |
| 888 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 889 | And = llvm::Instruction::And; |
| 890 | Or = llvm::Instruction::Or; |
| 891 | } |
| 892 | |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 893 | // Member data pointers are easy because there's a unique null |
| 894 | // value, so it just comes down to bitwise equality. |
| 895 | if (MPT->isMemberDataPointer()) |
| 896 | return Builder.CreateICmp(Eq, L, R); |
| 897 | |
| 898 | // For member function pointers, the tautologies are more complex. |
| 899 | // The Itanium tautology is: |
John McCall | 61a1488 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 900 | // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 901 | // The ARM tautology is: |
John McCall | 61a1488 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 902 | // (L == R) <==> (L.ptr == R.ptr && |
| 903 | // (L.adj == R.adj || |
| 904 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 905 | // The inequality tautologies have exactly the same structure, except |
| 906 | // applying De Morgan's laws. |
| 907 | |
| 908 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 909 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 910 | |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 911 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 912 | // true for equality to hold. |
| 913 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 914 | |
| 915 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 916 | // tests whether the pointers are both null. ARM imposes an extra |
| 917 | // condition. |
| 918 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 919 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 920 | |
| 921 | // This condition tests whether L.adj == R.adj. If this isn't |
| 922 | // true, the pointers are unequal unless they're both null. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 923 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 924 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 925 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 926 | |
| 927 | // Null member function pointers on ARM clear the low bit of Adj, |
| 928 | // so the zero condition has to check that neither low bit is set. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 929 | if (UseARMMethodPtrABI) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 930 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 931 | |
| 932 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 933 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 934 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 935 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 936 | "cmp.or.adj"); |
| 937 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 938 | } |
| 939 | |
| 940 | // Tie together all our conditions. |
| 941 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 942 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 943 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 944 | return Result; |
| 945 | } |
| 946 | |
| 947 | llvm::Value * |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 948 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 949 | llvm::Value *MemPtr, |
| 950 | const MemberPointerType *MPT) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 951 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 952 | |
| 953 | /// For member data pointers, this is just a check against -1. |
| 954 | if (MPT->isMemberDataPointer()) { |
Reid Kleckner | 9cffbc1 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 955 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | 7a9aac2 | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 956 | llvm::Value *NegativeOne = |
| 957 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 958 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 959 | } |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 960 | |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 961 | // In Itanium, a member function pointer is not null if 'ptr' is not null. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 962 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 963 | |
| 964 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 965 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 966 | |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 967 | // On ARM, a member function pointer is also non-null if the low bit of 'adj' |
| 968 | // (the virtual bit) is set. |
Mark Seaborn | edf0d38 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 969 | if (UseARMMethodPtrABI) { |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 970 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 971 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 972 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
Daniel Dunbar | 914bc41 | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 973 | llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, |
| 974 | "memptr.isvirtual"); |
| 975 | Result = Builder.CreateOr(Result, IsVirtual); |
John McCall | 131d97d | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | return Result; |
| 979 | } |
John McCall | 1c456c8 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 980 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 981 | bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const { |
| 982 | const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); |
| 983 | if (!RD) |
| 984 | return false; |
| 985 | |
Reid Kleckner | d355ca7 | 2014-05-15 01:26:32 +0000 | [diff] [blame] | 986 | // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor. |
| 987 | // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared |
| 988 | // special members. |
| 989 | if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 990 | auto Align = CGM.getContext().getTypeAlignInChars(FI.getReturnType()); |
| 991 | FI.getReturnInfo() = ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 992 | return true; |
| 993 | } |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 994 | return false; |
| 995 | } |
| 996 | |
John McCall | 614dbdc | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 997 | /// The Itanium ABI requires non-zero initialization only for data |
| 998 | /// member pointers, for which '0' is a valid offset. |
| 999 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
David Majnemer | 5fd33e0 | 2015-04-24 01:25:08 +0000 | [diff] [blame] | 1000 | return MPT->isMemberFunctionPointer(); |
John McCall | 84fa510 | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 1001 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1002 | |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1003 | /// The Itanium ABI always places an offset to the complete object |
| 1004 | /// at entry -2 in the vtable. |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1005 | void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, |
| 1006 | const CXXDeleteExpr *DE, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1007 | Address Ptr, |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1008 | QualType ElementType, |
| 1009 | const CXXDestructorDecl *Dtor) { |
| 1010 | bool UseGlobalDelete = DE->isGlobalDelete(); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1011 | if (UseGlobalDelete) { |
| 1012 | // Derive the complete-object pointer, which is what we need |
| 1013 | // to pass to the deallocation function. |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1014 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1015 | // Grab the vtable pointer as an intptr_t*. |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1016 | auto *ClassDecl = |
| 1017 | cast<CXXRecordDecl>(ElementType->getAs<RecordType>()->getDecl()); |
| 1018 | llvm::Value *VTable = |
| 1019 | CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo(), ClassDecl); |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1020 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1021 | // Track back to entry -2 and pull out the offset there. |
| 1022 | llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64( |
| 1023 | VTable, -2, "complete-offset.ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1024 | llvm::Value *Offset = |
| 1025 | CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign()); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Apply the offset. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1028 | llvm::Value *CompletePtr = |
| 1029 | CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1030 | CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset); |
| 1031 | |
| 1032 | // If we're supposed to call the global delete, make sure we do so |
| 1033 | // even if the destructor throws. |
David Majnemer | 0868137 | 2014-11-01 07:37:17 +0000 | [diff] [blame] | 1034 | CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr, |
| 1035 | ElementType); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | // FIXME: Provide a source location here even though there's no |
| 1039 | // CXXMemberCallExpr for dtor call. |
| 1040 | CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; |
| 1041 | EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); |
| 1042 | |
| 1043 | if (UseGlobalDelete) |
| 1044 | CGF.PopCleanupBlock(); |
John McCall | 82fb892 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
David Majnemer | 442d0a2 | 2014-11-25 07:20:20 +0000 | [diff] [blame] | 1047 | void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { |
| 1048 | // void __cxa_rethrow(); |
| 1049 | |
| 1050 | llvm::FunctionType *FTy = |
| 1051 | llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); |
| 1052 | |
| 1053 | llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); |
| 1054 | |
| 1055 | if (isNoReturn) |
| 1056 | CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None); |
| 1057 | else |
| 1058 | CGF.EmitRuntimeCallOrInvoke(Fn); |
| 1059 | } |
| 1060 | |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 1061 | static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) { |
| 1062 | // void *__cxa_allocate_exception(size_t thrown_size); |
| 1063 | |
| 1064 | llvm::FunctionType *FTy = |
| 1065 | llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false); |
| 1066 | |
| 1067 | return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception"); |
| 1068 | } |
| 1069 | |
| 1070 | static llvm::Constant *getThrowFn(CodeGenModule &CGM) { |
| 1071 | // void __cxa_throw(void *thrown_exception, std::type_info *tinfo, |
| 1072 | // void (*dest) (void *)); |
| 1073 | |
| 1074 | llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy }; |
| 1075 | llvm::FunctionType *FTy = |
| 1076 | llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false); |
| 1077 | |
| 1078 | return CGM.CreateRuntimeFunction(FTy, "__cxa_throw"); |
| 1079 | } |
| 1080 | |
| 1081 | void ItaniumCXXABI::emitThrow(CodeGenFunction &CGF, const CXXThrowExpr *E) { |
| 1082 | QualType ThrowType = E->getSubExpr()->getType(); |
| 1083 | // Now allocate the exception object. |
| 1084 | llvm::Type *SizeTy = CGF.ConvertType(getContext().getSizeType()); |
| 1085 | uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity(); |
| 1086 | |
| 1087 | llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM); |
| 1088 | llvm::CallInst *ExceptionPtr = CGF.EmitNounwindRuntimeCall( |
| 1089 | AllocExceptionFn, llvm::ConstantInt::get(SizeTy, TypeSize), "exception"); |
| 1090 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1091 | CharUnits ExnAlign = getAlignmentOfExnObject(); |
| 1092 | CGF.EmitAnyExprToExn(E->getSubExpr(), Address(ExceptionPtr, ExnAlign)); |
David Majnemer | 7c23707 | 2015-03-05 00:46:22 +0000 | [diff] [blame] | 1093 | |
| 1094 | // Now throw the exception. |
| 1095 | llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, |
| 1096 | /*ForEH=*/true); |
| 1097 | |
| 1098 | // The address of the destructor. If the exception type has a |
| 1099 | // trivial destructor (or isn't a record), we just pass null. |
| 1100 | llvm::Constant *Dtor = nullptr; |
| 1101 | if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) { |
| 1102 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 1103 | if (!Record->hasTrivialDestructor()) { |
| 1104 | CXXDestructorDecl *DtorD = Record->getDestructor(); |
| 1105 | Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete); |
| 1106 | Dtor = llvm::ConstantExpr::getBitCast(Dtor, CGM.Int8PtrTy); |
| 1107 | } |
| 1108 | } |
| 1109 | if (!Dtor) Dtor = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 1110 | |
| 1111 | llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor }; |
| 1112 | CGF.EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args); |
| 1113 | } |
| 1114 | |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1115 | static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) { |
| 1116 | // void *__dynamic_cast(const void *sub, |
| 1117 | // const abi::__class_type_info *src, |
| 1118 | // const abi::__class_type_info *dst, |
| 1119 | // std::ptrdiff_t src2dst_offset); |
| 1120 | |
| 1121 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
| 1122 | llvm::Type *PtrDiffTy = |
| 1123 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1124 | |
| 1125 | llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
| 1126 | |
| 1127 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 1128 | |
| 1129 | // Mark the function as nounwind readonly. |
| 1130 | llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, |
| 1131 | llvm::Attribute::ReadOnly }; |
| 1132 | llvm::AttributeSet Attrs = llvm::AttributeSet::get( |
| 1133 | CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); |
| 1134 | |
| 1135 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); |
| 1136 | } |
| 1137 | |
| 1138 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 1139 | // void __cxa_bad_cast(); |
| 1140 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
| 1141 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 1142 | } |
| 1143 | |
| 1144 | /// \brief Compute the src2dst_offset hint as described in the |
| 1145 | /// Itanium C++ ABI [2.9.7] |
| 1146 | static CharUnits computeOffsetHint(ASTContext &Context, |
| 1147 | const CXXRecordDecl *Src, |
| 1148 | const CXXRecordDecl *Dst) { |
| 1149 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 1150 | /*DetectVirtual=*/false); |
| 1151 | |
| 1152 | // If Dst is not derived from Src we can skip the whole computation below and |
| 1153 | // return that Src is not a public base of Dst. Record all inheritance paths. |
| 1154 | if (!Dst->isDerivedFrom(Src, Paths)) |
| 1155 | return CharUnits::fromQuantity(-2ULL); |
| 1156 | |
| 1157 | unsigned NumPublicPaths = 0; |
| 1158 | CharUnits Offset; |
| 1159 | |
| 1160 | // Now walk all possible inheritance paths. |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 1161 | for (const CXXBasePath &Path : Paths) { |
| 1162 | if (Path.Access != AS_public) // Ignore non-public inheritance. |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1163 | continue; |
| 1164 | |
| 1165 | ++NumPublicPaths; |
| 1166 | |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 1167 | for (const CXXBasePathElement &PathElement : Path) { |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1168 | // If the path contains a virtual base class we can't give any hint. |
| 1169 | // -1: no hint. |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 1170 | if (PathElement.Base->isVirtual()) |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1171 | return CharUnits::fromQuantity(-1ULL); |
| 1172 | |
| 1173 | if (NumPublicPaths > 1) // Won't use offsets, skip computation. |
| 1174 | continue; |
| 1175 | |
| 1176 | // Accumulate the base class offsets. |
Piotr Padlewski | 44b4ce8 | 2015-07-28 16:10:58 +0000 | [diff] [blame] | 1177 | const ASTRecordLayout &L = Context.getASTRecordLayout(PathElement.Class); |
| 1178 | Offset += L.getBaseClassOffset( |
| 1179 | PathElement.Base->getType()->getAsCXXRecordDecl()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // -2: Src is not a public base of Dst. |
| 1184 | if (NumPublicPaths == 0) |
| 1185 | return CharUnits::fromQuantity(-2ULL); |
| 1186 | |
| 1187 | // -3: Src is a multiple public base type but never a virtual base type. |
| 1188 | if (NumPublicPaths > 1) |
| 1189 | return CharUnits::fromQuantity(-3ULL); |
| 1190 | |
| 1191 | // Otherwise, the Src type is a unique public nonvirtual base type of Dst. |
| 1192 | // Return the offset of Src from the origin of Dst. |
| 1193 | return Offset; |
| 1194 | } |
| 1195 | |
| 1196 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 1197 | // void __cxa_bad_typeid(); |
| 1198 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
| 1199 | |
| 1200 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 1201 | } |
| 1202 | |
| 1203 | bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref, |
| 1204 | QualType SrcRecordTy) { |
| 1205 | return IsDeref; |
| 1206 | } |
| 1207 | |
| 1208 | void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { |
| 1209 | llvm::Value *Fn = getBadTypeidFn(CGF); |
| 1210 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
| 1211 | CGF.Builder.CreateUnreachable(); |
| 1212 | } |
| 1213 | |
| 1214 | llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF, |
| 1215 | QualType SrcRecordTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1216 | Address ThisPtr, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1217 | llvm::Type *StdTypeInfoPtrTy) { |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1218 | auto *ClassDecl = |
| 1219 | cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1220 | llvm::Value *Value = |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1221 | CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo(), ClassDecl); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Load the type info. |
| 1224 | Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1225 | return CGF.Builder.CreateAlignedLoad(Value, CGF.getPointerAlign()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 1229 | QualType SrcRecordTy) { |
| 1230 | return SrcIsPtr; |
| 1231 | } |
| 1232 | |
| 1233 | llvm::Value *ItaniumCXXABI::EmitDynamicCastCall( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1234 | CodeGenFunction &CGF, Address ThisAddr, QualType SrcRecordTy, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1235 | QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { |
| 1236 | llvm::Type *PtrDiffLTy = |
| 1237 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1238 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1239 | |
| 1240 | llvm::Value *SrcRTTI = |
| 1241 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1242 | llvm::Value *DestRTTI = |
| 1243 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1244 | |
| 1245 | // Compute the offset hint. |
| 1246 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 1247 | const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); |
| 1248 | llvm::Value *OffsetHint = llvm::ConstantInt::get( |
| 1249 | PtrDiffLTy, |
| 1250 | computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); |
| 1251 | |
| 1252 | // Emit the call to __dynamic_cast. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1253 | llvm::Value *Value = ThisAddr.getPointer(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1254 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1255 | |
| 1256 | llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; |
| 1257 | Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); |
| 1258 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1259 | |
| 1260 | /// C++ [expr.dynamic.cast]p9: |
| 1261 | /// A failed cast to reference type throws std::bad_cast |
| 1262 | if (DestTy->isReferenceType()) { |
| 1263 | llvm::BasicBlock *BadCastBlock = |
| 1264 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1265 | |
| 1266 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1267 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1268 | |
| 1269 | CGF.EmitBlock(BadCastBlock); |
| 1270 | EmitBadCastCall(CGF); |
| 1271 | } |
| 1272 | |
| 1273 | return Value; |
| 1274 | } |
| 1275 | |
| 1276 | llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1277 | Address ThisAddr, |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1278 | QualType SrcRecordTy, |
| 1279 | QualType DestTy) { |
| 1280 | llvm::Type *PtrDiffLTy = |
| 1281 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1282 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1283 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1284 | auto *ClassDecl = |
| 1285 | cast<CXXRecordDecl>(SrcRecordTy->getAs<RecordType>()->getDecl()); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1286 | // Get the vtable pointer. |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1287 | llvm::Value *VTable = CGF.GetVTablePtr(ThisAddr, PtrDiffLTy->getPointerTo(), |
| 1288 | ClassDecl); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Get the offset-to-top from the vtable. |
| 1291 | llvm::Value *OffsetToTop = |
| 1292 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1293 | OffsetToTop = |
| 1294 | CGF.Builder.CreateAlignedLoad(OffsetToTop, CGF.getPointerAlign(), |
| 1295 | "offset.to.top"); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1296 | |
| 1297 | // Finally, add the offset to the pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1298 | llvm::Value *Value = ThisAddr.getPointer(); |
David Majnemer | 1162d25 | 2014-06-22 19:05:33 +0000 | [diff] [blame] | 1299 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1300 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1301 | |
| 1302 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1303 | } |
| 1304 | |
| 1305 | bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { |
| 1306 | llvm::Value *Fn = getBadCastFn(CGF); |
| 1307 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
| 1308 | CGF.Builder.CreateUnreachable(); |
| 1309 | return true; |
| 1310 | } |
| 1311 | |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1312 | llvm::Value * |
| 1313 | ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1314 | Address This, |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1315 | const CXXRecordDecl *ClassDecl, |
| 1316 | const CXXRecordDecl *BaseClassDecl) { |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1317 | llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy, ClassDecl); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1318 | CharUnits VBaseOffsetOffset = |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1319 | CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, |
| 1320 | BaseClassDecl); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1321 | |
| 1322 | llvm::Value *VBaseOffsetPtr = |
| 1323 | CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), |
| 1324 | "vbase.offset.ptr"); |
| 1325 | VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, |
| 1326 | CGM.PtrDiffTy->getPointerTo()); |
| 1327 | |
| 1328 | llvm::Value *VBaseOffset = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1329 | CGF.Builder.CreateAlignedLoad(VBaseOffsetPtr, CGF.getPointerAlign(), |
| 1330 | "vbase.offset"); |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1331 | |
| 1332 | return VBaseOffset; |
| 1333 | } |
| 1334 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1335 | void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 1336 | // Just make sure we're in sync with TargetCXXABI. |
| 1337 | assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); |
| 1338 | |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 1339 | // The constructor used for constructing this as a base class; |
| 1340 | // ignores virtual bases. |
| 1341 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); |
| 1342 | |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1343 | // The constructor used for constructing this as a complete class; |
Nico Weber | 4c2ffb2 | 2015-01-07 05:25:05 +0000 | [diff] [blame] | 1344 | // constructs the virtual bases, then calls the base constructor. |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1345 | if (!D->getParent()->isAbstract()) { |
| 1346 | // We don't need to emit the complete ctor if the class is abstract. |
| 1347 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 1348 | } |
Timur Iskhodzhanov | 40f2fa9 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1351 | void |
| 1352 | ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 1353 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1354 | ASTContext &Context = getContext(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1355 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1356 | // All parameters are already in place except VTT, which goes after 'this'. |
| 1357 | // These are Clang types, so we don't need to worry about sret yet. |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Check if we need to add a VTT parameter (which has type void **). |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1360 | if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) |
| 1361 | ArgTys.insert(ArgTys.begin() + 1, |
| 1362 | Context.getPointerType(Context.VoidPtrTy)); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1365 | void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 1366 | // The destructor used for destructing this as a base class; ignores |
| 1367 | // virtual bases. |
| 1368 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1369 | |
| 1370 | // The destructor used for destructing this as a most-derived class; |
| 1371 | // call the base destructor and then destructs any virtual bases. |
| 1372 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
| 1373 | |
Rafael Espindola | c3cde36 | 2013-12-09 14:51:17 +0000 | [diff] [blame] | 1374 | // The destructor in a virtual table is always a 'deleting' |
| 1375 | // destructor, which calls the complete destructor and then uses the |
| 1376 | // appropriate operator delete. |
| 1377 | if (D->isVirtual()) |
| 1378 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1381 | void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, |
| 1382 | QualType &ResTy, |
| 1383 | FunctionArgList &Params) { |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1384 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1385 | assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Check if we need a VTT parameter as well. |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 1388 | if (NeedsVTTParameter(CGF.CurGD)) { |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1389 | ASTContext &Context = getContext(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1390 | |
| 1391 | // FIXME: avoid the fake decl |
| 1392 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 1393 | ImplicitParamDecl *VTTDecl |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1394 | = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(), |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1395 | &Context.Idents.get("vtt"), T); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1396 | Params.insert(Params.begin() + 1, VTTDecl); |
Reid Kleckner | 2af6d73 | 2013-12-13 00:09:59 +0000 | [diff] [blame] | 1397 | getStructorImplicitParamDecl(CGF) = VTTDecl; |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1401 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 1402 | /// Initialize the 'this' slot. |
| 1403 | EmitThisParam(CGF); |
| 1404 | |
| 1405 | /// Initialize the 'vtt' slot if needed. |
Reid Kleckner | 2af6d73 | 2013-12-13 00:09:59 +0000 | [diff] [blame] | 1406 | if (getStructorImplicitParamDecl(CGF)) { |
| 1407 | getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( |
| 1408 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1409 | } |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1410 | |
Stephen Lin | 9dc6eef | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1411 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 1412 | /// the return slot to 'this' at the start of the function. |
| 1413 | /// |
| 1414 | /// Unlike the setting of return types, this is done within the ABI |
| 1415 | /// implementation instead of by clients of CGCXXABI because: |
| 1416 | /// 1) getThisValue is currently protected |
| 1417 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 1418 | /// HasThisReturn only specifies a contract, not the implementation |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1419 | if (HasThisReturn(CGF.CurGD)) |
Eli Friedman | 9fbeba0 | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 1420 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1423 | unsigned ItaniumCXXABI::addImplicitConstructorArgs( |
| 1424 | CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, |
| 1425 | bool ForVirtualBase, bool Delegating, CallArgList &Args) { |
| 1426 | if (!NeedsVTTParameter(GlobalDecl(D, Type))) |
| 1427 | return 0; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1428 | |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 1429 | // Insert the implicit 'vtt' argument as the second argument. |
| 1430 | llvm::Value *VTT = |
| 1431 | CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); |
| 1432 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
| 1433 | Args.insert(Args.begin() + 1, |
| 1434 | CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); |
| 1435 | return 1; // Added one arg. |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 1439 | const CXXDestructorDecl *DD, |
| 1440 | CXXDtorType Type, bool ForVirtualBase, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1441 | bool Delegating, Address This) { |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1442 | GlobalDecl GD(DD, Type); |
| 1443 | llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); |
| 1444 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
| 1445 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1446 | llvm::Value *Callee = nullptr; |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1447 | if (getContext().getLangOpts().AppleKext) |
| 1448 | Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); |
| 1449 | |
| 1450 | if (!Callee) |
Rafael Espindola | 1ac0ec8 | 2014-09-11 15:42:06 +0000 | [diff] [blame] | 1451 | Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 1452 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1453 | CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), |
| 1454 | This.getPointer(), VTT, VTTTy, nullptr); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1457 | void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 1458 | const CXXRecordDecl *RD) { |
| 1459 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); |
| 1460 | if (VTable->hasInitializer()) |
| 1461 | return; |
| 1462 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1463 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1464 | const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); |
| 1465 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1466 | llvm::Constant *RTTI = |
| 1467 | CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD)); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Create and set the initializer. |
| 1470 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 1471 | RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), |
David Majnemer | d905da4 | 2014-07-01 20:30:31 +0000 | [diff] [blame] | 1472 | VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1473 | VTable->setInitializer(Init); |
| 1474 | |
| 1475 | // Set the correct linkage. |
| 1476 | VTable->setLinkage(Linkage); |
| 1477 | |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 1478 | if (CGM.supportsCOMDAT() && VTable->isWeakForLinker()) |
| 1479 | VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName())); |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 1480 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1481 | // Set the right visibility. |
John McCall | 8f80a61 | 2014-02-08 00:41:16 +0000 | [diff] [blame] | 1482 | CGM.setGlobalVisibility(VTable, RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1483 | |
Benjamin Kramer | 5d34a2b | 2014-09-10 12:50:59 +0000 | [diff] [blame] | 1484 | // Use pointer alignment for the vtable. Otherwise we would align them based |
| 1485 | // on the size of the initializer which doesn't make sense as only single |
| 1486 | // values are read. |
| 1487 | unsigned PAlign = CGM.getTarget().getPointerAlign(0); |
| 1488 | VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity()); |
| 1489 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1490 | // If this is the magic class __cxxabiv1::__fundamental_type_info, |
| 1491 | // we will emit the typeinfo for the fundamental types. This is the |
| 1492 | // same behaviour as GCC. |
| 1493 | const DeclContext *DC = RD->getDeclContext(); |
| 1494 | if (RD->getIdentifier() && |
| 1495 | RD->getIdentifier()->isStr("__fundamental_type_info") && |
| 1496 | isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1497 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && |
| 1498 | DC->getParent()->isTranslationUnit()) |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 1499 | EmitFundamentalRTTIDescriptors(); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1500 | |
| 1501 | CGM.EmitVTableBitSetEntries(VTable, VTLayout); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1504 | bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField( |
| 1505 | CodeGenFunction &CGF, CodeGenFunction::VPtr Vptr) { |
| 1506 | if (Vptr.NearestVBase == nullptr) |
| 1507 | return false; |
| 1508 | return NeedsVTTParameter(CGF.CurGD); |
Piotr Padlewski | 255652e | 2015-09-09 22:20:28 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1511 | llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( |
| 1512 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 1513 | const CXXRecordDecl *NearestVBase) { |
| 1514 | |
| 1515 | if ((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && |
| 1516 | NeedsVTTParameter(CGF.CurGD)) { |
| 1517 | return getVTableAddressPointInStructorWithVTT(CGF, VTableClass, Base, |
| 1518 | NearestVBase); |
| 1519 | } |
| 1520 | return getVTableAddressPoint(Base, VTableClass); |
| 1521 | } |
| 1522 | |
| 1523 | llvm::Constant * |
| 1524 | ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base, |
| 1525 | const CXXRecordDecl *VTableClass) { |
| 1526 | llvm::GlobalValue *VTable = getAddrOfVTable(VTableClass, CharUnits()); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1527 | |
| 1528 | // Find the appropriate vtable within the vtable group. |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1529 | uint64_t AddressPoint = CGM.getItaniumVTableContext() |
| 1530 | .getVTableLayout(VTableClass) |
| 1531 | .getAddressPoint(Base); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1532 | llvm::Value *Indices[] = { |
| 1533 | llvm::ConstantInt::get(CGM.Int64Ty, 0), |
| 1534 | llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) |
| 1535 | }; |
| 1536 | |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1537 | return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable->getValueType(), |
| 1538 | VTable, Indices); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1541 | llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructorWithVTT( |
| 1542 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 1543 | const CXXRecordDecl *NearestVBase) { |
| 1544 | assert((Base.getBase()->getNumVBases() || NearestVBase != nullptr) && |
| 1545 | NeedsVTTParameter(CGF.CurGD) && "This class doesn't have VTT"); |
| 1546 | |
| 1547 | // Get the secondary vpointer index. |
| 1548 | uint64_t VirtualPointerIndex = |
| 1549 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1550 | |
| 1551 | /// Load the VTT. |
| 1552 | llvm::Value *VTT = CGF.LoadCXXVTT(); |
| 1553 | if (VirtualPointerIndex) |
| 1554 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1555 | |
| 1556 | // And load the address point from the VTT. |
| 1557 | return CGF.Builder.CreateAlignedLoad(VTT, CGF.getPointerAlign()); |
| 1558 | } |
| 1559 | |
| 1560 | llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( |
| 1561 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
| 1562 | return getVTableAddressPoint(Base, VTableClass); |
| 1563 | } |
| 1564 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1565 | llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 1566 | CharUnits VPtrOffset) { |
| 1567 | assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); |
| 1568 | |
| 1569 | llvm::GlobalVariable *&VTable = VTables[RD]; |
| 1570 | if (VTable) |
| 1571 | return VTable; |
| 1572 | |
| 1573 | // Queue up this v-table for possible deferred emission. |
| 1574 | CGM.addDeferredVTable(RD); |
| 1575 | |
Yaron Keren | e46f7ed | 2015-07-29 14:21:47 +0000 | [diff] [blame] | 1576 | SmallString<256> Name; |
| 1577 | llvm::raw_svector_ostream Out(Name); |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1578 | getMangleContext().mangleCXXVTable(RD, Out); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1579 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1580 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1581 | llvm::ArrayType *ArrayType = llvm::ArrayType::get( |
| 1582 | CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents()); |
| 1583 | |
| 1584 | VTable = CGM.CreateOrReplaceCXXRuntimeVariable( |
| 1585 | Name, ArrayType, llvm::GlobalValue::ExternalLinkage); |
| 1586 | VTable->setUnnamedAddr(true); |
Hans Wennborg | da24e9c | 2014-06-02 23:13:03 +0000 | [diff] [blame] | 1587 | |
| 1588 | if (RD->hasAttr<DLLImportAttr>()) |
| 1589 | VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1590 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1591 | VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1592 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1593 | return VTable; |
| 1594 | } |
| 1595 | |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1596 | llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 1597 | GlobalDecl GD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1598 | Address This, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 1599 | llvm::Type *Ty, |
| 1600 | SourceLocation Loc) { |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1601 | GD = GD.getCanonicalDecl(); |
| 1602 | Ty = Ty->getPointerTo()->getPointerTo(); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1603 | auto *MethodDecl = cast<CXXMethodDecl>(GD.getDecl()); |
| 1604 | llvm::Value *VTable = CGF.GetVTablePtr(This, Ty, MethodDecl->getParent()); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1605 | |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 1606 | if (CGF.SanOpts.has(SanitizerKind::CFIVCall)) |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 1607 | CGF.EmitVTablePtrCheckForCall(MethodDecl, VTable, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 1608 | CodeGenFunction::CFITCK_VCall, Loc); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 1609 | |
Timur Iskhodzhanov | 5877663 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1610 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1611 | llvm::Value *VFuncPtr = |
| 1612 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
Renato Golin | 4110618 | 2015-10-01 12:58:41 +0000 | [diff] [blame] | 1613 | return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.getPointerAlign()); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1616 | llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( |
| 1617 | CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1618 | Address This, const CXXMemberCallExpr *CE) { |
Alexey Samsonov | a5bf76b | 2014-08-25 20:17:35 +0000 | [diff] [blame] | 1619 | assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1620 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 1621 | |
Rafael Espindola | 8d2a19b | 2014-09-08 16:01:27 +0000 | [diff] [blame] | 1622 | const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 1623 | Dtor, getFromDtorType(DtorType)); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1624 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 88fd439 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1625 | llvm::Value *Callee = |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 1626 | getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty, |
| 1627 | CE ? CE->getLocStart() : SourceLocation()); |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1628 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1629 | CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), |
| 1630 | This.getPointer(), /*ImplicitParam=*/nullptr, |
| 1631 | QualType(), CE); |
David Majnemer | 0c0b6d9 | 2014-10-31 20:09:12 +0000 | [diff] [blame] | 1632 | return nullptr; |
Timur Iskhodzhanov | d619711 | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1635 | void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1636 | CodeGenVTables &VTables = CGM.getVTables(); |
| 1637 | llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1638 | VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1641 | bool ItaniumCXXABI::canSpeculativelyEmitVTable(const CXXRecordDecl *RD) const { |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 1642 | // We don't emit available_externally vtables if we are in -fapple-kext mode |
| 1643 | // because kext mode does not permit devirtualization. |
| 1644 | if (CGM.getLangOpts().AppleKext) |
| 1645 | return false; |
| 1646 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1647 | // If we don't have any inline virtual functions, and if vtable is not hidden, |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 1648 | // then we are safe to emit available_externally copy of vtable. |
| 1649 | // FIXME we can still emit a copy of the vtable if we |
| 1650 | // can emit definition of the inline functions. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 1651 | return !hasAnyUsedVirtualInlineFunction(RD) && !isVTableHidden(RD); |
Piotr Padlewski | a68a787 | 2015-07-24 04:04:49 +0000 | [diff] [blame] | 1652 | } |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1653 | static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1654 | Address InitialPtr, |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1655 | int64_t NonVirtualAdjustment, |
| 1656 | int64_t VirtualAdjustment, |
| 1657 | bool IsReturnAdjustment) { |
| 1658 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1659 | return InitialPtr.getPointer(); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1660 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1661 | Address V = CGF.Builder.CreateElementBitCast(InitialPtr, CGF.Int8Ty); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1662 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1663 | // In a base-to-derived cast, the non-virtual adjustment is applied first. |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1664 | if (NonVirtualAdjustment && !IsReturnAdjustment) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1665 | V = CGF.Builder.CreateConstInBoundsByteGEP(V, |
| 1666 | CharUnits::fromQuantity(NonVirtualAdjustment)); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1669 | // Perform the virtual adjustment if we have one. |
| 1670 | llvm::Value *ResultPtr; |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1671 | if (VirtualAdjustment) { |
| 1672 | llvm::Type *PtrDiffTy = |
| 1673 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1674 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1675 | Address VTablePtrPtr = CGF.Builder.CreateElementBitCast(V, CGF.Int8PtrTy); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1676 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 1677 | |
| 1678 | llvm::Value *OffsetPtr = |
| 1679 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 1680 | |
| 1681 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 1682 | |
| 1683 | // Load the adjustment offset from the vtable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1684 | llvm::Value *Offset = |
| 1685 | CGF.Builder.CreateAlignedLoad(OffsetPtr, CGF.getPointerAlign()); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1686 | |
| 1687 | // Adjust our pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1688 | ResultPtr = CGF.Builder.CreateInBoundsGEP(V.getPointer(), Offset); |
| 1689 | } else { |
| 1690 | ResultPtr = V.getPointer(); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1693 | // In a derived-to-base conversion, the non-virtual adjustment is |
| 1694 | // applied second. |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1695 | if (NonVirtualAdjustment && IsReturnAdjustment) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1696 | ResultPtr = CGF.Builder.CreateConstInBoundsGEP1_64(ResultPtr, |
| 1697 | NonVirtualAdjustment); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | // Cast back to the original type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1701 | return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType()); |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1705 | Address This, |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1706 | const ThisAdjustment &TA) { |
Timur Iskhodzhanov | 053142a | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1707 | return performTypeAdjustment(CGF, This, TA.NonVirtual, |
| 1708 | TA.Virtual.Itanium.VCallOffsetOffset, |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1709 | /*IsReturnAdjustment=*/false); |
| 1710 | } |
| 1711 | |
| 1712 | llvm::Value * |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1713 | ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, Address Ret, |
Timur Iskhodzhanov | 0201432 | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1714 | const ReturnAdjustment &RA) { |
| 1715 | return performTypeAdjustment(CGF, Ret, RA.NonVirtual, |
| 1716 | RA.Virtual.Itanium.VBaseOffsetOffset, |
| 1717 | /*IsReturnAdjustment=*/true); |
| 1718 | } |
| 1719 | |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1720 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 1721 | RValue RV, QualType ResultType) { |
| 1722 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 1723 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 1724 | |
| 1725 | // Destructor thunks in the ARM ABI have indeterminate results. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1726 | llvm::Type *T = CGF.ReturnValue.getElementType(); |
John McCall | 5d865c32 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1727 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 1728 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 1729 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1730 | |
| 1731 | /************************** Array allocation cookies **************************/ |
| 1732 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1733 | CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
| 1734 | // The array cookie is a size_t; pad that up to the element alignment. |
| 1735 | // The cookie is actually right-justified in that space. |
| 1736 | return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), |
| 1737 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1740 | Address ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1741 | Address NewPtr, |
| 1742 | llvm::Value *NumElements, |
| 1743 | const CXXNewExpr *expr, |
| 1744 | QualType ElementType) { |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1745 | assert(requiresArrayCookie(expr)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1746 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1747 | unsigned AS = NewPtr.getAddressSpace(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1748 | |
John McCall | 9bca923 | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1749 | ASTContext &Ctx = getContext(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1750 | CharUnits SizeSize = CGF.getSizeSize(); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1751 | |
| 1752 | // The size of the cookie. |
| 1753 | CharUnits CookieSize = |
| 1754 | std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1755 | assert(CookieSize == getArrayCookieSizeImpl(ElementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1756 | |
| 1757 | // Compute an offset to the cookie. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1758 | Address CookiePtr = NewPtr; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1759 | CharUnits CookieOffset = CookieSize - SizeSize; |
| 1760 | if (!CookieOffset.isZero()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1761 | CookiePtr = CGF.Builder.CreateConstInBoundsByteGEP(CookiePtr, CookieOffset); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1762 | |
| 1763 | // Write the number of elements into the appropriate slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1764 | Address NumElementsPtr = |
| 1765 | CGF.Builder.CreateElementBitCast(CookiePtr, CGF.SizeTy); |
Kostya Serebryany | 4ee6904 | 2014-08-26 02:29:59 +0000 | [diff] [blame] | 1766 | llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1767 | |
| 1768 | // Handle the array cookie specially in ASan. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1769 | if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 && |
Kostya Serebryany | 4ee6904 | 2014-08-26 02:29:59 +0000 | [diff] [blame] | 1770 | expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) { |
Kostya Serebryany | 4a9187a | 2014-08-29 01:01:32 +0000 | [diff] [blame] | 1771 | // The store to the CookiePtr does not need to be instrumented. |
Kostya Serebryany | 4ee6904 | 2014-08-26 02:29:59 +0000 | [diff] [blame] | 1772 | CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); |
| 1773 | llvm::FunctionType *FTy = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1774 | llvm::FunctionType::get(CGM.VoidTy, NumElementsPtr.getType(), false); |
Kostya Serebryany | 4ee6904 | 2014-08-26 02:29:59 +0000 | [diff] [blame] | 1775 | llvm::Constant *F = |
| 1776 | CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1777 | CGF.Builder.CreateCall(F, NumElementsPtr.getPointer()); |
Kostya Serebryany | 4ee6904 | 2014-08-26 02:29:59 +0000 | [diff] [blame] | 1778 | } |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1781 | // over the cookie completely. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1782 | return CGF.Builder.CreateConstInBoundsByteGEP(NewPtr, CookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1785 | llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1786 | Address allocPtr, |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1787 | CharUnits cookieSize) { |
| 1788 | // The element size is right-justified in the cookie. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1789 | Address numElementsPtr = allocPtr; |
| 1790 | CharUnits numElementsOffset = cookieSize - CGF.getSizeSize(); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1791 | if (!numElementsOffset.isZero()) |
| 1792 | numElementsPtr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1793 | CGF.Builder.CreateConstInBoundsByteGEP(numElementsPtr, numElementsOffset); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1794 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1795 | unsigned AS = allocPtr.getAddressSpace(); |
| 1796 | numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy); |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1797 | if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0) |
Kostya Serebryany | 4a9187a | 2014-08-29 01:01:32 +0000 | [diff] [blame] | 1798 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 1799 | // In asan mode emit a function call instead of a regular load and let the |
| 1800 | // run-time deal with it: if the shadow is properly poisoned return the |
| 1801 | // cookie, otherwise return 0 to avoid an infinite loop calling DTORs. |
| 1802 | // We can't simply ignore this load using nosanitize metadata because |
| 1803 | // the metadata may be lost. |
| 1804 | llvm::FunctionType *FTy = |
| 1805 | llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false); |
| 1806 | llvm::Constant *F = |
| 1807 | CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1808 | return CGF.Builder.CreateCall(F, numElementsPtr.getPointer()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1811 | CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1812 | // ARM says that the cookie is always: |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1813 | // struct array_cookie { |
| 1814 | // std::size_t element_size; // element_size != 0 |
| 1815 | // std::size_t element_count; |
| 1816 | // }; |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1817 | // But the base ABI doesn't give anything an alignment greater than |
| 1818 | // 8, so we can dismiss this as typical ABI-author blindness to |
| 1819 | // actual language complexity and round up to the element alignment. |
| 1820 | return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), |
| 1821 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1824 | Address ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1825 | Address newPtr, |
| 1826 | llvm::Value *numElements, |
| 1827 | const CXXNewExpr *expr, |
| 1828 | QualType elementType) { |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1829 | assert(requiresArrayCookie(expr)); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1830 | |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1831 | // The cookie is always at the start of the buffer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1832 | Address cookie = newPtr; |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1833 | |
| 1834 | // The first element is the element size. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1835 | cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy); |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1836 | llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, |
| 1837 | getContext().getTypeSizeInChars(elementType).getQuantity()); |
| 1838 | CGF.Builder.CreateStore(elementSize, cookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1839 | |
| 1840 | // The second element is the element count. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1841 | cookie = CGF.Builder.CreateConstInBoundsGEP(cookie, 1, CGF.getSizeSize()); |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1842 | CGF.Builder.CreateStore(numElements, cookie); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1845 | // over the cookie completely. |
John McCall | c19c706 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1846 | CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1847 | return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1850 | llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1851 | Address allocPtr, |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1852 | CharUnits cookieSize) { |
| 1853 | // The number of elements is at offset sizeof(size_t) relative to |
| 1854 | // the allocated pointer. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1855 | Address numElementsPtr |
| 1856 | = CGF.Builder.CreateConstInBoundsByteGEP(allocPtr, CGF.getSizeSize()); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1857 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1858 | numElementsPtr = CGF.Builder.CreateElementBitCast(numElementsPtr, CGF.SizeTy); |
John McCall | b91cd66 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1859 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 8ed55a5 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1862 | /*********************** Static local initialization **************************/ |
| 1863 | |
| 1864 | static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1865 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1866 | // int __cxa_guard_acquire(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1867 | llvm::FunctionType *FTy = |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1868 | llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1869 | GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1870 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1871 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1872 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1873 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1877 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1878 | // void __cxa_guard_release(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1879 | llvm::FunctionType *FTy = |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1880 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1881 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1882 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1883 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1884 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1888 | llvm::PointerType *GuardPtrTy) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1889 | // void __cxa_guard_abort(__guard *guard_object); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1890 | llvm::FunctionType *FTy = |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1891 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | adcec49 | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1892 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", |
Bill Wendling | 8594fcb | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1893 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1894 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1895 | llvm::Attribute::NoUnwind)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1899 | struct CallGuardAbort final : EHScopeStack::Cleanup { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1900 | llvm::GlobalVariable *Guard; |
Chandler Carruth | 8453795 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1901 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1902 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1903 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1904 | CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), |
| 1905 | Guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1906 | } |
| 1907 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1908 | } |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1909 | |
| 1910 | /// The ARM code here follows the Itanium code closely enough that we |
| 1911 | /// just special-case it at particular places. |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1912 | void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, |
| 1913 | const VarDecl &D, |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1914 | llvm::GlobalVariable *var, |
| 1915 | bool shouldPerformInit) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1916 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1917 | |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1918 | // We only need to use thread-safe statics for local non-TLS variables; |
John McCall | cdf7ef5 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1919 | // global initialization is always single-threaded. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1920 | bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && |
| 1921 | D.isLocalVarDecl() && !D.getTLSKind(); |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1922 | |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1923 | // If we have a global variable with internal linkage and thread-safe statics |
| 1924 | // are disabled, we can just let the guard variable be of type i8. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1925 | bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); |
| 1926 | |
| 1927 | llvm::IntegerType *guardTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1928 | CharUnits guardAlignment; |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1929 | if (useInt8GuardVariable) { |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1930 | guardTy = CGF.Int8Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1931 | guardAlignment = CharUnits::One(); |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1932 | } else { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 1933 | // Guard variables are 64 bits in the generic ABI and size width on ARM |
| 1934 | // (i.e. 32-bit on AArch32, 64-bit on AArch64). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1935 | if (UseARMGuardVarABI) { |
| 1936 | guardTy = CGF.SizeTy; |
| 1937 | guardAlignment = CGF.getSizeAlign(); |
| 1938 | } else { |
| 1939 | guardTy = CGF.Int64Ty; |
| 1940 | guardAlignment = CharUnits::fromQuantity( |
| 1941 | CGM.getDataLayout().getABITypeAlignment(guardTy)); |
| 1942 | } |
Anders Carlsson | c5d3ba1 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1943 | } |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1944 | llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1945 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1946 | // Create the guard variable if we don't already have it (as we |
| 1947 | // might if we're double-emitting this function body). |
| 1948 | llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); |
| 1949 | if (!guard) { |
| 1950 | // Mangle the name for the guard. |
| 1951 | SmallString<256> guardName; |
| 1952 | { |
| 1953 | llvm::raw_svector_ostream out(guardName); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1954 | getMangleContext().mangleStaticGuardVariable(&D, out); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1955 | } |
John McCall | 8e7cb6d | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1956 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1957 | // Create the guard variable with a zero-initializer. |
| 1958 | // Just absorb linkage and visibility from the guarded variable. |
| 1959 | guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, |
| 1960 | false, var->getLinkage(), |
| 1961 | llvm::ConstantInt::get(guardTy, 0), |
| 1962 | guardName.str()); |
| 1963 | guard->setVisibility(var->getVisibility()); |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1964 | // If the variable is thread-local, so is its guard variable. |
| 1965 | guard->setThreadLocalMode(var->getThreadLocalMode()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1966 | guard->setAlignment(guardAlignment.getQuantity()); |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1967 | |
Yaron Keren | 5bfa108 | 2015-09-03 20:33:29 +0000 | [diff] [blame] | 1968 | // The ABI says: "It is suggested that it be emitted in the same COMDAT |
| 1969 | // group as the associated data object." In practice, this doesn't work for |
| 1970 | // non-ELF object formats, so only do it for ELF. |
Rafael Espindola | 0d4fb98 | 2015-01-12 22:13:53 +0000 | [diff] [blame] | 1971 | llvm::Comdat *C = var->getComdat(); |
Yaron Keren | 5bfa108 | 2015-09-03 20:33:29 +0000 | [diff] [blame] | 1972 | if (!D.isLocalVarDecl() && C && |
| 1973 | CGM.getTarget().getTriple().isOSBinFormatELF()) { |
Rafael Espindola | 2ae4b63 | 2014-09-19 19:43:18 +0000 | [diff] [blame] | 1974 | guard->setComdat(C); |
Rafael Espindola | 2ae4b63 | 2014-09-19 19:43:18 +0000 | [diff] [blame] | 1975 | CGF.CurFn->setComdat(C); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 1976 | } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) { |
| 1977 | guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName())); |
Rafael Espindola | 2ae4b63 | 2014-09-19 19:43:18 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1980 | CGM.setStaticLocalDeclGuardAddress(&D, guard); |
| 1981 | } |
John McCall | 87590e6 | 2012-03-30 07:09:50 +0000 | [diff] [blame] | 1982 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1983 | Address guardAddr = Address(guard, guardAlignment); |
| 1984 | |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1985 | // Test whether the variable has completed initialization. |
Justin Bogner | 0cbb6d8 | 2014-04-23 01:50:10 +0000 | [diff] [blame] | 1986 | // |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1987 | // Itanium C++ ABI 3.3.2: |
| 1988 | // The following is pseudo-code showing how these functions can be used: |
| 1989 | // if (obj_guard.first_byte == 0) { |
| 1990 | // if ( __cxa_guard_acquire (&obj_guard) ) { |
| 1991 | // try { |
| 1992 | // ... initialize the object ...; |
| 1993 | // } catch (...) { |
| 1994 | // __cxa_guard_abort (&obj_guard); |
| 1995 | // throw; |
| 1996 | // } |
| 1997 | // ... queue object destructor with __cxa_atexit() ...; |
| 1998 | // __cxa_guard_release (&obj_guard); |
| 1999 | // } |
| 2000 | // } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 2001 | |
Justin Bogner | 0cbb6d8 | 2014-04-23 01:50:10 +0000 | [diff] [blame] | 2002 | // Load the first byte of the guard variable. |
| 2003 | llvm::LoadInst *LI = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2004 | Builder.CreateLoad(Builder.CreateElementBitCast(guardAddr, CGM.Int8Ty)); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2005 | |
Justin Bogner | 0cbb6d8 | 2014-04-23 01:50:10 +0000 | [diff] [blame] | 2006 | // Itanium ABI: |
| 2007 | // An implementation supporting thread-safety on multiprocessor |
| 2008 | // systems must also guarantee that references to the initialized |
| 2009 | // object do not occur before the load of the initialization flag. |
| 2010 | // |
| 2011 | // In LLVM, we do this by marking the load Acquire. |
| 2012 | if (threadsafe) |
| 2013 | LI->setAtomic(llvm::Acquire); |
Eli Friedman | 84d2812 | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 2014 | |
Justin Bogner | 0cbb6d8 | 2014-04-23 01:50:10 +0000 | [diff] [blame] | 2015 | // For ARM, we should only check the first bit, rather than the entire byte: |
| 2016 | // |
| 2017 | // ARM C++ ABI 3.2.3.1: |
| 2018 | // To support the potential use of initialization guard variables |
| 2019 | // as semaphores that are the target of ARM SWP and LDREX/STREX |
| 2020 | // synchronizing instructions we define a static initialization |
| 2021 | // guard variable to be a 4-byte aligned, 4-byte word with the |
| 2022 | // following inline access protocol. |
| 2023 | // #define INITIALIZED 1 |
| 2024 | // if ((obj_guard & INITIALIZED) != INITIALIZED) { |
| 2025 | // if (__cxa_guard_acquire(&obj_guard)) |
| 2026 | // ... |
| 2027 | // } |
| 2028 | // |
| 2029 | // and similarly for ARM64: |
| 2030 | // |
| 2031 | // ARM64 C++ ABI 3.2.2: |
| 2032 | // This ABI instead only specifies the value bit 0 of the static guard |
| 2033 | // variable; all other bits are platform defined. Bit 0 shall be 0 when the |
| 2034 | // variable is not initialized and 1 when it is. |
| 2035 | llvm::Value *V = |
| 2036 | (UseARMGuardVarABI && !useInt8GuardVariable) |
| 2037 | ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1)) |
| 2038 | : LI; |
| 2039 | llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2040 | |
| 2041 | llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); |
| 2042 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 2043 | |
| 2044 | // Check if the first byte of the guard variable is zero. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 2045 | Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2046 | |
| 2047 | CGF.EmitBlock(InitCheckBlock); |
| 2048 | |
| 2049 | // Variables used when coping with thread-safe statics and exceptions. |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 2050 | if (threadsafe) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2051 | // Call __cxa_guard_acquire. |
| 2052 | llvm::Value *V |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2053 | = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2054 | |
| 2055 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 2056 | |
| 2057 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 2058 | InitBlock, EndBlock); |
| 2059 | |
| 2060 | // Call __cxa_guard_abort along the exceptional edge. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 2061 | CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2062 | |
| 2063 | CGF.EmitBlock(InitBlock); |
| 2064 | } |
| 2065 | |
| 2066 | // Emit the initializer and add a global destructor if appropriate. |
John McCall | b88a566 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 2067 | CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2068 | |
John McCall | 5aa5259 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 2069 | if (threadsafe) { |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2070 | // Pop the guard-abort cleanup if we pushed one. |
| 2071 | CGF.PopCleanupBlock(); |
| 2072 | |
| 2073 | // Call __cxa_guard_release. This cannot throw. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2074 | CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), |
| 2075 | guardAddr.getPointer()); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2076 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2077 | Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guardAddr); |
John McCall | 68ff037 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | CGF.EmitBlock(EndBlock); |
| 2081 | } |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2082 | |
| 2083 | /// Register a global destructor using __cxa_atexit. |
| 2084 | static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, |
| 2085 | llvm::Constant *dtor, |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2086 | llvm::Constant *addr, |
| 2087 | bool TLS) { |
Bill Wendling | 95cae88 | 2013-05-02 19:18:03 +0000 | [diff] [blame] | 2088 | const char *Name = "__cxa_atexit"; |
| 2089 | if (TLS) { |
| 2090 | const llvm::Triple &T = CGF.getTarget().getTriple(); |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 2091 | Name = T.isOSDarwin() ? "_tlv_atexit" : "__cxa_thread_atexit"; |
Bill Wendling | 95cae88 | 2013-05-02 19:18:03 +0000 | [diff] [blame] | 2092 | } |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2093 | |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2094 | // We're assuming that the destructor function is something we can |
| 2095 | // reasonably call with the default CC. Go ahead and cast it to the |
| 2096 | // right prototype. |
| 2097 | llvm::Type *dtorTy = |
| 2098 | llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); |
| 2099 | |
| 2100 | // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); |
| 2101 | llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; |
| 2102 | llvm::FunctionType *atexitTy = |
| 2103 | llvm::FunctionType::get(CGF.IntTy, paramTys, false); |
| 2104 | |
| 2105 | // Fetch the actual function. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2106 | llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2107 | if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) |
| 2108 | fn->setDoesNotThrow(); |
| 2109 | |
| 2110 | // Create a variable that binds the atexit to this shared object. |
| 2111 | llvm::Constant *handle = |
| 2112 | CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); |
| 2113 | |
| 2114 | llvm::Value *args[] = { |
| 2115 | llvm::ConstantExpr::getBitCast(dtor, dtorTy), |
| 2116 | llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), |
| 2117 | handle |
| 2118 | }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2119 | CGF.EmitNounwindRuntimeCall(atexit, args); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /// Register a global destructor as best as we know how. |
| 2123 | void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2124 | const VarDecl &D, |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2125 | llvm::Constant *dtor, |
| 2126 | llvm::Constant *addr) { |
| 2127 | // Use __cxa_atexit if available. |
Richard Smith | dbf74ba | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 2128 | if (CGM.getCodeGenOpts().CXAAtExit) |
| 2129 | return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); |
| 2130 | |
| 2131 | if (D.getTLSKind()) |
| 2132 | CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2133 | |
| 2134 | // In Apple kexts, we want to add a global destructor entry. |
| 2135 | // FIXME: shouldn't this be guarded by some variable? |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2136 | if (CGM.getLangOpts().AppleKext) { |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2137 | // Generate a global destructor entry. |
| 2138 | return CGM.AddCXXDtorEntry(dtor, addr); |
| 2139 | } |
| 2140 | |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 2141 | CGF.registerGlobalDtorWithAtExit(D, dtor, addr); |
John McCall | c84ed6a | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 2142 | } |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2143 | |
David Majnemer | 9b21c33 | 2014-07-11 20:28:10 +0000 | [diff] [blame] | 2144 | static bool isThreadWrapperReplaceable(const VarDecl *VD, |
| 2145 | CodeGen::CodeGenModule &CGM) { |
| 2146 | assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!"); |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 2147 | // Darwin prefers to have references to thread local variables to go through |
David Majnemer | 9b21c33 | 2014-07-11 20:28:10 +0000 | [diff] [blame] | 2148 | // the thread wrapper instead of directly referencing the backing variable. |
| 2149 | return VD->getTLSKind() == VarDecl::TLS_Dynamic && |
Manman Ren | f93fff2 | 2015-11-11 23:08:18 +0000 | [diff] [blame] | 2150 | CGM.getTarget().getTriple().isOSDarwin(); |
David Majnemer | 9b21c33 | 2014-07-11 20:28:10 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2153 | /// Get the appropriate linkage for the wrapper function. This is essentially |
David Majnemer | 4632e1e | 2014-06-27 16:56:27 +0000 | [diff] [blame] | 2154 | /// the weak form of the variable's linkage; every translation unit which needs |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2155 | /// the wrapper emits a copy, and we want the linker to merge them. |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2156 | static llvm::GlobalValue::LinkageTypes |
| 2157 | getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { |
| 2158 | llvm::GlobalValue::LinkageTypes VarLinkage = |
| 2159 | CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false); |
| 2160 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2161 | // For internal linkage variables, we don't need an external or weak wrapper. |
| 2162 | if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) |
| 2163 | return VarLinkage; |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2164 | |
David Majnemer | 9b21c33 | 2014-07-11 20:28:10 +0000 | [diff] [blame] | 2165 | // If the thread wrapper is replaceable, give it appropriate linkage. |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 2166 | if (isThreadWrapperReplaceable(VD, CGM)) |
| 2167 | if (!llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) && |
| 2168 | !llvm::GlobalVariable::isWeakODRLinkage(VarLinkage)) |
| 2169 | return VarLinkage; |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2170 | return llvm::GlobalValue::WeakODRLinkage; |
| 2171 | } |
| 2172 | |
| 2173 | llvm::Function * |
| 2174 | ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 2175 | llvm::Value *Val) { |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2176 | // Mangle the name for the thread_local wrapper function. |
| 2177 | SmallString<256> WrapperName; |
| 2178 | { |
| 2179 | llvm::raw_svector_ostream Out(WrapperName); |
| 2180 | getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 2183 | if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName)) |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2184 | return cast<llvm::Function>(V); |
| 2185 | |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 2186 | llvm::Type *RetTy = Val->getType(); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2187 | if (VD->getType()->isReferenceType()) |
| 2188 | RetTy = RetTy->getPointerElementType(); |
| 2189 | |
| 2190 | llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); |
David Majnemer | 35ab328 | 2014-06-11 04:08:55 +0000 | [diff] [blame] | 2191 | llvm::Function *Wrapper = |
| 2192 | llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM), |
| 2193 | WrapperName.str(), &CGM.getModule()); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2194 | // Always resolve references to the wrapper at link time. |
Manman Ren | 6815026 | 2015-11-11 22:42:31 +0000 | [diff] [blame] | 2195 | if (!Wrapper->hasLocalLinkage() && !(isThreadWrapperReplaceable(VD, CGM) && |
| 2196 | !llvm::GlobalVariable::isLinkOnceLinkage(Wrapper->getLinkage()) && |
| 2197 | !llvm::GlobalVariable::isWeakODRLinkage(Wrapper->getLinkage()))) |
Duncan P. N. Exon Smith | 4434d36 | 2014-05-07 22:36:11 +0000 | [diff] [blame] | 2198 | Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2199 | return Wrapper; |
| 2200 | } |
| 2201 | |
| 2202 | void ItaniumCXXABI::EmitThreadLocalInitFuncs( |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 2203 | CodeGenModule &CGM, |
| 2204 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 2205 | CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 2206 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { |
| 2207 | llvm::Function *InitFunc = nullptr; |
| 2208 | if (!CXXThreadLocalInits.empty()) { |
| 2209 | // Generate a guarded initialization function. |
| 2210 | llvm::FunctionType *FTy = |
| 2211 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
Akira Hatanaka | 7791f1a4 | 2015-10-31 01:28:07 +0000 | [diff] [blame] | 2212 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
| 2213 | InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", FI, |
Alexey Samsonov | 1444bb9 | 2014-10-17 00:20:19 +0000 | [diff] [blame] | 2214 | SourceLocation(), |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 2215 | /*TLS=*/true); |
| 2216 | llvm::GlobalVariable *Guard = new llvm::GlobalVariable( |
| 2217 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, |
| 2218 | llvm::GlobalVariable::InternalLinkage, |
| 2219 | llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard"); |
| 2220 | Guard->setThreadLocal(true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2221 | |
| 2222 | CharUnits GuardAlign = CharUnits::One(); |
| 2223 | Guard->setAlignment(GuardAlign.getQuantity()); |
| 2224 | |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 2225 | CodeGenFunction(CGM) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2226 | .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, |
| 2227 | Address(Guard, GuardAlign)); |
David Majnemer | b3341ea | 2014-10-05 05:05:40 +0000 | [diff] [blame] | 2228 | } |
Yaron Keren | ede6030 | 2015-08-01 19:11:36 +0000 | [diff] [blame] | 2229 | for (auto &I : CXXThreadLocals) { |
| 2230 | const VarDecl *VD = I.first; |
| 2231 | llvm::GlobalVariable *Var = I.second; |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2232 | |
David Majnemer | 9b21c33 | 2014-07-11 20:28:10 +0000 | [diff] [blame] | 2233 | // Some targets require that all access to thread local variables go through |
| 2234 | // the thread wrapper. This means that we cannot attempt to create a thread |
| 2235 | // wrapper or a thread helper. |
| 2236 | if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) |
| 2237 | continue; |
| 2238 | |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2239 | // Mangle the name for the thread_local initialization function. |
| 2240 | SmallString<256> InitFnName; |
| 2241 | { |
| 2242 | llvm::raw_svector_ostream Out(InitFnName); |
| 2243 | getMangleContext().mangleItaniumThreadLocalInit(VD, Out); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | // If we have a definition for the variable, emit the initialization |
| 2247 | // function as an alias to the global Init function (if any). Otherwise, |
| 2248 | // produce a declaration of the initialization function. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2249 | llvm::GlobalValue *Init = nullptr; |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2250 | bool InitIsInitFunc = false; |
| 2251 | if (VD->hasDefinition()) { |
| 2252 | InitIsInitFunc = true; |
| 2253 | if (InitFunc) |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 2254 | Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(), |
| 2255 | InitFunc); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2256 | } else { |
| 2257 | // Emit a weak global function referring to the initialization function. |
| 2258 | // This function will not exist if the TU defining the thread_local |
| 2259 | // variable in question does not need any dynamic initialization for |
| 2260 | // its thread_local variables. |
| 2261 | llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); |
| 2262 | Init = llvm::Function::Create( |
| 2263 | FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), |
| 2264 | &CGM.getModule()); |
| 2265 | } |
| 2266 | |
| 2267 | if (Init) |
| 2268 | Init->setVisibility(Var->getVisibility()); |
| 2269 | |
| 2270 | llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); |
| 2271 | llvm::LLVMContext &Context = CGM.getModule().getContext(); |
| 2272 | llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2273 | CGBuilderTy Builder(CGM, Entry); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2274 | if (InitIsInitFunc) { |
| 2275 | if (Init) |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 2276 | Builder.CreateCall(Init); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2277 | } else { |
| 2278 | // Don't know whether we have an init function. Call it if it exists. |
| 2279 | llvm::Value *Have = Builder.CreateIsNotNull(Init); |
| 2280 | llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 2281 | llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 2282 | Builder.CreateCondBr(Have, InitBB, ExitBB); |
| 2283 | |
| 2284 | Builder.SetInsertPoint(InitBB); |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 2285 | Builder.CreateCall(Init); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2286 | Builder.CreateBr(ExitBB); |
| 2287 | |
| 2288 | Builder.SetInsertPoint(ExitBB); |
| 2289 | } |
| 2290 | |
| 2291 | // For a reference, the result of the wrapper function is a pointer to |
| 2292 | // the referenced object. |
| 2293 | llvm::Value *Val = Var; |
| 2294 | if (VD->getType()->isReferenceType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2295 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 2296 | Val = Builder.CreateAlignedLoad(Val, Align); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2297 | } |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 2298 | if (Val->getType() != Wrapper->getReturnType()) |
| 2299 | Val = Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2300 | Val, Wrapper->getReturnType(), ""); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2301 | Builder.CreateRet(Val); |
| 2302 | } |
| 2303 | } |
| 2304 | |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2305 | LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, |
| 2306 | const VarDecl *VD, |
| 2307 | QualType LValType) { |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2308 | QualType T = VD->getType(); |
| 2309 | llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); |
| 2310 | llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); |
Alexander Musman | f94c318 | 2014-09-26 06:28:25 +0000 | [diff] [blame] | 2311 | llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2312 | |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 2313 | Val = CGF.Builder.CreateCall(Wrapper); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2314 | |
| 2315 | LValue LV; |
| 2316 | if (VD->getType()->isReferenceType()) |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2317 | LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2318 | else |
Richard Smith | 0f38374 | 2014-03-26 22:48:22 +0000 | [diff] [blame] | 2319 | LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD)); |
Richard Smith | 2fd1d7a | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2320 | // FIXME: need setObjCGCLValueClass? |
| 2321 | return LV; |
| 2322 | } |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 2323 | |
| 2324 | /// Return whether the given global decl needs a VTT parameter, which it does |
| 2325 | /// if it's a base constructor or destructor with virtual bases. |
| 2326 | bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { |
| 2327 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 2328 | |
| 2329 | // We don't have any virtual bases, just return early. |
| 2330 | if (!MD->getParent()->getNumVBases()) |
| 2331 | return false; |
| 2332 | |
| 2333 | // Check if we have a base constructor. |
| 2334 | if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) |
| 2335 | return true; |
| 2336 | |
| 2337 | // Check if we have a base destructor. |
| 2338 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 2339 | return true; |
| 2340 | |
| 2341 | return false; |
| 2342 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2343 | |
| 2344 | namespace { |
| 2345 | class ItaniumRTTIBuilder { |
| 2346 | CodeGenModule &CGM; // Per-module state. |
| 2347 | llvm::LLVMContext &VMContext; |
| 2348 | const ItaniumCXXABI &CXXABI; // Per-module state. |
| 2349 | |
| 2350 | /// Fields - The fields of the RTTI descriptor currently being built. |
| 2351 | SmallVector<llvm::Constant *, 16> Fields; |
| 2352 | |
| 2353 | /// GetAddrOfTypeName - Returns the mangled type name of the given type. |
| 2354 | llvm::GlobalVariable * |
| 2355 | GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage); |
| 2356 | |
| 2357 | /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI |
| 2358 | /// descriptor of the given type. |
| 2359 | llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); |
| 2360 | |
| 2361 | /// BuildVTablePointer - Build the vtable pointer for the given type. |
| 2362 | void BuildVTablePointer(const Type *Ty); |
| 2363 | |
| 2364 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
| 2365 | /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. |
| 2366 | void BuildSIClassTypeInfo(const CXXRecordDecl *RD); |
| 2367 | |
| 2368 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 2369 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 2370 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 2371 | void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); |
| 2372 | |
| 2373 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used |
| 2374 | /// for pointer types. |
| 2375 | void BuildPointerTypeInfo(QualType PointeeTy); |
| 2376 | |
| 2377 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of |
| 2378 | /// type_info for an object type. |
| 2379 | void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); |
| 2380 | |
| 2381 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 2382 | /// struct, used for member pointer types. |
| 2383 | void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); |
| 2384 | |
| 2385 | public: |
| 2386 | ItaniumRTTIBuilder(const ItaniumCXXABI &ABI) |
| 2387 | : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {} |
| 2388 | |
| 2389 | // Pointer type info flags. |
| 2390 | enum { |
| 2391 | /// PTI_Const - Type has const qualifier. |
| 2392 | PTI_Const = 0x1, |
| 2393 | |
| 2394 | /// PTI_Volatile - Type has volatile qualifier. |
| 2395 | PTI_Volatile = 0x2, |
| 2396 | |
| 2397 | /// PTI_Restrict - Type has restrict qualifier. |
| 2398 | PTI_Restrict = 0x4, |
| 2399 | |
| 2400 | /// PTI_Incomplete - Type is incomplete. |
| 2401 | PTI_Incomplete = 0x8, |
| 2402 | |
| 2403 | /// PTI_ContainingClassIncomplete - Containing class is incomplete. |
| 2404 | /// (in pointer to member). |
| 2405 | PTI_ContainingClassIncomplete = 0x10 |
| 2406 | }; |
| 2407 | |
| 2408 | // VMI type info flags. |
| 2409 | enum { |
| 2410 | /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. |
| 2411 | VMI_NonDiamondRepeat = 0x1, |
| 2412 | |
| 2413 | /// VMI_DiamondShaped - Class is diamond shaped. |
| 2414 | VMI_DiamondShaped = 0x2 |
| 2415 | }; |
| 2416 | |
| 2417 | // Base class type info flags. |
| 2418 | enum { |
| 2419 | /// BCTI_Virtual - Base class is virtual. |
| 2420 | BCTI_Virtual = 0x1, |
| 2421 | |
| 2422 | /// BCTI_Public - Base class is public. |
| 2423 | BCTI_Public = 0x2 |
| 2424 | }; |
| 2425 | |
| 2426 | /// BuildTypeInfo - Build the RTTI type info struct for the given type. |
| 2427 | /// |
| 2428 | /// \param Force - true to force the creation of this RTTI value |
| 2429 | llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false); |
| 2430 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2431 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2432 | |
| 2433 | llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName( |
| 2434 | QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) { |
Yaron Keren | e46f7ed | 2015-07-29 14:21:47 +0000 | [diff] [blame] | 2435 | SmallString<256> Name; |
| 2436 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2437 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2438 | |
| 2439 | // We know that the mangled name of the type starts at index 4 of the |
| 2440 | // mangled name of the typename, so we can just index into it in order to |
| 2441 | // get the mangled name of the type. |
| 2442 | llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, |
| 2443 | Name.substr(4)); |
| 2444 | |
| 2445 | llvm::GlobalVariable *GV = |
| 2446 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage); |
| 2447 | |
| 2448 | GV->setInitializer(Init); |
| 2449 | |
| 2450 | return GV; |
| 2451 | } |
| 2452 | |
| 2453 | llvm::Constant * |
| 2454 | ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { |
| 2455 | // Mangle the RTTI name. |
Yaron Keren | e46f7ed | 2015-07-29 14:21:47 +0000 | [diff] [blame] | 2456 | SmallString<256> Name; |
| 2457 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2458 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2459 | |
| 2460 | // Look for an existing global. |
| 2461 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); |
| 2462 | |
| 2463 | if (!GV) { |
| 2464 | // Create a new global variable. |
| 2465 | GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, |
| 2466 | /*Constant=*/true, |
| 2467 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 2468 | Name); |
David Majnemer | 1fb1a04 | 2014-11-07 07:26:38 +0000 | [diff] [blame] | 2469 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2470 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2471 | if (RD->hasAttr<DLLImportAttr>()) |
| 2472 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 2473 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 2477 | } |
| 2478 | |
| 2479 | /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type |
| 2480 | /// info for that type is defined in the standard library. |
| 2481 | static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { |
| 2482 | // Itanium C++ ABI 2.9.2: |
| 2483 | // Basic type information (e.g. for "int", "bool", etc.) will be kept in |
| 2484 | // the run-time support library. Specifically, the run-time support |
| 2485 | // library should contain type_info objects for the types X, X* and |
| 2486 | // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char, |
| 2487 | // unsigned char, signed char, short, unsigned short, int, unsigned int, |
| 2488 | // long, unsigned long, long long, unsigned long long, float, double, |
| 2489 | // long double, char16_t, char32_t, and the IEEE 754r decimal and |
| 2490 | // half-precision floating point types. |
| 2491 | switch (Ty->getKind()) { |
| 2492 | case BuiltinType::Void: |
| 2493 | case BuiltinType::NullPtr: |
| 2494 | case BuiltinType::Bool: |
| 2495 | case BuiltinType::WChar_S: |
| 2496 | case BuiltinType::WChar_U: |
| 2497 | case BuiltinType::Char_U: |
| 2498 | case BuiltinType::Char_S: |
| 2499 | case BuiltinType::UChar: |
| 2500 | case BuiltinType::SChar: |
| 2501 | case BuiltinType::Short: |
| 2502 | case BuiltinType::UShort: |
| 2503 | case BuiltinType::Int: |
| 2504 | case BuiltinType::UInt: |
| 2505 | case BuiltinType::Long: |
| 2506 | case BuiltinType::ULong: |
| 2507 | case BuiltinType::LongLong: |
| 2508 | case BuiltinType::ULongLong: |
| 2509 | case BuiltinType::Half: |
| 2510 | case BuiltinType::Float: |
| 2511 | case BuiltinType::Double: |
| 2512 | case BuiltinType::LongDouble: |
| 2513 | case BuiltinType::Char16: |
| 2514 | case BuiltinType::Char32: |
| 2515 | case BuiltinType::Int128: |
| 2516 | case BuiltinType::UInt128: |
| 2517 | case BuiltinType::OCLImage1d: |
| 2518 | case BuiltinType::OCLImage1dArray: |
| 2519 | case BuiltinType::OCLImage1dBuffer: |
| 2520 | case BuiltinType::OCLImage2d: |
| 2521 | case BuiltinType::OCLImage2dArray: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 2522 | case BuiltinType::OCLImage2dDepth: |
| 2523 | case BuiltinType::OCLImage2dArrayDepth: |
| 2524 | case BuiltinType::OCLImage2dMSAA: |
| 2525 | case BuiltinType::OCLImage2dArrayMSAA: |
| 2526 | case BuiltinType::OCLImage2dMSAADepth: |
| 2527 | case BuiltinType::OCLImage2dArrayMSAADepth: |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2528 | case BuiltinType::OCLImage3d: |
| 2529 | case BuiltinType::OCLSampler: |
| 2530 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 2531 | case BuiltinType::OCLClkEvent: |
| 2532 | case BuiltinType::OCLQueue: |
| 2533 | case BuiltinType::OCLNDRange: |
| 2534 | case BuiltinType::OCLReserveID: |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2535 | return true; |
| 2536 | |
| 2537 | case BuiltinType::Dependent: |
| 2538 | #define BUILTIN_TYPE(Id, SingletonId) |
| 2539 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 2540 | case BuiltinType::Id: |
| 2541 | #include "clang/AST/BuiltinTypes.def" |
| 2542 | llvm_unreachable("asking for RRTI for a placeholder type!"); |
| 2543 | |
| 2544 | case BuiltinType::ObjCId: |
| 2545 | case BuiltinType::ObjCClass: |
| 2546 | case BuiltinType::ObjCSel: |
| 2547 | llvm_unreachable("FIXME: Objective-C types are unsupported!"); |
| 2548 | } |
| 2549 | |
| 2550 | llvm_unreachable("Invalid BuiltinType Kind!"); |
| 2551 | } |
| 2552 | |
| 2553 | static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { |
| 2554 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2555 | const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); |
| 2556 | if (!BuiltinTy) |
| 2557 | return false; |
| 2558 | |
| 2559 | // Check the qualifiers. |
| 2560 | Qualifiers Quals = PointeeTy.getQualifiers(); |
| 2561 | Quals.removeConst(); |
| 2562 | |
| 2563 | if (!Quals.empty()) |
| 2564 | return false; |
| 2565 | |
| 2566 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 2567 | } |
| 2568 | |
| 2569 | /// IsStandardLibraryRTTIDescriptor - Returns whether the type |
| 2570 | /// information for the given type exists in the standard library. |
| 2571 | static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { |
| 2572 | // Type info for builtin types is defined in the standard library. |
| 2573 | if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) |
| 2574 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 2575 | |
| 2576 | // Type info for some pointer types to builtin types is defined in the |
| 2577 | // standard library. |
| 2578 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 2579 | return TypeInfoIsInStandardLibrary(PointerTy); |
| 2580 | |
| 2581 | return false; |
| 2582 | } |
| 2583 | |
| 2584 | /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for |
| 2585 | /// the given type exists somewhere else, and that we should not emit the type |
| 2586 | /// information in this translation unit. Assumes that it is not a |
| 2587 | /// standard-library type. |
| 2588 | static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, |
| 2589 | QualType Ty) { |
| 2590 | ASTContext &Context = CGM.getContext(); |
| 2591 | |
| 2592 | // If RTTI is disabled, assume it might be disabled in the |
| 2593 | // translation unit that defines any potential key function, too. |
| 2594 | if (!Context.getLangOpts().RTTI) return false; |
| 2595 | |
| 2596 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2597 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2598 | if (!RD->hasDefinition()) |
| 2599 | return false; |
| 2600 | |
| 2601 | if (!RD->isDynamicClass()) |
| 2602 | return false; |
| 2603 | |
| 2604 | // FIXME: this may need to be reconsidered if the key function |
| 2605 | // changes. |
David Majnemer | be9022c | 2015-08-06 20:56:55 +0000 | [diff] [blame] | 2606 | // N.B. We must always emit the RTTI data ourselves if there exists a key |
| 2607 | // function. |
| 2608 | bool IsDLLImport = RD->hasAttr<DLLImportAttr>(); |
David Majnemer | 1fb1a04 | 2014-11-07 07:26:38 +0000 | [diff] [blame] | 2609 | if (CGM.getVTables().isVTableExternal(RD)) |
David Majnemer | be9022c | 2015-08-06 20:56:55 +0000 | [diff] [blame] | 2610 | return IsDLLImport ? false : true; |
David Majnemer | 1fb1a04 | 2014-11-07 07:26:38 +0000 | [diff] [blame] | 2611 | |
David Majnemer | be9022c | 2015-08-06 20:56:55 +0000 | [diff] [blame] | 2612 | if (IsDLLImport) |
David Majnemer | 1fb1a04 | 2014-11-07 07:26:38 +0000 | [diff] [blame] | 2613 | return true; |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | return false; |
| 2617 | } |
| 2618 | |
| 2619 | /// IsIncompleteClassType - Returns whether the given record type is incomplete. |
| 2620 | static bool IsIncompleteClassType(const RecordType *RecordTy) { |
| 2621 | return !RecordTy->getDecl()->isCompleteDefinition(); |
| 2622 | } |
| 2623 | |
| 2624 | /// ContainsIncompleteClassType - Returns whether the given type contains an |
| 2625 | /// incomplete class type. This is true if |
| 2626 | /// |
| 2627 | /// * The given type is an incomplete class type. |
| 2628 | /// * The given type is a pointer type whose pointee type contains an |
| 2629 | /// incomplete class type. |
| 2630 | /// * The given type is a member pointer type whose class is an incomplete |
| 2631 | /// class type. |
| 2632 | /// * The given type is a member pointer type whoise pointee type contains an |
| 2633 | /// incomplete class type. |
| 2634 | /// is an indirect or direct pointer to an incomplete class type. |
| 2635 | static bool ContainsIncompleteClassType(QualType Ty) { |
| 2636 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2637 | if (IsIncompleteClassType(RecordTy)) |
| 2638 | return true; |
| 2639 | } |
| 2640 | |
| 2641 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 2642 | return ContainsIncompleteClassType(PointerTy->getPointeeType()); |
| 2643 | |
| 2644 | if (const MemberPointerType *MemberPointerTy = |
| 2645 | dyn_cast<MemberPointerType>(Ty)) { |
| 2646 | // Check if the class type is incomplete. |
| 2647 | const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); |
| 2648 | if (IsIncompleteClassType(ClassType)) |
| 2649 | return true; |
| 2650 | |
| 2651 | return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); |
| 2652 | } |
| 2653 | |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
| 2657 | // CanUseSingleInheritance - Return whether the given record decl has a "single, |
| 2658 | // public, non-virtual base at offset zero (i.e. the derived class is dynamic |
| 2659 | // iff the base is)", according to Itanium C++ ABI, 2.95p6b. |
| 2660 | static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { |
| 2661 | // Check the number of bases. |
| 2662 | if (RD->getNumBases() != 1) |
| 2663 | return false; |
| 2664 | |
| 2665 | // Get the base. |
| 2666 | CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); |
| 2667 | |
| 2668 | // Check that the base is not virtual. |
| 2669 | if (Base->isVirtual()) |
| 2670 | return false; |
| 2671 | |
| 2672 | // Check that the base is public. |
| 2673 | if (Base->getAccessSpecifier() != AS_public) |
| 2674 | return false; |
| 2675 | |
| 2676 | // Check that the class is dynamic iff the base is. |
| 2677 | const CXXRecordDecl *BaseDecl = |
| 2678 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 2679 | if (!BaseDecl->isEmpty() && |
| 2680 | BaseDecl->isDynamicClass() != RD->isDynamicClass()) |
| 2681 | return false; |
| 2682 | |
| 2683 | return true; |
| 2684 | } |
| 2685 | |
| 2686 | void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) { |
| 2687 | // abi::__class_type_info. |
| 2688 | static const char * const ClassTypeInfo = |
| 2689 | "_ZTVN10__cxxabiv117__class_type_infoE"; |
| 2690 | // abi::__si_class_type_info. |
| 2691 | static const char * const SIClassTypeInfo = |
| 2692 | "_ZTVN10__cxxabiv120__si_class_type_infoE"; |
| 2693 | // abi::__vmi_class_type_info. |
| 2694 | static const char * const VMIClassTypeInfo = |
| 2695 | "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; |
| 2696 | |
| 2697 | const char *VTableName = nullptr; |
| 2698 | |
| 2699 | switch (Ty->getTypeClass()) { |
| 2700 | #define TYPE(Class, Base) |
| 2701 | #define ABSTRACT_TYPE(Class, Base) |
| 2702 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2703 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2704 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2705 | #include "clang/AST/TypeNodes.def" |
| 2706 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
| 2707 | |
| 2708 | case Type::LValueReference: |
| 2709 | case Type::RValueReference: |
| 2710 | llvm_unreachable("References shouldn't get here"); |
| 2711 | |
| 2712 | case Type::Auto: |
| 2713 | llvm_unreachable("Undeduced auto type shouldn't get here"); |
| 2714 | |
| 2715 | case Type::Builtin: |
| 2716 | // GCC treats vector and complex types as fundamental types. |
| 2717 | case Type::Vector: |
| 2718 | case Type::ExtVector: |
| 2719 | case Type::Complex: |
| 2720 | case Type::Atomic: |
| 2721 | // FIXME: GCC treats block pointers as fundamental types?! |
| 2722 | case Type::BlockPointer: |
| 2723 | // abi::__fundamental_type_info. |
| 2724 | VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; |
| 2725 | break; |
| 2726 | |
| 2727 | case Type::ConstantArray: |
| 2728 | case Type::IncompleteArray: |
| 2729 | case Type::VariableArray: |
| 2730 | // abi::__array_type_info. |
| 2731 | VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; |
| 2732 | break; |
| 2733 | |
| 2734 | case Type::FunctionNoProto: |
| 2735 | case Type::FunctionProto: |
| 2736 | // abi::__function_type_info. |
| 2737 | VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; |
| 2738 | break; |
| 2739 | |
| 2740 | case Type::Enum: |
| 2741 | // abi::__enum_type_info. |
| 2742 | VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; |
| 2743 | break; |
| 2744 | |
| 2745 | case Type::Record: { |
| 2746 | const CXXRecordDecl *RD = |
| 2747 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 2748 | |
| 2749 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
| 2750 | VTableName = ClassTypeInfo; |
| 2751 | } else if (CanUseSingleInheritance(RD)) { |
| 2752 | VTableName = SIClassTypeInfo; |
| 2753 | } else { |
| 2754 | VTableName = VMIClassTypeInfo; |
| 2755 | } |
| 2756 | |
| 2757 | break; |
| 2758 | } |
| 2759 | |
| 2760 | case Type::ObjCObject: |
| 2761 | // Ignore protocol qualifiers. |
| 2762 | Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); |
| 2763 | |
| 2764 | // Handle id and Class. |
| 2765 | if (isa<BuiltinType>(Ty)) { |
| 2766 | VTableName = ClassTypeInfo; |
| 2767 | break; |
| 2768 | } |
| 2769 | |
| 2770 | assert(isa<ObjCInterfaceType>(Ty)); |
| 2771 | // Fall through. |
| 2772 | |
| 2773 | case Type::ObjCInterface: |
| 2774 | if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { |
| 2775 | VTableName = SIClassTypeInfo; |
| 2776 | } else { |
| 2777 | VTableName = ClassTypeInfo; |
| 2778 | } |
| 2779 | break; |
| 2780 | |
| 2781 | case Type::ObjCObjectPointer: |
| 2782 | case Type::Pointer: |
| 2783 | // abi::__pointer_type_info. |
| 2784 | VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; |
| 2785 | break; |
| 2786 | |
| 2787 | case Type::MemberPointer: |
| 2788 | // abi::__pointer_to_member_type_info. |
| 2789 | VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; |
| 2790 | break; |
| 2791 | } |
| 2792 | |
| 2793 | llvm::Constant *VTable = |
| 2794 | CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy); |
| 2795 | |
| 2796 | llvm::Type *PtrDiffTy = |
| 2797 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 2798 | |
| 2799 | // The vtable address point is 2. |
| 2800 | llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2801 | VTable = |
| 2802 | llvm::ConstantExpr::getInBoundsGetElementPtr(CGM.Int8PtrTy, VTable, Two); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2803 | VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy); |
| 2804 | |
| 2805 | Fields.push_back(VTable); |
| 2806 | } |
| 2807 | |
| 2808 | /// \brief Return the linkage that the type info and type info name constants |
| 2809 | /// should have for the given type. |
| 2810 | static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, |
| 2811 | QualType Ty) { |
| 2812 | // Itanium C++ ABI 2.9.5p7: |
| 2813 | // In addition, it and all of the intermediate abi::__pointer_type_info |
| 2814 | // structs in the chain down to the abi::__class_type_info for the |
| 2815 | // incomplete class type must be prevented from resolving to the |
| 2816 | // corresponding type_info structs for the complete class type, possibly |
| 2817 | // by making them local static objects. Finally, a dummy class RTTI is |
| 2818 | // generated for the incomplete type that will not resolve to the final |
| 2819 | // complete class RTTI (because the latter need not exist), possibly by |
| 2820 | // making it a local static object. |
| 2821 | if (ContainsIncompleteClassType(Ty)) |
| 2822 | return llvm::GlobalValue::InternalLinkage; |
| 2823 | |
| 2824 | switch (Ty->getLinkage()) { |
| 2825 | case NoLinkage: |
| 2826 | case InternalLinkage: |
| 2827 | case UniqueExternalLinkage: |
| 2828 | return llvm::GlobalValue::InternalLinkage; |
| 2829 | |
| 2830 | case VisibleNoLinkage: |
| 2831 | case ExternalLinkage: |
| 2832 | if (!CGM.getLangOpts().RTTI) { |
| 2833 | // RTTI is not enabled, which means that this type info struct is going |
| 2834 | // to be used for exception handling. Give it linkonce_odr linkage. |
| 2835 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2836 | } |
| 2837 | |
| 2838 | if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { |
| 2839 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 2840 | if (RD->hasAttr<WeakAttr>()) |
| 2841 | return llvm::GlobalValue::WeakODRLinkage; |
David Majnemer | be9022c | 2015-08-06 20:56:55 +0000 | [diff] [blame] | 2842 | if (RD->isDynamicClass()) { |
| 2843 | llvm::GlobalValue::LinkageTypes LT = CGM.getVTableLinkage(RD); |
| 2844 | // MinGW won't export the RTTI information when there is a key function. |
| 2845 | // Make sure we emit our own copy instead of attempting to dllimport it. |
| 2846 | if (RD->hasAttr<DLLImportAttr>() && |
| 2847 | llvm::GlobalValue::isAvailableExternallyLinkage(LT)) |
| 2848 | LT = llvm::GlobalValue::LinkOnceODRLinkage; |
| 2849 | return LT; |
| 2850 | } |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2854 | } |
| 2855 | |
| 2856 | llvm_unreachable("Invalid linkage!"); |
| 2857 | } |
| 2858 | |
| 2859 | llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) { |
| 2860 | // We want to operate on the canonical type. |
| 2861 | Ty = CGM.getContext().getCanonicalType(Ty); |
| 2862 | |
| 2863 | // Check if we've already emitted an RTTI descriptor for this type. |
Yaron Keren | e46f7ed | 2015-07-29 14:21:47 +0000 | [diff] [blame] | 2864 | SmallString<256> Name; |
| 2865 | llvm::raw_svector_ostream Out(Name); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2866 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2867 | |
| 2868 | llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); |
| 2869 | if (OldGV && !OldGV->isDeclaration()) { |
| 2870 | assert(!OldGV->hasAvailableExternallyLinkage() && |
| 2871 | "available_externally typeinfos not yet implemented"); |
| 2872 | |
| 2873 | return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy); |
| 2874 | } |
| 2875 | |
| 2876 | // Check if there is already an external RTTI descriptor for this type. |
| 2877 | bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); |
| 2878 | if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty))) |
| 2879 | return GetAddrOfExternalRTTIDescriptor(Ty); |
| 2880 | |
| 2881 | // Emit the standard library with external linkage. |
| 2882 | llvm::GlobalVariable::LinkageTypes Linkage; |
| 2883 | if (IsStdLib) |
| 2884 | Linkage = llvm::GlobalValue::ExternalLinkage; |
| 2885 | else |
| 2886 | Linkage = getTypeInfoLinkage(CGM, Ty); |
| 2887 | |
| 2888 | // Add the vtable pointer. |
| 2889 | BuildVTablePointer(cast<Type>(Ty)); |
| 2890 | |
| 2891 | // And the name. |
| 2892 | llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage); |
| 2893 | llvm::Constant *TypeNameField; |
| 2894 | |
| 2895 | // If we're supposed to demote the visibility, be sure to set a flag |
| 2896 | // to use a string comparison for type_info comparisons. |
| 2897 | ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness = |
| 2898 | CXXABI.classifyRTTIUniqueness(Ty, Linkage); |
| 2899 | if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) { |
| 2900 | // The flag is the sign bit, which on ARM64 is defined to be clear |
| 2901 | // for global pointers. This is very ARM64-specific. |
| 2902 | TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty); |
| 2903 | llvm::Constant *flag = |
| 2904 | llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63); |
| 2905 | TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag); |
| 2906 | TypeNameField = |
| 2907 | llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy); |
| 2908 | } else { |
| 2909 | TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy); |
| 2910 | } |
| 2911 | Fields.push_back(TypeNameField); |
| 2912 | |
| 2913 | switch (Ty->getTypeClass()) { |
| 2914 | #define TYPE(Class, Base) |
| 2915 | #define ABSTRACT_TYPE(Class, Base) |
| 2916 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2917 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2918 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2919 | #include "clang/AST/TypeNodes.def" |
| 2920 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
| 2921 | |
| 2922 | // GCC treats vector types as fundamental types. |
| 2923 | case Type::Builtin: |
| 2924 | case Type::Vector: |
| 2925 | case Type::ExtVector: |
| 2926 | case Type::Complex: |
| 2927 | case Type::BlockPointer: |
| 2928 | // Itanium C++ ABI 2.9.5p4: |
| 2929 | // abi::__fundamental_type_info adds no data members to std::type_info. |
| 2930 | break; |
| 2931 | |
| 2932 | case Type::LValueReference: |
| 2933 | case Type::RValueReference: |
| 2934 | llvm_unreachable("References shouldn't get here"); |
| 2935 | |
| 2936 | case Type::Auto: |
| 2937 | llvm_unreachable("Undeduced auto type shouldn't get here"); |
| 2938 | |
| 2939 | case Type::ConstantArray: |
| 2940 | case Type::IncompleteArray: |
| 2941 | case Type::VariableArray: |
| 2942 | // Itanium C++ ABI 2.9.5p5: |
| 2943 | // abi::__array_type_info adds no data members to std::type_info. |
| 2944 | break; |
| 2945 | |
| 2946 | case Type::FunctionNoProto: |
| 2947 | case Type::FunctionProto: |
| 2948 | // Itanium C++ ABI 2.9.5p5: |
| 2949 | // abi::__function_type_info adds no data members to std::type_info. |
| 2950 | break; |
| 2951 | |
| 2952 | case Type::Enum: |
| 2953 | // Itanium C++ ABI 2.9.5p5: |
| 2954 | // abi::__enum_type_info adds no data members to std::type_info. |
| 2955 | break; |
| 2956 | |
| 2957 | case Type::Record: { |
| 2958 | const CXXRecordDecl *RD = |
| 2959 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 2960 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
| 2961 | // We don't need to emit any fields. |
| 2962 | break; |
| 2963 | } |
| 2964 | |
| 2965 | if (CanUseSingleInheritance(RD)) |
| 2966 | BuildSIClassTypeInfo(RD); |
| 2967 | else |
| 2968 | BuildVMIClassTypeInfo(RD); |
| 2969 | |
| 2970 | break; |
| 2971 | } |
| 2972 | |
| 2973 | case Type::ObjCObject: |
| 2974 | case Type::ObjCInterface: |
| 2975 | BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); |
| 2976 | break; |
| 2977 | |
| 2978 | case Type::ObjCObjectPointer: |
| 2979 | BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); |
| 2980 | break; |
| 2981 | |
| 2982 | case Type::Pointer: |
| 2983 | BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); |
| 2984 | break; |
| 2985 | |
| 2986 | case Type::MemberPointer: |
| 2987 | BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); |
| 2988 | break; |
| 2989 | |
| 2990 | case Type::Atomic: |
| 2991 | // No fields, at least for the moment. |
| 2992 | break; |
| 2993 | } |
| 2994 | |
| 2995 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields); |
| 2996 | |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2997 | llvm::Module &M = CGM.getModule(); |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 2998 | llvm::GlobalVariable *GV = |
Rafael Espindola | cb92c19 | 2015-01-15 23:18:01 +0000 | [diff] [blame] | 2999 | new llvm::GlobalVariable(M, Init->getType(), |
| 3000 | /*Constant=*/true, Linkage, Init, Name); |
| 3001 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3002 | // If there's already an old global variable, replace it with the new one. |
| 3003 | if (OldGV) { |
| 3004 | GV->takeName(OldGV); |
| 3005 | llvm::Constant *NewPtr = |
| 3006 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 3007 | OldGV->replaceAllUsesWith(NewPtr); |
| 3008 | OldGV->eraseFromParent(); |
| 3009 | } |
| 3010 | |
Yaron Keren | 04da238 | 2015-07-29 15:42:28 +0000 | [diff] [blame] | 3011 | if (CGM.supportsCOMDAT() && GV->isWeakForLinker()) |
| 3012 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
| 3013 | |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3014 | // The Itanium ABI specifies that type_info objects must be globally |
| 3015 | // unique, with one exception: if the type is an incomplete class |
| 3016 | // type or a (possibly indirect) pointer to one. That exception |
| 3017 | // affects the general case of comparing type_info objects produced |
| 3018 | // by the typeid operator, which is why the comparison operators on |
| 3019 | // std::type_info generally use the type_info name pointers instead |
| 3020 | // of the object addresses. However, the language's built-in uses |
| 3021 | // of RTTI generally require class types to be complete, even when |
| 3022 | // manipulating pointers to those class types. This allows the |
| 3023 | // implementation of dynamic_cast to rely on address equality tests, |
| 3024 | // which is much faster. |
| 3025 | |
| 3026 | // All of this is to say that it's important that both the type_info |
| 3027 | // object and the type_info name be uniqued when weakly emitted. |
| 3028 | |
| 3029 | // Give the type_info object and name the formal visibility of the |
| 3030 | // type itself. |
| 3031 | llvm::GlobalValue::VisibilityTypes llvmVisibility; |
| 3032 | if (llvm::GlobalValue::isLocalLinkage(Linkage)) |
| 3033 | // If the linkage is local, only default visibility makes sense. |
| 3034 | llvmVisibility = llvm::GlobalValue::DefaultVisibility; |
| 3035 | else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden) |
| 3036 | llvmVisibility = llvm::GlobalValue::HiddenVisibility; |
| 3037 | else |
| 3038 | llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility()); |
| 3039 | TypeName->setVisibility(llvmVisibility); |
| 3040 | GV->setVisibility(llvmVisibility); |
| 3041 | |
| 3042 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 3043 | } |
| 3044 | |
| 3045 | /// ComputeQualifierFlags - Compute the pointer type info flags from the |
| 3046 | /// given qualifier. |
| 3047 | static unsigned ComputeQualifierFlags(Qualifiers Quals) { |
| 3048 | unsigned Flags = 0; |
| 3049 | |
| 3050 | if (Quals.hasConst()) |
| 3051 | Flags |= ItaniumRTTIBuilder::PTI_Const; |
| 3052 | if (Quals.hasVolatile()) |
| 3053 | Flags |= ItaniumRTTIBuilder::PTI_Volatile; |
| 3054 | if (Quals.hasRestrict()) |
| 3055 | Flags |= ItaniumRTTIBuilder::PTI_Restrict; |
| 3056 | |
| 3057 | return Flags; |
| 3058 | } |
| 3059 | |
| 3060 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info |
| 3061 | /// for the given Objective-C object type. |
| 3062 | void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { |
| 3063 | // Drop qualifiers. |
| 3064 | const Type *T = OT->getBaseType().getTypePtr(); |
| 3065 | assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); |
| 3066 | |
| 3067 | // The builtin types are abi::__class_type_infos and don't require |
| 3068 | // extra fields. |
| 3069 | if (isa<BuiltinType>(T)) return; |
| 3070 | |
| 3071 | ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); |
| 3072 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 3073 | |
| 3074 | // Root classes are also __class_type_info. |
| 3075 | if (!Super) return; |
| 3076 | |
| 3077 | QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); |
| 3078 | |
| 3079 | // Everything else is single inheritance. |
| 3080 | llvm::Constant *BaseTypeInfo = |
| 3081 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy); |
| 3082 | Fields.push_back(BaseTypeInfo); |
| 3083 | } |
| 3084 | |
| 3085 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
| 3086 | /// inheritance, according to the Itanium C++ ABI, 2.95p6b. |
| 3087 | void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { |
| 3088 | // Itanium C++ ABI 2.9.5p6b: |
| 3089 | // It adds to abi::__class_type_info a single member pointing to the |
| 3090 | // type_info structure for the base type, |
| 3091 | llvm::Constant *BaseTypeInfo = |
| 3092 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType()); |
| 3093 | Fields.push_back(BaseTypeInfo); |
| 3094 | } |
| 3095 | |
| 3096 | namespace { |
| 3097 | /// SeenBases - Contains virtual and non-virtual bases seen when traversing |
| 3098 | /// a class hierarchy. |
| 3099 | struct SeenBases { |
| 3100 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; |
| 3101 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; |
| 3102 | }; |
| 3103 | } |
| 3104 | |
| 3105 | /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in |
| 3106 | /// abi::__vmi_class_type_info. |
| 3107 | /// |
| 3108 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, |
| 3109 | SeenBases &Bases) { |
| 3110 | |
| 3111 | unsigned Flags = 0; |
| 3112 | |
| 3113 | const CXXRecordDecl *BaseDecl = |
| 3114 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 3115 | |
| 3116 | if (Base->isVirtual()) { |
| 3117 | // Mark the virtual base as seen. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3118 | if (!Bases.VirtualBases.insert(BaseDecl).second) { |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3119 | // If this virtual base has been seen before, then the class is diamond |
| 3120 | // shaped. |
| 3121 | Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped; |
| 3122 | } else { |
| 3123 | if (Bases.NonVirtualBases.count(BaseDecl)) |
| 3124 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 3125 | } |
| 3126 | } else { |
| 3127 | // Mark the non-virtual base as seen. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 3128 | if (!Bases.NonVirtualBases.insert(BaseDecl).second) { |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3129 | // If this non-virtual base has been seen before, then the class has non- |
| 3130 | // diamond shaped repeated inheritance. |
| 3131 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 3132 | } else { |
| 3133 | if (Bases.VirtualBases.count(BaseDecl)) |
| 3134 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | // Walk all bases. |
| 3139 | for (const auto &I : BaseDecl->bases()) |
| 3140 | Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); |
| 3141 | |
| 3142 | return Flags; |
| 3143 | } |
| 3144 | |
| 3145 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { |
| 3146 | unsigned Flags = 0; |
| 3147 | SeenBases Bases; |
| 3148 | |
| 3149 | // Walk all bases. |
| 3150 | for (const auto &I : RD->bases()) |
| 3151 | Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); |
| 3152 | |
| 3153 | return Flags; |
| 3154 | } |
| 3155 | |
| 3156 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 3157 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 3158 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 3159 | void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { |
| 3160 | llvm::Type *UnsignedIntLTy = |
| 3161 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 3162 | |
| 3163 | // Itanium C++ ABI 2.9.5p6c: |
| 3164 | // __flags is a word with flags describing details about the class |
| 3165 | // structure, which may be referenced by using the __flags_masks |
| 3166 | // enumeration. These flags refer to both direct and indirect bases. |
| 3167 | unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); |
| 3168 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 3169 | |
| 3170 | // Itanium C++ ABI 2.9.5p6c: |
| 3171 | // __base_count is a word with the number of direct proper base class |
| 3172 | // descriptions that follow. |
| 3173 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); |
| 3174 | |
| 3175 | if (!RD->getNumBases()) |
| 3176 | return; |
| 3177 | |
| 3178 | llvm::Type *LongLTy = |
| 3179 | CGM.getTypes().ConvertType(CGM.getContext().LongTy); |
| 3180 | |
| 3181 | // Now add the base class descriptions. |
| 3182 | |
| 3183 | // Itanium C++ ABI 2.9.5p6c: |
| 3184 | // __base_info[] is an array of base class descriptions -- one for every |
| 3185 | // direct proper base. Each description is of the type: |
| 3186 | // |
| 3187 | // struct abi::__base_class_type_info { |
| 3188 | // public: |
| 3189 | // const __class_type_info *__base_type; |
| 3190 | // long __offset_flags; |
| 3191 | // |
| 3192 | // enum __offset_flags_masks { |
| 3193 | // __virtual_mask = 0x1, |
| 3194 | // __public_mask = 0x2, |
| 3195 | // __offset_shift = 8 |
| 3196 | // }; |
| 3197 | // }; |
| 3198 | for (const auto &Base : RD->bases()) { |
| 3199 | // The __base_type member points to the RTTI for the base type. |
| 3200 | Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType())); |
| 3201 | |
| 3202 | const CXXRecordDecl *BaseDecl = |
| 3203 | cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 3204 | |
| 3205 | int64_t OffsetFlags = 0; |
| 3206 | |
| 3207 | // All but the lower 8 bits of __offset_flags are a signed offset. |
| 3208 | // For a non-virtual base, this is the offset in the object of the base |
| 3209 | // subobject. For a virtual base, this is the offset in the virtual table of |
| 3210 | // the virtual base offset for the virtual base referenced (negative). |
| 3211 | CharUnits Offset; |
| 3212 | if (Base.isVirtual()) |
| 3213 | Offset = |
| 3214 | CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl); |
| 3215 | else { |
| 3216 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 3217 | Offset = Layout.getBaseClassOffset(BaseDecl); |
| 3218 | }; |
| 3219 | |
| 3220 | OffsetFlags = uint64_t(Offset.getQuantity()) << 8; |
| 3221 | |
| 3222 | // The low-order byte of __offset_flags contains flags, as given by the |
| 3223 | // masks from the enumeration __offset_flags_masks. |
| 3224 | if (Base.isVirtual()) |
| 3225 | OffsetFlags |= BCTI_Virtual; |
| 3226 | if (Base.getAccessSpecifier() == AS_public) |
| 3227 | OffsetFlags |= BCTI_Public; |
| 3228 | |
| 3229 | Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags)); |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, |
| 3234 | /// used for pointer types. |
| 3235 | void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { |
| 3236 | Qualifiers Quals; |
| 3237 | QualType UnqualifiedPointeeTy = |
| 3238 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 3239 | |
| 3240 | // Itanium C++ ABI 2.9.5p7: |
| 3241 | // __flags is a flag word describing the cv-qualification and other |
| 3242 | // attributes of the type pointed to |
| 3243 | unsigned Flags = ComputeQualifierFlags(Quals); |
| 3244 | |
| 3245 | // Itanium C++ ABI 2.9.5p7: |
| 3246 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 3247 | // incomplete class type, the incomplete target type flag is set. |
| 3248 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
| 3249 | Flags |= PTI_Incomplete; |
| 3250 | |
| 3251 | llvm::Type *UnsignedIntLTy = |
| 3252 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 3253 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 3254 | |
| 3255 | // Itanium C++ ABI 2.9.5p7: |
| 3256 | // __pointee is a pointer to the std::type_info derivation for the |
| 3257 | // unqualified type being pointed to. |
| 3258 | llvm::Constant *PointeeTypeInfo = |
| 3259 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); |
| 3260 | Fields.push_back(PointeeTypeInfo); |
| 3261 | } |
| 3262 | |
| 3263 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 3264 | /// struct, used for member pointer types. |
| 3265 | void |
| 3266 | ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { |
| 3267 | QualType PointeeTy = Ty->getPointeeType(); |
| 3268 | |
| 3269 | Qualifiers Quals; |
| 3270 | QualType UnqualifiedPointeeTy = |
| 3271 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 3272 | |
| 3273 | // Itanium C++ ABI 2.9.5p7: |
| 3274 | // __flags is a flag word describing the cv-qualification and other |
| 3275 | // attributes of the type pointed to. |
| 3276 | unsigned Flags = ComputeQualifierFlags(Quals); |
| 3277 | |
| 3278 | const RecordType *ClassType = cast<RecordType>(Ty->getClass()); |
| 3279 | |
| 3280 | // Itanium C++ ABI 2.9.5p7: |
| 3281 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 3282 | // incomplete class type, the incomplete target type flag is set. |
| 3283 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
| 3284 | Flags |= PTI_Incomplete; |
| 3285 | |
| 3286 | if (IsIncompleteClassType(ClassType)) |
| 3287 | Flags |= PTI_ContainingClassIncomplete; |
| 3288 | |
| 3289 | llvm::Type *UnsignedIntLTy = |
| 3290 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 3291 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 3292 | |
| 3293 | // Itanium C++ ABI 2.9.5p7: |
| 3294 | // __pointee is a pointer to the std::type_info derivation for the |
| 3295 | // unqualified type being pointed to. |
| 3296 | llvm::Constant *PointeeTypeInfo = |
| 3297 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); |
| 3298 | Fields.push_back(PointeeTypeInfo); |
| 3299 | |
| 3300 | // Itanium C++ ABI 2.9.5p9: |
| 3301 | // __context is a pointer to an abi::__class_type_info corresponding to the |
| 3302 | // class type containing the member pointed to |
| 3303 | // (e.g., the "A" in "int A::*"). |
| 3304 | Fields.push_back( |
| 3305 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0))); |
| 3306 | } |
| 3307 | |
David Majnemer | 443250f | 2015-03-17 20:35:00 +0000 | [diff] [blame] | 3308 | llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) { |
David Majnemer | e2cb8d1 | 2014-07-07 06:20:47 +0000 | [diff] [blame] | 3309 | return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty); |
| 3310 | } |
| 3311 | |
| 3312 | void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) { |
| 3313 | QualType PointerType = getContext().getPointerType(Type); |
| 3314 | QualType PointerTypeConst = getContext().getPointerType(Type.withConst()); |
| 3315 | ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true); |
| 3316 | ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true); |
| 3317 | ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true); |
| 3318 | } |
| 3319 | |
| 3320 | void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() { |
| 3321 | QualType FundamentalTypes[] = { |
| 3322 | getContext().VoidTy, getContext().NullPtrTy, |
| 3323 | getContext().BoolTy, getContext().WCharTy, |
| 3324 | getContext().CharTy, getContext().UnsignedCharTy, |
| 3325 | getContext().SignedCharTy, getContext().ShortTy, |
| 3326 | getContext().UnsignedShortTy, getContext().IntTy, |
| 3327 | getContext().UnsignedIntTy, getContext().LongTy, |
| 3328 | getContext().UnsignedLongTy, getContext().LongLongTy, |
| 3329 | getContext().UnsignedLongLongTy, getContext().HalfTy, |
| 3330 | getContext().FloatTy, getContext().DoubleTy, |
| 3331 | getContext().LongDoubleTy, getContext().Char16Ty, |
| 3332 | getContext().Char32Ty, |
| 3333 | }; |
| 3334 | for (const QualType &FundamentalType : FundamentalTypes) |
| 3335 | EmitFundamentalRTTIDescriptor(FundamentalType); |
| 3336 | } |
| 3337 | |
| 3338 | /// What sort of uniqueness rules should we use for the RTTI for the |
| 3339 | /// given type? |
| 3340 | ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness( |
| 3341 | QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const { |
| 3342 | if (shouldRTTIBeUnique()) |
| 3343 | return RUK_Unique; |
| 3344 | |
| 3345 | // It's only necessary for linkonce_odr or weak_odr linkage. |
| 3346 | if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage && |
| 3347 | Linkage != llvm::GlobalValue::WeakODRLinkage) |
| 3348 | return RUK_Unique; |
| 3349 | |
| 3350 | // It's only necessary with default visibility. |
| 3351 | if (CanTy->getVisibility() != DefaultVisibility) |
| 3352 | return RUK_Unique; |
| 3353 | |
| 3354 | // If we're not required to publish this symbol, hide it. |
| 3355 | if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage) |
| 3356 | return RUK_NonUniqueHidden; |
| 3357 | |
| 3358 | // If we're required to publish this symbol, as we might be under an |
| 3359 | // explicit instantiation, leave it with default visibility but |
| 3360 | // enable string-comparisons. |
| 3361 | assert(Linkage == llvm::GlobalValue::WeakODRLinkage); |
| 3362 | return RUK_NonUniqueVisible; |
| 3363 | } |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3364 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3365 | // Find out how to codegen the complete destructor and constructor |
| 3366 | namespace { |
| 3367 | enum class StructorCodegen { Emit, RAUW, Alias, COMDAT }; |
| 3368 | } |
| 3369 | static StructorCodegen getCodegenToUse(CodeGenModule &CGM, |
| 3370 | const CXXMethodDecl *MD) { |
| 3371 | if (!CGM.getCodeGenOpts().CXXCtorDtorAliases) |
| 3372 | return StructorCodegen::Emit; |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3373 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3374 | // The complete and base structors are not equivalent if there are any virtual |
| 3375 | // bases, so emit separate functions. |
| 3376 | if (MD->getParent()->getNumVBases()) |
| 3377 | return StructorCodegen::Emit; |
| 3378 | |
| 3379 | GlobalDecl AliasDecl; |
| 3380 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 3381 | AliasDecl = GlobalDecl(DD, Dtor_Complete); |
| 3382 | } else { |
| 3383 | const auto *CD = cast<CXXConstructorDecl>(MD); |
| 3384 | AliasDecl = GlobalDecl(CD, Ctor_Complete); |
| 3385 | } |
| 3386 | llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); |
| 3387 | |
| 3388 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) |
| 3389 | return StructorCodegen::RAUW; |
| 3390 | |
| 3391 | // FIXME: Should we allow available_externally aliases? |
| 3392 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
| 3393 | return StructorCodegen::RAUW; |
| 3394 | |
Rafael Espindola | 0806f98 | 2014-09-16 20:19:43 +0000 | [diff] [blame] | 3395 | if (llvm::GlobalValue::isWeakForLinker(Linkage)) { |
| 3396 | // Only ELF supports COMDATs with arbitrary names (C5/D5). |
| 3397 | if (CGM.getTarget().getTriple().isOSBinFormatELF()) |
| 3398 | return StructorCodegen::COMDAT; |
| 3399 | return StructorCodegen::Emit; |
| 3400 | } |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3401 | |
| 3402 | return StructorCodegen::Alias; |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3405 | static void emitConstructorDestructorAlias(CodeGenModule &CGM, |
| 3406 | GlobalDecl AliasDecl, |
| 3407 | GlobalDecl TargetDecl) { |
| 3408 | llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); |
| 3409 | |
| 3410 | StringRef MangledName = CGM.getMangledName(AliasDecl); |
| 3411 | llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName); |
| 3412 | if (Entry && !Entry->isDeclaration()) |
| 3413 | return; |
| 3414 | |
| 3415 | auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl)); |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3416 | |
| 3417 | // Create the alias with no name. |
David Blaikie | 2a791d7 | 2015-09-14 18:38:22 +0000 | [diff] [blame] | 3418 | auto *Alias = llvm::GlobalAlias::create(Linkage, "", Aliasee); |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3419 | |
| 3420 | // Switch any previous uses to the alias. |
| 3421 | if (Entry) { |
NAKAMURA Takumi | e962104 | 2015-09-15 01:39:27 +0000 | [diff] [blame] | 3422 | assert(Entry->getType() == Aliasee->getType() && |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3423 | "declaration exists with different type"); |
| 3424 | Alias->takeName(Entry); |
| 3425 | Entry->replaceAllUsesWith(Alias); |
| 3426 | Entry->eraseFromParent(); |
| 3427 | } else { |
| 3428 | Alias->setName(MangledName); |
| 3429 | } |
| 3430 | |
| 3431 | // Finally, set up the alias with its proper name and attributes. |
Dario Domizioli | c4fb8ca7 | 2014-09-19 22:06:24 +0000 | [diff] [blame] | 3432 | CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD, |
| 3436 | StructorType Type) { |
| 3437 | auto *CD = dyn_cast<CXXConstructorDecl>(MD); |
| 3438 | const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD); |
| 3439 | |
| 3440 | StructorCodegen CGType = getCodegenToUse(CGM, MD); |
| 3441 | |
| 3442 | if (Type == StructorType::Complete) { |
| 3443 | GlobalDecl CompleteDecl; |
| 3444 | GlobalDecl BaseDecl; |
| 3445 | if (CD) { |
| 3446 | CompleteDecl = GlobalDecl(CD, Ctor_Complete); |
| 3447 | BaseDecl = GlobalDecl(CD, Ctor_Base); |
| 3448 | } else { |
| 3449 | CompleteDecl = GlobalDecl(DD, Dtor_Complete); |
| 3450 | BaseDecl = GlobalDecl(DD, Dtor_Base); |
| 3451 | } |
| 3452 | |
| 3453 | if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) { |
| 3454 | emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl); |
| 3455 | return; |
| 3456 | } |
| 3457 | |
| 3458 | if (CGType == StructorCodegen::RAUW) { |
| 3459 | StringRef MangledName = CGM.getMangledName(CompleteDecl); |
Andrey Bokhanko | cab5858 | 2015-08-31 13:20:44 +0000 | [diff] [blame] | 3460 | auto *Aliasee = CGM.GetAddrOfGlobal(BaseDecl); |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3461 | CGM.addReplacement(MangledName, Aliasee); |
| 3462 | return; |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | |
| 3466 | // The base destructor is equivalent to the base destructor of its |
| 3467 | // base class if there is exactly one non-virtual base class with a |
| 3468 | // non-trivial destructor, there are no fields with a non-trivial |
| 3469 | // destructor, and the body of the destructor is trivial. |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3470 | if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT && |
| 3471 | !CGM.TryEmitBaseDestructorAsAlias(DD)) |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3472 | return; |
| 3473 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3474 | llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type); |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3475 | |
Rafael Espindola | 1e4df92 | 2014-09-16 15:18:21 +0000 | [diff] [blame] | 3476 | if (CGType == StructorCodegen::COMDAT) { |
| 3477 | SmallString<256> Buffer; |
| 3478 | llvm::raw_svector_ostream Out(Buffer); |
| 3479 | if (DD) |
| 3480 | getMangleContext().mangleCXXDtorComdat(DD, Out); |
| 3481 | else |
| 3482 | getMangleContext().mangleCXXCtorComdat(CD, Out); |
| 3483 | llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str()); |
| 3484 | Fn->setComdat(C); |
Rafael Espindola | dbee8a7 | 2015-01-15 21:36:08 +0000 | [diff] [blame] | 3485 | } else { |
| 3486 | CGM.maybeSetTrivialComdat(*MD, *Fn); |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3487 | } |
Rafael Espindola | 91f68b4 | 2014-09-15 19:20:10 +0000 | [diff] [blame] | 3488 | } |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3489 | |
| 3490 | static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) { |
| 3491 | // void *__cxa_begin_catch(void*); |
| 3492 | llvm::FunctionType *FTy = llvm::FunctionType::get( |
| 3493 | CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
| 3494 | |
| 3495 | return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch"); |
| 3496 | } |
| 3497 | |
| 3498 | static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) { |
| 3499 | // void __cxa_end_catch(); |
| 3500 | llvm::FunctionType *FTy = |
| 3501 | llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); |
| 3502 | |
| 3503 | return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch"); |
| 3504 | } |
| 3505 | |
| 3506 | static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) { |
| 3507 | // void *__cxa_get_exception_ptr(void*); |
| 3508 | llvm::FunctionType *FTy = llvm::FunctionType::get( |
| 3509 | CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
| 3510 | |
| 3511 | return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr"); |
| 3512 | } |
| 3513 | |
| 3514 | namespace { |
| 3515 | /// A cleanup to call __cxa_end_catch. In many cases, the caught |
| 3516 | /// exception type lets us state definitively that the thrown exception |
| 3517 | /// type does not have a destructor. In particular: |
| 3518 | /// - Catch-alls tell us nothing, so we have to conservatively |
| 3519 | /// assume that the thrown exception might have a destructor. |
| 3520 | /// - Catches by reference behave according to their base types. |
| 3521 | /// - Catches of non-record types will only trigger for exceptions |
| 3522 | /// of non-record types, which never have destructors. |
| 3523 | /// - Catches of record types can trigger for arbitrary subclasses |
| 3524 | /// of the caught type, so we have to assume the actual thrown |
| 3525 | /// exception type might have a throwing destructor, even if the |
| 3526 | /// caught type's destructor is trivial or nothrow. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 3527 | struct CallEndCatch final : EHScopeStack::Cleanup { |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3528 | CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {} |
| 3529 | bool MightThrow; |
| 3530 | |
| 3531 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 3532 | if (!MightThrow) { |
| 3533 | CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM)); |
| 3534 | return; |
| 3535 | } |
| 3536 | |
| 3537 | CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM)); |
| 3538 | } |
| 3539 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3540 | } |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3541 | |
| 3542 | /// Emits a call to __cxa_begin_catch and enters a cleanup to call |
| 3543 | /// __cxa_end_catch. |
| 3544 | /// |
| 3545 | /// \param EndMightThrow - true if __cxa_end_catch might throw |
| 3546 | static llvm::Value *CallBeginCatch(CodeGenFunction &CGF, |
| 3547 | llvm::Value *Exn, |
| 3548 | bool EndMightThrow) { |
| 3549 | llvm::CallInst *call = |
| 3550 | CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn); |
| 3551 | |
| 3552 | CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow); |
| 3553 | |
| 3554 | return call; |
| 3555 | } |
| 3556 | |
| 3557 | /// A "special initializer" callback for initializing a catch |
| 3558 | /// parameter during catch initialization. |
| 3559 | static void InitCatchParam(CodeGenFunction &CGF, |
| 3560 | const VarDecl &CatchParam, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3561 | Address ParamAddr, |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3562 | SourceLocation Loc) { |
| 3563 | // Load the exception from where the landing pad saved it. |
| 3564 | llvm::Value *Exn = CGF.getExceptionFromSlot(); |
| 3565 | |
| 3566 | CanQualType CatchType = |
| 3567 | CGF.CGM.getContext().getCanonicalType(CatchParam.getType()); |
| 3568 | llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType); |
| 3569 | |
| 3570 | // If we're catching by reference, we can just cast the object |
| 3571 | // pointer to the appropriate pointer. |
| 3572 | if (isa<ReferenceType>(CatchType)) { |
| 3573 | QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType(); |
| 3574 | bool EndCatchMightThrow = CaughtType->isRecordType(); |
| 3575 | |
| 3576 | // __cxa_begin_catch returns the adjusted object pointer. |
| 3577 | llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow); |
| 3578 | |
| 3579 | // We have no way to tell the personality function that we're |
| 3580 | // catching by reference, so if we're catching a pointer, |
| 3581 | // __cxa_begin_catch will actually return that pointer by value. |
| 3582 | if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) { |
| 3583 | QualType PointeeType = PT->getPointeeType(); |
| 3584 | |
| 3585 | // When catching by reference, generally we should just ignore |
| 3586 | // this by-value pointer and use the exception object instead. |
| 3587 | if (!PointeeType->isRecordType()) { |
| 3588 | |
| 3589 | // Exn points to the struct _Unwind_Exception header, which |
| 3590 | // we have to skip past in order to reach the exception data. |
| 3591 | unsigned HeaderSize = |
| 3592 | CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException(); |
| 3593 | AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize); |
| 3594 | |
| 3595 | // However, if we're catching a pointer-to-record type that won't |
| 3596 | // work, because the personality function might have adjusted |
| 3597 | // the pointer. There's actually no way for us to fully satisfy |
| 3598 | // the language/ABI contract here: we can't use Exn because it |
| 3599 | // might have the wrong adjustment, but we can't use the by-value |
| 3600 | // pointer because it's off by a level of abstraction. |
| 3601 | // |
| 3602 | // The current solution is to dump the adjusted pointer into an |
| 3603 | // alloca, which breaks language semantics (because changing the |
| 3604 | // pointer doesn't change the exception) but at least works. |
| 3605 | // The better solution would be to filter out non-exact matches |
| 3606 | // and rethrow them, but this is tricky because the rethrow |
| 3607 | // really needs to be catchable by other sites at this landing |
| 3608 | // pad. The best solution is to fix the personality function. |
| 3609 | } else { |
| 3610 | // Pull the pointer for the reference type off. |
| 3611 | llvm::Type *PtrTy = |
| 3612 | cast<llvm::PointerType>(LLVMCatchTy)->getElementType(); |
| 3613 | |
| 3614 | // Create the temporary and write the adjusted pointer into it. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3615 | Address ExnPtrTmp = |
| 3616 | CGF.CreateTempAlloca(PtrTy, CGF.getPointerAlign(), "exn.byref.tmp"); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3617 | llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); |
| 3618 | CGF.Builder.CreateStore(Casted, ExnPtrTmp); |
| 3619 | |
| 3620 | // Bind the reference to the temporary. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3621 | AdjustedExn = ExnPtrTmp.getPointer(); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | llvm::Value *ExnCast = |
| 3626 | CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref"); |
| 3627 | CGF.Builder.CreateStore(ExnCast, ParamAddr); |
| 3628 | return; |
| 3629 | } |
| 3630 | |
| 3631 | // Scalars and complexes. |
| 3632 | TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType); |
| 3633 | if (TEK != TEK_Aggregate) { |
| 3634 | llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false); |
| 3635 | |
| 3636 | // If the catch type is a pointer type, __cxa_begin_catch returns |
| 3637 | // the pointer by value. |
| 3638 | if (CatchType->hasPointerRepresentation()) { |
| 3639 | llvm::Value *CastExn = |
| 3640 | CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted"); |
| 3641 | |
| 3642 | switch (CatchType.getQualifiers().getObjCLifetime()) { |
| 3643 | case Qualifiers::OCL_Strong: |
| 3644 | CastExn = CGF.EmitARCRetainNonBlock(CastExn); |
| 3645 | // fallthrough |
| 3646 | |
| 3647 | case Qualifiers::OCL_None: |
| 3648 | case Qualifiers::OCL_ExplicitNone: |
| 3649 | case Qualifiers::OCL_Autoreleasing: |
| 3650 | CGF.Builder.CreateStore(CastExn, ParamAddr); |
| 3651 | return; |
| 3652 | |
| 3653 | case Qualifiers::OCL_Weak: |
| 3654 | CGF.EmitARCInitWeak(ParamAddr, CastExn); |
| 3655 | return; |
| 3656 | } |
| 3657 | llvm_unreachable("bad ownership qualifier!"); |
| 3658 | } |
| 3659 | |
| 3660 | // Otherwise, it returns a pointer into the exception object. |
| 3661 | |
| 3662 | llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok |
| 3663 | llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy); |
| 3664 | |
| 3665 | LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3666 | LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3667 | switch (TEK) { |
| 3668 | case TEK_Complex: |
| 3669 | CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV, |
| 3670 | /*init*/ true); |
| 3671 | return; |
| 3672 | case TEK_Scalar: { |
| 3673 | llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc); |
| 3674 | CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true); |
| 3675 | return; |
| 3676 | } |
| 3677 | case TEK_Aggregate: |
| 3678 | llvm_unreachable("evaluation kind filtered out!"); |
| 3679 | } |
| 3680 | llvm_unreachable("bad evaluation kind"); |
| 3681 | } |
| 3682 | |
| 3683 | assert(isa<RecordType>(CatchType) && "unexpected catch type!"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3684 | auto catchRD = CatchType->getAsCXXRecordDecl(); |
| 3685 | CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3686 | |
| 3687 | llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok |
| 3688 | |
| 3689 | // Check for a copy expression. If we don't have a copy expression, |
| 3690 | // that means a trivial copy is okay. |
| 3691 | const Expr *copyExpr = CatchParam.getInit(); |
| 3692 | if (!copyExpr) { |
| 3693 | llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3694 | Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy), |
| 3695 | caughtExnAlignment); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3696 | CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType); |
| 3697 | return; |
| 3698 | } |
| 3699 | |
| 3700 | // We have to call __cxa_get_exception_ptr to get the adjusted |
| 3701 | // pointer before copying. |
| 3702 | llvm::CallInst *rawAdjustedExn = |
| 3703 | CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn); |
| 3704 | |
| 3705 | // Cast that to the appropriate type. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3706 | Address adjustedExn(CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy), |
| 3707 | caughtExnAlignment); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3708 | |
| 3709 | // The copy expression is defined in terms of an OpaqueValueExpr. |
| 3710 | // Find it and map it to the adjusted expression. |
| 3711 | CodeGenFunction::OpaqueValueMapping |
| 3712 | opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr), |
| 3713 | CGF.MakeAddrLValue(adjustedExn, CatchParam.getType())); |
| 3714 | |
| 3715 | // Call the copy ctor in a terminate scope. |
| 3716 | CGF.EHStack.pushTerminate(); |
| 3717 | |
| 3718 | // Perform the copy construction. |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3719 | CGF.EmitAggExpr(copyExpr, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3720 | AggValueSlot::forAddr(ParamAddr, Qualifiers(), |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3721 | AggValueSlot::IsNotDestructed, |
| 3722 | AggValueSlot::DoesNotNeedGCBarriers, |
| 3723 | AggValueSlot::IsNotAliased)); |
| 3724 | |
| 3725 | // Leave the terminate scope. |
| 3726 | CGF.EHStack.popTerminate(); |
| 3727 | |
| 3728 | // Undo the opaque value mapping. |
| 3729 | opaque.pop(); |
| 3730 | |
| 3731 | // Finally we can call __cxa_begin_catch. |
| 3732 | CallBeginCatch(CGF, Exn, true); |
| 3733 | } |
| 3734 | |
| 3735 | /// Begins a catch statement by initializing the catch variable and |
| 3736 | /// calling __cxa_begin_catch. |
| 3737 | void ItaniumCXXABI::emitBeginCatch(CodeGenFunction &CGF, |
| 3738 | const CXXCatchStmt *S) { |
| 3739 | // We have to be very careful with the ordering of cleanups here: |
| 3740 | // C++ [except.throw]p4: |
| 3741 | // The destruction [of the exception temporary] occurs |
| 3742 | // immediately after the destruction of the object declared in |
| 3743 | // the exception-declaration in the handler. |
| 3744 | // |
| 3745 | // So the precise ordering is: |
| 3746 | // 1. Construct catch variable. |
| 3747 | // 2. __cxa_begin_catch |
| 3748 | // 3. Enter __cxa_end_catch cleanup |
| 3749 | // 4. Enter dtor cleanup |
| 3750 | // |
| 3751 | // We do this by using a slightly abnormal initialization process. |
| 3752 | // Delegation sequence: |
| 3753 | // - ExitCXXTryStmt opens a RunCleanupsScope |
| 3754 | // - EmitAutoVarAlloca creates the variable and debug info |
| 3755 | // - InitCatchParam initializes the variable from the exception |
| 3756 | // - CallBeginCatch calls __cxa_begin_catch |
| 3757 | // - CallBeginCatch enters the __cxa_end_catch cleanup |
| 3758 | // - EmitAutoVarCleanups enters the variable destructor cleanup |
| 3759 | // - EmitCXXTryStmt emits the code for the catch body |
| 3760 | // - EmitCXXTryStmt close the RunCleanupsScope |
| 3761 | |
| 3762 | VarDecl *CatchParam = S->getExceptionDecl(); |
| 3763 | if (!CatchParam) { |
| 3764 | llvm::Value *Exn = CGF.getExceptionFromSlot(); |
| 3765 | CallBeginCatch(CGF, Exn, true); |
| 3766 | return; |
| 3767 | } |
| 3768 | |
| 3769 | // Emit the local. |
| 3770 | CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam); |
| 3771 | InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart()); |
| 3772 | CGF.EmitAutoVarCleanups(var); |
| 3773 | } |
| 3774 | |
| 3775 | /// Get or define the following function: |
| 3776 | /// void @__clang_call_terminate(i8* %exn) nounwind noreturn |
| 3777 | /// This code is used only in C++. |
| 3778 | static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) { |
| 3779 | llvm::FunctionType *fnTy = |
| 3780 | llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false); |
| 3781 | llvm::Constant *fnRef = |
| 3782 | CGM.CreateRuntimeFunction(fnTy, "__clang_call_terminate"); |
| 3783 | |
| 3784 | llvm::Function *fn = dyn_cast<llvm::Function>(fnRef); |
| 3785 | if (fn && fn->empty()) { |
| 3786 | fn->setDoesNotThrow(); |
| 3787 | fn->setDoesNotReturn(); |
| 3788 | |
| 3789 | // What we really want is to massively penalize inlining without |
| 3790 | // forbidding it completely. The difference between that and |
| 3791 | // 'noinline' is negligible. |
| 3792 | fn->addFnAttr(llvm::Attribute::NoInline); |
| 3793 | |
| 3794 | // Allow this function to be shared across translation units, but |
| 3795 | // we don't want it to turn into an exported symbol. |
| 3796 | fn->setLinkage(llvm::Function::LinkOnceODRLinkage); |
| 3797 | fn->setVisibility(llvm::Function::HiddenVisibility); |
NAKAMURA Takumi | c7da6da | 2015-05-09 21:10:07 +0000 | [diff] [blame] | 3798 | if (CGM.supportsCOMDAT()) |
| 3799 | fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName())); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3800 | |
| 3801 | // Set up the function. |
| 3802 | llvm::BasicBlock *entry = |
| 3803 | llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | CGBuilderTy builder(CGM, entry); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3805 | |
| 3806 | // Pull the exception pointer out of the parameter list. |
| 3807 | llvm::Value *exn = &*fn->arg_begin(); |
| 3808 | |
| 3809 | // Call __cxa_begin_catch(exn). |
| 3810 | llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn); |
| 3811 | catchCall->setDoesNotThrow(); |
| 3812 | catchCall->setCallingConv(CGM.getRuntimeCC()); |
| 3813 | |
| 3814 | // Call std::terminate(). |
David Blaikie | 4ba525b | 2015-07-14 17:27:39 +0000 | [diff] [blame] | 3815 | llvm::CallInst *termCall = builder.CreateCall(CGM.getTerminateFn()); |
Reid Kleckner | fff8e7f | 2015-03-03 19:21:04 +0000 | [diff] [blame] | 3816 | termCall->setDoesNotThrow(); |
| 3817 | termCall->setDoesNotReturn(); |
| 3818 | termCall->setCallingConv(CGM.getRuntimeCC()); |
| 3819 | |
| 3820 | // std::terminate cannot return. |
| 3821 | builder.CreateUnreachable(); |
| 3822 | } |
| 3823 | |
| 3824 | return fnRef; |
| 3825 | } |
| 3826 | |
| 3827 | llvm::CallInst * |
| 3828 | ItaniumCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF, |
| 3829 | llvm::Value *Exn) { |
| 3830 | // In C++, we want to call __cxa_begin_catch() before terminating. |
| 3831 | if (Exn) { |
| 3832 | assert(CGF.CGM.getLangOpts().CPlusPlus); |
| 3833 | return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn); |
| 3834 | } |
| 3835 | return CGF.EmitNounwindRuntimeCall(CGF.CGM.getTerminateFn()); |
| 3836 | } |