LLVM API Documentation

ValueTypes.h
Go to the documentation of this file.
00001 //===- CodeGen/ValueTypes.h - Low-Level Target independ. 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 low-level target independent types which various
00011 // values in the code generator are.  This allows the target specific behavior
00012 // of instructions to be described to target independent passes.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_VALUETYPES_H
00017 #define LLVM_CODEGEN_VALUETYPES_H
00018 
00019 #include "llvm/CodeGen/MachineValueType.h"
00020 #include <cassert>
00021 #include <string>
00022 
00023 namespace llvm {
00024 
00025   class LLVMContext;
00026   class Type;
00027 
00028   /// EVT - Extended Value Type.  Capable of holding value types which are not
00029   /// native for any processor (such as the i12345 type), as well as the types
00030   /// a MVT can represent.
00031   struct EVT {
00032   private:
00033     MVT V;
00034     Type *LLVMTy;
00035 
00036   public:
00037     EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
00038             LLVMTy(nullptr) {}
00039     EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) { }
00040     EVT(MVT S) : V(S), LLVMTy(nullptr) {}
00041 
00042     bool operator==(EVT VT) const {
00043       return !(*this != VT);
00044     }
00045     bool operator!=(EVT VT) const {
00046       if (V.SimpleTy != VT.V.SimpleTy)
00047         return true;
00048       if (V.SimpleTy < 0)
00049         return LLVMTy != VT.LLVMTy;
00050       return false;
00051     }
00052 
00053     /// getFloatingPointVT - Returns the EVT that represents a floating point
00054     /// type with the given number of bits.  There are two floating point types
00055     /// with 128 bits - this returns f128 rather than ppcf128.
00056     static EVT getFloatingPointVT(unsigned BitWidth) {
00057       return MVT::getFloatingPointVT(BitWidth);
00058     }
00059 
00060     /// getIntegerVT - Returns the EVT that represents an integer with the given
00061     /// number of bits.
00062     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
00063       MVT M = MVT::getIntegerVT(BitWidth);
00064       if (M.SimpleTy >= 0)
00065         return M;
00066       return getExtendedIntegerVT(Context, BitWidth);
00067     }
00068 
00069     /// getVectorVT - Returns the EVT that represents a vector NumElements in
00070     /// length, where each element is of type VT.
00071     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
00072       MVT M = MVT::getVectorVT(VT.V, NumElements);
00073       if (M.SimpleTy >= 0)
00074         return M;
00075       return getExtendedVectorVT(Context, VT, NumElements);
00076     }
00077 
00078     /// changeVectorElementTypeToInteger - Return a vector with the same number
00079     /// of elements as this vector, but with the element type converted to an
00080     /// integer type with the same bitwidth.
00081     EVT changeVectorElementTypeToInteger() const {
00082       if (!isSimple())
00083         return changeExtendedVectorElementTypeToInteger();
00084       MVT EltTy = getSimpleVT().getVectorElementType();
00085       unsigned BitWidth = EltTy.getSizeInBits();
00086       MVT IntTy = MVT::getIntegerVT(BitWidth);
00087       MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
00088       assert(VecTy.SimpleTy >= 0 &&
00089              "Simple vector VT not representable by simple integer vector VT!");
00090       return VecTy;
00091     }
00092 
00093     /// isSimple - Test if the given EVT is simple (as opposed to being
00094     /// extended).
00095     bool isSimple() const {
00096       return V.SimpleTy >= 0;
00097     }
00098 
00099     /// isExtended - Test if the given EVT is extended (as opposed to
00100     /// being simple).
00101     bool isExtended() const {
00102       return !isSimple();
00103     }
00104 
00105     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
00106     bool isFloatingPoint() const {
00107       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
00108     }
00109 
00110     /// isInteger - Return true if this is an integer, or a vector integer type.
00111     bool isInteger() const {
00112       return isSimple() ? V.isInteger() : isExtendedInteger();
00113     }
00114 
00115     /// isVector - Return true if this is a vector value type.
00116     bool isVector() const {
00117       return isSimple() ? V.isVector() : isExtendedVector();
00118     }
00119 
00120     /// is16BitVector - Return true if this is a 16-bit vector type.
00121     bool is16BitVector() const {
00122       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
00123     }
00124 
00125     /// is32BitVector - Return true if this is a 32-bit vector type.
00126     bool is32BitVector() const {
00127       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
00128     }
00129 
00130     /// is64BitVector - Return true if this is a 64-bit vector type.
00131     bool is64BitVector() const {
00132       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
00133     }
00134 
00135     /// is128BitVector - Return true if this is a 128-bit vector type.
00136     bool is128BitVector() const {
00137       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
00138     }
00139 
00140     /// is256BitVector - Return true if this is a 256-bit vector type.
00141     bool is256BitVector() const {
00142       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
00143     }
00144 
00145     /// is512BitVector - Return true if this is a 512-bit vector type.
00146     bool is512BitVector() const {
00147       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
00148     }
00149 
00150     /// is1024BitVector - Return true if this is a 1024-bit vector type.
00151     bool is1024BitVector() const {
00152       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
00153     }
00154 
00155     /// isOverloaded - Return true if this is an overloaded type for TableGen.
00156     bool isOverloaded() const {
00157       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
00158     }
00159 
00160     /// isByteSized - Return true if the bit size is a multiple of 8.
00161     bool isByteSized() const {
00162       return (getSizeInBits() & 7) == 0;
00163     }
00164 
00165     /// isRound - Return true if the size is a power-of-two number of bytes.
00166     bool isRound() const {
00167       unsigned BitSize = getSizeInBits();
00168       return BitSize >= 8 && !(BitSize & (BitSize - 1));
00169     }
00170 
00171     /// bitsEq - Return true if this has the same number of bits as VT.
00172     bool bitsEq(EVT VT) const {
00173       if (EVT::operator==(VT)) return true;
00174       return getSizeInBits() == VT.getSizeInBits();
00175     }
00176 
00177     /// bitsGT - Return true if this has more bits than VT.
00178     bool bitsGT(EVT VT) const {
00179       if (EVT::operator==(VT)) return false;
00180       return getSizeInBits() > VT.getSizeInBits();
00181     }
00182 
00183     /// bitsGE - Return true if this has no less bits than VT.
00184     bool bitsGE(EVT VT) const {
00185       if (EVT::operator==(VT)) return true;
00186       return getSizeInBits() >= VT.getSizeInBits();
00187     }
00188 
00189     /// bitsLT - Return true if this has less bits than VT.
00190     bool bitsLT(EVT VT) const {
00191       if (EVT::operator==(VT)) return false;
00192       return getSizeInBits() < VT.getSizeInBits();
00193     }
00194 
00195     /// bitsLE - Return true if this has no more bits than VT.
00196     bool bitsLE(EVT VT) const {
00197       if (EVT::operator==(VT)) return true;
00198       return getSizeInBits() <= VT.getSizeInBits();
00199     }
00200 
00201 
00202     /// getSimpleVT - Return the SimpleValueType held in the specified
00203     /// simple EVT.
00204     MVT getSimpleVT() const {
00205       assert(isSimple() && "Expected a SimpleValueType!");
00206       return V;
00207     }
00208 
00209     /// getScalarType - If this is a vector type, return the element type,
00210     /// otherwise return this.
00211     EVT getScalarType() const {
00212       return isVector() ? getVectorElementType() : *this;
00213     }
00214 
00215     /// getVectorElementType - Given a vector type, return the type of
00216     /// each element.
00217     EVT getVectorElementType() const {
00218       assert(isVector() && "Invalid vector type!");
00219       if (isSimple())
00220         return V.getVectorElementType();
00221       return getExtendedVectorElementType();
00222     }
00223 
00224     /// getVectorNumElements - Given a vector type, return the number of
00225     /// elements it contains.
00226     unsigned getVectorNumElements() const {
00227       assert(isVector() && "Invalid vector type!");
00228       if (isSimple())
00229         return V.getVectorNumElements();
00230       return getExtendedVectorNumElements();
00231     }
00232 
00233     /// getSizeInBits - Return the size of the specified value type in bits.
00234     unsigned getSizeInBits() const {
00235       if (isSimple())
00236         return V.getSizeInBits();
00237       return getExtendedSizeInBits();
00238     }
00239 
00240     unsigned getScalarSizeInBits() const {
00241       return getScalarType().getSizeInBits();
00242     }
00243 
00244     /// getStoreSize - Return the number of bytes overwritten by a store
00245     /// of the specified value type.
00246     unsigned getStoreSize() const {
00247       return (getSizeInBits() + 7) / 8;
00248     }
00249 
00250     /// getStoreSizeInBits - Return the number of bits overwritten by a store
00251     /// of the specified value type.
00252     unsigned getStoreSizeInBits() const {
00253       return getStoreSize() * 8;
00254     }
00255 
00256     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
00257     /// to the nearest power of two (and at least to eight), and returns the
00258     /// integer EVT with that number of bits.
00259     EVT getRoundIntegerType(LLVMContext &Context) const {
00260       assert(isInteger() && !isVector() && "Invalid integer type!");
00261       unsigned BitWidth = getSizeInBits();
00262       if (BitWidth <= 8)
00263         return EVT(MVT::i8);
00264       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
00265     }
00266 
00267     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
00268     /// greater than or equal to half the width of this EVT. If no simple
00269     /// value type can be found, an extended integer value type of half the
00270     /// size (rounded up) is returned.
00271     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
00272       assert(isInteger() && !isVector() && "Invalid integer type!");
00273       unsigned EVTSize = getSizeInBits();
00274       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
00275           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
00276         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
00277         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
00278           return HalfVT;
00279       }
00280       return getIntegerVT(Context, (EVTSize + 1) / 2);
00281     }
00282 
00283     /// \brief Return a VT for an integer vector type with the size of the
00284     /// elements doubled. The typed returned may be an extended type.
00285     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
00286       EVT EltVT = getVectorElementType();
00287       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
00288       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
00289     }
00290 
00291     /// isPow2VectorType - Returns true if the given vector is a power of 2.
00292     bool isPow2VectorType() const {
00293       unsigned NElts = getVectorNumElements();
00294       return !(NElts & (NElts - 1));
00295     }
00296 
00297     /// getPow2VectorType - Widens the length of the given vector EVT up to
00298     /// the nearest power of 2 and returns that type.
00299     EVT getPow2VectorType(LLVMContext &Context) const {
00300       if (!isPow2VectorType()) {
00301         unsigned NElts = getVectorNumElements();
00302         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
00303         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
00304       }
00305       else {
00306         return *this;
00307       }
00308     }
00309 
00310     /// getEVTString - This function returns value type as a string,
00311     /// e.g. "i32".
00312     std::string getEVTString() const;
00313 
00314     /// getTypeForEVT - This method returns an LLVM type corresponding to the
00315     /// specified EVT.  For integer types, this returns an unsigned type.  Note
00316     /// that this will abort for types that cannot be represented.
00317     Type *getTypeForEVT(LLVMContext &Context) const;
00318 
00319     /// getEVT - Return the value type corresponding to the specified type.
00320     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
00321     /// types are returned as Other, otherwise they are invalid.
00322     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
00323 
00324     intptr_t getRawBits() const {
00325       if (isSimple())
00326         return V.SimpleTy;
00327       else
00328         return (intptr_t)(LLVMTy);
00329     }
00330 
00331     /// compareRawBits - A meaningless but well-behaved order, useful for
00332     /// constructing containers.
00333     struct compareRawBits {
00334       bool operator()(EVT L, EVT R) const {
00335         if (L.V.SimpleTy == R.V.SimpleTy)
00336           return L.LLVMTy < R.LLVMTy;
00337         else
00338           return L.V.SimpleTy < R.V.SimpleTy;
00339       }
00340     };
00341 
00342   private:
00343     // Methods for handling the Extended-type case in functions above.
00344     // These are all out-of-line to prevent users of this header file
00345     // from having a dependency on Type.h.
00346     EVT changeExtendedVectorElementTypeToInteger() const;
00347     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
00348     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
00349                                    unsigned NumElements);
00350     bool isExtendedFloatingPoint() const LLVM_READONLY;
00351     bool isExtendedInteger() const LLVM_READONLY;
00352     bool isExtendedVector() const LLVM_READONLY;
00353     bool isExtended16BitVector() const LLVM_READONLY;
00354     bool isExtended32BitVector() const LLVM_READONLY;
00355     bool isExtended64BitVector() const LLVM_READONLY;
00356     bool isExtended128BitVector() const LLVM_READONLY;
00357     bool isExtended256BitVector() const LLVM_READONLY;
00358     bool isExtended512BitVector() const LLVM_READONLY;
00359     bool isExtended1024BitVector() const LLVM_READONLY;
00360     EVT getExtendedVectorElementType() const;
00361     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
00362     unsigned getExtendedSizeInBits() const;
00363   };
00364 
00365 } // End llvm namespace
00366 
00367 #endif