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