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