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