Index: llvm/lib/Analysis/TypeMetadataUtils.cpp =================================================================== --- llvm/lib/Analysis/TypeMetadataUtils.cpp +++ llvm/lib/Analysis/TypeMetadataUtils.cpp @@ -155,5 +155,20 @@ return getPointerAtOffset(cast(I->getOperand(Op)), Offset % ElemSize, M); } + if (auto *CI = dyn_cast(I)) { + if (Offset == 0 && CI->getZExtValue() == 0) { + return I; + } + } + if (auto *C = dyn_cast(I)) { + switch (C->getOpcode()) { + case Instruction::Trunc: + case Instruction::PtrToInt: + case Instruction::Sub: + return getPointerAtOffset(cast(C->getOperand(0)), Offset, M); + default: + return nullptr; + } + } return nullptr; } Index: llvm/lib/Transforms/IPO/GlobalDCE.cpp =================================================================== --- llvm/lib/Transforms/IPO/GlobalDCE.cpp +++ llvm/lib/Transforms/IPO/GlobalDCE.cpp @@ -131,11 +131,38 @@ // complete information about all virtual call sites which could call // though this vtable, then skip it, because the call site information will // be more precise. + bool IgnoreDependency = false; + if (VFESafeVTables.count(GVU) && isa(&GV)) { + // Scan the !type metadata on the vtable and also ignore those deps that + // have an offset in one of the !type entries. + if (auto VTable = dyn_cast(GVU)) { + SmallVector Types; + VTable->getMetadata(LLVMContext::MD_type, Types); + if (!VTable->isDeclaration() && !Types.empty()) { + for (MDNode *Type : Types) { + uint64_t OffsetInType = + cast( + cast(Type->getOperand(0))->getValue()) + ->getZExtValue(); + Constant *Ptr = getPointerAtOffset( + VTable->getInitializer(), OffsetInType, *VTable->getParent()); + Ptr = Ptr ? Ptr->stripPointerCasts() : nullptr; + if (Ptr == &GV) { + IgnoreDependency = true; + break; + } + } + } + } + } + + if (IgnoreDependency) { LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU->getName() << " -> " << GV.getName() << "\n"); continue; } + GVDependencies[GVU].insert(&GV); } } Index: llvm/test/Transforms/GlobalDCE/virtual-functions-swift.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/GlobalDCE/virtual-functions-swift.ll @@ -0,0 +1,48 @@ +; RUN: opt < %s -globaldce -S +; RUN: opt < %s -globaldce -S | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata) + +; A vtable with "relative pointers", slots don't contain pointers to implementations, but instead have an i32 offset from the vtable itself to the implementation. +@vtable = internal unnamed_addr constant { [3 x i32] } { [3 x i32] [ + i32 trunc (i64 sub (i64 ptrtoint (void ()* @vfunc1_live to i64), i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32), + i32 trunc (i64 sub (i64 ptrtoint (void ()* @vfunc2_dead to i64), i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32), + i32 trunc (i64 sub (i64 ptrtoint (void ()* @regular_non_virtual_func to i64), i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32) +]}, align 8, !type !0, !type !1, !vcall_visibility !{i64 2} +!0 = !{i64 0, !"vfunc1.type"} +!1 = !{i64 4, !"vfunc2.type"} + +; CHECK: @vtable = internal unnamed_addr constant { [3 x i32] } { [3 x i32] [ +; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (void ()* @vfunc1_live to i64), i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32), +; CHECK-SAME: i32 trunc (i64 sub (i64 0, i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32), +; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (void ()* @regular_non_virtual_func to i64), i64 ptrtoint ({ [3 x i32] }* @vtable to i64)) to i32) +; CHECK-SAME: ] }, align 8, !type !0, !type !1, !vcall_visibility !2 + +; (1) vfunc1_live is referenced from @main, stays alive +define internal void @vfunc1_live() { + ; CHECK: define internal void @vfunc1_live( + ret void +} + +; (2) vfunc2_dead is never referenced, gets removed and vtable slot is null'd +define internal void @vfunc2_dead() { + ; CHECK-NOT: define internal void @vfunc2_dead( + ret void +} + +; (3) regular, non-virtual function that just happens to be referenced from the vtable data structure, should stay alive +define internal void @regular_non_virtual_func() { + ; CHECK: define internal void @regular_non_virtual_func( + ret void +} + +define void @main() { + %1 = ptrtoint { [3 x i32] }* @vtable to i64 ; to keep @vtable alive + %2 = tail call { i8*, i1 } @llvm.type.checked.load(i8* null, i32 0, metadata !"vfunc1.type") + ret void +} + +!999 = !{i32 1, !"Virtual Function Elim", i32 1} +!llvm.module.flags = !{!999}