1 //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===// 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 implements methods in the CodeGen/ValueTypes.h header. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/ValueTypes.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/IR/DerivedTypes.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/IR/Type.h" 19 #include "llvm/Support/ErrorHandling.h" 20 using namespace llvm; 21 22 EVT EVT::changeExtendedTypeToInteger() const { 23 LLVMContext &Context = LLVMTy->getContext(); 24 return getIntegerVT(Context, getSizeInBits()); 25 } 26 27 EVT EVT::changeExtendedVectorElementTypeToInteger() const { 28 LLVMContext &Context = LLVMTy->getContext(); 29 EVT IntTy = getIntegerVT(Context, getVectorElementType().getSizeInBits()); 30 return getVectorVT(Context, IntTy, getVectorNumElements()); 31 } 32 33 EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) { 34 EVT VT; 35 VT.LLVMTy = IntegerType::get(Context, BitWidth); 36 assert(VT.isExtended() && "Type is not extended!"); 37 return VT; 38 } 39 40 EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, 41 unsigned NumElements) { 42 EVT ResultVT; 43 ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), NumElements); 44 assert(ResultVT.isExtended() && "Type is not extended!"); 45 return ResultVT; 46 } 47 48 bool EVT::isExtendedFloatingPoint() const { 49 assert(isExtended() && "Type is not extended!"); 50 return LLVMTy->isFPOrFPVectorTy(); 51 } 52 53 bool EVT::isExtendedInteger() const { 54 assert(isExtended() && "Type is not extended!"); 55 return LLVMTy->isIntOrIntVectorTy(); 56 } 57 58 bool EVT::isExtendedVector() const { 59 assert(isExtended() && "Type is not extended!"); 60 return LLVMTy->isVectorTy(); 61 } 62 63 bool EVT::isExtended16BitVector() const { 64 return isExtendedVector() && getExtendedSizeInBits() == 16; 65 } 66 67 bool EVT::isExtended32BitVector() const { 68 return isExtendedVector() && getExtendedSizeInBits() == 32; 69 } 70 71 bool EVT::isExtended64BitVector() const { 72 return isExtendedVector() && getExtendedSizeInBits() == 64; 73 } 74 75 bool EVT::isExtended128BitVector() const { 76 return isExtendedVector() && getExtendedSizeInBits() == 128; 77 } 78 79 bool EVT::isExtended256BitVector() const { 80 return isExtendedVector() && getExtendedSizeInBits() == 256; 81 } 82 83 bool EVT::isExtended512BitVector() const { 84 return isExtendedVector() && getExtendedSizeInBits() == 512; 85 } 86 87 bool EVT::isExtended1024BitVector() const { 88 return isExtendedVector() && getExtendedSizeInBits() == 1024; 89 } 90 91 bool EVT::isExtended2048BitVector() const { 92 return isExtendedVector() && getExtendedSizeInBits() == 2048; 93 } 94 95 EVT EVT::getExtendedVectorElementType() const { 96 assert(isExtended() && "Type is not extended!"); 97 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType()); 98 } 99 100 unsigned EVT::getExtendedVectorNumElements() const { 101 assert(isExtended() && "Type is not extended!"); 102 return cast<VectorType>(LLVMTy)->getNumElements(); 103 } 104 105 unsigned EVT::getExtendedSizeInBits() const { 106 assert(isExtended() && "Type is not extended!"); 107 if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy)) 108 return ITy->getBitWidth(); 109 if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy)) 110 return VTy->getBitWidth(); 111 llvm_unreachable("Unrecognized extended type!"); 112 } 113 114 /// getEVTString - This function returns value type as a string, e.g. "i32". 115 std::string EVT::getEVTString() const { 116 switch (V.SimpleTy) { 117 default: 118 if (isVector()) 119 return "v" + utostr(getVectorNumElements()) + 120 getVectorElementType().getEVTString(); 121 if (isInteger()) 122 return "i" + utostr(getSizeInBits()); 123 llvm_unreachable("Invalid EVT!"); 124 case MVT::i1: return "i1"; 125 case MVT::i8: return "i8"; 126 case MVT::i16: return "i16"; 127 case MVT::i32: return "i32"; 128 case MVT::i64: return "i64"; 129 case MVT::i128: return "i128"; 130 case MVT::f16: return "f16"; 131 case MVT::f32: return "f32"; 132 case MVT::f64: return "f64"; 133 case MVT::f80: return "f80"; 134 case MVT::f128: return "f128"; 135 case MVT::ppcf128: return "ppcf128"; 136 case MVT::isVoid: return "isVoid"; 137 case MVT::Other: return "ch"; 138 case MVT::Glue: return "glue"; 139 case MVT::x86mmx: return "x86mmx"; 140 case MVT::v2i1: return "v2i1"; 141 case MVT::v4i1: return "v4i1"; 142 case MVT::v8i1: return "v8i1"; 143 case MVT::v16i1: return "v16i1"; 144 case MVT::v32i1: return "v32i1"; 145 case MVT::v64i1: return "v64i1"; 146 case MVT::v512i1: return "v512i1"; 147 case MVT::v1024i1: return "v1024i1"; 148 case MVT::v1i8: return "v1i8"; 149 case MVT::v2i8: return "v2i8"; 150 case MVT::v4i8: return "v4i8"; 151 case MVT::v8i8: return "v8i8"; 152 case MVT::v16i8: return "v16i8"; 153 case MVT::v32i8: return "v32i8"; 154 case MVT::v64i8: return "v64i8"; 155 case MVT::v128i8: return "v128i8"; 156 case MVT::v256i8: return "v256i8"; 157 case MVT::v1i16: return "v1i16"; 158 case MVT::v2i16: return "v2i16"; 159 case MVT::v4i16: return "v4i16"; 160 case MVT::v8i16: return "v8i16"; 161 case MVT::v16i16: return "v16i16"; 162 case MVT::v32i16: return "v32i16"; 163 case MVT::v64i16: return "v64i16"; 164 case MVT::v128i16: return "v128i16"; 165 case MVT::v1i32: return "v1i32"; 166 case MVT::v2i32: return "v2i32"; 167 case MVT::v4i32: return "v4i32"; 168 case MVT::v8i32: return "v8i32"; 169 case MVT::v16i32: return "v16i32"; 170 case MVT::v32i32: return "v32i32"; 171 case MVT::v64i32: return "v64i32"; 172 case MVT::v1i64: return "v1i64"; 173 case MVT::v2i64: return "v2i64"; 174 case MVT::v4i64: return "v4i64"; 175 case MVT::v8i64: return "v8i64"; 176 case MVT::v16i64: return "v16i64"; 177 case MVT::v32i64: return "v32i64"; 178 case MVT::v1i128: return "v1i128"; 179 case MVT::v1f32: return "v1f32"; 180 case MVT::v2f32: return "v2f32"; 181 case MVT::v2f16: return "v2f16"; 182 case MVT::v4f16: return "v4f16"; 183 case MVT::v8f16: return "v8f16"; 184 case MVT::v4f32: return "v4f32"; 185 case MVT::v8f32: return "v8f32"; 186 case MVT::v16f32: return "v16f32"; 187 case MVT::v1f64: return "v1f64"; 188 case MVT::v2f64: return "v2f64"; 189 case MVT::v4f64: return "v4f64"; 190 case MVT::v8f64: return "v8f64"; 191 case MVT::Metadata:return "Metadata"; 192 case MVT::Untyped: return "Untyped"; 193 } 194 } 195 196 /// getTypeForEVT - This method returns an LLVM type corresponding to the 197 /// specified EVT. For integer types, this returns an unsigned type. Note 198 /// that this will abort for types that cannot be represented. 199 Type *EVT::getTypeForEVT(LLVMContext &Context) const { 200 switch (V.SimpleTy) { 201 default: 202 assert(isExtended() && "Type is not extended!"); 203 return LLVMTy; 204 case MVT::isVoid: return Type::getVoidTy(Context); 205 case MVT::i1: return Type::getInt1Ty(Context); 206 case MVT::i8: return Type::getInt8Ty(Context); 207 case MVT::i16: return Type::getInt16Ty(Context); 208 case MVT::i32: return Type::getInt32Ty(Context); 209 case MVT::i64: return Type::getInt64Ty(Context); 210 case MVT::i128: return IntegerType::get(Context, 128); 211 case MVT::f16: return Type::getHalfTy(Context); 212 case MVT::f32: return Type::getFloatTy(Context); 213 case MVT::f64: return Type::getDoubleTy(Context); 214 case MVT::f80: return Type::getX86_FP80Ty(Context); 215 case MVT::f128: return Type::getFP128Ty(Context); 216 case MVT::ppcf128: return Type::getPPC_FP128Ty(Context); 217 case MVT::x86mmx: return Type::getX86_MMXTy(Context); 218 case MVT::v2i1: return VectorType::get(Type::getInt1Ty(Context), 2); 219 case MVT::v4i1: return VectorType::get(Type::getInt1Ty(Context), 4); 220 case MVT::v8i1: return VectorType::get(Type::getInt1Ty(Context), 8); 221 case MVT::v16i1: return VectorType::get(Type::getInt1Ty(Context), 16); 222 case MVT::v32i1: return VectorType::get(Type::getInt1Ty(Context), 32); 223 case MVT::v64i1: return VectorType::get(Type::getInt1Ty(Context), 64); 224 case MVT::v512i1: return VectorType::get(Type::getInt1Ty(Context), 512); 225 case MVT::v1024i1: return VectorType::get(Type::getInt1Ty(Context), 1024); 226 case MVT::v1i8: return VectorType::get(Type::getInt8Ty(Context), 1); 227 case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2); 228 case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4); 229 case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8); 230 case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16); 231 case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32); 232 case MVT::v64i8: return VectorType::get(Type::getInt8Ty(Context), 64); 233 case MVT::v128i8: return VectorType::get(Type::getInt8Ty(Context), 128); 234 case MVT::v256i8: return VectorType::get(Type::getInt8Ty(Context), 256); 235 case MVT::v1i16: return VectorType::get(Type::getInt16Ty(Context), 1); 236 case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2); 237 case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4); 238 case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8); 239 case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16); 240 case MVT::v32i16: return VectorType::get(Type::getInt16Ty(Context), 32); 241 case MVT::v64i16: return VectorType::get(Type::getInt16Ty(Context), 64); 242 case MVT::v128i16: return VectorType::get(Type::getInt16Ty(Context), 128); 243 case MVT::v1i32: return VectorType::get(Type::getInt32Ty(Context), 1); 244 case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2); 245 case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4); 246 case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8); 247 case MVT::v16i32: return VectorType::get(Type::getInt32Ty(Context), 16); 248 case MVT::v32i32: return VectorType::get(Type::getInt32Ty(Context), 32); 249 case MVT::v64i32: return VectorType::get(Type::getInt32Ty(Context), 64); 250 case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1); 251 case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2); 252 case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4); 253 case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8); 254 case MVT::v16i64: return VectorType::get(Type::getInt64Ty(Context), 16); 255 case MVT::v32i64: return VectorType::get(Type::getInt64Ty(Context), 32); 256 case MVT::v1i128: return VectorType::get(Type::getInt128Ty(Context), 1); 257 case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2); 258 case MVT::v4f16: return VectorType::get(Type::getHalfTy(Context), 4); 259 case MVT::v8f16: return VectorType::get(Type::getHalfTy(Context), 8); 260 case MVT::v1f32: return VectorType::get(Type::getFloatTy(Context), 1); 261 case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2); 262 case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4); 263 case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8); 264 case MVT::v16f32: return VectorType::get(Type::getFloatTy(Context), 16); 265 case MVT::v1f64: return VectorType::get(Type::getDoubleTy(Context), 1); 266 case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2); 267 case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4); 268 case MVT::v8f64: return VectorType::get(Type::getDoubleTy(Context), 8); 269 case MVT::Metadata: return Type::getMetadataTy(Context); 270 } 271 } 272 273 /// Return the value type corresponding to the specified type. This returns all 274 /// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned 275 /// as Other, otherwise they are invalid. 276 MVT MVT::getVT(Type *Ty, bool HandleUnknown){ 277 switch (Ty->getTypeID()) { 278 default: 279 if (HandleUnknown) return MVT(MVT::Other); 280 llvm_unreachable("Unknown type!"); 281 case Type::VoidTyID: 282 return MVT::isVoid; 283 case Type::IntegerTyID: 284 return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth()); 285 case Type::HalfTyID: return MVT(MVT::f16); 286 case Type::FloatTyID: return MVT(MVT::f32); 287 case Type::DoubleTyID: return MVT(MVT::f64); 288 case Type::X86_FP80TyID: return MVT(MVT::f80); 289 case Type::X86_MMXTyID: return MVT(MVT::x86mmx); 290 case Type::FP128TyID: return MVT(MVT::f128); 291 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128); 292 case Type::PointerTyID: return MVT(MVT::iPTR); 293 case Type::VectorTyID: { 294 VectorType *VTy = cast<VectorType>(Ty); 295 return getVectorVT( 296 getVT(VTy->getElementType(), false), VTy->getNumElements()); 297 } 298 } 299 } 300 301 /// getEVT - Return the value type corresponding to the specified type. This 302 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types 303 /// are returned as Other, otherwise they are invalid. 304 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){ 305 switch (Ty->getTypeID()) { 306 default: 307 return MVT::getVT(Ty, HandleUnknown); 308 case Type::IntegerTyID: 309 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth()); 310 case Type::VectorTyID: { 311 VectorType *VTy = cast<VectorType>(Ty); 312 return getVectorVT(Ty->getContext(), getEVT(VTy->getElementType(), false), 313 VTy->getNumElements()); 314 } 315 } 316 } 317