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