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