1 //== llvm/Support/LowLevelTypeImpl.h --------------------------- -*- 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 /// \file 9 /// Implement a low-level type suitable for MachineInstr level instruction 10 /// selection. 11 /// 12 /// For a type attached to a MachineInstr, we only care about 2 details: total 13 /// size and the number of vector lanes (if any). Accordingly, there are 4 14 /// possible valid type-kinds: 15 /// 16 /// * `sN` for scalars and aggregates 17 /// * `<N x sM>` for vectors, which must have at least 2 elements. 18 /// * `pN` for pointers 19 /// 20 /// Other information required for correct selection is expected to be carried 21 /// by the opcode, or non-type flags. For example the distinction between G_ADD 22 /// and G_FADD for int/float or fast-math flags. 23 /// 24 //===----------------------------------------------------------------------===// 25 26 #ifndef LLVM_SUPPORT_LOWLEVELTYPEIMPL_H 27 #define LLVM_SUPPORT_LOWLEVELTYPEIMPL_H 28 29 #include "llvm/ADT/DenseMapInfo.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/MachineValueType.h" 32 #include <cassert> 33 34 namespace llvm { 35 36 class Type; 37 class raw_ostream; 38 39 class LLT { 40 public: 41 /// Get a low-level scalar or aggregate "bag of bits". scalar(unsigned SizeInBits)42 static LLT scalar(unsigned SizeInBits) { 43 return LLT{/*isPointer=*/false, /*isVector=*/false, /*isScalar=*/true, 44 ElementCount::getFixed(0), SizeInBits, 45 /*AddressSpace=*/0}; 46 } 47 48 /// Get a low-level pointer in the given address space. pointer(unsigned AddressSpace,unsigned SizeInBits)49 static LLT pointer(unsigned AddressSpace, unsigned SizeInBits) { 50 assert(SizeInBits > 0 && "invalid pointer size"); 51 return LLT{/*isPointer=*/true, /*isVector=*/false, /*isScalar=*/false, 52 ElementCount::getFixed(0), SizeInBits, AddressSpace}; 53 } 54 55 /// Get a low-level vector of some number of elements and element width. vector(ElementCount EC,unsigned ScalarSizeInBits)56 static LLT vector(ElementCount EC, unsigned ScalarSizeInBits) { 57 assert(!EC.isScalar() && "invalid number of vector elements"); 58 return LLT{/*isPointer=*/false, /*isVector=*/true, /*isScalar=*/false, 59 EC, ScalarSizeInBits, /*AddressSpace=*/0}; 60 } 61 62 /// Get a low-level vector of some number of elements and element type. vector(ElementCount EC,LLT ScalarTy)63 static LLT vector(ElementCount EC, LLT ScalarTy) { 64 assert(!EC.isScalar() && "invalid number of vector elements"); 65 assert(!ScalarTy.isVector() && "invalid vector element type"); 66 return LLT{ScalarTy.isPointer(), /*isVector=*/true, /*isScalar=*/false, 67 EC, 68 ScalarTy.getSizeInBits().getFixedSize(), 69 ScalarTy.isPointer() ? ScalarTy.getAddressSpace() : 0}; 70 } 71 72 /// Get a low-level fixed-width vector of some number of elements and element 73 /// width. fixed_vector(unsigned NumElements,unsigned ScalarSizeInBits)74 static LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits) { 75 return vector(ElementCount::getFixed(NumElements), ScalarSizeInBits); 76 } 77 78 /// Get a low-level fixed-width vector of some number of elements and element 79 /// type. fixed_vector(unsigned NumElements,LLT ScalarTy)80 static LLT fixed_vector(unsigned NumElements, LLT ScalarTy) { 81 return vector(ElementCount::getFixed(NumElements), ScalarTy); 82 } 83 84 /// Get a low-level scalable vector of some number of elements and element 85 /// width. scalable_vector(unsigned MinNumElements,unsigned ScalarSizeInBits)86 static LLT scalable_vector(unsigned MinNumElements, 87 unsigned ScalarSizeInBits) { 88 return vector(ElementCount::getScalable(MinNumElements), ScalarSizeInBits); 89 } 90 91 /// Get a low-level scalable vector of some number of elements and element 92 /// type. scalable_vector(unsigned MinNumElements,LLT ScalarTy)93 static LLT scalable_vector(unsigned MinNumElements, LLT ScalarTy) { 94 return vector(ElementCount::getScalable(MinNumElements), ScalarTy); 95 } 96 scalarOrVector(ElementCount EC,LLT ScalarTy)97 static LLT scalarOrVector(ElementCount EC, LLT ScalarTy) { 98 return EC.isScalar() ? ScalarTy : LLT::vector(EC, ScalarTy); 99 } 100 scalarOrVector(ElementCount EC,uint64_t ScalarSize)101 static LLT scalarOrVector(ElementCount EC, uint64_t ScalarSize) { 102 assert(ScalarSize <= std::numeric_limits<unsigned>::max() && 103 "Not enough bits in LLT to represent size"); 104 return scalarOrVector(EC, LLT::scalar(static_cast<unsigned>(ScalarSize))); 105 } 106 LLT(bool isPointer,bool isVector,bool isScalar,ElementCount EC,uint64_t SizeInBits,unsigned AddressSpace)107 explicit LLT(bool isPointer, bool isVector, bool isScalar, ElementCount EC, 108 uint64_t SizeInBits, unsigned AddressSpace) { 109 init(isPointer, isVector, isScalar, EC, SizeInBits, AddressSpace); 110 } LLT()111 explicit LLT() 112 : IsScalar(false), IsPointer(false), IsVector(false), RawData(0) {} 113 114 explicit LLT(MVT VT); 115 isValid()116 bool isValid() const { return IsScalar || RawData != 0; } 117 isScalar()118 bool isScalar() const { return IsScalar; } 119 isPointer()120 bool isPointer() const { return isValid() && IsPointer && !IsVector; } 121 isVector()122 bool isVector() const { return isValid() && IsVector; } 123 124 /// Returns the number of elements in a vector LLT. Must only be called on 125 /// vector types. getNumElements()126 uint16_t getNumElements() const { 127 if (isScalable()) 128 llvm::reportInvalidSizeRequest( 129 "Possible incorrect use of LLT::getNumElements() for " 130 "scalable vector. Scalable flag may be dropped, use " 131 "LLT::getElementCount() instead"); 132 return getElementCount().getKnownMinValue(); 133 } 134 135 /// Returns true if the LLT is a scalable vector. Must only be called on 136 /// vector types. isScalable()137 bool isScalable() const { 138 assert(isVector() && "Expected a vector type"); 139 return IsPointer ? getFieldValue(PointerVectorScalableFieldInfo) 140 : getFieldValue(VectorScalableFieldInfo); 141 } 142 getElementCount()143 ElementCount getElementCount() const { 144 assert(IsVector && "cannot get number of elements on scalar/aggregate"); 145 return ElementCount::get(IsPointer 146 ? getFieldValue(PointerVectorElementsFieldInfo) 147 : getFieldValue(VectorElementsFieldInfo), 148 isScalable()); 149 } 150 151 /// Returns the total size of the type. Must only be called on sized types. getSizeInBits()152 TypeSize getSizeInBits() const { 153 if (isPointer() || isScalar()) 154 return TypeSize::Fixed(getScalarSizeInBits()); 155 auto EC = getElementCount(); 156 return TypeSize(getScalarSizeInBits() * EC.getKnownMinValue(), 157 EC.isScalable()); 158 } 159 160 /// Returns the total size of the type in bytes, i.e. number of whole bytes 161 /// needed to represent the size in bits. Must only be called on sized types. getSizeInBytes()162 TypeSize getSizeInBytes() const { 163 TypeSize BaseSize = getSizeInBits(); 164 return {(BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable()}; 165 } 166 getScalarType()167 LLT getScalarType() const { 168 return isVector() ? getElementType() : *this; 169 } 170 171 /// If this type is a vector, return a vector with the same number of elements 172 /// but the new element type. Otherwise, return the new element type. changeElementType(LLT NewEltTy)173 LLT changeElementType(LLT NewEltTy) const { 174 return isVector() ? LLT::vector(getElementCount(), NewEltTy) : NewEltTy; 175 } 176 177 /// If this type is a vector, return a vector with the same number of elements 178 /// but the new element size. Otherwise, return the new element type. Invalid 179 /// for pointer types. For pointer types, use changeElementType. changeElementSize(unsigned NewEltSize)180 LLT changeElementSize(unsigned NewEltSize) const { 181 assert(!getScalarType().isPointer() && 182 "invalid to directly change element size for pointers"); 183 return isVector() ? LLT::vector(getElementCount(), NewEltSize) 184 : LLT::scalar(NewEltSize); 185 } 186 187 /// Return a vector or scalar with the same element type and the new element 188 /// count. changeElementCount(ElementCount EC)189 LLT changeElementCount(ElementCount EC) const { 190 return LLT::scalarOrVector(EC, getScalarType()); 191 } 192 193 /// Return a type that is \p Factor times smaller. Reduces the number of 194 /// elements if this is a vector, or the bitwidth for scalar/pointers. Does 195 /// not attempt to handle cases that aren't evenly divisible. divide(int Factor)196 LLT divide(int Factor) const { 197 assert(Factor != 1); 198 assert((!isScalar() || getScalarSizeInBits() != 0) && 199 "cannot divide scalar of size zero"); 200 if (isVector()) { 201 assert(getElementCount().isKnownMultipleOf(Factor)); 202 return scalarOrVector(getElementCount().divideCoefficientBy(Factor), 203 getElementType()); 204 } 205 206 assert(getScalarSizeInBits() % Factor == 0); 207 return scalar(getScalarSizeInBits() / Factor); 208 } 209 210 /// Produce a vector type that is \p Factor times bigger, preserving the 211 /// element type. For a scalar or pointer, this will produce a new vector with 212 /// \p Factor elements. multiplyElements(int Factor)213 LLT multiplyElements(int Factor) const { 214 if (isVector()) { 215 return scalarOrVector(getElementCount().multiplyCoefficientBy(Factor), 216 getElementType()); 217 } 218 219 return fixed_vector(Factor, *this); 220 } 221 isByteSized()222 bool isByteSized() const { return getSizeInBits().isKnownMultipleOf(8); } 223 getScalarSizeInBits()224 unsigned getScalarSizeInBits() const { 225 if (IsScalar) 226 return getFieldValue(ScalarSizeFieldInfo); 227 if (IsVector) { 228 if (!IsPointer) 229 return getFieldValue(VectorSizeFieldInfo); 230 else 231 return getFieldValue(PointerVectorSizeFieldInfo); 232 } else if (IsPointer) 233 return getFieldValue(PointerSizeFieldInfo); 234 else 235 llvm_unreachable("unexpected LLT"); 236 } 237 getAddressSpace()238 unsigned getAddressSpace() const { 239 assert(RawData != 0 && "Invalid Type"); 240 assert(IsPointer && "cannot get address space of non-pointer type"); 241 if (!IsVector) 242 return getFieldValue(PointerAddressSpaceFieldInfo); 243 else 244 return getFieldValue(PointerVectorAddressSpaceFieldInfo); 245 } 246 247 /// Returns the vector's element type. Only valid for vector types. getElementType()248 LLT getElementType() const { 249 assert(isVector() && "cannot get element type of scalar/aggregate"); 250 if (IsPointer) 251 return pointer(getAddressSpace(), getScalarSizeInBits()); 252 else 253 return scalar(getScalarSizeInBits()); 254 } 255 256 void print(raw_ostream &OS) const; 257 258 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) dump()259 LLVM_DUMP_METHOD void dump() const { 260 print(dbgs()); 261 dbgs() << '\n'; 262 } 263 #endif 264 265 bool operator==(const LLT &RHS) const { 266 return IsPointer == RHS.IsPointer && IsVector == RHS.IsVector && 267 IsScalar == RHS.IsScalar && RHS.RawData == RawData; 268 } 269 270 bool operator!=(const LLT &RHS) const { return !(*this == RHS); } 271 272 friend struct DenseMapInfo<LLT>; 273 friend class GISelInstProfileBuilder; 274 275 private: 276 /// LLT is packed into 64 bits as follows: 277 /// isScalar : 1 278 /// isPointer : 1 279 /// isVector : 1 280 /// with 61 bits remaining for Kind-specific data, packed in bitfields 281 /// as described below. As there isn't a simple portable way to pack bits 282 /// into bitfields, here the different fields in the packed structure is 283 /// described in static const *Field variables. Each of these variables 284 /// is a 2-element array, with the first element describing the bitfield size 285 /// and the second element describing the bitfield offset. 286 typedef int BitFieldInfo[2]; 287 /// 288 /// This is how the bitfields are packed per Kind: 289 /// * Invalid: 290 /// gets encoded as RawData == 0, as that is an invalid encoding, since for 291 /// valid encodings, SizeInBits/SizeOfElement must be larger than 0. 292 /// * Non-pointer scalar (isPointer == 0 && isVector == 0): 293 /// SizeInBits: 32; 294 static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0}; 295 /// * Pointer (isPointer == 1 && isVector == 0): 296 /// SizeInBits: 16; 297 /// AddressSpace: 24; 298 static const constexpr BitFieldInfo PointerSizeFieldInfo{16, 0}; 299 static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo{ 300 24, PointerSizeFieldInfo[0] + PointerSizeFieldInfo[1]}; 301 static_assert((PointerAddressSpaceFieldInfo[0] + 302 PointerAddressSpaceFieldInfo[1]) <= 61, 303 "Insufficient bits to encode all data"); 304 /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1): 305 /// NumElements: 16; 306 /// SizeOfElement: 32; 307 /// Scalable: 1; 308 static const constexpr BitFieldInfo VectorElementsFieldInfo{16, 0}; 309 static const constexpr BitFieldInfo VectorSizeFieldInfo{ 310 32, VectorElementsFieldInfo[0] + VectorElementsFieldInfo[1]}; 311 static const constexpr BitFieldInfo VectorScalableFieldInfo{ 312 1, VectorSizeFieldInfo[0] + VectorSizeFieldInfo[1]}; 313 static_assert((VectorSizeFieldInfo[0] + VectorSizeFieldInfo[1]) <= 61, 314 "Insufficient bits to encode all data"); 315 /// * Vector-of-pointer (isPointer == 1 && isVector == 1): 316 /// NumElements: 16; 317 /// SizeOfElement: 16; 318 /// AddressSpace: 24; 319 /// Scalable: 1; 320 static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0}; 321 static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{ 322 16, 323 PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]}; 324 static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{ 325 24, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]}; 326 static const constexpr BitFieldInfo PointerVectorScalableFieldInfo{ 327 1, PointerVectorAddressSpaceFieldInfo[0] + 328 PointerVectorAddressSpaceFieldInfo[1]}; 329 static_assert((PointerVectorAddressSpaceFieldInfo[0] + 330 PointerVectorAddressSpaceFieldInfo[1]) <= 61, 331 "Insufficient bits to encode all data"); 332 333 uint64_t IsScalar : 1; 334 uint64_t IsPointer : 1; 335 uint64_t IsVector : 1; 336 uint64_t RawData : 61; 337 338 static uint64_t getMask(const BitFieldInfo FieldInfo) { 339 const int FieldSizeInBits = FieldInfo[0]; 340 return (((uint64_t)1) << FieldSizeInBits) - 1; 341 } 342 static uint64_t maskAndShift(uint64_t Val, uint64_t Mask, uint8_t Shift) { 343 assert(Val <= Mask && "Value too large for field"); 344 return (Val & Mask) << Shift; 345 } 346 static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) { 347 return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]); 348 } 349 uint64_t getFieldValue(const BitFieldInfo FieldInfo) const { 350 return getMask(FieldInfo) & (RawData >> FieldInfo[1]); 351 } 352 353 void init(bool IsPointer, bool IsVector, bool IsScalar, ElementCount EC, 354 uint64_t SizeInBits, unsigned AddressSpace) { 355 assert(SizeInBits <= std::numeric_limits<unsigned>::max() && 356 "Not enough bits in LLT to represent size"); 357 this->IsPointer = IsPointer; 358 this->IsVector = IsVector; 359 this->IsScalar = IsScalar; 360 if (IsScalar) 361 RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo); 362 else if (IsVector) { 363 assert(EC.isVector() && "invalid number of vector elements"); 364 if (!IsPointer) 365 RawData = 366 maskAndShift(EC.getKnownMinValue(), VectorElementsFieldInfo) | 367 maskAndShift(SizeInBits, VectorSizeFieldInfo) | 368 maskAndShift(EC.isScalable() ? 1 : 0, VectorScalableFieldInfo); 369 else 370 RawData = 371 maskAndShift(EC.getKnownMinValue(), 372 PointerVectorElementsFieldInfo) | 373 maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) | 374 maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo) | 375 maskAndShift(EC.isScalable() ? 1 : 0, 376 PointerVectorScalableFieldInfo); 377 } else if (IsPointer) 378 RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) | 379 maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo); 380 else 381 llvm_unreachable("unexpected LLT configuration"); 382 } 383 384 public: 385 uint64_t getUniqueRAWLLTData() const { 386 return ((uint64_t)RawData) << 3 | ((uint64_t)IsScalar) << 2 | 387 ((uint64_t)IsPointer) << 1 | ((uint64_t)IsVector); 388 } 389 }; 390 391 inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) { 392 Ty.print(OS); 393 return OS; 394 } 395 396 template<> struct DenseMapInfo<LLT> { 397 static inline LLT getEmptyKey() { 398 LLT Invalid; 399 Invalid.IsPointer = true; 400 return Invalid; 401 } 402 static inline LLT getTombstoneKey() { 403 LLT Invalid; 404 Invalid.IsVector = true; 405 return Invalid; 406 } 407 static inline unsigned getHashValue(const LLT &Ty) { 408 uint64_t Val = Ty.getUniqueRAWLLTData(); 409 return DenseMapInfo<uint64_t>::getHashValue(Val); 410 } 411 static bool isEqual(const LLT &LHS, const LLT &RHS) { 412 return LHS == RHS; 413 } 414 }; 415 416 } 417 418 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H 419