36d8299f48fcd67778934e00ead5647fd2f966a3
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file This file contains the declarations for the subclasses of Constant, 
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are 
13 /// fully shared by structural equivalence.  This means that two structurally
14 /// equivalent constants will always have the same address.  Constant's are
15 /// created on demand as needed and never deleted: thus clients don't have to
16 /// worry about the lifetime of the objects.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CONSTANTS_H
21 #define LLVM_CONSTANTS_H
22
23 #include "llvm/Constant.h"
24 #include "llvm/Type.h"
25
26 namespace llvm {
27
28 class ArrayType;
29 class StructType;
30 class PointerType;
31 class PackedType;
32
33 template<class ConstantClass, class TypeClass, class ValType>
34 struct ConstantCreator;
35 template<class ConstantClass, class TypeClass>
36 struct ConvertConstantType;
37
38 //===----------------------------------------------------------------------===//
39 /// This is the shared superclass of boolean and integer constants. This class 
40 /// just defines some common interfaces to be implemented by the subclasses.
41 /// @brief An abstract class for integer constants.
42 class ConstantIntegral : public Constant {
43 protected:
44   uint64_t Val;
45   ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V);
46 public:
47     
48   /// ConstantIntegral::get - Return a bool or integer constant.
49   static ConstantIntegral *get(const Type *Ty, int64_t V);
50     
51   /// Return the constant as a 64-bit unsigned integer value after it
52   /// has been zero extended as appropriate for the type of this constant.
53   /// @brief Return the zero extended value.
54   inline uint64_t getZExtValue() const {
55     return Val;
56   }
57
58   /// Return the constant as a 64-bit integer value after it has been sign
59   /// sign extended as appropriate for the type of this constant.
60   /// @brief Return the sign extended value.
61   inline int64_t getSExtValue() const {
62     unsigned Size = getType()->getPrimitiveSizeInBits();
63     return (int64_t(Val) << (64-Size)) >> (64-Size);
64   }
65   
66   /// This function is implemented by subclasses and will return true iff this
67   /// constant represents the the "null" value that would be returned by the
68   /// getNullValue method.
69   /// @returns true if the constant's value is 0.
70   /// @brief Determine if the value is null.
71   virtual bool isNullValue() const = 0;
72
73   /// This function is implemented by sublcasses and will return true iff this
74   /// constant represents the the largest value that may be represented by this
75   /// constant's type.
76   /// @returns true if the constant's value is maximal.
77   /// @brief Determine if the value is maximal.
78   virtual bool isMaxValue(bool isSigned) const = 0;
79
80   /// This function is implemented by subclasses and will return true iff this 
81   /// constant represents the smallest value that may be represented by this 
82   /// constant's type.
83   /// @returns true if the constant's value is minimal
84   /// @brief Determine if the value is minimal.
85   virtual bool isMinValue(bool isSigned) const = 0;
86
87   /// This function is implemented by subclasses and will return true iff every
88   /// bit in this constant is set to true.
89   /// @returns true if all bits of the constant are ones.
90   /// @brief Determine if the value is all ones.
91   virtual bool isAllOnesValue() const = 0;
92
93   /// @returns the value for an integer constant of the given type that has all
94   /// its bits set to true.
95   /// @brief Get the all ones value
96   static ConstantIntegral *getAllOnesValue(const Type *Ty);
97
98   /// Methods to support type inquiry through isa, cast, and dyn_cast:
99   static inline bool classof(const ConstantIntegral *) { return true; }
100   static bool classof(const Value *V) {
101     return V->getValueType() == ConstantBoolVal ||
102            V->getValueType() == ConstantIntVal;
103   }
104 };
105
106
107 //===----------------------------------------------------------------------===//
108 /// This concrete class represents constant values of type BoolTy. There are 
109 /// only two instances of this class constructed: the True and False static 
110 /// members. The constructor is hidden to ensure this invariant.
111 /// @brief Constant Boolean class
112 class ConstantBool : public ConstantIntegral {
113   ConstantBool(bool V);
114 public:
115   /// getTrue/getFalse - Return the singleton true/false values.
116   static ConstantBool *getTrue();
117   static ConstantBool *getFalse();
118
119   /// This method is provided mostly for compatibility with the other 
120   /// ConstantIntegral subclasses.
121   /// @brief Static factory method for getting a ConstantBool instance.
122   static ConstantBool *get(bool Value) { return Value ? getTrue() : getFalse();}
123
124   /// This method is provided mostly for compatibility with the other 
125   /// ConstantIntegral subclasses.
126   /// @brief Static factory method for getting a ConstantBool instance.
127   static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
128
129   /// Returns the opposite value of this ConstantBool value.
130   /// @brief Get inverse value.
131   inline ConstantBool *inverted() const {
132     return getValue() ? getFalse() : getTrue();
133   }
134
135   /// @returns the value of this ConstantBool
136   /// @brief return the boolean value of this constant.
137   inline bool getValue() const { return static_cast<bool>(getZExtValue()); }
138
139   /// @see ConstantIntegral for details
140   /// @brief Implement overrides
141   virtual bool isNullValue() const { return getValue() == false; }
142   virtual bool isMaxValue(bool isSigned) const { return getValue() == true; }
143   virtual bool isMinValue(bool isSigned) const { return getValue() == false; }
144   virtual bool isAllOnesValue() const { return getValue() == true; }
145
146   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast:
147   static inline bool classof(const ConstantBool *) { return true; }
148   static bool classof(const Value *V) {
149     return V->getValueType() == ConstantBoolVal;
150   }
151 };
152
153
154 //===----------------------------------------------------------------------===//
155 /// This is concrete integer subclass of ConstantIntegral that represents 
156 /// both signed and unsigned integral constants, other than boolean.
157 /// @brief Class for constant integers.
158 class ConstantInt : public ConstantIntegral {
159 protected:
160   ConstantInt(const ConstantInt &);      // DO NOT IMPLEMENT
161   ConstantInt(const Type *Ty, uint64_t V);
162   ConstantInt(const Type *Ty, int64_t V);
163   friend struct ConstantCreator<ConstantInt, Type, uint64_t>;
164 public:
165   /// A helper method that can be used to determine if the constant contained 
166   /// within is equal to a constant.  This only works for very small values, 
167   /// because this is all that can be represented with all types.
168   /// @brief Determine if this constant's value is same as an unsigned char.
169   bool equalsInt(unsigned char V) const {
170     assert(V <= 127 &&
171            "equalsInt: Can only be used with very small positive constants!");
172     return Val == V;
173   }
174
175   /// Return a ConstantInt with the specified value for the specified type. The
176   /// value V will be canonicalized to a uint64_t but accessing it with either
177   /// getSExtValue() or getZExtValue() (ConstantIntegral) will yield the correct
178   /// sized/signed value for the type Ty.
179   /// @brief Get a ConstantInt for a specific value.
180   static ConstantInt *get(const Type *Ty, int64_t V);
181
182   /// This static method returns true if the type Ty is big enough to 
183   /// represent the value V. This can be used to avoid having the get method 
184   /// assert when V is larger than Ty can represent.
185   /// @returns true if V is a valid value for type Ty
186   /// @brief Determine if the value is in range for the given type.
187   static bool isValueValidForType(const Type *Ty, int64_t V);
188
189   /// @returns true if this is the null integer value.
190   /// @see ConstantIntegral for details
191   /// @brief Implement override.
192   virtual bool isNullValue() const { return Val == 0; }
193
194   /// @returns true iff this constant's bits are all set to true.
195   /// @see ConstantIntegral
196   /// @brief Override implementation
197   virtual bool isAllOnesValue() const { return getSExtValue() == -1; }
198
199   /// @returns true iff this is the largest value that may be represented 
200   /// by this type.
201   /// @see ConstantIntegeral
202   /// @brief Override implementation
203   virtual bool isMaxValue(bool isSigned) const {
204     if (isSigned) {
205       int64_t V = getSExtValue();
206       if (V < 0) return false;    // Be careful about wrap-around on 'long's
207       ++V;
208       return !isValueValidForType(getType()->getSignedVersion(), V) || V < 0;
209     }
210     return isAllOnesValue();
211   }
212
213   /// @returns true if this is the smallest value that may be represented by 
214   /// this type.
215   /// @see ConstantIntegral
216   /// @brief Override implementation
217   virtual bool isMinValue(bool isSigned) const {
218     if (isSigned) {
219       int64_t V = getSExtValue();
220       if (V > 0) return false;    // Be careful about wrap-around on 'long's
221       --V;
222       return !isValueValidForType(getType()->getSignedVersion(), V) || V > 0;
223     }
224     return getZExtValue() == 0;
225   }
226
227   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
228   static inline bool classof(const ConstantInt *) { return true; }
229   static bool classof(const Value *V) {
230     return V->getValueType() == ConstantIntVal;
231   }
232 };
233
234
235 //===----------------------------------------------------------------------===//
236 /// ConstantFP - Floating Point Values [float, double]
237 ///
238 class ConstantFP : public Constant {
239   double Val;
240   friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
241   friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
242   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
243 protected:
244   ConstantFP(const Type *Ty, double V);
245 public:
246   /// get() - Static factory methods - Return objects of the specified value
247   static ConstantFP *get(const Type *Ty, double V);
248
249   /// isValueValidForType - return true if Ty is big enough to represent V.
250   static bool isValueValidForType(const Type *Ty, double V);
251   inline double getValue() const { return Val; }
252
253   /// isNullValue - Return true if this is the value that would be returned by
254   /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
255   /// considers -0.0 to be null as well as 0.0.  :(
256   virtual bool isNullValue() const;
257
258   /// isExactlyValue - We don't rely on operator== working on double values, as
259   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
260   /// As such, this method can be used to do an exact bit-for-bit comparison of
261   /// two floating point values.
262   bool isExactlyValue(double V) const;
263
264   /// Methods for support type inquiry through isa, cast, and dyn_cast:
265   static inline bool classof(const ConstantFP *) { return true; }
266   static bool classof(const Value *V) {
267     return V->getValueType() == ConstantFPVal;
268   }
269 };
270
271 //===----------------------------------------------------------------------===//
272 /// ConstantAggregateZero - All zero aggregate value
273 ///
274 class ConstantAggregateZero : public Constant {
275   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
276   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
277 protected:
278   ConstantAggregateZero(const Type *Ty)
279     : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
280 public:
281   /// get() - static factory method for creating a null aggregate.  It is
282   /// illegal to call this method with a non-aggregate type.
283   static Constant *get(const Type *Ty);
284
285   /// isNullValue - Return true if this is the value that would be returned by
286   /// getNullValue.
287   virtual bool isNullValue() const { return true; }
288
289   virtual void destroyConstant();
290
291   /// Methods for support type inquiry through isa, cast, and dyn_cast:
292   ///
293   static bool classof(const ConstantAggregateZero *) { return true; }
294   static bool classof(const Value *V) {
295     return V->getValueType() == ConstantAggregateZeroVal;
296   }
297 };
298
299
300 //===----------------------------------------------------------------------===//
301 /// ConstantArray - Constant Array Declarations
302 ///
303 class ConstantArray : public Constant {
304   friend struct ConstantCreator<ConstantArray, ArrayType,
305                                     std::vector<Constant*> >;
306   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
307 protected:
308   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
309   ~ConstantArray();
310 public:
311   /// get() - Static factory methods - Return objects of the specified value
312   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
313
314   /// This method constructs a ConstantArray and initializes it with a text
315   /// string. The default behavior (AddNull==true) causes a null terminator to
316   /// be placed at the end of the array. This effectively increases the length
317   /// of the array by one (you've been warned).  However, in some situations 
318   /// this is not desired so if AddNull==false then the string is copied without
319   /// null termination. 
320   static Constant *get(const std::string &Initializer, bool AddNull = true);
321
322   /// getType - Specialize the getType() method to always return an ArrayType,
323   /// which reduces the amount of casting needed in parts of the compiler.
324   ///
325   inline const ArrayType *getType() const {
326     return reinterpret_cast<const ArrayType*>(Value::getType());
327   }
328
329   /// isString - This method returns true if the array is an array of sbyte or
330   /// ubyte, and if the elements of the array are all ConstantInt's.
331   bool isString() const;
332
333   /// isCString - This method returns true if the array is a string (see
334   /// isString) and it ends in a null byte \0 and does not contains any other
335   /// null bytes except its terminator.
336   bool isCString() const;
337
338   /// getAsString - If this array is isString(), then this method converts the
339   /// array to an std::string and returns it.  Otherwise, it asserts out.
340   ///
341   std::string getAsString() const;
342
343   /// isNullValue - Return true if this is the value that would be returned by
344   /// getNullValue.  This always returns false because zero arrays are always
345   /// created as ConstantAggregateZero objects.
346   virtual bool isNullValue() const { return false; }
347
348   virtual void destroyConstant();
349   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
350
351   /// Methods for support type inquiry through isa, cast, and dyn_cast:
352   static inline bool classof(const ConstantArray *) { return true; }
353   static bool classof(const Value *V) {
354     return V->getValueType() == ConstantArrayVal;
355   }
356 };
357
358
359 //===----------------------------------------------------------------------===//
360 // ConstantStruct - Constant Struct Declarations
361 //
362 class ConstantStruct : public Constant {
363   friend struct ConstantCreator<ConstantStruct, StructType,
364                                     std::vector<Constant*> >;
365   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
366 protected:
367   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
368   ~ConstantStruct();
369 public:
370   /// get() - Static factory methods - Return objects of the specified value
371   ///
372   static Constant *get(const StructType *T, const std::vector<Constant*> &V);
373   static Constant *get(const std::vector<Constant*> &V, bool packed = false);
374
375   /// getType() specialization - Reduce amount of casting...
376   ///
377   inline const StructType *getType() const {
378     return reinterpret_cast<const StructType*>(Value::getType());
379   }
380
381   /// isNullValue - Return true if this is the value that would be returned by
382   /// getNullValue.  This always returns false because zero structs are always
383   /// created as ConstantAggregateZero objects.
384   virtual bool isNullValue() const {
385     return false;
386   }
387
388   virtual void destroyConstant();
389   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
390
391   /// Methods for support type inquiry through isa, cast, and dyn_cast:
392   static inline bool classof(const ConstantStruct *) { return true; }
393   static bool classof(const Value *V) {
394     return V->getValueType() == ConstantStructVal;
395   }
396 };
397
398 //===----------------------------------------------------------------------===//
399 /// ConstantPacked - Constant Packed Declarations
400 ///
401 class ConstantPacked : public Constant {
402   friend struct ConstantCreator<ConstantPacked, PackedType,
403                                     std::vector<Constant*> >;
404   ConstantPacked(const ConstantPacked &);      // DO NOT IMPLEMENT
405 protected:
406   ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
407   ~ConstantPacked();
408 public:
409   /// get() - Static factory methods - Return objects of the specified value
410   static Constant *get(const PackedType *T, const std::vector<Constant*> &);
411   static Constant *get(const std::vector<Constant*> &V);
412
413   /// getType - Specialize the getType() method to always return an PackedType,
414   /// which reduces the amount of casting needed in parts of the compiler.
415   ///
416   inline const PackedType *getType() const {
417     return reinterpret_cast<const PackedType*>(Value::getType());
418   }
419
420   /// isNullValue - Return true if this is the value that would be returned by
421   /// getNullValue.  This always returns false because zero arrays are always
422   /// created as ConstantAggregateZero objects.
423   virtual bool isNullValue() const { return false; }
424
425   virtual void destroyConstant();
426   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
427
428   /// Methods for support type inquiry through isa, cast, and dyn_cast:
429   static inline bool classof(const ConstantPacked *) { return true; }
430   static bool classof(const Value *V) {
431     return V->getValueType() == ConstantPackedVal;
432   }
433 };
434
435 //===----------------------------------------------------------------------===//
436 /// ConstantPointerNull - a constant pointer value that points to null
437 ///
438 class ConstantPointerNull : public Constant {
439   friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
440   ConstantPointerNull(const ConstantPointerNull &);      // DO NOT IMPLEMENT
441 protected:
442   ConstantPointerNull(const PointerType *T)
443     : Constant(reinterpret_cast<const Type*>(T),
444                Value::ConstantPointerNullVal, 0, 0) {}
445
446 public:
447
448   /// get() - Static factory methods - Return objects of the specified value
449   static ConstantPointerNull *get(const PointerType *T);
450
451   /// isNullValue - Return true if this is the value that would be returned by
452   /// getNullValue.
453   virtual bool isNullValue() const { return true; }
454
455   virtual void destroyConstant();
456
457   /// getType - Specialize the getType() method to always return an PointerType,
458   /// which reduces the amount of casting needed in parts of the compiler.
459   ///
460   inline const PointerType *getType() const {
461     return reinterpret_cast<const PointerType*>(Value::getType());
462   }
463
464   /// Methods for support type inquiry through isa, cast, and dyn_cast:
465   static inline bool classof(const ConstantPointerNull *) { return true; }
466   static bool classof(const Value *V) {
467     return V->getValueType() == ConstantPointerNullVal;
468   }
469 };
470
471
472 /// ConstantExpr - a constant value that is initialized with an expression using
473 /// other constant values.
474 ///
475 /// This class uses the standard Instruction opcodes to define the various
476 /// constant expressions.  The Opcode field for the ConstantExpr class is
477 /// maintained in the Value::SubclassData field.
478 class ConstantExpr : public Constant {
479   friend struct ConstantCreator<ConstantExpr,Type,
480                             std::pair<unsigned, std::vector<Constant*> > >;
481   friend struct ConvertConstantType<ConstantExpr, Type>;
482
483 protected:
484   ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
485     : Constant(Ty, ConstantExprVal, Ops, NumOps) {
486     // Operation type (an Instruction opcode) is stored as the SubclassData.
487     SubclassData = Opcode;
488   }
489
490   // These private methods are used by the type resolution code to create
491   // ConstantExprs in intermediate forms.
492   static Constant *getTy(const Type *Ty, unsigned Opcode,
493                          Constant *C1, Constant *C2);
494   static Constant *getCompareTy(unsigned Opcode, unsigned short pred, 
495                                 Constant *C1, Constant *C2);
496   static Constant *getShiftTy(const Type *Ty,
497                               unsigned Opcode, Constant *C1, Constant *C2);
498   static Constant *getSelectTy(const Type *Ty,
499                                Constant *C1, Constant *C2, Constant *C3);
500   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
501                                       const std::vector<Value*> &IdxList);
502   static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
503                                        Constant *Idx);
504   static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
505                                       Constant *Elt, Constant *Idx);
506   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
507                                       Constant *V2, Constant *Mask);
508
509 public:
510   // Static methods to construct a ConstantExpr of different kinds.  Note that
511   // these methods may return a object that is not an instance of the
512   // ConstantExpr class, because they will attempt to fold the constant
513   // expression into something simpler if possible.
514
515   /// Cast constant expr
516   ///
517   static Constant *getTrunc   (Constant *C, const Type *Ty);
518   static Constant *getSExt    (Constant *C, const Type *Ty);
519   static Constant *getZExt    (Constant *C, const Type *Ty);
520   static Constant *getFPTrunc (Constant *C, const Type *Ty);
521   static Constant *getFPExtend(Constant *C, const Type *Ty);
522   static Constant *getUIToFP  (Constant *C, const Type *Ty);
523   static Constant *getSIToFP  (Constant *C, const Type *Ty);
524   static Constant *getFPToUI  (Constant *C, const Type *Ty);
525   static Constant *getFPToSI  (Constant *C, const Type *Ty);
526   static Constant *getPtrToInt(Constant *C, const Type *Ty);
527   static Constant *getIntToPtr(Constant *C, const Type *Ty);
528   static Constant *getBitCast (Constant *C, const Type *Ty);
529
530   // @brief Convenience function for getting one of the casting operations
531   // using a CastOps opcode.
532   static Constant *getCast(
533     unsigned ops,  ///< The opcode for the conversion
534     Constant *C,   ///< The constant to be converted
535     const Type *Ty ///< The type to which the constant is converted
536   );
537
538   // @brief Create a ZExt or BitCast cast constant expression
539   static Constant *getZExtOrBitCast(
540     Constant *C,   ///< The constant to zext or bitcast
541     const Type *Ty ///< The type to zext or bitcast C to
542   );
543
544   // @brief Create a SExt or BitCast cast constant expression 
545   static Constant *getSExtOrBitCast(
546     Constant *C,   ///< The constant to sext or bitcast
547     const Type *Ty ///< The type to sext or bitcast C to
548   );
549
550   // @brief Create a Trunc or BitCast cast constant expression
551   static Constant *getTruncOrBitCast(
552     Constant *C,   ///< The constant to trunc or bitcast
553     const Type *Ty ///< The type to trunc or bitcast C to
554   );
555
556   /// @brief Create a BitCast or a PtrToInt cast constant expression
557   static Constant *getPointerCast(
558     Constant *C,   ///< The pointer value to be casted (operand 0)
559     const Type *Ty ///< The type to which cast should be made
560   );
561
562   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
563   static Constant *getIntegerCast(
564     Constant *C,    ///< The integer constant to be casted 
565     const Type *Ty, ///< The integer type to cast to
566     bool isSigned   ///< Whether C should be treated as signed or not
567   );
568
569   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
570   static Constant *getFPCast(
571     Constant *C,    ///< The integer constant to be casted 
572     const Type *Ty ///< The integer type to cast to
573   );
574
575   /// @brief Return true if this is a convert constant expression
576   bool isCast() const;
577
578   /// @brief Return true if this is a compare constant expression
579   bool isCompare() const;
580
581   /// Select constant expr
582   ///
583   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
584     return getSelectTy(V1->getType(), C, V1, V2);
585   }
586
587   /// getSizeOf constant expr - computes the size of a type in a target
588   /// independent way (Note: the return type is a ULong).
589   ///
590   static Constant *getSizeOf(const Type *Ty);
591
592   /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
593   /// return a pointer to a pointer of the array element type.
594   static Constant *getPtrPtrFromArrayPtr(Constant *C);
595
596   /// ConstantExpr::get - Return a binary or shift operator constant expression,
597   /// folding if possible.
598   ///
599   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
600
601   /// @brief Return an ICmp or FCmp comparison operator constant expression.
602   static Constant *getCompare(unsigned Opcode, unsigned short pred, 
603                               Constant *C1, Constant *C2);
604
605   /// ConstantExpr::get* - Return some common constants without having to
606   /// specify the full Instruction::OPCODE identifier.
607   ///
608   static Constant *getNeg(Constant *C);
609   static Constant *getNot(Constant *C);
610   static Constant *getAdd(Constant *C1, Constant *C2);
611   static Constant *getSub(Constant *C1, Constant *C2);
612   static Constant *getMul(Constant *C1, Constant *C2);
613   static Constant *getUDiv(Constant *C1, Constant *C2);
614   static Constant *getSDiv(Constant *C1, Constant *C2);
615   static Constant *getFDiv(Constant *C1, Constant *C2);
616   static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
617   static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
618   static Constant *getFRem(Constant *C1, Constant *C2);
619   static Constant *getAnd(Constant *C1, Constant *C2);
620   static Constant *getOr(Constant *C1, Constant *C2);
621   static Constant *getXor(Constant *C1, Constant *C2);
622   static Constant *getSetEQ(Constant *C1, Constant *C2);
623   static Constant *getSetNE(Constant *C1, Constant *C2);
624   static Constant *getSetLT(Constant *C1, Constant *C2);
625   static Constant *getSetGT(Constant *C1, Constant *C2);
626   static Constant *getSetLE(Constant *C1, Constant *C2);
627   static Constant *getSetGE(Constant *C1, Constant *C2);
628   static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS);
629   static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS);
630   static Constant *getShl(Constant *C1, Constant *C2);
631   static Constant *getLShr(Constant *C1, Constant *C2);
632   static Constant *getAShr(Constant *C1, Constant *C2);
633
634   /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
635   /// all elements must be Constant's.
636   ///
637   static Constant *getGetElementPtr(Constant *C,
638                                     const std::vector<Constant*> &IdxList);
639   static Constant *getGetElementPtr(Constant *C,
640                                     const std::vector<Value*> &IdxList);
641
642   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
643   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
644   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
645
646   /// isNullValue - Return true if this is the value that would be returned by
647   /// getNullValue.
648   virtual bool isNullValue() const { return false; }
649
650   /// getOpcode - Return the opcode at the root of this constant expression
651   unsigned getOpcode() const { return SubclassData; }
652
653   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
654   /// not an ICMP or FCMP constant expression.
655   unsigned getPredicate() const;
656
657   /// getOpcodeName - Return a string representation for an opcode.
658   const char *getOpcodeName() const;
659
660   /// getWithOperandReplaced - Return a constant expression identical to this
661   /// one, but with the specified operand set to the specified value.
662   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
663   
664   /// getWithOperands - This returns the current constant expression with the
665   /// operands replaced with the specified values.  The specified operands must
666   /// match count and type with the existing ones.
667   Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
668   
669   virtual void destroyConstant();
670   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
671
672   /// Override methods to provide more type information...
673   inline Constant *getOperand(unsigned i) {
674     return cast<Constant>(User::getOperand(i));
675   }
676   inline Constant *getOperand(unsigned i) const {
677     return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
678   }
679
680
681   /// Methods for support type inquiry through isa, cast, and dyn_cast:
682   static inline bool classof(const ConstantExpr *) { return true; }
683   static inline bool classof(const Value *V) {
684     return V->getValueType() == ConstantExprVal;
685   }
686 };
687
688
689 //===----------------------------------------------------------------------===//
690 /// UndefValue - 'undef' values are things that do not have specified contents.
691 /// These are used for a variety of purposes, including global variable
692 /// initializers and operands to instructions.  'undef' values can occur with
693 /// any type.
694 ///
695 class UndefValue : public Constant {
696   friend struct ConstantCreator<UndefValue, Type, char>;
697   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
698 protected:
699   UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
700 public:
701   /// get() - Static factory methods - Return an 'undef' object of the specified
702   /// type.
703   ///
704   static UndefValue *get(const Type *T);
705
706   /// isNullValue - Return true if this is the value that would be returned by
707   /// getNullValue.
708   virtual bool isNullValue() const { return false; }
709
710   virtual void destroyConstant();
711
712   /// Methods for support type inquiry through isa, cast, and dyn_cast:
713   static inline bool classof(const UndefValue *) { return true; }
714   static bool classof(const Value *V) {
715     return V->getValueType() == UndefValueVal;
716   }
717 };
718
719 } // End llvm namespace
720
721 #endif