#ifndef LLVM_CODEGEN_VALUETYPES_H
#define LLVM_CODEGEN_VALUETYPES_H
#include <cassert>
#include <string>
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
namespace llvm {
class Type;
struct MVT { public:
enum SimpleValueType {
Other = 0, i1 = 1, i8 = 2, i16 = 3, i32 = 4, i64 = 5, i128 = 6,
FIRST_INTEGER_VALUETYPE = i1,
LAST_INTEGER_VALUETYPE = i128,
f32 = 7, f64 = 8, f80 = 9, f128 = 10, ppcf128 = 11, Flag = 12,
isVoid = 13,
v2i8 = 14, v4i8 = 15, v2i16 = 16, v8i8 = 17, v4i16 = 18, v2i32 = 19, v1i64 = 20, v16i8 = 21, v8i16 = 22, v3i32 = 23, v4i32 = 24, v2i64 = 25,
v2f32 = 26, v3f32 = 27, v4f32 = 28, v2f64 = 29,
FIRST_VECTOR_VALUETYPE = v2i8,
LAST_VECTOR_VALUETYPE = v2f64,
LAST_VALUETYPE = 30,
iPTRAny = 252,
fAny = 253,
iAny = 254,
iPTR = 255,
LastSimpleValueType = 255
};
private:
union {
uintptr_t V;
const Type *LLVMTy;
};
public:
MVT() {}
MVT(SimpleValueType S) : V(S) {}
bool operator==(const MVT VT) const {
return getRawBits() == VT.getRawBits();
}
bool operator!=(const MVT VT) const {
return getRawBits() != VT.getRawBits();
}
static MVT getFloatingPointVT(unsigned BitWidth) {
switch (BitWidth) {
default:
assert(false && "Bad bit width!");
case 32:
return f32;
case 64:
return f64;
case 80:
return f80;
case 128:
return f128;
}
}
static MVT getIntegerVT(unsigned BitWidth) {
switch (BitWidth) {
default:
break;
case 1:
return i1;
case 8:
return i8;
case 16:
return i16;
case 32:
return i32;
case 64:
return i64;
case 128:
return i128;
}
return getExtendedIntegerVT(BitWidth);
}
static MVT getVectorVT(MVT VT, unsigned NumElements) {
switch (VT.V) {
default:
break;
case i8:
if (NumElements == 2) return v2i8;
if (NumElements == 4) return v4i8;
if (NumElements == 8) return v8i8;
if (NumElements == 16) return v16i8;
break;
case i16:
if (NumElements == 2) return v2i16;
if (NumElements == 4) return v4i16;
if (NumElements == 8) return v8i16;
break;
case i32:
if (NumElements == 2) return v2i32;
if (NumElements == 3) return v3i32;
if (NumElements == 4) return v4i32;
break;
case i64:
if (NumElements == 1) return v1i64;
if (NumElements == 2) return v2i64;
break;
case f32:
if (NumElements == 2) return v2f32;
if (NumElements == 3) return v3f32;
if (NumElements == 4) return v4f32;
break;
case f64:
if (NumElements == 2) return v2f64;
break;
}
return getExtendedVectorVT(VT, NumElements);
}
static MVT getIntVectorWithNumElements(unsigned NumElts) {
switch (NumElts) {
default: return getVectorVT(i8, NumElts);
case 1: return v1i64;
case 2: return v2i32;
case 3: return v3i32;
case 4: return v4i16;
case 8: return v8i8;
case 16: return v16i8;
}
}
bool isSimple() const {
return V <= LastSimpleValueType;
}
bool isExtended() const {
return !isSimple();
}
bool isFloatingPoint() const {
return isSimple() ?
((V >= f32 && V <= ppcf128) || (V >= v2f32 && V <= v2f64)) :
isExtendedFloatingPoint();
}
bool isInteger() const {
return isSimple() ?
((V >= FIRST_INTEGER_VALUETYPE && V <= LAST_INTEGER_VALUETYPE) ||
(V >= v2i8 && V <= v2i64)) : isExtendedInteger();
}
bool isVector() const {
return isSimple() ?
(V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) :
isExtendedVector();
}
bool is64BitVector() const {
return isSimple() ?
(V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32) :
isExtended64BitVector();
}
bool is128BitVector() const {
return isSimple() ?
(V==v16i8 || V==v8i16 || V==v4i32 ||
V==v2i64 || V==v4f32 || V==v2f64) :
isExtended128BitVector();
}
bool isByteSized() const {
return (getSizeInBits() & 7) == 0;
}
bool isRound() const {
unsigned BitSize = getSizeInBits();
return BitSize >= 8 && !(BitSize & (BitSize - 1));
}
bool bitsEq(MVT VT) const {
return getSizeInBits() == VT.getSizeInBits();
}
bool bitsGT(MVT VT) const {
return getSizeInBits() > VT.getSizeInBits();
}
bool bitsGE(MVT VT) const {
return getSizeInBits() >= VT.getSizeInBits();
}
bool bitsLT(MVT VT) const {
return getSizeInBits() < VT.getSizeInBits();
}
bool bitsLE(MVT VT) const {
return getSizeInBits() <= VT.getSizeInBits();
}
SimpleValueType getSimpleVT() const {
assert(isSimple() && "Expected a SimpleValueType!");
return SimpleValueType(V);
}
MVT getVectorElementType() const {
assert(isVector() && "Invalid vector type!");
switch (V) {
default:
return getExtendedVectorElementType();
case v2i8 :
case v4i8 :
case v8i8 :
case v16i8: return i8;
case v2i16:
case v4i16:
case v8i16: return i16;
case v2i32:
case v3i32:
case v4i32: return i32;
case v1i64:
case v2i64: return i64;
case v2f32:
case v3f32:
case v4f32: return f32;
case v2f64: return f64;
}
}
unsigned getVectorNumElements() const {
assert(isVector() && "Invalid vector type!");
switch (V) {
default:
return getExtendedVectorNumElements();
case v16i8: return 16;
case v8i8 :
case v8i16: return 8;
case v4i8:
case v4i16:
case v4i32:
case v4f32: return 4;
case v3i32:
case v3f32: return 3;
case v2i8:
case v2i16:
case v2i32:
case v2i64:
case v2f32:
case v2f64: return 2;
case v1i64: return 1;
}
}
unsigned getSizeInBits() const {
switch (V) {
case iPTR:
assert(0 && "Value type size is target-dependent. Ask TLI.");
case iPTRAny:
case iAny:
case fAny:
assert(0 && "Value type is overloaded.");
default:
return getExtendedSizeInBits();
case i1 : return 1;
case i8 : return 8;
case i16 :
case v2i8: return 16;
case f32 :
case i32 :
case v4i8:
case v2i16: return 32;
case f64 :
case i64 :
case v8i8:
case v4i16:
case v2i32:
case v1i64:
case v2f32: return 64;
case f80 : return 80;
case v3i32:
case v3f32: return 96;
case f128:
case ppcf128:
case i128:
case v16i8:
case v8i16:
case v4i32:
case v2i64:
case v4f32:
case v2f64: return 128;
}
}
unsigned getStoreSizeInBits() const {
return (getSizeInBits() + 7)/8*8;
}
MVT getRoundIntegerType() const {
assert(isInteger() && !isVector() && "Invalid integer type!");
unsigned BitWidth = getSizeInBits();
if (BitWidth <= 8)
return i8;
else
return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
}
bool isPow2VectorType() const {
unsigned NElts = getVectorNumElements();
return !(NElts & (NElts - 1));
}
MVT getPow2VectorType() const {
if (!isPow2VectorType()) {
unsigned NElts = getVectorNumElements();
unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
}
else {
return *this;
}
}
std::string getMVTString() const;
const Type *getTypeForMVT() const;
static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
uintptr_t getRawBits() const { return V; }
struct compareRawBits {
bool operator()(MVT L, MVT R) const {
return L.getRawBits() < R.getRawBits();
}
};
private:
static MVT getExtendedIntegerVT(unsigned BitWidth);
static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
bool isExtendedFloatingPoint() const;
bool isExtendedInteger() const;
bool isExtendedVector() const;
bool isExtended64BitVector() const;
bool isExtended128BitVector() const;
MVT getExtendedVectorElementType() const;
unsigned getExtendedVectorNumElements() const;
unsigned getExtendedSizeInBits() const;
};
}
#endif