2c7f577e34930c1f49e1c4b67b69203046b0966d
[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 /// @file This file contains the declarations for the subclasses of Constant, 
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are 
13 /// fully shared by structural equivalence.  This means that two structurally
14 /// equivalent constants will always have the same address.  Constant's are
15 /// created on demand as needed and never deleted: thus clients don't have to
16 /// worry about the lifetime of the objects.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CONSTANTS_H
21 #define LLVM_CONSTANTS_H
22
23 #include "llvm/Constant.h"
24 #include "llvm/Type.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/APFloat.h"
27
28 namespace llvm {
29
30 class ArrayType;
31 class StructType;
32 class PointerType;
33 class VectorType;
34
35 template<class ConstantClass, class TypeClass, class ValType>
36 struct ConstantCreator;
37 template<class ConstantClass, class TypeClass>
38 struct ConvertConstantType;
39
40 //===----------------------------------------------------------------------===//
41 /// This is the shared class of boolean and integer constants. This class 
42 /// represents both boolean and integral constants.
43 /// @brief Class for constant integers.
44 class ConstantInt : public Constant {
45   static ConstantInt *TheTrueVal, *TheFalseVal;
46   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
47   ConstantInt(const IntegerType *Ty, const APInt& V);
48   APInt Val;
49 protected:
50   static void destroyThis(ConstantInt*v) {
51     Constant::destroyThis(v);
52   }
53   friend class Value;
54 public:
55   /// Return the constant as an APInt value reference. This allows clients to
56   /// obtain a copy of the value, with all its precision in tact.
57   /// @brief Return the constant's value.
58   inline const APInt& getValue() const {
59     return Val;
60   }
61   
62   /// getBitWidth - Return the bitwidth of this constant.
63   unsigned getBitWidth() const { return Val.getBitWidth(); }
64
65   /// Return the constant as a 64-bit unsigned integer value after it
66   /// has been zero extended as appropriate for the type of this constant. Note
67   /// that this method can assert if the value does not fit in 64 bits.
68   /// @deprecated
69   /// @brief Return the zero extended value.
70   inline uint64_t getZExtValue() const {
71     return Val.getZExtValue();
72   }
73
74   /// Return the constant as a 64-bit integer value after it has been sign
75   /// sign extended as appropriate for the type of this constant. Note that
76   /// this method can assert if the value does not fit in 64 bits.
77   /// @deprecated
78   /// @brief Return the sign extended value.
79   inline int64_t getSExtValue() const {
80     return Val.getSExtValue();
81   }
82
83   /// A helper method that can be used to determine if the constant contained 
84   /// within is equal to a constant.  This only works for very small values, 
85   /// because this is all that can be represented with all types.
86   /// @brief Determine if this constant's value is same as an unsigned char.
87   bool equalsInt(uint64_t V) const {
88     return Val == V;
89   }
90
91   /// getTrue/getFalse - Return the singleton true/false values.
92   static inline ConstantInt *getTrue() {
93     if (TheTrueVal) return TheTrueVal;
94     return CreateTrueFalseVals(true);
95   }
96   static inline ConstantInt *getFalse() {
97     if (TheFalseVal) return TheFalseVal;
98     return CreateTrueFalseVals(false);
99   }
100
101   /// Return a ConstantInt with the specified value for the specified type. The
102   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
103   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
104   /// signed value for the type Ty.
105   /// @brief Get a ConstantInt for a specific value.
106   static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
107
108   /// Return a ConstantInt with the specified value and an implied Type. The
109   /// type is the integer type that corresponds to the bit width of the value.
110   static ConstantInt *get(const APInt &V);
111
112   /// getType - Specialize the getType() method to always return an IntegerType,
113   /// which reduces the amount of casting needed in parts of the compiler.
114   ///
115   inline const IntegerType *getType() const {
116     return reinterpret_cast<const IntegerType*>(Value::getType());
117   }
118
119   /// This static method returns true if the type Ty is big enough to 
120   /// represent the value V. This can be used to avoid having the get method 
121   /// assert when V is larger than Ty can represent. Note that there are two
122   /// versions of this method, one for unsigned and one for signed integers.
123   /// Although ConstantInt canonicalizes everything to an unsigned integer, 
124   /// the signed version avoids callers having to convert a signed quantity
125   /// to the appropriate unsigned type before calling the method.
126   /// @returns true if V is a valid value for type Ty
127   /// @brief Determine if the value is in range for the given type.
128   static bool isValueValidForType(const Type *Ty, uint64_t V);
129   static bool isValueValidForType(const Type *Ty, int64_t V);
130
131   /// This function will return true iff this constant represents the "null"
132   /// value that would be returned by the getNullValue method.
133   /// @returns true if this is the null integer value.
134   /// @brief Determine if the value is null.
135   virtual bool isNullValue() const { 
136     return Val == 0; 
137   }
138
139   /// This is just a convenience method to make client code smaller for a
140   /// common code. It also correctly performs the comparison without the
141   /// potential for an assertion from getZExtValue().
142   bool isZero() const {
143     return Val == 0;
144   }
145
146   /// This is just a convenience method to make client code smaller for a 
147   /// common case. It also correctly performs the comparison without the
148   /// potential for an assertion from getZExtValue().
149   /// @brief Determine if the value is one.
150   bool isOne() const {
151     return Val == 1;
152   }
153
154   /// This function will return true iff every bit in this constant is set
155   /// to true.
156   /// @returns true iff this constant's bits are all set to true.
157   /// @brief Determine if the value is all ones.
158   bool isAllOnesValue() const { 
159     return Val.isAllOnesValue();
160   }
161
162   /// This function will return true iff this constant represents the largest
163   /// value that may be represented by the constant's type.
164   /// @returns true iff this is the largest value that may be represented 
165   /// by this type.
166   /// @brief Determine if the value is maximal.
167   bool isMaxValue(bool isSigned) const {
168     if (isSigned) 
169       return Val.isMaxSignedValue();
170     else
171       return Val.isMaxValue();
172   }
173
174   /// This function will return true iff this constant represents the smallest
175   /// value that may be represented by this constant's type.
176   /// @returns true if this is the smallest value that may be represented by 
177   /// this type.
178   /// @brief Determine if the value is minimal.
179   bool isMinValue(bool isSigned) const {
180     if (isSigned) 
181       return Val.isMinSignedValue();
182     else
183       return Val.isMinValue();
184   }
185
186   /// This function will return true iff this constant represents a value with
187   /// active bits bigger than 64 bits or a value greater than the given uint64_t
188   /// value.
189   /// @returns true iff this constant is greater or equal to the given number.
190   /// @brief Determine if the value is greater or equal to the given number.
191   bool uge(uint64_t Num) {
192     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
193   }
194
195   /// @returns the 64-bit value of this constant if its active bits number is 
196   /// not greater than 64, otherwise, just return the given uint64_t number.
197   /// @brief Get the constant's value if possible.
198   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
199     return Val.getLimitedValue(Limit);
200   }
201
202   /// @returns the value for an integer constant of the given type that has all
203   /// its bits set to true.
204   /// @brief Get the all ones value
205   static ConstantInt *getAllOnesValue(const Type *Ty);
206
207   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
208   static inline bool classof(const ConstantInt *) { return true; }
209   static bool classof(const Value *V) {
210     return V->getValueID() == ConstantIntVal;
211   }
212   static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
213 private:
214   static ConstantInt *CreateTrueFalseVals(bool WhichOne);
215 };
216
217
218 //===----------------------------------------------------------------------===//
219 /// ConstantFP - Floating Point Values [float, double]
220 ///
221 class ConstantFP : public Constant {
222   APFloat Val;
223   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
224 protected:
225   ConstantFP(const Type *Ty, const APFloat& V);
226   static void destroyThis(ConstantFP*v) {
227     Constant::destroyThis(v);
228   }
229   friend class Value;
230 public:
231   /// get() - Static factory methods - Return objects of the specified value
232   static ConstantFP *get(const Type *Ty, const APFloat& V);
233
234   /// isValueValidForType - return true if Ty is big enough to represent V.
235   static bool isValueValidForType(const Type *Ty, const APFloat& V);
236   inline const APFloat& getValueAPF() const { return Val; }
237
238   /// isNullValue - Return true if this is the value that would be returned by
239   /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
240   /// considers -0.0 to be null as well as 0.0.  :(
241   virtual bool isNullValue() const;
242
243   // Get a negative zero.
244   static ConstantFP *getNegativeZero(const Type* Ty);
245
246   /// isExactlyValue - We don't rely on operator== working on double values, as
247   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
248   /// As such, this method can be used to do an exact bit-for-bit comparison of
249   /// two floating point values.  The version with a double operand is retained
250   /// because it's so convenient to write isExactlyValue(2.0), but please use
251   /// it only for constants.
252   bool isExactlyValue(const APFloat& V) const;
253
254   bool isExactlyValue(double V) const {
255     if (&Val.getSemantics() == &APFloat::IEEEdouble)
256       return isExactlyValue(APFloat(V));
257     else if (&Val.getSemantics() == &APFloat::IEEEsingle)
258       return isExactlyValue(APFloat((float)V));
259     assert(0);
260     return false;
261   }
262   /// Methods for support type inquiry through isa, cast, and dyn_cast:
263   static inline bool classof(const ConstantFP *) { return true; }
264   static bool classof(const Value *V) {
265     return V->getValueID() == ConstantFPVal;
266   }
267 };
268
269 //===----------------------------------------------------------------------===//
270 /// ConstantAggregateZero - All zero aggregate value
271 ///
272 class ConstantAggregateZero : public Constant {
273   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
274   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
275 protected:
276   explicit ConstantAggregateZero(const Type *Ty)
277     : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
278
279   static void destroyThis(ConstantAggregateZero*v) {
280     Constant::destroyThis(v);
281   }
282   friend class Value;
283 public:
284   /// get() - static factory method for creating a null aggregate.  It is
285   /// illegal to call this method with a non-aggregate type.
286   static Constant *get(const Type *Ty);
287
288   /// isNullValue - Return true if this is the value that would be returned by
289   /// getNullValue.
290   virtual bool isNullValue() const { return true; }
291
292   virtual void destroyConstant();
293
294   /// Methods for support type inquiry through isa, cast, and dyn_cast:
295   ///
296   static bool classof(const ConstantAggregateZero *) { return true; }
297   static bool classof(const Value *V) {
298     return V->getValueID() == ConstantAggregateZeroVal;
299   }
300 };
301
302
303 //===----------------------------------------------------------------------===//
304 /// ConstantArray - Constant Array Declarations
305 ///
306 class ConstantArray : public Constant {
307   friend struct ConstantCreator<ConstantArray, ArrayType,
308                                     std::vector<Constant*> >;
309   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
310 protected:
311   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
312   static void destroyThis(ConstantArray*);
313   friend class Value;
314 public:
315   /// get() - Static factory methods - Return objects of the specified value
316   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
317   static Constant *get(const ArrayType *T,
318                        Constant*const*Vals, unsigned NumVals) {
319     // FIXME: make this the primary ctor method.
320     return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
321   }
322
323   /// This method constructs a ConstantArray and initializes it with a text
324   /// string. The default behavior (AddNull==true) causes a null terminator to
325   /// be placed at the end of the array. This effectively increases the length
326   /// of the array by one (you've been warned).  However, in some situations 
327   /// this is not desired so if AddNull==false then the string is copied without
328   /// null termination. 
329   static Constant *get(const std::string &Initializer, bool AddNull = true);
330
331   /// getType - Specialize the getType() method to always return an ArrayType,
332   /// which reduces the amount of casting needed in parts of the compiler.
333   ///
334   inline const ArrayType *getType() const {
335     return reinterpret_cast<const ArrayType*>(Value::getType());
336   }
337
338   /// isString - This method returns true if the array is an array of sbyte or
339   /// ubyte, and if the elements of the array are all ConstantInt's.
340   bool isString() const;
341
342   /// isCString - This method returns true if the array is a string (see
343   /// @verbatim
344   /// isString) and it ends in a null byte \0 and does not contains any other
345   /// @endverbatim
346   /// null bytes except its terminator.
347   bool isCString() const;
348
349   /// getAsString - If this array is isString(), then this method converts the
350   /// array to an std::string and returns it.  Otherwise, it asserts out.
351   ///
352   std::string getAsString() const;
353
354   /// isNullValue - Return true if this is the value that would be returned by
355   /// getNullValue.  This always returns false because zero arrays are always
356   /// created as ConstantAggregateZero objects.
357   virtual bool isNullValue() const { return false; }
358
359   virtual void destroyConstant();
360   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
361
362   /// Methods for support type inquiry through isa, cast, and dyn_cast:
363   static inline bool classof(const ConstantArray *) { return true; }
364   static bool classof(const Value *V) {
365     return V->getValueID() == ConstantArrayVal;
366   }
367 };
368
369
370 //===----------------------------------------------------------------------===//
371 // ConstantStruct - Constant Struct Declarations
372 //
373 class ConstantStruct : public Constant {
374   friend struct ConstantCreator<ConstantStruct, StructType,
375                                     std::vector<Constant*> >;
376   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
377 protected:
378   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
379   static void destroyThis(ConstantStruct*);
380   friend class Value;
381 public:
382   /// get() - Static factory methods - Return objects of the specified value
383   ///
384   static Constant *get(const StructType *T, const std::vector<Constant*> &V);
385   static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
386   static Constant *get(Constant*const* Vals, unsigned NumVals,
387                        bool Packed = false) {
388     // FIXME: make this the primary ctor method.
389     return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
390   }
391   
392   /// getType() specialization - Reduce amount of casting...
393   ///
394   inline const StructType *getType() const {
395     return reinterpret_cast<const StructType*>(Value::getType());
396   }
397
398   /// isNullValue - Return true if this is the value that would be returned by
399   /// getNullValue.  This always returns false because zero structs are always
400   /// created as ConstantAggregateZero objects.
401   virtual bool isNullValue() const {
402     return false;
403   }
404
405   virtual void destroyConstant();
406   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
407
408   /// Methods for support type inquiry through isa, cast, and dyn_cast:
409   static inline bool classof(const ConstantStruct *) { return true; }
410   static bool classof(const Value *V) {
411     return V->getValueID() == ConstantStructVal;
412   }
413 };
414
415 //===----------------------------------------------------------------------===//
416 /// ConstantVector - Constant Vector Declarations
417 ///
418 class ConstantVector : public Constant {
419   friend struct ConstantCreator<ConstantVector, VectorType,
420                                     std::vector<Constant*> >;
421   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
422 protected:
423   ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
424   static void destroyThis(ConstantVector*v);
425   friend class Value;
426 public:
427   /// get() - Static factory methods - Return objects of the specified value
428   static Constant *get(const VectorType *T, const std::vector<Constant*> &);
429   static Constant *get(const std::vector<Constant*> &V);
430   static Constant *get(Constant*const* Vals, unsigned NumVals) {
431     // FIXME: make this the primary ctor method.
432     return get(std::vector<Constant*>(Vals, Vals+NumVals));
433   }
434   
435   /// getType - Specialize the getType() method to always return a VectorType,
436   /// which reduces the amount of casting needed in parts of the compiler.
437   ///
438   inline const VectorType *getType() const {
439     return reinterpret_cast<const VectorType*>(Value::getType());
440   }
441
442   /// @returns the value for a vector integer constant of the given type that
443   /// has all its bits set to true.
444   /// @brief Get the all ones value
445   static ConstantVector *getAllOnesValue(const VectorType *Ty);
446   
447   /// isNullValue - Return true if this is the value that would be returned by
448   /// getNullValue.  This always returns false because zero vectors are always
449   /// created as ConstantAggregateZero objects.
450   virtual bool isNullValue() const { return false; }
451
452   /// This function will return true iff every element in this vector constant
453   /// is set to all ones.
454   /// @returns true iff this constant's emements are all set to all ones.
455   /// @brief Determine if the value is all ones.
456   bool isAllOnesValue() const;
457
458   /// getSplatValue - If this is a splat constant, meaning that all of the
459   /// elements have the same value, return that value. Otherwise return NULL.
460   Constant *getSplatValue();
461
462   virtual void destroyConstant();
463   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
464
465   /// Methods for support type inquiry through isa, cast, and dyn_cast:
466   static inline bool classof(const ConstantVector *) { return true; }
467   static bool classof(const Value *V) {
468     return V->getValueID() == ConstantVectorVal;
469   }
470 };
471
472 //===----------------------------------------------------------------------===//
473 /// ConstantPointerNull - a constant pointer value that points to null
474 ///
475 class ConstantPointerNull : public Constant {
476   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
477   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
478 protected:
479   explicit ConstantPointerNull(const PointerType *T)
480     : Constant(reinterpret_cast<const Type*>(T),
481                Value::ConstantPointerNullVal, 0, 0) {}
482   static void destroyThis(ConstantPointerNull*v) {
483     Constant::destroyThis(v);
484   }
485   friend class Value;
486 public:
487
488   /// get() - Static factory methods - Return objects of the specified value
489   static ConstantPointerNull *get(const PointerType *T);
490
491   /// isNullValue - Return true if this is the value that would be returned by
492   /// getNullValue.
493   virtual bool isNullValue() const { return true; }
494
495   virtual void destroyConstant();
496
497   /// getType - Specialize the getType() method to always return an PointerType,
498   /// which reduces the amount of casting needed in parts of the compiler.
499   ///
500   inline const PointerType *getType() const {
501     return reinterpret_cast<const PointerType*>(Value::getType());
502   }
503
504   /// Methods for support type inquiry through isa, cast, and dyn_cast:
505   static inline bool classof(const ConstantPointerNull *) { return true; }
506   static bool classof(const Value *V) {
507     return V->getValueID() == ConstantPointerNullVal;
508   }
509 };
510
511
512 /// ConstantExpr - a constant value that is initialized with an expression using
513 /// other constant values.
514 ///
515 /// This class uses the standard Instruction opcodes to define the various
516 /// constant expressions.  The Opcode field for the ConstantExpr class is
517 /// maintained in the Value::SubclassData field.
518 class ConstantExpr : public Constant {
519   friend struct ConstantCreator<ConstantExpr,Type,
520                             std::pair<unsigned, std::vector<Constant*> > >;
521   friend struct ConvertConstantType<ConstantExpr, Type>;
522
523 protected:
524   ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
525     : Constant(Ty, ConstantExprVal, Ops, NumOps) {
526     // Operation type (an Instruction opcode) is stored as the SubclassData.
527     SubclassData = Opcode;
528   }
529
530   // These private methods are used by the type resolution code to create
531   // ConstantExprs in intermediate forms.
532   static Constant *getTy(const Type *Ty, unsigned Opcode,
533                          Constant *C1, Constant *C2);
534   static Constant *getCompareTy(unsigned short pred, Constant *C1, 
535                                 Constant *C2);
536   static Constant *getSelectTy(const Type *Ty,
537                                Constant *C1, Constant *C2, Constant *C3);
538   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
539                                       Value* const *Idxs, unsigned NumIdxs);
540   static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
541                                        Constant *Idx);
542   static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
543                                       Constant *Elt, Constant *Idx);
544   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
545                                       Constant *V2, Constant *Mask);
546
547   static void destroyThis(ConstantExpr* v) {
548     Constant::destroyThis(v);
549   }
550   friend class Value;
551 public:
552   // Static methods to construct a ConstantExpr of different kinds.  Note that
553   // these methods may return a object that is not an instance of the
554   // ConstantExpr class, because they will attempt to fold the constant
555   // expression into something simpler if possible.
556
557   /// Cast constant expr
558   ///
559   static Constant *getTrunc   (Constant *C, const Type *Ty);
560   static Constant *getSExt    (Constant *C, const Type *Ty);
561   static Constant *getZExt    (Constant *C, const Type *Ty);
562   static Constant *getFPTrunc (Constant *C, const Type *Ty);
563   static Constant *getFPExtend(Constant *C, const Type *Ty);
564   static Constant *getUIToFP  (Constant *C, const Type *Ty);
565   static Constant *getSIToFP  (Constant *C, const Type *Ty);
566   static Constant *getFPToUI  (Constant *C, const Type *Ty);
567   static Constant *getFPToSI  (Constant *C, const Type *Ty);
568   static Constant *getPtrToInt(Constant *C, const Type *Ty);
569   static Constant *getIntToPtr(Constant *C, const Type *Ty);
570   static Constant *getBitCast (Constant *C, const Type *Ty);
571
572   // @brief Convenience function for getting one of the casting operations
573   // using a CastOps opcode.
574   static Constant *getCast(
575     unsigned ops,  ///< The opcode for the conversion
576     Constant *C,   ///< The constant to be converted
577     const Type *Ty ///< The type to which the constant is converted
578   );
579
580   // @brief Create a ZExt or BitCast cast constant expression
581   static Constant *getZExtOrBitCast(
582     Constant *C,   ///< The constant to zext or bitcast
583     const Type *Ty ///< The type to zext or bitcast C to
584   );
585
586   // @brief Create a SExt or BitCast cast constant expression 
587   static Constant *getSExtOrBitCast(
588     Constant *C,   ///< The constant to sext or bitcast
589     const Type *Ty ///< The type to sext or bitcast C to
590   );
591
592   // @brief Create a Trunc or BitCast cast constant expression
593   static Constant *getTruncOrBitCast(
594     Constant *C,   ///< The constant to trunc or bitcast
595     const Type *Ty ///< The type to trunc or bitcast C to
596   );
597
598   /// @brief Create a BitCast or a PtrToInt cast constant expression
599   static Constant *getPointerCast(
600     Constant *C,   ///< The pointer value to be casted (operand 0)
601     const Type *Ty ///< The type to which cast should be made
602   );
603
604   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
605   static Constant *getIntegerCast(
606     Constant *C,    ///< The integer constant to be casted 
607     const Type *Ty, ///< The integer type to cast to
608     bool isSigned   ///< Whether C should be treated as signed or not
609   );
610
611   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
612   static Constant *getFPCast(
613     Constant *C,    ///< The integer constant to be casted 
614     const Type *Ty ///< The integer type to cast to
615   );
616
617   /// @brief Return true if this is a convert constant expression
618   bool isCast() const;
619
620   /// @brief Return true if this is a compare constant expression
621   bool isCompare() const;
622
623   /// Select constant expr
624   ///
625   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
626     return getSelectTy(V1->getType(), C, V1, V2);
627   }
628
629   /// getSizeOf constant expr - computes the size of a type in a target
630   /// independent way (Note: the return type is an i64).
631   ///
632   static Constant *getSizeOf(const Type *Ty);
633
634   /// ConstantExpr::get - Return a binary or shift operator constant expression,
635   /// folding if possible.
636   ///
637   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
638
639   /// @brief Return an ICmp or FCmp comparison operator constant expression.
640   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
641
642   /// ConstantExpr::get* - Return some common constants without having to
643   /// specify the full Instruction::OPCODE identifier.
644   ///
645   static Constant *getNeg(Constant *C);
646   static Constant *getNot(Constant *C);
647   static Constant *getAdd(Constant *C1, Constant *C2);
648   static Constant *getSub(Constant *C1, Constant *C2);
649   static Constant *getMul(Constant *C1, Constant *C2);
650   static Constant *getUDiv(Constant *C1, Constant *C2);
651   static Constant *getSDiv(Constant *C1, Constant *C2);
652   static Constant *getFDiv(Constant *C1, Constant *C2);
653   static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
654   static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
655   static Constant *getFRem(Constant *C1, Constant *C2);
656   static Constant *getAnd(Constant *C1, Constant *C2);
657   static Constant *getOr(Constant *C1, Constant *C2);
658   static Constant *getXor(Constant *C1, Constant *C2);
659   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
660   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
661   static Constant *getShl(Constant *C1, Constant *C2);
662   static Constant *getLShr(Constant *C1, Constant *C2);
663   static Constant *getAShr(Constant *C1, Constant *C2);
664
665   /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
666   /// all elements must be Constant's.
667   ///
668   static Constant *getGetElementPtr(Constant *C,
669                                     Constant* const *IdxList, unsigned NumIdx);
670   static Constant *getGetElementPtr(Constant *C,
671                                     Value* const *IdxList, unsigned NumIdx);
672   
673   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
674   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
675   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
676
677   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
678   /// method returns the negative zero constant for floating point or vector
679   /// floating point types; for all other types, it returns the null value.
680   static Constant *getZeroValueForNegationExpr(const Type *Ty);
681
682   /// isNullValue - Return true if this is the value that would be returned by
683   /// getNullValue.
684   virtual bool isNullValue() const { return false; }
685
686   /// getOpcode - Return the opcode at the root of this constant expression
687   unsigned getOpcode() const { return SubclassData; }
688
689   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
690   /// not an ICMP or FCMP constant expression.
691   unsigned getPredicate() const;
692
693   /// getOpcodeName - Return a string representation for an opcode.
694   const char *getOpcodeName() const;
695
696   /// getWithOperandReplaced - Return a constant expression identical to this
697   /// one, but with the specified operand set to the specified value.
698   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
699   
700   /// getWithOperands - This returns the current constant expression with the
701   /// operands replaced with the specified values.  The specified operands must
702   /// match count and type with the existing ones.
703   Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
704   
705   virtual void destroyConstant();
706   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
707
708   /// Override methods to provide more type information...
709   inline Constant *getOperand(unsigned i) {
710     return cast<Constant>(User::getOperand(i));
711   }
712   inline Constant *getOperand(unsigned i) const {
713     return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
714   }
715
716
717   /// Methods for support type inquiry through isa, cast, and dyn_cast:
718   static inline bool classof(const ConstantExpr *) { return true; }
719   static inline bool classof(const Value *V) {
720     return V->getValueID() == ConstantExprVal;
721   }
722 };
723
724
725 //===----------------------------------------------------------------------===//
726 /// UndefValue - 'undef' values are things that do not have specified contents.
727 /// These are used for a variety of purposes, including global variable
728 /// initializers and operands to instructions.  'undef' values can occur with
729 /// any type.
730 ///
731 class UndefValue : public Constant {
732   friend struct ConstantCreator<UndefValue, Type, char>;
733   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
734 protected:
735   explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
736   static void destroyThis(UndefValue*v) {
737     Constant::destroyThis(v);
738   }
739   friend class Value;
740 public:
741   /// get() - Static factory methods - Return an 'undef' object of the specified
742   /// type.
743   ///
744   static UndefValue *get(const Type *T);
745
746   /// isNullValue - Return true if this is the value that would be returned by
747   /// getNullValue.
748   virtual bool isNullValue() const { return false; }
749
750   virtual void destroyConstant();
751
752   /// Methods for support type inquiry through isa, cast, and dyn_cast:
753   static inline bool classof(const UndefValue *) { return true; }
754   static bool classof(const Value *V) {
755     return V->getValueID() == UndefValueVal;
756   }
757 };
758
759 /// GetElementPtrConstantExpr - Helper class for Constants.cpp, 
760 /// used behind the scenes to implement getelementpr constant exprs.
761 class GetElementPtrConstantExpr : public ConstantExpr {
762 protected:
763   static void destroyThis(GetElementPtrConstantExpr*v) {
764     delete [] v->OperandList;
765     ConstantExpr::destroyThis(v);
766   }
767   friend class Value;
768 public:
769   GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
770                                                         const Type *DestTy);
771 };
772
773 /// UnaryConstantExpr - Helper class for Constants.cpp, used
774 /// behind the scenes to implement unary constant exprs.
775 class UnaryConstantExpr : public ConstantExpr {
776   Use Op;
777 protected:
778   static void destroyThis(UnaryConstantExpr*v) {
779     ConstantExpr::destroyThis(v);
780   }
781   friend class Value;
782 public:
783   UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty);
784 };
785
786 /// BinaryConstantExpr - Helper class for Constants.cpp, used
787 /// behind the scenes to implement binary constant exprs.
788 class BinaryConstantExpr : public ConstantExpr {
789   Use Ops[2];
790 protected:
791   static void destroyThis(BinaryConstantExpr*v) {
792     ConstantExpr::destroyThis(v);
793   }
794   friend class Value;
795 public:
796   BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
797     : ConstantExpr(C1->getType(), Opcode, Ops, 2) {
798     Ops[0].init(C1, this);
799     Ops[1].init(C2, this);
800   }
801 };
802
803 /// SelectConstantExpr - Helper class for Constants.cpp, used
804 /// behind the scenes to implement select constant exprs.
805 class SelectConstantExpr : public ConstantExpr {
806   Use Ops[3];
807 protected:
808   static void destroyThis(SelectConstantExpr*v) {
809     ConstantExpr::destroyThis(v);
810   }
811   friend class Value;
812 public:
813   SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3);
814 };
815
816 /// ExtractElementConstantExpr - Helper class for Constants.cpp, used 
817 /// behind the scenes to implement extractelement constant exprs.
818 class ExtractElementConstantExpr : public ConstantExpr {
819   Use Ops[2];
820 protected:
821   static void destroyThis(ExtractElementConstantExpr*v) {
822     ConstantExpr::destroyThis(v);
823   }
824   friend class Value;
825 public:
826   ExtractElementConstantExpr(Constant *C1, Constant *C2);
827 };
828
829 /// InsertElementConstantExpr - Helper class for Constants.cpp, used 
830 /// behind the scenes to implement insertelement constant exprs.
831 class InsertElementConstantExpr : public ConstantExpr {
832   Use Ops[3];
833 protected:
834   static void destroyThis(InsertElementConstantExpr*v) {
835     ConstantExpr::destroyThis(v);
836   }
837   friend class Value;
838 public:
839   InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3);
840 };
841
842 /// ShuffleVectorConstantExpr - Helper class for Constants.cpp, used 
843 /// behind the scenes to implement shufflevector constant exprs.
844 class ShuffleVectorConstantExpr : public ConstantExpr {
845   Use Ops[3];
846 protected:
847   static void destroyThis(ShuffleVectorConstantExpr*v) {
848     ConstantExpr::destroyThis(v);
849   }
850   friend class Value;
851 public:
852   ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3);
853 };
854
855
856
857 // CompareConstantExpr - Helper class for Constants.cpp, used
858 // behind the scenes to implement ICmp and FCmp constant expressions. This is
859 // needed in order to store the predicate value for these instructions.
860 class CompareConstantExpr : public ConstantExpr {
861 protected:
862   static void destroyThis(CompareConstantExpr*v) {
863     ConstantExpr::destroyThis(v);
864   }
865   friend class Value;
866 public:
867   unsigned short predicate;
868   Use Ops[2];
869   CompareConstantExpr(unsigned opc, unsigned short pred, 
870                       Constant* LHS, Constant* RHS);
871 };
872
873 } // End llvm namespace
874
875 #endif