Implement support for globally unique constants. Constants no longer live
[oota-llvm.git] / include / llvm / ConstPoolVals.h
1 //===-- llvm/ConstPoolVals.h - Constant Value nodes --------------*- C++ -*--=//
2 //
3 // This file contains the declarations for the ConstPoolVal class and all of
4 // its subclasses, which represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
10
11 #include "llvm/User.h"
12 #include "llvm/Support/DataTypes.h"
13 #include <vector>
14
15 class ArrayType;
16 class StructType;
17
18 //===----------------------------------------------------------------------===//
19 //                            ConstPoolVal Class
20 //===----------------------------------------------------------------------===//
21
22 class ConstPoolVal : public User {
23 protected:
24   inline ConstPoolVal(const Type *Ty) : User(Ty, Value::ConstantVal) {}
25
26 public:
27   // Specialize setName to handle symbol table majik...
28   virtual void setName(const string &name, SymbolTable *ST = 0);
29
30   virtual string getStrValue() const = 0;
31
32   // Static constructor to get a '0' constant of arbitrary type...
33   static ConstPoolVal *getNullConstant(const Type *Ty);
34 };
35
36
37
38 //===----------------------------------------------------------------------===//
39 //              Classes to represent constant pool variable defs
40 //===----------------------------------------------------------------------===//
41
42 //===---------------------------------------------------------------------------
43 // ConstPoolBool - Boolean Values
44 //
45 class ConstPoolBool : public ConstPoolVal {
46   bool Val;
47   ConstPoolBool(const ConstPoolBool &);     // DO NOT IMPLEMENT
48   ConstPoolBool(bool V);
49 public:
50   static ConstPoolBool *True, *False;  // The True & False values
51
52   // Factory objects - Return objects of the specified value
53   static ConstPoolBool *get(bool Value) { return Value ? True : False; }
54   static ConstPoolBool *get(const Type *Ty, bool Value) { return get(Value); }
55
56   // inverted - Return the opposite value of the current value.
57   inline ConstPoolBool *inverted() const { return (this==True) ? False : True; }
58
59   virtual string getStrValue() const;
60   inline bool getValue() const { return Val; }
61 };
62
63
64 //===---------------------------------------------------------------------------
65 // ConstPoolInt - Superclass of ConstPoolSInt & ConstPoolUInt, to make dealing
66 // with integral constants easier.
67 //
68 class ConstPoolInt : public ConstPoolVal {
69 protected:
70   union {
71     int64_t  Signed;
72     uint64_t Unsigned;
73   } Val;
74   ConstPoolInt(const ConstPoolInt &);      // DO NOT IMPLEMENT
75   ConstPoolInt(const Type *Ty, uint64_t V);
76 public:
77   // equalsInt - Provide a helper method that can be used to determine if the 
78   // constant contained within is equal to a constant.  This only works for very
79   // small values, because this is all that can be represented with all types.
80   //
81   bool equalsInt(unsigned char V) const {
82     assert(V <= 127 &&
83            "equals: Can only be used with very small positive constants!");
84     return Val.Unsigned == V;
85   }
86
87   // ConstPoolInt::get static method: return a constant pool int with the
88   // specified value.  as above, we work only with very small values here.
89   //
90   static ConstPoolInt *get(const Type *Ty, unsigned char V);
91 };
92
93
94 //===---------------------------------------------------------------------------
95 // ConstPoolSInt - Signed Integer Values [sbyte, short, int, long]
96 //
97 class ConstPoolSInt : public ConstPoolInt {
98   ConstPoolSInt(const ConstPoolSInt &);      // DO NOT IMPLEMENT
99 protected:
100   ConstPoolSInt(const Type *Ty, int64_t V);
101 public:
102   static ConstPoolSInt *get(const Type *Ty,  int64_t V);
103
104   virtual string getStrValue() const;
105
106   static bool isValueValidForType(const Type *Ty, int64_t V);
107   inline int64_t getValue() const { return Val.Signed; }
108 };
109
110
111 //===---------------------------------------------------------------------------
112 // ConstPoolUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
113 //
114 class ConstPoolUInt : public ConstPoolInt {
115   ConstPoolUInt(const ConstPoolUInt &);      // DO NOT IMPLEMENT
116 protected:
117   ConstPoolUInt(const Type *Ty, uint64_t V);
118 public:
119   static ConstPoolUInt *get(const Type *Ty, uint64_t V);
120
121   virtual string getStrValue() const;
122
123   static bool isValueValidForType(const Type *Ty, uint64_t V);
124   inline uint64_t getValue() const { return Val.Unsigned; }
125 };
126
127
128 //===---------------------------------------------------------------------------
129 // ConstPoolFP - Floating Point Values [float, double]
130 //
131 class ConstPoolFP : public ConstPoolVal {
132   double Val;
133   ConstPoolFP(const ConstPoolFP &);      // DO NOT IMPLEMENT
134 protected:
135   ConstPoolFP(const Type *Ty, double V);
136 public:
137   static ConstPoolFP *get(const Type *Ty, double V);
138
139   virtual string getStrValue() const;
140
141   static bool isValueValidForType(const Type *Ty, double V);
142   inline double getValue() const { return Val; }
143 };
144
145
146 //===---------------------------------------------------------------------------
147 // ConstPoolArray - Constant Array Declarations
148 //
149 class ConstPoolArray : public ConstPoolVal {
150   ConstPoolArray(const ConstPoolArray &);      // DO NOT IMPLEMENT
151 protected:
152   ConstPoolArray(const ArrayType *T, const vector<ConstPoolVal*> &Val);
153 public:
154   static ConstPoolArray *get(const ArrayType *T, const vector<ConstPoolVal*> &);
155
156   virtual string getStrValue() const;
157
158   inline const vector<Use> &getValues() const { return Operands; }
159 };
160
161
162 //===---------------------------------------------------------------------------
163 // ConstPoolStruct - Constant Struct Declarations
164 //
165 class ConstPoolStruct : public ConstPoolVal {
166   ConstPoolStruct(const ConstPoolStruct &);      // DO NOT IMPLEMENT
167 protected:
168   ConstPoolStruct(const StructType *T, const vector<ConstPoolVal*> &Val);
169 public:
170   static ConstPoolStruct *get(const StructType *T,
171                               const vector<ConstPoolVal*> &V);
172
173   virtual string getStrValue() const;
174
175   inline const vector<Use> &getValues() const { return Operands; }
176 };
177
178 //===---------------------------------------------------------------------------
179 // External functions
180 //
181
182 // Convenience functions to get the value of an integer constant, for an
183 // appropriate integer or non-integer type that can be held in an integer.
184 // The type of the argument must be the following:
185 //   GetSignedIntConstantValue:   signed integer or bool
186 //   GetUnsignedIntConstantValue: unsigned integer, bool, or pointer
187 //   GetConstantValueAsSignedInt: any of the above, but the value
188 //                                must fit into a int64_t.
189 // 
190 // isValidConstant is set to true if a valid constant was found.
191 // 
192 int64_t  GetSignedIntConstantValue      (const Value* val, bool& isValidConst);
193 uint64_t GetUnsignedIntConstantValue    (const Value* val, bool& isValidConst);
194 int64_t  GetConstantValueAsSignedInt    (const Value* val, bool& isValidConst);
195
196 //===---------------------------------------------------------------------------
197
198 #endif