#ifndef LLVM_IR_DERIVEDTYPES_H
#define LLVM_IR_DERIVEDTYPES_H
#include "llvm/IR/Type.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
namespace llvm {
class Value;
class APInt;
class LLVMContext;
template<typename T> class ArrayRef;
class StringRef;
class IntegerType : public Type {
friend class LLVMContextImpl;
protected:
explicit IntegerType(LLVMContext &C, unsigned NumBits) : Type(C, IntegerTyID){
setSubclassData(NumBits);
}
public:
enum {
MIN_INT_BITS = 1, MAX_INT_BITS = (1<<23)-1 };
static IntegerType *get(LLVMContext &C, unsigned NumBits);
unsigned getBitWidth() const { return getSubclassData(); }
uint64_t getBitMask() const {
return ~uint64_t(0UL) >> (64-getBitWidth());
}
uint64_t getSignBit() const {
return 1ULL << (getBitWidth()-1);
}
APInt getMask() const;
bool isPowerOf2ByteWidth() const;
static inline bool classof(const Type *T) {
return T->getTypeID() == IntegerTyID;
}
};
unsigned Type::getIntegerBitWidth() const {
return cast<IntegerType>(this)->getBitWidth();
}
class FunctionType : public Type {
FunctionType(const FunctionType &) = delete;
const FunctionType &operator=(const FunctionType &) = delete;
FunctionType(Type *Result, ArrayRef<Type*> Params, bool IsVarArgs);
public:
static FunctionType *get(Type *Result,
ArrayRef<Type*> Params, bool isVarArg);
static FunctionType *get(Type *Result, bool isVarArg);
static bool isValidReturnType(Type *RetTy);
static bool isValidArgumentType(Type *ArgTy);
bool isVarArg() const { return getSubclassData()!=0; }
Type *getReturnType() const { return ContainedTys[0]; }
typedef Type::subtype_iterator param_iterator;
param_iterator param_begin() const { return ContainedTys + 1; }
param_iterator param_end() const { return &ContainedTys[NumContainedTys]; }
ArrayRef<Type *> params() const {
return makeArrayRef(param_begin(), param_end());
}
Type *getParamType(unsigned i) const { return ContainedTys[i+1]; }
unsigned getNumParams() const { return NumContainedTys - 1; }
static inline bool classof(const Type *T) {
return T->getTypeID() == FunctionTyID;
}
};
static_assert(AlignOf<FunctionType>::Alignment >= AlignOf<Type *>::Alignment,
"Alignment sufficient for objects appended to FunctionType");
bool Type::isFunctionVarArg() const {
return cast<FunctionType>(this)->isVarArg();
}
Type *Type::getFunctionParamType(unsigned i) const {
return cast<FunctionType>(this)->getParamType(i);
}
unsigned Type::getFunctionNumParams() const {
return cast<FunctionType>(this)->getNumParams();
}
class CompositeType : public Type {
protected:
explicit CompositeType(LLVMContext &C, TypeID tid) : Type(C, tid) {}
public:
Type *getTypeAtIndex(const Value *V) const;
Type *getTypeAtIndex(unsigned Idx) const;
bool indexValid(const Value *V) const;
bool indexValid(unsigned Idx) const;
static inline bool classof(const Type *T) {
return T->getTypeID() == ArrayTyID ||
T->getTypeID() == StructTyID ||
T->getTypeID() == PointerTyID ||
T->getTypeID() == VectorTyID;
}
};
class StructType : public CompositeType {
StructType(const StructType &) = delete;
const StructType &operator=(const StructType &) = delete;
StructType(LLVMContext &C)
: CompositeType(C, StructTyID), SymbolTableEntry(nullptr) {}
enum {
SCDB_HasBody = 1,
SCDB_Packed = 2,
SCDB_IsLiteral = 4,
SCDB_IsSized = 8
};
void *SymbolTableEntry;
public:
static StructType *create(LLVMContext &Context, StringRef Name);
static StructType *create(LLVMContext &Context);
static StructType *create(ArrayRef<Type *> Elements, StringRef Name,
bool isPacked = false);
static StructType *create(ArrayRef<Type *> Elements);
static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
StringRef Name, bool isPacked = false);
static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
bool isPacked = false);
static StructType *get(LLVMContext &Context, bool isPacked = false);
static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
bool isLiteral() const { return (getSubclassData() & SCDB_IsLiteral) != 0; }
bool isOpaque() const { return (getSubclassData() & SCDB_HasBody) == 0; }
bool isSized(SmallPtrSetImpl<Type *> *Visited = nullptr) const;
bool hasName() const { return SymbolTableEntry != nullptr; }
StringRef getName() const;
void setName(StringRef Name);
void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
static bool isValidElementType(Type *ElemTy);
typedef Type::subtype_iterator element_iterator;
element_iterator element_begin() const { return ContainedTys; }
element_iterator element_end() const { return &ContainedTys[NumContainedTys];}
ArrayRef<Type *> const elements() const {
return makeArrayRef(element_begin(), element_end());
}
bool isLayoutIdentical(StructType *Other) const;
unsigned getNumElements() const { return NumContainedTys; }
Type *getElementType(unsigned N) const {
assert(N < NumContainedTys && "Element number out of range!");
return ContainedTys[N];
}
static inline bool classof(const Type *T) {
return T->getTypeID() == StructTyID;
}
};
StringRef Type::getStructName() const {
return cast<StructType>(this)->getName();
}
unsigned Type::getStructNumElements() const {
return cast<StructType>(this)->getNumElements();
}
Type *Type::getStructElementType(unsigned N) const {
return cast<StructType>(this)->getElementType(N);
}
class SequentialType : public CompositeType {
Type *ContainedType; SequentialType(const SequentialType &) = delete;
const SequentialType &operator=(const SequentialType &) = delete;
protected:
SequentialType(TypeID TID, Type *ElType)
: CompositeType(ElType->getContext(), TID), ContainedType(ElType) {
ContainedTys = &ContainedType;
NumContainedTys = 1;
}
public:
Type *getElementType() const { return ContainedTys[0]; }
static inline bool classof(const Type *T) {
return T->getTypeID() == ArrayTyID ||
T->getTypeID() == PointerTyID ||
T->getTypeID() == VectorTyID;
}
};
Type *Type::getSequentialElementType() const {
return cast<SequentialType>(this)->getElementType();
}
class ArrayType : public SequentialType {
uint64_t NumElements;
ArrayType(const ArrayType &) = delete;
const ArrayType &operator=(const ArrayType &) = delete;
ArrayType(Type *ElType, uint64_t NumEl);
public:
static ArrayType *get(Type *ElementType, uint64_t NumElements);
static bool isValidElementType(Type *ElemTy);
uint64_t getNumElements() const { return NumElements; }
static inline bool classof(const Type *T) {
return T->getTypeID() == ArrayTyID;
}
};
uint64_t Type::getArrayNumElements() const {
return cast<ArrayType>(this)->getNumElements();
}
class VectorType : public SequentialType {
unsigned NumElements;
VectorType(const VectorType &) = delete;
const VectorType &operator=(const VectorType &) = delete;
VectorType(Type *ElType, unsigned NumEl);
public:
static VectorType *get(Type *ElementType, unsigned NumElements);
static VectorType *getInteger(VectorType *VTy) {
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
assert(EltBits && "Element size must be of a non-zero size");
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits);
return VectorType::get(EltTy, VTy->getNumElements());
}
static VectorType *getExtendedElementVectorType(VectorType *VTy) {
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits * 2);
return VectorType::get(EltTy, VTy->getNumElements());
}
static VectorType *getTruncatedElementVectorType(VectorType *VTy) {
unsigned EltBits = VTy->getElementType()->getPrimitiveSizeInBits();
assert((EltBits & 1) == 0 &&
"Cannot truncate vector element with odd bit-width");
Type *EltTy = IntegerType::get(VTy->getContext(), EltBits / 2);
return VectorType::get(EltTy, VTy->getNumElements());
}
static VectorType *getHalfElementsVectorType(VectorType *VTy) {
unsigned NumElts = VTy->getNumElements();
assert ((NumElts & 1) == 0 &&
"Cannot halve vector with odd number of elements.");
return VectorType::get(VTy->getElementType(), NumElts/2);
}
static VectorType *getDoubleElementsVectorType(VectorType *VTy) {
unsigned NumElts = VTy->getNumElements();
return VectorType::get(VTy->getElementType(), NumElts*2);
}
static bool isValidElementType(Type *ElemTy);
unsigned getNumElements() const { return NumElements; }
unsigned getBitWidth() const {
return NumElements * getElementType()->getPrimitiveSizeInBits();
}
static inline bool classof(const Type *T) {
return T->getTypeID() == VectorTyID;
}
};
unsigned Type::getVectorNumElements() const {
return cast<VectorType>(this)->getNumElements();
}
class PointerType : public SequentialType {
PointerType(const PointerType &) = delete;
const PointerType &operator=(const PointerType &) = delete;
explicit PointerType(Type *ElType, unsigned AddrSpace);
public:
static PointerType *get(Type *ElementType, unsigned AddressSpace);
static PointerType *getUnqual(Type *ElementType) {
return PointerType::get(ElementType, 0);
}
static bool isValidElementType(Type *ElemTy);
static bool isLoadableOrStorableType(Type *ElemTy);
inline unsigned getAddressSpace() const { return getSubclassData(); }
static inline bool classof(const Type *T) {
return T->getTypeID() == PointerTyID;
}
};
unsigned Type::getPointerAddressSpace() const {
return cast<PointerType>(getScalarType())->getAddressSpace();
}
}
#endif