#ifndef LLVM_SUPPORT_TYPESIZE_H
#define LLVM_SUPPORT_TYPESIZE_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <type_traits>
namespace llvm {
void reportInvalidSizeRequest(const char *Msg);
template <typename LeafTy> struct LinearPolyBaseTypeTraits {};
template <typename LeafTy>
class LinearPolyBase {
public:
using ScalarTy = typename LinearPolyBaseTypeTraits<LeafTy>::ScalarTy;
static constexpr auto Dimensions = LinearPolyBaseTypeTraits<LeafTy>::Dimensions;
static_assert(Dimensions != std::numeric_limits<unsigned>::max(),
"Dimensions out of range");
private:
std::array<ScalarTy, Dimensions> Coefficients;
protected:
LinearPolyBase(ArrayRef<ScalarTy> Values) {
std::copy(Values.begin(), Values.end(), Coefficients.begin());
}
public:
friend LeafTy &operator+=(LeafTy &LHS, const LeafTy &RHS) {
for (unsigned I=0; I<Dimensions; ++I)
LHS.Coefficients[I] += RHS.Coefficients[I];
return LHS;
}
friend LeafTy &operator-=(LeafTy &LHS, const LeafTy &RHS) {
for (unsigned I=0; I<Dimensions; ++I)
LHS.Coefficients[I] -= RHS.Coefficients[I];
return LHS;
}
friend LeafTy &operator*=(LeafTy &LHS, ScalarTy RHS) {
for (auto &C : LHS.Coefficients)
C *= RHS;
return LHS;
}
friend LeafTy operator+(const LeafTy &LHS, const LeafTy &RHS) {
LeafTy Copy = LHS;
return Copy += RHS;
}
friend LeafTy operator-(const LeafTy &LHS, const LeafTy &RHS) {
LeafTy Copy = LHS;
return Copy -= RHS;
}
friend LeafTy operator*(const LeafTy &LHS, ScalarTy RHS) {
LeafTy Copy = LHS;
return Copy *= RHS;
}
template <typename U = ScalarTy>
friend typename std::enable_if_t<std::is_signed<U>::value, LeafTy>
operator-(const LeafTy &LHS) {
LeafTy Copy = LHS;
return Copy *= -1;
}
bool operator==(const LinearPolyBase &RHS) const {
return std::equal(Coefficients.begin(), Coefficients.end(),
RHS.Coefficients.begin());
}
bool operator!=(const LinearPolyBase &RHS) const {
return !(*this == RHS);
}
bool isZero() const {
return all_of(Coefficients, [](const ScalarTy &C) { return C == 0; });
}
bool isNonZero() const { return !isZero(); }
explicit operator bool() const { return isNonZero(); }
ScalarTy getValue(unsigned Dim) const { return Coefficients[Dim]; }
};
class StackOffset;
template <> struct LinearPolyBaseTypeTraits<StackOffset> {
using ScalarTy = int64_t;
static constexpr unsigned Dimensions = 2;
};
class StackOffset : public LinearPolyBase<StackOffset> {
protected:
StackOffset(ScalarTy Fixed, ScalarTy Scalable)
: LinearPolyBase<StackOffset>({Fixed, Scalable}) {}
public:
StackOffset() : StackOffset({0, 0}) {}
StackOffset(const LinearPolyBase<StackOffset> &Other)
: LinearPolyBase<StackOffset>(Other) {}
static StackOffset getFixed(ScalarTy Fixed) { return {Fixed, 0}; }
static StackOffset getScalable(ScalarTy Scalable) { return {0, Scalable}; }
static StackOffset get(ScalarTy Fixed, ScalarTy Scalable) {
return {Fixed, Scalable};
}
ScalarTy getFixed() const { return this->getValue(0); }
ScalarTy getScalable() const { return this->getValue(1); }
};
template <typename LeafTy>
class UnivariateLinearPolyBase {
public:
using ScalarTy = typename LinearPolyBaseTypeTraits<LeafTy>::ScalarTy;
static constexpr auto Dimensions = LinearPolyBaseTypeTraits<LeafTy>::Dimensions;
static_assert(Dimensions != std::numeric_limits<unsigned>::max(),
"Dimensions out of range");
protected:
ScalarTy Value; unsigned UnivariateDim;
UnivariateLinearPolyBase(ScalarTy Val, unsigned UnivariateDim)
: Value(Val), UnivariateDim(UnivariateDim) {
assert(UnivariateDim < Dimensions && "Dimension out of range");
}
friend LeafTy &operator+=(LeafTy &LHS, const LeafTy &RHS) {
assert(LHS.UnivariateDim == RHS.UnivariateDim && "Invalid dimensions");
LHS.Value += RHS.Value;
return LHS;
}
friend LeafTy &operator-=(LeafTy &LHS, const LeafTy &RHS) {
assert(LHS.UnivariateDim == RHS.UnivariateDim && "Invalid dimensions");
LHS.Value -= RHS.Value;
return LHS;
}
friend LeafTy &operator*=(LeafTy &LHS, ScalarTy RHS) {
LHS.Value *= RHS;
return LHS;
}
friend LeafTy operator+(const LeafTy &LHS, const LeafTy &RHS) {
LeafTy Copy = LHS;
return Copy += RHS;
}
friend LeafTy operator-(const LeafTy &LHS, const LeafTy &RHS) {
LeafTy Copy = LHS;
return Copy -= RHS;
}
friend LeafTy operator*(const LeafTy &LHS, ScalarTy RHS) {
LeafTy Copy = LHS;
return Copy *= RHS;
}
template <typename U = ScalarTy>
friend typename std::enable_if<std::is_signed<U>::value, LeafTy>::type
operator-(const LeafTy &LHS) {
LeafTy Copy = LHS;
return Copy *= -1;
}
public:
bool operator==(const UnivariateLinearPolyBase &RHS) const {
return Value == RHS.Value && UnivariateDim == RHS.UnivariateDim;
}
bool operator!=(const UnivariateLinearPolyBase &RHS) const {
return !(*this == RHS);
}
bool isZero() const { return !Value; }
bool isNonZero() const { return !isZero(); }
explicit operator bool() const { return isNonZero(); }
ScalarTy getValue(unsigned Dim) const {
return Dim == UnivariateDim ? Value : 0;
}
LeafTy getWithIncrement(ScalarTy RHS) const {
return static_cast<LeafTy>(
UnivariateLinearPolyBase(Value + RHS, UnivariateDim));
}
LeafTy getWithDecrement(ScalarTy RHS) const {
return static_cast<LeafTy>(
UnivariateLinearPolyBase(Value - RHS, UnivariateDim));
}
};
template <typename LeafTy>
class LinearPolySize : public UnivariateLinearPolyBase<LeafTy> {
friend class UnivariateLinearPolyBase<LeafTy>;
public:
using ScalarTy = typename UnivariateLinearPolyBase<LeafTy>::ScalarTy;
enum Dims : unsigned { FixedDim = 0, ScalableDim = 1 };
protected:
LinearPolySize(ScalarTy MinVal, Dims D)
: UnivariateLinearPolyBase<LeafTy>(MinVal, D) {}
LinearPolySize(const UnivariateLinearPolyBase<LeafTy> &V)
: UnivariateLinearPolyBase<LeafTy>(V) {}
public:
static LeafTy getFixed(ScalarTy MinVal) {
return static_cast<LeafTy>(LinearPolySize(MinVal, FixedDim));
}
static LeafTy getScalable(ScalarTy MinVal) {
return static_cast<LeafTy>(LinearPolySize(MinVal, ScalableDim));
}
static LeafTy get(ScalarTy MinVal, bool Scalable) {
return static_cast<LeafTy>(
LinearPolySize(MinVal, Scalable ? ScalableDim : FixedDim));
}
static LeafTy getNull() { return get(0, false); }
ScalarTy getKnownMinValue() const { return this->Value; }
bool isScalable() const { return this->UnivariateDim == ScalableDim; }
bool isKnownEven() const { return (getKnownMinValue() & 0x1) == 0; }
bool isKnownMultipleOf(ScalarTy RHS) const {
return getKnownMinValue() % RHS == 0;
}
ScalarTy getFixedValue() const {
assert(!isScalable() &&
"Request for a fixed element count on a scalable object");
return getKnownMinValue();
}
static bool isKnownLT(const LinearPolySize &LHS, const LinearPolySize &RHS) {
if (!LHS.isScalable() || RHS.isScalable())
return LHS.getKnownMinValue() < RHS.getKnownMinValue();
return false;
}
static bool isKnownGT(const LinearPolySize &LHS, const LinearPolySize &RHS) {
if (LHS.isScalable() || !RHS.isScalable())
return LHS.getKnownMinValue() > RHS.getKnownMinValue();
return false;
}
static bool isKnownLE(const LinearPolySize &LHS, const LinearPolySize &RHS) {
if (!LHS.isScalable() || RHS.isScalable())
return LHS.getKnownMinValue() <= RHS.getKnownMinValue();
return false;
}
static bool isKnownGE(const LinearPolySize &LHS, const LinearPolySize &RHS) {
if (LHS.isScalable() || !RHS.isScalable())
return LHS.getKnownMinValue() >= RHS.getKnownMinValue();
return false;
}
LeafTy divideCoefficientBy(ScalarTy RHS) const {
return static_cast<LeafTy>(
LinearPolySize::get(getKnownMinValue() / RHS, isScalable()));
}
LeafTy multiplyCoefficientBy(ScalarTy RHS) const {
return static_cast<LeafTy>(
LinearPolySize::get(getKnownMinValue() * RHS, isScalable()));
}
LeafTy coefficientNextPowerOf2() const {
return static_cast<LeafTy>(LinearPolySize::get(
static_cast<ScalarTy>(llvm::NextPowerOf2(getKnownMinValue())),
isScalable()));
}
bool hasKnownScalarFactor(const LinearPolySize &RHS) const {
return isScalable() == RHS.isScalable() &&
getKnownMinValue() % RHS.getKnownMinValue() == 0;
}
ScalarTy getKnownScalarFactor(const LinearPolySize &RHS) const {
assert(hasKnownScalarFactor(RHS) && "Expected RHS to be a known factor!");
return getKnownMinValue() / RHS.getKnownMinValue();
}
void print(raw_ostream &OS) const {
if (isScalable())
OS << "vscale x ";
OS << getKnownMinValue();
}
};
class ElementCount;
template <> struct LinearPolyBaseTypeTraits<ElementCount> {
using ScalarTy = unsigned;
static constexpr unsigned Dimensions = 2;
};
class ElementCount : public LinearPolySize<ElementCount> {
public:
ElementCount() : LinearPolySize(LinearPolySize::getNull()) {}
ElementCount(const LinearPolySize<ElementCount> &V) : LinearPolySize(V) {}
bool isScalar() const { return !isScalable() && getKnownMinValue() == 1; }
bool isVector() const {
return (isScalable() && getKnownMinValue() != 0) || getKnownMinValue() > 1;
}
};
class TypeSize;
template <> struct LinearPolyBaseTypeTraits<TypeSize> {
using ScalarTy = uint64_t;
static constexpr unsigned Dimensions = 2;
};
class TypeSize : public LinearPolySize<TypeSize> {
public:
TypeSize(const LinearPolySize<TypeSize> &V) : LinearPolySize(V) {}
TypeSize(ScalarTy MinVal, bool IsScalable)
: LinearPolySize(LinearPolySize::get(MinVal, IsScalable)) {}
static TypeSize Fixed(ScalarTy MinVal) { return TypeSize(MinVal, false); }
static TypeSize Scalable(ScalarTy MinVal) { return TypeSize(MinVal, true); }
ScalarTy getFixedSize() const { return getFixedValue(); }
ScalarTy getKnownMinSize() const { return getKnownMinValue(); }
operator ScalarTy() const;
friend TypeSize operator*(const TypeSize &LHS, const int RHS) {
return LHS * (ScalarTy)RHS;
}
friend TypeSize operator*(const TypeSize &LHS, const unsigned RHS) {
return LHS * (ScalarTy)RHS;
}
friend TypeSize operator*(const TypeSize &LHS, const int64_t RHS) {
return LHS * (ScalarTy)RHS;
}
friend TypeSize operator*(const int LHS, const TypeSize &RHS) {
return RHS * LHS;
}
friend TypeSize operator*(const unsigned LHS, const TypeSize &RHS) {
return RHS * LHS;
}
friend TypeSize operator*(const int64_t LHS, const TypeSize &RHS) {
return RHS * LHS;
}
friend TypeSize operator*(const uint64_t LHS, const TypeSize &RHS) {
return RHS * LHS;
}
};
inline TypeSize alignTo(TypeSize Size, uint64_t Align) {
assert(Align != 0u && "Align must be non-zero");
return {(Size.getKnownMinValue() + Align - 1) / Align * Align,
Size.isScalable()};
}
template <typename LeafTy>
inline raw_ostream &operator<<(raw_ostream &OS,
const LinearPolySize<LeafTy> &PS) {
PS.print(OS);
return OS;
}
template <> struct DenseMapInfo<ElementCount, void> {
static inline ElementCount getEmptyKey() {
return ElementCount::getScalable(~0U);
}
static inline ElementCount getTombstoneKey() {
return ElementCount::getFixed(~0U - 1);
}
static unsigned getHashValue(const ElementCount &EltCnt) {
unsigned HashVal = EltCnt.getKnownMinValue() * 37U;
if (EltCnt.isScalable())
return (HashVal - 1U);
return HashVal;
}
static bool isEqual(const ElementCount &LHS, const ElementCount &RHS) {
return LHS == RHS;
}
};
}
#endif