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