Home | History | Annotate | Download | only in IR
      1 //===-- llvm/Type.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 declaration of the Type class.  For more "Type"
     11 // stuff, look in DerivedTypes.h.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_IR_TYPE_H
     16 #define LLVM_IR_TYPE_H
     17 
     18 #include "llvm/ADT/APFloat.h"
     19 #include "llvm/Support/Casting.h"
     20 #include "llvm/Support/DataTypes.h"
     21 #include "llvm/Support/ErrorHandling.h"
     22 
     23 namespace llvm {
     24 
     25 class PointerType;
     26 class IntegerType;
     27 class raw_ostream;
     28 class Module;
     29 class LLVMContext;
     30 class LLVMContextImpl;
     31 class StringRef;
     32 template<class GraphType> struct GraphTraits;
     33 
     34 /// The instances of the Type class are immutable: once they are created,
     35 /// they are never changed.  Also note that only one instance of a particular
     36 /// type is ever created.  Thus seeing if two types are equal is a matter of
     37 /// doing a trivial pointer comparison. To enforce that no two equal instances
     38 /// are created, Type instances can only be created via static factory methods
     39 /// in class Type and in derived classes.  Once allocated, Types are never
     40 /// free'd.
     41 ///
     42 class Type {
     43 public:
     44   //===--------------------------------------------------------------------===//
     45   /// Definitions of all of the base types for the Type system.  Based on this
     46   /// value, you can cast to a class defined in DerivedTypes.h.
     47   /// Note: If you add an element to this, you need to add an element to the
     48   /// Type::getPrimitiveType function, or else things will break!
     49   /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
     50   ///
     51   enum TypeID {
     52     // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
     53     VoidTyID = 0,    ///<  0: type with no size
     54     HalfTyID,        ///<  1: 16-bit floating point type
     55     FloatTyID,       ///<  2: 32-bit floating point type
     56     DoubleTyID,      ///<  3: 64-bit floating point type
     57     X86_FP80TyID,    ///<  4: 80-bit floating point type (X87)
     58     FP128TyID,       ///<  5: 128-bit floating point type (112-bit mantissa)
     59     PPC_FP128TyID,   ///<  6: 128-bit floating point type (two 64-bits, PowerPC)
     60     LabelTyID,       ///<  7: Labels
     61     MetadataTyID,    ///<  8: Metadata
     62     X86_MMXTyID,     ///<  9: MMX vectors (64 bits, X86 specific)
     63 
     64     // Derived types... see DerivedTypes.h file.
     65     // Make sure FirstDerivedTyID stays up to date!
     66     IntegerTyID,     ///< 10: Arbitrary bit width integers
     67     FunctionTyID,    ///< 11: Functions
     68     StructTyID,      ///< 12: Structures
     69     ArrayTyID,       ///< 13: Arrays
     70     PointerTyID,     ///< 14: Pointers
     71     VectorTyID,      ///< 15: SIMD 'packed' format, or other vector type
     72 
     73     NumTypeIDs,                         // Must remain as last defined ID
     74     LastPrimitiveTyID = X86_MMXTyID,
     75     FirstDerivedTyID = IntegerTyID
     76   };
     77 
     78 private:
     79   /// Context - This refers to the LLVMContext in which this type was uniqued.
     80   LLVMContext &Context;
     81 
     82   // Due to Ubuntu GCC bug 910363:
     83   // https://bugs.launchpad.net/ubuntu/+source/gcc-4.5/+bug/910363
     84   // Bitpack ID and SubclassData manually.
     85   // Note: TypeID : low 8 bit; SubclassData : high 24 bit.
     86   uint32_t IDAndSubclassData;
     87 
     88 protected:
     89   friend class LLVMContextImpl;
     90   explicit Type(LLVMContext &C, TypeID tid)
     91     : Context(C), IDAndSubclassData(0),
     92       NumContainedTys(0), ContainedTys(0) {
     93     setTypeID(tid);
     94   }
     95   ~Type() {}
     96 
     97   void setTypeID(TypeID ID) {
     98     IDAndSubclassData = (ID & 0xFF) | (IDAndSubclassData & 0xFFFFFF00);
     99     assert(getTypeID() == ID && "TypeID data too large for field");
    100   }
    101 
    102   unsigned getSubclassData() const { return IDAndSubclassData >> 8; }
    103 
    104   void setSubclassData(unsigned val) {
    105     IDAndSubclassData = (IDAndSubclassData & 0xFF) | (val << 8);
    106     // Ensure we don't have any accidental truncation.
    107     assert(getSubclassData() == val && "Subclass data too large for field");
    108   }
    109 
    110   /// NumContainedTys - Keeps track of how many Type*'s there are in the
    111   /// ContainedTys list.
    112   unsigned NumContainedTys;
    113 
    114   /// ContainedTys - A pointer to the array of Types contained by this Type.
    115   /// For example, this includes the arguments of a function type, the elements
    116   /// of a structure, the pointee of a pointer, the element type of an array,
    117   /// etc.  This pointer may be 0 for types that don't contain other types
    118   /// (Integer, Double, Float).
    119   Type * const *ContainedTys;
    120 
    121 public:
    122   void print(raw_ostream &O) const;
    123   void dump() const;
    124 
    125   /// getContext - Return the LLVMContext in which this type was uniqued.
    126   LLVMContext &getContext() const { return Context; }
    127 
    128   //===--------------------------------------------------------------------===//
    129   // Accessors for working with types.
    130   //
    131 
    132   /// getTypeID - Return the type id for the type.  This will return one
    133   /// of the TypeID enum elements defined above.
    134   ///
    135   TypeID getTypeID() const { return (TypeID)(IDAndSubclassData & 0xFF); }
    136 
    137   /// isVoidTy - Return true if this is 'void'.
    138   bool isVoidTy() const { return getTypeID() == VoidTyID; }
    139 
    140   /// isHalfTy - Return true if this is 'half', a 16-bit IEEE fp type.
    141   bool isHalfTy() const { return getTypeID() == HalfTyID; }
    142 
    143   /// isFloatTy - Return true if this is 'float', a 32-bit IEEE fp type.
    144   bool isFloatTy() const { return getTypeID() == FloatTyID; }
    145 
    146   /// isDoubleTy - Return true if this is 'double', a 64-bit IEEE fp type.
    147   bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
    148 
    149   /// isX86_FP80Ty - Return true if this is x86 long double.
    150   bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
    151 
    152   /// isFP128Ty - Return true if this is 'fp128'.
    153   bool isFP128Ty() const { return getTypeID() == FP128TyID; }
    154 
    155   /// isPPC_FP128Ty - Return true if this is powerpc long double.
    156   bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
    157 
    158   /// isFloatingPointTy - Return true if this is one of the six floating point
    159   /// types
    160   bool isFloatingPointTy() const {
    161     return getTypeID() == HalfTyID || getTypeID() == FloatTyID ||
    162            getTypeID() == DoubleTyID ||
    163            getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
    164            getTypeID() == PPC_FP128TyID;
    165   }
    166 
    167   const fltSemantics &getFltSemantics() const {
    168     switch (getTypeID()) {
    169     case HalfTyID: return APFloat::IEEEhalf;
    170     case FloatTyID: return APFloat::IEEEsingle;
    171     case DoubleTyID: return APFloat::IEEEdouble;
    172     case X86_FP80TyID: return APFloat::x87DoubleExtended;
    173     case FP128TyID: return APFloat::IEEEquad;
    174     case PPC_FP128TyID: return APFloat::PPCDoubleDouble;
    175     default: llvm_unreachable("Invalid floating type");
    176     }
    177   }
    178 
    179   /// isX86_MMXTy - Return true if this is X86 MMX.
    180   bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
    181 
    182   /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP.
    183   ///
    184   bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
    185 
    186   /// isLabelTy - Return true if this is 'label'.
    187   bool isLabelTy() const { return getTypeID() == LabelTyID; }
    188 
    189   /// isMetadataTy - Return true if this is 'metadata'.
    190   bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
    191 
    192   /// isIntegerTy - True if this is an instance of IntegerType.
    193   ///
    194   bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
    195 
    196   /// isIntegerTy - Return true if this is an IntegerType of the given width.
    197   bool isIntegerTy(unsigned Bitwidth) const;
    198 
    199   /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
    200   /// integer types.
    201   ///
    202   bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
    203 
    204   /// isFunctionTy - True if this is an instance of FunctionType.
    205   ///
    206   bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
    207 
    208   /// isStructTy - True if this is an instance of StructType.
    209   ///
    210   bool isStructTy() const { return getTypeID() == StructTyID; }
    211 
    212   /// isArrayTy - True if this is an instance of ArrayType.
    213   ///
    214   bool isArrayTy() const { return getTypeID() == ArrayTyID; }
    215 
    216   /// isPointerTy - True if this is an instance of PointerType.
    217   ///
    218   bool isPointerTy() const { return getTypeID() == PointerTyID; }
    219 
    220   /// isPtrOrPtrVectorTy - Return true if this is a pointer type or a vector of
    221   /// pointer types.
    222   ///
    223   bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
    224 
    225   /// isVectorTy - True if this is an instance of VectorType.
    226   ///
    227   bool isVectorTy() const { return getTypeID() == VectorTyID; }
    228 
    229   /// canLosslesslyBitCastTo - Return true if this type could be converted
    230   /// with a lossless BitCast to type 'Ty'. For example, i8* to i32*. BitCasts
    231   /// are valid for types of the same size only where no re-interpretation of
    232   /// the bits is done.
    233   /// @brief Determine if this type could be losslessly bitcast to Ty
    234   bool canLosslesslyBitCastTo(Type *Ty) const;
    235 
    236   /// isEmptyTy - Return true if this type is empty, that is, it has no
    237   /// elements or all its elements are empty.
    238   bool isEmptyTy() const;
    239 
    240   /// Here are some useful little methods to query what type derived types are
    241   /// Note that all other types can just compare to see if this == Type::xxxTy;
    242   ///
    243   bool isPrimitiveType() const { return getTypeID() <= LastPrimitiveTyID; }
    244   bool isDerivedType()   const { return getTypeID() >= FirstDerivedTyID; }
    245 
    246   /// isFirstClassType - Return true if the type is "first class", meaning it
    247   /// is a valid type for a Value.
    248   ///
    249   bool isFirstClassType() const {
    250     return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
    251   }
    252 
    253   /// isSingleValueType - Return true if the type is a valid type for a
    254   /// register in codegen.  This includes all first-class types except struct
    255   /// and array types.
    256   ///
    257   bool isSingleValueType() const {
    258     return (getTypeID() != VoidTyID && isPrimitiveType()) ||
    259             getTypeID() == IntegerTyID || getTypeID() == PointerTyID ||
    260             getTypeID() == VectorTyID;
    261   }
    262 
    263   /// isAggregateType - Return true if the type is an aggregate type. This
    264   /// means it is valid as the first operand of an insertvalue or
    265   /// extractvalue instruction. This includes struct and array types, but
    266   /// does not include vector types.
    267   ///
    268   bool isAggregateType() const {
    269     return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
    270   }
    271 
    272   /// isSized - Return true if it makes sense to take the size of this type.  To
    273   /// get the actual size for a particular target, it is reasonable to use the
    274   /// DataLayout subsystem to do this.
    275   ///
    276   bool isSized() const {
    277     // If it's a primitive, it is always sized.
    278     if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
    279         getTypeID() == PointerTyID ||
    280         getTypeID() == X86_MMXTyID)
    281       return true;
    282     // If it is not something that can have a size (e.g. a function or label),
    283     // it doesn't have a size.
    284     if (getTypeID() != StructTyID && getTypeID() != ArrayTyID &&
    285         getTypeID() != VectorTyID)
    286       return false;
    287     // Otherwise we have to try harder to decide.
    288     return isSizedDerivedType();
    289   }
    290 
    291   /// getPrimitiveSizeInBits - Return the basic size of this type if it is a
    292   /// primitive type.  These are fixed by LLVM and are not target dependent.
    293   /// This will return zero if the type does not have a size or is not a
    294   /// primitive type.
    295   ///
    296   /// Note that this may not reflect the size of memory allocated for an
    297   /// instance of the type or the number of bytes that are written when an
    298   /// instance of the type is stored to memory. The DataLayout class provides
    299   /// additional query functions to provide this information.
    300   ///
    301   unsigned getPrimitiveSizeInBits() const;
    302 
    303   /// getScalarSizeInBits - If this is a vector type, return the
    304   /// getPrimitiveSizeInBits value for the element type. Otherwise return the
    305   /// getPrimitiveSizeInBits value for this type.
    306   unsigned getScalarSizeInBits();
    307 
    308   /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
    309   /// is only valid on floating point types.  If the FP type does not
    310   /// have a stable mantissa (e.g. ppc long double), this method returns -1.
    311   int getFPMantissaWidth() const;
    312 
    313   /// getScalarType - If this is a vector type, return the element type,
    314   /// otherwise return 'this'.
    315   const Type *getScalarType() const;
    316   Type *getScalarType();
    317 
    318   //===--------------------------------------------------------------------===//
    319   // Type Iteration support.
    320   //
    321   typedef Type * const *subtype_iterator;
    322   subtype_iterator subtype_begin() const { return ContainedTys; }
    323   subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
    324 
    325   /// getContainedType - This method is used to implement the type iterator
    326   /// (defined a the end of the file).  For derived types, this returns the
    327   /// types 'contained' in the derived type.
    328   ///
    329   Type *getContainedType(unsigned i) const {
    330     assert(i < NumContainedTys && "Index out of range!");
    331     return ContainedTys[i];
    332   }
    333 
    334   /// getNumContainedTypes - Return the number of types in the derived type.
    335   ///
    336   unsigned getNumContainedTypes() const { return NumContainedTys; }
    337 
    338   //===--------------------------------------------------------------------===//
    339   // Helper methods corresponding to subclass methods.  This forces a cast to
    340   // the specified subclass and calls its accessor.  "getVectorNumElements" (for
    341   // example) is shorthand for cast<VectorType>(Ty)->getNumElements().  This is
    342   // only intended to cover the core methods that are frequently used, helper
    343   // methods should not be added here.
    344 
    345   unsigned getIntegerBitWidth() const;
    346 
    347   Type *getFunctionParamType(unsigned i) const;
    348   unsigned getFunctionNumParams() const;
    349   bool isFunctionVarArg() const;
    350 
    351   StringRef getStructName() const;
    352   unsigned getStructNumElements() const;
    353   Type *getStructElementType(unsigned N) const;
    354 
    355   Type *getSequentialElementType() const;
    356 
    357   uint64_t getArrayNumElements() const;
    358   Type *getArrayElementType() const { return getSequentialElementType(); }
    359 
    360   unsigned getVectorNumElements() const;
    361   Type *getVectorElementType() const { return getSequentialElementType(); }
    362 
    363   Type *getPointerElementType() const { return getSequentialElementType(); }
    364 
    365   /// \brief Get the address space of this pointer or pointer vector type.
    366   unsigned getPointerAddressSpace() const;
    367 
    368   //===--------------------------------------------------------------------===//
    369   // Static members exported by the Type class itself.  Useful for getting
    370   // instances of Type.
    371   //
    372 
    373   /// getPrimitiveType - Return a type based on an identifier.
    374   static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
    375 
    376   //===--------------------------------------------------------------------===//
    377   // These are the builtin types that are always available.
    378   //
    379   static Type *getVoidTy(LLVMContext &C);
    380   static Type *getLabelTy(LLVMContext &C);
    381   static Type *getHalfTy(LLVMContext &C);
    382   static Type *getFloatTy(LLVMContext &C);
    383   static Type *getDoubleTy(LLVMContext &C);
    384   static Type *getMetadataTy(LLVMContext &C);
    385   static Type *getX86_FP80Ty(LLVMContext &C);
    386   static Type *getFP128Ty(LLVMContext &C);
    387   static Type *getPPC_FP128Ty(LLVMContext &C);
    388   static Type *getX86_MMXTy(LLVMContext &C);
    389   static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
    390   static IntegerType *getInt1Ty(LLVMContext &C);
    391   static IntegerType *getInt8Ty(LLVMContext &C);
    392   static IntegerType *getInt16Ty(LLVMContext &C);
    393   static IntegerType *getInt32Ty(LLVMContext &C);
    394   static IntegerType *getInt64Ty(LLVMContext &C);
    395 
    396   //===--------------------------------------------------------------------===//
    397   // Convenience methods for getting pointer types with one of the above builtin
    398   // types as pointee.
    399   //
    400   static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
    401   static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
    402   static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
    403   static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
    404   static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
    405   static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
    406   static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
    407   static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
    408   static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
    409   static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
    410   static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
    411   static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
    412   static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
    413 
    414   /// getPointerTo - Return a pointer to the current type.  This is equivalent
    415   /// to PointerType::get(Foo, AddrSpace).
    416   PointerType *getPointerTo(unsigned AddrSpace = 0);
    417 
    418 private:
    419   /// isSizedDerivedType - Derived types like structures and arrays are sized
    420   /// iff all of the members of the type are sized as well.  Since asking for
    421   /// their size is relatively uncommon, move this operation out of line.
    422   bool isSizedDerivedType() const;
    423 };
    424 
    425 // Printing of types.
    426 static inline raw_ostream &operator<<(raw_ostream &OS, Type &T) {
    427   T.print(OS);
    428   return OS;
    429 }
    430 
    431 // allow isa<PointerType>(x) to work without DerivedTypes.h included.
    432 template <> struct isa_impl<PointerType, Type> {
    433   static inline bool doit(const Type &Ty) {
    434     return Ty.getTypeID() == Type::PointerTyID;
    435   }
    436 };
    437 
    438 
    439 //===----------------------------------------------------------------------===//
    440 // Provide specializations of GraphTraits to be able to treat a type as a
    441 // graph of sub types.
    442 
    443 
    444 template <> struct GraphTraits<Type*> {
    445   typedef Type NodeType;
    446   typedef Type::subtype_iterator ChildIteratorType;
    447 
    448   static inline NodeType *getEntryNode(Type *T) { return T; }
    449   static inline ChildIteratorType child_begin(NodeType *N) {
    450     return N->subtype_begin();
    451   }
    452   static inline ChildIteratorType child_end(NodeType *N) {
    453     return N->subtype_end();
    454   }
    455 };
    456 
    457 template <> struct GraphTraits<const Type*> {
    458   typedef const Type NodeType;
    459   typedef Type::subtype_iterator ChildIteratorType;
    460 
    461   static inline NodeType *getEntryNode(NodeType *T) { return T; }
    462   static inline ChildIteratorType child_begin(NodeType *N) {
    463     return N->subtype_begin();
    464   }
    465   static inline ChildIteratorType child_end(NodeType *N) {
    466     return N->subtype_end();
    467   }
    468 };
    469 
    470 } // End llvm namespace
    471 
    472 #endif
    473