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