LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DerivedTypes.h
Go to the documentation of this file.
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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...
13 //
14 // The implementations of these classes live in the Type.cpp file.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_IR_DERIVEDTYPES_H
19 #define LLVM_IR_DERIVEDTYPES_H
20 
21 #include "llvm/IR/Type.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
24 
25 namespace llvm {
26 
27 class Value;
28 class APInt;
29 class LLVMContext;
30 template<typename T> class ArrayRef;
31 class StringRef;
32 
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
35 /// Int64Ty.
36 /// @brief Integer representation type
37 class IntegerType : public Type {
38  friend class LLVMContextImpl;
39 
40 protected:
41  explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
42  setSubclassData(NumBits);
43  }
44 public:
45  /// This enum is just used to hold constants we need for IntegerType.
46  enum {
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.
51  };
52 
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);
59 
60  /// @brief Get the number of bits in this IntegerType
61  unsigned getBitWidth() const { return getSubclassData(); }
62 
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());
68  }
69 
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);
74  }
75 
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;
80 
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;
86 
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;
90  }
91 };
92 
93 
94 /// FunctionType - Class to represent function types
95 ///
96 class FunctionType : public Type {
98  const FunctionType &operator=(const FunctionType &) LLVM_DELETED_FUNCTION;
99  FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
100 
101 public:
102  /// FunctionType::get - This static method is the primary way of constructing
103  /// a FunctionType.
104  ///
105  static FunctionType *get(Type *Result,
106  ArrayRef<Type*> Params, bool isVarArg);
107 
108  /// FunctionType::get - Create a FunctionType taking no parameters.
109  ///
110  static FunctionType *get(Type *Result, bool isVarArg);
111 
112  /// isValidReturnType - Return true if the specified type is valid as a return
113  /// type.
114  static bool isValidReturnType(Type *RetTy);
115 
116  /// isValidArgumentType - Return true if the specified type is valid as an
117  /// argument type.
118  static bool isValidArgumentType(Type *ArgTy);
119 
120  bool isVarArg() const { return getSubclassData()!=0; }
121  Type *getReturnType() const { return ContainedTys[0]; }
122 
124  param_iterator param_begin() const { return ContainedTys + 1; }
126 
127  /// Parameter type accessors.
128  Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
129 
130  /// getNumParams - Return the number of fixed parameters this function type
131  /// requires. This does not consider varargs.
132  ///
133  unsigned getNumParams() const { return NumContainedTys - 1; }
134 
135  /// Methods for support type inquiry through isa, cast, and dyn_cast.
136  static inline bool classof(const Type *T) {
137  return T->getTypeID() == FunctionTyID;
138  }
139 };
140 
141 
142 /// CompositeType - Common super class of ArrayType, StructType, PointerType
143 /// and VectorType.
144 class CompositeType : public Type {
145 protected:
146  explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
147 public:
148 
149  /// getTypeAtIndex - Given an index value into the type, return the type of
150  /// the element.
151  ///
152  Type *getTypeAtIndex(const Value *V);
153  Type *getTypeAtIndex(unsigned Idx);
154  bool indexValid(const Value *V) const;
155  bool indexValid(unsigned Idx) const;
156 
157  /// Methods for support type inquiry through isa, cast, and dyn_cast.
158  static inline bool classof(const Type *T) {
159  return T->getTypeID() == ArrayTyID ||
160  T->getTypeID() == StructTyID ||
161  T->getTypeID() == PointerTyID ||
162  T->getTypeID() == VectorTyID;
163  }
164 };
165 
166 
167 /// StructType - Class to represent struct types. There are two different kinds
168 /// of struct types: Literal structs and Identified structs.
169 ///
170 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
171 /// always have a body when created. You can get one of these by using one of
172 /// the StructType::get() forms.
173 ///
174 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
175 /// uniqued. The names for identified structs are managed at the LLVMContext
176 /// level, so there can only be a single identified struct with a given name in
177 /// a particular LLVMContext. Identified structs may also optionally be opaque
178 /// (have no body specified). You get one of these by using one of the
179 /// StructType::create() forms.
180 ///
181 /// Independent of what kind of struct you have, the body of a struct type are
182 /// laid out in memory consequtively with the elements directly one after the
183 /// other (if the struct is packed) or (if not packed) with padding between the
184 /// elements as defined by DataLayout (which is required to match what the code
185 /// generator for a target expects).
186 ///
187 class StructType : public CompositeType {
189  const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION;
191  : CompositeType(C, StructTyID), SymbolTableEntry(0) {}
192  enum {
193  /// This is the contents of the SubClassData field.
194  SCDB_HasBody = 1,
195  SCDB_Packed = 2,
196  SCDB_IsLiteral = 4,
197  SCDB_IsSized = 8
198  };
199 
200  /// SymbolTableEntry - For a named struct that actually has a name, this is a
201  /// pointer to the symbol table entry (maintained by LLVMContext) for the
202  /// struct. This is null if the type is an literal struct or if it is
203  /// a identified type that has an empty name.
204  ///
205  void *SymbolTableEntry;
206 public:
208  delete [] ContainedTys; // Delete the body.
209  }
210 
211  /// StructType::create - This creates an identified struct.
212  static StructType *create(LLVMContext &Context, StringRef Name);
213  static StructType *create(LLVMContext &Context);
214 
215  static StructType *create(ArrayRef<Type*> Elements,
216  StringRef Name,
217  bool isPacked = false);
218  static StructType *create(ArrayRef<Type*> Elements);
219  static StructType *create(LLVMContext &Context,
220  ArrayRef<Type*> Elements,
221  StringRef Name,
222  bool isPacked = false);
223  static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
224  static StructType *create(StringRef Name, Type *elt1, ...) END_WITH_NULL;
225 
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);
230 
231  /// StructType::get - Create an empty structure type.
232  ///
233  static StructType *get(LLVMContext &Context, bool isPacked = false);
234 
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
238  /// element type.
239  static StructType *get(Type *elt1, ...) END_WITH_NULL;
240 
241  bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
242 
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; }
246 
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; }
250 
251  /// isSized - Return true if this is a sized type.
252  bool isSized() const;
253 
254  /// hasName - Return true if this is a named struct that has a non-empty name.
255  bool hasName() const { return SymbolTableEntry != 0; }
256 
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;
261 
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
264  /// type.
265  void setName(StringRef Name);
266 
267  /// setBody - Specify a body for an opaque identified type.
268  void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
269  void setBody(Type *elt1, ...) END_WITH_NULL;
270 
271  /// isValidElementType - Return true if the specified type is valid as a
272  /// element type.
273  static bool isValidElementType(Type *ElemTy);
274 
275 
276  // Iterator access to the elements.
278  element_iterator element_begin() const { return ContainedTys; }
280 
281  /// isLayoutIdentical - Return true if this is layout identical to the
282  /// specified struct.
283  bool isLayoutIdentical(StructType *Other) const;
284 
285  /// Random access to the elements
286  unsigned getNumElements() const { return NumContainedTys; }
287  Type *getElementType(unsigned N) const {
288  assert(N < NumContainedTys && "Element number out of range!");
289  return ContainedTys[N];
290  }
291 
292  /// Methods for support type inquiry through isa, cast, and dyn_cast.
293  static inline bool classof(const Type *T) {
294  return T->getTypeID() == StructTyID;
295  }
296 };
297 
298 /// SequentialType - This is the superclass of the array, pointer and vector
299 /// type classes. All of these represent "arrays" in memory. The array type
300 /// represents a specifically sized array, pointer types are unsized/unknown
301 /// size arrays, vector types represent specifically sized arrays that
302 /// allow for use of SIMD instructions. SequentialType holds the common
303 /// features of all, which stem from the fact that all three lay their
304 /// components out in memory identically.
305 ///
307  Type *ContainedType; ///< Storage for the single contained type.
309  const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION;
310 
311 protected:
312  SequentialType(TypeID TID, Type *ElType)
313  : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
314  ContainedTys = &ContainedType;
315  NumContainedTys = 1;
316  }
317 
318 public:
319  Type *getElementType() const { return ContainedTys[0]; }
320 
321  /// Methods for support type inquiry through isa, cast, and dyn_cast.
322  static inline bool classof(const Type *T) {
323  return T->getTypeID() == ArrayTyID ||
324  T->getTypeID() == PointerTyID ||
325  T->getTypeID() == VectorTyID;
326  }
327 };
328 
329 
330 /// ArrayType - Class to represent array types.
331 ///
332 class ArrayType : public SequentialType {
333  uint64_t NumElements;
334 
336  const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION;
337  ArrayType(Type *ElType, uint64_t NumEl);
338 public:
339  /// ArrayType::get - This static method is the primary way to construct an
340  /// ArrayType
341  ///
342  static ArrayType *get(Type *ElementType, uint64_t NumElements);
343 
344  /// isValidElementType - Return true if the specified type is valid as a
345  /// element type.
346  static bool isValidElementType(Type *ElemTy);
347 
348  uint64_t getNumElements() const { return NumElements; }
349 
350  /// Methods for support type inquiry through isa, cast, and dyn_cast.
351  static inline bool classof(const Type *T) {
352  return T->getTypeID() == ArrayTyID;
353  }
354 };
355 
356 /// VectorType - Class to represent vector types.
357 ///
358 class VectorType : public SequentialType {
359  unsigned NumElements;
360 
362  const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION;
363  VectorType(Type *ElType, unsigned NumEl);
364 public:
365  /// VectorType::get - This static method is the primary way to construct an
366  /// VectorType.
367  ///
368  static VectorType *get(Type *ElementType, unsigned NumElements);
369 
370  /// VectorType::getInteger - This static method gets a VectorType with the
371  /// same number of elements as the input type, and the element type is an
372  /// integer type of the same width as the input element type.
373  ///
375  unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
376  assert(EltBits && "Element size must be of a non-zero size");
377  Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
378  return VectorType::get(EltTy, VTy->getNumElements());
379  }
380 
381  /// VectorType::getExtendedElementVectorType - This static method is like
382  /// getInteger except that the element types are twice as wide as the
383  /// elements in the input type.
384  ///
386  unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
387  Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
388  return VectorType::get(EltTy, VTy->getNumElements());
389  }
390 
391  /// VectorType::getTruncatedElementVectorType - This static method is like
392  /// getInteger except that the element types are half as wide as the
393  /// elements in the input type.
394  ///
396  unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
397  assert((EltBits & 1) == 0 &&
398  "Cannot truncate vector element with odd bit-width");
399  Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
400  return VectorType::get(EltTy, VTy->getNumElements());
401  }
402 
403  /// isValidElementType - Return true if the specified type is valid as a
404  /// element type.
405  static bool isValidElementType(Type *ElemTy);
406 
407  /// @brief Return the number of elements in the Vector type.
408  unsigned getNumElements() const { return NumElements; }
409 
410  /// @brief Return the number of bits in the Vector type.
411  /// Returns zero when the vector is a vector of pointers.
412  unsigned getBitWidth() const {
413  return NumElements * getElementType()->getPrimitiveSizeInBits();
414  }
415 
416  /// Methods for support type inquiry through isa, cast, and dyn_cast.
417  static inline bool classof(const Type *T) {
418  return T->getTypeID() == VectorTyID;
419  }
420 };
421 
422 
423 /// PointerType - Class to represent pointers.
424 ///
425 class PointerType : public SequentialType {
427  const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION;
428  explicit PointerType(Type *ElType, unsigned AddrSpace);
429 public:
430  /// PointerType::get - This constructs a pointer to an object of the specified
431  /// type in a numbered address space.
432  static PointerType *get(Type *ElementType, unsigned AddressSpace);
433 
434  /// PointerType::getUnqual - This constructs a pointer to an object of the
435  /// specified type in the generic address space (address space zero).
436  static PointerType *getUnqual(Type *ElementType) {
437  return PointerType::get(ElementType, 0);
438  }
439 
440  /// isValidElementType - Return true if the specified type is valid as a
441  /// element type.
442  static bool isValidElementType(Type *ElemTy);
443 
444  /// @brief Return the address space of the Pointer type.
445  inline unsigned getAddressSpace() const { return getSubclassData(); }
446 
447  /// Implement support type inquiry through isa, cast, and dyn_cast.
448  static inline bool classof(const Type *T) {
449  return T->getTypeID() == PointerTyID;
450  }
451 };
452 
453 } // End llvm namespace
454 
455 #endif
CompositeType(LLVMContext &C, TypeID tid)
Definition: DerivedTypes.h:146
bool isOpaque() const
Definition: DerivedTypes.h:249
APInt getMask() const
Get a bit mask for this type.
Definition: Type.cpp:333
unsigned getNumParams() const
Definition: DerivedTypes.h:133
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:123
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:351
static PointerType * get(Type *ElementType, unsigned AddressSpace)
Definition: Type.cpp:730
12: Structures
Definition: Type.h:70
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:445
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:61
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:136
14: Pointers
Definition: Type.h:72
static VectorType * getTruncatedElementVectorType(VectorType *VTy)
Definition: DerivedTypes.h:395
11: Functions
Definition: Type.h:69
Type *const * ContainedTys
Definition: Type.h:121
SequentialType(TypeID TID, Type *ElType)
Definition: DerivedTypes.h:312
element_iterator element_end() const
Definition: DerivedTypes.h:279
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:277
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:417
param_iterator param_end() const
Definition: DerivedTypes.h:125
uint64_t getSignBit() const
Definition: DerivedTypes.h:72
unsigned getBitWidth() const
Return the number of bits in the Vector type. Returns zero when the vector is a vector of pointers...
Definition: DerivedTypes.h:412
bool isLiteral() const
Definition: DerivedTypes.h:245
TypeID
Definition: Type.h:53
#define false
Definition: ConvertUTF.c:64
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
Minimum number of bits that can be specified.
Definition: DerivedTypes.h:47
TypeID getTypeID() const
Definition: Type.h:137
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:408
unsigned getSubclassData() const
Definition: Type.h:104
Type * getElementType() const
Definition: DerivedTypes.h:319
10: Arbitrary bit width integers
Definition: Type.h:68
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:128
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:287
param_iterator param_begin() const
Definition: DerivedTypes.h:124
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:158
static VectorType * getInteger(VectorType *VTy)
Definition: DerivedTypes.h:374
uint64_t getNumElements() const
Definition: DerivedTypes.h:348
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:322
Integer representation type.
Definition: DerivedTypes.h:37
13: Arrays
Definition: Type.h:71
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Definition: Type.cpp:305
static PointerType * getUnqual(Type *ElementType)
Definition: DerivedTypes.h:436
15: SIMD 'packed' format, or other vector type
Definition: Type.h:73
Type *const * subtype_iterator
Definition: Type.h:323
bool hasName() const
hasName - Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:255
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:293
AddressSpace
Definition: NVPTXBaseInfo.h:22
static VectorType * getExtendedElementVectorType(VectorType *VTy)
Definition: DerivedTypes.h:385
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
Class for arbitrary precision integers.
Definition: APInt.h:75
uint64_t getBitMask() const
Definition: DerivedTypes.h:66
static bool classof(const Type *T)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:88
std::string getName(ID id, ArrayRef< Type * > Tys=None)
Definition: Function.cpp:400
#define N
bool isPowerOf2ByteWidth() const
Is this a power-of-2 byte-width IntegerType ?
Definition: Type.cpp:328
unsigned getPrimitiveSizeInBits() const
Definition: Type.cpp:117
static bool classof(const Type *T)
Implement support type inquiry through isa, cast, and dyn_cast.
Definition: DerivedTypes.h:448
IntegerType(LLVMContext &C, unsigned NumBits)
Definition: DerivedTypes.h:41
bool isVarArg() const
Definition: DerivedTypes.h:120
void setSubclassData(unsigned val)
Definition: Type.h:106
Type * getReturnType() const
Definition: DerivedTypes.h:121
LLVM Value Representation.
Definition: Value.h:66
static VectorType * get(Type *ElementType, unsigned NumElements)
Definition: Type.cpp:706
bool isSized() const
Definition: Type.h:278
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:286
unsigned NumContainedTys
Definition: Type.h:114