From 29ab9f83481cd21abf3055c7c32ea1df953ae167 Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Sun, 14 Jul 2002 22:47:54 +0000 Subject: [PATCH] Added subclass ConstantExpr to represent expressions consructed from constants using operators such as cast, getelementptr, add, shl, etc. Note that a ConstantExpr can be of any type, so classof() in most other subclasses (that discriminate by type) have to check that it is also not a ConstantExpr. This is why isConstantExpr() is needed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2891 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Constants.h | 74 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index 119dd8ffa5b..b927120ad6e 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -11,16 +11,17 @@ #include "llvm/Constant.h" #include "Support/DataTypes.h" + class ArrayType; class StructType; class PointerType; +class ConstantExpr; //===--------------------------------------------------------------------------- // ConstantBool - Boolean Values // class ConstantBool : public Constant { bool Val; - ConstantBool(const ConstantBool &); // DO NOT IMPLEMENT ConstantBool(bool V); ~ConstantBool() {} public: @@ -253,7 +254,7 @@ public: // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ConstantPointer *) { return true; } - static bool classof(const Constant *CPV); // defined in CPV.cpp + static bool classof(const Constant *CPV); // defined in Constants.cpp static inline bool classof(const Value *V) { return isa(V) && classof(cast(V)); } @@ -277,7 +278,7 @@ public: // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ConstantPointerNull *) { return true; } static inline bool classof(const ConstantPointer *P) { - return P->getNumOperands() == 0; + return (P->getNumOperands() == 0 && P->isNullValue()); } static inline bool classof(const Constant *CPV) { return isa(CPV) && classof(cast(CPV)); @@ -313,7 +314,8 @@ public: // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const ConstantPointerRef *) { return true; } static inline bool classof(const ConstantPointer *CPV) { - return CPV->getNumOperands() == 1; + // check for a single operand (the target value) + return (CPV->getNumOperands() == 1); } static inline bool classof(const Constant *CPV) { return isa(CPV) && classof(cast(CPV)); @@ -324,10 +326,72 @@ public: // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD // NOT USE THIS!! - void mutateReference(GlobalValue *NewGV); + // Returns the number of uses of OldV that were replaced. + virtual unsigned mutateReferences(Value* OldV, Value *NewV); // END WARNING!! }; +// ConstantExpr - a constant value that is initialized with +// an expression using other constant values. This is only used +// to represent values that cannot be evaluated at compile-time +// (e.g., something derived from an address) because it does +// not have a mechanism to store the actual value. +// Use the appropriate Constant subclass above for known constants. +// +class ConstantExpr : public Constant { +protected: + unsigned iType; // operation type + +protected: + ConstantExpr(unsigned opCode, Constant *C, const Type *Ty); + ConstantExpr(unsigned opCode, Constant* C1, Constant* C2, const Type *Ty); + ConstantExpr(unsigned opCode, Constant* C, + const std::vector& IdxList, const Type *Ty); + ~ConstantExpr() {} + + virtual void destroyConstant() { destroyConstantImpl(); } + +public: + // Static methods to construct a ConstantExpr of different kinds. + static ConstantExpr *get(unsigned opCode, Constant *C, const Type *Ty); + static ConstantExpr *get(unsigned opCode, + Constant *C1, Constant *C2, const Type *Ty); + static ConstantExpr *get(unsigned opCode, Constant* C, + const std::vector& idxList, const Type *Ty); + + // isNullValue - Return true if this is the value that would be returned by + // getNullValue. + virtual bool isNullValue() const { return false; } + + // getOpcode - Return the opcode at the root of this constant expression + unsigned getOpcode() const { return iType; } + + // getOpcodeName - Return a string representation for an opcode. + static const char* getOpcodeName(unsigned opCode); + const char* getOpcodeName() const { + return getOpcodeName(getOpcode()); + } + + // isConstantExpr - Return true if this is a ConstantExpr + virtual bool isConstantExpr() const { return true; } + + // Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const ConstantExpr *) { return true; } + static inline bool classof(const Constant *CPV) { + return CPV->isConstantExpr(); + } + static inline bool classof(const Value *V) { + return isa(V) && classof(cast(V)); + } + +public: + // WARNING: Only to be used by Bytecode & Assembly Parsers! USER CODE SHOULD + // NOT USE THIS!! + // Returns the number of uses of OldV that were replaced. + virtual unsigned mutateReferences(Value* OldV, Value *NewV); + // END WARNING!! +}; + #endif -- 2.34.1