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_IR_CONSTANTS_H
22 #define LLVM_IR_CONSTANTS_H
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/OperandTraits.h"
29 #include "llvm/IR/DerivedTypes.h"
40 template<class ConstantClass, class TypeClass, class ValType>
41 struct ConstantCreator;
42 template<class ConstantClass, class TypeClass>
43 struct ConstantArrayCreator;
44 template<class ConstantClass, class TypeClass>
45 struct ConvertConstantType;
47 //===----------------------------------------------------------------------===//
48 /// This is the shared class of boolean and integer constants. This class
49 /// represents both boolean and integral constants.
50 /// @brief Class for constant integers.
51 class ConstantInt : public Constant {
52 virtual void anchor();
53 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
54 ConstantInt(const ConstantInt &) LLVM_DELETED_FUNCTION;
55 ConstantInt(IntegerType *Ty, const APInt& V);
58 // allocate space for exactly zero operands
59 void *operator new(size_t s) {
60 return User::operator new(s, 0);
63 static ConstantInt *getTrue(LLVMContext &Context);
64 static ConstantInt *getFalse(LLVMContext &Context);
65 static Constant *getTrue(Type *Ty);
66 static Constant *getFalse(Type *Ty);
68 /// If Ty is a vector type, return a Constant with a splat of the given
69 /// value. Otherwise return a ConstantInt for the given value.
70 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
72 /// Return a ConstantInt with the specified integer value for the specified
73 /// type. If the type is wider than 64 bits, the value will be zero-extended
74 /// to fit the type, unless isSigned is true, in which case the value will
75 /// be interpreted as a 64-bit signed integer and sign-extended to fit
77 /// @brief Get a ConstantInt for a specific value.
78 static ConstantInt *get(IntegerType *Ty, uint64_t V,
79 bool isSigned = false);
81 /// Return a ConstantInt with the specified value for the specified type. The
82 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
83 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
84 /// signed value for the type Ty.
85 /// @brief Get a ConstantInt for a specific signed value.
86 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
87 static Constant *getSigned(Type *Ty, int64_t V);
89 /// Return a ConstantInt with the specified value and an implied Type. The
90 /// type is the integer type that corresponds to the bit width of the value.
91 static ConstantInt *get(LLVMContext &Context, const APInt &V);
93 /// Return a ConstantInt constructed from the string strStart with the given
95 static ConstantInt *get(IntegerType *Ty, StringRef Str,
98 /// If Ty is a vector type, return a Constant with a splat of the given
99 /// value. Otherwise return a ConstantInt for the given value.
100 static Constant *get(Type* Ty, const APInt& V);
102 /// Return the constant as an APInt value reference. This allows clients to
103 /// obtain a copy of the value, with all its precision in tact.
104 /// @brief Return the constant's value.
105 inline const APInt &getValue() const {
109 /// getBitWidth - Return the bitwidth of this constant.
110 unsigned getBitWidth() const { return Val.getBitWidth(); }
112 /// Return the constant as a 64-bit unsigned integer value after it
113 /// has been zero extended as appropriate for the type of this constant. Note
114 /// 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.
123 /// @brief Return the sign extended value.
124 inline int64_t getSExtValue() const {
125 return Val.getSExtValue();
128 /// A helper method that can be used to determine if the constant contained
129 /// within is equal to a constant. This only works for very small values,
130 /// because this is all that can be represented with all types.
131 /// @brief Determine if this constant's value is same as an unsigned char.
132 bool equalsInt(uint64_t V) const {
136 /// getType - Specialize the getType() method to always return an IntegerType,
137 /// which reduces the amount of casting needed in parts of the compiler.
139 inline IntegerType *getType() const {
140 return cast<IntegerType>(Value::getType());
143 /// This static method returns true if the type Ty is big enough to
144 /// represent the value V. This can be used to avoid having the get method
145 /// assert when V is larger than Ty can represent. Note that there are two
146 /// versions of this method, one for unsigned and one for signed integers.
147 /// Although ConstantInt canonicalizes everything to an unsigned integer,
148 /// the signed version avoids callers having to convert a signed quantity
149 /// to the appropriate unsigned type before calling the method.
150 /// @returns true if V is a valid value for type Ty
151 /// @brief Determine if the value is in range for the given type.
152 static bool isValueValidForType(Type *Ty, uint64_t V);
153 static bool isValueValidForType(Type *Ty, int64_t V);
155 bool isNegative() const { return Val.isNegative(); }
157 /// This is just a convenience method to make client code smaller for a
158 /// common code. It also correctly performs the comparison without the
159 /// potential for an assertion from getZExtValue().
160 bool isZero() const {
164 /// This is just a convenience method to make client code smaller for a
165 /// common case. It also correctly performs the comparison without the
166 /// potential for an assertion from getZExtValue().
167 /// @brief Determine if the value is one.
172 /// This function will return true iff every bit in this constant is set
174 /// @returns true iff this constant's bits are all set to true.
175 /// @brief Determine if the value is all ones.
176 bool isMinusOne() const {
177 return Val.isAllOnesValue();
180 /// This function will return true iff this constant represents the largest
181 /// value that may be represented by the constant's type.
182 /// @returns true iff this is the largest value that may be represented
184 /// @brief Determine if the value is maximal.
185 bool isMaxValue(bool isSigned) const {
187 return Val.isMaxSignedValue();
189 return Val.isMaxValue();
192 /// This function will return true iff this constant represents the smallest
193 /// value that may be represented by this constant's type.
194 /// @returns true if this is the smallest value that may be represented by
196 /// @brief Determine if the value is minimal.
197 bool isMinValue(bool isSigned) const {
199 return Val.isMinSignedValue();
201 return Val.isMinValue();
204 /// This function will return true iff this constant represents a value with
205 /// active bits bigger than 64 bits or a value greater than the given uint64_t
207 /// @returns true iff this constant is greater or equal to the given number.
208 /// @brief Determine if the value is greater or equal to the given number.
209 bool uge(uint64_t Num) const {
210 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
213 /// getLimitedValue - If the value is smaller than the specified limit,
214 /// return it, otherwise return the limit value. This causes the value
215 /// to saturate to the limit.
216 /// @returns the min of the value of the constant and the specified value
217 /// @brief Get the constant's value with a saturation limit
218 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
219 return Val.getLimitedValue(Limit);
222 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
223 static bool classof(const Value *V) {
224 return V->getValueID() == ConstantIntVal;
229 //===----------------------------------------------------------------------===//
230 /// ConstantFP - Floating Point Values [float, double]
232 class ConstantFP : public Constant {
234 virtual void anchor();
235 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
236 ConstantFP(const ConstantFP &) LLVM_DELETED_FUNCTION;
237 friend class LLVMContextImpl;
239 ConstantFP(Type *Ty, const APFloat& V);
241 // allocate space for exactly zero operands
242 void *operator new(size_t s) {
243 return User::operator new(s, 0);
246 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
247 /// method returns the negative zero constant for floating point or vector
248 /// floating point types; for all other types, it returns the null value.
249 static Constant *getZeroValueForNegation(Type *Ty);
251 /// get() - This returns a ConstantFP, or a vector containing a splat of a
252 /// ConstantFP, for the specified value in the specified type. This should
253 /// only be used for simple constant values like 2.0/1.0 etc, that are
254 /// known-valid both as host double and as the target format.
255 static Constant *get(Type* Ty, double V);
256 static Constant *get(Type* Ty, StringRef Str);
257 static ConstantFP *get(LLVMContext &Context, const APFloat &V);
258 static ConstantFP *getNegativeZero(Type* Ty);
259 static ConstantFP *getInfinity(Type *Ty, bool Negative = false);
261 /// isValueValidForType - return true if Ty is big enough to represent V.
262 static bool isValueValidForType(Type *Ty, const APFloat &V);
263 inline const APFloat &getValueAPF() const { return Val; }
265 /// isZero - Return true if the value is positive or negative zero.
266 bool isZero() const { return Val.isZero(); }
268 /// isNegative - Return true if the sign bit is set.
269 bool isNegative() const { return Val.isNegative(); }
271 /// isNaN - Return true if the value is a NaN.
272 bool isNaN() const { return Val.isNaN(); }
274 /// isExactlyValue - We don't rely on operator== working on double values, as
275 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
276 /// As such, this method can be used to do an exact bit-for-bit comparison of
277 /// two floating point values. The version with a double operand is retained
278 /// because it's so convenient to write isExactlyValue(2.0), but please use
279 /// it only for simple constants.
280 bool isExactlyValue(const APFloat &V) const;
282 bool isExactlyValue(double V) const {
285 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
286 return isExactlyValue(FV);
288 /// Methods for support type inquiry through isa, cast, and dyn_cast:
289 static bool classof(const Value *V) {
290 return V->getValueID() == ConstantFPVal;
294 //===----------------------------------------------------------------------===//
295 /// ConstantAggregateZero - All zero aggregate value
297 class ConstantAggregateZero : public Constant {
298 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
299 ConstantAggregateZero(const ConstantAggregateZero &) LLVM_DELETED_FUNCTION;
301 explicit ConstantAggregateZero(Type *ty)
302 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
304 // allocate space for exactly zero operands
305 void *operator new(size_t s) {
306 return User::operator new(s, 0);
309 static ConstantAggregateZero *get(Type *Ty);
311 virtual void destroyConstant();
313 /// getSequentialElement - If this CAZ has array or vector type, return a zero
314 /// with the right element type.
315 Constant *getSequentialElement() const;
317 /// getStructElement - If this CAZ has struct type, return a zero with the
318 /// right element type for the specified element.
319 Constant *getStructElement(unsigned Elt) const;
321 /// getElementValue - Return a zero of the right value for the specified GEP
323 Constant *getElementValue(Constant *C) const;
325 /// getElementValue - Return a zero of the right value for the specified GEP
327 Constant *getElementValue(unsigned Idx) const;
329 /// Methods for support type inquiry through isa, cast, and dyn_cast:
331 static bool classof(const Value *V) {
332 return V->getValueID() == ConstantAggregateZeroVal;
337 //===----------------------------------------------------------------------===//
338 /// ConstantArray - Constant Array Declarations
340 class ConstantArray : public Constant {
341 friend struct ConstantArrayCreator<ConstantArray, ArrayType>;
342 ConstantArray(const ConstantArray &) LLVM_DELETED_FUNCTION;
344 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
346 // ConstantArray accessors
347 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
349 /// Transparently provide more efficient getOperand methods.
350 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
352 /// getType - Specialize the getType() method to always return an ArrayType,
353 /// which reduces the amount of casting needed in parts of the compiler.
355 inline ArrayType *getType() const {
356 return cast<ArrayType>(Value::getType());
359 virtual void destroyConstant();
360 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
362 /// Methods for support type inquiry through isa, cast, and dyn_cast:
363 static bool classof(const Value *V) {
364 return V->getValueID() == ConstantArrayVal;
369 struct OperandTraits<ConstantArray> :
370 public VariadicOperandTraits<ConstantArray> {
373 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
375 //===----------------------------------------------------------------------===//
376 // ConstantStruct - Constant Struct Declarations
378 class ConstantStruct : public Constant {
379 friend struct ConstantArrayCreator<ConstantStruct, StructType>;
380 ConstantStruct(const ConstantStruct &) LLVM_DELETED_FUNCTION;
382 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
384 // ConstantStruct accessors
385 static Constant *get(StructType *T, ArrayRef<Constant*> V);
386 static Constant *get(StructType *T, ...) END_WITH_NULL;
388 /// getAnon - Return an anonymous struct that has the specified
389 /// elements. If the struct is possibly empty, then you must specify a
391 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
392 return get(getTypeForElements(V, Packed), V);
394 static Constant *getAnon(LLVMContext &Ctx,
395 ArrayRef<Constant*> V, bool Packed = false) {
396 return get(getTypeForElements(Ctx, V, Packed), V);
399 /// getTypeForElements - Return an anonymous struct type to use for a constant
400 /// with the specified set of elements. The list must not be empty.
401 static StructType *getTypeForElements(ArrayRef<Constant*> V,
402 bool Packed = false);
403 /// getTypeForElements - This version of the method allows an empty list.
404 static StructType *getTypeForElements(LLVMContext &Ctx,
405 ArrayRef<Constant*> V,
406 bool Packed = false);
408 /// Transparently provide more efficient getOperand methods.
409 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
411 /// getType() specialization - Reduce amount of casting...
413 inline StructType *getType() const {
414 return cast<StructType>(Value::getType());
417 virtual void destroyConstant();
418 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
420 /// Methods for support type inquiry through isa, cast, and dyn_cast:
421 static bool classof(const Value *V) {
422 return V->getValueID() == ConstantStructVal;
427 struct OperandTraits<ConstantStruct> :
428 public VariadicOperandTraits<ConstantStruct> {
431 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
434 //===----------------------------------------------------------------------===//
435 /// ConstantVector - Constant Vector Declarations
437 class ConstantVector : public Constant {
438 friend struct ConstantArrayCreator<ConstantVector, VectorType>;
439 ConstantVector(const ConstantVector &) LLVM_DELETED_FUNCTION;
441 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
443 // ConstantVector accessors
444 static Constant *get(ArrayRef<Constant*> V);
446 /// getSplat - Return a ConstantVector with the specified constant in each
448 static Constant *getSplat(unsigned NumElts, Constant *Elt);
450 /// Transparently provide more efficient getOperand methods.
451 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
453 /// getType - Specialize the getType() method to always return a VectorType,
454 /// which reduces the amount of casting needed in parts of the compiler.
456 inline VectorType *getType() const {
457 return cast<VectorType>(Value::getType());
460 /// getSplatValue - If this is a splat constant, meaning that all of the
461 /// elements have the same value, return that value. Otherwise return NULL.
462 Constant *getSplatValue() const;
464 virtual void destroyConstant();
465 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
467 /// Methods for support type inquiry through isa, cast, and dyn_cast:
468 static bool classof(const Value *V) {
469 return V->getValueID() == ConstantVectorVal;
474 struct OperandTraits<ConstantVector> :
475 public VariadicOperandTraits<ConstantVector> {
478 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
480 //===----------------------------------------------------------------------===//
481 /// ConstantPointerNull - a constant pointer value that points to null
483 class ConstantPointerNull : public Constant {
484 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
485 ConstantPointerNull(const ConstantPointerNull &) LLVM_DELETED_FUNCTION;
487 explicit ConstantPointerNull(PointerType *T)
489 Value::ConstantPointerNullVal, 0, 0) {}
492 // allocate space for exactly zero operands
493 void *operator new(size_t s) {
494 return User::operator new(s, 0);
497 /// get() - Static factory methods - Return objects of the specified value
498 static ConstantPointerNull *get(PointerType *T);
500 virtual void destroyConstant();
502 /// getType - Specialize the getType() method to always return an PointerType,
503 /// which reduces the amount of casting needed in parts of the compiler.
505 inline PointerType *getType() const {
506 return cast<PointerType>(Value::getType());
509 /// Methods for support type inquiry through isa, cast, and dyn_cast:
510 static bool classof(const Value *V) {
511 return V->getValueID() == ConstantPointerNullVal;
515 //===----------------------------------------------------------------------===//
516 /// ConstantDataSequential - A vector or array constant whose element type is a
517 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
518 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
519 /// operands because it stores all of the elements of the constant as densely
520 /// packed data, instead of as Value*'s.
522 /// This is the common base class of ConstantDataArray and ConstantDataVector.
524 class ConstantDataSequential : public Constant {
525 friend class LLVMContextImpl;
526 /// DataElements - A pointer to the bytes underlying this constant (which is
527 /// owned by the uniquing StringMap).
528 const char *DataElements;
530 /// Next - This forms a link list of ConstantDataSequential nodes that have
531 /// the same value but different type. For example, 0,0,0,1 could be a 4
532 /// element array of i8, or a 1-element array of i32. They'll both end up in
533 /// the same StringMap bucket, linked up.
534 ConstantDataSequential *Next;
535 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
536 ConstantDataSequential(const ConstantDataSequential &) LLVM_DELETED_FUNCTION;
538 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
539 : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
540 ~ConstantDataSequential() { delete Next; }
542 static Constant *getImpl(StringRef Bytes, Type *Ty);
545 // allocate space for exactly zero operands.
546 void *operator new(size_t s) {
547 return User::operator new(s, 0);
551 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
552 /// formed with a vector or array of the specified element type.
553 /// ConstantDataArray only works with normal float and int types that are
554 /// stored densely in memory, not with things like i42 or x86_f80.
555 static bool isElementTypeCompatible(const Type *Ty);
557 /// getElementAsInteger - If this is a sequential container of integers (of
558 /// any size), return the specified element in the low bits of a uint64_t.
559 uint64_t getElementAsInteger(unsigned i) const;
561 /// getElementAsAPFloat - If this is a sequential container of floating point
562 /// type, return the specified element as an APFloat.
563 APFloat getElementAsAPFloat(unsigned i) const;
565 /// getElementAsFloat - If this is an sequential container of floats, return
566 /// the specified element as a float.
567 float getElementAsFloat(unsigned i) const;
569 /// getElementAsDouble - If this is an sequential container of doubles, return
570 /// the specified element as a double.
571 double getElementAsDouble(unsigned i) const;
573 /// getElementAsConstant - Return a Constant for a specified index's element.
574 /// Note that this has to compute a new constant to return, so it isn't as
575 /// efficient as getElementAsInteger/Float/Double.
576 Constant *getElementAsConstant(unsigned i) const;
578 /// getType - Specialize the getType() method to always return a
579 /// SequentialType, which reduces the amount of casting needed in parts of the
581 inline SequentialType *getType() const {
582 return cast<SequentialType>(Value::getType());
585 /// getElementType - Return the element type of the array/vector.
586 Type *getElementType() const;
588 /// getNumElements - Return the number of elements in the array or vector.
589 unsigned getNumElements() const;
591 /// getElementByteSize - Return the size (in bytes) of each element in the
592 /// array/vector. The size of the elements is known to be a multiple of one
594 uint64_t getElementByteSize() const;
597 /// isString - This method returns true if this is an array of i8.
598 bool isString() const;
600 /// isCString - This method returns true if the array "isString", ends with a
601 /// nul byte, and does not contains any other nul bytes.
602 bool isCString() const;
604 /// getAsString - If this array is isString(), then this method returns the
605 /// array as a StringRef. Otherwise, it asserts out.
607 StringRef getAsString() const {
608 assert(isString() && "Not a string");
609 return getRawDataValues();
612 /// getAsCString - If this array is isCString(), then this method returns the
613 /// array (without the trailing null byte) as a StringRef. Otherwise, it
616 StringRef getAsCString() const {
617 assert(isCString() && "Isn't a C string");
618 StringRef Str = getAsString();
619 return Str.substr(0, Str.size()-1);
622 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
623 /// that this is an extremely tricky thing to work with, as it exposes the
624 /// host endianness of the data elements.
625 StringRef getRawDataValues() const;
627 virtual void destroyConstant();
629 /// Methods for support type inquiry through isa, cast, and dyn_cast:
631 static bool classof(const Value *V) {
632 return V->getValueID() == ConstantDataArrayVal ||
633 V->getValueID() == ConstantDataVectorVal;
636 const char *getElementPointer(unsigned Elt) const;
639 //===----------------------------------------------------------------------===//
640 /// ConstantDataArray - An array constant whose element type is a simple
641 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
642 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
643 /// operands because it stores all of the elements of the constant as densely
644 /// packed data, instead of as Value*'s.
645 class ConstantDataArray : public ConstantDataSequential {
646 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
647 ConstantDataArray(const ConstantDataArray &) LLVM_DELETED_FUNCTION;
648 virtual void anchor();
649 friend class ConstantDataSequential;
650 explicit ConstantDataArray(Type *ty, const char *Data)
651 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
653 // allocate space for exactly zero operands.
654 void *operator new(size_t s) {
655 return User::operator new(s, 0);
659 /// get() constructors - Return a constant with array type with an element
660 /// count and element type matching the ArrayRef passed in. Note that this
661 /// can return a ConstantAggregateZero object.
662 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
663 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
664 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
665 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
666 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
667 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
669 /// getString - This method constructs a CDS and initializes it with a text
670 /// string. The default behavior (AddNull==true) causes a null terminator to
671 /// be placed at the end of the array (increasing the length of the string by
672 /// one more than the StringRef would normally indicate. Pass AddNull=false
673 /// to disable this behavior.
674 static Constant *getString(LLVMContext &Context, StringRef Initializer,
675 bool AddNull = true);
677 /// getType - Specialize the getType() method to always return an ArrayType,
678 /// which reduces the amount of casting needed in parts of the compiler.
680 inline ArrayType *getType() const {
681 return cast<ArrayType>(Value::getType());
684 /// Methods for support type inquiry through isa, cast, and dyn_cast:
686 static bool classof(const Value *V) {
687 return V->getValueID() == ConstantDataArrayVal;
691 //===----------------------------------------------------------------------===//
692 /// ConstantDataVector - A vector constant whose element type is a simple
693 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
694 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
695 /// operands because it stores all of the elements of the constant as densely
696 /// packed data, instead of as Value*'s.
697 class ConstantDataVector : public ConstantDataSequential {
698 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
699 ConstantDataVector(const ConstantDataVector &) LLVM_DELETED_FUNCTION;
700 virtual void anchor();
701 friend class ConstantDataSequential;
702 explicit ConstantDataVector(Type *ty, const char *Data)
703 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
705 // allocate space for exactly zero operands.
706 void *operator new(size_t s) {
707 return User::operator new(s, 0);
711 /// get() constructors - Return a constant with vector type with an element
712 /// count and element type matching the ArrayRef passed in. Note that this
713 /// can return a ConstantAggregateZero object.
714 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
715 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
716 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
717 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
718 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
719 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
721 /// getSplat - Return a ConstantVector with the specified constant in each
722 /// element. The specified constant has to be a of a compatible type (i8/i16/
723 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
724 static Constant *getSplat(unsigned NumElts, Constant *Elt);
726 /// getSplatValue - If this is a splat constant, meaning that all of the
727 /// elements have the same value, return that value. Otherwise return NULL.
728 Constant *getSplatValue() const;
730 /// getType - Specialize the getType() method to always return a VectorType,
731 /// which reduces the amount of casting needed in parts of the compiler.
733 inline VectorType *getType() const {
734 return cast<VectorType>(Value::getType());
737 /// Methods for support type inquiry through isa, cast, and dyn_cast:
739 static bool classof(const Value *V) {
740 return V->getValueID() == ConstantDataVectorVal;
746 /// BlockAddress - The address of a basic block.
748 class BlockAddress : public Constant {
749 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
750 void *operator new(size_t s) { return User::operator new(s, 2); }
751 BlockAddress(Function *F, BasicBlock *BB);
753 /// get - Return a BlockAddress for the specified function and basic block.
754 static BlockAddress *get(Function *F, BasicBlock *BB);
756 /// get - Return a BlockAddress for the specified basic block. The basic
757 /// block must be embedded into a function.
758 static BlockAddress *get(BasicBlock *BB);
760 /// Transparently provide more efficient getOperand methods.
761 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
763 Function *getFunction() const { return (Function*)Op<0>().get(); }
764 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
766 virtual void destroyConstant();
767 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
769 /// Methods for support type inquiry through isa, cast, and dyn_cast:
770 static inline bool classof(const Value *V) {
771 return V->getValueID() == BlockAddressVal;
776 struct OperandTraits<BlockAddress> :
777 public FixedNumOperandTraits<BlockAddress, 2> {
780 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
783 //===----------------------------------------------------------------------===//
784 /// ConstantExpr - a constant value that is initialized with an expression using
785 /// other constant values.
787 /// This class uses the standard Instruction opcodes to define the various
788 /// constant expressions. The Opcode field for the ConstantExpr class is
789 /// maintained in the Value::SubclassData field.
790 class ConstantExpr : public Constant {
791 friend struct ConstantCreator<ConstantExpr,Type,
792 std::pair<unsigned, std::vector<Constant*> > >;
793 friend struct ConvertConstantType<ConstantExpr, Type>;
796 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
797 : Constant(ty, ConstantExprVal, Ops, NumOps) {
798 // Operation type (an Instruction opcode) is stored as the SubclassData.
799 setValueSubclassData(Opcode);
803 // Static methods to construct a ConstantExpr of different kinds. Note that
804 // these methods may return a object that is not an instance of the
805 // ConstantExpr class, because they will attempt to fold the constant
806 // expression into something simpler if possible.
808 /// getAlignOf constant expr - computes the alignment of a type in a target
809 /// independent way (Note: the return type is an i64).
810 static Constant *getAlignOf(Type *Ty);
812 /// getSizeOf constant expr - computes the (alloc) size of a type (in
813 /// address-units, not bits) in a target independent way (Note: the return
816 static Constant *getSizeOf(Type *Ty);
818 /// getOffsetOf constant expr - computes the offset of a struct field in a
819 /// target independent way (Note: the return type is an i64).
821 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
823 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
824 /// which supports any aggregate type, and any Constant index.
826 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
828 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
829 static Constant *getFNeg(Constant *C);
830 static Constant *getNot(Constant *C);
831 static Constant *getAdd(Constant *C1, Constant *C2,
832 bool HasNUW = false, bool HasNSW = false);
833 static Constant *getFAdd(Constant *C1, Constant *C2);
834 static Constant *getSub(Constant *C1, Constant *C2,
835 bool HasNUW = false, bool HasNSW = false);
836 static Constant *getFSub(Constant *C1, Constant *C2);
837 static Constant *getMul(Constant *C1, Constant *C2,
838 bool HasNUW = false, bool HasNSW = false);
839 static Constant *getFMul(Constant *C1, Constant *C2);
840 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
841 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
842 static Constant *getFDiv(Constant *C1, Constant *C2);
843 static Constant *getURem(Constant *C1, Constant *C2);
844 static Constant *getSRem(Constant *C1, Constant *C2);
845 static Constant *getFRem(Constant *C1, Constant *C2);
846 static Constant *getAnd(Constant *C1, Constant *C2);
847 static Constant *getOr(Constant *C1, Constant *C2);
848 static Constant *getXor(Constant *C1, Constant *C2);
849 static Constant *getShl(Constant *C1, Constant *C2,
850 bool HasNUW = false, bool HasNSW = false);
851 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
852 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
853 static Constant *getTrunc (Constant *C, Type *Ty);
854 static Constant *getSExt (Constant *C, Type *Ty);
855 static Constant *getZExt (Constant *C, Type *Ty);
856 static Constant *getFPTrunc (Constant *C, Type *Ty);
857 static Constant *getFPExtend(Constant *C, Type *Ty);
858 static Constant *getUIToFP (Constant *C, Type *Ty);
859 static Constant *getSIToFP (Constant *C, Type *Ty);
860 static Constant *getFPToUI (Constant *C, Type *Ty);
861 static Constant *getFPToSI (Constant *C, Type *Ty);
862 static Constant *getPtrToInt(Constant *C, Type *Ty);
863 static Constant *getIntToPtr(Constant *C, Type *Ty);
864 static Constant *getBitCast (Constant *C, Type *Ty);
866 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
867 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
868 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
869 return getAdd(C1, C2, false, true);
871 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
872 return getAdd(C1, C2, true, false);
874 static Constant *getNSWSub(Constant *C1, Constant *C2) {
875 return getSub(C1, C2, false, true);
877 static Constant *getNUWSub(Constant *C1, Constant *C2) {
878 return getSub(C1, C2, true, false);
880 static Constant *getNSWMul(Constant *C1, Constant *C2) {
881 return getMul(C1, C2, false, true);
883 static Constant *getNUWMul(Constant *C1, Constant *C2) {
884 return getMul(C1, C2, true, false);
886 static Constant *getNSWShl(Constant *C1, Constant *C2) {
887 return getShl(C1, C2, false, true);
889 static Constant *getNUWShl(Constant *C1, Constant *C2) {
890 return getShl(C1, C2, true, false);
892 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
893 return getSDiv(C1, C2, true);
895 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
896 return getUDiv(C1, C2, true);
898 static Constant *getExactAShr(Constant *C1, Constant *C2) {
899 return getAShr(C1, C2, true);
901 static Constant *getExactLShr(Constant *C1, Constant *C2) {
902 return getLShr(C1, C2, true);
905 /// getBinOpIdentity - Return the identity for the given binary operation,
906 /// i.e. a constant C such that X op C = X and C op X = X for every X. It
907 /// returns null if the operator doesn't have an identity.
908 static Constant *getBinOpIdentity(unsigned Opcode, Type *Ty);
910 /// getBinOpAbsorber - Return the absorbing element for the given binary
911 /// operation, i.e. a constant C such that X op C = C and C op X = C for
912 /// every X. For example, this returns zero for integer multiplication.
913 /// It returns null if the operator doesn't have an absorbing element.
914 static Constant *getBinOpAbsorber(unsigned Opcode, Type *Ty);
916 /// Transparently provide more efficient getOperand methods.
917 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
919 // @brief Convenience function for getting one of the casting operations
920 // using a CastOps opcode.
921 static Constant *getCast(
922 unsigned ops, ///< The opcode for the conversion
923 Constant *C, ///< The constant to be converted
924 Type *Ty ///< The type to which the constant is converted
927 // @brief Create a ZExt or BitCast cast constant expression
928 static Constant *getZExtOrBitCast(
929 Constant *C, ///< The constant to zext or bitcast
930 Type *Ty ///< The type to zext or bitcast C to
933 // @brief Create a SExt or BitCast cast constant expression
934 static Constant *getSExtOrBitCast(
935 Constant *C, ///< The constant to sext or bitcast
936 Type *Ty ///< The type to sext or bitcast C to
939 // @brief Create a Trunc or BitCast cast constant expression
940 static Constant *getTruncOrBitCast(
941 Constant *C, ///< The constant to trunc or bitcast
942 Type *Ty ///< The type to trunc or bitcast C to
945 /// @brief Create a BitCast or a PtrToInt cast constant expression
946 static Constant *getPointerCast(
947 Constant *C, ///< The pointer value to be casted (operand 0)
948 Type *Ty ///< The type to which cast should be made
951 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
952 static Constant *getIntegerCast(
953 Constant *C, ///< The integer constant to be casted
954 Type *Ty, ///< The integer type to cast to
955 bool isSigned ///< Whether C should be treated as signed or not
958 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
959 static Constant *getFPCast(
960 Constant *C, ///< The integer constant to be casted
961 Type *Ty ///< The integer type to cast to
964 /// @brief Return true if this is a convert constant expression
967 /// @brief Return true if this is a compare constant expression
968 bool isCompare() const;
970 /// @brief Return true if this is an insertvalue or extractvalue expression,
971 /// and the getIndices() method may be used.
972 bool hasIndices() const;
974 /// @brief Return true if this is a getelementptr expression and all
975 /// the index operands are compile-time known integers within the
976 /// corresponding notional static array extents. Note that this is
977 /// not equivalant to, a subset of, or a superset of the "inbounds"
979 bool isGEPWithNoNotionalOverIndexing() const;
981 /// Select constant expr
983 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
985 /// get - Return a binary or shift operator constant expression,
986 /// folding if possible.
988 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
991 /// @brief Return an ICmp or FCmp comparison operator constant expression.
992 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
994 /// get* - Return some common constants without having to
995 /// specify the full Instruction::OPCODE identifier.
997 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
998 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1000 /// Getelementptr form. Value* is only accepted for convenience;
1001 /// all elements must be Constant's.
1003 static Constant *getGetElementPtr(Constant *C,
1004 ArrayRef<Constant *> IdxList,
1005 bool InBounds = false) {
1006 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1010 static Constant *getGetElementPtr(Constant *C,
1012 bool InBounds = false) {
1013 // This form of the function only exists to avoid ambiguous overload
1014 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1015 // ArrayRef<Value *>.
1016 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1018 static Constant *getGetElementPtr(Constant *C,
1019 ArrayRef<Value *> IdxList,
1020 bool InBounds = false);
1022 /// Create an "inbounds" getelementptr. See the documentation for the
1023 /// "inbounds" flag in LangRef.html for details.
1024 static Constant *getInBoundsGetElementPtr(Constant *C,
1025 ArrayRef<Constant *> IdxList) {
1026 return getGetElementPtr(C, IdxList, true);
1028 static Constant *getInBoundsGetElementPtr(Constant *C,
1030 // This form of the function only exists to avoid ambiguous overload
1031 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1032 // ArrayRef<Value *>.
1033 return getGetElementPtr(C, Idx, true);
1035 static Constant *getInBoundsGetElementPtr(Constant *C,
1036 ArrayRef<Value *> IdxList) {
1037 return getGetElementPtr(C, IdxList, true);
1040 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1041 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1042 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1043 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1044 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1045 ArrayRef<unsigned> Idxs);
1047 /// getOpcode - Return the opcode at the root of this constant expression
1048 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1050 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1051 /// not an ICMP or FCMP constant expression.
1052 unsigned getPredicate() const;
1054 /// getIndices - Assert that this is an insertvalue or exactvalue
1055 /// expression and return the list of indices.
1056 ArrayRef<unsigned> getIndices() const;
1058 /// getOpcodeName - Return a string representation for an opcode.
1059 const char *getOpcodeName() const;
1061 /// getWithOperandReplaced - Return a constant expression identical to this
1062 /// one, but with the specified operand set to the specified value.
1063 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1065 /// getWithOperands - This returns the current constant expression with the
1066 /// operands replaced with the specified values. The specified array must
1067 /// have the same number of operands as our current one.
1068 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1069 return getWithOperands(Ops, getType());
1072 /// getWithOperands - This returns the current constant expression with the
1073 /// operands replaced with the specified values and with the specified result
1074 /// type. The specified array must have the same number of operands as our
1076 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1078 /// getAsInstruction - Returns an Instruction which implements the same operation
1079 /// as this ConstantExpr. The instruction is not linked to any basic block.
1081 /// A better approach to this could be to have a constructor for Instruction
1082 /// which would take a ConstantExpr parameter, but that would have spread
1083 /// implementation details of ConstantExpr outside of Constants.cpp, which
1084 /// would make it harder to remove ConstantExprs altogether.
1085 Instruction *getAsInstruction();
1087 virtual void destroyConstant();
1088 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1090 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1091 static inline bool classof(const Value *V) {
1092 return V->getValueID() == ConstantExprVal;
1096 // Shadow Value::setValueSubclassData with a private forwarding method so that
1097 // subclasses cannot accidentally use it.
1098 void setValueSubclassData(unsigned short D) {
1099 Value::setValueSubclassData(D);
1104 struct OperandTraits<ConstantExpr> :
1105 public VariadicOperandTraits<ConstantExpr, 1> {
1108 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1110 //===----------------------------------------------------------------------===//
1111 /// UndefValue - 'undef' values are things that do not have specified contents.
1112 /// These are used for a variety of purposes, including global variable
1113 /// initializers and operands to instructions. 'undef' values can occur with
1114 /// any first-class type.
1116 /// Undef values aren't exactly constants; if they have multiple uses, they
1117 /// can appear to have different bit patterns at each use. See
1118 /// LangRef.html#undefvalues for details.
1120 class UndefValue : public Constant {
1121 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1122 UndefValue(const UndefValue &) LLVM_DELETED_FUNCTION;
1124 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1126 // allocate space for exactly zero operands
1127 void *operator new(size_t s) {
1128 return User::operator new(s, 0);
1131 /// get() - Static factory methods - Return an 'undef' object of the specified
1134 static UndefValue *get(Type *T);
1136 /// getSequentialElement - If this Undef has array or vector type, return a
1137 /// undef with the right element type.
1138 UndefValue *getSequentialElement() const;
1140 /// getStructElement - If this undef has struct type, return a undef with the
1141 /// right element type for the specified element.
1142 UndefValue *getStructElement(unsigned Elt) const;
1144 /// getElementValue - Return an undef of the right value for the specified GEP
1146 UndefValue *getElementValue(Constant *C) const;
1148 /// getElementValue - Return an undef of the right value for the specified GEP
1150 UndefValue *getElementValue(unsigned Idx) const;
1152 virtual void destroyConstant();
1154 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1155 static bool classof(const Value *V) {
1156 return V->getValueID() == UndefValueVal;
1160 } // End llvm namespace