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