ff599512e6f70d89489bb699a81242805993f633
[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/ArrayRef.h"
29
30 namespace llvm {
31
32 class ArrayType;
33 class IntegerType;
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   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
49   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
50   ConstantInt(const IntegerType *Ty, const APInt& V);
51   APInt Val;
52 protected:
53   // allocate space for exactly zero operands
54   void *operator new(size_t s) {
55     return User::operator new(s, 0);
56   }
57 public:
58   static ConstantInt *getTrue(LLVMContext &Context);
59   static ConstantInt *getFalse(LLVMContext &Context);
60   static Constant *getTrue(const Type *Ty);
61   static Constant *getFalse(const Type *Ty);
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.  For ConstantFP, this is +0.0, but not -0.0.  To handle the
270   /// two the same, use isZero().
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   /// isZero - Return true if the value is positive or negative zero.
280   bool isZero() const { return Val.isZero(); }
281
282   /// isNaN - Return true if the value is a NaN.
283   bool isNaN() const { return Val.isNaN(); }
284
285   /// isExactlyValue - We don't rely on operator== working on double values, as
286   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
287   /// As such, this method can be used to do an exact bit-for-bit comparison of
288   /// two floating point values.  The version with a double operand is retained
289   /// because it's so convenient to write isExactlyValue(2.0), but please use
290   /// it only for simple constants.
291   bool isExactlyValue(const APFloat &V) const;
292
293   bool isExactlyValue(double V) const {
294     bool ignored;
295     // convert is not supported on this type
296     if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
297       return false;
298     APFloat FV(V);
299     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
300     return isExactlyValue(FV);
301   }
302   /// Methods for support type inquiry through isa, cast, and dyn_cast:
303   static inline bool classof(const ConstantFP *) { return true; }
304   static bool classof(const Value *V) {
305     return V->getValueID() == ConstantFPVal;
306   }
307 };
308
309 //===----------------------------------------------------------------------===//
310 /// ConstantAggregateZero - All zero aggregate value
311 ///
312 class ConstantAggregateZero : public Constant {
313   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
314   void *operator new(size_t, unsigned);                      // DO NOT IMPLEMENT
315   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
316 protected:
317   explicit ConstantAggregateZero(const Type *ty)
318     : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
319 protected:
320   // allocate space for exactly zero operands
321   void *operator new(size_t s) {
322     return User::operator new(s, 0);
323   }
324 public:
325   static ConstantAggregateZero* get(const Type *Ty);
326   
327   /// isNullValue - Return true if this is the value that would be returned by
328   /// getNullValue.
329   virtual bool isNullValue() const { return true; }
330
331   virtual void destroyConstant();
332
333   /// Methods for support type inquiry through isa, cast, and dyn_cast:
334   ///
335   static bool classof(const ConstantAggregateZero *) { return true; }
336   static bool classof(const Value *V) {
337     return V->getValueID() == ConstantAggregateZeroVal;
338   }
339 };
340
341
342 //===----------------------------------------------------------------------===//
343 /// ConstantArray - Constant Array Declarations
344 ///
345 class ConstantArray : public Constant {
346   friend struct ConstantCreator<ConstantArray, ArrayType,
347                                     std::vector<Constant*> >;
348   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
349 protected:
350   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
351 public:
352   // ConstantArray accessors
353   static Constant *get(const ArrayType *T, ArrayRef<Constant*> V);
354                              
355   /// This method constructs a ConstantArray and initializes it with a text
356   /// string. The default behavior (AddNull==true) causes a null terminator to
357   /// be placed at the end of the array. This effectively increases the length
358   /// of the array by one (you've been warned).  However, in some situations 
359   /// this is not desired so if AddNull==false then the string is copied without
360   /// null termination.
361   static Constant *get(LLVMContext &Context, StringRef Initializer,
362                        bool AddNull = true);
363   
364   /// Transparently provide more efficient getOperand methods.
365   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
366
367   /// getType - Specialize the getType() method to always return an ArrayType,
368   /// which reduces the amount of casting needed in parts of the compiler.
369   ///
370   inline const ArrayType *getType() const {
371     return reinterpret_cast<const ArrayType*>(Value::getType());
372   }
373
374   /// isString - This method returns true if the array is an array of i8 and
375   /// the elements of the array are all ConstantInt's.
376   bool isString() const;
377
378   /// isCString - This method returns true if the array is a string (see
379   /// @verbatim
380   /// isString) and it ends in a null byte \0 and does not contains any other
381   /// @endverbatim
382   /// null bytes except its terminator.
383   bool isCString() const;
384
385   /// getAsString - If this array is isString(), then this method converts the
386   /// array to an std::string and returns it.  Otherwise, it asserts out.
387   ///
388   std::string getAsString() const;
389
390   /// isNullValue - Return true if this is the value that would be returned by
391   /// getNullValue.  This always returns false because zero arrays are always
392   /// created as ConstantAggregateZero objects.
393   virtual bool isNullValue() const { return false; }
394
395   virtual void destroyConstant();
396   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
397
398   /// Methods for support type inquiry through isa, cast, and dyn_cast:
399   static inline bool classof(const ConstantArray *) { return true; }
400   static bool classof(const Value *V) {
401     return V->getValueID() == ConstantArrayVal;
402   }
403 };
404
405 template <>
406 struct OperandTraits<ConstantArray> :
407   public VariadicOperandTraits<ConstantArray> {
408 };
409
410 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
411
412 //===----------------------------------------------------------------------===//
413 // ConstantStruct - Constant Struct Declarations
414 //
415 class ConstantStruct : public Constant {
416   friend struct ConstantCreator<ConstantStruct, StructType,
417                                     std::vector<Constant*> >;
418   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
419 protected:
420   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
421 public:
422   // ConstantStruct accessors
423   static Constant *get(const StructType *T, ArrayRef<Constant*> V);
424   static Constant *get(const StructType *T, ...) END_WITH_NULL;
425
426   /// getAnon - Return an anonymous struct that has the specified
427   /// elements.  If the struct is possibly empty, then you must specify a
428   /// context.
429   static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
430     return get(getTypeForElements(V, Packed), V);
431   }
432   static Constant *getAnon(LLVMContext &Ctx, 
433                            ArrayRef<Constant*> V, bool Packed = false) {
434     return get(getTypeForElements(Ctx, V, Packed), V);
435   }
436
437   /// getTypeForElements - Return an anonymous struct type to use for a constant
438   /// with the specified set of elements.  The list must not be empty.
439   static StructType *getTypeForElements(ArrayRef<Constant*> V,
440                                         bool Packed = false);
441   /// getTypeForElements - This version of the method allows an empty list.
442   static StructType *getTypeForElements(LLVMContext &Ctx,
443                                         ArrayRef<Constant*> V,
444                                         bool Packed = false);
445   
446   /// Transparently provide more efficient getOperand methods.
447   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
448
449   /// getType() specialization - Reduce amount of casting...
450   ///
451   inline const StructType *getType() const {
452     return reinterpret_cast<const StructType*>(Value::getType());
453   }
454
455   /// isNullValue - Return true if this is the value that would be returned by
456   /// getNullValue.  This always returns false because zero structs are always
457   /// created as ConstantAggregateZero objects.
458   virtual bool isNullValue() const {
459     return false;
460   }
461
462   virtual void destroyConstant();
463   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
464
465   /// Methods for support type inquiry through isa, cast, and dyn_cast:
466   static inline bool classof(const ConstantStruct *) { return true; }
467   static bool classof(const Value *V) {
468     return V->getValueID() == ConstantStructVal;
469   }
470 };
471
472 template <>
473 struct OperandTraits<ConstantStruct> :
474   public VariadicOperandTraits<ConstantStruct> {
475 };
476
477 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
478
479
480 //===----------------------------------------------------------------------===//
481 /// ConstantVector - Constant Vector Declarations
482 ///
483 class ConstantVector : public Constant {
484   friend struct ConstantCreator<ConstantVector, VectorType,
485                                     std::vector<Constant*> >;
486   ConstantVector(const ConstantVector &);      // DO NOT IMPLEMENT
487 protected:
488   ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
489 public:
490   // ConstantVector accessors
491   static Constant *get(ArrayRef<Constant*> V);
492   
493   /// Transparently provide more efficient getOperand methods.
494   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
495
496   /// getType - Specialize the getType() method to always return a VectorType,
497   /// which reduces the amount of casting needed in parts of the compiler.
498   ///
499   inline const VectorType *getType() const {
500     return reinterpret_cast<const VectorType*>(Value::getType());
501   }
502   
503   /// isNullValue - Return true if this is the value that would be returned by
504   /// getNullValue.  This always returns false because zero vectors are always
505   /// created as ConstantAggregateZero objects.
506   virtual bool isNullValue() const { return false; }
507
508   /// This function will return true iff every element in this vector constant
509   /// is set to all ones.
510   /// @returns true iff this constant's emements are all set to all ones.
511   /// @brief Determine if the value is all ones.
512   bool isAllOnesValue() const;
513
514   /// getSplatValue - If this is a splat constant, meaning that all of the
515   /// elements have the same value, return that value. Otherwise return NULL.
516   Constant *getSplatValue() const;
517
518   virtual void destroyConstant();
519   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
520
521   /// Methods for support type inquiry through isa, cast, and dyn_cast:
522   static inline bool classof(const ConstantVector *) { return true; }
523   static bool classof(const Value *V) {
524     return V->getValueID() == ConstantVectorVal;
525   }
526 };
527
528 template <>
529 struct OperandTraits<ConstantVector> :
530   public VariadicOperandTraits<ConstantVector> {
531 };
532
533 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
534
535 //===----------------------------------------------------------------------===//
536 /// ConstantPointerNull - a constant pointer value that points to null
537 ///
538 class ConstantPointerNull : public Constant {
539   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
540   void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
541   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
542 protected:
543   explicit ConstantPointerNull(const PointerType *T)
544     : Constant(reinterpret_cast<const Type*>(T),
545                Value::ConstantPointerNullVal, 0, 0) {}
546
547 protected:
548   // allocate space for exactly zero operands
549   void *operator new(size_t s) {
550     return User::operator new(s, 0);
551   }
552 public:
553   /// get() - Static factory methods - Return objects of the specified value
554   static ConstantPointerNull *get(const PointerType *T);
555
556   /// isNullValue - Return true if this is the value that would be returned by
557   /// getNullValue.
558   virtual bool isNullValue() const { return true; }
559
560   virtual void destroyConstant();
561
562   /// getType - Specialize the getType() method to always return an PointerType,
563   /// which reduces the amount of casting needed in parts of the compiler.
564   ///
565   inline const PointerType *getType() const {
566     return reinterpret_cast<const PointerType*>(Value::getType());
567   }
568
569   /// Methods for support type inquiry through isa, cast, and dyn_cast:
570   static inline bool classof(const ConstantPointerNull *) { return true; }
571   static bool classof(const Value *V) {
572     return V->getValueID() == ConstantPointerNullVal;
573   }
574 };
575
576 /// BlockAddress - The address of a basic block.
577 ///
578 class BlockAddress : public Constant {
579   void *operator new(size_t, unsigned);                  // DO NOT IMPLEMENT
580   void *operator new(size_t s) { return User::operator new(s, 2); }
581   BlockAddress(Function *F, BasicBlock *BB);
582 public:
583   /// get - Return a BlockAddress for the specified function and basic block.
584   static BlockAddress *get(Function *F, BasicBlock *BB);
585   
586   /// get - Return a BlockAddress for the specified basic block.  The basic
587   /// block must be embedded into a function.
588   static BlockAddress *get(BasicBlock *BB);
589   
590   /// Transparently provide more efficient getOperand methods.
591   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
592   
593   Function *getFunction() const { return (Function*)Op<0>().get(); }
594   BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
595   
596   /// isNullValue - Return true if this is the value that would be returned by
597   /// getNullValue.
598   virtual bool isNullValue() const { return false; }
599   
600   virtual void destroyConstant();
601   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
602   
603   /// Methods for support type inquiry through isa, cast, and dyn_cast:
604   static inline bool classof(const BlockAddress *) { return true; }
605   static inline bool classof(const Value *V) {
606     return V->getValueID() == BlockAddressVal;
607   }
608 };
609
610 template <>
611 struct OperandTraits<BlockAddress> :
612   public FixedNumOperandTraits<BlockAddress, 2> {
613 };
614
615 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(BlockAddress, Value)
616   
617
618 //===----------------------------------------------------------------------===//
619 /// ConstantExpr - a constant value that is initialized with an expression using
620 /// other constant values.
621 ///
622 /// This class uses the standard Instruction opcodes to define the various
623 /// constant expressions.  The Opcode field for the ConstantExpr class is
624 /// maintained in the Value::SubclassData field.
625 class ConstantExpr : public Constant {
626   friend struct ConstantCreator<ConstantExpr,Type,
627                             std::pair<unsigned, std::vector<Constant*> > >;
628   friend struct ConvertConstantType<ConstantExpr, Type>;
629
630 protected:
631   ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
632     : Constant(ty, ConstantExprVal, Ops, NumOps) {
633     // Operation type (an Instruction opcode) is stored as the SubclassData.
634     setValueSubclassData(Opcode);
635   }
636
637   // These private methods are used by the type resolution code to create
638   // ConstantExprs in intermediate forms.
639   static Constant *getTy(const Type *Ty, unsigned Opcode,
640                          Constant *C1, Constant *C2,
641                          unsigned Flags = 0);
642   static Constant *getCompareTy(unsigned short pred, Constant *C1,
643                                 Constant *C2);
644   static Constant *getSelectTy(const Type *Ty,
645                                Constant *C1, Constant *C2, Constant *C3);
646   template<typename IndexTy>
647   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
648                                       IndexTy const *Idxs, unsigned NumIdxs,
649                                       bool InBounds);
650   static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
651                                        Constant *Idx);
652   static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
653                                       Constant *Elt, Constant *Idx);
654   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
655                                       Constant *V2, Constant *Mask);
656   static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
657                                      const unsigned *Idxs, unsigned NumIdxs);
658   static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
659                                     Constant *Val,
660                                     const unsigned *Idxs, unsigned NumIdxs);
661   template<typename IndexTy>
662   static Constant *getGetElementPtrImpl(Constant *C,
663                                         IndexTy const *IdxList,
664                                         unsigned NumIdx, bool InBounds);
665
666 public:
667   // Static methods to construct a ConstantExpr of different kinds.  Note that
668   // these methods may return a object that is not an instance of the
669   // ConstantExpr class, because they will attempt to fold the constant
670   // expression into something simpler if possible.
671
672   /// getAlignOf constant expr - computes the alignment of a type in a target
673   /// independent way (Note: the return type is an i64).
674   static Constant *getAlignOf(const Type *Ty);
675   
676   /// getSizeOf constant expr - computes the (alloc) size of a type (in
677   /// address-units, not bits) in a target independent way (Note: the return
678   /// type is an i64).
679   ///
680   static Constant *getSizeOf(const Type *Ty);
681
682   /// getOffsetOf constant expr - computes the offset of a struct field in a 
683   /// target independent way (Note: the return type is an i64).
684   ///
685   static Constant *getOffsetOf(const StructType *STy, unsigned FieldNo);
686
687   /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
688   /// which supports any aggregate type, and any Constant index.
689   ///
690   static Constant *getOffsetOf(const Type *Ty, Constant *FieldNo);
691   
692   static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
693   static Constant *getFNeg(Constant *C);
694   static Constant *getNot(Constant *C);
695   static Constant *getAdd(Constant *C1, Constant *C2,
696                           bool HasNUW = false, bool HasNSW = false);
697   static Constant *getFAdd(Constant *C1, Constant *C2);
698   static Constant *getSub(Constant *C1, Constant *C2,
699                           bool HasNUW = false, bool HasNSW = false);
700   static Constant *getFSub(Constant *C1, Constant *C2);
701   static Constant *getMul(Constant *C1, Constant *C2,
702                           bool HasNUW = false, bool HasNSW = false);
703   static Constant *getFMul(Constant *C1, Constant *C2);
704   static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
705   static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
706   static Constant *getFDiv(Constant *C1, Constant *C2);
707   static Constant *getURem(Constant *C1, Constant *C2);
708   static Constant *getSRem(Constant *C1, Constant *C2);
709   static Constant *getFRem(Constant *C1, Constant *C2);
710   static Constant *getAnd(Constant *C1, Constant *C2);
711   static Constant *getOr(Constant *C1, Constant *C2);
712   static Constant *getXor(Constant *C1, Constant *C2);
713   static Constant *getShl(Constant *C1, Constant *C2,
714                           bool HasNUW = false, bool HasNSW = false);
715   static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
716   static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
717   static Constant *getTrunc   (Constant *C, const Type *Ty);
718   static Constant *getSExt    (Constant *C, const Type *Ty);
719   static Constant *getZExt    (Constant *C, const Type *Ty);
720   static Constant *getFPTrunc (Constant *C, const Type *Ty);
721   static Constant *getFPExtend(Constant *C, const Type *Ty);
722   static Constant *getUIToFP  (Constant *C, const Type *Ty);
723   static Constant *getSIToFP  (Constant *C, const Type *Ty);
724   static Constant *getFPToUI  (Constant *C, const Type *Ty);
725   static Constant *getFPToSI  (Constant *C, const Type *Ty);
726   static Constant *getPtrToInt(Constant *C, const Type *Ty);
727   static Constant *getIntToPtr(Constant *C, const Type *Ty);
728   static Constant *getBitCast (Constant *C, const Type *Ty);
729
730   static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
731   static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
732   static Constant *getNSWAdd(Constant *C1, Constant *C2) {
733     return getAdd(C1, C2, false, true);
734   }
735   static Constant *getNUWAdd(Constant *C1, Constant *C2) {
736     return getAdd(C1, C2, true, false);
737   }
738   static Constant *getNSWSub(Constant *C1, Constant *C2) {
739     return getSub(C1, C2, false, true);
740   }
741   static Constant *getNUWSub(Constant *C1, Constant *C2) {
742     return getSub(C1, C2, true, false);
743   }
744   static Constant *getNSWMul(Constant *C1, Constant *C2) {
745     return getMul(C1, C2, false, true);
746   }
747   static Constant *getNUWMul(Constant *C1, Constant *C2) {
748     return getMul(C1, C2, true, false);
749   }
750   static Constant *getNSWShl(Constant *C1, Constant *C2) {
751     return getShl(C1, C2, false, true);
752   }
753   static Constant *getNUWShl(Constant *C1, Constant *C2) {
754     return getShl(C1, C2, true, false);
755   }
756   static Constant *getExactSDiv(Constant *C1, Constant *C2) {
757     return getSDiv(C1, C2, true);
758   }
759   static Constant *getExactUDiv(Constant *C1, Constant *C2) {
760     return getUDiv(C1, C2, true);
761   }
762   static Constant *getExactAShr(Constant *C1, Constant *C2) {
763     return getAShr(C1, C2, true);
764   }
765   static Constant *getExactLShr(Constant *C1, Constant *C2) {
766     return getLShr(C1, C2, true);
767   }
768
769   /// Transparently provide more efficient getOperand methods.
770   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
771
772   // @brief Convenience function for getting one of the casting operations
773   // using a CastOps opcode.
774   static Constant *getCast(
775     unsigned ops,  ///< The opcode for the conversion
776     Constant *C,   ///< The constant to be converted
777     const Type *Ty ///< The type to which the constant is converted
778   );
779
780   // @brief Create a ZExt or BitCast cast constant expression
781   static Constant *getZExtOrBitCast(
782     Constant *C,   ///< The constant to zext or bitcast
783     const Type *Ty ///< The type to zext or bitcast C to
784   );
785
786   // @brief Create a SExt or BitCast cast constant expression 
787   static Constant *getSExtOrBitCast(
788     Constant *C,   ///< The constant to sext or bitcast
789     const Type *Ty ///< The type to sext or bitcast C to
790   );
791
792   // @brief Create a Trunc or BitCast cast constant expression
793   static Constant *getTruncOrBitCast(
794     Constant *C,   ///< The constant to trunc or bitcast
795     const Type *Ty ///< The type to trunc or bitcast C to
796   );
797
798   /// @brief Create a BitCast or a PtrToInt cast constant expression
799   static Constant *getPointerCast(
800     Constant *C,   ///< The pointer value to be casted (operand 0)
801     const Type *Ty ///< The type to which cast should be made
802   );
803
804   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
805   static Constant *getIntegerCast(
806     Constant *C,    ///< The integer constant to be casted 
807     const Type *Ty, ///< The integer type to cast to
808     bool isSigned   ///< Whether C should be treated as signed or not
809   );
810
811   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
812   static Constant *getFPCast(
813     Constant *C,    ///< The integer constant to be casted 
814     const Type *Ty ///< The integer type to cast to
815   );
816
817   /// @brief Return true if this is a convert constant expression
818   bool isCast() const;
819
820   /// @brief Return true if this is a compare constant expression
821   bool isCompare() const;
822
823   /// @brief Return true if this is an insertvalue or extractvalue expression,
824   /// and the getIndices() method may be used.
825   bool hasIndices() const;
826
827   /// @brief Return true if this is a getelementptr expression and all
828   /// the index operands are compile-time known integers within the
829   /// corresponding notional static array extents. Note that this is
830   /// not equivalant to, a subset of, or a superset of the "inbounds"
831   /// property.
832   bool isGEPWithNoNotionalOverIndexing() const;
833
834   /// Select constant expr
835   ///
836   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
837     return getSelectTy(V1->getType(), C, V1, V2);
838   }
839
840   /// get - Return a binary or shift operator constant expression,
841   /// folding if possible.
842   ///
843   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
844                        unsigned Flags = 0);
845
846   /// @brief Return an ICmp or FCmp comparison operator constant expression.
847   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
848
849   /// get* - Return some common constants without having to
850   /// specify the full Instruction::OPCODE identifier.
851   ///
852   static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
853   static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
854
855   /// Getelementptr form.  Value* is only accepted for convenience;
856   /// all elements must be Constant's.
857   ///
858   static Constant *getGetElementPtr(Constant *C,
859                                     Constant *const *IdxList, unsigned NumIdx,
860                                     bool InBounds = false);
861   static Constant *getGetElementPtr(Constant *C,
862                                     Value *const *IdxList, unsigned NumIdx,
863                                     bool InBounds = false);
864
865   /// Create an "inbounds" getelementptr. See the documentation for the
866   /// "inbounds" flag in LangRef.html for details.
867   static Constant *getInBoundsGetElementPtr(Constant *C,
868                                             Constant *const *IdxList,
869                                             unsigned NumIdx) {
870     return getGetElementPtr(C, IdxList, NumIdx, true);
871   }
872   static Constant *getInBoundsGetElementPtr(Constant *C,
873                                             Value* const *IdxList,
874                                             unsigned NumIdx) {
875     return getGetElementPtr(C, IdxList, NumIdx, true);
876   }
877
878   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
879   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
880   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
881   static Constant *getExtractValue(Constant *Agg,
882                                    const unsigned *IdxList, unsigned NumIdx);
883   static Constant *getInsertValue(Constant *Agg, Constant *Val,
884                                   const unsigned *IdxList, unsigned NumIdx);
885
886   /// isNullValue - Return true if this is the value that would be returned by
887   /// getNullValue.
888   virtual bool isNullValue() const { return false; }
889
890   /// getOpcode - Return the opcode at the root of this constant expression
891   unsigned getOpcode() const { return getSubclassDataFromValue(); }
892
893   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
894   /// not an ICMP or FCMP constant expression.
895   unsigned getPredicate() const;
896
897   /// getIndices - Assert that this is an insertvalue or exactvalue
898   /// expression and return the list of indices.
899   ArrayRef<unsigned> getIndices() const;
900
901   /// getOpcodeName - Return a string representation for an opcode.
902   const char *getOpcodeName() const;
903
904   /// getWithOperandReplaced - Return a constant expression identical to this
905   /// one, but with the specified operand set to the specified value.
906   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
907   
908   /// getWithOperands - This returns the current constant expression with the
909   /// operands replaced with the specified values.  The specified operands must
910   /// match count and type with the existing ones.
911   Constant *getWithOperands(ArrayRef<Constant*> Ops) const;
912   
913   virtual void destroyConstant();
914   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
915
916   /// Methods for support type inquiry through isa, cast, and dyn_cast:
917   static inline bool classof(const ConstantExpr *) { return true; }
918   static inline bool classof(const Value *V) {
919     return V->getValueID() == ConstantExprVal;
920   }
921   
922 private:
923   // Shadow Value::setValueSubclassData with a private forwarding method so that
924   // subclasses cannot accidentally use it.
925   void setValueSubclassData(unsigned short D) {
926     Value::setValueSubclassData(D);
927   }
928 };
929
930 template <>
931 struct OperandTraits<ConstantExpr> :
932   public VariadicOperandTraits<ConstantExpr, 1> {
933 };
934
935 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
936
937 //===----------------------------------------------------------------------===//
938 /// UndefValue - 'undef' values are things that do not have specified contents.
939 /// These are used for a variety of purposes, including global variable
940 /// initializers and operands to instructions.  'undef' values can occur with
941 /// any first-class type.
942 ///
943 /// Undef values aren't exactly constants; if they have multiple uses, they
944 /// can appear to have different bit patterns at each use. See
945 /// LangRef.html#undefvalues for details.
946 ///
947 class UndefValue : public Constant {
948   friend struct ConstantCreator<UndefValue, Type, char>;
949   void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
950   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
951 protected:
952   explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
953 protected:
954   // allocate space for exactly zero operands
955   void *operator new(size_t s) {
956     return User::operator new(s, 0);
957   }
958 public:
959   /// get() - Static factory methods - Return an 'undef' object of the specified
960   /// type.
961   ///
962   static UndefValue *get(const Type *T);
963
964   /// isNullValue - Return true if this is the value that would be returned by
965   /// getNullValue.
966   virtual bool isNullValue() const { return false; }
967
968   virtual void destroyConstant();
969
970   /// Methods for support type inquiry through isa, cast, and dyn_cast:
971   static inline bool classof(const UndefValue *) { return true; }
972   static bool classof(const Value *V) {
973     return V->getValueID() == UndefValueVal;
974   }
975 };
976
977 } // End llvm namespace
978
979 #endif