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