• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
IntegerType(LLVMContext & C,unsigned NumBits)41   explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
42     setSubclassData(NumBits);
43   }
44 
45 public:
46   /// This enum is just used to hold constants we need for IntegerType.
47   enum {
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.
52   };
53 
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);
60 
61   /// @brief Get the number of bits in this IntegerType
getBitWidth()62   unsigned getBitWidth() const { return getSubclassData(); }
63 
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.
getBitMask()67   uint64_t getBitMask() const {
68     return ~uint64_t(0UL) >> (64-getBitWidth());
69   }
70 
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).
getSignBit()73   uint64_t getSignBit() const {
74     return 1ULL << (getBitWidth()-1);
75   }
76 
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;
81 
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;
87 
88   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)89   static inline bool classof(const Type *T) {
90     return T->getTypeID() == IntegerTyID;
91   }
92 };
93 
94 /// FunctionType - Class to represent function types
95 ///
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);
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 
isVarArg()120   bool isVarArg() const { return getSubclassData()!=0; }
getReturnType()121   Type *getReturnType() const { return ContainedTys[0]; }
122 
123   typedef Type::subtype_iterator param_iterator;
param_begin()124   param_iterator param_begin() const { return ContainedTys + 1; }
param_end()125   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
params()126   ArrayRef<Type *> params() const {
127     return makeArrayRef(param_begin(), param_end());
128   }
129 
130   /// Parameter type accessors.
getParamType(unsigned i)131   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
132 
133   /// getNumParams - Return the number of fixed parameters this function type
134   /// requires.  This does not consider varargs.
135   ///
getNumParams()136   unsigned getNumParams() const { return NumContainedTys - 1; }
137 
138   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)139   static inline bool classof(const Type *T) {
140     return T->getTypeID() == FunctionTyID;
141   }
142 };
143 static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
144               "Alignment sufficient for objects appended to FunctionType");
145 
146 /// CompositeType - Common super class of ArrayType, StructType, PointerType
147 /// and VectorType.
148 class CompositeType : public Type {
149 protected:
CompositeType(LLVMContext & C,TypeID tid)150   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {}
151 
152 public:
153   /// getTypeAtIndex - Given an index value into the type, return the type of
154   /// the element.
155   ///
156   Type *getTypeAtIndex(const Value *V) const;
157   Type *getTypeAtIndex(unsigned Idx) const;
158   bool indexValid(const Value *V) const;
159   bool indexValid(unsigned Idx) const;
160 
161   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)162   static inline bool classof(const Type *T) {
163     return T->getTypeID() == ArrayTyID ||
164            T->getTypeID() == StructTyID ||
165            T->getTypeID() == PointerTyID ||
166            T->getTypeID() == VectorTyID;
167   }
168 };
169 
170 /// StructType - Class to represent struct types.  There are two different kinds
171 /// of struct types: Literal structs and Identified structs.
172 ///
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.
176 ///
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.
183 ///
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).
189 ///
190 class StructType : public CompositeType {
191   StructType(const StructType &) = delete;
192   const StructType &operator=(const StructType &) = delete;
StructType(LLVMContext & C)193   StructType(LLVMContext &C)
194     : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
195   enum {
196     /// This is the contents of the SubClassData field.
197     SCDB_HasBody = 1,
198     SCDB_Packed = 2,
199     SCDB_IsLiteral = 4,
200     SCDB_IsSized = 8
201   };
202 
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.
207   ///
208   void *SymbolTableEntry;
209 
210 public:
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, StringRef Name,
216                             bool isPacked = false);
217   static StructType *create(ArrayRef<Type *> Elements);
218   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
219                             StringRef Name, bool isPacked = false);
220   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
221   static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
222 
223   /// StructType::get - This static method is the primary way to create a
224   /// literal StructType.
225   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
226                          bool isPacked = false);
227 
228   /// StructType::get - Create an empty structure type.
229   ///
230   static StructType *get(LLVMContext &Context, bool isPacked = false);
231 
232   /// StructType::get - This static method is a convenience method for creating
233   /// structure types by specifying the elements as arguments.  Note that this
234   /// method always returns a non-packed struct, and requires at least one
235   /// element type.
236   static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
237 
isPacked()238   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
239 
240   /// isLiteral - Return true if this type is uniqued by structural
241   /// equivalence, false if it is a struct definition.
isLiteral()242   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
243 
244   /// isOpaque - Return true if this is a type with an identity that has no body
245   /// specified yet.  These prints as 'opaque' in .ll files.
isOpaque()246   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
247 
248   /// isSized - Return true if this is a sized type.
249   bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
250 
251   /// hasName - Return true if this is a named struct that has a non-empty name.
hasName()252   bool hasName() const { return SymbolTableEntry != nullptr; }
253 
254   /// getName - Return the name for this struct type if it has an identity.
255   /// This may return an empty string for an unnamed struct type.  Do not call
256   /// this on an literal type.
257   StringRef getName() const;
258 
259   /// setName - Change the name of this type to the specified name, or to a name
260   /// with a suffix if there is a collision.  Do not call this on an literal
261   /// type.
262   void setName(StringRef Name);
263 
264   /// setBody - Specify a body for an opaque identified type.
265   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
266   void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
267 
268   /// isValidElementType - Return true if the specified type is valid as a
269   /// element type.
270   static bool isValidElementType(Type *ElemTy);
271 
272   // Iterator access to the elements.
273   typedef Type::subtype_iterator element_iterator;
element_begin()274   element_iterator element_begin() const { return ContainedTys; }
element_end()275   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
elements()276   ArrayRef<Type *> const elements() const {
277     return makeArrayRef(element_begin(), element_end());
278   }
279 
280   /// isLayoutIdentical - Return true if this is layout identical to the
281   /// specified struct.
282   bool isLayoutIdentical(StructType *Other) const;
283 
284   /// Random access to the elements
getNumElements()285   unsigned getNumElements() const { return NumContainedTys; }
getElementType(unsigned N)286   Type *getElementType(unsigned N) const {
287     assert(N < NumContainedTys && "Element number out of range!");
288     return ContainedTys[N];
289   }
290 
291   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)292   static inline bool classof(const Type *T) {
293     return T->getTypeID() == StructTyID;
294   }
295 };
296 
297 /// SequentialType - This is the superclass of the array, pointer and vector
298 /// type classes.  All of these represent "arrays" in memory.  The array type
299 /// represents a specifically sized array, pointer types are unsized/unknown
300 /// size arrays, vector types represent specifically sized arrays that
301 /// allow for use of SIMD instructions.  SequentialType holds the common
302 /// features of all, which stem from the fact that all three lay their
303 /// components out in memory identically.
304 ///
305 class SequentialType : public CompositeType {
306   Type *ContainedType;               ///< Storage for the single contained type.
307   SequentialType(const SequentialType &) = delete;
308   const SequentialType &operator=(const SequentialType &) = delete;
309 
310 protected:
SequentialType(TypeID TID,Type * ElType)311   SequentialType(TypeID TID, Type *ElType)
312     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
313     ContainedTys = &ContainedType;
314     NumContainedTys = 1;
315   }
316 
317 public:
getElementType()318   Type *getElementType() const { return ContainedTys[0]; }
319 
320   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)321   static inline bool classof(const Type *T) {
322     return T->getTypeID() == ArrayTyID ||
323            T->getTypeID() == PointerTyID ||
324            T->getTypeID() == VectorTyID;
325   }
326 };
327 
328 /// ArrayType - Class to represent array types.
329 ///
330 class ArrayType : public SequentialType {
331   uint64_t NumElements;
332 
333   ArrayType(const ArrayType &) = delete;
334   const ArrayType &operator=(const ArrayType &) = delete;
335   ArrayType(Type *ElType, uint64_t NumEl);
336 
337 public:
338   /// ArrayType::get - This static method is the primary way to construct an
339   /// ArrayType
340   ///
341   static ArrayType *get(Type *ElementType, uint64_t NumElements);
342 
343   /// isValidElementType - Return true if the specified type is valid as a
344   /// element type.
345   static bool isValidElementType(Type *ElemTy);
346 
getNumElements()347   uint64_t getNumElements() const { return NumElements; }
348 
349   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)350   static inline bool classof(const Type *T) {
351     return T->getTypeID() == ArrayTyID;
352   }
353 };
354 
355 /// VectorType - Class to represent vector types.
356 ///
357 class VectorType : public SequentialType {
358   unsigned NumElements;
359 
360   VectorType(const VectorType &) = delete;
361   const VectorType &operator=(const VectorType &) = delete;
362   VectorType(Type *ElType, unsigned NumEl);
363 
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   ///
getInteger(VectorType * VTy)374   static VectorType *getInteger(VectorType *VTy) {
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   ///
getExtendedElementVectorType(VectorType * VTy)385   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
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   ///
getTruncatedElementVectorType(VectorType * VTy)395   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
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   /// VectorType::getHalfElementsVectorType - This static method returns
404   /// a VectorType with half as many elements as the input type and the
405   /// same element type.
406   ///
getHalfElementsVectorType(VectorType * VTy)407   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
408     unsigned NumElts = VTy->getNumElements();
409     assert ((NumElts & 1) == 0 &&
410             "Cannot halve vector with odd number of elements.");
411     return VectorType::get(VTy->getElementType(), NumElts/2);
412   }
413 
414   /// VectorType::getDoubleElementsVectorType - This static method returns
415   /// a VectorType with twice  as many elements as the input type and the
416   /// same element type.
417   ///
getDoubleElementsVectorType(VectorType * VTy)418   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
419     unsigned NumElts = VTy->getNumElements();
420     return VectorType::get(VTy->getElementType(), NumElts*2);
421   }
422 
423   /// isValidElementType - Return true if the specified type is valid as a
424   /// element type.
425   static bool isValidElementType(Type *ElemTy);
426 
427   /// @brief Return the number of elements in the Vector type.
getNumElements()428   unsigned getNumElements() const { return NumElements; }
429 
430   /// @brief Return the number of bits in the Vector type.
431   /// Returns zero when the vector is a vector of pointers.
getBitWidth()432   unsigned getBitWidth() const {
433     return NumElements * getElementType()->getPrimitiveSizeInBits();
434   }
435 
436   /// Methods for support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)437   static inline bool classof(const Type *T) {
438     return T->getTypeID() == VectorTyID;
439   }
440 };
441 
442 /// PointerType - Class to represent pointers.
443 ///
444 class PointerType : public SequentialType {
445   PointerType(const PointerType &) = delete;
446   const PointerType &operator=(const PointerType &) = delete;
447   explicit PointerType(Type *ElType, unsigned AddrSpace);
448 
449 public:
450   /// PointerType::get - This constructs a pointer to an object of the specified
451   /// type in a numbered address space.
452   static PointerType *get(Type *ElementType, unsigned AddressSpace);
453 
454   /// PointerType::getUnqual - This constructs a pointer to an object of the
455   /// specified type in the generic address space (address space zero).
getUnqual(Type * ElementType)456   static PointerType *getUnqual(Type *ElementType) {
457     return PointerType::get(ElementType, 0);
458   }
459 
460   /// isValidElementType - Return true if the specified type is valid as a
461   /// element type.
462   static bool isValidElementType(Type *ElemTy);
463 
464   /// Return true if we can load or store from a pointer to this type.
465   static bool isLoadableOrStorableType(Type *ElemTy);
466 
467   /// @brief Return the address space of the Pointer type.
getAddressSpace()468   inline unsigned getAddressSpace() const { return getSubclassData(); }
469 
470   /// Implement support type inquiry through isa, cast, and dyn_cast.
classof(const Type * T)471   static inline bool classof(const Type *T) {
472     return T->getTypeID() == PointerTyID;
473   }
474 };
475 
476 } // End llvm namespace
477 
478 #endif
479