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