Add function for testing string attributes to InvokeInst and CallSite. NFC.
[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
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
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.
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).
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.
89   static inline bool classof(const Type *T) {
90     return T->getTypeID() == IntegerTyID;
91   }
92 };
93
94 unsigned Type::getIntegerBitWidth() const {
95   return cast<IntegerType>(this)->getBitWidth();
96 }
97
98 /// FunctionType - Class to represent function types
99 ///
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);
104
105 public:
106   /// FunctionType::get - This static method is the primary way of constructing
107   /// a FunctionType.
108   ///
109   static FunctionType *get(Type *Result,
110                            ArrayRef<Type*> Params, bool isVarArg);
111
112   /// FunctionType::get - Create a FunctionType taking no parameters.
113   ///
114   static FunctionType *get(Type *Result, bool isVarArg);
115
116   /// isValidReturnType - Return true if the specified type is valid as a return
117   /// type.
118   static bool isValidReturnType(Type *RetTy);
119
120   /// isValidArgumentType - Return true if the specified type is valid as an
121   /// argument type.
122   static bool isValidArgumentType(Type *ArgTy);
123
124   bool isVarArg() const { return getSubclassData()!=0; }
125   Type *getReturnType() const { return ContainedTys[0]; }
126
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());
132   }
133
134   /// Parameter type accessors.
135   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
136
137   /// getNumParams - Return the number of fixed parameters this function type
138   /// requires.  This does not consider varargs.
139   ///
140   unsigned getNumParams() const { return NumContainedTys - 1; }
141
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;
145   }
146 };
147 static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
148               "Alignment sufficient for objects appended to FunctionType");
149
150 bool Type::isFunctionVarArg() const {
151   return cast<FunctionType>(this)->isVarArg();
152 }
153
154 Type *Type::getFunctionParamType(unsigned i) const {
155   return cast<FunctionType>(this)->getParamType(i);
156 }
157
158 unsigned Type::getFunctionNumParams() const {
159   return cast<FunctionType>(this)->getNumParams();
160 }
161
162 /// CompositeType - Common super class of ArrayType, StructType, PointerType
163 /// and VectorType.
164 class CompositeType : public Type {
165 protected:
166   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {}
167
168 public:
169   /// getTypeAtIndex - Given an index value into the type, return the type of
170   /// the element.
171   ///
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;
176
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;
183   }
184 };
185
186 /// StructType - Class to represent struct types.  There are two different kinds
187 /// of struct types: Literal structs and Identified structs.
188 ///
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.
192 ///
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.
199 ///
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).
205 ///
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) {}
211   enum {
212     /// This is the contents of the SubClassData field.
213     SCDB_HasBody = 1,
214     SCDB_Packed = 2,
215     SCDB_IsLiteral = 4,
216     SCDB_IsSized = 8
217   };
218
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.
223   ///
224   void *SymbolTableEntry;
225
226 public:
227   /// StructType::create - This creates an identified struct.
228   static StructType *create(LLVMContext &Context, StringRef Name);
229   static StructType *create(LLVMContext &Context);
230
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;
238
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);
243
244   /// StructType::get - Create an empty structure type.
245   ///
246   static StructType *get(LLVMContext &Context, bool isPacked = false);
247
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
251   /// element type.
252   static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
253
254   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
255
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; }
259
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; }
263
264   /// isSized - Return true if this is a sized type.
265   bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
266
267   /// hasName - Return true if this is a named struct that has a non-empty name.
268   bool hasName() const { return SymbolTableEntry != nullptr; }
269
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;
274
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
277   /// type.
278   void setName(StringRef Name);
279
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;
283
284   /// isValidElementType - Return true if the specified type is valid as a
285   /// element type.
286   static bool isValidElementType(Type *ElemTy);
287
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());
294   }
295
296   /// isLayoutIdentical - Return true if this is layout identical to the
297   /// specified struct.
298   bool isLayoutIdentical(StructType *Other) const;
299
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];
305   }
306
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;
310   }
311 };
312
313 StringRef Type::getStructName() const {
314   return cast<StructType>(this)->getName();
315 }
316
317 unsigned Type::getStructNumElements() const {
318   return cast<StructType>(this)->getNumElements();
319 }
320
321 Type *Type::getStructElementType(unsigned N) const {
322   return cast<StructType>(this)->getElementType(N);
323 }
324
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.
332 ///
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;
337
338 protected:
339   SequentialType(TypeID TID, Type *ElType)
340     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
341     ContainedTys = &ContainedType;
342     NumContainedTys = 1;
343   }
344
345 public:
346   Type *getElementType() const { return ContainedTys[0]; }
347
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;
353   }
354 };
355
356 Type *Type::getSequentialElementType() const {
357   return cast<SequentialType>(this)->getElementType();
358 }
359
360 /// ArrayType - Class to represent array types.
361 ///
362 class ArrayType : public SequentialType {
363   uint64_t NumElements;
364
365   ArrayType(const ArrayType &) = delete;
366   const ArrayType &operator=(const ArrayType &) = delete;
367   ArrayType(Type *ElType, uint64_t NumEl);
368
369 public:
370   /// ArrayType::get - This static method is the primary way to construct an
371   /// ArrayType
372   ///
373   static ArrayType *get(Type *ElementType, uint64_t NumElements);
374
375   /// isValidElementType - Return true if the specified type is valid as a
376   /// element type.
377   static bool isValidElementType(Type *ElemTy);
378
379   uint64_t getNumElements() const { return NumElements; }
380
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;
384   }
385 };
386
387 uint64_t Type::getArrayNumElements() const {
388   return cast<ArrayType>(this)->getNumElements();
389 }
390
391 /// VectorType - Class to represent vector types.
392 ///
393 class VectorType : public SequentialType {
394   unsigned NumElements;
395
396   VectorType(const VectorType &) = delete;
397   const VectorType &operator=(const VectorType &) = delete;
398   VectorType(Type *ElType, unsigned NumEl);
399
400 public:
401   /// VectorType::get - This static method is the primary way to construct an
402   /// VectorType.
403   ///
404   static VectorType *get(Type *ElementType, unsigned NumElements);
405
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.
409   ///
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());
415   }
416
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.
420   ///
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());
425   }
426
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.
430   ///
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());
437   }
438
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.
442   ///
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);
448   }
449
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.
453   ///
454   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
455     unsigned NumElts = VTy->getNumElements();
456     return VectorType::get(VTy->getElementType(), NumElts*2);
457   }
458
459   /// isValidElementType - Return true if the specified type is valid as a
460   /// element type.
461   static bool isValidElementType(Type *ElemTy);
462
463   /// @brief Return the number of elements in the Vector type.
464   unsigned getNumElements() const { return NumElements; }
465
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();
470   }
471
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;
475   }
476 };
477
478 unsigned Type::getVectorNumElements() const {
479   return cast<VectorType>(this)->getNumElements();
480 }
481
482 /// PointerType - Class to represent pointers.
483 ///
484 class PointerType : public SequentialType {
485   PointerType(const PointerType &) = delete;
486   const PointerType &operator=(const PointerType &) = delete;
487   explicit PointerType(Type *ElType, unsigned AddrSpace);
488
489 public:
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);
493
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);
498   }
499
500   /// isValidElementType - Return true if the specified type is valid as a
501   /// element type.
502   static bool isValidElementType(Type *ElemTy);
503
504   /// Return true if we can load or store from a pointer to this type.
505   static bool isLoadableOrStorableType(Type *ElemTy);
506
507   /// @brief Return the address space of the Pointer type.
508   inline unsigned getAddressSpace() const { return getSubclassData(); }
509
510   /// Implement support type inquiry through isa, cast, and dyn_cast.
511   static inline bool classof(const Type *T) {
512     return T->getTypeID() == PointerTyID;
513   }
514 };
515
516 unsigned Type::getPointerAddressSpace() const {
517   return cast<PointerType>(getScalarType())->getAddressSpace();
518 }
519
520 } // End llvm namespace
521
522 #endif