07a82edbd7a568282a7fad98da3f30b2996fa533
[oota-llvm.git] / include / llvm / Constants.h
1 //===-- llvm/Constants.h - Constant class subclass definitions ---*- C++ -*--=//
2 //
3 // This file contains the declarations for the subclasses of Constant, which
4 // represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTANTS_H
9 #define LLVM_CONSTANTS_H
10
11 #include "llvm/Constant.h"
12 #include "Support/DataTypes.h"
13
14 class ArrayType;
15 class StructType;
16 class PointerType;
17
18 //===---------------------------------------------------------------------------
19 // ConstantBool - Boolean Values
20 //
21 class ConstantBool : public Constant {
22   bool Val;
23   ConstantBool(bool V);
24   ~ConstantBool() {}
25 public:
26   static ConstantBool *True, *False;  // The True & False values
27
28   // Factory objects - Return objects of the specified value
29   static ConstantBool *get(bool Value) { return Value ? True : False; }
30   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
31
32   // inverted - Return the opposite value of the current value.
33   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
34
35   inline bool getValue() const { return Val; }
36
37   // isNullValue - Return true if this is the value that would be returned by
38   // getNullValue.
39   virtual bool isNullValue() const { return this == False; }
40
41   // Methods for support type inquiry through isa, cast, and dyn_cast:
42   static inline bool classof(const ConstantBool *) { return true; }
43   static bool classof(const Constant *CPV) {
44     return (CPV == True) | (CPV == False);
45   }
46   static inline bool classof(const Value *V) {
47     return isa<Constant>(V) && classof(cast<Constant>(V));
48   }
49 };
50
51
52 //===---------------------------------------------------------------------------
53 // ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
54 // with integral constants easier.
55 //
56 class ConstantInt : public Constant {
57 protected:
58   union {
59     int64_t  Signed;
60     uint64_t Unsigned;
61   } Val;
62   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
63   ConstantInt(const Type *Ty, uint64_t V);
64   ~ConstantInt() {}
65 public:
66   // equalsInt - Provide a helper method that can be used to determine if the 
67   // constant contained within is equal to a constant.  This only works for very
68   // small values, because this is all that can be represented with all types.
69   //
70   bool equalsInt(unsigned char V) const {
71     assert(V <= 127 &&
72            "equals: Can only be used with very small positive constants!");
73     return Val.Unsigned == V;
74   }
75
76   // ConstantInt::get static method: return a constant pool int with the
77   // specified value.  as above, we work only with very small values here.
78   //
79   static ConstantInt *get(const Type *Ty, unsigned char V);
80
81   // isNullValue - Return true if this is the value that would be returned by
82   // getNullValue.
83   virtual bool isNullValue() const { return Val.Unsigned == 0; }
84
85   // Methods for support type inquiry through isa, cast, and dyn_cast:
86   static inline bool classof(const ConstantInt *) { return true; }
87   static bool classof(const Constant *CPV);  // defined in CPV.cpp
88   static inline bool classof(const Value *V) {
89     return isa<Constant>(V) && classof(cast<Constant>(V));
90   }
91 };
92
93
94 //===---------------------------------------------------------------------------
95 // ConstantSInt - Signed Integer Values [sbyte, short, int, long]
96 //
97 class ConstantSInt : public ConstantInt {
98   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
99 protected:
100   ConstantSInt(const Type *Ty, int64_t V);
101   ~ConstantSInt() {}
102 public:
103   static ConstantSInt *get(const Type *Ty, int64_t V);
104
105   static bool isValueValidForType(const Type *Ty, int64_t V);
106   inline int64_t getValue() const { return Val.Signed; }
107
108   // Methods for support type inquiry through isa, cast, and dyn_cast:
109   static inline bool classof(const ConstantSInt *) { return true; }
110   static bool classof(const Constant *CPV);  // defined in CPV.cpp
111   static inline bool classof(const Value *V) {
112     return isa<Constant>(V) && classof(cast<Constant>(V));
113   }
114 };
115
116 //===---------------------------------------------------------------------------
117 // ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
118 //
119 class ConstantUInt : public ConstantInt {
120   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
121 protected:
122   ConstantUInt(const Type *Ty, uint64_t V);
123   ~ConstantUInt() {}
124 public:
125   static ConstantUInt *get(const Type *Ty, uint64_t V);
126
127   static bool isValueValidForType(const Type *Ty, uint64_t V);
128   inline uint64_t getValue() const { return Val.Unsigned; }
129
130   // Methods for support type inquiry through isa, cast, and dyn_cast:
131   static inline bool classof(const ConstantUInt *) { return true; }
132   static bool classof(const Constant *CPV);  // defined in CPV.cpp
133   static inline bool classof(const Value *V) {
134     return isa<Constant>(V) && classof(cast<Constant>(V));
135   }
136 };
137
138
139 //===---------------------------------------------------------------------------
140 // ConstantFP - Floating Point Values [float, double]
141 //
142 class ConstantFP : public Constant {
143   double Val;
144   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
145 protected:
146   ConstantFP(const Type *Ty, double V);
147   ~ConstantFP() {}
148 public:
149   static ConstantFP *get(const Type *Ty, double V);
150
151   static bool isValueValidForType(const Type *Ty, double V);
152   inline double getValue() const { return Val; }
153
154   // isNullValue - Return true if this is the value that would be returned by
155   // getNullValue.
156   virtual bool isNullValue() const { return Val == 0; }
157
158   // Methods for support type inquiry through isa, cast, and dyn_cast:
159   static inline bool classof(const ConstantFP *) { return true; }
160   static bool classof(const Constant *CPV);  // defined in CPV.cpp
161   static inline bool classof(const Value *V) {
162     return isa<Constant>(V) && classof(cast<Constant>(V));
163   }
164 };
165
166
167 //===---------------------------------------------------------------------------
168 // ConstantArray - Constant Array Declarations
169 //
170 class ConstantArray : public Constant {
171   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
172 protected:
173   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
174   ~ConstantArray() {}
175
176   virtual void destroyConstant();
177 public:
178   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
179   static ConstantArray *get(const std::string &Initializer);
180   
181   inline const ArrayType *getType() const {
182     return (ArrayType*)Value::getType();
183   }
184
185   inline const std::vector<Use> &getValues() const { return Operands; }
186
187   // isNullValue - Return true if this is the value that would be returned by
188   // getNullValue.
189   virtual bool isNullValue() const { return false; }
190
191   // Methods for support type inquiry through isa, cast, and dyn_cast:
192   static inline bool classof(const ConstantArray *) { return true; }
193   static bool classof(const Constant *CPV);  // defined in CPV.cpp
194   static inline bool classof(const Value *V) {
195     return isa<Constant>(V) && classof(cast<Constant>(V));
196   }
197 };
198
199
200 //===---------------------------------------------------------------------------
201 // ConstantStruct - Constant Struct Declarations
202 //
203 class ConstantStruct : public Constant {
204   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
205 protected:
206   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
207   ~ConstantStruct() {}
208
209   virtual void destroyConstant();
210 public:
211   static ConstantStruct *get(const StructType *T,
212                              const std::vector<Constant*> &V);
213
214   inline const StructType *getType() const {
215     return (StructType*)Value::getType();
216   }
217
218   inline const std::vector<Use> &getValues() const { return Operands; }
219
220   // isNullValue - Return true if this is the value that would be returned by
221   // getNullValue.
222   virtual bool isNullValue() const { return false; }
223
224   // Methods for support type inquiry through isa, cast, and dyn_cast:
225   static inline bool classof(const ConstantStruct *) { return true; }
226   static bool classof(const Constant *CPV);  // defined in CPV.cpp
227   static inline bool classof(const Value *V) {
228     return isa<Constant>(V) && classof(cast<Constant>(V));
229   }
230 };
231
232 //===---------------------------------------------------------------------------
233 // ConstantPointer - Constant Pointer Declarations
234 //
235 // The ConstantPointer class represents a null pointer of a specific type. For
236 // a more specific/useful instance, a subclass of ConstantPointer should be
237 // used.
238 //
239 class ConstantPointer : public Constant {
240   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
241 protected:
242   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
243   ~ConstantPointer() {}
244 public:
245   inline const PointerType *getType() const {
246     return (PointerType*)Value::getType();
247   }
248
249   // isNullValue - Return true if this is the value that would be returned by
250   // getNullValue.
251   virtual bool isNullValue() const { return false; }
252
253   // Methods for support type inquiry through isa, cast, and dyn_cast:
254   static inline bool classof(const ConstantPointer *) { return true; }
255   static bool classof(const Constant *CPV);  // defined in Constants.cpp
256   static inline bool classof(const Value *V) {
257     return isa<Constant>(V) && classof(cast<Constant>(V));
258   }
259 };
260
261 // ConstantPointerNull - a constant pointer value that points to null
262 //
263 class ConstantPointerNull : public ConstantPointer {
264   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
265 protected:
266   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
267   inline ~ConstantPointerNull() {}
268 public:
269
270   static ConstantPointerNull *get(const PointerType *T);
271
272   // isNullValue - Return true if this is the value that would be returned by
273   // getNullValue.
274   virtual bool isNullValue() const { return true; }
275
276   // Methods for support type inquiry through isa, cast, and dyn_cast:
277   static inline bool classof(const ConstantPointerNull *) { return true; }
278   static inline bool classof(const ConstantPointer *P) {
279     return (P->getNumOperands() == 0 && P->isNullValue());
280   }
281   static inline bool classof(const Constant *CPV) {
282     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
283   }
284   static inline bool classof(const Value *V) {
285     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
286   }
287 };
288
289
290 // ConstantPointerRef - a constant pointer value that is initialized to
291 // point to a global value, which lies at a constant, fixed address.
292 //
293 class ConstantPointerRef : public ConstantPointer {
294   friend class Module;   // Modules maintain these references
295   ConstantPointerRef(const ConstantPointerRef &); // DNI!
296
297 protected:
298   ConstantPointerRef(GlobalValue *GV);
299   ~ConstantPointerRef() {}
300
301   virtual void destroyConstant() { destroyConstantImpl(); }
302 public:
303   static ConstantPointerRef *get(GlobalValue *GV);
304
305   const GlobalValue *getValue() const { 
306     return cast<GlobalValue>(Operands[0].get());
307   }
308   GlobalValue *getValue() {
309     return cast<GlobalValue>(Operands[0].get());
310   }
311
312   // Methods for support type inquiry through isa, cast, and dyn_cast:
313   static inline bool classof(const ConstantPointerRef *) { return true; }
314   static inline bool classof(const ConstantPointer *CPV) {
315     // check for a single operand (the target value)
316     return (CPV->getNumOperands() == 1);
317   }
318   static inline bool classof(const Constant *CPV) {
319     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
320   }
321   static inline bool classof(const Value *V) {
322     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
323   }
324
325   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
326   // NOT USE THIS!!
327   // Returns the number of uses of OldV that were replaced.
328   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
329   // END WARNING!!
330 };
331
332
333 // ConstantExpr - a constant value that is initialized with
334 // an expression using other constant values.  This is only used
335 // to represent values that cannot be evaluated at compile-time
336 // (e.g., something derived from an address) because it does
337 // not have a mechanism to store the actual value.
338 // Use the appropriate Constant subclass above for known constants.
339 //
340 class ConstantExpr : public Constant {
341   unsigned iType;      // Operation type
342   
343 protected:
344   ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
345   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
346   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
347                const Type *DestTy);
348   ~ConstantExpr() {}
349   
350   virtual void destroyConstant();
351   
352 public:
353   // Static methods to construct a ConstantExpr of different kinds.
354   
355   // Unary constant expr - Use with unary operators and casts
356   static ConstantExpr *get(unsigned Opcode, Constant *C, const Type *Ty);
357
358   // Binary constant expr - Use with binary operators...
359   static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
360
361   // Getelementptr form...
362   static ConstantExpr *getGetElementPtr(Constant *C,
363                                         const std::vector<Constant*> &IdxList);
364   
365   // isNullValue - Return true if this is the value that would be returned by
366   // getNullValue.
367   virtual bool isNullValue() const { return false; }
368   
369   // getOpcode - Return the opcode at the root of this constant expression
370   unsigned getOpcode() const { return iType; }
371
372   // getOpcodeName - Return a string representation for an opcode.
373   const char *getOpcodeName() const;
374   
375   // isConstantExpr - Return true if this is a ConstantExpr
376   virtual bool isConstantExpr() const { return true; }
377   
378   // Methods for support type inquiry through isa, cast, and dyn_cast:
379   static inline bool classof(const ConstantExpr *) { return true; }
380   static inline bool classof(const Constant *CPV) {
381     return CPV->isConstantExpr();
382   }
383   static inline bool classof(const Value *V) {
384     return isa<Constant>(V) && classof(cast<Constant>(V));
385   }
386
387 public:
388   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
389   // NOT USE THIS!!
390   // Returns the number of uses of OldV that were replaced.
391   virtual unsigned mutateReferences(Value* OldV, Value *NewV);
392   // END WARNING!!
393 };
394
395
396 #endif