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