1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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. Constant's are
16 /// created on demand as needed and never deleted: thus clients don't have to
17 /// worry about the lifetime of the objects.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CONSTANTS_H
22 #define LLVM_CONSTANTS_H
24 #include "llvm/Constant.h"
25 #include "llvm/OperandTraits.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/ADT/ArrayRef.h"
39 template<class ConstantClass, class TypeClass, class ValType>
40 struct ConstantCreator;
41 template<class ConstantClass, class TypeClass>
42 struct ConstantArrayCreator;
43 template<class ConstantClass, class TypeClass>
44 struct ConvertConstantType;
46 //===----------------------------------------------------------------------===//
47 /// This is the shared class of boolean and integer constants. This class
48 /// represents both boolean and integral constants.
49 /// @brief Class for constant integers.
50 class ConstantInt : public Constant {
51 virtual void anchor();
52 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
53 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
54 ConstantInt(IntegerType *Ty, const APInt& V);
57 // allocate space for exactly zero operands
58 void *operator new(size_t s) {
59 return User::operator new(s, 0);
62 static ConstantInt *getTrue(LLVMContext &Context);
63 static ConstantInt *getFalse(LLVMContext &Context);
64 static Constant *getTrue(Type *Ty);
65 static Constant *getFalse(Type *Ty);
67 /// If Ty is a vector type, return a Constant with a splat of the given
68 /// value. Otherwise return a ConstantInt for the given value.
69 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
71 /// Return a ConstantInt with the specified integer value for the specified
72 /// type. If the type is wider than 64 bits, the value will be zero-extended
73 /// to fit the type, unless isSigned is true, in which case the value will
74 /// be interpreted as a 64-bit signed integer and sign-extended to fit
76 /// @brief Get a ConstantInt for a specific value.
77 static ConstantInt *get(IntegerType *Ty, uint64_t V,
78 bool isSigned = false);
80 /// Return a ConstantInt with the specified value for the specified type. The
81 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
82 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
83 /// signed value for the type Ty.
84 /// @brief Get a ConstantInt for a specific signed value.
85 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
86 static Constant *getSigned(Type *Ty, int64_t V);
88 /// Return a ConstantInt with the specified value and an implied Type. The
89 /// type is the integer type that corresponds to the bit width of the value.
90 static ConstantInt *get(LLVMContext &Context, const APInt &V);
92 /// Return a ConstantInt constructed from the string strStart with the given
94 static ConstantInt *get(IntegerType *Ty, StringRef Str,
97 /// If Ty is a vector type, return a Constant with a splat of the given
98 /// value. Otherwise return a ConstantInt for the given value.
99 static Constant *get(Type* Ty, const APInt& V);
101 /// Return the constant as an APInt value reference. This allows clients to
102 /// obtain a copy of the value, with all its precision in tact.
103 /// @brief Return the constant's value.
104 inline const APInt &getValue() const {
108 /// getBitWidth - Return the bitwidth of this constant.
109 unsigned getBitWidth() const { return Val.getBitWidth(); }
111 /// Return the constant as a 64-bit unsigned integer value after it
112 /// has been zero extended as appropriate for the type of this constant. Note
113 /// that this method can assert if the value does not fit in 64 bits.
115 /// @brief Return the zero extended value.
116 inline uint64_t getZExtValue() const {
117 return Val.getZExtValue();
120 /// Return the constant as a 64-bit integer value after it has been sign
121 /// extended as appropriate for the type of this constant. Note that
122 /// 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();
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 {
137 /// getType - Specialize the getType() method to always return an IntegerType,
138 /// which reduces the amount of casting needed in parts of the compiler.
140 inline IntegerType *getType() const {
141 return reinterpret_cast<IntegerType*>(Value::getType());
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);
156 bool isNegative() const { return Val.isNegative(); }
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 {
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.
173 /// This function will return true iff every bit in this constant is set
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();
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
185 /// @brief Determine if the value is maximal.
186 bool isMaxValue(bool isSigned) const {
188 return Val.isMaxSignedValue();
190 return Val.isMaxValue();
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
197 /// @brief Determine if the value is minimal.
198 bool isMinValue(bool isSigned) const {
200 return Val.isMinSignedValue();
202 return Val.isMinValue();
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
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;
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);
223 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
224 static inline bool classof(const ConstantInt *) { return true; }
225 static bool classof(const Value *V) {
226 return V->getValueID() == ConstantIntVal;
231 //===----------------------------------------------------------------------===//
232 /// ConstantFP - Floating Point Values [float, double]
234 class ConstantFP : public Constant {
236 virtual void anchor();
237 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
238 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
239 friend class LLVMContextImpl;
241 ConstantFP(Type *Ty, const APFloat& V);
243 // allocate space for exactly zero operands
244 void *operator new(size_t s) {
245 return User::operator new(s, 0);
248 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
249 /// method returns the negative zero constant for floating point or vector
250 /// floating point types; for all other types, it returns the null value.
251 static Constant *getZeroValueForNegation(Type *Ty);
253 /// get() - This returns a ConstantFP, or a vector containing a splat of a
254 /// ConstantFP, for the specified value in the specified type. This should
255 /// only be used for simple constant values like 2.0/1.0 etc, that are
256 /// known-valid both as host double and as the target format.
257 static Constant *get(Type* Ty, double V);
258 static Constant *get(Type* Ty, StringRef Str);
259 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
260 static ConstantFP *getNegativeZero(Type* Ty);
261 static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
263 /// isValueValidForType - return true if Ty is big enough to represent V.
264 static bool isValueValidForType(Type *Ty, const APFloat &V);
265 inline const APFloat &getValueAPF() const { return Val; }
267 /// isZero - Return true if the value is positive or negative zero.
268 bool isZero() const { return Val.isZero(); }
270 /// isNegative - Return true if the sign bit is set.
271 bool isNegative() const { return Val.isNegative(); }
273 /// isNaN - Return true if the value is a NaN.
274 bool isNaN() const { return Val.isNaN(); }
276 /// isExactlyValue - We don't rely on operator== working on double values, as
277 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
278 /// As such, this method can be used to do an exact bit-for-bit comparison of
279 /// two floating point values. The version with a double operand is retained
280 /// because it's so convenient to write isExactlyValue(2.0), but please use
281 /// it only for simple constants.
282 bool isExactlyValue(const APFloat &V) const;
284 bool isExactlyValue(double V) const {
286 // convert is not supported on this type
287 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
290 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
291 return isExactlyValue(FV);
293 /// Methods for support type inquiry through isa, cast, and dyn_cast:
294 static inline bool classof(const ConstantFP *) { return true; }
295 static bool classof(const Value *V) {
296 return V->getValueID() == ConstantFPVal;
300 //===----------------------------------------------------------------------===//
301 /// ConstantAggregateZero - All zero aggregate value
303 class ConstantAggregateZero : public Constant {
304 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
305 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
307 explicit ConstantAggregateZero(Type *ty)
308 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
310 // allocate space for exactly zero operands
311 void *operator new(size_t s) {
312 return User::operator new(s, 0);
315 static ConstantAggregateZero *get(Type *Ty);
317 virtual void destroyConstant();
319 /// getSequentialElement - If this CAZ has array or vector type, return a zero
320 /// with the right element type.
321 Constant *getSequentialElement() const;
323 /// getStructElement - If this CAZ has struct type, return a zero with the
324 /// right element type for the specified element.
325 Constant *getStructElement(unsigned Elt) const;
327 /// getElementValue - Return a zero of the right value for the specified GEP
329 Constant *getElementValue(Constant *C) const;
331 /// getElementValue - Return a zero of the right value for the specified GEP
333 Constant *getElementValue(unsigned Idx) const;
335 /// Methods for support type inquiry through isa, cast, and dyn_cast:
337 static bool classof(const ConstantAggregateZero *) { return true; }
338 static bool classof(const Value *V) {
339 return V->getValueID() == ConstantAggregateZeroVal;
344 //===----------------------------------------------------------------------===//
345 /// ConstantArray - Constant Array Declarations
347 class ConstantArray : public Constant {
348 friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
349 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
351 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
353 // ConstantArray accessors
354 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
356 /// Transparently provide more efficient getOperand methods.
357 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
359 /// getType - Specialize the getType() method to always return an ArrayType,
360 /// which reduces the amount of casting needed in parts of the compiler.
362 inline ArrayType *getType() const {
363 return reinterpret_cast<ArrayType*>(Value::getType());
366 virtual void destroyConstant();
367 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
369 /// Methods for support type inquiry through isa, cast, and dyn_cast:
370 static inline bool classof(const ConstantArray *) { return true; }
371 static bool classof(const Value *V) {
372 return V->getValueID() == ConstantArrayVal;
377 struct OperandTraits<ConstantArray> :
378 public VariadicOperandTraits<ConstantArray> {
381 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
383 //===----------------------------------------------------------------------===//
384 // ConstantStruct - Constant Struct Declarations
386 class ConstantStruct : public Constant {
387 friend struct ConstantArrayCreator<ConstantStruct, StructType>;
388 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
390 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
392 // ConstantStruct accessors
393 static Constant *get(StructType *T, ArrayRef<Constant*> V);
394 static Constant *get(StructType *T, ...) END_WITH_NULL;
396 /// getAnon - Return an anonymous struct that has the specified
397 /// elements. If the struct is possibly empty, then you must specify a
399 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
400 return get(getTypeForElements(V, Packed), V);
402 static Constant *getAnon(LLVMContext &Ctx,
403 ArrayRef<Constant*> V, bool Packed = false) {
404 return get(getTypeForElements(Ctx, V, Packed), V);
407 /// getTypeForElements - Return an anonymous struct type to use for a constant
408 /// with the specified set of elements. The list must not be empty.
409 static StructType *getTypeForElements(ArrayRef<Constant*> V,
410 bool Packed = false);
411 /// getTypeForElements - This version of the method allows an empty list.
412 static StructType *getTypeForElements(LLVMContext &Ctx,
413 ArrayRef<Constant*> V,
414 bool Packed = false);
416 /// Transparently provide more efficient getOperand methods.
417 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
419 /// getType() specialization - Reduce amount of casting...
421 inline StructType *getType() const {
422 return reinterpret_cast<StructType*>(Value::getType());
425 virtual void destroyConstant();
426 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
428 /// Methods for support type inquiry through isa, cast, and dyn_cast:
429 static inline bool classof(const ConstantStruct *) { return true; }
430 static bool classof(const Value *V) {
431 return V->getValueID() == ConstantStructVal;
436 struct OperandTraits<ConstantStruct> :
437 public VariadicOperandTraits<ConstantStruct> {
440 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
443 //===----------------------------------------------------------------------===//
444 /// ConstantVector - Constant Vector Declarations
446 class ConstantVector : public Constant {
447 friend struct ConstantArrayCreator<ConstantVector, VectorType>;
448 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
450 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
452 // ConstantVector accessors
453 static Constant *get(ArrayRef<Constant*> V);
455 /// getSplat - Return a ConstantVector with the specified constant in each
457 static Constant *getSplat(unsigned NumElts, Constant *Elt);
459 /// Transparently provide more efficient getOperand methods.
460 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
462 /// getType - Specialize the getType() method to always return a VectorType,
463 /// which reduces the amount of casting needed in parts of the compiler.
465 inline VectorType *getType() const {
466 return reinterpret_cast<VectorType*>(Value::getType());
469 /// getSplatValue - If this is a splat constant, meaning that all of the
470 /// elements have the same value, return that value. Otherwise return NULL.
471 Constant *getSplatValue() const;
473 virtual void destroyConstant();
474 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
476 /// Methods for support type inquiry through isa, cast, and dyn_cast:
477 static inline bool classof(const ConstantVector *) { return true; }
478 static bool classof(const Value *V) {
479 return V->getValueID() == ConstantVectorVal;
484 struct OperandTraits<ConstantVector> :
485 public VariadicOperandTraits<ConstantVector> {
488 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
490 //===----------------------------------------------------------------------===//
491 /// ConstantPointerNull - a constant pointer value that points to null
493 class ConstantPointerNull : public Constant {
494 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
495 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
497 explicit ConstantPointerNull(PointerType *T)
498 : Constant(reinterpret_cast<Type*>(T),
499 Value::ConstantPointerNullVal, 0, 0) {}
502 // allocate space for exactly zero operands
503 void *operator new(size_t s) {
504 return User::operator new(s, 0);
507 /// get() - Static factory methods - Return objects of the specified value
508 static ConstantPointerNull *get(PointerType *T);
510 virtual void destroyConstant();
512 /// getType - Specialize the getType() method to always return an PointerType,
513 /// which reduces the amount of casting needed in parts of the compiler.
515 inline PointerType *getType() const {
516 return reinterpret_cast<PointerType*>(Value::getType());
519 /// Methods for support type inquiry through isa, cast, and dyn_cast:
520 static inline bool classof(const ConstantPointerNull *) { return true; }
521 static bool classof(const Value *V) {
522 return V->getValueID() == ConstantPointerNullVal;
526 //===----------------------------------------------------------------------===//
527 /// ConstantDataSequential - A vector or array constant whose element type is a
528 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
529 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
530 /// operands because it stores all of the elements of the constant as densely
531 /// packed data, instead of as Value*'s.
533 /// This is the common base class of ConstantDataArray and ConstantDataVector.
535 class ConstantDataSequential : public Constant {
536 friend class LLVMContextImpl;
537 /// DataElements - A pointer to the bytes underlying this constant (which is
538 /// owned by the uniquing StringMap).
539 const char *DataElements;
541 /// Next - This forms a link list of ConstantDataSequential nodes that have
542 /// the same value but different type. For example, 0,0,0,1 could be a 4
543 /// element array of i8, or a 1-element array of i32. They'll both end up in
544 /// the same StringMap bucket, linked up.
545 ConstantDataSequential *Next;
546 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
547 ConstantDataSequential(const ConstantDataSequential &); // DO NOT IMPLEMENT
549 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
550 : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
551 ~ConstantDataSequential() { delete Next; }
553 static Constant *getImpl(StringRef Bytes, Type *Ty);
556 // allocate space for exactly zero operands.
557 void *operator new(size_t s) {
558 return User::operator new(s, 0);
562 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
563 /// formed with a vector or array of the specified element type.
564 /// ConstantDataArray only works with normal float and int types that are
565 /// stored densely in memory, not with things like i42 or x86_f80.
566 static bool isElementTypeCompatible(const Type *Ty);
568 /// getElementAsInteger - If this is a sequential container of integers (of
569 /// any size), return the specified element in the low bits of a uint64_t.
570 uint64_t getElementAsInteger(unsigned i) const;
572 /// getElementAsAPFloat - If this is a sequential container of floating point
573 /// type, return the specified element as an APFloat.
574 APFloat getElementAsAPFloat(unsigned i) const;
576 /// getElementAsFloat - If this is an sequential container of floats, return
577 /// the specified element as a float.
578 float getElementAsFloat(unsigned i) const;
580 /// getElementAsDouble - If this is an sequential container of doubles, return
581 /// the specified element as a double.
582 double getElementAsDouble(unsigned i) const;
584 /// getElementAsConstant - Return a Constant for a specified index's element.
585 /// Note that this has to compute a new constant to return, so it isn't as
586 /// efficient as getElementAsInteger/Float/Double.
587 Constant *getElementAsConstant(unsigned i) const;
589 /// getType - Specialize the getType() method to always return a
590 /// SequentialType, which reduces the amount of casting needed in parts of the
592 inline SequentialType *getType() const {
593 return reinterpret_cast<SequentialType*>(Value::getType());
596 /// getElementType - Return the element type of the array/vector.
597 Type *getElementType() const;
599 /// getNumElements - Return the number of elements in the array or vector.
600 unsigned getNumElements() const;
602 /// getElementByteSize - Return the size (in bytes) of each element in the
603 /// array/vector. The size of the elements is known to be a multiple of one
605 uint64_t getElementByteSize() const;
608 /// isString - This method returns true if this is an array of i8.
609 bool isString() const;
611 /// isCString - This method returns true if the array "isString", ends with a
612 /// nul byte, and does not contains any other nul bytes.
613 bool isCString() const;
615 /// getAsString - If this array is isString(), then this method returns the
616 /// array as a StringRef. Otherwise, it asserts out.
618 StringRef getAsString() const {
619 assert(isString() && "Not a string");
620 return getRawDataValues();
623 /// getAsCString - If this array is isCString(), then this method returns the
624 /// array (without the trailing null byte) as a StringRef. Otherwise, it
627 StringRef getAsCString() const {
628 assert(isCString() && "Isn't a C string");
629 StringRef Str = getAsString();
630 return Str.substr(0, Str.size()-1);
633 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
634 /// that this is an extremely tricky thing to work with, as it exposes the
635 /// host endianness of the data elements.
636 StringRef getRawDataValues() const;
638 virtual void destroyConstant();
640 /// Methods for support type inquiry through isa, cast, and dyn_cast:
642 static bool classof(const ConstantDataSequential *) { return true; }
643 static bool classof(const Value *V) {
644 return V->getValueID() == ConstantDataArrayVal ||
645 V->getValueID() == ConstantDataVectorVal;
648 const char *getElementPointer(unsigned Elt) const;
651 //===----------------------------------------------------------------------===//
652 /// ConstantDataArray - An array constant whose element type is a simple
653 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
654 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
655 /// operands because it stores all of the elements of the constant as densely
656 /// packed data, instead of as Value*'s.
657 class ConstantDataArray : public ConstantDataSequential {
658 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
659 ConstantDataArray(const ConstantDataArray &); // DO NOT IMPLEMENT
660 virtual void anchor();
661 friend class ConstantDataSequential;
662 explicit ConstantDataArray(Type *ty, const char *Data)
663 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
665 // allocate space for exactly zero operands.
666 void *operator new(size_t s) {
667 return User::operator new(s, 0);
671 /// get() constructors - Return a constant with array type with an element
672 /// count and element type matching the ArrayRef passed in. Note that this
673 /// can return a ConstantAggregateZero object.
674 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
675 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
676 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
677 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
678 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
679 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
681 /// getString - This method constructs a CDS and initializes it with a text
682 /// string. The default behavior (AddNull==true) causes a null terminator to
683 /// be placed at the end of the array (increasing the length of the string by
684 /// one more than the StringRef would normally indicate. Pass AddNull=false
685 /// to disable this behavior.
686 static Constant *getString(LLVMContext &Context, StringRef Initializer,
687 bool AddNull = true);
689 /// getType - Specialize the getType() method to always return an ArrayType,
690 /// which reduces the amount of casting needed in parts of the compiler.
692 inline ArrayType *getType() const {
693 return reinterpret_cast<ArrayType*>(Value::getType());
696 /// Methods for support type inquiry through isa, cast, and dyn_cast:
698 static bool classof(const ConstantDataArray *) { return true; }
699 static bool classof(const Value *V) {
700 return V->getValueID() == ConstantDataArrayVal;
704 //===----------------------------------------------------------------------===//
705 /// ConstantDataVector - A vector constant whose element type is a simple
706 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
707 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
708 /// operands because it stores all of the elements of the constant as densely
709 /// packed data, instead of as Value*'s.
710 class ConstantDataVector : public ConstantDataSequential {
711 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
712 ConstantDataVector(const ConstantDataVector &); // DO NOT IMPLEMENT
713 virtual void anchor();
714 friend class ConstantDataSequential;
715 explicit ConstantDataVector(Type *ty, const char *Data)
716 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
718 // allocate space for exactly zero operands.
719 void *operator new(size_t s) {
720 return User::operator new(s, 0);
724 /// get() constructors - Return a constant with vector type with an element
725 /// count and element type matching the ArrayRef passed in. Note that this
726 /// can return a ConstantAggregateZero object.
727 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
728 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
729 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
730 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
731 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
732 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
734 /// getSplat - Return a ConstantVector with the specified constant in each
735 /// element. The specified constant has to be a of a compatible type (i8/i16/
736 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
737 static Constant *getSplat(unsigned NumElts, Constant *Elt);
739 /// getSplatValue - If this is a splat constant, meaning that all of the
740 /// elements have the same value, return that value. Otherwise return NULL.
741 Constant *getSplatValue() const;
743 /// getType - Specialize the getType() method to always return a VectorType,
744 /// which reduces the amount of casting needed in parts of the compiler.
746 inline VectorType *getType() const {
747 return reinterpret_cast<VectorType*>(Value::getType());
750 /// Methods for support type inquiry through isa, cast, and dyn_cast:
752 static bool classof(const ConstantDataVector *) { return true; }
753 static bool classof(const Value *V) {
754 return V->getValueID() == ConstantDataVectorVal;
760 /// BlockAddress - The address of a basic block.
762 class BlockAddress : public Constant {
763 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
764 void *operator new(size_t s) { return User::operator new(s, 2); }
765 BlockAddress(Function *F, BasicBlock *BB);
767 /// get - Return a BlockAddress for the specified function and basic block.
768 static BlockAddress *get(Function *F, BasicBlock *BB);
770 /// get - Return a BlockAddress for the specified basic block. The basic
771 /// block must be embedded into a function.
772 static BlockAddress *get(BasicBlock *BB);
774 /// Transparently provide more efficient getOperand methods.
775 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
777 Function *getFunction() const { return (Function*)Op<0>().get(); }
778 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
780 virtual void destroyConstant();
781 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
783 /// Methods for support type inquiry through isa, cast, and dyn_cast:
784 static inline bool classof(const BlockAddress *) { return true; }
785 static inline bool classof(const Value *V) {
786 return V->getValueID() == BlockAddressVal;
791 struct OperandTraits<BlockAddress> :
792 public FixedNumOperandTraits<BlockAddress, 2> {
795 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
798 //===----------------------------------------------------------------------===//
799 /// ConstantExpr - a constant value that is initialized with an expression using
800 /// other constant values.
802 /// This class uses the standard Instruction opcodes to define the various
803 /// constant expressions. The Opcode field for the ConstantExpr class is
804 /// maintained in the Value::SubclassData field.
805 class ConstantExpr : public Constant {
806 friend struct ConstantCreator<ConstantExpr,Type,
807 std::pair<unsigned, std::vector<Constant*> > >;
808 friend struct ConvertConstantType<ConstantExpr, Type>;
811 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
812 : Constant(ty, ConstantExprVal, Ops, NumOps) {
813 // Operation type (an Instruction opcode) is stored as the SubclassData.
814 setValueSubclassData(Opcode);
818 // Static methods to construct a ConstantExpr of different kinds. Note that
819 // these methods may return a object that is not an instance of the
820 // ConstantExpr class, because they will attempt to fold the constant
821 // expression into something simpler if possible.
823 /// getAlignOf constant expr - computes the alignment of a type in a target
824 /// independent way (Note: the return type is an i64).
825 static Constant *getAlignOf(Type *Ty);
827 /// getSizeOf constant expr - computes the (alloc) size of a type (in
828 /// address-units, not bits) in a target independent way (Note: the return
831 static Constant *getSizeOf(Type *Ty);
833 /// getOffsetOf constant expr - computes the offset of a struct field in a
834 /// target independent way (Note: the return type is an i64).
836 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
838 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
839 /// which supports any aggregate type, and any Constant index.
841 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
843 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
844 static Constant *getFNeg(Constant *C);
845 static Constant *getNot(Constant *C);
846 static Constant *getAdd(Constant *C1, Constant *C2,
847 bool HasNUW = false, bool HasNSW = false);
848 static Constant *getFAdd(Constant *C1, Constant *C2);
849 static Constant *getSub(Constant *C1, Constant *C2,
850 bool HasNUW = false, bool HasNSW = false);
851 static Constant *getFSub(Constant *C1, Constant *C2);
852 static Constant *getMul(Constant *C1, Constant *C2,
853 bool HasNUW = false, bool HasNSW = false);
854 static Constant *getFMul(Constant *C1, Constant *C2);
855 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
856 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
857 static Constant *getFDiv(Constant *C1, Constant *C2);
858 static Constant *getURem(Constant *C1, Constant *C2);
859 static Constant *getSRem(Constant *C1, Constant *C2);
860 static Constant *getFRem(Constant *C1, Constant *C2);
861 static Constant *getAnd(Constant *C1, Constant *C2);
862 static Constant *getOr(Constant *C1, Constant *C2);
863 static Constant *getXor(Constant *C1, Constant *C2);
864 static Constant *getShl(Constant *C1, Constant *C2,
865 bool HasNUW = false, bool HasNSW = false);
866 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
867 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
868 static Constant *getTrunc (Constant *C, Type *Ty);
869 static Constant *getSExt (Constant *C, Type *Ty);
870 static Constant *getZExt (Constant *C, Type *Ty);
871 static Constant *getFPTrunc (Constant *C, Type *Ty);
872 static Constant *getFPExtend(Constant *C, Type *Ty);
873 static Constant *getUIToFP (Constant *C, Type *Ty);
874 static Constant *getSIToFP (Constant *C, Type *Ty);
875 static Constant *getFPToUI (Constant *C, Type *Ty);
876 static Constant *getFPToSI (Constant *C, Type *Ty);
877 static Constant *getPtrToInt(Constant *C, Type *Ty);
878 static Constant *getIntToPtr(Constant *C, Type *Ty);
879 static Constant *getBitCast (Constant *C, Type *Ty);
881 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
882 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
883 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
884 return getAdd(C1, C2, false, true);
886 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
887 return getAdd(C1, C2, true, false);
889 static Constant *getNSWSub(Constant *C1, Constant *C2) {
890 return getSub(C1, C2, false, true);
892 static Constant *getNUWSub(Constant *C1, Constant *C2) {
893 return getSub(C1, C2, true, false);
895 static Constant *getNSWMul(Constant *C1, Constant *C2) {
896 return getMul(C1, C2, false, true);
898 static Constant *getNUWMul(Constant *C1, Constant *C2) {
899 return getMul(C1, C2, true, false);
901 static Constant *getNSWShl(Constant *C1, Constant *C2) {
902 return getShl(C1, C2, false, true);
904 static Constant *getNUWShl(Constant *C1, Constant *C2) {
905 return getShl(C1, C2, true, false);
907 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
908 return getSDiv(C1, C2, true);
910 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
911 return getUDiv(C1, C2, true);
913 static Constant *getExactAShr(Constant *C1, Constant *C2) {
914 return getAShr(C1, C2, true);
916 static Constant *getExactLShr(Constant *C1, Constant *C2) {
917 return getLShr(C1, C2, true);
920 /// getBinOpIdentity - Return the identity for the given binary operation,
921 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
922 /// returns null if the operator doesn't have an identity.
923 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
925 /// getBinOpAbsorber - Return the absorbing element for the given binary
926 /// operation, i.e. a constant C such that X op C = C and C op X = C for
927 /// every X. For example, this returns zero for integer multiplication.
928 /// It returns null if the operator doesn't have an absorbing element.
929 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
931 /// Transparently provide more efficient getOperand methods.
932 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
934 // @brief Convenience function for getting one of the casting operations
935 // using a CastOps opcode.
936 static Constant *getCast(
937 unsigned ops, ///< The opcode for the conversion
938 Constant *C, ///< The constant to be converted
939 Type *Ty ///< The type to which the constant is converted
942 // @brief Create a ZExt or BitCast cast constant expression
943 static Constant *getZExtOrBitCast(
944 Constant *C, ///< The constant to zext or bitcast
945 Type *Ty ///< The type to zext or bitcast C to
948 // @brief Create a SExt or BitCast cast constant expression
949 static Constant *getSExtOrBitCast(
950 Constant *C, ///< The constant to sext or bitcast
951 Type *Ty ///< The type to sext or bitcast C to
954 // @brief Create a Trunc or BitCast cast constant expression
955 static Constant *getTruncOrBitCast(
956 Constant *C, ///< The constant to trunc or bitcast
957 Type *Ty ///< The type to trunc or bitcast C to
960 /// @brief Create a BitCast or a PtrToInt cast constant expression
961 static Constant *getPointerCast(
962 Constant *C, ///< The pointer value to be casted (operand 0)
963 Type *Ty ///< The type to which cast should be made
966 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
967 static Constant *getIntegerCast(
968 Constant *C, ///< The integer constant to be casted
969 Type *Ty, ///< The integer type to cast to
970 bool isSigned ///< Whether C should be treated as signed or not
973 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
974 static Constant *getFPCast(
975 Constant *C, ///< The integer constant to be casted
976 Type *Ty ///< The integer type to cast to
979 /// @brief Return true if this is a convert constant expression
982 /// @brief Return true if this is a compare constant expression
983 bool isCompare() const;
985 /// @brief Return true if this is an insertvalue or extractvalue expression,
986 /// and the getIndices() method may be used.
987 bool hasIndices() const;
989 /// @brief Return true if this is a getelementptr expression and all
990 /// the index operands are compile-time known integers within the
991 /// corresponding notional static array extents. Note that this is
992 /// not equivalant to, a subset of, or a superset of the "inbounds"
994 bool isGEPWithNoNotionalOverIndexing() const;
996 /// Select constant expr
998 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
1000 /// get - Return a binary or shift operator constant expression,
1001 /// folding if possible.
1003 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1004 unsigned Flags = 0);
1006 /// @brief Return an ICmp or FCmp comparison operator constant expression.
1007 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
1009 /// get* - Return some common constants without having to
1010 /// specify the full Instruction::OPCODE identifier.
1012 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1013 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1015 /// Getelementptr form. Value* is only accepted for convenience;
1016 /// all elements must be Constant's.
1018 static Constant *getGetElementPtr(Constant *C,
1019 ArrayRef<Constant *> IdxList,
1020 bool InBounds = false) {
1021 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1025 static Constant *getGetElementPtr(Constant *C,
1027 bool InBounds = false) {
1028 // This form of the function only exists to avoid ambiguous overload
1029 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1030 // ArrayRef<Value *>.
1031 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1033 static Constant *getGetElementPtr(Constant *C,
1034 ArrayRef<Value *> IdxList,
1035 bool InBounds = false);
1037 /// Create an "inbounds" getelementptr. See the documentation for the
1038 /// "inbounds" flag in LangRef.html for details.
1039 static Constant *getInBoundsGetElementPtr(Constant *C,
1040 ArrayRef<Constant *> IdxList) {
1041 return getGetElementPtr(C, IdxList, true);
1043 static Constant *getInBoundsGetElementPtr(Constant *C,
1045 // This form of the function only exists to avoid ambiguous overload
1046 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1047 // ArrayRef<Value *>.
1048 return getGetElementPtr(C, Idx, true);
1050 static Constant *getInBoundsGetElementPtr(Constant *C,
1051 ArrayRef<Value *> IdxList) {
1052 return getGetElementPtr(C, IdxList, true);
1055 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1056 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1057 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1058 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1059 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1060 ArrayRef<unsigned> Idxs);
1062 /// getOpcode - Return the opcode at the root of this constant expression
1063 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1065 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1066 /// not an ICMP or FCMP constant expression.
1067 unsigned getPredicate() const;
1069 /// getIndices - Assert that this is an insertvalue or exactvalue
1070 /// expression and return the list of indices.
1071 ArrayRef<unsigned> getIndices() const;
1073 /// getOpcodeName - Return a string representation for an opcode.
1074 const char *getOpcodeName() const;
1076 /// getWithOperandReplaced - Return a constant expression identical to this
1077 /// one, but with the specified operand set to the specified value.
1078 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1080 /// getWithOperands - This returns the current constant expression with the
1081 /// operands replaced with the specified values. The specified array must
1082 /// have the same number of operands as our current one.
1083 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1084 return getWithOperands(Ops, getType());
1087 /// getWithOperands - This returns the current constant expression with the
1088 /// operands replaced with the specified values and with the specified result
1089 /// type. The specified array must have the same number of operands as our
1091 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1093 virtual void destroyConstant();
1094 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1096 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1097 static inline bool classof(const ConstantExpr *) { return true; }
1098 static inline bool classof(const Value *V) {
1099 return V->getValueID() == ConstantExprVal;
1103 // Shadow Value::setValueSubclassData with a private forwarding method so that
1104 // subclasses cannot accidentally use it.
1105 void setValueSubclassData(unsigned short D) {
1106 Value::setValueSubclassData(D);
1111 struct OperandTraits<ConstantExpr> :
1112 public VariadicOperandTraits<ConstantExpr, 1> {
1115 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1117 //===----------------------------------------------------------------------===//
1118 /// UndefValue - 'undef' values are things that do not have specified contents.
1119 /// These are used for a variety of purposes, including global variable
1120 /// initializers and operands to instructions. 'undef' values can occur with
1121 /// any first-class type.
1123 /// Undef values aren't exactly constants; if they have multiple uses, they
1124 /// can appear to have different bit patterns at each use. See
1125 /// LangRef.html#undefvalues for details.
1127 class UndefValue : public Constant {
1128 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1129 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
1131 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1133 // allocate space for exactly zero operands
1134 void *operator new(size_t s) {
1135 return User::operator new(s, 0);
1138 /// get() - Static factory methods - Return an 'undef' object of the specified
1141 static UndefValue *get(Type *T);
1143 /// getSequentialElement - If this Undef has array or vector type, return a
1144 /// undef with the right element type.
1145 UndefValue *getSequentialElement() const;
1147 /// getStructElement - If this undef has struct type, return a undef with the
1148 /// right element type for the specified element.
1149 UndefValue *getStructElement(unsigned Elt) const;
1151 /// getElementValue - Return an undef of the right value for the specified GEP
1153 UndefValue *getElementValue(Constant *C) const;
1155 /// getElementValue - Return an undef of the right value for the specified GEP
1157 UndefValue *getElementValue(unsigned Idx) const;
1159 virtual void destroyConstant();
1161 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1162 static inline bool classof(const UndefValue *) { return true; }
1163 static bool classof(const Value *V) {
1164 return V->getValueID() == UndefValueVal;
1168 } // End llvm namespace