LLVM  mainline
DerivedTypes.h
Go to the documentation of this file.
00001 //===-- llvm/DerivedTypes.h - Classes for handling data types ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declarations of classes that represent "derived
00011 // types".  These are things like "arrays of x" or "structure of x, y, z" or
00012 // "function returning x taking (y,z) as parameters", etc...
00013 //
00014 // The implementations of these classes live in the Type.cpp file.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_IR_DERIVEDTYPES_H
00019 #define LLVM_IR_DERIVEDTYPES_H
00020 
00021 #include "llvm/IR/Type.h"
00022 #include "llvm/Support/Compiler.h"
00023 #include "llvm/Support/DataTypes.h"
00024 
00025 namespace llvm {
00026 
00027 class Value;
00028 class APInt;
00029 class LLVMContext;
00030 template<typename T> class ArrayRef;
00031 class StringRef;
00032 
00033 /// Class to represent integer types. Note that this class is also used to
00034 /// represent the built-in integer types: Int1Ty, Int8Ty, Int16Ty, Int32Ty and
00035 /// Int64Ty.
00036 /// @brief Integer representation type
00037 class IntegerType : public Type {
00038   friend class LLVMContextImpl;
00039   
00040 protected:
00041   explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
00042     setSubclassData(NumBits);
00043   }
00044 public:
00045   /// This enum is just used to hold constants we need for IntegerType.
00046   enum {
00047     MIN_INT_BITS = 1,        ///< Minimum number of bits that can be specified
00048     MAX_INT_BITS = (1<<23)-1 ///< Maximum number of bits that can be specified
00049       ///< Note that bit width is stored in the Type classes SubclassData field
00050       ///< which has 23 bits. This yields a maximum bit width of 8,388,607 bits.
00051   };
00052 
00053   /// This static method is the primary way of constructing an IntegerType.
00054   /// If an IntegerType with the same NumBits value was previously instantiated,
00055   /// that instance will be returned. Otherwise a new one will be created. Only
00056   /// one instance with a given NumBits value is ever created.
00057   /// @brief Get or create an IntegerType instance.
00058   static IntegerType *get(LLVMContext &C, unsigned NumBits);
00059 
00060   /// @brief Get the number of bits in this IntegerType
00061   unsigned getBitWidth() const { return getSubclassData(); }
00062 
00063   /// getBitMask - Return a bitmask with ones set for all of the bits
00064   /// that can be set by an unsigned version of this type.  This is 0xFF for
00065   /// i8, 0xFFFF for i16, etc.
00066   uint64_t getBitMask() const {
00067     return ~uint64_t(0UL) >> (64-getBitWidth());
00068   }
00069 
00070   /// getSignBit - Return a uint64_t with just the most significant bit set (the
00071   /// sign bit, if the value is treated as a signed number).
00072   uint64_t getSignBit() const {
00073     return 1ULL << (getBitWidth()-1);
00074   }
00075 
00076   /// For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
00077   /// @returns a bit mask with ones set for all the bits of this type.
00078   /// @brief Get a bit mask for this type.
00079   APInt getMask() const;
00080 
00081   /// This method determines if the width of this IntegerType is a power-of-2
00082   /// in terms of 8 bit bytes.
00083   /// @returns true if this is a power-of-2 byte width.
00084   /// @brief Is this a power-of-2 byte-width IntegerType ?
00085   bool isPowerOf2ByteWidth() const;
00086 
00087   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00088   static inline bool classof(const Type *T) {
00089     return T->getTypeID() == IntegerTyID;
00090   }
00091 };
00092 
00093 
00094 /// FunctionType - Class to represent function types
00095 ///
00096 class FunctionType : public Type {
00097   FunctionType(const FunctionType &) = delete;
00098   const FunctionType &operator=(const FunctionType &) = delete;
00099   FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
00100 
00101 public:
00102   /// FunctionType::get - This static method is the primary way of constructing
00103   /// a FunctionType.
00104   ///
00105   static FunctionType *get(Type *Result,
00106                            ArrayRef<Type*> Params, bool isVarArg);
00107 
00108   /// FunctionType::get - Create a FunctionType taking no parameters.
00109   ///
00110   static FunctionType *get(Type *Result, bool isVarArg);
00111   
00112   /// isValidReturnType - Return true if the specified type is valid as a return
00113   /// type.
00114   static bool isValidReturnType(Type *RetTy);
00115 
00116   /// isValidArgumentType - Return true if the specified type is valid as an
00117   /// argument type.
00118   static bool isValidArgumentType(Type *ArgTy);
00119 
00120   bool isVarArg() const { return getSubclassData()!=0; }
00121   Type *getReturnType() const { return ContainedTys[0]; }
00122 
00123   typedef Type::subtype_iterator param_iterator;
00124   param_iterator param_begin() const { return ContainedTys + 1; }
00125   param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
00126   ArrayRef<Type *> params() const {
00127     return makeArrayRef(param_begin(), param_end());
00128   }
00129 
00130   /// Parameter type accessors.
00131   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
00132 
00133   /// getNumParams - Return the number of fixed parameters this function type
00134   /// requires.  This does not consider varargs.
00135   ///
00136   unsigned getNumParams() const { return NumContainedTys - 1; }
00137 
00138   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00139   static inline bool classof(const Type *T) {
00140     return T->getTypeID() == FunctionTyID;
00141   }
00142 };
00143 static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
00144               "Alignment sufficient for objects appended to FunctionType");
00145 
00146 /// CompositeType - Common super class of ArrayType, StructType, PointerType
00147 /// and VectorType.
00148 class CompositeType : public Type {
00149 protected:
00150   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
00151 public:
00152 
00153   /// getTypeAtIndex - Given an index value into the type, return the type of
00154   /// the element.
00155   ///
00156   Type *getTypeAtIndex(const Value *V);
00157   Type *getTypeAtIndex(unsigned Idx);
00158   bool indexValid(const Value *V) const;
00159   bool indexValid(unsigned Idx) const;
00160 
00161   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00162   static inline bool classof(const Type *T) {
00163     return T->getTypeID() == ArrayTyID ||
00164            T->getTypeID() == StructTyID ||
00165            T->getTypeID() == PointerTyID ||
00166            T->getTypeID() == VectorTyID;
00167   }
00168 };
00169 
00170 
00171 /// StructType - Class to represent struct types.  There are two different kinds
00172 /// of struct types: Literal structs and Identified structs.
00173 ///
00174 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
00175 /// always have a body when created.  You can get one of these by using one of
00176 /// the StructType::get() forms.
00177 ///  
00178 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
00179 /// uniqued.  The names for identified structs are managed at the LLVMContext
00180 /// level, so there can only be a single identified struct with a given name in
00181 /// a particular LLVMContext.  Identified structs may also optionally be opaque
00182 /// (have no body specified).  You get one of these by using one of the
00183 /// StructType::create() forms.
00184 ///
00185 /// Independent of what kind of struct you have, the body of a struct type are
00186 /// laid out in memory consequtively with the elements directly one after the
00187 /// other (if the struct is packed) or (if not packed) with padding between the
00188 /// elements as defined by DataLayout (which is required to match what the code
00189 /// generator for a target expects).
00190 ///
00191 class StructType : public CompositeType {
00192   StructType(const StructType &) = delete;
00193   const StructType &operator=(const StructType &) = delete;
00194   StructType(LLVMContext &C)
00195     : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
00196   enum {
00197     /// This is the contents of the SubClassData field.
00198     SCDB_HasBody = 1,
00199     SCDB_Packed = 2,
00200     SCDB_IsLiteral = 4,
00201     SCDB_IsSized = 8
00202   };
00203 
00204   /// SymbolTableEntry - For a named struct that actually has a name, this is a
00205   /// pointer to the symbol table entry (maintained by LLVMContext) for the
00206   /// struct.  This is null if the type is an literal struct or if it is
00207   /// a identified type that has an empty name.
00208   /// 
00209   void *SymbolTableEntry;
00210 public:
00211 
00212   /// StructType::create - This creates an identified struct.
00213   static StructType *create(LLVMContext &Context, StringRef Name);
00214   static StructType *create(LLVMContext &Context);
00215   
00216   static StructType *create(ArrayRef<Type*> Elements,
00217                             StringRef Name,
00218                             bool isPacked = false);
00219   static StructType *create(ArrayRef<Type*> Elements);
00220   static StructType *create(LLVMContext &Context,
00221                             ArrayRef<Type*> Elements,
00222                             StringRef Name,
00223                             bool isPacked = false);
00224   static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
00225   static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
00226 
00227   /// StructType::get - This static method is the primary way to create a
00228   /// literal StructType.
00229   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
00230                          bool isPacked = false);
00231 
00232   /// StructType::get - Create an empty structure type.
00233   ///
00234   static StructType *get(LLVMContext &Context, bool isPacked = false);
00235   
00236   /// StructType::get - This static method is a convenience method for creating
00237   /// structure types by specifying the elements as arguments.  Note that this
00238   /// method always returns a non-packed struct, and requires at least one
00239   /// element type.
00240   static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
00241 
00242   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
00243   
00244   /// isLiteral - Return true if this type is uniqued by structural
00245   /// equivalence, false if it is a struct definition.
00246   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
00247   
00248   /// isOpaque - Return true if this is a type with an identity that has no body
00249   /// specified yet.  These prints as 'opaque' in .ll files.
00250   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
00251 
00252   /// isSized - Return true if this is a sized type.
00253   bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const;
00254   
00255   /// hasName - Return true if this is a named struct that has a non-empty name.
00256   bool hasName() const { return SymbolTableEntry != nullptr; }
00257   
00258   /// getName - Return the name for this struct type if it has an identity.
00259   /// This may return an empty string for an unnamed struct type.  Do not call
00260   /// this on an literal type.
00261   StringRef getName() const;
00262   
00263   /// setName - Change the name of this type to the specified name, or to a name
00264   /// with a suffix if there is a collision.  Do not call this on an literal
00265   /// type.
00266   void setName(StringRef Name);
00267 
00268   /// setBody - Specify a body for an opaque identified type.
00269   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
00270   void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
00271   
00272   /// isValidElementType - Return true if the specified type is valid as a
00273   /// element type.
00274   static bool isValidElementType(Type *ElemTy);
00275   
00276 
00277   // Iterator access to the elements.
00278   typedef Type::subtype_iterator element_iterator;
00279   element_iterator element_begin() const { return ContainedTys; }
00280   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
00281   ArrayRef<Type *> const elements() const {
00282     return makeArrayRef(element_begin(), element_end());
00283   }
00284 
00285   /// isLayoutIdentical - Return true if this is layout identical to the
00286   /// specified struct.
00287   bool isLayoutIdentical(StructType *Other) const;  
00288   
00289   /// Random access to the elements
00290   unsigned getNumElements() const { return NumContainedTys; }
00291   Type *getElementType(unsigned N) const {
00292     assert(N < NumContainedTys && "Element number out of range!");
00293     return ContainedTys[N];
00294   }
00295 
00296   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00297   static inline bool classof(const Type *T) {
00298     return T->getTypeID() == StructTyID;
00299   }
00300 };
00301 
00302 /// SequentialType - This is the superclass of the array, pointer and vector
00303 /// type classes.  All of these represent "arrays" in memory.  The array type
00304 /// represents a specifically sized array, pointer types are unsized/unknown
00305 /// size arrays, vector types represent specifically sized arrays that
00306 /// allow for use of SIMD instructions.  SequentialType holds the common
00307 /// features of all, which stem from the fact that all three lay their
00308 /// components out in memory identically.
00309 ///
00310 class SequentialType : public CompositeType {
00311   Type *ContainedType;               ///< Storage for the single contained type.
00312   SequentialType(const SequentialType &) = delete;
00313   const SequentialType &operator=(const SequentialType &) = delete;
00314 
00315 protected:
00316   SequentialType(TypeID TID, Type *ElType)
00317     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
00318     ContainedTys = &ContainedType;
00319     NumContainedTys = 1;
00320   }
00321 
00322 public:
00323   Type *getElementType() const { return ContainedTys[0]; }
00324 
00325   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00326   static inline bool classof(const Type *T) {
00327     return T->getTypeID() == ArrayTyID ||
00328            T->getTypeID() == PointerTyID ||
00329            T->getTypeID() == VectorTyID;
00330   }
00331 };
00332 
00333 
00334 /// ArrayType - Class to represent array types.
00335 ///
00336 class ArrayType : public SequentialType {
00337   uint64_t NumElements;
00338 
00339   ArrayType(const ArrayType &) = delete;
00340   const ArrayType &operator=(const ArrayType &) = delete;
00341   ArrayType(Type *ElType, uint64_t NumEl);
00342 public:
00343   /// ArrayType::get - This static method is the primary way to construct an
00344   /// ArrayType
00345   ///
00346   static ArrayType *get(Type *ElementType, uint64_t NumElements);
00347 
00348   /// isValidElementType - Return true if the specified type is valid as a
00349   /// element type.
00350   static bool isValidElementType(Type *ElemTy);
00351 
00352   uint64_t getNumElements() const { return NumElements; }
00353 
00354   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00355   static inline bool classof(const Type *T) {
00356     return T->getTypeID() == ArrayTyID;
00357   }
00358 };
00359 
00360 /// VectorType - Class to represent vector types.
00361 ///
00362 class VectorType : public SequentialType {
00363   unsigned NumElements;
00364 
00365   VectorType(const VectorType &) = delete;
00366   const VectorType &operator=(const VectorType &) = delete;
00367   VectorType(Type *ElType, unsigned NumEl);
00368 public:
00369   /// VectorType::get - This static method is the primary way to construct an
00370   /// VectorType.
00371   ///
00372   static VectorType *get(Type *ElementType, unsigned NumElements);
00373 
00374   /// VectorType::getInteger - This static method gets a VectorType with the
00375   /// same number of elements as the input type, and the element type is an
00376   /// integer type of the same width as the input element type.
00377   ///
00378   static VectorType *getInteger(VectorType *VTy) {
00379     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00380     assert(EltBits && "Element size must be of a non-zero size");
00381     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
00382     return VectorType::get(EltTy, VTy->getNumElements());
00383   }
00384 
00385   /// VectorType::getExtendedElementVectorType - This static method is like
00386   /// getInteger except that the element types are twice as wide as the
00387   /// elements in the input type.
00388   ///
00389   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
00390     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00391     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
00392     return VectorType::get(EltTy, VTy->getNumElements());
00393   }
00394 
00395   /// VectorType::getTruncatedElementVectorType - This static method is like
00396   /// getInteger except that the element types are half as wide as the
00397   /// elements in the input type.
00398   ///
00399   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
00400     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00401     assert((EltBits & 1) == 0 &&
00402            "Cannot truncate vector element with odd bit-width");
00403     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
00404     return VectorType::get(EltTy, VTy->getNumElements());
00405   }
00406 
00407   /// VectorType::getHalfElementsVectorType - This static method returns
00408   /// a VectorType with half as many elements as the input type and the
00409   /// same element type.
00410   ///
00411   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
00412     unsigned NumElts = VTy->getNumElements();
00413     assert ((NumElts & 1) == 0 &&
00414             "Cannot halve vector with odd number of elements.");
00415     return VectorType::get(VTy->getElementType(), NumElts/2);
00416   }
00417 
00418   /// VectorType::getDoubleElementsVectorType - This static method returns
00419   /// a VectorType with twice  as many elements as the input type and the
00420   /// same element type.
00421   ///
00422   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
00423     unsigned NumElts = VTy->getNumElements();
00424     return VectorType::get(VTy->getElementType(), NumElts*2);
00425   }
00426 
00427   /// isValidElementType - Return true if the specified type is valid as a
00428   /// element type.
00429   static bool isValidElementType(Type *ElemTy);
00430 
00431   /// @brief Return the number of elements in the Vector type.
00432   unsigned getNumElements() const { return NumElements; }
00433 
00434   /// @brief Return the number of bits in the Vector type.
00435   /// Returns zero when the vector is a vector of pointers.
00436   unsigned getBitWidth() const {
00437     return NumElements * getElementType()->getPrimitiveSizeInBits();
00438   }
00439 
00440   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00441   static inline bool classof(const Type *T) {
00442     return T->getTypeID() == VectorTyID;
00443   }
00444 };
00445 
00446 
00447 /// PointerType - Class to represent pointers.
00448 ///
00449 class PointerType : public SequentialType {
00450   PointerType(const PointerType &) = delete;
00451   const PointerType &operator=(const PointerType &) = delete;
00452   explicit PointerType(Type *ElType, unsigned AddrSpace);
00453 public:
00454   /// PointerType::get - This constructs a pointer to an object of the specified
00455   /// type in a numbered address space.
00456   static PointerType *get(Type *ElementType, unsigned AddressSpace);
00457 
00458   /// PointerType::getUnqual - This constructs a pointer to an object of the
00459   /// specified type in the generic address space (address space zero).
00460   static PointerType *getUnqual(Type *ElementType) {
00461     return PointerType::get(ElementType, 0);
00462   }
00463 
00464   /// isValidElementType - Return true if the specified type is valid as a
00465   /// element type.
00466   static bool isValidElementType(Type *ElemTy);
00467 
00468   /// Return true if we can load or store from a pointer to this type.
00469   static bool isLoadableOrStorableType(Type *ElemTy);
00470 
00471   /// @brief Return the address space of the Pointer type.
00472   inline unsigned getAddressSpace() const { return getSubclassData(); }
00473 
00474   /// Implement support type inquiry through isa, cast, and dyn_cast.
00475   static inline bool classof(const Type *T) {
00476     return T->getTypeID() == PointerTyID;
00477   }
00478 };
00479 
00480 } // End llvm namespace
00481 
00482 #endif