| 1 | //===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code dealing with C++ code generation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | // We might split this into multiple files if it gets too unwieldy |
| 14 | |
| 15 | #include "CGCXXABI.h" |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Attr.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclCXX.h" |
| 22 | #include "clang/AST/DeclObjC.h" |
| 23 | #include "clang/AST/Mangle.h" |
| 24 | #include "clang/AST/RecordLayout.h" |
| 25 | #include "clang/Basic/CodeGenOptions.h" |
| 26 | #include "llvm/IR/IRBuilder.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
| 31 | |
| 32 | /// Try to emit a base destructor as an alias to its primary |
| 33 | /// base-class destructor. |
| 34 | bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { |
| 35 | if (!getCodeGenOpts().CXXCtorDtorAliases) |
| 36 | return true; |
| 37 | |
| 38 | // Producing an alias to a base class ctor/dtor can degrade debug quality |
| 39 | // as the debugger cannot tell them apart. |
| 40 | if (getCodeGenOpts().OptimizationLevel == 0) |
| 41 | return true; |
| 42 | |
| 43 | // Disable this optimization for ARM64EC. FIXME: This probably should work, |
| 44 | // but getting the symbol table correct is complicated. |
| 45 | if (getTarget().getTriple().isWindowsArm64EC()) |
| 46 | return true; |
| 47 | |
| 48 | // If sanitizing memory to check for use-after-dtor, do not emit as |
| 49 | // an alias, unless this class owns no members. |
| 50 | if (getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 51 | !D->getParent()->field_empty()) |
| 52 | return true; |
| 53 | |
| 54 | // If the destructor doesn't have a trivial body, we have to emit it |
| 55 | // separately. |
| 56 | if (!D->hasTrivialBody()) |
| 57 | return true; |
| 58 | |
| 59 | const CXXRecordDecl *Class = D->getParent(); |
| 60 | |
| 61 | // We are going to instrument this destructor, so give up even if it is |
| 62 | // currently empty. |
| 63 | if (Class->mayInsertExtraPadding()) |
| 64 | return true; |
| 65 | |
| 66 | // If we need to manipulate a VTT parameter, give up. |
| 67 | if (Class->getNumVBases()) { |
| 68 | // Extra Credit: passing extra parameters is perfectly safe |
| 69 | // in many calling conventions, so only bail out if the ctor's |
| 70 | // calling convention is nonstandard. |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // If any field has a non-trivial destructor, we have to emit the |
| 75 | // destructor separately. |
| 76 | for (const auto *I : Class->fields()) |
| 77 | if (I->getType().isDestructedType()) |
| 78 | return true; |
| 79 | |
| 80 | // Try to find a unique base class with a non-trivial destructor. |
| 81 | const CXXRecordDecl *UniqueBase = nullptr; |
| 82 | for (const auto &I : Class->bases()) { |
| 83 | |
| 84 | // We're in the base destructor, so skip virtual bases. |
| 85 | if (I.isVirtual()) continue; |
| 86 | |
| 87 | // Skip base classes with trivial destructors. |
| 88 | const auto *Base = I.getType()->castAsCXXRecordDecl(); |
| 89 | if (Base->hasTrivialDestructor()) continue; |
| 90 | |
| 91 | // If we've already found a base class with a non-trivial |
| 92 | // destructor, give up. |
| 93 | if (UniqueBase) return true; |
| 94 | UniqueBase = Base; |
| 95 | } |
| 96 | |
| 97 | // If we didn't find any bases with a non-trivial destructor, then |
| 98 | // the base destructor is actually effectively trivial, which can |
| 99 | // happen if it was needlessly user-defined or if there are virtual |
| 100 | // bases with non-trivial destructors. |
| 101 | if (!UniqueBase) |
| 102 | return true; |
| 103 | |
| 104 | // If the base is at a non-zero offset, give up. |
| 105 | const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(D: Class); |
| 106 | if (!ClassLayout.getBaseClassOffset(Base: UniqueBase).isZero()) |
| 107 | return true; |
| 108 | |
| 109 | // Give up if the calling conventions don't match. We could update the call, |
| 110 | // but it is probably not worth it. |
| 111 | const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); |
| 112 | if (BaseD->getType()->castAs<FunctionType>()->getCallConv() != |
| 113 | D->getType()->castAs<FunctionType>()->getCallConv()) |
| 114 | return true; |
| 115 | |
| 116 | GlobalDecl AliasDecl(D, Dtor_Base); |
| 117 | GlobalDecl TargetDecl(BaseD, Dtor_Base); |
| 118 | |
| 119 | // The alias will use the linkage of the referent. If we can't |
| 120 | // support aliases with that linkage, fail. |
| 121 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(GD: AliasDecl); |
| 122 | |
| 123 | // We can't use an alias if the linkage is not valid for one. |
| 124 | if (!llvm::GlobalAlias::isValidLinkage(L: Linkage)) |
| 125 | return true; |
| 126 | |
| 127 | llvm::GlobalValue::LinkageTypes TargetLinkage = |
| 128 | getFunctionLinkage(GD: TargetDecl); |
| 129 | |
| 130 | // Check if we have it already. |
| 131 | StringRef MangledName = getMangledName(GD: AliasDecl); |
| 132 | llvm::GlobalValue *Entry = GetGlobalValue(Ref: MangledName); |
| 133 | if (Entry && !Entry->isDeclaration()) |
| 134 | return false; |
| 135 | if (Replacements.count(Key: MangledName)) |
| 136 | return false; |
| 137 | |
| 138 | llvm::Type *AliasValueType = getTypes().GetFunctionType(GD: AliasDecl); |
| 139 | |
| 140 | // Find the referent. |
| 141 | auto *Aliasee = cast<llvm::GlobalValue>(Val: GetAddrOfGlobal(GD: TargetDecl)); |
| 142 | |
| 143 | // Instead of creating as alias to a linkonce_odr, replace all of the uses |
| 144 | // of the aliasee. |
| 145 | if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) && |
| 146 | !(TargetLinkage == llvm::GlobalValue::AvailableExternallyLinkage && |
| 147 | TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) { |
| 148 | // FIXME: An extern template instantiation will create functions with |
| 149 | // linkage "AvailableExternally". In libc++, some classes also define |
| 150 | // members with attribute "AlwaysInline" and expect no reference to |
| 151 | // be generated. It is desirable to reenable this optimisation after |
| 152 | // corresponding LLVM changes. |
| 153 | addReplacement(Name: MangledName, C: Aliasee); |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | // If we have a weak, non-discardable alias (weak, weak_odr), like an extern |
| 158 | // template instantiation or a dllexported class, avoid forming it on COFF. |
| 159 | // A COFF weak external alias cannot satisfy a normal undefined symbol |
| 160 | // reference from another TU. The other TU must also mark the referenced |
| 161 | // symbol as weak, which we cannot rely on. |
| 162 | if (llvm::GlobalValue::isWeakForLinker(Linkage) && |
| 163 | getTriple().isOSBinFormatCOFF()) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // If we don't have a definition for the destructor yet or the definition is |
| 168 | // avaialable_externally, don't emit an alias. We can't emit aliases to |
| 169 | // declarations; that's just not how aliases work. |
| 170 | if (Aliasee->isDeclarationForLinker()) |
| 171 | return true; |
| 172 | |
| 173 | // Don't create an alias to a linker weak symbol. This avoids producing |
| 174 | // different COMDATs in different TUs. Another option would be to |
| 175 | // output the alias both for weak_odr and linkonce_odr, but that |
| 176 | // requires explicit comdat support in the IL. |
| 177 | if (llvm::GlobalValue::isWeakForLinker(Linkage: TargetLinkage)) |
| 178 | return true; |
| 179 | // Create the alias with no name. |
| 180 | auto *Alias = llvm::GlobalAlias::create(Ty: AliasValueType, AddressSpace: 0, Linkage, Name: "" , |
| 181 | Aliasee, Parent: &getModule()); |
| 182 | |
| 183 | // Destructors are always unnamed_addr. |
| 184 | Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 185 | |
| 186 | // Switch any previous uses to the alias. |
| 187 | if (Entry) { |
| 188 | assert(Entry->getValueType() == AliasValueType && |
| 189 | Entry->getAddressSpace() == Alias->getAddressSpace() && |
| 190 | "declaration exists with different type" ); |
| 191 | Alias->takeName(V: Entry); |
| 192 | Entry->replaceAllUsesWith(V: Alias); |
| 193 | Entry->eraseFromParent(); |
| 194 | } else { |
| 195 | Alias->setName(MangledName); |
| 196 | } |
| 197 | |
| 198 | // Finally, set up the alias with its proper name and attributes. |
| 199 | SetCommonAttributes(GD: AliasDecl, GV: Alias); |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | /// Emit a definition as a global alias for another definition, unconditionally. |
| 205 | void CodeGenModule::EmitDefinitionAsAlias(GlobalDecl AliasDecl, |
| 206 | GlobalDecl TargetDecl) { |
| 207 | |
| 208 | llvm::Type *AliasValueType = getTypes().GetFunctionType(GD: AliasDecl); |
| 209 | |
| 210 | StringRef MangledName = getMangledName(GD: AliasDecl); |
| 211 | llvm::GlobalValue *Entry = GetGlobalValue(Ref: MangledName); |
| 212 | if (Entry && !Entry->isDeclaration()) |
| 213 | return; |
| 214 | auto *Aliasee = cast<llvm::GlobalValue>(Val: GetAddrOfGlobal(GD: TargetDecl)); |
| 215 | |
| 216 | // Determine the linkage type for the alias. |
| 217 | llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(GD: AliasDecl); |
| 218 | |
| 219 | // Create the alias with no name. |
| 220 | auto *Alias = llvm::GlobalAlias::create(Ty: AliasValueType, AddressSpace: 0, Linkage, Name: "" , |
| 221 | Aliasee, Parent: &getModule()); |
| 222 | // Destructors are always unnamed_addr. |
| 223 | Alias->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 224 | |
| 225 | if (Entry) { |
| 226 | assert(Entry->getValueType() == AliasValueType && |
| 227 | Entry->getAddressSpace() == Alias->getAddressSpace() && |
| 228 | "declaration exists with different type" ); |
| 229 | Alias->takeName(V: Entry); |
| 230 | Entry->replaceAllUsesWith(V: Alias); |
| 231 | Entry->eraseFromParent(); |
| 232 | } else { |
| 233 | Alias->setName(MangledName); |
| 234 | } |
| 235 | |
| 236 | // Set any additional necessary attributes for the alias. |
| 237 | SetCommonAttributes(GD: AliasDecl, GV: Alias); |
| 238 | } |
| 239 | |
| 240 | // For an implicit __host__ __device__ destructor, this trap body is reachable |
| 241 | // only when a host-allocated object is destroyed on the device through the |
| 242 | // vtable. HIP documents that pattern as invalid: an object with virtual |
| 243 | // member functions constructed on the host cannot be destroyed on the device. |
| 244 | // Device-side construction either pulls the dtor in as an organic device |
| 245 | // caller (errors surface in Sema) or compiles cleanly (the real body is |
| 246 | // emitted, no trap). |
| 247 | bool CodeGenModule::tryEmitCUDADeviceInvalidFunctionBody(GlobalDecl GD, |
| 248 | llvm::Function *Fn) { |
| 249 | if (!getLangOpts().CUDAIsDevice) |
| 250 | return false; |
| 251 | const auto *FD = dyn_cast<FunctionDecl>(Val: GD.getDecl()); |
| 252 | if (!FD || !getContext().CUDADeviceInvalidFuncs.count(Ptr: FD->getCanonicalDecl())) |
| 253 | return false; |
| 254 | llvm::BasicBlock *BB = |
| 255 | llvm::BasicBlock::Create(Context&: getLLVMContext(), Name: "entry" , Parent: Fn); |
| 256 | llvm::IRBuilder<> Builder(BB); |
| 257 | Builder.CreateIntrinsic(ID: llvm::Intrinsic::trap, Args: {}); |
| 258 | llvm::Type *RetTy = Fn->getReturnType(); |
| 259 | if (RetTy->isVoidTy()) |
| 260 | Builder.CreateRetVoid(); |
| 261 | else |
| 262 | Builder.CreateRet(V: llvm::PoisonValue::get(T: RetTy)); |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | llvm::Function *CodeGenModule::codegenCXXStructor(GlobalDecl GD) { |
| 267 | const CGFunctionInfo &FnInfo = getTypes().arrangeCXXStructorDeclaration(GD); |
| 268 | auto *Fn = cast<llvm::Function>( |
| 269 | Val: getAddrOfCXXStructor(GD, FnInfo: &FnInfo, /*FnType=*/nullptr, |
| 270 | /*DontDefer=*/true, IsForDefinition: ForDefinition)); |
| 271 | |
| 272 | setFunctionLinkage(GD, F: Fn); |
| 273 | |
| 274 | if (!tryEmitCUDADeviceInvalidFunctionBody(GD, Fn)) |
| 275 | CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo); |
| 276 | setNonAliasAttributes(GD, GO: Fn); |
| 277 | SetLLVMFunctionAttributesForDefinition(D: cast<CXXMethodDecl>(Val: GD.getDecl()), F: Fn); |
| 278 | return Fn; |
| 279 | } |
| 280 | |
| 281 | llvm::FunctionCallee CodeGenModule::getAddrAndTypeOfCXXStructor( |
| 282 | GlobalDecl GD, const CGFunctionInfo *FnInfo, llvm::FunctionType *FnType, |
| 283 | bool DontDefer, ForDefinition_t IsForDefinition) { |
| 284 | auto *MD = cast<CXXMethodDecl>(Val: GD.getDecl()); |
| 285 | |
| 286 | if (isa<CXXDestructorDecl>(Val: MD)) { |
| 287 | // Always alias equivalent complete destructors to base destructors in the |
| 288 | // MS ABI. |
| 289 | if (getTarget().getCXXABI().isMicrosoft() && |
| 290 | GD.getDtorType() == Dtor_Complete && |
| 291 | MD->getParent()->getNumVBases() == 0) |
| 292 | GD = GD.getWithDtorType(Type: Dtor_Base); |
| 293 | } |
| 294 | |
| 295 | if (!FnType) { |
| 296 | if (!FnInfo) |
| 297 | FnInfo = &getTypes().arrangeCXXStructorDeclaration(GD); |
| 298 | FnType = getTypes().GetFunctionType(Info: *FnInfo); |
| 299 | } |
| 300 | |
| 301 | llvm::Constant *Ptr = GetOrCreateLLVMFunction( |
| 302 | MangledName: getMangledName(GD), Ty: FnType, D: GD, /*ForVTable=*/false, DontDefer, |
| 303 | /*IsThunk=*/false, /*ExtraAttrs=*/llvm::AttributeList(), IsForDefinition); |
| 304 | return {FnType, Ptr}; |
| 305 | } |
| 306 | |
| 307 | static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF, |
| 308 | GlobalDecl GD, |
| 309 | llvm::Type *Ty, |
| 310 | const CXXRecordDecl *RD) { |
| 311 | assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() && |
| 312 | "No kext in Microsoft ABI" ); |
| 313 | CodeGenModule &CGM = CGF.CGM; |
| 314 | llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, VPtrOffset: CharUnits()); |
| 315 | Ty = llvm::PointerType::getUnqual(C&: CGM.getLLVMContext()); |
| 316 | assert(VTable && "BuildVirtualCall = kext vtbl pointer is null" ); |
| 317 | uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD); |
| 318 | const VTableLayout &VTLayout = CGM.getItaniumVTableContext().getVTableLayout(RD); |
| 319 | VTableLayout::AddressPointLocation AddressPoint = |
| 320 | VTLayout.getAddressPoint(Base: BaseSubobject(RD, CharUnits::Zero())); |
| 321 | VTableIndex += VTLayout.getVTableOffset(i: AddressPoint.VTableIndex) + |
| 322 | AddressPoint.AddressPointIndex; |
| 323 | llvm::Value *VFuncPtr = |
| 324 | CGF.Builder.CreateConstInBoundsGEP1_64(Ty, Ptr: VTable, Idx0: VTableIndex, Name: "vfnkxt" ); |
| 325 | llvm::Value *VFunc = CGF.Builder.CreateAlignedLoad( |
| 326 | Ty, Ptr: VFuncPtr, Align: llvm::Align(CGF.PointerAlignInBytes)); |
| 327 | |
| 328 | CGPointerAuthInfo PointerAuth; |
| 329 | if (auto &Schema = |
| 330 | CGM.getCodeGenOpts().PointerAuth.CXXVirtualFunctionPointers) { |
| 331 | GlobalDecl OrigMD = |
| 332 | CGM.getItaniumVTableContext().findOriginalMethod(GD: GD.getCanonicalDecl()); |
| 333 | PointerAuth = CGF.EmitPointerAuthInfo(Schema, StorageAddress: VFuncPtr, SchemaDecl: OrigMD, SchemaType: QualType()); |
| 334 | } |
| 335 | |
| 336 | CGCallee Callee(GD, VFunc, PointerAuth); |
| 337 | return Callee; |
| 338 | } |
| 339 | |
| 340 | /// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making |
| 341 | /// indirect call to virtual functions. It makes the call through indexing |
| 342 | /// into the vtable. |
| 343 | CGCallee CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, |
| 344 | NestedNameSpecifier Qual, |
| 345 | llvm::Type *Ty) { |
| 346 | const CXXRecordDecl *RD = Qual.getAsRecordDecl(); |
| 347 | assert(RD && "BuildAppleKextVirtualCall - Qual must be record" ); |
| 348 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(Val: MD)) |
| 349 | return BuildAppleKextVirtualDestructorCall(DD, Type: Dtor_Complete, RD); |
| 350 | |
| 351 | return ::BuildAppleKextVirtualCall(CGF&: *this, GD: MD, Ty, RD); |
| 352 | } |
| 353 | |
| 354 | /// BuildVirtualCall - This routine makes indirect vtable call for |
| 355 | /// call to virtual destructors. It returns 0 if it could not do it. |
| 356 | CGCallee |
| 357 | CodeGenFunction::BuildAppleKextVirtualDestructorCall( |
| 358 | const CXXDestructorDecl *DD, |
| 359 | CXXDtorType Type, |
| 360 | const CXXRecordDecl *RD) { |
| 361 | assert(DD->isVirtual() && Type != Dtor_Base); |
| 362 | // Compute the function type we're calling. |
| 363 | const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration( |
| 364 | GD: GlobalDecl(DD, Dtor_Complete)); |
| 365 | llvm::Type *Ty = CGM.getTypes().GetFunctionType(Info: FInfo); |
| 366 | return ::BuildAppleKextVirtualCall(CGF&: *this, GD: GlobalDecl(DD, Type), Ty, RD); |
| 367 | } |
| 368 | |