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   /// Extended Value Type. Capable of holding value types which are not native
     29   /// for any processor (such as the i12345 type), as well as the types an MVT
     30   /// can represent.
     31   struct EVT {
     32   private:
     33     MVT V;
     34     Type *LLVMTy;
     35 
     36   public:
     37     constexpr EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
     38     constexpr EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
     39     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     /// Returns the EVT that represents a floating-point type with the given
     53     /// number of bits. There are two floating-point types with 128 bits - this
     54     /// returns f128 rather than ppcf128.
     55     static EVT getFloatingPointVT(unsigned BitWidth) {
     56       return MVT::getFloatingPointVT(BitWidth);
     57     }
     58 
     59     /// Returns the EVT that represents an integer with the given number of
     60     /// 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     /// Returns the EVT that represents a vector NumElements in length, where
     69     /// 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     /// Return a vector with the same number of elements as this vector, but
     78     /// with the element type converted to an integer type with the same
     79     /// 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     /// Test if the given EVT is simple (as opposed to being extended).
    106     bool isSimple() const {
    107       return V.SimpleTy >= 0;
    108     }
    109 
    110     /// Test if the given EVT is extended (as opposed to being simple).
    111     bool isExtended() const {
    112       return !isSimple();
    113     }
    114 
    115     /// Return true if this is a FP or a vector FP type.
    116     bool isFloatingPoint() const {
    117       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
    118     }
    119 
    120     /// Return true if this is an integer or a vector integer type.
    121     bool isInteger() const {
    122       return isSimple() ? V.isInteger() : isExtendedInteger();
    123     }
    124 
    125     /// Return true if this is an integer, but not a vector.
    126     bool isScalarInteger() const {
    127       return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
    128     }
    129 
    130     /// Return true if this is a vector value type.
    131     bool isVector() const {
    132       return isSimple() ? V.isVector() : isExtendedVector();
    133     }
    134 
    135     /// Return true if this is a 16-bit vector type.
    136     bool is16BitVector() const {
    137       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
    138     }
    139 
    140     /// Return true if this is a 32-bit vector type.
    141     bool is32BitVector() const {
    142       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
    143     }
    144 
    145     /// Return true if this is a 64-bit vector type.
    146     bool is64BitVector() const {
    147       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
    148     }
    149 
    150     /// Return true if this is a 128-bit vector type.
    151     bool is128BitVector() const {
    152       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
    153     }
    154 
    155     /// Return true if this is a 256-bit vector type.
    156     bool is256BitVector() const {
    157       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
    158     }
    159 
    160     /// Return true if this is a 512-bit vector type.
    161     bool is512BitVector() const {
    162       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
    163     }
    164 
    165     /// Return true if this is a 1024-bit vector type.
    166     bool is1024BitVector() const {
    167       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
    168     }
    169 
    170     /// Return true if this is a 2048-bit vector type.
    171     bool is2048BitVector() const {
    172       return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
    173     }
    174 
    175     /// Return true if this is an overloaded type for TableGen.
    176     bool isOverloaded() const {
    177       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
    178     }
    179 
    180     /// Return true if the bit size is a multiple of 8.
    181     bool isByteSized() const {
    182       return (getSizeInBits() & 7) == 0;
    183     }
    184 
    185     /// Return true if the size is a power-of-two number of bytes.
    186     bool isRound() const {
    187       unsigned BitSize = getSizeInBits();
    188       return BitSize >= 8 && !(BitSize & (BitSize - 1));
    189     }
    190 
    191     /// Return true if this has the same number of bits as VT.
    192     bool bitsEq(EVT VT) const {
    193       if (EVT::operator==(VT)) return true;
    194       return getSizeInBits() == VT.getSizeInBits();
    195     }
    196 
    197     /// Return true if this has more bits than VT.
    198     bool bitsGT(EVT VT) const {
    199       if (EVT::operator==(VT)) return false;
    200       return getSizeInBits() > VT.getSizeInBits();
    201     }
    202 
    203     /// Return true if this has no less bits than VT.
    204     bool bitsGE(EVT VT) const {
    205       if (EVT::operator==(VT)) return true;
    206       return getSizeInBits() >= VT.getSizeInBits();
    207     }
    208 
    209     /// Return true if this has less bits than VT.
    210     bool bitsLT(EVT VT) const {
    211       if (EVT::operator==(VT)) return false;
    212       return getSizeInBits() < VT.getSizeInBits();
    213     }
    214 
    215     /// Return true if this has no more bits than VT.
    216     bool bitsLE(EVT VT) const {
    217       if (EVT::operator==(VT)) return true;
    218       return getSizeInBits() <= VT.getSizeInBits();
    219     }
    220 
    221 
    222     /// Return the SimpleValueType held in the specified simple EVT.
    223     MVT getSimpleVT() const {
    224       assert(isSimple() && "Expected a SimpleValueType!");
    225       return V;
    226     }
    227 
    228     /// If this is a vector type, return the element type, otherwise return
    229     /// this.
    230     EVT getScalarType() const {
    231       return isVector() ? getVectorElementType() : *this;
    232     }
    233 
    234     /// Given a vector type, return the type of each element.
    235     EVT getVectorElementType() const {
    236       assert(isVector() && "Invalid vector type!");
    237       if (isSimple())
    238         return V.getVectorElementType();
    239       return getExtendedVectorElementType();
    240     }
    241 
    242     /// Given a vector type, return the number of 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     /// 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     /// Return the number of bytes overwritten by a store of the specified value
    262     /// type.
    263     unsigned getStoreSize() const {
    264       return (getSizeInBits() + 7) / 8;
    265     }
    266 
    267     /// Return the number of bits overwritten by a store of the specified value
    268     /// type.
    269     unsigned getStoreSizeInBits() const {
    270       return getStoreSize() * 8;
    271     }
    272 
    273     /// Rounds the bit-width of the given integer EVT up to the nearest power of
    274     /// two (and at least to eight), and returns the integer EVT with that
    275     /// 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     /// Finds the smallest simple value type that is greater than or equal to
    285     /// half the width of this EVT. If no simple value type can be found, an
    286     /// extended integer value type of half the size (rounded up) is returned.
    287     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
    288       assert(isInteger() && !isVector() && "Invalid integer type!");
    289       unsigned EVTSize = getSizeInBits();
    290       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
    291           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
    292         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
    293         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
    294           return HalfVT;
    295       }
    296       return getIntegerVT(Context, (EVTSize + 1) / 2);
    297     }
    298 
    299     /// Return a VT for an integer vector type with the size of the
    300     /// elements doubled. The typed returned may be an extended type.
    301     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
    302       EVT EltVT = getVectorElementType();
    303       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
    304       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
    305     }
    306 
    307     /// Returns true if the given vector is a power of 2.
    308     bool isPow2VectorType() const {
    309       unsigned NElts = getVectorNumElements();
    310       return !(NElts & (NElts - 1));
    311     }
    312 
    313     /// Widens the length of the given vector EVT up to the nearest power of 2
    314     /// and returns that type.
    315     EVT getPow2VectorType(LLVMContext &Context) const {
    316       if (!isPow2VectorType()) {
    317         unsigned NElts = getVectorNumElements();
    318         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
    319         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
    320       }
    321       else {
    322         return *this;
    323       }
    324     }
    325 
    326     /// This function returns value type as a string, e.g. "i32".
    327     std::string getEVTString() const;
    328 
    329     /// This method returns an LLVM type corresponding to the specified EVT.
    330     /// For integer types, this returns an unsigned type. Note that this will
    331     /// abort for types that cannot be represented.
    332     Type *getTypeForEVT(LLVMContext &Context) const;
    333 
    334     /// Return the value type corresponding to the specified type.
    335     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
    336     /// types are returned as Other, otherwise they are invalid.
    337     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
    338 
    339     intptr_t getRawBits() const {
    340       if (isSimple())
    341         return V.SimpleTy;
    342       else
    343         return (intptr_t)(LLVMTy);
    344     }
    345 
    346     /// A meaningless but well-behaved order, useful for constructing
    347     /// containers.
    348     struct compareRawBits {
    349       bool operator()(EVT L, EVT R) const {
    350         if (L.V.SimpleTy == R.V.SimpleTy)
    351           return L.LLVMTy < R.LLVMTy;
    352         else
    353           return L.V.SimpleTy < R.V.SimpleTy;
    354       }
    355     };
    356 
    357   private:
    358     // Methods for handling the Extended-type case in functions above.
    359     // These are all out-of-line to prevent users of this header file
    360     // from having a dependency on Type.h.
    361     EVT changeExtendedTypeToInteger() const;
    362     EVT changeExtendedVectorElementTypeToInteger() const;
    363     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
    364     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
    365                                    unsigned NumElements);
    366     bool isExtendedFloatingPoint() const LLVM_READONLY;
    367     bool isExtendedInteger() const LLVM_READONLY;
    368     bool isExtendedScalarInteger() const LLVM_READONLY;
    369     bool isExtendedVector() const LLVM_READONLY;
    370     bool isExtended16BitVector() const LLVM_READONLY;
    371     bool isExtended32BitVector() const LLVM_READONLY;
    372     bool isExtended64BitVector() const LLVM_READONLY;
    373     bool isExtended128BitVector() const LLVM_READONLY;
    374     bool isExtended256BitVector() const LLVM_READONLY;
    375     bool isExtended512BitVector() const LLVM_READONLY;
    376     bool isExtended1024BitVector() const LLVM_READONLY;
    377     bool isExtended2048BitVector() const LLVM_READONLY;
    378     EVT getExtendedVectorElementType() const;
    379     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
    380     unsigned getExtendedSizeInBits() const LLVM_READONLY;
    381   };
    382 
    383 } // End llvm namespace
    384 
    385 #endif
    386