LLVM API Documentation

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 &) LLVM_DELETED_FUNCTION;
00098   const FunctionType &operator=(const FunctionType &) LLVM_DELETED_FUNCTION;
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 
00144 
00145 /// CompositeType - Common super class of ArrayType, StructType, PointerType
00146 /// and VectorType.
00147 class CompositeType : public Type {
00148 protected:
00149   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
00150 public:
00151 
00152   /// getTypeAtIndex - Given an index value into the type, return the type of
00153   /// the element.
00154   ///
00155   Type *getTypeAtIndex(const Value *V);
00156   Type *getTypeAtIndex(unsigned Idx);
00157   bool indexValid(const Value *V) const;
00158   bool indexValid(unsigned Idx) const;
00159 
00160   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00161   static inline bool classof(const Type *T) {
00162     return T->getTypeID() == ArrayTyID ||
00163            T->getTypeID() == StructTyID ||
00164            T->getTypeID() == PointerTyID ||
00165            T->getTypeID() == VectorTyID;
00166   }
00167 };
00168 
00169 
00170 /// StructType - Class to represent struct types.  There are two different kinds
00171 /// of struct types: Literal structs and Identified structs.
00172 ///
00173 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
00174 /// always have a body when created.  You can get one of these by using one of
00175 /// the StructType::get() forms.
00176 ///  
00177 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
00178 /// uniqued.  The names for identified structs are managed at the LLVMContext
00179 /// level, so there can only be a single identified struct with a given name in
00180 /// a particular LLVMContext.  Identified structs may also optionally be opaque
00181 /// (have no body specified).  You get one of these by using one of the
00182 /// StructType::create() forms.
00183 ///
00184 /// Independent of what kind of struct you have, the body of a struct type are
00185 /// laid out in memory consequtively with the elements directly one after the
00186 /// other (if the struct is packed) or (if not packed) with padding between the
00187 /// elements as defined by DataLayout (which is required to match what the code
00188 /// generator for a target expects).
00189 ///
00190 class StructType : public CompositeType {
00191   StructType(const StructType &) LLVM_DELETED_FUNCTION;
00192   const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION;
00193   StructType(LLVMContext &C)
00194     : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
00195   enum {
00196     /// This is the contents of the SubClassData field.
00197     SCDB_HasBody = 1,
00198     SCDB_Packed = 2,
00199     SCDB_IsLiteral = 4,
00200     SCDB_IsSized = 8
00201   };
00202 
00203   /// SymbolTableEntry - For a named struct that actually has a name, this is a
00204   /// pointer to the symbol table entry (maintained by LLVMContext) for the
00205   /// struct.  This is null if the type is an literal struct or if it is
00206   /// a identified type that has an empty name.
00207   /// 
00208   void *SymbolTableEntry;
00209 public:
00210 
00211   /// StructType::create - This creates an identified struct.
00212   static StructType *create(LLVMContext &Context, StringRef Name);
00213   static StructType *create(LLVMContext &Context);
00214   
00215   static StructType *create(ArrayRef<Type*> Elements,
00216                             StringRef Name,
00217                             bool isPacked = false);
00218   static StructType *create(ArrayRef<Type*> Elements);
00219   static StructType *create(LLVMContext &Context,
00220                             ArrayRef<Type*> Elements,
00221                             StringRef Name,
00222                             bool isPacked = false);
00223   static StructType *create(LLVMContext &Context, ArrayRef<Type*> Elements);
00224   static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
00225 
00226   /// StructType::get - This static method is the primary way to create a
00227   /// literal StructType.
00228   static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
00229                          bool isPacked = false);
00230 
00231   /// StructType::get - Create an empty structure type.
00232   ///
00233   static StructType *get(LLVMContext &Context, bool isPacked = false);
00234   
00235   /// StructType::get - This static method is a convenience method for creating
00236   /// structure types by specifying the elements as arguments.  Note that this
00237   /// method always returns a non-packed struct, and requires at least one
00238   /// element type.
00239   static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
00240 
00241   bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
00242   
00243   /// isLiteral - Return true if this type is uniqued by structural
00244   /// equivalence, false if it is a struct definition.
00245   bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
00246   
00247   /// isOpaque - Return true if this is a type with an identity that has no body
00248   /// specified yet.  These prints as 'opaque' in .ll files.
00249   bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
00250 
00251   /// isSized - Return true if this is a sized type.
00252   bool isSized(SmallPtrSetImpl<const Type*> *Visited = nullptr) const;
00253   
00254   /// hasName - Return true if this is a named struct that has a non-empty name.
00255   bool hasName() const { return SymbolTableEntry != nullptr; }
00256   
00257   /// getName - Return the name for this struct type if it has an identity.
00258   /// This may return an empty string for an unnamed struct type.  Do not call
00259   /// this on an literal type.
00260   StringRef getName() const;
00261   
00262   /// setName - Change the name of this type to the specified name, or to a name
00263   /// with a suffix if there is a collision.  Do not call this on an literal
00264   /// type.
00265   void setName(StringRef Name);
00266 
00267   /// setBody - Specify a body for an opaque identified type.
00268   void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
00269   void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
00270   
00271   /// isValidElementType - Return true if the specified type is valid as a
00272   /// element type.
00273   static bool isValidElementType(Type *ElemTy);
00274   
00275 
00276   // Iterator access to the elements.
00277   typedef Type::subtype_iterator element_iterator;
00278   element_iterator element_begin() const { return ContainedTys; }
00279   element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
00280   ArrayRef<Type *> const elements() const {
00281     return makeArrayRef(element_begin(), element_end());
00282   }
00283 
00284   /// isLayoutIdentical - Return true if this is layout identical to the
00285   /// specified struct.
00286   bool isLayoutIdentical(StructType *Other) const;  
00287   
00288   /// Random access to the elements
00289   unsigned getNumElements() const { return NumContainedTys; }
00290   Type *getElementType(unsigned N) const {
00291     assert(N < NumContainedTys && "Element number out of range!");
00292     return ContainedTys[N];
00293   }
00294 
00295   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00296   static inline bool classof(const Type *T) {
00297     return T->getTypeID() == StructTyID;
00298   }
00299 };
00300 
00301 /// SequentialType - This is the superclass of the array, pointer and vector
00302 /// type classes.  All of these represent "arrays" in memory.  The array type
00303 /// represents a specifically sized array, pointer types are unsized/unknown
00304 /// size arrays, vector types represent specifically sized arrays that
00305 /// allow for use of SIMD instructions.  SequentialType holds the common
00306 /// features of all, which stem from the fact that all three lay their
00307 /// components out in memory identically.
00308 ///
00309 class SequentialType : public CompositeType {
00310   Type *ContainedType;               ///< Storage for the single contained type.
00311   SequentialType(const SequentialType &) LLVM_DELETED_FUNCTION;
00312   const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION;
00313 
00314 protected:
00315   SequentialType(TypeID TID, Type *ElType)
00316     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
00317     ContainedTys = &ContainedType;
00318     NumContainedTys = 1;
00319   }
00320 
00321 public:
00322   Type *getElementType() const { return ContainedTys[0]; }
00323 
00324   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00325   static inline bool classof(const Type *T) {
00326     return T->getTypeID() == ArrayTyID ||
00327            T->getTypeID() == PointerTyID ||
00328            T->getTypeID() == VectorTyID;
00329   }
00330 };
00331 
00332 
00333 /// ArrayType - Class to represent array types.
00334 ///
00335 class ArrayType : public SequentialType {
00336   uint64_t NumElements;
00337 
00338   ArrayType(const ArrayType &) LLVM_DELETED_FUNCTION;
00339   const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION;
00340   ArrayType(Type *ElType, uint64_t NumEl);
00341 public:
00342   /// ArrayType::get - This static method is the primary way to construct an
00343   /// ArrayType
00344   ///
00345   static ArrayType *get(Type *ElementType, uint64_t NumElements);
00346 
00347   /// isValidElementType - Return true if the specified type is valid as a
00348   /// element type.
00349   static bool isValidElementType(Type *ElemTy);
00350 
00351   uint64_t getNumElements() const { return NumElements; }
00352 
00353   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00354   static inline bool classof(const Type *T) {
00355     return T->getTypeID() == ArrayTyID;
00356   }
00357 };
00358 
00359 /// VectorType - Class to represent vector types.
00360 ///
00361 class VectorType : public SequentialType {
00362   unsigned NumElements;
00363 
00364   VectorType(const VectorType &) LLVM_DELETED_FUNCTION;
00365   const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION;
00366   VectorType(Type *ElType, unsigned NumEl);
00367 public:
00368   /// VectorType::get - This static method is the primary way to construct an
00369   /// VectorType.
00370   ///
00371   static VectorType *get(Type *ElementType, unsigned NumElements);
00372 
00373   /// VectorType::getInteger - This static method gets a VectorType with the
00374   /// same number of elements as the input type, and the element type is an
00375   /// integer type of the same width as the input element type.
00376   ///
00377   static VectorType *getInteger(VectorType *VTy) {
00378     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00379     assert(EltBits && "Element size must be of a non-zero size");
00380     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
00381     return VectorType::get(EltTy, VTy->getNumElements());
00382   }
00383 
00384   /// VectorType::getExtendedElementVectorType - This static method is like
00385   /// getInteger except that the element types are twice as wide as the
00386   /// elements in the input type.
00387   ///
00388   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
00389     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00390     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
00391     return VectorType::get(EltTy, VTy->getNumElements());
00392   }
00393 
00394   /// VectorType::getTruncatedElementVectorType - This static method is like
00395   /// getInteger except that the element types are half as wide as the
00396   /// elements in the input type.
00397   ///
00398   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
00399     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00400     assert((EltBits & 1) == 0 &&
00401            "Cannot truncate vector element with odd bit-width");
00402     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
00403     return VectorType::get(EltTy, VTy->getNumElements());
00404   }
00405 
00406   /// VectorType::getHalfElementsVectorType - This static method returns
00407   /// a VectorType with half as many elements as the input type and the
00408   /// same element type.
00409   ///
00410   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
00411     unsigned NumElts = VTy->getNumElements();
00412     assert ((NumElts & 1) == 0 &&
00413             "Cannot halve vector with odd number of elements.");
00414     return VectorType::get(VTy->getElementType(), NumElts/2);
00415   }
00416 
00417   /// VectorType::getDoubleElementsVectorType - This static method returns
00418   /// a VectorType with twice  as many elements as the input type and the
00419   /// same element type.
00420   ///
00421   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
00422     unsigned NumElts = VTy->getNumElements();
00423     return VectorType::get(VTy->getElementType(), NumElts*2);
00424   }
00425 
00426   /// isValidElementType - Return true if the specified type is valid as a
00427   /// element type.
00428   static bool isValidElementType(Type *ElemTy);
00429 
00430   /// @brief Return the number of elements in the Vector type.
00431   unsigned getNumElements() const { return NumElements; }
00432 
00433   /// @brief Return the number of bits in the Vector type.
00434   /// Returns zero when the vector is a vector of pointers.
00435   unsigned getBitWidth() const {
00436     return NumElements * getElementType()->getPrimitiveSizeInBits();
00437   }
00438 
00439   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00440   static inline bool classof(const Type *T) {
00441     return T->getTypeID() == VectorTyID;
00442   }
00443 };
00444 
00445 
00446 /// PointerType - Class to represent pointers.
00447 ///
00448 class PointerType : public SequentialType {
00449   PointerType(const PointerType &) LLVM_DELETED_FUNCTION;
00450   const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION;
00451   explicit PointerType(Type *ElType, unsigned AddrSpace);
00452 public:
00453   /// PointerType::get - This constructs a pointer to an object of the specified
00454   /// type in a numbered address space.
00455   static PointerType *get(Type *ElementType, unsigned AddressSpace);
00456 
00457   /// PointerType::getUnqual - This constructs a pointer to an object of the
00458   /// specified type in the generic address space (address space zero).
00459   static PointerType *getUnqual(Type *ElementType) {
00460     return PointerType::get(ElementType, 0);
00461   }
00462 
00463   /// isValidElementType - Return true if the specified type is valid as a
00464   /// element type.
00465   static bool isValidElementType(Type *ElemTy);
00466 
00467   /// @brief Return the address space of the Pointer type.
00468   inline unsigned getAddressSpace() const { return getSubclassData(); }
00469 
00470   /// Implement support type inquiry through isa, cast, and dyn_cast.
00471   static inline bool classof(const Type *T) {
00472     return T->getTypeID() == PointerTyID;
00473   }
00474 };
00475 
00476 } // End llvm namespace
00477 
00478 #endif