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