Skip to content

[CallPromotionUtil] See through function alias when devirtualizing a virtual call on an alloca. #80736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/include/llvm/Analysis/TypeMetadataUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define LLVM_ANALYSIS_TYPEMETADATAUTILS_H

#include <cstdint>
#include <utility>

namespace llvm {

Expand All @@ -24,6 +25,7 @@ class CallInst;
class Constant;
class Function;
class DominatorTree;
class GlobalVariable;
class Instruction;
class Module;

Expand Down Expand Up @@ -77,6 +79,13 @@ void findDevirtualizableCallsForTypeCheckedLoad(
Constant *getPointerAtOffset(Constant *I, uint64_t Offset, Module &M,
Constant *TopLevelGlobal = nullptr);

/// Given a vtable and a specified offset, returns the function and the trivial
/// pointer at the specified offset in pair iff the pointer at the specified
/// offset is a function or an alias to a function. Returns a pair of nullptr
/// otherwise.
std::pair<Function *, Constant *>
getFunctionAtVTableOffset(GlobalVariable *GV, uint64_t Offset, Module &M);

/// Finds the same "relative pointer" pattern as described above, where the
/// target is `F`, and replaces the entire pattern with a constant zero.
void replaceRelativePointerUsersWithZero(Function *F);
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/Analysis/TypeMetadataUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ Constant *llvm::getPointerAtOffset(Constant *I, uint64_t Offset, Module &M,
return nullptr;
}

std::pair<Function *, Constant *>
llvm::getFunctionAtVTableOffset(GlobalVariable *GV, uint64_t Offset,
Module &M) {
Constant *Ptr = getPointerAtOffset(GV->getInitializer(), Offset, M, GV);
if (!Ptr)
return std::pair<Function *, Constant *>(nullptr, nullptr);

auto C = Ptr->stripPointerCasts();
// Make sure this is a function or alias to a function.
auto Fn = dyn_cast<Function>(C);
auto A = dyn_cast<GlobalAlias>(C);
if (!Fn && A)
Fn = dyn_cast<Function>(A->getAliasee());

if (!Fn)
return std::pair<Function *, Constant *>(nullptr, nullptr);

return std::pair<Function *, Constant *>(Fn, C);
}

void llvm::replaceRelativePointerUsersWithZero(Function *F) {
for (auto *U : F->users()) {
auto *PtrExpr = dyn_cast<ConstantExpr>(U);
Expand Down
15 changes: 4 additions & 11 deletions llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,17 +1066,10 @@ bool DevirtModule::tryFindVirtualCallTargets(
GlobalObject::VCallVisibilityPublic)
return false;

Constant *Ptr = getPointerAtOffset(TM.Bits->GV->getInitializer(),
TM.Offset + ByteOffset, M, TM.Bits->GV);
if (!Ptr)
return false;

auto C = Ptr->stripPointerCasts();
// Make sure this is a function or alias to a function.
auto Fn = dyn_cast<Function>(C);
auto A = dyn_cast<GlobalAlias>(C);
if (!Fn && A)
Fn = dyn_cast<Function>(A->getAliasee());
Function *Fn = nullptr;
Constant *C = nullptr;
std::tie(Fn, C) =
getFunctionAtVTableOffset(TM.Bits->GV, TM.Offset + ByteOffset, M);

if (!Fn)
return false;
Expand Down
11 changes: 4 additions & 7 deletions llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,13 @@ bool llvm::tryPromoteCall(CallBase &CB) {
// Not in the form of a global constant variable with an initializer.
return false;

Constant *VTableGVInitializer = GV->getInitializer();
APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
if (!(VTableGVOffset.getActiveBits() <= 64))
return false; // Out of range.
Constant *Ptr = getPointerAtOffset(VTableGVInitializer,
VTableGVOffset.getZExtValue(),
*M);
if (!Ptr)
return false; // No constant (function) pointer found.
Function *DirectCallee = dyn_cast<Function>(Ptr->stripPointerCasts());

Function *DirectCallee = nullptr;
std::tie(DirectCallee, std::ignore) =
getFunctionAtVTableOffset(GV, VTableGVOffset.getZExtValue(), *M);
if (!DirectCallee)
return false; // No function pointer found.

Expand Down