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