1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the set of low-level target independent types which various 10 // values in the code generator are. This allows the target specific behavior 11 // of instructions to be described to target independent passes. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CODEGEN_VALUETYPES_H 16 #define LLVM_CODEGEN_VALUETYPES_H 17 18 #include "llvm/Support/Compiler.h" 19 #include "llvm/Support/MachineValueType.h" 20 #include "llvm/Support/MathExtras.h" 21 #include "llvm/Support/TypeSize.h" 22 #include "llvm/Support/WithColor.h" 23 #include <cassert> 24 #include <cstdint> 25 #include <string> 26 27 namespace llvm { 28 29 class LLVMContext; 30 class Type; 31 32 /// Extended Value Type. Capable of holding value types which are not native 33 /// for any processor (such as the i12345 type), as well as the types an MVT 34 /// can represent. 35 struct EVT { 36 private: 37 MVT V = MVT::INVALID_SIMPLE_VALUE_TYPE; 38 Type *LLVMTy = nullptr; 39 40 public: 41 constexpr EVT() = default; EVTEVT42 constexpr EVT(MVT::SimpleValueType SVT) : V(SVT) {} EVTEVT43 constexpr EVT(MVT S) : V(S) {} 44 45 bool operator==(EVT VT) const { 46 return !(*this != VT); 47 } 48 bool operator!=(EVT VT) const { 49 if (V.SimpleTy != VT.V.SimpleTy) 50 return true; 51 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE) 52 return LLVMTy != VT.LLVMTy; 53 return false; 54 } 55 56 /// Returns the EVT that represents a floating-point type with the given 57 /// number of bits. There are two floating-point types with 128 bits - this 58 /// returns f128 rather than ppcf128. getFloatingPointVTEVT59 static EVT getFloatingPointVT(unsigned BitWidth) { 60 return MVT::getFloatingPointVT(BitWidth); 61 } 62 63 /// Returns the EVT that represents an integer with the given number of 64 /// bits. getIntegerVTEVT65 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) { 66 MVT M = MVT::getIntegerVT(BitWidth); 67 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE) 68 return M; 69 return getExtendedIntegerVT(Context, BitWidth); 70 } 71 72 /// Returns the EVT that represents a vector NumElements in length, where 73 /// each element is of type VT. 74 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, 75 bool IsScalable = false) { 76 MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable); 77 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE) 78 return M; 79 return getExtendedVectorVT(Context, VT, NumElements, IsScalable); 80 } 81 82 /// Returns the EVT that represents a vector EC.Min elements in length, 83 /// where each element is of type VT. getVectorVTEVT84 static EVT getVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) { 85 MVT M = MVT::getVectorVT(VT.V, EC); 86 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE) 87 return M; 88 return getExtendedVectorVT(Context, VT, EC); 89 } 90 91 /// Return a vector with the same number of elements as this vector, but 92 /// with the element type converted to an integer type with the same 93 /// bitwidth. changeVectorElementTypeToIntegerEVT94 EVT changeVectorElementTypeToInteger() const { 95 if (isSimple()) 96 return getSimpleVT().changeVectorElementTypeToInteger(); 97 return changeExtendedVectorElementTypeToInteger(); 98 } 99 100 /// Return a VT for a vector type whose attributes match ourselves 101 /// with the exception of the element type that is chosen by the caller. changeVectorElementTypeEVT102 EVT changeVectorElementType(EVT EltVT) const { 103 if (isSimple()) { 104 assert(EltVT.isSimple() && 105 "Can't change simple vector VT to have extended element VT"); 106 return getSimpleVT().changeVectorElementType(EltVT.getSimpleVT()); 107 } 108 return changeExtendedVectorElementType(EltVT); 109 } 110 111 /// Return the type converted to an equivalently sized integer or vector 112 /// with integer element type. Similar to changeVectorElementTypeToInteger, 113 /// but also handles scalars. changeTypeToIntegerEVT114 EVT changeTypeToInteger() { 115 if (isVector()) 116 return changeVectorElementTypeToInteger(); 117 118 if (isSimple()) 119 return getSimpleVT().changeTypeToInteger(); 120 return changeExtendedTypeToInteger(); 121 } 122 123 /// Test if the given EVT has zero size, this will fail if called on a 124 /// scalable type isZeroSizedEVT125 bool isZeroSized() const { 126 return !isScalableVector() && getSizeInBits() == 0; 127 } 128 129 /// Test if the given EVT is simple (as opposed to being extended). isSimpleEVT130 bool isSimple() const { 131 return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE; 132 } 133 134 /// Test if the given EVT is extended (as opposed to being simple). isExtendedEVT135 bool isExtended() const { 136 return !isSimple(); 137 } 138 139 /// Return true if this is a FP or a vector FP type. isFloatingPointEVT140 bool isFloatingPoint() const { 141 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint(); 142 } 143 144 /// Return true if this is an integer or a vector integer type. isIntegerEVT145 bool isInteger() const { 146 return isSimple() ? V.isInteger() : isExtendedInteger(); 147 } 148 149 /// Return true if this is an integer, but not a vector. isScalarIntegerEVT150 bool isScalarInteger() const { 151 return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger(); 152 } 153 154 /// Return true if this is a vector value type. isVectorEVT155 bool isVector() const { 156 return isSimple() ? V.isVector() : isExtendedVector(); 157 } 158 159 /// Return true if this is a vector type where the runtime 160 /// length is machine dependent isScalableVectorEVT161 bool isScalableVector() const { 162 return isSimple() ? V.isScalableVector() : isExtendedScalableVector(); 163 } 164 isFixedLengthVectorEVT165 bool isFixedLengthVector() const { 166 return isSimple() ? V.isFixedLengthVector() 167 : isExtendedFixedLengthVector(); 168 } 169 170 /// Return true if this is a 16-bit vector type. is16BitVectorEVT171 bool is16BitVector() const { 172 return isSimple() ? V.is16BitVector() : isExtended16BitVector(); 173 } 174 175 /// Return true if this is a 32-bit vector type. is32BitVectorEVT176 bool is32BitVector() const { 177 return isSimple() ? V.is32BitVector() : isExtended32BitVector(); 178 } 179 180 /// Return true if this is a 64-bit vector type. is64BitVectorEVT181 bool is64BitVector() const { 182 return isSimple() ? V.is64BitVector() : isExtended64BitVector(); 183 } 184 185 /// Return true if this is a 128-bit vector type. is128BitVectorEVT186 bool is128BitVector() const { 187 return isSimple() ? V.is128BitVector() : isExtended128BitVector(); 188 } 189 190 /// Return true if this is a 256-bit vector type. is256BitVectorEVT191 bool is256BitVector() const { 192 return isSimple() ? V.is256BitVector() : isExtended256BitVector(); 193 } 194 195 /// Return true if this is a 512-bit vector type. is512BitVectorEVT196 bool is512BitVector() const { 197 return isSimple() ? V.is512BitVector() : isExtended512BitVector(); 198 } 199 200 /// Return true if this is a 1024-bit vector type. is1024BitVectorEVT201 bool is1024BitVector() const { 202 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector(); 203 } 204 205 /// Return true if this is a 2048-bit vector type. is2048BitVectorEVT206 bool is2048BitVector() const { 207 return isSimple() ? V.is2048BitVector() : isExtended2048BitVector(); 208 } 209 210 /// Return true if this is an overloaded type for TableGen. isOverloadedEVT211 bool isOverloaded() const { 212 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny); 213 } 214 215 /// Return true if the bit size is a multiple of 8. isByteSizedEVT216 bool isByteSized() const { 217 return !isZeroSized() && getSizeInBits().isKnownMultipleOf(8); 218 } 219 220 /// Return true if the size is a power-of-two number of bytes. isRoundEVT221 bool isRound() const { 222 if (isScalableVector()) 223 return false; 224 unsigned BitSize = getSizeInBits(); 225 return BitSize >= 8 && !(BitSize & (BitSize - 1)); 226 } 227 228 /// Return true if this has the same number of bits as VT. bitsEqEVT229 bool bitsEq(EVT VT) const { 230 if (EVT::operator==(VT)) return true; 231 return getSizeInBits() == VT.getSizeInBits(); 232 } 233 234 /// Return true if we know at compile time this has more bits than VT. knownBitsGTEVT235 bool knownBitsGT(EVT VT) const { 236 return TypeSize::isKnownGT(getSizeInBits(), VT.getSizeInBits()); 237 } 238 239 /// Return true if we know at compile time this has more than or the same 240 /// bits as VT. knownBitsGEEVT241 bool knownBitsGE(EVT VT) const { 242 return TypeSize::isKnownGE(getSizeInBits(), VT.getSizeInBits()); 243 } 244 245 /// Return true if we know at compile time this has fewer bits than VT. knownBitsLTEVT246 bool knownBitsLT(EVT VT) const { 247 return TypeSize::isKnownLT(getSizeInBits(), VT.getSizeInBits()); 248 } 249 250 /// Return true if we know at compile time this has fewer than or the same 251 /// bits as VT. knownBitsLEEVT252 bool knownBitsLE(EVT VT) const { 253 return TypeSize::isKnownLE(getSizeInBits(), VT.getSizeInBits()); 254 } 255 256 /// Return true if this has more bits than VT. bitsGTEVT257 bool bitsGT(EVT VT) const { 258 if (EVT::operator==(VT)) return false; 259 assert(isScalableVector() == VT.isScalableVector() && 260 "Comparison between scalable and fixed types"); 261 return knownBitsGT(VT); 262 } 263 264 /// Return true if this has no less bits than VT. bitsGEEVT265 bool bitsGE(EVT VT) const { 266 if (EVT::operator==(VT)) return true; 267 assert(isScalableVector() == VT.isScalableVector() && 268 "Comparison between scalable and fixed types"); 269 return knownBitsGE(VT); 270 } 271 272 /// Return true if this has less bits than VT. bitsLTEVT273 bool bitsLT(EVT VT) const { 274 if (EVT::operator==(VT)) return false; 275 assert(isScalableVector() == VT.isScalableVector() && 276 "Comparison between scalable and fixed types"); 277 return knownBitsLT(VT); 278 } 279 280 /// Return true if this has no more bits than VT. bitsLEEVT281 bool bitsLE(EVT VT) const { 282 if (EVT::operator==(VT)) return true; 283 assert(isScalableVector() == VT.isScalableVector() && 284 "Comparison between scalable and fixed types"); 285 return knownBitsLE(VT); 286 } 287 288 /// Return the SimpleValueType held in the specified simple EVT. getSimpleVTEVT289 MVT getSimpleVT() const { 290 assert(isSimple() && "Expected a SimpleValueType!"); 291 return V; 292 } 293 294 /// If this is a vector type, return the element type, otherwise return 295 /// this. getScalarTypeEVT296 EVT getScalarType() const { 297 return isVector() ? getVectorElementType() : *this; 298 } 299 300 /// Given a vector type, return the type of each element. getVectorElementTypeEVT301 EVT getVectorElementType() const { 302 assert(isVector() && "Invalid vector type!"); 303 if (isSimple()) 304 return V.getVectorElementType(); 305 return getExtendedVectorElementType(); 306 } 307 308 /// Given a vector type, return the number of elements it contains. getVectorNumElementsEVT309 unsigned getVectorNumElements() const { 310 assert(isVector() && "Invalid vector type!"); 311 312 if (isScalableVector()) 313 llvm::reportInvalidSizeRequest( 314 "Possible incorrect use of EVT::getVectorNumElements() for " 315 "scalable vector. Scalable flag may be dropped, use " 316 "EVT::getVectorElementCount() instead"); 317 318 return isSimple() ? V.getVectorNumElements() 319 : getExtendedVectorNumElements(); 320 } 321 322 // Given a (possibly scalable) vector type, return the ElementCount getVectorElementCountEVT323 ElementCount getVectorElementCount() const { 324 assert((isVector()) && "Invalid vector type!"); 325 if (isSimple()) 326 return V.getVectorElementCount(); 327 328 return getExtendedVectorElementCount(); 329 } 330 331 /// Given a vector type, return the minimum number of elements it contains. getVectorMinNumElementsEVT332 unsigned getVectorMinNumElements() const { 333 return getVectorElementCount().getKnownMinValue(); 334 } 335 336 /// Return the size of the specified value type in bits. 337 /// 338 /// If the value type is a scalable vector type, the scalable property will 339 /// be set and the runtime size will be a positive integer multiple of the 340 /// base size. getSizeInBitsEVT341 TypeSize getSizeInBits() const { 342 if (isSimple()) 343 return V.getSizeInBits(); 344 return getExtendedSizeInBits(); 345 } 346 347 /// Return the size of the specified fixed width value type in bits. The 348 /// function will assert if the type is scalable. getFixedSizeInBitsEVT349 uint64_t getFixedSizeInBits() const { 350 return getSizeInBits().getFixedSize(); 351 } 352 getScalarSizeInBitsEVT353 uint64_t getScalarSizeInBits() const { 354 return getScalarType().getSizeInBits().getFixedSize(); 355 } 356 357 /// Return the number of bytes overwritten by a store of the specified value 358 /// type. 359 /// 360 /// If the value type is a scalable vector type, the scalable property will 361 /// be set and the runtime size will be a positive integer multiple of the 362 /// base size. getStoreSizeEVT363 TypeSize getStoreSize() const { 364 TypeSize BaseSize = getSizeInBits(); 365 return {(BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable()}; 366 } 367 368 /// Return the number of bits overwritten by a store of the specified value 369 /// type. 370 /// 371 /// If the value type is a scalable vector type, the scalable property will 372 /// be set and the runtime size will be a positive integer multiple of the 373 /// base size. getStoreSizeInBitsEVT374 TypeSize getStoreSizeInBits() const { 375 return getStoreSize() * 8; 376 } 377 378 /// Rounds the bit-width of the given integer EVT up to the nearest power of 379 /// two (and at least to eight), and returns the integer EVT with that 380 /// number of bits. getRoundIntegerTypeEVT381 EVT getRoundIntegerType(LLVMContext &Context) const { 382 assert(isInteger() && !isVector() && "Invalid integer type!"); 383 unsigned BitWidth = getSizeInBits(); 384 if (BitWidth <= 8) 385 return EVT(MVT::i8); 386 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth)); 387 } 388 389 /// Finds the smallest simple value type that is greater than or equal to 390 /// half the width of this EVT. If no simple value type can be found, an 391 /// extended integer value type of half the size (rounded up) is returned. getHalfSizedIntegerVTEVT392 EVT getHalfSizedIntegerVT(LLVMContext &Context) const { 393 assert(isInteger() && !isVector() && "Invalid integer type!"); 394 unsigned EVTSize = getSizeInBits(); 395 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE; 396 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) { 397 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT); 398 if (HalfVT.getSizeInBits() * 2 >= EVTSize) 399 return HalfVT; 400 } 401 return getIntegerVT(Context, (EVTSize + 1) / 2); 402 } 403 404 /// Return a VT for an integer vector type with the size of the 405 /// elements doubled. The typed returned may be an extended type. widenIntegerVectorElementTypeEVT406 EVT widenIntegerVectorElementType(LLVMContext &Context) const { 407 EVT EltVT = getVectorElementType(); 408 EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits()); 409 return EVT::getVectorVT(Context, EltVT, getVectorElementCount()); 410 } 411 412 // Return a VT for a vector type with the same element type but 413 // half the number of elements. The type returned may be an 414 // extended type. getHalfNumVectorElementsVTEVT415 EVT getHalfNumVectorElementsVT(LLVMContext &Context) const { 416 EVT EltVT = getVectorElementType(); 417 auto EltCnt = getVectorElementCount(); 418 assert(EltCnt.isKnownEven() && "Splitting vector, but not in half!"); 419 return EVT::getVectorVT(Context, EltVT, EltCnt.divideCoefficientBy(2)); 420 } 421 422 // Return a VT for a vector type with the same element type but 423 // double the number of elements. The type returned may be an 424 // extended type. getDoubleNumVectorElementsVTEVT425 EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const { 426 EVT EltVT = getVectorElementType(); 427 auto EltCnt = getVectorElementCount(); 428 return EVT::getVectorVT(Context, EltVT, EltCnt * 2); 429 } 430 431 /// Returns true if the given vector is a power of 2. isPow2VectorTypeEVT432 bool isPow2VectorType() const { 433 unsigned NElts = getVectorMinNumElements(); 434 return !(NElts & (NElts - 1)); 435 } 436 437 /// Widens the length of the given vector EVT up to the nearest power of 2 438 /// and returns that type. getPow2VectorTypeEVT439 EVT getPow2VectorType(LLVMContext &Context) const { 440 if (!isPow2VectorType()) { 441 ElementCount NElts = getVectorElementCount(); 442 unsigned NewMinCount = 1 << Log2_32_Ceil(NElts.getKnownMinValue()); 443 NElts = ElementCount::get(NewMinCount, NElts.isScalable()); 444 return EVT::getVectorVT(Context, getVectorElementType(), NElts); 445 } 446 else { 447 return *this; 448 } 449 } 450 451 /// This function returns value type as a string, e.g. "i32". 452 std::string getEVTString() const; 453 454 /// This method returns an LLVM type corresponding to the specified EVT. 455 /// For integer types, this returns an unsigned type. Note that this will 456 /// abort for types that cannot be represented. 457 Type *getTypeForEVT(LLVMContext &Context) const; 458 459 /// Return the value type corresponding to the specified type. 460 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown 461 /// types are returned as Other, otherwise they are invalid. 462 static EVT getEVT(Type *Ty, bool HandleUnknown = false); 463 getRawBitsEVT464 intptr_t getRawBits() const { 465 if (isSimple()) 466 return V.SimpleTy; 467 else 468 return (intptr_t)(LLVMTy); 469 } 470 471 /// A meaningless but well-behaved order, useful for constructing 472 /// containers. 473 struct compareRawBits { operatorEVT::compareRawBits474 bool operator()(EVT L, EVT R) const { 475 if (L.V.SimpleTy == R.V.SimpleTy) 476 return L.LLVMTy < R.LLVMTy; 477 else 478 return L.V.SimpleTy < R.V.SimpleTy; 479 } 480 }; 481 482 private: 483 // Methods for handling the Extended-type case in functions above. 484 // These are all out-of-line to prevent users of this header file 485 // from having a dependency on Type.h. 486 EVT changeExtendedTypeToInteger() const; 487 EVT changeExtendedVectorElementType(EVT EltVT) const; 488 EVT changeExtendedVectorElementTypeToInteger() const; 489 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth); 490 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT, unsigned NumElements, 491 bool IsScalable); 492 static EVT getExtendedVectorVT(LLVMContext &Context, EVT VT, 493 ElementCount EC); 494 bool isExtendedFloatingPoint() const LLVM_READONLY; 495 bool isExtendedInteger() const LLVM_READONLY; 496 bool isExtendedScalarInteger() const LLVM_READONLY; 497 bool isExtendedVector() const LLVM_READONLY; 498 bool isExtended16BitVector() const LLVM_READONLY; 499 bool isExtended32BitVector() const LLVM_READONLY; 500 bool isExtended64BitVector() const LLVM_READONLY; 501 bool isExtended128BitVector() const LLVM_READONLY; 502 bool isExtended256BitVector() const LLVM_READONLY; 503 bool isExtended512BitVector() const LLVM_READONLY; 504 bool isExtended1024BitVector() const LLVM_READONLY; 505 bool isExtended2048BitVector() const LLVM_READONLY; 506 bool isExtendedFixedLengthVector() const LLVM_READONLY; 507 bool isExtendedScalableVector() const LLVM_READONLY; 508 EVT getExtendedVectorElementType() const; 509 unsigned getExtendedVectorNumElements() const LLVM_READONLY; 510 ElementCount getExtendedVectorElementCount() const LLVM_READONLY; 511 TypeSize getExtendedSizeInBits() const LLVM_READONLY; 512 }; 513 514 } // end namespace llvm 515 516 #endif // LLVM_CODEGEN_VALUETYPES_H 517