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