Add PMStack, a Pass Manager stack.
[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. Note that there are two
185   /// versions of this method, one for unsigned and one for signed integers.
186   /// Although ConstantInt canonicalizes everything to an unsigned integer, 
187   /// the signed version avoids callers having to convert a signed quantity
188   /// to the appropriate unsigned type before calling the method.
189   /// @returns true if V is a valid value for type Ty
190   /// @brief Determine if the value is in range for the given type.
191   static bool isValueValidForType(const Type *Ty, uint64_t V);
192   static bool isValueValidForType(const Type *Ty, int64_t V);
193
194   /// @returns true if this is the null integer value.
195   /// @see ConstantIntegral for details
196   /// @brief Implement override.
197   virtual bool isNullValue() const { return Val == 0; }
198
199   /// @returns true iff this constant's bits are all set to true.
200   /// @see ConstantIntegral
201   /// @brief Override implementation
202   virtual bool isAllOnesValue() const { return getSExtValue() == -1; }
203
204   /// @returns true iff this is the largest value that may be represented 
205   /// by this type.
206   /// @see ConstantIntegeral
207   /// @brief Override implementation
208   virtual bool isMaxValue(bool isSigned) const {
209     if (isSigned) {
210       int64_t V = getSExtValue();
211       if (V < 0) return false;    // Be careful about wrap-around on 'long's
212       ++V;
213       return !isValueValidForType(getType(), V) || V < 0;
214     }
215     return isAllOnesValue();
216   }
217
218   /// @returns true if this is the smallest value that may be represented by 
219   /// this type.
220   /// @see ConstantIntegral
221   /// @brief Override implementation
222   virtual bool isMinValue(bool isSigned) const {
223     if (isSigned) {
224       int64_t V = getSExtValue();
225       if (V > 0) return false;    // Be careful about wrap-around on 'long's
226       --V;
227       return !isValueValidForType(getType(), V) || V > 0;
228     }
229     return getZExtValue() == 0;
230   }
231
232   /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
233   static inline bool classof(const ConstantInt *) { return true; }
234   static bool classof(const Value *V) {
235     return V->getValueType() == ConstantIntVal;
236   }
237 };
238
239
240 //===----------------------------------------------------------------------===//
241 /// ConstantFP - Floating Point Values [float, double]
242 ///
243 class ConstantFP : public Constant {
244   double Val;
245   friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
246   friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
247   ConstantFP(const ConstantFP &);      // DO NOT IMPLEMENT
248 protected:
249   ConstantFP(const Type *Ty, double V);
250 public:
251   /// get() - Static factory methods - Return objects of the specified value
252   static ConstantFP *get(const Type *Ty, double V);
253
254   /// isValueValidForType - return true if Ty is big enough to represent V.
255   static bool isValueValidForType(const Type *Ty, double V);
256   inline double getValue() const { return Val; }
257
258   /// isNullValue - Return true if this is the value that would be returned by
259   /// getNullValue.  Don't depend on == for doubles to tell us it's zero, it
260   /// considers -0.0 to be null as well as 0.0.  :(
261   virtual bool isNullValue() const;
262
263   /// isExactlyValue - We don't rely on operator== working on double values, as
264   /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
265   /// As such, this method can be used to do an exact bit-for-bit comparison of
266   /// two floating point values.
267   bool isExactlyValue(double V) const;
268
269   /// Methods for support type inquiry through isa, cast, and dyn_cast:
270   static inline bool classof(const ConstantFP *) { return true; }
271   static bool classof(const Value *V) {
272     return V->getValueType() == ConstantFPVal;
273   }
274 };
275
276 //===----------------------------------------------------------------------===//
277 /// ConstantAggregateZero - All zero aggregate value
278 ///
279 class ConstantAggregateZero : public Constant {
280   friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
281   ConstantAggregateZero(const ConstantAggregateZero &);      // DO NOT IMPLEMENT
282 protected:
283   ConstantAggregateZero(const Type *Ty)
284     : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
285 public:
286   /// get() - static factory method for creating a null aggregate.  It is
287   /// illegal to call this method with a non-aggregate type.
288   static Constant *get(const Type *Ty);
289
290   /// isNullValue - Return true if this is the value that would be returned by
291   /// getNullValue.
292   virtual bool isNullValue() const { return true; }
293
294   virtual void destroyConstant();
295
296   /// Methods for support type inquiry through isa, cast, and dyn_cast:
297   ///
298   static bool classof(const ConstantAggregateZero *) { return true; }
299   static bool classof(const Value *V) {
300     return V->getValueType() == ConstantAggregateZeroVal;
301   }
302 };
303
304
305 //===----------------------------------------------------------------------===//
306 /// ConstantArray - Constant Array Declarations
307 ///
308 class ConstantArray : public Constant {
309   friend struct ConstantCreator<ConstantArray, ArrayType,
310                                     std::vector<Constant*> >;
311   ConstantArray(const ConstantArray &);      // DO NOT IMPLEMENT
312 protected:
313   ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
314   ~ConstantArray();
315 public:
316   /// get() - Static factory methods - Return objects of the specified value
317   static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
318
319   /// This method constructs a ConstantArray and initializes it with a text
320   /// string. The default behavior (AddNull==true) causes a null terminator to
321   /// be placed at the end of the array. This effectively increases the length
322   /// of the array by one (you've been warned).  However, in some situations 
323   /// this is not desired so if AddNull==false then the string is copied without
324   /// null termination. 
325   static Constant *get(const std::string &Initializer, bool AddNull = true);
326
327   /// getType - Specialize the getType() method to always return an ArrayType,
328   /// which reduces the amount of casting needed in parts of the compiler.
329   ///
330   inline const ArrayType *getType() const {
331     return reinterpret_cast<const ArrayType*>(Value::getType());
332   }
333
334   /// isString - This method returns true if the array is an array of sbyte or
335   /// ubyte, and if the elements of the array are all ConstantInt's.
336   bool isString() const;
337
338   /// isCString - This method returns true if the array is a string (see
339   /// isString) and it ends in a null byte \0 and does not contains any other
340   /// null bytes except its terminator.
341   bool isCString() const;
342
343   /// getAsString - If this array is isString(), then this method converts the
344   /// array to an std::string and returns it.  Otherwise, it asserts out.
345   ///
346   std::string getAsString() const;
347
348   /// isNullValue - Return true if this is the value that would be returned by
349   /// getNullValue.  This always returns false because zero arrays are always
350   /// created as ConstantAggregateZero objects.
351   virtual bool isNullValue() const { return false; }
352
353   virtual void destroyConstant();
354   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
355
356   /// Methods for support type inquiry through isa, cast, and dyn_cast:
357   static inline bool classof(const ConstantArray *) { return true; }
358   static bool classof(const Value *V) {
359     return V->getValueType() == ConstantArrayVal;
360   }
361 };
362
363
364 //===----------------------------------------------------------------------===//
365 // ConstantStruct - Constant Struct Declarations
366 //
367 class ConstantStruct : public Constant {
368   friend struct ConstantCreator<ConstantStruct, StructType,
369                                     std::vector<Constant*> >;
370   ConstantStruct(const ConstantStruct &);      // DO NOT IMPLEMENT
371 protected:
372   ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
373   ~ConstantStruct();
374 public:
375   /// get() - Static factory methods - Return objects of the specified value
376   ///
377   static Constant *get(const StructType *T, const std::vector<Constant*> &V);
378   static Constant *get(const std::vector<Constant*> &V, bool packed = false);
379
380   /// getType() specialization - Reduce amount of casting...
381   ///
382   inline const StructType *getType() const {
383     return reinterpret_cast<const StructType*>(Value::getType());
384   }
385
386   /// isNullValue - Return true if this is the value that would be returned by
387   /// getNullValue.  This always returns false because zero structs are always
388   /// created as ConstantAggregateZero objects.
389   virtual bool isNullValue() const {
390     return false;
391   }
392
393   virtual void destroyConstant();
394   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
395
396   /// Methods for support type inquiry through isa, cast, and dyn_cast:
397   static inline bool classof(const ConstantStruct *) { return true; }
398   static bool classof(const Value *V) {
399     return V->getValueType() == ConstantStructVal;
400   }
401 };
402
403 //===----------------------------------------------------------------------===//
404 /// ConstantPacked - Constant Packed Declarations
405 ///
406 class ConstantPacked : public Constant {
407   friend struct ConstantCreator<ConstantPacked, PackedType,
408                                     std::vector<Constant*> >;
409   ConstantPacked(const ConstantPacked &);      // DO NOT IMPLEMENT
410 protected:
411   ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
412   ~ConstantPacked();
413 public:
414   /// get() - Static factory methods - Return objects of the specified value
415   static Constant *get(const PackedType *T, const std::vector<Constant*> &);
416   static Constant *get(const std::vector<Constant*> &V);
417
418   /// getType - Specialize the getType() method to always return an PackedType,
419   /// which reduces the amount of casting needed in parts of the compiler.
420   ///
421   inline const PackedType *getType() const {
422     return reinterpret_cast<const PackedType*>(Value::getType());
423   }
424
425   /// @returns the value for an packed integer constant of the given type that
426   /// has all its bits set to true.
427   /// @brief Get the all ones value
428   static ConstantPacked *getAllOnesValue(const PackedType *Ty);
429   
430   /// isNullValue - Return true if this is the value that would be returned by
431   /// getNullValue.  This always returns false because zero arrays are always
432   /// created as ConstantAggregateZero objects.
433   virtual bool isNullValue() const { return false; }
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 ConstantPacked *) { return true; }
440   static bool classof(const Value *V) {
441     return V->getValueType() == ConstantPackedVal;
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   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->getValueType() == 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 *getShiftTy(const Type *Ty,
507                               unsigned Opcode, Constant *C1, Constant *C2);
508   static Constant *getSelectTy(const Type *Ty,
509                                Constant *C1, Constant *C2, Constant *C3);
510   static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
511                                       const std::vector<Value*> &IdxList);
512   static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
513                                        Constant *Idx);
514   static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
515                                       Constant *Elt, Constant *Idx);
516   static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
517                                       Constant *V2, Constant *Mask);
518
519 public:
520   // Static methods to construct a ConstantExpr of different kinds.  Note that
521   // these methods may return a object that is not an instance of the
522   // ConstantExpr class, because they will attempt to fold the constant
523   // expression into something simpler if possible.
524
525   /// Cast constant expr
526   ///
527   static Constant *getTrunc   (Constant *C, const Type *Ty);
528   static Constant *getSExt    (Constant *C, const Type *Ty);
529   static Constant *getZExt    (Constant *C, const Type *Ty);
530   static Constant *getFPTrunc (Constant *C, const Type *Ty);
531   static Constant *getFPExtend(Constant *C, const Type *Ty);
532   static Constant *getUIToFP  (Constant *C, const Type *Ty);
533   static Constant *getSIToFP  (Constant *C, const Type *Ty);
534   static Constant *getFPToUI  (Constant *C, const Type *Ty);
535   static Constant *getFPToSI  (Constant *C, const Type *Ty);
536   static Constant *getPtrToInt(Constant *C, const Type *Ty);
537   static Constant *getIntToPtr(Constant *C, const Type *Ty);
538   static Constant *getBitCast (Constant *C, const Type *Ty);
539
540   // @brief Convenience function for getting one of the casting operations
541   // using a CastOps opcode.
542   static Constant *getCast(
543     unsigned ops,  ///< The opcode for the conversion
544     Constant *C,   ///< The constant to be converted
545     const Type *Ty ///< The type to which the constant is converted
546   );
547
548   // @brief Create a ZExt or BitCast cast constant expression
549   static Constant *getZExtOrBitCast(
550     Constant *C,   ///< The constant to zext or bitcast
551     const Type *Ty ///< The type to zext or bitcast C to
552   );
553
554   // @brief Create a SExt or BitCast cast constant expression 
555   static Constant *getSExtOrBitCast(
556     Constant *C,   ///< The constant to sext or bitcast
557     const Type *Ty ///< The type to sext or bitcast C to
558   );
559
560   // @brief Create a Trunc or BitCast cast constant expression
561   static Constant *getTruncOrBitCast(
562     Constant *C,   ///< The constant to trunc or bitcast
563     const Type *Ty ///< The type to trunc or bitcast C to
564   );
565
566   /// @brief Create a BitCast or a PtrToInt cast constant expression
567   static Constant *getPointerCast(
568     Constant *C,   ///< The pointer value to be casted (operand 0)
569     const Type *Ty ///< The type to which cast should be made
570   );
571
572   /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
573   static Constant *getIntegerCast(
574     Constant *C,    ///< The integer constant to be casted 
575     const Type *Ty, ///< The integer type to cast to
576     bool isSigned   ///< Whether C should be treated as signed or not
577   );
578
579   /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
580   static Constant *getFPCast(
581     Constant *C,    ///< The integer constant to be casted 
582     const Type *Ty ///< The integer type to cast to
583   );
584
585   /// @brief Return true if this is a convert constant expression
586   bool isCast() const;
587
588   /// @brief Return true if this is a compare constant expression
589   bool isCompare() const;
590
591   /// Select constant expr
592   ///
593   static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
594     return getSelectTy(V1->getType(), C, V1, V2);
595   }
596
597   /// getSizeOf constant expr - computes the size of a type in a target
598   /// independent way (Note: the return type is a ULong).
599   ///
600   static Constant *getSizeOf(const Type *Ty);
601
602   /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
603   /// return a pointer to a pointer of the array element type.
604   static Constant *getPtrPtrFromArrayPtr(Constant *C);
605
606   /// ConstantExpr::get - Return a binary or shift operator constant expression,
607   /// folding if possible.
608   ///
609   static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
610
611   /// @brief Return an ICmp or FCmp comparison operator constant expression.
612   static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
613
614   /// ConstantExpr::get* - Return some common constants without having to
615   /// specify the full Instruction::OPCODE identifier.
616   ///
617   static Constant *getNeg(Constant *C);
618   static Constant *getNot(Constant *C);
619   static Constant *getAdd(Constant *C1, Constant *C2);
620   static Constant *getSub(Constant *C1, Constant *C2);
621   static Constant *getMul(Constant *C1, Constant *C2);
622   static Constant *getUDiv(Constant *C1, Constant *C2);
623   static Constant *getSDiv(Constant *C1, Constant *C2);
624   static Constant *getFDiv(Constant *C1, Constant *C2);
625   static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
626   static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
627   static Constant *getFRem(Constant *C1, Constant *C2);
628   static Constant *getAnd(Constant *C1, Constant *C2);
629   static Constant *getOr(Constant *C1, Constant *C2);
630   static Constant *getXor(Constant *C1, Constant *C2);
631   static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS);
632   static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS);
633   static Constant *getShl(Constant *C1, Constant *C2);
634   static Constant *getLShr(Constant *C1, Constant *C2);
635   static Constant *getAShr(Constant *C1, Constant *C2);
636
637   /// Getelementptr form.  std::vector<Value*> is only accepted for convenience:
638   /// all elements must be Constant's.
639   ///
640   static Constant *getGetElementPtr(Constant *C,
641                                     const std::vector<Constant*> &IdxList);
642   static Constant *getGetElementPtr(Constant *C,
643                                     const std::vector<Value*> &IdxList);
644
645   static Constant *getExtractElement(Constant *Vec, Constant *Idx);
646   static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
647   static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
648
649   /// isNullValue - Return true if this is the value that would be returned by
650   /// getNullValue.
651   virtual bool isNullValue() const { return false; }
652
653   /// getOpcode - Return the opcode at the root of this constant expression
654   unsigned getOpcode() const { return SubclassData; }
655
656   /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
657   /// not an ICMP or FCMP constant expression.
658   unsigned getPredicate() const;
659
660   /// getOpcodeName - Return a string representation for an opcode.
661   const char *getOpcodeName() const;
662
663   /// getWithOperandReplaced - Return a constant expression identical to this
664   /// one, but with the specified operand set to the specified value.
665   Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
666   
667   /// getWithOperands - This returns the current constant expression with the
668   /// operands replaced with the specified values.  The specified operands must
669   /// match count and type with the existing ones.
670   Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
671   
672   virtual void destroyConstant();
673   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
674
675   /// Override methods to provide more type information...
676   inline Constant *getOperand(unsigned i) {
677     return cast<Constant>(User::getOperand(i));
678   }
679   inline Constant *getOperand(unsigned i) const {
680     return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
681   }
682
683
684   /// Methods for support type inquiry through isa, cast, and dyn_cast:
685   static inline bool classof(const ConstantExpr *) { return true; }
686   static inline bool classof(const Value *V) {
687     return V->getValueType() == ConstantExprVal;
688   }
689 };
690
691
692 //===----------------------------------------------------------------------===//
693 /// UndefValue - 'undef' values are things that do not have specified contents.
694 /// These are used for a variety of purposes, including global variable
695 /// initializers and operands to instructions.  'undef' values can occur with
696 /// any type.
697 ///
698 class UndefValue : public Constant {
699   friend struct ConstantCreator<UndefValue, Type, char>;
700   UndefValue(const UndefValue &);      // DO NOT IMPLEMENT
701 protected:
702   UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
703 public:
704   /// get() - Static factory methods - Return an 'undef' object of the specified
705   /// type.
706   ///
707   static UndefValue *get(const Type *T);
708
709   /// isNullValue - Return true if this is the value that would be returned by
710   /// getNullValue.
711   virtual bool isNullValue() const { return false; }
712
713   virtual void destroyConstant();
714
715   /// Methods for support type inquiry through isa, cast, and dyn_cast:
716   static inline bool classof(const UndefValue *) { return true; }
717   static bool classof(const Value *V) {
718     return V->getValueType() == UndefValueVal;
719   }
720 };
721
722 } // End llvm namespace
723
724 #endif