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);
46 /// This enum is just used to hold constants we need for IntegerType.
48 MIN_INT_BITS = 1, ///< Minimum number of bits that can be specified
49 MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
50 ///< Note that bit width is stored in the Type classes SubclassData field
51 ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
54 /// This static method is the primary way of constructing an IntegerType.
55 /// If an IntegerType with the same NumBits value was previously instantiated,
56 /// that instance will be returned. Otherwise a new one will be created. Only
57 /// one instance with a given NumBits value is ever created.
58 /// @brief Get or create an IntegerType instance.
59 static IntegerType *get(LLVMContext &C, unsigned NumBits);
61 /// @brief Get the number of bits in this IntegerType
62 unsigned getBitWidth() const { return getSubclassData(); }
64 /// getBitMask - Return a bitmask with ones set for all of the bits
65 /// that can be set by an unsigned version of this type. This is 0xFF for
66 /// i8, 0xFFFF for i16, etc.
67 uint64_t getBitMask() const {
68 return ~uint64_t(0UL) >> (64-getBitWidth());
71 /// getSignBit - Return a uint64_t with just the most significant bit set (the
72 /// sign bit, if the value is treated as a signed number).
73 uint64_t getSignBit() const {
74 return 1ULL << (getBitWidth()-1);
77 /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
78 /// @returns a bit mask with ones set for all the bits of this type.
79 /// @brief Get a bit mask for this type.
80 APInt getMask() const;
82 /// This method determines if the width of this IntegerType is a power-of-2
83 /// in terms of 8 bit bytes.
84 /// @returns true if this is a power-of-2 byte width.
85 /// @brief Is this a power-of-2 byte-width IntegerType ?
86 bool isPowerOf2ByteWidth() const;
88 /// Methods for support type inquiry through isa, cast, and dyn_cast.
89 static inline bool classof(const Type *T) {
90 return T->getTypeID() == IntegerTyID;
94 unsigned Type::getIntegerBitWidth() const {
95 return cast<IntegerType>(this)->getBitWidth();
98 /// FunctionType - Class to represent function types
100 class FunctionType : public Type {
101 FunctionType(const FunctionType &) = delete;
102 const FunctionType &operator=(const FunctionType &) = delete;
103 FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
106 /// FunctionType::get - This static method is the primary way of constructing
109 static FunctionType *get(Type *Result,
110 ArrayRef<Type*> Params, bool isVarArg);
112 /// FunctionType::get - Create a FunctionType taking no parameters.
114 static FunctionType *get(Type *Result, bool isVarArg);
116 /// isValidReturnType - Return true if the specified type is valid as a return
118 static bool isValidReturnType(Type *RetTy);
120 /// isValidArgumentType - Return true if the specified type is valid as an
122 static bool isValidArgumentType(Type *ArgTy);
124 bool isVarArg() const { return getSubclassData()!=0; }
125 Type *getReturnType() const { return ContainedTys[0]; }
127 typedef Type::subtype_iterator param_iterator;
128 param_iterator param_begin() const { return ContainedTys + 1; }
129 param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
130 ArrayRef<Type *> params() const {
131 return makeArrayRef(param_begin(), param_end());
134 /// Parameter type accessors.
135 Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
137 /// getNumParams - Return the number of fixed parameters this function type
138 /// requires. This does not consider varargs.
140 unsigned getNumParams() const { return NumContainedTys - 1; }
142 /// Methods for support type inquiry through isa, cast, and dyn_cast.
143 static inline bool classof(const Type *T) {
144 return T->getTypeID() == FunctionTyID;
147 static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
148 "Alignment sufficient for objects appended to FunctionType");
150 bool Type::isFunctionVarArg() const {
151 return cast<FunctionType>(this)->isVarArg();
154 Type *Type::getFunctionParamType(unsigned i) const {
155 return cast<FunctionType>(this)->getParamType(i);
158 unsigned Type::getFunctionNumParams() const {
159 return cast<FunctionType>(this)->getNumParams();
162 /// CompositeType - Common super class of ArrayType, StructType, PointerType
164 class CompositeType : public Type {
166 explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {}
169 /// getTypeAtIndex - Given an index value into the type, return the type of
172 Type *getTypeAtIndex(const Value *V) const;
173 Type *getTypeAtIndex(unsigned Idx) const;
174 bool indexValid(const Value *V) const;
175 bool indexValid(unsigned Idx) const;
177 /// Methods for support type inquiry through isa, cast, and dyn_cast.
178 static inline bool classof(const Type *T) {
179 return T->getTypeID() == ArrayTyID ||
180 T->getTypeID() == StructTyID ||
181 T->getTypeID() == PointerTyID ||
182 T->getTypeID() == VectorTyID;
186 /// StructType - Class to represent struct types. There are two different kinds
187 /// of struct types: Literal structs and Identified structs.
189 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
190 /// always have a body when created. You can get one of these by using one of
191 /// the StructType::get() forms.
193 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
194 /// uniqued. The names for identified structs are managed at the LLVMContext
195 /// level, so there can only be a single identified struct with a given name in
196 /// a particular LLVMContext. Identified structs may also optionally be opaque
197 /// (have no body specified). You get one of these by using one of the
198 /// StructType::create() forms.
200 /// Independent of what kind of struct you have, the body of a struct type are
201 /// laid out in memory consequtively with the elements directly one after the
202 /// other (if the struct is packed) or (if not packed) with padding between the
203 /// elements as defined by DataLayout (which is required to match what the code
204 /// generator for a target expects).
206 class StructType : public CompositeType {
207 StructType(const StructType &) = delete;
208 const StructType &operator=(const StructType &) = delete;
209 StructType(LLVMContext &C)
210 : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
212 /// This is the contents of the SubClassData field.
219 /// SymbolTableEntry - For a named struct that actually has a name, this is a
220 /// pointer to the symbol table entry (maintained by LLVMContext) for the
221 /// struct. This is null if the type is an literal struct or if it is
222 /// a identified type that has an empty name.
224 void *SymbolTableEntry;
227 /// StructType::create - This creates an identified struct.
228 static StructType *create(LLVMContext &Context, StringRef Name);
229 static StructType *create(LLVMContext &Context);
231 static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
232 bool isPacked = false);
233 static StructType *create(ArrayRef<Type *> Elements);
234 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
235 StringRef Name, bool isPacked = false);
236 static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
237 static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
239 /// StructType::get - This static method is the primary way to create a
240 /// literal StructType.
241 static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
242 bool isPacked = false);
244 /// StructType::get - Create an empty structure type.
246 static StructType *get(LLVMContext &Context, bool isPacked = false);
248 /// StructType::get - This static method is a convenience method for creating
249 /// structure types by specifying the elements as arguments. Note that this
250 /// method always returns a non-packed struct, and requires at least one
252 static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
254 bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
256 /// isLiteral - Return true if this type is uniqued by structural
257 /// equivalence, false if it is a struct definition.
258 bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
260 /// isOpaque - Return true if this is a type with an identity that has no body
261 /// specified yet. These prints as 'opaque' in .ll files.
262 bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
264 /// isSized - Return true if this is a sized type.
265 bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
267 /// hasName - Return true if this is a named struct that has a non-empty name.
268 bool hasName() const { return SymbolTableEntry != nullptr; }
270 /// getName - Return the name for this struct type if it has an identity.
271 /// This may return an empty string for an unnamed struct type. Do not call
272 /// this on an literal type.
273 StringRef getName() const;
275 /// setName - Change the name of this type to the specified name, or to a name
276 /// with a suffix if there is a collision. Do not call this on an literal
278 void setName(StringRef Name);
280 /// setBody - Specify a body for an opaque identified type.
281 void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
282 void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
284 /// isValidElementType - Return true if the specified type is valid as a
286 static bool isValidElementType(Type *ElemTy);
288 // Iterator access to the elements.
289 typedef Type::subtype_iterator element_iterator;
290 element_iterator element_begin() const { return ContainedTys; }
291 element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
292 ArrayRef<Type *> const elements() const {
293 return makeArrayRef(element_begin(), element_end());
296 /// isLayoutIdentical - Return true if this is layout identical to the
297 /// specified struct.
298 bool isLayoutIdentical(StructType *Other) const;
300 /// Random access to the elements
301 unsigned getNumElements() const { return NumContainedTys; }
302 Type *getElementType(unsigned N) const {
303 assert(N < NumContainedTys && "Element number out of range!");
304 return ContainedTys[N];
307 /// Methods for support type inquiry through isa, cast, and dyn_cast.
308 static inline bool classof(const Type *T) {
309 return T->getTypeID() == StructTyID;
313 StringRef Type::getStructName() const {
314 return cast<StructType>(this)->getName();
317 unsigned Type::getStructNumElements() const {
318 return cast<StructType>(this)->getNumElements();
321 Type *Type::getStructElementType(unsigned N) const {
322 return cast<StructType>(this)->getElementType(N);
325 /// SequentialType - This is the superclass of the array, pointer and vector
326 /// type classes. All of these represent "arrays" in memory. The array type
327 /// represents a specifically sized array, pointer types are unsized/unknown
328 /// size arrays, vector types represent specifically sized arrays that
329 /// allow for use of SIMD instructions. SequentialType holds the common
330 /// features of all, which stem from the fact that all three lay their
331 /// components out in memory identically.
333 class SequentialType : public CompositeType {
334 Type *ContainedType; ///< Storage for the single contained type.
335 SequentialType(const SequentialType &) = delete;
336 const SequentialType &operator=(const SequentialType &) = delete;
339 SequentialType(TypeID TID, Type *ElType)
340 : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
341 ContainedTys = &ContainedType;
346 Type *getElementType() const { return ContainedTys[0]; }
348 /// Methods for support type inquiry through isa, cast, and dyn_cast.
349 static inline bool classof(const Type *T) {
350 return T->getTypeID() == ArrayTyID ||
351 T->getTypeID() == PointerTyID ||
352 T->getTypeID() == VectorTyID;
356 Type *Type::getSequentialElementType() const {
357 return cast<SequentialType>(this)->getElementType();
360 /// ArrayType - Class to represent array types.
362 class ArrayType : public SequentialType {
363 uint64_t NumElements;
365 ArrayType(const ArrayType &) = delete;
366 const ArrayType &operator=(const ArrayType &) = delete;
367 ArrayType(Type *ElType, uint64_t NumEl);
370 /// ArrayType::get - This static method is the primary way to construct an
373 static ArrayType *get(Type *ElementType, uint64_t NumElements);
375 /// isValidElementType - Return true if the specified type is valid as a
377 static bool isValidElementType(Type *ElemTy);
379 uint64_t getNumElements() const { return NumElements; }
381 /// Methods for support type inquiry through isa, cast, and dyn_cast.
382 static inline bool classof(const Type *T) {
383 return T->getTypeID() == ArrayTyID;
387 uint64_t Type::getArrayNumElements() const {
388 return cast<ArrayType>(this)->getNumElements();
391 /// VectorType - Class to represent vector types.
393 class VectorType : public SequentialType {
394 unsigned NumElements;
396 VectorType(const VectorType &) = delete;
397 const VectorType &operator=(const VectorType &) = delete;
398 VectorType(Type *ElType, unsigned NumEl);
401 /// VectorType::get - This static method is the primary way to construct an
404 static VectorType *get(Type *ElementType, unsigned NumElements);
406 /// VectorType::getInteger - This static method gets a VectorType with the
407 /// same number of elements as the input type, and the element type is an
408 /// integer type of the same width as the input element type.
410 static VectorType *getInteger(VectorType *VTy) {
411 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
412 assert(EltBits && "Element size must be of a non-zero size");
413 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
414 return VectorType::get(EltTy, VTy->getNumElements());
417 /// VectorType::getExtendedElementVectorType - This static method is like
418 /// getInteger except that the element types are twice as wide as the
419 /// elements in the input type.
421 static VectorType *getExtendedElementVectorType(VectorType *VTy) {
422 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
423 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
424 return VectorType::get(EltTy, VTy->getNumElements());
427 /// VectorType::getTruncatedElementVectorType - This static method is like
428 /// getInteger except that the element types are half as wide as the
429 /// elements in the input type.
431 static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
432 unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
433 assert((EltBits & 1) == 0 &&
434 "Cannot truncate vector element with odd bit-width");
435 Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
436 return VectorType::get(EltTy, VTy->getNumElements());
439 /// VectorType::getHalfElementsVectorType - This static method returns
440 /// a VectorType with half as many elements as the input type and the
441 /// same element type.
443 static VectorType *getHalfElementsVectorType(VectorType *VTy) {
444 unsigned NumElts = VTy->getNumElements();
445 assert ((NumElts & 1) == 0 &&
446 "Cannot halve vector with odd number of elements.");
447 return VectorType::get(VTy->getElementType(), NumElts/2);
450 /// VectorType::getDoubleElementsVectorType - This static method returns
451 /// a VectorType with twice as many elements as the input type and the
452 /// same element type.
454 static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
455 unsigned NumElts = VTy->getNumElements();
456 return VectorType::get(VTy->getElementType(), NumElts*2);
459 /// isValidElementType - Return true if the specified type is valid as a
461 static bool isValidElementType(Type *ElemTy);
463 /// @brief Return the number of elements in the Vector type.
464 unsigned getNumElements() const { return NumElements; }
466 /// @brief Return the number of bits in the Vector type.
467 /// Returns zero when the vector is a vector of pointers.
468 unsigned getBitWidth() const {
469 return NumElements * getElementType()->getPrimitiveSizeInBits();
472 /// Methods for support type inquiry through isa, cast, and dyn_cast.
473 static inline bool classof(const Type *T) {
474 return T->getTypeID() == VectorTyID;
478 unsigned Type::getVectorNumElements() const {
479 return cast<VectorType>(this)->getNumElements();
482 /// PointerType - Class to represent pointers.
484 class PointerType : public SequentialType {
485 PointerType(const PointerType &) = delete;
486 const PointerType &operator=(const PointerType &) = delete;
487 explicit PointerType(Type *ElType, unsigned AddrSpace);
490 /// PointerType::get - This constructs a pointer to an object of the specified
491 /// type in a numbered address space.
492 static PointerType *get(Type *ElementType, unsigned AddressSpace);
494 /// PointerType::getUnqual - This constructs a pointer to an object of the
495 /// specified type in the generic address space (address space zero).
496 static PointerType *getUnqual(Type *ElementType) {
497 return PointerType::get(ElementType, 0);
500 /// isValidElementType - Return true if the specified type is valid as a
502 static bool isValidElementType(Type *ElemTy);
504 /// Return true if we can load or store from a pointer to this type.
505 static bool isLoadableOrStorableType(Type *ElemTy);
507 /// @brief Return the address space of the Pointer type.
508 inline unsigned getAddressSpace() const { return getSubclassData(); }
510 /// Implement support type inquiry through isa, cast, and dyn_cast.
511 static inline bool classof(const Type *T) {
512 return T->getTypeID() == PointerTyID;
516 unsigned Type::getPointerAddressSpace() const {
517 return cast<PointerType>(getScalarType())->getAddressSpace();
520 } // End llvm namespace