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 
00127   /// Parameter type accessors.
00128   Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
00129 
00130   /// getNumParams - Return the number of fixed parameters this function type
00131   /// requires.  This does not consider varargs.
00132   ///
00133   unsigned getNumParams() const { return NumContainedTys - 1; }
00134 
00135   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00136   static inline bool classof(const Type *T) {
00137     return T->getTypeID() == FunctionTyID;
00138   }
00139 };
00140 
00141 
00142 /// CompositeType - Common super class of ArrayType, StructType, PointerType
00143 /// and VectorType.
00144 class CompositeType : public Type {
00145 protected:
00146   explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) { }
00147 public:
00148 
00149   /// getTypeAtIndex - Given an index value into the type, return the type of
00150   /// the element.
00151   ///
00152   Type *getTypeAtIndex(const Value *V);
00153   Type *getTypeAtIndex(unsigned Idx);
00154   bool indexValid(const Value *V) const;
00155   bool indexValid(unsigned Idx) const;
00156 
00157   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00158   static inline bool classof(const Type *T) {
00159     return T->getTypeID() == ArrayTyID ||
00160            T->getTypeID() == StructTyID ||
00161            T->getTypeID() == PointerTyID ||
00162            T->getTypeID() == VectorTyID;
00163   }
00164 };
00165 
00166 
00167 /// StructType - Class to represent struct types.  There are two different kinds
00168 /// of struct types: Literal structs and Identified structs.
00169 ///
00170 /// Literal struct types (e.g. { i32, i32 }) are uniqued structurally, and must
00171 /// always have a body when created.  You can get one of these by using one of
00172 /// the StructType::get() forms.
00173 ///  
00174 /// Identified structs (e.g. %foo or %42) may optionally have a name and are not
00175 /// uniqued.  The names for identified structs are managed at the LLVMContext
00176 /// level, so there can only be a single identified struct with a given name in
00177 /// a particular LLVMContext.  Identified structs may also optionally be opaque
00178 /// (have no body specified).  You get one of these by using one of the
00179 /// StructType::create() forms.
00180 ///
00181 /// Independent of what kind of struct you have, the body of a struct type are
00182 /// laid out in memory consequtively with the elements directly one after the
00183 /// other (if the struct is packed) or (if not packed) with padding between the
00184 /// elements as defined by DataLayout (which is required to match what the code
00185 /// generator for a target expects).
00186 ///
00187 class StructType : public CompositeType {
00188   StructType(const StructType &) LLVM_DELETED_FUNCTION;
00189   const StructType &operator=(const StructType &) LLVM_DELETED_FUNCTION;
00190   StructType(LLVMContext &C)
00191     : CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
00192   enum {
00193     /// This is the contents of the SubClassData field.
00194     SCDB_HasBody = 1,
00195     SCDB_Packed = 2,
00196     SCDB_IsLiteral = 4,
00197     SCDB_IsSized = 8
00198   };
00199 
00200   /// SymbolTableEntry - For a named struct that actually has a name, this is a
00201   /// pointer to the symbol table entry (maintained by LLVMContext) for the
00202   /// struct.  This is null if the type is an literal struct or if it is
00203   /// a identified type that has an empty name.
00204   /// 
00205   void *SymbolTableEntry;
00206 public:
00207   ~StructType() {
00208     delete [] ContainedTys; // Delete the body.
00209   }
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, ...) 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, ...) 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(SmallPtrSet<const Type*, 4> *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, ...) 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 
00281   /// isLayoutIdentical - Return true if this is layout identical to the
00282   /// specified struct.
00283   bool isLayoutIdentical(StructType *Other) const;  
00284   
00285   /// Random access to the elements
00286   unsigned getNumElements() const { return NumContainedTys; }
00287   Type *getElementType(unsigned N) const {
00288     assert(N < NumContainedTys && "Element number out of range!");
00289     return ContainedTys[N];
00290   }
00291 
00292   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00293   static inline bool classof(const Type *T) {
00294     return T->getTypeID() == StructTyID;
00295   }
00296 };
00297 
00298 /// SequentialType - This is the superclass of the array, pointer and vector
00299 /// type classes.  All of these represent "arrays" in memory.  The array type
00300 /// represents a specifically sized array, pointer types are unsized/unknown
00301 /// size arrays, vector types represent specifically sized arrays that
00302 /// allow for use of SIMD instructions.  SequentialType holds the common
00303 /// features of all, which stem from the fact that all three lay their
00304 /// components out in memory identically.
00305 ///
00306 class SequentialType : public CompositeType {
00307   Type *ContainedType;               ///< Storage for the single contained type.
00308   SequentialType(const SequentialType &) LLVM_DELETED_FUNCTION;
00309   const SequentialType &operator=(const SequentialType &) LLVM_DELETED_FUNCTION;
00310 
00311 protected:
00312   SequentialType(TypeID TID, Type *ElType)
00313     : CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
00314     ContainedTys = &ContainedType;
00315     NumContainedTys = 1;
00316   }
00317 
00318 public:
00319   Type *getElementType() const { return ContainedTys[0]; }
00320 
00321   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00322   static inline bool classof(const Type *T) {
00323     return T->getTypeID() == ArrayTyID ||
00324            T->getTypeID() == PointerTyID ||
00325            T->getTypeID() == VectorTyID;
00326   }
00327 };
00328 
00329 
00330 /// ArrayType - Class to represent array types.
00331 ///
00332 class ArrayType : public SequentialType {
00333   uint64_t NumElements;
00334 
00335   ArrayType(const ArrayType &) LLVM_DELETED_FUNCTION;
00336   const ArrayType &operator=(const ArrayType &) LLVM_DELETED_FUNCTION;
00337   ArrayType(Type *ElType, uint64_t NumEl);
00338 public:
00339   /// ArrayType::get - This static method is the primary way to construct an
00340   /// ArrayType
00341   ///
00342   static ArrayType *get(Type *ElementType, uint64_t NumElements);
00343 
00344   /// isValidElementType - Return true if the specified type is valid as a
00345   /// element type.
00346   static bool isValidElementType(Type *ElemTy);
00347 
00348   uint64_t getNumElements() const { return NumElements; }
00349 
00350   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00351   static inline bool classof(const Type *T) {
00352     return T->getTypeID() == ArrayTyID;
00353   }
00354 };
00355 
00356 /// VectorType - Class to represent vector types.
00357 ///
00358 class VectorType : public SequentialType {
00359   unsigned NumElements;
00360 
00361   VectorType(const VectorType &) LLVM_DELETED_FUNCTION;
00362   const VectorType &operator=(const VectorType &) LLVM_DELETED_FUNCTION;
00363   VectorType(Type *ElType, unsigned NumEl);
00364 public:
00365   /// VectorType::get - This static method is the primary way to construct an
00366   /// VectorType.
00367   ///
00368   static VectorType *get(Type *ElementType, unsigned NumElements);
00369 
00370   /// VectorType::getInteger - This static method gets a VectorType with the
00371   /// same number of elements as the input type, and the element type is an
00372   /// integer type of the same width as the input element type.
00373   ///
00374   static VectorType *getInteger(VectorType *VTy) {
00375     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00376     assert(EltBits && "Element size must be of a non-zero size");
00377     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
00378     return VectorType::get(EltTy, VTy->getNumElements());
00379   }
00380 
00381   /// VectorType::getExtendedElementVectorType - This static method is like
00382   /// getInteger except that the element types are twice as wide as the
00383   /// elements in the input type.
00384   ///
00385   static VectorType *getExtendedElementVectorType(VectorType *VTy) {
00386     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00387     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
00388     return VectorType::get(EltTy, VTy->getNumElements());
00389   }
00390 
00391   /// VectorType::getTruncatedElementVectorType - This static method is like
00392   /// getInteger except that the element types are half as wide as the
00393   /// elements in the input type.
00394   ///
00395   static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
00396     unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
00397     assert((EltBits & 1) == 0 &&
00398            "Cannot truncate vector element with odd bit-width");
00399     Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
00400     return VectorType::get(EltTy, VTy->getNumElements());
00401   }
00402 
00403   /// VectorType::getHalfElementsVectorType - This static method returns
00404   /// a VectorType with half as many elements as the input type and the
00405   /// same element type.
00406   ///
00407   static VectorType *getHalfElementsVectorType(VectorType *VTy) {
00408     unsigned NumElts = VTy->getNumElements();
00409     assert ((NumElts & 1) == 0 &&
00410             "Cannot halve vector with odd number of elements.");
00411     return VectorType::get(VTy->getElementType(), NumElts/2);
00412   }
00413 
00414   /// VectorType::getDoubleElementsVectorType - This static method returns
00415   /// a VectorType with twice  as many elements as the input type and the
00416   /// same element type.
00417   ///
00418   static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
00419     unsigned NumElts = VTy->getNumElements();
00420     return VectorType::get(VTy->getElementType(), NumElts*2);
00421   }
00422 
00423   /// isValidElementType - Return true if the specified type is valid as a
00424   /// element type.
00425   static bool isValidElementType(Type *ElemTy);
00426 
00427   /// @brief Return the number of elements in the Vector type.
00428   unsigned getNumElements() const { return NumElements; }
00429 
00430   /// @brief Return the number of bits in the Vector type.
00431   /// Returns zero when the vector is a vector of pointers.
00432   unsigned getBitWidth() const {
00433     return NumElements * getElementType()->getPrimitiveSizeInBits();
00434   }
00435 
00436   /// Methods for support type inquiry through isa, cast, and dyn_cast.
00437   static inline bool classof(const Type *T) {
00438     return T->getTypeID() == VectorTyID;
00439   }
00440 };
00441 
00442 
00443 /// PointerType - Class to represent pointers.
00444 ///
00445 class PointerType : public SequentialType {
00446   PointerType(const PointerType &) LLVM_DELETED_FUNCTION;
00447   const PointerType &operator=(const PointerType &) LLVM_DELETED_FUNCTION;
00448   explicit PointerType(Type *ElType, unsigned AddrSpace);
00449 public:
00450   /// PointerType::get - This constructs a pointer to an object of the specified
00451   /// type in a numbered address space.
00452   static PointerType *get(Type *ElementType, unsigned AddressSpace);
00453 
00454   /// PointerType::getUnqual - This constructs a pointer to an object of the
00455   /// specified type in the generic address space (address space zero).
00456   static PointerType *getUnqual(Type *ElementType) {
00457     return PointerType::get(ElementType, 0);
00458   }
00459 
00460   /// isValidElementType - Return true if the specified type is valid as a
00461   /// element type.
00462   static bool isValidElementType(Type *ElemTy);
00463 
00464   /// @brief Return the address space of the Pointer type.
00465   inline unsigned getAddressSpace() const { return getSubclassData(); }
00466 
00467   /// Implement support type inquiry through isa, cast, and dyn_cast.
00468   static inline bool classof(const Type *T) {
00469     return T->getTypeID() == PointerTyID;
00470   }
00471 };
00472 
00473 } // End llvm namespace
00474 
00475 #endif