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