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