1 //===-- llvm/Constants.h - Constant class subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
33 template<class ConstantClass, class TypeClass, class ValType>
34 struct ConstantCreator;
35 template<class ConstantClass, class TypeClass>
36 struct ConvertConstantType;
38 //===----------------------------------------------------------------------===//
39 /// This is the shared class of boolean and integrer constants. This class
40 /// represents both boolean and integral constants.
41 /// @brief Class for constant integers.
42 class ConstantInt : public Constant {
46 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
47 ConstantInt(const Type *Ty, uint64_t V);
48 ConstantInt(const Type *Ty, int64_t V);
50 friend struct ConstantCreator<ConstantInt, Type, uint64_t>;
52 /// Return the constant as a 64-bit unsigned integer value after it
53 /// has been zero extended as appropriate for the type of this constant.
54 /// @brief Return the zero extended value.
55 inline uint64_t getZExtValue() const {
59 /// Return the constant as a 64-bit integer value after it has been sign
60 /// sign extended as appropriate for the type of this constant.
61 /// @brief Return the sign extended value.
62 inline int64_t getSExtValue() const {
63 unsigned Size = getType()->getPrimitiveSizeInBits();
64 return (int64_t(Val) << (64-Size)) >> (64-Size);
66 /// A helper method that can be used to determine if the constant contained
67 /// within is equal to a constant. This only works for very small values,
68 /// because this is all that can be represented with all types.
69 /// @brief Determine if this constant's value is same as an unsigned char.
70 bool equalsInt(unsigned char V) const {
72 "equalsInt: Can only be used with very small positive constants!");
76 /// getTrue/getFalse - Return the singleton true/false values.
77 static inline ConstantInt *getTrue() {
78 static ConstantInt *T = 0;
80 return T = new ConstantInt(true);
82 static inline ConstantInt *getFalse() {
83 static ConstantInt *F = 0;
85 return F = new ConstantInt(false);
88 /// Return a ConstantInt with the specified value for the specified type. The
89 /// value V will be canonicalized to a uint64_t but accessing it with either
90 /// getSExtValue() or getZExtValue() (ConstantInt) will yield the correct
91 /// sized/signed value for the type Ty.
92 /// @brief Get a ConstantInt for a specific value.
93 static ConstantInt *get(const Type *Ty, int64_t V);
95 /// This static method returns true if the type Ty is big enough to
96 /// represent the value V. This can be used to avoid having the get method
97 /// assert when V is larger than Ty can represent. Note that there are two
98 /// versions of this method, one for unsigned and one for signed integers.
99 /// Although ConstantInt canonicalizes everything to an unsigned integer,
100 /// the signed version avoids callers having to convert a signed quantity
101 /// to the appropriate unsigned type before calling the method.
102 /// @returns true if V is a valid value for type Ty
103 /// @brief Determine if the value is in range for the given type.
104 static bool isValueValidForType(const Type *Ty, uint64_t V);
105 static bool isValueValidForType(const Type *Ty, int64_t V);
107 /// This function will return true iff this constant represents the "null"
108 /// value that would be returned by the getNullValue method.
109 /// @returns true if this is the null integer value.
110 /// @brief Determine if the value is null.
111 virtual bool isNullValue() const {
115 /// This function will return true iff every bit in this constant is set
117 /// @returns true iff this constant's bits are all set to true.
118 /// @brief Determine if the value is all ones.
119 bool isAllOnesValue() const {
120 return getSExtValue() == -1;
123 /// This function will return true iff this constant represents the largest
124 /// value that may be represented by the constant's type.
125 /// @returns true iff this is the largest value that may be represented
127 /// @brief Determine if the value is maximal.
128 bool isMaxValue(bool isSigned) const {
130 int64_t V = getSExtValue();
131 if (V < 0) return false; // Be careful about wrap-around on 'long's
133 return !isValueValidForType(getType(), V) || V < 0;
135 return isAllOnesValue();
138 /// This function will return true iff this constant represents the smallest
139 /// value that may be represented by this constant's type.
140 /// @returns true if this is the smallest value that may be represented by
142 /// @brief Determine if the value is minimal.
143 bool isMinValue(bool isSigned) const {
145 int64_t V = getSExtValue();
146 if (V > 0) return false; // Be careful about wrap-around on 'long's
148 return !isValueValidForType(getType(), V) || V > 0;
150 return getZExtValue() == 0;
153 /// @returns the value for an integer constant of the given type that has all
154 /// its bits set to true.
155 /// @brief Get the all ones value
156 static ConstantInt *getAllOnesValue(const Type *Ty);
158 /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
159 static inline bool classof(const ConstantInt *) { return true; }
160 static bool classof(const Value *V) {
161 return V->getValueType() == ConstantIntVal;
166 //===----------------------------------------------------------------------===//
167 /// ConstantFP - Floating Point Values [float, double]
169 class ConstantFP : public Constant {
171 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
172 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
173 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
175 ConstantFP(const Type *Ty, double V);
177 /// get() - Static factory methods - Return objects of the specified value
178 static ConstantFP *get(const Type *Ty, double V);
180 /// isValueValidForType - return true if Ty is big enough to represent V.
181 static bool isValueValidForType(const Type *Ty, double V);
182 inline double getValue() const { return Val; }
184 /// isNullValue - Return true if this is the value that would be returned by
185 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
186 /// considers -0.0 to be null as well as 0.0. :(
187 virtual bool isNullValue() const;
189 /// isExactlyValue - We don't rely on operator== working on double values, as
190 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
191 /// As such, this method can be used to do an exact bit-for-bit comparison of
192 /// two floating point values.
193 bool isExactlyValue(double V) const;
195 /// Methods for support type inquiry through isa, cast, and dyn_cast:
196 static inline bool classof(const ConstantFP *) { return true; }
197 static bool classof(const Value *V) {
198 return V->getValueType() == ConstantFPVal;
202 //===----------------------------------------------------------------------===//
203 /// ConstantAggregateZero - All zero aggregate value
205 class ConstantAggregateZero : public Constant {
206 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
207 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
209 ConstantAggregateZero(const Type *Ty)
210 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
212 /// get() - static factory method for creating a null aggregate. It is
213 /// illegal to call this method with a non-aggregate type.
214 static Constant *get(const Type *Ty);
216 /// isNullValue - Return true if this is the value that would be returned by
218 virtual bool isNullValue() const { return true; }
220 virtual void destroyConstant();
222 /// Methods for support type inquiry through isa, cast, and dyn_cast:
224 static bool classof(const ConstantAggregateZero *) { return true; }
225 static bool classof(const Value *V) {
226 return V->getValueType() == ConstantAggregateZeroVal;
231 //===----------------------------------------------------------------------===//
232 /// ConstantArray - Constant Array Declarations
234 class ConstantArray : public Constant {
235 friend struct ConstantCreator<ConstantArray, ArrayType,
236 std::vector<Constant*> >;
237 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
239 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
242 /// get() - Static factory methods - Return objects of the specified value
243 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
245 /// This method constructs a ConstantArray and initializes it with a text
246 /// string. The default behavior (AddNull==true) causes a null terminator to
247 /// be placed at the end of the array. This effectively increases the length
248 /// of the array by one (you've been warned). However, in some situations
249 /// this is not desired so if AddNull==false then the string is copied without
250 /// null termination.
251 static Constant *get(const std::string &Initializer, bool AddNull = true);
253 /// getType - Specialize the getType() method to always return an ArrayType,
254 /// which reduces the amount of casting needed in parts of the compiler.
256 inline const ArrayType *getType() const {
257 return reinterpret_cast<const ArrayType*>(Value::getType());
260 /// isString - This method returns true if the array is an array of sbyte or
261 /// ubyte, and if the elements of the array are all ConstantInt's.
262 bool isString() const;
264 /// isCString - This method returns true if the array is a string (see
265 /// isString) and it ends in a null byte \0 and does not contains any other
266 /// null bytes except its terminator.
267 bool isCString() const;
269 /// getAsString - If this array is isString(), then this method converts the
270 /// array to an std::string and returns it. Otherwise, it asserts out.
272 std::string getAsString() const;
274 /// isNullValue - Return true if this is the value that would be returned by
275 /// getNullValue. This always returns false because zero arrays are always
276 /// created as ConstantAggregateZero objects.
277 virtual bool isNullValue() const { return false; }
279 virtual void destroyConstant();
280 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
282 /// Methods for support type inquiry through isa, cast, and dyn_cast:
283 static inline bool classof(const ConstantArray *) { return true; }
284 static bool classof(const Value *V) {
285 return V->getValueType() == ConstantArrayVal;
290 //===----------------------------------------------------------------------===//
291 // ConstantStruct - Constant Struct Declarations
293 class ConstantStruct : public Constant {
294 friend struct ConstantCreator<ConstantStruct, StructType,
295 std::vector<Constant*> >;
296 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
298 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
301 /// get() - Static factory methods - Return objects of the specified value
303 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
304 static Constant *get(const std::vector<Constant*> &V, bool packed = false);
306 /// getType() specialization - Reduce amount of casting...
308 inline const StructType *getType() const {
309 return reinterpret_cast<const StructType*>(Value::getType());
312 /// isNullValue - Return true if this is the value that would be returned by
313 /// getNullValue. This always returns false because zero structs are always
314 /// created as ConstantAggregateZero objects.
315 virtual bool isNullValue() const {
319 virtual void destroyConstant();
320 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
322 /// Methods for support type inquiry through isa, cast, and dyn_cast:
323 static inline bool classof(const ConstantStruct *) { return true; }
324 static bool classof(const Value *V) {
325 return V->getValueType() == ConstantStructVal;
329 //===----------------------------------------------------------------------===//
330 /// ConstantPacked - Constant Packed Declarations
332 class ConstantPacked : public Constant {
333 friend struct ConstantCreator<ConstantPacked, PackedType,
334 std::vector<Constant*> >;
335 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
337 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
340 /// get() - Static factory methods - Return objects of the specified value
341 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
342 static Constant *get(const std::vector<Constant*> &V);
344 /// getType - Specialize the getType() method to always return an PackedType,
345 /// which reduces the amount of casting needed in parts of the compiler.
347 inline const PackedType *getType() const {
348 return reinterpret_cast<const PackedType*>(Value::getType());
351 /// @returns the value for an packed integer constant of the given type that
352 /// has all its bits set to true.
353 /// @brief Get the all ones value
354 static ConstantPacked *getAllOnesValue(const PackedType *Ty);
356 /// isNullValue - Return true if this is the value that would be returned by
357 /// getNullValue. This always returns false because zero arrays are always
358 /// created as ConstantAggregateZero objects.
359 virtual bool isNullValue() const { return false; }
361 /// This function will return true iff every element in this packed constant
362 /// is set to all ones.
363 /// @returns true iff this constant's emements are all set to all ones.
364 /// @brief Determine if the value is all ones.
365 bool isAllOnesValue() const;
367 virtual void destroyConstant();
368 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
370 /// Methods for support type inquiry through isa, cast, and dyn_cast:
371 static inline bool classof(const ConstantPacked *) { return true; }
372 static bool classof(const Value *V) {
373 return V->getValueType() == ConstantPackedVal;
377 //===----------------------------------------------------------------------===//
378 /// ConstantPointerNull - a constant pointer value that points to null
380 class ConstantPointerNull : public Constant {
381 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
382 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
384 ConstantPointerNull(const PointerType *T)
385 : Constant(reinterpret_cast<const Type*>(T),
386 Value::ConstantPointerNullVal, 0, 0) {}
390 /// get() - Static factory methods - Return objects of the specified value
391 static ConstantPointerNull *get(const PointerType *T);
393 /// isNullValue - Return true if this is the value that would be returned by
395 virtual bool isNullValue() const { return true; }
397 virtual void destroyConstant();
399 /// getType - Specialize the getType() method to always return an PointerType,
400 /// which reduces the amount of casting needed in parts of the compiler.
402 inline const PointerType *getType() const {
403 return reinterpret_cast<const PointerType*>(Value::getType());
406 /// Methods for support type inquiry through isa, cast, and dyn_cast:
407 static inline bool classof(const ConstantPointerNull *) { return true; }
408 static bool classof(const Value *V) {
409 return V->getValueType() == ConstantPointerNullVal;
414 /// ConstantExpr - a constant value that is initialized with an expression using
415 /// other constant values.
417 /// This class uses the standard Instruction opcodes to define the various
418 /// constant expressions. The Opcode field for the ConstantExpr class is
419 /// maintained in the Value::SubclassData field.
420 class ConstantExpr : public Constant {
421 friend struct ConstantCreator<ConstantExpr,Type,
422 std::pair<unsigned, std::vector<Constant*> > >;
423 friend struct ConvertConstantType<ConstantExpr, Type>;
426 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
427 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
428 // Operation type (an Instruction opcode) is stored as the SubclassData.
429 SubclassData = Opcode;
432 // These private methods are used by the type resolution code to create
433 // ConstantExprs in intermediate forms.
434 static Constant *getTy(const Type *Ty, unsigned Opcode,
435 Constant *C1, Constant *C2);
436 static Constant *getCompareTy(unsigned short pred, Constant *C1,
438 static Constant *getShiftTy(const Type *Ty,
439 unsigned Opcode, Constant *C1, Constant *C2);
440 static Constant *getSelectTy(const Type *Ty,
441 Constant *C1, Constant *C2, Constant *C3);
442 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
443 const std::vector<Value*> &IdxList);
444 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
446 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
447 Constant *Elt, Constant *Idx);
448 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
449 Constant *V2, Constant *Mask);
452 // Static methods to construct a ConstantExpr of different kinds. Note that
453 // these methods may return a object that is not an instance of the
454 // ConstantExpr class, because they will attempt to fold the constant
455 // expression into something simpler if possible.
457 /// Cast constant expr
459 static Constant *getTrunc (Constant *C, const Type *Ty);
460 static Constant *getSExt (Constant *C, const Type *Ty);
461 static Constant *getZExt (Constant *C, const Type *Ty);
462 static Constant *getFPTrunc (Constant *C, const Type *Ty);
463 static Constant *getFPExtend(Constant *C, const Type *Ty);
464 static Constant *getUIToFP (Constant *C, const Type *Ty);
465 static Constant *getSIToFP (Constant *C, const Type *Ty);
466 static Constant *getFPToUI (Constant *C, const Type *Ty);
467 static Constant *getFPToSI (Constant *C, const Type *Ty);
468 static Constant *getPtrToInt(Constant *C, const Type *Ty);
469 static Constant *getIntToPtr(Constant *C, const Type *Ty);
470 static Constant *getBitCast (Constant *C, const Type *Ty);
472 // @brief Convenience function for getting one of the casting operations
473 // using a CastOps opcode.
474 static Constant *getCast(
475 unsigned ops, ///< The opcode for the conversion
476 Constant *C, ///< The constant to be converted
477 const Type *Ty ///< The type to which the constant is converted
480 // @brief Create a ZExt or BitCast cast constant expression
481 static Constant *getZExtOrBitCast(
482 Constant *C, ///< The constant to zext or bitcast
483 const Type *Ty ///< The type to zext or bitcast C to
486 // @brief Create a SExt or BitCast cast constant expression
487 static Constant *getSExtOrBitCast(
488 Constant *C, ///< The constant to sext or bitcast
489 const Type *Ty ///< The type to sext or bitcast C to
492 // @brief Create a Trunc or BitCast cast constant expression
493 static Constant *getTruncOrBitCast(
494 Constant *C, ///< The constant to trunc or bitcast
495 const Type *Ty ///< The type to trunc or bitcast C to
498 /// @brief Create a BitCast or a PtrToInt cast constant expression
499 static Constant *getPointerCast(
500 Constant *C, ///< The pointer value to be casted (operand 0)
501 const Type *Ty ///< The type to which cast should be made
504 /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
505 static Constant *getIntegerCast(
506 Constant *C, ///< The integer constant to be casted
507 const Type *Ty, ///< The integer type to cast to
508 bool isSigned ///< Whether C should be treated as signed or not
511 /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
512 static Constant *getFPCast(
513 Constant *C, ///< The integer constant to be casted
514 const Type *Ty ///< The integer type to cast to
517 /// @brief Return true if this is a convert constant expression
520 /// @brief Return true if this is a compare constant expression
521 bool isCompare() const;
523 /// Select constant expr
525 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
526 return getSelectTy(V1->getType(), C, V1, V2);
529 /// getSizeOf constant expr - computes the size of a type in a target
530 /// independent way (Note: the return type is a ULong).
532 static Constant *getSizeOf(const Type *Ty);
534 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
535 /// return a pointer to a pointer of the array element type.
536 static Constant *getPtrPtrFromArrayPtr(Constant *C);
538 /// ConstantExpr::get - Return a binary or shift operator constant expression,
539 /// folding if possible.
541 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
543 /// @brief Return an ICmp or FCmp comparison operator constant expression.
544 static Constant *getCompare(unsigned short pred, Constant *C1, Constant *C2);
546 /// ConstantExpr::get* - Return some common constants without having to
547 /// specify the full Instruction::OPCODE identifier.
549 static Constant *getNeg(Constant *C);
550 static Constant *getNot(Constant *C);
551 static Constant *getAdd(Constant *C1, Constant *C2);
552 static Constant *getSub(Constant *C1, Constant *C2);
553 static Constant *getMul(Constant *C1, Constant *C2);
554 static Constant *getUDiv(Constant *C1, Constant *C2);
555 static Constant *getSDiv(Constant *C1, Constant *C2);
556 static Constant *getFDiv(Constant *C1, Constant *C2);
557 static Constant *getURem(Constant *C1, Constant *C2); // unsigned rem
558 static Constant *getSRem(Constant *C1, Constant *C2); // signed rem
559 static Constant *getFRem(Constant *C1, Constant *C2);
560 static Constant *getAnd(Constant *C1, Constant *C2);
561 static Constant *getOr(Constant *C1, Constant *C2);
562 static Constant *getXor(Constant *C1, Constant *C2);
563 static Constant* getICmp(unsigned short pred, Constant* LHS, Constant* RHS);
564 static Constant* getFCmp(unsigned short pred, Constant* LHS, Constant* RHS);
565 static Constant *getShl(Constant *C1, Constant *C2);
566 static Constant *getLShr(Constant *C1, Constant *C2);
567 static Constant *getAShr(Constant *C1, Constant *C2);
569 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
570 /// all elements must be Constant's.
572 static Constant *getGetElementPtr(Constant *C,
573 const std::vector<Constant*> &IdxList);
574 static Constant *getGetElementPtr(Constant *C,
575 const std::vector<Value*> &IdxList);
577 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
578 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
579 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
581 /// isNullValue - Return true if this is the value that would be returned by
583 virtual bool isNullValue() const { return false; }
585 /// getOpcode - Return the opcode at the root of this constant expression
586 unsigned getOpcode() const { return SubclassData; }
588 /// getPredicate - Return the ICMP or FCMP predicate value. Assert if this is
589 /// not an ICMP or FCMP constant expression.
590 unsigned getPredicate() const;
592 /// getOpcodeName - Return a string representation for an opcode.
593 const char *getOpcodeName() const;
595 /// getWithOperandReplaced - Return a constant expression identical to this
596 /// one, but with the specified operand set to the specified value.
597 Constant *getWithOperandReplaced(unsigned OpNo, Constant *Op) const;
599 /// getWithOperands - This returns the current constant expression with the
600 /// operands replaced with the specified values. The specified operands must
601 /// match count and type with the existing ones.
602 Constant *getWithOperands(const std::vector<Constant*> &Ops) const;
604 virtual void destroyConstant();
605 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
607 /// Override methods to provide more type information...
608 inline Constant *getOperand(unsigned i) {
609 return cast<Constant>(User::getOperand(i));
611 inline Constant *getOperand(unsigned i) const {
612 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
616 /// Methods for support type inquiry through isa, cast, and dyn_cast:
617 static inline bool classof(const ConstantExpr *) { return true; }
618 static inline bool classof(const Value *V) {
619 return V->getValueType() == ConstantExprVal;
624 //===----------------------------------------------------------------------===//
625 /// UndefValue - 'undef' values are things that do not have specified contents.
626 /// These are used for a variety of purposes, including global variable
627 /// initializers and operands to instructions. 'undef' values can occur with
630 class UndefValue : public Constant {
631 friend struct ConstantCreator<UndefValue, Type, char>;
632 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
634 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
636 /// get() - Static factory methods - Return an 'undef' object of the specified
639 static UndefValue *get(const Type *T);
641 /// isNullValue - Return true if this is the value that would be returned by
643 virtual bool isNullValue() const { return false; }
645 virtual void destroyConstant();
647 /// Methods for support type inquiry through isa, cast, and dyn_cast:
648 static inline bool classof(const UndefValue *) { return true; }
649 static bool classof(const Value *V) {
650 return V->getValueType() == UndefValueVal;
654 } // End llvm namespace