#ifndef LLVM_CODEGEN_VALUETYPES_H
#define LLVM_CODEGEN_VALUETYPES_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MachineValueType.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/TypeSize.h"
#include <cassert>
#include <cstdint>
#include <string>
namespace llvm {
class LLVMContext;
class Type;
struct EVT {
private:
MVT V = MVT::INVALID_SIMPLE_VALUE_TYPE;
Type *LLVMTy = nullptr;
public:
constexpr EVT() = default;
constexpr EVT(MVT::SimpleValueType SVT) : V(SVT) {}
constexpr EVT(MVT S) : V(S) {}
bool operator==(EVT VT) const {
return !(*this != VT);
}
bool operator!=(EVT VT) const {
if (V.SimpleTy != VT.V.SimpleTy)
return true;
if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
return LLVMTy != VT.LLVMTy;
return false;
}
static EVT getFloatingPointVT(unsigned BitWidth) {
return MVT::getFloatingPointVT(BitWidth);
}
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
MVT M = MVT::getIntegerVT(BitWidth);
if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
return M;
return getExtendedIntegerVT(Context, BitWidth);
}
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
bool IsScalable = false) {
MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
return M;
return getExtendedVectorVT(Context, VT, NumElements, IsScalable);
}
static EVT getVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) {
MVT M = MVT::getVectorVT(VT.V, EC);
if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
return M;
return getExtendedVectorVT(Context, VT, EC);
}
EVT changeVectorElementTypeToInteger() const {
if (isSimple())
return getSimpleVT().changeVectorElementTypeToInteger();
return changeExtendedVectorElementTypeToInteger();
}
EVT changeVectorElementType(EVT EltVT) const {
if (isSimple()) {
assert(EltVT.isSimple() &&
"Can't change simple vector VT to have extended element VT");
return getSimpleVT().changeVectorElementType(EltVT.getSimpleVT());
}
return changeExtendedVectorElementType(EltVT);
}
EVT changeTypeToInteger() {
if (isVector())
return changeVectorElementTypeToInteger();
if (isSimple())
return getSimpleVT().changeTypeToInteger();
return changeExtendedTypeToInteger();
}
bool isZeroSized() const {
return !isScalableVector() && getSizeInBits() == 0;
}
bool isSimple() const {
return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE;
}
bool isExtended() const {
return !isSimple();
}
bool isFloatingPoint() const {
return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
}
bool isInteger() const {
return isSimple() ? V.isInteger() : isExtendedInteger();
}
bool isScalarInteger() const {
return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
}
bool isVector() const {
return isSimple() ? V.isVector() : isExtendedVector();
}
bool isScalableVector() const {
return isSimple() ? V.isScalableVector() : isExtendedScalableVector();
}
bool isFixedLengthVector() const {
return isSimple() ? V.isFixedLengthVector()
: isExtendedFixedLengthVector();
}
bool is16BitVector() const {
return isSimple() ? V.is16BitVector() : isExtended16BitVector();
}
bool is32BitVector() const {
return isSimple() ? V.is32BitVector() : isExtended32BitVector();
}
bool is64BitVector() const {
return isSimple() ? V.is64BitVector() : isExtended64BitVector();
}
bool is128BitVector() const {
return isSimple() ? V.is128BitVector() : isExtended128BitVector();
}
bool is256BitVector() const {
return isSimple() ? V.is256BitVector() : isExtended256BitVector();
}
bool is512BitVector() const {
return isSimple() ? V.is512BitVector() : isExtended512BitVector();
}
bool is1024BitVector() const {
return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
}
bool is2048BitVector() const {
return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
}
bool isOverloaded() const {
return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
}
bool isByteSized() const {
return !isZeroSized() && getSizeInBits().isKnownMultipleOf(8);
}
bool isRound() const {
if (isScalableVector())
return false;
unsigned BitSize = getSizeInBits();
return BitSize >= 8 && !(BitSize & (BitSize - 1));
}
bool bitsEq(EVT VT) const {
if (EVT::operator==(VT)) return true;
return getSizeInBits() == VT.getSizeInBits();
}
bool knownBitsGT(EVT VT) const {
return TypeSize::isKnownGT(getSizeInBits(), VT.getSizeInBits());
}
bool knownBitsGE(EVT VT) const {
return TypeSize::isKnownGE(getSizeInBits(), VT.getSizeInBits());
}
bool knownBitsLT(EVT VT) const {
return TypeSize::isKnownLT(getSizeInBits(), VT.getSizeInBits());
}
bool knownBitsLE(EVT VT) const {
return TypeSize::isKnownLE(getSizeInBits(), VT.getSizeInBits());
}
bool bitsGT(EVT VT) const {
if (EVT::operator==(VT)) return false;
assert(isScalableVector() == VT.isScalableVector() &&
"Comparison between scalable and fixed types");
return knownBitsGT(VT);
}
bool bitsGE(EVT VT) const {
if (EVT::operator==(VT)) return true;
assert(isScalableVector() == VT.isScalableVector() &&
"Comparison between scalable and fixed types");
return knownBitsGE(VT);
}
bool bitsLT(EVT VT) const {
if (EVT::operator==(VT)) return false;
assert(isScalableVector() == VT.isScalableVector() &&
"Comparison between scalable and fixed types");
return knownBitsLT(VT);
}
bool bitsLE(EVT VT) const {
if (EVT::operator==(VT)) return true;
assert(isScalableVector() == VT.isScalableVector() &&
"Comparison between scalable and fixed types");
return knownBitsLE(VT);
}
MVT getSimpleVT() const {
assert(isSimple() && "Expected a SimpleValueType!");
return V;
}
EVT getScalarType() const {
return isVector() ? getVectorElementType() : *this;
}
EVT getVectorElementType() const {
assert(isVector() && "Invalid vector type!");
if (isSimple())
return V.getVectorElementType();
return getExtendedVectorElementType();
}
unsigned getVectorNumElements() const {
assert(isVector() && "Invalid vector type!");
if (isScalableVector())
llvm::reportInvalidSizeRequest(
"Possible incorrect use of EVT::getVectorNumElements() for "
"scalable vector. Scalable flag may be dropped, use "
"EVT::getVectorElementCount() instead");
return isSimple() ? V.getVectorNumElements()
: getExtendedVectorNumElements();
}
ElementCount getVectorElementCount() const {
assert((isVector()) && "Invalid vector type!");
if (isSimple())
return V.getVectorElementCount();
return getExtendedVectorElementCount();
}
unsigned getVectorMinNumElements() const {
return getVectorElementCount().getKnownMinValue();
}
TypeSize getSizeInBits() const {
if (isSimple())
return V.getSizeInBits();
return getExtendedSizeInBits();
}
uint64_t getFixedSizeInBits() const {
return getSizeInBits().getFixedSize();
}
uint64_t getScalarSizeInBits() const {
return getScalarType().getSizeInBits().getFixedSize();
}
TypeSize getStoreSize() const {
TypeSize BaseSize = getSizeInBits();
return {(BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable()};
}
uint64_t getScalarStoreSize() const {
return getScalarType().getStoreSize().getFixedSize();
}
TypeSize getStoreSizeInBits() const {
return getStoreSize() * 8;
}
EVT getRoundIntegerType(LLVMContext &Context) const {
assert(isInteger() && !isVector() && "Invalid integer type!");
unsigned BitWidth = getSizeInBits();
if (BitWidth <= 8)
return EVT(MVT::i8);
return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
}
EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
assert(isInteger() && !isVector() && "Invalid integer type!");
unsigned EVTSize = getSizeInBits();
for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
if (HalfVT.getSizeInBits() * 2 >= EVTSize)
return HalfVT;
}
return getIntegerVT(Context, (EVTSize + 1) / 2);
}
EVT widenIntegerVectorElementType(LLVMContext &Context) const {
EVT EltVT = getVectorElementType();
EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
}
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const {
EVT EltVT = getVectorElementType();
auto EltCnt = getVectorElementCount();
assert(EltCnt.isKnownEven() && "Splitting vector, but not in half!");
return EVT::getVectorVT(Context, EltVT, EltCnt.divideCoefficientBy(2));
}
EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const {
EVT EltVT = getVectorElementType();
auto EltCnt = getVectorElementCount();
return EVT::getVectorVT(Context, EltVT, EltCnt * 2);
}
bool isPow2VectorType() const {
unsigned NElts = getVectorMinNumElements();
return !(NElts & (NElts - 1));
}
EVT getPow2VectorType(LLVMContext &Context) const {
if (!isPow2VectorType()) {
ElementCount NElts = getVectorElementCount();
unsigned NewMinCount = 1 << Log2_32_Ceil(NElts.getKnownMinValue());
NElts = ElementCount::get(NewMinCount, NElts.isScalable());
return EVT::getVectorVT(Context, getVectorElementType(), NElts);
}
else {
return *this;
}
}
std::string getEVTString() const;
Type *getTypeForEVT(LLVMContext &Context) const;
static EVT getEVT(Type *Ty, bool HandleUnknown = false);
intptr_t getRawBits() const {
if (isSimple())
return V.SimpleTy;
else
return (intptr_t)(LLVMTy);
}
struct compareRawBits {
bool operator()(EVT L, EVT R) const {
if (L.V.SimpleTy == R.V.SimpleTy)
return L.LLVMTy < R.LLVMTy;
else
return L.V.SimpleTy < R.V.SimpleTy;
}
};
private:
EVT changeExtendedTypeToInteger() const;
EVT changeExtendedVectorElementType(EVT EltVT) const;
EVT changeExtendedVectorElementTypeToInteger() const;
static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
static EVT getExtendedVectorVT(LLVMContext &C, EVT VT, unsigned NumElements,
bool IsScalable);
static EVT getExtendedVectorVT(LLVMContext &Context, EVT VT,
ElementCount EC);
bool isExtendedFloatingPoint() const LLVM_READONLY;
bool isExtendedInteger() const LLVM_READONLY;
bool isExtendedScalarInteger() const LLVM_READONLY;
bool isExtendedVector() const LLVM_READONLY;
bool isExtended16BitVector() const LLVM_READONLY;
bool isExtended32BitVector() const LLVM_READONLY;
bool isExtended64BitVector() const LLVM_READONLY;
bool isExtended128BitVector() const LLVM_READONLY;
bool isExtended256BitVector() const LLVM_READONLY;
bool isExtended512BitVector() const LLVM_READONLY;
bool isExtended1024BitVector() const LLVM_READONLY;
bool isExtended2048BitVector() const LLVM_READONLY;
bool isExtendedFixedLengthVector() const LLVM_READONLY;
bool isExtendedScalableVector() const LLVM_READONLY;
EVT getExtendedVectorElementType() const;
unsigned getExtendedVectorNumElements() const LLVM_READONLY;
ElementCount getExtendedVectorElementCount() const LLVM_READONLY;
TypeSize getExtendedSizeInBits() const LLVM_READONLY;
};
}
#endif