LLVM  7.0.0svn
Constants.h
Go to the documentation of this file.
1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// This file contains the declarations for the subclasses of Constant,
12 /// which represent the different flavors of constant values that live in LLVM.
13 /// Note that Constants are immutable (once created they never change) and are
14 /// fully shared by structural equivalence. This means that two structurally
15 /// equivalent constants will always have the same address. Constants are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_IR_CONSTANTS_H
22 #define LLVM_IR_CONSTANTS_H
23 
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/None.h"
28 #include "llvm/ADT/Optional.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/IR/Constant.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/OperandTraits.h"
34 #include "llvm/IR/User.h"
35 #include "llvm/IR/Value.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/Compiler.h"
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 
43 namespace llvm {
44 
45 class ArrayType;
46 class IntegerType;
47 class PointerType;
48 class SequentialType;
49 class StructType;
50 class VectorType;
51 template <class ConstantClass> struct ConstantAggrKeyType;
52 
53 /// Base class for constants with no operands.
54 ///
55 /// These constants have no operands; they represent their data directly.
56 /// Since they can be in use by unrelated modules (and are never based on
57 /// GlobalValues), it never makes sense to RAUW them.
58 class ConstantData : public Constant {
59  friend class Constant;
60 
61  Value *handleOperandChangeImpl(Value *From, Value *To) {
62  llvm_unreachable("Constant data does not have operands!");
63  }
64 
65 protected:
66  explicit ConstantData(Type *Ty, ValueTy VT) : Constant(Ty, VT, nullptr, 0) {}
67 
68  void *operator new(size_t s) { return User::operator new(s, 0); }
69 
70 public:
71  ConstantData(const ConstantData &) = delete;
72 
73  /// Methods to support type inquiry through isa, cast, and dyn_cast.
74  static bool classof(const Value *V) {
75  return V->getValueID() >= ConstantDataFirstVal &&
76  V->getValueID() <= ConstantDataLastVal;
77  }
78 };
79 
80 //===----------------------------------------------------------------------===//
81 /// This is the shared class of boolean and integer constants. This class
82 /// represents both boolean and integral constants.
83 /// @brief Class for constant integers.
84 class ConstantInt final : public ConstantData {
85  friend class Constant;
86 
87  APInt Val;
88 
89  ConstantInt(IntegerType *Ty, const APInt& V);
90 
91  void destroyConstantImpl();
92 
93 public:
94  ConstantInt(const ConstantInt &) = delete;
95 
98  static Constant *getTrue(Type *Ty);
99  static Constant *getFalse(Type *Ty);
100 
101  /// If Ty is a vector type, return a Constant with a splat of the given
102  /// value. Otherwise return a ConstantInt for the given value.
103  static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
104 
105  /// Return a ConstantInt with the specified integer value for the specified
106  /// type. If the type is wider than 64 bits, the value will be zero-extended
107  /// to fit the type, unless isSigned is true, in which case the value will
108  /// be interpreted as a 64-bit signed integer and sign-extended to fit
109  /// the type.
110  /// @brief Get a ConstantInt for a specific value.
111  static ConstantInt *get(IntegerType *Ty, uint64_t V,
112  bool isSigned = false);
113 
114  /// Return a ConstantInt with the specified value for the specified type. The
115  /// value V will be canonicalized to a an unsigned APInt. Accessing it with
116  /// either getSExtValue() or getZExtValue() will yield a correctly sized and
117  /// signed value for the type Ty.
118  /// @brief Get a ConstantInt for a specific signed value.
119  static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
120  static Constant *getSigned(Type *Ty, int64_t V);
121 
122  /// Return a ConstantInt with the specified value and an implied Type. The
123  /// type is the integer type that corresponds to the bit width of the value.
124  static ConstantInt *get(LLVMContext &Context, const APInt &V);
125 
126  /// Return a ConstantInt constructed from the string strStart with the given
127  /// radix.
128  static ConstantInt *get(IntegerType *Ty, StringRef Str,
129  uint8_t radix);
130 
131  /// If Ty is a vector type, return a Constant with a splat of the given
132  /// value. Otherwise return a ConstantInt for the given value.
133  static Constant *get(Type* Ty, const APInt& V);
134 
135  /// Return the constant as an APInt value reference. This allows clients to
136  /// obtain a full-precision copy of the value.
137  /// @brief Return the constant's value.
138  inline const APInt &getValue() const {
139  return Val;
140  }
141 
142  /// getBitWidth - Return the bitwidth of this constant.
143  unsigned getBitWidth() const { return Val.getBitWidth(); }
144 
145  /// Return the constant as a 64-bit unsigned integer value after it
146  /// has been zero extended as appropriate for the type of this constant. Note
147  /// that this method can assert if the value does not fit in 64 bits.
148  /// @brief Return the zero extended value.
149  inline uint64_t getZExtValue() const {
150  return Val.getZExtValue();
151  }
152 
153  /// Return the constant as a 64-bit integer value after it has been sign
154  /// extended as appropriate for the type of this constant. Note that
155  /// this method can assert if the value does not fit in 64 bits.
156  /// @brief Return the sign extended value.
157  inline int64_t getSExtValue() const {
158  return Val.getSExtValue();
159  }
160 
161  /// A helper method that can be used to determine if the constant contained
162  /// within is equal to a constant. This only works for very small values,
163  /// because this is all that can be represented with all types.
164  /// @brief Determine if this constant's value is same as an unsigned char.
165  bool equalsInt(uint64_t V) const {
166  return Val == V;
167  }
168 
169  /// getType - Specialize the getType() method to always return an IntegerType,
170  /// which reduces the amount of casting needed in parts of the compiler.
171  ///
172  inline IntegerType *getType() const {
173  return cast<IntegerType>(Value::getType());
174  }
175 
176  /// This static method returns true if the type Ty is big enough to
177  /// represent the value V. This can be used to avoid having the get method
178  /// assert when V is larger than Ty can represent. Note that there are two
179  /// versions of this method, one for unsigned and one for signed integers.
180  /// Although ConstantInt canonicalizes everything to an unsigned integer,
181  /// the signed version avoids callers having to convert a signed quantity
182  /// to the appropriate unsigned type before calling the method.
183  /// @returns true if V is a valid value for type Ty
184  /// @brief Determine if the value is in range for the given type.
185  static bool isValueValidForType(Type *Ty, uint64_t V);
186  static bool isValueValidForType(Type *Ty, int64_t V);
187 
188  bool isNegative() const { return Val.isNegative(); }
189 
190  /// This is just a convenience method to make client code smaller for a
191  /// common code. It also correctly performs the comparison without the
192  /// potential for an assertion from getZExtValue().
193  bool isZero() const {
194  return Val.isNullValue();
195  }
196 
197  /// This is just a convenience method to make client code smaller for a
198  /// common case. It also correctly performs the comparison without the
199  /// potential for an assertion from getZExtValue().
200  /// @brief Determine if the value is one.
201  bool isOne() const {
202  return Val.isOneValue();
203  }
204 
205  /// This function will return true iff every bit in this constant is set
206  /// to true.
207  /// @returns true iff this constant's bits are all set to true.
208  /// @brief Determine if the value is all ones.
209  bool isMinusOne() const {
210  return Val.isAllOnesValue();
211  }
212 
213  /// This function will return true iff this constant represents the largest
214  /// value that may be represented by the constant's type.
215  /// @returns true iff this is the largest value that may be represented
216  /// by this type.
217  /// @brief Determine if the value is maximal.
218  bool isMaxValue(bool isSigned) const {
219  if (isSigned)
220  return Val.isMaxSignedValue();
221  else
222  return Val.isMaxValue();
223  }
224 
225  /// This function will return true iff this constant represents the smallest
226  /// value that may be represented by this constant's type.
227  /// @returns true if this is the smallest value that may be represented by
228  /// this type.
229  /// @brief Determine if the value is minimal.
230  bool isMinValue(bool isSigned) const {
231  if (isSigned)
232  return Val.isMinSignedValue();
233  else
234  return Val.isMinValue();
235  }
236 
237  /// This function will return true iff this constant represents a value with
238  /// active bits bigger than 64 bits or a value greater than the given uint64_t
239  /// value.
240  /// @returns true iff this constant is greater or equal to the given number.
241  /// @brief Determine if the value is greater or equal to the given number.
242  bool uge(uint64_t Num) const {
243  return Val.uge(Num);
244  }
245 
246  /// getLimitedValue - If the value is smaller than the specified limit,
247  /// return it, otherwise return the limit value. This causes the value
248  /// to saturate to the limit.
249  /// @returns the min of the value of the constant and the specified value
250  /// @brief Get the constant's value with a saturation limit
251  uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
252  return Val.getLimitedValue(Limit);
253  }
254 
255  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
256  static bool classof(const Value *V) {
257  return V->getValueID() == ConstantIntVal;
258  }
259 };
260 
261 //===----------------------------------------------------------------------===//
262 /// ConstantFP - Floating Point Values [float, double]
263 ///
264 class ConstantFP final : public ConstantData {
265  friend class Constant;
266 
267  APFloat Val;
268 
269  ConstantFP(Type *Ty, const APFloat& V);
270 
271  void destroyConstantImpl();
272 
273 public:
274  ConstantFP(const ConstantFP &) = delete;
275 
276  /// Floating point negation must be implemented with f(x) = -0.0 - x. This
277  /// method returns the negative zero constant for floating point or vector
278  /// floating point types; for all other types, it returns the null value.
279  static Constant *getZeroValueForNegation(Type *Ty);
280 
281  /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
282  /// for the specified value in the specified type. This should only be used
283  /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
284  /// host double and as the target format.
285  static Constant *get(Type* Ty, double V);
286 
287  /// If Ty is a vector type, return a Constant with a splat of the given
288  /// value. Otherwise return a ConstantFP for the given value.
289  static Constant *get(Type *Ty, const APFloat &V);
290 
291  static Constant *get(Type* Ty, StringRef Str);
292  static ConstantFP *get(LLVMContext &Context, const APFloat &V);
293  static Constant *getNaN(Type *Ty, bool Negative = false, unsigned type = 0);
294  static Constant *getNegativeZero(Type *Ty);
295  static Constant *getInfinity(Type *Ty, bool Negative = false);
296 
297  /// Return true if Ty is big enough to represent V.
298  static bool isValueValidForType(Type *Ty, const APFloat &V);
299  inline const APFloat &getValueAPF() const { return Val; }
300 
301  /// Return true if the value is positive or negative zero.
302  bool isZero() const { return Val.isZero(); }
303 
304  /// Return true if the sign bit is set.
305  bool isNegative() const { return Val.isNegative(); }
306 
307  /// Return true if the value is infinity
308  bool isInfinity() const { return Val.isInfinity(); }
309 
310  /// Return true if the value is a NaN.
311  bool isNaN() const { return Val.isNaN(); }
312 
313  /// We don't rely on operator== working on double values, as it returns true
314  /// for things that are clearly not equal, like -0.0 and 0.0.
315  /// As such, this method can be used to do an exact bit-for-bit comparison of
316  /// two floating point values. The version with a double operand is retained
317  /// because it's so convenient to write isExactlyValue(2.0), but please use
318  /// it only for simple constants.
319  bool isExactlyValue(const APFloat &V) const;
320 
321  bool isExactlyValue(double V) const {
322  bool ignored;
323  APFloat FV(V);
325  return isExactlyValue(FV);
326  }
327 
328  /// Methods for support type inquiry through isa, cast, and dyn_cast:
329  static bool classof(const Value *V) {
330  return V->getValueID() == ConstantFPVal;
331  }
332 };
333 
334 //===----------------------------------------------------------------------===//
335 /// All zero aggregate value
336 ///
337 class ConstantAggregateZero final : public ConstantData {
338  friend class Constant;
339 
340  explicit ConstantAggregateZero(Type *Ty)
341  : ConstantData(Ty, ConstantAggregateZeroVal) {}
342 
343  void destroyConstantImpl();
344 
345 public:
347 
348  static ConstantAggregateZero *get(Type *Ty);
349 
350  /// If this CAZ has array or vector type, return a zero with the right element
351  /// type.
352  Constant *getSequentialElement() const;
353 
354  /// If this CAZ has struct type, return a zero with the right element type for
355  /// the specified element.
356  Constant *getStructElement(unsigned Elt) const;
357 
358  /// Return a zero of the right value for the specified GEP index if we can,
359  /// otherwise return null (e.g. if C is a ConstantExpr).
360  Constant *getElementValue(Constant *C) const;
361 
362  /// Return a zero of the right value for the specified GEP index.
363  Constant *getElementValue(unsigned Idx) const;
364 
365  /// Return the number of elements in the array, vector, or struct.
366  unsigned getNumElements() const;
367 
368  /// Methods for support type inquiry through isa, cast, and dyn_cast:
369  ///
370  static bool classof(const Value *V) {
371  return V->getValueID() == ConstantAggregateZeroVal;
372  }
373 };
374 
375 /// Base class for aggregate constants (with operands).
376 ///
377 /// These constants are aggregates of other constants, which are stored as
378 /// operands.
379 ///
380 /// Subclasses are \a ConstantStruct, \a ConstantArray, and \a
381 /// ConstantVector.
382 ///
383 /// \note Some subclasses of \a ConstantData are semantically aggregates --
384 /// such as \a ConstantDataArray -- but are not subclasses of this because they
385 /// use operands.
386 class ConstantAggregate : public Constant {
387 protected:
389 
390 public:
391  /// Transparently provide more efficient getOperand methods.
393 
394  /// Methods for support type inquiry through isa, cast, and dyn_cast:
395  static bool classof(const Value *V) {
396  return V->getValueID() >= ConstantAggregateFirstVal &&
397  V->getValueID() <= ConstantAggregateLastVal;
398  }
399 };
400 
401 template <>
403  : public VariadicOperandTraits<ConstantAggregate> {};
404 
406 
407 //===----------------------------------------------------------------------===//
408 /// ConstantArray - Constant Array Declarations
409 ///
410 class ConstantArray final : public ConstantAggregate {
412  friend class Constant;
413 
415 
416  void destroyConstantImpl();
417  Value *handleOperandChangeImpl(Value *From, Value *To);
418 
419 public:
420  // ConstantArray accessors
421  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
422 
423 private:
425 
426 public:
427  /// Specialize the getType() method to always return an ArrayType,
428  /// which reduces the amount of casting needed in parts of the compiler.
429  inline ArrayType *getType() const {
430  return cast<ArrayType>(Value::getType());
431  }
432 
433  /// Methods for support type inquiry through isa, cast, and dyn_cast:
434  static bool classof(const Value *V) {
435  return V->getValueID() == ConstantArrayVal;
436  }
437 };
438 
439 //===----------------------------------------------------------------------===//
440 // Constant Struct Declarations
441 //
442 class ConstantStruct final : public ConstantAggregate {
444  friend class Constant;
445 
447 
448  void destroyConstantImpl();
449  Value *handleOperandChangeImpl(Value *From, Value *To);
450 
451 public:
452  // ConstantStruct accessors
453  static Constant *get(StructType *T, ArrayRef<Constant*> V);
454 
455  template <typename... Csts>
456  static typename std::enable_if<are_base_of<Constant, Csts...>::value,
457  Constant *>::type
458  get(StructType *T, Csts *... Vs) {
459  SmallVector<Constant *, 8> Values({Vs...});
460  return get(T, Values);
461  }
462 
463  /// Return an anonymous struct that has the specified elements.
464  /// If the struct is possibly empty, then you must specify a context.
465  static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
466  return get(getTypeForElements(V, Packed), V);
467  }
468  static Constant *getAnon(LLVMContext &Ctx,
469  ArrayRef<Constant*> V, bool Packed = false) {
470  return get(getTypeForElements(Ctx, V, Packed), V);
471  }
472 
473  /// Return an anonymous struct type to use for a constant with the specified
474  /// set of elements. The list must not be empty.
475  static StructType *getTypeForElements(ArrayRef<Constant*> V,
476  bool Packed = false);
477  /// This version of the method allows an empty list.
478  static StructType *getTypeForElements(LLVMContext &Ctx,
480  bool Packed = false);
481 
482  /// Specialization - reduce amount of casting.
483  inline StructType *getType() const {
484  return cast<StructType>(Value::getType());
485  }
486 
487  /// Methods for support type inquiry through isa, cast, and dyn_cast:
488  static bool classof(const Value *V) {
489  return V->getValueID() == ConstantStructVal;
490  }
491 };
492 
493 //===----------------------------------------------------------------------===//
494 /// Constant Vector Declarations
495 ///
496 class ConstantVector final : public ConstantAggregate {
498  friend class Constant;
499 
501 
502  void destroyConstantImpl();
503  Value *handleOperandChangeImpl(Value *From, Value *To);
504 
505 public:
506  // ConstantVector accessors
507  static Constant *get(ArrayRef<Constant*> V);
508 
509 private:
511 
512 public:
513  /// Return a ConstantVector with the specified constant in each element.
514  static Constant *getSplat(unsigned NumElts, Constant *Elt);
515 
516  /// Specialize the getType() method to always return a VectorType,
517  /// which reduces the amount of casting needed in parts of the compiler.
518  inline VectorType *getType() const {
519  return cast<VectorType>(Value::getType());
520  }
521 
522  /// If this is a splat constant, meaning that all of the elements have the
523  /// same value, return that value. Otherwise return NULL.
524  Constant *getSplatValue() const;
525 
526  /// Methods for support type inquiry through isa, cast, and dyn_cast:
527  static bool classof(const Value *V) {
528  return V->getValueID() == ConstantVectorVal;
529  }
530 };
531 
532 //===----------------------------------------------------------------------===//
533 /// A constant pointer value that points to null
534 ///
535 class ConstantPointerNull final : public ConstantData {
536  friend class Constant;
537 
539  : ConstantData(T, Value::ConstantPointerNullVal) {}
540 
541  void destroyConstantImpl();
542 
543 public:
544  ConstantPointerNull(const ConstantPointerNull &) = delete;
545 
546  /// Static factory methods - Return objects of the specified value
547  static ConstantPointerNull *get(PointerType *T);
548 
549  /// Specialize the getType() method to always return an PointerType,
550  /// which reduces the amount of casting needed in parts of the compiler.
551  inline PointerType *getType() const {
552  return cast<PointerType>(Value::getType());
553  }
554 
555  /// Methods for support type inquiry through isa, cast, and dyn_cast:
556  static bool classof(const Value *V) {
557  return V->getValueID() == ConstantPointerNullVal;
558  }
559 };
560 
561 //===----------------------------------------------------------------------===//
562 /// ConstantDataSequential - A vector or array constant whose element type is a
563 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
564 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
565 /// operands because it stores all of the elements of the constant as densely
566 /// packed data, instead of as Value*'s.
567 ///
568 /// This is the common base class of ConstantDataArray and ConstantDataVector.
569 ///
571  friend class LLVMContextImpl;
572  friend class Constant;
573 
574  /// A pointer to the bytes underlying this constant (which is owned by the
575  /// uniquing StringMap).
576  const char *DataElements;
577 
578  /// This forms a link list of ConstantDataSequential nodes that have
579  /// the same value but different type. For example, 0,0,0,1 could be a 4
580  /// element array of i8, or a 1-element array of i32. They'll both end up in
581  /// the same StringMap bucket, linked up.
583 
584  void destroyConstantImpl();
585 
586 protected:
587  explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
588  : ConstantData(ty, VT), DataElements(Data), Next(nullptr) {}
589  ~ConstantDataSequential() { delete Next; }
590 
591  static Constant *getImpl(StringRef Bytes, Type *Ty);
592 
593 public:
595 
596  /// Return true if a ConstantDataSequential can be formed with a vector or
597  /// array of the specified element type.
598  /// ConstantDataArray only works with normal float and int types that are
599  /// stored densely in memory, not with things like i42 or x86_f80.
600  static bool isElementTypeCompatible(Type *Ty);
601 
602  /// If this is a sequential container of integers (of any size), return the
603  /// specified element in the low bits of a uint64_t.
604  uint64_t getElementAsInteger(unsigned i) const;
605 
606  /// If this is a sequential container of integers (of any size), return the
607  /// specified element as an APInt.
608  APInt getElementAsAPInt(unsigned i) const;
609 
610  /// If this is a sequential container of floating point type, return the
611  /// specified element as an APFloat.
612  APFloat getElementAsAPFloat(unsigned i) const;
613 
614  /// If this is an sequential container of floats, return the specified element
615  /// as a float.
616  float getElementAsFloat(unsigned i) const;
617 
618  /// If this is an sequential container of doubles, return the specified
619  /// element as a double.
620  double getElementAsDouble(unsigned i) const;
621 
622  /// Return a Constant for a specified index's element.
623  /// Note that this has to compute a new constant to return, so it isn't as
624  /// efficient as getElementAsInteger/Float/Double.
625  Constant *getElementAsConstant(unsigned i) const;
626 
627  /// Specialize the getType() method to always return a SequentialType, which
628  /// reduces the amount of casting needed in parts of the compiler.
629  inline SequentialType *getType() const {
630  return cast<SequentialType>(Value::getType());
631  }
632 
633  /// Return the element type of the array/vector.
634  Type *getElementType() const;
635 
636  /// Return the number of elements in the array or vector.
637  unsigned getNumElements() const;
638 
639  /// Return the size (in bytes) of each element in the array/vector.
640  /// The size of the elements is known to be a multiple of one byte.
641  uint64_t getElementByteSize() const;
642 
643  /// This method returns true if this is an array of \p CharSize integers.
644  bool isString(unsigned CharSize = 8) const;
645 
646  /// This method returns true if the array "isString", ends with a null byte,
647  /// and does not contains any other null bytes.
648  bool isCString() const;
649 
650  /// If this array is isString(), then this method returns the array as a
651  /// StringRef. Otherwise, it asserts out.
653  assert(isString() && "Not a string");
654  return getRawDataValues();
655  }
656 
657  /// If this array is isCString(), then this method returns the array (without
658  /// the trailing null byte) as a StringRef. Otherwise, it asserts out.
660  assert(isCString() && "Isn't a C string");
661  StringRef Str = getAsString();
662  return Str.substr(0, Str.size()-1);
663  }
664 
665  /// Return the raw, underlying, bytes of this data. Note that this is an
666  /// extremely tricky thing to work with, as it exposes the host endianness of
667  /// the data elements.
668  StringRef getRawDataValues() const;
669 
670  /// Methods for support type inquiry through isa, cast, and dyn_cast:
671  static bool classof(const Value *V) {
672  return V->getValueID() == ConstantDataArrayVal ||
673  V->getValueID() == ConstantDataVectorVal;
674  }
675 
676 private:
677  const char *getElementPointer(unsigned Elt) const;
678 };
679 
680 //===----------------------------------------------------------------------===//
681 /// An array constant whose element type is a simple 1/2/4/8-byte integer or
682 /// float/double, and whose elements are just simple data values
683 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
684 /// stores all of the elements of the constant as densely packed data, instead
685 /// of as Value*'s.
688 
689  explicit ConstantDataArray(Type *ty, const char *Data)
690  : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
691 
692 public:
693  ConstantDataArray(const ConstantDataArray &) = delete;
694 
695  /// get() constructors - Return a constant with array type with an element
696  /// count and element type matching the ArrayRef passed in. Note that this
697  /// can return a ConstantAggregateZero object.
698  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
699  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
700  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
701  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
702  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
703  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
704 
705  /// getFP() constructors - Return a constant with array type with an element
706  /// count and element type of float with precision matching the number of
707  /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
708  /// double for 64bits) Note that this can return a ConstantAggregateZero
709  /// object.
710  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
711  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
712  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
713 
714  /// This method constructs a CDS and initializes it with a text string.
715  /// The default behavior (AddNull==true) causes a null terminator to
716  /// be placed at the end of the array (increasing the length of the string by
717  /// one more than the StringRef would normally indicate. Pass AddNull=false
718  /// to disable this behavior.
719  static Constant *getString(LLVMContext &Context, StringRef Initializer,
720  bool AddNull = true);
721 
722  /// Specialize the getType() method to always return an ArrayType,
723  /// which reduces the amount of casting needed in parts of the compiler.
724  inline ArrayType *getType() const {
725  return cast<ArrayType>(Value::getType());
726  }
727 
728  /// Methods for support type inquiry through isa, cast, and dyn_cast:
729  static bool classof(const Value *V) {
730  return V->getValueID() == ConstantDataArrayVal;
731  }
732 };
733 
734 //===----------------------------------------------------------------------===//
735 /// A vector constant whose element type is a simple 1/2/4/8-byte integer or
736 /// float/double, and whose elements are just simple data values
737 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
738 /// stores all of the elements of the constant as densely packed data, instead
739 /// of as Value*'s.
742 
743  explicit ConstantDataVector(Type *ty, const char *Data)
744  : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
745 
746 public:
747  ConstantDataVector(const ConstantDataVector &) = delete;
748 
749  /// get() constructors - Return a constant with vector type with an element
750  /// count and element type matching the ArrayRef passed in. Note that this
751  /// can return a ConstantAggregateZero object.
752  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
753  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
754  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
755  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
756  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
757  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
758 
759  /// getFP() constructors - Return a constant with vector type with an element
760  /// count and element type of float with the precision matching the number of
761  /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
762  /// double for 64bits) Note that this can return a ConstantAggregateZero
763  /// object.
764  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
765  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
766  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
767 
768  /// Return a ConstantVector with the specified constant in each element.
769  /// The specified constant has to be a of a compatible type (i8/i16/
770  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
771  static Constant *getSplat(unsigned NumElts, Constant *Elt);
772 
773  /// Returns true if this is a splat constant, meaning that all elements have
774  /// the same value.
775  bool isSplat() const;
776 
777  /// If this is a splat constant, meaning that all of the elements have the
778  /// same value, return that value. Otherwise return NULL.
779  Constant *getSplatValue() const;
780 
781  /// Specialize the getType() method to always return a VectorType,
782  /// which reduces the amount of casting needed in parts of the compiler.
783  inline VectorType *getType() const {
784  return cast<VectorType>(Value::getType());
785  }
786 
787  /// Methods for support type inquiry through isa, cast, and dyn_cast:
788  static bool classof(const Value *V) {
789  return V->getValueID() == ConstantDataVectorVal;
790  }
791 };
792 
793 //===----------------------------------------------------------------------===//
794 /// A constant token which is empty
795 ///
796 class ConstantTokenNone final : public ConstantData {
797  friend class Constant;
798 
800  : ConstantData(Type::getTokenTy(Context), ConstantTokenNoneVal) {}
801 
802  void destroyConstantImpl();
803 
804 public:
805  ConstantTokenNone(const ConstantTokenNone &) = delete;
806 
807  /// Return the ConstantTokenNone.
808  static ConstantTokenNone *get(LLVMContext &Context);
809 
810  /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
811  static bool classof(const Value *V) {
812  return V->getValueID() == ConstantTokenNoneVal;
813  }
814 };
815 
816 /// The address of a basic block.
817 ///
818 class BlockAddress final : public Constant {
819  friend class Constant;
820 
822 
823  void *operator new(size_t s) { return User::operator new(s, 2); }
824 
825  void destroyConstantImpl();
826  Value *handleOperandChangeImpl(Value *From, Value *To);
827 
828 public:
829  /// Return a BlockAddress for the specified function and basic block.
830  static BlockAddress *get(Function *F, BasicBlock *BB);
831 
832  /// Return a BlockAddress for the specified basic block. The basic
833  /// block must be embedded into a function.
834  static BlockAddress *get(BasicBlock *BB);
835 
836  /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
837  ///
838  /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
839  static BlockAddress *lookup(const BasicBlock *BB);
840 
841  /// Transparently provide more efficient getOperand methods.
843 
844  Function *getFunction() const { return (Function*)Op<0>().get(); }
845  BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
846 
847  /// Methods for support type inquiry through isa, cast, and dyn_cast:
848  static bool classof(const Value *V) {
849  return V->getValueID() == BlockAddressVal;
850  }
851 };
852 
853 template <>
855  public FixedNumOperandTraits<BlockAddress, 2> {
856 };
857 
859 
860 //===----------------------------------------------------------------------===//
861 /// A constant value that is initialized with an expression using
862 /// other constant values.
863 ///
864 /// This class uses the standard Instruction opcodes to define the various
865 /// constant expressions. The Opcode field for the ConstantExpr class is
866 /// maintained in the Value::SubclassData field.
867 class ConstantExpr : public Constant {
868  friend struct ConstantExprKeyType;
869  friend class Constant;
870 
871  void destroyConstantImpl();
872  Value *handleOperandChangeImpl(Value *From, Value *To);
873 
874 protected:
875  ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
876  : Constant(ty, ConstantExprVal, Ops, NumOps) {
877  // Operation type (an Instruction opcode) is stored as the SubclassData.
878  setValueSubclassData(Opcode);
879  }
880 
881 public:
882  // Static methods to construct a ConstantExpr of different kinds. Note that
883  // these methods may return a object that is not an instance of the
884  // ConstantExpr class, because they will attempt to fold the constant
885  // expression into something simpler if possible.
886 
887  /// getAlignOf constant expr - computes the alignment of a type in a target
888  /// independent way (Note: the return type is an i64).
889  static Constant *getAlignOf(Type *Ty);
890 
891  /// getSizeOf constant expr - computes the (alloc) size of a type (in
892  /// address-units, not bits) in a target independent way (Note: the return
893  /// type is an i64).
894  ///
895  static Constant *getSizeOf(Type *Ty);
896 
897  /// getOffsetOf constant expr - computes the offset of a struct field in a
898  /// target independent way (Note: the return type is an i64).
899  ///
900  static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
901 
902  /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
903  /// which supports any aggregate type, and any Constant index.
904  ///
905  static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
906 
907  static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
908  static Constant *getFNeg(Constant *C);
909  static Constant *getNot(Constant *C);
910  static Constant *getAdd(Constant *C1, Constant *C2,
911  bool HasNUW = false, bool HasNSW = false);
912  static Constant *getFAdd(Constant *C1, Constant *C2);
913  static Constant *getSub(Constant *C1, Constant *C2,
914  bool HasNUW = false, bool HasNSW = false);
915  static Constant *getFSub(Constant *C1, Constant *C2);
916  static Constant *getMul(Constant *C1, Constant *C2,
917  bool HasNUW = false, bool HasNSW = false);
918  static Constant *getFMul(Constant *C1, Constant *C2);
919  static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
920  static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
921  static Constant *getFDiv(Constant *C1, Constant *C2);
922  static Constant *getURem(Constant *C1, Constant *C2);
923  static Constant *getSRem(Constant *C1, Constant *C2);
924  static Constant *getFRem(Constant *C1, Constant *C2);
925  static Constant *getAnd(Constant *C1, Constant *C2);
926  static Constant *getOr(Constant *C1, Constant *C2);
927  static Constant *getXor(Constant *C1, Constant *C2);
928  static Constant *getShl(Constant *C1, Constant *C2,
929  bool HasNUW = false, bool HasNSW = false);
930  static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
931  static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
932  static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
933  static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
934  static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
935  static Constant *getFPTrunc(Constant *C, Type *Ty,
936  bool OnlyIfReduced = false);
937  static Constant *getFPExtend(Constant *C, Type *Ty,
938  bool OnlyIfReduced = false);
939  static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
940  static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
941  static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
942  static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
943  static Constant *getPtrToInt(Constant *C, Type *Ty,
944  bool OnlyIfReduced = false);
945  static Constant *getIntToPtr(Constant *C, Type *Ty,
946  bool OnlyIfReduced = false);
947  static Constant *getBitCast(Constant *C, Type *Ty,
948  bool OnlyIfReduced = false);
949  static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
950  bool OnlyIfReduced = false);
951 
952  static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
953  static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
954 
955  static Constant *getNSWAdd(Constant *C1, Constant *C2) {
956  return getAdd(C1, C2, false, true);
957  }
958 
959  static Constant *getNUWAdd(Constant *C1, Constant *C2) {
960  return getAdd(C1, C2, true, false);
961  }
962 
963  static Constant *getNSWSub(Constant *C1, Constant *C2) {
964  return getSub(C1, C2, false, true);
965  }
966 
967  static Constant *getNUWSub(Constant *C1, Constant *C2) {
968  return getSub(C1, C2, true, false);
969  }
970 
971  static Constant *getNSWMul(Constant *C1, Constant *C2) {
972  return getMul(C1, C2, false, true);
973  }
974 
975  static Constant *getNUWMul(Constant *C1, Constant *C2) {
976  return getMul(C1, C2, true, false);
977  }
978 
979  static Constant *getNSWShl(Constant *C1, Constant *C2) {
980  return getShl(C1, C2, false, true);
981  }
982 
983  static Constant *getNUWShl(Constant *C1, Constant *C2) {
984  return getShl(C1, C2, true, false);
985  }
986 
988  return getSDiv(C1, C2, true);
989  }
990 
992  return getUDiv(C1, C2, true);
993  }
994 
996  return getAShr(C1, C2, true);
997  }
998 
1000  return getLShr(C1, C2, true);
1001  }
1002 
1003  /// Return the identity for the given binary operation,
1004  /// i.e. a constant C such that X op C = X and C op X = X for every X. It
1005  /// returns null if the operator doesn't have an identity.
1006  static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
1007 
1008  /// Return the absorbing element for the given binary
1009  /// operation, i.e. a constant C such that X op C = C and C op X = C for
1010  /// every X. For example, this returns zero for integer multiplication.
1011  /// It returns null if the operator doesn't have an absorbing element.
1012  static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
1013 
1014  /// Transparently provide more efficient getOperand methods.
1016 
1017  /// \brief Convenience function for getting a Cast operation.
1018  ///
1019  /// \param ops The opcode for the conversion
1020  /// \param C The constant to be converted
1021  /// \param Ty The type to which the constant is converted
1022  /// \param OnlyIfReduced see \a getWithOperands() docs.
1023  static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
1024  bool OnlyIfReduced = false);
1025 
1026  // @brief Create a ZExt or BitCast cast constant expression
1027  static Constant *getZExtOrBitCast(
1028  Constant *C, ///< The constant to zext or bitcast
1029  Type *Ty ///< The type to zext or bitcast C to
1030  );
1031 
1032  // @brief Create a SExt or BitCast cast constant expression
1033  static Constant *getSExtOrBitCast(
1034  Constant *C, ///< The constant to sext or bitcast
1035  Type *Ty ///< The type to sext or bitcast C to
1036  );
1037 
1038  // @brief Create a Trunc or BitCast cast constant expression
1039  static Constant *getTruncOrBitCast(
1040  Constant *C, ///< The constant to trunc or bitcast
1041  Type *Ty ///< The type to trunc or bitcast C to
1042  );
1043 
1044  /// @brief Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
1045  /// expression.
1046  static Constant *getPointerCast(
1047  Constant *C, ///< The pointer value to be casted (operand 0)
1048  Type *Ty ///< The type to which cast should be made
1049  );
1050 
1051  /// @brief Create a BitCast or AddrSpaceCast for a pointer type depending on
1052  /// the address space.
1053  static Constant *getPointerBitCastOrAddrSpaceCast(
1054  Constant *C, ///< The constant to addrspacecast or bitcast
1055  Type *Ty ///< The type to bitcast or addrspacecast C to
1056  );
1057 
1058  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
1059  static Constant *getIntegerCast(
1060  Constant *C, ///< The integer constant to be casted
1061  Type *Ty, ///< The integer type to cast to
1062  bool isSigned ///< Whether C should be treated as signed or not
1063  );
1064 
1065  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1066  static Constant *getFPCast(
1067  Constant *C, ///< The integer constant to be casted
1068  Type *Ty ///< The integer type to cast to
1069  );
1070 
1071  /// @brief Return true if this is a convert constant expression
1072  bool isCast() const;
1073 
1074  /// @brief Return true if this is a compare constant expression
1075  bool isCompare() const;
1076 
1077  /// @brief Return true if this is an insertvalue or extractvalue expression,
1078  /// and the getIndices() method may be used.
1079  bool hasIndices() const;
1080 
1081  /// @brief Return true if this is a getelementptr expression and all
1082  /// the index operands are compile-time known integers within the
1083  /// corresponding notional static array extents. Note that this is
1084  /// not equivalant to, a subset of, or a superset of the "inbounds"
1085  /// property.
1086  bool isGEPWithNoNotionalOverIndexing() const;
1087 
1088  /// Select constant expr
1089  ///
1090  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1091  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1092  Type *OnlyIfReducedTy = nullptr);
1093 
1094  /// get - Return a binary or shift operator constant expression,
1095  /// folding if possible.
1096  ///
1097  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1098  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1099  unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1100 
1101  /// \brief Return an ICmp or FCmp comparison operator constant expression.
1102  ///
1103  /// \param OnlyIfReduced see \a getWithOperands() docs.
1104  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1105  bool OnlyIfReduced = false);
1106 
1107  /// get* - Return some common constants without having to
1108  /// specify the full Instruction::OPCODE identifier.
1109  ///
1110  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1111  bool OnlyIfReduced = false);
1112  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1113  bool OnlyIfReduced = false);
1114 
1115  /// Getelementptr form. Value* is only accepted for convenience;
1116  /// all elements must be Constants.
1117  ///
1118  /// \param InRangeIndex the inrange index if present or None.
1119  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1121  ArrayRef<Constant *> IdxList,
1122  bool InBounds = false,
1123  Optional<unsigned> InRangeIndex = None,
1124  Type *OnlyIfReducedTy = nullptr) {
1125  return getGetElementPtr(
1126  Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1127  InBounds, InRangeIndex, OnlyIfReducedTy);
1128  }
1130  bool InBounds = false,
1131  Optional<unsigned> InRangeIndex = None,
1132  Type *OnlyIfReducedTy = nullptr) {
1133  // This form of the function only exists to avoid ambiguous overload
1134  // warnings about whether to convert Idx to ArrayRef<Constant *> or
1135  // ArrayRef<Value *>.
1136  return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, InRangeIndex,
1137  OnlyIfReducedTy);
1138  }
1139  static Constant *getGetElementPtr(Type *Ty, Constant *C,
1140  ArrayRef<Value *> IdxList,
1141  bool InBounds = false,
1142  Optional<unsigned> InRangeIndex = None,
1143  Type *OnlyIfReducedTy = nullptr);
1144 
1145  /// Create an "inbounds" getelementptr. See the documentation for the
1146  /// "inbounds" flag in LangRef.html for details.
1148  ArrayRef<Constant *> IdxList) {
1149  return getGetElementPtr(Ty, C, IdxList, true);
1150  }
1152  Constant *Idx) {
1153  // This form of the function only exists to avoid ambiguous overload
1154  // warnings about whether to convert Idx to ArrayRef<Constant *> or
1155  // ArrayRef<Value *>.
1156  return getGetElementPtr(Ty, C, Idx, true);
1157  }
1159  ArrayRef<Value *> IdxList) {
1160  return getGetElementPtr(Ty, C, IdxList, true);
1161  }
1162 
1163  static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1164  Type *OnlyIfReducedTy = nullptr);
1165  static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1166  Type *OnlyIfReducedTy = nullptr);
1167  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1168  Type *OnlyIfReducedTy = nullptr);
1169  static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1170  Type *OnlyIfReducedTy = nullptr);
1171  static Constant *getInsertValue(Constant *Agg, Constant *Val,
1172  ArrayRef<unsigned> Idxs,
1173  Type *OnlyIfReducedTy = nullptr);
1174 
1175  /// Return the opcode at the root of this constant expression
1176  unsigned getOpcode() const { return getSubclassDataFromValue(); }
1177 
1178  /// Return the ICMP or FCMP predicate value. Assert if this is not an ICMP or
1179  /// FCMP constant expression.
1180  unsigned getPredicate() const;
1181 
1182  /// Assert that this is an insertvalue or exactvalue
1183  /// expression and return the list of indices.
1184  ArrayRef<unsigned> getIndices() const;
1185 
1186  /// Return a string representation for an opcode.
1187  const char *getOpcodeName() const;
1188 
1189  /// Return a constant expression identical to this one, but with the specified
1190  /// operand set to the specified value.
1191  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1192 
1193  /// This returns the current constant expression with the operands replaced
1194  /// with the specified values. The specified array must have the same number
1195  /// of operands as our current one.
1197  return getWithOperands(Ops, getType());
1198  }
1199 
1200  /// Get the current expression with the operands replaced.
1201  ///
1202  /// Return the current constant expression with the operands replaced with \c
1203  /// Ops and the type with \c Ty. The new operands must have the same number
1204  /// as the current ones.
1205  ///
1206  /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1207  /// gets constant-folded, the type changes, or the expression is otherwise
1208  /// canonicalized. This parameter should almost always be \c false.
1209  Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1210  bool OnlyIfReduced = false,
1211  Type *SrcTy = nullptr) const;
1212 
1213  /// Returns an Instruction which implements the same operation as this
1214  /// ConstantExpr. The instruction is not linked to any basic block.
1215  ///
1216  /// A better approach to this could be to have a constructor for Instruction
1217  /// which would take a ConstantExpr parameter, but that would have spread
1218  /// implementation details of ConstantExpr outside of Constants.cpp, which
1219  /// would make it harder to remove ConstantExprs altogether.
1220  Instruction *getAsInstruction();
1221 
1222  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1223  static bool classof(const Value *V) {
1224  return V->getValueID() == ConstantExprVal;
1225  }
1226 
1227 private:
1228  // Shadow Value::setValueSubclassData with a private forwarding method so that
1229  // subclasses cannot accidentally use it.
1230  void setValueSubclassData(unsigned short D) {
1232  }
1233 };
1234 
1235 template <>
1237  public VariadicOperandTraits<ConstantExpr, 1> {
1238 };
1239 
1241 
1242 //===----------------------------------------------------------------------===//
1243 /// 'undef' values are things that do not have specified contents.
1244 /// These are used for a variety of purposes, including global variable
1245 /// initializers and operands to instructions. 'undef' values can occur with
1246 /// any first-class type.
1247 ///
1248 /// Undef values aren't exactly constants; if they have multiple uses, they
1249 /// can appear to have different bit patterns at each use. See
1250 /// LangRef.html#undefvalues for details.
1251 ///
1252 class UndefValue final : public ConstantData {
1253  friend class Constant;
1254 
1255  explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {}
1256 
1257  void destroyConstantImpl();
1258 
1259 public:
1260  UndefValue(const UndefValue &) = delete;
1261 
1262  /// Static factory methods - Return an 'undef' object of the specified type.
1263  static UndefValue *get(Type *T);
1264 
1265  /// If this Undef has array or vector type, return a undef with the right
1266  /// element type.
1267  UndefValue *getSequentialElement() const;
1268 
1269  /// If this undef has struct type, return a undef with the right element type
1270  /// for the specified element.
1271  UndefValue *getStructElement(unsigned Elt) const;
1272 
1273  /// Return an undef of the right value for the specified GEP index if we can,
1274  /// otherwise return null (e.g. if C is a ConstantExpr).
1275  UndefValue *getElementValue(Constant *C) const;
1276 
1277  /// Return an undef of the right value for the specified GEP index.
1278  UndefValue *getElementValue(unsigned Idx) const;
1279 
1280  /// Return the number of elements in the array, vector, or struct.
1281  unsigned getNumElements() const;
1282 
1283  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1284  static bool classof(const Value *V) {
1285  return V->getValueID() == UndefValueVal;
1286  }
1287 };
1288 
1289 } // end namespace llvm
1290 
1291 #endif // LLVM_IR_CONSTANTS_H
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:740
uint64_t CallInst * C
unsigned short getSubclassDataFromValue() const
Definition: Value.h:659
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:172
bool isZero() const
Definition: APFloat.h:1143
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1176
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1542
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:469
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static Constant * getNSWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:955
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1120
ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
Definition: Constants.h:587
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static Constant * getExactSDiv(Constant *C1, Constant *C2)
Definition: Constants.h:987
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:556
StringRef getAsCString() const
If this array is isCString(), then this method returns the array (without the trailing null byte) as ...
Definition: Constants.h:659
static Constant * getNUWShl(Constant *C1, Constant *C2)
Definition: Constants.h:983
F(f)
const fltSemantics & getSemantics() const
Definition: APFloat.h:1155
bool equalsInt(uint64_t V) const
A helper method that can be used to determine if the constant contained within is equal to a constant...
Definition: Constants.h:165
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:848
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true...
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:1284
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:143
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
Function * getFunction() const
Definition: Constants.h:844
The address of a basic block.
Definition: Constants.h:818
bool isExactlyValue(double V) const
Definition: Constants.h:321
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
&#39;undef&#39; values are things that do not have specified contents.
Definition: Constants.h:1252
Class to represent struct types.
Definition: DerivedTypes.h:201
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:488
static Constant * getNUWNeg(Constant *C)
Definition: Constants.h:953
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
Definition: Constants.h:242
static Constant * getExactAShr(Constant *C1, Constant *C2)
Definition: Constants.h:995
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:527
static const uint16_t * lookup(unsigned opcode, unsigned domain, ArrayRef< uint16_t[3]> Table)
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:729
This file implements a class to represent arbitrary precision integral constant values and operations...
All zero aggregate value.
Definition: Constants.h:337
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:201
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:867
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1554
bool isInfinity() const
Definition: APFloat.h:1144
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
ArrayType * getType() const
Specialize the getType() method to always return an ArrayType, which reduces the amount of casting ne...
Definition: Constants.h:724
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4441
static bool classof(const Value *V)
Methods to support type inquiry through isa, cast, and dyn_cast.
Definition: Constants.h:811
static Constant * getGetElementPtr(Type *Ty, Constant *C, Constant *Idx, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.h:1129
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:370
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:570
#define T
bool isNegative() const
Return true if the sign bit is set.
Definition: Constants.h:305
Class to represent array types.
Definition: DerivedTypes.h:369
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:788
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
BasicBlock * getBasicBlock() const
Definition: Constants.h:845
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:209
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
static Constant * getNSWNeg(Constant *C)
Definition: Constants.h:952
Class to represent pointers.
Definition: DerivedTypes.h:467
VectorType * getType() const
Specialize the getType() method to always return a VectorType, which reduces the amount of casting ne...
Definition: Constants.h:783
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:357
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:389
ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
Definition: Constants.h:875
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:149
bool isNegative() const
Definition: APFloat.h:1147
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
Definition: Constants.h:686
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
static Constant * getAnon(LLVMContext &Ctx, ArrayRef< Constant *> V, bool Packed=false)
Definition: Constants.h:468
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
A constant token which is empty.
Definition: Constants.h:796
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
bool isNaN() const
Definition: APFloat.h:1145
This is an important base class in LLVM.
Definition: Constant.h:42
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:264
bool isOneValue() const
Determine if this is a value of 1.
Definition: APInt.h:404
static Constant * getNSWShl(Constant *C1, Constant *C2)
Definition: Constants.h:979
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:436
This file declares a class to represent arbitrary precision floating point values and provide a varie...
bool isMaxValue(bool isSigned) const
This function will return true iff this constant represents the largest value that may be represented...
Definition: Constants.h:218
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:434
static bool classof(const Value *V)
Methods to support type inquiry through isa, cast, and dyn_cast.
Definition: Constants.h:256
Class to represent integer types.
Definition: DerivedTypes.h:40
Constant Vector Declarations.
Definition: Constants.h:496
#define DECLARE_TRANSPARENT_OPERAND_ACCESSORS(VALUECLASS)
Macro for generating in-class operand accessor declarations.
friend class Constant
Definition: Constants.h:59
Constant * getWithOperands(ArrayRef< Constant *> Ops) const
This returns the current constant expression with the operands replaced with the specified values...
Definition: Constants.h:1196
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx)
Definition: Constants.h:1151
Constant * getSplatValue() const
If this is a splat vector constant, meaning that all of the elements have the same value...
Definition: Constants.cpp:1322
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const T * data() const
Definition: ArrayRef.h:146
bool isNegative() const
Definition: Constants.h:188
hexagon gen pred
const APFloat & getValueAPF() const
Definition: Constants.h:299
bool isMinValue(bool isSigned) const
This function will return true iff this constant represents the smallest value that may be represente...
Definition: Constants.h:230
This is the superclass of the array and vector type classes.
Definition: DerivedTypes.h:343
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:329
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:85
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:251
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:420
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:652
A constant pointer value that points to null.
Definition: Constants.h:535
static Constant * getNUWMul(Constant *C1, Constant *C2)
Definition: Constants.h:975
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:414
static Constant * getNSWSub(Constant *C1, Constant *C2)
Definition: Constants.h:963
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:1223
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1272
ValueTy
Concrete subclass of this.
Definition: Value.h:450
void setValueSubclassData(unsigned short D)
Definition: Value.h:660
Class to represent vector types.
Definition: DerivedTypes.h:393
ConstantArray - Constant Array Declarations.
Definition: Constants.h:410
Class for arbitrary precision integers.
Definition: APInt.h:69
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false...
ArrayType * getType() const
Specialize the getType() method to always return an ArrayType, which reduces the amount of casting ne...
Definition: Constants.h:429
Common super class of ArrayType, StructType and VectorType.
Definition: DerivedTypes.h:162
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:395
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList)
Definition: Constants.h:1158
static Constant * getNSWMul(Constant *C1, Constant *C2)
Definition: Constants.h:971
SequentialType * getType() const
Specialize the getType() method to always return a SequentialType, which reduces the amount of castin...
Definition: Constants.h:629
xray Insert XRay ops
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
Definition: APInt.h:475
bool isZero() const
Return true if the value is positive or negative zero.
Definition: Constants.h:302
VectorType * getType() const
Specialize the getType() method to always return a VectorType, which reduces the amount of casting ne...
Definition: Constants.h:518
bool isNaN() const
Return true if the value is a NaN.
Definition: Constants.h:311
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1147
static Constant * getExactLShr(Constant *C1, Constant *C2)
Definition: Constants.h:999
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
Definition: Constants.h:551
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
bool isInfinity() const
Return true if the value is infinity.
Definition: Constants.h:308
Compile-time customization of User operands.
Definition: User.h:43
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:430
static bool classof(const Value *V)
Methods to support type inquiry through isa, cast, and dyn_cast.
Definition: Constants.h:74
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Base class for aggregate constants (with operands).
Definition: Constants.h:386
traits class for checking whether type T is a base class for all the given types in the variadic list...
Definition: STLExtras.h:705
LLVM Value Representation.
Definition: Value.h:73
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
static Constant * getAnon(ArrayRef< Constant *> V, bool Packed=false)
Return an anonymous struct that has the specified elements.
Definition: Constants.h:465
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:31
static bool isSplat(ArrayRef< Value *> VL)
ConstantData(Type *Ty, ValueTy VT)
Definition: Constants.h:66
Use & Op()
Definition: User.h:118
static Constant * getExactUDiv(Constant *C1, Constant *C2)
Definition: Constants.h:991
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:671
VariadicOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:69
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:157
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
static Constant * getNUWSub(Constant *C1, Constant *C2)
Definition: Constants.h:967
Base class for constants with no operands.
Definition: Constants.h:58
static Constant * getNUWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:959
StructType * getType() const
Specialization - reduce amount of casting.
Definition: Constants.h:483
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:399