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