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 ConvertConstantType;
44 //===----------------------------------------------------------------------===//
45 /// This is the shared class of boolean and integer constants. This class
46 /// represents both boolean and integral constants.
47 /// @brief Class for constant integers.
48 class ConstantInt : public Constant {
49 virtual void anchor();
50 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
51 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
52 ConstantInt(IntegerType *Ty, const APInt& V);
55 // allocate space for exactly zero operands
56 void *operator new(size_t s) {
57 return User::operator new(s, 0);
60 static ConstantInt *getTrue(LLVMContext &Context);
61 static ConstantInt *getFalse(LLVMContext &Context);
62 static Constant *getTrue(Type *Ty);
63 static Constant *getFalse(Type *Ty);
65 /// If Ty is a vector type, return a Constant with a splat of the given
66 /// value. Otherwise return a ConstantInt for the given value.
67 static Constant *get(Type *Ty, uint64_t V, bool isSigned = false);
69 /// Return a ConstantInt with the specified integer value for the specified
70 /// type. If the type is wider than 64 bits, the value will be zero-extended
71 /// to fit the type, unless isSigned is true, in which case the value will
72 /// be interpreted as a 64-bit signed integer and sign-extended to fit
74 /// @brief Get a ConstantInt for a specific value.
75 static ConstantInt *get(IntegerType *Ty, uint64_t V,
76 bool isSigned = false);
78 /// Return a ConstantInt with the specified value for the specified type. The
79 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
80 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
81 /// signed value for the type Ty.
82 /// @brief Get a ConstantInt for a specific signed value.
83 static ConstantInt *getSigned(IntegerType *Ty, int64_t V);
84 static Constant *getSigned(Type *Ty, int64_t V);
86 /// Return a ConstantInt with the specified value and an implied Type. The
87 /// type is the integer type that corresponds to the bit width of the value.
88 static ConstantInt *get(LLVMContext &Context, const APInt &V);
90 /// Return a ConstantInt constructed from the string strStart with the given
92 static ConstantInt *get(IntegerType *Ty, StringRef Str,
95 /// If Ty is a vector type, return a Constant with a splat of the given
96 /// value. Otherwise return a ConstantInt for the given value.
97 static Constant *get(Type* Ty, const APInt& V);
99 /// Return the constant as an APInt value reference. This allows clients to
100 /// obtain a copy of the value, with all its precision in tact.
101 /// @brief Return the constant's value.
102 inline const APInt &getValue() const {
106 /// getBitWidth - Return the bitwidth of this constant.
107 unsigned getBitWidth() const { return Val.getBitWidth(); }
109 /// Return the constant as a 64-bit unsigned integer value after it
110 /// has been zero extended as appropriate for the type of this constant. Note
111 /// that this method can assert if the value does not fit in 64 bits.
113 /// @brief Return the zero extended value.
114 inline uint64_t getZExtValue() const {
115 return Val.getZExtValue();
118 /// Return the constant as a 64-bit integer value after it has been sign
119 /// extended as appropriate for the type of this constant. Note that
120 /// this method can assert if the value does not fit in 64 bits.
122 /// @brief Return the sign extended value.
123 inline int64_t getSExtValue() const {
124 return Val.getSExtValue();
127 /// A helper method that can be used to determine if the constant contained
128 /// within is equal to a constant. This only works for very small values,
129 /// because this is all that can be represented with all types.
130 /// @brief Determine if this constant's value is same as an unsigned char.
131 bool equalsInt(uint64_t V) const {
135 /// getType - Specialize the getType() method to always return an IntegerType,
136 /// which reduces the amount of casting needed in parts of the compiler.
138 inline IntegerType *getType() const {
139 return reinterpret_cast<IntegerType*>(Value::getType());
142 /// This static method returns true if the type Ty is big enough to
143 /// represent the value V. This can be used to avoid having the get method
144 /// assert when V is larger than Ty can represent. Note that there are two
145 /// versions of this method, one for unsigned and one for signed integers.
146 /// Although ConstantInt canonicalizes everything to an unsigned integer,
147 /// the signed version avoids callers having to convert a signed quantity
148 /// to the appropriate unsigned type before calling the method.
149 /// @returns true if V is a valid value for type Ty
150 /// @brief Determine if the value is in range for the given type.
151 static bool isValueValidForType(Type *Ty, uint64_t V);
152 static bool isValueValidForType(Type *Ty, int64_t V);
154 bool isNegative() const { return Val.isNegative(); }
156 /// This is just a convenience method to make client code smaller for a
157 /// common code. It also correctly performs the comparison without the
158 /// potential for an assertion from getZExtValue().
159 bool isZero() const {
163 /// This is just a convenience method to make client code smaller for a
164 /// common case. It also correctly performs the comparison without the
165 /// potential for an assertion from getZExtValue().
166 /// @brief Determine if the value is one.
171 /// This function will return true iff every bit in this constant is set
173 /// @returns true iff this constant's bits are all set to true.
174 /// @brief Determine if the value is all ones.
175 bool isMinusOne() const {
176 return Val.isAllOnesValue();
179 /// This function will return true iff this constant represents the largest
180 /// value that may be represented by the constant's type.
181 /// @returns true iff this is the largest value that may be represented
183 /// @brief Determine if the value is maximal.
184 bool isMaxValue(bool isSigned) const {
186 return Val.isMaxSignedValue();
188 return Val.isMaxValue();
191 /// This function will return true iff this constant represents the smallest
192 /// value that may be represented by this constant's type.
193 /// @returns true if this is the smallest value that may be represented by
195 /// @brief Determine if the value is minimal.
196 bool isMinValue(bool isSigned) const {
198 return Val.isMinSignedValue();
200 return Val.isMinValue();
203 /// This function will return true iff this constant represents a value with
204 /// active bits bigger than 64 bits or a value greater than the given uint64_t
206 /// @returns true iff this constant is greater or equal to the given number.
207 /// @brief Determine if the value is greater or equal to the given number.
208 bool uge(uint64_t Num) const {
209 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
212 /// getLimitedValue - If the value is smaller than the specified limit,
213 /// return it, otherwise return the limit value. This causes the value
214 /// to saturate to the limit.
215 /// @returns the min of the value of the constant and the specified value
216 /// @brief Get the constant's value with a saturation limit
217 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
218 return Val.getLimitedValue(Limit);
221 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
222 static inline bool classof(const ConstantInt *) { return true; }
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);// DO NOT IMPLEMENT
236 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
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 {
284 // convert is not supported on this type
285 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
288 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven, &ignored);
289 return isExactlyValue(FV);
291 /// Methods for support type inquiry through isa, cast, and dyn_cast:
292 static inline bool classof(const ConstantFP *) { return true; }
293 static bool classof(const Value *V) {
294 return V->getValueID() == ConstantFPVal;
298 //===----------------------------------------------------------------------===//
299 /// ConstantAggregateZero - All zero aggregate value
301 class ConstantAggregateZero : public Constant {
302 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
303 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
305 explicit ConstantAggregateZero(Type *ty)
306 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
308 // allocate space for exactly zero operands
309 void *operator new(size_t s) {
310 return User::operator new(s, 0);
313 static ConstantAggregateZero *get(Type *Ty);
315 virtual void destroyConstant();
317 /// getSequentialElement - If this CAZ has array or vector type, return a zero
318 /// with the right element type.
319 Constant *getSequentialElement() const;
321 /// getStructElement - If this CAZ has struct type, return a zero with the
322 /// right element type for the specified element.
323 Constant *getStructElement(unsigned Elt) const;
325 /// getElementValue - Return a zero of the right value for the specified GEP
327 Constant *getElementValue(Constant *C) const;
329 /// getElementValue - Return a zero of the right value for the specified GEP
331 Constant *getElementValue(unsigned Idx) const;
333 /// Methods for support type inquiry through isa, cast, and dyn_cast:
335 static bool classof(const ConstantAggregateZero *) { return true; }
336 static bool classof(const Value *V) {
337 return V->getValueID() == ConstantAggregateZeroVal;
342 //===----------------------------------------------------------------------===//
343 /// ConstantArray - Constant Array Declarations
345 class ConstantArray : public Constant {
346 friend struct ConstantCreator<ConstantArray, ArrayType,
347 std::vector<Constant*> >;
348 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
350 ConstantArray(ArrayType *T, ArrayRef<Constant *> Val);
352 // ConstantArray accessors
353 static Constant *get(ArrayType *T, ArrayRef<Constant*> V);
355 /// This method constructs a ConstantArray and initializes it with a text
356 /// string. The default behavior (AddNull==true) causes a null terminator to
357 /// be placed at the end of the array. This effectively increases the length
358 /// of the array by one (you've been warned). However, in some situations
359 /// this is not desired so if AddNull==false then the string is copied without
360 /// null termination.
362 // FIXME Remove this.
363 static Constant *get(LLVMContext &Context, StringRef Initializer,
364 bool AddNull = true);
366 /// Transparently provide more efficient getOperand methods.
367 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
369 /// getType - Specialize the getType() method to always return an ArrayType,
370 /// which reduces the amount of casting needed in parts of the compiler.
372 inline ArrayType *getType() const {
373 return reinterpret_cast<ArrayType*>(Value::getType());
376 // FIXME: String methods will eventually be removed.
379 /// isString - This method returns true if the array is an array of i8 and
380 /// the elements of the array are all ConstantInt's.
381 bool isString() const;
383 /// isCString - This method returns true if the array is a string (see
385 /// isString) and it ends in a null byte \0 and does not contains any other
387 /// null bytes except its terminator.
388 bool isCString() const;
390 /// getAsString - If this array is isString(), then this method converts the
391 /// array to an std::string and returns it. Otherwise, it asserts out.
393 std::string getAsString() const;
395 /// getAsCString - If this array is isCString(), then this method converts the
396 /// array (without the trailing null byte) to an std::string and returns it.
397 /// Otherwise, it asserts out.
399 std::string getAsCString() const;
401 virtual void destroyConstant();
402 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
404 /// Methods for support type inquiry through isa, cast, and dyn_cast:
405 static inline bool classof(const ConstantArray *) { return true; }
406 static bool classof(const Value *V) {
407 return V->getValueID() == ConstantArrayVal;
412 struct OperandTraits<ConstantArray> :
413 public VariadicOperandTraits<ConstantArray> {
416 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantArray, Constant)
418 //===----------------------------------------------------------------------===//
419 // ConstantStruct - Constant Struct Declarations
421 class ConstantStruct : public Constant {
422 friend struct ConstantCreator<ConstantStruct, StructType,
423 std::vector<Constant*> >;
424 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
426 ConstantStruct(StructType *T, ArrayRef<Constant *> Val);
428 // ConstantStruct accessors
429 static Constant *get(StructType *T, ArrayRef<Constant*> V);
430 static Constant *get(StructType *T, ...) END_WITH_NULL;
432 /// getAnon - Return an anonymous struct that has the specified
433 /// elements. If the struct is possibly empty, then you must specify a
435 static Constant *getAnon(ArrayRef<Constant*> V, bool Packed = false) {
436 return get(getTypeForElements(V, Packed), V);
438 static Constant *getAnon(LLVMContext &Ctx,
439 ArrayRef<Constant*> V, bool Packed = false) {
440 return get(getTypeForElements(Ctx, V, Packed), V);
443 /// getTypeForElements - Return an anonymous struct type to use for a constant
444 /// with the specified set of elements. The list must not be empty.
445 static StructType *getTypeForElements(ArrayRef<Constant*> V,
446 bool Packed = false);
447 /// getTypeForElements - This version of the method allows an empty list.
448 static StructType *getTypeForElements(LLVMContext &Ctx,
449 ArrayRef<Constant*> V,
450 bool Packed = false);
452 /// Transparently provide more efficient getOperand methods.
453 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
455 /// getType() specialization - Reduce amount of casting...
457 inline StructType *getType() const {
458 return reinterpret_cast<StructType*>(Value::getType());
461 virtual void destroyConstant();
462 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
464 /// Methods for support type inquiry through isa, cast, and dyn_cast:
465 static inline bool classof(const ConstantStruct *) { return true; }
466 static bool classof(const Value *V) {
467 return V->getValueID() == ConstantStructVal;
472 struct OperandTraits<ConstantStruct> :
473 public VariadicOperandTraits<ConstantStruct> {
476 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantStruct, Constant)
479 //===----------------------------------------------------------------------===//
480 /// ConstantVector - Constant Vector Declarations
482 class ConstantVector : public Constant {
483 friend struct ConstantCreator<ConstantVector, VectorType,
484 std::vector<Constant*> >;
485 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
487 ConstantVector(VectorType *T, ArrayRef<Constant *> Val);
489 // ConstantVector accessors
490 static Constant *get(ArrayRef<Constant*> V);
492 /// getSplat - Return a ConstantVector with the specified constant in each
494 static Constant *getSplat(unsigned NumElts, Constant *Elt);
496 /// Transparently provide more efficient getOperand methods.
497 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
499 /// getType - Specialize the getType() method to always return a VectorType,
500 /// which reduces the amount of casting needed in parts of the compiler.
502 inline VectorType *getType() const {
503 return reinterpret_cast<VectorType*>(Value::getType());
506 /// getSplatValue - If this is a splat constant, meaning that all of the
507 /// elements have the same value, return that value. Otherwise return NULL.
508 Constant *getSplatValue() const;
510 virtual void destroyConstant();
511 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
513 /// Methods for support type inquiry through isa, cast, and dyn_cast:
514 static inline bool classof(const ConstantVector *) { return true; }
515 static bool classof(const Value *V) {
516 return V->getValueID() == ConstantVectorVal;
521 struct OperandTraits<ConstantVector> :
522 public VariadicOperandTraits<ConstantVector> {
525 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantVector, Constant)
527 //===----------------------------------------------------------------------===//
528 /// ConstantPointerNull - a constant pointer value that points to null
530 class ConstantPointerNull : public Constant {
531 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
532 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
534 explicit ConstantPointerNull(PointerType *T)
535 : Constant(reinterpret_cast<Type*>(T),
536 Value::ConstantPointerNullVal, 0, 0) {}
539 // allocate space for exactly zero operands
540 void *operator new(size_t s) {
541 return User::operator new(s, 0);
544 /// get() - Static factory methods - Return objects of the specified value
545 static ConstantPointerNull *get(PointerType *T);
547 virtual void destroyConstant();
549 /// getType - Specialize the getType() method to always return an PointerType,
550 /// which reduces the amount of casting needed in parts of the compiler.
552 inline PointerType *getType() const {
553 return reinterpret_cast<PointerType*>(Value::getType());
556 /// Methods for support type inquiry through isa, cast, and dyn_cast:
557 static inline bool classof(const ConstantPointerNull *) { return true; }
558 static bool classof(const Value *V) {
559 return V->getValueID() == ConstantPointerNullVal;
563 //===----------------------------------------------------------------------===//
564 /// ConstantDataSequential - A vector or array constant whose element type is a
565 /// simple 1/2/4/8-byte integer or float/double, and whose elements are just
566 /// simple data values (i.e. ConstantInt/ConstantFP). This Constant node has no
567 /// operands because it stores all of the elements of the constant as densely
568 /// packed data, instead of as Value*'s.
570 /// This is the common base class of ConstantDataArray and ConstantDataVector.
572 class ConstantDataSequential : public Constant {
573 friend class LLVMContextImpl;
574 /// DataElements - A pointer to the bytes underlying this constant (which is
575 /// owned by the uniquing StringMap).
576 const char *DataElements;
578 /// Next - This forms a link list of ConstantDataSequential nodes that have
579 /// the same value but different type. For example, 0,0,0,1 could be a 4
580 /// element array of i8, or a 1-element array of i32. They'll both end up in
581 /// the same StringMap bucket, linked up.
582 ConstantDataSequential *Next;
583 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
584 ConstantDataSequential(const ConstantDataSequential &); // DO NOT IMPLEMENT
586 explicit ConstantDataSequential(Type *ty, ValueTy VT, const char *Data)
587 : Constant(ty, VT, 0, 0), DataElements(Data), Next(0) {}
588 ~ConstantDataSequential() { delete Next; }
590 static Constant *getImpl(StringRef Bytes, Type *Ty);
593 // allocate space for exactly zero operands.
594 void *operator new(size_t s) {
595 return User::operator new(s, 0);
599 /// isElementTypeCompatible - Return true if a ConstantDataSequential can be
600 /// formed with a vector or array of the specified element type.
601 /// ConstantDataArray only works with normal float and int types that are
602 /// stored densely in memory, not with things like i42 or x86_f80.
603 static bool isElementTypeCompatible(const Type *Ty);
605 /// getElementAsInteger - If this is a sequential container of integers (of
606 /// any size), return the specified element in the low bits of a uint64_t.
607 uint64_t getElementAsInteger(unsigned i) const;
609 /// getElementAsAPFloat - If this is a sequential container of floating point
610 /// type, return the specified element as an APFloat.
611 APFloat getElementAsAPFloat(unsigned i) const;
613 /// getElementAsFloat - If this is an sequential container of floats, return
614 /// the specified element as a float.
615 float getElementAsFloat(unsigned i) const;
617 /// getElementAsDouble - If this is an sequential container of doubles, return
618 /// the specified element as a double.
619 double getElementAsDouble(unsigned i) const;
621 /// getElementAsConstant - Return a Constant for a specified index's element.
622 /// Note that this has to compute a new constant to return, so it isn't as
623 /// efficient as getElementAsInteger/Float/Double.
624 Constant *getElementAsConstant(unsigned i) const;
626 /// getType - Specialize the getType() method to always return a
627 /// SequentialType, which reduces the amount of casting needed in parts of the
629 inline SequentialType *getType() const {
630 return reinterpret_cast<SequentialType*>(Value::getType());
633 /// getElementType - Return the element type of the array/vector.
634 Type *getElementType() const;
636 /// getNumElements - Return the number of elements in the array or vector.
637 unsigned getNumElements() const;
639 /// getElementByteSize - Return the size (in bytes) of each element in the
640 /// array/vector. The size of the elements is known to be a multiple of one
642 uint64_t getElementByteSize() const;
645 /// isString - This method returns true if this is an array of i8.
646 bool isString() const;
648 /// isCString - This method returns true if the array "isString", ends with a
649 /// nul byte, and does not contains any other nul bytes.
650 bool isCString() const;
652 /// getAsString - If this array is isString(), then this method returns the
653 /// array as a StringRef. Otherwise, it asserts out.
655 StringRef getAsString() const {
656 assert(isString() && "Not a string");
657 return getRawDataValues();
660 /// getAsCString - If this array is isCString(), then this method returns the
661 /// array (without the trailing null byte) as a StringRef. Otherwise, it
664 StringRef getAsCString() const {
665 assert(isCString() && "Isn't a C string");
666 StringRef Str = getAsString();
667 return Str.substr(0, Str.size()-1);
670 /// getRawDataValues - Return the raw, underlying, bytes of this data. Note
671 /// that this is an extremely tricky thing to work with, as it exposes the
672 /// host endianness of the data elements.
673 StringRef getRawDataValues() const;
675 virtual void destroyConstant();
677 /// Methods for support type inquiry through isa, cast, and dyn_cast:
679 static bool classof(const ConstantDataSequential *) { return true; }
680 static bool classof(const Value *V) {
681 return V->getValueID() == ConstantDataArrayVal ||
682 V->getValueID() == ConstantDataVectorVal;
685 const char *getElementPointer(unsigned Elt) const;
688 //===----------------------------------------------------------------------===//
689 /// ConstantDataArray - An array constant whose element type is a simple
690 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
691 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
692 /// operands because it stores all of the elements of the constant as densely
693 /// packed data, instead of as Value*'s.
694 class ConstantDataArray : public ConstantDataSequential {
695 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
696 ConstantDataArray(const ConstantDataArray &); // DO NOT IMPLEMENT
697 virtual void anchor();
698 friend class ConstantDataSequential;
699 explicit ConstantDataArray(Type *ty, const char *Data)
700 : ConstantDataSequential(ty, ConstantDataArrayVal, Data) {}
702 // allocate space for exactly zero operands.
703 void *operator new(size_t s) {
704 return User::operator new(s, 0);
708 /// get() constructors - Return a constant with array type with an element
709 /// count and element type matching the ArrayRef passed in. Note that this
710 /// can return a ConstantAggregateZero object.
711 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
712 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
713 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
714 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
715 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
716 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
718 /// getString - This method constructs a CDS and initializes it with a text
719 /// string. The default behavior (AddNull==true) causes a null terminator to
720 /// be placed at the end of the array (increasing the length of the string by
721 /// one more than the StringRef would normally indicate. Pass AddNull=false
722 /// to disable this behavior.
723 static Constant *getString(LLVMContext &Context, StringRef Initializer,
724 bool AddNull = true);
726 /// getType - Specialize the getType() method to always return an ArrayType,
727 /// which reduces the amount of casting needed in parts of the compiler.
729 inline ArrayType *getType() const {
730 return reinterpret_cast<ArrayType*>(Value::getType());
733 /// Methods for support type inquiry through isa, cast, and dyn_cast:
735 static bool classof(const ConstantDataArray *) { return true; }
736 static bool classof(const Value *V) {
737 return V->getValueID() == ConstantDataArrayVal;
741 //===----------------------------------------------------------------------===//
742 /// ConstantDataVector - A vector constant whose element type is a simple
743 /// 1/2/4/8-byte integer or float/double, and whose elements are just simple
744 /// data values (i.e. ConstantInt/ConstantFP). This Constant node has no
745 /// operands because it stores all of the elements of the constant as densely
746 /// packed data, instead of as Value*'s.
747 class ConstantDataVector : public ConstantDataSequential {
748 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
749 ConstantDataVector(const ConstantDataVector &); // DO NOT IMPLEMENT
750 virtual void anchor();
751 friend class ConstantDataSequential;
752 explicit ConstantDataVector(Type *ty, const char *Data)
753 : ConstantDataSequential(ty, ConstantDataVectorVal, Data) {}
755 // allocate space for exactly zero operands.
756 void *operator new(size_t s) {
757 return User::operator new(s, 0);
761 /// get() constructors - Return a constant with vector type with an element
762 /// count and element type matching the ArrayRef passed in. Note that this
763 /// can return a ConstantAggregateZero object.
764 static Constant *get(LLVMContext &Context, ArrayRef<uint8_t> Elts);
765 static Constant *get(LLVMContext &Context, ArrayRef<uint16_t> Elts);
766 static Constant *get(LLVMContext &Context, ArrayRef<uint32_t> Elts);
767 static Constant *get(LLVMContext &Context, ArrayRef<uint64_t> Elts);
768 static Constant *get(LLVMContext &Context, ArrayRef<float> Elts);
769 static Constant *get(LLVMContext &Context, ArrayRef<double> Elts);
771 /// getSplat - Return a ConstantVector with the specified constant in each
772 /// element. The specified constant has to be a of a compatible type (i8/i16/
773 /// i32/i64/float/double) and must be a ConstantFP or ConstantInt.
774 static Constant *getSplat(unsigned NumElts, Constant *Elt);
776 /// getSplatValue - If this is a splat constant, meaning that all of the
777 /// elements have the same value, return that value. Otherwise return NULL.
778 Constant *getSplatValue() const;
780 /// getType - Specialize the getType() method to always return a VectorType,
781 /// which reduces the amount of casting needed in parts of the compiler.
783 inline VectorType *getType() const {
784 return reinterpret_cast<VectorType*>(Value::getType());
787 /// Methods for support type inquiry through isa, cast, and dyn_cast:
789 static bool classof(const ConstantDataVector *) { return true; }
790 static bool classof(const Value *V) {
791 return V->getValueID() == ConstantDataVectorVal;
797 /// BlockAddress - The address of a basic block.
799 class BlockAddress : public Constant {
800 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
801 void *operator new(size_t s) { return User::operator new(s, 2); }
802 BlockAddress(Function *F, BasicBlock *BB);
804 /// get - Return a BlockAddress for the specified function and basic block.
805 static BlockAddress *get(Function *F, BasicBlock *BB);
807 /// get - Return a BlockAddress for the specified basic block. The basic
808 /// block must be embedded into a function.
809 static BlockAddress *get(BasicBlock *BB);
811 /// Transparently provide more efficient getOperand methods.
812 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
814 Function *getFunction() const { return (Function*)Op<0>().get(); }
815 BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
817 virtual void destroyConstant();
818 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
820 /// Methods for support type inquiry through isa, cast, and dyn_cast:
821 static inline bool classof(const BlockAddress *) { return true; }
822 static inline bool classof(const Value *V) {
823 return V->getValueID() == BlockAddressVal;
828 struct OperandTraits<BlockAddress> :
829 public FixedNumOperandTraits<BlockAddress, 2> {
832 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
835 //===----------------------------------------------------------------------===//
836 /// ConstantExpr - a constant value that is initialized with an expression using
837 /// other constant values.
839 /// This class uses the standard Instruction opcodes to define the various
840 /// constant expressions. The Opcode field for the ConstantExpr class is
841 /// maintained in the Value::SubclassData field.
842 class ConstantExpr : public Constant {
843 friend struct ConstantCreator<ConstantExpr,Type,
844 std::pair<unsigned, std::vector<Constant*> > >;
845 friend struct ConvertConstantType<ConstantExpr, Type>;
848 ConstantExpr(Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
849 : Constant(ty, ConstantExprVal, Ops, NumOps) {
850 // Operation type (an Instruction opcode) is stored as the SubclassData.
851 setValueSubclassData(Opcode);
855 // Static methods to construct a ConstantExpr of different kinds. Note that
856 // these methods may return a object that is not an instance of the
857 // ConstantExpr class, because they will attempt to fold the constant
858 // expression into something simpler if possible.
860 /// getAlignOf constant expr - computes the alignment of a type in a target
861 /// independent way (Note: the return type is an i64).
862 static Constant *getAlignOf(Type *Ty);
864 /// getSizeOf constant expr - computes the (alloc) size of a type (in
865 /// address-units, not bits) in a target independent way (Note: the return
868 static Constant *getSizeOf(Type *Ty);
870 /// getOffsetOf constant expr - computes the offset of a struct field in a
871 /// target independent way (Note: the return type is an i64).
873 static Constant *getOffsetOf(StructType *STy, unsigned FieldNo);
875 /// getOffsetOf constant expr - This is a generalized form of getOffsetOf,
876 /// which supports any aggregate type, and any Constant index.
878 static Constant *getOffsetOf(Type *Ty, Constant *FieldNo);
880 static Constant *getNeg(Constant *C, bool HasNUW = false, bool HasNSW =false);
881 static Constant *getFNeg(Constant *C);
882 static Constant *getNot(Constant *C);
883 static Constant *getAdd(Constant *C1, Constant *C2,
884 bool HasNUW = false, bool HasNSW = false);
885 static Constant *getFAdd(Constant *C1, Constant *C2);
886 static Constant *getSub(Constant *C1, Constant *C2,
887 bool HasNUW = false, bool HasNSW = false);
888 static Constant *getFSub(Constant *C1, Constant *C2);
889 static Constant *getMul(Constant *C1, Constant *C2,
890 bool HasNUW = false, bool HasNSW = false);
891 static Constant *getFMul(Constant *C1, Constant *C2);
892 static Constant *getUDiv(Constant *C1, Constant *C2, bool isExact = false);
893 static Constant *getSDiv(Constant *C1, Constant *C2, bool isExact = false);
894 static Constant *getFDiv(Constant *C1, Constant *C2);
895 static Constant *getURem(Constant *C1, Constant *C2);
896 static Constant *getSRem(Constant *C1, Constant *C2);
897 static Constant *getFRem(Constant *C1, Constant *C2);
898 static Constant *getAnd(Constant *C1, Constant *C2);
899 static Constant *getOr(Constant *C1, Constant *C2);
900 static Constant *getXor(Constant *C1, Constant *C2);
901 static Constant *getShl(Constant *C1, Constant *C2,
902 bool HasNUW = false, bool HasNSW = false);
903 static Constant *getLShr(Constant *C1, Constant *C2, bool isExact = false);
904 static Constant *getAShr(Constant *C1, Constant *C2, bool isExact = false);
905 static Constant *getTrunc (Constant *C, Type *Ty);
906 static Constant *getSExt (Constant *C, Type *Ty);
907 static Constant *getZExt (Constant *C, Type *Ty);
908 static Constant *getFPTrunc (Constant *C, Type *Ty);
909 static Constant *getFPExtend(Constant *C, Type *Ty);
910 static Constant *getUIToFP (Constant *C, Type *Ty);
911 static Constant *getSIToFP (Constant *C, Type *Ty);
912 static Constant *getFPToUI (Constant *C, Type *Ty);
913 static Constant *getFPToSI (Constant *C, Type *Ty);
914 static Constant *getPtrToInt(Constant *C, Type *Ty);
915 static Constant *getIntToPtr(Constant *C, Type *Ty);
916 static Constant *getBitCast (Constant *C, Type *Ty);
918 static Constant *getNSWNeg(Constant *C) { return getNeg(C, false, true); }
919 static Constant *getNUWNeg(Constant *C) { return getNeg(C, true, false); }
920 static Constant *getNSWAdd(Constant *C1, Constant *C2) {
921 return getAdd(C1, C2, false, true);
923 static Constant *getNUWAdd(Constant *C1, Constant *C2) {
924 return getAdd(C1, C2, true, false);
926 static Constant *getNSWSub(Constant *C1, Constant *C2) {
927 return getSub(C1, C2, false, true);
929 static Constant *getNUWSub(Constant *C1, Constant *C2) {
930 return getSub(C1, C2, true, false);
932 static Constant *getNSWMul(Constant *C1, Constant *C2) {
933 return getMul(C1, C2, false, true);
935 static Constant *getNUWMul(Constant *C1, Constant *C2) {
936 return getMul(C1, C2, true, false);
938 static Constant *getNSWShl(Constant *C1, Constant *C2) {
939 return getShl(C1, C2, false, true);
941 static Constant *getNUWShl(Constant *C1, Constant *C2) {
942 return getShl(C1, C2, true, false);
944 static Constant *getExactSDiv(Constant *C1, Constant *C2) {
945 return getSDiv(C1, C2, true);
947 static Constant *getExactUDiv(Constant *C1, Constant *C2) {
948 return getUDiv(C1, C2, true);
950 static Constant *getExactAShr(Constant *C1, Constant *C2) {
951 return getAShr(C1, C2, true);
953 static Constant *getExactLShr(Constant *C1, Constant *C2) {
954 return getLShr(C1, C2, true);
957 /// Transparently provide more efficient getOperand methods.
958 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
960 // @brief Convenience function for getting one of the casting operations
961 // using a CastOps opcode.
962 static Constant *getCast(
963 unsigned ops, ///< The opcode for the conversion
964 Constant *C, ///< The constant to be converted
965 Type *Ty ///< The type to which the constant is converted
968 // @brief Create a ZExt or BitCast cast constant expression
969 static Constant *getZExtOrBitCast(
970 Constant *C, ///< The constant to zext or bitcast
971 Type *Ty ///< The type to zext or bitcast C to
974 // @brief Create a SExt or BitCast cast constant expression
975 static Constant *getSExtOrBitCast(
976 Constant *C, ///< The constant to sext or bitcast
977 Type *Ty ///< The type to sext or bitcast C to
980 // @brief Create a Trunc or BitCast cast constant expression
981 static Constant *getTruncOrBitCast(
982 Constant *C, ///< The constant to trunc or bitcast
983 Type *Ty ///< The type to trunc or bitcast C to
986 /// @brief Create a BitCast or a PtrToInt cast constant expression
987 static Constant *getPointerCast(
988 Constant *C, ///< The pointer value to be casted (operand 0)
989 Type *Ty ///< The type to which cast should be made
992 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
993 static Constant *getIntegerCast(
994 Constant *C, ///< The integer constant to be casted
995 Type *Ty, ///< The integer type to cast to
996 bool isSigned ///< Whether C should be treated as signed or not
999 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
1000 static Constant *getFPCast(
1001 Constant *C, ///< The integer constant to be casted
1002 Type *Ty ///< The integer type to cast to
1005 /// @brief Return true if this is a convert constant expression
1006 bool isCast() const;
1008 /// @brief Return true if this is a compare constant expression
1009 bool isCompare() const;
1011 /// @brief Return true if this is an insertvalue or extractvalue expression,
1012 /// and the getIndices() method may be used.
1013 bool hasIndices() const;
1015 /// @brief Return true if this is a getelementptr expression and all
1016 /// the index operands are compile-time known integers within the
1017 /// corresponding notional static array extents. Note that this is
1018 /// not equivalant to, a subset of, or a superset of the "inbounds"
1020 bool isGEPWithNoNotionalOverIndexing() const;
1022 /// Select constant expr
1024 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2);
1026 /// get - Return a binary or shift operator constant expression,
1027 /// folding if possible.
1029 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2,
1030 unsigned Flags = 0);
1032 /// @brief Return an ICmp or FCmp comparison operator constant expression.
1033 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
1035 /// get* - Return some common constants without having to
1036 /// specify the full Instruction::OPCODE identifier.
1038 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
1039 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
1041 /// Getelementptr form. Value* is only accepted for convenience;
1042 /// all elements must be Constant's.
1044 static Constant *getGetElementPtr(Constant *C,
1045 ArrayRef<Constant *> IdxList,
1046 bool InBounds = false) {
1047 return getGetElementPtr(C, makeArrayRef((Value * const *)IdxList.data(),
1051 static Constant *getGetElementPtr(Constant *C,
1053 bool InBounds = false) {
1054 // This form of the function only exists to avoid ambiguous overload
1055 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1056 // ArrayRef<Value *>.
1057 return getGetElementPtr(C, cast<Value>(Idx), InBounds);
1059 static Constant *getGetElementPtr(Constant *C,
1060 ArrayRef<Value *> IdxList,
1061 bool InBounds = false);
1063 /// Create an "inbounds" getelementptr. See the documentation for the
1064 /// "inbounds" flag in LangRef.html for details.
1065 static Constant *getInBoundsGetElementPtr(Constant *C,
1066 ArrayRef<Constant *> IdxList) {
1067 return getGetElementPtr(C, IdxList, true);
1069 static Constant *getInBoundsGetElementPtr(Constant *C,
1071 // This form of the function only exists to avoid ambiguous overload
1072 // warnings about whether to convert Idx to ArrayRef<Constant *> or
1073 // ArrayRef<Value *>.
1074 return getGetElementPtr(C, Idx, true);
1076 static Constant *getInBoundsGetElementPtr(Constant *C,
1077 ArrayRef<Value *> IdxList) {
1078 return getGetElementPtr(C, IdxList, true);
1081 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
1082 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
1083 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
1084 static Constant *getExtractValue(Constant *Agg, ArrayRef<unsigned> Idxs);
1085 static Constant *getInsertValue(Constant *Agg, Constant *Val,
1086 ArrayRef<unsigned> Idxs);
1088 /// getOpcode - Return the opcode at the root of this constant expression
1089 unsigned getOpcode() const { return getSubclassDataFromValue(); }
1091 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
1092 /// not an ICMP or FCMP constant expression.
1093 unsigned getPredicate() const;
1095 /// getIndices - Assert that this is an insertvalue or exactvalue
1096 /// expression and return the list of indices.
1097 ArrayRef<unsigned> getIndices() const;
1099 /// getOpcodeName - Return a string representation for an opcode.
1100 const char *getOpcodeName() const;
1102 /// getWithOperandReplaced - Return a constant expression identical to this
1103 /// one, but with the specified operand set to the specified value.
1104 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
1106 /// getWithOperands - This returns the current constant expression with the
1107 /// operands replaced with the specified values. The specified array must
1108 /// have the same number of operands as our current one.
1109 Constant *getWithOperands(ArrayRef<Constant*> Ops) const {
1110 return getWithOperands(Ops, getType());
1113 /// getWithOperands - This returns the current constant expression with the
1114 /// operands replaced with the specified values and with the specified result
1115 /// type. The specified array must have the same number of operands as our
1117 Constant *getWithOperands(ArrayRef<Constant*> Ops, Type *Ty) const;
1119 virtual void destroyConstant();
1120 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
1122 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1123 static inline bool classof(const ConstantExpr *) { return true; }
1124 static inline bool classof(const Value *V) {
1125 return V->getValueID() == ConstantExprVal;
1129 // Shadow Value::setValueSubclassData with a private forwarding method so that
1130 // subclasses cannot accidentally use it.
1131 void setValueSubclassData(unsigned short D) {
1132 Value::setValueSubclassData(D);
1137 struct OperandTraits<ConstantExpr> :
1138 public VariadicOperandTraits<ConstantExpr, 1> {
1141 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant)
1143 //===----------------------------------------------------------------------===//
1144 /// UndefValue - 'undef' values are things that do not have specified contents.
1145 /// These are used for a variety of purposes, including global variable
1146 /// initializers and operands to instructions. 'undef' values can occur with
1147 /// any first-class type.
1149 /// Undef values aren't exactly constants; if they have multiple uses, they
1150 /// can appear to have different bit patterns at each use. See
1151 /// LangRef.html#undefvalues for details.
1153 class UndefValue : public Constant {
1154 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1155 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
1157 explicit UndefValue(Type *T) : Constant(T, UndefValueVal, 0, 0) {}
1159 // allocate space for exactly zero operands
1160 void *operator new(size_t s) {
1161 return User::operator new(s, 0);
1164 /// get() - Static factory methods - Return an 'undef' object of the specified
1167 static UndefValue *get(Type *T);
1169 /// getSequentialElement - If this Undef has array or vector type, return a
1170 /// undef with the right element type.
1171 UndefValue *getSequentialElement() const;
1173 /// getStructElement - If this undef has struct type, return a undef with the
1174 /// right element type for the specified element.
1175 UndefValue *getStructElement(unsigned Elt) const;
1177 /// getElementValue - Return an undef of the right value for the specified GEP
1179 UndefValue *getElementValue(Constant *C) const;
1181 /// getElementValue - Return an undef of the right value for the specified GEP
1183 UndefValue *getElementValue(unsigned Idx) const;
1185 virtual void destroyConstant();
1187 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1188 static inline bool classof(const UndefValue *) { return true; }
1189 static bool classof(const Value *V) {
1190 return V->getValueID() == UndefValueVal;
1194 } // End llvm namespace