• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// This file contains the declarations for the subclasses of Constant,
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are
13 /// fully shared by structural equivalence.  This means that two structurally
14 /// equivalent constants will always have the same address.  Constants are
15 /// created on demand as needed and never deleted: thus clients don't have to
16 /// worry about the lifetime of the objects.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_IR_CONSTANTS_H
21 #define LLVM_IR_CONSTANTS_H
22 
23 #include "llvm/ADT/APFloat.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/None.h"
27 #include "llvm/ADT/Optional.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/IR/Constant.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/OperandTraits.h"
33 #include "llvm/IR/User.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include <cassert>
39 #include <cstddef>
40 #include <cstdint>
41 
42 namespace llvm {
43 
44 class ArrayType;
45 class IntegerType;
46 class PointerType;
47 class SequentialType;
48 class StructType;
49 class VectorType;
50 template <class ConstantClass> struct ConstantAggrKeyType;
51 
52 /// Base class for constants with no operands.
53 ///
54 /// These constants have no operands; they represent their data directly.
55 /// Since they can be in use by unrelated modules (and are never based on
56 /// GlobalValues), it never makes sense to RAUW them.
57 class ConstantData : public Constant {
58   friend class Constant;
59 
handleOperandChangeImpl(Value * From,Value * To)60   Value *handleOperandChangeImpl(Value *From, Value *To) {
61     llvm_unreachable("Constant data does not have operands!");
62   }
63 
64 protected:
ConstantData(Type * Ty,ValueTy VT)65   explicit ConstantData(Type *Ty, ValueTy VT) : Constant(Ty, VT, nullptr, 0) {}
66 
new(size_t s)67   void *operator new(size_t s) { return User::operator new(s, 0); }
68 
69 public:
70   ConstantData(const ConstantData &) = delete;
71 
72   /// Methods to support type inquiry through isa, cast, and dyn_cast.
classof(const Value * V)73   static bool classof(const Value *V) {
74     return V->getValueID() >= ConstantDataFirstVal &&
75            V->getValueID() <= ConstantDataLastVal;
76   }
77 };
78 
79 //===----------------------------------------------------------------------===//
80 /// This is the shared class of boolean and integer constants. This class
81 /// represents both boolean and integral constants.
82 /// Class for constant integers.
83 class ConstantInt final : public ConstantData {
84   friend class Constant;
85 
86   APInt Val;
87 
88   ConstantInt(IntegerType *Ty, const APInt& V);
89 
90   void destroyConstantImpl();
91 
92 public:
93   ConstantInt(const ConstantInt &) = delete;
94 
95   static ConstantInt *getTrue(LLVMContext &Context);
96   static ConstantInt *getFalse(LLVMContext &Context);
97   static Constant *getTrue(Type *Ty);
98   static Constant *getFalse(Type *Ty);
99 
100   /// If Ty is a vector type, return a Constant with a splat of the given
101   /// value. Otherwise return a ConstantInt for the given value.
102   static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
103 
104   /// Return a ConstantInt with the specified integer value for the specified
105   /// type. If the type is wider than 64 bits, the value will be zero-extended
106   /// to fit the type, unless isSigned is true, in which case the value will
107   /// be interpreted as a 64-bit signed integer and sign-extended to fit
108   /// the type.
109   /// Get a ConstantInt for a specific value.
110   static ConstantInt *get(IntegerType *Ty, uint64_t V,
111                           bool isSigned = false);
112 
113   /// Return a ConstantInt with the specified value for the specified type. The
114   /// value V will be canonicalized to a an unsigned APInt. Accessing it with
115   /// either getSExtValue() or getZExtValue() will yield a correctly sized and
116   /// signed value for the type Ty.
117   /// Get a ConstantInt for a specific signed value.
118   static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
119   static Constant *getSigned(Type *Ty, int64_t V);
120 
121   /// Return a ConstantInt with the specified value and an implied Type. The
122   /// type is the integer type that corresponds to the bit width of the value.
123   static ConstantInt *get(LLVMContext &Context, const APInt &V);
124 
125   /// Return a ConstantInt constructed from the string strStart with the given
126   /// radix.
127   static ConstantInt *get(IntegerType *Ty, StringRef Str,
128                           uint8_t radix);
129 
130   /// If Ty is a vector type, return a Constant with a splat of the given
131   /// value. Otherwise return a ConstantInt for the given value.
132   static Constant *get(Type* Ty, const APInt& V);
133 
134   /// Return the constant as an APInt value reference. This allows clients to
135   /// obtain a full-precision copy of the value.
136   /// Return the constant's value.
getValue()137   inline const APInt &getValue() const {
138     return Val;
139   }
140 
141   /// getBitWidth - Return the bitwidth of this constant.
getBitWidth()142   unsigned getBitWidth() const { return Val.getBitWidth(); }
143 
144   /// Return the constant as a 64-bit unsigned integer value after it
145   /// has been zero extended as appropriate for the type of this constant. Note
146   /// that this method can assert if the value does not fit in 64 bits.
147   /// Return the zero extended value.
getZExtValue()148   inline uint64_t getZExtValue() const {
149     return Val.getZExtValue();
150   }
151 
152   /// Return the constant as a 64-bit integer value after it has been sign
153   /// extended as appropriate for the type of this constant. Note that
154   /// this method can assert if the value does not fit in 64 bits.
155   /// Return the sign extended value.
getSExtValue()156   inline int64_t getSExtValue() const {
157     return Val.getSExtValue();
158   }
159 
160   /// A helper method that can be used to determine if the constant contained
161   /// within is equal to a constant.  This only works for very small values,
162   /// because this is all that can be represented with all types.
163   /// Determine if this constant's value is same as an unsigned char.
equalsInt(uint64_t V)164   bool equalsInt(uint64_t V) const {
165     return Val == V;
166   }
167 
168   /// getType - Specialize the getType() method to always return an IntegerType,
169   /// which reduces the amount of casting needed in parts of the compiler.
170   ///
getType()171   inline IntegerType *getType() const {
172     return cast<IntegerType>(Value::getType());
173   }
174 
175   /// This static method returns true if the type Ty is big enough to
176   /// represent the value V. This can be used to avoid having the get method
177   /// assert when V is larger than Ty can represent. Note that there are two
178   /// versions of this method, one for unsigned and one for signed integers.
179   /// Although ConstantInt canonicalizes everything to an unsigned integer,
180   /// the signed version avoids callers having to convert a signed quantity
181   /// to the appropriate unsigned type before calling the method.
182   /// @returns true if V is a valid value for type Ty
183   /// Determine if the value is in range for the given type.
184   static bool isValueValidForType(Type *Ty, uint64_t V);
185   static bool isValueValidForType(Type *Ty, int64_t V);
186 
isNegative()187   bool isNegative() const { return Val.isNegative(); }
188 
189   /// This is just a convenience method to make client code smaller for a
190   /// common code. It also correctly performs the comparison without the
191   /// potential for an assertion from getZExtValue().
isZero()192   bool isZero() const {
193     return Val.isNullValue();
194   }
195 
196   /// This is just a convenience method to make client code smaller for a
197   /// common case. It also correctly performs the comparison without the
198   /// potential for an assertion from getZExtValue().
199   /// Determine if the value is one.
isOne()200   bool isOne() const {
201     return Val.isOneValue();
202   }
203 
204   /// This function will return true iff every bit in this constant is set
205   /// to true.
206   /// @returns true iff this constant's bits are all set to true.
207   /// Determine if the value is all ones.
isMinusOne()208   bool isMinusOne() const {
209     return Val.isAllOnesValue();
210   }
211 
212   /// This function will return true iff this constant represents the largest
213   /// value that may be represented by the constant's type.
214   /// @returns true iff this is the largest value that may be represented
215   /// by this type.
216   /// Determine if the value is maximal.
isMaxValue(bool isSigned)217   bool isMaxValue(bool isSigned) const {
218     if (isSigned)
219       return Val.isMaxSignedValue();
220     else
221       return Val.isMaxValue();
222   }
223 
224   /// This function will return true iff this constant represents the smallest
225   /// value that may be represented by this constant's type.
226   /// @returns true if this is the smallest value that may be represented by
227   /// this type.
228   /// Determine if the value is minimal.
isMinValue(bool isSigned)229   bool isMinValue(bool isSigned) const {
230     if (isSigned)
231       return Val.isMinSignedValue();
232     else
233       return Val.isMinValue();
234   }
235 
236   /// This function will return true iff this constant represents a value with
237   /// active bits bigger than 64 bits or a value greater than the given uint64_t
238   /// value.
239   /// @returns true iff this constant is greater or equal to the given number.
240   /// Determine if the value is greater or equal to the given number.
uge(uint64_t Num)241   bool uge(uint64_t Num) const {
242     return Val.uge(Num);
243   }
244 
245   /// getLimitedValue - If the value is smaller than the specified limit,
246   /// return it, otherwise return the limit value.  This causes the value
247   /// to saturate to the limit.
248   /// @returns the min of the value of the constant and the specified value
249   /// Get the constant's value with a saturation limit
250   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
251     return Val.getLimitedValue(Limit);
252   }
253 
254   /// Methods to support type inquiry through isa, cast, and dyn_cast.
classof(const Value * V)255   static bool classof(const Value *V) {
256     return V->getValueID() == ConstantIntVal;
257   }
258 };
259 
260 //===----------------------------------------------------------------------===//
261 /// ConstantFP - Floating Point Values [float, double]
262 ///
263 class ConstantFP final : public ConstantData {
264   friend class Constant;
265 
266   APFloat Val;
267 
268   ConstantFP(Type *Ty, const APFloat& V);
269 
270   void destroyConstantImpl();
271 
272 public:
273   ConstantFP(const ConstantFP &) = delete;
274 
275   /// Floating point negation must be implemented with f(x) = -0.0 - x. This
276   /// method returns the negative zero constant for floating point or vector
277   /// floating point types; for all other types, it returns the null value.
278   static Constant *getZeroValueForNegation(Type *Ty);
279 
280   /// This returns a ConstantFP, or a vector containing a splat of a ConstantFP,
281   /// for the specified value in the specified type. This should only be used
282   /// for simple constant values like 2.0/1.0 etc, that are known-valid both as
283   /// host double and as the target format.
284   static Constant *get(Type* Ty, double V);
285 
286   /// If Ty is a vector type, return a Constant with a splat of the given
287   /// value. Otherwise return a ConstantFP for the given value.
288   static Constant *get(Type *Ty, const APFloat &V);
289 
290   static Constant *get(Type* Ty, StringRef Str);
291   static ConstantFP *get(LLVMContext &Context, const APFloat &V);
292   static Constant *getNaN(Type *Ty, bool Negative = false, uint64_t Payload = 0);
293   static Constant *getQNaN(Type *Ty, bool Negative = false,
294                            APInt *Payload = nullptr);
295   static Constant *getSNaN(Type *Ty, bool Negative = false,
296                            APInt *Payload = nullptr);
297   static Constant *getNegativeZero(Type *Ty);
298   static Constant *getInfinity(Type *Ty, bool Negative = false);
299 
300   /// Return true if Ty is big enough to represent V.
301   static bool isValueValidForType(Type *Ty, const APFloat &V);
getValueAPF()302   inline const APFloat &getValueAPF() const { return Val; }
303 
304   /// Return true if the value is positive or negative zero.
isZero()305   bool isZero() const { return Val.isZero(); }
306 
307   /// Return true if the sign bit is set.
isNegative()308   bool isNegative() const { return Val.isNegative(); }
309 
310   /// Return true if the value is infinity
isInfinity()311   bool isInfinity() const { return Val.isInfinity(); }
312 
313   /// Return true if the value is a NaN.
isNaN()314   bool isNaN() const { return Val.isNaN(); }
315 
316   /// We don't rely on operator== working on double values, as it returns true
317   /// for things that are clearly not equal, like -0.0 and 0.0.
318   /// As such, this method can be used to do an exact bit-for-bit comparison of
319   /// two floating point values.  The version with a double operand is retained
320   /// because it's so convenient to write isExactlyValue(2.0), but please use
321   /// it only for simple constants.
322   bool isExactlyValue(const APFloat &V) const;
323 
isExactlyValue(double V)324   bool isExactlyValue(double V) const {
325     bool ignored;
326     APFloat FV(V);
327     FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
328     return isExactlyValue(FV);
329   }
330 
331   /// Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const Value * V)332   static bool classof(const Value *V) {
333     return V->getValueID() == ConstantFPVal;
334   }
335 };
336 
337 //===----------------------------------------------------------------------===//
338 /// All zero aggregate value
339 ///
340 class ConstantAggregateZero final : public ConstantData {
341   friend class Constant;
342 
ConstantAggregateZero(Type * Ty)343   explicit ConstantAggregateZero(Type *Ty)
344       : ConstantData(Ty, ConstantAggregateZeroVal) {}
345 
346   void destroyConstantImpl();
347 
348 public:
349   ConstantAggregateZero(const ConstantAggregateZero &) = delete;
350 
351   static ConstantAggregateZero *get(Type *Ty);
352 
353   /// If this CAZ has array or vector type, return a zero with the right element
354   /// type.
355   Constant *getSequentialElement() const;
356 
357   /// If this CAZ has struct type, return a zero with the right element type for
358   /// the specified element.
359   Constant *getStructElement(unsigned Elt) const;
360 
361   /// Return a zero of the right value for the specified GEP index if we can,
362   /// otherwise return null (e.g. if C is a ConstantExpr).
363   Constant *getElementValue(Constant *C) const;
364 
365   /// Return a zero of the right value for the specified GEP index.
366   Constant *getElementValue(unsigned Idx) const;
367 
368   /// Return the number of elements in the array, vector, or struct.
369   unsigned getNumElements() const;
370 
371   /// Methods for support type inquiry through isa, cast, and dyn_cast:
372   ///
classof(const Value * V)373   static bool classof(const Value *V) {
374     return V->getValueID() == ConstantAggregateZeroVal;
375   }
376 };
377 
378 /// Base class for aggregate constants (with operands).
379 ///
380 /// These constants are aggregates of other constants, which are stored as
381 /// operands.
382 ///
383 /// Subclasses are \a ConstantStruct, \a ConstantArray, and \a
384 /// ConstantVector.
385 ///
386 /// \note Some subclasses of \a ConstantData are semantically aggregates --
387 /// such as \a ConstantDataArray -- but are not subclasses of this because they
388 /// use operands.
389 class ConstantAggregate : public Constant {
390 protected:
391   ConstantAggregate(CompositeType *T, ValueTy VT, ArrayRef<Constant *> V);
392 
393 public:
394   /// Transparently provide more efficient getOperand methods.
395   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
396 
397   /// Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const Value * V)398   static bool classof(const Value *V) {
399     return V->getValueID() >= ConstantAggregateFirstVal &&
400            V->getValueID() <= ConstantAggregateLastVal;
401   }
402 };
403 
404 template <>
405 struct OperandTraits<ConstantAggregate>
406     : public VariadicOperandTraits<ConstantAggregate> {};
407 
408 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantAggregate, Constant)
409 
410 //===----------------------------------------------------------------------===//
411 /// ConstantArray - Constant Array Declarations
412 ///
413 class ConstantArray final : public ConstantAggregate {
414   friend struct ConstantAggrKeyType<ConstantArray>;
415   friend class Constant;
416 
417   ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
418 
419   void destroyConstantImpl();
420   Value *handleOperandChangeImpl(Value *From, Value *To);
421 
422 public:
423   // ConstantArray accessors
424   static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
425 
426 private:
427   static Constant *getImpl(ArrayType *T, ArrayRef<Constant *> V);
428 
429 public:
430   /// Specialize the getType() method to always return an ArrayType,
431   /// which reduces the amount of casting needed in parts of the compiler.
432   inline ArrayType *getType() const {
433     return cast<ArrayType>(Value::getType());
434   }
435 
436   /// Methods for support type inquiry through isa, cast, and dyn_cast:
437   static bool classof(const Value *V) {
438     return V->getValueID() == ConstantArrayVal;
439   }
440 };
441 
442 //===----------------------------------------------------------------------===//
443 // Constant Struct Declarations
444 //
445 class ConstantStruct final : public ConstantAggregate {
446   friend struct ConstantAggrKeyType<ConstantStruct>;
447   friend class Constant;
448 
449   ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
450 
451   void destroyConstantImpl();
452   Value *handleOperandChangeImpl(Value *From, Value *To);
453 
454 public:
455   // ConstantStruct accessors
456   static Constant *get(StructType *T, ArrayRef<Constant*> V);
457 
458   template <typename... Csts>
459   static typename std::enable_if<are_base_of<Constant, Csts...>::value,
460                                  Constant *>::type
461   get(StructType *T, Csts *... Vs) {
462     SmallVector<Constant *, 8> Values({Vs...});
463     return get(T, Values);
464   }
465 
466   /// Return an anonymous struct that has the specified elements.
467   /// If the struct is possibly empty, then you must specify a context.
468   static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
469     return get(getTypeForElements(V, Packed), V);
470   }
471   static Constant *getAnon(LLVMContext &Ctx,
472                            ArrayRef<Constant*> V, bool Packed = false) {
473     return get(getTypeForElements(Ctx, V, Packed), V);
474   }
475 
476   /// Return an anonymous struct type to use for a constant with the specified
477   /// set of elements. The list must not be empty.
478   static StructType *getTypeForElements(ArrayRef<Constant*> V,
479                                         bool Packed = false);
480   /// This version of the method allows an empty list.
481   static StructType *getTypeForElements(LLVMContext &Ctx,
482                                         ArrayRef<Constant*> V,
483                                         bool Packed = false);
484 
485   /// Specialization - reduce amount of casting.
486   inline StructType *getType() const {
487     return cast<StructType>(Value::getType());
488   }
489 
490   /// Methods for support type inquiry through isa, cast, and dyn_cast:
491   static bool classof(const Value *V) {
492     return V->getValueID() == ConstantStructVal;
493   }
494 };
495 
496 //===----------------------------------------------------------------------===//
497 /// Constant Vector Declarations
498 ///
499 class ConstantVector final : public ConstantAggregate {
500   friend struct ConstantAggrKeyType<ConstantVector>;
501   friend class Constant;
502 
503   ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
504 
505   void destroyConstantImpl();
506   Value *handleOperandChangeImpl(Value *From, Value *To);
507 
508 public:
509   // ConstantVector accessors
510   static Constant *get(ArrayRef<Constant*> V);
511 
512 private:
513   static Constant *getImpl(ArrayRef<Constant *> V);
514 
515 public:
516   /// Return a ConstantVector with the specified constant in each element.
517   static Constant *getSplat(unsigned NumElts, Constant *Elt);
518 
519   /// Specialize the getType() method to always return a VectorType,
520   /// which reduces the amount of casting needed in parts of the compiler.
521   inline VectorType *getType() const {
522     return cast<VectorType>(Value::getType());
523   }
524 
525   /// If all elements of the vector constant have the same value, return that
526   /// value. Otherwise, return nullptr. Ignore undefined elements by setting
527   /// AllowUndefs to true.
528   Constant *getSplatValue(bool AllowUndefs = false) 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 
542   explicit ConstantPointerNull(PointerType *T)
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 ///
574 class ConstantDataSequential : public ConstantData {
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.
586   ConstantDataSequential *Next;
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:
598   ConstantDataSequential(const ConstantDataSequential &) = delete;
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.
656   StringRef getAsString() const {
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.
663   StringRef getAsCString() const {
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.
690 class ConstantDataArray final : public ConstantDataSequential {
691   friend class ConstantDataSequential;
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>
703   static Constant *get(LLVMContext &Context, ArrayRef<ElementTy> Elts) {
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.
762 class ConstantDataVector final : public ConstantDataSequential {
763   friend class ConstantDataSequential;
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 
821   explicit ConstantTokenNone(LLVMContext &Context)
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 
843   BlockAddress(Function *F, BasicBlock *BB);
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.
864   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
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 <>
876 struct OperandTraits<BlockAddress> :
877   public FixedNumOperandTraits<BlockAddress, 2> {
878 };
879 
880 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
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 
1009   static Constant *getExactSDiv(Constant *C1, Constant *C2) {
1010     return getSDiv(C1, C2, true);
1011   }
1012 
1013   static Constant *getExactUDiv(Constant *C1, Constant *C2) {
1014     return getUDiv(C1, C2, true);
1015   }
1016 
1017   static Constant *getExactAShr(Constant *C1, Constant *C2) {
1018     return getAShr(C1, C2, true);
1019   }
1020 
1021   static Constant *getExactLShr(Constant *C1, Constant *C2) {
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.
1042   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
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.
1154   static Constant *getGetElementPtr(Type *Ty, Constant *C,
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   }
1163   static Constant *getGetElementPtr(Type *Ty, Constant *C, Constant *Idx,
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.
1181   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
1182                                             ArrayRef<Constant *> IdxList) {
1183     return getGetElementPtr(Ty, C, IdxList, true);
1184   }
1185   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
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   }
1192   static Constant *getInBoundsGetElementPtr(Type *Ty, Constant *C,
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.
1230   Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
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() const;
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) {
1265     Value::setValueSubclassData(D);
1266   }
1267 };
1268 
1269 template <>
1270 struct OperandTraits<ConstantExpr> :
1271   public VariadicOperandTraits<ConstantExpr, 1> {
1272 };
1273 
1274 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
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
1326