LLVM  8.0.1
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 /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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  /// 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, uint64_t Payload = 0);
294  static Constant *getQNaN(Type *Ty, bool Negative = false,
295  APInt *Payload = nullptr);
296  static Constant *getSNaN(Type *Ty, bool Negative = false,
297  APInt *Payload = nullptr);
298  static Constant *getNegativeZero(Type *Ty);
299  static Constant *getInfinity(Type *Ty, bool Negative = false);
300 
301  /// Return true if Ty is big enough to represent V.
302  static bool isValueValidForType(Type *Ty, const APFloat &V);
303  inline const APFloat &getValueAPF() const { return Val; }
304 
305  /// Return true if the value is positive or negative zero.
306  bool isZero() const { return Val.isZero(); }
307 
308  /// Return true if the sign bit is set.
309  bool isNegative() const { return Val.isNegative(); }
310 
311  /// Return true if the value is infinity
312  bool isInfinity() const { return Val.isInfinity(); }
313 
314  /// Return true if the value is a NaN.
315  bool isNaN() const { return Val.isNaN(); }
316 
317  /// We don't rely on operator== working on double values, as it returns true
318  /// for things that are clearly not equal, like -0.0 and 0.0.
319  /// As such, this method can be used to do an exact bit-for-bit comparison of
320  /// two floating point values. The version with a double operand is retained
321  /// because it's so convenient to write isExactlyValue(2.0), but please use
322  /// it only for simple constants.
323  bool isExactlyValue(const APFloat &V) const;
324 
325  bool isExactlyValue(double V) const {
326  bool ignored;
327  APFloat FV(V);
329  return isExactlyValue(FV);
330  }
331 
332  /// Methods for support type inquiry through isa, cast, and dyn_cast:
333  static bool classof(const Value *V) {
334  return V->getValueID() == ConstantFPVal;
335  }
336 };
337 
338 //===----------------------------------------------------------------------===//
339 /// All zero aggregate value
340 ///
341 class ConstantAggregateZero final : public ConstantData {
342  friend class Constant;
343 
344  explicit ConstantAggregateZero(Type *Ty)
345  : ConstantData(Ty, ConstantAggregateZeroVal) {}
346 
347  void destroyConstantImpl();
348 
349 public:
351 
352  static ConstantAggregateZero *get(Type *Ty);
353 
354  /// If this CAZ has array or vector type, return a zero with the right element
355  /// type.
356  Constant *getSequentialElement() const;
357 
358  /// If this CAZ has struct type, return a zero with the right element type for
359  /// the specified element.
360  Constant *getStructElement(unsigned Elt) const;
361 
362  /// Return a zero of the right value for the specified GEP index if we can,
363  /// otherwise return null (e.g. if C is a ConstantExpr).
364  Constant *getElementValue(Constant *C) const;
365 
366  /// Return a zero of the right value for the specified GEP index.
367  Constant *getElementValue(unsigned Idx) const;
368 
369  /// Return the number of elements in the array, vector, or struct.
370  unsigned getNumElements() const;
371 
372  /// Methods for support type inquiry through isa, cast, and dyn_cast:
373  ///
374  static bool classof(const Value *V) {
375  return V->getValueID() == ConstantAggregateZeroVal;
376  }
377 };
378 
379 /// Base class for aggregate constants (with operands).
380 ///
381 /// These constants are aggregates of other constants, which are stored as
382 /// operands.
383 ///
384 /// Subclasses are \a ConstantStruct, \a ConstantArray, and \a
385 /// ConstantVector.
386 ///
387 /// \note Some subclasses of \a ConstantData are semantically aggregates --
388 /// such as \a ConstantDataArray -- but are not subclasses of this because they
389 /// use operands.
390 class ConstantAggregate : public Constant {
391 protected:
393 
394 public:
395  /// Transparently provide more efficient getOperand methods.
397 
398  /// Methods for support type inquiry through isa, cast, and dyn_cast:
399  static bool classof(const Value *V) {
400  return V->getValueID() >= ConstantAggregateFirstVal &&
401  V->getValueID() <= ConstantAggregateLastVal;
402  }
403 };
404 
405 template <>
407  : public VariadicOperandTraits<ConstantAggregate> {};
408 
410 
411 //===----------------------------------------------------------------------===//
412 /// ConstantArray - Constant Array Declarations
413 ///
414 class ConstantArray final : public ConstantAggregate {
416  friend class Constant;
417 
419 
420  void destroyConstantImpl();
421  Value *handleOperandChangeImpl(Value *From, Value *To);
422 
423 public:
424  // ConstantArray accessors
425  static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
426 
427 private:
429 
430 public:
431  /// Specialize the getType() method to always return an ArrayType,
432  /// which reduces the amount of casting needed in parts of the compiler.
433  inline ArrayType *getType() const {
434  return cast<ArrayType>(Value::getType());
435  }
436 
437  /// Methods for support type inquiry through isa, cast, and dyn_cast:
438  static bool classof(const Value *V) {
439  return V->getValueID() == ConstantArrayVal;
440  }
441 };
442 
443 //===----------------------------------------------------------------------===//
444 // Constant Struct Declarations
445 //
446 class ConstantStruct final : public ConstantAggregate {
448  friend class Constant;
449 
451 
452  void destroyConstantImpl();
453  Value *handleOperandChangeImpl(Value *From, Value *To);
454 
455 public:
456  // ConstantStruct accessors
457  static Constant *get(StructType *T, ArrayRef<Constant*> V);
458 
459  template <typename... Csts>
460  static typename std::enable_if<are_base_of<Constant, Csts...>::value,
461  Constant *>::type
462  get(StructType *T, Csts *... Vs) {
463  SmallVector<Constant *, 8> Values({Vs...});
464  return get(T, Values);
465  }
466 
467  /// Return an anonymous struct that has the specified elements.
468  /// If the struct is possibly empty, then you must specify a context.
469  static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
470  return get(getTypeForElements(V, Packed), V);
471  }
472  static Constant *getAnon(LLVMContext &Ctx,
473  ArrayRef<Constant*> V, bool Packed = false) {
474  return get(getTypeForElements(Ctx, V, Packed), V);
475  }
476 
477  /// Return an anonymous struct type to use for a constant with the specified
478  /// set of elements. The list must not be empty.
479  static StructType *getTypeForElements(ArrayRef<Constant*> V,
480  bool Packed = false);
481  /// This version of the method allows an empty list.
482  static StructType *getTypeForElements(LLVMContext &Ctx,
484  bool Packed = false);
485 
486  /// Specialization - reduce amount of casting.
487  inline StructType *getType() const {
488  return cast<StructType>(Value::getType());
489  }
490 
491  /// Methods for support type inquiry through isa, cast, and dyn_cast:
492  static bool classof(const Value *V) {
493  return V->getValueID() == ConstantStructVal;
494  }
495 };
496 
497 //===----------------------------------------------------------------------===//
498 /// Constant Vector Declarations
499 ///
500 class ConstantVector final : public ConstantAggregate {
502  friend class Constant;
503 
505 
506  void destroyConstantImpl();
507  Value *handleOperandChangeImpl(Value *From, Value *To);
508 
509 public:
510  // ConstantVector accessors
511  static Constant *get(ArrayRef<Constant*> V);
512 
513 private:
515 
516 public:
517  /// Return a ConstantVector with the specified constant in each element.
518  static Constant *getSplat(unsigned NumElts, Constant *Elt);
519 
520  /// Specialize the getType() method to always return a VectorType,
521  /// which reduces the amount of casting needed in parts of the compiler.
522  inline VectorType *getType() const {
523  return cast<VectorType>(Value::getType());
524  }
525 
526  /// If this is a splat constant, meaning that all of the elements have the
527  /// same value, return that value. Otherwise return NULL.
528  Constant *getSplatValue() const;
529 
530  /// Methods for support type inquiry through isa, cast, and dyn_cast:
531  static bool classof(const Value *V) {
532  return V->getValueID() == ConstantVectorVal;
533  }
534 };
535 
536 //===----------------------------------------------------------------------===//
537 /// A constant pointer value that points to null
538 ///
539 class ConstantPointerNull final : public ConstantData {
540  friend class Constant;
541 
543  : ConstantData(T, Value::ConstantPointerNullVal) {}
544 
545  void destroyConstantImpl();
546 
547 public:
548  ConstantPointerNull(const ConstantPointerNull &) = delete;
549 
550  /// Static factory methods - Return objects of the specified value
551  static ConstantPointerNull *get(PointerType *T);
552 
553  /// Specialize the getType() method to always return an PointerType,
554  /// which reduces the amount of casting needed in parts of the compiler.
555  inline PointerType *getType() const {
556  return cast<PointerType>(Value::getType());
557  }
558 
559  /// Methods for support type inquiry through isa, cast, and dyn_cast:
560  static bool classof(const Value *V) {
561  return V->getValueID() == ConstantPointerNullVal;
562  }
563 };
564 
565 //===----------------------------------------------------------------------===//
566 /// ConstantDataSequential - A vector or array constant whose element type is a
567 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
568 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
569 /// operands because it stores all of the elements of the constant as densely
570 /// packed data, instead of as Value*'s.
571 ///
572 /// This is the common base class of ConstantDataArray and ConstantDataVector.
573 ///
575  friend class LLVMContextImpl;
576  friend class Constant;
577 
578  /// A pointer to the bytes underlying this constant (which is owned by the
579  /// uniquing StringMap).
580  const char *DataElements;
581 
582  /// This forms a link list of ConstantDataSequential nodes that have
583  /// the same value but different type. For example, 0,0,0,1 could be a 4
584  /// element array of i8, or a 1-element array of i32. They'll both end up in
585  /// the same StringMap bucket, linked up.
587 
588  void destroyConstantImpl();
589 
590 protected:
591  explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
592  : ConstantData(ty, VT), DataElements(Data), Next(nullptr) {}
593  ~ConstantDataSequential() { delete Next; }
594 
595  static Constant *getImpl(StringRef Bytes, Type *Ty);
596 
597 public:
599 
600  /// Return true if a ConstantDataSequential can be formed with a vector or
601  /// array of the specified element type.
602  /// ConstantDataArray only works with normal float and int types that are
603  /// stored densely in memory, not with things like i42 or x86_f80.
604  static bool isElementTypeCompatible(Type *Ty);
605 
606  /// If this is a sequential container of integers (of any size), return the
607  /// specified element in the low bits of a uint64_t.
608  uint64_t getElementAsInteger(unsigned i) const;
609 
610  /// If this is a sequential container of integers (of any size), return the
611  /// specified element as an APInt.
612  APInt getElementAsAPInt(unsigned i) const;
613 
614  /// If this is a sequential container of floating point type, return the
615  /// specified element as an APFloat.
616  APFloat getElementAsAPFloat(unsigned i) const;
617 
618  /// If this is an sequential container of floats, return the specified element
619  /// as a float.
620  float getElementAsFloat(unsigned i) const;
621 
622  /// If this is an sequential container of doubles, return the specified
623  /// element as a double.
624  double getElementAsDouble(unsigned i) const;
625 
626  /// Return a Constant for a specified index's element.
627  /// Note that this has to compute a new constant to return, so it isn't as
628  /// efficient as getElementAsInteger/Float/Double.
629  Constant *getElementAsConstant(unsigned i) const;
630 
631  /// Specialize the getType() method to always return a SequentialType, which
632  /// reduces the amount of casting needed in parts of the compiler.
633  inline SequentialType *getType() const {
634  return cast<SequentialType>(Value::getType());
635  }
636 
637  /// Return the element type of the array/vector.
638  Type *getElementType() const;
639 
640  /// Return the number of elements in the array or vector.
641  unsigned getNumElements() const;
642 
643  /// Return the size (in bytes) of each element in the array/vector.
644  /// The size of the elements is known to be a multiple of one byte.
645  uint64_t getElementByteSize() const;
646 
647  /// This method returns true if this is an array of \p CharSize integers.
648  bool isString(unsigned CharSize = 8) const;
649 
650  /// This method returns true if the array "isString", ends with a null byte,
651  /// and does not contains any other null bytes.
652  bool isCString() const;
653 
654  /// If this array is isString(), then this method returns the array as a
655  /// StringRef. Otherwise, it asserts out.
657  assert(isString() && "Not a string");
658  return getRawDataValues();
659  }
660 
661  /// If this array is isCString(), then this method returns the array (without
662  /// the trailing null byte) as a StringRef. Otherwise, it asserts out.
664  assert(isCString() && "Isn't a C string");
665  StringRef Str = getAsString();
666  return Str.substr(0, Str.size()-1);
667  }
668 
669  /// Return the raw, underlying, bytes of this data. Note that this is an
670  /// extremely tricky thing to work with, as it exposes the host endianness of
671  /// the data elements.
672  StringRef getRawDataValues() const;
673 
674  /// Methods for support type inquiry through isa, cast, and dyn_cast:
675  static bool classof(const Value *V) {
676  return V->getValueID() == ConstantDataArrayVal ||
677  V->getValueID() == ConstantDataVectorVal;
678  }
679 
680 private:
681  const char *getElementPointer(unsigned Elt) const;
682 };
683 
684 //===----------------------------------------------------------------------===//
685 /// An array constant whose element type is a simple 1/2/4/8-byte integer or
686 /// float/double, and whose elements are just simple data values
687 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
688 /// stores all of the elements of the constant as densely packed data, instead
689 /// of as Value*'s.
692 
693  explicit ConstantDataArray(Type *ty, const char *Data)
694  : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
695 
696 public:
697  ConstantDataArray(const ConstantDataArray &) = delete;
698 
699  /// get() constructor - Return a constant with array type with an element
700  /// count and element type matching the ArrayRef passed in. Note that this
701  /// can return a ConstantAggregateZero object.
702  template <typename ElementTy>
704  const char *Data = reinterpret_cast<const char *>(Elts.data());
705  return getRaw(StringRef(Data, Elts.size() * sizeof(ElementTy)), Elts.size(),
706  Type::getScalarTy<ElementTy>(Context));
707  }
708 
709  /// get() constructor - ArrayTy needs to be compatible with
710  /// ArrayRef<ElementTy>. Calls get(LLVMContext, ArrayRef<ElementTy>).
711  template <typename ArrayTy>
712  static Constant *get(LLVMContext &Context, ArrayTy &Elts) {
713  return ConstantDataArray::get(Context, makeArrayRef(Elts));
714  }
715 
716  /// get() constructor - Return a constant with array type with an element
717  /// count and element type matching the NumElements and ElementTy parameters
718  /// passed in. Note that this can return a ConstantAggregateZero object.
719  /// ElementTy needs to be one of i8/i16/i32/i64/float/double. Data is the
720  /// buffer containing the elements. Be careful to make sure Data uses the
721  /// right endianness, the buffer will be used as-is.
722  static Constant *getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy) {
723  Type *Ty = ArrayType::get(ElementTy, NumElements);
724  return getImpl(Data, Ty);
725  }
726 
727  /// getFP() constructors - Return a constant with array type with an element
728  /// count and element type of float with precision matching the number of
729  /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
730  /// double for 64bits) Note that this can return a ConstantAggregateZero
731  /// object.
732  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
733  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
734  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
735 
736  /// This method constructs a CDS and initializes it with a text string.
737  /// The default behavior (AddNull==true) causes a null terminator to
738  /// be placed at the end of the array (increasing the length of the string by
739  /// one more than the StringRef would normally indicate. Pass AddNull=false
740  /// to disable this behavior.
741  static Constant *getString(LLVMContext &Context, StringRef Initializer,
742  bool AddNull = true);
743 
744  /// Specialize the getType() method to always return an ArrayType,
745  /// which reduces the amount of casting needed in parts of the compiler.
746  inline ArrayType *getType() const {
747  return cast<ArrayType>(Value::getType());
748  }
749 
750  /// Methods for support type inquiry through isa, cast, and dyn_cast:
751  static bool classof(const Value *V) {
752  return V->getValueID() == ConstantDataArrayVal;
753  }
754 };
755 
756 //===----------------------------------------------------------------------===//
757 /// A vector constant whose element type is a simple 1/2/4/8-byte integer or
758 /// float/double, and whose elements are just simple data values
759 /// (i.e. ConstantInt/ConstantFP). This Constant node has no operands because it
760 /// stores all of the elements of the constant as densely packed data, instead
761 /// of as Value*'s.
764 
765  explicit ConstantDataVector(Type *ty, const char *Data)
766  : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
767 
768 public:
769  ConstantDataVector(const ConstantDataVector &) = delete;
770 
771  /// get() constructors - Return a constant with vector type with an element
772  /// count and element type matching the ArrayRef passed in. Note that this
773  /// can return a ConstantAggregateZero object.
774  static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
775  static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
776  static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
777  static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
778  static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
779  static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
780 
781  /// getFP() constructors - Return a constant with vector type with an element
782  /// count and element type of float with the precision matching the number of
783  /// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits,
784  /// double for 64bits) Note that this can return a ConstantAggregateZero
785  /// object.
786  static Constant *getFP(LLVMContext &Context, ArrayRef<uint16_t> Elts);
787  static Constant *getFP(LLVMContext &Context, ArrayRef<uint32_t> Elts);
788  static Constant *getFP(LLVMContext &Context, ArrayRef<uint64_t> Elts);
789 
790  /// Return a ConstantVector with the specified constant in each element.
791  /// The specified constant has to be a of a compatible type (i8/i16/
792  /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
793  static Constant *getSplat(unsigned NumElts, Constant *Elt);
794 
795  /// Returns true if this is a splat constant, meaning that all elements have
796  /// the same value.
797  bool isSplat() const;
798 
799  /// If this is a splat constant, meaning that all of the elements have the
800  /// same value, return that value. Otherwise return NULL.
801  Constant *getSplatValue() const;
802 
803  /// Specialize the getType() method to always return a VectorType,
804  /// which reduces the amount of casting needed in parts of the compiler.
805  inline VectorType *getType() const {
806  return cast<VectorType>(Value::getType());
807  }
808 
809  /// Methods for support type inquiry through isa, cast, and dyn_cast:
810  static bool classof(const Value *V) {
811  return V->getValueID() == ConstantDataVectorVal;
812  }
813 };
814 
815 //===----------------------------------------------------------------------===//
816 /// A constant token which is empty
817 ///
818 class ConstantTokenNone final : public ConstantData {
819  friend class Constant;
820 
822  : ConstantData(Type::getTokenTy(Context), ConstantTokenNoneVal) {}
823 
824  void destroyConstantImpl();
825 
826 public:
827  ConstantTokenNone(const ConstantTokenNone &) = delete;
828 
829  /// Return the ConstantTokenNone.
830  static ConstantTokenNone *get(LLVMContext &Context);
831 
832  /// Methods to support type inquiry through isa, cast, and dyn_cast.
833  static bool classof(const Value *V) {
834  return V->getValueID() == ConstantTokenNoneVal;
835  }
836 };
837 
838 /// The address of a basic block.
839 ///
840 class BlockAddress final : public Constant {
841  friend class Constant;
842 
844 
845  void *operator new(size_t s) { return User::operator new(s, 2); }
846 
847  void destroyConstantImpl();
848  Value *handleOperandChangeImpl(Value *From, Value *To);
849 
850 public:
851  /// Return a BlockAddress for the specified function and basic block.
852  static BlockAddress *get(Function *F, BasicBlock *BB);
853 
854  /// Return a BlockAddress for the specified basic block. The basic
855  /// block must be embedded into a function.
856  static BlockAddress *get(BasicBlock *BB);
857 
858  /// Lookup an existing \c BlockAddress constant for the given BasicBlock.
859  ///
860  /// \returns 0 if \c !BB->hasAddressTaken(), otherwise the \c BlockAddress.
861  static BlockAddress *lookup(const BasicBlock *BB);
862 
863  /// Transparently provide more efficient getOperand methods.
865 
866  Function *getFunction() const { return (Function*)Op<0>().get(); }
867  BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
868 
869  /// Methods for support type inquiry through isa, cast, and dyn_cast:
870  static bool classof(const Value *V) {
871  return V->getValueID() == BlockAddressVal;
872  }
873 };
874 
875 template <>
877  public FixedNumOperandTraits<BlockAddress, 2> {
878 };
879 
881 
882 //===----------------------------------------------------------------------===//
883 /// A constant value that is initialized with an expression using
884 /// other constant values.
885 ///
886 /// This class uses the standard Instruction opcodes to define the various
887 /// constant expressions. The Opcode field for the ConstantExpr class is
888 /// maintained in the Value::SubclassData field.
889 class ConstantExpr : public Constant {
890  friend struct ConstantExprKeyType;
891  friend class Constant;
892 
893  void destroyConstantImpl();
894  Value *handleOperandChangeImpl(Value *From, Value *To);
895 
896 protected:
897  ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
898  : Constant(ty, ConstantExprVal, Ops, NumOps) {
899  // Operation type (an Instruction opcode) is stored as the SubclassData.
900  setValueSubclassData(Opcode);
901  }
902 
903 public:
904  // Static methods to construct a ConstantExpr of different kinds. Note that
905  // these methods may return a object that is not an instance of the
906  // ConstantExpr class, because they will attempt to fold the constant
907  // expression into something simpler if possible.
908 
909  /// getAlignOf constant expr - computes the alignment of a type in a target
910  /// independent way (Note: the return type is an i64).
911  static Constant *getAlignOf(Type *Ty);
912 
913  /// getSizeOf constant expr - computes the (alloc) size of a type (in
914  /// address-units, not bits) in a target independent way (Note: the return
915  /// type is an i64).
916  ///
917  static Constant *getSizeOf(Type *Ty);
918 
919  /// getOffsetOf constant expr - computes the offset of a struct field in a
920  /// target independent way (Note: the return type is an i64).
921  ///
922  static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
923 
924  /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
925  /// which supports any aggregate type, and any Constant index.
926  ///
927  static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
928 
929  static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
930  static Constant *getFNeg(Constant *C);
931  static Constant *getNot(Constant *C);
932  static Constant *getAdd(Constant *C1, Constant *C2,
933  bool HasNUW = false, bool HasNSW = false);
934  static Constant *getFAdd(Constant *C1, Constant *C2);
935  static Constant *getSub(Constant *C1, Constant *C2,
936  bool HasNUW = false, bool HasNSW = false);
937  static Constant *getFSub(Constant *C1, Constant *C2);
938  static Constant *getMul(Constant *C1, Constant *C2,
939  bool HasNUW = false, bool HasNSW = false);
940  static Constant *getFMul(Constant *C1, Constant *C2);
941  static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
942  static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
943  static Constant *getFDiv(Constant *C1, Constant *C2);
944  static Constant *getURem(Constant *C1, Constant *C2);
945  static Constant *getSRem(Constant *C1, Constant *C2);
946  static Constant *getFRem(Constant *C1, Constant *C2);
947  static Constant *getAnd(Constant *C1, Constant *C2);
948  static Constant *getOr(Constant *C1, Constant *C2);
949  static Constant *getXor(Constant *C1, Constant *C2);
950  static Constant *getShl(Constant *C1, Constant *C2,
951  bool HasNUW = false, bool HasNSW = false);
952  static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
953  static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
954  static Constant *getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced = false);
955  static Constant *getSExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
956  static Constant *getZExt(Constant *C, Type *Ty, bool OnlyIfReduced = false);
957  static Constant *getFPTrunc(Constant *C, Type *Ty,
958  bool OnlyIfReduced = false);
959  static Constant *getFPExtend(Constant *C, Type *Ty,
960  bool OnlyIfReduced = false);
961  static Constant *getUIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
962  static Constant *getSIToFP(Constant *C, Type *Ty, bool OnlyIfReduced = false);
963  static Constant *getFPToUI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
964  static Constant *getFPToSI(Constant *C, Type *Ty, bool OnlyIfReduced = false);
965  static Constant *getPtrToInt(Constant *C, Type *Ty,
966  bool OnlyIfReduced = false);
967  static Constant *getIntToPtr(Constant *C, Type *Ty,
968  bool OnlyIfReduced = false);
969  static Constant *getBitCast(Constant *C, Type *Ty,
970  bool OnlyIfReduced = false);
971  static Constant *getAddrSpaceCast(Constant *C, Type *Ty,
972  bool OnlyIfReduced = false);
973 
974  static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
975  static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
976 
977  static Constant *getNSWAdd(Constant *C1, Constant *C2) {
978  return getAdd(C1, C2, false, true);
979  }
980 
981  static Constant *getNUWAdd(Constant *C1, Constant *C2) {
982  return getAdd(C1, C2, true, false);
983  }
984 
985  static Constant *getNSWSub(Constant *C1, Constant *C2) {
986  return getSub(C1, C2, false, true);
987  }
988 
989  static Constant *getNUWSub(Constant *C1, Constant *C2) {
990  return getSub(C1, C2, true, false);
991  }
992 
993  static Constant *getNSWMul(Constant *C1, Constant *C2) {
994  return getMul(C1, C2, false, true);
995  }
996 
997  static Constant *getNUWMul(Constant *C1, Constant *C2) {
998  return getMul(C1, C2, true, false);
999  }
1000 
1001  static Constant *getNSWShl(Constant *C1, Constant *C2) {
1002  return getShl(C1, C2, false, true);
1003  }
1004 
1005  static Constant *getNUWShl(Constant *C1, Constant *C2) {
1006  return getShl(C1, C2, true, false);
1007  }
1008 
1010  return getSDiv(C1, C2, true);
1011  }
1012 
1014  return getUDiv(C1, C2, true);
1015  }
1016 
1018  return getAShr(C1, C2, true);
1019  }
1020 
1022  return getLShr(C1, C2, true);
1023  }
1024 
1025  /// Return the identity constant for a binary opcode.
1026  /// The identity constant C is defined as X op C = X and C op X = X for every
1027  /// X when the binary operation is commutative. If the binop is not
1028  /// commutative, callers can acquire the operand 1 identity constant by
1029  /// setting AllowRHSConstant to true. For example, any shift has a zero
1030  /// identity constant for operand 1: X shift 0 = X.
1031  /// Return nullptr if the operator does not have an identity constant.
1032  static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty,
1033  bool AllowRHSConstant = false);
1034 
1035  /// Return the absorbing element for the given binary
1036  /// operation, i.e. a constant C such that X op C = C and C op X = C for
1037  /// every X. For example, this returns zero for integer multiplication.
1038  /// It returns null if the operator doesn't have an absorbing element.
1039  static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
1040 
1041  /// Transparently provide more efficient getOperand methods.
1043 
1044  /// Convenience function for getting a Cast operation.
1045  ///
1046  /// \param ops The opcode for the conversion
1047  /// \param C The constant to be converted
1048  /// \param Ty The type to which the constant is converted
1049  /// \param OnlyIfReduced see \a getWithOperands() docs.
1050  static Constant *getCast(unsigned ops, Constant *C, Type *Ty,
1051  bool OnlyIfReduced = false);
1052 
1053  // Create a ZExt or BitCast cast constant expression
1054  static Constant *getZExtOrBitCast(
1055  Constant *C, ///< The constant to zext or bitcast
1056  Type *Ty ///< The type to zext or bitcast C to
1057  );
1058 
1059  // Create a SExt or BitCast cast constant expression
1060  static Constant *getSExtOrBitCast(
1061  Constant *C, ///< The constant to sext or bitcast
1062  Type *Ty ///< The type to sext or bitcast C to
1063  );
1064 
1065  // Create a Trunc or BitCast cast constant expression
1066  static Constant *getTruncOrBitCast(
1067  Constant *C, ///< The constant to trunc or bitcast
1068  Type *Ty ///< The type to trunc or bitcast C to
1069  );
1070 
1071  /// Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant
1072  /// expression.
1073  static Constant *getPointerCast(
1074  Constant *C, ///< The pointer value to be casted (operand 0)
1075  Type *Ty ///< The type to which cast should be made
1076  );
1077 
1078  /// Create a BitCast or AddrSpaceCast for a pointer type depending on
1079  /// the address space.
1080  static Constant *getPointerBitCastOrAddrSpaceCast(
1081  Constant *C, ///< The constant to addrspacecast or bitcast
1082  Type *Ty ///< The type to bitcast or addrspacecast C to
1083  );
1084 
1085  /// Create a ZExt, Bitcast or Trunc for integer -> integer casts
1086  static Constant *getIntegerCast(
1087  Constant *C, ///< The integer constant to be casted
1088  Type *Ty, ///< The integer type to cast to
1089  bool isSigned ///< Whether C should be treated as signed or not
1090  );
1091 
1092  /// Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1093  static Constant *getFPCast(
1094  Constant *C, ///< The integer constant to be casted
1095  Type *Ty ///< The integer type to cast to
1096  );
1097 
1098  /// Return true if this is a convert constant expression
1099  bool isCast() const;
1100 
1101  /// Return true if this is a compare constant expression
1102  bool isCompare() const;
1103 
1104  /// Return true if this is an insertvalue or extractvalue expression,
1105  /// and the getIndices() method may be used.
1106  bool hasIndices() const;
1107 
1108  /// Return true if this is a getelementptr expression and all
1109  /// the index operands are compile-time known integers within the
1110  /// corresponding notional static array extents. Note that this is
1111  /// not equivalant to, a subset of, or a superset of the "inbounds"
1112  /// property.
1113  bool isGEPWithNoNotionalOverIndexing() const;
1114 
1115  /// Select constant expr
1116  ///
1117  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1118  static Constant *getSelect(Constant *C, Constant *V1, Constant *V2,
1119  Type *OnlyIfReducedTy = nullptr);
1120 
1121  /// get - Return a unary operator constant expression,
1122  /// folding if possible.
1123  ///
1124  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1125  static Constant *get(unsigned Opcode, Constant *C1, unsigned Flags = 0,
1126  Type *OnlyIfReducedTy = nullptr);
1127 
1128  /// get - Return a binary or shift operator constant expression,
1129  /// folding if possible.
1130  ///
1131  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1132  static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1133  unsigned Flags = 0, Type *OnlyIfReducedTy = nullptr);
1134 
1135  /// Return an ICmp or FCmp comparison operator constant expression.
1136  ///
1137  /// \param OnlyIfReduced see \a getWithOperands() docs.
1138  static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2,
1139  bool OnlyIfReduced = false);
1140 
1141  /// get* - Return some common constants without having to
1142  /// specify the full Instruction::OPCODE identifier.
1143  ///
1144  static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS,
1145  bool OnlyIfReduced = false);
1146  static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS,
1147  bool OnlyIfReduced = false);
1148 
1149  /// Getelementptr form. Value* is only accepted for convenience;
1150  /// all elements must be Constants.
1151  ///
1152  /// \param InRangeIndex the inrange index if present or None.
1153  /// \param OnlyIfReducedTy see \a getWithOperands() docs.
1155  ArrayRef<Constant *> IdxList,
1156  bool InBounds = false,
1157  Optional<unsigned> InRangeIndex = None,
1158  Type *OnlyIfReducedTy = nullptr) {
1159  return getGetElementPtr(
1160  Ty, C, makeArrayRef((Value * const *)IdxList.data(), IdxList.size()),
1161  InBounds, InRangeIndex, OnlyIfReducedTy);
1162  }
1164  bool InBounds = false,
1165  Optional<unsigned> InRangeIndex = None,
1166  Type *OnlyIfReducedTy = nullptr) {
1167  // This form of the function only exists to avoid ambiguous overload
1168  // warnings about whether to convert Idx to ArrayRef<Constant *> or
1169  // ArrayRef<Value *>.
1170  return getGetElementPtr(Ty, C, cast<Value>(Idx), InBounds, InRangeIndex,
1171  OnlyIfReducedTy);
1172  }
1173  static Constant *getGetElementPtr(Type *Ty, Constant *C,
1174  ArrayRef<Value *> IdxList,
1175  bool InBounds = false,
1176  Optional<unsigned> InRangeIndex = None,
1177  Type *OnlyIfReducedTy = nullptr);
1178 
1179  /// Create an "inbounds" getelementptr. See the documentation for the
1180  /// "inbounds" flag in LangRef.html for details.
1182  ArrayRef<Constant *> IdxList) {
1183  return getGetElementPtr(Ty, C, IdxList, true);
1184  }
1186  Constant *Idx) {
1187  // This form of the function only exists to avoid ambiguous overload
1188  // warnings about whether to convert Idx to ArrayRef<Constant *> or
1189  // ArrayRef<Value *>.
1190  return getGetElementPtr(Ty, C, Idx, true);
1191  }
1193  ArrayRef<Value *> IdxList) {
1194  return getGetElementPtr(Ty, C, IdxList, true);
1195  }
1196 
1197  static Constant *getExtractElement(Constant *Vec, Constant *Idx,
1198  Type *OnlyIfReducedTy = nullptr);
1199  static Constant *getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx,
1200  Type *OnlyIfReducedTy = nullptr);
1201  static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask,
1202  Type *OnlyIfReducedTy = nullptr);
1203  static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs,
1204  Type *OnlyIfReducedTy = nullptr);
1205  static Constant *getInsertValue(Constant *Agg, Constant *Val,
1206  ArrayRef<unsigned> Idxs,
1207  Type *OnlyIfReducedTy = nullptr);
1208 
1209  /// Return the opcode at the root of this constant expression
1210  unsigned getOpcode() const { return getSubclassDataFromValue(); }
1211 
1212  /// Return the ICMP or FCMP predicate value. Assert if this is not an ICMP or
1213  /// FCMP constant expression.
1214  unsigned getPredicate() const;
1215 
1216  /// Assert that this is an insertvalue or exactvalue
1217  /// expression and return the list of indices.
1218  ArrayRef<unsigned> getIndices() const;
1219 
1220  /// Return a string representation for an opcode.
1221  const char *getOpcodeName() const;
1222 
1223  /// Return a constant expression identical to this one, but with the specified
1224  /// operand set to the specified value.
1225  Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1226 
1227  /// This returns the current constant expression with the operands replaced
1228  /// with the specified values. The specified array must have the same number
1229  /// of operands as our current one.
1231  return getWithOperands(Ops, getType());
1232  }
1233 
1234  /// Get the current expression with the operands replaced.
1235  ///
1236  /// Return the current constant expression with the operands replaced with \c
1237  /// Ops and the type with \c Ty. The new operands must have the same number
1238  /// as the current ones.
1239  ///
1240  /// If \c OnlyIfReduced is \c true, nullptr will be returned unless something
1241  /// gets constant-folded, the type changes, or the expression is otherwise
1242  /// canonicalized. This parameter should almost always be \c false.
1243  Constant *getWithOperands(ArrayRef<Constant *> Ops, Type *Ty,
1244  bool OnlyIfReduced = false,
1245  Type *SrcTy = nullptr) const;
1246 
1247  /// Returns an Instruction which implements the same operation as this
1248  /// ConstantExpr. The instruction is not linked to any basic block.
1249  ///
1250  /// A better approach to this could be to have a constructor for Instruction
1251  /// which would take a ConstantExpr parameter, but that would have spread
1252  /// implementation details of ConstantExpr outside of Constants.cpp, which
1253  /// would make it harder to remove ConstantExprs altogether.
1254  Instruction *getAsInstruction();
1255 
1256  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1257  static bool classof(const Value *V) {
1258  return V->getValueID() == ConstantExprVal;
1259  }
1260 
1261 private:
1262  // Shadow Value::setValueSubclassData with a private forwarding method so that
1263  // subclasses cannot accidentally use it.
1264  void setValueSubclassData(unsigned short D) {
1266  }
1267 };
1268 
1269 template <>
1271  public VariadicOperandTraits<ConstantExpr, 1> {
1272 };
1273 
1275 
1276 //===----------------------------------------------------------------------===//
1277 /// 'undef' values are things that do not have specified contents.
1278 /// These are used for a variety of purposes, including global variable
1279 /// initializers and operands to instructions. 'undef' values can occur with
1280 /// any first-class type.
1281 ///
1282 /// Undef values aren't exactly constants; if they have multiple uses, they
1283 /// can appear to have different bit patterns at each use. See
1284 /// LangRef.html#undefvalues for details.
1285 ///
1286 class UndefValue final : public ConstantData {
1287  friend class Constant;
1288 
1289  explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {}
1290 
1291  void destroyConstantImpl();
1292 
1293 public:
1294  UndefValue(const UndefValue &) = delete;
1295 
1296  /// Static factory methods - Return an 'undef' object of the specified type.
1297  static UndefValue *get(Type *T);
1298 
1299  /// If this Undef has array or vector type, return a undef with the right
1300  /// element type.
1301  UndefValue *getSequentialElement() const;
1302 
1303  /// If this undef has struct type, return a undef with the right element type
1304  /// for the specified element.
1305  UndefValue *getStructElement(unsigned Elt) const;
1306 
1307  /// Return an undef of the right value for the specified GEP index if we can,
1308  /// otherwise return null (e.g. if C is a ConstantExpr).
1309  UndefValue *getElementValue(Constant *C) const;
1310 
1311  /// Return an undef of the right value for the specified GEP index.
1312  UndefValue *getElementValue(unsigned Idx) const;
1313 
1314  /// Return the number of elements in the array, vector, or struct.
1315  unsigned getNumElements() const;
1316 
1317  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1318  static bool classof(const Value *V) {
1319  return V->getValueID() == UndefValueVal;
1320  }
1321 };
1322 
1323 } // end namespace llvm
1324 
1325 #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:762
uint64_t CallInst * C
unsigned short getSubclassDataFromValue() const
Definition: Value.h:655
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:1210
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1563
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:464
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static Constant * getNSWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:977
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:1154
ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
Definition: Constants.h:591
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:1009
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:560
StringRef getAsCString() const
If this array is isCString(), then this method returns the array (without the trailing null byte) as ...
Definition: Constants.h:663
static Constant * getNUWShl(Constant *C1, Constant *C2)
Definition: Constants.h:1005
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 Constant * getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:722
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:870
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:1318
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:1509
Function * getFunction() const
Definition: Constants.h:866
The address of a basic block.
Definition: Constants.h:840
bool isExactlyValue(double V) const
Definition: Constants.h:325
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:1286
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:492
static Constant * getNUWNeg(Constant *C)
Definition: Constants.h:975
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:1017
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:531
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:751
This file implements a class to represent arbitrary precision integral constant values and operations...
All zero aggregate value.
Definition: Constants.h:341
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:889
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1575
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:746
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:4444
static bool classof(const Value *V)
Methods to support type inquiry through isa, cast, and dyn_cast.
Definition: Constants.h:833
static Constant * getGetElementPtr(Type *Ty, Constant *C, Constant *Idx, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.h:1163
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:374
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:574
#define T
bool isNegative() const
Return true if the sign bit is set.
Definition: Constants.h:309
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:810
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:867
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:974
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:805
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:364
bool isAllOnesValue() const
Determine if all bits are set.
Definition: APInt.h:396
ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
Definition: Constants.h:897
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:690
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
static Constant * getAnon(LLVMContext &Ctx, ArrayRef< Constant *> V, bool Packed=false)
Definition: Constants.h:472
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:818
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:411
static Constant * getNSWShl(Constant *C1, Constant *C2)
Definition: Constants.h:1001
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:443
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:438
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:500
#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:1230
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx)
Definition: Constants.h:1185
Constant * getSplatValue() const
If this is a splat vector constant, meaning that all of the elements have the same value...
Definition: Constants.cpp:1368
#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:303
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:333
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:88
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:427
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
BlockVerifier::State From
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:656
A constant pointer value that points to null.
Definition: Constants.h:539
static Constant * getNUWMul(Constant *C1, Constant *C2)
Definition: Constants.h:997
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition: APInt.h:421
static Constant * getNSWSub(Constant *C1, Constant *C2)
Definition: Constants.h:985
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:1257
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1293
ValueTy
Concrete subclass of this.
Definition: Value.h:445
void setValueSubclassData(unsigned short D)
Definition: Value.h:656
Class to represent vector types.
Definition: DerivedTypes.h:393
ConstantArray - Constant Array Declarations.
Definition: Constants.h:414
Class for arbitrary precision integers.
Definition: APInt.h:70
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:433
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:399
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList)
Definition: Constants.h:1192
static Constant * getNSWMul(Constant *C1, Constant *C2)
Definition: Constants.h:993
SequentialType * getType() const
Specialize the getType() method to always return a SequentialType, which reduces the amount of castin...
Definition: Constants.h:633
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:482
bool isZero() const
Return true if the value is positive or negative zero.
Definition: Constants.h:306
VectorType * getType() const
Specialize the getType() method to always return a VectorType, which reduces the amount of casting ne...
Definition: Constants.h:522
bool isNaN() const
Return true if the value is a NaN.
Definition: Constants.h:315
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1181
static Constant * getExactLShr(Constant *C1, Constant *C2)
Definition: Constants.h:1021
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
Definition: Constants.h:555
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:312
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:581
Compile-time customization of User operands.
Definition: User.h:43
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition: APInt.h:437
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:390
traits class for checking whether type T is a base class for all the given types in the variadic list...
Definition: STLExtras.h:1028
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:469
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:703
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:134
static Constant * getExactUDiv(Constant *C1, Constant *C2)
Definition: Constants.h:1013
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Constants.h:675
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:989
Base class for constants with no operands.
Definition: Constants.h:58
static Constant * getNUWAdd(Constant *C1, Constant *C2)
Definition: Constants.h:981
StructType * getType() const
Specialization - reduce amount of casting.
Definition: Constants.h:487
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:406