Add static functions to clear singleton maps. Patch contributed by
[oota-llvm.git] / include / llvm / Constant.h
1 //===-- llvm/Constant.h - Constant class definition -------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Constant class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CONSTANT_H
15 #define LLVM_CONSTANT_H
16
17 #include "llvm/User.h"
18
19 namespace llvm {
20
21 class Constant : public User {
22 protected:
23   inline Constant(const Type *Ty, ValueTy vty = Value::SimpleConstantVal, 
24                   const std::string& Name = "")
25   : User(Ty, vty, Name) {}
26   ~Constant() {}
27
28   void destroyConstantImpl();
29 public:
30   // setName - Specialize setName to handle symbol table majik...
31   virtual void setName(const std::string &name, SymbolTable *ST = 0);
32
33   /// Static constructor to get a '0' constant of arbitrary type...
34   ///
35   static Constant *getNullValue(const Type *Ty);
36
37   /// isNullValue - Return true if this is the value that would be returned by
38   /// getNullValue.
39   virtual bool isNullValue() const = 0;
40
41   virtual void print(std::ostream &O) const;
42
43   // Specialize get/setOperand for Constant's as their operands are always
44   // constants as well.
45   Constant *getOperand(unsigned i) { 
46     return static_cast<Constant*>(User::getOperand(i));
47   }
48   const Constant *getOperand(unsigned i) const {
49     return static_cast<const Constant*>(User::getOperand(i));
50   }
51   void setOperand(unsigned i, Constant *C) {
52     User::setOperand(i, C);
53   }
54
55   /// destroyConstant - Called if some element of this constant is no longer
56   /// valid.  At this point only other constants may be on the use_list for this
57   /// constant.  Any constants on our Use list must also be destroy'd.  The
58   /// implementation must be sure to remove the constant from the list of
59   /// available cached constants.  Implementations should call
60   /// destroyConstantImpl as the last thing they do, to destroy all users and
61   /// delete this.
62   virtual void destroyConstant() { assert(0 && "Not reached!"); }
63   
64   //// Methods for support type inquiry through isa, cast, and dyn_cast:
65   static inline bool classof(const Constant *) { return true; }
66   static inline bool classof(const GlobalValue *) { return true; }
67   static inline bool classof(const Value *V) {
68     return V->getValueType() == Value::SimpleConstantVal ||
69            V->getValueType() == Value::ConstantExprVal ||
70            V->getValueType() == Value::ConstantAggregateZeroVal ||
71            V->getValueType() == Value::FunctionVal ||
72            V->getValueType() == Value::GlobalVariableVal ||
73            V->getValueType() == Value::UndefValueVal;
74   }
75
76   /// replaceUsesOfWithOnConstant - This method is a special form of
77   /// User::replaceUsesOfWith (which does not work on constants) that does work
78   /// on constants.  Basically this method goes through the trouble of building
79   /// a new constant that is equivalent to the current one, with all uses of
80   /// From replaced with uses of To.  After this construction is completed, all
81   /// of the users of 'this' are replaced to use the new constant, and then
82   /// 'this' is deleted.  In general, you should not call this method, instead,
83   /// use Value::replaceAllUsesWith, which automatically dispatches to this
84   /// method as needed.
85   ///
86   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
87                                            bool DisableChecking = false) {
88     // Provide a default implementation for constants (like integers) that
89     // cannot use any other values.  This cannot be called at runtime, but needs
90     // to be here to avoid link errors.
91     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
92            "implemented for all constants that have operands!");
93     assert(0 && "Constants that do not have operands cannot be using 'From'!");
94   }
95
96   /// clearAllValueMaps - This method frees all internal memory used by the
97   /// constant subsystem, which can be used in environments where this memory
98   /// is otherwise reported as a leak.
99   static void clearAllValueMaps();
100 };
101
102 } // End llvm namespace
103
104 #endif