1249259Sdim//===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim/// @file
11249259Sdim/// This file contains the declarations for the subclasses of Constant,
12249259Sdim/// which represent the different flavors of constant values that live in LLVM.
13249259Sdim/// Note that Constants are immutable (once created they never change) and are
14249259Sdim/// fully shared by structural equivalence.  This means that two structurally
15249259Sdim/// equivalent constants will always have the same address.  Constant's are
16249259Sdim/// created on demand as needed and never deleted: thus clients don't have to
17249259Sdim/// worry about the lifetime of the objects.
18249259Sdim//
19249259Sdim//===----------------------------------------------------------------------===//
20249259Sdim
21249259Sdim#ifndef LLVM_IR_CONSTANTS_H
22249259Sdim#define LLVM_IR_CONSTANTS_H
23249259Sdim
24249259Sdim#include "llvm/ADT/APFloat.h"
25249259Sdim#include "llvm/ADT/APInt.h"
26249259Sdim#include "llvm/ADT/ArrayRef.h"
27249259Sdim#include "llvm/IR/Constant.h"
28249259Sdim#include "llvm/IR/OperandTraits.h"
29251662Sdim#include "llvm/IR/DerivedTypes.h"
30249259Sdim
31249259Sdimnamespace llvm {
32249259Sdim
33249259Sdimclass ArrayType;
34249259Sdimclass IntegerType;
35249259Sdimclass StructType;
36249259Sdimclass PointerType;
37249259Sdimclass VectorType;
38249259Sdimclass SequentialType;
39249259Sdim
40249259Sdimtemplate<class ConstantClass, class TypeClass, class ValType>
41249259Sdimstruct ConstantCreator;
42249259Sdimtemplate<class ConstantClass, class TypeClass>
43249259Sdimstruct ConstantArrayCreator;
44249259Sdimtemplate<class ConstantClass, class TypeClass>
45249259Sdimstruct ConvertConstantType;
46249259Sdim
47249259Sdim//===----------------------------------------------------------------------===//
48249259Sdim/// This is the shared class of boolean and integer constants. This class
49249259Sdim/// represents both boolean and integral constants.
50249259Sdim/// @brief Class for constant integers.
51249259Sdimclass ConstantInt : public Constant {
52249259Sdim  virtual void anchor();
53249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
54249259Sdim  ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
55249259Sdim  ConstantInt(IntegerType *Ty, const APInt& V);
56249259Sdim  APInt Val;
57249259Sdimprotected:
58249259Sdim  // allocate space for exactly zero operands
59249259Sdim  void *operator new(size_t s) {
60249259Sdim    return User::operator new(s, 0);
61249259Sdim  }
62249259Sdimpublic:
63249259Sdim  static ConstantInt *getTrue(LLVMContext &Context);
64249259Sdim  static ConstantInt *getFalse(LLVMContext &Context);
65249259Sdim  static Constant *getTrue(Type *Ty);
66249259Sdim  static Constant *getFalse(Type *Ty);
67249259Sdim
68249259Sdim  /// If Ty is a vector type, return a Constant with a splat of the given
69249259Sdim  /// value. Otherwise return a ConstantInt for the given value.
70249259Sdim  static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
71249259Sdim
72249259Sdim  /// Return a ConstantInt with the specified integer value for the specified
73249259Sdim  /// type. If the type is wider than 64 bits, the value will be zero-extended
74249259Sdim  /// to fit the type, unless isSigned is true, in which case the value will
75249259Sdim  /// be interpreted as a 64-bit signed integer and sign-extended to fit
76249259Sdim  /// the type.
77249259Sdim  /// @brief Get a ConstantInt for a specific value.
78249259Sdim  static ConstantInt *get(IntegerType *Ty, uint64_t V,
79249259Sdim                          bool isSigned = false);
80249259Sdim
81249259Sdim  /// Return a ConstantInt with the specified value for the specified type. The
82249259Sdim  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
83249259Sdim  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
84249259Sdim  /// signed value for the type Ty.
85249259Sdim  /// @brief Get a ConstantInt for a specific signed value.
86249259Sdim  static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
87249259Sdim  static Constant *getSigned(Type *Ty, int64_t V);
88249259Sdim
89249259Sdim  /// Return a ConstantInt with the specified value and an implied Type. The
90249259Sdim  /// type is the integer type that corresponds to the bit width of the value.
91249259Sdim  static ConstantInt *get(LLVMContext &Context, const APInt &V);
92249259Sdim
93249259Sdim  /// Return a ConstantInt constructed from the string strStart with the given
94249259Sdim  /// radix.
95249259Sdim  static ConstantInt *get(IntegerType *Ty, StringRef Str,
96249259Sdim                          uint8_t radix);
97249259Sdim
98249259Sdim  /// If Ty is a vector type, return a Constant with a splat of the given
99249259Sdim  /// value. Otherwise return a ConstantInt for the given value.
100249259Sdim  static Constant *get(Type* Ty, const APInt& V);
101249259Sdim
102249259Sdim  /// Return the constant as an APInt value reference. This allows clients to
103249259Sdim  /// obtain a copy of the value, with all its precision in tact.
104249259Sdim  /// @brief Return the constant's value.
105249259Sdim  inline const APInt &getValue() const {
106249259Sdim    return Val;
107249259Sdim  }
108249259Sdim
109249259Sdim  /// getBitWidth - Return the bitwidth of this constant.
110249259Sdim  unsigned getBitWidth() const { return Val.getBitWidth(); }
111249259Sdim
112249259Sdim  /// Return the constant as a 64-bit unsigned integer value after it
113249259Sdim  /// has been zero extended as appropriate for the type of this constant. Note
114249259Sdim  /// that this method can assert if the value does not fit in 64 bits.
115249259Sdim  /// @brief Return the zero extended value.
116249259Sdim  inline uint64_t getZExtValue() const {
117249259Sdim    return Val.getZExtValue();
118249259Sdim  }
119249259Sdim
120249259Sdim  /// Return the constant as a 64-bit integer value after it has been sign
121249259Sdim  /// extended as appropriate for the type of this constant. Note that
122249259Sdim  /// this method can assert if the value does not fit in 64 bits.
123249259Sdim  /// @brief Return the sign extended value.
124249259Sdim  inline int64_t getSExtValue() const {
125249259Sdim    return Val.getSExtValue();
126249259Sdim  }
127249259Sdim
128249259Sdim  /// A helper method that can be used to determine if the constant contained
129249259Sdim  /// within is equal to a constant.  This only works for very small values,
130249259Sdim  /// because this is all that can be represented with all types.
131249259Sdim  /// @brief Determine if this constant's value is same as an unsigned char.
132249259Sdim  bool equalsInt(uint64_t V) const {
133249259Sdim    return Val == V;
134249259Sdim  }
135249259Sdim
136249259Sdim  /// getType - Specialize the getType() method to always return an IntegerType,
137249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
138249259Sdim  ///
139249259Sdim  inline IntegerType *getType() const {
140251662Sdim    return cast<IntegerType>(Value::getType());
141249259Sdim  }
142249259Sdim
143249259Sdim  /// This static method returns true if the type Ty is big enough to
144249259Sdim  /// represent the value V. This can be used to avoid having the get method
145249259Sdim  /// assert when V is larger than Ty can represent. Note that there are two
146249259Sdim  /// versions of this method, one for unsigned and one for signed integers.
147249259Sdim  /// Although ConstantInt canonicalizes everything to an unsigned integer,
148249259Sdim  /// the signed version avoids callers having to convert a signed quantity
149249259Sdim  /// to the appropriate unsigned type before calling the method.
150249259Sdim  /// @returns true if V is a valid value for type Ty
151249259Sdim  /// @brief Determine if the value is in range for the given type.
152249259Sdim  static bool isValueValidForType(Type *Ty, uint64_t V);
153249259Sdim  static bool isValueValidForType(Type *Ty, int64_t V);
154249259Sdim
155249259Sdim  bool isNegative() const { return Val.isNegative(); }
156249259Sdim
157249259Sdim  /// This is just a convenience method to make client code smaller for a
158249259Sdim  /// common code. It also correctly performs the comparison without the
159249259Sdim  /// potential for an assertion from getZExtValue().
160249259Sdim  bool isZero() const {
161249259Sdim    return Val == 0;
162249259Sdim  }
163249259Sdim
164249259Sdim  /// This is just a convenience method to make client code smaller for a
165249259Sdim  /// common case. It also correctly performs the comparison without the
166249259Sdim  /// potential for an assertion from getZExtValue().
167249259Sdim  /// @brief Determine if the value is one.
168249259Sdim  bool isOne() const {
169249259Sdim    return Val == 1;
170249259Sdim  }
171249259Sdim
172249259Sdim  /// This function will return true iff every bit in this constant is set
173249259Sdim  /// to true.
174249259Sdim  /// @returns true iff this constant's bits are all set to true.
175249259Sdim  /// @brief Determine if the value is all ones.
176249259Sdim  bool isMinusOne() const {
177249259Sdim    return Val.isAllOnesValue();
178249259Sdim  }
179249259Sdim
180249259Sdim  /// This function will return true iff this constant represents the largest
181249259Sdim  /// value that may be represented by the constant's type.
182249259Sdim  /// @returns true iff this is the largest value that may be represented
183249259Sdim  /// by this type.
184249259Sdim  /// @brief Determine if the value is maximal.
185249259Sdim  bool isMaxValue(bool isSigned) const {
186249259Sdim    if (isSigned)
187249259Sdim      return Val.isMaxSignedValue();
188249259Sdim    else
189249259Sdim      return Val.isMaxValue();
190249259Sdim  }
191249259Sdim
192249259Sdim  /// This function will return true iff this constant represents the smallest
193249259Sdim  /// value that may be represented by this constant's type.
194249259Sdim  /// @returns true if this is the smallest value that may be represented by
195249259Sdim  /// this type.
196249259Sdim  /// @brief Determine if the value is minimal.
197249259Sdim  bool isMinValue(bool isSigned) const {
198249259Sdim    if (isSigned)
199249259Sdim      return Val.isMinSignedValue();
200249259Sdim    else
201249259Sdim      return Val.isMinValue();
202249259Sdim  }
203249259Sdim
204249259Sdim  /// This function will return true iff this constant represents a value with
205249259Sdim  /// active bits bigger than 64 bits or a value greater than the given uint64_t
206249259Sdim  /// value.
207249259Sdim  /// @returns true iff this constant is greater or equal to the given number.
208249259Sdim  /// @brief Determine if the value is greater or equal to the given number.
209249259Sdim  bool uge(uint64_t Num) const {
210249259Sdim    return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
211249259Sdim  }
212249259Sdim
213249259Sdim  /// getLimitedValue - If the value is smaller than the specified limit,
214249259Sdim  /// return it, otherwise return the limit value.  This causes the value
215249259Sdim  /// to saturate to the limit.
216249259Sdim  /// @returns the min of the value of the constant and the specified value
217249259Sdim  /// @brief Get the constant's value with a saturation limit
218249259Sdim  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
219249259Sdim    return Val.getLimitedValue(Limit);
220249259Sdim  }
221249259Sdim
222249259Sdim  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
223249259Sdim  static bool classof(const Value *V) {
224249259Sdim    return V->getValueID() == ConstantIntVal;
225249259Sdim  }
226249259Sdim};
227249259Sdim
228249259Sdim
229249259Sdim//===----------------------------------------------------------------------===//
230249259Sdim/// ConstantFP - Floating Point Values [float, double]
231249259Sdim///
232249259Sdimclass ConstantFP : public Constant {
233249259Sdim  APFloat Val;
234249259Sdim  virtual void anchor();
235249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
236249259Sdim  ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
237249259Sdim  friend class LLVMContextImpl;
238249259Sdimprotected:
239249259Sdim  ConstantFP(Type *Ty, const APFloat& V);
240249259Sdimprotected:
241249259Sdim  // allocate space for exactly zero operands
242249259Sdim  void *operator new(size_t s) {
243249259Sdim    return User::operator new(s, 0);
244249259Sdim  }
245249259Sdimpublic:
246249259Sdim  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
247249259Sdim  /// method returns the negative zero constant for floating point or vector
248249259Sdim  /// floating point types; for all other types, it returns the null value.
249249259Sdim  static Constant *getZeroValueForNegation(Type *Ty);
250249259Sdim
251249259Sdim  /// get() - This returns a ConstantFP, or a vector containing a splat of a
252249259Sdim  /// ConstantFP, for the specified value in the specified type.  This should
253249259Sdim  /// only be used for simple constant values like 2.0/1.0 etc, that are
254249259Sdim  /// known-valid both as host double and as the target format.
255249259Sdim  static Constant *get(Type* Ty, double V);
256249259Sdim  static Constant *get(Type* Ty, StringRef Str);
257249259Sdim  static ConstantFP *get(LLVMContext &Context, const APFloat &V);
258249259Sdim  static ConstantFP *getNegativeZero(Type* Ty);
259249259Sdim  static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
260249259Sdim
261249259Sdim  /// isValueValidForType - return true if Ty is big enough to represent V.
262249259Sdim  static bool isValueValidForType(Type *Ty, const APFloat &V);
263249259Sdim  inline const APFloat &getValueAPF() const { return Val; }
264249259Sdim
265249259Sdim  /// isZero - Return true if the value is positive or negative zero.
266249259Sdim  bool isZero() const { return Val.isZero(); }
267249259Sdim
268249259Sdim  /// isNegative - Return true if the sign bit is set.
269249259Sdim  bool isNegative() const { return Val.isNegative(); }
270249259Sdim
271249259Sdim  /// isNaN - Return true if the value is a NaN.
272249259Sdim  bool isNaN() const { return Val.isNaN(); }
273249259Sdim
274249259Sdim  /// isExactlyValue - We don't rely on operator== working on double values, as
275249259Sdim  /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
276249259Sdim  /// As such, this method can be used to do an exact bit-for-bit comparison of
277249259Sdim  /// two floating point values.  The version with a double operand is retained
278249259Sdim  /// because it's so convenient to write isExactlyValue(2.0), but please use
279249259Sdim  /// it only for simple constants.
280249259Sdim  bool isExactlyValue(const APFloat &V) const;
281249259Sdim
282249259Sdim  bool isExactlyValue(double V) const {
283249259Sdim    bool ignored;
284249259Sdim    APFloat FV(V);
285249259Sdim    FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
286249259Sdim    return isExactlyValue(FV);
287249259Sdim  }
288249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
289249259Sdim  static bool classof(const Value *V) {
290249259Sdim    return V->getValueID() == ConstantFPVal;
291249259Sdim  }
292249259Sdim};
293249259Sdim
294249259Sdim//===----------------------------------------------------------------------===//
295249259Sdim/// ConstantAggregateZero - All zero aggregate value
296249259Sdim///
297249259Sdimclass ConstantAggregateZero : public Constant {
298249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
299249259Sdim  ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
300249259Sdimprotected:
301249259Sdim  explicit ConstantAggregateZero(Type *ty)
302249259Sdim    : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
303249259Sdimprotected:
304249259Sdim  // allocate space for exactly zero operands
305249259Sdim  void *operator new(size_t s) {
306249259Sdim    return User::operator new(s, 0);
307249259Sdim  }
308249259Sdimpublic:
309249259Sdim  static ConstantAggregateZero *get(Type *Ty);
310249259Sdim
311249259Sdim  virtual void destroyConstant();
312249259Sdim
313249259Sdim  /// getSequentialElement - If this CAZ has array or vector type, return a zero
314249259Sdim  /// with the right element type.
315249259Sdim  Constant *getSequentialElement() const;
316249259Sdim
317249259Sdim  /// getStructElement - If this CAZ has struct type, return a zero with the
318249259Sdim  /// right element type for the specified element.
319249259Sdim  Constant *getStructElement(unsigned Elt) const;
320249259Sdim
321249259Sdim  /// getElementValue - Return a zero of the right value for the specified GEP
322249259Sdim  /// index.
323249259Sdim  Constant *getElementValue(Constant *C) const;
324249259Sdim
325249259Sdim  /// getElementValue - Return a zero of the right value for the specified GEP
326249259Sdim  /// index.
327249259Sdim  Constant *getElementValue(unsigned Idx) const;
328249259Sdim
329249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
330249259Sdim  ///
331249259Sdim  static bool classof(const Value *V) {
332249259Sdim    return V->getValueID() == ConstantAggregateZeroVal;
333249259Sdim  }
334249259Sdim};
335249259Sdim
336249259Sdim
337249259Sdim//===----------------------------------------------------------------------===//
338249259Sdim/// ConstantArray - Constant Array Declarations
339249259Sdim///
340249259Sdimclass ConstantArray : public Constant {
341249259Sdim  friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
342249259Sdim  ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
343249259Sdimprotected:
344249259Sdim  ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
345249259Sdimpublic:
346249259Sdim  // ConstantArray accessors
347249259Sdim  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
348249259Sdim
349249259Sdim  /// Transparently provide more efficient getOperand methods.
350249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
351249259Sdim
352249259Sdim  /// getType - Specialize the getType() method to always return an ArrayType,
353249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
354249259Sdim  ///
355249259Sdim  inline ArrayType *getType() const {
356251662Sdim    return cast<ArrayType>(Value::getType());
357249259Sdim  }
358249259Sdim
359249259Sdim  virtual void destroyConstant();
360249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
361249259Sdim
362249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
363249259Sdim  static bool classof(const Value *V) {
364249259Sdim    return V->getValueID() == ConstantArrayVal;
365249259Sdim  }
366249259Sdim};
367249259Sdim
368249259Sdimtemplate <>
369249259Sdimstruct OperandTraits<ConstantArray> :
370249259Sdim  public VariadicOperandTraits<ConstantArray> {
371249259Sdim};
372249259Sdim
373249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
374249259Sdim
375249259Sdim//===----------------------------------------------------------------------===//
376249259Sdim// ConstantStruct - Constant Struct Declarations
377249259Sdim//
378249259Sdimclass ConstantStruct : public Constant {
379249259Sdim  friend struct ConstantArrayCreator<ConstantStruct, StructType>;
380249259Sdim  ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
381249259Sdimprotected:
382249259Sdim  ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
383249259Sdimpublic:
384249259Sdim  // ConstantStruct accessors
385249259Sdim  static Constant *get(StructType *T, ArrayRef<Constant*> V);
386249259Sdim  static Constant *get(StructType *T, ...) END_WITH_NULL;
387249259Sdim
388249259Sdim  /// getAnon - Return an anonymous struct that has the specified
389249259Sdim  /// elements.  If the struct is possibly empty, then you must specify a
390249259Sdim  /// context.
391249259Sdim  static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
392249259Sdim    return get(getTypeForElements(V, Packed), V);
393249259Sdim  }
394249259Sdim  static Constant *getAnon(LLVMContext &Ctx,
395249259Sdim                           ArrayRef<Constant*> V, bool Packed = false) {
396249259Sdim    return get(getTypeForElements(Ctx, V, Packed), V);
397249259Sdim  }
398249259Sdim
399249259Sdim  /// getTypeForElements - Return an anonymous struct type to use for a constant
400249259Sdim  /// with the specified set of elements.  The list must not be empty.
401249259Sdim  static StructType *getTypeForElements(ArrayRef<Constant*> V,
402249259Sdim                                        bool Packed = false);
403249259Sdim  /// getTypeForElements - This version of the method allows an empty list.
404249259Sdim  static StructType *getTypeForElements(LLVMContext &Ctx,
405249259Sdim                                        ArrayRef<Constant*> V,
406249259Sdim                                        bool Packed = false);
407249259Sdim
408249259Sdim  /// Transparently provide more efficient getOperand methods.
409249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
410249259Sdim
411249259Sdim  /// getType() specialization - Reduce amount of casting...
412249259Sdim  ///
413249259Sdim  inline StructType *getType() const {
414251662Sdim    return cast<StructType>(Value::getType());
415249259Sdim  }
416249259Sdim
417249259Sdim  virtual void destroyConstant();
418249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
419249259Sdim
420249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
421249259Sdim  static bool classof(const Value *V) {
422249259Sdim    return V->getValueID() == ConstantStructVal;
423249259Sdim  }
424249259Sdim};
425249259Sdim
426249259Sdimtemplate <>
427249259Sdimstruct OperandTraits<ConstantStruct> :
428249259Sdim  public VariadicOperandTraits<ConstantStruct> {
429249259Sdim};
430249259Sdim
431249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
432249259Sdim
433249259Sdim
434249259Sdim//===----------------------------------------------------------------------===//
435249259Sdim/// ConstantVector - Constant Vector Declarations
436249259Sdim///
437249259Sdimclass ConstantVector : public Constant {
438249259Sdim  friend struct ConstantArrayCreator<ConstantVector, VectorType>;
439249259Sdim  ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
440249259Sdimprotected:
441249259Sdim  ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
442249259Sdimpublic:
443249259Sdim  // ConstantVector accessors
444249259Sdim  static Constant *get(ArrayRef<Constant*> V);
445249259Sdim
446249259Sdim  /// getSplat - Return a ConstantVector with the specified constant in each
447249259Sdim  /// element.
448249259Sdim  static Constant *getSplat(unsigned NumElts, Constant *Elt);
449249259Sdim
450249259Sdim  /// Transparently provide more efficient getOperand methods.
451249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
452249259Sdim
453249259Sdim  /// getType - Specialize the getType() method to always return a VectorType,
454249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
455249259Sdim  ///
456249259Sdim  inline VectorType *getType() const {
457251662Sdim    return cast<VectorType>(Value::getType());
458249259Sdim  }
459249259Sdim
460249259Sdim  /// getSplatValue - If this is a splat constant, meaning that all of the
461249259Sdim  /// elements have the same value, return that value. Otherwise return NULL.
462249259Sdim  Constant *getSplatValue() const;
463249259Sdim
464249259Sdim  virtual void destroyConstant();
465249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
466249259Sdim
467249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
468249259Sdim  static bool classof(const Value *V) {
469249259Sdim    return V->getValueID() == ConstantVectorVal;
470249259Sdim  }
471249259Sdim};
472249259Sdim
473249259Sdimtemplate <>
474249259Sdimstruct OperandTraits<ConstantVector> :
475249259Sdim  public VariadicOperandTraits<ConstantVector> {
476249259Sdim};
477249259Sdim
478249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
479249259Sdim
480249259Sdim//===----------------------------------------------------------------------===//
481249259Sdim/// ConstantPointerNull - a constant pointer value that points to null
482249259Sdim///
483249259Sdimclass ConstantPointerNull : public Constant {
484249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
485249259Sdim  ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
486249259Sdimprotected:
487249259Sdim  explicit ConstantPointerNull(PointerType *T)
488251662Sdim    : Constant(T,
489249259Sdim               Value::ConstantPointerNullVal, 0, 0) {}
490249259Sdim
491249259Sdimprotected:
492249259Sdim  // allocate space for exactly zero operands
493249259Sdim  void *operator new(size_t s) {
494249259Sdim    return User::operator new(s, 0);
495249259Sdim  }
496249259Sdimpublic:
497249259Sdim  /// get() - Static factory methods - Return objects of the specified value
498249259Sdim  static ConstantPointerNull *get(PointerType *T);
499249259Sdim
500249259Sdim  virtual void destroyConstant();
501249259Sdim
502249259Sdim  /// getType - Specialize the getType() method to always return an PointerType,
503249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
504249259Sdim  ///
505249259Sdim  inline PointerType *getType() const {
506251662Sdim    return cast<PointerType>(Value::getType());
507249259Sdim  }
508249259Sdim
509249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
510249259Sdim  static bool classof(const Value *V) {
511249259Sdim    return V->getValueID() == ConstantPointerNullVal;
512249259Sdim  }
513249259Sdim};
514249259Sdim
515249259Sdim//===----------------------------------------------------------------------===//
516249259Sdim/// ConstantDataSequential - A vector or array constant whose element type is a
517249259Sdim/// simple 1/2/4/8-byte integer or float/double, and whose elements are just
518249259Sdim/// simple data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
519249259Sdim/// operands because it stores all of the elements of the constant as densely
520249259Sdim/// packed data, instead of as Value*'s.
521249259Sdim///
522249259Sdim/// This is the common base class of ConstantDataArray and ConstantDataVector.
523249259Sdim///
524249259Sdimclass ConstantDataSequential : public Constant {
525249259Sdim  friend class LLVMContextImpl;
526249259Sdim  /// DataElements - A pointer to the bytes underlying this constant (which is
527249259Sdim  /// owned by the uniquing StringMap).
528249259Sdim  const char *DataElements;
529249259Sdim
530249259Sdim  /// Next - This forms a link list of ConstantDataSequential nodes that have
531249259Sdim  /// the same value but different type.  For example, 0,0,0,1 could be a 4
532249259Sdim  /// element array of i8, or a 1-element array of i32.  They'll both end up in
533249259Sdim  /// the same StringMap bucket, linked up.
534249259Sdim  ConstantDataSequential *Next;
535249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
536249259Sdim  ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
537249259Sdimprotected:
538249259Sdim  explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
539249259Sdim    : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
540249259Sdim  ~ConstantDataSequential() { delete Next; }
541249259Sdim
542249259Sdim  static Constant *getImpl(StringRef Bytes, Type *Ty);
543249259Sdim
544249259Sdimprotected:
545249259Sdim  // allocate space for exactly zero operands.
546249259Sdim  void *operator new(size_t s) {
547249259Sdim    return User::operator new(s, 0);
548249259Sdim  }
549249259Sdimpublic:
550249259Sdim
551249259Sdim  /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
552249259Sdim  /// formed with a vector or array of the specified element type.
553249259Sdim  /// ConstantDataArray only works with normal float and int types that are
554249259Sdim  /// stored densely in memory, not with things like i42 or x86_f80.
555249259Sdim  static bool isElementTypeCompatible(const Type *Ty);
556249259Sdim
557249259Sdim  /// getElementAsInteger - If this is a sequential container of integers (of
558249259Sdim  /// any size), return the specified element in the low bits of a uint64_t.
559249259Sdim  uint64_t getElementAsInteger(unsigned i) const;
560249259Sdim
561249259Sdim  /// getElementAsAPFloat - If this is a sequential container of floating point
562249259Sdim  /// type, return the specified element as an APFloat.
563249259Sdim  APFloat getElementAsAPFloat(unsigned i) const;
564249259Sdim
565249259Sdim  /// getElementAsFloat - If this is an sequential container of floats, return
566249259Sdim  /// the specified element as a float.
567249259Sdim  float getElementAsFloat(unsigned i) const;
568249259Sdim
569249259Sdim  /// getElementAsDouble - If this is an sequential container of doubles, return
570249259Sdim  /// the specified element as a double.
571249259Sdim  double getElementAsDouble(unsigned i) const;
572249259Sdim
573249259Sdim  /// getElementAsConstant - Return a Constant for a specified index's element.
574249259Sdim  /// Note that this has to compute a new constant to return, so it isn't as
575249259Sdim  /// efficient as getElementAsInteger/Float/Double.
576249259Sdim  Constant *getElementAsConstant(unsigned i) const;
577249259Sdim
578249259Sdim  /// getType - Specialize the getType() method to always return a
579249259Sdim  /// SequentialType, which reduces the amount of casting needed in parts of the
580249259Sdim  /// compiler.
581249259Sdim  inline SequentialType *getType() const {
582251662Sdim    return cast<SequentialType>(Value::getType());
583249259Sdim  }
584249259Sdim
585249259Sdim  /// getElementType - Return the element type of the array/vector.
586249259Sdim  Type *getElementType() const;
587249259Sdim
588249259Sdim  /// getNumElements - Return the number of elements in the array or vector.
589249259Sdim  unsigned getNumElements() const;
590249259Sdim
591249259Sdim  /// getElementByteSize - Return the size (in bytes) of each element in the
592249259Sdim  /// array/vector.  The size of the elements is known to be a multiple of one
593249259Sdim  /// byte.
594249259Sdim  uint64_t getElementByteSize() const;
595249259Sdim
596249259Sdim
597249259Sdim  /// isString - This method returns true if this is an array of i8.
598249259Sdim  bool isString() const;
599249259Sdim
600249259Sdim  /// isCString - This method returns true if the array "isString", ends with a
601249259Sdim  /// nul byte, and does not contains any other nul bytes.
602249259Sdim  bool isCString() const;
603249259Sdim
604249259Sdim  /// getAsString - If this array is isString(), then this method returns the
605249259Sdim  /// array as a StringRef.  Otherwise, it asserts out.
606249259Sdim  ///
607249259Sdim  StringRef getAsString() const {
608249259Sdim    assert(isString() && "Not a string");
609249259Sdim    return getRawDataValues();
610249259Sdim  }
611249259Sdim
612249259Sdim  /// getAsCString - If this array is isCString(), then this method returns the
613249259Sdim  /// array (without the trailing null byte) as a StringRef. Otherwise, it
614249259Sdim  /// asserts out.
615249259Sdim  ///
616249259Sdim  StringRef getAsCString() const {
617249259Sdim    assert(isCString() && "Isn't a C string");
618249259Sdim    StringRef Str = getAsString();
619249259Sdim    return Str.substr(0, Str.size()-1);
620249259Sdim  }
621249259Sdim
622249259Sdim  /// getRawDataValues - Return the raw, underlying, bytes of this data.  Note
623249259Sdim  /// that this is an extremely tricky thing to work with, as it exposes the
624249259Sdim  /// host endianness of the data elements.
625249259Sdim  StringRef getRawDataValues() const;
626249259Sdim
627249259Sdim  virtual void destroyConstant();
628249259Sdim
629249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
630249259Sdim  ///
631249259Sdim  static bool classof(const Value *V) {
632249259Sdim    return V->getValueID() == ConstantDataArrayVal ||
633249259Sdim           V->getValueID() == ConstantDataVectorVal;
634249259Sdim  }
635249259Sdimprivate:
636249259Sdim  const char *getElementPointer(unsigned Elt) const;
637249259Sdim};
638249259Sdim
639249259Sdim//===----------------------------------------------------------------------===//
640249259Sdim/// ConstantDataArray - An array constant whose element type is a simple
641249259Sdim/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
642249259Sdim/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
643249259Sdim/// operands because it stores all of the elements of the constant as densely
644249259Sdim/// packed data, instead of as Value*'s.
645249259Sdimclass ConstantDataArray : public ConstantDataSequential {
646249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
647249259Sdim  ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
648249259Sdim  virtual void anchor();
649249259Sdim  friend class ConstantDataSequential;
650249259Sdim  explicit ConstantDataArray(Type *ty, const char *Data)
651249259Sdim    : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
652249259Sdimprotected:
653249259Sdim  // allocate space for exactly zero operands.
654249259Sdim  void *operator new(size_t s) {
655249259Sdim    return User::operator new(s, 0);
656249259Sdim  }
657249259Sdimpublic:
658249259Sdim
659249259Sdim  /// get() constructors - Return a constant with array type with an element
660249259Sdim  /// count and element type matching the ArrayRef passed in.  Note that this
661249259Sdim  /// can return a ConstantAggregateZero object.
662249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
663249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
664249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
665249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
666249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
667249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
668249259Sdim
669249259Sdim  /// getString - This method constructs a CDS and initializes it with a text
670249259Sdim  /// string. The default behavior (AddNull==true) causes a null terminator to
671249259Sdim  /// be placed at the end of the array (increasing the length of the string by
672249259Sdim  /// one more than the StringRef would normally indicate.  Pass AddNull=false
673249259Sdim  /// to disable this behavior.
674249259Sdim  static Constant *getString(LLVMContext &Context, StringRef Initializer,
675249259Sdim                             bool AddNull = true);
676249259Sdim
677249259Sdim  /// getType - Specialize the getType() method to always return an ArrayType,
678249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
679249259Sdim  ///
680249259Sdim  inline ArrayType *getType() const {
681251662Sdim    return cast<ArrayType>(Value::getType());
682249259Sdim  }
683249259Sdim
684249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
685249259Sdim  ///
686249259Sdim  static bool classof(const Value *V) {
687249259Sdim    return V->getValueID() == ConstantDataArrayVal;
688249259Sdim  }
689249259Sdim};
690249259Sdim
691249259Sdim//===----------------------------------------------------------------------===//
692249259Sdim/// ConstantDataVector - A vector constant whose element type is a simple
693249259Sdim/// 1/2/4/8-byte integer or float/double, and whose elements are just simple
694249259Sdim/// data values (i.e. ConstantInt/ConstantFP).  This Constant node has no
695249259Sdim/// operands because it stores all of the elements of the constant as densely
696249259Sdim/// packed data, instead of as Value*'s.
697249259Sdimclass ConstantDataVector : public ConstantDataSequential {
698249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
699249259Sdim  ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
700249259Sdim  virtual void anchor();
701249259Sdim  friend class ConstantDataSequential;
702249259Sdim  explicit ConstantDataVector(Type *ty, const char *Data)
703249259Sdim  : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
704249259Sdimprotected:
705249259Sdim  // allocate space for exactly zero operands.
706249259Sdim  void *operator new(size_t s) {
707249259Sdim    return User::operator new(s, 0);
708249259Sdim  }
709249259Sdimpublic:
710249259Sdim
711249259Sdim  /// get() constructors - Return a constant with vector type with an element
712249259Sdim  /// count and element type matching the ArrayRef passed in.  Note that this
713249259Sdim  /// can return a ConstantAggregateZero object.
714249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
715249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
716249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
717249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
718249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
719249259Sdim  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
720249259Sdim
721249259Sdim  /// getSplat - Return a ConstantVector with the specified constant in each
722249259Sdim  /// element.  The specified constant has to be a of a compatible type (i8/i16/
723249259Sdim  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
724249259Sdim  static Constant *getSplat(unsigned NumElts, Constant *Elt);
725249259Sdim
726249259Sdim  /// getSplatValue - If this is a splat constant, meaning that all of the
727249259Sdim  /// elements have the same value, return that value. Otherwise return NULL.
728249259Sdim  Constant *getSplatValue() const;
729249259Sdim
730249259Sdim  /// getType - Specialize the getType() method to always return a VectorType,
731249259Sdim  /// which reduces the amount of casting needed in parts of the compiler.
732249259Sdim  ///
733249259Sdim  inline VectorType *getType() const {
734251662Sdim    return cast<VectorType>(Value::getType());
735249259Sdim  }
736249259Sdim
737249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
738249259Sdim  ///
739249259Sdim  static bool classof(const Value *V) {
740249259Sdim    return V->getValueID() == ConstantDataVectorVal;
741249259Sdim  }
742249259Sdim};
743249259Sdim
744249259Sdim
745249259Sdim
746249259Sdim/// BlockAddress - The address of a basic block.
747249259Sdim///
748249259Sdimclass BlockAddress : public Constant {
749249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
750249259Sdim  void *operator new(size_t s) { return User::operator new(s, 2); }
751249259Sdim  BlockAddress(Function *F, BasicBlock *BB);
752249259Sdimpublic:
753249259Sdim  /// get - Return a BlockAddress for the specified function and basic block.
754249259Sdim  static BlockAddress *get(Function *F, BasicBlock *BB);
755249259Sdim
756249259Sdim  /// get - Return a BlockAddress for the specified basic block.  The basic
757249259Sdim  /// block must be embedded into a function.
758249259Sdim  static BlockAddress *get(BasicBlock *BB);
759249259Sdim
760249259Sdim  /// Transparently provide more efficient getOperand methods.
761249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
762249259Sdim
763249259Sdim  Function *getFunction() const { return (Function*)Op<0>().get(); }
764249259Sdim  BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
765249259Sdim
766249259Sdim  virtual void destroyConstant();
767249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
768249259Sdim
769249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
770249259Sdim  static inline bool classof(const Value *V) {
771249259Sdim    return V->getValueID() == BlockAddressVal;
772249259Sdim  }
773249259Sdim};
774249259Sdim
775249259Sdimtemplate <>
776249259Sdimstruct OperandTraits<BlockAddress> :
777249259Sdim  public FixedNumOperandTraits<BlockAddress, 2> {
778249259Sdim};
779249259Sdim
780249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
781249259Sdim
782249259Sdim
783249259Sdim//===----------------------------------------------------------------------===//
784249259Sdim/// ConstantExpr - a constant value that is initialized with an expression using
785249259Sdim/// other constant values.
786249259Sdim///
787249259Sdim/// This class uses the standard Instruction opcodes to define the various
788249259Sdim/// constant expressions.  The Opcode field for the ConstantExpr class is
789249259Sdim/// maintained in the Value::SubclassData field.
790249259Sdimclass ConstantExpr : public Constant {
791249259Sdim  friend struct ConstantCreator<ConstantExpr,Type,
792249259Sdim                            std::pair<unsigned, std::vector<Constant*> > >;
793249259Sdim  friend struct ConvertConstantType<ConstantExpr, Type>;
794249259Sdim
795249259Sdimprotected:
796249259Sdim  ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
797249259Sdim    : Constant(ty, ConstantExprVal, Ops, NumOps) {
798249259Sdim    // Operation type (an Instruction opcode) is stored as the SubclassData.
799249259Sdim    setValueSubclassData(Opcode);
800249259Sdim  }
801249259Sdim
802249259Sdimpublic:
803249259Sdim  // Static methods to construct a ConstantExpr of different kinds.  Note that
804249259Sdim  // these methods may return a object that is not an instance of the
805249259Sdim  // ConstantExpr class, because they will attempt to fold the constant
806249259Sdim  // expression into something simpler if possible.
807249259Sdim
808249259Sdim  /// getAlignOf constant expr - computes the alignment of a type in a target
809249259Sdim  /// independent way (Note: the return type is an i64).
810249259Sdim  static Constant *getAlignOf(Type *Ty);
811249259Sdim
812249259Sdim  /// getSizeOf constant expr - computes the (alloc) size of a type (in
813249259Sdim  /// address-units, not bits) in a target independent way (Note: the return
814249259Sdim  /// type is an i64).
815249259Sdim  ///
816249259Sdim  static Constant *getSizeOf(Type *Ty);
817249259Sdim
818249259Sdim  /// getOffsetOf constant expr - computes the offset of a struct field in a
819249259Sdim  /// target independent way (Note: the return type is an i64).
820249259Sdim  ///
821249259Sdim  static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
822249259Sdim
823249259Sdim  /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
824249259Sdim  /// which supports any aggregate type, and any Constant index.
825249259Sdim  ///
826249259Sdim  static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
827249259Sdim
828249259Sdim  static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
829249259Sdim  static Constant *getFNeg(Constant *C);
830249259Sdim  static Constant *getNot(Constant *C);
831249259Sdim  static Constant *getAdd(Constant *C1, Constant *C2,
832249259Sdim                          bool HasNUW = false, bool HasNSW = false);
833249259Sdim  static Constant *getFAdd(Constant *C1, Constant *C2);
834249259Sdim  static Constant *getSub(Constant *C1, Constant *C2,
835249259Sdim                          bool HasNUW = false, bool HasNSW = false);
836249259Sdim  static Constant *getFSub(Constant *C1, Constant *C2);
837249259Sdim  static Constant *getMul(Constant *C1, Constant *C2,
838249259Sdim                          bool HasNUW = false, bool HasNSW = false);
839249259Sdim  static Constant *getFMul(Constant *C1, Constant *C2);
840249259Sdim  static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
841249259Sdim  static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
842249259Sdim  static Constant *getFDiv(Constant *C1, Constant *C2);
843249259Sdim  static Constant *getURem(Constant *C1, Constant *C2);
844249259Sdim  static Constant *getSRem(Constant *C1, Constant *C2);
845249259Sdim  static Constant *getFRem(Constant *C1, Constant *C2);
846249259Sdim  static Constant *getAnd(Constant *C1, Constant *C2);
847249259Sdim  static Constant *getOr(Constant *C1, Constant *C2);
848249259Sdim  static Constant *getXor(Constant *C1, Constant *C2);
849249259Sdim  static Constant *getShl(Constant *C1, Constant *C2,
850249259Sdim                          bool HasNUW = false, bool HasNSW = false);
851249259Sdim  static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
852249259Sdim  static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
853249259Sdim  static Constant *getTrunc   (Constant *C, Type *Ty);
854249259Sdim  static Constant *getSExt    (Constant *C, Type *Ty);
855249259Sdim  static Constant *getZExt    (Constant *C, Type *Ty);
856249259Sdim  static Constant *getFPTrunc (Constant *C, Type *Ty);
857249259Sdim  static Constant *getFPExtend(Constant *C, Type *Ty);
858249259Sdim  static Constant *getUIToFP  (Constant *C, Type *Ty);
859249259Sdim  static Constant *getSIToFP  (Constant *C, Type *Ty);
860249259Sdim  static Constant *getFPToUI  (Constant *C, Type *Ty);
861249259Sdim  static Constant *getFPToSI  (Constant *C, Type *Ty);
862249259Sdim  static Constant *getPtrToInt(Constant *C, Type *Ty);
863249259Sdim  static Constant *getIntToPtr(Constant *C, Type *Ty);
864249259Sdim  static Constant *getBitCast (Constant *C, Type *Ty);
865263508Sdim  static Constant *getAddrSpaceCast(Constant *C, Type *Ty);
866249259Sdim
867249259Sdim  static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
868249259Sdim  static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
869249259Sdim  static Constant *getNSWAdd(Constant *C1, Constant *C2) {
870249259Sdim    return getAdd(C1, C2, false, true);
871249259Sdim  }
872249259Sdim  static Constant *getNUWAdd(Constant *C1, Constant *C2) {
873249259Sdim    return getAdd(C1, C2, true, false);
874249259Sdim  }
875249259Sdim  static Constant *getNSWSub(Constant *C1, Constant *C2) {
876249259Sdim    return getSub(C1, C2, false, true);
877249259Sdim  }
878249259Sdim  static Constant *getNUWSub(Constant *C1, Constant *C2) {
879249259Sdim    return getSub(C1, C2, true, false);
880249259Sdim  }
881249259Sdim  static Constant *getNSWMul(Constant *C1, Constant *C2) {
882249259Sdim    return getMul(C1, C2, false, true);
883249259Sdim  }
884249259Sdim  static Constant *getNUWMul(Constant *C1, Constant *C2) {
885249259Sdim    return getMul(C1, C2, true, false);
886249259Sdim  }
887249259Sdim  static Constant *getNSWShl(Constant *C1, Constant *C2) {
888249259Sdim    return getShl(C1, C2, false, true);
889249259Sdim  }
890249259Sdim  static Constant *getNUWShl(Constant *C1, Constant *C2) {
891249259Sdim    return getShl(C1, C2, true, false);
892249259Sdim  }
893249259Sdim  static Constant *getExactSDiv(Constant *C1, Constant *C2) {
894249259Sdim    return getSDiv(C1, C2, true);
895249259Sdim  }
896249259Sdim  static Constant *getExactUDiv(Constant *C1, Constant *C2) {
897249259Sdim    return getUDiv(C1, C2, true);
898249259Sdim  }
899249259Sdim  static Constant *getExactAShr(Constant *C1, Constant *C2) {
900249259Sdim    return getAShr(C1, C2, true);
901249259Sdim  }
902249259Sdim  static Constant *getExactLShr(Constant *C1, Constant *C2) {
903249259Sdim    return getLShr(C1, C2, true);
904249259Sdim  }
905249259Sdim
906249259Sdim  /// getBinOpIdentity - Return the identity for the given binary operation,
907249259Sdim  /// i.e. a constant C such that X op C = X and C op X = X for every X.  It
908249259Sdim  /// returns null if the operator doesn't have an identity.
909249259Sdim  static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
910249259Sdim
911249259Sdim  /// getBinOpAbsorber - Return the absorbing element for the given binary
912249259Sdim  /// operation, i.e. a constant C such that X op C = C and C op X = C for
913249259Sdim  /// every X.  For example, this returns zero for integer multiplication.
914249259Sdim  /// It returns null if the operator doesn't have an absorbing element.
915249259Sdim  static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
916249259Sdim
917249259Sdim  /// Transparently provide more efficient getOperand methods.
918249259Sdim  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
919249259Sdim
920249259Sdim  // @brief Convenience function for getting one of the casting operations
921249259Sdim  // using a CastOps opcode.
922249259Sdim  static Constant *getCast(
923249259Sdim    unsigned ops,  ///< The opcode for the conversion
924249259Sdim    Constant *C,   ///< The constant to be converted
925249259Sdim    Type *Ty ///< The type to which the constant is converted
926249259Sdim  );
927249259Sdim
928249259Sdim  // @brief Create a ZExt or BitCast cast constant expression
929249259Sdim  static Constant *getZExtOrBitCast(
930249259Sdim    Constant *C,   ///< The constant to zext or bitcast
931249259Sdim    Type *Ty ///< The type to zext or bitcast C to
932249259Sdim  );
933249259Sdim
934249259Sdim  // @brief Create a SExt or BitCast cast constant expression
935249259Sdim  static Constant *getSExtOrBitCast(
936249259Sdim    Constant *C,   ///< The constant to sext or bitcast
937249259Sdim    Type *Ty ///< The type to sext or bitcast C to
938249259Sdim  );
939249259Sdim
940249259Sdim  // @brief Create a Trunc or BitCast cast constant expression
941249259Sdim  static Constant *getTruncOrBitCast(
942249259Sdim    Constant *C,   ///< The constant to trunc or bitcast
943249259Sdim    Type *Ty ///< The type to trunc or bitcast C to
944249259Sdim  );
945249259Sdim
946263508Sdim  /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
947263508Sdim  /// expression.
948249259Sdim  static Constant *getPointerCast(
949249259Sdim    Constant *C,   ///< The pointer value to be casted (operand 0)
950249259Sdim    Type *Ty ///< The type to which cast should be made
951249259Sdim  );
952249259Sdim
953263508Sdim  /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
954263508Sdim  /// the address space.
955263508Sdim  static Constant *getPointerBitCastOrAddrSpaceCast(
956263508Sdim    Constant *C,   ///< The constant to addrspacecast or bitcast
957263508Sdim    Type *Ty ///< The type to bitcast or addrspacecast C to
958263508Sdim  );
959263508Sdim
960249259Sdim  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
961249259Sdim  static Constant *getIntegerCast(
962249259Sdim    Constant *C,    ///< The integer constant to be casted
963249259Sdim    Type *Ty, ///< The integer type to cast to
964249259Sdim    bool isSigned   ///< Whether C should be treated as signed or not
965249259Sdim  );
966249259Sdim
967249259Sdim  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
968249259Sdim  static Constant *getFPCast(
969249259Sdim    Constant *C,    ///< The integer constant to be casted
970249259Sdim    Type *Ty ///< The integer type to cast to
971249259Sdim  );
972249259Sdim
973249259Sdim  /// @brief Return true if this is a convert constant expression
974249259Sdim  bool isCast() const;
975249259Sdim
976249259Sdim  /// @brief Return true if this is a compare constant expression
977249259Sdim  bool isCompare() const;
978249259Sdim
979249259Sdim  /// @brief Return true if this is an insertvalue or extractvalue expression,
980249259Sdim  /// and the getIndices() method may be used.
981249259Sdim  bool hasIndices() const;
982249259Sdim
983249259Sdim  /// @brief Return true if this is a getelementptr expression and all
984249259Sdim  /// the index operands are compile-time known integers within the
985249259Sdim  /// corresponding notional static array extents. Note that this is
986249259Sdim  /// not equivalant to, a subset of, or a superset of the "inbounds"
987249259Sdim  /// property.
988249259Sdim  bool isGEPWithNoNotionalOverIndexing() const;
989249259Sdim
990249259Sdim  /// Select constant expr
991249259Sdim  ///
992249259Sdim  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
993249259Sdim
994249259Sdim  /// get - Return a binary or shift operator constant expression,
995249259Sdim  /// folding if possible.
996249259Sdim  ///
997249259Sdim  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
998249259Sdim                       unsigned Flags = 0);
999249259Sdim
1000249259Sdim  /// @brief Return an ICmp or FCmp comparison operator constant expression.
1001249259Sdim  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
1002249259Sdim
1003249259Sdim  /// get* - Return some common constants without having to
1004249259Sdim  /// specify the full Instruction::OPCODE identifier.
1005249259Sdim  ///
1006249259Sdim  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1007249259Sdim  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1008249259Sdim
1009249259Sdim  /// Getelementptr form.  Value* is only accepted for convenience;
1010249259Sdim  /// all elements must be Constant's.
1011249259Sdim  ///
1012249259Sdim  static Constant *getGetElementPtr(Constant *C,
1013249259Sdim                                    ArrayRef<Constant *> IdxList,
1014249259Sdim                                    bool InBounds = false) {
1015249259Sdim    return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1016249259Sdim                                            IdxList.size()),
1017249259Sdim                            InBounds);
1018249259Sdim  }
1019249259Sdim  static Constant *getGetElementPtr(Constant *C,
1020249259Sdim                                    Constant *Idx,
1021249259Sdim                                    bool InBounds = false) {
1022249259Sdim    // This form of the function only exists to avoid ambiguous overload
1023249259Sdim    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1024249259Sdim    // ArrayRef<Value *>.
1025249259Sdim    return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1026249259Sdim  }
1027249259Sdim  static Constant *getGetElementPtr(Constant *C,
1028249259Sdim                                    ArrayRef<Value *> IdxList,
1029249259Sdim                                    bool InBounds = false);
1030249259Sdim
1031249259Sdim  /// Create an "inbounds" getelementptr. See the documentation for the
1032249259Sdim  /// "inbounds" flag in LangRef.html for details.
1033249259Sdim  static Constant *getInBoundsGetElementPtr(Constant *C,
1034249259Sdim                                            ArrayRef<Constant *> IdxList) {
1035249259Sdim    return getGetElementPtr(C, IdxList, true);
1036249259Sdim  }
1037249259Sdim  static Constant *getInBoundsGetElementPtr(Constant *C,
1038249259Sdim                                            Constant *Idx) {
1039249259Sdim    // This form of the function only exists to avoid ambiguous overload
1040249259Sdim    // warnings about whether to convert Idx to ArrayRef<Constant *> or
1041249259Sdim    // ArrayRef<Value *>.
1042249259Sdim    return getGetElementPtr(C, Idx, true);
1043249259Sdim  }
1044249259Sdim  static Constant *getInBoundsGetElementPtr(Constant *C,
1045249259Sdim                                            ArrayRef<Value *> IdxList) {
1046249259Sdim    return getGetElementPtr(C, IdxList, true);
1047249259Sdim  }
1048249259Sdim
1049249259Sdim  static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1050249259Sdim  static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1051249259Sdim  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1052249259Sdim  static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1053249259Sdim  static Constant *getInsertValue(Constant *Agg, Constant *Val,
1054249259Sdim                                  ArrayRef<unsigned> Idxs);
1055249259Sdim
1056249259Sdim  /// getOpcode - Return the opcode at the root of this constant expression
1057249259Sdim  unsigned getOpcode() const { return getSubclassDataFromValue(); }
1058249259Sdim
1059249259Sdim  /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1060249259Sdim  /// not an ICMP or FCMP constant expression.
1061249259Sdim  unsigned getPredicate() const;
1062249259Sdim
1063249259Sdim  /// getIndices - Assert that this is an insertvalue or exactvalue
1064249259Sdim  /// expression and return the list of indices.
1065249259Sdim  ArrayRef<unsigned> getIndices() const;
1066249259Sdim
1067249259Sdim  /// getOpcodeName - Return a string representation for an opcode.
1068249259Sdim  const char *getOpcodeName() const;
1069249259Sdim
1070249259Sdim  /// getWithOperandReplaced - Return a constant expression identical to this
1071249259Sdim  /// one, but with the specified operand set to the specified value.
1072249259Sdim  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1073249259Sdim
1074249259Sdim  /// getWithOperands - This returns the current constant expression with the
1075249259Sdim  /// operands replaced with the specified values.  The specified array must
1076249259Sdim  /// have the same number of operands as our current one.
1077249259Sdim  Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1078249259Sdim    return getWithOperands(Ops, getType());
1079249259Sdim  }
1080249259Sdim
1081249259Sdim  /// getWithOperands - This returns the current constant expression with the
1082249259Sdim  /// operands replaced with the specified values and with the specified result
1083249259Sdim  /// type.  The specified array must have the same number of operands as our
1084249259Sdim  /// current one.
1085249259Sdim  Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1086249259Sdim
1087249259Sdim  /// getAsInstruction - Returns an Instruction which implements the same operation
1088249259Sdim  /// as this ConstantExpr. The instruction is not linked to any basic block.
1089249259Sdim  ///
1090249259Sdim  /// A better approach to this could be to have a constructor for Instruction
1091263508Sdim  /// which would take a ConstantExpr parameter, but that would have spread
1092263508Sdim  /// implementation details of ConstantExpr outside of Constants.cpp, which
1093249259Sdim  /// would make it harder to remove ConstantExprs altogether.
1094249259Sdim  Instruction *getAsInstruction();
1095249259Sdim
1096249259Sdim  virtual void destroyConstant();
1097249259Sdim  virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1098249259Sdim
1099249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1100249259Sdim  static inline bool classof(const Value *V) {
1101249259Sdim    return V->getValueID() == ConstantExprVal;
1102249259Sdim  }
1103249259Sdim
1104249259Sdimprivate:
1105249259Sdim  // Shadow Value::setValueSubclassData with a private forwarding method so that
1106249259Sdim  // subclasses cannot accidentally use it.
1107249259Sdim  void setValueSubclassData(unsigned short D) {
1108249259Sdim    Value::setValueSubclassData(D);
1109249259Sdim  }
1110249259Sdim};
1111249259Sdim
1112249259Sdimtemplate <>
1113249259Sdimstruct OperandTraits<ConstantExpr> :
1114249259Sdim  public VariadicOperandTraits<ConstantExpr, 1> {
1115249259Sdim};
1116249259Sdim
1117249259SdimDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1118249259Sdim
1119249259Sdim//===----------------------------------------------------------------------===//
1120249259Sdim/// UndefValue - 'undef' values are things that do not have specified contents.
1121249259Sdim/// These are used for a variety of purposes, including global variable
1122249259Sdim/// initializers and operands to instructions.  'undef' values can occur with
1123249259Sdim/// any first-class type.
1124249259Sdim///
1125249259Sdim/// Undef values aren't exactly constants; if they have multiple uses, they
1126249259Sdim/// can appear to have different bit patterns at each use. See
1127249259Sdim/// LangRef.html#undefvalues for details.
1128249259Sdim///
1129249259Sdimclass UndefValue : public Constant {
1130249259Sdim  void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1131249259Sdim  UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1132249259Sdimprotected:
1133249259Sdim  explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1134249259Sdimprotected:
1135249259Sdim  // allocate space for exactly zero operands
1136249259Sdim  void *operator new(size_t s) {
1137249259Sdim    return User::operator new(s, 0);
1138249259Sdim  }
1139249259Sdimpublic:
1140249259Sdim  /// get() - Static factory methods - Return an 'undef' object of the specified
1141249259Sdim  /// type.
1142249259Sdim  ///
1143249259Sdim  static UndefValue *get(Type *T);
1144249259Sdim
1145249259Sdim  /// getSequentialElement - If this Undef has array or vector type, return a
1146249259Sdim  /// undef with the right element type.
1147249259Sdim  UndefValue *getSequentialElement() const;
1148249259Sdim
1149249259Sdim  /// getStructElement - If this undef has struct type, return a undef with the
1150249259Sdim  /// right element type for the specified element.
1151249259Sdim  UndefValue *getStructElement(unsigned Elt) const;
1152249259Sdim
1153249259Sdim  /// getElementValue - Return an undef of the right value for the specified GEP
1154249259Sdim  /// index.
1155249259Sdim  UndefValue *getElementValue(Constant *C) const;
1156249259Sdim
1157249259Sdim  /// getElementValue - Return an undef of the right value for the specified GEP
1158249259Sdim  /// index.
1159249259Sdim  UndefValue *getElementValue(unsigned Idx) const;
1160249259Sdim
1161249259Sdim  virtual void destroyConstant();
1162249259Sdim
1163249259Sdim  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1164249259Sdim  static bool classof(const Value *V) {
1165249259Sdim    return V->getValueID() == UndefValueVal;
1166249259Sdim  }
1167249259Sdim};
1168249259Sdim
1169249259Sdim} // End llvm namespace
1170249259Sdim
1171249259Sdim#endif
1172