Skip to content

Commit

Permalink
Devirtualize Constant::destroyConstant.
Browse files Browse the repository at this point in the history
This reorganizes destroyConstant and destroyConstantImpl.

Now there is only destroyConstant in Constant itself, while
subclasses are required to implement destroyConstantImpl.

destroyConstantImpl no longer calls delete but is instead only
responsible for removing the constant from any maps in which it
is contained.

Reviewed by Duncan Exon Smith.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240471 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
cooperp committed Jun 23, 2015
1 parent 3ebe082 commit d621812
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
13 changes: 6 additions & 7 deletions include/llvm/IR/Constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class Constant : public User {
Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
: User(ty, vty, Ops, NumOps) {}

void destroyConstantImpl();
void replaceUsesOfWithOnConstantImpl(Constant *Replacement);

public:
Expand Down Expand Up @@ -126,14 +125,14 @@ class Constant : public User {
/// vector of constant integers, all equal, and the common value is returned.
const APInt &getUniqueInteger() const;

/// destroyConstant - Called if some element of this constant is no longer
/// valid. At this point only other constants may be on the use_list for this
/// Called if some element of this constant is no longer valid.
/// At this point only other constants may be on the use_list for this
/// constant. Any constants on our Use list must also be destroy'd. The
/// implementation must be sure to remove the constant from the list of
/// available cached constants. Implementations should call
/// destroyConstantImpl as the last thing they do, to destroy all users and
/// delete this.
virtual void destroyConstant() { llvm_unreachable("Not reached!"); }
/// available cached constants. Implementations should implement
/// destroyConstantImpl to remove constants from any pools/maps they are
/// contained it.
void destroyConstant();

//// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const Value *V) {
Expand Down
56 changes: 43 additions & 13 deletions include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class ConstantInt : public Constant {
ConstantInt(const ConstantInt &) = delete;
ConstantInt(IntegerType *Ty, const APInt& V);
APInt Val;

friend class Constant;
void destroyConstantImpl();

protected:
// allocate space for exactly zero operands
void *operator new(size_t s) {
Expand Down Expand Up @@ -231,6 +235,10 @@ class ConstantFP : public Constant {
void *operator new(size_t, unsigned) = delete;
ConstantFP(const ConstantFP &) = delete;
friend class LLVMContextImpl;

friend class Constant;
void destroyConstantImpl();

protected:
ConstantFP(Type *Ty, const APFloat& V);
protected:
Expand Down Expand Up @@ -297,6 +305,10 @@ class ConstantFP : public Constant {
class ConstantAggregateZero : public Constant {
void *operator new(size_t, unsigned) = delete;
ConstantAggregateZero(const ConstantAggregateZero &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
explicit ConstantAggregateZero(Type *ty)
: Constant(ty, ConstantAggregateZeroVal, nullptr, 0) {}
Expand All @@ -308,8 +320,6 @@ class ConstantAggregateZero : public Constant {
public:
static ConstantAggregateZero *get(Type *Ty);

void destroyConstant() override;

/// getSequentialElement - If this CAZ has array or vector type, return a zero
/// with the right element type.
Constant *getSequentialElement() const;
Expand Down Expand Up @@ -343,6 +353,10 @@ class ConstantAggregateZero : public Constant {
class ConstantArray : public Constant {
friend struct ConstantAggrKeyType<ConstantArray>;
ConstantArray(const ConstantArray &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
public:
Expand All @@ -363,7 +377,6 @@ class ConstantArray : public Constant {
return cast<ArrayType>(Value::getType());
}

void destroyConstant() override;
void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
Expand All @@ -385,6 +398,10 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
class ConstantStruct : public Constant {
friend struct ConstantAggrKeyType<ConstantStruct>;
ConstantStruct(const ConstantStruct &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
public:
Expand Down Expand Up @@ -421,7 +438,6 @@ class ConstantStruct : public Constant {
return cast<StructType>(Value::getType());
}

void destroyConstant() override;
void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
Expand All @@ -444,6 +460,10 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
class ConstantVector : public Constant {
friend struct ConstantAggrKeyType<ConstantVector>;
ConstantVector(const ConstantVector &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
public:
Expand Down Expand Up @@ -472,7 +492,6 @@ class ConstantVector : public Constant {
/// elements have the same value, return that value. Otherwise return NULL.
Constant *getSplatValue() const;

void destroyConstant() override;
void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
Expand All @@ -494,6 +513,10 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
class ConstantPointerNull : public Constant {
void *operator new(size_t, unsigned) = delete;
ConstantPointerNull(const ConstantPointerNull &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
explicit ConstantPointerNull(PointerType *T)
: Constant(T,
Expand All @@ -508,8 +531,6 @@ class ConstantPointerNull : public Constant {
/// get() - Static factory methods - Return objects of the specified value
static ConstantPointerNull *get(PointerType *T);

void destroyConstant() override;

/// getType - Specialize the getType() method to always return an PointerType,
/// which reduces the amount of casting needed in parts of the compiler.
///
Expand Down Expand Up @@ -545,6 +566,10 @@ class ConstantDataSequential : public Constant {
ConstantDataSequential *Next;
void *operator new(size_t, unsigned) = delete;
ConstantDataSequential(const ConstantDataSequential &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
: Constant(ty, VT, nullptr, 0), DataElements(Data), Next(nullptr) {}
Expand Down Expand Up @@ -635,8 +660,6 @@ class ConstantDataSequential : public Constant {
/// host endianness of the data elements.
StringRef getRawDataValues() const;

void destroyConstant() override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
///
static bool classof(const Value *V) {
Expand Down Expand Up @@ -778,6 +801,10 @@ class BlockAddress : public Constant {
void *operator new(size_t, unsigned) = delete;
void *operator new(size_t s) { return User::operator new(s, 2); }
BlockAddress(Function *F, BasicBlock *BB);

friend class Constant;
void destroyConstantImpl();

public:
/// get - Return a BlockAddress for the specified function and basic block.
static BlockAddress *get(Function *F, BasicBlock *BB);
Expand All @@ -798,7 +825,6 @@ class BlockAddress : public Constant {
Function *getFunction() const { return (Function*)Op<0>().get(); }
BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }

void destroyConstant() override;
void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
Expand All @@ -825,6 +851,9 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
class ConstantExpr : public Constant {
friend struct ConstantExprKeyType;

friend class Constant;
void destroyConstantImpl();

protected:
ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
: Constant(ty, ConstantExprVal, Ops, NumOps) {
Expand Down Expand Up @@ -1156,7 +1185,6 @@ class ConstantExpr : public Constant {
/// would make it harder to remove ConstantExprs altogether.
Instruction *getAsInstruction();

void destroyConstant() override;
void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U) override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
Expand Down Expand Up @@ -1192,6 +1220,10 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
class UndefValue : public Constant {
void *operator new(size_t, unsigned) = delete;
UndefValue(const UndefValue &) = delete;

friend class Constant;
void destroyConstantImpl();

protected:
explicit UndefValue(Type *T) : Constant(T, UndefValueVal, nullptr, 0) {}
protected:
Expand Down Expand Up @@ -1224,8 +1256,6 @@ class UndefValue : public Constant {
/// \brief Return the number of elements in the array, vector, or struct.
unsigned getNumElements() const;

void destroyConstant() override;

/// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V) {
return V->getValueID() == UndefValueVal;
Expand Down
6 changes: 3 additions & 3 deletions include/llvm/IR/GlobalValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ class GlobalValue : public Constant {
// (19 + 3 + 2 + 1 + 2 + 5) == 32.
unsigned SubClassData : GlobalValueSubClassDataBits;

friend class Constant;
void destroyConstantImpl();

protected:
/// \brief The intrinsic ID for this subclass (which must be a Function).
///
Expand Down Expand Up @@ -334,9 +337,6 @@ class GlobalValue : public Constant {

/// @}

/// Override from Constant class.
void destroyConstant() override;

/// Return true if the primary definition of this global value is outside of
/// the current translation unit.
bool isDeclaration() const;
Expand Down
Loading

0 comments on commit d621812

Please sign in to comment.