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