1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- 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 // This file contains the declarations of classes that represent "derived
11 // types". These are things like "arrays of x" or "structure of x, y, z" or
12 // "function returning x taking (y,z) as parameters", etc...
14 // The implementations of these classes live in the Type.cpp file.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_DERIVED_TYPES_H
19 #define LLVM_DERIVED_TYPES_H
21 #include "llvm/Type.h"
22 #include "llvm/Support/DataTypes.h"
27 template<class ValType, class TypeClass> class TypeMap;
28 class FunctionValType;
36 template<typename T> class ArrayRef;
38 class DerivedType : public Type {
42 explicit DerivedType(LLVMContext &C, TypeID id) : Type(C, id) {}
44 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
45 /// that the current type has transitioned from being abstract to being
48 void notifyUsesThatTypeBecameConcrete();
50 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
51 /// another (more concrete) type, we must eliminate all references to other
52 /// types, to avoid some circular reference problems.
54 void dropAllTypeUses();
58 //===--------------------------------------------------------------------===//
59 // Abstract Type handling methods - These types have special lifetimes, which
60 // are managed by (add|remove)AbstractTypeUser. See comments in
61 // AbstractTypeUser.h for more information.
63 /// refineAbstractTypeTo - This function is used to when it is discovered that
64 /// the 'this' abstract type is actually equivalent to the NewType specified.
65 /// This causes all users of 'this' to switch to reference the more concrete
66 /// type NewType and for 'this' to be deleted.
68 void refineAbstractTypeTo(const Type *NewType);
70 void dump() const { Type::dump(); }
72 // Methods for support type inquiry through isa, cast, and dyn_cast.
73 static inline bool classof(const DerivedType *) { return true; }
74 static inline bool classof(const Type *T) {
75 return T->isDerivedType();
79 /// Class to represent integer types. Note that this class is also used to
80 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
82 /// @brief Integer representation type
83 class IntegerType : public DerivedType {
84 friend class LLVMContextImpl;
87 explicit IntegerType(LLVMContext &C, unsigned NumBits) :
88 DerivedType(C, IntegerTyID) {
89 setSubclassData(NumBits);
91 friend class TypeMap<IntegerValType, IntegerType>;
93 /// This enum is just used to hold constants we need for IntegerType.
95 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
96 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
97 ///< Note that bit width is stored in the Type classes SubclassData field
98 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
101 /// This static method is the primary way of constructing an IntegerType.
102 /// If an IntegerType with the same NumBits value was previously instantiated,
103 /// that instance will be returned. Otherwise a new one will be created. Only
104 /// one instance with a given NumBits value is ever created.
105 /// @brief Get or create an IntegerType instance.
106 static const IntegerType *get(LLVMContext &C, unsigned NumBits);
108 /// @brief Get the number of bits in this IntegerType
109 unsigned getBitWidth() const { return getSubclassData(); }
111 /// getBitMask - Return a bitmask with ones set for all of the bits
112 /// that can be set by an unsigned version of this type. This is 0xFF for
113 /// i8, 0xFFFF for i16, etc.
114 uint64_t getBitMask() const {
115 return ~uint64_t(0UL) >> (64-getBitWidth());
118 /// getSignBit - Return a uint64_t with just the most significant bit set (the
119 /// sign bit, if the value is treated as a signed number).
120 uint64_t getSignBit() const {
121 return 1ULL << (getBitWidth()-1);
124 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
125 /// @returns a bit mask with ones set for all the bits of this type.
126 /// @brief Get a bit mask for this type.
127 APInt getMask() const;
129 /// This method determines if the width of this IntegerType is a power-of-2
130 /// in terms of 8 bit bytes.
131 /// @returns true if this is a power-of-2 byte width.
132 /// @brief Is this a power-of-2 byte-width IntegerType ?
133 bool isPowerOf2ByteWidth() const;
135 // Methods for support type inquiry through isa, cast, and dyn_cast.
136 static inline bool classof(const IntegerType *) { return true; }
137 static inline bool classof(const Type *T) {
138 return T->getTypeID() == IntegerTyID;
143 /// FunctionType - Class to represent function types
145 class FunctionType : public DerivedType {
146 friend class TypeMap<FunctionValType, FunctionType>;
147 FunctionType(const FunctionType &); // Do not implement
148 const FunctionType &operator=(const FunctionType &); // Do not implement
149 FunctionType(const Type *Result, ArrayRef<const Type*> Params,
153 /// FunctionType::get - This static method is the primary way of constructing
156 static FunctionType *get(const Type *Result,
157 ArrayRef<const Type*> Params, bool isVarArg);
159 /// FunctionType::get - Create a FunctionType taking no parameters.
161 static FunctionType *get(const Type *Result, bool isVarArg);
163 /// isValidReturnType - Return true if the specified type is valid as a return
165 static bool isValidReturnType(const Type *RetTy);
167 /// isValidArgumentType - Return true if the specified type is valid as an
169 static bool isValidArgumentType(const Type *ArgTy);
171 bool isVarArg() const { return getSubclassData(); }
172 const Type *getReturnType() const { return ContainedTys[0]; }
174 typedef Type::subtype_iterator param_iterator;
175 param_iterator param_begin() const { return ContainedTys + 1; }
176 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
178 // Parameter type accessors.
179 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
181 /// getNumParams - Return the number of fixed parameters this function type
182 /// requires. This does not consider varargs.
184 unsigned getNumParams() const { return NumContainedTys - 1; }
186 // Implement the AbstractTypeUser interface.
187 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
188 virtual void typeBecameConcrete(const DerivedType *AbsTy);
190 // Methods for support type inquiry through isa, cast, and dyn_cast.
191 static inline bool classof(const FunctionType *) { return true; }
192 static inline bool classof(const Type *T) {
193 return T->getTypeID() == FunctionTyID;
198 /// CompositeType - Common super class of ArrayType, StructType, PointerType
200 class CompositeType : public DerivedType {
202 explicit CompositeType(LLVMContext &C, TypeID tid) : DerivedType(C, tid) { }
205 /// getTypeAtIndex - Given an index value into the type, return the type of
208 const Type *getTypeAtIndex(const Value *V) const;
209 const Type *getTypeAtIndex(unsigned Idx) const;
210 bool indexValid(const Value *V) const;
211 bool indexValid(unsigned Idx) const;
213 // Methods for support type inquiry through isa, cast, and dyn_cast.
214 static inline bool classof(const CompositeType *) { return true; }
215 static inline bool classof(const Type *T) {
216 return T->getTypeID() == ArrayTyID ||
217 T->getTypeID() == StructTyID ||
218 T->getTypeID() == PointerTyID ||
219 T->getTypeID() == VectorTyID;
224 /// StructType - Class to represent struct types, both normal and packed.
226 class StructType : public CompositeType {
227 friend class TypeMap<StructValType, StructType>;
228 StructType(const StructType &); // Do not implement
229 const StructType &operator=(const StructType &); // Do not implement
230 StructType(LLVMContext &C, ArrayRef<const Type*> Types, bool isPacked);
232 /// StructType::get - This static method is the primary way to create a
235 static StructType *get(LLVMContext &Context, ArrayRef<const Type*> Elements,
236 bool isPacked = false);
238 /// StructType::get - Create an empty structure type.
240 static StructType *get(LLVMContext &Context, bool isPacked = false);
242 /// StructType::get - This static method is a convenience method for creating
243 /// structure types by specifying the elements as arguments. Note that this
244 /// method always returns a non-packed struct, and requires at least one
246 static StructType *get(const Type *elt1, ...) END_WITH_NULL;
248 /// isValidElementType - Return true if the specified type is valid as a
250 static bool isValidElementType(const Type *ElemTy);
252 bool isPacked() const { return getSubclassData() != 0 ? true : false; }
254 // Iterator access to the elements.
255 typedef Type::subtype_iterator element_iterator;
256 element_iterator element_begin() const { return ContainedTys; }
257 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
259 /// isLayoutIdentical - Return true if this is layout identical to the
260 /// specified struct.
261 bool isLayoutIdentical(const StructType *Other) const {
262 return this == Other;
266 // Random access to the elements
267 unsigned getNumElements() const { return NumContainedTys; }
268 const Type *getElementType(unsigned N) const {
269 assert(N < NumContainedTys && "Element number out of range!");
270 return ContainedTys[N];
273 // Implement the AbstractTypeUser interface.
274 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
275 virtual void typeBecameConcrete(const DerivedType *AbsTy);
277 // Methods for support type inquiry through isa, cast, and dyn_cast.
278 static inline bool classof(const StructType *) { return true; }
279 static inline bool classof(const Type *T) {
280 return T->getTypeID() == StructTyID;
284 /// SequentialType - This is the superclass of the array, pointer and vector
285 /// type classes. All of these represent "arrays" in memory. The array type
286 /// represents a specifically sized array, pointer types are unsized/unknown
287 /// size arrays, vector types represent specifically sized arrays that
288 /// allow for use of SIMD instructions. SequentialType holds the common
289 /// features of all, which stem from the fact that all three lay their
290 /// components out in memory identically.
292 class SequentialType : public CompositeType {
293 PATypeHandle ContainedType; ///< Storage for the single contained type.
294 SequentialType(const SequentialType &); // Do not implement!
295 const SequentialType &operator=(const SequentialType &); // Do not implement!
297 // avoiding warning: 'this' : used in base member initializer list
298 SequentialType *this_() { return this; }
300 SequentialType(TypeID TID, const Type *ElType)
301 : CompositeType(ElType->getContext(), TID), ContainedType(ElType, this_()) {
302 ContainedTys = &ContainedType;
307 const Type *getElementType() const { return ContainedTys[0]; }
309 // Methods for support type inquiry through isa, cast, and dyn_cast.
310 static inline bool classof(const SequentialType *) { return true; }
311 static inline bool classof(const Type *T) {
312 return T->getTypeID() == ArrayTyID ||
313 T->getTypeID() == PointerTyID ||
314 T->getTypeID() == VectorTyID;
319 /// ArrayType - Class to represent array types.
321 class ArrayType : public SequentialType {
322 friend class TypeMap<ArrayValType, ArrayType>;
323 uint64_t NumElements;
325 ArrayType(const ArrayType &); // Do not implement
326 const ArrayType &operator=(const ArrayType &); // Do not implement
327 ArrayType(const Type *ElType, uint64_t NumEl);
329 /// ArrayType::get - This static method is the primary way to construct an
332 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
334 /// isValidElementType - Return true if the specified type is valid as a
336 static bool isValidElementType(const Type *ElemTy);
338 uint64_t getNumElements() const { return NumElements; }
340 // Implement the AbstractTypeUser interface.
341 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
342 virtual void typeBecameConcrete(const DerivedType *AbsTy);
344 // Methods for support type inquiry through isa, cast, and dyn_cast.
345 static inline bool classof(const ArrayType *) { return true; }
346 static inline bool classof(const Type *T) {
347 return T->getTypeID() == ArrayTyID;
351 /// VectorType - Class to represent vector types.
353 class VectorType : public SequentialType {
354 friend class TypeMap<VectorValType, VectorType>;
355 unsigned NumElements;
357 VectorType(const VectorType &); // Do not implement
358 const VectorType &operator=(const VectorType &); // Do not implement
359 VectorType(const Type *ElType, unsigned NumEl);
361 /// VectorType::get - This static method is the primary way to construct an
364 static VectorType *get(const Type *ElementType, unsigned NumElements);
366 /// VectorType::getInteger - This static method gets a VectorType with the
367 /// same number of elements as the input type, and the element type is an
368 /// integer type of the same width as the input element type.
370 static VectorType *getInteger(const VectorType *VTy) {
371 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
372 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
373 return VectorType::get(EltTy, VTy->getNumElements());
376 /// VectorType::getExtendedElementVectorType - This static method is like
377 /// getInteger except that the element types are twice as wide as the
378 /// elements in the input type.
380 static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
381 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
382 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
383 return VectorType::get(EltTy, VTy->getNumElements());
386 /// VectorType::getTruncatedElementVectorType - This static method is like
387 /// getInteger except that the element types are half as wide as the
388 /// elements in the input type.
390 static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
391 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
392 assert((EltBits & 1) == 0 &&
393 "Cannot truncate vector element with odd bit-width");
394 const Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
395 return VectorType::get(EltTy, VTy->getNumElements());
398 /// isValidElementType - Return true if the specified type is valid as a
400 static bool isValidElementType(const Type *ElemTy);
402 /// @brief Return the number of elements in the Vector type.
403 unsigned getNumElements() const { return NumElements; }
405 /// @brief Return the number of bits in the Vector type.
406 unsigned getBitWidth() const {
407 return NumElements * getElementType()->getPrimitiveSizeInBits();
410 // Implement the AbstractTypeUser interface.
411 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
412 virtual void typeBecameConcrete(const DerivedType *AbsTy);
414 // Methods for support type inquiry through isa, cast, and dyn_cast.
415 static inline bool classof(const VectorType *) { return true; }
416 static inline bool classof(const Type *T) {
417 return T->getTypeID() == VectorTyID;
422 /// PointerType - Class to represent pointers.
424 class PointerType : public SequentialType {
425 friend class TypeMap<PointerValType, PointerType>;
427 PointerType(const PointerType &); // Do not implement
428 const PointerType &operator=(const PointerType &); // Do not implement
429 explicit PointerType(const Type *ElType, unsigned AddrSpace);
431 /// PointerType::get - This constructs a pointer to an object of the specified
432 /// type in a numbered address space.
433 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
435 /// PointerType::getUnqual - This constructs a pointer to an object of the
436 /// specified type in the generic address space (address space zero).
437 static PointerType *getUnqual(const Type *ElementType) {
438 return PointerType::get(ElementType, 0);
441 /// isValidElementType - Return true if the specified type is valid as a
443 static bool isValidElementType(const Type *ElemTy);
445 /// @brief Return the address space of the Pointer type.
446 inline unsigned getAddressSpace() const { return getSubclassData(); }
448 // Implement the AbstractTypeUser interface.
449 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
450 virtual void typeBecameConcrete(const DerivedType *AbsTy);
452 // Implement support type inquiry through isa, cast, and dyn_cast.
453 static inline bool classof(const PointerType *) { return true; }
454 static inline bool classof(const Type *T) {
455 return T->getTypeID() == PointerTyID;
460 /// OpaqueType - Class to represent opaque types.
462 class OpaqueType : public DerivedType {
463 friend class LLVMContextImpl;
464 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
465 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
466 OpaqueType(LLVMContext &C);
468 /// OpaqueType::get - Static factory method for the OpaqueType class.
470 static OpaqueType *get(LLVMContext &C);
472 // Implement support for type inquiry through isa, cast, and dyn_cast.
473 static inline bool classof(const OpaqueType *) { return true; }
474 static inline bool classof(const Type *T) {
475 return T->getTypeID() == OpaqueTyID;
479 } // End llvm namespace