Allow for "unsafe" replaceAllUsesWith operatations, for use during type resolution
[oota-llvm.git] / include / llvm / Constant.h
1 //===-- llvm/Constant.h - Constant class definition --------------*- C++ -*--=//
2 //
3 // This file contains the declaration of the Constant class.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_CONSTANT_H
8 #define LLVM_CONSTANT_H
9
10 #include "llvm/User.h"
11
12 class Constant : public User {
13 protected:
14   inline Constant(const Type *Ty) : User(Ty, Value::ConstantVal) {}
15   ~Constant() {}
16
17   void destroyConstantImpl();
18 public:
19   // setName - Specialize setName to handle symbol table majik...
20   virtual void setName(const std::string &name, SymbolTable *ST = 0);
21
22   /// Static constructor to get a '0' constant of arbitrary type...
23   ///
24   static Constant *getNullValue(const Type *Ty);
25
26   /// isNullValue - Return true if this is the value that would be returned by
27   /// getNullValue.
28   virtual bool isNullValue() const = 0;
29
30   virtual void print(std::ostream &O) const;
31
32   /// isConstantExpr - Return true if this is a ConstantExpr
33   ///
34   virtual bool isConstantExpr() const { return false; }
35
36
37   /// destroyConstant - Called if some element of this constant is no longer
38   /// valid.  At this point only other constants may be on the use_list for this
39   /// constant.  Any constants on our Use list must also be destroy'd.  The
40   /// implementation must be sure to remove the constant from the list of
41   /// available cached constants.  Implementations should call
42   /// destroyConstantImpl as the last thing they do, to destroy all users and
43   /// delete this.
44   ///
45   /// Note that this call is only valid on non-primitive constants: You cannot
46   /// destroy an integer constant for example.  This API is used to delete
47   /// constants that have ConstantPointerRef's embeded in them when the module
48   /// is deleted, and it is used by GlobalDCE to remove ConstantPointerRefs that
49   /// are unneeded, allowing globals to be DCE'd.
50   ///
51   virtual void destroyConstant() { assert(0 && "Not reached!"); }
52
53   
54   //// Methods for support type inquiry through isa, cast, and dyn_cast:
55   static inline bool classof(const Constant *) { return true; }
56   static inline bool classof(const Value *V) {
57     return V->getValueType() == Value::ConstantVal;
58   }
59
60   /// replaceUsesOfWithOnConstant - This method is a special form of
61   /// User::replaceUsesOfWith (which does not work on constants) that does work
62   /// on constants.  Basically this method goes through the trouble of building
63   /// a new constant that is equivalent to the current one, with all uses of
64   /// From replaced with uses of To.  After this construction is completed, all
65   /// of the users of 'this' are replaced to use the new constant, and then
66   /// 'this' is deleted.  In general, you should not call this method, instead,
67   /// use Value::replaceAllUsesWith, which automatically dispatches to this
68   /// method as needed.
69   ///
70   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
71                                            bool DisableChecking = false) {
72     // Provide a default implementation for constants (like integers) that
73     // cannot use any other values.  This cannot be called at runtime, but needs
74     // to be here to avoid link errors.
75     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
76            "implemented for all constants that have operands!");
77     assert(0 && "Constants that do not have operands cannot be using 'From'!");
78   }
79
80   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
81   // NOT USE THIS!!
82   // Returns the number of uses of OldV that were replaced.
83   unsigned mutateReferences(Value* OldV, Value *NewV);
84   // END WARNING!!
85 };
86
87 #endif