Home | History | Annotate | Download | only in CodeGen
      1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. 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 defines the set of low-level target independent types which various
     11 // values in the code generator are.  This allows the target specific behavior
     12 // of instructions to be described to target independent passes.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CODEGEN_VALUETYPES_H
     17 #define LLVM_CODEGEN_VALUETYPES_H
     18 
     19 #include "llvm/CodeGen/MachineValueType.h"
     20 #include <cassert>
     21 #include <string>
     22 
     23 namespace llvm {
     24 
     25   class LLVMContext;
     26   class Type;
     27 
     28   /// EVT - Extended Value Type.  Capable of holding value types which are not
     29   /// native for any processor (such as the i12345 type), as well as the types
     30   /// a MVT can represent.
     31   struct EVT {
     32   private:
     33     MVT V;
     34     Type *LLVMTy;
     35 
     36   public:
     37     LLVM_CONSTEXPR EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
     38     LLVM_CONSTEXPR EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
     39     LLVM_CONSTEXPR EVT(MVT S) : V(S), LLVMTy(nullptr) {}
     40 
     41     bool operator==(EVT VT) const {
     42       return !(*this != VT);
     43     }
     44     bool operator!=(EVT VT) const {
     45       if (V.SimpleTy != VT.V.SimpleTy)
     46         return true;
     47       if (V.SimpleTy < 0)
     48         return LLVMTy != VT.LLVMTy;
     49       return false;
     50     }
     51 
     52     /// getFloatingPointVT - Returns the EVT that represents a floating point
     53     /// type with the given number of bits.  There are two floating point types
     54     /// with 128 bits - this returns f128 rather than ppcf128.
     55     static EVT getFloatingPointVT(unsigned BitWidth) {
     56       return MVT::getFloatingPointVT(BitWidth);
     57     }
     58 
     59     /// getIntegerVT - Returns the EVT that represents an integer with the given
     60     /// number of bits.
     61     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
     62       MVT M = MVT::getIntegerVT(BitWidth);
     63       if (M.SimpleTy >= 0)
     64         return M;
     65       return getExtendedIntegerVT(Context, BitWidth);
     66     }
     67 
     68     /// getVectorVT - Returns the EVT that represents a vector NumElements in
     69     /// length, where each element is of type VT.
     70     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
     71       MVT M = MVT::getVectorVT(VT.V, NumElements);
     72       if (M.SimpleTy >= 0)
     73         return M;
     74       return getExtendedVectorVT(Context, VT, NumElements);
     75     }
     76 
     77     /// changeVectorElementTypeToInteger - Return a vector with the same number
     78     /// of elements as this vector, but with the element type converted to an
     79     /// integer type with the same bitwidth.
     80     EVT changeVectorElementTypeToInteger() const {
     81       if (!isSimple())
     82         return changeExtendedVectorElementTypeToInteger();
     83       MVT EltTy = getSimpleVT().getVectorElementType();
     84       unsigned BitWidth = EltTy.getSizeInBits();
     85       MVT IntTy = MVT::getIntegerVT(BitWidth);
     86       MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
     87       assert(VecTy.SimpleTy >= 0 &&
     88              "Simple vector VT not representable by simple integer vector VT!");
     89       return VecTy;
     90     }
     91 
     92     /// Return the type converted to an equivalently sized integer or vector
     93     /// with integer element type. Similar to changeVectorElementTypeToInteger,
     94     /// but also handles scalars.
     95     EVT changeTypeToInteger() {
     96       if (isVector())
     97         return changeVectorElementTypeToInteger();
     98 
     99       if (isSimple())
    100         return MVT::getIntegerVT(getSizeInBits());
    101 
    102       return changeExtendedTypeToInteger();
    103     }
    104 
    105     /// isSimple - Test if the given EVT is simple (as opposed to being
    106     /// extended).
    107     bool isSimple() const {
    108       return V.SimpleTy >= 0;
    109     }
    110 
    111     /// isExtended - Test if the given EVT is extended (as opposed to
    112     /// being simple).
    113     bool isExtended() const {
    114       return !isSimple();
    115     }
    116 
    117     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
    118     bool isFloatingPoint() const {
    119       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
    120     }
    121 
    122     /// isInteger - Return true if this is an integer, or a vector integer type.
    123     bool isInteger() const {
    124       return isSimple() ? V.isInteger() : isExtendedInteger();
    125     }
    126 
    127     /// isVector - Return true if this is a vector value type.
    128     bool isVector() const {
    129       return isSimple() ? V.isVector() : isExtendedVector();
    130     }
    131 
    132     /// is16BitVector - Return true if this is a 16-bit vector type.
    133     bool is16BitVector() const {
    134       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
    135     }
    136 
    137     /// is32BitVector - Return true if this is a 32-bit vector type.
    138     bool is32BitVector() const {
    139       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
    140     }
    141 
    142     /// is64BitVector - Return true if this is a 64-bit vector type.
    143     bool is64BitVector() const {
    144       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
    145     }
    146 
    147     /// is128BitVector - Return true if this is a 128-bit vector type.
    148     bool is128BitVector() const {
    149       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
    150     }
    151 
    152     /// is256BitVector - Return true if this is a 256-bit vector type.
    153     bool is256BitVector() const {
    154       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
    155     }
    156 
    157     /// is512BitVector - Return true if this is a 512-bit vector type.
    158     bool is512BitVector() const {
    159       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
    160     }
    161 
    162     /// is1024BitVector - Return true if this is a 1024-bit vector type.
    163     bool is1024BitVector() const {
    164       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
    165     }
    166 
    167     /// is2048BitVector - Return true if this is a 2048-bit vector type.
    168     bool is2048BitVector() const {
    169       return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
    170     }
    171 
    172     /// isOverloaded - Return true if this is an overloaded type for TableGen.
    173     bool isOverloaded() const {
    174       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
    175     }
    176 
    177     /// isByteSized - Return true if the bit size is a multiple of 8.
    178     bool isByteSized() const {
    179       return (getSizeInBits() & 7) == 0;
    180     }
    181 
    182     /// isRound - Return true if the size is a power-of-two number of bytes.
    183     bool isRound() const {
    184       unsigned BitSize = getSizeInBits();
    185       return BitSize >= 8 && !(BitSize & (BitSize - 1));
    186     }
    187 
    188     /// bitsEq - Return true if this has the same number of bits as VT.
    189     bool bitsEq(EVT VT) const {
    190       if (EVT::operator==(VT)) return true;
    191       return getSizeInBits() == VT.getSizeInBits();
    192     }
    193 
    194     /// bitsGT - Return true if this has more bits than VT.
    195     bool bitsGT(EVT VT) const {
    196       if (EVT::operator==(VT)) return false;
    197       return getSizeInBits() > VT.getSizeInBits();
    198     }
    199 
    200     /// bitsGE - Return true if this has no less bits than VT.
    201     bool bitsGE(EVT VT) const {
    202       if (EVT::operator==(VT)) return true;
    203       return getSizeInBits() >= VT.getSizeInBits();
    204     }
    205 
    206     /// bitsLT - Return true if this has less bits than VT.
    207     bool bitsLT(EVT VT) const {
    208       if (EVT::operator==(VT)) return false;
    209       return getSizeInBits() < VT.getSizeInBits();
    210     }
    211 
    212     /// bitsLE - Return true if this has no more bits than VT.
    213     bool bitsLE(EVT VT) const {
    214       if (EVT::operator==(VT)) return true;
    215       return getSizeInBits() <= VT.getSizeInBits();
    216     }
    217 
    218 
    219     /// getSimpleVT - Return the SimpleValueType held in the specified
    220     /// simple EVT.
    221     MVT getSimpleVT() const {
    222       assert(isSimple() && "Expected a SimpleValueType!");
    223       return V;
    224     }
    225 
    226     /// getScalarType - If this is a vector type, return the element type,
    227     /// otherwise return this.
    228     EVT getScalarType() const {
    229       return isVector() ? getVectorElementType() : *this;
    230     }
    231 
    232     /// getVectorElementType - Given a vector type, return the type of
    233     /// each element.
    234     EVT getVectorElementType() const {
    235       assert(isVector() && "Invalid vector type!");
    236       if (isSimple())
    237         return V.getVectorElementType();
    238       return getExtendedVectorElementType();
    239     }
    240 
    241     /// getVectorNumElements - Given a vector type, return the number of
    242     /// elements it contains.
    243     unsigned getVectorNumElements() const {
    244       assert(isVector() && "Invalid vector type!");
    245       if (isSimple())
    246         return V.getVectorNumElements();
    247       return getExtendedVectorNumElements();
    248     }
    249 
    250     /// getSizeInBits - Return the size of the specified value type in bits.
    251     unsigned getSizeInBits() const {
    252       if (isSimple())
    253         return V.getSizeInBits();
    254       return getExtendedSizeInBits();
    255     }
    256 
    257     unsigned getScalarSizeInBits() const {
    258       return getScalarType().getSizeInBits();
    259     }
    260 
    261     /// getStoreSize - Return the number of bytes overwritten by a store
    262     /// of the specified value type.
    263     unsigned getStoreSize() const {
    264       return (getSizeInBits() + 7) / 8;
    265     }
    266 
    267     /// getStoreSizeInBits - Return the number of bits overwritten by a store
    268     /// of the specified value type.
    269     unsigned getStoreSizeInBits() const {
    270       return getStoreSize() * 8;
    271     }
    272 
    273     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
    274     /// to the nearest power of two (and at least to eight), and returns the
    275     /// integer EVT with that number of bits.
    276     EVT getRoundIntegerType(LLVMContext &Context) const {
    277       assert(isInteger() && !isVector() && "Invalid integer type!");
    278       unsigned BitWidth = getSizeInBits();
    279       if (BitWidth <= 8)
    280         return EVT(MVT::i8);
    281       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
    282     }
    283 
    284     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
    285     /// greater than or equal to half the width of this EVT. If no simple
    286     /// value type can be found, an extended integer value type of half the
    287     /// size (rounded up) is returned.
    288     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
    289       assert(isInteger() && !isVector() && "Invalid integer type!");
    290       unsigned EVTSize = getSizeInBits();
    291       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
    292           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
    293         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
    294         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
    295           return HalfVT;
    296       }
    297       return getIntegerVT(Context, (EVTSize + 1) / 2);
    298     }
    299 
    300     /// \brief Return a VT for an integer vector type with the size of the
    301     /// elements doubled. The typed returned may be an extended type.
    302     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
    303       EVT EltVT = getVectorElementType();
    304       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
    305       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
    306     }
    307 
    308     /// isPow2VectorType - Returns true if the given vector is a power of 2.
    309     bool isPow2VectorType() const {
    310       unsigned NElts = getVectorNumElements();
    311       return !(NElts & (NElts - 1));
    312     }
    313 
    314     /// getPow2VectorType - Widens the length of the given vector EVT up to
    315     /// the nearest power of 2 and returns that type.
    316     EVT getPow2VectorType(LLVMContext &Context) const {
    317       if (!isPow2VectorType()) {
    318         unsigned NElts = getVectorNumElements();
    319         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
    320         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
    321       }
    322       else {
    323         return *this;
    324       }
    325     }
    326 
    327     /// getEVTString - This function returns value type as a string,
    328     /// e.g. "i32".
    329     std::string getEVTString() const;
    330 
    331     /// getTypeForEVT - This method returns an LLVM type corresponding to the
    332     /// specified EVT.  For integer types, this returns an unsigned type.  Note
    333     /// that this will abort for types that cannot be represented.
    334     Type *getTypeForEVT(LLVMContext &Context) const;
    335 
    336     /// getEVT - Return the value type corresponding to the specified type.
    337     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
    338     /// types are returned as Other, otherwise they are invalid.
    339     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
    340 
    341     intptr_t getRawBits() const {
    342       if (isSimple())
    343         return V.SimpleTy;
    344       else
    345         return (intptr_t)(LLVMTy);
    346     }
    347 
    348     /// compareRawBits - A meaningless but well-behaved order, useful for
    349     /// constructing containers.
    350     struct compareRawBits {
    351       bool operator()(EVT L, EVT R) const {
    352         if (L.V.SimpleTy == R.V.SimpleTy)
    353           return L.LLVMTy < R.LLVMTy;
    354         else
    355           return L.V.SimpleTy < R.V.SimpleTy;
    356       }
    357     };
    358 
    359   private:
    360     // Methods for handling the Extended-type case in functions above.
    361     // These are all out-of-line to prevent users of this header file
    362     // from having a dependency on Type.h.
    363     EVT changeExtendedTypeToInteger() const;
    364     EVT changeExtendedVectorElementTypeToInteger() const;
    365     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
    366     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
    367                                    unsigned NumElements);
    368     bool isExtendedFloatingPoint() const LLVM_READONLY;
    369     bool isExtendedInteger() const LLVM_READONLY;
    370     bool isExtendedVector() const LLVM_READONLY;
    371     bool isExtended16BitVector() const LLVM_READONLY;
    372     bool isExtended32BitVector() const LLVM_READONLY;
    373     bool isExtended64BitVector() const LLVM_READONLY;
    374     bool isExtended128BitVector() const LLVM_READONLY;
    375     bool isExtended256BitVector() const LLVM_READONLY;
    376     bool isExtended512BitVector() const LLVM_READONLY;
    377     bool isExtended1024BitVector() const LLVM_READONLY;
    378     bool isExtended2048BitVector() const LLVM_READONLY;
    379     EVT getExtendedVectorElementType() const;
    380     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
    381     unsigned getExtendedSizeInBits() const;
    382   };
    383 
    384 } // End llvm namespace
    385 
    386 #endif
    387