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