//===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
//
// This file defines the very important Value class. This is subclassed by a
// bunch of other important classes, like Instruction, Function, Type, etc...
#define LLVM_VALUE_H
#include "llvm/AbstractTypeUser.h"
-#include "Support/Annotation.h"
+#include "llvm/Use.h"
#include "Support/Casting.h"
#include <iostream>
-#include <vector>
-class User;
+namespace llvm {
+
class Type;
class Constant;
class Argument;
/// Value - The base class of all values computed by a program that may be used
/// as operands to other values.
///
-struct Value : public Annotable { // Values are annotable
+struct Value {
enum ValueTy {
TypeVal, // This is an instance of Type
ConstantVal, // This is an instance of Constant
};
private:
- std::vector<User *> Uses;
+ iplist<Use> Uses;
std::string Name;
PATypeHolder Ty;
ValueTy VTy;
inline const Type *getType() const { return Ty; }
// All values can potentially be named...
- inline bool hasName() const { return Name != ""; }
+ inline bool hasName() const { return !Name.empty(); }
inline const std::string &getName() const { return Name; }
virtual void setName(const std::string &name, SymbolTable * = 0) {
//----------------------------------------------------------------------
// Methods for handling the vector of uses of this Value.
//
- typedef std::vector<User*>::iterator use_iterator;
- typedef std::vector<User*>::const_iterator use_const_iterator;
-
- inline unsigned use_size() const { return Uses.size(); }
- inline bool use_empty() const { return Uses.empty(); }
- inline use_iterator use_begin() { return Uses.begin(); }
- inline use_const_iterator use_begin() const { return Uses.begin(); }
- inline use_iterator use_end() { return Uses.end(); }
- inline use_const_iterator use_end() const { return Uses.end(); }
- inline User *use_back() { return Uses.back(); }
- inline const User *use_back() const { return Uses.back(); }
-
- /// addUse/killUse - These two methods should only be used by the Use class
- /// below.
- inline void addUse(User *I) { Uses.push_back(I); }
- void killUse(User *I);
+ typedef UseListIteratorWrapper use_iterator;
+ typedef UseListConstIteratorWrapper use_const_iterator;
+
+ unsigned use_size() const { return Uses.size(); }
+ bool use_empty() const { return Uses.empty(); }
+ use_iterator use_begin() { return Uses.begin(); }
+ use_const_iterator use_begin() const { return Uses.begin(); }
+ use_iterator use_end() { return Uses.end(); }
+ use_const_iterator use_end() const { return Uses.end(); }
+ User *use_back() { return Uses.back().getUser(); }
+ const User *use_back() const { return Uses.back().getUser(); }
+
+ /// hasOneUse - Return true if there is exactly one user of this value. This
+ /// is specialized because it is a common request and does not require
+ /// traversing the whole use list.
+ ///
+ bool hasOneUse() const {
+ iplist<Use>::const_iterator I = Uses.begin(), E = Uses.end();
+ if (I == E) return false;
+ return ++I == E;
+ }
+
+ /// addUse/killUse - These two methods should only be used by the Use class.
+ ///
+ void addUse(Use &U) { Uses.push_back(&U); }
+ void killUse(Use &U) { Uses.remove(&U); }
};
inline std::ostream &operator<<(std::ostream &OS, const Value *V) {
}
-//===----------------------------------------------------------------------===//
-// Use Class
-//===----------------------------------------------------------------------===//
+inline User *UseListIteratorWrapper::operator*() const {
+ return Super::operator*().getUser();
+}
-// Use is here to make keeping the "use" list of a Value up-to-date really easy.
-//
-class Use {
- Value *Val;
- User *U;
-public:
- inline Use(Value *v, User *user) {
- Val = v; U = user;
- if (Val) Val->addUse(U);
- }
+inline const User *UseListConstIteratorWrapper::operator*() const {
+ return Super::operator*().getUser();
+}
- inline Use(const Use &user) {
- Val = 0;
- U = user.U;
- operator=(user.Val);
- }
- inline ~Use() { if (Val) Val->killUse(U); }
- inline operator Value*() const { return Val; }
-
- inline Value *operator=(Value *V) {
- if (Val) Val->killUse(U);
- Val = V;
- if (V) V->addUse(U);
- return V;
- }
- inline Value *operator->() { return Val; }
- inline const Value *operator->() const { return Val; }
+Use::Use(Value *v, User *user) : Val(v), U(user) {
+ if (Val) Val->addUse(*this);
+}
- inline Value *get() { return Val; }
- inline const Value *get() const { return Val; }
+Use::Use(const Use &u) : Val(u.Val), U(u.U) {
+ if (Val) Val->addUse(*this);
+}
- inline const Use &operator=(const Use &user) {
- if (Val) Val->killUse(U);
- Val = user.Val;
- Val->addUse(U);
- return *this;
- }
-};
+Use::~Use() {
+ if (Val) Val->killUse(*this);
+}
+
+void Use::set(Value *V) {
+ if (Val) Val->killUse(*this);
+ Val = V;
+ if (V) V->addUse(*this);
+}
-template<> struct simplify_type<Use> {
- typedef Value* SimpleType;
-
- static SimpleType getSimplifiedValue(const Use &Val) {
- return (SimpleType)Val.get();
- }
-};
-template<> struct simplify_type<const Use> {
- typedef Value* SimpleType;
-
- static SimpleType getSimplifiedValue(const Use &Val) {
- return (SimpleType)Val.get();
- }
-};
// isa - Provide some specializations of isa so that we don't have to include
// the subtype header files to test to see if the value is a subclass...
return isa<GlobalVariable>(Val) || isa<Function>(Val);
}
+} // End llvm namespace
+
#endif