From e80ee4cbd9a79efa99cabdbb42faba7c7b01e70e Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Mon, 13 Sep 2021 14:44:49 -0700 Subject: [GlobalDCE] In VFE support for relative pointers, allow GEP references to the base symbol This is for Swift VFE support. In some vtable forms that Swift emits, the "base" of a relative pointer is not the global symbol itself directly, but a GEP into it -- so the pointer is relative to a particular field in the global. So getPointerAtOffset() needs to be able to see through the GEP and allow it in a SUB expression, to correctly recognize the offset as a vtable slot. Differential Revision: https://reviews.llvm.org/D109169 --- llvm/lib/Analysis/TypeMetadataUtils.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/Analysis/TypeMetadataUtils.cpp b/llvm/lib/Analysis/TypeMetadataUtils.cpp index c55071a..65fa2bb 100644 --- a/llvm/lib/Analysis/TypeMetadataUtils.cpp +++ b/llvm/lib/Analysis/TypeMetadataUtils.cpp @@ -173,10 +173,19 @@ Constant *llvm::getPointerAtOffset(Constant *I, uint64_t Offset, Module &M, case Instruction::Sub: { auto *Operand0 = cast(C->getOperand(0)); auto *Operand1 = cast(C->getOperand(1)); - auto *Operand1TargetGlobal = getPointerAtOffset(Operand1, 0, M); + + auto StripGEP = [](Constant *C) { + auto *CE = dyn_cast(C); + if (!CE) + return C; + if (CE->getOpcode() != Instruction::GetElementPtr) + return C; + return CE->getOperand(0); + }; + auto *Operand1TargetGlobal = StripGEP(getPointerAtOffset(Operand1, 0, M)); // Check that in the "sub (@a, @b)" expression, @b points back to the top - // level global that we're processing. Otherwise bail. + // level global (or a GEP thereof) that we're processing. Otherwise bail. if (Operand1TargetGlobal != TopLevelGlobal) return nullptr; -- cgit v1.1