LLVM  mainline
ValueTypes.cpp
Go to the documentation of this file.
00001 //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===//
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 implements methods in the CodeGen/ValueTypes.h header.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/ValueTypes.h"
00015 #include "llvm/ADT/StringExtras.h"
00016 #include "llvm/IR/DerivedTypes.h"
00017 #include "llvm/IR/LLVMContext.h"
00018 #include "llvm/IR/Type.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 using namespace llvm;
00021 
00022 EVT EVT::changeExtendedTypeToInteger() const {
00023   LLVMContext &Context = LLVMTy->getContext();
00024   return getIntegerVT(Context, getSizeInBits());
00025 }
00026 
00027 EVT EVT::changeExtendedVectorElementTypeToInteger() const {
00028   LLVMContext &Context = LLVMTy->getContext();
00029   EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits());
00030   return getVectorVT(Context, IntTy, getVectorNumElements());
00031 }
00032 
00033 EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) {
00034   EVT VT;
00035   VT.LLVMTy = IntegerType::get(Context, BitWidth);
00036   assert(VT.isExtended() && "Type is not extended!");
00037   return VT;
00038 }
00039 
00040 EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT,
00041                              unsigned NumElements) {
00042   EVT ResultVT;
00043   ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), NumElements);
00044   assert(ResultVT.isExtended() && "Type is not extended!");
00045   return ResultVT;
00046 }
00047 
00048 bool EVT::isExtendedFloatingPoint() const {
00049   assert(isExtended() && "Type is not extended!");
00050   return LLVMTy->isFPOrFPVectorTy();
00051 }
00052 
00053 bool EVT::isExtendedInteger() const {
00054   assert(isExtended() && "Type is not extended!");
00055   return LLVMTy->isIntOrIntVectorTy();
00056 }
00057 
00058 bool EVT::isExtendedVector() const {
00059   assert(isExtended() && "Type is not extended!");
00060   return LLVMTy->isVectorTy();
00061 }
00062 
00063 bool EVT::isExtended16BitVector() const {
00064   return isExtendedVector() && getExtendedSizeInBits() == 16;
00065 }
00066 
00067 bool EVT::isExtended32BitVector() const {
00068   return isExtendedVector() && getExtendedSizeInBits() == 32;
00069 }
00070 
00071 bool EVT::isExtended64BitVector() const {
00072   return isExtendedVector() && getExtendedSizeInBits() == 64;
00073 }
00074 
00075 bool EVT::isExtended128BitVector() const {
00076   return isExtendedVector() && getExtendedSizeInBits() == 128;
00077 }
00078 
00079 bool EVT::isExtended256BitVector() const {
00080   return isExtendedVector() && getExtendedSizeInBits() == 256;
00081 }
00082 
00083 bool EVT::isExtended512BitVector() const {
00084   return isExtendedVector() && getExtendedSizeInBits() == 512;
00085 }
00086 
00087 bool EVT::isExtended1024BitVector() const {
00088   return isExtendedVector() && getExtendedSizeInBits() == 1024;
00089 }
00090 
00091 bool EVT::isExtended2048BitVector() const {
00092   return isExtendedVector() && getExtendedSizeInBits() == 2048;
00093 }
00094 
00095 EVT EVT::getExtendedVectorElementType() const {
00096   assert(isExtended() && "Type is not extended!");
00097   return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
00098 }
00099 
00100 unsigned EVT::getExtendedVectorNumElements() const {
00101   assert(isExtended() && "Type is not extended!");
00102   return cast<VectorType>(LLVMTy)->getNumElements();
00103 }
00104 
00105 unsigned EVT::getExtendedSizeInBits() const {
00106   assert(isExtended() && "Type is not extended!");
00107   if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
00108     return ITy->getBitWidth();
00109   if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
00110     return VTy->getBitWidth();
00111   llvm_unreachable("Unrecognized extended type!");
00112 }
00113 
00114 /// getEVTString - This function returns value type as a string, e.g. "i32".
00115 std::string EVT::getEVTString() const {
00116   switch (V.SimpleTy) {
00117   default:
00118     if (isVector())
00119       return "v" + utostr(getVectorNumElements()) +
00120              getVectorElementType().getEVTString();
00121     if (isInteger())
00122       return "i" + utostr(getSizeInBits());
00123     llvm_unreachable("Invalid EVT!");
00124   case MVT::i1:      return "i1";
00125   case MVT::i8:      return "i8";
00126   case MVT::i16:     return "i16";
00127   case MVT::i32:     return "i32";
00128   case MVT::i64:     return "i64";
00129   case MVT::i128:    return "i128";
00130   case MVT::f16:     return "f16";
00131   case MVT::f32:     return "f32";
00132   case MVT::f64:     return "f64";
00133   case MVT::f80:     return "f80";
00134   case MVT::f128:    return "f128";
00135   case MVT::ppcf128: return "ppcf128";
00136   case MVT::isVoid:  return "isVoid";
00137   case MVT::Other:   return "ch";
00138   case MVT::Glue:    return "glue";
00139   case MVT::x86mmx:  return "x86mmx";
00140   case MVT::v2i1:    return "v2i1";
00141   case MVT::v4i1:    return "v4i1";
00142   case MVT::v8i1:    return "v8i1";
00143   case MVT::v16i1:   return "v16i1";
00144   case MVT::v32i1:   return "v32i1";
00145   case MVT::v64i1:   return "v64i1";
00146   case MVT::v512i1:  return "v512i1";
00147   case MVT::v1024i1: return "v1024i1";
00148   case MVT::v1i8:    return "v1i8";
00149   case MVT::v2i8:    return "v2i8";
00150   case MVT::v4i8:    return "v4i8";
00151   case MVT::v8i8:    return "v8i8";
00152   case MVT::v16i8:   return "v16i8";
00153   case MVT::v32i8:   return "v32i8";
00154   case MVT::v64i8:   return "v64i8";
00155   case MVT::v128i8:  return "v128i8";
00156   case MVT::v256i8:  return "v256i8";
00157   case MVT::v1i16:   return "v1i16";
00158   case MVT::v2i16:   return "v2i16";
00159   case MVT::v4i16:   return "v4i16";
00160   case MVT::v8i16:   return "v8i16";
00161   case MVT::v16i16:  return "v16i16";
00162   case MVT::v32i16:  return "v32i16";
00163   case MVT::v64i16:  return "v64i16";
00164   case MVT::v128i16: return "v128i16";
00165   case MVT::v1i32:   return "v1i32";
00166   case MVT::v2i32:   return "v2i32";
00167   case MVT::v4i32:   return "v4i32";
00168   case MVT::v8i32:   return "v8i32";
00169   case MVT::v16i32:  return "v16i32";
00170   case MVT::v32i32:  return "v32i32";
00171   case MVT::v64i32:  return "v64i32";
00172   case MVT::v1i64:   return "v1i64";
00173   case MVT::v2i64:   return "v2i64";
00174   case MVT::v4i64:   return "v4i64";
00175   case MVT::v8i64:   return "v8i64";
00176   case MVT::v16i64:  return "v16i64";
00177   case MVT::v32i64:  return "v32i64";
00178   case MVT::v1i128:  return "v1i128";
00179   case MVT::v1f32:   return "v1f32";
00180   case MVT::v2f32:   return "v2f32";
00181   case MVT::v2f16:   return "v2f16";
00182   case MVT::v4f16:   return "v4f16";
00183   case MVT::v8f16:   return "v8f16";
00184   case MVT::v4f32:   return "v4f32";
00185   case MVT::v8f32:   return "v8f32";
00186   case MVT::v16f32:  return "v16f32";
00187   case MVT::v1f64:   return "v1f64";
00188   case MVT::v2f64:   return "v2f64";
00189   case MVT::v4f64:   return "v4f64";
00190   case MVT::v8f64:   return "v8f64";
00191   case MVT::Metadata:return "Metadata";
00192   case MVT::Untyped: return "Untyped";
00193   }
00194 }
00195 
00196 /// getTypeForEVT - This method returns an LLVM type corresponding to the
00197 /// specified EVT.  For integer types, this returns an unsigned type.  Note
00198 /// that this will abort for types that cannot be represented.
00199 Type *EVT::getTypeForEVT(LLVMContext &Context) const {
00200   switch (V.SimpleTy) {
00201   default:
00202     assert(isExtended() && "Type is not extended!");
00203     return LLVMTy;
00204   case MVT::isVoid:  return Type::getVoidTy(Context);
00205   case MVT::i1:      return Type::getInt1Ty(Context);
00206   case MVT::i8:      return Type::getInt8Ty(Context);
00207   case MVT::i16:     return Type::getInt16Ty(Context);
00208   case MVT::i32:     return Type::getInt32Ty(Context);
00209   case MVT::i64:     return Type::getInt64Ty(Context);
00210   case MVT::i128:    return IntegerType::get(Context, 128);
00211   case MVT::f16:     return Type::getHalfTy(Context);
00212   case MVT::f32:     return Type::getFloatTy(Context);
00213   case MVT::f64:     return Type::getDoubleTy(Context);
00214   case MVT::f80:     return Type::getX86_FP80Ty(Context);
00215   case MVT::f128:    return Type::getFP128Ty(Context);
00216   case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
00217   case MVT::x86mmx:  return Type::getX86_MMXTy(Context);
00218   case MVT::v2i1:    return VectorType::get(Type::getInt1Ty(Context), 2);
00219   case MVT::v4i1:    return VectorType::get(Type::getInt1Ty(Context), 4);
00220   case MVT::v8i1:    return VectorType::get(Type::getInt1Ty(Context), 8);
00221   case MVT::v16i1:   return VectorType::get(Type::getInt1Ty(Context), 16);
00222   case MVT::v32i1:   return VectorType::get(Type::getInt1Ty(Context), 32);
00223   case MVT::v64i1:   return VectorType::get(Type::getInt1Ty(Context), 64);
00224   case MVT::v512i1:  return VectorType::get(Type::getInt1Ty(Context), 512);
00225   case MVT::v1024i1: return VectorType::get(Type::getInt1Ty(Context), 1024);
00226   case MVT::v1i8:    return VectorType::get(Type::getInt8Ty(Context), 1);
00227   case MVT::v2i8:    return VectorType::get(Type::getInt8Ty(Context), 2);
00228   case MVT::v4i8:    return VectorType::get(Type::getInt8Ty(Context), 4);
00229   case MVT::v8i8:    return VectorType::get(Type::getInt8Ty(Context), 8);
00230   case MVT::v16i8:   return VectorType::get(Type::getInt8Ty(Context), 16);
00231   case MVT::v32i8:   return VectorType::get(Type::getInt8Ty(Context), 32);
00232   case MVT::v64i8:   return VectorType::get(Type::getInt8Ty(Context), 64);
00233   case MVT::v128i8:  return VectorType::get(Type::getInt8Ty(Context), 128);
00234   case MVT::v256i8:  return VectorType::get(Type::getInt8Ty(Context), 256);
00235   case MVT::v1i16:   return VectorType::get(Type::getInt16Ty(Context), 1);
00236   case MVT::v2i16:   return VectorType::get(Type::getInt16Ty(Context), 2);
00237   case MVT::v4i16:   return VectorType::get(Type::getInt16Ty(Context), 4);
00238   case MVT::v8i16:   return VectorType::get(Type::getInt16Ty(Context), 8);
00239   case MVT::v16i16:  return VectorType::get(Type::getInt16Ty(Context), 16);
00240   case MVT::v32i16:  return VectorType::get(Type::getInt16Ty(Context), 32);
00241   case MVT::v64i16:  return VectorType::get(Type::getInt16Ty(Context), 64);
00242   case MVT::v128i16: return VectorType::get(Type::getInt16Ty(Context), 128);
00243   case MVT::v1i32:   return VectorType::get(Type::getInt32Ty(Context), 1);
00244   case MVT::v2i32:   return VectorType::get(Type::getInt32Ty(Context), 2);
00245   case MVT::v4i32:   return VectorType::get(Type::getInt32Ty(Context), 4);
00246   case MVT::v8i32:   return VectorType::get(Type::getInt32Ty(Context), 8);
00247   case MVT::v16i32:  return VectorType::get(Type::getInt32Ty(Context), 16);
00248   case MVT::v32i32:  return VectorType::get(Type::getInt32Ty(Context), 32);
00249   case MVT::v64i32:  return VectorType::get(Type::getInt32Ty(Context), 64);
00250   case MVT::v1i64:   return VectorType::get(Type::getInt64Ty(Context), 1);
00251   case MVT::v2i64:   return VectorType::get(Type::getInt64Ty(Context), 2);
00252   case MVT::v4i64:   return VectorType::get(Type::getInt64Ty(Context), 4);
00253   case MVT::v8i64:   return VectorType::get(Type::getInt64Ty(Context), 8);
00254   case MVT::v16i64:  return VectorType::get(Type::getInt64Ty(Context), 16);
00255   case MVT::v32i64:  return VectorType::get(Type::getInt64Ty(Context), 32);
00256   case MVT::v1i128:  return VectorType::get(Type::getInt128Ty(Context), 1);
00257   case MVT::v2f16:   return VectorType::get(Type::getHalfTy(Context), 2);
00258   case MVT::v4f16:   return VectorType::get(Type::getHalfTy(Context), 4);
00259   case MVT::v8f16:   return VectorType::get(Type::getHalfTy(Context), 8);
00260   case MVT::v1f32:   return VectorType::get(Type::getFloatTy(Context), 1);
00261   case MVT::v2f32:   return VectorType::get(Type::getFloatTy(Context), 2);
00262   case MVT::v4f32:   return VectorType::get(Type::getFloatTy(Context), 4);
00263   case MVT::v8f32:   return VectorType::get(Type::getFloatTy(Context), 8);
00264   case MVT::v16f32:   return VectorType::get(Type::getFloatTy(Context), 16);
00265   case MVT::v1f64:   return VectorType::get(Type::getDoubleTy(Context), 1);
00266   case MVT::v2f64:   return VectorType::get(Type::getDoubleTy(Context), 2);
00267   case MVT::v4f64:   return VectorType::get(Type::getDoubleTy(Context), 4); 
00268   case MVT::v8f64:   return VectorType::get(Type::getDoubleTy(Context), 8); 
00269   case MVT::Metadata: return Type::getMetadataTy(Context);
00270  }
00271 }
00272 
00273 /// Return the value type corresponding to the specified type.  This returns all
00274 /// pointers as MVT::iPTR.  If HandleUnknown is true, unknown types are returned
00275 /// as Other, otherwise they are invalid.
00276 MVT MVT::getVT(Type *Ty, bool HandleUnknown){
00277   switch (Ty->getTypeID()) {
00278   default:
00279     if (HandleUnknown) return MVT(MVT::Other);
00280     llvm_unreachable("Unknown type!");
00281   case Type::VoidTyID:
00282     return MVT::isVoid;
00283   case Type::IntegerTyID:
00284     return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
00285   case Type::HalfTyID:      return MVT(MVT::f16);
00286   case Type::FloatTyID:     return MVT(MVT::f32);
00287   case Type::DoubleTyID:    return MVT(MVT::f64);
00288   case Type::X86_FP80TyID:  return MVT(MVT::f80);
00289   case Type::X86_MMXTyID:   return MVT(MVT::x86mmx);
00290   case Type::FP128TyID:     return MVT(MVT::f128);
00291   case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
00292   case Type::PointerTyID:   return MVT(MVT::iPTR);
00293   case Type::VectorTyID: {
00294     VectorType *VTy = cast<VectorType>(Ty);
00295     return getVectorVT(
00296       getVT(VTy->getElementType(), false), VTy->getNumElements());
00297   }
00298   }
00299 }
00300 
00301 /// getEVT - Return the value type corresponding to the specified type.  This
00302 /// returns all pointers as MVT::iPTR.  If HandleUnknown is true, unknown types
00303 /// are returned as Other, otherwise they are invalid.
00304 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
00305   switch (Ty->getTypeID()) {
00306   default:
00307     return MVT::getVT(Ty, HandleUnknown);
00308   case Type::IntegerTyID:
00309     return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
00310   case Type::VectorTyID: {
00311     VectorType *VTy = cast<VectorType>(Ty);
00312     return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false),
00313                        VTy->getNumElements());
00314   }
00315   }
00316 }