LLVM API Documentation

MachineValueType.h
Go to the documentation of this file.
00001 //===- CodeGen/MachineValueType.h - Machine-Level 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 machine-level target independent types which
00011 // legal values in the code generator use.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_MACHINEVALUETYPE_H
00016 #define LLVM_CODEGEN_MACHINEVALUETYPE_H
00017 
00018 #include "llvm/Support/ErrorHandling.h"
00019 #include "llvm/Support/MathExtras.h"
00020 
00021 namespace llvm {
00022 
00023   class Type;
00024 
00025   /// MVT - Machine Value Type. Every type that is supported natively by some
00026   /// processor targeted by LLVM occurs here. This means that any legal value
00027   /// type can be represented by an MVT.
00028   class MVT {
00029   public:
00030     enum SimpleValueType {
00031       // INVALID_SIMPLE_VALUE_TYPE - Simple value types less than zero are
00032       // considered extended value types.
00033       INVALID_SIMPLE_VALUE_TYPE = -1,
00034 
00035       // If you change this numbering, you must change the values in
00036       // ValueTypes.td as well!
00037       Other          =   0,   // This is a non-standard value
00038       i1             =   1,   // This is a 1 bit integer value
00039       i8             =   2,   // This is an 8 bit integer value
00040       i16            =   3,   // This is a 16 bit integer value
00041       i32            =   4,   // This is a 32 bit integer value
00042       i64            =   5,   // This is a 64 bit integer value
00043       i128           =   6,   // This is a 128 bit integer value
00044 
00045       FIRST_INTEGER_VALUETYPE = i1,
00046       LAST_INTEGER_VALUETYPE  = i128,
00047 
00048       f16            =   7,   // This is a 16 bit floating point value
00049       f32            =   8,   // This is a 32 bit floating point value
00050       f64            =   9,   // This is a 64 bit floating point value
00051       f80            =  10,   // This is a 80 bit floating point value
00052       f128           =  11,   // This is a 128 bit floating point value
00053       ppcf128        =  12,   // This is a PPC 128-bit floating point value
00054 
00055       FIRST_FP_VALUETYPE = f16,
00056       LAST_FP_VALUETYPE  = ppcf128,
00057 
00058       v2i1           =  13,   //  2 x i1
00059       v4i1           =  14,   //  4 x i1
00060       v8i1           =  15,   //  8 x i1
00061       v16i1          =  16,   // 16 x i1
00062       v32i1          =  17,   // 32 x i1
00063       v64i1          =  18,   // 64 x i1
00064 
00065       v1i8           =  19,   //  1 x i8
00066       v2i8           =  20,   //  2 x i8
00067       v4i8           =  21,   //  4 x i8
00068       v8i8           =  22,   //  8 x i8
00069       v16i8          =  23,   // 16 x i8
00070       v32i8          =  24,   // 32 x i8
00071       v64i8          =  25,   // 64 x i8
00072       v1i16          =  26,   //  1 x i16
00073       v2i16          =  27,   //  2 x i16
00074       v4i16          =  28,   //  4 x i16
00075       v8i16          =  29,   //  8 x i16
00076       v16i16         =  30,   // 16 x i16
00077       v32i16         =  31,   // 32 x i16
00078       v1i32          =  32,   //  1 x i32
00079       v2i32          =  33,   //  2 x i32
00080       v4i32          =  34,   //  4 x i32
00081       v8i32          =  35,   //  8 x i32
00082       v16i32         =  36,   // 16 x i32
00083       v1i64          =  37,   //  1 x i64
00084       v2i64          =  38,   //  2 x i64
00085       v4i64          =  39,   //  4 x i64
00086       v8i64          =  40,   //  8 x i64
00087       v16i64         =  41,   // 16 x i64
00088 
00089       FIRST_INTEGER_VECTOR_VALUETYPE = v2i1,
00090       LAST_INTEGER_VECTOR_VALUETYPE = v16i64,
00091 
00092       v2f16          =  42,   //  2 x f16
00093       v4f16          =  43,   //  4 x f16
00094       v8f16          =  44,   //  8 x f16
00095       v1f32          =  45,   //  1 x f32
00096       v2f32          =  46,   //  2 x f32
00097       v4f32          =  47,   //  4 x f32
00098       v8f32          =  48,   //  8 x f32
00099       v16f32         =  49,   // 16 x f32
00100       v1f64          =  50,   //  1 x f64
00101       v2f64          =  51,   //  2 x f64
00102       v4f64          =  52,   //  4 x f64
00103       v8f64          =  53,   //  8 x f64
00104 
00105       FIRST_FP_VECTOR_VALUETYPE = v2f16,
00106       LAST_FP_VECTOR_VALUETYPE = v8f64,
00107 
00108       FIRST_VECTOR_VALUETYPE = v2i1,
00109       LAST_VECTOR_VALUETYPE  = v8f64,
00110 
00111       x86mmx         =  54,   // This is an X86 MMX value
00112 
00113       Glue           =  55,   // This glues nodes together during pre-RA sched
00114 
00115       isVoid         =  56,   // This has no value
00116 
00117       Untyped        =  57,   // This value takes a register, but has
00118                               // unspecified type.  The register class
00119                               // will be determined by the opcode.
00120 
00121       LAST_VALUETYPE =  58,   // This always remains at the end of the list.
00122 
00123       // This is the current maximum for LAST_VALUETYPE.
00124       // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
00125       // This value must be a multiple of 32.
00126       MAX_ALLOWED_VALUETYPE = 64,
00127 
00128       // Metadata - This is MDNode or MDString.
00129       Metadata       = 250,
00130 
00131       // iPTRAny - An int value the size of the pointer of the current
00132       // target to any address space. This must only be used internal to
00133       // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
00134       iPTRAny        = 251,
00135 
00136       // vAny - A vector with any length and element size. This is used
00137       // for intrinsics that have overloadings based on vector types.
00138       // This is only for tblgen's consumption!
00139       vAny           = 252,
00140 
00141       // fAny - Any floating-point or vector floating-point value. This is used
00142       // for intrinsics that have overloadings based on floating-point types.
00143       // This is only for tblgen's consumption!
00144       fAny           = 253,
00145 
00146       // iAny - An integer or vector integer value of any bit width. This is
00147       // used for intrinsics that have overloadings based on integer bit widths.
00148       // This is only for tblgen's consumption!
00149       iAny           = 254,
00150 
00151       // iPTR - An int value the size of the pointer of the current
00152       // target.  This should only be used internal to tblgen!
00153       iPTR           = 255
00154     };
00155 
00156     SimpleValueType SimpleTy;
00157 
00158     MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
00159     MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
00160 
00161     bool operator>(const MVT& S)  const { return SimpleTy >  S.SimpleTy; }
00162     bool operator<(const MVT& S)  const { return SimpleTy <  S.SimpleTy; }
00163     bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
00164     bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
00165     bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
00166     bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
00167 
00168     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
00169     bool isFloatingPoint() const {
00170       return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
00171                SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
00172               (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
00173                SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
00174     }
00175 
00176     /// isInteger - Return true if this is an integer, or a vector integer type.
00177     bool isInteger() const {
00178       return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
00179                SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
00180               (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
00181                SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
00182     }
00183 
00184     /// isVector - Return true if this is a vector value type.
00185     bool isVector() const {
00186       return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
00187               SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
00188     }
00189 
00190     /// is16BitVector - Return true if this is a 16-bit vector type.
00191     bool is16BitVector() const {
00192       return (SimpleTy == MVT::v2i8  || SimpleTy == MVT::v1i16 ||
00193               SimpleTy == MVT::v16i1);
00194     }
00195 
00196     /// is32BitVector - Return true if this is a 32-bit vector type.
00197     bool is32BitVector() const {
00198       return (SimpleTy == MVT::v4i8  || SimpleTy == MVT::v2i16 ||
00199               SimpleTy == MVT::v1i32 || SimpleTy == MVT::v2f16 ||
00200               SimpleTy == MVT::v1f32);
00201     }
00202 
00203     /// is64BitVector - Return true if this is a 64-bit vector type.
00204     bool is64BitVector() const {
00205       return (SimpleTy == MVT::v8i8  || SimpleTy == MVT::v4i16 ||
00206               SimpleTy == MVT::v2i32 || SimpleTy == MVT::v1i64 ||
00207               SimpleTy == MVT::v4f16 || SimpleTy == MVT::v2f32 ||
00208               SimpleTy == MVT::v1f64);
00209     }
00210 
00211     /// is128BitVector - Return true if this is a 128-bit vector type.
00212     bool is128BitVector() const {
00213       return (SimpleTy == MVT::v16i8 || SimpleTy == MVT::v8i16 ||
00214               SimpleTy == MVT::v4i32 || SimpleTy == MVT::v2i64 ||
00215               SimpleTy == MVT::v8f16 || SimpleTy == MVT::v4f32 ||
00216               SimpleTy == MVT::v2f64);
00217     }
00218 
00219     /// is256BitVector - Return true if this is a 256-bit vector type.
00220     bool is256BitVector() const {
00221       return (SimpleTy == MVT::v8f32 || SimpleTy == MVT::v4f64  ||
00222               SimpleTy == MVT::v32i8 || SimpleTy == MVT::v16i16 ||
00223               SimpleTy == MVT::v8i32 || SimpleTy == MVT::v4i64);
00224     }
00225 
00226     /// is512BitVector - Return true if this is a 512-bit vector type.
00227     bool is512BitVector() const {
00228       return (SimpleTy == MVT::v8f64 || SimpleTy == MVT::v16f32 ||
00229               SimpleTy == MVT::v64i8 || SimpleTy == MVT::v32i16 ||
00230               SimpleTy == MVT::v8i64 || SimpleTy == MVT::v16i32);
00231     }
00232 
00233     /// is1024BitVector - Return true if this is a 1024-bit vector type.
00234     bool is1024BitVector() const {
00235       return (SimpleTy == MVT::v16i64);
00236     }
00237 
00238     /// isOverloaded - Return true if this is an overloaded type for TableGen.
00239     bool isOverloaded() const {
00240       return (SimpleTy==MVT::iAny || SimpleTy==MVT::fAny ||
00241               SimpleTy==MVT::vAny || SimpleTy==MVT::iPTRAny);
00242     }
00243 
00244     /// isPow2VectorType - Returns true if the given vector is a power of 2.
00245     bool isPow2VectorType() const {
00246       unsigned NElts = getVectorNumElements();
00247       return !(NElts & (NElts - 1));
00248     }
00249 
00250     /// getPow2VectorType - Widens the length of the given vector MVT up to
00251     /// the nearest power of 2 and returns that type.
00252     MVT getPow2VectorType() const {
00253       if (isPow2VectorType())
00254         return *this;
00255 
00256       unsigned NElts = getVectorNumElements();
00257       unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
00258       return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
00259     }
00260 
00261     /// getScalarType - If this is a vector type, return the element type,
00262     /// otherwise return this.
00263     MVT getScalarType() const {
00264       return isVector() ? getVectorElementType() : *this;
00265     }
00266 
00267     MVT getVectorElementType() const {
00268       switch (SimpleTy) {
00269       default:
00270         llvm_unreachable("Not a vector MVT!");
00271       case v2i1 :
00272       case v4i1 :
00273       case v8i1 :
00274       case v16i1 :
00275       case v32i1 :
00276       case v64i1: return i1;
00277       case v1i8 :
00278       case v2i8 :
00279       case v4i8 :
00280       case v8i8 :
00281       case v16i8:
00282       case v32i8:
00283       case v64i8: return i8;
00284       case v1i16:
00285       case v2i16:
00286       case v4i16:
00287       case v8i16:
00288       case v16i16:
00289       case v32i16: return i16;
00290       case v1i32:
00291       case v2i32:
00292       case v4i32:
00293       case v8i32:
00294       case v16i32: return i32;
00295       case v1i64:
00296       case v2i64:
00297       case v4i64:
00298       case v8i64:
00299       case v16i64: return i64;
00300       case v2f16:
00301       case v4f16:
00302       case v8f16: return f16;
00303       case v1f32:
00304       case v2f32:
00305       case v4f32:
00306       case v8f32:
00307       case v16f32: return f32;
00308       case v1f64:
00309       case v2f64:
00310       case v4f64:
00311       case v8f64: return f64;
00312       }
00313     }
00314 
00315     unsigned getVectorNumElements() const {
00316       switch (SimpleTy) {
00317       default:
00318         llvm_unreachable("Not a vector MVT!");
00319       case v32i1:
00320       case v32i8:
00321       case v32i16: return 32;
00322       case v64i1:
00323       case v64i8: return 64;
00324       case v16i1:
00325       case v16i8:
00326       case v16i16:
00327       case v16i32:
00328       case v16i64:
00329       case v16f32: return 16;
00330       case v8i1 :
00331       case v8i8 :
00332       case v8i16:
00333       case v8i32:
00334       case v8i64:
00335       case v8f16:
00336       case v8f32:
00337       case v8f64: return 8;
00338       case v4i1:
00339       case v4i8:
00340       case v4i16:
00341       case v4i32:
00342       case v4i64:
00343       case v4f16:
00344       case v4f32:
00345       case v4f64: return 4;
00346       case v2i1:
00347       case v2i8:
00348       case v2i16:
00349       case v2i32:
00350       case v2i64:
00351       case v2f16:
00352       case v2f32:
00353       case v2f64: return 2;
00354       case v1i8:
00355       case v1i16:
00356       case v1i32:
00357       case v1i64:
00358       case v1f32:
00359       case v1f64: return 1;
00360       }
00361     }
00362 
00363     unsigned getSizeInBits() const {
00364       switch (SimpleTy) {
00365       default:
00366         llvm_unreachable("getSizeInBits called on extended MVT.");
00367       case Other:
00368         llvm_unreachable("Value type is non-standard value, Other.");
00369       case iPTR:
00370         llvm_unreachable("Value type size is target-dependent. Ask TLI.");
00371       case iPTRAny:
00372       case iAny:
00373       case fAny:
00374       case vAny:
00375         llvm_unreachable("Value type is overloaded.");
00376       case Metadata:
00377         llvm_unreachable("Value type is metadata.");
00378       case i1  :  return 1;
00379       case v2i1:  return 2;
00380       case v4i1:  return 4;
00381       case i8  :
00382       case v1i8:
00383       case v8i1: return 8;
00384       case i16 :
00385       case f16:
00386       case v16i1:
00387       case v2i8:
00388       case v1i16: return 16;
00389       case f32 :
00390       case i32 :
00391       case v32i1:
00392       case v4i8:
00393       case v2i16:
00394       case v2f16:
00395       case v1f32:
00396       case v1i32: return 32;
00397       case x86mmx:
00398       case f64 :
00399       case i64 :
00400       case v64i1:
00401       case v8i8:
00402       case v4i16:
00403       case v2i32:
00404       case v1i64:
00405       case v4f16:
00406       case v2f32:
00407       case v1f64: return 64;
00408       case f80 :  return 80;
00409       case f128:
00410       case ppcf128:
00411       case i128:
00412       case v16i8:
00413       case v8i16:
00414       case v4i32:
00415       case v2i64:
00416       case v8f16:
00417       case v4f32:
00418       case v2f64: return 128;
00419       case v32i8:
00420       case v16i16:
00421       case v8i32:
00422       case v4i64:
00423       case v8f32:
00424       case v4f64: return 256;
00425       case v64i8:
00426       case v32i16:
00427       case v16i32:
00428       case v8i64:
00429       case v16f32:
00430       case v8f64: return 512;
00431       case v16i64:return 1024;
00432       }
00433     }
00434 
00435     unsigned getScalarSizeInBits() const {
00436       return getScalarType().getSizeInBits();
00437     }
00438 
00439     /// getStoreSize - Return the number of bytes overwritten by a store
00440     /// of the specified value type.
00441     unsigned getStoreSize() const {
00442       return (getSizeInBits() + 7) / 8;
00443     }
00444 
00445     /// getStoreSizeInBits - Return the number of bits overwritten by a store
00446     /// of the specified value type.
00447     unsigned getStoreSizeInBits() const {
00448       return getStoreSize() * 8;
00449     }
00450 
00451     /// Return true if this has more bits than VT.
00452     bool bitsGT(MVT VT) const {
00453       return getSizeInBits() > VT.getSizeInBits();
00454     }
00455 
00456     /// Return true if this has no less bits than VT.
00457     bool bitsGE(MVT VT) const {
00458       return getSizeInBits() >= VT.getSizeInBits();
00459     }
00460 
00461     /// Return true if this has less bits than VT.
00462     bool bitsLT(MVT VT) const {
00463       return getSizeInBits() < VT.getSizeInBits();
00464     }
00465 
00466     /// Return true if this has no more bits than VT.
00467     bool bitsLE(MVT VT) const {
00468       return getSizeInBits() <= VT.getSizeInBits();
00469     }
00470 
00471 
00472     static MVT getFloatingPointVT(unsigned BitWidth) {
00473       switch (BitWidth) {
00474       default:
00475         llvm_unreachable("Bad bit width!");
00476       case 16:
00477         return MVT::f16;
00478       case 32:
00479         return MVT::f32;
00480       case 64:
00481         return MVT::f64;
00482       case 80:
00483         return MVT::f80;
00484       case 128:
00485         return MVT::f128;
00486       }
00487     }
00488 
00489     static MVT getIntegerVT(unsigned BitWidth) {
00490       switch (BitWidth) {
00491       default:
00492         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
00493       case 1:
00494         return MVT::i1;
00495       case 8:
00496         return MVT::i8;
00497       case 16:
00498         return MVT::i16;
00499       case 32:
00500         return MVT::i32;
00501       case 64:
00502         return MVT::i64;
00503       case 128:
00504         return MVT::i128;
00505       }
00506     }
00507 
00508     static MVT getVectorVT(MVT VT, unsigned NumElements) {
00509       switch (VT.SimpleTy) {
00510       default:
00511         break;
00512       case MVT::i1:
00513         if (NumElements == 2)  return MVT::v2i1;
00514         if (NumElements == 4)  return MVT::v4i1;
00515         if (NumElements == 8)  return MVT::v8i1;
00516         if (NumElements == 16) return MVT::v16i1;
00517         if (NumElements == 32) return MVT::v32i1;
00518         if (NumElements == 64) return MVT::v64i1;
00519         break;
00520       case MVT::i8:
00521         if (NumElements == 1)  return MVT::v1i8;
00522         if (NumElements == 2)  return MVT::v2i8;
00523         if (NumElements == 4)  return MVT::v4i8;
00524         if (NumElements == 8)  return MVT::v8i8;
00525         if (NumElements == 16) return MVT::v16i8;
00526         if (NumElements == 32) return MVT::v32i8;
00527         if (NumElements == 64) return MVT::v64i8;
00528         break;
00529       case MVT::i16:
00530         if (NumElements == 1)  return MVT::v1i16;
00531         if (NumElements == 2)  return MVT::v2i16;
00532         if (NumElements == 4)  return MVT::v4i16;
00533         if (NumElements == 8)  return MVT::v8i16;
00534         if (NumElements == 16) return MVT::v16i16;
00535         if (NumElements == 32) return MVT::v32i16;
00536         break;
00537       case MVT::i32:
00538         if (NumElements == 1)  return MVT::v1i32;
00539         if (NumElements == 2)  return MVT::v2i32;
00540         if (NumElements == 4)  return MVT::v4i32;
00541         if (NumElements == 8)  return MVT::v8i32;
00542         if (NumElements == 16) return MVT::v16i32;
00543         break;
00544       case MVT::i64:
00545         if (NumElements == 1)  return MVT::v1i64;
00546         if (NumElements == 2)  return MVT::v2i64;
00547         if (NumElements == 4)  return MVT::v4i64;
00548         if (NumElements == 8)  return MVT::v8i64;
00549         if (NumElements == 16) return MVT::v16i64;
00550         break;
00551       case MVT::f16:
00552         if (NumElements == 2)  return MVT::v2f16;
00553         if (NumElements == 4)  return MVT::v4f16;
00554         if (NumElements == 8)  return MVT::v8f16;
00555         break;
00556       case MVT::f32:
00557         if (NumElements == 1)  return MVT::v1f32;
00558         if (NumElements == 2)  return MVT::v2f32;
00559         if (NumElements == 4)  return MVT::v4f32;
00560         if (NumElements == 8)  return MVT::v8f32;
00561         if (NumElements == 16) return MVT::v16f32;
00562         break;
00563       case MVT::f64:
00564         if (NumElements == 1)  return MVT::v1f64;
00565         if (NumElements == 2)  return MVT::v2f64;
00566         if (NumElements == 4)  return MVT::v4f64;
00567         if (NumElements == 8)  return MVT::v8f64;
00568         break;
00569       }
00570       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
00571     }
00572 
00573     /// Return the value type corresponding to the specified type.  This returns
00574     /// all pointers as iPTR.  If HandleUnknown is true, unknown types are
00575     /// returned as Other, otherwise they are invalid.
00576     static MVT getVT(Type *Ty, bool HandleUnknown = false);
00577 
00578   };
00579 
00580 } // End llvm namespace
00581 
00582 #endif