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 == MVT::INVALID_SIMPLE_VALUE_TYPE)
     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 != MVT::INVALID_SIMPLE_VALUE_TYPE)
     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                            bool IsScalable = false) {
     72       MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
     73       if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
     74         return M;
     75 
     76       assert(!IsScalable && "We don't support extended scalable types yet");
     77       return getExtendedVectorVT(Context, VT, NumElements);
     78     }
     79 
     80     /// Returns the EVT that represents a vector EC.Min elements in length,
     81     /// where each element is of type VT.
     82     static EVT getVectorVT(LLVMContext &Context, EVT VT, MVT::ElementCount EC) {
     83       MVT M = MVT::getVectorVT(VT.V, EC);
     84       if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
     85         return M;
     86       assert (!EC.Scalable && "We don't support extended scalable types yet");
     87       return getExtendedVectorVT(Context, VT, EC.Min);
     88     }
     89 
     90     /// Return a vector with the same number of elements as this vector, but
     91     /// with the element type converted to an integer type with the same
     92     /// bitwidth.
     93     EVT changeVectorElementTypeToInteger() const {
     94       if (!isSimple()) {
     95         assert (!isScalableVector() &&
     96                 "We don't support extended scalable types yet");
     97         return changeExtendedVectorElementTypeToInteger();
     98       }
     99       MVT EltTy = getSimpleVT().getVectorElementType();
    100       unsigned BitWidth = EltTy.getSizeInBits();
    101       MVT IntTy = MVT::getIntegerVT(BitWidth);
    102       MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements(),
    103                                    isScalableVector());
    104       assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
    105              "Simple vector VT not representable by simple integer vector VT!");
    106       return VecTy;
    107     }
    108 
    109     /// Return the type converted to an equivalently sized integer or vector
    110     /// with integer element type. Similar to changeVectorElementTypeToInteger,
    111     /// but also handles scalars.
    112     EVT changeTypeToInteger() {
    113       if (isVector())
    114         return changeVectorElementTypeToInteger();
    115 
    116       if (isSimple())
    117         return MVT::getIntegerVT(getSizeInBits());
    118 
    119       return changeExtendedTypeToInteger();
    120     }
    121 
    122     /// Test if the given EVT is simple (as opposed to being extended).
    123     bool isSimple() const {
    124       return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE;
    125     }
    126 
    127     /// Test if the given EVT is extended (as opposed to being simple).
    128     bool isExtended() const {
    129       return !isSimple();
    130     }
    131 
    132     /// Return true if this is a FP or a vector FP type.
    133     bool isFloatingPoint() const {
    134       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
    135     }
    136 
    137     /// Return true if this is an integer or a vector integer type.
    138     bool isInteger() const {
    139       return isSimple() ? V.isInteger() : isExtendedInteger();
    140     }
    141 
    142     /// Return true if this is an integer, but not a vector.
    143     bool isScalarInteger() const {
    144       return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
    145     }
    146 
    147     /// Return true if this is a vector value type.
    148     bool isVector() const {
    149       return isSimple() ? V.isVector() : isExtendedVector();
    150     }
    151 
    152     /// Return true if this is a vector type where the runtime
    153     /// length is machine dependent
    154     bool isScalableVector() const {
    155       // FIXME: We don't support extended scalable types yet, because the
    156       // matching IR type doesn't exist. Once it has been added, this can
    157       // be changed to call isExtendedScalableVector.
    158       if (!isSimple())
    159         return false;
    160       return V.isScalableVector();
    161     }
    162 
    163     /// Return true if this is a 16-bit vector type.
    164     bool is16BitVector() const {
    165       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
    166     }
    167 
    168     /// Return true if this is a 32-bit vector type.
    169     bool is32BitVector() const {
    170       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
    171     }
    172 
    173     /// Return true if this is a 64-bit vector type.
    174     bool is64BitVector() const {
    175       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
    176     }
    177 
    178     /// Return true if this is a 128-bit vector type.
    179     bool is128BitVector() const {
    180       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
    181     }
    182 
    183     /// Return true if this is a 256-bit vector type.
    184     bool is256BitVector() const {
    185       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
    186     }
    187 
    188     /// Return true if this is a 512-bit vector type.
    189     bool is512BitVector() const {
    190       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
    191     }
    192 
    193     /// Return true if this is a 1024-bit vector type.
    194     bool is1024BitVector() const {
    195       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
    196     }
    197 
    198     /// Return true if this is a 2048-bit vector type.
    199     bool is2048BitVector() const {
    200       return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
    201     }
    202 
    203     /// Return true if this is an overloaded type for TableGen.
    204     bool isOverloaded() const {
    205       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
    206     }
    207 
    208     /// Return true if the bit size is a multiple of 8.
    209     bool isByteSized() const {
    210       return (getSizeInBits() & 7) == 0;
    211     }
    212 
    213     /// Return true if the size is a power-of-two number of bytes.
    214     bool isRound() const {
    215       unsigned BitSize = getSizeInBits();
    216       return BitSize >= 8 && !(BitSize & (BitSize - 1));
    217     }
    218 
    219     /// Return true if this has the same number of bits as VT.
    220     bool bitsEq(EVT VT) const {
    221       if (EVT::operator==(VT)) return true;
    222       return getSizeInBits() == VT.getSizeInBits();
    223     }
    224 
    225     /// Return true if this has more bits than VT.
    226     bool bitsGT(EVT VT) const {
    227       if (EVT::operator==(VT)) return false;
    228       return getSizeInBits() > VT.getSizeInBits();
    229     }
    230 
    231     /// Return true if this has no less bits than VT.
    232     bool bitsGE(EVT VT) const {
    233       if (EVT::operator==(VT)) return true;
    234       return getSizeInBits() >= VT.getSizeInBits();
    235     }
    236 
    237     /// Return true if this has less bits than VT.
    238     bool bitsLT(EVT VT) const {
    239       if (EVT::operator==(VT)) return false;
    240       return getSizeInBits() < VT.getSizeInBits();
    241     }
    242 
    243     /// Return true if this has no more bits than VT.
    244     bool bitsLE(EVT VT) const {
    245       if (EVT::operator==(VT)) return true;
    246       return getSizeInBits() <= VT.getSizeInBits();
    247     }
    248 
    249 
    250     /// Return the SimpleValueType held in the specified simple EVT.
    251     MVT getSimpleVT() const {
    252       assert(isSimple() && "Expected a SimpleValueType!");
    253       return V;
    254     }
    255 
    256     /// If this is a vector type, return the element type, otherwise return
    257     /// this.
    258     EVT getScalarType() const {
    259       return isVector() ? getVectorElementType() : *this;
    260     }
    261 
    262     /// Given a vector type, return the type of each element.
    263     EVT getVectorElementType() const {
    264       assert(isVector() && "Invalid vector type!");
    265       if (isSimple())
    266         return V.getVectorElementType();
    267       return getExtendedVectorElementType();
    268     }
    269 
    270     /// Given a vector type, return the number of elements it contains.
    271     unsigned getVectorNumElements() const {
    272       assert(isVector() && "Invalid vector type!");
    273       if (isSimple())
    274         return V.getVectorNumElements();
    275       return getExtendedVectorNumElements();
    276     }
    277 
    278     // Given a (possibly scalable) vector type, return the ElementCount
    279     MVT::ElementCount getVectorElementCount() const {
    280       assert((isVector()) && "Invalid vector type!");
    281       if (isSimple())
    282         return V.getVectorElementCount();
    283 
    284       assert(!isScalableVector() &&
    285              "We don't support extended scalable types yet");
    286       return {getExtendedVectorNumElements(), false};
    287     }
    288 
    289     /// Return the size of the specified value type in bits.
    290     unsigned getSizeInBits() const {
    291       if (isSimple())
    292         return V.getSizeInBits();
    293       return getExtendedSizeInBits();
    294     }
    295 
    296     unsigned getScalarSizeInBits() const {
    297       return getScalarType().getSizeInBits();
    298     }
    299 
    300     /// Return the number of bytes overwritten by a store of the specified value
    301     /// type.
    302     unsigned getStoreSize() const {
    303       return (getSizeInBits() + 7) / 8;
    304     }
    305 
    306     /// Return the number of bits overwritten by a store of the specified value
    307     /// type.
    308     unsigned getStoreSizeInBits() const {
    309       return getStoreSize() * 8;
    310     }
    311 
    312     /// Rounds the bit-width of the given integer EVT up to the nearest power of
    313     /// two (and at least to eight), and returns the integer EVT with that
    314     /// number of bits.
    315     EVT getRoundIntegerType(LLVMContext &Context) const {
    316       assert(isInteger() && !isVector() && "Invalid integer type!");
    317       unsigned BitWidth = getSizeInBits();
    318       if (BitWidth <= 8)
    319         return EVT(MVT::i8);
    320       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
    321     }
    322 
    323     /// Finds the smallest simple value type that is greater than or equal to
    324     /// half the width of this EVT. If no simple value type can be found, an
    325     /// extended integer value type of half the size (rounded up) is returned.
    326     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
    327       assert(isInteger() && !isVector() && "Invalid integer type!");
    328       unsigned EVTSize = getSizeInBits();
    329       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
    330           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
    331         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
    332         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
    333           return HalfVT;
    334       }
    335       return getIntegerVT(Context, (EVTSize + 1) / 2);
    336     }
    337 
    338     /// Return a VT for an integer vector type with the size of the
    339     /// elements doubled. The typed returned may be an extended type.
    340     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
    341       EVT EltVT = getVectorElementType();
    342       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
    343       return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
    344     }
    345 
    346     // Return a VT for a vector type with the same element type but
    347     // half the number of elements. The type returned may be an
    348     // extended type.
    349     EVT getHalfNumVectorElementsVT(LLVMContext &Context) const {
    350       EVT EltVT = getVectorElementType();
    351       auto EltCnt = getVectorElementCount();
    352       assert(!(EltCnt.Min & 1) && "Splitting vector, but not in half!");
    353       return EVT::getVectorVT(Context, EltVT, EltCnt / 2);
    354     }
    355 
    356     /// Returns true if the given vector is a power of 2.
    357     bool isPow2VectorType() const {
    358       unsigned NElts = getVectorNumElements();
    359       return !(NElts & (NElts - 1));
    360     }
    361 
    362     /// Widens the length of the given vector EVT up to the nearest power of 2
    363     /// and returns that type.
    364     EVT getPow2VectorType(LLVMContext &Context) const {
    365       if (!isPow2VectorType()) {
    366         unsigned NElts = getVectorNumElements();
    367         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
    368         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts,
    369                                 isScalableVector());
    370       }
    371       else {
    372         return *this;
    373       }
    374     }
    375 
    376     /// This function returns value type as a string, e.g. "i32".
    377     std::string getEVTString() const;
    378 
    379     /// This method returns an LLVM type corresponding to the specified EVT.
    380     /// For integer types, this returns an unsigned type. Note that this will
    381     /// abort for types that cannot be represented.
    382     Type *getTypeForEVT(LLVMContext &Context) const;
    383 
    384     /// Return the value type corresponding to the specified type.
    385     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
    386     /// types are returned as Other, otherwise they are invalid.
    387     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
    388 
    389     intptr_t getRawBits() const {
    390       if (isSimple())
    391         return V.SimpleTy;
    392       else
    393         return (intptr_t)(LLVMTy);
    394     }
    395 
    396     /// A meaningless but well-behaved order, useful for constructing
    397     /// containers.
    398     struct compareRawBits {
    399       bool operator()(EVT L, EVT R) const {
    400         if (L.V.SimpleTy == R.V.SimpleTy)
    401           return L.LLVMTy < R.LLVMTy;
    402         else
    403           return L.V.SimpleTy < R.V.SimpleTy;
    404       }
    405     };
    406 
    407   private:
    408     // Methods for handling the Extended-type case in functions above.
    409     // These are all out-of-line to prevent users of this header file
    410     // from having a dependency on Type.h.
    411     EVT changeExtendedTypeToInteger() const;
    412     EVT changeExtendedVectorElementTypeToInteger() const;
    413     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
    414     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
    415                                    unsigned NumElements);
    416     bool isExtendedFloatingPoint() const LLVM_READONLY;
    417     bool isExtendedInteger() const LLVM_READONLY;
    418     bool isExtendedScalarInteger() const LLVM_READONLY;
    419     bool isExtendedVector() const LLVM_READONLY;
    420     bool isExtended16BitVector() const LLVM_READONLY;
    421     bool isExtended32BitVector() const LLVM_READONLY;
    422     bool isExtended64BitVector() const LLVM_READONLY;
    423     bool isExtended128BitVector() const LLVM_READONLY;
    424     bool isExtended256BitVector() const LLVM_READONLY;
    425     bool isExtended512BitVector() const LLVM_READONLY;
    426     bool isExtended1024BitVector() const LLVM_READONLY;
    427     bool isExtended2048BitVector() const LLVM_READONLY;
    428     EVT getExtendedVectorElementType() const;
    429     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
    430     unsigned getExtendedSizeInBits() const LLVM_READONLY;
    431   };
    432 
    433 } // End llvm namespace
    434 
    435 #endif
    436