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