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 // This file contains the declarations for the subclasses of Constant, which
11 // represent the different flavors of constant values that live in LLVM. Note
12 // that Constants are immutable (once created they never change) and are fully
13 // 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 /// ConstantIntegral - Shared superclass of boolean and integer constants.
41 /// This class just defines some common interfaces to be implemented.
43 class ConstantIntegral : public Constant {
49 ConstantIntegral(const Type *Ty, ValueTy VT, uint64_t V);
52 /// getRawValue - return the underlying value of this constant as a 64-bit
53 /// unsigned integer value.
55 inline uint64_t getRawValue() const { return Val.Unsigned; }
57 /// getZExtValue - Return the constant zero extended as appropriate for this
59 inline uint64_t getZExtValue() const {
60 unsigned Size = getType()->getPrimitiveSizeInBits();
61 return Val.Unsigned & (~uint64_t(0UL) >> (64-Size));
64 /// getSExtValue - Return the constant sign extended as appropriate for this
66 inline int64_t getSExtValue() const {
67 unsigned Size = getType()->getPrimitiveSizeInBits();
68 return (Val.Signed << (64-Size)) >> (64-Size);
71 /// isNullValue - Return true if this is the value that would be returned by
74 virtual bool isNullValue() const = 0;
76 /// isMaxValue - Return true if this is the largest value that may be
77 /// represented by this type.
79 virtual bool isMaxValue() const = 0;
81 /// isMinValue - Return true if this is the smallest value that may be
82 /// represented by this type.
84 virtual bool isMinValue() const = 0;
86 /// isAllOnesValue - Return true if every bit in this constant is set to true.
88 virtual bool isAllOnesValue() const = 0;
90 /// Static constructor to get the maximum/minimum/allones constant of
91 /// specified (integral) type...
93 static ConstantIntegral *getMaxValue(const Type *Ty);
94 static ConstantIntegral *getMinValue(const Type *Ty);
95 static ConstantIntegral *getAllOnesValue(const Type *Ty);
97 /// Methods for support type inquiry through isa, cast, and dyn_cast:
98 static inline bool classof(const ConstantIntegral *) { return true; }
99 static bool classof(const Value *V) {
100 return V->getValueType() == ConstantBoolVal ||
101 V->getValueType() == ConstantSIntVal ||
102 V->getValueType() == ConstantUIntVal;
107 //===----------------------------------------------------------------------===//
108 /// ConstantBool - Boolean Values
110 class ConstantBool : public ConstantIntegral {
111 ConstantBool(bool V);
113 static ConstantBool *True, *False; // The True & False values
115 /// get() - Static factory methods - Return objects of the specified value
116 static ConstantBool *get(bool Value) { return Value ? True : False; }
117 static ConstantBool *get(const Type *Ty, bool Value) { return get(Value); }
119 /// inverted - Return the opposite value of the current value.
120 inline ConstantBool *inverted() const { return (this==True) ? False : True; }
122 /// getValue - return the boolean value of this constant.
124 inline bool getValue() const { return static_cast<bool>(getRawValue()); }
126 /// isNullValue - Return true if this is the value that would be returned by
129 virtual bool isNullValue() const { return this == False; }
130 virtual bool isMaxValue() const { return this == True; }
131 virtual bool isMinValue() const { return this == False; }
132 virtual bool isAllOnesValue() const { return this == True; }
134 /// Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const ConstantBool *) { return true; }
136 static bool classof(const Value *V) {
137 return V->getValueType() == ConstantBoolVal;
142 //===----------------------------------------------------------------------===//
143 /// ConstantInt - Superclass of ConstantSInt & ConstantUInt, to make dealing
144 /// with integral constants easier.
146 class ConstantInt : public ConstantIntegral {
148 ConstantInt(const ConstantInt &); // DO NOT IMPLEMENT
149 ConstantInt(const Type *Ty, ValueTy VT, uint64_t V);
151 /// equalsInt - Provide a helper method that can be used to determine if the
152 /// constant contained within is equal to a constant. This only works for
153 /// very small values, because this is all that can be represented with all
156 bool equalsInt(unsigned char V) const {
158 "equalsInt: Can only be used with very small positive constants!");
159 return Val.Unsigned == V;
162 /// ConstantInt::get static method: return a ConstantInt with the specified
163 /// value. as above, we work only with very small values here.
165 static ConstantInt *get(const Type *Ty, unsigned char V);
167 /// isNullValue - Return true if this is the value that would be returned by
169 virtual bool isNullValue() const { return Val.Unsigned == 0; }
170 virtual bool isMaxValue() const = 0;
171 virtual bool isMinValue() const = 0;
173 /// Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const ConstantInt *) { return true; }
175 static bool classof(const Value *V) {
176 return V->getValueType() == ConstantSIntVal ||
177 V->getValueType() == ConstantUIntVal;
182 //===----------------------------------------------------------------------===//
183 /// ConstantSInt - Signed Integer Values [sbyte, short, int, long]
185 class ConstantSInt : public ConstantInt {
186 ConstantSInt(const ConstantSInt &); // DO NOT IMPLEMENT
187 friend struct ConstantCreator<ConstantSInt, Type, int64_t>;
190 ConstantSInt(const Type *Ty, int64_t V);
192 /// get() - Static factory methods - Return objects of the specified value
194 static ConstantSInt *get(const Type *Ty, int64_t V);
196 /// isValueValidForType - return true if Ty is big enough to represent V.
198 static bool isValueValidForType(const Type *Ty, int64_t V);
200 /// getValue - return the underlying value of this constant.
202 inline int64_t getValue() const { return Val.Signed; }
204 virtual bool isAllOnesValue() const { return getValue() == -1; }
206 /// isMaxValue - Return true if this is the largest value that may be
207 /// represented by this type.
209 virtual bool isMaxValue() const {
210 int64_t V = getValue();
211 if (V < 0) return false; // Be careful about wrap-around on 'long's
213 return !isValueValidForType(getType(), V) || V < 0;
216 /// isMinValue - Return true if this is the smallest value that may be
217 /// represented by this type.
219 virtual bool isMinValue() const {
220 int64_t V = getValue();
221 if (V > 0) return false; // Be careful about wrap-around on 'long's
223 return !isValueValidForType(getType(), V) || V > 0;
226 /// Methods for support type inquiry through isa, cast, and dyn_cast:
228 static inline bool classof(const ConstantSInt *) { return true; }
229 static bool classof(const Value *V) {
230 return V->getValueType() == ConstantSIntVal;
234 //===----------------------------------------------------------------------===//
235 /// ConstantUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
237 class ConstantUInt : public ConstantInt {
238 ConstantUInt(const ConstantUInt &); // DO NOT IMPLEMENT
239 friend struct ConstantCreator<ConstantUInt, Type, uint64_t>;
241 ConstantUInt(const Type *Ty, uint64_t V);
243 /// get() - Static factory methods - Return objects of the specified value
245 static ConstantUInt *get(const Type *Ty, uint64_t V);
247 /// isValueValidForType - return true if Ty is big enough to represent V.
249 static bool isValueValidForType(const Type *Ty, uint64_t V);
251 /// getValue - return the underlying value of this constant.
253 inline uint64_t getValue() const { return Val.Unsigned; }
255 /// isMaxValue - Return true if this is the largest value that may be
256 /// represented by this type.
258 virtual bool isAllOnesValue() const;
259 virtual bool isMaxValue() const { return isAllOnesValue(); }
260 virtual bool isMinValue() const { return getValue() == 0; }
262 /// Methods for support type inquiry through isa, cast, and dyn_cast:
263 static inline bool classof(const ConstantUInt *) { return true; }
264 static bool classof(const Value *V) {
265 return V->getValueType() == ConstantUIntVal;
270 //===----------------------------------------------------------------------===//
271 /// ConstantFP - Floating Point Values [float, double]
273 class ConstantFP : public Constant {
275 friend struct ConstantCreator<ConstantFP, Type, uint64_t>;
276 friend struct ConstantCreator<ConstantFP, Type, uint32_t>;
277 ConstantFP(const ConstantFP &); // DO NOT IMPLEMENT
279 ConstantFP(const Type *Ty, double V);
281 /// get() - Static factory methods - Return objects of the specified value
282 static ConstantFP *get(const Type *Ty, double V);
284 /// isValueValidForType - return true if Ty is big enough to represent V.
285 static bool isValueValidForType(const Type *Ty, double V);
286 inline double getValue() const { return Val; }
288 /// isNullValue - Return true if this is the value that would be returned by
289 /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
290 /// considers -0.0 to be null as well as 0.0. :(
291 virtual bool isNullValue() const;
293 /// isExactlyValue - We don't rely on operator== working on double values, as
294 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
295 /// As such, this method can be used to do an exact bit-for-bit comparison of
296 /// two floating point values.
297 bool isExactlyValue(double V) const;
299 /// Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const ConstantFP *) { return true; }
301 static bool classof(const Value *V) {
302 return V->getValueType() == ConstantFPVal;
306 //===----------------------------------------------------------------------===//
307 /// ConstantAggregateZero - All zero aggregate value
309 class ConstantAggregateZero : public Constant {
310 friend struct ConstantCreator<ConstantAggregateZero, Type, char>;
311 ConstantAggregateZero(const ConstantAggregateZero &); // DO NOT IMPLEMENT
313 ConstantAggregateZero(const Type *Ty)
314 : Constant(Ty, ConstantAggregateZeroVal, 0, 0) {}
316 /// get() - static factory method for creating a null aggregate. It is
317 /// illegal to call this method with a non-aggregate type.
318 static Constant *get(const Type *Ty);
320 /// isNullValue - Return true if this is the value that would be returned by
322 virtual bool isNullValue() const { return true; }
324 virtual void destroyConstant();
326 /// Methods for support type inquiry through isa, cast, and dyn_cast:
328 static bool classof(const ConstantAggregateZero *) { return true; }
329 static bool classof(const Value *V) {
330 return V->getValueType() == ConstantAggregateZeroVal;
335 //===----------------------------------------------------------------------===//
336 /// ConstantArray - Constant Array Declarations
338 class ConstantArray : public Constant {
339 friend struct ConstantCreator<ConstantArray, ArrayType,
340 std::vector<Constant*> >;
341 ConstantArray(const ConstantArray &); // DO NOT IMPLEMENT
343 ConstantArray(const ArrayType *T, const std::vector<Constant*> &Val);
346 /// get() - Static factory methods - Return objects of the specified value
347 static Constant *get(const ArrayType *T, const std::vector<Constant*> &);
349 /// This method constructs a ConstantArray and initializes it with a text
350 /// string. The default behavior (len==0) causes the null terminator to
351 /// be copied as well. However, in some situations this is not desired so
352 /// if len <= Initializer.length() (but not 0) then only that portion of
353 /// the string is copied and there is no null termination. If len >
354 /// than Initializer's length then the function asserts out (don't do that).
355 static Constant *get(const std::string &Initializer, unsigned len = 0);
357 /// getType - Specialize the getType() method to always return an ArrayType,
358 /// which reduces the amount of casting needed in parts of the compiler.
360 inline const ArrayType *getType() const {
361 return reinterpret_cast<const ArrayType*>(Value::getType());
364 /// isString - This method returns true if the array is an array of sbyte or
365 /// ubyte, and if the elements of the array are all ConstantInt's.
366 bool isString() const;
368 /// getAsString - If this array is isString(), then this method converts the
369 /// array to an std::string and returns it. Otherwise, it asserts out.
371 std::string getAsString() const;
373 /// isNullValue - Return true if this is the value that would be returned by
374 /// getNullValue. This always returns false because zero arrays are always
375 /// created as ConstantAggregateZero objects.
376 virtual bool isNullValue() const { return false; }
378 virtual void destroyConstant();
379 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
381 /// Methods for support type inquiry through isa, cast, and dyn_cast:
382 static inline bool classof(const ConstantArray *) { return true; }
383 static bool classof(const Value *V) {
384 return V->getValueType() == ConstantArrayVal;
389 //===----------------------------------------------------------------------===//
390 // ConstantStruct - Constant Struct Declarations
392 class ConstantStruct : public Constant {
393 friend struct ConstantCreator<ConstantStruct, StructType,
394 std::vector<Constant*> >;
395 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
397 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
400 /// get() - Static factory methods - Return objects of the specified value
402 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
403 static Constant *get(const std::vector<Constant*> &V);
405 /// getType() specialization - Reduce amount of casting...
407 inline const StructType *getType() const {
408 return reinterpret_cast<const StructType*>(Value::getType());
411 /// isNullValue - Return true if this is the value that would be returned by
412 /// getNullValue. This always returns false because zero structs are always
413 /// created as ConstantAggregateZero objects.
414 virtual bool isNullValue() const {
418 virtual void destroyConstant();
419 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
421 /// Methods for support type inquiry through isa, cast, and dyn_cast:
422 static inline bool classof(const ConstantStruct *) { return true; }
423 static bool classof(const Value *V) {
424 return V->getValueType() == ConstantStructVal;
428 //===----------------------------------------------------------------------===//
429 /// ConstantPacked - Constant Packed Declarations
431 class ConstantPacked : public Constant {
432 friend struct ConstantCreator<ConstantPacked, PackedType,
433 std::vector<Constant*> >;
434 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
436 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
439 /// get() - Static factory methods - Return objects of the specified value
440 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
441 static Constant *get(const std::vector<Constant*> &V);
443 /// getType - Specialize the getType() method to always return an PackedType,
444 /// which reduces the amount of casting needed in parts of the compiler.
446 inline const PackedType *getType() const {
447 return reinterpret_cast<const PackedType*>(Value::getType());
450 /// isNullValue - Return true if this is the value that would be returned by
451 /// getNullValue. This always returns false because zero arrays are always
452 /// created as ConstantAggregateZero objects.
453 virtual bool isNullValue() const { return false; }
455 virtual void destroyConstant();
456 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
458 /// Methods for support type inquiry through isa, cast, and dyn_cast:
459 static inline bool classof(const ConstantPacked *) { return true; }
460 static bool classof(const Value *V) {
461 return V->getValueType() == ConstantPackedVal;
465 //===----------------------------------------------------------------------===//
466 /// ConstantPointerNull - a constant pointer value that points to null
468 class ConstantPointerNull : public Constant {
469 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
470 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
472 ConstantPointerNull(const PointerType *T)
473 : Constant(reinterpret_cast<const Type*>(T),
474 Value::ConstantPointerNullVal, 0, 0) {}
478 /// get() - Static factory methods - Return objects of the specified value
479 static ConstantPointerNull *get(const PointerType *T);
481 /// isNullValue - Return true if this is the value that would be returned by
483 virtual bool isNullValue() const { return true; }
485 virtual void destroyConstant();
487 /// getType - Specialize the getType() method to always return an PointerType,
488 /// which reduces the amount of casting needed in parts of the compiler.
490 inline const PointerType *getType() const {
491 return reinterpret_cast<const PointerType*>(Value::getType());
494 /// Methods for support type inquiry through isa, cast, and dyn_cast:
495 static inline bool classof(const ConstantPointerNull *) { return true; }
496 static bool classof(const Value *V) {
497 return V->getValueType() == ConstantPointerNullVal;
502 /// ConstantExpr - a constant value that is initialized with an expression using
503 /// other constant values.
505 /// This class uses the standard Instruction opcodes to define the various
506 /// constant expressions. The Opcode field for the ConstantExpr class is
507 /// maintained in the Value::SubclassData field.
508 class ConstantExpr : public Constant {
509 friend struct ConstantCreator<ConstantExpr,Type,
510 std::pair<unsigned, std::vector<Constant*> > >;
511 friend struct ConvertConstantType<ConstantExpr, Type>;
514 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
515 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
516 // Operation type (an Instruction opcode) is stored as the SubclassData.
517 SubclassData = Opcode;
520 // These private methods are used by the type resolution code to create
521 // ConstantExprs in intermediate forms.
522 static Constant *getTy(const Type *Ty, unsigned Opcode,
523 Constant *C1, Constant *C2);
524 static Constant *getShiftTy(const Type *Ty,
525 unsigned Opcode, Constant *C1, Constant *C2);
526 static Constant *getSelectTy(const Type *Ty,
527 Constant *C1, Constant *C2, Constant *C3);
528 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
529 const std::vector<Value*> &IdxList);
530 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
532 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
533 Constant *Elt, Constant *Idx);
534 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
535 Constant *V2, Constant *Mask);
538 // Static methods to construct a ConstantExpr of different kinds. Note that
539 // these methods may return a object that is not an instance of the
540 // ConstantExpr class, because they will attempt to fold the constant
541 // expression into something simpler if possible.
543 /// Cast constant expr
545 static Constant *getCast(Constant *C, const Type *Ty);
546 static Constant *getSignExtend(Constant *C, const Type *Ty);
547 static Constant *getZeroExtend(Constant *C, const Type *Ty);
549 /// Select constant expr
551 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
552 return getSelectTy(V1->getType(), C, V1, V2);
555 /// getSizeOf constant expr - computes the size of a type in a target
556 /// independent way (Note: the return type is ULong but the object is not
557 /// necessarily a ConstantUInt).
559 static Constant *getSizeOf(const Type *Ty);
561 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
562 /// return a pointer to a pointer of the array element type.
563 static Constant *getPtrPtrFromArrayPtr(Constant *C);
565 /// ConstantExpr::get - Return a binary or shift operator constant expression,
566 /// folding if possible.
568 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
570 /// ConstantExpr::get* - Return some common constants without having to
571 /// specify the full Instruction::OPCODE identifier.
573 static Constant *getNeg(Constant *C);
574 static Constant *getNot(Constant *C);
575 static Constant *getAdd(Constant *C1, Constant *C2);
576 static Constant *getSub(Constant *C1, Constant *C2);
577 static Constant *getMul(Constant *C1, Constant *C2);
578 static Constant *getDiv(Constant *C1, Constant *C2);
579 static Constant *getRem(Constant *C1, Constant *C2);
580 static Constant *getAnd(Constant *C1, Constant *C2);
581 static Constant *getOr(Constant *C1, Constant *C2);
582 static Constant *getXor(Constant *C1, Constant *C2);
583 static Constant *getSetEQ(Constant *C1, Constant *C2);
584 static Constant *getSetNE(Constant *C1, Constant *C2);
585 static Constant *getSetLT(Constant *C1, Constant *C2);
586 static Constant *getSetGT(Constant *C1, Constant *C2);
587 static Constant *getSetLE(Constant *C1, Constant *C2);
588 static Constant *getSetGE(Constant *C1, Constant *C2);
589 static Constant *getShl(Constant *C1, Constant *C2);
590 static Constant *getShr(Constant *C1, Constant *C2);
592 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
593 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
595 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
596 /// all elements must be Constant's.
598 static Constant *getGetElementPtr(Constant *C,
599 const std::vector<Constant*> &IdxList);
600 static Constant *getGetElementPtr(Constant *C,
601 const std::vector<Value*> &IdxList);
603 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
604 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
605 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
607 /// isNullValue - Return true if this is the value that would be returned by
609 virtual bool isNullValue() const { return false; }
611 /// getOpcode - Return the opcode at the root of this constant expression
612 unsigned getOpcode() const { return SubclassData; }
614 /// getOpcodeName - Return a string representation for an opcode.
615 const char *getOpcodeName() const;
617 virtual void destroyConstant();
618 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
620 /// Override methods to provide more type information...
621 inline Constant *getOperand(unsigned i) {
622 return cast<Constant>(User::getOperand(i));
624 inline Constant *getOperand(unsigned i) const {
625 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
629 /// Methods for support type inquiry through isa, cast, and dyn_cast:
630 static inline bool classof(const ConstantExpr *) { return true; }
631 static inline bool classof(const Value *V) {
632 return V->getValueType() == ConstantExprVal;
637 //===----------------------------------------------------------------------===//
638 /// UndefValue - 'undef' values are things that do not have specified contents.
639 /// These are used for a variety of purposes, including global variable
640 /// initializers and operands to instructions. 'undef' values can occur with
643 class UndefValue : public Constant {
644 friend struct ConstantCreator<UndefValue, Type, char>;
645 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
647 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
649 /// get() - Static factory methods - Return an 'undef' object of the specified
652 static UndefValue *get(const Type *T);
654 /// isNullValue - Return true if this is the value that would be returned by
656 virtual bool isNullValue() const { return false; }
658 virtual void destroyConstant();
660 /// Methods for support type inquiry through isa, cast, and dyn_cast:
661 static inline bool classof(const UndefValue *) { return true; }
662 static bool classof(const Value *V) {
663 return V->getValueType() == UndefValueVal;
667 } // End llvm namespace