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