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