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