mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-10-22 12:33:33 +02:00
6aaf81e821
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 llvm-svn: 375094
216 lines
7.1 KiB
C++
216 lines
7.1 KiB
C++
//===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
|
|
//
|
|
// 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 represents an independent object. That is, a function or a global
|
|
// variable, but not an alias.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_IR_GLOBALOBJECT_H
|
|
#define LLVM_IR_GLOBALOBJECT_H
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/IR/GlobalValue.h"
|
|
#include "llvm/IR/Value.h"
|
|
#include "llvm/Support/Alignment.h"
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace llvm {
|
|
|
|
class Comdat;
|
|
class MDNode;
|
|
class Metadata;
|
|
|
|
class GlobalObject : public GlobalValue {
|
|
public:
|
|
// VCallVisibility - values for visibility metadata attached to vtables. This
|
|
// describes the scope in which a virtual call could end up being dispatched
|
|
// through this vtable.
|
|
enum VCallVisibility {
|
|
// Type is potentially visible to external code.
|
|
VCallVisibilityPublic = 0,
|
|
// Type is only visible to code which will be in the current Module after
|
|
// LTO internalization.
|
|
VCallVisibilityLinkageUnit = 1,
|
|
// Type is only visible to code in the current Module.
|
|
VCallVisibilityTranslationUnit = 2,
|
|
};
|
|
|
|
protected:
|
|
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
|
|
LinkageTypes Linkage, const Twine &Name,
|
|
unsigned AddressSpace = 0)
|
|
: GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
|
|
ObjComdat(nullptr) {
|
|
setGlobalValueSubClassData(0);
|
|
}
|
|
|
|
Comdat *ObjComdat;
|
|
enum {
|
|
LastAlignmentBit = 4,
|
|
HasMetadataHashEntryBit,
|
|
HasSectionHashEntryBit,
|
|
|
|
GlobalObjectBits,
|
|
};
|
|
static const unsigned GlobalObjectSubClassDataBits =
|
|
GlobalValueSubClassDataBits - GlobalObjectBits;
|
|
|
|
private:
|
|
static const unsigned AlignmentBits = LastAlignmentBit + 1;
|
|
static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
|
|
static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
|
|
|
|
public:
|
|
GlobalObject(const GlobalObject &) = delete;
|
|
|
|
unsigned getAlignment() const {
|
|
unsigned Data = getGlobalValueSubClassData();
|
|
unsigned AlignmentData = Data & AlignmentMask;
|
|
MaybeAlign Align = decodeMaybeAlign(AlignmentData);
|
|
return Align ? Align->value() : 0;
|
|
}
|
|
|
|
/// FIXME: Remove this setter once the migration to MaybeAlign is over.
|
|
LLVM_ATTRIBUTE_DEPRECATED(void setAlignment(unsigned Align),
|
|
"Please use `void setAlignment(MaybeAlign Align)`");
|
|
void setAlignment(MaybeAlign Align);
|
|
|
|
unsigned getGlobalObjectSubClassData() const {
|
|
unsigned ValueData = getGlobalValueSubClassData();
|
|
return ValueData >> GlobalObjectBits;
|
|
}
|
|
|
|
void setGlobalObjectSubClassData(unsigned Val) {
|
|
unsigned OldData = getGlobalValueSubClassData();
|
|
setGlobalValueSubClassData((OldData & GlobalObjectMask) |
|
|
(Val << GlobalObjectBits));
|
|
assert(getGlobalObjectSubClassData() == Val && "representation error");
|
|
}
|
|
|
|
/// Check if this global has a custom object file section.
|
|
///
|
|
/// This is more efficient than calling getSection() and checking for an empty
|
|
/// string.
|
|
bool hasSection() const {
|
|
return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
|
|
}
|
|
|
|
/// Get the custom section of this global if it has one.
|
|
///
|
|
/// If this global does not have a custom section, this will be empty and the
|
|
/// default object file section (.text, .data, etc) will be used.
|
|
StringRef getSection() const {
|
|
return hasSection() ? getSectionImpl() : StringRef();
|
|
}
|
|
|
|
/// Change the section for this global.
|
|
///
|
|
/// Setting the section to the empty string tells LLVM to choose an
|
|
/// appropriate default object file section.
|
|
void setSection(StringRef S);
|
|
|
|
bool hasComdat() const { return getComdat() != nullptr; }
|
|
const Comdat *getComdat() const { return ObjComdat; }
|
|
Comdat *getComdat() { return ObjComdat; }
|
|
void setComdat(Comdat *C) { ObjComdat = C; }
|
|
|
|
/// Check if this has any metadata.
|
|
bool hasMetadata() const { return hasMetadataHashEntry(); }
|
|
|
|
/// Check if this has any metadata of the given kind.
|
|
bool hasMetadata(unsigned KindID) const {
|
|
return getMetadata(KindID) != nullptr;
|
|
}
|
|
bool hasMetadata(StringRef Kind) const {
|
|
return getMetadata(Kind) != nullptr;
|
|
}
|
|
|
|
/// Get the current metadata attachments for the given kind, if any.
|
|
///
|
|
/// These functions require that the function have at most a single attachment
|
|
/// of the given kind, and return \c nullptr if such an attachment is missing.
|
|
/// @{
|
|
MDNode *getMetadata(unsigned KindID) const;
|
|
MDNode *getMetadata(StringRef Kind) const;
|
|
/// @}
|
|
|
|
/// Appends all attachments with the given ID to \c MDs in insertion order.
|
|
/// If the global has no attachments with the given ID, or if ID is invalid,
|
|
/// leaves MDs unchanged.
|
|
/// @{
|
|
void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const;
|
|
void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const;
|
|
/// @}
|
|
|
|
/// Set a particular kind of metadata attachment.
|
|
///
|
|
/// Sets the given attachment to \c MD, erasing it if \c MD is \c nullptr or
|
|
/// replacing it if it already exists.
|
|
/// @{
|
|
void setMetadata(unsigned KindID, MDNode *MD);
|
|
void setMetadata(StringRef Kind, MDNode *MD);
|
|
/// @}
|
|
|
|
/// Add a metadata attachment.
|
|
/// @{
|
|
void addMetadata(unsigned KindID, MDNode &MD);
|
|
void addMetadata(StringRef Kind, MDNode &MD);
|
|
/// @}
|
|
|
|
/// Appends all attachments for the global to \c MDs, sorting by attachment
|
|
/// ID. Attachments with the same ID appear in insertion order.
|
|
void
|
|
getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
|
|
|
|
/// Erase all metadata attachments with the given kind.
|
|
///
|
|
/// \returns true if any metadata was removed.
|
|
bool eraseMetadata(unsigned KindID);
|
|
|
|
/// Copy metadata from Src, adjusting offsets by Offset.
|
|
void copyMetadata(const GlobalObject *Src, unsigned Offset);
|
|
|
|
void addTypeMetadata(unsigned Offset, Metadata *TypeID);
|
|
void addVCallVisibilityMetadata(VCallVisibility Visibility);
|
|
VCallVisibility getVCallVisibility() const;
|
|
|
|
protected:
|
|
void copyAttributesFrom(const GlobalObject *Src);
|
|
|
|
public:
|
|
// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
static bool classof(const Value *V) {
|
|
return V->getValueID() == Value::FunctionVal ||
|
|
V->getValueID() == Value::GlobalVariableVal;
|
|
}
|
|
|
|
void clearMetadata();
|
|
|
|
private:
|
|
void setGlobalObjectFlag(unsigned Bit, bool Val) {
|
|
unsigned Mask = 1 << Bit;
|
|
setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
|
|
(Val ? Mask : 0u));
|
|
}
|
|
|
|
bool hasMetadataHashEntry() const {
|
|
return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
|
|
}
|
|
void setHasMetadataHashEntry(bool HasEntry) {
|
|
setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
|
|
}
|
|
|
|
StringRef getSectionImpl() const;
|
|
};
|
|
|
|
} // end namespace llvm
|
|
|
|
#endif // LLVM_IR_GLOBALOBJECT_H
|