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 //===----------------------------------------------------------------------===//
10 /// @file This file contains the declarations for the subclasses of Constant,
11 /// which represent the different flavors of constant values that live in LLVM.
12 /// Note that Constants are immutable (once created they never change) and are
13 /// fully shared by structural equivalence. This means that two structurally
14 /// equivalent constants will always have the same address. Constant's are
15 /// created on demand as needed and never deleted: thus clients don't have to
16 /// worry about the lifetime of the objects.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CONSTANTS_H
21 #define LLVM_CONSTANTS_H
23 #include "llvm/Constant.h"
24 #include "llvm/Type.h"
25 #include "llvm/OperandTraits.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/APFloat.h"
36 template<class ConstantClass, class TypeClass, class ValType>
37 struct ConstantCreator;
38 template<class ConstantClass, class TypeClass>
39 struct ConvertConstantType;
41 //===----------------------------------------------------------------------===//
42 /// This is the shared class of boolean and integer constants. This class
43 /// represents both boolean and integral constants.
44 /// @brief Class for constant integers.
45 class ConstantInt : public Constant {
46 static ConstantInt *TheTrueVal, *TheFalseVal;
47 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
48 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
49 ConstantInt(const IntegerType *Ty, const APInt& V);
52 // allocate space for exactly zero operands
53 void *operator new(size_t s) {
54 return User::operator new(s, 0);
57 /// Return the constant as an APInt value reference. This allows clients to
58 /// obtain a copy of the value, with all its precision in tact.
59 /// @brief Return the constant's value.
60 inline const APInt& getValue() const {
64 /// getBitWidth - Return the bitwidth of this constant.
65 unsigned getBitWidth() const { return Val.getBitWidth(); }
67 /// Return the constant as a 64-bit unsigned integer value after it
68 /// has been zero extended as appropriate for the type of this constant. Note
69 /// that this method can assert if the value does not fit in 64 bits.
71 /// @brief Return the zero extended value.
72 inline uint64_t getZExtValue() const {
73 return Val.getZExtValue();
76 /// Return the constant as a 64-bit integer value after it has been sign
77 /// sign extended as appropriate for the type of this constant. Note that
78 /// this method can assert if the value does not fit in 64 bits.
80 /// @brief Return the sign extended value.
81 inline int64_t getSExtValue() const {
82 return Val.getSExtValue();
85 /// A helper method that can be used to determine if the constant contained
86 /// within is equal to a constant. This only works for very small values,
87 /// because this is all that can be represented with all types.
88 /// @brief Determine if this constant's value is same as an unsigned char.
89 bool equalsInt(uint64_t V) const {
93 /// getTrue/getFalse - Return the singleton true/false values.
94 static inline ConstantInt *getTrue() {
95 if (TheTrueVal) return TheTrueVal;
96 return CreateTrueFalseVals(true);
98 static inline ConstantInt *getFalse() {
99 if (TheFalseVal) return TheFalseVal;
100 return CreateTrueFalseVals(false);
103 /// Return a ConstantInt with the specified value for the specified type. The
104 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
105 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
106 /// signed value for the type Ty.
107 /// @brief Get a ConstantInt for a specific value.
108 static ConstantInt *get(const Type *Ty, uint64_t V, bool isSigned = false);
110 /// Return a ConstantInt with the specified value and an implied Type. The
111 /// type is the integer type that corresponds to the bit width of the value.
112 static ConstantInt *get(const APInt &V);
114 /// getType - Specialize the getType() method to always return an IntegerType,
115 /// which reduces the amount of casting needed in parts of the compiler.
117 inline const IntegerType *getType() const {
118 return reinterpret_cast<const IntegerType*>(Value::getType());
121 /// This static method returns true if the type Ty is big enough to
122 /// represent the value V. This can be used to avoid having the get method
123 /// assert when V is larger than Ty can represent. Note that there are two
124 /// versions of this method, one for unsigned and one for signed integers.
125 /// Although ConstantInt canonicalizes everything to an unsigned integer,
126 /// the signed version avoids callers having to convert a signed quantity
127 /// to the appropriate unsigned type before calling the method.
128 /// @returns true if V is a valid value for type Ty
129 /// @brief Determine if the value is in range for the given type.
130 static bool isValueValidForType(const Type *Ty, uint64_t V);
131 static bool isValueValidForType(const Type *Ty, int64_t V);
133 /// This function will return true iff this constant represents the "null"
134 /// value that would be returned by the getNullValue method.
135 /// @returns true if this is the null integer value.
136 /// @brief Determine if the value is null.
137 virtual bool isNullValue() const {
141 /// This is just a convenience method to make client code smaller for a
142 /// common code. It also correctly performs the comparison without the
143 /// potential for an assertion from getZExtValue().
144 bool isZero() const {
148 /// This is just a convenience method to make client code smaller for a
149 /// common case. It also correctly performs the comparison without the
150 /// potential for an assertion from getZExtValue().
151 /// @brief Determine if the value is one.
156 /// This function will return true iff every bit in this constant is set
158 /// @returns true iff this constant's bits are all set to true.
159 /// @brief Determine if the value is all ones.
160 bool isAllOnesValue() const {
161 return Val.isAllOnesValue();
164 /// This function will return true iff this constant represents the largest
165 /// value that may be represented by the constant's type.
166 /// @returns true iff this is the largest value that may be represented
168 /// @brief Determine if the value is maximal.
169 bool isMaxValue(bool isSigned) const {
171 return Val.isMaxSignedValue();
173 return Val.isMaxValue();
176 /// This function will return true iff this constant represents the smallest
177 /// value that may be represented by this constant's type.
178 /// @returns true if this is the smallest value that may be represented by
180 /// @brief Determine if the value is minimal.
181 bool isMinValue(bool isSigned) const {
183 return Val.isMinSignedValue();
185 return Val.isMinValue();
188 /// This function will return true iff this constant represents a value with
189 /// active bits bigger than 64 bits or a value greater than the given uint64_t
191 /// @returns true iff this constant is greater or equal to the given number.
192 /// @brief Determine if the value is greater or equal to the given number.
193 bool uge(uint64_t Num) {
194 return Val.getActiveBits() > 64 || Val.getZExtValue() >= Num;
197 /// @returns the 64-bit value of this constant if its active bits number is
198 /// not greater than 64, otherwise, just return the given uint64_t number.
199 /// @brief Get the constant's value if possible.
200 uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
201 return Val.getLimitedValue(Limit);
204 /// @returns the value for an integer constant of the given type that has all
205 /// its bits set to true.
206 /// @brief Get the all ones value
207 static ConstantInt *getAllOnesValue(const Type *Ty);
209 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
210 static inline bool classof(const ConstantInt *) { return true; }
211 static bool classof(const Value *V) {
212 return V->getValueID() == ConstantIntVal;
214 static void ResetTrueFalse() { TheTrueVal = TheFalseVal = 0; }
216 static ConstantInt *CreateTrueFalseVals(bool WhichOne);
220 //===----------------------------------------------------------------------===//
221 /// ConstantFP - Floating Point Values [float, double]
223 class ConstantFP : public Constant {
225 void *operator new(size_t, unsigned);// DO NOT IMPLEMENT
226 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
228 ConstantFP(const Type *Ty, const APFloat& V);
230 // allocate space for exactly zero operands
231 void *operator new(size_t s) {
232 return User::operator new(s, 0);
235 /// get() - Static factory methods - Return objects of the specified value
236 static ConstantFP *get(const APFloat &V);
238 /// get() - This returns a constant fp for the specified value in the
239 /// specified type. This should only be used for simple constant values like
240 /// 2.0/1.0 etc, that are known-valid both as double and as the target format.
241 static ConstantFP *get(const Type *Ty, double V);
243 /// isValueValidForType - return true if Ty is big enough to represent V.
244 static bool isValueValidForType(const Type *Ty, const APFloat& V);
245 inline const APFloat& getValueAPF() const { return Val; }
247 /// isNullValue - Return true if this is the value that would be returned by
248 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
249 /// considers -0.0 to be null as well as 0.0. :(
250 virtual bool isNullValue() const;
252 // Get a negative zero.
253 static ConstantFP *getNegativeZero(const Type* Ty);
255 /// isExactlyValue - We don't rely on operator== working on double values, as
256 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
257 /// As such, this method can be used to do an exact bit-for-bit comparison of
258 /// two floating point values. The version with a double operand is retained
259 /// because it's so convenient to write isExactlyValue(2.0), but please use
260 /// it only for simple constants.
261 bool isExactlyValue(const APFloat& V) const;
263 bool isExactlyValue(double V) const {
264 // convert is not supported on this type
265 if (&Val.getSemantics() == &APFloat::PPCDoubleDouble)
268 FV.convert(Val.getSemantics(), APFloat::rmNearestTiesToEven);
269 return isExactlyValue(FV);
271 /// Methods for support type inquiry through isa, cast, and dyn_cast:
272 static inline bool classof(const ConstantFP *) { return true; }
273 static bool classof(const Value *V) {
274 return V->getValueID() == ConstantFPVal;
278 //===----------------------------------------------------------------------===//
279 /// ConstantAggregateZero - All zero aggregate value
281 class ConstantAggregateZero : public Constant {
282 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
283 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
284 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
286 explicit ConstantAggregateZero(const Type *ty)
287 : Constant(ty, ConstantAggregateZeroVal, 0, 0) {}
289 // allocate space for exactly zero operands
290 void *operator new(size_t s) {
291 return User::operator new(s, 0);
294 /// get() - static factory method for creating a null aggregate. It is
295 /// illegal to call this method with a non-aggregate type.
296 static Constant *get(const Type *Ty);
298 /// isNullValue - Return true if this is the value that would be returned by
300 virtual bool isNullValue() const { return true; }
302 virtual void destroyConstant();
304 /// Methods for support type inquiry through isa, cast, and dyn_cast:
306 static bool classof(const ConstantAggregateZero *) { return true; }
307 static bool classof(const Value *V) {
308 return V->getValueID() == ConstantAggregateZeroVal;
313 //===----------------------------------------------------------------------===//
314 /// ConstantArray - Constant Array Declarations
316 class ConstantArray : public Constant {
317 friend struct ConstantCreator<ConstantArray, ArrayType,
318 std::vector<Constant*> >;
319 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
321 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
323 /// get() - Static factory methods - Return objects of the specified value
324 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
325 static Constant *get(const ArrayType *T,
326 Constant*const*Vals, unsigned NumVals) {
327 // FIXME: make this the primary ctor method.
328 return get(T, std::vector<Constant*>(Vals, Vals+NumVals));
331 /// This method constructs a ConstantArray and initializes it with a text
332 /// string. The default behavior (AddNull==true) causes a null terminator to
333 /// be placed at the end of the array. This effectively increases the length
334 /// of the array by one (you've been warned). However, in some situations
335 /// this is not desired so if AddNull==false then the string is copied without
336 /// null termination.
337 static Constant *get(const std::string &Initializer, bool AddNull = true);
339 /// Transparently provide more efficient getOperand methods.
340 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
342 /// getType - Specialize the getType() method to always return an ArrayType,
343 /// which reduces the amount of casting needed in parts of the compiler.
345 inline const ArrayType *getType() const {
346 return reinterpret_cast<const ArrayType*>(Value::getType());
349 /// isString - This method returns true if the array is an array of i8 and
350 /// the elements of the array are all ConstantInt's.
351 bool isString() const;
353 /// isCString - This method returns true if the array is a string (see
355 /// isString) and it ends in a null byte \0 and does not contains any other
357 /// null bytes except its terminator.
358 bool isCString() const;
360 /// getAsString - If this array is isString(), then this method converts the
361 /// array to an std::string and returns it. Otherwise, it asserts out.
363 std::string getAsString() const;
365 /// isNullValue - Return true if this is the value that would be returned by
366 /// getNullValue. This always returns false because zero arrays are always
367 /// created as ConstantAggregateZero objects.
368 virtual bool isNullValue() const { return false; }
370 virtual void destroyConstant();
371 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
373 /// Methods for support type inquiry through isa, cast, and dyn_cast:
374 static inline bool classof(const ConstantArray *) { return true; }
375 static bool classof(const Value *V) {
376 return V->getValueID() == ConstantArrayVal;
381 struct OperandTraits<ConstantArray> : VariadicOperandTraits<> {
384 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantArray, Constant)
386 //===----------------------------------------------------------------------===//
387 // ConstantStruct - Constant Struct Declarations
389 class ConstantStruct : public Constant {
390 friend struct ConstantCreator<ConstantStruct, StructType,
391 std::vector<Constant*> >;
392 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
394 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
396 /// get() - Static factory methods - Return objects of the specified value
398 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
399 static Constant *get(const std::vector<Constant*> &V, bool Packed = false);
400 static Constant *get(Constant*const* Vals, unsigned NumVals,
401 bool Packed = false) {
402 // FIXME: make this the primary ctor method.
403 return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
406 /// Transparently provide more efficient getOperand methods.
407 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
409 /// getType() specialization - Reduce amount of casting...
411 inline const StructType *getType() const {
412 return reinterpret_cast<const StructType*>(Value::getType());
415 /// isNullValue - Return true if this is the value that would be returned by
416 /// getNullValue. This always returns false because zero structs are always
417 /// created as ConstantAggregateZero objects.
418 virtual bool isNullValue() const {
422 virtual void destroyConstant();
423 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
425 /// Methods for support type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const ConstantStruct *) { return true; }
427 static bool classof(const Value *V) {
428 return V->getValueID() == ConstantStructVal;
433 struct OperandTraits<ConstantStruct> : VariadicOperandTraits<> {
436 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantStruct, Constant)
438 //===----------------------------------------------------------------------===//
439 /// ConstantVector - Constant Vector Declarations
441 class ConstantVector : public Constant {
442 friend struct ConstantCreator<ConstantVector, VectorType,
443 std::vector<Constant*> >;
444 ConstantVector(const ConstantVector &); // DO NOT IMPLEMENT
446 ConstantVector(const VectorType *T, const std::vector<Constant*> &Val);
448 /// get() - Static factory methods - Return objects of the specified value
449 static Constant *get(const VectorType *T, const std::vector<Constant*> &);
450 static Constant *get(const std::vector<Constant*> &V);
451 static Constant *get(Constant*const* Vals, unsigned NumVals) {
452 // FIXME: make this the primary ctor method.
453 return get(std::vector<Constant*>(Vals, Vals+NumVals));
456 /// Transparently provide more efficient getOperand methods.
457 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
459 /// getType - Specialize the getType() method to always return a VectorType,
460 /// which reduces the amount of casting needed in parts of the compiler.
462 inline const VectorType *getType() const {
463 return reinterpret_cast<const VectorType*>(Value::getType());
466 /// @returns the value for a vector integer constant of the given type that
467 /// has all its bits set to true.
468 /// @brief Get the all ones value
469 static ConstantVector *getAllOnesValue(const VectorType *Ty);
471 /// isNullValue - Return true if this is the value that would be returned by
472 /// getNullValue. This always returns false because zero vectors are always
473 /// created as ConstantAggregateZero objects.
474 virtual bool isNullValue() const { return false; }
476 /// This function will return true iff every element in this vector constant
477 /// is set to all ones.
478 /// @returns true iff this constant's emements are all set to all ones.
479 /// @brief Determine if the value is all ones.
480 bool isAllOnesValue() const;
482 /// getSplatValue - If this is a splat constant, meaning that all of the
483 /// elements have the same value, return that value. Otherwise return NULL.
484 Constant *getSplatValue();
486 virtual void destroyConstant();
487 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
489 /// Methods for support type inquiry through isa, cast, and dyn_cast:
490 static inline bool classof(const ConstantVector *) { return true; }
491 static bool classof(const Value *V) {
492 return V->getValueID() == ConstantVectorVal;
497 struct OperandTraits<ConstantVector> : VariadicOperandTraits<> {
500 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantVector, Constant)
502 //===----------------------------------------------------------------------===//
503 /// ConstantPointerNull - a constant pointer value that points to null
505 class ConstantPointerNull : public Constant {
506 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
507 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
508 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
510 explicit ConstantPointerNull(const PointerType *T)
511 : Constant(reinterpret_cast<const Type*>(T),
512 Value::ConstantPointerNullVal, 0, 0) {}
515 // allocate space for exactly zero operands
516 void *operator new(size_t s) {
517 return User::operator new(s, 0);
520 /// get() - Static factory methods - Return objects of the specified value
521 static ConstantPointerNull *get(const PointerType *T);
523 /// isNullValue - Return true if this is the value that would be returned by
525 virtual bool isNullValue() const { return true; }
527 virtual void destroyConstant();
529 /// getType - Specialize the getType() method to always return an PointerType,
530 /// which reduces the amount of casting needed in parts of the compiler.
532 inline const PointerType *getType() const {
533 return reinterpret_cast<const PointerType*>(Value::getType());
536 /// Methods for support type inquiry through isa, cast, and dyn_cast:
537 static inline bool classof(const ConstantPointerNull *) { return true; }
538 static bool classof(const Value *V) {
539 return V->getValueID() == ConstantPointerNullVal;
544 /// ConstantExpr - a constant value that is initialized with an expression using
545 /// other constant values.
547 /// This class uses the standard Instruction opcodes to define the various
548 /// constant expressions. The Opcode field for the ConstantExpr class is
549 /// maintained in the Value::SubclassData field.
550 class ConstantExpr : public Constant {
551 friend struct ConstantCreator<ConstantExpr,Type,
552 std::pair<unsigned, std::vector<Constant*> > >;
553 friend struct ConvertConstantType<ConstantExpr, Type>;
556 ConstantExpr(const Type *ty, unsigned Opcode, Use *Ops, unsigned NumOps)
557 : Constant(ty, ConstantExprVal, Ops, NumOps) {
558 // Operation type (an Instruction opcode) is stored as the SubclassData.
559 SubclassData = Opcode;
562 // These private methods are used by the type resolution code to create
563 // ConstantExprs in intermediate forms.
564 static Constant *getTy(const Type *Ty, unsigned Opcode,
565 Constant *C1, Constant *C2);
566 static Constant *getCompareTy(unsigned short pred, Constant *C1,
568 static Constant *getSelectTy(const Type *Ty,
569 Constant *C1, Constant *C2, Constant *C3);
570 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
571 Value* const *Idxs, unsigned NumIdxs);
572 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
574 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
575 Constant *Elt, Constant *Idx);
576 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
577 Constant *V2, Constant *Mask);
578 static Constant *getExtractValueTy(const Type *Ty, Constant *Agg,
579 Constant * const *Idxs, unsigned NumIdxs);
580 static Constant *getInsertValueTy(const Type *Ty, Constant *Agg,
582 Constant * const *Idxs, unsigned NumIdxs);
585 // Static methods to construct a ConstantExpr of different kinds. Note that
586 // these methods may return a object that is not an instance of the
587 // ConstantExpr class, because they will attempt to fold the constant
588 // expression into something simpler if possible.
590 /// Cast constant expr
592 static Constant *getTrunc (Constant *C, const Type *Ty);
593 static Constant *getSExt (Constant *C, const Type *Ty);
594 static Constant *getZExt (Constant *C, const Type *Ty);
595 static Constant *getFPTrunc (Constant *C, const Type *Ty);
596 static Constant *getFPExtend(Constant *C, const Type *Ty);
597 static Constant *getUIToFP (Constant *C, const Type *Ty);
598 static Constant *getSIToFP (Constant *C, const Type *Ty);
599 static Constant *getFPToUI (Constant *C, const Type *Ty);
600 static Constant *getFPToSI (Constant *C, const Type *Ty);
601 static Constant *getPtrToInt(Constant *C, const Type *Ty);
602 static Constant *getIntToPtr(Constant *C, const Type *Ty);
603 static Constant *getBitCast (Constant *C, const Type *Ty);
605 /// Transparently provide more efficient getOperand methods.
606 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
608 // @brief Convenience function for getting one of the casting operations
609 // using a CastOps opcode.
610 static Constant *getCast(
611 unsigned ops, ///< The opcode for the conversion
612 Constant *C, ///< The constant to be converted
613 const Type *Ty ///< The type to which the constant is converted
616 // @brief Create a ZExt or BitCast cast constant expression
617 static Constant *getZExtOrBitCast(
618 Constant *C, ///< The constant to zext or bitcast
619 const Type *Ty ///< The type to zext or bitcast C to
622 // @brief Create a SExt or BitCast cast constant expression
623 static Constant *getSExtOrBitCast(
624 Constant *C, ///< The constant to sext or bitcast
625 const Type *Ty ///< The type to sext or bitcast C to
628 // @brief Create a Trunc or BitCast cast constant expression
629 static Constant *getTruncOrBitCast(
630 Constant *C, ///< The constant to trunc or bitcast
631 const Type *Ty ///< The type to trunc or bitcast C to
634 /// @brief Create a BitCast or a PtrToInt cast constant expression
635 static Constant *getPointerCast(
636 Constant *C, ///< The pointer value to be casted (operand 0)
637 const Type *Ty ///< The type to which cast should be made
640 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
641 static Constant *getIntegerCast(
642 Constant *C, ///< The integer constant to be casted
643 const Type *Ty, ///< The integer type to cast to
644 bool isSigned ///< Whether C should be treated as signed or not
647 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
648 static Constant *getFPCast(
649 Constant *C, ///< The integer constant to be casted
650 const Type *Ty ///< The integer type to cast to
653 /// @brief Return true if this is a convert constant expression
656 /// @brief Return true if this is a compare constant expression
657 bool isCompare() const;
659 /// Select constant expr
661 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
662 return getSelectTy(V1->getType(), C, V1, V2);
665 /// getSizeOf constant expr - computes the size of a type in a target
666 /// independent way (Note: the return type is an i64).
668 static Constant *getSizeOf(const Type *Ty);
670 /// ConstantExpr::get - Return a binary or shift operator constant expression,
671 /// folding if possible.
673 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
675 /// @brief Return an ICmp or FCmp comparison operator constant expression.
676 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
678 /// ConstantExpr::get* - Return some common constants without having to
679 /// specify the full Instruction::OPCODE identifier.
681 static Constant *getNeg(Constant *C);
682 static Constant *getNot(Constant *C);
683 static Constant *getAdd(Constant *C1, Constant *C2);
684 static Constant *getSub(Constant *C1, Constant *C2);
685 static Constant *getMul(Constant *C1, Constant *C2);
686 static Constant *getUDiv(Constant *C1, Constant *C2);
687 static Constant *getSDiv(Constant *C1, Constant *C2);
688 static Constant *getFDiv(Constant *C1, Constant *C2);
689 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
690 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
691 static Constant *getFRem(Constant *C1, Constant *C2);
692 static Constant *getAnd(Constant *C1, Constant *C2);
693 static Constant *getOr(Constant *C1, Constant *C2);
694 static Constant *getXor(Constant *C1, Constant *C2);
695 static Constant *getICmp(unsigned short pred, Constant *LHS, Constant *RHS);
696 static Constant *getFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
697 static Constant *getVICmp(unsigned short pred, Constant *LHS, Constant *RHS);
698 static Constant *getVFCmp(unsigned short pred, Constant *LHS, Constant *RHS);
699 static Constant *getShl(Constant *C1, Constant *C2);
700 static Constant *getLShr(Constant *C1, Constant *C2);
701 static Constant *getAShr(Constant *C1, Constant *C2);
703 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
704 /// all elements must be Constant's.
706 static Constant *getGetElementPtr(Constant *C,
707 Constant* const *IdxList, unsigned NumIdx);
708 static Constant *getGetElementPtr(Constant *C,
709 Value* const *IdxList, unsigned NumIdx);
711 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
712 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
713 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
714 static Constant *getExtractValue(Constant *Agg,
715 Constant* const *IdxList, unsigned NumIdx);
716 static Constant *getInsertValue(Constant *Agg, Constant *Val,
717 Constant* const *IdxList, unsigned NumIdx);
719 /// Floating point negation must be implemented with f(x) = -0.0 - x. This
720 /// method returns the negative zero constant for floating point or vector
721 /// floating point types; for all other types, it returns the null value.
722 static Constant *getZeroValueForNegationExpr(const Type *Ty);
724 /// isNullValue - Return true if this is the value that would be returned by
726 virtual bool isNullValue() const { return false; }
728 /// getOpcode - Return the opcode at the root of this constant expression
729 unsigned getOpcode() const { return SubclassData; }
731 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
732 /// not an ICMP or FCMP constant expression.
733 unsigned getPredicate() const;
735 /// getOpcodeName - Return a string representation for an opcode.
736 const char *getOpcodeName() const;
738 /// getWithOperandReplaced - Return a constant expression identical to this
739 /// one, but with the specified operand set to the specified value.
740 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
742 /// getWithOperands - This returns the current constant expression with the
743 /// operands replaced with the specified values. The specified operands must
744 /// match count and type with the existing ones.
745 Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
747 virtual void destroyConstant();
748 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
750 /// Methods for support type inquiry through isa, cast, and dyn_cast:
751 static inline bool classof(const ConstantExpr *) { return true; }
752 static inline bool classof(const Value *V) {
753 return V->getValueID() == ConstantExprVal;
758 struct OperandTraits<ConstantExpr> : VariadicOperandTraits<1> {
761 DEFINE_TRANSPARENT_CASTED_OPERAND_ACCESSORS(ConstantExpr, Constant)
763 //===----------------------------------------------------------------------===//
764 /// UndefValue - 'undef' values are things that do not have specified contents.
765 /// These are used for a variety of purposes, including global variable
766 /// initializers and operands to instructions. 'undef' values can occur with
769 class UndefValue : public Constant {
770 friend struct ConstantCreator<UndefValue, Type, char>;
771 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
772 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
774 explicit UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
776 // allocate space for exactly zero operands
777 void *operator new(size_t s) {
778 return User::operator new(s, 0);
781 /// get() - Static factory methods - Return an 'undef' object of the specified
784 static UndefValue *get(const Type *T);
786 /// isNullValue - Return true if this is the value that would be returned by
788 virtual bool isNullValue() const { return false; }
790 virtual void destroyConstant();
792 /// Methods for support type inquiry through isa, cast, and dyn_cast:
793 static inline bool classof(const UndefValue *) { return true; }
794 static bool classof(const Value *V) {
795 return V->getValueID() == UndefValueVal;
799 } // End llvm namespace