"I got annoyed at the compiler warnings from ConstantInt::get(Ty, -1,
[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/FoldingSet.h"
30 #include "llvm/ADT/SmallVector.h"
31
32 namespace llvm {
33
34 class ArrayType;
35 class StructType;
36 class PointerType;
37 class VectorType;
38
39 template<class ConstantClass, class TypeClass, class ValType>
40 struct ConstantCreator;
41 template<class ConstantClass, class TypeClass>
42 struct ConvertConstantType;
43
44 //===----------------------------------------------------------------------===//
45 /// This is the shared class of boolean and integer constants. This class 
46 /// represents both boolean and integral constants.
47 /// @brief Class for constant integers.
48 class ConstantInt : public Constant {
49   static ConstantInt *TheTrueVal, *TheFalseVal;
50   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
51   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
52   ConstantInt(const IntegerType *Ty, const APInt& V);
53   APInt Val;
54 protected:
55   // allocate space for exactly zero operands
56   void *operator new(size_t s) {
57     return User::operator new(s, 0);
58   }
59 public:
60   /// Return the constant as an APInt value reference. This allows clients to
61   /// obtain a copy of the value, with all its precision in tact.
62   /// @brief Return the constant's value.
63   inline const APInt& getValue() const {
64     return Val;
65   }
66   
67   /// getBitWidth - Return the bitwidth of this constant.
68   unsigned getBitWidth() const { return Val.getBitWidth(); }
69
70   /// Return the constant as a 64-bit unsigned integer value after it
71   /// has been zero extended as appropriate for the type of this constant. Note
72   /// that this method can assert if the value does not fit in 64 bits.
73   /// @deprecated
74   /// @brief Return the zero extended value.
75   inline uint64_t getZExtValue() const {
76     return Val.getZExtValue();
77   }
78
79   /// Return the constant as a 64-bit integer value after it has been sign
80   /// extended as appropriate for the type of this constant. Note that
81   /// this method can assert if the value does not fit in 64 bits.
82   /// @deprecated
83   /// @brief Return the sign extended value.
84   inline int64_t getSExtValue() const {
85     return Val.getSExtValue();
86   }
87
88   /// A helper method that can be used to determine if the constant contained 
89   /// within is equal to a constant.  This only works for very small values, 
90   /// because this is all that can be represented with all types.
91   /// @brief Determine if this constant's value is same as an unsigned char.
92   bool equalsInt(uint64_t V) const {
93     return Val == V;
94   }
95
96   /// getTrue/getFalse - Return the singleton true/false values.
97   static inline ConstantInt *getTrue() {
98     if (TheTrueVal) return TheTrueVal;
99     return CreateTrueFalseVals(true);
100   }
101   static inline ConstantInt *getFalse() {
102     if (TheFalseVal) return TheFalseVal;
103     return CreateTrueFalseVals(false);
104   }
105
106   /// Return a ConstantInt with the specified value for the specified type. The
107   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
108   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
109   /// signed value for the type Ty.
110   /// @brief Get a ConstantInt for a specific value.
111   static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
112
113   /// Return a ConstantInt with the specified value for the specified type. The
114   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
115   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
116   /// signed value for the type Ty.
117   /// @brief Get a ConstantInt for a specific signed value.
118   static ConstantInt *getSigned(const Type *Ty, int64_t V) {
119     return get(Ty, V, true);
120   }
121
122   /// Return a ConstantInt with the specified value and an implied Type. The
123   /// type is the integer type that corresponds to the bit width of the value.
124   static ConstantInt *get(const APInt &V);
125
126   /// getType - Specialize the getType() method to always return an IntegerType,
127   /// which reduces the amount of casting needed in parts of the compiler.
128   ///
129   inline const IntegerType *getType() const {
130     return reinterpret_cast<const IntegerType*>(Value::getType());
131   }
132
133   /// This static method returns true if the type Ty is big enough to 
134   /// represent the value V. This can be used to avoid having the get method 
135   /// assert when V is larger than Ty can represent. Note that there are two
136   /// versions of this method, one for unsigned and one for signed integers.
137   /// Although ConstantInt canonicalizes everything to an unsigned integer, 
138   /// the signed version avoids callers having to convert a signed quantity
139   /// to the appropriate unsigned type before calling the method.
140   /// @returns true if V is a valid value for type Ty
141   /// @brief Determine if the value is in range for the given type.
142   static bool isValueValidForType(const Type *Ty, uint64_t V);
143   static bool isValueValidForType(const Type *Ty, int64_t V);
144
145   /// This function will return true iff this constant represents the "null"
146   /// value that would be returned by the getNullValue method.
147   /// @returns true if this is the null integer value.
148   /// @brief Determine if the value is null.
149   virtual bool isNullValue() const { 
150     return Val == 0; 
151   }
152
153   /// This is just a convenience method to make client code smaller for a
154   /// common code. It also correctly performs the comparison without the
155   /// potential for an assertion from getZExtValue().
156   bool isZero() const {
157     return Val == 0;
158   }
159
160   /// This is just a convenience method to make client code smaller for a 
161   /// common case. It also correctly performs the comparison without the
162   /// potential for an assertion from getZExtValue().
163   /// @brief Determine if the value is one.
164   bool isOne() const {
165     return Val == 1;
166   }
167
168   /// This function will return true iff every bit in this constant is set
169   /// to true.
170   /// @returns true iff this constant's bits are all set to true.
171   /// @brief Determine if the value is all ones.
172   bool isAllOnesValue() const { 
173     return Val.isAllOnesValue();
174   }
175
176   /// This function will return true iff this constant represents the largest
177   /// value that may be represented by the constant's type.
178   /// @returns true iff this is the largest value that may be represented 
179   /// by this type.
180   /// @brief Determine if the value is maximal.
181   bool isMaxValue(bool isSigned) const {
182     if (isSigned) 
183       return Val.isMaxSignedValue();
184     else
185       return Val.isMaxValue();
186   }
187
188   /// This function will return true iff this constant represents the smallest
189   /// value that may be represented by this constant's type.
190   /// @returns true if this is the smallest value that may be represented by 
191   /// this type.
192   /// @brief Determine if the value is minimal.
193   bool isMinValue(bool isSigned) const {
194     if (isSigned) 
195       return Val.isMinSignedValue();
196     else
197       return Val.isMinValue();
198   }
199
200   /// This function will return true iff this constant represents a value with
201   /// active bits bigger than 64 bits or a value greater than the given uint64_t
202   /// value.
203   /// @returns true iff this constant is greater or equal to the given number.
204   /// @brief Determine if the value is greater or equal to the given number.
205   bool uge(uint64_t Num) {
206     return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
207   }
208
209   /// getLimitedValue - If the value is smaller than the specified limit,
210   /// return it, otherwise return the limit value.  This causes the value
211   /// to saturate to the limit.
212   /// @returns the min of the value of the constant and the specified value
213   /// @brief Get the constant's value with a saturation limit
214   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
215     return Val.getLimitedValue(Limit);
216   }
217
218   /// @returns the value for an integer constant of the given type that has all
219   /// its bits set to true.
220   /// @brief Get the all ones value
221   static ConstantInt *getAllOnesValue(const Type *Ty);
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   static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
229 private:
230   static ConstantInt *CreateTrueFalseVals(bool WhichOne);
231 };
232
233
234 //===----------------------------------------------------------------------===//
235 /// ConstantFP - Floating Point Values [float, double]
236 ///
237 class ConstantFP : public Constant {
238   APFloat Val;
239   void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
240   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
241 protected:
242   ConstantFP(const Type *Ty, const APFloat& V);
243 protected:
244   // allocate space for exactly zero operands
245   void *operator new(size_t s) {
246     return User::operator new(s, 0);
247   }
248 public:
249   /// get() - Static factory methods - Return objects of the specified value
250   static ConstantFP *get(const APFloat &V);
251
252   /// get() - This returns a constant fp for the specified value in the
253   /// specified type.  This should only be used for simple constant values like
254   /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
255   static ConstantFP *get(const Type *Ty, double V);
256
257   /// isValueValidForType - return true if Ty is big enough to represent V.
258   static bool isValueValidForType(const Type *Ty, const APFloat& V);
259   inline const APFloat& getValueAPF() const { return Val; }
260
261   /// isNullValue - Return true if this is the value that would be returned by
262   /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
263   /// considers -0.0 to be null as well as 0.0.  :(
264   virtual bool isNullValue() const;
265
266   // Get a negative zero.
267   static ConstantFP *getNegativeZero(const Type* Ty);
268
269   /// isExactlyValue - We don't rely on operator== working on double values, as
270   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
271   /// As such, this method can be used to do an exact bit-for-bit comparison of
272   /// two floating point values.  The version with a double operand is retained
273   /// because it's so convenient to write isExactlyValue(2.0), but please use
274   /// it only for simple constants.
275   bool isExactlyValue(const APFloat& V) const;
276
277   bool isExactlyValue(double V) const {
278     bool ignored;
279     // convert is not supported on this type
280     if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
281       return false;
282     APFloat FV(V);
283     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
284     return isExactlyValue(FV);
285   }
286   /// Methods for support type inquiry through isa, cast, and dyn_cast:
287   static inline bool classof(const ConstantFP *) { return true; }
288   static bool classof(const Value *V) {
289     return V->getValueID() == ConstantFPVal;
290   }
291 };
292
293 //===----------------------------------------------------------------------===//
294 /// ConstantAggregateZero - All zero aggregate value
295 ///
296 class ConstantAggregateZero : public Constant {
297   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
298   void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
299   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
300 protected:
301   explicit ConstantAggregateZero(const Type *ty)
302     : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
303 protected:
304   // allocate space for exactly zero operands
305   void *operator new(size_t s) {
306     return User::operator new(s, 0);
307   }
308 public:
309   /// get() - static factory method for creating a null aggregate.  It is
310   /// illegal to call this method with a non-aggregate type.
311   static ConstantAggregateZero *get(const Type *Ty);
312
313   /// isNullValue - Return true if this is the value that would be returned by
314   /// getNullValue.
315   virtual bool isNullValue() const { return true; }
316
317   virtual void destroyConstant();
318
319   /// Methods for support type inquiry through isa, cast, and dyn_cast:
320   ///
321   static bool classof(const ConstantAggregateZero *) { return true; }
322   static bool classof(const Value *V) {
323     return V->getValueID() == ConstantAggregateZeroVal;
324   }
325 };
326
327
328 //===----------------------------------------------------------------------===//
329 /// ConstantArray - Constant Array Declarations
330 ///
331 class ConstantArray : public Constant {
332   friend struct ConstantCreator<ConstantArray, ArrayType,
333                                     std::vector<Constant*> >;
334   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
335 protected:
336   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
337 public:
338   /// get() - Static factory methods - Return objects of the specified value
339   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
340   static Constant *get(const ArrayType *T,
341                        Constant*const*Vals, unsigned NumVals) {
342     // FIXME: make this the primary ctor method.
343     return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
344   }
345
346   /// This method constructs a ConstantArray and initializes it with a text
347   /// string. The default behavior (AddNull==true) causes a null terminator to
348   /// be placed at the end of the array. This effectively increases the length
349   /// of the array by one (you've been warned).  However, in some situations 
350   /// this is not desired so if AddNull==false then the string is copied without
351   /// null termination. 
352   static Constant *get(const std::string &Initializer, bool AddNull = true);
353
354   /// Transparently provide more efficient getOperand methods.
355   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
356
357   /// getType - Specialize the getType() method to always return an ArrayType,
358   /// which reduces the amount of casting needed in parts of the compiler.
359   ///
360   inline const ArrayType *getType() const {
361     return reinterpret_cast<const ArrayType*>(Value::getType());
362   }
363
364   /// isString - This method returns true if the array is an array of i8 and
365   /// the elements of the array are all ConstantInt's.
366   bool isString() const;
367
368   /// isCString - This method returns true if the array is a string (see
369   /// @verbatim
370   /// isString) and it ends in a null byte \0 and does not contains any other
371   /// @endverbatim
372   /// null bytes except its terminator.
373   bool isCString() const;
374
375   /// getAsString - If this array is isString(), then this method converts the
376   /// array to an std::string and returns it.  Otherwise, it asserts out.
377   ///
378   std::string getAsString() const;
379
380   /// isNullValue - Return true if this is the value that would be returned by
381   /// getNullValue.  This always returns false because zero arrays are always
382   /// created as ConstantAggregateZero objects.
383   virtual bool isNullValue() const { return false; }
384
385   virtual void destroyConstant();
386   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
387
388   /// Methods for support type inquiry through isa, cast, and dyn_cast:
389   static inline bool classof(const ConstantArray *) { return true; }
390   static bool classof(const Value *V) {
391     return V->getValueID() == ConstantArrayVal;
392   }
393 };
394
395 template <>
396 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
397 };
398
399 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
400
401 //===----------------------------------------------------------------------===//
402 // ConstantStruct - Constant Struct Declarations
403 //
404 class ConstantStruct : public Constant {
405   friend struct ConstantCreator<ConstantStruct, StructType,
406                                     std::vector<Constant*> >;
407   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
408 protected:
409   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
410 public:
411   /// get() - Static factory methods - Return objects of the specified value
412   ///
413   static Constant *get(const StructType *T, const std::vector<Constant*> &V);
414   static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
415   static Constant *get(Constant*const* Vals, unsigned NumVals,
416                        bool Packed = false) {
417     // FIXME: make this the primary ctor method.
418     return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
419   }
420   
421   /// Transparently provide more efficient getOperand methods.
422   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
423
424   /// getType() specialization - Reduce amount of casting...
425   ///
426   inline const StructType *getType() const {
427     return reinterpret_cast<const StructType*>(Value::getType());
428   }
429
430   /// isNullValue - Return true if this is the value that would be returned by
431   /// getNullValue.  This always returns false because zero structs are always
432   /// created as ConstantAggregateZero objects.
433   virtual bool isNullValue() const {
434     return false;
435   }
436
437   virtual void destroyConstant();
438   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
439
440   /// Methods for support type inquiry through isa, cast, and dyn_cast:
441   static inline bool classof(const ConstantStruct *) { return true; }
442   static bool classof(const Value *V) {
443     return V->getValueID() == ConstantStructVal;
444   }
445 };
446
447 template <>
448 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
449 };
450
451 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
452
453 //===----------------------------------------------------------------------===//
454 /// ConstantVector - Constant Vector Declarations
455 ///
456 class ConstantVector : public Constant {
457   friend struct ConstantCreator<ConstantVector, VectorType,
458                                     std::vector<Constant*> >;
459   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
460 protected:
461   ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
462 public:
463   /// get() - Static factory methods - Return objects of the specified value
464   static Constant *get(const VectorType *T, const std::vector<Constant*> &);
465   static Constant *get(const std::vector<Constant*> &V);
466   static Constant *get(Constant*const* Vals, unsigned NumVals) {
467     // FIXME: make this the primary ctor method.
468     return get(std::vector<Constant*>(Vals, Vals+NumVals));
469   }
470   
471   /// Transparently provide more efficient getOperand methods.
472   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
473
474   /// getType - Specialize the getType() method to always return a VectorType,
475   /// which reduces the amount of casting needed in parts of the compiler.
476   ///
477   inline const VectorType *getType() const {
478     return reinterpret_cast<const VectorType*>(Value::getType());
479   }
480
481   /// @returns the value for a vector integer constant of the given type that
482   /// has all its bits set to true.
483   /// @brief Get the all ones value
484   static ConstantVector *getAllOnesValue(const VectorType *Ty);
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   /// getSizeOf constant expr - computes the size of a type in a target
685   /// independent way (Note: the return type is an i64).
686   ///
687   static Constant *getSizeOf(const Type *Ty);
688
689   /// ConstantExpr::get - Return a binary or shift operator constant expression,
690   /// folding if possible.
691   ///
692   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
693
694   /// @brief Return an ICmp, FCmp, VICmp, or VFCmp comparison operator constant
695   /// expression.
696   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
697
698   /// ConstantExpr::get* - Return some common constants without having to
699   /// specify the full Instruction::OPCODE identifier.
700   ///
701   static Constant *getNeg(Constant *C);
702   static Constant *getNot(Constant *C);
703   static Constant *getAdd(Constant *C1, Constant *C2);
704   static Constant *getSub(Constant *C1, Constant *C2);
705   static Constant *getMul(Constant *C1, Constant *C2);
706   static Constant *getUDiv(Constant *C1, Constant *C2);
707   static Constant *getSDiv(Constant *C1, Constant *C2);
708   static Constant *getFDiv(Constant *C1, Constant *C2);
709   static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
710   static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
711   static Constant *getFRem(Constant *C1, Constant *C2);
712   static Constant *getAnd(Constant *C1, Constant *C2);
713   static Constant *getOr(Constant *C1, Constant *C2);
714   static Constant *getXor(Constant *C1, Constant *C2);
715   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
716   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
717   static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
718   static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
719   static Constant *getShl(Constant *C1, Constant *C2);
720   static Constant *getLShr(Constant *C1, Constant *C2);
721   static Constant *getAShr(Constant *C1, Constant *C2);
722
723   /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
724   /// all elements must be Constant's.
725   ///
726   static Constant *getGetElementPtr(Constant *C,
727                                     Constant* const *IdxList, unsigned NumIdx);
728   static Constant *getGetElementPtr(Constant *C,
729                                     Value* const *IdxList, unsigned NumIdx);
730   
731   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
732   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
733   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
734   static Constant *getExtractValue(Constant *Agg,
735                                    const unsigned *IdxList, unsigned NumIdx);
736   static Constant *getInsertValue(Constant *Agg, Constant *Val,
737                                   const unsigned *IdxList, unsigned NumIdx);
738
739   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
740   /// method returns the negative zero constant for floating point or vector
741   /// floating point types; for all other types, it returns the null value.
742   static Constant *getZeroValueForNegationExpr(const Type *Ty);
743
744   /// isNullValue - Return true if this is the value that would be returned by
745   /// getNullValue.
746   virtual bool isNullValue() const { return false; }
747
748   /// getOpcode - Return the opcode at the root of this constant expression
749   unsigned getOpcode() const { return SubclassData; }
750
751   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
752   /// not an ICMP or FCMP constant expression.
753   unsigned getPredicate() const;
754
755   /// getIndices - Assert that this is an insertvalue or exactvalue
756   /// expression and return the list of indices.
757   const SmallVector<unsigned, 4> &getIndices() const;
758
759   /// getOpcodeName - Return a string representation for an opcode.
760   const char *getOpcodeName() const;
761
762   /// getWithOperandReplaced - Return a constant expression identical to this
763   /// one, but with the specified operand set to the specified value.
764   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
765   
766   /// getWithOperands - This returns the current constant expression with the
767   /// operands replaced with the specified values.  The specified operands must
768   /// match count and type with the existing ones.
769   Constant *getWithOperands(const std::vector<Constant*> &Ops) const {
770     return getWithOperands(&Ops[0], (unsigned)Ops.size());
771   }
772   Constant *getWithOperands(Constant* const *Ops, unsigned NumOps) const;
773   
774   virtual void destroyConstant();
775   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
776
777   /// Methods for support type inquiry through isa, cast, and dyn_cast:
778   static inline bool classof(const ConstantExpr *) { return true; }
779   static inline bool classof(const Value *V) {
780     return V->getValueID() == ConstantExprVal;
781   }
782 };
783
784 template <>
785 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
786 };
787
788 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
789
790 //===----------------------------------------------------------------------===//
791 /// UndefValue - 'undef' values are things that do not have specified contents.
792 /// These are used for a variety of purposes, including global variable
793 /// initializers and operands to instructions.  'undef' values can occur with
794 /// any type.
795 ///
796 class UndefValue : public Constant {
797   friend struct ConstantCreator<UndefValue, Type, char>;
798   void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
799   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
800 protected:
801   explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
802 protected:
803   // allocate space for exactly zero operands
804   void *operator new(size_t s) {
805     return User::operator new(s, 0);
806   }
807 public:
808   /// get() - Static factory methods - Return an 'undef' object of the specified
809   /// type.
810   ///
811   static UndefValue *get(const Type *T);
812
813   /// isNullValue - Return true if this is the value that would be returned by
814   /// getNullValue.
815   virtual bool isNullValue() const { return false; }
816
817   virtual void destroyConstant();
818
819   /// Methods for support type inquiry through isa, cast, and dyn_cast:
820   static inline bool classof(const UndefValue *) { return true; }
821   static bool classof(const Value *V) {
822     return V->getValueID() == UndefValueVal;
823   }
824 };
825
826 //===----------------------------------------------------------------------===//
827 /// MDString - a single uniqued string.
828 /// These are used to efficiently contain a byte sequence for metadata.
829 ///
830 class MDString : public Constant {
831   MDString(const MDString &);            // DO NOT IMPLEMENT
832   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
833   MDString(const char *begin, const char *end);
834
835   const char *StrBegin, *StrEnd;
836 protected:
837   // allocate space for exactly zero operands
838   void *operator new(size_t s) {
839     return User::operator new(s, 0);
840   }
841 public:
842   /// get() - Static factory methods - Return objects of the specified value.
843   ///
844   static MDString *get(const char *StrBegin, const char *StrEnd);
845
846   /// size() - The length of this string.
847   ///
848   unsigned size() const { return StrEnd - StrBegin; }
849
850   /// begin() - Pointer to the first byte of the string.
851   ///
852   const char *begin() const { return StrBegin; }
853
854   /// end() - Pointer to one byte past the end of the string.
855   ///
856   const char *end() const { return StrEnd; }
857
858   /// getType() specialization - Type is always an empty struct.
859   ///
860   inline const Type *getType() const {
861     return Type::EmptyStructTy;
862   }
863
864   /// isNullValue - Return true if this is the value that would be returned by
865   /// getNullValue.  This always returns false because getNullValue will never
866   /// produce metadata.
867   virtual bool isNullValue() const {
868     return false;
869   }
870
871   virtual void destroyConstant();
872
873   /// Methods for support type inquiry through isa, cast, and dyn_cast:
874   static inline bool classof(const MDString *) { return true; }
875   static bool classof(const Value *V) {
876     return V->getValueID() == MDStringVal;
877   }
878 };
879
880 //===----------------------------------------------------------------------===//
881 /// MDNode - a tuple of other values.
882 /// These contain a list of the Constants that represent the metadata.
883 ///
884 class MDNode : public Constant, public FoldingSetNode {
885   MDNode(const MDNode &);      // DO NOT IMPLEMENT
886 protected:
887   explicit MDNode(Constant*const* Vals, unsigned NumVals);
888 public:
889   /// get() - Static factory methods - Return objects of the specified value.
890   ///
891   static MDNode *get(Constant*const* Vals, unsigned NumVals);
892
893   // Transparently provide more efficient getOperand methods.
894   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
895
896   /// getType() specialization - Type is always an empty struct.
897   ///
898   inline const Type *getType() const {
899     return Type::EmptyStructTy;
900   }
901
902   /// isNullValue - Return true if this is the value that would be returned by
903   /// getNullValue.  This always returns false because getNullValue will never
904   /// produce metadata.
905   virtual bool isNullValue() const {
906     return false;
907   }
908
909   /// Profile - calculate a unique identifier for this MDNode to collapse
910   /// duplicates
911   void Profile(FoldingSetNodeID &ID);
912
913   virtual void destroyConstant();
914   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
915
916   /// Methods for support type inquiry through isa, cast, and dyn_cast:
917   static inline bool classof(const MDNode *) { return true; }
918   static bool classof(const Value *V) {
919     return V->getValueID() == MDNodeVal;
920   }
921 };
922
923 template <>
924 struct OperandTraits<MDNode> : VariadicOperandTraits<> {
925 };
926
927 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(MDNode, Constant)
928
929 } // End llvm namespace
930
931 #endif