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