mirror of
https://github.com/RPCS3/llvm.git
synced 2026-01-31 01:25:19 +01:00
Remove dead virtual functions from vtables with replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up correctly. Original commit message: Currently, it is hard for the compiler to remove unused C++ virtual functions, because they are all referenced from vtables, which are referenced by constructors. This means that if the constructor is called from any live code, then we keep every virtual function in the final link, even if there are no call sites which can use it. This patch allows unused virtual functions to be removed during LTO (and regular compilation in limited circumstances) by using type metadata to match virtual function call sites to the vtable slots they might load from. This information can then be used in the global dead code elimination pass instead of the references from vtables to virtual functions, to more accurately determine which functions are reachable. To make this transformation safe, I have changed clang's code-generation to always load virtual function pointers using the llvm.type.checked.load intrinsic, instead of regular load instructions. I originally tried writing this using clang's existing code-generation, which uses the llvm.type.test and llvm.assume intrinsics after doing a normal load. However, it is possible for optimisations to obscure the relationship between the GEP, load and llvm.type.test, causing GlobalDCE to fail to find virtual function call sites. The existing linkage and visibility types don't accurately describe the scope in which a virtual call could be made which uses a given vtable. This is wider than the visibility of the type itself, because a virtual function call could be made using a more-visible base class. I've added a new !vcall_visibility metadata type to represent this, described in TypeMetadata.rst. The internalization pass and libLTO have been updated to change this metadata when linking is performed. This doesn't currently work with ThinLTO, because it needs to see every call to llvm.type.checked.load in the linkage unit. It might be possible to extend this optimisation to be able to use the ThinLTO summary, as was done for devirtualization, but until then that combination is rejected in the clang driver. To test this, I've written a fuzzer which generates random C++ programs with complex class inheritance graphs, and virtual functions called through object and function pointers of different types. The programs are spread across multiple translation units and DSOs to test the different visibility restrictions. I've also tried doing bootstrap builds of LLVM to test this. This isn't ideal, because only classes in anonymous namespaces can be optimised with -fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not work correctly with -fvisibility=hidden. However, there are only 12 test failures when building with -fvisibility=hidden (and an unmodified compiler), and this change does not cause any new failures for either value of -fvisibility. On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size reduction of ~6%, over a baseline compiled with "-O2 -flto -fvisibility=hidden -fwhole-program-vtables". The best cases are reductions of ~14% in 450.soplex and 483.xalancbmk, and there are no code size increases. I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which show a geomean size reduction of ~3%, again with no size increases. I had hoped that this would have no effect on performance, which would allow it to awlays be enabled (when using -fwhole-program-vtables). However, the changes in clang to use the llvm.type.checked.load intrinsic are causing ~1% performance regression in the C++ parts of SPEC2006. It should be possible to recover some of this perf loss by teaching optimisations about the llvm.type.checked.load intrinsic, which would make it worth turning this on by default (though it's still dependent on -fwhole-program-vtables). Differential revision: https://reviews.llvm.org/D63932 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375094 91177308-0d34-0410-b5e6-96231b3b80d8
162 lines
5.9 KiB
C++
162 lines
5.9 KiB
C++
//===- TypeMetadataUtils.cpp - Utilities related to type metadata ---------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains functions that make it easier to manipulate type metadata
|
|
// for devirtualization.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Analysis/TypeMetadataUtils.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/Dominators.h"
|
|
#include "llvm/IR/Intrinsics.h"
|
|
#include "llvm/IR/Module.h"
|
|
|
|
using namespace llvm;
|
|
|
|
// Search for virtual calls that call FPtr and add them to DevirtCalls.
|
|
static void
|
|
findCallsAtConstantOffset(SmallVectorImpl<DevirtCallSite> &DevirtCalls,
|
|
bool *HasNonCallUses, Value *FPtr, uint64_t Offset,
|
|
const CallInst *CI, DominatorTree &DT) {
|
|
for (const Use &U : FPtr->uses()) {
|
|
Instruction *User = cast<Instruction>(U.getUser());
|
|
// Ignore this instruction if it is not dominated by the type intrinsic
|
|
// being analyzed. Otherwise we may transform a call sharing the same
|
|
// vtable pointer incorrectly. Specifically, this situation can arise
|
|
// after indirect call promotion and inlining, where we may have uses
|
|
// of the vtable pointer guarded by a function pointer check, and a fallback
|
|
// indirect call.
|
|
if (!DT.dominates(CI, User))
|
|
continue;
|
|
if (isa<BitCastInst>(User)) {
|
|
findCallsAtConstantOffset(DevirtCalls, HasNonCallUses, User, Offset, CI,
|
|
DT);
|
|
} else if (auto CI = dyn_cast<CallInst>(User)) {
|
|
DevirtCalls.push_back({Offset, CI});
|
|
} else if (auto II = dyn_cast<InvokeInst>(User)) {
|
|
DevirtCalls.push_back({Offset, II});
|
|
} else if (HasNonCallUses) {
|
|
*HasNonCallUses = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Search for virtual calls that load from VPtr and add them to DevirtCalls.
|
|
static void findLoadCallsAtConstantOffset(
|
|
const Module *M, SmallVectorImpl<DevirtCallSite> &DevirtCalls, Value *VPtr,
|
|
int64_t Offset, const CallInst *CI, DominatorTree &DT) {
|
|
for (const Use &U : VPtr->uses()) {
|
|
Value *User = U.getUser();
|
|
if (isa<BitCastInst>(User)) {
|
|
findLoadCallsAtConstantOffset(M, DevirtCalls, User, Offset, CI, DT);
|
|
} else if (isa<LoadInst>(User)) {
|
|
findCallsAtConstantOffset(DevirtCalls, nullptr, User, Offset, CI, DT);
|
|
} else if (auto GEP = dyn_cast<GetElementPtrInst>(User)) {
|
|
// Take into account the GEP offset.
|
|
if (VPtr == GEP->getPointerOperand() && GEP->hasAllConstantIndices()) {
|
|
SmallVector<Value *, 8> Indices(GEP->op_begin() + 1, GEP->op_end());
|
|
int64_t GEPOffset = M->getDataLayout().getIndexedOffsetInType(
|
|
GEP->getSourceElementType(), Indices);
|
|
findLoadCallsAtConstantOffset(M, DevirtCalls, User, Offset + GEPOffset,
|
|
CI, DT);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void llvm::findDevirtualizableCallsForTypeTest(
|
|
SmallVectorImpl<DevirtCallSite> &DevirtCalls,
|
|
SmallVectorImpl<CallInst *> &Assumes, const CallInst *CI,
|
|
DominatorTree &DT) {
|
|
assert(CI->getCalledFunction()->getIntrinsicID() == Intrinsic::type_test);
|
|
|
|
const Module *M = CI->getParent()->getParent()->getParent();
|
|
|
|
// Find llvm.assume intrinsics for this llvm.type.test call.
|
|
for (const Use &CIU : CI->uses()) {
|
|
if (auto *AssumeCI = dyn_cast<CallInst>(CIU.getUser())) {
|
|
Function *F = AssumeCI->getCalledFunction();
|
|
if (F && F->getIntrinsicID() == Intrinsic::assume)
|
|
Assumes.push_back(AssumeCI);
|
|
}
|
|
}
|
|
|
|
// If we found any, search for virtual calls based on %p and add them to
|
|
// DevirtCalls.
|
|
if (!Assumes.empty())
|
|
findLoadCallsAtConstantOffset(
|
|
M, DevirtCalls, CI->getArgOperand(0)->stripPointerCasts(), 0, CI, DT);
|
|
}
|
|
|
|
void llvm::findDevirtualizableCallsForTypeCheckedLoad(
|
|
SmallVectorImpl<DevirtCallSite> &DevirtCalls,
|
|
SmallVectorImpl<Instruction *> &LoadedPtrs,
|
|
SmallVectorImpl<Instruction *> &Preds, bool &HasNonCallUses,
|
|
const CallInst *CI, DominatorTree &DT) {
|
|
assert(CI->getCalledFunction()->getIntrinsicID() ==
|
|
Intrinsic::type_checked_load);
|
|
|
|
auto *Offset = dyn_cast<ConstantInt>(CI->getArgOperand(1));
|
|
if (!Offset) {
|
|
HasNonCallUses = true;
|
|
return;
|
|
}
|
|
|
|
for (const Use &U : CI->uses()) {
|
|
auto CIU = U.getUser();
|
|
if (auto EVI = dyn_cast<ExtractValueInst>(CIU)) {
|
|
if (EVI->getNumIndices() == 1 && EVI->getIndices()[0] == 0) {
|
|
LoadedPtrs.push_back(EVI);
|
|
continue;
|
|
}
|
|
if (EVI->getNumIndices() == 1 && EVI->getIndices()[0] == 1) {
|
|
Preds.push_back(EVI);
|
|
continue;
|
|
}
|
|
}
|
|
HasNonCallUses = true;
|
|
}
|
|
|
|
for (Value *LoadedPtr : LoadedPtrs)
|
|
findCallsAtConstantOffset(DevirtCalls, &HasNonCallUses, LoadedPtr,
|
|
Offset->getZExtValue(), CI, DT);
|
|
}
|
|
|
|
Constant *llvm::getPointerAtOffset(Constant *I, uint64_t Offset, Module &M) {
|
|
if (I->getType()->isPointerTy()) {
|
|
if (Offset == 0)
|
|
return I;
|
|
return nullptr;
|
|
}
|
|
|
|
const DataLayout &DL = M.getDataLayout();
|
|
|
|
if (auto *C = dyn_cast<ConstantStruct>(I)) {
|
|
const StructLayout *SL = DL.getStructLayout(C->getType());
|
|
if (Offset >= SL->getSizeInBytes())
|
|
return nullptr;
|
|
|
|
unsigned Op = SL->getElementContainingOffset(Offset);
|
|
return getPointerAtOffset(cast<Constant>(I->getOperand(Op)),
|
|
Offset - SL->getElementOffset(Op), M);
|
|
}
|
|
if (auto *C = dyn_cast<ConstantArray>(I)) {
|
|
ArrayType *VTableTy = C->getType();
|
|
uint64_t ElemSize = DL.getTypeAllocSize(VTableTy->getElementType());
|
|
|
|
unsigned Op = Offset / ElemSize;
|
|
if (Op >= C->getNumOperands())
|
|
return nullptr;
|
|
|
|
return getPointerAtOffset(cast<Constant>(I->getOperand(Op)),
|
|
Offset % ElemSize, M);
|
|
}
|
|
return nullptr;
|
|
}
|