2014-11-05 02:36:22 +01:00
|
|
|
//===-- llvm/GlobalObject.h - Class to represent global objects -*- C++ -*-===//
|
2014-05-13 20:45:48 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2016-12-05 22:55:02 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-05-13 20:45:48 +02:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2016-12-05 22:55:02 +01:00
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2014-05-13 20:45:48 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-12-05 22:55:02 +01:00
|
|
|
|
2014-06-27 20:19:56 +02:00
|
|
|
class Comdat;
|
2016-06-01 01:01:54 +02:00
|
|
|
class MDNode;
|
IR: New representation for CFI and virtual call optimization pass metadata.
The bitset metadata currently used in LLVM has a few problems:
1. It has the wrong name. The name "bitset" refers to an implementation
detail of one use of the metadata (i.e. its original use case, CFI).
This makes it harder to understand, as the name makes no sense in the
context of virtual call optimization.
2. It is represented using a global named metadata node, rather than
being directly associated with a global. This makes it harder to
manipulate the metadata when rebuilding global variables, summarise it
as part of ThinLTO and drop unused metadata when associated globals are
dropped. For this reason, CFI does not currently work correctly when
both CFI and vcall opt are enabled, as vcall opt needs to rebuild vtable
globals, and fails to associate metadata with the rebuilt globals. As I
understand it, the same problem could also affect ASan, which rebuilds
globals with a red zone.
This patch solves both of those problems in the following way:
1. Rename the metadata to "type metadata". This new name reflects how
the metadata is currently being used (i.e. to represent type information
for CFI and vtable opt). The new name is reflected in the name for the
associated intrinsic (llvm.type.test) and pass (LowerTypeTests).
2. Attach metadata directly to the globals that it pertains to, rather
than using the "llvm.bitsets" global metadata node as we are doing now.
This is done using the newly introduced capability to attach
metadata to global variables (r271348 and r271358).
See also: http://lists.llvm.org/pipermail/llvm-dev/2016-June/100462.html
Differential Revision: http://reviews.llvm.org/D21053
llvm-svn: 273729
2016-06-24 23:21:32 +02:00
|
|
|
class Metadata;
|
2014-05-13 20:45:48 +02:00
|
|
|
|
|
|
|
class GlobalObject : public GlobalValue {
|
|
|
|
protected:
|
2015-08-21 23:35:28 +02:00
|
|
|
GlobalObject(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
|
|
|
|
LinkageTypes Linkage, const Twine &Name,
|
|
|
|
unsigned AddressSpace = 0)
|
2015-09-14 23:47:27 +02:00
|
|
|
: GlobalValue(Ty, VTy, Ops, NumOps, Linkage, Name, AddressSpace),
|
2015-08-21 23:35:28 +02:00
|
|
|
ObjComdat(nullptr) {
|
2014-05-13 20:45:48 +02:00
|
|
|
setGlobalValueSubClassData(0);
|
|
|
|
}
|
|
|
|
|
2014-06-27 20:19:56 +02:00
|
|
|
Comdat *ObjComdat;
|
2016-06-01 01:01:54 +02:00
|
|
|
enum {
|
|
|
|
LastAlignmentBit = 4,
|
|
|
|
HasMetadataHashEntryBit,
|
Move the section name from GlobalObject to the LLVMContext
Summary:
Convention wisdom says that bytes in Function are precious, and the
vast, vast majority of globals do not live in special sections. Even
when they do, they tend to live in the same section. Store the section
name on the LLVMContext in a StringSet, and maintain a map from
GlobalObject* to section name like we do for metadata, prefix data, etc.
The fact that we've survived this long wasting at least three pointers
of space in Function suggests that Function bytes are perhaps not as
precious as we once thought. Given that most functions have metadata
attachments when debug info is enabled, we might consider adding a
pointer here to make that access more efficient.
Reviewers: jlebar, dexonsmith, mehdi_amini
Subscribers: mehdi_amini, aprantl, llvm-commits
Differential Revision: https://reviews.llvm.org/D28150
llvm-svn: 291613
2017-01-11 00:23:58 +01:00
|
|
|
HasSectionHashEntryBit,
|
2016-06-01 01:01:54 +02:00
|
|
|
|
|
|
|
GlobalObjectBits,
|
|
|
|
};
|
2014-10-24 20:13:04 +02:00
|
|
|
static const unsigned GlobalObjectSubClassDataBits =
|
2016-06-01 01:01:54 +02:00
|
|
|
GlobalValueSubClassDataBits - GlobalObjectBits;
|
2014-10-24 20:13:04 +02:00
|
|
|
|
|
|
|
private:
|
2016-06-01 01:01:54 +02:00
|
|
|
static const unsigned AlignmentBits = LastAlignmentBit + 1;
|
2014-10-24 20:13:04 +02:00
|
|
|
static const unsigned AlignmentMask = (1 << AlignmentBits) - 1;
|
2016-06-01 01:01:54 +02:00
|
|
|
static const unsigned GlobalObjectMask = (1 << GlobalObjectBits) - 1;
|
2014-10-24 20:13:04 +02:00
|
|
|
|
2014-05-13 20:45:48 +02:00
|
|
|
public:
|
2016-12-05 22:55:02 +01:00
|
|
|
GlobalObject(const GlobalObject &) = delete;
|
|
|
|
|
2014-05-13 20:45:48 +02:00
|
|
|
unsigned getAlignment() const {
|
2014-10-24 20:13:04 +02:00
|
|
|
unsigned Data = getGlobalValueSubClassData();
|
|
|
|
unsigned AlignmentData = Data & AlignmentMask;
|
|
|
|
return (1u << AlignmentData) >> 1;
|
2014-05-13 20:45:48 +02:00
|
|
|
}
|
|
|
|
void setAlignment(unsigned Align);
|
|
|
|
|
2017-03-16 17:57:31 +01:00
|
|
|
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");
|
|
|
|
}
|
2014-10-24 20:13:04 +02:00
|
|
|
|
Move the section name from GlobalObject to the LLVMContext
Summary:
Convention wisdom says that bytes in Function are precious, and the
vast, vast majority of globals do not live in special sections. Even
when they do, they tend to live in the same section. Store the section
name on the LLVMContext in a StringSet, and maintain a map from
GlobalObject* to section name like we do for metadata, prefix data, etc.
The fact that we've survived this long wasting at least three pointers
of space in Function suggests that Function bytes are perhaps not as
precious as we once thought. Given that most functions have metadata
attachments when debug info is enabled, we might consider adding a
pointer here to make that access more efficient.
Reviewers: jlebar, dexonsmith, mehdi_amini
Subscribers: mehdi_amini, aprantl, llvm-commits
Differential Revision: https://reviews.llvm.org/D28150
llvm-svn: 291613
2017-01-11 00:23:58 +01:00
|
|
|
/// 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.
|
2014-05-13 20:45:48 +02:00
|
|
|
void setSection(StringRef S);
|
|
|
|
|
2014-06-27 20:19:56 +02:00
|
|
|
bool hasComdat() const { return getComdat() != nullptr; }
|
|
|
|
const Comdat *getComdat() const { return ObjComdat; }
|
|
|
|
Comdat *getComdat() { return ObjComdat; }
|
|
|
|
void setComdat(Comdat *C) { ObjComdat = C; }
|
|
|
|
|
2016-06-01 01:01:54 +02:00
|
|
|
/// Check if this has any metadata.
|
|
|
|
bool hasMetadata() const { return hasMetadataHashEntry(); }
|
|
|
|
|
2016-06-01 03:17:57 +02:00
|
|
|
/// Get the current metadata attachments for the given kind, if any.
|
2016-06-01 01:01:54 +02:00
|
|
|
///
|
2016-06-01 03:17:57 +02:00
|
|
|
/// 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.
|
2016-06-01 01:01:54 +02:00
|
|
|
/// @{
|
|
|
|
MDNode *getMetadata(unsigned KindID) const;
|
|
|
|
MDNode *getMetadata(StringRef Kind) const;
|
|
|
|
/// @}
|
|
|
|
|
2016-06-01 03:17:57 +02:00
|
|
|
/// 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;
|
|
|
|
/// @}
|
|
|
|
|
2016-06-01 01:01:54 +02:00
|
|
|
/// 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);
|
|
|
|
/// @}
|
|
|
|
|
2016-06-01 03:17:57 +02:00
|
|
|
/// 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.
|
2016-06-01 01:01:54 +02:00
|
|
|
void
|
|
|
|
getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const;
|
|
|
|
|
2016-06-01 03:17:57 +02:00
|
|
|
/// Erase all metadata attachments with the given kind.
|
|
|
|
void eraseMetadata(unsigned KindID);
|
2016-06-01 01:01:54 +02:00
|
|
|
|
IR: New representation for CFI and virtual call optimization pass metadata.
The bitset metadata currently used in LLVM has a few problems:
1. It has the wrong name. The name "bitset" refers to an implementation
detail of one use of the metadata (i.e. its original use case, CFI).
This makes it harder to understand, as the name makes no sense in the
context of virtual call optimization.
2. It is represented using a global named metadata node, rather than
being directly associated with a global. This makes it harder to
manipulate the metadata when rebuilding global variables, summarise it
as part of ThinLTO and drop unused metadata when associated globals are
dropped. For this reason, CFI does not currently work correctly when
both CFI and vcall opt are enabled, as vcall opt needs to rebuild vtable
globals, and fails to associate metadata with the rebuilt globals. As I
understand it, the same problem could also affect ASan, which rebuilds
globals with a red zone.
This patch solves both of those problems in the following way:
1. Rename the metadata to "type metadata". This new name reflects how
the metadata is currently being used (i.e. to represent type information
for CFI and vtable opt). The new name is reflected in the name for the
associated intrinsic (llvm.type.test) and pass (LowerTypeTests).
2. Attach metadata directly to the globals that it pertains to, rather
than using the "llvm.bitsets" global metadata node as we are doing now.
This is done using the newly introduced capability to attach
metadata to global variables (r271348 and r271358).
See also: http://lists.llvm.org/pipermail/llvm-dev/2016-June/100462.html
Differential Revision: http://reviews.llvm.org/D21053
llvm-svn: 273729
2016-06-24 23:21:32 +02:00
|
|
|
/// Copy metadata from Src, adjusting offsets by Offset.
|
|
|
|
void copyMetadata(const GlobalObject *Src, unsigned Offset);
|
|
|
|
|
|
|
|
void addTypeMetadata(unsigned Offset, Metadata *TypeID);
|
2016-06-24 19:42:21 +02:00
|
|
|
|
2017-05-11 23:14:29 +02:00
|
|
|
protected:
|
|
|
|
void copyAttributesFrom(const GlobalObject *Src);
|
2014-05-13 20:45:48 +02:00
|
|
|
|
2017-05-11 23:14:29 +02:00
|
|
|
public:
|
2014-05-13 20:45:48 +02:00
|
|
|
// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const Value *V) {
|
|
|
|
return V->getValueID() == Value::FunctionVal ||
|
|
|
|
V->getValueID() == Value::GlobalVariableVal;
|
|
|
|
}
|
2016-06-01 01:01:54 +02:00
|
|
|
|
|
|
|
void clearMetadata();
|
|
|
|
|
|
|
|
private:
|
Move the section name from GlobalObject to the LLVMContext
Summary:
Convention wisdom says that bytes in Function are precious, and the
vast, vast majority of globals do not live in special sections. Even
when they do, they tend to live in the same section. Store the section
name on the LLVMContext in a StringSet, and maintain a map from
GlobalObject* to section name like we do for metadata, prefix data, etc.
The fact that we've survived this long wasting at least three pointers
of space in Function suggests that Function bytes are perhaps not as
precious as we once thought. Given that most functions have metadata
attachments when debug info is enabled, we might consider adding a
pointer here to make that access more efficient.
Reviewers: jlebar, dexonsmith, mehdi_amini
Subscribers: mehdi_amini, aprantl, llvm-commits
Differential Revision: https://reviews.llvm.org/D28150
llvm-svn: 291613
2017-01-11 00:23:58 +01:00
|
|
|
void setGlobalObjectFlag(unsigned Bit, bool Val) {
|
|
|
|
unsigned Mask = 1 << Bit;
|
|
|
|
setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
|
|
|
|
(Val ? Mask : 0u));
|
|
|
|
}
|
|
|
|
|
2016-06-01 01:01:54 +02:00
|
|
|
bool hasMetadataHashEntry() const {
|
|
|
|
return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
|
|
|
|
}
|
|
|
|
void setHasMetadataHashEntry(bool HasEntry) {
|
Move the section name from GlobalObject to the LLVMContext
Summary:
Convention wisdom says that bytes in Function are precious, and the
vast, vast majority of globals do not live in special sections. Even
when they do, they tend to live in the same section. Store the section
name on the LLVMContext in a StringSet, and maintain a map from
GlobalObject* to section name like we do for metadata, prefix data, etc.
The fact that we've survived this long wasting at least three pointers
of space in Function suggests that Function bytes are perhaps not as
precious as we once thought. Given that most functions have metadata
attachments when debug info is enabled, we might consider adding a
pointer here to make that access more efficient.
Reviewers: jlebar, dexonsmith, mehdi_amini
Subscribers: mehdi_amini, aprantl, llvm-commits
Differential Revision: https://reviews.llvm.org/D28150
llvm-svn: 291613
2017-01-11 00:23:58 +01:00
|
|
|
setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
|
2016-06-01 01:01:54 +02:00
|
|
|
}
|
Move the section name from GlobalObject to the LLVMContext
Summary:
Convention wisdom says that bytes in Function are precious, and the
vast, vast majority of globals do not live in special sections. Even
when they do, they tend to live in the same section. Store the section
name on the LLVMContext in a StringSet, and maintain a map from
GlobalObject* to section name like we do for metadata, prefix data, etc.
The fact that we've survived this long wasting at least three pointers
of space in Function suggests that Function bytes are perhaps not as
precious as we once thought. Given that most functions have metadata
attachments when debug info is enabled, we might consider adding a
pointer here to make that access more efficient.
Reviewers: jlebar, dexonsmith, mehdi_amini
Subscribers: mehdi_amini, aprantl, llvm-commits
Differential Revision: https://reviews.llvm.org/D28150
llvm-svn: 291613
2017-01-11 00:23:58 +01:00
|
|
|
|
|
|
|
StringRef getSectionImpl() const;
|
2014-05-13 20:45:48 +02:00
|
|
|
};
|
|
|
|
|
2016-12-05 22:55:02 +01:00
|
|
|
} // end namespace llvm
|
2014-05-13 20:45:48 +02:00
|
|
|
|
2016-12-05 22:55:02 +01:00
|
|
|
#endif // LLVM_IR_GLOBALOBJECT_H
|