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 // "method 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"
26 template<class ValType, class TypeClass> class TypeMap;
27 class FunctionValType;
35 class DerivedType : public Type {
39 explicit DerivedType(TypeID id) : Type(id) {}
41 /// notifyUsesThatTypeBecameConcrete - Notify AbstractTypeUsers of this type
42 /// that the current type has transitioned from being abstract to being
45 void notifyUsesThatTypeBecameConcrete();
47 /// dropAllTypeUses - When this (abstract) type is resolved to be equal to
48 /// another (more concrete) type, we must eliminate all references to other
49 /// types, to avoid some circular reference problems.
51 void dropAllTypeUses();
53 /// unlockedRefineAbstractTypeTo - Internal version of refineAbstractTypeTo
54 /// that performs no locking. Only used for internal recursion.
55 void unlockedRefineAbstractTypeTo(const Type *NewType);
59 //===--------------------------------------------------------------------===//
60 // Abstract Type handling methods - These types have special lifetimes, which
61 // are managed by (add|remove)AbstractTypeUser. See comments in
62 // AbstractTypeUser.h for more information.
64 /// refineAbstractTypeTo - This function is used to when it is discovered that
65 /// the 'this' abstract type is actually equivalent to the NewType specified.
66 /// This causes all users of 'this' to switch to reference the more concrete
67 /// type NewType and for 'this' to be deleted.
69 void refineAbstractTypeTo(const Type *NewType);
71 void dump() const { Type::dump(); }
73 // Methods for support type inquiry through isa, cast, and dyn_cast:
74 static inline bool classof(const DerivedType *) { return true; }
75 static inline bool classof(const Type *T) {
76 return T->isDerivedType();
80 /// Class to represent integer types. Note that this class is also used to
81 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
83 /// @brief Integer representation type
84 class IntegerType : public DerivedType {
86 explicit IntegerType(unsigned NumBits) : DerivedType(IntegerTyID) {
87 setSubclassData(NumBits);
89 friend class TypeMap<IntegerValType, IntegerType>;
91 /// This enum is just used to hold constants we need for IntegerType.
93 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
94 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
95 ///< Note that bit width is stored in the Type classes SubclassData field
96 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
99 /// This static method is the primary way of constructing an IntegerType.
100 /// If an IntegerType with the same NumBits value was previously instantiated,
101 /// that instance will be returned. Otherwise a new one will be created. Only
102 /// one instance with a given NumBits value is ever created.
103 /// @brief Get or create an IntegerType instance.
104 static const IntegerType* get(unsigned NumBits);
106 /// @brief Get the number of bits in this IntegerType
107 unsigned getBitWidth() const { return getSubclassData(); }
109 /// getBitMask - Return a bitmask with ones set for all of the bits
110 /// that can be set by an unsigned version of this type. This is 0xFF for
111 /// i8, 0xFFFF for i16, etc.
112 uint64_t getBitMask() const {
113 return ~uint64_t(0UL) >> (64-getBitWidth());
116 /// getSignBit - Return a uint64_t with just the most significant bit set (the
117 /// sign bit, if the value is treated as a signed number).
118 uint64_t getSignBit() const {
119 return 1ULL << (getBitWidth()-1);
122 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
123 /// @returns a bit mask with ones set for all the bits of this type.
124 /// @brief Get a bit mask for this type.
125 APInt getMask() const;
127 /// This method determines if the width of this IntegerType is a power-of-2
128 /// in terms of 8 bit bytes.
129 /// @returns true if this is a power-of-2 byte width.
130 /// @brief Is this a power-of-2 byte-width IntegerType ?
131 bool isPowerOf2ByteWidth() const;
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const IntegerType *) { return true; }
135 static inline bool classof(const Type *T) {
136 return T->getTypeID() == IntegerTyID;
141 /// FunctionType - Class to represent function types
143 class FunctionType : public DerivedType {
144 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, const std::vector<const Type*> &Params,
153 /// FunctionType::get - This static method is the primary way of constructing
156 static FunctionType *get(
157 const Type *Result, ///< The result type
158 const std::vector<const Type*> &Params, ///< The types of the parameters
159 bool isVarArg ///< Whether this is a variable argument length function
162 /// FunctionType::get - Create a FunctionType taking no parameters.
164 static FunctionType *get(
165 const Type *Result, ///< The result type
166 bool isVarArg ///< Whether this is a variable argument length function
168 return get(Result, std::vector<const Type *>(), isVarArg);
171 /// isValidReturnType - Return true if the specified type is valid as a return
173 static bool isValidReturnType(const Type *RetTy);
175 /// isValidArgumentType - Return true if the specified type is valid as an
177 static bool isValidArgumentType(const Type *ArgTy);
179 inline bool isVarArg() const { return isVarArgs; }
180 inline const Type *getReturnType() const { return ContainedTys[0]; }
182 typedef Type::subtype_iterator param_iterator;
183 param_iterator param_begin() const { return ContainedTys + 1; }
184 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
186 // Parameter type accessors...
187 const Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
189 /// getNumParams - Return the number of fixed parameters this function type
190 /// requires. This does not consider varargs.
192 unsigned getNumParams() const { return NumContainedTys - 1; }
194 // Implement the AbstractTypeUser interface.
195 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
196 virtual void typeBecameConcrete(const DerivedType *AbsTy);
198 // Methods for support type inquiry through isa, cast, and dyn_cast:
199 static inline bool classof(const FunctionType *) { return true; }
200 static inline bool classof(const Type *T) {
201 return T->getTypeID() == FunctionTyID;
206 /// CompositeType - Common super class of ArrayType, StructType, PointerType
208 class CompositeType : public DerivedType {
210 inline explicit CompositeType(TypeID id) : DerivedType(id) { }
213 /// getTypeAtIndex - Given an index value into the type, return the type of
216 virtual const Type *getTypeAtIndex(const Value *V) const = 0;
217 virtual const Type *getTypeAtIndex(unsigned Idx) const = 0;
218 virtual bool indexValid(const Value *V) const = 0;
219 virtual bool indexValid(unsigned Idx) const = 0;
221 // Methods for support type inquiry through isa, cast, and dyn_cast:
222 static inline bool classof(const CompositeType *) { return true; }
223 static inline bool classof(const Type *T) {
224 return T->getTypeID() == ArrayTyID ||
225 T->getTypeID() == StructTyID ||
226 T->getTypeID() == PointerTyID ||
227 T->getTypeID() == VectorTyID;
232 /// StructType - Class to represent struct types
234 class StructType : public CompositeType {
235 friend class TypeMap<StructValType, StructType>;
236 StructType(const StructType &); // Do not implement
237 const StructType &operator=(const StructType &); // Do not implement
238 StructType(const std::vector<const Type*> &Types, bool isPacked);
240 /// StructType::get - This static method is the primary way to create a
243 static StructType *get(const std::vector<const Type*> &Params,
244 bool isPacked=false);
246 /// StructType::get - Create an empty structure type.
248 static StructType *get(bool isPacked=false) {
249 return get(std::vector<const Type*>(), isPacked);
252 /// StructType::get - This static method is a convenience method for
253 /// creating structure types by specifying the elements as arguments.
254 /// Note that this method always returns a non-packed struct. To get
255 /// an empty struct, pass NULL, NULL.
256 static StructType *get(const Type *type, ...) END_WITH_NULL;
258 /// isValidElementType - Return true if the specified type is valid as a
260 static bool isValidElementType(const Type *ElemTy);
262 // Iterator access to the elements
263 typedef Type::subtype_iterator element_iterator;
264 element_iterator element_begin() const { return ContainedTys; }
265 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
267 // Random access to the elements
268 unsigned getNumElements() const { return NumContainedTys; }
269 const Type *getElementType(unsigned N) const {
270 assert(N < NumContainedTys && "Element number out of range!");
271 return ContainedTys[N];
274 /// getTypeAtIndex - Given an index value into the type, return the type of
275 /// the element. For a structure type, this must be a constant value...
277 virtual const Type *getTypeAtIndex(const Value *V) const;
278 virtual const Type *getTypeAtIndex(unsigned Idx) const;
279 virtual bool indexValid(const Value *V) const;
280 virtual bool indexValid(unsigned Idx) const;
282 // Implement the AbstractTypeUser interface.
283 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
284 virtual void typeBecameConcrete(const DerivedType *AbsTy);
286 // Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const StructType *) { return true; }
288 static inline bool classof(const Type *T) {
289 return T->getTypeID() == StructTyID;
292 bool isPacked() const { return (0 != getSubclassData()) ? true : false; }
296 /// SequentialType - This is the superclass of the array, pointer and vector
297 /// type classes. All of these represent "arrays" in memory. The array type
298 /// represents a specifically sized array, pointer types are unsized/unknown
299 /// size arrays, vector types represent specifically sized arrays that
300 /// allow for use of SIMD instructions. SequentialType holds the common
301 /// features of all, which stem from the fact that all three lay their
302 /// components out in memory identically.
304 class SequentialType : public CompositeType {
305 PATypeHandle ContainedType; ///< Storage for the single contained type
306 SequentialType(const SequentialType &); // Do not implement!
307 const SequentialType &operator=(const SequentialType &); // Do not implement!
309 // avoiding warning: 'this' : used in base member initializer list
310 SequentialType* this_() { return this; }
312 SequentialType(TypeID TID, const Type *ElType)
313 : CompositeType(TID), ContainedType(ElType, this_()) {
314 ContainedTys = &ContainedType;
319 inline const Type *getElementType() const { return ContainedTys[0]; }
321 virtual bool indexValid(const Value *V) const;
322 virtual bool indexValid(unsigned) const {
326 /// getTypeAtIndex - Given an index value into the type, return the type of
327 /// the element. For sequential types, there is only one subtype...
329 virtual const Type *getTypeAtIndex(const Value *) const {
330 return ContainedTys[0];
332 virtual const Type *getTypeAtIndex(unsigned) const {
333 return ContainedTys[0];
336 // Methods for support type inquiry through isa, cast, and dyn_cast:
337 static inline bool classof(const SequentialType *) { return true; }
338 static inline bool classof(const Type *T) {
339 return T->getTypeID() == ArrayTyID ||
340 T->getTypeID() == PointerTyID ||
341 T->getTypeID() == VectorTyID;
346 /// ArrayType - Class to represent array types
348 class ArrayType : public SequentialType {
349 friend class TypeMap<ArrayValType, ArrayType>;
350 uint64_t NumElements;
352 ArrayType(const ArrayType &); // Do not implement
353 const ArrayType &operator=(const ArrayType &); // Do not implement
354 ArrayType(const Type *ElType, uint64_t NumEl);
356 /// ArrayType::get - This static method is the primary way to construct an
359 static ArrayType *get(const Type *ElementType, uint64_t NumElements);
361 /// isValidElementType - Return true if the specified type is valid as a
363 static bool isValidElementType(const Type *ElemTy);
365 inline uint64_t getNumElements() const { return NumElements; }
367 // Implement the AbstractTypeUser interface.
368 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
369 virtual void typeBecameConcrete(const DerivedType *AbsTy);
371 // Methods for support type inquiry through isa, cast, and dyn_cast:
372 static inline bool classof(const ArrayType *) { return true; }
373 static inline bool classof(const Type *T) {
374 return T->getTypeID() == ArrayTyID;
378 /// VectorType - Class to represent vector types
380 class VectorType : public SequentialType {
381 friend class TypeMap<VectorValType, VectorType>;
382 unsigned NumElements;
384 VectorType(const VectorType &); // Do not implement
385 const VectorType &operator=(const VectorType &); // Do not implement
386 VectorType(const Type *ElType, unsigned NumEl);
388 /// VectorType::get - This static method is the primary way to construct an
391 static VectorType *get(const Type *ElementType, unsigned NumElements);
393 /// VectorType::getInteger - This static method gets a VectorType with the
394 /// same number of elements as the input type, and the element type is an
395 /// integer type of the same width as the input element type.
397 static VectorType *getInteger(const VectorType *VTy) {
398 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
399 const Type *EltTy = IntegerType::get(EltBits);
400 return VectorType::get(EltTy, VTy->getNumElements());
403 /// VectorType::getExtendedElementVectorType - This static method is like
404 /// getInteger except that the element types are twice as wide as the
405 /// elements in the input type.
407 static VectorType *getExtendedElementVectorType(const VectorType *VTy) {
408 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
409 const Type *EltTy = IntegerType::get(EltBits * 2);
410 return VectorType::get(EltTy, VTy->getNumElements());
413 /// VectorType::getTruncatedElementVectorType - This static method is like
414 /// getInteger except that the element types are half as wide as the
415 /// elements in the input type.
417 static VectorType *getTruncatedElementVectorType(const VectorType *VTy) {
418 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
419 assert((EltBits & 1) == 0 &&
420 "Cannot truncate vector element with odd bit-width");
421 const Type *EltTy = IntegerType::get(EltBits / 2);
422 return VectorType::get(EltTy, VTy->getNumElements());
425 /// isValidElementType - Return true if the specified type is valid as a
427 static bool isValidElementType(const Type *ElemTy);
429 /// @brief Return the number of elements in the Vector type.
430 inline unsigned getNumElements() const { return NumElements; }
432 /// @brief Return the number of bits in the Vector type.
433 inline unsigned getBitWidth() const {
434 return NumElements *getElementType()->getPrimitiveSizeInBits();
437 // Implement the AbstractTypeUser interface.
438 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
439 virtual void typeBecameConcrete(const DerivedType *AbsTy);
441 // Methods for support type inquiry through isa, cast, and dyn_cast:
442 static inline bool classof(const VectorType *) { return true; }
443 static inline bool classof(const Type *T) {
444 return T->getTypeID() == VectorTyID;
449 /// PointerType - Class to represent pointers
451 class PointerType : public SequentialType {
452 friend class TypeMap<PointerValType, PointerType>;
453 unsigned AddressSpace;
455 PointerType(const PointerType &); // Do not implement
456 const PointerType &operator=(const PointerType &); // Do not implement
457 explicit PointerType(const Type *ElType, unsigned AddrSpace);
459 /// PointerType::get - This constructs a pointer to an object of the specified
460 /// type in a numbered address space.
461 static PointerType *get(const Type *ElementType, unsigned AddressSpace);
463 /// PointerType::getUnqual - This constructs a pointer to an object of the
464 /// specified type in the generic address space (address space zero).
465 static PointerType *getUnqual(const Type *ElementType) {
466 return PointerType::get(ElementType, 0);
469 /// isValidElementType - Return true if the specified type is valid as a
471 static bool isValidElementType(const Type *ElemTy);
473 /// @brief Return the address space of the Pointer type.
474 inline unsigned getAddressSpace() const { return AddressSpace; }
476 // Implement the AbstractTypeUser interface.
477 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
478 virtual void typeBecameConcrete(const DerivedType *AbsTy);
480 // Implement support type inquiry through isa, cast, and dyn_cast:
481 static inline bool classof(const PointerType *) { return true; }
482 static inline bool classof(const Type *T) {
483 return T->getTypeID() == PointerTyID;
488 /// OpaqueType - Class to represent abstract types
490 class OpaqueType : public DerivedType {
491 OpaqueType(const OpaqueType &); // DO NOT IMPLEMENT
492 const OpaqueType &operator=(const OpaqueType &); // DO NOT IMPLEMENT
495 /// OpaqueType::get - Static factory method for the OpaqueType class...
497 static OpaqueType *get() {
498 return new OpaqueType(); // All opaque types are distinct
501 // Implement support for type inquiry through isa, cast, and dyn_cast:
502 static inline bool classof(const OpaqueType *) { return true; }
503 static inline bool classof(const Type *T) {
504 return T->getTypeID() == OpaqueTyID;
508 } // End llvm namespace