2008-07-06 00:41:37 +02:00
|
|
|
//===-- llvm/OperandTraits.h - OperandTraits class definition ---*- C++ -*-===//
|
2008-05-10 10:32:32 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the traits classes that are handy for enforcing the correct
|
|
|
|
// layout of various User subclasses. It also provides the means for accessing
|
|
|
|
// the operands in the most efficient manner.
|
|
|
|
//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_IR_OPERANDTRAITS_H
|
|
|
|
#define LLVM_IR_OPERANDTRAITS_H
|
2008-05-10 10:32:32 +02:00
|
|
|
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/User.h"
|
2008-05-10 10:32:32 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-03-16 11:59:48 +01:00
|
|
|
// FixedNumOperand Trait Class
|
2008-05-10 10:32:32 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 00:58:33 +02:00
|
|
|
/// FixedNumOperandTraits - determine the allocation regime of the Use array
|
|
|
|
/// when it is a prefix to the User object, and the number of Use objects is
|
|
|
|
/// known at compile time.
|
|
|
|
|
2011-01-11 16:07:38 +01:00
|
|
|
template <typename SubClass, unsigned ARITY>
|
2008-05-10 10:32:32 +02:00
|
|
|
struct FixedNumOperandTraits {
|
2011-01-11 16:07:38 +01:00
|
|
|
static Use *op_begin(SubClass* U) {
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
static_assert(
|
|
|
|
!std::is_polymorphic<SubClass>::value,
|
|
|
|
"adding virtual methods to subclasses of User breaks use lists");
|
2008-05-10 10:32:32 +02:00
|
|
|
return reinterpret_cast<Use*>(U) - ARITY;
|
|
|
|
}
|
2011-01-11 16:07:38 +01:00
|
|
|
static Use *op_end(SubClass* U) {
|
2008-05-10 10:32:32 +02:00
|
|
|
return reinterpret_cast<Use*>(U);
|
|
|
|
}
|
|
|
|
static unsigned operands(const User*) {
|
|
|
|
return ARITY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-03-16 11:59:48 +01:00
|
|
|
// OptionalOperand Trait Class
|
2008-05-10 10:32:32 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-03-16 11:59:48 +01:00
|
|
|
/// OptionalOperandTraits - when the number of operands may change at runtime.
|
|
|
|
/// Naturally it may only decrease, because the allocations may not change.
|
|
|
|
|
2011-01-11 16:07:38 +01:00
|
|
|
template <typename SubClass, unsigned ARITY = 1>
|
|
|
|
struct OptionalOperandTraits : public FixedNumOperandTraits<SubClass, ARITY> {
|
2008-05-10 10:32:32 +02:00
|
|
|
static unsigned operands(const User *U) {
|
|
|
|
return U->getNumOperands();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// VariadicOperand Trait Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 00:58:33 +02:00
|
|
|
/// VariadicOperandTraits - determine the allocation regime of the Use array
|
|
|
|
/// when it is a prefix to the User object, and the number of Use objects is
|
|
|
|
/// only known at allocation time.
|
|
|
|
|
2011-01-11 16:07:38 +01:00
|
|
|
template <typename SubClass, unsigned MINARITY = 0>
|
2008-05-10 10:32:32 +02:00
|
|
|
struct VariadicOperandTraits {
|
2011-01-11 16:07:38 +01:00
|
|
|
static Use *op_begin(SubClass* U) {
|
[IR] De-virtualize ~Value to save a vptr
Summary:
Implements PR889
Removing the virtual table pointer from Value saves 1% of RSS when doing
LTO of llc on Linux. The impact on time was positive, but too noisy to
conclusively say that performance improved. Here is a link to the
spreadsheet with the original data:
https://docs.google.com/spreadsheets/d/1F4FHir0qYnV0MEp2sYYp_BuvnJgWlWPhWOwZ6LbW7W4/edit?usp=sharing
This change makes it invalid to directly delete a Value, User, or
Instruction pointer. Instead, such code can be rewritten to a null check
and a call Value::deleteValue(). Value objects tend to have their
lifetimes managed through iplist, so for the most part, this isn't a big
deal. However, there are some places where LLVM deletes values, and
those places had to be migrated to deleteValue. I have also created
llvm::unique_value, which has a custom deleter, so it can be used in
place of std::unique_ptr<Value>.
I had to add the "DerivedUser" Deleter escape hatch for MemorySSA, which
derives from User outside of lib/IR. Code in IR cannot include MemorySSA
headers or call the MemoryAccess object destructors without introducing
a circular dependency, so we need some level of indirection.
Unfortunately, no class derived from User may have any virtual methods,
because adding a virtual method would break User::getHungOffOperands(),
which assumes that it can find the use list immediately prior to the
User object. I've added a static_assert to the appropriate OperandTraits
templates to help people avoid this trap.
Reviewers: chandlerc, mehdi_amini, pete, dberlin, george.burgess.iv
Reviewed By: chandlerc
Subscribers: krytarowski, eraman, george.burgess.iv, mzolotukhin, Prazek, nlewycky, hans, inglorion, pcc, tejohnson, dberlin, llvm-commits
Differential Revision: https://reviews.llvm.org/D31261
llvm-svn: 303362
2017-05-18 19:24:10 +02:00
|
|
|
static_assert(
|
|
|
|
!std::is_polymorphic<SubClass>::value,
|
|
|
|
"adding virtual methods to subclasses of User breaks use lists");
|
2011-01-11 16:07:38 +01:00
|
|
|
return reinterpret_cast<Use*>(U) - static_cast<User*>(U)->getNumOperands();
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
2011-01-11 16:07:38 +01:00
|
|
|
static Use *op_end(SubClass* U) {
|
2008-05-10 10:32:32 +02:00
|
|
|
return reinterpret_cast<Use*>(U);
|
|
|
|
}
|
|
|
|
static unsigned operands(const User *U) {
|
|
|
|
return U->getNumOperands();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// HungoffOperand Trait Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 00:58:33 +02:00
|
|
|
/// HungoffOperandTraits - determine the allocation regime of the Use array
|
|
|
|
/// when it is not a prefix to the User object, but allocated at an unrelated
|
|
|
|
/// heap address.
|
|
|
|
///
|
|
|
|
/// This is the traits class that is needed when the Use array must be
|
|
|
|
/// resizable.
|
|
|
|
|
2008-05-10 10:32:32 +02:00
|
|
|
template <unsigned MINARITY = 1>
|
|
|
|
struct HungoffOperandTraits {
|
|
|
|
static Use *op_begin(User* U) {
|
2015-06-12 19:48:05 +02:00
|
|
|
return U->getOperandList();
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
|
|
|
static Use *op_end(User* U) {
|
2015-06-12 19:48:05 +02:00
|
|
|
return U->getOperandList() + U->getNumOperands();
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
|
|
|
static unsigned operands(const User *U) {
|
|
|
|
return U->getNumOperands();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Macro for generating in-class operand accessor declarations.
|
|
|
|
/// It should only be called in the public section of the interface.
|
|
|
|
///
|
|
|
|
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS) \
|
|
|
|
public: \
|
|
|
|
inline VALUECLASS *getOperand(unsigned) const; \
|
|
|
|
inline void setOperand(unsigned, VALUECLASS*); \
|
2009-02-11 23:09:00 +01:00
|
|
|
inline op_iterator op_begin(); \
|
|
|
|
inline const_op_iterator op_begin() const; \
|
|
|
|
inline op_iterator op_end(); \
|
|
|
|
inline const_op_iterator op_end() const; \
|
2008-05-10 10:32:32 +02:00
|
|
|
protected: \
|
2009-03-11 00:02:13 +01:00
|
|
|
template <int> inline Use &Op(); \
|
|
|
|
template <int> inline const Use &Op() const; \
|
2008-05-10 10:32:32 +02:00
|
|
|
public: \
|
|
|
|
inline unsigned getNumOperands() const
|
|
|
|
|
|
|
|
/// Macro for generating out-of-class operand accessor definitions
|
|
|
|
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS) \
|
2009-02-11 23:09:00 +01:00
|
|
|
CLASS::op_iterator CLASS::op_begin() { \
|
|
|
|
return OperandTraits<CLASS>::op_begin(this); \
|
|
|
|
} \
|
|
|
|
CLASS::const_op_iterator CLASS::op_begin() const { \
|
|
|
|
return OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this)); \
|
|
|
|
} \
|
|
|
|
CLASS::op_iterator CLASS::op_end() { \
|
|
|
|
return OperandTraits<CLASS>::op_end(this); \
|
|
|
|
} \
|
|
|
|
CLASS::const_op_iterator CLASS::op_end() const { \
|
|
|
|
return OperandTraits<CLASS>::op_end(const_cast<CLASS*>(this)); \
|
|
|
|
} \
|
2008-05-10 10:32:32 +02:00
|
|
|
VALUECLASS *CLASS::getOperand(unsigned i_nocapture) const { \
|
|
|
|
assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
|
|
|
|
&& "getOperand() out of range!"); \
|
2011-08-22 11:37:03 +02:00
|
|
|
return cast_or_null<VALUECLASS>( \
|
|
|
|
OperandTraits<CLASS>::op_begin(const_cast<CLASS*>(this))[i_nocapture].get()); \
|
2008-05-10 10:32:32 +02:00
|
|
|
} \
|
|
|
|
void CLASS::setOperand(unsigned i_nocapture, VALUECLASS *Val_nocapture) { \
|
|
|
|
assert(i_nocapture < OperandTraits<CLASS>::operands(this) \
|
|
|
|
&& "setOperand() out of range!"); \
|
|
|
|
OperandTraits<CLASS>::op_begin(this)[i_nocapture] = Val_nocapture; \
|
|
|
|
} \
|
|
|
|
unsigned CLASS::getNumOperands() const { \
|
|
|
|
return OperandTraits<CLASS>::operands(this); \
|
|
|
|
} \
|
2009-03-11 00:02:13 +01:00
|
|
|
template <int Idx_nocapture> Use &CLASS::Op() { \
|
|
|
|
return this->OpFrom<Idx_nocapture>(this); \
|
2008-05-10 10:32:32 +02:00
|
|
|
} \
|
2009-03-11 00:02:13 +01:00
|
|
|
template <int Idx_nocapture> const Use &CLASS::Op() const { \
|
|
|
|
return this->OpFrom<Idx_nocapture>(this); \
|
2008-05-10 10:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-23 11:49:53 +02:00
|
|
|
} // End llvm namespace
|
2008-05-10 10:32:32 +02:00
|
|
|
|
|
|
|
#endif
|