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