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 & (~0ULL >> (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*> &);
348 static Constant *get(const std::string &Initializer);
350 /// getType - Specialize the getType() method to always return an ArrayType,
351 /// which reduces the amount of casting needed in parts of the compiler.
353 inline const ArrayType *getType() const {
354 return reinterpret_cast<const ArrayType*>(Value::getType());
357 /// isString - This method returns true if the array is an array of sbyte or
358 /// ubyte, and if the elements of the array are all ConstantInt's.
359 bool isString() const;
361 /// getAsString - If this array is isString(), then this method converts the
362 /// array to an std::string and returns it. Otherwise, it asserts out.
364 std::string getAsString() const;
366 /// isNullValue - Return true if this is the value that would be returned by
367 /// getNullValue. This always returns false because zero arrays are always
368 /// created as ConstantAggregateZero objects.
369 virtual bool isNullValue() const { return false; }
371 virtual void destroyConstant();
372 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
374 /// Methods for support type inquiry through isa, cast, and dyn_cast:
375 static inline bool classof(const ConstantArray *) { return true; }
376 static bool classof(const Value *V) {
377 return V->getValueType() == ConstantArrayVal;
382 //===----------------------------------------------------------------------===//
383 // ConstantStruct - Constant Struct Declarations
385 class ConstantStruct : public Constant {
386 friend struct ConstantCreator<ConstantStruct, StructType,
387 std::vector<Constant*> >;
388 ConstantStruct(const ConstantStruct &); // DO NOT IMPLEMENT
390 ConstantStruct(const StructType *T, const std::vector<Constant*> &Val);
393 /// get() - Static factory methods - Return objects of the specified value
395 static Constant *get(const StructType *T, const std::vector<Constant*> &V);
396 static Constant *get(const std::vector<Constant*> &V);
398 /// getType() specialization - Reduce amount of casting...
400 inline const StructType *getType() const {
401 return reinterpret_cast<const StructType*>(Value::getType());
404 /// isNullValue - Return true if this is the value that would be returned by
405 /// getNullValue. This always returns false because zero structs are always
406 /// created as ConstantAggregateZero objects.
407 virtual bool isNullValue() const {
411 virtual void destroyConstant();
412 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
414 /// Methods for support type inquiry through isa, cast, and dyn_cast:
415 static inline bool classof(const ConstantStruct *) { return true; }
416 static bool classof(const Value *V) {
417 return V->getValueType() == ConstantStructVal;
421 //===----------------------------------------------------------------------===//
422 /// ConstantPacked - Constant Packed Declarations
424 class ConstantPacked : public Constant {
425 friend struct ConstantCreator<ConstantPacked, PackedType,
426 std::vector<Constant*> >;
427 ConstantPacked(const ConstantPacked &); // DO NOT IMPLEMENT
429 ConstantPacked(const PackedType *T, const std::vector<Constant*> &Val);
432 /// get() - Static factory methods - Return objects of the specified value
433 static Constant *get(const PackedType *T, const std::vector<Constant*> &);
434 static Constant *get(const std::vector<Constant*> &V);
436 /// getType - Specialize the getType() method to always return an PackedType,
437 /// which reduces the amount of casting needed in parts of the compiler.
439 inline const PackedType *getType() const {
440 return reinterpret_cast<const PackedType*>(Value::getType());
443 /// isNullValue - Return true if this is the value that would be returned by
444 /// getNullValue. This always returns false because zero arrays are always
445 /// created as ConstantAggregateZero objects.
446 virtual bool isNullValue() const { return false; }
448 virtual void destroyConstant();
449 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
451 /// Methods for support type inquiry through isa, cast, and dyn_cast:
452 static inline bool classof(const ConstantPacked *) { return true; }
453 static bool classof(const Value *V) {
454 return V->getValueType() == ConstantPackedVal;
458 //===----------------------------------------------------------------------===//
459 /// ConstantPointerNull - a constant pointer value that points to null
461 class ConstantPointerNull : public Constant {
462 friend struct ConstantCreator<ConstantPointerNull, PointerType, char>;
463 ConstantPointerNull(const ConstantPointerNull &); // DO NOT IMPLEMENT
465 ConstantPointerNull(const PointerType *T)
466 : Constant(reinterpret_cast<const Type*>(T),
467 Value::ConstantPointerNullVal, 0, 0) {}
471 /// get() - Static factory methods - Return objects of the specified value
472 static ConstantPointerNull *get(const PointerType *T);
474 /// isNullValue - Return true if this is the value that would be returned by
476 virtual bool isNullValue() const { return true; }
478 virtual void destroyConstant();
480 /// getType - Specialize the getType() method to always return an PointerType,
481 /// which reduces the amount of casting needed in parts of the compiler.
483 inline const PointerType *getType() const {
484 return reinterpret_cast<const PointerType*>(Value::getType());
487 /// Methods for support type inquiry through isa, cast, and dyn_cast:
488 static inline bool classof(const ConstantPointerNull *) { return true; }
489 static bool classof(const Value *V) {
490 return V->getValueType() == ConstantPointerNullVal;
495 /// ConstantExpr - a constant value that is initialized with an expression using
496 /// other constant values.
498 /// This class uses the standard Instruction opcodes to define the various
499 /// constant expressions. The Opcode field for the ConstantExpr class is
500 /// maintained in the Value::SubclassData field.
501 class ConstantExpr : public Constant {
502 friend struct ConstantCreator<ConstantExpr,Type,
503 std::pair<unsigned, std::vector<Constant*> > >;
504 friend struct ConvertConstantType<ConstantExpr, Type>;
507 ConstantExpr(const Type *Ty, unsigned Opcode, Use *Ops, unsigned NumOps)
508 : Constant(Ty, ConstantExprVal, Ops, NumOps) {
509 // Operation type (an Instruction opcode) is stored as the SubclassData.
510 SubclassData = Opcode;
513 // These private methods are used by the type resolution code to create
514 // ConstantExprs in intermediate forms.
515 static Constant *getTy(const Type *Ty, unsigned Opcode,
516 Constant *C1, Constant *C2);
517 static Constant *getShiftTy(const Type *Ty,
518 unsigned Opcode, Constant *C1, Constant *C2);
519 static Constant *getSelectTy(const Type *Ty,
520 Constant *C1, Constant *C2, Constant *C3);
521 static Constant *getGetElementPtrTy(const Type *Ty, Constant *C,
522 const std::vector<Value*> &IdxList);
523 static Constant *getExtractElementTy(const Type *Ty, Constant *Val,
525 static Constant *getInsertElementTy(const Type *Ty, Constant *Val,
526 Constant *Elt, Constant *Idx);
527 static Constant *getShuffleVectorTy(const Type *Ty, Constant *V1,
528 Constant *V2, Constant *Mask);
531 // Static methods to construct a ConstantExpr of different kinds. Note that
532 // these methods may return a object that is not an instance of the
533 // ConstantExpr class, because they will attempt to fold the constant
534 // expression into something simpler if possible.
536 /// Cast constant expr
538 static Constant *getCast(Constant *C, const Type *Ty);
539 static Constant *getSignExtend(Constant *C, const Type *Ty);
540 static Constant *getZeroExtend(Constant *C, const Type *Ty);
542 /// Select constant expr
544 static Constant *getSelect(Constant *C, Constant *V1, Constant *V2) {
545 return getSelectTy(V1->getType(), C, V1, V2);
548 /// getSizeOf constant expr - computes the size of a type in a target
549 /// independent way (Note: the return type is ULong but the object is not
550 /// necessarily a ConstantUInt).
552 static Constant *getSizeOf(const Type *Ty);
554 /// getPtrPtrFromArrayPtr constant expr - given a pointer to a constant array,
555 /// return a pointer to a pointer of the array element type.
556 static Constant *getPtrPtrFromArrayPtr(Constant *C);
558 /// ConstantExpr::get - Return a binary or shift operator constant expression,
559 /// folding if possible.
561 static Constant *get(unsigned Opcode, Constant *C1, Constant *C2);
563 /// ConstantExpr::get* - Return some common constants without having to
564 /// specify the full Instruction::OPCODE identifier.
566 static Constant *getNeg(Constant *C);
567 static Constant *getNot(Constant *C);
568 static Constant *getAdd(Constant *C1, Constant *C2);
569 static Constant *getSub(Constant *C1, Constant *C2);
570 static Constant *getMul(Constant *C1, Constant *C2);
571 static Constant *getDiv(Constant *C1, Constant *C2);
572 static Constant *getRem(Constant *C1, Constant *C2);
573 static Constant *getAnd(Constant *C1, Constant *C2);
574 static Constant *getOr(Constant *C1, Constant *C2);
575 static Constant *getXor(Constant *C1, Constant *C2);
576 static Constant *getSetEQ(Constant *C1, Constant *C2);
577 static Constant *getSetNE(Constant *C1, Constant *C2);
578 static Constant *getSetLT(Constant *C1, Constant *C2);
579 static Constant *getSetGT(Constant *C1, Constant *C2);
580 static Constant *getSetLE(Constant *C1, Constant *C2);
581 static Constant *getSetGE(Constant *C1, Constant *C2);
582 static Constant *getShl(Constant *C1, Constant *C2);
583 static Constant *getShr(Constant *C1, Constant *C2);
585 static Constant *getUShr(Constant *C1, Constant *C2); // unsigned shr
586 static Constant *getSShr(Constant *C1, Constant *C2); // signed shr
588 /// Getelementptr form. std::vector<Value*> is only accepted for convenience:
589 /// all elements must be Constant's.
591 static Constant *getGetElementPtr(Constant *C,
592 const std::vector<Constant*> &IdxList);
593 static Constant *getGetElementPtr(Constant *C,
594 const std::vector<Value*> &IdxList);
596 static Constant *getExtractElement(Constant *Vec, Constant *Idx);
597 static Constant *getInsertElement(Constant *Vec, Constant *Elt,Constant *Idx);
598 static Constant *getShuffleVector(Constant *V1, Constant *V2, Constant *Mask);
600 /// isNullValue - Return true if this is the value that would be returned by
602 virtual bool isNullValue() const { return false; }
604 /// getOpcode - Return the opcode at the root of this constant expression
605 unsigned getOpcode() const { return SubclassData; }
607 /// getOpcodeName - Return a string representation for an opcode.
608 const char *getOpcodeName() const;
610 virtual void destroyConstant();
611 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
613 /// Override methods to provide more type information...
614 inline Constant *getOperand(unsigned i) {
615 return cast<Constant>(User::getOperand(i));
617 inline Constant *getOperand(unsigned i) const {
618 return const_cast<Constant*>(cast<Constant>(User::getOperand(i)));
622 /// Methods for support type inquiry through isa, cast, and dyn_cast:
623 static inline bool classof(const ConstantExpr *) { return true; }
624 static inline bool classof(const Value *V) {
625 return V->getValueType() == ConstantExprVal;
630 //===----------------------------------------------------------------------===//
631 /// UndefValue - 'undef' values are things that do not have specified contents.
632 /// These are used for a variety of purposes, including global variable
633 /// initializers and operands to instructions. 'undef' values can occur with
636 class UndefValue : public Constant {
637 friend struct ConstantCreator<UndefValue, Type, char>;
638 UndefValue(const UndefValue &); // DO NOT IMPLEMENT
640 UndefValue(const Type *T) : Constant(T, UndefValueVal, 0, 0) {}
642 /// get() - Static factory methods - Return an 'undef' object of the specified
645 static UndefValue *get(const Type *T);
647 /// isNullValue - Return true if this is the value that would be returned by
649 virtual bool isNullValue() const { return false; }
651 virtual void destroyConstant();
653 /// Methods for support type inquiry through isa, cast, and dyn_cast:
654 static inline bool classof(const UndefValue *) { return true; }
655 static bool classof(const Value *V) {
656 return V->getValueType() == UndefValueVal;
660 } // End llvm namespace