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/Support/DataTypes.h"
     20 #include "llvm/Support/ErrorHandling.h"
     21 #include "llvm/Support/MathExtras.h"
     22 #include <cassert>
     23 #include <string>
     24 
     25 namespace llvm {
     26   class Type;
     27   class LLVMContext;
     28   struct EVT;
     29 
     30   /// MVT - Machine Value Type.  Every type that is supported natively by some
     31   /// processor targeted by LLVM occurs here.  This means that any legal value
     32   /// type can be represented by a MVT.
     33   class MVT {
     34   public:
     35     enum SimpleValueType {
     36       // INVALID_SIMPLE_VALUE_TYPE - Simple value types less than zero are
     37       // considered extended value types.
     38       INVALID_SIMPLE_VALUE_TYPE = -1,
     39 
     40       // If you change this numbering, you must change the values in
     41       // ValueTypes.td as well!
     42       Other          =   0,   // This is a non-standard value
     43       i1             =   1,   // This is a 1 bit integer value
     44       i8             =   2,   // This is an 8 bit integer value
     45       i16            =   3,   // This is a 16 bit integer value
     46       i32            =   4,   // This is a 32 bit integer value
     47       i64            =   5,   // This is a 64 bit integer value
     48       i128           =   6,   // This is a 128 bit integer value
     49 
     50       FIRST_INTEGER_VALUETYPE = i1,
     51       LAST_INTEGER_VALUETYPE  = i128,
     52 
     53       f16            =   7,   // This is a 16 bit floating point value
     54       f32            =   8,   // This is a 32 bit floating point value
     55       f64            =   9,   // This is a 64 bit floating point value
     56       f80            =  10,   // This is a 80 bit floating point value
     57       f128           =  11,   // This is a 128 bit floating point value
     58       ppcf128        =  12,   // This is a PPC 128-bit floating point value
     59 
     60       FIRST_FP_VALUETYPE = f16,
     61       LAST_FP_VALUETYPE  = ppcf128,
     62 
     63       v2i1           =  13,   //  2 x i1
     64       v4i1           =  14,   //  4 x i1
     65       v8i1           =  15,   //  8 x i1
     66       v16i1          =  16,   // 16 x i1
     67       v32i1          =  17,   // 32 x i1
     68       v64i1          =  18,   // 64 x i1
     69 
     70       v2i8           =  19,   //  2 x i8
     71       v4i8           =  20,   //  4 x i8
     72       v8i8           =  21,   //  8 x i8
     73       v16i8          =  22,   // 16 x i8
     74       v32i8          =  23,   // 32 x i8
     75       v64i8          =  24,   // 64 x i8
     76       v1i16          =  25,   //  1 x i16
     77       v2i16          =  26,   //  2 x i16
     78       v4i16          =  27,   //  4 x i16
     79       v8i16          =  28,   //  8 x i16
     80       v16i16         =  29,   // 16 x i16
     81       v32i16         =  30,   // 32 x i16
     82       v1i32          =  31,   //  1 x i32
     83       v2i32          =  32,   //  2 x i32
     84       v4i32          =  33,   //  4 x i32
     85       v8i32          =  34,   //  8 x i32
     86       v16i32         =  35,   // 16 x i32
     87       v1i64          =  36,   //  1 x i64
     88       v2i64          =  37,   //  2 x i64
     89       v4i64          =  38,   //  4 x i64
     90       v8i64          =  39,   //  8 x i64
     91       v16i64         =  40,   // 16 x i64
     92 
     93       FIRST_INTEGER_VECTOR_VALUETYPE = v2i1,
     94       LAST_INTEGER_VECTOR_VALUETYPE = v16i64,
     95 
     96       v2f16          =  41,   //  2 x f16
     97       v2f32          =  42,   //  2 x f32
     98       v4f32          =  43,   //  4 x f32
     99       v8f32          =  44,   //  8 x f32
    100       v16f32         =  45,   // 16 x f32
    101       v2f64          =  46,   //  2 x f64
    102       v4f64          =  47,   //  4 x f64
    103       v8f64          =  48,   //  8 x f64
    104 
    105       FIRST_FP_VECTOR_VALUETYPE = v2f16,
    106       LAST_FP_VECTOR_VALUETYPE = v8f64,
    107 
    108       FIRST_VECTOR_VALUETYPE = v2i1,
    109       LAST_VECTOR_VALUETYPE  = v8f64,
    110 
    111       x86mmx         =  49,   // This is an X86 MMX value
    112 
    113       Glue           =  50,   // This glues nodes together during pre-RA sched
    114 
    115       isVoid         =  51,   // This has no value
    116 
    117       Untyped        =  52,   // This value takes a register, but has
    118                               // unspecified type.  The register class
    119                               // will be determined by the opcode.
    120 
    121       LAST_VALUETYPE =  53,   // This always remains at the end of the list.
    122 
    123       // This is the current maximum for LAST_VALUETYPE.
    124       // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
    125       // This value must be a multiple of 32.
    126       MAX_ALLOWED_VALUETYPE = 64,
    127 
    128       // Metadata - This is MDNode or MDString.
    129       Metadata       = 250,
    130 
    131       // iPTRAny - An int value the size of the pointer of the current
    132       // target to any address space. This must only be used internal to
    133       // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
    134       iPTRAny        = 251,
    135 
    136       // vAny - A vector with any length and element size. This is used
    137       // for intrinsics that have overloadings based on vector types.
    138       // This is only for tblgen's consumption!
    139       vAny           = 252,
    140 
    141       // fAny - Any floating-point or vector floating-point value. This is used
    142       // for intrinsics that have overloadings based on floating-point types.
    143       // This is only for tblgen's consumption!
    144       fAny           = 253,
    145 
    146       // iAny - An integer or vector integer value of any bit width. This is
    147       // used for intrinsics that have overloadings based on integer bit widths.
    148       // This is only for tblgen's consumption!
    149       iAny           = 254,
    150 
    151       // iPTR - An int value the size of the pointer of the current
    152       // target.  This should only be used internal to tblgen!
    153       iPTR           = 255
    154     };
    155 
    156     SimpleValueType SimpleTy;
    157 
    158     MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
    159     MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
    160 
    161     bool operator>(const MVT& S)  const { return SimpleTy >  S.SimpleTy; }
    162     bool operator<(const MVT& S)  const { return SimpleTy <  S.SimpleTy; }
    163     bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
    164     bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
    165     bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
    166     bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
    167 
    168     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
    169     bool isFloatingPoint() const {
    170       return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
    171                SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
    172               (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
    173                SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
    174     }
    175 
    176     /// isInteger - Return true if this is an integer, or a vector integer type.
    177     bool isInteger() const {
    178       return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
    179                SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
    180               (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
    181                SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
    182     }
    183 
    184     /// isVector - Return true if this is a vector value type.
    185     bool isVector() const {
    186       return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
    187               SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
    188     }
    189 
    190     /// is16BitVector - Return true if this is a 16-bit vector type.
    191     bool is16BitVector() const {
    192       return (SimpleTy == MVT::v2i8  || SimpleTy == MVT::v1i16 ||
    193               SimpleTy == MVT::v16i1);
    194     }
    195 
    196     /// is32BitVector - Return true if this is a 32-bit vector type.
    197     bool is32BitVector() const {
    198       return (SimpleTy == MVT::v4i8  || SimpleTy == MVT::v2i16 ||
    199               SimpleTy == MVT::v1i32);
    200     }
    201 
    202     /// is64BitVector - Return true if this is a 64-bit vector type.
    203     bool is64BitVector() const {
    204       return (SimpleTy == MVT::v8i8  || SimpleTy == MVT::v4i16 ||
    205               SimpleTy == MVT::v2i32 || SimpleTy == MVT::v1i64 ||
    206               SimpleTy == MVT::v2f32);
    207     }
    208 
    209     /// is128BitVector - Return true if this is a 128-bit vector type.
    210     bool is128BitVector() const {
    211       return (SimpleTy == MVT::v16i8 || SimpleTy == MVT::v8i16 ||
    212               SimpleTy == MVT::v4i32 || SimpleTy == MVT::v2i64 ||
    213               SimpleTy == MVT::v4f32 || SimpleTy == MVT::v2f64);
    214     }
    215 
    216     /// is256BitVector - Return true if this is a 256-bit vector type.
    217     bool is256BitVector() const {
    218       return (SimpleTy == MVT::v8f32 || SimpleTy == MVT::v4f64  ||
    219               SimpleTy == MVT::v32i8 || SimpleTy == MVT::v16i16 ||
    220               SimpleTy == MVT::v8i32 || SimpleTy == MVT::v4i64);
    221     }
    222 
    223     /// is512BitVector - Return true if this is a 512-bit vector type.
    224     bool is512BitVector() const {
    225       return (SimpleTy == MVT::v8f64 || SimpleTy == MVT::v16f32 ||
    226               SimpleTy == MVT::v64i8 || SimpleTy == MVT::v32i16 ||
    227               SimpleTy == MVT::v8i64 || SimpleTy == MVT::v16i32);
    228     }
    229 
    230     /// is1024BitVector - Return true if this is a 1024-bit vector type.
    231     bool is1024BitVector() const {
    232       return (SimpleTy == MVT::v16i64);
    233     }
    234 
    235     /// isPow2VectorType - Returns true if the given vector is a power of 2.
    236     bool isPow2VectorType() const {
    237       unsigned NElts = getVectorNumElements();
    238       return !(NElts & (NElts - 1));
    239     }
    240 
    241     /// getPow2VectorType - Widens the length of the given vector MVT up to
    242     /// the nearest power of 2 and returns that type.
    243     MVT getPow2VectorType() const {
    244       if (isPow2VectorType())
    245         return *this;
    246 
    247       unsigned NElts = getVectorNumElements();
    248       unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
    249       return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
    250     }
    251 
    252     /// getScalarType - If this is a vector type, return the element type,
    253     /// otherwise return this.
    254     MVT getScalarType() const {
    255       return isVector() ? getVectorElementType() : *this;
    256     }
    257 
    258     MVT getVectorElementType() const {
    259       switch (SimpleTy) {
    260       default:
    261         llvm_unreachable("Not a vector MVT!");
    262       case v2i1 :
    263       case v4i1 :
    264       case v8i1 :
    265       case v16i1 :
    266       case v32i1 :
    267       case v64i1: return i1;
    268       case v2i8 :
    269       case v4i8 :
    270       case v8i8 :
    271       case v16i8:
    272       case v32i8:
    273       case v64i8: return i8;
    274       case v1i16:
    275       case v2i16:
    276       case v4i16:
    277       case v8i16:
    278       case v16i16:
    279       case v32i16: return i16;
    280       case v1i32:
    281       case v2i32:
    282       case v4i32:
    283       case v8i32:
    284       case v16i32: return i32;
    285       case v1i64:
    286       case v2i64:
    287       case v4i64:
    288       case v8i64:
    289       case v16i64: return i64;
    290       case v2f16: return f16;
    291       case v2f32:
    292       case v4f32:
    293       case v8f32:
    294       case v16f32: return f32;
    295       case v2f64:
    296       case v4f64:
    297       case v8f64: return f64;
    298       }
    299     }
    300 
    301     unsigned getVectorNumElements() const {
    302       switch (SimpleTy) {
    303       default:
    304         llvm_unreachable("Not a vector MVT!");
    305       case v32i1:
    306       case v32i8:
    307       case v32i16: return 32;
    308       case v64i1:
    309       case v64i8: return 64;
    310       case v16i1:
    311       case v16i8:
    312       case v16i16:
    313       case v16i32:
    314       case v16i64:
    315       case v16f32: return 16;
    316       case v8i1 :
    317       case v8i8 :
    318       case v8i16:
    319       case v8i32:
    320       case v8i64:
    321       case v8f32:
    322       case v8f64: return 8;
    323       case v4i1:
    324       case v4i8:
    325       case v4i16:
    326       case v4i32:
    327       case v4i64:
    328       case v4f32:
    329       case v4f64: return 4;
    330       case v2i1:
    331       case v2i8:
    332       case v2i16:
    333       case v2i32:
    334       case v2i64:
    335       case v2f16:
    336       case v2f32:
    337       case v2f64: return 2;
    338       case v1i16:
    339       case v1i32:
    340       case v1i64: return 1;
    341       }
    342     }
    343 
    344     unsigned getSizeInBits() const {
    345       switch (SimpleTy) {
    346       default:
    347         llvm_unreachable("getSizeInBits called on extended MVT.");
    348       case Other:
    349         llvm_unreachable("Value type is non-standard value, Other.");
    350       case iPTR:
    351         llvm_unreachable("Value type size is target-dependent. Ask TLI.");
    352       case iPTRAny:
    353       case iAny:
    354       case fAny:
    355       case vAny:
    356         llvm_unreachable("Value type is overloaded.");
    357       case Metadata:
    358         llvm_unreachable("Value type is metadata.");
    359       case i1  :  return 1;
    360       case v2i1:  return 2;
    361       case v4i1:  return 4;
    362       case i8  :
    363       case v8i1: return 8;
    364       case i16 :
    365       case f16:
    366       case v16i1:
    367       case v2i8:
    368       case v1i16: return 16;
    369       case f32 :
    370       case i32 :
    371       case v32i1:
    372       case v4i8:
    373       case v2i16:
    374       case v2f16:
    375       case v1i32: return 32;
    376       case x86mmx:
    377       case f64 :
    378       case i64 :
    379       case v64i1:
    380       case v8i8:
    381       case v4i16:
    382       case v2i32:
    383       case v1i64:
    384       case v2f32: return 64;
    385       case f80 :  return 80;
    386       case f128:
    387       case ppcf128:
    388       case i128:
    389       case v16i8:
    390       case v8i16:
    391       case v4i32:
    392       case v2i64:
    393       case v4f32:
    394       case v2f64: return 128;
    395       case v32i8:
    396       case v16i16:
    397       case v8i32:
    398       case v4i64:
    399       case v8f32:
    400       case v4f64: return 256;
    401       case v64i8:
    402       case v32i16:
    403       case v16i32:
    404       case v8i64:
    405       case v16f32:
    406       case v8f64: return 512;
    407       case v16i64:return 1024;
    408       }
    409     }
    410 
    411     /// getStoreSize - Return the number of bytes overwritten by a store
    412     /// of the specified value type.
    413     unsigned getStoreSize() const {
    414       return (getSizeInBits() + 7) / 8;
    415     }
    416 
    417     /// getStoreSizeInBits - Return the number of bits overwritten by a store
    418     /// of the specified value type.
    419     unsigned getStoreSizeInBits() const {
    420       return getStoreSize() * 8;
    421     }
    422 
    423     /// Return true if this has more bits than VT.
    424     bool bitsGT(MVT VT) const {
    425       return getSizeInBits() > VT.getSizeInBits();
    426     }
    427 
    428     /// Return true if this has no less bits than VT.
    429     bool bitsGE(MVT VT) const {
    430       return getSizeInBits() >= VT.getSizeInBits();
    431     }
    432 
    433     /// Return true if this has less bits than VT.
    434     bool bitsLT(MVT VT) const {
    435       return getSizeInBits() < VT.getSizeInBits();
    436     }
    437 
    438     /// Return true if this has no more bits than VT.
    439     bool bitsLE(MVT VT) const {
    440       return getSizeInBits() <= VT.getSizeInBits();
    441     }
    442 
    443 
    444     static MVT getFloatingPointVT(unsigned BitWidth) {
    445       switch (BitWidth) {
    446       default:
    447         llvm_unreachable("Bad bit width!");
    448       case 16:
    449         return MVT::f16;
    450       case 32:
    451         return MVT::f32;
    452       case 64:
    453         return MVT::f64;
    454       case 80:
    455         return MVT::f80;
    456       case 128:
    457         return MVT::f128;
    458       }
    459     }
    460 
    461     static MVT getIntegerVT(unsigned BitWidth) {
    462       switch (BitWidth) {
    463       default:
    464         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
    465       case 1:
    466         return MVT::i1;
    467       case 8:
    468         return MVT::i8;
    469       case 16:
    470         return MVT::i16;
    471       case 32:
    472         return MVT::i32;
    473       case 64:
    474         return MVT::i64;
    475       case 128:
    476         return MVT::i128;
    477       }
    478     }
    479 
    480     static MVT getVectorVT(MVT VT, unsigned NumElements) {
    481       switch (VT.SimpleTy) {
    482       default:
    483         break;
    484       case MVT::i1:
    485         if (NumElements == 2)  return MVT::v2i1;
    486         if (NumElements == 4)  return MVT::v4i1;
    487         if (NumElements == 8)  return MVT::v8i1;
    488         if (NumElements == 16) return MVT::v16i1;
    489         if (NumElements == 32) return MVT::v32i1;
    490         if (NumElements == 64) return MVT::v64i1;
    491         break;
    492       case MVT::i8:
    493         if (NumElements == 2)  return MVT::v2i8;
    494         if (NumElements == 4)  return MVT::v4i8;
    495         if (NumElements == 8)  return MVT::v8i8;
    496         if (NumElements == 16) return MVT::v16i8;
    497         if (NumElements == 32) return MVT::v32i8;
    498         if (NumElements == 64) return MVT::v64i8;
    499         break;
    500       case MVT::i16:
    501         if (NumElements == 1)  return MVT::v1i16;
    502         if (NumElements == 2)  return MVT::v2i16;
    503         if (NumElements == 4)  return MVT::v4i16;
    504         if (NumElements == 8)  return MVT::v8i16;
    505         if (NumElements == 16) return MVT::v16i16;
    506         if (NumElements == 32) return MVT::v32i16;
    507         break;
    508       case MVT::i32:
    509         if (NumElements == 1)  return MVT::v1i32;
    510         if (NumElements == 2)  return MVT::v2i32;
    511         if (NumElements == 4)  return MVT::v4i32;
    512         if (NumElements == 8)  return MVT::v8i32;
    513         if (NumElements == 16) return MVT::v16i32;
    514         break;
    515       case MVT::i64:
    516         if (NumElements == 1)  return MVT::v1i64;
    517         if (NumElements == 2)  return MVT::v2i64;
    518         if (NumElements == 4)  return MVT::v4i64;
    519         if (NumElements == 8)  return MVT::v8i64;
    520         if (NumElements == 16) return MVT::v16i64;
    521         break;
    522       case MVT::f16:
    523         if (NumElements == 2)  return MVT::v2f16;
    524         break;
    525       case MVT::f32:
    526         if (NumElements == 2)  return MVT::v2f32;
    527         if (NumElements == 4)  return MVT::v4f32;
    528         if (NumElements == 8)  return MVT::v8f32;
    529         if (NumElements == 16) return MVT::v16f32;
    530         break;
    531       case MVT::f64:
    532         if (NumElements == 2)  return MVT::v2f64;
    533         if (NumElements == 4)  return MVT::v4f64;
    534         if (NumElements == 8)  return MVT::v8f64;
    535         break;
    536       }
    537       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
    538     }
    539 
    540     /// Return the value type corresponding to the specified type.  This returns
    541     /// all pointers as iPTR.  If HandleUnknown is true, unknown types are
    542     /// returned as Other, otherwise they are invalid.
    543     static MVT getVT(Type *Ty, bool HandleUnknown = false);
    544 
    545   };
    546 
    547 
    548   /// EVT - Extended Value Type.  Capable of holding value types which are not
    549   /// native for any processor (such as the i12345 type), as well as the types
    550   /// a MVT can represent.
    551   struct EVT {
    552   private:
    553     MVT V;
    554     Type *LLVMTy;
    555 
    556   public:
    557     EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
    558             LLVMTy(0) {}
    559     EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
    560     EVT(MVT S) : V(S), LLVMTy(0) {}
    561 
    562     bool operator==(EVT VT) const {
    563       return !(*this != VT);
    564     }
    565     bool operator!=(EVT VT) const {
    566       if (V.SimpleTy != VT.V.SimpleTy)
    567         return true;
    568       if (V.SimpleTy < 0)
    569         return LLVMTy != VT.LLVMTy;
    570       return false;
    571     }
    572 
    573     /// getFloatingPointVT - Returns the EVT that represents a floating point
    574     /// type with the given number of bits.  There are two floating point types
    575     /// with 128 bits - this returns f128 rather than ppcf128.
    576     static EVT getFloatingPointVT(unsigned BitWidth) {
    577       return MVT::getFloatingPointVT(BitWidth);
    578     }
    579 
    580     /// getIntegerVT - Returns the EVT that represents an integer with the given
    581     /// number of bits.
    582     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
    583       MVT M = MVT::getIntegerVT(BitWidth);
    584       if (M.SimpleTy >= 0)
    585         return M;
    586       return getExtendedIntegerVT(Context, BitWidth);
    587     }
    588 
    589     /// getVectorVT - Returns the EVT that represents a vector NumElements in
    590     /// length, where each element is of type VT.
    591     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
    592       MVT M = MVT::getVectorVT(VT.V, NumElements);
    593       if (M.SimpleTy >= 0)
    594         return M;
    595       return getExtendedVectorVT(Context, VT, NumElements);
    596     }
    597 
    598     /// changeVectorElementTypeToInteger - Return a vector with the same number
    599     /// of elements as this vector, but with the element type converted to an
    600     /// integer type with the same bitwidth.
    601     EVT changeVectorElementTypeToInteger() const {
    602       if (!isSimple())
    603         return changeExtendedVectorElementTypeToInteger();
    604       MVT EltTy = getSimpleVT().getVectorElementType();
    605       unsigned BitWidth = EltTy.getSizeInBits();
    606       MVT IntTy = MVT::getIntegerVT(BitWidth);
    607       MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
    608       assert(VecTy.SimpleTy >= 0 &&
    609              "Simple vector VT not representable by simple integer vector VT!");
    610       return VecTy;
    611     }
    612 
    613     /// isSimple - Test if the given EVT is simple (as opposed to being
    614     /// extended).
    615     bool isSimple() const {
    616       return V.SimpleTy >= 0;
    617     }
    618 
    619     /// isExtended - Test if the given EVT is extended (as opposed to
    620     /// being simple).
    621     bool isExtended() const {
    622       return !isSimple();
    623     }
    624 
    625     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
    626     bool isFloatingPoint() const {
    627       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
    628     }
    629 
    630     /// isInteger - Return true if this is an integer, or a vector integer type.
    631     bool isInteger() const {
    632       return isSimple() ? V.isInteger() : isExtendedInteger();
    633     }
    634 
    635     /// isVector - Return true if this is a vector value type.
    636     bool isVector() const {
    637       return isSimple() ? V.isVector() : isExtendedVector();
    638     }
    639 
    640     /// is16BitVector - Return true if this is a 16-bit vector type.
    641     bool is16BitVector() const {
    642       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
    643     }
    644 
    645     /// is32BitVector - Return true if this is a 32-bit vector type.
    646     bool is32BitVector() const {
    647       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
    648     }
    649 
    650     /// is64BitVector - Return true if this is a 64-bit vector type.
    651     bool is64BitVector() const {
    652       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
    653     }
    654 
    655     /// is128BitVector - Return true if this is a 128-bit vector type.
    656     bool is128BitVector() const {
    657       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
    658     }
    659 
    660     /// is256BitVector - Return true if this is a 256-bit vector type.
    661     bool is256BitVector() const {
    662       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
    663     }
    664 
    665     /// is512BitVector - Return true if this is a 512-bit vector type.
    666     bool is512BitVector() const {
    667       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
    668     }
    669 
    670     /// is1024BitVector - Return true if this is a 1024-bit vector type.
    671     bool is1024BitVector() const {
    672       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
    673     }
    674 
    675     /// isOverloaded - Return true if this is an overloaded type for TableGen.
    676     bool isOverloaded() const {
    677       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
    678     }
    679 
    680     /// isByteSized - Return true if the bit size is a multiple of 8.
    681     bool isByteSized() const {
    682       return (getSizeInBits() & 7) == 0;
    683     }
    684 
    685     /// isRound - Return true if the size is a power-of-two number of bytes.
    686     bool isRound() const {
    687       unsigned BitSize = getSizeInBits();
    688       return BitSize >= 8 && !(BitSize & (BitSize - 1));
    689     }
    690 
    691     /// bitsEq - Return true if this has the same number of bits as VT.
    692     bool bitsEq(EVT VT) const {
    693       if (EVT::operator==(VT)) return true;
    694       return getSizeInBits() == VT.getSizeInBits();
    695     }
    696 
    697     /// bitsGT - Return true if this has more bits than VT.
    698     bool bitsGT(EVT VT) const {
    699       if (EVT::operator==(VT)) return false;
    700       return getSizeInBits() > VT.getSizeInBits();
    701     }
    702 
    703     /// bitsGE - Return true if this has no less bits than VT.
    704     bool bitsGE(EVT VT) const {
    705       if (EVT::operator==(VT)) return true;
    706       return getSizeInBits() >= VT.getSizeInBits();
    707     }
    708 
    709     /// bitsLT - Return true if this has less bits than VT.
    710     bool bitsLT(EVT VT) const {
    711       if (EVT::operator==(VT)) return false;
    712       return getSizeInBits() < VT.getSizeInBits();
    713     }
    714 
    715     /// bitsLE - Return true if this has no more bits than VT.
    716     bool bitsLE(EVT VT) const {
    717       if (EVT::operator==(VT)) return true;
    718       return getSizeInBits() <= VT.getSizeInBits();
    719     }
    720 
    721 
    722     /// getSimpleVT - Return the SimpleValueType held in the specified
    723     /// simple EVT.
    724     MVT getSimpleVT() const {
    725       assert(isSimple() && "Expected a SimpleValueType!");
    726       return V;
    727     }
    728 
    729     /// getScalarType - If this is a vector type, return the element type,
    730     /// otherwise return this.
    731     EVT getScalarType() const {
    732       return isVector() ? getVectorElementType() : *this;
    733     }
    734 
    735     /// getVectorElementType - Given a vector type, return the type of
    736     /// each element.
    737     EVT getVectorElementType() const {
    738       assert(isVector() && "Invalid vector type!");
    739       if (isSimple())
    740         return V.getVectorElementType();
    741       return getExtendedVectorElementType();
    742     }
    743 
    744     /// getVectorNumElements - Given a vector type, return the number of
    745     /// elements it contains.
    746     unsigned getVectorNumElements() const {
    747       assert(isVector() && "Invalid vector type!");
    748       if (isSimple())
    749         return V.getVectorNumElements();
    750       return getExtendedVectorNumElements();
    751     }
    752 
    753     /// getSizeInBits - Return the size of the specified value type in bits.
    754     unsigned getSizeInBits() const {
    755       if (isSimple())
    756         return V.getSizeInBits();
    757       return getExtendedSizeInBits();
    758     }
    759 
    760     /// getStoreSize - Return the number of bytes overwritten by a store
    761     /// of the specified value type.
    762     unsigned getStoreSize() const {
    763       return (getSizeInBits() + 7) / 8;
    764     }
    765 
    766     /// getStoreSizeInBits - Return the number of bits overwritten by a store
    767     /// of the specified value type.
    768     unsigned getStoreSizeInBits() const {
    769       return getStoreSize() * 8;
    770     }
    771 
    772     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
    773     /// to the nearest power of two (and at least to eight), and returns the
    774     /// integer EVT with that number of bits.
    775     EVT getRoundIntegerType(LLVMContext &Context) const {
    776       assert(isInteger() && !isVector() && "Invalid integer type!");
    777       unsigned BitWidth = getSizeInBits();
    778       if (BitWidth <= 8)
    779         return EVT(MVT::i8);
    780       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
    781     }
    782 
    783     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
    784     /// greater than or equal to half the width of this EVT. If no simple
    785     /// value type can be found, an extended integer value type of half the
    786     /// size (rounded up) is returned.
    787     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
    788       assert(isInteger() && !isVector() && "Invalid integer type!");
    789       unsigned EVTSize = getSizeInBits();
    790       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
    791           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
    792         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
    793         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
    794           return HalfVT;
    795       }
    796       return getIntegerVT(Context, (EVTSize + 1) / 2);
    797     }
    798 
    799     /// isPow2VectorType - Returns true if the given vector is a power of 2.
    800     bool isPow2VectorType() const {
    801       unsigned NElts = getVectorNumElements();
    802       return !(NElts & (NElts - 1));
    803     }
    804 
    805     /// getPow2VectorType - Widens the length of the given vector EVT up to
    806     /// the nearest power of 2 and returns that type.
    807     EVT getPow2VectorType(LLVMContext &Context) const {
    808       if (!isPow2VectorType()) {
    809         unsigned NElts = getVectorNumElements();
    810         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
    811         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
    812       }
    813       else {
    814         return *this;
    815       }
    816     }
    817 
    818     /// getEVTString - This function returns value type as a string,
    819     /// e.g. "i32".
    820     std::string getEVTString() const;
    821 
    822     /// getTypeForEVT - This method returns an LLVM type corresponding to the
    823     /// specified EVT.  For integer types, this returns an unsigned type.  Note
    824     /// that this will abort for types that cannot be represented.
    825     Type *getTypeForEVT(LLVMContext &Context) const;
    826 
    827     /// getEVT - Return the value type corresponding to the specified type.
    828     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
    829     /// types are returned as Other, otherwise they are invalid.
    830     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
    831 
    832     intptr_t getRawBits() const {
    833       if (isSimple())
    834         return V.SimpleTy;
    835       else
    836         return (intptr_t)(LLVMTy);
    837     }
    838 
    839     /// compareRawBits - A meaningless but well-behaved order, useful for
    840     /// constructing containers.
    841     struct compareRawBits {
    842       bool operator()(EVT L, EVT R) const {
    843         if (L.V.SimpleTy == R.V.SimpleTy)
    844           return L.LLVMTy < R.LLVMTy;
    845         else
    846           return L.V.SimpleTy < R.V.SimpleTy;
    847       }
    848     };
    849 
    850   private:
    851     // Methods for handling the Extended-type case in functions above.
    852     // These are all out-of-line to prevent users of this header file
    853     // from having a dependency on Type.h.
    854     EVT changeExtendedVectorElementTypeToInteger() const;
    855     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
    856     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
    857                                    unsigned NumElements);
    858     bool isExtendedFloatingPoint() const;
    859     bool isExtendedInteger() const;
    860     bool isExtendedVector() const;
    861     bool isExtended16BitVector() const;
    862     bool isExtended32BitVector() const;
    863     bool isExtended64BitVector() const;
    864     bool isExtended128BitVector() const;
    865     bool isExtended256BitVector() const;
    866     bool isExtended512BitVector() const;
    867     bool isExtended1024BitVector() const;
    868     EVT getExtendedVectorElementType() const;
    869     unsigned getExtendedVectorNumElements() const;
    870     unsigned getExtendedSizeInBits() const;
    871   };
    872 
    873 } // End llvm namespace
    874 
    875 #endif
    876