Charles Davis | 3a811f1 | 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 | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Itanium C++ ABI. The class |
Charles Davis | 3a811f1 | 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 | ee79a4c | 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 | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "CGCXXABI.h" |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 22 | #include "CGRecordLayout.h" |
Charles Davis | 9ee494f | 2012-06-23 23:44:00 +0000 | [diff] [blame] | 23 | #include "CGVTables.h" |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 24 | #include "CodeGenFunction.h" |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 25 | #include "CodeGenModule.h" |
Craig Topper | ba77cb9 | 2012-09-15 18:47:51 +0000 | [diff] [blame] | 26 | #include "clang/AST/Mangle.h" |
| 27 | #include "clang/AST/Type.h" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 28 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/Intrinsics.h" |
| 31 | #include "llvm/IR/Value.h" |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace clang; |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 34 | using namespace CodeGen; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 37 | class ItaniumCXXABI : public CodeGen::CGCXXABI { |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 38 | /// VTables - All the vtables which have been defined. |
| 39 | llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> VTables; |
| 40 | |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 41 | protected: |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 42 | bool UseARMMethodPtrABI; |
| 43 | bool UseARMGuardVarABI; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 44 | |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 45 | ItaniumMangleContext &getMangleContext() { |
| 46 | return cast<ItaniumMangleContext>(CodeGen::CGCXXABI::getMangleContext()); |
| 47 | } |
| 48 | |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 49 | public: |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 50 | ItaniumCXXABI(CodeGen::CodeGenModule &CGM, |
| 51 | bool UseARMMethodPtrABI = false, |
| 52 | bool UseARMGuardVarABI = false) : |
| 53 | CGCXXABI(CGM), UseARMMethodPtrABI(UseARMMethodPtrABI), |
| 54 | UseARMGuardVarABI(UseARMGuardVarABI) { } |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 55 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 56 | bool classifyReturnType(CGFunctionInfo &FI) const override; |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 57 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 58 | RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override { |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 59 | // Structures with either a non-trivial destructor or a non-trivial |
| 60 | // copy constructor are always indirect. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 61 | // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared |
| 62 | // special members. |
| 63 | if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) |
Timur Iskhodzhanov | ed23bdf | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 64 | return RAA_Indirect; |
| 65 | return RAA_Default; |
| 66 | } |
| 67 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 68 | bool isZeroInitializable(const MemberPointerType *MPT) override; |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 69 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 70 | llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 71 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 72 | llvm::Value * |
| 73 | EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, |
| 74 | const Expr *E, |
| 75 | llvm::Value *&This, |
| 76 | llvm::Value *MemFnPtr, |
| 77 | const MemberPointerType *MPT) override; |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 78 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 79 | llvm::Value * |
| 80 | EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, |
| 81 | llvm::Value *Base, |
| 82 | llvm::Value *MemPtr, |
| 83 | const MemberPointerType *MPT) override; |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 84 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 85 | llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 86 | const CastExpr *E, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 87 | llvm::Value *Src) override; |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 88 | llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 89 | llvm::Constant *Src) override; |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 90 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 91 | llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 92 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 93 | llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 94 | llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 95 | CharUnits offset) override; |
| 96 | llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 97 | llvm::Constant *BuildMemberPointer(const CXXMethodDecl *MD, |
| 98 | CharUnits ThisAdjustment); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 99 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 100 | llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 101 | llvm::Value *L, llvm::Value *R, |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 102 | const MemberPointerType *MPT, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 103 | bool Inequality) override; |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 104 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 105 | llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 106 | llvm::Value *Addr, |
| 107 | const MemberPointerType *MPT) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 108 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 109 | void emitVirtualObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, |
| 110 | llvm::Value *Ptr, QualType ElementType, |
| 111 | const CXXDestructorDecl *Dtor) override; |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 112 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 113 | void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; |
| 114 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 115 | void EmitFundamentalRTTIDescriptor(QualType Type); |
| 116 | void EmitFundamentalRTTIDescriptors(); |
| 117 | llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; |
| 118 | |
| 119 | bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; |
| 120 | void EmitBadTypeidCall(CodeGenFunction &CGF) override; |
| 121 | llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, |
| 122 | llvm::Value *ThisPtr, |
| 123 | llvm::Type *StdTypeInfoPtrTy) override; |
| 124 | |
| 125 | bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 126 | QualType SrcRecordTy) override; |
| 127 | |
| 128 | llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, |
| 129 | QualType SrcRecordTy, QualType DestTy, |
| 130 | QualType DestRecordTy, |
| 131 | llvm::BasicBlock *CastEnd) override; |
| 132 | |
| 133 | llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, |
| 134 | QualType SrcRecordTy, |
| 135 | QualType DestTy) override; |
| 136 | |
| 137 | bool EmitBadCastCall(CodeGenFunction &CGF) override; |
| 138 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 139 | llvm::Value * |
| 140 | GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, |
| 141 | const CXXRecordDecl *ClassDecl, |
| 142 | const CXXRecordDecl *BaseClassDecl) override; |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 143 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 144 | void EmitCXXConstructors(const CXXConstructorDecl *D) override; |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 145 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 146 | void buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 147 | SmallVectorImpl<CanQualType> &ArgTys) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 148 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 149 | bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 150 | CXXDtorType DT) const override { |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 151 | // Itanium does not emit any destructor variant as an inline thunk. |
| 152 | // Delegating may occur as an optimization, but all variants are either |
| 153 | // emitted with external linkage or as linkonce if they are inline and used. |
| 154 | return false; |
| 155 | } |
| 156 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 157 | void EmitCXXDestructors(const CXXDestructorDecl *D) override; |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 158 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 159 | void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, |
| 160 | FunctionArgList &Params) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 161 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 162 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 163 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 164 | unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, |
| 165 | const CXXConstructorDecl *D, |
| 166 | CXXCtorType Type, bool ForVirtualBase, |
| 167 | bool Delegating, |
| 168 | CallArgList &Args) override; |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 169 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 170 | void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, |
| 171 | CXXDtorType Type, bool ForVirtualBase, |
| 172 | bool Delegating, llvm::Value *This) override; |
| 173 | |
| 174 | void emitVTableDefinitions(CodeGenVTables &CGVT, |
| 175 | const CXXRecordDecl *RD) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 176 | |
| 177 | llvm::Value *getVTableAddressPointInStructor( |
| 178 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, |
| 179 | BaseSubobject Base, const CXXRecordDecl *NearestVBase, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 180 | bool &NeedsVirtualOffset) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 181 | |
| 182 | llvm::Constant * |
| 183 | getVTableAddressPointForConstExpr(BaseSubobject Base, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 184 | const CXXRecordDecl *VTableClass) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 185 | |
| 186 | llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 187 | CharUnits VPtrOffset) override; |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 188 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 189 | llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 190 | llvm::Value *This, |
| 191 | llvm::Type *Ty) override; |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 192 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 193 | llvm::Value *EmitVirtualDestructorCall(CodeGenFunction &CGF, |
| 194 | const CXXDestructorDecl *Dtor, |
| 195 | CXXDtorType DtorType, |
| 196 | llvm::Value *This, |
| 197 | const CXXMemberCallExpr *CE) override; |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 198 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 199 | void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 200 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 201 | void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, GlobalDecl GD, |
| 202 | bool ReturnAdjustment) override { |
Timur Iskhodzhanov | 2cb17a0 | 2013-10-09 09:23:58 +0000 | [diff] [blame] | 203 | // Allow inlining of thunks by emitting them with available_externally |
| 204 | // linkage together with vtables when needed. |
| 205 | if (ForVTable) |
| 206 | Thunk->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); |
| 207 | } |
| 208 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 209 | llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 210 | const ThisAdjustment &TA) override; |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 211 | |
| 212 | llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 213 | const ReturnAdjustment &RA) override; |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 214 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 215 | size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, |
| 216 | FunctionArgList &Args) const override { |
| 217 | assert(!Args.empty() && "expected the arglist to not be empty!"); |
| 218 | return Args.size() - 1; |
| 219 | } |
| 220 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 221 | StringRef GetPureVirtualCallName() override { return "__cxa_pure_virtual"; } |
| 222 | StringRef GetDeletedVirtualCallName() override |
| 223 | { return "__cxa_deleted_virtual"; } |
Joao Matos | 285baac | 2012-07-17 17:10:11 +0000 | [diff] [blame] | 224 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 225 | CharUnits getArrayCookieSizeImpl(QualType elementType) override; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 226 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 227 | llvm::Value *NewPtr, |
| 228 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 229 | const CXXNewExpr *expr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 230 | QualType ElementType) override; |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 231 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 232 | llvm::Value *allocPtr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 233 | CharUnits cookieSize) override; |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 234 | |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 235 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 236 | llvm::GlobalVariable *DeclPtr, |
| 237 | bool PerformInit) override; |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 238 | void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 239 | llvm::Constant *dtor, llvm::Constant *addr) override; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 240 | |
| 241 | llvm::Function *getOrCreateThreadLocalWrapper(const VarDecl *VD, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 242 | llvm::Value *Val); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 243 | void EmitThreadLocalInitFuncs( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 244 | CodeGenModule &CGM, |
| 245 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 246 | CXXThreadLocals, |
| 247 | ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 248 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) override; |
| 249 | |
| 250 | bool usesThreadWrapperFunction() const override { return true; } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 251 | LValue EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, const VarDecl *VD, |
| 252 | QualType LValType) override; |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 253 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 254 | bool NeedsVTTParameter(GlobalDecl GD) override; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 255 | |
| 256 | /**************************** RTTI Uniqueness ******************************/ |
| 257 | |
| 258 | protected: |
| 259 | /// Returns true if the ABI requires RTTI type_info objects to be unique |
| 260 | /// across a program. |
| 261 | virtual bool shouldRTTIBeUnique() const { return true; } |
| 262 | |
| 263 | public: |
| 264 | /// What sort of unique-RTTI behavior should we use? |
| 265 | enum RTTIUniquenessKind { |
| 266 | /// We are guaranteeing, or need to guarantee, that the RTTI string |
| 267 | /// is unique. |
| 268 | RUK_Unique, |
| 269 | |
| 270 | /// We are not guaranteeing uniqueness for the RTTI string, so we |
| 271 | /// can demote to hidden visibility but must use string comparisons. |
| 272 | RUK_NonUniqueHidden, |
| 273 | |
| 274 | /// We are not guaranteeing uniqueness for the RTTI string, so we |
| 275 | /// have to use string comparisons, but we also have to emit it with |
| 276 | /// non-hidden visibility. |
| 277 | RUK_NonUniqueVisible |
| 278 | }; |
| 279 | |
| 280 | /// Return the required visibility status for the given type and linkage in |
| 281 | /// the current ABI. |
| 282 | RTTIUniquenessKind |
| 283 | classifyRTTIUniqueness(QualType CanTy, |
| 284 | llvm::GlobalValue::LinkageTypes Linkage) const; |
| 285 | friend class ItaniumRTTIBuilder; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 286 | |
| 287 | void emitCXXStructor(const CXXMethodDecl *MD, StructorType Type) override; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 288 | }; |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 289 | |
| 290 | class ARMCXXABI : public ItaniumCXXABI { |
| 291 | public: |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 292 | ARMCXXABI(CodeGen::CodeGenModule &CGM) : |
| 293 | ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 294 | /* UseARMGuardVarABI = */ true) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 295 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 296 | bool HasThisReturn(GlobalDecl GD) const override { |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 297 | return (isa<CXXConstructorDecl>(GD.getDecl()) || ( |
| 298 | isa<CXXDestructorDecl>(GD.getDecl()) && |
| 299 | GD.getDtorType() != Dtor_Deleting)); |
| 300 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 301 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 302 | void EmitReturnFromThunk(CodeGenFunction &CGF, RValue RV, |
| 303 | QualType ResTy) override; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 304 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 305 | CharUnits getArrayCookieSizeImpl(QualType elementType) override; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 306 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 307 | llvm::Value *NewPtr, |
| 308 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 309 | const CXXNewExpr *expr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 310 | QualType ElementType) override; |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 311 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 312 | CharUnits cookieSize) override; |
| 313 | }; |
| 314 | |
| 315 | class iOS64CXXABI : public ARMCXXABI { |
| 316 | public: |
| 317 | iOS64CXXABI(CodeGen::CodeGenModule &CGM) : ARMCXXABI(CGM) {} |
| 318 | |
| 319 | // ARM64 libraries are prepared for non-unique RTTI. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 320 | bool shouldRTTIBeUnique() const override { return false; } |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 321 | }; |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 324 | CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 325 | switch (CGM.getTarget().getCXXABI().getKind()) { |
John McCall | 96fcde0 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 326 | // For IR-generation purposes, there's no significant difference |
| 327 | // between the ARM and iOS ABIs. |
| 328 | case TargetCXXABI::GenericARM: |
| 329 | case TargetCXXABI::iOS: |
| 330 | return new ARMCXXABI(CGM); |
Charles Davis | 3a811f1 | 2010-05-25 19:52:27 +0000 | [diff] [blame] | 331 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 332 | case TargetCXXABI::iOS64: |
| 333 | return new iOS64CXXABI(CGM); |
| 334 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 335 | // Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't |
| 336 | // include the other 32-bit ARM oddities: constructor/destructor return values |
| 337 | // and array cookies. |
| 338 | case TargetCXXABI::GenericAArch64: |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 339 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 340 | /* UseARMGuardVarABI = */ true); |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 341 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 342 | case TargetCXXABI::GenericMIPS: |
| 343 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true); |
| 344 | |
John McCall | 96fcde0 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 345 | case TargetCXXABI::GenericItanium: |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 346 | if (CGM.getContext().getTargetInfo().getTriple().getArch() |
| 347 | == llvm::Triple::le32) { |
| 348 | // For PNaCl, use ARM-style method pointers so that PNaCl code |
| 349 | // does not assume anything about the alignment of function |
| 350 | // pointers. |
| 351 | return new ItaniumCXXABI(CGM, /* UseARMMethodPtrABI = */ true, |
| 352 | /* UseARMGuardVarABI = */ false); |
| 353 | } |
John McCall | 96fcde0 | 2013-01-25 23:36:14 +0000 | [diff] [blame] | 354 | return new ItaniumCXXABI(CGM); |
| 355 | |
| 356 | case TargetCXXABI::Microsoft: |
| 357 | llvm_unreachable("Microsoft ABI is not Itanium-based"); |
| 358 | } |
| 359 | llvm_unreachable("bad ABI kind"); |
John McCall | ee79a4c | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 362 | llvm::Type * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 363 | ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { |
| 364 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 365 | return CGM.PtrDiffTy; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 366 | return llvm::StructType::get(CGM.PtrDiffTy, CGM.PtrDiffTy, nullptr); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 367 | } |
| 368 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 369 | /// In the Itanium and ARM ABIs, method pointers have the form: |
| 370 | /// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr; |
| 371 | /// |
| 372 | /// In the Itanium ABI: |
| 373 | /// - method pointers are virtual if (memptr.ptr & 1) is nonzero |
| 374 | /// - the this-adjustment is (memptr.adj) |
| 375 | /// - the virtual offset is (memptr.ptr - 1) |
| 376 | /// |
| 377 | /// In the ARM ABI: |
| 378 | /// - method pointers are virtual if (memptr.adj & 1) is nonzero |
| 379 | /// - the this-adjustment is (memptr.adj >> 1) |
| 380 | /// - the virtual offset is (memptr.ptr) |
| 381 | /// ARM uses 'adj' for the virtual flag because Thumb functions |
| 382 | /// may be only single-byte aligned. |
| 383 | /// |
| 384 | /// If the member is virtual, the adjusted 'this' pointer points |
| 385 | /// to a vtable pointer from which the virtual offset is applied. |
| 386 | /// |
| 387 | /// If the member is non-virtual, memptr.ptr is the address of |
| 388 | /// the function to call. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 389 | llvm::Value *ItaniumCXXABI::EmitLoadOfMemberFunctionPointer( |
| 390 | CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, |
| 391 | llvm::Value *MemFnPtr, const MemberPointerType *MPT) { |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 392 | CGBuilderTy &Builder = CGF.Builder; |
| 393 | |
| 394 | const FunctionProtoType *FPT = |
| 395 | MPT->getPointeeType()->getAs<FunctionProtoType>(); |
| 396 | const CXXRecordDecl *RD = |
| 397 | cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); |
| 398 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 399 | llvm::FunctionType *FTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 400 | CGM.getTypes().GetFunctionType( |
| 401 | CGM.getTypes().arrangeCXXMethodType(RD, FPT)); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 402 | |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 403 | llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(CGM.PtrDiffTy, 1); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 404 | |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 405 | llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual"); |
| 406 | llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual"); |
| 407 | llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end"); |
| 408 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 409 | // Extract memptr.adj, which is in the second field. |
| 410 | llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj"); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 411 | |
| 412 | // Compute the true adjustment. |
| 413 | llvm::Value *Adj = RawAdj; |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 414 | if (UseARMMethodPtrABI) |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 415 | Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 416 | |
| 417 | // Apply the adjustment and cast back to the original struct type |
| 418 | // for consistency. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 419 | llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy()); |
| 420 | Ptr = Builder.CreateInBoundsGEP(Ptr, Adj); |
| 421 | This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 422 | |
| 423 | // Load the function pointer. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 424 | llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 425 | |
| 426 | // If the LSB in the function pointer is 1, the function pointer points to |
| 427 | // a virtual function. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 428 | llvm::Value *IsVirtual; |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 429 | if (UseARMMethodPtrABI) |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 430 | IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1); |
| 431 | else |
| 432 | IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1); |
| 433 | IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 434 | Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); |
| 435 | |
| 436 | // In the virtual path, the adjustment left 'This' pointing to the |
| 437 | // vtable of the correct base subobject. The "function pointer" is an |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 438 | // offset within the vtable (+1 for the virtual flag on non-ARM). |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 439 | CGF.EmitBlock(FnVirtual); |
| 440 | |
| 441 | // Cast the adjusted this to a pointer to vtable pointer and load. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 442 | llvm::Type *VTableTy = Builder.getInt8PtrTy(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 443 | llvm::Value *VTable = CGF.GetVTablePtr(This, VTableTy); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 444 | |
| 445 | // Apply the offset. |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 446 | llvm::Value *VTableOffset = FnAsInt; |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 447 | if (!UseARMMethodPtrABI) |
| 448 | VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 449 | VTable = Builder.CreateGEP(VTable, VTableOffset); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 450 | |
| 451 | // Load the virtual function to call. |
| 452 | VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo()); |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 453 | llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 454 | CGF.EmitBranch(FnEnd); |
| 455 | |
| 456 | // In the non-virtual path, the function pointer is actually a |
| 457 | // function pointer. |
| 458 | CGF.EmitBlock(FnNonVirtual); |
| 459 | llvm::Value *NonVirtualFn = |
John McCall | babc9a9 | 2010-08-22 00:59:17 +0000 | [diff] [blame] | 460 | Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn"); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 461 | |
| 462 | // We're done. |
| 463 | CGF.EmitBlock(FnEnd); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 464 | llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo(), 2); |
John McCall | 93d557b | 2010-08-22 00:05:51 +0000 | [diff] [blame] | 465 | Callee->addIncoming(VirtualFn, FnVirtual); |
| 466 | Callee->addIncoming(NonVirtualFn, FnNonVirtual); |
| 467 | return Callee; |
| 468 | } |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 469 | |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 470 | /// Compute an l-value by applying the given pointer-to-member to a |
| 471 | /// base object. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 472 | llvm::Value *ItaniumCXXABI::EmitMemberDataPointerAddress( |
| 473 | CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, |
| 474 | const MemberPointerType *MPT) { |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 475 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 476 | |
| 477 | CGBuilderTy &Builder = CGF.Builder; |
| 478 | |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 479 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 480 | |
| 481 | // Cast to char*. |
| 482 | Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); |
| 483 | |
| 484 | // Apply the offset, which we assume is non-null. |
| 485 | llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, MemPtr, "memptr.offset"); |
| 486 | |
| 487 | // Cast the address to the appropriate pointer type, adopting the |
| 488 | // address space of the base pointer. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 489 | llvm::Type *PType |
Douglas Gregor | eede61a | 2010-09-02 17:38:50 +0000 | [diff] [blame] | 490 | = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); |
John McCall | 6c2ab1d | 2010-08-31 21:07:20 +0000 | [diff] [blame] | 491 | return Builder.CreateBitCast(Addr, PType); |
| 492 | } |
| 493 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 494 | /// Perform a bitcast, derived-to-base, or base-to-derived member pointer |
| 495 | /// conversion. |
| 496 | /// |
| 497 | /// Bitcast conversions are always a no-op under Itanium. |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 498 | /// |
| 499 | /// Obligatory offset/adjustment diagram: |
| 500 | /// <-- offset --> <-- adjustment --> |
| 501 | /// |--------------------------|----------------------|--------------------| |
| 502 | /// ^Derived address point ^Base address point ^Member address point |
| 503 | /// |
| 504 | /// So when converting a base member pointer to a derived member pointer, |
| 505 | /// we add the offset to the adjustment because the address point has |
| 506 | /// decreased; and conversely, when converting a derived MP to a base MP |
| 507 | /// we subtract the offset from the adjustment because the address point |
| 508 | /// has increased. |
| 509 | /// |
| 510 | /// The standard forbids (at compile time) conversion to and from |
| 511 | /// virtual bases, which is why we don't have to consider them here. |
| 512 | /// |
| 513 | /// The standard forbids (at run time) casting a derived MP to a base |
| 514 | /// MP when the derived MP does not point to a member of the base. |
| 515 | /// This is why -1 is a reasonable choice for null data member |
| 516 | /// pointers. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 517 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 518 | ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, |
| 519 | const CastExpr *E, |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 520 | llvm::Value *src) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 521 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 522 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 523 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 524 | |
| 525 | // Under Itanium, reinterprets don't require any additional processing. |
| 526 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 527 | |
| 528 | // Use constant emission if we can. |
| 529 | if (isa<llvm::Constant>(src)) |
| 530 | return EmitMemberPointerConversion(E, cast<llvm::Constant>(src)); |
| 531 | |
| 532 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 533 | if (!adj) return src; |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 534 | |
| 535 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 536 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 537 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 538 | const MemberPointerType *destTy = |
| 539 | E->getType()->castAs<MemberPointerType>(); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 540 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 541 | // For member data pointers, this is just a matter of adding the |
| 542 | // offset if the source is non-null. |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 543 | if (destTy->isMemberDataPointer()) { |
| 544 | llvm::Value *dst; |
| 545 | if (isDerivedToBase) |
| 546 | dst = Builder.CreateNSWSub(src, adj, "adj"); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 547 | else |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 548 | dst = Builder.CreateNSWAdd(src, adj, "adj"); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 549 | |
| 550 | // Null check. |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 551 | llvm::Value *null = llvm::Constant::getAllOnesValue(src->getType()); |
| 552 | llvm::Value *isNull = Builder.CreateICmpEQ(src, null, "memptr.isnull"); |
| 553 | return Builder.CreateSelect(isNull, src, dst); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 554 | } |
| 555 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 556 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 557 | if (UseARMMethodPtrABI) { |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 558 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 559 | offset <<= 1; |
| 560 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 561 | } |
| 562 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 563 | llvm::Value *srcAdj = Builder.CreateExtractValue(src, 1, "src.adj"); |
| 564 | llvm::Value *dstAdj; |
| 565 | if (isDerivedToBase) |
| 566 | dstAdj = Builder.CreateNSWSub(srcAdj, adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 567 | else |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 568 | dstAdj = Builder.CreateNSWAdd(srcAdj, adj, "adj"); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 569 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 570 | return Builder.CreateInsertValue(src, dstAdj, 1); |
| 571 | } |
| 572 | |
| 573 | llvm::Constant * |
| 574 | ItaniumCXXABI::EmitMemberPointerConversion(const CastExpr *E, |
| 575 | llvm::Constant *src) { |
| 576 | assert(E->getCastKind() == CK_DerivedToBaseMemberPointer || |
| 577 | E->getCastKind() == CK_BaseToDerivedMemberPointer || |
| 578 | E->getCastKind() == CK_ReinterpretMemberPointer); |
| 579 | |
| 580 | // Under Itanium, reinterprets don't require any additional processing. |
| 581 | if (E->getCastKind() == CK_ReinterpretMemberPointer) return src; |
| 582 | |
| 583 | // If the adjustment is trivial, we don't need to do anything. |
| 584 | llvm::Constant *adj = getMemberPointerAdjustment(E); |
| 585 | if (!adj) return src; |
| 586 | |
| 587 | bool isDerivedToBase = (E->getCastKind() == CK_DerivedToBaseMemberPointer); |
| 588 | |
| 589 | const MemberPointerType *destTy = |
| 590 | E->getType()->castAs<MemberPointerType>(); |
| 591 | |
| 592 | // For member data pointers, this is just a matter of adding the |
| 593 | // offset if the source is non-null. |
| 594 | if (destTy->isMemberDataPointer()) { |
| 595 | // null maps to null. |
| 596 | if (src->isAllOnesValue()) return src; |
| 597 | |
| 598 | if (isDerivedToBase) |
| 599 | return llvm::ConstantExpr::getNSWSub(src, adj); |
| 600 | else |
| 601 | return llvm::ConstantExpr::getNSWAdd(src, adj); |
| 602 | } |
| 603 | |
| 604 | // The this-adjustment is left-shifted by 1 on ARM. |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 605 | if (UseARMMethodPtrABI) { |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 606 | uint64_t offset = cast<llvm::ConstantInt>(adj)->getZExtValue(); |
| 607 | offset <<= 1; |
| 608 | adj = llvm::ConstantInt::get(adj->getType(), offset); |
| 609 | } |
| 610 | |
| 611 | llvm::Constant *srcAdj = llvm::ConstantExpr::getExtractValue(src, 1); |
| 612 | llvm::Constant *dstAdj; |
| 613 | if (isDerivedToBase) |
| 614 | dstAdj = llvm::ConstantExpr::getNSWSub(srcAdj, adj); |
| 615 | else |
| 616 | dstAdj = llvm::ConstantExpr::getNSWAdd(srcAdj, adj); |
| 617 | |
| 618 | return llvm::ConstantExpr::getInsertValue(src, dstAdj, 1); |
John McCall | 3023def | 2010-08-22 03:04:22 +0000 | [diff] [blame] | 619 | } |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 620 | |
| 621 | llvm::Constant * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 622 | ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 623 | // Itanium C++ ABI 2.3: |
| 624 | // A NULL pointer is represented as -1. |
| 625 | if (MPT->isMemberDataPointer()) |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 626 | return llvm::ConstantInt::get(CGM.PtrDiffTy, -1ULL, /*isSigned=*/true); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 627 | |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 628 | llvm::Constant *Zero = llvm::ConstantInt::get(CGM.PtrDiffTy, 0); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 629 | llvm::Constant *Values[2] = { Zero, Zero }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 630 | return llvm::ConstantStruct::getAnon(Values); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 631 | } |
| 632 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 633 | llvm::Constant * |
| 634 | ItaniumCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, |
| 635 | CharUnits offset) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 636 | // Itanium C++ ABI 2.3: |
| 637 | // A pointer to data member is an offset from the base address of |
| 638 | // the class object containing it, represented as a ptrdiff_t |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 639 | return llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 640 | } |
| 641 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 642 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 643 | return BuildMemberPointer(MD, CharUnits::Zero()); |
| 644 | } |
| 645 | |
| 646 | llvm::Constant *ItaniumCXXABI::BuildMemberPointer(const CXXMethodDecl *MD, |
| 647 | CharUnits ThisAdjustment) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 648 | assert(MD->isInstance() && "Member function must not be static!"); |
| 649 | MD = MD->getCanonicalDecl(); |
| 650 | |
| 651 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 652 | |
| 653 | // Get the function pointer (or index if this is a virtual function). |
| 654 | llvm::Constant *MemPtr[2]; |
| 655 | if (MD->isVirtual()) { |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 656 | uint64_t Index = CGM.getItaniumVTableContext().getMethodVTableIndex(MD); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 657 | |
Ken Dyck | 1246ba6 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 658 | const ASTContext &Context = getContext(); |
| 659 | CharUnits PointerWidth = |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 660 | Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); |
Ken Dyck | 1246ba6 | 2011-04-09 01:30:02 +0000 | [diff] [blame] | 661 | uint64_t VTableOffset = (Index * PointerWidth.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 662 | |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 663 | if (UseARMMethodPtrABI) { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 664 | // ARM C++ ABI 3.2.1: |
| 665 | // This ABI specifies that adj contains twice the this |
| 666 | // adjustment, plus 1 if the member function is virtual. The |
| 667 | // least significant bit of adj then makes exactly the same |
| 668 | // discrimination as the least significant bit of ptr does for |
| 669 | // Itanium. |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 670 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset); |
| 671 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 672 | 2 * ThisAdjustment.getQuantity() + 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 673 | } else { |
| 674 | // Itanium C++ ABI 2.3: |
| 675 | // For a virtual function, [the pointer field] is 1 plus the |
| 676 | // virtual table offset (in bytes) of the function, |
| 677 | // represented as a ptrdiff_t. |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 678 | MemPtr[0] = llvm::ConstantInt::get(CGM.PtrDiffTy, VTableOffset + 1); |
| 679 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 680 | ThisAdjustment.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 681 | } |
| 682 | } else { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 683 | const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 684 | llvm::Type *Ty; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 685 | // Check whether the function has a computable LLVM signature. |
Chris Lattner | f742eb0 | 2011-07-10 00:18:59 +0000 | [diff] [blame] | 686 | if (Types.isFuncTypeConvertible(FPT)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 687 | // The function has a computable LLVM signature; use the correct type. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 688 | Ty = Types.GetFunctionType(Types.arrangeCXXMethodDeclaration(MD)); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 689 | } else { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 690 | // Use an arbitrary non-function type to tell GetAddrOfFunction that the |
| 691 | // function type is incomplete. |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 692 | Ty = CGM.PtrDiffTy; |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 693 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 694 | llvm::Constant *addr = CGM.GetAddrOfFunction(MD, Ty); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 695 | |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 696 | MemPtr[0] = llvm::ConstantExpr::getPtrToInt(addr, CGM.PtrDiffTy); |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 697 | MemPtr[1] = llvm::ConstantInt::get(CGM.PtrDiffTy, |
| 698 | (UseARMMethodPtrABI ? 2 : 1) * |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 699 | ThisAdjustment.getQuantity()); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 700 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 701 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 702 | return llvm::ConstantStruct::getAnon(MemPtr); |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 705 | llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const APValue &MP, |
| 706 | QualType MPType) { |
| 707 | const MemberPointerType *MPT = MPType->castAs<MemberPointerType>(); |
| 708 | const ValueDecl *MPD = MP.getMemberPointerDecl(); |
| 709 | if (!MPD) |
| 710 | return EmitNullMemberPointer(MPT); |
| 711 | |
Reid Kleckner | f632730 | 2013-05-09 21:01:17 +0000 | [diff] [blame] | 712 | CharUnits ThisAdjustment = getMemberPointerPathAdjustment(MP); |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 713 | |
| 714 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) |
| 715 | return BuildMemberPointer(MD, ThisAdjustment); |
| 716 | |
| 717 | CharUnits FieldOffset = |
| 718 | getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); |
| 719 | return EmitMemberDataPointer(MPT, ThisAdjustment + FieldOffset); |
| 720 | } |
| 721 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 722 | /// The comparison algorithm is pretty easy: the member pointers are |
| 723 | /// the same if they're either bitwise identical *or* both null. |
| 724 | /// |
| 725 | /// ARM is different here only because null-ness is more complicated. |
| 726 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 727 | ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, |
| 728 | llvm::Value *L, |
| 729 | llvm::Value *R, |
| 730 | const MemberPointerType *MPT, |
| 731 | bool Inequality) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 732 | CGBuilderTy &Builder = CGF.Builder; |
| 733 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 734 | llvm::ICmpInst::Predicate Eq; |
| 735 | llvm::Instruction::BinaryOps And, Or; |
| 736 | if (Inequality) { |
| 737 | Eq = llvm::ICmpInst::ICMP_NE; |
| 738 | And = llvm::Instruction::Or; |
| 739 | Or = llvm::Instruction::And; |
| 740 | } else { |
| 741 | Eq = llvm::ICmpInst::ICMP_EQ; |
| 742 | And = llvm::Instruction::And; |
| 743 | Or = llvm::Instruction::Or; |
| 744 | } |
| 745 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 746 | // Member data pointers are easy because there's a unique null |
| 747 | // value, so it just comes down to bitwise equality. |
| 748 | if (MPT->isMemberDataPointer()) |
| 749 | return Builder.CreateICmp(Eq, L, R); |
| 750 | |
| 751 | // For member function pointers, the tautologies are more complex. |
| 752 | // The Itanium tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 753 | // (L == R) <==> (L.ptr == R.ptr && (L.ptr == 0 || L.adj == R.adj)) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 754 | // The ARM tautology is: |
John McCall | de719f7 | 2010-08-23 06:56:36 +0000 | [diff] [blame] | 755 | // (L == R) <==> (L.ptr == R.ptr && |
| 756 | // (L.adj == R.adj || |
| 757 | // (L.ptr == 0 && ((L.adj|R.adj) & 1) == 0))) |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 758 | // The inequality tautologies have exactly the same structure, except |
| 759 | // applying De Morgan's laws. |
| 760 | |
| 761 | llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr"); |
| 762 | llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr"); |
| 763 | |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 764 | // This condition tests whether L.ptr == R.ptr. This must always be |
| 765 | // true for equality to hold. |
| 766 | llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr"); |
| 767 | |
| 768 | // This condition, together with the assumption that L.ptr == R.ptr, |
| 769 | // tests whether the pointers are both null. ARM imposes an extra |
| 770 | // condition. |
| 771 | llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType()); |
| 772 | llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null"); |
| 773 | |
| 774 | // This condition tests whether L.adj == R.adj. If this isn't |
| 775 | // true, the pointers are unequal unless they're both null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 776 | llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj"); |
| 777 | llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 778 | llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj"); |
| 779 | |
| 780 | // Null member function pointers on ARM clear the low bit of Adj, |
| 781 | // so the zero condition has to check that neither low bit is set. |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 782 | if (UseARMMethodPtrABI) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 783 | llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1); |
| 784 | |
| 785 | // Compute (l.adj | r.adj) & 1 and test it against zero. |
| 786 | llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj"); |
| 787 | llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One); |
| 788 | llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero, |
| 789 | "cmp.or.adj"); |
| 790 | EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero); |
| 791 | } |
| 792 | |
| 793 | // Tie together all our conditions. |
| 794 | llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq); |
| 795 | Result = Builder.CreateBinOp(And, PtrEq, Result, |
| 796 | Inequality ? "memptr.ne" : "memptr.eq"); |
| 797 | return Result; |
| 798 | } |
| 799 | |
| 800 | llvm::Value * |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 801 | ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, |
| 802 | llvm::Value *MemPtr, |
| 803 | const MemberPointerType *MPT) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 804 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 805 | |
| 806 | /// For member data pointers, this is just a check against -1. |
| 807 | if (MPT->isMemberDataPointer()) { |
Reid Kleckner | 92e44d9 | 2013-03-22 16:13:10 +0000 | [diff] [blame] | 808 | assert(MemPtr->getType() == CGM.PtrDiffTy); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 809 | llvm::Value *NegativeOne = |
| 810 | llvm::Constant::getAllOnesValue(MemPtr->getType()); |
| 811 | return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool"); |
| 812 | } |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 814 | // In Itanium, a member function pointer is not null if 'ptr' is not null. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 815 | llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 816 | |
| 817 | llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0); |
| 818 | llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool"); |
| 819 | |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 820 | // On ARM, a member function pointer is also non-null if the low bit of 'adj' |
| 821 | // (the virtual bit) is set. |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 822 | if (UseARMMethodPtrABI) { |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 823 | llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 824 | llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj"); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 825 | llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit"); |
Daniel Dunbar | db27b5f | 2011-04-19 23:10:47 +0000 | [diff] [blame] | 826 | llvm::Value *IsVirtual = Builder.CreateICmpNE(VirtualBit, Zero, |
| 827 | "memptr.isvirtual"); |
| 828 | Result = Builder.CreateOr(Result, IsVirtual); |
John McCall | e9fd7eb | 2010-08-22 08:30:07 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | return Result; |
| 832 | } |
John McCall | 875ab10 | 2010-08-22 06:43:33 +0000 | [diff] [blame] | 833 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 834 | bool ItaniumCXXABI::classifyReturnType(CGFunctionInfo &FI) const { |
| 835 | const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); |
| 836 | if (!RD) |
| 837 | return false; |
| 838 | |
| 839 | // Return indirectly if we have a non-trivial copy ctor or non-trivial dtor. |
| 840 | // FIXME: Use canCopyArgument() when it is fixed to handle lazily declared |
| 841 | // special members. |
| 842 | if (RD->hasNonTrivialDestructor() || RD->hasNonTrivialCopyConstructor()) { |
| 843 | FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 844 | return true; |
| 845 | } |
| 846 | return false; |
| 847 | } |
| 848 | |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 849 | /// The Itanium ABI requires non-zero initialization only for data |
| 850 | /// member pointers, for which '0' is a valid offset. |
| 851 | bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) { |
| 852 | return MPT->getPointeeType()->isFunctionType(); |
John McCall | cf2c85e | 2010-08-22 04:16:24 +0000 | [diff] [blame] | 853 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 854 | |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 855 | /// The Itanium ABI always places an offset to the complete object |
| 856 | /// at entry -2 in the vtable. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 857 | void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF, |
| 858 | const CXXDeleteExpr *DE, |
| 859 | llvm::Value *Ptr, |
| 860 | QualType ElementType, |
| 861 | const CXXDestructorDecl *Dtor) { |
| 862 | bool UseGlobalDelete = DE->isGlobalDelete(); |
| 863 | if (UseGlobalDelete) { |
| 864 | // Derive the complete-object pointer, which is what we need |
| 865 | // to pass to the deallocation function. |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 866 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 867 | // Grab the vtable pointer as an intptr_t*. |
| 868 | llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.IntPtrTy->getPointerTo()); |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 869 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 870 | // Track back to entry -2 and pull out the offset there. |
| 871 | llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64( |
| 872 | VTable, -2, "complete-offset.ptr"); |
| 873 | llvm::LoadInst *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 874 | Offset->setAlignment(CGF.PointerAlignInBytes); |
| 875 | |
| 876 | // Apply the offset. |
| 877 | llvm::Value *CompletePtr = CGF.Builder.CreateBitCast(Ptr, CGF.Int8PtrTy); |
| 878 | CompletePtr = CGF.Builder.CreateInBoundsGEP(CompletePtr, Offset); |
| 879 | |
| 880 | // If we're supposed to call the global delete, make sure we do so |
| 881 | // even if the destructor throws. |
| 882 | CGF.pushCallObjectDeleteCleanup(DE->getOperatorDelete(), CompletePtr, |
| 883 | ElementType); |
| 884 | } |
| 885 | |
| 886 | // FIXME: Provide a source location here even though there's no |
| 887 | // CXXMemberCallExpr for dtor call. |
| 888 | CXXDtorType DtorType = UseGlobalDelete ? Dtor_Complete : Dtor_Deleting; |
| 889 | EmitVirtualDestructorCall(CGF, Dtor, DtorType, Ptr, /*CE=*/nullptr); |
| 890 | |
| 891 | if (UseGlobalDelete) |
| 892 | CGF.PopCleanupBlock(); |
John McCall | ecd03b4 | 2012-09-25 10:10:39 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 895 | void ItaniumCXXABI::emitRethrow(CodeGenFunction &CGF, bool isNoReturn) { |
| 896 | // void __cxa_rethrow(); |
| 897 | |
| 898 | llvm::FunctionType *FTy = |
| 899 | llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false); |
| 900 | |
| 901 | llvm::Constant *Fn = CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow"); |
| 902 | |
| 903 | if (isNoReturn) |
| 904 | CGF.EmitNoreturnRuntimeCallOrInvoke(Fn, None); |
| 905 | else |
| 906 | CGF.EmitRuntimeCallOrInvoke(Fn); |
| 907 | } |
| 908 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 909 | static llvm::Constant *getItaniumDynamicCastFn(CodeGenFunction &CGF) { |
| 910 | // void *__dynamic_cast(const void *sub, |
| 911 | // const abi::__class_type_info *src, |
| 912 | // const abi::__class_type_info *dst, |
| 913 | // std::ptrdiff_t src2dst_offset); |
| 914 | |
| 915 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
| 916 | llvm::Type *PtrDiffTy = |
| 917 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 918 | |
| 919 | llvm::Type *Args[4] = { Int8PtrTy, Int8PtrTy, Int8PtrTy, PtrDiffTy }; |
| 920 | |
| 921 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
| 922 | |
| 923 | // Mark the function as nounwind readonly. |
| 924 | llvm::Attribute::AttrKind FuncAttrs[] = { llvm::Attribute::NoUnwind, |
| 925 | llvm::Attribute::ReadOnly }; |
| 926 | llvm::AttributeSet Attrs = llvm::AttributeSet::get( |
| 927 | CGF.getLLVMContext(), llvm::AttributeSet::FunctionIndex, FuncAttrs); |
| 928 | |
| 929 | return CGF.CGM.CreateRuntimeFunction(FTy, "__dynamic_cast", Attrs); |
| 930 | } |
| 931 | |
| 932 | static llvm::Constant *getBadCastFn(CodeGenFunction &CGF) { |
| 933 | // void __cxa_bad_cast(); |
| 934 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
| 935 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_cast"); |
| 936 | } |
| 937 | |
| 938 | /// \brief Compute the src2dst_offset hint as described in the |
| 939 | /// Itanium C++ ABI [2.9.7] |
| 940 | static CharUnits computeOffsetHint(ASTContext &Context, |
| 941 | const CXXRecordDecl *Src, |
| 942 | const CXXRecordDecl *Dst) { |
| 943 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 944 | /*DetectVirtual=*/false); |
| 945 | |
| 946 | // If Dst is not derived from Src we can skip the whole computation below and |
| 947 | // return that Src is not a public base of Dst. Record all inheritance paths. |
| 948 | if (!Dst->isDerivedFrom(Src, Paths)) |
| 949 | return CharUnits::fromQuantity(-2ULL); |
| 950 | |
| 951 | unsigned NumPublicPaths = 0; |
| 952 | CharUnits Offset; |
| 953 | |
| 954 | // Now walk all possible inheritance paths. |
| 955 | for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end(); I != E; |
| 956 | ++I) { |
| 957 | if (I->Access != AS_public) // Ignore non-public inheritance. |
| 958 | continue; |
| 959 | |
| 960 | ++NumPublicPaths; |
| 961 | |
| 962 | for (CXXBasePath::iterator J = I->begin(), JE = I->end(); J != JE; ++J) { |
| 963 | // If the path contains a virtual base class we can't give any hint. |
| 964 | // -1: no hint. |
| 965 | if (J->Base->isVirtual()) |
| 966 | return CharUnits::fromQuantity(-1ULL); |
| 967 | |
| 968 | if (NumPublicPaths > 1) // Won't use offsets, skip computation. |
| 969 | continue; |
| 970 | |
| 971 | // Accumulate the base class offsets. |
| 972 | const ASTRecordLayout &L = Context.getASTRecordLayout(J->Class); |
| 973 | Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl()); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | // -2: Src is not a public base of Dst. |
| 978 | if (NumPublicPaths == 0) |
| 979 | return CharUnits::fromQuantity(-2ULL); |
| 980 | |
| 981 | // -3: Src is a multiple public base type but never a virtual base type. |
| 982 | if (NumPublicPaths > 1) |
| 983 | return CharUnits::fromQuantity(-3ULL); |
| 984 | |
| 985 | // Otherwise, the Src type is a unique public nonvirtual base type of Dst. |
| 986 | // Return the offset of Src from the origin of Dst. |
| 987 | return Offset; |
| 988 | } |
| 989 | |
| 990 | static llvm::Constant *getBadTypeidFn(CodeGenFunction &CGF) { |
| 991 | // void __cxa_bad_typeid(); |
| 992 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGF.VoidTy, false); |
| 993 | |
| 994 | return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_bad_typeid"); |
| 995 | } |
| 996 | |
| 997 | bool ItaniumCXXABI::shouldTypeidBeNullChecked(bool IsDeref, |
| 998 | QualType SrcRecordTy) { |
| 999 | return IsDeref; |
| 1000 | } |
| 1001 | |
| 1002 | void ItaniumCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { |
| 1003 | llvm::Value *Fn = getBadTypeidFn(CGF); |
| 1004 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
| 1005 | CGF.Builder.CreateUnreachable(); |
| 1006 | } |
| 1007 | |
| 1008 | llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF, |
| 1009 | QualType SrcRecordTy, |
| 1010 | llvm::Value *ThisPtr, |
| 1011 | llvm::Type *StdTypeInfoPtrTy) { |
| 1012 | llvm::Value *Value = |
| 1013 | CGF.GetVTablePtr(ThisPtr, StdTypeInfoPtrTy->getPointerTo()); |
| 1014 | |
| 1015 | // Load the type info. |
| 1016 | Value = CGF.Builder.CreateConstInBoundsGEP1_64(Value, -1ULL); |
| 1017 | return CGF.Builder.CreateLoad(Value); |
| 1018 | } |
| 1019 | |
| 1020 | bool ItaniumCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, |
| 1021 | QualType SrcRecordTy) { |
| 1022 | return SrcIsPtr; |
| 1023 | } |
| 1024 | |
| 1025 | llvm::Value *ItaniumCXXABI::EmitDynamicCastCall( |
| 1026 | CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, |
| 1027 | QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { |
| 1028 | llvm::Type *PtrDiffLTy = |
| 1029 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1030 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1031 | |
| 1032 | llvm::Value *SrcRTTI = |
| 1033 | CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); |
| 1034 | llvm::Value *DestRTTI = |
| 1035 | CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); |
| 1036 | |
| 1037 | // Compute the offset hint. |
| 1038 | const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); |
| 1039 | const CXXRecordDecl *DestDecl = DestRecordTy->getAsCXXRecordDecl(); |
| 1040 | llvm::Value *OffsetHint = llvm::ConstantInt::get( |
| 1041 | PtrDiffLTy, |
| 1042 | computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); |
| 1043 | |
| 1044 | // Emit the call to __dynamic_cast. |
| 1045 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1046 | |
| 1047 | llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; |
| 1048 | Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); |
| 1049 | Value = CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1050 | |
| 1051 | /// C++ [expr.dynamic.cast]p9: |
| 1052 | /// A failed cast to reference type throws std::bad_cast |
| 1053 | if (DestTy->isReferenceType()) { |
| 1054 | llvm::BasicBlock *BadCastBlock = |
| 1055 | CGF.createBasicBlock("dynamic_cast.bad_cast"); |
| 1056 | |
| 1057 | llvm::Value *IsNull = CGF.Builder.CreateIsNull(Value); |
| 1058 | CGF.Builder.CreateCondBr(IsNull, BadCastBlock, CastEnd); |
| 1059 | |
| 1060 | CGF.EmitBlock(BadCastBlock); |
| 1061 | EmitBadCastCall(CGF); |
| 1062 | } |
| 1063 | |
| 1064 | return Value; |
| 1065 | } |
| 1066 | |
| 1067 | llvm::Value *ItaniumCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, |
| 1068 | llvm::Value *Value, |
| 1069 | QualType SrcRecordTy, |
| 1070 | QualType DestTy) { |
| 1071 | llvm::Type *PtrDiffLTy = |
| 1072 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1073 | llvm::Type *DestLTy = CGF.ConvertType(DestTy); |
| 1074 | |
| 1075 | // Get the vtable pointer. |
| 1076 | llvm::Value *VTable = CGF.GetVTablePtr(Value, PtrDiffLTy->getPointerTo()); |
| 1077 | |
| 1078 | // Get the offset-to-top from the vtable. |
| 1079 | llvm::Value *OffsetToTop = |
| 1080 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, -2ULL); |
| 1081 | OffsetToTop = CGF.Builder.CreateLoad(OffsetToTop, "offset.to.top"); |
| 1082 | |
| 1083 | // Finally, add the offset to the pointer. |
| 1084 | Value = CGF.EmitCastToVoidPtr(Value); |
| 1085 | Value = CGF.Builder.CreateInBoundsGEP(Value, OffsetToTop); |
| 1086 | |
| 1087 | return CGF.Builder.CreateBitCast(Value, DestLTy); |
| 1088 | } |
| 1089 | |
| 1090 | bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { |
| 1091 | llvm::Value *Fn = getBadCastFn(CGF); |
| 1092 | CGF.EmitRuntimeCallOrInvoke(Fn).setDoesNotReturn(); |
| 1093 | CGF.Builder.CreateUnreachable(); |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1097 | llvm::Value * |
| 1098 | ItaniumCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, |
| 1099 | llvm::Value *This, |
| 1100 | const CXXRecordDecl *ClassDecl, |
| 1101 | const CXXRecordDecl *BaseClassDecl) { |
| 1102 | llvm::Value *VTablePtr = CGF.GetVTablePtr(This, CGM.Int8PtrTy); |
| 1103 | CharUnits VBaseOffsetOffset = |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1104 | CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(ClassDecl, |
| 1105 | BaseClassDecl); |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1106 | |
| 1107 | llvm::Value *VBaseOffsetPtr = |
| 1108 | CGF.Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), |
| 1109 | "vbase.offset.ptr"); |
| 1110 | VBaseOffsetPtr = CGF.Builder.CreateBitCast(VBaseOffsetPtr, |
| 1111 | CGM.PtrDiffTy->getPointerTo()); |
| 1112 | |
| 1113 | llvm::Value *VBaseOffset = |
| 1114 | CGF.Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1115 | |
| 1116 | return VBaseOffset; |
| 1117 | } |
| 1118 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1119 | void ItaniumCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { |
| 1120 | // Just make sure we're in sync with TargetCXXABI. |
| 1121 | assert(CGM.getTarget().getCXXABI().hasConstructorVariants()); |
| 1122 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1123 | // The constructor used for constructing this as a base class; |
| 1124 | // ignores virtual bases. |
| 1125 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Base)); |
| 1126 | |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1127 | // The constructor used for constructing this as a complete class; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1128 | // constructs the virtual bases, then calls the base constructor. |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1129 | if (!D->getParent()->isAbstract()) { |
| 1130 | // We don't need to emit the complete ctor if the class is abstract. |
| 1131 | CGM.EmitGlobal(GlobalDecl(D, Ctor_Complete)); |
| 1132 | } |
Timur Iskhodzhanov | bb1b797 | 2013-08-04 17:30:04 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1135 | void |
| 1136 | ItaniumCXXABI::buildStructorSignature(const CXXMethodDecl *MD, StructorType T, |
| 1137 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1138 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1139 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1140 | // All parameters are already in place except VTT, which goes after 'this'. |
| 1141 | // These are Clang types, so we don't need to worry about sret yet. |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1142 | |
| 1143 | // Check if we need to add a VTT parameter (which has type void **). |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1144 | if (T == StructorType::Base && MD->getParent()->getNumVBases() != 0) |
| 1145 | ArgTys.insert(ArgTys.begin() + 1, |
| 1146 | Context.getPointerType(Context.VoidPtrTy)); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1149 | void ItaniumCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1150 | // The destructor used for destructing this as a base class; ignores |
| 1151 | // virtual bases. |
| 1152 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1153 | |
| 1154 | // The destructor used for destructing this as a most-derived class; |
| 1155 | // call the base destructor and then destructs any virtual bases. |
| 1156 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Complete)); |
| 1157 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1158 | // The destructor in a virtual table is always a 'deleting' |
| 1159 | // destructor, which calls the complete destructor and then uses the |
| 1160 | // appropriate operator delete. |
| 1161 | if (D->isVirtual()) |
| 1162 | CGM.EmitGlobal(GlobalDecl(D, Dtor_Deleting)); |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1165 | void ItaniumCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, |
| 1166 | QualType &ResTy, |
| 1167 | FunctionArgList &Params) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1168 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1169 | assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1170 | |
| 1171 | // Check if we need a VTT parameter as well. |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 1172 | if (NeedsVTTParameter(CGF.CurGD)) { |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1173 | ASTContext &Context = getContext(); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1174 | |
| 1175 | // FIXME: avoid the fake decl |
| 1176 | QualType T = Context.getPointerType(Context.VoidPtrTy); |
| 1177 | ImplicitParamDecl *VTTDecl |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1178 | = ImplicitParamDecl::Create(Context, nullptr, MD->getLocation(), |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1179 | &Context.Idents.get("vtt"), T); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1180 | Params.insert(Params.begin() + 1, VTTDecl); |
| 1181 | getStructorImplicitParamDecl(CGF) = VTTDecl; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1185 | void ItaniumCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 1186 | /// Initialize the 'this' slot. |
| 1187 | EmitThisParam(CGF); |
| 1188 | |
| 1189 | /// Initialize the 'vtt' slot if needed. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1190 | if (getStructorImplicitParamDecl(CGF)) { |
| 1191 | getStructorImplicitParamValue(CGF) = CGF.Builder.CreateLoad( |
| 1192 | CGF.GetAddrOfLocalVar(getStructorImplicitParamDecl(CGF)), "vtt"); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1193 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1194 | |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1195 | /// If this is a function that the ABI specifies returns 'this', initialize |
| 1196 | /// the return slot to 'this' at the start of the function. |
| 1197 | /// |
| 1198 | /// Unlike the setting of return types, this is done within the ABI |
| 1199 | /// implementation instead of by clients of CGCXXABI because: |
| 1200 | /// 1) getThisValue is currently protected |
| 1201 | /// 2) in theory, an ABI could implement 'this' returns some other way; |
| 1202 | /// HasThisReturn only specifies a contract, not the implementation |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1203 | if (HasThisReturn(CGF.CurGD)) |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 1204 | CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1207 | unsigned ItaniumCXXABI::addImplicitConstructorArgs( |
| 1208 | CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, |
| 1209 | bool ForVirtualBase, bool Delegating, CallArgList &Args) { |
| 1210 | if (!NeedsVTTParameter(GlobalDecl(D, Type))) |
| 1211 | return 0; |
| 1212 | |
| 1213 | // Insert the implicit 'vtt' argument as the second argument. |
| 1214 | llvm::Value *VTT = |
| 1215 | CGF.GetVTTParameter(GlobalDecl(D, Type), ForVirtualBase, Delegating); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1216 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1217 | Args.insert(Args.begin() + 1, |
| 1218 | CallArg(RValue::get(VTT), VTTTy, /*needscopy=*/false)); |
| 1219 | return 1; // Added one arg. |
| 1220 | } |
| 1221 | |
| 1222 | void ItaniumCXXABI::EmitDestructorCall(CodeGenFunction &CGF, |
| 1223 | const CXXDestructorDecl *DD, |
| 1224 | CXXDtorType Type, bool ForVirtualBase, |
| 1225 | bool Delegating, llvm::Value *This) { |
| 1226 | GlobalDecl GD(DD, Type); |
| 1227 | llvm::Value *VTT = CGF.GetVTTParameter(GD, ForVirtualBase, Delegating); |
| 1228 | QualType VTTTy = getContext().getPointerType(getContext().VoidPtrTy); |
| 1229 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1230 | llvm::Value *Callee = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1231 | if (getContext().getLangOpts().AppleKext) |
| 1232 | Callee = CGF.BuildAppleKextVirtualDestructorCall(DD, Type, DD->getParent()); |
| 1233 | |
| 1234 | if (!Callee) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1235 | Callee = CGM.getAddrOfCXXStructor(DD, getFromDtorType(Type)); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1236 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1237 | CGF.EmitCXXMemberOrOperatorCall(DD, Callee, ReturnValueSlot(), This, VTT, |
| 1238 | VTTTy, nullptr); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1241 | void ItaniumCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, |
| 1242 | const CXXRecordDecl *RD) { |
| 1243 | llvm::GlobalVariable *VTable = getAddrOfVTable(RD, CharUnits()); |
| 1244 | if (VTable->hasInitializer()) |
| 1245 | return; |
| 1246 | |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1247 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1248 | const VTableLayout &VTLayout = VTContext.getVTableLayout(RD); |
| 1249 | llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1250 | llvm::Constant *RTTI = |
| 1251 | CGM.GetAddrOfRTTIDescriptor(CGM.getContext().getTagDeclType(RD)); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Create and set the initializer. |
| 1254 | llvm::Constant *Init = CGVT.CreateVTableInitializer( |
| 1255 | RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1256 | VTLayout.vtable_thunk_begin(), VTLayout.getNumVTableThunks(), RTTI); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1257 | VTable->setInitializer(Init); |
| 1258 | |
| 1259 | // Set the correct linkage. |
| 1260 | VTable->setLinkage(Linkage); |
| 1261 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1262 | if (CGM.supportsCOMDAT() && VTable->isWeakForLinker()) |
| 1263 | VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName())); |
| 1264 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1265 | // Set the right visibility. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1266 | CGM.setGlobalVisibility(VTable, RD); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1267 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1268 | // Use pointer alignment for the vtable. Otherwise we would align them based |
| 1269 | // on the size of the initializer which doesn't make sense as only single |
| 1270 | // values are read. |
| 1271 | unsigned PAlign = CGM.getTarget().getPointerAlign(0); |
| 1272 | VTable->setAlignment(getContext().toCharUnitsFromBits(PAlign).getQuantity()); |
| 1273 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1274 | // If this is the magic class __cxxabiv1::__fundamental_type_info, |
| 1275 | // we will emit the typeinfo for the fundamental types. This is the |
| 1276 | // same behaviour as GCC. |
| 1277 | const DeclContext *DC = RD->getDeclContext(); |
| 1278 | if (RD->getIdentifier() && |
| 1279 | RD->getIdentifier()->isStr("__fundamental_type_info") && |
| 1280 | isa<NamespaceDecl>(DC) && cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1281 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") && |
| 1282 | DC->getParent()->isTranslationUnit()) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1283 | EmitFundamentalRTTIDescriptors(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1284 | |
| 1285 | CGM.EmitVTableBitSetEntries(VTable, VTLayout); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | llvm::Value *ItaniumCXXABI::getVTableAddressPointInStructor( |
| 1289 | CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, |
| 1290 | const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { |
| 1291 | bool NeedsVTTParam = CGM.getCXXABI().NeedsVTTParameter(CGF.CurGD); |
| 1292 | NeedsVirtualOffset = (NeedsVTTParam && NearestVBase); |
| 1293 | |
| 1294 | llvm::Value *VTableAddressPoint; |
| 1295 | if (NeedsVTTParam && (Base.getBase()->getNumVBases() || NearestVBase)) { |
| 1296 | // Get the secondary vpointer index. |
| 1297 | uint64_t VirtualPointerIndex = |
| 1298 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1299 | |
| 1300 | /// Load the VTT. |
| 1301 | llvm::Value *VTT = CGF.LoadCXXVTT(); |
| 1302 | if (VirtualPointerIndex) |
| 1303 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1304 | |
| 1305 | // And load the address point from the VTT. |
| 1306 | VTableAddressPoint = CGF.Builder.CreateLoad(VTT); |
| 1307 | } else { |
| 1308 | llvm::Constant *VTable = |
| 1309 | CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits()); |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1310 | uint64_t AddressPoint = CGM.getItaniumVTableContext() |
| 1311 | .getVTableLayout(VTableClass) |
| 1312 | .getAddressPoint(Base); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1313 | VTableAddressPoint = |
| 1314 | CGF.Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
| 1315 | } |
| 1316 | |
| 1317 | return VTableAddressPoint; |
| 1318 | } |
| 1319 | |
| 1320 | llvm::Constant *ItaniumCXXABI::getVTableAddressPointForConstExpr( |
| 1321 | BaseSubobject Base, const CXXRecordDecl *VTableClass) { |
| 1322 | llvm::Constant *VTable = getAddrOfVTable(VTableClass, CharUnits()); |
| 1323 | |
| 1324 | // Find the appropriate vtable within the vtable group. |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1325 | uint64_t AddressPoint = CGM.getItaniumVTableContext() |
| 1326 | .getVTableLayout(VTableClass) |
| 1327 | .getAddressPoint(Base); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1328 | llvm::Value *Indices[] = { |
| 1329 | llvm::ConstantInt::get(CGM.Int64Ty, 0), |
| 1330 | llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) |
| 1331 | }; |
| 1332 | |
| 1333 | return llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices); |
| 1334 | } |
| 1335 | |
| 1336 | llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, |
| 1337 | CharUnits VPtrOffset) { |
| 1338 | assert(VPtrOffset.isZero() && "Itanium ABI only supports zero vptr offsets"); |
| 1339 | |
| 1340 | llvm::GlobalVariable *&VTable = VTables[RD]; |
| 1341 | if (VTable) |
| 1342 | return VTable; |
| 1343 | |
| 1344 | // Queue up this v-table for possible deferred emission. |
| 1345 | CGM.addDeferredVTable(RD); |
| 1346 | |
| 1347 | SmallString<256> OutName; |
| 1348 | llvm::raw_svector_ostream Out(OutName); |
Timur Iskhodzhanov | 11f22a3 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 1349 | getMangleContext().mangleCXXVTable(RD, Out); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1350 | Out.flush(); |
| 1351 | StringRef Name = OutName.str(); |
| 1352 | |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1353 | ItaniumVTableContext &VTContext = CGM.getItaniumVTableContext(); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1354 | llvm::ArrayType *ArrayType = llvm::ArrayType::get( |
| 1355 | CGM.Int8PtrTy, VTContext.getVTableLayout(RD).getNumVTableComponents()); |
| 1356 | |
| 1357 | VTable = CGM.CreateOrReplaceCXXRuntimeVariable( |
| 1358 | Name, ArrayType, llvm::GlobalValue::ExternalLinkage); |
| 1359 | VTable->setUnnamedAddr(true); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1360 | |
| 1361 | if (RD->hasAttr<DLLImportAttr>()) |
| 1362 | VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1363 | else if (RD->hasAttr<DLLExportAttr>()) |
| 1364 | VTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1365 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1366 | return VTable; |
| 1367 | } |
| 1368 | |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1369 | llvm::Value *ItaniumCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, |
| 1370 | GlobalDecl GD, |
| 1371 | llvm::Value *This, |
| 1372 | llvm::Type *Ty) { |
| 1373 | GD = GD.getCanonicalDecl(); |
| 1374 | Ty = Ty->getPointerTo()->getPointerTo(); |
| 1375 | llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); |
| 1376 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1377 | CGF.EmitVTablePtrCheckForCall(cast<CXXMethodDecl>(GD.getDecl()), VTable); |
| 1378 | |
Timur Iskhodzhanov | 5f0db58 | 2013-11-05 15:54:58 +0000 | [diff] [blame] | 1379 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1380 | llvm::Value *VFuncPtr = |
| 1381 | CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); |
| 1382 | return CGF.Builder.CreateLoad(VFuncPtr); |
| 1383 | } |
| 1384 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1385 | llvm::Value *ItaniumCXXABI::EmitVirtualDestructorCall( |
| 1386 | CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, |
| 1387 | llvm::Value *This, const CXXMemberCallExpr *CE) { |
| 1388 | assert(CE == nullptr || CE->arg_begin() == CE->arg_end()); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1389 | assert(DtorType == Dtor_Deleting || DtorType == Dtor_Complete); |
| 1390 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1391 | const CGFunctionInfo *FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( |
| 1392 | Dtor, getFromDtorType(DtorType)); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1393 | llvm::Type *Ty = CGF.CGM.getTypes().GetFunctionType(*FInfo); |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 1394 | llvm::Value *Callee = |
| 1395 | getVirtualFunctionPointer(CGF, GlobalDecl(Dtor, DtorType), This, Ty); |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1396 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1397 | CGF.EmitCXXMemberOrOperatorCall(Dtor, Callee, ReturnValueSlot(), This, |
| 1398 | /*ImplicitParam=*/nullptr, QualType(), CE); |
| 1399 | return nullptr; |
Timur Iskhodzhanov | 0f9827f | 2013-02-15 14:45:22 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1402 | void ItaniumCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1403 | CodeGenVTables &VTables = CGM.getVTables(); |
| 1404 | llvm::GlobalVariable *VTT = VTables.GetAddrOfVTT(RD); |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1405 | VTables.EmitVTTDefinition(VTT, CGM.getVTableLinkage(RD), RD); |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1408 | static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF, |
| 1409 | llvm::Value *Ptr, |
| 1410 | int64_t NonVirtualAdjustment, |
| 1411 | int64_t VirtualAdjustment, |
| 1412 | bool IsReturnAdjustment) { |
| 1413 | if (!NonVirtualAdjustment && !VirtualAdjustment) |
| 1414 | return Ptr; |
| 1415 | |
| 1416 | llvm::Type *Int8PtrTy = CGF.Int8PtrTy; |
| 1417 | llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy); |
| 1418 | |
| 1419 | if (NonVirtualAdjustment && !IsReturnAdjustment) { |
| 1420 | // Perform the non-virtual adjustment for a base-to-derived cast. |
| 1421 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 1422 | } |
| 1423 | |
| 1424 | if (VirtualAdjustment) { |
| 1425 | llvm::Type *PtrDiffTy = |
| 1426 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 1427 | |
| 1428 | // Perform the virtual adjustment. |
| 1429 | llvm::Value *VTablePtrPtr = |
| 1430 | CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo()); |
| 1431 | |
| 1432 | llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr); |
| 1433 | |
| 1434 | llvm::Value *OffsetPtr = |
| 1435 | CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); |
| 1436 | |
| 1437 | OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo()); |
| 1438 | |
| 1439 | // Load the adjustment offset from the vtable. |
| 1440 | llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr); |
| 1441 | |
| 1442 | // Adjust our pointer. |
| 1443 | V = CGF.Builder.CreateInBoundsGEP(V, Offset); |
| 1444 | } |
| 1445 | |
| 1446 | if (NonVirtualAdjustment && IsReturnAdjustment) { |
| 1447 | // Perform the non-virtual adjustment for a derived-to-base cast. |
| 1448 | V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment); |
| 1449 | } |
| 1450 | |
| 1451 | // Cast back to the original type. |
| 1452 | return CGF.Builder.CreateBitCast(V, Ptr->getType()); |
| 1453 | } |
| 1454 | |
| 1455 | llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF, |
| 1456 | llvm::Value *This, |
| 1457 | const ThisAdjustment &TA) { |
Timur Iskhodzhanov | 58b6db7 | 2013-11-06 06:24:31 +0000 | [diff] [blame] | 1458 | return performTypeAdjustment(CGF, This, TA.NonVirtual, |
| 1459 | TA.Virtual.Itanium.VCallOffsetOffset, |
Timur Iskhodzhanov | c70cc5d | 2013-10-30 11:55:43 +0000 | [diff] [blame] | 1460 | /*IsReturnAdjustment=*/false); |
| 1461 | } |
| 1462 | |
| 1463 | llvm::Value * |
| 1464 | ItaniumCXXABI::performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, |
| 1465 | const ReturnAdjustment &RA) { |
| 1466 | return performTypeAdjustment(CGF, Ret, RA.NonVirtual, |
| 1467 | RA.Virtual.Itanium.VBaseOffsetOffset, |
| 1468 | /*IsReturnAdjustment=*/true); |
| 1469 | } |
| 1470 | |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1471 | void ARMCXXABI::EmitReturnFromThunk(CodeGenFunction &CGF, |
| 1472 | RValue RV, QualType ResultType) { |
| 1473 | if (!isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) |
| 1474 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, RV, ResultType); |
| 1475 | |
| 1476 | // Destructor thunks in the ARM ABI have indeterminate results. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1477 | llvm::Type *T = |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 1478 | cast<llvm::PointerType>(CGF.ReturnValue->getType())->getElementType(); |
| 1479 | RValue Undef = RValue::get(llvm::UndefValue::get(T)); |
| 1480 | return ItaniumCXXABI::EmitReturnFromThunk(CGF, Undef, ResultType); |
| 1481 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1482 | |
| 1483 | /************************** Array allocation cookies **************************/ |
| 1484 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1485 | CharUnits ItaniumCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
| 1486 | // The array cookie is a size_t; pad that up to the element alignment. |
| 1487 | // The cookie is actually right-justified in that space. |
| 1488 | return std::max(CharUnits::fromQuantity(CGM.SizeSizeInBytes), |
| 1489 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | llvm::Value *ItaniumCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 1493 | llvm::Value *NewPtr, |
| 1494 | llvm::Value *NumElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1495 | const CXXNewExpr *expr, |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1496 | QualType ElementType) { |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1497 | assert(requiresArrayCookie(expr)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1498 | |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1499 | unsigned AS = NewPtr->getType()->getPointerAddressSpace(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1500 | |
John McCall | 9cb2cee | 2010-09-02 10:25:57 +0000 | [diff] [blame] | 1501 | ASTContext &Ctx = getContext(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1502 | QualType SizeTy = Ctx.getSizeType(); |
| 1503 | CharUnits SizeSize = Ctx.getTypeSizeInChars(SizeTy); |
| 1504 | |
| 1505 | // The size of the cookie. |
| 1506 | CharUnits CookieSize = |
| 1507 | std::max(SizeSize, Ctx.getTypeAlignInChars(ElementType)); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1508 | assert(CookieSize == getArrayCookieSizeImpl(ElementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Compute an offset to the cookie. |
| 1511 | llvm::Value *CookiePtr = NewPtr; |
| 1512 | CharUnits CookieOffset = CookieSize - SizeSize; |
| 1513 | if (!CookieOffset.isZero()) |
| 1514 | CookiePtr = CGF.Builder.CreateConstInBoundsGEP1_64(CookiePtr, |
| 1515 | CookieOffset.getQuantity()); |
| 1516 | |
| 1517 | // Write the number of elements into the appropriate slot. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1518 | llvm::Type *NumElementsTy = CGF.ConvertType(SizeTy)->getPointerTo(AS); |
| 1519 | llvm::Value *NumElementsPtr = |
| 1520 | CGF.Builder.CreateBitCast(CookiePtr, NumElementsTy); |
| 1521 | llvm::Instruction *SI = CGF.Builder.CreateStore(NumElements, NumElementsPtr); |
| 1522 | if (CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) && AS == 0 && |
| 1523 | expr->getOperatorNew()->isReplaceableGlobalAllocationFunction()) { |
| 1524 | // The store to the CookiePtr does not need to be instrumented. |
| 1525 | CGM.getSanitizerMetadata()->disableSanitizerForInstruction(SI); |
| 1526 | llvm::FunctionType *FTy = |
| 1527 | llvm::FunctionType::get(CGM.VoidTy, NumElementsTy, false); |
| 1528 | llvm::Constant *F = |
| 1529 | CGM.CreateRuntimeFunction(FTy, "__asan_poison_cxx_array_cookie"); |
| 1530 | CGF.Builder.CreateCall(F, NumElementsPtr); |
| 1531 | } |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1532 | |
| 1533 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1534 | // over the cookie completely. |
| 1535 | return CGF.Builder.CreateConstInBoundsGEP1_64(NewPtr, |
| 1536 | CookieSize.getQuantity()); |
| 1537 | } |
| 1538 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1539 | llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1540 | llvm::Value *allocPtr, |
| 1541 | CharUnits cookieSize) { |
| 1542 | // The element size is right-justified in the cookie. |
| 1543 | llvm::Value *numElementsPtr = allocPtr; |
| 1544 | CharUnits numElementsOffset = |
| 1545 | cookieSize - CharUnits::fromQuantity(CGF.SizeSizeInBytes); |
| 1546 | if (!numElementsOffset.isZero()) |
| 1547 | numElementsPtr = |
| 1548 | CGF.Builder.CreateConstInBoundsGEP1_64(numElementsPtr, |
| 1549 | numElementsOffset.getQuantity()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1550 | |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1551 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1552 | numElementsPtr = |
| 1553 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1554 | if (!CGM.getLangOpts().Sanitize.has(SanitizerKind::Address) || AS != 0) |
| 1555 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 1556 | // In asan mode emit a function call instead of a regular load and let the |
| 1557 | // run-time deal with it: if the shadow is properly poisoned return the |
| 1558 | // cookie, otherwise return 0 to avoid an infinite loop calling DTORs. |
| 1559 | // We can't simply ignore this load using nosanitize metadata because |
| 1560 | // the metadata may be lost. |
| 1561 | llvm::FunctionType *FTy = |
| 1562 | llvm::FunctionType::get(CGF.SizeTy, CGF.SizeTy->getPointerTo(0), false); |
| 1563 | llvm::Constant *F = |
| 1564 | CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie"); |
| 1565 | return CGF.Builder.CreateCall(F, numElementsPtr); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1568 | CharUnits ARMCXXABI::getArrayCookieSizeImpl(QualType elementType) { |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1569 | // ARM says that the cookie is always: |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1570 | // struct array_cookie { |
| 1571 | // std::size_t element_size; // element_size != 0 |
| 1572 | // std::size_t element_count; |
| 1573 | // }; |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1574 | // But the base ABI doesn't give anything an alignment greater than |
| 1575 | // 8, so we can dismiss this as typical ABI-author blindness to |
| 1576 | // actual language complexity and round up to the element alignment. |
| 1577 | return std::max(CharUnits::fromQuantity(2 * CGM.SizeSizeInBytes), |
| 1578 | CGM.getContext().getTypeAlignInChars(elementType)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | llvm::Value *ARMCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1582 | llvm::Value *newPtr, |
| 1583 | llvm::Value *numElements, |
John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1584 | const CXXNewExpr *expr, |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1585 | QualType elementType) { |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1586 | assert(requiresArrayCookie(expr)); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1587 | |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1588 | // NewPtr is a char*, but we generalize to arbitrary addrspaces. |
| 1589 | unsigned AS = newPtr->getType()->getPointerAddressSpace(); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1590 | |
| 1591 | // The cookie is always at the start of the buffer. |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1592 | llvm::Value *cookie = newPtr; |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1593 | |
| 1594 | // The first element is the element size. |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1595 | cookie = CGF.Builder.CreateBitCast(cookie, CGF.SizeTy->getPointerTo(AS)); |
| 1596 | llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, |
| 1597 | getContext().getTypeSizeInChars(elementType).getQuantity()); |
| 1598 | CGF.Builder.CreateStore(elementSize, cookie); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1599 | |
| 1600 | // The second element is the element count. |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1601 | cookie = CGF.Builder.CreateConstInBoundsGEP1_32(cookie, 1); |
| 1602 | CGF.Builder.CreateStore(numElements, cookie); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1603 | |
| 1604 | // Finally, compute a pointer to the actual data buffer by skipping |
| 1605 | // over the cookie completely. |
John McCall | f3bbb15 | 2013-01-25 23:36:19 +0000 | [diff] [blame] | 1606 | CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); |
| 1607 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 1608 | cookieSize.getQuantity()); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1611 | llvm::Value *ARMCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 1612 | llvm::Value *allocPtr, |
| 1613 | CharUnits cookieSize) { |
| 1614 | // The number of elements is at offset sizeof(size_t) relative to |
| 1615 | // the allocated pointer. |
| 1616 | llvm::Value *numElementsPtr |
| 1617 | = CGF.Builder.CreateConstInBoundsGEP1_64(allocPtr, CGF.SizeSizeInBytes); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1618 | |
Micah Villmow | 956a5a1 | 2012-10-25 15:39:14 +0000 | [diff] [blame] | 1619 | unsigned AS = allocPtr->getType()->getPointerAddressSpace(); |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 1620 | numElementsPtr = |
| 1621 | CGF.Builder.CreateBitCast(numElementsPtr, CGF.SizeTy->getPointerTo(AS)); |
| 1622 | return CGF.Builder.CreateLoad(numElementsPtr); |
John McCall | 1e7fe75 | 2010-09-02 09:58:18 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1625 | /*********************** Static local initialization **************************/ |
| 1626 | |
| 1627 | static llvm::Constant *getGuardAcquireFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1628 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1629 | // int __cxa_guard_acquire(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1630 | llvm::FunctionType *FTy = |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1631 | llvm::FunctionType::get(CGM.getTypes().ConvertType(CGM.getContext().IntTy), |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1632 | GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1633 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_acquire", |
Bill Wendling | c4c62fd | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1634 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1635 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1636 | llvm::Attribute::NoUnwind)); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | static llvm::Constant *getGuardReleaseFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1640 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1641 | // void __cxa_guard_release(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1642 | llvm::FunctionType *FTy = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1643 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1644 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_release", |
Bill Wendling | c4c62fd | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1645 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1646 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1647 | llvm::Attribute::NoUnwind)); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | static llvm::Constant *getGuardAbortFn(CodeGenModule &CGM, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1651 | llvm::PointerType *GuardPtrTy) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1652 | // void __cxa_guard_abort(__guard *guard_object); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1653 | llvm::FunctionType *FTy = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1654 | llvm::FunctionType::get(CGM.VoidTy, GuardPtrTy, /*isVarArg=*/false); |
Nick Lewycky | e76872e | 2012-02-13 23:45:02 +0000 | [diff] [blame] | 1655 | return CGM.CreateRuntimeFunction(FTy, "__cxa_guard_abort", |
Bill Wendling | c4c62fd | 2013-01-31 00:30:05 +0000 | [diff] [blame] | 1656 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1657 | llvm::AttributeSet::FunctionIndex, |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1658 | llvm::Attribute::NoUnwind)); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | namespace { |
| 1662 | struct CallGuardAbort : EHScopeStack::Cleanup { |
| 1663 | llvm::GlobalVariable *Guard; |
Chandler Carruth | 0f30a12 | 2012-03-30 19:44:53 +0000 | [diff] [blame] | 1664 | CallGuardAbort(llvm::GlobalVariable *Guard) : Guard(Guard) {} |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1665 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1666 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1667 | CGF.EmitNounwindRuntimeCall(getGuardAbortFn(CGF.CGM, Guard->getType()), |
| 1668 | Guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1669 | } |
| 1670 | }; |
| 1671 | } |
| 1672 | |
| 1673 | /// The ARM code here follows the Itanium code closely enough that we |
| 1674 | /// just special-case it at particular places. |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1675 | void ItaniumCXXABI::EmitGuardedInit(CodeGenFunction &CGF, |
| 1676 | const VarDecl &D, |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1677 | llvm::GlobalVariable *var, |
| 1678 | bool shouldPerformInit) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1679 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1680 | |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1681 | // We only need to use thread-safe statics for local non-TLS variables; |
John McCall | 3030eb8 | 2010-11-06 09:44:32 +0000 | [diff] [blame] | 1682 | // global initialization is always single-threaded. |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1683 | bool threadsafe = getContext().getLangOpts().ThreadsafeStatics && |
| 1684 | D.isLocalVarDecl() && !D.getTLSKind(); |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1685 | |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1686 | // If we have a global variable with internal linkage and thread-safe statics |
| 1687 | // are disabled, we can just let the guard variable be of type i8. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1688 | bool useInt8GuardVariable = !threadsafe && var->hasInternalLinkage(); |
| 1689 | |
| 1690 | llvm::IntegerType *guardTy; |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1691 | if (useInt8GuardVariable) { |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1692 | guardTy = CGF.Int8Ty; |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1693 | } else { |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 1694 | // Guard variables are 64 bits in the generic ABI and size width on ARM |
| 1695 | // (i.e. 32-bit on AArch32, 64-bit on AArch64). |
Mark Seaborn | 21fe450 | 2013-07-24 16:25:13 +0000 | [diff] [blame] | 1696 | guardTy = (UseARMGuardVarABI ? CGF.SizeTy : CGF.Int64Ty); |
Anders Carlsson | 173d512 | 2011-04-27 04:37:08 +0000 | [diff] [blame] | 1697 | } |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1698 | llvm::PointerType *guardPtrTy = guardTy->getPointerTo(); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1699 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1700 | // Create the guard variable if we don't already have it (as we |
| 1701 | // might if we're double-emitting this function body). |
| 1702 | llvm::GlobalVariable *guard = CGM.getStaticLocalDeclGuardAddress(&D); |
| 1703 | if (!guard) { |
| 1704 | // Mangle the name for the guard. |
| 1705 | SmallString<256> guardName; |
| 1706 | { |
| 1707 | llvm::raw_svector_ostream out(guardName); |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 1708 | getMangleContext().mangleStaticGuardVariable(&D, out); |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1709 | out.flush(); |
| 1710 | } |
John McCall | 112c967 | 2010-11-02 21:04:24 +0000 | [diff] [blame] | 1711 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1712 | // Create the guard variable with a zero-initializer. |
| 1713 | // Just absorb linkage and visibility from the guarded variable. |
| 1714 | guard = new llvm::GlobalVariable(CGM.getModule(), guardTy, |
| 1715 | false, var->getLinkage(), |
| 1716 | llvm::ConstantInt::get(guardTy, 0), |
| 1717 | guardName.str()); |
| 1718 | guard->setVisibility(var->getVisibility()); |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1719 | // If the variable is thread-local, so is its guard variable. |
| 1720 | guard->setThreadLocalMode(var->getThreadLocalMode()); |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1721 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1722 | // The ABI says: It is suggested that it be emitted in the same COMDAT group |
| 1723 | // as the associated data object |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1724 | llvm::Comdat *C = var->getComdat(); |
| 1725 | if (!D.isLocalVarDecl() && C) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1726 | guard->setComdat(C); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1727 | CGF.CurFn->setComdat(C); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 1728 | } else if (CGM.supportsCOMDAT() && guard->isWeakForLinker()) { |
| 1729 | guard->setComdat(CGM.getModule().getOrInsertComdat(guard->getName())); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1730 | } |
| 1731 | |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1732 | CGM.setStaticLocalDeclGuardAddress(&D, guard); |
| 1733 | } |
John McCall | 49d26d2 | 2012-03-30 07:09:50 +0000 | [diff] [blame] | 1734 | |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1735 | // Test whether the variable has completed initialization. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1736 | // |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1737 | // Itanium C++ ABI 3.3.2: |
| 1738 | // The following is pseudo-code showing how these functions can be used: |
| 1739 | // if (obj_guard.first_byte == 0) { |
| 1740 | // if ( __cxa_guard_acquire (&obj_guard) ) { |
| 1741 | // try { |
| 1742 | // ... initialize the object ...; |
| 1743 | // } catch (...) { |
| 1744 | // __cxa_guard_abort (&obj_guard); |
| 1745 | // throw; |
| 1746 | // } |
| 1747 | // ... queue object destructor with __cxa_atexit() ...; |
| 1748 | // __cxa_guard_release (&obj_guard); |
| 1749 | // } |
| 1750 | // } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1751 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1752 | // Load the first byte of the guard variable. |
| 1753 | llvm::LoadInst *LI = |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1754 | Builder.CreateLoad(Builder.CreateBitCast(guard, CGM.Int8PtrTy)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1755 | LI->setAlignment(1); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1756 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1757 | // Itanium ABI: |
| 1758 | // An implementation supporting thread-safety on multiprocessor |
| 1759 | // systems must also guarantee that references to the initialized |
| 1760 | // object do not occur before the load of the initialization flag. |
| 1761 | // |
| 1762 | // In LLVM, we do this by marking the load Acquire. |
| 1763 | if (threadsafe) |
| 1764 | LI->setAtomic(llvm::Acquire); |
Eli Friedman | eb43f4a | 2011-09-13 22:21:56 +0000 | [diff] [blame] | 1765 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1766 | // For ARM, we should only check the first bit, rather than the entire byte: |
| 1767 | // |
| 1768 | // ARM C++ ABI 3.2.3.1: |
| 1769 | // To support the potential use of initialization guard variables |
| 1770 | // as semaphores that are the target of ARM SWP and LDREX/STREX |
| 1771 | // synchronizing instructions we define a static initialization |
| 1772 | // guard variable to be a 4-byte aligned, 4-byte word with the |
| 1773 | // following inline access protocol. |
| 1774 | // #define INITIALIZED 1 |
| 1775 | // if ((obj_guard & INITIALIZED) != INITIALIZED) { |
| 1776 | // if (__cxa_guard_acquire(&obj_guard)) |
| 1777 | // ... |
| 1778 | // } |
| 1779 | // |
| 1780 | // and similarly for ARM64: |
| 1781 | // |
| 1782 | // ARM64 C++ ABI 3.2.2: |
| 1783 | // This ABI instead only specifies the value bit 0 of the static guard |
| 1784 | // variable; all other bits are platform defined. Bit 0 shall be 0 when the |
| 1785 | // variable is not initialized and 1 when it is. |
| 1786 | llvm::Value *V = |
| 1787 | (UseARMGuardVarABI && !useInt8GuardVariable) |
| 1788 | ? Builder.CreateAnd(LI, llvm::ConstantInt::get(CGM.Int8Ty, 1)) |
| 1789 | : LI; |
| 1790 | llvm::Value *isInitialized = Builder.CreateIsNull(V, "guard.uninitialized"); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1791 | |
| 1792 | llvm::BasicBlock *InitCheckBlock = CGF.createBasicBlock("init.check"); |
| 1793 | llvm::BasicBlock *EndBlock = CGF.createBasicBlock("init.end"); |
| 1794 | |
| 1795 | // Check if the first byte of the guard variable is zero. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1796 | Builder.CreateCondBr(isInitialized, InitCheckBlock, EndBlock); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1797 | |
| 1798 | CGF.EmitBlock(InitCheckBlock); |
| 1799 | |
| 1800 | // Variables used when coping with thread-safe statics and exceptions. |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1801 | if (threadsafe) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1802 | // Call __cxa_guard_acquire. |
| 1803 | llvm::Value *V |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1804 | = CGF.EmitNounwindRuntimeCall(getGuardAcquireFn(CGM, guardPtrTy), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1805 | |
| 1806 | llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); |
| 1807 | |
| 1808 | Builder.CreateCondBr(Builder.CreateIsNotNull(V, "tobool"), |
| 1809 | InitBlock, EndBlock); |
| 1810 | |
| 1811 | // Call __cxa_guard_abort along the exceptional edge. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1812 | CGF.EHStack.pushCleanup<CallGuardAbort>(EHCleanup, guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1813 | |
| 1814 | CGF.EmitBlock(InitBlock); |
| 1815 | } |
| 1816 | |
| 1817 | // Emit the initializer and add a global destructor if appropriate. |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1818 | CGF.EmitCXXGlobalVarDeclInit(D, var, shouldPerformInit); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1819 | |
John McCall | 0502a22 | 2011-06-17 07:33:57 +0000 | [diff] [blame] | 1820 | if (threadsafe) { |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1821 | // Pop the guard-abort cleanup if we pushed one. |
| 1822 | CGF.PopCleanupBlock(); |
| 1823 | |
| 1824 | // Call __cxa_guard_release. This cannot throw. |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1825 | CGF.EmitNounwindRuntimeCall(getGuardReleaseFn(CGM, guardPtrTy), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1826 | } else { |
John McCall | 355bba7 | 2012-03-30 21:00:39 +0000 | [diff] [blame] | 1827 | Builder.CreateStore(llvm::ConstantInt::get(guardTy, 1), guard); |
John McCall | 5cd91b5 | 2010-09-08 01:44:27 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | CGF.EmitBlock(EndBlock); |
| 1831 | } |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1832 | |
| 1833 | /// Register a global destructor using __cxa_atexit. |
| 1834 | static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF, |
| 1835 | llvm::Constant *dtor, |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1836 | llvm::Constant *addr, |
| 1837 | bool TLS) { |
Bill Wendling | 4e3b54b | 2013-05-02 19:18:03 +0000 | [diff] [blame] | 1838 | const char *Name = "__cxa_atexit"; |
| 1839 | if (TLS) { |
| 1840 | const llvm::Triple &T = CGF.getTarget().getTriple(); |
| 1841 | Name = T.isMacOSX() ? "_tlv_atexit" : "__cxa_thread_atexit"; |
| 1842 | } |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1843 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1844 | // We're assuming that the destructor function is something we can |
| 1845 | // reasonably call with the default CC. Go ahead and cast it to the |
| 1846 | // right prototype. |
| 1847 | llvm::Type *dtorTy = |
| 1848 | llvm::FunctionType::get(CGF.VoidTy, CGF.Int8PtrTy, false)->getPointerTo(); |
| 1849 | |
| 1850 | // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d); |
| 1851 | llvm::Type *paramTys[] = { dtorTy, CGF.Int8PtrTy, CGF.Int8PtrTy }; |
| 1852 | llvm::FunctionType *atexitTy = |
| 1853 | llvm::FunctionType::get(CGF.IntTy, paramTys, false); |
| 1854 | |
| 1855 | // Fetch the actual function. |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1856 | llvm::Constant *atexit = CGF.CGM.CreateRuntimeFunction(atexitTy, Name); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1857 | if (llvm::Function *fn = dyn_cast<llvm::Function>(atexit)) |
| 1858 | fn->setDoesNotThrow(); |
| 1859 | |
| 1860 | // Create a variable that binds the atexit to this shared object. |
| 1861 | llvm::Constant *handle = |
| 1862 | CGF.CGM.CreateRuntimeVariable(CGF.Int8Ty, "__dso_handle"); |
| 1863 | |
| 1864 | llvm::Value *args[] = { |
| 1865 | llvm::ConstantExpr::getBitCast(dtor, dtorTy), |
| 1866 | llvm::ConstantExpr::getBitCast(addr, CGF.Int8PtrTy), |
| 1867 | handle |
| 1868 | }; |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1869 | CGF.EmitNounwindRuntimeCall(atexit, args); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /// Register a global destructor as best as we know how. |
| 1873 | void ItaniumCXXABI::registerGlobalDtor(CodeGenFunction &CGF, |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1874 | const VarDecl &D, |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1875 | llvm::Constant *dtor, |
| 1876 | llvm::Constant *addr) { |
| 1877 | // Use __cxa_atexit if available. |
Richard Smith | 04e5176 | 2013-04-14 23:01:42 +0000 | [diff] [blame] | 1878 | if (CGM.getCodeGenOpts().CXAAtExit) |
| 1879 | return emitGlobalDtorWithCXAAtExit(CGF, dtor, addr, D.getTLSKind()); |
| 1880 | |
| 1881 | if (D.getTLSKind()) |
| 1882 | CGM.ErrorUnsupported(&D, "non-trivial TLS destruction"); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1883 | |
| 1884 | // In Apple kexts, we want to add a global destructor entry. |
| 1885 | // FIXME: shouldn't this be guarded by some variable? |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1886 | if (CGM.getLangOpts().AppleKext) { |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1887 | // Generate a global destructor entry. |
| 1888 | return CGM.AddCXXDtorEntry(dtor, addr); |
| 1889 | } |
| 1890 | |
David Blaikie | c7971a9 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 1891 | CGF.registerGlobalDtorWithAtExit(D, dtor, addr); |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 1892 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1893 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1894 | static bool isThreadWrapperReplaceable(const VarDecl *VD, |
| 1895 | CodeGen::CodeGenModule &CGM) { |
| 1896 | assert(!VD->isStaticLocal() && "static local VarDecls don't need wrappers!"); |
| 1897 | // OS X prefers to have references to thread local variables to go through |
| 1898 | // the thread wrapper instead of directly referencing the backing variable. |
| 1899 | return VD->getTLSKind() == VarDecl::TLS_Dynamic && |
| 1900 | CGM.getTarget().getTriple().isMacOSX(); |
| 1901 | } |
| 1902 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1903 | /// Get the appropriate linkage for the wrapper function. This is essentially |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1904 | /// the weak form of the variable's linkage; every translation unit which needs |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1905 | /// the wrapper emits a copy, and we want the linker to merge them. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1906 | static llvm::GlobalValue::LinkageTypes |
| 1907 | getThreadLocalWrapperLinkage(const VarDecl *VD, CodeGen::CodeGenModule &CGM) { |
| 1908 | llvm::GlobalValue::LinkageTypes VarLinkage = |
| 1909 | CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false); |
| 1910 | |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1911 | // For internal linkage variables, we don't need an external or weak wrapper. |
| 1912 | if (llvm::GlobalValue::isLocalLinkage(VarLinkage)) |
| 1913 | return VarLinkage; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1914 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1915 | // If the thread wrapper is replaceable, give it appropriate linkage. |
| 1916 | if (isThreadWrapperReplaceable(VD, CGM)) { |
| 1917 | if (llvm::GlobalVariable::isLinkOnceLinkage(VarLinkage) || |
| 1918 | llvm::GlobalVariable::isWeakODRLinkage(VarLinkage)) |
| 1919 | return llvm::GlobalVariable::WeakAnyLinkage; |
| 1920 | return VarLinkage; |
| 1921 | } |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1922 | return llvm::GlobalValue::WeakODRLinkage; |
| 1923 | } |
| 1924 | |
| 1925 | llvm::Function * |
| 1926 | ItaniumCXXABI::getOrCreateThreadLocalWrapper(const VarDecl *VD, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1927 | llvm::Value *Val) { |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1928 | // Mangle the name for the thread_local wrapper function. |
| 1929 | SmallString<256> WrapperName; |
| 1930 | { |
| 1931 | llvm::raw_svector_ostream Out(WrapperName); |
| 1932 | getMangleContext().mangleItaniumThreadLocalWrapper(VD, Out); |
| 1933 | Out.flush(); |
| 1934 | } |
| 1935 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1936 | if (llvm::Value *V = CGM.getModule().getNamedValue(WrapperName)) |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1937 | return cast<llvm::Function>(V); |
| 1938 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1939 | llvm::Type *RetTy = Val->getType(); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1940 | if (VD->getType()->isReferenceType()) |
| 1941 | RetTy = RetTy->getPointerElementType(); |
| 1942 | |
| 1943 | llvm::FunctionType *FnTy = llvm::FunctionType::get(RetTy, false); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1944 | llvm::Function *Wrapper = |
| 1945 | llvm::Function::Create(FnTy, getThreadLocalWrapperLinkage(VD, CGM), |
| 1946 | WrapperName.str(), &CGM.getModule()); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1947 | // Always resolve references to the wrapper at link time. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1948 | if (!Wrapper->hasLocalLinkage() && !isThreadWrapperReplaceable(VD, CGM)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1949 | Wrapper->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1950 | return Wrapper; |
| 1951 | } |
| 1952 | |
| 1953 | void ItaniumCXXABI::EmitThreadLocalInitFuncs( |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1954 | CodeGenModule &CGM, |
| 1955 | ArrayRef<std::pair<const VarDecl *, llvm::GlobalVariable *>> |
| 1956 | CXXThreadLocals, ArrayRef<llvm::Function *> CXXThreadLocalInits, |
| 1957 | ArrayRef<llvm::GlobalVariable *> CXXThreadLocalInitVars) { |
| 1958 | llvm::Function *InitFunc = nullptr; |
| 1959 | if (!CXXThreadLocalInits.empty()) { |
| 1960 | // Generate a guarded initialization function. |
| 1961 | llvm::FunctionType *FTy = |
| 1962 | llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); |
| 1963 | InitFunc = CGM.CreateGlobalInitOrDestructFunction(FTy, "__tls_init", |
| 1964 | SourceLocation(), |
| 1965 | /*TLS=*/true); |
| 1966 | llvm::GlobalVariable *Guard = new llvm::GlobalVariable( |
| 1967 | CGM.getModule(), CGM.Int8Ty, /*isConstant=*/false, |
| 1968 | llvm::GlobalVariable::InternalLinkage, |
| 1969 | llvm::ConstantInt::get(CGM.Int8Ty, 0), "__tls_guard"); |
| 1970 | Guard->setThreadLocal(true); |
| 1971 | CodeGenFunction(CGM) |
| 1972 | .GenerateCXXGlobalInitFunc(InitFunc, CXXThreadLocalInits, Guard); |
| 1973 | } |
| 1974 | for (unsigned I = 0, N = CXXThreadLocals.size(); I != N; ++I) { |
| 1975 | const VarDecl *VD = CXXThreadLocals[I].first; |
| 1976 | llvm::GlobalVariable *Var = CXXThreadLocals[I].second; |
| 1977 | |
| 1978 | // Some targets require that all access to thread local variables go through |
| 1979 | // the thread wrapper. This means that we cannot attempt to create a thread |
| 1980 | // wrapper or a thread helper. |
| 1981 | if (isThreadWrapperReplaceable(VD, CGM) && !VD->hasDefinition()) |
| 1982 | continue; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1983 | |
| 1984 | // Mangle the name for the thread_local initialization function. |
| 1985 | SmallString<256> InitFnName; |
| 1986 | { |
| 1987 | llvm::raw_svector_ostream Out(InitFnName); |
| 1988 | getMangleContext().mangleItaniumThreadLocalInit(VD, Out); |
| 1989 | Out.flush(); |
| 1990 | } |
| 1991 | |
| 1992 | // If we have a definition for the variable, emit the initialization |
| 1993 | // function as an alias to the global Init function (if any). Otherwise, |
| 1994 | // produce a declaration of the initialization function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1995 | llvm::GlobalValue *Init = nullptr; |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 1996 | bool InitIsInitFunc = false; |
| 1997 | if (VD->hasDefinition()) { |
| 1998 | InitIsInitFunc = true; |
| 1999 | if (InitFunc) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2000 | Init = llvm::GlobalAlias::create(Var->getLinkage(), InitFnName.str(), |
| 2001 | InitFunc); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2002 | } else { |
| 2003 | // Emit a weak global function referring to the initialization function. |
| 2004 | // This function will not exist if the TU defining the thread_local |
| 2005 | // variable in question does not need any dynamic initialization for |
| 2006 | // its thread_local variables. |
| 2007 | llvm::FunctionType *FnTy = llvm::FunctionType::get(CGM.VoidTy, false); |
| 2008 | Init = llvm::Function::Create( |
| 2009 | FnTy, llvm::GlobalVariable::ExternalWeakLinkage, InitFnName.str(), |
| 2010 | &CGM.getModule()); |
| 2011 | } |
| 2012 | |
| 2013 | if (Init) |
| 2014 | Init->setVisibility(Var->getVisibility()); |
| 2015 | |
| 2016 | llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Var); |
| 2017 | llvm::LLVMContext &Context = CGM.getModule().getContext(); |
| 2018 | llvm::BasicBlock *Entry = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 2019 | CGBuilderTy Builder(Entry); |
| 2020 | if (InitIsInitFunc) { |
| 2021 | if (Init) |
| 2022 | Builder.CreateCall(Init); |
| 2023 | } else { |
| 2024 | // Don't know whether we have an init function. Call it if it exists. |
| 2025 | llvm::Value *Have = Builder.CreateIsNotNull(Init); |
| 2026 | llvm::BasicBlock *InitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 2027 | llvm::BasicBlock *ExitBB = llvm::BasicBlock::Create(Context, "", Wrapper); |
| 2028 | Builder.CreateCondBr(Have, InitBB, ExitBB); |
| 2029 | |
| 2030 | Builder.SetInsertPoint(InitBB); |
| 2031 | Builder.CreateCall(Init); |
| 2032 | Builder.CreateBr(ExitBB); |
| 2033 | |
| 2034 | Builder.SetInsertPoint(ExitBB); |
| 2035 | } |
| 2036 | |
| 2037 | // For a reference, the result of the wrapper function is a pointer to |
| 2038 | // the referenced object. |
| 2039 | llvm::Value *Val = Var; |
| 2040 | if (VD->getType()->isReferenceType()) { |
| 2041 | llvm::LoadInst *LI = Builder.CreateLoad(Val); |
| 2042 | LI->setAlignment(CGM.getContext().getDeclAlign(VD).getQuantity()); |
| 2043 | Val = LI; |
| 2044 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2045 | if (Val->getType() != Wrapper->getReturnType()) |
| 2046 | Val = Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 2047 | Val, Wrapper->getReturnType(), ""); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2048 | Builder.CreateRet(Val); |
| 2049 | } |
| 2050 | } |
| 2051 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2052 | LValue ItaniumCXXABI::EmitThreadLocalVarDeclLValue(CodeGenFunction &CGF, |
| 2053 | const VarDecl *VD, |
| 2054 | QualType LValType) { |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2055 | QualType T = VD->getType(); |
| 2056 | llvm::Type *Ty = CGF.getTypes().ConvertTypeForMem(T); |
| 2057 | llvm::Value *Val = CGF.CGM.GetAddrOfGlobalVar(VD, Ty); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2058 | llvm::Function *Wrapper = getOrCreateThreadLocalWrapper(VD, Val); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2059 | |
| 2060 | Val = CGF.Builder.CreateCall(Wrapper); |
| 2061 | |
| 2062 | LValue LV; |
| 2063 | if (VD->getType()->isReferenceType()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2064 | LV = CGF.MakeNaturalAlignAddrLValue(Val, LValType); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2065 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2066 | LV = CGF.MakeAddrLValue(Val, LValType, CGF.getContext().getDeclAlign(VD)); |
Richard Smith | b80a16e | 2013-04-19 16:42:07 +0000 | [diff] [blame] | 2067 | // FIXME: need setObjCGCLValueClass? |
| 2068 | return LV; |
| 2069 | } |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 2070 | |
| 2071 | /// Return whether the given global decl needs a VTT parameter, which it does |
| 2072 | /// if it's a base constructor or destructor with virtual bases. |
| 2073 | bool ItaniumCXXABI::NeedsVTTParameter(GlobalDecl GD) { |
| 2074 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 2075 | |
| 2076 | // We don't have any virtual bases, just return early. |
| 2077 | if (!MD->getParent()->getNumVBases()) |
| 2078 | return false; |
| 2079 | |
| 2080 | // Check if we have a base constructor. |
| 2081 | if (isa<CXXConstructorDecl>(MD) && GD.getCtorType() == Ctor_Base) |
| 2082 | return true; |
| 2083 | |
| 2084 | // Check if we have a base destructor. |
| 2085 | if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) |
| 2086 | return true; |
| 2087 | |
| 2088 | return false; |
| 2089 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2090 | |
| 2091 | namespace { |
| 2092 | class ItaniumRTTIBuilder { |
| 2093 | CodeGenModule &CGM; // Per-module state. |
| 2094 | llvm::LLVMContext &VMContext; |
| 2095 | const ItaniumCXXABI &CXXABI; // Per-module state. |
| 2096 | |
| 2097 | /// Fields - The fields of the RTTI descriptor currently being built. |
| 2098 | SmallVector<llvm::Constant *, 16> Fields; |
| 2099 | |
| 2100 | /// GetAddrOfTypeName - Returns the mangled type name of the given type. |
| 2101 | llvm::GlobalVariable * |
| 2102 | GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage); |
| 2103 | |
| 2104 | /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI |
| 2105 | /// descriptor of the given type. |
| 2106 | llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); |
| 2107 | |
| 2108 | /// BuildVTablePointer - Build the vtable pointer for the given type. |
| 2109 | void BuildVTablePointer(const Type *Ty); |
| 2110 | |
| 2111 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
| 2112 | /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. |
| 2113 | void BuildSIClassTypeInfo(const CXXRecordDecl *RD); |
| 2114 | |
| 2115 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 2116 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 2117 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 2118 | void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); |
| 2119 | |
| 2120 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used |
| 2121 | /// for pointer types. |
| 2122 | void BuildPointerTypeInfo(QualType PointeeTy); |
| 2123 | |
| 2124 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of |
| 2125 | /// type_info for an object type. |
| 2126 | void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); |
| 2127 | |
| 2128 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 2129 | /// struct, used for member pointer types. |
| 2130 | void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); |
| 2131 | |
| 2132 | public: |
| 2133 | ItaniumRTTIBuilder(const ItaniumCXXABI &ABI) |
| 2134 | : CGM(ABI.CGM), VMContext(CGM.getModule().getContext()), CXXABI(ABI) {} |
| 2135 | |
| 2136 | // Pointer type info flags. |
| 2137 | enum { |
| 2138 | /// PTI_Const - Type has const qualifier. |
| 2139 | PTI_Const = 0x1, |
| 2140 | |
| 2141 | /// PTI_Volatile - Type has volatile qualifier. |
| 2142 | PTI_Volatile = 0x2, |
| 2143 | |
| 2144 | /// PTI_Restrict - Type has restrict qualifier. |
| 2145 | PTI_Restrict = 0x4, |
| 2146 | |
| 2147 | /// PTI_Incomplete - Type is incomplete. |
| 2148 | PTI_Incomplete = 0x8, |
| 2149 | |
| 2150 | /// PTI_ContainingClassIncomplete - Containing class is incomplete. |
| 2151 | /// (in pointer to member). |
| 2152 | PTI_ContainingClassIncomplete = 0x10 |
| 2153 | }; |
| 2154 | |
| 2155 | // VMI type info flags. |
| 2156 | enum { |
| 2157 | /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. |
| 2158 | VMI_NonDiamondRepeat = 0x1, |
| 2159 | |
| 2160 | /// VMI_DiamondShaped - Class is diamond shaped. |
| 2161 | VMI_DiamondShaped = 0x2 |
| 2162 | }; |
| 2163 | |
| 2164 | // Base class type info flags. |
| 2165 | enum { |
| 2166 | /// BCTI_Virtual - Base class is virtual. |
| 2167 | BCTI_Virtual = 0x1, |
| 2168 | |
| 2169 | /// BCTI_Public - Base class is public. |
| 2170 | BCTI_Public = 0x2 |
| 2171 | }; |
| 2172 | |
| 2173 | /// BuildTypeInfo - Build the RTTI type info struct for the given type. |
| 2174 | /// |
| 2175 | /// \param Force - true to force the creation of this RTTI value |
| 2176 | llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false); |
| 2177 | }; |
| 2178 | } |
| 2179 | |
| 2180 | llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName( |
| 2181 | QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage) { |
| 2182 | SmallString<256> OutName; |
| 2183 | llvm::raw_svector_ostream Out(OutName); |
| 2184 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out); |
| 2185 | Out.flush(); |
| 2186 | StringRef Name = OutName.str(); |
| 2187 | |
| 2188 | // We know that the mangled name of the type starts at index 4 of the |
| 2189 | // mangled name of the typename, so we can just index into it in order to |
| 2190 | // get the mangled name of the type. |
| 2191 | llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, |
| 2192 | Name.substr(4)); |
| 2193 | |
| 2194 | llvm::GlobalVariable *GV = |
| 2195 | CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage); |
| 2196 | |
| 2197 | GV->setInitializer(Init); |
| 2198 | |
| 2199 | return GV; |
| 2200 | } |
| 2201 | |
| 2202 | llvm::Constant * |
| 2203 | ItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { |
| 2204 | // Mangle the RTTI name. |
| 2205 | SmallString<256> OutName; |
| 2206 | llvm::raw_svector_ostream Out(OutName); |
| 2207 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); |
| 2208 | Out.flush(); |
| 2209 | StringRef Name = OutName.str(); |
| 2210 | |
| 2211 | // Look for an existing global. |
| 2212 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); |
| 2213 | |
| 2214 | if (!GV) { |
| 2215 | // Create a new global variable. |
| 2216 | GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, |
| 2217 | /*Constant=*/true, |
| 2218 | llvm::GlobalValue::ExternalLinkage, nullptr, |
| 2219 | Name); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2220 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2221 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2222 | if (RD->hasAttr<DLLImportAttr>()) |
| 2223 | GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); |
| 2224 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 2228 | } |
| 2229 | |
| 2230 | /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type |
| 2231 | /// info for that type is defined in the standard library. |
| 2232 | static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { |
| 2233 | // Itanium C++ ABI 2.9.2: |
| 2234 | // Basic type information (e.g. for "int", "bool", etc.) will be kept in |
| 2235 | // the run-time support library. Specifically, the run-time support |
| 2236 | // library should contain type_info objects for the types X, X* and |
| 2237 | // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char, |
| 2238 | // unsigned char, signed char, short, unsigned short, int, unsigned int, |
| 2239 | // long, unsigned long, long long, unsigned long long, float, double, |
| 2240 | // long double, char16_t, char32_t, and the IEEE 754r decimal and |
| 2241 | // half-precision floating point types. |
| 2242 | switch (Ty->getKind()) { |
| 2243 | case BuiltinType::Void: |
| 2244 | case BuiltinType::NullPtr: |
| 2245 | case BuiltinType::Bool: |
| 2246 | case BuiltinType::WChar_S: |
| 2247 | case BuiltinType::WChar_U: |
| 2248 | case BuiltinType::Char_U: |
| 2249 | case BuiltinType::Char_S: |
| 2250 | case BuiltinType::UChar: |
| 2251 | case BuiltinType::SChar: |
| 2252 | case BuiltinType::Short: |
| 2253 | case BuiltinType::UShort: |
| 2254 | case BuiltinType::Int: |
| 2255 | case BuiltinType::UInt: |
| 2256 | case BuiltinType::Long: |
| 2257 | case BuiltinType::ULong: |
| 2258 | case BuiltinType::LongLong: |
| 2259 | case BuiltinType::ULongLong: |
| 2260 | case BuiltinType::Half: |
| 2261 | case BuiltinType::Float: |
| 2262 | case BuiltinType::Double: |
| 2263 | case BuiltinType::LongDouble: |
| 2264 | case BuiltinType::Char16: |
| 2265 | case BuiltinType::Char32: |
| 2266 | case BuiltinType::Int128: |
| 2267 | case BuiltinType::UInt128: |
| 2268 | case BuiltinType::OCLImage1d: |
| 2269 | case BuiltinType::OCLImage1dArray: |
| 2270 | case BuiltinType::OCLImage1dBuffer: |
| 2271 | case BuiltinType::OCLImage2d: |
| 2272 | case BuiltinType::OCLImage2dArray: |
| 2273 | case BuiltinType::OCLImage3d: |
| 2274 | case BuiltinType::OCLSampler: |
| 2275 | case BuiltinType::OCLEvent: |
| 2276 | return true; |
| 2277 | |
| 2278 | case BuiltinType::Dependent: |
| 2279 | #define BUILTIN_TYPE(Id, SingletonId) |
| 2280 | #define PLACEHOLDER_TYPE(Id, SingletonId) \ |
| 2281 | case BuiltinType::Id: |
| 2282 | #include "clang/AST/BuiltinTypes.def" |
| 2283 | llvm_unreachable("asking for RRTI for a placeholder type!"); |
| 2284 | |
| 2285 | case BuiltinType::ObjCId: |
| 2286 | case BuiltinType::ObjCClass: |
| 2287 | case BuiltinType::ObjCSel: |
| 2288 | llvm_unreachable("FIXME: Objective-C types are unsupported!"); |
| 2289 | } |
| 2290 | |
| 2291 | llvm_unreachable("Invalid BuiltinType Kind!"); |
| 2292 | } |
| 2293 | |
| 2294 | static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { |
| 2295 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2296 | const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); |
| 2297 | if (!BuiltinTy) |
| 2298 | return false; |
| 2299 | |
| 2300 | // Check the qualifiers. |
| 2301 | Qualifiers Quals = PointeeTy.getQualifiers(); |
| 2302 | Quals.removeConst(); |
| 2303 | |
| 2304 | if (!Quals.empty()) |
| 2305 | return false; |
| 2306 | |
| 2307 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 2308 | } |
| 2309 | |
| 2310 | /// IsStandardLibraryRTTIDescriptor - Returns whether the type |
| 2311 | /// information for the given type exists in the standard library. |
| 2312 | static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { |
| 2313 | // Type info for builtin types is defined in the standard library. |
| 2314 | if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) |
| 2315 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 2316 | |
| 2317 | // Type info for some pointer types to builtin types is defined in the |
| 2318 | // standard library. |
| 2319 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 2320 | return TypeInfoIsInStandardLibrary(PointerTy); |
| 2321 | |
| 2322 | return false; |
| 2323 | } |
| 2324 | |
| 2325 | /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for |
| 2326 | /// the given type exists somewhere else, and that we should not emit the type |
| 2327 | /// information in this translation unit. Assumes that it is not a |
| 2328 | /// standard-library type. |
| 2329 | static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, |
| 2330 | QualType Ty) { |
| 2331 | ASTContext &Context = CGM.getContext(); |
| 2332 | |
| 2333 | // If RTTI is disabled, assume it might be disabled in the |
| 2334 | // translation unit that defines any potential key function, too. |
| 2335 | if (!Context.getLangOpts().RTTI) return false; |
| 2336 | |
| 2337 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2338 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 2339 | if (!RD->hasDefinition()) |
| 2340 | return false; |
| 2341 | |
| 2342 | if (!RD->isDynamicClass()) |
| 2343 | return false; |
| 2344 | |
| 2345 | // FIXME: this may need to be reconsidered if the key function |
| 2346 | // changes. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2347 | if (CGM.getVTables().isVTableExternal(RD)) |
| 2348 | return true; |
| 2349 | |
| 2350 | if (RD->hasAttr<DLLImportAttr>()) |
| 2351 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
| 2357 | /// IsIncompleteClassType - Returns whether the given record type is incomplete. |
| 2358 | static bool IsIncompleteClassType(const RecordType *RecordTy) { |
| 2359 | return !RecordTy->getDecl()->isCompleteDefinition(); |
| 2360 | } |
| 2361 | |
| 2362 | /// ContainsIncompleteClassType - Returns whether the given type contains an |
| 2363 | /// incomplete class type. This is true if |
| 2364 | /// |
| 2365 | /// * The given type is an incomplete class type. |
| 2366 | /// * The given type is a pointer type whose pointee type contains an |
| 2367 | /// incomplete class type. |
| 2368 | /// * The given type is a member pointer type whose class is an incomplete |
| 2369 | /// class type. |
| 2370 | /// * The given type is a member pointer type whoise pointee type contains an |
| 2371 | /// incomplete class type. |
| 2372 | /// is an indirect or direct pointer to an incomplete class type. |
| 2373 | static bool ContainsIncompleteClassType(QualType Ty) { |
| 2374 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 2375 | if (IsIncompleteClassType(RecordTy)) |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 2380 | return ContainsIncompleteClassType(PointerTy->getPointeeType()); |
| 2381 | |
| 2382 | if (const MemberPointerType *MemberPointerTy = |
| 2383 | dyn_cast<MemberPointerType>(Ty)) { |
| 2384 | // Check if the class type is incomplete. |
| 2385 | const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); |
| 2386 | if (IsIncompleteClassType(ClassType)) |
| 2387 | return true; |
| 2388 | |
| 2389 | return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); |
| 2390 | } |
| 2391 | |
| 2392 | return false; |
| 2393 | } |
| 2394 | |
| 2395 | // CanUseSingleInheritance - Return whether the given record decl has a "single, |
| 2396 | // public, non-virtual base at offset zero (i.e. the derived class is dynamic |
| 2397 | // iff the base is)", according to Itanium C++ ABI, 2.95p6b. |
| 2398 | static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { |
| 2399 | // Check the number of bases. |
| 2400 | if (RD->getNumBases() != 1) |
| 2401 | return false; |
| 2402 | |
| 2403 | // Get the base. |
| 2404 | CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); |
| 2405 | |
| 2406 | // Check that the base is not virtual. |
| 2407 | if (Base->isVirtual()) |
| 2408 | return false; |
| 2409 | |
| 2410 | // Check that the base is public. |
| 2411 | if (Base->getAccessSpecifier() != AS_public) |
| 2412 | return false; |
| 2413 | |
| 2414 | // Check that the class is dynamic iff the base is. |
| 2415 | const CXXRecordDecl *BaseDecl = |
| 2416 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 2417 | if (!BaseDecl->isEmpty() && |
| 2418 | BaseDecl->isDynamicClass() != RD->isDynamicClass()) |
| 2419 | return false; |
| 2420 | |
| 2421 | return true; |
| 2422 | } |
| 2423 | |
| 2424 | void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) { |
| 2425 | // abi::__class_type_info. |
| 2426 | static const char * const ClassTypeInfo = |
| 2427 | "_ZTVN10__cxxabiv117__class_type_infoE"; |
| 2428 | // abi::__si_class_type_info. |
| 2429 | static const char * const SIClassTypeInfo = |
| 2430 | "_ZTVN10__cxxabiv120__si_class_type_infoE"; |
| 2431 | // abi::__vmi_class_type_info. |
| 2432 | static const char * const VMIClassTypeInfo = |
| 2433 | "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; |
| 2434 | |
| 2435 | const char *VTableName = nullptr; |
| 2436 | |
| 2437 | switch (Ty->getTypeClass()) { |
| 2438 | #define TYPE(Class, Base) |
| 2439 | #define ABSTRACT_TYPE(Class, Base) |
| 2440 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2441 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2442 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2443 | #include "clang/AST/TypeNodes.def" |
| 2444 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
| 2445 | |
| 2446 | case Type::LValueReference: |
| 2447 | case Type::RValueReference: |
| 2448 | llvm_unreachable("References shouldn't get here"); |
| 2449 | |
| 2450 | case Type::Auto: |
| 2451 | llvm_unreachable("Undeduced auto type shouldn't get here"); |
| 2452 | |
| 2453 | case Type::Builtin: |
| 2454 | // GCC treats vector and complex types as fundamental types. |
| 2455 | case Type::Vector: |
| 2456 | case Type::ExtVector: |
| 2457 | case Type::Complex: |
| 2458 | case Type::Atomic: |
| 2459 | // FIXME: GCC treats block pointers as fundamental types?! |
| 2460 | case Type::BlockPointer: |
| 2461 | // abi::__fundamental_type_info. |
| 2462 | VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; |
| 2463 | break; |
| 2464 | |
| 2465 | case Type::ConstantArray: |
| 2466 | case Type::IncompleteArray: |
| 2467 | case Type::VariableArray: |
| 2468 | // abi::__array_type_info. |
| 2469 | VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; |
| 2470 | break; |
| 2471 | |
| 2472 | case Type::FunctionNoProto: |
| 2473 | case Type::FunctionProto: |
| 2474 | // abi::__function_type_info. |
| 2475 | VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; |
| 2476 | break; |
| 2477 | |
| 2478 | case Type::Enum: |
| 2479 | // abi::__enum_type_info. |
| 2480 | VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; |
| 2481 | break; |
| 2482 | |
| 2483 | case Type::Record: { |
| 2484 | const CXXRecordDecl *RD = |
| 2485 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 2486 | |
| 2487 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
| 2488 | VTableName = ClassTypeInfo; |
| 2489 | } else if (CanUseSingleInheritance(RD)) { |
| 2490 | VTableName = SIClassTypeInfo; |
| 2491 | } else { |
| 2492 | VTableName = VMIClassTypeInfo; |
| 2493 | } |
| 2494 | |
| 2495 | break; |
| 2496 | } |
| 2497 | |
| 2498 | case Type::ObjCObject: |
| 2499 | // Ignore protocol qualifiers. |
| 2500 | Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); |
| 2501 | |
| 2502 | // Handle id and Class. |
| 2503 | if (isa<BuiltinType>(Ty)) { |
| 2504 | VTableName = ClassTypeInfo; |
| 2505 | break; |
| 2506 | } |
| 2507 | |
| 2508 | assert(isa<ObjCInterfaceType>(Ty)); |
| 2509 | // Fall through. |
| 2510 | |
| 2511 | case Type::ObjCInterface: |
| 2512 | if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { |
| 2513 | VTableName = SIClassTypeInfo; |
| 2514 | } else { |
| 2515 | VTableName = ClassTypeInfo; |
| 2516 | } |
| 2517 | break; |
| 2518 | |
| 2519 | case Type::ObjCObjectPointer: |
| 2520 | case Type::Pointer: |
| 2521 | // abi::__pointer_type_info. |
| 2522 | VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; |
| 2523 | break; |
| 2524 | |
| 2525 | case Type::MemberPointer: |
| 2526 | // abi::__pointer_to_member_type_info. |
| 2527 | VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; |
| 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | llvm::Constant *VTable = |
| 2532 | CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy); |
| 2533 | |
| 2534 | llvm::Type *PtrDiffTy = |
| 2535 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 2536 | |
| 2537 | // The vtable address point is 2. |
| 2538 | llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); |
| 2539 | VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two); |
| 2540 | VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy); |
| 2541 | |
| 2542 | Fields.push_back(VTable); |
| 2543 | } |
| 2544 | |
| 2545 | /// \brief Return the linkage that the type info and type info name constants |
| 2546 | /// should have for the given type. |
| 2547 | static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(CodeGenModule &CGM, |
| 2548 | QualType Ty) { |
| 2549 | // Itanium C++ ABI 2.9.5p7: |
| 2550 | // In addition, it and all of the intermediate abi::__pointer_type_info |
| 2551 | // structs in the chain down to the abi::__class_type_info for the |
| 2552 | // incomplete class type must be prevented from resolving to the |
| 2553 | // corresponding type_info structs for the complete class type, possibly |
| 2554 | // by making them local static objects. Finally, a dummy class RTTI is |
| 2555 | // generated for the incomplete type that will not resolve to the final |
| 2556 | // complete class RTTI (because the latter need not exist), possibly by |
| 2557 | // making it a local static object. |
| 2558 | if (ContainsIncompleteClassType(Ty)) |
| 2559 | return llvm::GlobalValue::InternalLinkage; |
| 2560 | |
| 2561 | switch (Ty->getLinkage()) { |
| 2562 | case NoLinkage: |
| 2563 | case InternalLinkage: |
| 2564 | case UniqueExternalLinkage: |
| 2565 | return llvm::GlobalValue::InternalLinkage; |
| 2566 | |
| 2567 | case VisibleNoLinkage: |
| 2568 | case ExternalLinkage: |
| 2569 | if (!CGM.getLangOpts().RTTI) { |
| 2570 | // RTTI is not enabled, which means that this type info struct is going |
| 2571 | // to be used for exception handling. Give it linkonce_odr linkage. |
| 2572 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2573 | } |
| 2574 | |
| 2575 | if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { |
| 2576 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 2577 | if (RD->hasAttr<WeakAttr>()) |
| 2578 | return llvm::GlobalValue::WeakODRLinkage; |
| 2579 | if (RD->isDynamicClass()) |
| 2580 | return CGM.getVTableLinkage(RD); |
| 2581 | } |
| 2582 | |
| 2583 | return llvm::GlobalValue::LinkOnceODRLinkage; |
| 2584 | } |
| 2585 | |
| 2586 | llvm_unreachable("Invalid linkage!"); |
| 2587 | } |
| 2588 | |
| 2589 | llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) { |
| 2590 | // We want to operate on the canonical type. |
| 2591 | Ty = CGM.getContext().getCanonicalType(Ty); |
| 2592 | |
| 2593 | // Check if we've already emitted an RTTI descriptor for this type. |
| 2594 | SmallString<256> OutName; |
| 2595 | llvm::raw_svector_ostream Out(OutName); |
| 2596 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out); |
| 2597 | Out.flush(); |
| 2598 | StringRef Name = OutName.str(); |
| 2599 | |
| 2600 | llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); |
| 2601 | if (OldGV && !OldGV->isDeclaration()) { |
| 2602 | assert(!OldGV->hasAvailableExternallyLinkage() && |
| 2603 | "available_externally typeinfos not yet implemented"); |
| 2604 | |
| 2605 | return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy); |
| 2606 | } |
| 2607 | |
| 2608 | // Check if there is already an external RTTI descriptor for this type. |
| 2609 | bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); |
| 2610 | if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty))) |
| 2611 | return GetAddrOfExternalRTTIDescriptor(Ty); |
| 2612 | |
| 2613 | // Emit the standard library with external linkage. |
| 2614 | llvm::GlobalVariable::LinkageTypes Linkage; |
| 2615 | if (IsStdLib) |
| 2616 | Linkage = llvm::GlobalValue::ExternalLinkage; |
| 2617 | else |
| 2618 | Linkage = getTypeInfoLinkage(CGM, Ty); |
| 2619 | |
| 2620 | // Add the vtable pointer. |
| 2621 | BuildVTablePointer(cast<Type>(Ty)); |
| 2622 | |
| 2623 | // And the name. |
| 2624 | llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage); |
| 2625 | llvm::Constant *TypeNameField; |
| 2626 | |
| 2627 | // If we're supposed to demote the visibility, be sure to set a flag |
| 2628 | // to use a string comparison for type_info comparisons. |
| 2629 | ItaniumCXXABI::RTTIUniquenessKind RTTIUniqueness = |
| 2630 | CXXABI.classifyRTTIUniqueness(Ty, Linkage); |
| 2631 | if (RTTIUniqueness != ItaniumCXXABI::RUK_Unique) { |
| 2632 | // The flag is the sign bit, which on ARM64 is defined to be clear |
| 2633 | // for global pointers. This is very ARM64-specific. |
| 2634 | TypeNameField = llvm::ConstantExpr::getPtrToInt(TypeName, CGM.Int64Ty); |
| 2635 | llvm::Constant *flag = |
| 2636 | llvm::ConstantInt::get(CGM.Int64Ty, ((uint64_t)1) << 63); |
| 2637 | TypeNameField = llvm::ConstantExpr::getAdd(TypeNameField, flag); |
| 2638 | TypeNameField = |
| 2639 | llvm::ConstantExpr::getIntToPtr(TypeNameField, CGM.Int8PtrTy); |
| 2640 | } else { |
| 2641 | TypeNameField = llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy); |
| 2642 | } |
| 2643 | Fields.push_back(TypeNameField); |
| 2644 | |
| 2645 | switch (Ty->getTypeClass()) { |
| 2646 | #define TYPE(Class, Base) |
| 2647 | #define ABSTRACT_TYPE(Class, Base) |
| 2648 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2649 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2650 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2651 | #include "clang/AST/TypeNodes.def" |
| 2652 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
| 2653 | |
| 2654 | // GCC treats vector types as fundamental types. |
| 2655 | case Type::Builtin: |
| 2656 | case Type::Vector: |
| 2657 | case Type::ExtVector: |
| 2658 | case Type::Complex: |
| 2659 | case Type::BlockPointer: |
| 2660 | // Itanium C++ ABI 2.9.5p4: |
| 2661 | // abi::__fundamental_type_info adds no data members to std::type_info. |
| 2662 | break; |
| 2663 | |
| 2664 | case Type::LValueReference: |
| 2665 | case Type::RValueReference: |
| 2666 | llvm_unreachable("References shouldn't get here"); |
| 2667 | |
| 2668 | case Type::Auto: |
| 2669 | llvm_unreachable("Undeduced auto type shouldn't get here"); |
| 2670 | |
| 2671 | case Type::ConstantArray: |
| 2672 | case Type::IncompleteArray: |
| 2673 | case Type::VariableArray: |
| 2674 | // Itanium C++ ABI 2.9.5p5: |
| 2675 | // abi::__array_type_info adds no data members to std::type_info. |
| 2676 | break; |
| 2677 | |
| 2678 | case Type::FunctionNoProto: |
| 2679 | case Type::FunctionProto: |
| 2680 | // Itanium C++ ABI 2.9.5p5: |
| 2681 | // abi::__function_type_info adds no data members to std::type_info. |
| 2682 | break; |
| 2683 | |
| 2684 | case Type::Enum: |
| 2685 | // Itanium C++ ABI 2.9.5p5: |
| 2686 | // abi::__enum_type_info adds no data members to std::type_info. |
| 2687 | break; |
| 2688 | |
| 2689 | case Type::Record: { |
| 2690 | const CXXRecordDecl *RD = |
| 2691 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 2692 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
| 2693 | // We don't need to emit any fields. |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | if (CanUseSingleInheritance(RD)) |
| 2698 | BuildSIClassTypeInfo(RD); |
| 2699 | else |
| 2700 | BuildVMIClassTypeInfo(RD); |
| 2701 | |
| 2702 | break; |
| 2703 | } |
| 2704 | |
| 2705 | case Type::ObjCObject: |
| 2706 | case Type::ObjCInterface: |
| 2707 | BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); |
| 2708 | break; |
| 2709 | |
| 2710 | case Type::ObjCObjectPointer: |
| 2711 | BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); |
| 2712 | break; |
| 2713 | |
| 2714 | case Type::Pointer: |
| 2715 | BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); |
| 2716 | break; |
| 2717 | |
| 2718 | case Type::MemberPointer: |
| 2719 | BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); |
| 2720 | break; |
| 2721 | |
| 2722 | case Type::Atomic: |
| 2723 | // No fields, at least for the moment. |
| 2724 | break; |
| 2725 | } |
| 2726 | |
| 2727 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields); |
| 2728 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2729 | llvm::Module &M = CGM.getModule(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2730 | llvm::GlobalVariable *GV = |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 2731 | new llvm::GlobalVariable(M, Init->getType(), |
| 2732 | /*Constant=*/true, Linkage, Init, Name); |
| 2733 | |
| 2734 | if (CGM.supportsCOMDAT() && GV->isWeakForLinker()) |
| 2735 | GV->setComdat(M.getOrInsertComdat(GV->getName())); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2736 | |
| 2737 | // If there's already an old global variable, replace it with the new one. |
| 2738 | if (OldGV) { |
| 2739 | GV->takeName(OldGV); |
| 2740 | llvm::Constant *NewPtr = |
| 2741 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 2742 | OldGV->replaceAllUsesWith(NewPtr); |
| 2743 | OldGV->eraseFromParent(); |
| 2744 | } |
| 2745 | |
| 2746 | // The Itanium ABI specifies that type_info objects must be globally |
| 2747 | // unique, with one exception: if the type is an incomplete class |
| 2748 | // type or a (possibly indirect) pointer to one. That exception |
| 2749 | // affects the general case of comparing type_info objects produced |
| 2750 | // by the typeid operator, which is why the comparison operators on |
| 2751 | // std::type_info generally use the type_info name pointers instead |
| 2752 | // of the object addresses. However, the language's built-in uses |
| 2753 | // of RTTI generally require class types to be complete, even when |
| 2754 | // manipulating pointers to those class types. This allows the |
| 2755 | // implementation of dynamic_cast to rely on address equality tests, |
| 2756 | // which is much faster. |
| 2757 | |
| 2758 | // All of this is to say that it's important that both the type_info |
| 2759 | // object and the type_info name be uniqued when weakly emitted. |
| 2760 | |
| 2761 | // Give the type_info object and name the formal visibility of the |
| 2762 | // type itself. |
| 2763 | llvm::GlobalValue::VisibilityTypes llvmVisibility; |
| 2764 | if (llvm::GlobalValue::isLocalLinkage(Linkage)) |
| 2765 | // If the linkage is local, only default visibility makes sense. |
| 2766 | llvmVisibility = llvm::GlobalValue::DefaultVisibility; |
| 2767 | else if (RTTIUniqueness == ItaniumCXXABI::RUK_NonUniqueHidden) |
| 2768 | llvmVisibility = llvm::GlobalValue::HiddenVisibility; |
| 2769 | else |
| 2770 | llvmVisibility = CodeGenModule::GetLLVMVisibility(Ty->getVisibility()); |
| 2771 | TypeName->setVisibility(llvmVisibility); |
| 2772 | GV->setVisibility(llvmVisibility); |
| 2773 | |
| 2774 | return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); |
| 2775 | } |
| 2776 | |
| 2777 | /// ComputeQualifierFlags - Compute the pointer type info flags from the |
| 2778 | /// given qualifier. |
| 2779 | static unsigned ComputeQualifierFlags(Qualifiers Quals) { |
| 2780 | unsigned Flags = 0; |
| 2781 | |
| 2782 | if (Quals.hasConst()) |
| 2783 | Flags |= ItaniumRTTIBuilder::PTI_Const; |
| 2784 | if (Quals.hasVolatile()) |
| 2785 | Flags |= ItaniumRTTIBuilder::PTI_Volatile; |
| 2786 | if (Quals.hasRestrict()) |
| 2787 | Flags |= ItaniumRTTIBuilder::PTI_Restrict; |
| 2788 | |
| 2789 | return Flags; |
| 2790 | } |
| 2791 | |
| 2792 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info |
| 2793 | /// for the given Objective-C object type. |
| 2794 | void ItaniumRTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { |
| 2795 | // Drop qualifiers. |
| 2796 | const Type *T = OT->getBaseType().getTypePtr(); |
| 2797 | assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); |
| 2798 | |
| 2799 | // The builtin types are abi::__class_type_infos and don't require |
| 2800 | // extra fields. |
| 2801 | if (isa<BuiltinType>(T)) return; |
| 2802 | |
| 2803 | ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); |
| 2804 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 2805 | |
| 2806 | // Root classes are also __class_type_info. |
| 2807 | if (!Super) return; |
| 2808 | |
| 2809 | QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); |
| 2810 | |
| 2811 | // Everything else is single inheritance. |
| 2812 | llvm::Constant *BaseTypeInfo = |
| 2813 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(SuperTy); |
| 2814 | Fields.push_back(BaseTypeInfo); |
| 2815 | } |
| 2816 | |
| 2817 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
| 2818 | /// inheritance, according to the Itanium C++ ABI, 2.95p6b. |
| 2819 | void ItaniumRTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { |
| 2820 | // Itanium C++ ABI 2.9.5p6b: |
| 2821 | // It adds to abi::__class_type_info a single member pointing to the |
| 2822 | // type_info structure for the base type, |
| 2823 | llvm::Constant *BaseTypeInfo = |
| 2824 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(RD->bases_begin()->getType()); |
| 2825 | Fields.push_back(BaseTypeInfo); |
| 2826 | } |
| 2827 | |
| 2828 | namespace { |
| 2829 | /// SeenBases - Contains virtual and non-virtual bases seen when traversing |
| 2830 | /// a class hierarchy. |
| 2831 | struct SeenBases { |
| 2832 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; |
| 2833 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; |
| 2834 | }; |
| 2835 | } |
| 2836 | |
| 2837 | /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in |
| 2838 | /// abi::__vmi_class_type_info. |
| 2839 | /// |
| 2840 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, |
| 2841 | SeenBases &Bases) { |
| 2842 | |
| 2843 | unsigned Flags = 0; |
| 2844 | |
| 2845 | const CXXRecordDecl *BaseDecl = |
| 2846 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 2847 | |
| 2848 | if (Base->isVirtual()) { |
| 2849 | // Mark the virtual base as seen. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2850 | if (!Bases.VirtualBases.insert(BaseDecl).second) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2851 | // If this virtual base has been seen before, then the class is diamond |
| 2852 | // shaped. |
| 2853 | Flags |= ItaniumRTTIBuilder::VMI_DiamondShaped; |
| 2854 | } else { |
| 2855 | if (Bases.NonVirtualBases.count(BaseDecl)) |
| 2856 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 2857 | } |
| 2858 | } else { |
| 2859 | // Mark the non-virtual base as seen. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2860 | if (!Bases.NonVirtualBases.insert(BaseDecl).second) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2861 | // If this non-virtual base has been seen before, then the class has non- |
| 2862 | // diamond shaped repeated inheritance. |
| 2863 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 2864 | } else { |
| 2865 | if (Bases.VirtualBases.count(BaseDecl)) |
| 2866 | Flags |= ItaniumRTTIBuilder::VMI_NonDiamondRepeat; |
| 2867 | } |
| 2868 | } |
| 2869 | |
| 2870 | // Walk all bases. |
| 2871 | for (const auto &I : BaseDecl->bases()) |
| 2872 | Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); |
| 2873 | |
| 2874 | return Flags; |
| 2875 | } |
| 2876 | |
| 2877 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { |
| 2878 | unsigned Flags = 0; |
| 2879 | SeenBases Bases; |
| 2880 | |
| 2881 | // Walk all bases. |
| 2882 | for (const auto &I : RD->bases()) |
| 2883 | Flags |= ComputeVMIClassTypeInfoFlags(&I, Bases); |
| 2884 | |
| 2885 | return Flags; |
| 2886 | } |
| 2887 | |
| 2888 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 2889 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 2890 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 2891 | void ItaniumRTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { |
| 2892 | llvm::Type *UnsignedIntLTy = |
| 2893 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 2894 | |
| 2895 | // Itanium C++ ABI 2.9.5p6c: |
| 2896 | // __flags is a word with flags describing details about the class |
| 2897 | // structure, which may be referenced by using the __flags_masks |
| 2898 | // enumeration. These flags refer to both direct and indirect bases. |
| 2899 | unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); |
| 2900 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 2901 | |
| 2902 | // Itanium C++ ABI 2.9.5p6c: |
| 2903 | // __base_count is a word with the number of direct proper base class |
| 2904 | // descriptions that follow. |
| 2905 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); |
| 2906 | |
| 2907 | if (!RD->getNumBases()) |
| 2908 | return; |
| 2909 | |
| 2910 | llvm::Type *LongLTy = |
| 2911 | CGM.getTypes().ConvertType(CGM.getContext().LongTy); |
| 2912 | |
| 2913 | // Now add the base class descriptions. |
| 2914 | |
| 2915 | // Itanium C++ ABI 2.9.5p6c: |
| 2916 | // __base_info[] is an array of base class descriptions -- one for every |
| 2917 | // direct proper base. Each description is of the type: |
| 2918 | // |
| 2919 | // struct abi::__base_class_type_info { |
| 2920 | // public: |
| 2921 | // const __class_type_info *__base_type; |
| 2922 | // long __offset_flags; |
| 2923 | // |
| 2924 | // enum __offset_flags_masks { |
| 2925 | // __virtual_mask = 0x1, |
| 2926 | // __public_mask = 0x2, |
| 2927 | // __offset_shift = 8 |
| 2928 | // }; |
| 2929 | // }; |
| 2930 | for (const auto &Base : RD->bases()) { |
| 2931 | // The __base_type member points to the RTTI for the base type. |
| 2932 | Fields.push_back(ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(Base.getType())); |
| 2933 | |
| 2934 | const CXXRecordDecl *BaseDecl = |
| 2935 | cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 2936 | |
| 2937 | int64_t OffsetFlags = 0; |
| 2938 | |
| 2939 | // All but the lower 8 bits of __offset_flags are a signed offset. |
| 2940 | // For a non-virtual base, this is the offset in the object of the base |
| 2941 | // subobject. For a virtual base, this is the offset in the virtual table of |
| 2942 | // the virtual base offset for the virtual base referenced (negative). |
| 2943 | CharUnits Offset; |
| 2944 | if (Base.isVirtual()) |
| 2945 | Offset = |
| 2946 | CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl); |
| 2947 | else { |
| 2948 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 2949 | Offset = Layout.getBaseClassOffset(BaseDecl); |
| 2950 | }; |
| 2951 | |
| 2952 | OffsetFlags = uint64_t(Offset.getQuantity()) << 8; |
| 2953 | |
| 2954 | // The low-order byte of __offset_flags contains flags, as given by the |
| 2955 | // masks from the enumeration __offset_flags_masks. |
| 2956 | if (Base.isVirtual()) |
| 2957 | OffsetFlags |= BCTI_Virtual; |
| 2958 | if (Base.getAccessSpecifier() == AS_public) |
| 2959 | OffsetFlags |= BCTI_Public; |
| 2960 | |
| 2961 | Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags)); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, |
| 2966 | /// used for pointer types. |
| 2967 | void ItaniumRTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { |
| 2968 | Qualifiers Quals; |
| 2969 | QualType UnqualifiedPointeeTy = |
| 2970 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 2971 | |
| 2972 | // Itanium C++ ABI 2.9.5p7: |
| 2973 | // __flags is a flag word describing the cv-qualification and other |
| 2974 | // attributes of the type pointed to |
| 2975 | unsigned Flags = ComputeQualifierFlags(Quals); |
| 2976 | |
| 2977 | // Itanium C++ ABI 2.9.5p7: |
| 2978 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 2979 | // incomplete class type, the incomplete target type flag is set. |
| 2980 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
| 2981 | Flags |= PTI_Incomplete; |
| 2982 | |
| 2983 | llvm::Type *UnsignedIntLTy = |
| 2984 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 2985 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 2986 | |
| 2987 | // Itanium C++ ABI 2.9.5p7: |
| 2988 | // __pointee is a pointer to the std::type_info derivation for the |
| 2989 | // unqualified type being pointed to. |
| 2990 | llvm::Constant *PointeeTypeInfo = |
| 2991 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); |
| 2992 | Fields.push_back(PointeeTypeInfo); |
| 2993 | } |
| 2994 | |
| 2995 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 2996 | /// struct, used for member pointer types. |
| 2997 | void |
| 2998 | ItaniumRTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { |
| 2999 | QualType PointeeTy = Ty->getPointeeType(); |
| 3000 | |
| 3001 | Qualifiers Quals; |
| 3002 | QualType UnqualifiedPointeeTy = |
| 3003 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 3004 | |
| 3005 | // Itanium C++ ABI 2.9.5p7: |
| 3006 | // __flags is a flag word describing the cv-qualification and other |
| 3007 | // attributes of the type pointed to. |
| 3008 | unsigned Flags = ComputeQualifierFlags(Quals); |
| 3009 | |
| 3010 | const RecordType *ClassType = cast<RecordType>(Ty->getClass()); |
| 3011 | |
| 3012 | // Itanium C++ ABI 2.9.5p7: |
| 3013 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 3014 | // incomplete class type, the incomplete target type flag is set. |
| 3015 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
| 3016 | Flags |= PTI_Incomplete; |
| 3017 | |
| 3018 | if (IsIncompleteClassType(ClassType)) |
| 3019 | Flags |= PTI_ContainingClassIncomplete; |
| 3020 | |
| 3021 | llvm::Type *UnsignedIntLTy = |
| 3022 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 3023 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 3024 | |
| 3025 | // Itanium C++ ABI 2.9.5p7: |
| 3026 | // __pointee is a pointer to the std::type_info derivation for the |
| 3027 | // unqualified type being pointed to. |
| 3028 | llvm::Constant *PointeeTypeInfo = |
| 3029 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(UnqualifiedPointeeTy); |
| 3030 | Fields.push_back(PointeeTypeInfo); |
| 3031 | |
| 3032 | // Itanium C++ ABI 2.9.5p9: |
| 3033 | // __context is a pointer to an abi::__class_type_info corresponding to the |
| 3034 | // class type containing the member pointed to |
| 3035 | // (e.g., the "A" in "int A::*"). |
| 3036 | Fields.push_back( |
| 3037 | ItaniumRTTIBuilder(CXXABI).BuildTypeInfo(QualType(ClassType, 0))); |
| 3038 | } |
| 3039 | |
| 3040 | llvm::Constant *ItaniumCXXABI::getAddrOfRTTIDescriptor(QualType Ty) { |
| 3041 | return ItaniumRTTIBuilder(*this).BuildTypeInfo(Ty); |
| 3042 | } |
| 3043 | |
| 3044 | void ItaniumCXXABI::EmitFundamentalRTTIDescriptor(QualType Type) { |
| 3045 | QualType PointerType = getContext().getPointerType(Type); |
| 3046 | QualType PointerTypeConst = getContext().getPointerType(Type.withConst()); |
| 3047 | ItaniumRTTIBuilder(*this).BuildTypeInfo(Type, true); |
| 3048 | ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerType, true); |
| 3049 | ItaniumRTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true); |
| 3050 | } |
| 3051 | |
| 3052 | void ItaniumCXXABI::EmitFundamentalRTTIDescriptors() { |
| 3053 | QualType FundamentalTypes[] = { |
| 3054 | getContext().VoidTy, getContext().NullPtrTy, |
| 3055 | getContext().BoolTy, getContext().WCharTy, |
| 3056 | getContext().CharTy, getContext().UnsignedCharTy, |
| 3057 | getContext().SignedCharTy, getContext().ShortTy, |
| 3058 | getContext().UnsignedShortTy, getContext().IntTy, |
| 3059 | getContext().UnsignedIntTy, getContext().LongTy, |
| 3060 | getContext().UnsignedLongTy, getContext().LongLongTy, |
| 3061 | getContext().UnsignedLongLongTy, getContext().HalfTy, |
| 3062 | getContext().FloatTy, getContext().DoubleTy, |
| 3063 | getContext().LongDoubleTy, getContext().Char16Ty, |
| 3064 | getContext().Char32Ty, |
| 3065 | }; |
| 3066 | for (const QualType &FundamentalType : FundamentalTypes) |
| 3067 | EmitFundamentalRTTIDescriptor(FundamentalType); |
| 3068 | } |
| 3069 | |
| 3070 | /// What sort of uniqueness rules should we use for the RTTI for the |
| 3071 | /// given type? |
| 3072 | ItaniumCXXABI::RTTIUniquenessKind ItaniumCXXABI::classifyRTTIUniqueness( |
| 3073 | QualType CanTy, llvm::GlobalValue::LinkageTypes Linkage) const { |
| 3074 | if (shouldRTTIBeUnique()) |
| 3075 | return RUK_Unique; |
| 3076 | |
| 3077 | // It's only necessary for linkonce_odr or weak_odr linkage. |
| 3078 | if (Linkage != llvm::GlobalValue::LinkOnceODRLinkage && |
| 3079 | Linkage != llvm::GlobalValue::WeakODRLinkage) |
| 3080 | return RUK_Unique; |
| 3081 | |
| 3082 | // It's only necessary with default visibility. |
| 3083 | if (CanTy->getVisibility() != DefaultVisibility) |
| 3084 | return RUK_Unique; |
| 3085 | |
| 3086 | // If we're not required to publish this symbol, hide it. |
| 3087 | if (Linkage == llvm::GlobalValue::LinkOnceODRLinkage) |
| 3088 | return RUK_NonUniqueHidden; |
| 3089 | |
| 3090 | // If we're required to publish this symbol, as we might be under an |
| 3091 | // explicit instantiation, leave it with default visibility but |
| 3092 | // enable string-comparisons. |
| 3093 | assert(Linkage == llvm::GlobalValue::WeakODRLinkage); |
| 3094 | return RUK_NonUniqueVisible; |
| 3095 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3096 | |
| 3097 | // Find out how to codegen the complete destructor and constructor |
| 3098 | namespace { |
| 3099 | enum class StructorCodegen { Emit, RAUW, Alias, COMDAT }; |
| 3100 | } |
| 3101 | static StructorCodegen getCodegenToUse(CodeGenModule &CGM, |
| 3102 | const CXXMethodDecl *MD) { |
| 3103 | if (!CGM.getCodeGenOpts().CXXCtorDtorAliases) |
| 3104 | return StructorCodegen::Emit; |
| 3105 | |
| 3106 | // The complete and base structors are not equivalent if there are any virtual |
| 3107 | // bases, so emit separate functions. |
| 3108 | if (MD->getParent()->getNumVBases()) |
| 3109 | return StructorCodegen::Emit; |
| 3110 | |
| 3111 | GlobalDecl AliasDecl; |
| 3112 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 3113 | AliasDecl = GlobalDecl(DD, Dtor_Complete); |
| 3114 | } else { |
| 3115 | const auto *CD = cast<CXXConstructorDecl>(MD); |
| 3116 | AliasDecl = GlobalDecl(CD, Ctor_Complete); |
| 3117 | } |
| 3118 | llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); |
| 3119 | |
| 3120 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage)) |
| 3121 | return StructorCodegen::RAUW; |
| 3122 | |
| 3123 | // FIXME: Should we allow available_externally aliases? |
| 3124 | if (!llvm::GlobalAlias::isValidLinkage(Linkage)) |
| 3125 | return StructorCodegen::RAUW; |
| 3126 | |
| 3127 | if (llvm::GlobalValue::isWeakForLinker(Linkage)) { |
| 3128 | // Only ELF supports COMDATs with arbitrary names (C5/D5). |
| 3129 | if (CGM.getTarget().getTriple().isOSBinFormatELF()) |
| 3130 | return StructorCodegen::COMDAT; |
| 3131 | return StructorCodegen::Emit; |
| 3132 | } |
| 3133 | |
| 3134 | return StructorCodegen::Alias; |
| 3135 | } |
| 3136 | |
| 3137 | static void emitConstructorDestructorAlias(CodeGenModule &CGM, |
| 3138 | GlobalDecl AliasDecl, |
| 3139 | GlobalDecl TargetDecl) { |
| 3140 | llvm::GlobalValue::LinkageTypes Linkage = CGM.getFunctionLinkage(AliasDecl); |
| 3141 | |
| 3142 | StringRef MangledName = CGM.getMangledName(AliasDecl); |
| 3143 | llvm::GlobalValue *Entry = CGM.GetGlobalValue(MangledName); |
| 3144 | if (Entry && !Entry->isDeclaration()) |
| 3145 | return; |
| 3146 | |
| 3147 | auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(TargetDecl)); |
| 3148 | llvm::PointerType *AliasType = Aliasee->getType(); |
| 3149 | |
| 3150 | // Create the alias with no name. |
| 3151 | auto *Alias = llvm::GlobalAlias::create( |
| 3152 | AliasType->getElementType(), 0, Linkage, "", Aliasee, &CGM.getModule()); |
| 3153 | |
| 3154 | // Switch any previous uses to the alias. |
| 3155 | if (Entry) { |
| 3156 | assert(Entry->getType() == AliasType && |
| 3157 | "declaration exists with different type"); |
| 3158 | Alias->takeName(Entry); |
| 3159 | Entry->replaceAllUsesWith(Alias); |
| 3160 | Entry->eraseFromParent(); |
| 3161 | } else { |
| 3162 | Alias->setName(MangledName); |
| 3163 | } |
| 3164 | |
| 3165 | // Finally, set up the alias with its proper name and attributes. |
| 3166 | CGM.setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); |
| 3167 | } |
| 3168 | |
| 3169 | void ItaniumCXXABI::emitCXXStructor(const CXXMethodDecl *MD, |
| 3170 | StructorType Type) { |
| 3171 | auto *CD = dyn_cast<CXXConstructorDecl>(MD); |
| 3172 | const CXXDestructorDecl *DD = CD ? nullptr : cast<CXXDestructorDecl>(MD); |
| 3173 | |
| 3174 | StructorCodegen CGType = getCodegenToUse(CGM, MD); |
| 3175 | |
| 3176 | if (Type == StructorType::Complete) { |
| 3177 | GlobalDecl CompleteDecl; |
| 3178 | GlobalDecl BaseDecl; |
| 3179 | if (CD) { |
| 3180 | CompleteDecl = GlobalDecl(CD, Ctor_Complete); |
| 3181 | BaseDecl = GlobalDecl(CD, Ctor_Base); |
| 3182 | } else { |
| 3183 | CompleteDecl = GlobalDecl(DD, Dtor_Complete); |
| 3184 | BaseDecl = GlobalDecl(DD, Dtor_Base); |
| 3185 | } |
| 3186 | |
| 3187 | if (CGType == StructorCodegen::Alias || CGType == StructorCodegen::COMDAT) { |
| 3188 | emitConstructorDestructorAlias(CGM, CompleteDecl, BaseDecl); |
| 3189 | return; |
| 3190 | } |
| 3191 | |
| 3192 | if (CGType == StructorCodegen::RAUW) { |
| 3193 | StringRef MangledName = CGM.getMangledName(CompleteDecl); |
| 3194 | auto *Aliasee = cast<llvm::GlobalValue>(CGM.GetAddrOfGlobal(BaseDecl)); |
| 3195 | CGM.addReplacement(MangledName, Aliasee); |
| 3196 | return; |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | // The base destructor is equivalent to the base destructor of its |
| 3201 | // base class if there is exactly one non-virtual base class with a |
| 3202 | // non-trivial destructor, there are no fields with a non-trivial |
| 3203 | // destructor, and the body of the destructor is trivial. |
| 3204 | if (DD && Type == StructorType::Base && CGType != StructorCodegen::COMDAT && |
| 3205 | !CGM.TryEmitBaseDestructorAsAlias(DD)) |
| 3206 | return; |
| 3207 | |
| 3208 | llvm::Function *Fn = CGM.codegenCXXStructor(MD, Type); |
| 3209 | |
| 3210 | if (CGType == StructorCodegen::COMDAT) { |
| 3211 | SmallString<256> Buffer; |
| 3212 | llvm::raw_svector_ostream Out(Buffer); |
| 3213 | if (DD) |
| 3214 | getMangleContext().mangleCXXDtorComdat(DD, Out); |
| 3215 | else |
| 3216 | getMangleContext().mangleCXXCtorComdat(CD, Out); |
| 3217 | llvm::Comdat *C = CGM.getModule().getOrInsertComdat(Out.str()); |
| 3218 | Fn->setComdat(C); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame^] | 3219 | } else { |
| 3220 | CGM.maybeSetTrivialComdat(*MD, *Fn); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3221 | } |
| 3222 | } |