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