Extend struct and array constants to support isNullValue
[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 //===---------------------------------------------------------------------------
20 /// ConstantIntegral - Shared superclass of boolean and integer constants.
21 ///
22 /// This class just defines some common interfaces to be implemented.
23 ///
24 class ConstantIntegral : public Constant {
25 protected:
26   ConstantIntegral(const Type *Ty) : Constant(Ty) {}
27 public:
28
29   /// isNullValue - Return true if this is the value that would be returned by
30   /// getNullValue.
31   ///
32   virtual bool isNullValue() const = 0;
33
34   /// isMaxValue - Return true if this is the largest value that may be
35   /// represented by this type.
36   ///
37   virtual bool isMaxValue() const = 0;
38
39   /// isMinValue - Return true if this is the smallest value that may be
40   /// represented by this type.
41   ///
42   virtual bool isMinValue() const = 0;
43
44   /// isAllOnesValue - Return true if every bit in this constant is set to true.
45   ///
46   virtual bool isAllOnesValue() const = 0;
47
48   /// Static constructor to get the maximum/minimum/allones constant of
49   /// specified (integral) type...
50   ///
51   static ConstantIntegral *getMaxValue(const Type *Ty);
52   static ConstantIntegral *getMinValue(const Type *Ty);
53   static ConstantIntegral *getAllOnesValue(const Type *Ty);
54
55   /// Methods for support type inquiry through isa, cast, and dyn_cast:
56   static inline bool classof(const ConstantIntegral *) { return true; }
57   static bool classof(const Constant *CPV);  // defined in Constants.cpp
58   static inline bool classof(const Value *V) {
59     return isa<Constant>(V) && classof(cast<Constant>(V));
60   }
61 };
62
63
64 //===---------------------------------------------------------------------------
65 /// ConstantBool - Boolean Values
66 ///
67 class ConstantBool : public ConstantIntegral {
68   bool Val;
69   ConstantBool(bool V);
70   ~ConstantBool() {}
71 public:
72   static ConstantBool *True, *False;  // The True & False values
73
74   /// get() - Static factory methods - Return objects of the specified value
75   static ConstantBool *get(bool Value) { return Value ? True : False; }
76   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
77
78   /// inverted - Return the opposite value of the current value.
79   inline ConstantBool *inverted() const { return (this==True) ? False : True; }
80
81   /// getValue - return the boolean value of this constant.
82   ///
83   inline bool getValue() const { return Val; }
84
85   /// isNullValue - Return true if this is the value that would be returned by
86   /// getNullValue.
87   ///
88   virtual bool isNullValue() const { return this == False; }
89   virtual bool isMaxValue() const { return this == True; }
90   virtual bool isMinValue() const { return this == False; }
91   virtual bool isAllOnesValue() const { return this == True; }
92
93   /// Methods for support type inquiry through isa, cast, and dyn_cast:
94   static inline bool classof(const ConstantBool *) { return true; }
95   static bool classof(const Constant *CPV) {
96     return (CPV == True) | (CPV == False);
97   }
98   static inline bool classof(const Value *V) {
99     return isa<Constant>(V) && classof(cast<Constant>(V));
100   }
101 };
102
103
104 //===---------------------------------------------------------------------------
105 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
106 /// with integral constants easier.
107 ///
108 class ConstantInt : public ConstantIntegral {
109 protected:
110   union {
111     int64_t  Signed;
112     uint64_t Unsigned;
113   } Val;
114   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
115   ConstantInt(const Type *Ty, uint64_t V);
116   ~ConstantInt() {}
117 public:
118   /// equalsInt - Provide a helper method that can be used to determine if the
119   /// constant contained within is equal to a constant.  This only works for
120   /// very small values, because this is all that can be represented with all
121   /// types.
122   ///
123   bool equalsInt(unsigned char V) const {
124     assert(V <= 127 &&
125            "equals: Can only be used with very small positive constants!");
126     return Val.Unsigned == V;
127   }
128
129   /// ConstantInt::get static method: return a ConstantInt with the specified
130   /// value.  as above, we work only with very small values here.
131   ///
132   static ConstantInt *get(const Type *Ty, unsigned char V);
133
134   /// isNullValue - Return true if this is the value that would be returned by
135   /// getNullValue.
136   virtual bool isNullValue() const { return Val.Unsigned == 0; }
137   virtual bool isAllOnesValue() const { return Val.Signed == -1; }
138   virtual bool isMaxValue() const = 0;
139   virtual bool isMinValue() const = 0;
140
141   /// Methods for support type inquiry through isa, cast, and dyn_cast:
142   static inline bool classof(const ConstantInt *) { return true; }
143   static bool classof(const Constant *CPV);  // defined in Constants.cpp
144   static inline bool classof(const Value *V) {
145     return isa<Constant>(V) && classof(cast<Constant>(V));
146   }
147 };
148
149
150 //===---------------------------------------------------------------------------
151 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
152 ///
153 class ConstantSInt : public ConstantInt {
154   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
155 protected:
156   ConstantSInt(const Type *Ty, int64_t V);
157   ~ConstantSInt() {}
158 public:
159   /// get() - Static factory methods - Return objects of the specified value
160   static ConstantSInt *get(const Type *Ty, int64_t V);
161
162   /// isValueValidForType - return true if Ty is big enough to represent V.
163   static bool isValueValidForType(const Type *Ty, int64_t V);
164
165   /// getValue - return the underlying value of this constant.
166   inline int64_t getValue() const { return Val.Signed; }
167
168   /// isMaxValue - Return true if this is the largest value that may be
169   /// represented by this type.
170   ///
171   virtual bool isMaxValue() const {
172     int64_t V = getValue();
173     if (V < 0) return false;    // Be careful about wrap-around on 'long's
174     ++V;
175     return !isValueValidForType(getType(), V) || V < 0;
176   }
177
178   /// isMinValue - Return true if this is the smallest value that may be
179   /// represented by this type.
180   ///
181   virtual bool isMinValue() const {
182     int64_t V = getValue();
183     if (V > 0) return false;    // Be careful about wrap-around on 'long's
184     --V;
185     return !isValueValidForType(getType(), V) || V > 0;
186   }
187
188   /// Methods for support type inquiry through isa, cast, and dyn_cast:
189   static inline bool classof(const ConstantSInt *) { return true; }
190   static bool classof(const Constant *CPV);  // defined in Constants.cpp
191   static inline bool classof(const Value *V) {
192     return isa<Constant>(V) && classof(cast<Constant>(V));
193   }
194 };
195
196 //===---------------------------------------------------------------------------
197 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
198 ///
199 class ConstantUInt : public ConstantInt {
200   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
201 protected:
202   ConstantUInt(const Type *Ty, uint64_t V);
203   ~ConstantUInt() {}
204 public:
205   /// get() - Static factory methods - Return objects of the specified value
206   static ConstantUInt *get(const Type *Ty, uint64_t V);
207
208   /// isValueValidForType - return true if Ty is big enough to represent V.
209   static bool isValueValidForType(const Type *Ty, uint64_t V);
210
211   /// getValue - return the underlying value of this constant.
212   inline uint64_t getValue() const { return Val.Unsigned; }
213
214   /// isMaxValue - Return true if this is the largest value that may be
215   /// represented by this type.
216   ///
217   virtual bool isMaxValue() const { return isAllOnesValue(); }
218   virtual bool isMinValue() const { return getValue() == 0; }
219
220   /// Methods for support type inquiry through isa, cast, and dyn_cast:
221   static inline bool classof(const ConstantUInt *) { return true; }
222   static bool classof(const Constant *CPV);  // defined in Constants.cpp
223   static inline bool classof(const Value *V) {
224     return isa<Constant>(V) && classof(cast<Constant>(V));
225   }
226 };
227
228
229 //===---------------------------------------------------------------------------
230 /// ConstantFP - Floating Point Values [float, double]
231 ///
232 class ConstantFP : public Constant {
233   double Val;
234   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
235 protected:
236   ConstantFP(const Type *Ty, double V);
237   ~ConstantFP() {}
238 public:
239   /// get() - Static factory methods - Return objects of the specified value
240   static ConstantFP *get(const Type *Ty, double V);
241
242   /// isValueValidForType - return true if Ty is big enough to represent V.
243   static bool isValueValidForType(const Type *Ty, double V);
244   inline double getValue() const { return Val; }
245
246   /// isNullValue - Return true if this is the value that would be returned by
247   /// getNullValue.
248   virtual bool isNullValue() const { return Val == 0; }
249
250   /// Methods for support type inquiry through isa, cast, and dyn_cast:
251   static inline bool classof(const ConstantFP *) { return true; }
252   static bool classof(const Constant *CPV);  // defined in Constants.cpp
253   static inline bool classof(const Value *V) {
254     return isa<Constant>(V) && classof(cast<Constant>(V));
255   }
256 };
257
258
259 //===---------------------------------------------------------------------------
260 /// ConstantArray - Constant Array Declarations
261 ///
262 class ConstantArray : public Constant {
263   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
264 protected:
265   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
266   ~ConstantArray() {}
267
268 public:
269   /// get() - Static factory methods - Return objects of the specified value
270   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
271   static ConstantArray *get(const std::string &Initializer);
272   
273   /// getType - Specialize the getType() method to always return an ArrayType,
274   /// which reduces the amount of casting needed in parts of the compiler.
275   ///
276   inline const ArrayType *getType() const {
277     return (ArrayType*)Value::getType();
278   }
279
280   /// getAsString - If the sub-element type of this array is either sbyte or
281   /// ubyte, then this method converts the array to an std::string and returns
282   /// it.  Otherwise, it asserts out.
283   ///
284   std::string getAsString() const;
285
286   /// getValues - Return a vector of the component constants that make up this
287   /// array.
288   inline const std::vector<Use> &getValues() const { return Operands; }
289
290   /// isNullValue - Return true if this is the value that would be returned by
291   /// getNullValue.
292   virtual bool isNullValue() const {
293     // FIXME: This should be made to be MUCH faster.  Just check against well
294     // known null value!
295     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
296       if (!cast<Constant>(getOperand(i))->isNullValue())
297         return false; 
298     return true;
299   }
300
301   virtual void destroyConstant();
302   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
303
304   /// Methods for support type inquiry through isa, cast, and dyn_cast:
305   static inline bool classof(const ConstantArray *) { return true; }
306   static bool classof(const Constant *CPV);  // defined in Constants.cpp
307   static inline bool classof(const Value *V) {
308     return isa<Constant>(V) && classof(cast<Constant>(V));
309   }
310 };
311
312
313 //===---------------------------------------------------------------------------
314 // ConstantStruct - Constant Struct Declarations
315 //
316 class ConstantStruct : public Constant {
317   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
318 protected:
319   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
320   ~ConstantStruct() {}
321
322 public:
323   /// get() - Static factory methods - Return objects of the specified value
324   static ConstantStruct *get(const StructType *T,
325                              const std::vector<Constant*> &V);
326
327   /// getType() specialization - Reduce amount of casting...
328   inline const StructType *getType() const {
329     return (StructType*)Value::getType();
330   }
331
332   /// getValues - Return a vector of the component constants that make up this
333   /// structure.
334   inline const std::vector<Use> &getValues() const { return Operands; }
335
336   /// isNullValue - Return true if this is the value that would be returned by
337   /// getNullValue.
338   virtual bool isNullValue() const {
339     // FIXME: This should be made to be MUCH faster.  Just check against well
340     // known null value!
341     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
342       if (!cast<Constant>(getOperand(i))->isNullValue())
343         return false; 
344     return true;
345   }
346
347   virtual void destroyConstant();
348   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
349   
350   /// Methods for support type inquiry through isa, cast, and dyn_cast:
351   static inline bool classof(const ConstantStruct *) { return true; }
352   static bool classof(const Constant *CPV);  // defined in Constants.cpp
353   static inline bool classof(const Value *V) {
354     return isa<Constant>(V) && classof(cast<Constant>(V));
355   }
356 };
357
358 //===---------------------------------------------------------------------------
359 /// ConstantPointer - Constant Pointer Declarations
360 ///
361 /// The ConstantPointer class represents a null pointer of a specific type. For
362 /// a more specific/useful instance, a subclass of ConstantPointer should be
363 /// used.
364 ///
365 class ConstantPointer : public Constant {
366   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
367 protected:
368   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T){}
369   ~ConstantPointer() {}
370 public:
371   inline const PointerType *getType() const {
372     return (PointerType*)Value::getType();
373   }
374
375   /// isNullValue - Return true if this is the value that would be returned by
376   /// getNullValue.
377   virtual bool isNullValue() const { return false; }
378
379   /// Methods for support type inquiry through isa, cast, and dyn_cast:
380   static inline bool classof(const ConstantPointer *) { return true; }
381   static bool classof(const Constant *CPV);  // defined in Constants.cpp
382   static inline bool classof(const Value *V) {
383     return isa<Constant>(V) && classof(cast<Constant>(V));
384   }
385 };
386
387 /// ConstantPointerNull - a constant pointer value that points to null
388 ///
389 class ConstantPointerNull : public ConstantPointer {
390   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
391 protected:
392   inline ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
393   inline ~ConstantPointerNull() {}
394 public:
395
396   /// get() - Static factory methods - Return objects of the specified value
397   static ConstantPointerNull *get(const PointerType *T);
398
399   /// isNullValue - Return true if this is the value that would be returned by
400   /// getNullValue.
401   virtual bool isNullValue() const { return true; }
402
403   virtual void destroyConstant();
404
405   /// Methods for support type inquiry through isa, cast, and dyn_cast:
406   static inline bool classof(const ConstantPointerNull *) { return true; }
407   static inline bool classof(const ConstantPointer *P) {
408     return (P->getNumOperands() == 0 && P->isNullValue());
409   }
410   static inline bool classof(const Constant *CPV) {
411     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
412   }
413   static inline bool classof(const Value *V) {
414     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
415   }
416 };
417
418
419 /// ConstantPointerRef - a constant pointer value that is initialized to
420 /// point to a global value, which lies at a constant, fixed address.
421 ///
422 class ConstantPointerRef : public ConstantPointer {
423   friend class Module;   // Modules maintain these references
424   ConstantPointerRef(const ConstantPointerRef &); // DNI!
425
426 protected:
427   ConstantPointerRef(GlobalValue *GV);
428   ~ConstantPointerRef() {}
429 public:
430   /// get() - Static factory methods - Return objects of the specified value
431   static ConstantPointerRef *get(GlobalValue *GV);
432
433   const GlobalValue *getValue() const { 
434     return cast<GlobalValue>(Operands[0].get());
435   }
436
437   GlobalValue *getValue() {
438     return cast<GlobalValue>(Operands[0].get());
439   }
440
441   virtual void destroyConstant();
442   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
443
444   /// Methods for support type inquiry through isa, cast, and dyn_cast:
445   static inline bool classof(const ConstantPointerRef *) { return true; }
446   static inline bool classof(const ConstantPointer *CPV) {
447     // check for a single operand (the target value)
448     return (CPV->getNumOperands() == 1);
449   }
450   static inline bool classof(const Constant *CPV) {
451     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
452   }
453   static inline bool classof(const Value *V) {
454     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
455   }
456 };
457
458
459 // ConstantExpr - a constant value that is initialized with
460 // an expression using other constant values.  This is only used
461 // to represent values that cannot be evaluated at compile-time
462 // (e.g., something derived from an address) because it does
463 // not have a mechanism to store the actual value.
464 // Use the appropriate Constant subclass above for known constants.
465 //
466 class ConstantExpr : public Constant {
467   unsigned iType;      // Operation type
468   
469 protected:
470   ConstantExpr(unsigned Opcode, Constant *C,  const Type *Ty);
471   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
472   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
473                const Type *DestTy);
474   ~ConstantExpr() {}
475   
476 public:
477   // Static methods to construct a ConstantExpr of different kinds.
478   
479   /// Cast constant expr
480   static ConstantExpr *getCast(Constant *C, const Type *Ty);
481
482   /// Binary constant expr - Use with binary operators...
483   static ConstantExpr *get(unsigned Opcode, Constant *C1, Constant *C2);
484
485   /// Getelementptr form...
486   static ConstantExpr *getGetElementPtr(Constant *C,
487                                         const std::vector<Constant*> &IdxList);
488   
489   /// isNullValue - Return true if this is the value that would be returned by
490   /// getNullValue.
491   virtual bool isNullValue() const { return false; }
492   
493   /// getOpcode - Return the opcode at the root of this constant expression
494   unsigned getOpcode() const { return iType; }
495
496   /// getOpcodeName - Return a string representation for an opcode.
497   const char *getOpcodeName() const;
498   
499   /// isConstantExpr - Return true if this is a ConstantExpr
500   virtual bool isConstantExpr() const { return true; }
501
502   virtual void destroyConstant();
503   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
504     
505   /// Methods for support type inquiry through isa, cast, and dyn_cast:
506   static inline bool classof(const ConstantExpr *) { return true; }
507   static inline bool classof(const Constant *CPV) {
508     return CPV->isConstantExpr();
509   }
510   static inline bool classof(const Value *V) {
511     return isa<Constant>(V) && classof(cast<Constant>(V));
512   }
513 };
514
515 #endif