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