Remove a ton of extraneous #includes
[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   /// 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 isMaxValue() const = 0;
138   virtual bool isMinValue() const = 0;
139
140   /// Methods for support type inquiry through isa, cast, and dyn_cast:
141   static inline bool classof(const ConstantInt *) { return true; }
142   static bool classof(const Constant *CPV);  // defined in Constants.cpp
143   static inline bool classof(const Value *V) {
144     return isa<Constant>(V) && classof(cast<Constant>(V));
145   }
146 };
147
148
149 //===---------------------------------------------------------------------------
150 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
151 ///
152 class ConstantSInt : public ConstantInt {
153   ConstantSInt(const ConstantSInt &);      // DO NOT IMPLEMENT
154   friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
155
156 protected:
157   ConstantSInt(const Type *Ty, int64_t V);
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   virtual bool isAllOnesValue() const { return getValue() == -1; }
169
170   /// isMaxValue - Return true if this is the largest value that may be
171   /// represented by this type.
172   ///
173   virtual bool isMaxValue() const {
174     int64_t V = getValue();
175     if (V < 0) return false;    // Be careful about wrap-around on 'long's
176     ++V;
177     return !isValueValidForType(getType(), V) || V < 0;
178   }
179
180   /// isMinValue - Return true if this is the smallest value that may be
181   /// represented by this type.
182   ///
183   virtual bool isMinValue() const {
184     int64_t V = getValue();
185     if (V > 0) return false;    // Be careful about wrap-around on 'long's
186     --V;
187     return !isValueValidForType(getType(), V) || V > 0;
188   }
189
190   /// Methods for support type inquiry through isa, cast, and dyn_cast:
191   static inline bool classof(const ConstantSInt *) { return true; }
192   static bool classof(const Constant *CPV);  // defined in Constants.cpp
193   static inline bool classof(const Value *V) {
194     return isa<Constant>(V) && classof(cast<Constant>(V));
195   }
196 };
197
198 //===---------------------------------------------------------------------------
199 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
200 ///
201 class ConstantUInt : public ConstantInt {
202   ConstantUInt(const ConstantUInt &);      // DO NOT IMPLEMENT
203   friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
204 protected:
205   ConstantUInt(const Type *Ty, uint64_t V);
206 public:
207   /// get() - Static factory methods - Return objects of the specified value
208   static ConstantUInt *get(const Type *Ty, uint64_t V);
209
210   /// isValueValidForType - return true if Ty is big enough to represent V.
211   static bool isValueValidForType(const Type *Ty, uint64_t V);
212
213   /// getValue - return the underlying value of this constant.
214   inline uint64_t getValue() const { return Val.Unsigned; }
215
216   /// isMaxValue - Return true if this is the largest value that may be
217   /// represented by this type.
218   ///
219   virtual bool isAllOnesValue() const;
220   virtual bool isMaxValue() const { return isAllOnesValue(); }
221   virtual bool isMinValue() const { return getValue() == 0; }
222
223   /// Methods for support type inquiry through isa, cast, and dyn_cast:
224   static inline bool classof(const ConstantUInt *) { return true; }
225   static bool classof(const Constant *CPV);  // defined in Constants.cpp
226   static inline bool classof(const Value *V) {
227     return isa<Constant>(V) && classof(cast<Constant>(V));
228   }
229 };
230
231
232 //===---------------------------------------------------------------------------
233 /// ConstantFP - Floating Point Values [float, double]
234 ///
235 class ConstantFP : public Constant {
236   double Val;
237   friend struct ConstantCreator<ConstantFP, Type, double>;
238   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
239 protected:
240   ConstantFP(const Type *Ty, double V);
241 public:
242   /// get() - Static factory methods - Return objects of the specified value
243   static ConstantFP *get(const Type *Ty, double V);
244
245   /// isValueValidForType - return true if Ty is big enough to represent V.
246   static bool isValueValidForType(const Type *Ty, double V);
247   inline double getValue() const { return Val; }
248
249   /// isNullValue - Return true if this is the value that would be returned by
250   /// getNullValue.
251   virtual bool isNullValue() const { return Val == 0; }
252
253   /// Methods for support type inquiry through isa, cast, and dyn_cast:
254   static inline bool classof(const ConstantFP *) { 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
262 //===---------------------------------------------------------------------------
263 /// ConstantArray - Constant Array Declarations
264 ///
265 class ConstantArray : public Constant {
266   friend struct ConstantCreator<ConstantArray, ArrayType,
267                                     std::vector<Constant*> >;
268   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
269 protected:
270   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
271   void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
272 public:
273   /// get() - Static factory methods - Return objects of the specified value
274   static ConstantArray *get(const ArrayType *T, const std::vector<Constant*> &);
275   static ConstantArray *get(const std::string &Initializer);
276   
277   /// getType - Specialize the getType() method to always return an ArrayType,
278   /// which reduces the amount of casting needed in parts of the compiler.
279   ///
280   inline const ArrayType *getType() const {
281     return (ArrayType*)Value::getType();
282   }
283
284   /// getAsString - If the sub-element type of this array is either sbyte or
285   /// ubyte, then this method converts the array to an std::string and returns
286   /// it.  Otherwise, it asserts out.
287   ///
288   std::string getAsString() const;
289
290   /// getValues - Return a vector of the component constants that make up this
291   /// array.
292   inline const std::vector<Use> &getValues() const { return Operands; }
293
294   /// isNullValue - Return true if this is the value that would be returned by
295   /// getNullValue.
296   virtual bool isNullValue() const {
297     // FIXME: This should be made to be MUCH faster.  Just check against well
298     // known null value!
299     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
300       if (!cast<Constant>(getOperand(i))->isNullValue())
301         return false; 
302     return true;
303   }
304
305   virtual void destroyConstant();
306   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
307
308   /// Methods for support type inquiry through isa, cast, and dyn_cast:
309   static inline bool classof(const ConstantArray *) { return true; }
310   static bool classof(const Constant *CPV);  // defined in Constants.cpp
311   static inline bool classof(const Value *V) {
312     return isa<Constant>(V) && classof(cast<Constant>(V));
313   }
314 };
315
316
317 //===---------------------------------------------------------------------------
318 // ConstantStruct - Constant Struct Declarations
319 //
320 class ConstantStruct : public Constant {
321   friend struct ConstantCreator<ConstantStruct, StructType,
322                                     std::vector<Constant*> >;
323   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
324 protected:
325   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
326   void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
327 public:
328   /// get() - Static factory methods - Return objects of the specified value
329   static ConstantStruct *get(const StructType *T,
330                              const std::vector<Constant*> &V);
331
332   /// getType() specialization - Reduce amount of casting...
333   inline const StructType *getType() const {
334     return (StructType*)Value::getType();
335   }
336
337   /// getValues - Return a vector of the component constants that make up this
338   /// structure.
339   inline const std::vector<Use> &getValues() const { return Operands; }
340
341   /// isNullValue - Return true if this is the value that would be returned by
342   /// getNullValue.
343   virtual bool isNullValue() const {
344     // FIXME: This should be made to be MUCH faster.  Just check against well
345     // known null value!
346     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
347       if (!cast<Constant>(getOperand(i))->isNullValue())
348         return false; 
349     return true;
350   }
351
352   virtual void destroyConstant();
353   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
354   
355   /// Methods for support type inquiry through isa, cast, and dyn_cast:
356   static inline bool classof(const ConstantStruct *) { return true; }
357   static bool classof(const Constant *CPV);  // defined in Constants.cpp
358   static inline bool classof(const Value *V) {
359     return isa<Constant>(V) && classof(cast<Constant>(V));
360   }
361 };
362
363 //===---------------------------------------------------------------------------
364 /// ConstantPointer - Constant Pointer Declarations
365 ///
366 /// The ConstantPointer class represents a null pointer of a specific type. For
367 /// a more specific/useful instance, a subclass of ConstantPointer should be
368 /// used.
369 ///
370 class ConstantPointer : public Constant {
371   ConstantPointer(const ConstantPointer &);      // DO NOT IMPLEMENT
372 protected:
373   inline ConstantPointer(const PointerType *T) : Constant((const Type*)T) {}
374 public:
375   inline const PointerType *getType() const {
376     return (PointerType*)Value::getType();
377   }
378
379   /// isNullValue - Return true if this is the value that would be returned by
380   /// getNullValue.
381   virtual bool isNullValue() const { return false; }
382
383   /// Methods for support type inquiry through isa, cast, and dyn_cast:
384   static inline bool classof(const ConstantPointer *) { return true; }
385   static bool classof(const Constant *CPV);  // defined in Constants.cpp
386   static inline bool classof(const Value *V) {
387     return isa<Constant>(V) && classof(cast<Constant>(V));
388   }
389 };
390
391 /// ConstantPointerNull - a constant pointer value that points to null
392 ///
393 class ConstantPointerNull : public ConstantPointer {
394   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
395   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
396 protected:
397   ConstantPointerNull(const PointerType *T) : ConstantPointer(T) {}
398   void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
399
400 public:
401
402   /// get() - Static factory methods - Return objects of the specified value
403   static ConstantPointerNull *get(const PointerType *T);
404
405   /// isNullValue - Return true if this is the value that would be returned by
406   /// getNullValue.
407   virtual bool isNullValue() const { return true; }
408
409   virtual void destroyConstant();
410
411   /// Methods for support type inquiry through isa, cast, and dyn_cast:
412   static inline bool classof(const ConstantPointerNull *) { return true; }
413   static inline bool classof(const ConstantPointer *P) {
414     return (P->getNumOperands() == 0 && P->isNullValue());
415   }
416   static inline bool classof(const Constant *CPV) {
417     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
418   }
419   static inline bool classof(const Value *V) {
420     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
421   }
422 };
423
424
425 /// ConstantPointerRef - a constant pointer value that is initialized to
426 /// point to a global value, which lies at a constant, fixed address.
427 ///
428 class ConstantPointerRef : public ConstantPointer {
429   friend class Module;   // Modules maintain these references
430   ConstantPointerRef(const ConstantPointerRef &); // DNI!
431
432 protected:
433   ConstantPointerRef(GlobalValue *GV);
434 public:
435   /// get() - Static factory methods - Return objects of the specified value
436   static ConstantPointerRef *get(GlobalValue *GV);
437
438   const GlobalValue *getValue() const { 
439     return cast<GlobalValue>(Operands[0].get());
440   }
441
442   GlobalValue *getValue() {
443     return cast<GlobalValue>(Operands[0].get());
444   }
445
446   virtual void destroyConstant();
447   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
448
449   /// Methods for support type inquiry through isa, cast, and dyn_cast:
450   static inline bool classof(const ConstantPointerRef *) { return true; }
451   static inline bool classof(const ConstantPointer *CPV) {
452     // check for a single operand (the target value)
453     return (CPV->getNumOperands() == 1);
454   }
455   static inline bool classof(const Constant *CPV) {
456     return isa<ConstantPointer>(CPV) && classof(cast<ConstantPointer>(CPV));
457   }
458   static inline bool classof(const Value *V) {
459     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
460   }
461 };
462
463 // ConstantExpr - a constant value that is initialized with an expression using
464 // other constant values.  This is only used to represent values that cannot be
465 // evaluated at compile-time (e.g., something derived from an address) because
466 // it does not have a mechanism to store the actual value.  Use the appropriate
467 // Constant subclass above for known constants.
468 //
469 class ConstantExpr : public Constant {
470   unsigned iType;      // Operation type (an Instruction opcode)
471   friend struct ConstantCreator<ConstantExpr,Type,
472                             std::pair<unsigned, std::vector<Constant*> > >;
473   
474 protected:
475   // Cast creation ctor
476   ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
477   // Binary/Shift instruction creation ctor
478   ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2);
479   // GEP instruction creation ctor
480   ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
481                const Type *DestTy);
482   void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
483   
484 public:
485   // Static methods to construct a ConstantExpr of different kinds.  Note that
486   // these methods may return a object that is not an instance of the
487   // ConstantExpr class, because they will attempt to fold the constant
488   // expression into something simpler if possible.
489   
490   /// Cast constant expr
491   static Constant *getCast(Constant *C, const Type *Ty);
492
493   /// Binary constant expr - Use with binary operators...
494   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
495
496   /// getShift - Return a shift left or shift right constant expr
497   static Constant *getShift(unsigned Opcode, Constant *C1, Constant *C2);
498
499   /// Getelementptr form...
500   static Constant *getGetElementPtr(Constant *C,
501                                     const std::vector<Constant*> &IdxList);
502   
503   /// isNullValue - Return true if this is the value that would be returned by
504   /// getNullValue.
505   virtual bool isNullValue() const { return false; }
506   
507   /// getOpcode - Return the opcode at the root of this constant expression
508   unsigned getOpcode() const { return iType; }
509
510   /// getOpcodeName - Return a string representation for an opcode.
511   const char *getOpcodeName() const;
512   
513   /// isConstantExpr - Return true if this is a ConstantExpr
514   virtual bool isConstantExpr() const { return true; }
515
516   virtual void destroyConstant();
517   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To);
518     
519   /// Override methods to provide more type information...
520   inline Constant *getOperand(unsigned i) { 
521     return cast<Constant>(User::getOperand(i));
522   }
523   inline Constant *getOperand(unsigned i) const {
524     return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
525   }
526   
527
528   /// Methods for support type inquiry through isa, cast, and dyn_cast:
529   static inline bool classof(const ConstantExpr *) { return true; }
530   static inline bool classof(const Constant *CPV) {
531     return CPV->isConstantExpr();
532   }
533   static inline bool classof(const Value *V) {
534     return isa<Constant>(V) && classof(cast<Constant>(V));
535   }
536 };
537
538 #endif