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