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"
29 template<typename T
> class ArrayRef
;
32 class DerivedType
: public Type
{
34 explicit DerivedType(LLVMContext
&C
, TypeID id
) : Type(C
, id
) {}
37 // Methods for support type inquiry through isa, cast, and dyn_cast.
38 static inline bool classof(const DerivedType
*) { return true; }
39 static inline bool classof(const Type
*T
) {
40 return T
->isDerivedType();
44 /// Class to represent integer types. Note that this class is also used to
45 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
47 /// @brief Integer representation type
48 class IntegerType
: public DerivedType
{
49 friend class LLVMContextImpl
;
52 explicit IntegerType(LLVMContext
&C
, unsigned NumBits
) :
53 DerivedType(C
, IntegerTyID
) {
54 setSubclassData(NumBits
);
57 /// This enum is just used to hold constants we need for IntegerType.
59 MIN_INT_BITS
= 1, ///< Minimum number of bits that can be specified
60 MAX_INT_BITS
= (1<<23)-1 ///< Maximum number of bits that can be specified
61 ///< Note that bit width is stored in the Type classes SubclassData field
62 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
65 /// This static method is the primary way of constructing an IntegerType.
66 /// If an IntegerType with the same NumBits value was previously instantiated,
67 /// that instance will be returned. Otherwise a new one will be created. Only
68 /// one instance with a given NumBits value is ever created.
69 /// @brief Get or create an IntegerType instance.
70 static IntegerType
*get(LLVMContext
&C
, unsigned NumBits
);
72 /// @brief Get the number of bits in this IntegerType
73 unsigned getBitWidth() const { return getSubclassData(); }
75 /// getBitMask - Return a bitmask with ones set for all of the bits
76 /// that can be set by an unsigned version of this type. This is 0xFF for
77 /// i8, 0xFFFF for i16, etc.
78 uint64_t getBitMask() const {
79 return ~uint64_t(0UL) >> (64-getBitWidth());
82 /// getSignBit - Return a uint64_t with just the most significant bit set (the
83 /// sign bit, if the value is treated as a signed number).
84 uint64_t getSignBit() const {
85 return 1ULL << (getBitWidth()-1);
88 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
89 /// @returns a bit mask with ones set for all the bits of this type.
90 /// @brief Get a bit mask for this type.
91 APInt
getMask() const;
93 /// This method determines if the width of this IntegerType is a power-of-2
94 /// in terms of 8 bit bytes.
95 /// @returns true if this is a power-of-2 byte width.
96 /// @brief Is this a power-of-2 byte-width IntegerType ?
97 bool isPowerOf2ByteWidth() const;
99 // Methods for support type inquiry through isa, cast, and dyn_cast.
100 static inline bool classof(const IntegerType
*) { return true; }
101 static inline bool classof(const Type
*T
) {
102 return T
->getTypeID() == IntegerTyID
;
107 /// FunctionType - Class to represent function types
109 class FunctionType
: public DerivedType
{
110 FunctionType(const FunctionType
&); // Do not implement
111 const FunctionType
&operator=(const FunctionType
&); // Do not implement
112 FunctionType(const Type
*Result
, ArrayRef
<Type
*> Params
, bool IsVarArgs
);
115 /// FunctionType::get - This static method is the primary way of constructing
118 static FunctionType
*get(const Type
*Result
,
119 ArrayRef
<const Type
*> Params
, bool isVarArg
);
120 static FunctionType
*get(const Type
*Result
,
121 ArrayRef
<Type
*> Params
, bool isVarArg
);
123 /// FunctionType::get - Create a FunctionType taking no parameters.
125 static FunctionType
*get(const Type
*Result
, bool isVarArg
);
127 /// isValidReturnType - Return true if the specified type is valid as a return
129 static bool isValidReturnType(const Type
*RetTy
);
131 /// isValidArgumentType - Return true if the specified type is valid as an
133 static bool isValidArgumentType(const Type
*ArgTy
);
135 bool isVarArg() const { return getSubclassData(); }
136 Type
*getReturnType() const { return ContainedTys
[0]; }
138 typedef Type::subtype_iterator param_iterator
;
139 param_iterator
param_begin() const { return ContainedTys
+ 1; }
140 param_iterator
param_end() const { return &ContainedTys
[NumContainedTys
]; }
142 // Parameter type accessors.
143 Type
*getParamType(unsigned i
) const { return ContainedTys
[i
+1]; }
145 /// getNumParams - Return the number of fixed parameters this function type
146 /// requires. This does not consider varargs.
148 unsigned getNumParams() const { return NumContainedTys
- 1; }
150 // Methods for support type inquiry through isa, cast, and dyn_cast.
151 static inline bool classof(const FunctionType
*) { return true; }
152 static inline bool classof(const Type
*T
) {
153 return T
->getTypeID() == FunctionTyID
;
158 /// CompositeType - Common super class of ArrayType, StructType, PointerType
160 class CompositeType
: public DerivedType
{
162 explicit CompositeType(LLVMContext
&C
, TypeID tid
) : DerivedType(C
, tid
) { }
165 /// getTypeAtIndex - Given an index value into the type, return the type of
168 Type
*getTypeAtIndex(const Value
*V
) const;
169 Type
*getTypeAtIndex(unsigned Idx
) const;
170 bool indexValid(const Value
*V
) const;
171 bool indexValid(unsigned Idx
) const;
173 // Methods for support type inquiry through isa, cast, and dyn_cast.
174 static inline bool classof(const CompositeType
*) { return true; }
175 static inline bool classof(const Type
*T
) {
176 return T
->getTypeID() == ArrayTyID
||
177 T
->getTypeID() == StructTyID
||
178 T
->getTypeID() == PointerTyID
||
179 T
->getTypeID() == VectorTyID
;
184 /// StructType - Class to represent struct types, both normal and packed.
185 /// Besides being optionally packed, structs can be either "anonymous" or may
186 /// have an identity. Anonymous structs are uniqued by structural equivalence,
187 /// but types are each unique when created, and optionally have a name.
189 class StructType
: public CompositeType
{
190 StructType(const StructType
&); // Do not implement
191 const StructType
&operator=(const StructType
&); // Do not implement
192 StructType(LLVMContext
&C
)
193 : CompositeType(C
, StructTyID
), SymbolTableEntry(0) {}
195 // This is the contents of the SubClassData field.
201 /// SymbolTableEntry - For a named struct that actually has a name, this is a
202 /// pointer to the symbol table entry (maintained by LLVMContext) for the
203 /// struct. This is null if the type is an anonymous struct or if it is
205 void *SymbolTableEntry
;
207 /// StructType::createNamed - This creates a named struct with no body
208 /// specified. If the name is empty, it creates an unnamed struct, which has
209 /// a unique identity but no actual name.
210 static StructType
*createNamed(LLVMContext
&Context
, StringRef Name
);
212 static StructType
*createNamed(StringRef Name
, ArrayRef
<Type
*> Elements
,
213 bool isPacked
= false);
214 static StructType
*createNamed(LLVMContext
&Context
, StringRef Name
,
215 ArrayRef
<Type
*> Elements
,
216 bool isPacked
= false);
217 static StructType
*createNamed(StringRef Name
, Type
*elt1
, ...) END_WITH_NULL
;
219 /// StructType::get - This static method is the primary way to create a
222 /// FIXME: Remove the 'const Type*' version of this when types are pervasively
224 static StructType
*get(LLVMContext
&Context
, ArrayRef
<const Type
*> Elements
,
225 bool isPacked
= false);
226 static StructType
*get(LLVMContext
&Context
, ArrayRef
<Type
*> Elements
,
227 bool isPacked
= false);
229 /// StructType::get - Create an empty structure type.
231 static StructType
*get(LLVMContext
&Context
, bool isPacked
= false);
233 /// StructType::get - This static method is a convenience method for creating
234 /// structure types by specifying the elements as arguments. Note that this
235 /// method always returns a non-packed struct, and requires at least one
237 static StructType
*get(const Type
*elt1
, ...) END_WITH_NULL
;
239 bool isPacked() const { return (getSubclassData() & SCDB_Packed
) != 0; }
241 /// isAnonymous - Return true if this type is uniqued by structural
242 /// equivalence, false if it has an identity.
243 bool isAnonymous() const {return (getSubclassData() & SCDB_IsAnonymous
) != 0;}
245 /// isOpaque - Return true if this is a type with an identity that has no body
246 /// specified yet. These prints as 'opaque' in .ll files.
247 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody
) == 0; }
249 /// hasName - Return true if this is a named struct that has a non-empty name.
250 bool hasName() const { return SymbolTableEntry
!= 0; }
252 /// getName - Return the name for this struct type if it has an identity.
253 /// This may return an empty string for an unnamed struct type. Do not call
254 /// this on an anonymous type.
255 StringRef
getName() const;
257 /// setName - Change the name of this type to the specified name, or to a name
258 /// with a suffix if there is a collision. Do not call this on an anonymous
260 void setName(StringRef Name
);
262 /// setBody - Specify a body for an opaque type.
263 void setBody(ArrayRef
<Type
*> Elements
, bool isPacked
= false);
264 void setBody(Type
*elt1
, ...) END_WITH_NULL
;
266 /// isValidElementType - Return true if the specified type is valid as a
268 static bool isValidElementType(const Type
*ElemTy
);
271 // Iterator access to the elements.
272 typedef Type::subtype_iterator element_iterator
;
273 element_iterator
element_begin() const { return ContainedTys
; }
274 element_iterator
element_end() const { return &ContainedTys
[NumContainedTys
];}
276 /// isLayoutIdentical - Return true if this is layout identical to the
277 /// specified struct.
278 bool isLayoutIdentical(const StructType
*Other
) const;
280 // Random access to the elements
281 unsigned getNumElements() const { return NumContainedTys
; }
282 Type
*getElementType(unsigned N
) const {
283 assert(N
< NumContainedTys
&& "Element number out of range!");
284 return ContainedTys
[N
];
287 // Methods for support type inquiry through isa, cast, and dyn_cast.
288 static inline bool classof(const StructType
*) { return true; }
289 static inline bool classof(const Type
*T
) {
290 return T
->getTypeID() == StructTyID
;
294 /// SequentialType - This is the superclass of the array, pointer and vector
295 /// type classes. All of these represent "arrays" in memory. The array type
296 /// represents a specifically sized array, pointer types are unsized/unknown
297 /// size arrays, vector types represent specifically sized arrays that
298 /// allow for use of SIMD instructions. SequentialType holds the common
299 /// features of all, which stem from the fact that all three lay their
300 /// components out in memory identically.
302 class SequentialType
: public CompositeType
{
303 Type
*ContainedType
; ///< Storage for the single contained type.
304 SequentialType(const SequentialType
&); // Do not implement!
305 const SequentialType
&operator=(const SequentialType
&); // Do not implement!
308 SequentialType(TypeID TID
, Type
*ElType
)
309 : CompositeType(ElType
->getContext(), TID
), ContainedType(ElType
) {
310 ContainedTys
= &ContainedType
;
315 Type
*getElementType() const { return ContainedTys
[0]; }
317 // Methods for support type inquiry through isa, cast, and dyn_cast.
318 static inline bool classof(const SequentialType
*) { return true; }
319 static inline bool classof(const Type
*T
) {
320 return T
->getTypeID() == ArrayTyID
||
321 T
->getTypeID() == PointerTyID
||
322 T
->getTypeID() == VectorTyID
;
327 /// ArrayType - Class to represent array types.
329 class ArrayType
: public SequentialType
{
330 uint64_t NumElements
;
332 ArrayType(const ArrayType
&); // Do not implement
333 const ArrayType
&operator=(const ArrayType
&); // Do not implement
334 ArrayType(Type
*ElType
, uint64_t NumEl
);
336 /// ArrayType::get - This static method is the primary way to construct an
339 static ArrayType
*get(const Type
*ElementType
, uint64_t NumElements
);
341 /// isValidElementType - Return true if the specified type is valid as a
343 static bool isValidElementType(const Type
*ElemTy
);
345 uint64_t getNumElements() const { return NumElements
; }
347 // Methods for support type inquiry through isa, cast, and dyn_cast.
348 static inline bool classof(const ArrayType
*) { return true; }
349 static inline bool classof(const Type
*T
) {
350 return T
->getTypeID() == ArrayTyID
;
354 /// VectorType - Class to represent vector types.
356 class VectorType
: public SequentialType
{
357 unsigned NumElements
;
359 VectorType(const VectorType
&); // Do not implement
360 const VectorType
&operator=(const VectorType
&); // Do not implement
361 VectorType(Type
*ElType
, unsigned NumEl
);
363 /// VectorType::get - This static method is the primary way to construct an
366 static VectorType
*get(const Type
*ElementType
, unsigned NumElements
);
368 /// VectorType::getInteger - This static method gets a VectorType with the
369 /// same number of elements as the input type, and the element type is an
370 /// integer type of the same width as the input element type.
372 static VectorType
*getInteger(const VectorType
*VTy
) {
373 unsigned EltBits
= VTy
->getElementType()->getPrimitiveSizeInBits();
374 Type
*EltTy
= IntegerType::get(VTy
->getContext(), EltBits
);
375 return VectorType::get(EltTy
, VTy
->getNumElements());
378 /// VectorType::getExtendedElementVectorType - This static method is like
379 /// getInteger except that the element types are twice as wide as the
380 /// elements in the input type.
382 static VectorType
*getExtendedElementVectorType(const VectorType
*VTy
) {
383 unsigned EltBits
= VTy
->getElementType()->getPrimitiveSizeInBits();
384 Type
*EltTy
= IntegerType::get(VTy
->getContext(), EltBits
* 2);
385 return VectorType::get(EltTy
, VTy
->getNumElements());
388 /// VectorType::getTruncatedElementVectorType - This static method is like
389 /// getInteger except that the element types are half as wide as the
390 /// elements in the input type.
392 static VectorType
*getTruncatedElementVectorType(const VectorType
*VTy
) {
393 unsigned EltBits
= VTy
->getElementType()->getPrimitiveSizeInBits();
394 assert((EltBits
& 1) == 0 &&
395 "Cannot truncate vector element with odd bit-width");
396 Type
*EltTy
= IntegerType::get(VTy
->getContext(), EltBits
/ 2);
397 return VectorType::get(EltTy
, VTy
->getNumElements());
400 /// isValidElementType - Return true if the specified type is valid as a
402 static bool isValidElementType(const Type
*ElemTy
);
404 /// @brief Return the number of elements in the Vector type.
405 unsigned getNumElements() const { return NumElements
; }
407 /// @brief Return the number of bits in the Vector type.
408 unsigned getBitWidth() const {
409 return NumElements
* getElementType()->getPrimitiveSizeInBits();
412 // Methods for support type inquiry through isa, cast, and dyn_cast.
413 static inline bool classof(const VectorType
*) { return true; }
414 static inline bool classof(const Type
*T
) {
415 return T
->getTypeID() == VectorTyID
;
420 /// PointerType - Class to represent pointers.
422 class PointerType
: public SequentialType
{
423 PointerType(const PointerType
&); // Do not implement
424 const PointerType
&operator=(const PointerType
&); // Do not implement
425 explicit PointerType(Type
*ElType
, unsigned AddrSpace
);
427 /// PointerType::get - This constructs a pointer to an object of the specified
428 /// type in a numbered address space.
429 static PointerType
*get(const Type
*ElementType
, unsigned AddressSpace
);
431 /// PointerType::getUnqual - This constructs a pointer to an object of the
432 /// specified type in the generic address space (address space zero).
433 static PointerType
*getUnqual(const Type
*ElementType
) {
434 return PointerType::get(ElementType
, 0);
437 /// isValidElementType - Return true if the specified type is valid as a
439 static bool isValidElementType(const Type
*ElemTy
);
441 /// @brief Return the address space of the Pointer type.
442 inline unsigned getAddressSpace() const { return getSubclassData(); }
444 // Implement support type inquiry through isa, cast, and dyn_cast.
445 static inline bool classof(const PointerType
*) { return true; }
446 static inline bool classof(const Type
*T
) {
447 return T
->getTypeID() == PointerTyID
;
451 } // End llvm namespace