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