#ifndef LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
#define LLVM_CLANG_LIB_CODEGEN_CGVALUE_H
#include "Address.h"
#include "CGPointerAuthInfo.h"
#include "CodeGenTBAA.h"
#include "EHScopeStack.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
namespace llvm {
class Constant;
class MDNode;
}
namespace clang {
namespace CodeGen {
class AggValueSlot;
class CGBuilderTy;
class CodeGenFunction;
struct CGBitFieldInfo;
class RValue {
friend struct DominatingValue<RValue>;
enum FlavorEnum { Scalar, Complex, Aggregate };
union {
struct {
llvm::Value *first;
llvm::Value *second;
} Vals;
Address AggregateAddr;
};
unsigned IsVolatile : 1;
unsigned Flavor : 2;
public:
RValue() : Vals{nullptr, nullptr}, Flavor(Scalar) {}
bool isScalar() const { return Flavor == Scalar; }
bool isComplex() const { return Flavor == Complex; }
bool isAggregate() const { return Flavor == Aggregate; }
bool isVolatileQualified() const { return IsVolatile; }
llvm::Value *getScalarVal() const {
assert(isScalar() && "Not a scalar!");
return Vals.first;
}
std::pair<llvm::Value *, llvm::Value *> getComplexVal() const {
return std::make_pair(Vals.first, Vals.second);
}
Address getAggregateAddress() const {
assert(isAggregate() && "Not an aggregate!");
return AggregateAddr;
}
llvm::Value *getAggregatePointer(QualType PointeeType,
CodeGenFunction &CGF) const {
return getAggregateAddress().getBasePointer();
}
static RValue getIgnored() {
return get(nullptr);
}
static RValue get(llvm::Value *V) {
RValue ER;
ER.Vals.first = V;
ER.Flavor = Scalar;
ER.IsVolatile = false;
return ER;
}
static RValue get(Address Addr, CodeGenFunction &CGF) {
return RValue::get(Addr.emitRawPointer(CGF));
}
static RValue getComplex(llvm::Value *V1, llvm::Value *V2) {
RValue ER;
ER.Vals = {V1, V2};
ER.Flavor = Complex;
ER.IsVolatile = false;
return ER;
}
static RValue getComplex(const std::pair<llvm::Value *, llvm::Value *> &C) {
return getComplex(C.first, C.second);
}
static RValue getAggregate(Address addr, bool isVolatile = false) {
RValue ER;
ER.AggregateAddr = addr;
ER.Flavor = Aggregate;
ER.IsVolatile = isVolatile;
return ER;
}
};
enum ARCPreciseLifetime_t {
ARCImpreciseLifetime, ARCPreciseLifetime
};
enum class AlignmentSource {
Decl,
AttributedType,
Type
};
static inline AlignmentSource getFieldAlignmentSource(AlignmentSource Source) {
return AlignmentSource::Decl;
}
class LValueBaseInfo {
AlignmentSource AlignSource;
public:
explicit LValueBaseInfo(AlignmentSource Source = AlignmentSource::Type)
: AlignSource(Source) {}
AlignmentSource getAlignmentSource() const { return AlignSource; }
void setAlignmentSource(AlignmentSource Source) { AlignSource = Source; }
void mergeForCast(const LValueBaseInfo &Info) {
setAlignmentSource(Info.getAlignmentSource());
}
};
class LValue {
enum {
Simple,
VectorElt,
BitField,
ExtVectorElt,
GlobalReg,
MatrixElt
} LVType;
union {
Address Addr = Address::invalid();
llvm::Value *V;
};
union {
llvm::Value *VectorIdx;
llvm::Constant *VectorElts;
const CGBitFieldInfo *BitFieldInfo;
};
QualType Type;
Qualifiers Quals;
bool Ivar:1;
bool ObjIsArray:1;
bool NonGC: 1;
bool GlobalObjCRef : 1;
bool ThreadLocalRef : 1;
bool ImpreciseLifetime : 1;
bool Nontemporal : 1;
LValueBaseInfo BaseInfo;
TBAAAccessInfo TBAAInfo;
Expr *BaseIvarExp;
private:
void Initialize(QualType Type, Qualifiers Quals, Address Addr,
LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
this->Type = Type;
this->Quals = Quals;
const unsigned MaxAlign = 1U << 31;
CharUnits Alignment = Addr.getAlignment();
assert((isGlobalReg() || !Alignment.isZero() || Type->isIncompleteType()) &&
"initializing l-value with zero alignment!");
if (Alignment.getQuantity() > MaxAlign) {
assert(false && "Alignment exceeds allowed max!");
Alignment = CharUnits::fromQuantity(MaxAlign);
}
this->Addr = Addr;
this->BaseInfo = BaseInfo;
this->TBAAInfo = TBAAInfo;
this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false;
this->ImpreciseLifetime = false;
this->Nontemporal = false;
this->ThreadLocalRef = false;
this->BaseIvarExp = nullptr;
}
void initializeSimpleLValue(Address Addr, QualType Type,
LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo,
ASTContext &Context) {
Qualifiers QS = Type.getQualifiers();
QS.setObjCGCAttr(Context.getObjCGCAttrKind(Type));
LVType = Simple;
Initialize(Type, QS, Addr, BaseInfo, TBAAInfo);
assert(Addr.getBasePointer()->getType()->isPointerTy());
}
public:
bool isSimple() const { return LVType == Simple; }
bool isVectorElt() const { return LVType == VectorElt; }
bool isBitField() const { return LVType == BitField; }
bool isExtVectorElt() const { return LVType == ExtVectorElt; }
bool isGlobalReg() const { return LVType == GlobalReg; }
bool isMatrixElt() const { return LVType == MatrixElt; }
bool isVolatileQualified() const { return Quals.hasVolatile(); }
bool isRestrictQualified() const { return Quals.hasRestrict(); }
unsigned getVRQualifiers() const {
return Quals.getCVRQualifiers() & ~Qualifiers::Const;
}
QualType getType() const { return Type; }
Qualifiers::ObjCLifetime getObjCLifetime() const {
return Quals.getObjCLifetime();
}
bool isObjCIvar() const { return Ivar; }
void setObjCIvar(bool Value) { Ivar = Value; }
bool isObjCArray() const { return ObjIsArray; }
void setObjCArray(bool Value) { ObjIsArray = Value; }
bool isNonGC () const { return NonGC; }
void setNonGC(bool Value) { NonGC = Value; }
bool isGlobalObjCRef() const { return GlobalObjCRef; }
void setGlobalObjCRef(bool Value) { GlobalObjCRef = Value; }
bool isThreadLocalRef() const { return ThreadLocalRef; }
void setThreadLocalRef(bool Value) { ThreadLocalRef = Value;}
ARCPreciseLifetime_t isARCPreciseLifetime() const {
return ARCPreciseLifetime_t(!ImpreciseLifetime);
}
void setARCPreciseLifetime(ARCPreciseLifetime_t value) {
ImpreciseLifetime = (value == ARCImpreciseLifetime);
}
bool isNontemporal() const { return Nontemporal; }
void setNontemporal(bool Value) { Nontemporal = Value; }
bool isObjCWeak() const {
return Quals.getObjCGCAttr() == Qualifiers::Weak;
}
bool isObjCStrong() const {
return Quals.getObjCGCAttr() == Qualifiers::Strong;
}
bool isVolatile() const {
return Quals.hasVolatile();
}
Expr *getBaseIvarExp() const { return BaseIvarExp; }
void setBaseIvarExp(Expr *V) { BaseIvarExp = V; }
TBAAAccessInfo getTBAAInfo() const { return TBAAInfo; }
void setTBAAInfo(TBAAAccessInfo Info) { TBAAInfo = Info; }
const Qualifiers &getQuals() const { return Quals; }
Qualifiers &getQuals() { return Quals; }
LangAS getAddressSpace() const { return Quals.getAddressSpace(); }
CharUnits getAlignment() const { return Addr.getAlignment(); }
void setAlignment(CharUnits A) { Addr.setAlignment(A); }
LValueBaseInfo getBaseInfo() const { return BaseInfo; }
void setBaseInfo(LValueBaseInfo Info) { BaseInfo = Info; }
KnownNonNull_t isKnownNonNull() const { return Addr.isKnownNonNull(); }
LValue setKnownNonNull() {
Addr.setKnownNonNull();
return *this;
}
llvm::Value *getPointer(CodeGenFunction &CGF) const;
llvm::Value *emitResignedPointer(QualType PointeeTy,
CodeGenFunction &CGF) const;
llvm::Value *emitRawPointer(CodeGenFunction &CGF) const;
Address getAddress() const { return Addr; }
void setAddress(Address address) { Addr = address; }
CGPointerAuthInfo getPointerAuthInfo() const {
return Addr.getPointerAuthInfo();
}
Address getVectorAddress() const {
assert(isVectorElt());
return Addr;
}
llvm::Value *getRawVectorPointer(CodeGenFunction &CGF) const {
assert(isVectorElt());
return Addr.emitRawPointer(CGF);
}
llvm::Value *getVectorPointer() const {
assert(isVectorElt());
return Addr.getBasePointer();
}
llvm::Value *getVectorIdx() const {
assert(isVectorElt());
return VectorIdx;
}
Address getMatrixAddress() const {
assert(isMatrixElt());
return Addr;
}
llvm::Value *getMatrixPointer() const {
assert(isMatrixElt());
return Addr.getBasePointer();
}
llvm::Value *getMatrixIdx() const {
assert(isMatrixElt());
return VectorIdx;
}
Address getExtVectorAddress() const {
assert(isExtVectorElt());
return Addr;
}
llvm::Value *getRawExtVectorPointer(CodeGenFunction &CGF) const {
assert(isExtVectorElt());
return Addr.emitRawPointer(CGF);
}
llvm::Constant *getExtVectorElts() const {
assert(isExtVectorElt());
return VectorElts;
}
Address getBitFieldAddress() const {
assert(isBitField());
return Addr;
}
llvm::Value *getRawBitFieldPointer(CodeGenFunction &CGF) const {
assert(isBitField());
return Addr.emitRawPointer(CGF);
}
const CGBitFieldInfo &getBitFieldInfo() const {
assert(isBitField());
return *BitFieldInfo;
}
llvm::Value *getGlobalReg() const { assert(isGlobalReg()); return V; }
static LValue MakeAddr(Address Addr, QualType type, ASTContext &Context,
LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo) {
LValue R;
R.LVType = Simple;
R.initializeSimpleLValue(Addr, type, BaseInfo, TBAAInfo, Context);
R.Addr = Addr;
assert(Addr.getType()->isPointerTy());
return R;
}
static LValue MakeVectorElt(Address vecAddress, llvm::Value *Idx,
QualType type, LValueBaseInfo BaseInfo,
TBAAAccessInfo TBAAInfo) {
LValue R;
R.LVType = VectorElt;
R.VectorIdx = Idx;
R.Initialize(type, type.getQualifiers(), vecAddress, BaseInfo, TBAAInfo);
return R;
}
static LValue MakeExtVectorElt(Address Addr, llvm::Constant *Elts,
QualType type, LValueBaseInfo BaseInfo,
TBAAAccessInfo TBAAInfo) {
LValue R;
R.LVType = ExtVectorElt;
R.VectorElts = Elts;
R.Initialize(type, type.getQualifiers(), Addr, BaseInfo, TBAAInfo);
return R;
}
static LValue MakeBitfield(Address Addr, const CGBitFieldInfo &Info,
QualType type, LValueBaseInfo BaseInfo,
TBAAAccessInfo TBAAInfo) {
LValue R;
R.LVType = BitField;
R.BitFieldInfo = &Info;
R.Initialize(type, type.getQualifiers(), Addr, BaseInfo, TBAAInfo);
return R;
}
static LValue MakeGlobalReg(llvm::Value *V, CharUnits alignment,
QualType type) {
LValue R;
R.LVType = GlobalReg;
R.Initialize(type, type.getQualifiers(), Address::invalid(),
LValueBaseInfo(AlignmentSource::Decl), TBAAAccessInfo());
R.V = V;
return R;
}
static LValue MakeMatrixElt(Address matAddress, llvm::Value *Idx,
QualType type, LValueBaseInfo BaseInfo,
TBAAAccessInfo TBAAInfo) {
LValue R;
R.LVType = MatrixElt;
R.VectorIdx = Idx;
R.Initialize(type, type.getQualifiers(), matAddress, BaseInfo, TBAAInfo);
return R;
}
RValue asAggregateRValue() const {
return RValue::getAggregate(getAddress(), isVolatileQualified());
}
};
class AggValueSlot {
Address Addr;
Qualifiers Quals;
bool DestructedFlag : 1;
bool ObjCGCFlag : 1;
bool ZeroedFlag : 1;
bool AliasedFlag : 1;
bool OverlapFlag : 1;
bool SanitizerCheckedFlag : 1;
AggValueSlot(Address Addr, Qualifiers Quals, bool DestructedFlag,
bool ObjCGCFlag, bool ZeroedFlag, bool AliasedFlag,
bool OverlapFlag, bool SanitizerCheckedFlag)
: Addr(Addr), Quals(Quals), DestructedFlag(DestructedFlag),
ObjCGCFlag(ObjCGCFlag), ZeroedFlag(ZeroedFlag),
AliasedFlag(AliasedFlag), OverlapFlag(OverlapFlag),
SanitizerCheckedFlag(SanitizerCheckedFlag) {}
public:
enum IsAliased_t { IsNotAliased, IsAliased };
enum IsDestructed_t { IsNotDestructed, IsDestructed };
enum IsZeroed_t { IsNotZeroed, IsZeroed };
enum Overlap_t { DoesNotOverlap, MayOverlap };
enum NeedsGCBarriers_t { DoesNotNeedGCBarriers, NeedsGCBarriers };
enum IsSanitizerChecked_t { IsNotSanitizerChecked, IsSanitizerChecked };
static AggValueSlot ignored() {
return forAddr(Address::invalid(), Qualifiers(), IsNotDestructed,
DoesNotNeedGCBarriers, IsNotAliased, DoesNotOverlap);
}
static AggValueSlot forAddr(Address addr,
Qualifiers quals,
IsDestructed_t isDestructed,
NeedsGCBarriers_t needsGC,
IsAliased_t isAliased,
Overlap_t mayOverlap,
IsZeroed_t isZeroed = IsNotZeroed,
IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
if (addr.isValid())
addr.setKnownNonNull();
return AggValueSlot(addr, quals, isDestructed, needsGC, isZeroed, isAliased,
mayOverlap, isChecked);
}
static AggValueSlot
forLValue(const LValue &LV, IsDestructed_t isDestructed,
NeedsGCBarriers_t needsGC, IsAliased_t isAliased,
Overlap_t mayOverlap, IsZeroed_t isZeroed = IsNotZeroed,
IsSanitizerChecked_t isChecked = IsNotSanitizerChecked) {
return forAddr(LV.getAddress(), LV.getQuals(), isDestructed, needsGC,
isAliased, mayOverlap, isZeroed, isChecked);
}
IsDestructed_t isExternallyDestructed() const {
return IsDestructed_t(DestructedFlag);
}
void setExternallyDestructed(bool destructed = true) {
DestructedFlag = destructed;
}
Qualifiers getQualifiers() const { return Quals; }
bool isVolatile() const {
return Quals.hasVolatile();
}
void setVolatile(bool flag) {
if (flag)
Quals.addVolatile();
else
Quals.removeVolatile();
}
Qualifiers::ObjCLifetime getObjCLifetime() const {
return Quals.getObjCLifetime();
}
NeedsGCBarriers_t requiresGCollection() const {
return NeedsGCBarriers_t(ObjCGCFlag);
}
llvm::Value *getPointer(QualType PointeeTy, CodeGenFunction &CGF) const;
llvm::Value *emitRawPointer(CodeGenFunction &CGF) const {
return Addr.isValid() ? Addr.emitRawPointer(CGF) : nullptr;
}
Address getAddress() const {
return Addr;
}
bool isIgnored() const { return !Addr.isValid(); }
CharUnits getAlignment() const {
return Addr.getAlignment();
}
IsAliased_t isPotentiallyAliased() const {
return IsAliased_t(AliasedFlag);
}
Overlap_t mayOverlap() const {
return Overlap_t(OverlapFlag);
}
bool isSanitizerChecked() const {
return SanitizerCheckedFlag;
}
RValue asRValue() const {
if (isIgnored()) {
return RValue::getIgnored();
} else {
return RValue::getAggregate(getAddress(), isVolatile());
}
}
void setZeroed(bool V = true) { ZeroedFlag = V; }
IsZeroed_t isZeroed() const {
return IsZeroed_t(ZeroedFlag);
}
CharUnits getPreferredSize(ASTContext &Ctx, QualType Type) const {
return mayOverlap() ? Ctx.getTypeInfoDataSizeInChars(Type).Width
: Ctx.getTypeSizeInChars(Type);
}
};
}
}
#endif