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_IR_DERIVEDTYPES_H
19 #define LLVM_IR_DERIVEDTYPES_H
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
30 template<typename T> class ArrayRef;
33 /// Class to represent integer types. Note that this class is also used to
34 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
36 /// @brief Integer representation type
37 class IntegerType : public Type {
38 friend class LLVMContextImpl;
41 explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
42 setSubclassData(NumBits);
45 /// This enum is just used to hold constants we need for IntegerType.
47 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
48 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
49 ///< Note that bit width is stored in the Type classes SubclassData field
50 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
53 /// This static method is the primary way of constructing an IntegerType.
54 /// If an IntegerType with the same NumBits value was previously instantiated,
55 /// that instance will be returned. Otherwise a new one will be created. Only
56 /// one instance with a given NumBits value is ever created.
57 /// @brief Get or create an IntegerType instance.
58 static IntegerType *get(LLVMContext &C, unsigned NumBits);
60 /// @brief Get the number of bits in this IntegerType
61 unsigned getBitWidth() const { return getSubclassData(); }
63 /// getBitMask - Return a bitmask with ones set for all of the bits
64 /// that can be set by an unsigned version of this type. This is 0xFF for
65 /// i8, 0xFFFF for i16, etc.
66 uint64_t getBitMask() const {
67 return ~uint64_t(0UL) >> (64-getBitWidth());
70 /// getSignBit - Return a uint64_t with just the most significant bit set (the
71 /// sign bit, if the value is treated as a signed number).
72 uint64_t getSignBit() const {
73 return 1ULL << (getBitWidth()-1);
76 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
77 /// @returns a bit mask with ones set for all the bits of this type.
78 /// @brief Get a bit mask for this type.
79 APInt getMask() const;
81 /// This method determines if the width of this IntegerType is a power-of-2
82 /// in terms of 8 bit bytes.
83 /// @returns true if this is a power-of-2 byte width.
84 /// @brief Is this a power-of-2 byte-width IntegerType ?
85 bool isPowerOf2ByteWidth() const;
87 /// Methods for support type inquiry through isa, cast, and dyn_cast.
88 static inline bool classof(const Type *T) {
89 return T->getTypeID() == IntegerTyID;
94 /// FunctionType - Class to represent function types
96 class FunctionType : public Type {
97 FunctionType(const FunctionType &) = delete;
98 const FunctionType &operator=(const FunctionType &) = delete;
99 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
102 /// FunctionType::get - This static method is the primary way of constructing
105 static FunctionType *get(Type *Result,
106 ArrayRef<Type*> Params, bool isVarArg);
108 /// FunctionType::get - Create a FunctionType taking no parameters.
110 static FunctionType *get(Type *Result, bool isVarArg);
112 /// isValidReturnType - Return true if the specified type is valid as a return
114 static bool isValidReturnType(Type *RetTy);
116 /// isValidArgumentType - Return true if the specified type is valid as an
118 static bool isValidArgumentType(Type *ArgTy);
120 bool isVarArg() const { return getSubclassData()!=0; }
121 Type *getReturnType() const { return ContainedTys[0]; }
123 typedef Type::subtype_iterator param_iterator;
124 param_iterator param_begin() const { return ContainedTys + 1; }
125 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
126 ArrayRef<Type *> params() const {
127 return makeArrayRef(param_begin(), param_end());
130 /// Parameter type accessors.
131 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
133 /// getNumParams - Return the number of fixed parameters this function type
134 /// requires. This does not consider varargs.
136 unsigned getNumParams() const { return NumContainedTys - 1; }
138 /// Methods for support type inquiry through isa, cast, and dyn_cast.
139 static inline bool classof(const Type *T) {
140 return T->getTypeID() == FunctionTyID;
145 /// CompositeType - Common super class of ArrayType, StructType, PointerType
147 class CompositeType : public Type {
149 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
152 /// getTypeAtIndex - Given an index value into the type, return the type of
155 Type *getTypeAtIndex(const Value *V);
156 Type *getTypeAtIndex(unsigned Idx);
157 bool indexValid(const Value *V) const;
158 bool indexValid(unsigned Idx) const;
160 /// Methods for support type inquiry through isa, cast, and dyn_cast.
161 static inline bool classof(const Type *T) {
162 return T->getTypeID() == ArrayTyID ||
163 T->getTypeID() == StructTyID ||
164 T->getTypeID() == PointerTyID ||
165 T->getTypeID() == VectorTyID;
170 /// StructType - Class to represent struct types. There are two different kinds
171 /// of struct types: Literal structs and Identified structs.
173 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
174 /// always have a body when created. You can get one of these by using one of
175 /// the StructType::get() forms.
177 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
178 /// uniqued. The names for identified structs are managed at the LLVMContext
179 /// level, so there can only be a single identified struct with a given name in
180 /// a particular LLVMContext. Identified structs may also optionally be opaque
181 /// (have no body specified). You get one of these by using one of the
182 /// StructType::create() forms.
184 /// Independent of what kind of struct you have, the body of a struct type are
185 /// laid out in memory consequtively with the elements directly one after the
186 /// other (if the struct is packed) or (if not packed) with padding between the
187 /// elements as defined by DataLayout (which is required to match what the code
188 /// generator for a target expects).
190 class StructType : public CompositeType {
191 StructType(const StructType &) = delete;
192 const StructType &operator=(const StructType &) = delete;
193 StructType(LLVMContext &C)
194 : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
196 /// This is the contents of the SubClassData field.
203 /// SymbolTableEntry - For a named struct that actually has a name, this is a
204 /// pointer to the symbol table entry (maintained by LLVMContext) for the
205 /// struct. This is null if the type is an literal struct or if it is
206 /// a identified type that has an empty name.
208 void *SymbolTableEntry;
211 /// StructType::create - This creates an identified struct.
212 static StructType *create(LLVMContext &Context, StringRef Name);
213 static StructType *create(LLVMContext &Context);
215 static StructType *create(ArrayRef<Type*> Elements,
217 bool isPacked = false);
218 static StructType *create(ArrayRef<Type*> Elements);
219 static StructType *create(LLVMContext &Context,
220 ArrayRef<Type*> Elements,
222 bool isPacked = false);
223 static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
224 static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
226 /// StructType::get - This static method is the primary way to create a
227 /// literal StructType.
228 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
229 bool isPacked = false);
231 /// StructType::get - Create an empty structure type.
233 static StructType *get(LLVMContext &Context, bool isPacked = false);
235 /// StructType::get - This static method is a convenience method for creating
236 /// structure types by specifying the elements as arguments. Note that this
237 /// method always returns a non-packed struct, and requires at least one
239 static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
241 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
243 /// isLiteral - Return true if this type is uniqued by structural
244 /// equivalence, false if it is a struct definition.
245 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
247 /// isOpaque - Return true if this is a type with an identity that has no body
248 /// specified yet. These prints as 'opaque' in .ll files.
249 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
251 /// isSized - Return true if this is a sized type.
252 bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const;
254 /// hasName - Return true if this is a named struct that has a non-empty name.
255 bool hasName() const { return SymbolTableEntry != nullptr; }
257 /// getName - Return the name for this struct type if it has an identity.
258 /// This may return an empty string for an unnamed struct type. Do not call
259 /// this on an literal type.
260 StringRef getName() const;
262 /// setName - Change the name of this type to the specified name, or to a name
263 /// with a suffix if there is a collision. Do not call this on an literal
265 void setName(StringRef Name);
267 /// setBody - Specify a body for an opaque identified type.
268 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
269 void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
271 /// isValidElementType - Return true if the specified type is valid as a
273 static bool isValidElementType(Type *ElemTy);
276 // Iterator access to the elements.
277 typedef Type::subtype_iterator element_iterator;
278 element_iterator element_begin() const { return ContainedTys; }
279 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
280 ArrayRef<Type *> const elements() const {
281 return makeArrayRef(element_begin(), element_end());
284 /// isLayoutIdentical - Return true if this is layout identical to the
285 /// specified struct.
286 bool isLayoutIdentical(StructType *Other) const;
288 /// Random access to the elements
289 unsigned getNumElements() const { return NumContainedTys; }
290 Type *getElementType(unsigned N) const {
291 assert(N < NumContainedTys && "Element number out of range!");
292 return ContainedTys[N];
295 /// Methods for support type inquiry through isa, cast, and dyn_cast.
296 static inline bool classof(const Type *T) {
297 return T->getTypeID() == StructTyID;
301 /// SequentialType - This is the superclass of the array, pointer and vector
302 /// type classes. All of these represent "arrays" in memory. The array type
303 /// represents a specifically sized array, pointer types are unsized/unknown
304 /// size arrays, vector types represent specifically sized arrays that
305 /// allow for use of SIMD instructions. SequentialType holds the common
306 /// features of all, which stem from the fact that all three lay their
307 /// components out in memory identically.
309 class SequentialType : public CompositeType {
310 Type *ContainedType; ///< Storage for the single contained type.
311 SequentialType(const SequentialType &) = delete;
312 const SequentialType &operator=(const SequentialType &) = delete;
315 SequentialType(TypeID TID, Type *ElType)
316 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
317 ContainedTys = &ContainedType;
322 Type *getElementType() const { return ContainedTys[0]; }
324 /// Methods for support type inquiry through isa, cast, and dyn_cast.
325 static inline bool classof(const Type *T) {
326 return T->getTypeID() == ArrayTyID ||
327 T->getTypeID() == PointerTyID ||
328 T->getTypeID() == VectorTyID;
333 /// ArrayType - Class to represent array types.
335 class ArrayType : public SequentialType {
336 uint64_t NumElements;
338 ArrayType(const ArrayType &) = delete;
339 const ArrayType &operator=(const ArrayType &) = delete;
340 ArrayType(Type *ElType, uint64_t NumEl);
342 /// ArrayType::get - This static method is the primary way to construct an
345 static ArrayType *get(Type *ElementType, uint64_t NumElements);
347 /// isValidElementType - Return true if the specified type is valid as a
349 static bool isValidElementType(Type *ElemTy);
351 uint64_t getNumElements() const { return NumElements; }
353 /// Methods for support type inquiry through isa, cast, and dyn_cast.
354 static inline bool classof(const Type *T) {
355 return T->getTypeID() == ArrayTyID;
359 /// VectorType - Class to represent vector types.
361 class VectorType : public SequentialType {
362 unsigned NumElements;
364 VectorType(const VectorType &) = delete;
365 const VectorType &operator=(const VectorType &) = delete;
366 VectorType(Type *ElType, unsigned NumEl);
368 /// VectorType::get - This static method is the primary way to construct an
371 static VectorType *get(Type *ElementType, unsigned NumElements);
373 /// VectorType::getInteger - This static method gets a VectorType with the
374 /// same number of elements as the input type, and the element type is an
375 /// integer type of the same width as the input element type.
377 static VectorType *getInteger(VectorType *VTy) {
378 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
379 assert(EltBits && "Element size must be of a non-zero size");
380 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
381 return VectorType::get(EltTy, VTy->getNumElements());
384 /// VectorType::getExtendedElementVectorType - This static method is like
385 /// getInteger except that the element types are twice as wide as the
386 /// elements in the input type.
388 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
389 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
390 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
391 return VectorType::get(EltTy, VTy->getNumElements());
394 /// VectorType::getTruncatedElementVectorType - This static method is like
395 /// getInteger except that the element types are half as wide as the
396 /// elements in the input type.
398 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
399 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
400 assert((EltBits & 1) == 0 &&
401 "Cannot truncate vector element with odd bit-width");
402 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
403 return VectorType::get(EltTy, VTy->getNumElements());
406 /// VectorType::getHalfElementsVectorType - This static method returns
407 /// a VectorType with half as many elements as the input type and the
408 /// same element type.
410 static VectorType *getHalfElementsVectorType(VectorType *VTy) {
411 unsigned NumElts = VTy->getNumElements();
412 assert ((NumElts & 1) == 0 &&
413 "Cannot halve vector with odd number of elements.");
414 return VectorType::get(VTy->getElementType(), NumElts/2);
417 /// VectorType::getDoubleElementsVectorType - This static method returns
418 /// a VectorType with twice as many elements as the input type and the
419 /// same element type.
421 static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
422 unsigned NumElts = VTy->getNumElements();
423 return VectorType::get(VTy->getElementType(), NumElts*2);
426 /// isValidElementType - Return true if the specified type is valid as a
428 static bool isValidElementType(Type *ElemTy);
430 /// @brief Return the number of elements in the Vector type.
431 unsigned getNumElements() const { return NumElements; }
433 /// @brief Return the number of bits in the Vector type.
434 /// Returns zero when the vector is a vector of pointers.
435 unsigned getBitWidth() const {
436 return NumElements * getElementType()->getPrimitiveSizeInBits();
439 /// Methods for support type inquiry through isa, cast, and dyn_cast.
440 static inline bool classof(const Type *T) {
441 return T->getTypeID() == VectorTyID;
446 /// PointerType - Class to represent pointers.
448 class PointerType : public SequentialType {
449 PointerType(const PointerType &) = delete;
450 const PointerType &operator=(const PointerType &) = delete;
451 explicit PointerType(Type *ElType, unsigned AddrSpace);
453 /// PointerType::get - This constructs a pointer to an object of the specified
454 /// type in a numbered address space.
455 static PointerType *get(Type *ElementType, unsigned AddressSpace);
457 /// PointerType::getUnqual - This constructs a pointer to an object of the
458 /// specified type in the generic address space (address space zero).
459 static PointerType *getUnqual(Type *ElementType) {
460 return PointerType::get(ElementType, 0);
463 /// isValidElementType - Return true if the specified type is valid as a
465 static bool isValidElementType(Type *ElemTy);
467 /// Return true if we can load or store from a pointer to this type.
468 static bool isLoadableOrStorableType(Type *ElemTy);
470 /// @brief Return the address space of the Pointer type.
471 inline unsigned getAddressSpace() const { return getSubclassData(); }
473 /// Implement support type inquiry through isa, cast, and dyn_cast.
474 static inline bool classof(const Type *T) {
475 return T->getTypeID() == PointerTyID;
479 } // End llvm namespace