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