Reformat.
[oota-llvm.git] / include / llvm / IR / DerivedTypes.h
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 {
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
120   bool isVarArg() const { return getSubclassData()!=0; }
121   Type *getReturnType() const { return ContainedTys[0]; }
122
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());
128   }
129
130   /// Parameter type accessors.
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   ///
136   unsigned getNumParams() const { return NumContainedTys - 1; }
137
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;
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:
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.
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
171 /// StructType - Class to represent struct types.  There are two different kinds
172 /// of struct types: Literal structs and Identified structs.
173 ///
174 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
175 /// always have a body when created.  You can get one of these by using one of
176 /// the StructType::get() forms.
177 ///  
178 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
179 /// uniqued.  The names for identified structs are managed at the LLVMContext
180 /// level, so there can only be a single identified struct with a given name in
181 /// a particular LLVMContext.  Identified structs may also optionally be opaque
182 /// (have no body specified).  You get one of these by using one of the
183 /// StructType::create() forms.
184 ///
185 /// Independent of what kind of struct you have, the body of a struct type are
186 /// laid out in memory consequtively with the elements directly one after the
187 /// other (if the struct is packed) or (if not packed) with padding between the
188 /// elements as defined by DataLayout (which is required to match what the code
189 /// generator for a target expects).
190 ///
191 class StructType : public CompositeType {
192   StructType(const StructType &) = delete;
193   const StructType &operator=(const StructType &) = delete;
194   StructType(LLVMContext &C)
195     : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
196   enum {
197     /// This is the contents of the SubClassData field.
198     SCDB_HasBody = 1,
199     SCDB_Packed = 2,
200     SCDB_IsLiteral = 4,
201     SCDB_IsSized = 8
202   };
203
204   /// SymbolTableEntry - For a named struct that actually has a name, this is a
205   /// pointer to the symbol table entry (maintained by LLVMContext) for the
206   /// struct.  This is null if the type is an literal struct or if it is
207   /// a identified type that has an empty name.
208   /// 
209   void *SymbolTableEntry;
210 public:
211
212   /// StructType::create - This creates an identified struct.
213   static StructType *create(LLVMContext &Context, StringRef Name);
214   static StructType *create(LLVMContext &Context);
215
216   static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
217                             bool isPacked = false);
218   static StructType *create(ArrayRef<Type *> Elements);
219   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
220                             StringRef Name, bool isPacked = false);
221   static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
222   static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
223
224   /// StructType::get - This static method is the primary way to create a
225   /// literal StructType.
226   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
227                          bool isPacked = false);
228
229   /// StructType::get - Create an empty structure type.
230   ///
231   static StructType *get(LLVMContext &Context, bool isPacked = false);
232   
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
236   /// element type.
237   static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
238
239   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
240   
241   /// isLiteral - Return true if this type is uniqued by structural
242   /// equivalence, false if it is a struct definition.
243   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
244   
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; }
248
249   /// isSized - Return true if this is a sized type.
250   bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
251
252   /// hasName - Return true if this is a named struct that has a non-empty name.
253   bool hasName() const { return SymbolTableEntry != nullptr; }
254   
255   /// getName - Return the name for this struct type if it has an identity.
256   /// This may return an empty string for an unnamed struct type.  Do not call
257   /// this on an literal type.
258   StringRef getName() const;
259   
260   /// setName - Change the name of this type to the specified name, or to a name
261   /// with a suffix if there is a collision.  Do not call this on an literal
262   /// type.
263   void setName(StringRef Name);
264
265   /// setBody - Specify a body for an opaque identified type.
266   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
267   void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
268   
269   /// isValidElementType - Return true if the specified type is valid as a
270   /// element type.
271   static bool isValidElementType(Type *ElemTy);
272   
273
274   // Iterator access to the elements.
275   typedef Type::subtype_iterator element_iterator;
276   element_iterator element_begin() const { return ContainedTys; }
277   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
278   ArrayRef<Type *> const elements() const {
279     return makeArrayRef(element_begin(), element_end());
280   }
281
282   /// isLayoutIdentical - Return true if this is layout identical to the
283   /// specified struct.
284   bool isLayoutIdentical(StructType *Other) const;  
285   
286   /// Random access to the elements
287   unsigned getNumElements() const { return NumContainedTys; }
288   Type *getElementType(unsigned N) const {
289     assert(N < NumContainedTys && "Element number out of range!");
290     return ContainedTys[N];
291   }
292
293   /// Methods for support type inquiry through isa, cast, and dyn_cast.
294   static inline bool classof(const Type *T) {
295     return T->getTypeID() == StructTyID;
296   }
297 };
298
299 /// SequentialType - This is the superclass of the array, pointer and vector
300 /// type classes.  All of these represent "arrays" in memory.  The array type
301 /// represents a specifically sized array, pointer types are unsized/unknown
302 /// size arrays, vector types represent specifically sized arrays that
303 /// allow for use of SIMD instructions.  SequentialType holds the common
304 /// features of all, which stem from the fact that all three lay their
305 /// components out in memory identically.
306 ///
307 class SequentialType : public CompositeType {
308   Type *ContainedType;               ///< Storage for the single contained type.
309   SequentialType(const SequentialType &) = delete;
310   const SequentialType &operator=(const SequentialType &) = delete;
311
312 protected:
313   SequentialType(TypeID TID, Type *ElType)
314     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
315     ContainedTys = &ContainedType;
316     NumContainedTys = 1;
317   }
318
319 public:
320   Type *getElementType() const { return ContainedTys[0]; }
321
322   /// Methods for support type inquiry through isa, cast, and dyn_cast.
323   static inline bool classof(const Type *T) {
324     return T->getTypeID() == ArrayTyID ||
325            T->getTypeID() == PointerTyID ||
326            T->getTypeID() == VectorTyID;
327   }
328 };
329
330
331 /// ArrayType - Class to represent array types.
332 ///
333 class ArrayType : public SequentialType {
334   uint64_t NumElements;
335
336   ArrayType(const ArrayType &) = delete;
337   const ArrayType &operator=(const ArrayType &) = delete;
338   ArrayType(Type *ElType, uint64_t NumEl);
339 public:
340   /// ArrayType::get - This static method is the primary way to construct an
341   /// ArrayType
342   ///
343   static ArrayType *get(Type *ElementType, uint64_t NumElements);
344
345   /// isValidElementType - Return true if the specified type is valid as a
346   /// element type.
347   static bool isValidElementType(Type *ElemTy);
348
349   uint64_t getNumElements() const { return NumElements; }
350
351   /// Methods for support type inquiry through isa, cast, and dyn_cast.
352   static inline bool classof(const Type *T) {
353     return T->getTypeID() == ArrayTyID;
354   }
355 };
356
357 /// VectorType - Class to represent vector types.
358 ///
359 class VectorType : public SequentialType {
360   unsigned NumElements;
361
362   VectorType(const VectorType &) = delete;
363   const VectorType &operator=(const VectorType &) = delete;
364   VectorType(Type *ElType, unsigned NumEl);
365 public:
366   /// VectorType::get - This static method is the primary way to construct an
367   /// VectorType.
368   ///
369   static VectorType *get(Type *ElementType, unsigned NumElements);
370
371   /// VectorType::getInteger - This static method gets a VectorType with the
372   /// same number of elements as the input type, and the element type is an
373   /// integer type of the same width as the input element type.
374   ///
375   static VectorType *getInteger(VectorType *VTy) {
376     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
377     assert(EltBits && "Element size must be of a non-zero size");
378     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
379     return VectorType::get(EltTy, VTy->getNumElements());
380   }
381
382   /// VectorType::getExtendedElementVectorType - This static method is like
383   /// getInteger except that the element types are twice as wide as the
384   /// elements in the input type.
385   ///
386   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
387     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
388     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
389     return VectorType::get(EltTy, VTy->getNumElements());
390   }
391
392   /// VectorType::getTruncatedElementVectorType - This static method is like
393   /// getInteger except that the element types are half as wide as the
394   /// elements in the input type.
395   ///
396   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
397     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
398     assert((EltBits & 1) == 0 &&
399            "Cannot truncate vector element with odd bit-width");
400     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
401     return VectorType::get(EltTy, VTy->getNumElements());
402   }
403
404   /// VectorType::getHalfElementsVectorType - This static method returns
405   /// a VectorType with half as many elements as the input type and the
406   /// same element type.
407   ///
408   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
409     unsigned NumElts = VTy->getNumElements();
410     assert ((NumElts & 1) == 0 &&
411             "Cannot halve vector with odd number of elements.");
412     return VectorType::get(VTy->getElementType(), NumElts/2);
413   }
414
415   /// VectorType::getDoubleElementsVectorType - This static method returns
416   /// a VectorType with twice  as many elements as the input type and the
417   /// same element type.
418   ///
419   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
420     unsigned NumElts = VTy->getNumElements();
421     return VectorType::get(VTy->getElementType(), NumElts*2);
422   }
423
424   /// isValidElementType - Return true if the specified type is valid as a
425   /// element type.
426   static bool isValidElementType(Type *ElemTy);
427
428   /// @brief Return the number of elements in the Vector type.
429   unsigned getNumElements() const { return NumElements; }
430
431   /// @brief Return the number of bits in the Vector type.
432   /// Returns zero when the vector is a vector of pointers.
433   unsigned getBitWidth() const {
434     return NumElements * getElementType()->getPrimitiveSizeInBits();
435   }
436
437   /// Methods for support type inquiry through isa, cast, and dyn_cast.
438   static inline bool classof(const Type *T) {
439     return T->getTypeID() == VectorTyID;
440   }
441 };
442
443
444 /// PointerType - Class to represent pointers.
445 ///
446 class PointerType : public SequentialType {
447   PointerType(const PointerType &) = delete;
448   const PointerType &operator=(const PointerType &) = delete;
449   explicit PointerType(Type *ElType, unsigned AddrSpace);
450 public:
451   /// PointerType::get - This constructs a pointer to an object of the specified
452   /// type in a numbered address space.
453   static PointerType *get(Type *ElementType, unsigned AddressSpace);
454
455   /// PointerType::getUnqual - This constructs a pointer to an object of the
456   /// specified type in the generic address space (address space zero).
457   static PointerType *getUnqual(Type *ElementType) {
458     return PointerType::get(ElementType, 0);
459   }
460
461   /// isValidElementType - Return true if the specified type is valid as a
462   /// element type.
463   static bool isValidElementType(Type *ElemTy);
464
465   /// Return true if we can load or store from a pointer to this type.
466   static bool isLoadableOrStorableType(Type *ElemTy);
467
468   /// @brief Return the address space of the Pointer type.
469   inline unsigned getAddressSpace() const { return getSubclassData(); }
470
471   /// Implement support type inquiry through isa, cast, and dyn_cast.
472   static inline bool classof(const Type *T) {
473     return T->getTypeID() == PointerTyID;
474   }
475 };
476
477 } // End llvm namespace
478
479 #endif