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