#ifndef LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
#define LLVM_CLANG_LIB_CODEGEN_ADDRESS_H
#include "clang/AST/CharUnits.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/IR/Constants.h"
#include "llvm/Support/MathExtras.h"
namespace clang {
namespace CodeGen {
template <typename T, bool = alignof(llvm::Value *) >= 8> class AddressImpl {};
template <typename T> class AddressImpl<T, false> {
llvm::Value *Pointer;
llvm::Type *ElementType;
CharUnits Alignment;
public:
AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType,
CharUnits Alignment)
: Pointer(Pointer), ElementType(ElementType), Alignment(Alignment) {}
llvm::Value *getPointer() const { return Pointer; }
llvm::Type *getElementType() const { return ElementType; }
CharUnits getAlignment() const { return Alignment; }
};
template <typename T> class AddressImpl<T, true> {
llvm::PointerIntPair<llvm::Value *, 3, unsigned> Pointer;
llvm::PointerIntPair<llvm::Type *, 3, unsigned> ElementType;
public:
AddressImpl(llvm::Value *Pointer, llvm::Type *ElementType,
CharUnits Alignment)
: Pointer(Pointer), ElementType(ElementType) {
if (Alignment.isZero())
return;
assert(Alignment.isPowerOfTwo() && "Alignment cannot be zero");
auto AlignLog = llvm::Log2_64(Alignment.getQuantity());
assert(AlignLog < (1 << 6) && "cannot fit alignment into 6 bits");
this->Pointer.setInt(AlignLog >> 3);
this->ElementType.setInt(AlignLog & 7);
}
llvm::Value *getPointer() const { return Pointer.getPointer(); }
llvm::Type *getElementType() const { return ElementType.getPointer(); }
CharUnits getAlignment() const {
unsigned AlignLog = (Pointer.getInt() << 3) | ElementType.getInt();
return CharUnits::fromQuantity(CharUnits::QuantityType(1) << AlignLog);
}
};
class Address {
AddressImpl<void> A;
protected:
Address(std::nullptr_t) : A(nullptr, nullptr, CharUnits::Zero()) {}
public:
Address(llvm::Value *Pointer, llvm::Type *ElementType, CharUnits Alignment)
: A(Pointer, ElementType, Alignment) {
assert(Pointer != nullptr && "Pointer cannot be null");
assert(ElementType != nullptr && "Element type cannot be null");
assert(llvm::cast<llvm::PointerType>(Pointer->getType())
->isOpaqueOrPointeeTypeMatches(ElementType) &&
"Incorrect pointer element type");
}
static Address invalid() { return Address(nullptr); }
bool isValid() const { return A.getPointer() != nullptr; }
llvm::Value *getPointer() const {
assert(isValid());
return A.getPointer();
}
llvm::PointerType *getType() const {
return llvm::cast<llvm::PointerType>(getPointer()->getType());
}
llvm::Type *getElementType() const {
assert(isValid());
return A.getElementType();
}
unsigned getAddressSpace() const {
return getType()->getAddressSpace();
}
llvm::StringRef getName() const {
return getPointer()->getName();
}
CharUnits getAlignment() const {
assert(isValid());
return A.getAlignment();
}
Address withPointer(llvm::Value *NewPointer) const {
return Address(NewPointer, getElementType(), getAlignment());
}
Address withAlignment(CharUnits NewAlignment) const {
return Address(getPointer(), getElementType(), NewAlignment);
}
};
class ConstantAddress : public Address {
ConstantAddress(std::nullptr_t) : Address(nullptr) {}
public:
ConstantAddress(llvm::Constant *pointer, llvm::Type *elementType,
CharUnits alignment)
: Address(pointer, elementType, alignment) {}
static ConstantAddress invalid() {
return ConstantAddress(nullptr);
}
llvm::Constant *getPointer() const {
return llvm::cast<llvm::Constant>(Address::getPointer());
}
ConstantAddress getElementBitCast(llvm::Type *ElemTy) const {
llvm::Constant *BitCast = llvm::ConstantExpr::getBitCast(
getPointer(), ElemTy->getPointerTo(getAddressSpace()));
return ConstantAddress(BitCast, ElemTy, getAlignment());
}
static bool isaImpl(Address addr) {
return llvm::isa<llvm::Constant>(addr.getPointer());
}
static ConstantAddress castImpl(Address addr) {
return ConstantAddress(llvm::cast<llvm::Constant>(addr.getPointer()),
addr.getElementType(), addr.getAlignment());
}
};
}
template <class U> inline U cast(CodeGen::Address addr) {
return U::castImpl(addr);
}
template <class U> inline bool isa(CodeGen::Address addr) {
return U::isaImpl(addr);
}
}
#endif