LLVM API Documentation

ValueTypes.h

Go to the documentation of this file.
00001 //===- CodeGen/ValueTypes.h - Low-Level Target independ. 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 defines the set of low-level target independent types which various
00011 // values in the code generator are.  This allows the target specific behavior
00012 // of instructions to be described to target independent passes.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_VALUETYPES_H
00017 #define LLVM_CODEGEN_VALUETYPES_H
00018 
00019 #include <cassert>
00020 #include <string>
00021 #include "llvm/Support/DataTypes.h"
00022 #include "llvm/Support/MathExtras.h"
00023 
00024 namespace llvm {
00025   class Type;
00026 
00027   struct MVT { // MVT = Machine Value Type
00028   public:
00029     enum SimpleValueType {
00030       // If you change this numbering, you must change the values in
00031       // ValueTypes.td as well!
00032       Other          =   0,   // This is a non-standard value
00033       i1             =   1,   // This is a 1 bit integer value
00034       i8             =   2,   // This is an 8 bit integer value
00035       i16            =   3,   // This is a 16 bit integer value
00036       i32            =   4,   // This is a 32 bit integer value
00037       i64            =   5,   // This is a 64 bit integer value
00038       i128           =   6,   // This is a 128 bit integer value
00039 
00040       FIRST_INTEGER_VALUETYPE = i1,
00041       LAST_INTEGER_VALUETYPE  = i128,
00042 
00043       f32            =   7,   // This is a 32 bit floating point value
00044       f64            =   8,   // This is a 64 bit floating point value
00045       f80            =   9,   // This is a 80 bit floating point value
00046       f128           =  10,   // This is a 128 bit floating point value
00047       ppcf128        =  11,   // This is a PPC 128-bit floating point value
00048       Flag           =  12,   // This is a condition code or machine flag.
00049 
00050       isVoid         =  13,   // This has no value
00051 
00052       v2i8           =  14,   //  2 x i8
00053       v4i8           =  15,   //  4 x i8
00054       v2i16          =  16,   //  2 x i16
00055       v8i8           =  17,   //  8 x i8
00056       v4i16          =  18,   //  4 x i16
00057       v2i32          =  19,   //  2 x i32
00058       v1i64          =  20,   //  1 x i64
00059       v16i8          =  21,   // 16 x i8
00060       v8i16          =  22,   //  8 x i16
00061       v3i32          =  23,   //  3 x i32
00062       v4i32          =  24,   //  4 x i32
00063       v2i64          =  25,   //  2 x i64
00064 
00065       v2f32          =  26,   //  2 x f32
00066       v3f32          =  27,   //  3 x f32
00067       v4f32          =  28,   //  4 x f32
00068       v2f64          =  29,   //  2 x f64
00069 
00070       FIRST_VECTOR_VALUETYPE = v2i8,
00071       LAST_VECTOR_VALUETYPE  = v2f64,
00072 
00073       LAST_VALUETYPE =  30,   // This always remains at the end of the list.
00074 
00075       // iPTRAny - An int value the size of the pointer of the current
00076       // target to any address space. This must only be used internal to
00077       // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
00078       iPTRAny        =  252,
00079 
00080       // fAny - Any floating-point or vector floating-point value. This is used
00081       // for intrinsics that have overloadings based on floating-point types.
00082       // This is only for tblgen's consumption!
00083       fAny           =  253,
00084 
00085       // iAny - An integer or vector integer value of any bit width. This is
00086       // used for intrinsics that have overloadings based on integer bit widths.
00087       // This is only for tblgen's consumption!
00088       iAny           =  254,
00089 
00090       // iPTR - An int value the size of the pointer of the current
00091       // target.  This should only be used internal to tblgen!
00092       iPTR           =  255,
00093 
00094       // LastSimpleValueType - The greatest valid SimpleValueType value.
00095       LastSimpleValueType = 255
00096     };
00097 
00098   private:
00099     /// This union holds low-level value types. Valid values include any of
00100     /// the values in the SimpleValueType enum, or any value returned from one
00101     /// of the MVT methods.  Any value type equal to one of the SimpleValueType
00102     /// enum values is a "simple" value type.  All others are "extended".
00103     ///
00104     /// Note that simple doesn't necessary mean legal for the target machine.
00105     /// All legal value types must be simple, but often there are some simple
00106     /// value types that are not legal.
00107     ///
00108     union {
00109       uintptr_t V;
00110       SimpleValueType SimpleTy;
00111       const Type *LLVMTy;
00112     };
00113 
00114   public:
00115     MVT() {}
00116     MVT(SimpleValueType S) : V(S) {}
00117 
00118     bool operator==(const MVT VT) const {
00119       return getRawBits() == VT.getRawBits();
00120     }
00121     bool operator!=(const MVT VT) const {
00122       return getRawBits() != VT.getRawBits();
00123     }
00124 
00125     /// getFloatingPointVT - Returns the MVT that represents a floating point
00126     /// type with the given number of bits.  There are two floating point types
00127     /// with 128 bits - this returns f128 rather than ppcf128.
00128     static MVT getFloatingPointVT(unsigned BitWidth) {
00129       switch (BitWidth) {
00130       default:
00131         assert(false && "Bad bit width!");
00132       case 32:
00133         return f32;
00134       case 64:
00135         return f64;
00136       case 80:
00137         return f80;
00138       case 128:
00139         return f128;
00140       }
00141     }
00142 
00143     /// getIntegerVT - Returns the MVT that represents an integer with the given
00144     /// number of bits.
00145     static MVT getIntegerVT(unsigned BitWidth) {
00146       switch (BitWidth) {
00147       default:
00148         break;
00149       case 1:
00150         return i1;
00151       case 8:
00152         return i8;
00153       case 16:
00154         return i16;
00155       case 32:
00156         return i32;
00157       case 64:
00158         return i64;
00159       case 128:
00160         return i128;
00161       }
00162       return getExtendedIntegerVT(BitWidth);
00163     }
00164 
00165     /// getVectorVT - Returns the MVT that represents a vector NumElements in
00166     /// length, where each element is of type VT.
00167     static MVT getVectorVT(MVT VT, unsigned NumElements) {
00168       switch (VT.V) {
00169       default:
00170         break;
00171       case i8:
00172         if (NumElements == 2)  return v2i8;
00173         if (NumElements == 4)  return v4i8;
00174         if (NumElements == 8)  return v8i8;
00175         if (NumElements == 16) return v16i8;
00176         break;
00177       case i16:
00178         if (NumElements == 2)  return v2i16;
00179         if (NumElements == 4)  return v4i16;
00180         if (NumElements == 8)  return v8i16;
00181         break;
00182       case i32:
00183         if (NumElements == 2)  return v2i32;
00184         if (NumElements == 3)  return v3i32;
00185         if (NumElements == 4)  return v4i32;
00186         break;
00187       case i64:
00188         if (NumElements == 1)  return v1i64;
00189         if (NumElements == 2)  return v2i64;
00190         break;
00191       case f32:
00192         if (NumElements == 2)  return v2f32;
00193         if (NumElements == 3)  return v3f32;
00194         if (NumElements == 4)  return v4f32;
00195         break;
00196       case f64:
00197         if (NumElements == 2)  return v2f64;
00198         break;
00199       }
00200       return getExtendedVectorVT(VT, NumElements);
00201     }
00202 
00203     /// getIntVectorWithNumElements - Return any integer vector type that has
00204     /// the specified number of elements.
00205     static MVT getIntVectorWithNumElements(unsigned NumElts) {
00206       switch (NumElts) {
00207       default: return getVectorVT(i8, NumElts);
00208       case  1: return v1i64;
00209       case  2: return v2i32;
00210       case  3: return v3i32;
00211       case  4: return v4i16;
00212       case  8: return v8i8;
00213       case 16: return v16i8;
00214       }
00215     }
00216 
00217     /// isSimple - Test if the given MVT is simple (as opposed to being
00218     /// extended).
00219     bool isSimple() const {
00220       return V <= LastSimpleValueType;
00221     }
00222 
00223     /// isExtended - Test if the given MVT is extended (as opposed to
00224     /// being simple).
00225     bool isExtended() const {
00226       return !isSimple();
00227     }
00228 
00229     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
00230     bool isFloatingPoint() const {
00231       return isSimple() ?
00232              ((SimpleTy >= f32 && SimpleTy <= ppcf128) ||
00233               (SimpleTy >= v2f32 && SimpleTy <= v2f64)) :
00234              isExtendedFloatingPoint();
00235     }
00236 
00237     /// isInteger - Return true if this is an integer, or a vector integer type.
00238     bool isInteger() const {
00239       return isSimple() ?
00240              ((SimpleTy >= FIRST_INTEGER_VALUETYPE &&
00241                SimpleTy <= LAST_INTEGER_VALUETYPE) ||
00242               (SimpleTy >= v2i8 && SimpleTy <= v2i64)) :
00243              isExtendedInteger();
00244     }
00245 
00246     /// isVector - Return true if this is a vector value type.
00247     bool isVector() const {
00248       return isSimple() ?
00249              (SimpleTy >= FIRST_VECTOR_VALUETYPE &&
00250               SimpleTy <= LAST_VECTOR_VALUETYPE) :
00251              isExtendedVector();
00252     }
00253 
00254     /// is64BitVector - Return true if this is a 64-bit vector type.
00255     bool is64BitVector() const {
00256       return isSimple() ?
00257              (SimpleTy==v8i8 || SimpleTy==v4i16 || SimpleTy==v2i32 ||
00258               SimpleTy==v1i64 || SimpleTy==v2f32) :
00259              isExtended64BitVector();
00260     }
00261 
00262     /// is128BitVector - Return true if this is a 128-bit vector type.
00263     bool is128BitVector() const {
00264       return isSimple() ?
00265              (SimpleTy==v16i8 || SimpleTy==v8i16 || SimpleTy==v4i32 ||
00266               SimpleTy==v2i64 || SimpleTy==v4f32 || SimpleTy==v2f64) :
00267              isExtended128BitVector();
00268     }
00269 
00270     /// isByteSized - Return true if the bit size is a multiple of 8.
00271     bool isByteSized() const {
00272       return (getSizeInBits() & 7) == 0;
00273     }
00274 
00275     /// isRound - Return true if the size is a power-of-two number of bytes.
00276     bool isRound() const {
00277       unsigned BitSize = getSizeInBits();
00278       return BitSize >= 8 && !(BitSize & (BitSize - 1));
00279     }
00280 
00281     /// bitsEq - Return true if this has the same number of bits as VT.
00282     bool bitsEq(MVT VT) const {
00283       return getSizeInBits() == VT.getSizeInBits();
00284     }
00285 
00286     /// bitsGT - Return true if this has more bits than VT.
00287     bool bitsGT(MVT VT) const {
00288       return getSizeInBits() > VT.getSizeInBits();
00289     }
00290 
00291     /// bitsGE - Return true if this has no less bits than VT.
00292     bool bitsGE(MVT VT) const {
00293       return getSizeInBits() >= VT.getSizeInBits();
00294     }
00295 
00296     /// bitsLT - Return true if this has less bits than VT.
00297     bool bitsLT(MVT VT) const {
00298       return getSizeInBits() < VT.getSizeInBits();
00299     }
00300 
00301     /// bitsLE - Return true if this has no more bits than VT.
00302     bool bitsLE(MVT VT) const {
00303       return getSizeInBits() <= VT.getSizeInBits();
00304     }
00305 
00306 
00307     /// getSimpleVT - Return the SimpleValueType held in the specified
00308     /// simple MVT.
00309     SimpleValueType getSimpleVT() const {
00310       assert(isSimple() && "Expected a SimpleValueType!");
00311       return SimpleTy;
00312     }
00313 
00314     /// getVectorElementType - Given a vector type, return the type of
00315     /// each element.
00316     MVT getVectorElementType() const {
00317       assert(isVector() && "Invalid vector type!");
00318       switch (V) {
00319       default:
00320         return getExtendedVectorElementType();
00321       case v2i8 :
00322       case v4i8 :
00323       case v8i8 :
00324       case v16i8: return i8;
00325       case v2i16:
00326       case v4i16:
00327       case v8i16: return i16;
00328       case v2i32:
00329       case v3i32:
00330       case v4i32: return i32;
00331       case v1i64:
00332       case v2i64: return i64;
00333       case v2f32:
00334       case v3f32:
00335       case v4f32: return f32;
00336       case v2f64: return f64;
00337       }
00338     }
00339 
00340     /// getVectorNumElements - Given a vector type, return the number of
00341     /// elements it contains.
00342     unsigned getVectorNumElements() const {
00343       assert(isVector() && "Invalid vector type!");
00344       switch (V) {
00345       default:
00346         return getExtendedVectorNumElements();
00347       case v16i8: return 16;
00348       case v8i8 :
00349       case v8i16: return 8;
00350       case v4i8:
00351       case v4i16:
00352       case v4i32:
00353       case v4f32: return 4;
00354       case v3i32:
00355       case v3f32: return 3;
00356       case v2i8:
00357       case v2i16:
00358       case v2i32:
00359       case v2i64:
00360       case v2f32:
00361       case v2f64: return 2;
00362       case v1i64: return 1;
00363       }
00364     }
00365 
00366     /// getSizeInBits - Return the size of the specified value type in bits.
00367     unsigned getSizeInBits() const {
00368       switch (V) {
00369       case iPTR:
00370         assert(0 && "Value type size is target-dependent. Ask TLI.");
00371       case iPTRAny:
00372       case iAny:
00373       case fAny:
00374         assert(0 && "Value type is overloaded.");
00375       default:
00376         return getExtendedSizeInBits();
00377       case i1  :  return 1;
00378       case i8  :  return 8;
00379       case i16 :
00380       case v2i8:  return 16;
00381       case f32 :
00382       case i32 :
00383       case v4i8:
00384       case v2i16: return 32;
00385       case f64 :
00386       case i64 :
00387       case v8i8:
00388       case v4i16:
00389       case v2i32:
00390       case v1i64:
00391       case v2f32: return 64;
00392       case f80 :  return 80;
00393       case v3i32:
00394       case v3f32: return 96;
00395       case f128:
00396       case ppcf128:
00397       case i128:
00398       case v16i8:
00399       case v8i16:
00400       case v4i32:
00401       case v2i64:
00402       case v4f32:
00403       case v2f64: return 128;
00404       }
00405     }
00406 
00407     /// getStoreSizeInBits - Return the number of bits overwritten by a store
00408     /// of the specified value type.
00409     unsigned getStoreSizeInBits() const {
00410       return (getSizeInBits() + 7)/8*8;
00411     }
00412 
00413     /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
00414     /// to the nearest power of two (and at least to eight), and returns the
00415     /// integer MVT with that number of bits.
00416     MVT getRoundIntegerType() const {
00417       assert(isInteger() && !isVector() && "Invalid integer type!");
00418       unsigned BitWidth = getSizeInBits();
00419       if (BitWidth <= 8)
00420         return i8;
00421       else
00422         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
00423     }
00424 
00425     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
00426     bool isPow2VectorType() const {
00427       unsigned NElts = getVectorNumElements();
00428       return !(NElts & (NElts - 1));
00429     }
00430 
00431     /// getPow2VectorType - Widens the length of the given vector MVT up to
00432     /// the nearest power of 2 and returns that type.
00433     MVT getPow2VectorType() const {
00434       if (!isPow2VectorType()) {
00435         unsigned NElts = getVectorNumElements();
00436         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
00437         return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
00438       }
00439       else {
00440         return *this;
00441       }
00442     }
00443 
00444     /// getMVTString - This function returns value type as a string,
00445     /// e.g. "i32".
00446     std::string getMVTString() const;
00447 
00448     /// getTypeForMVT - This method returns an LLVM type corresponding to the
00449     /// specified MVT.  For integer types, this returns an unsigned type.  Note
00450     /// that this will abort for types that cannot be represented.
00451     const Type *getTypeForMVT() const;
00452 
00453     /// getMVT - Return the value type corresponding to the specified type.
00454     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
00455     /// types are returned as Other, otherwise they are invalid.
00456     static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
00457 
00458     /// getRawBits - Represent the type as a bunch of bits.
00459     uintptr_t getRawBits() const { return V; }
00460 
00461     /// compareRawBits - A meaningless but well-behaved order, useful for
00462     /// constructing containers.
00463     struct compareRawBits {
00464       bool operator()(MVT L, MVT R) const {
00465         return L.getRawBits() < R.getRawBits();
00466       }
00467     };
00468 
00469   private:
00470     // Methods for handling the Extended-type case in functions above.
00471     // These are all out-of-line to prevent users of this header file
00472     // from having a dependency on Type.h.
00473     static MVT getExtendedIntegerVT(unsigned BitWidth);
00474     static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
00475     bool isExtendedFloatingPoint() const;
00476     bool isExtendedInteger() const;
00477     bool isExtendedVector() const;
00478     bool isExtended64BitVector() const;
00479     bool isExtended128BitVector() const;
00480     MVT getExtendedVectorElementType() const;
00481     unsigned getExtendedVectorNumElements() const;
00482     unsigned getExtendedSizeInBits() const;
00483   };
00484 
00485 } // End llvm namespace
00486 
00487 #endif