Initial commit for the AttributesImpl class.

This opaque class will contain all of the attributes. All attribute queries will
go through this object. This object will also be uniqued in the LLVMContext.
Currently not used, so no implementation change.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164722 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling
2012-09-26 21:07:29 +00:00
parent b500e9249a
commit 2c79ecbd70
5 changed files with 239 additions and 55 deletions

View File

@ -22,6 +22,7 @@
namespace llvm {
class LLVMContext;
class Type;
namespace Attribute {
@ -134,14 +135,125 @@ const AttrConst MutuallyIncompatible[5] = {
} // namespace Attribute
/// AttributeImpl - The internal representation of the Attributes class. This is
/// uniquified.
class AttributesImpl;
/// Attributes - A bitset of attributes.
class Attributes {
// Currently, we need less than 64 bits.
uint64_t Bits;
explicit Attributes(AttributesImpl *A);
public:
Attributes() : Bits(0) { }
explicit Attributes(uint64_t Val) : Bits(Val) { }
/*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) { }
Attributes() : Bits(0) {}
explicit Attributes(uint64_t Val) : Bits(Val) {}
/*implicit*/ Attributes(Attribute::AttrConst Val) : Bits(Val.v) {}
class Builder {
friend class Attributes;
uint64_t Bits;
public:
void addZExtAttr() {
Bits |= Attribute::ZExt_i;
}
void addSExtAttr() {
Bits |= Attribute::SExt_i;
}
void addNoReturnAttr() {
Bits |= Attribute::NoReturn_i;
}
void addInRegAttr() {
Bits |= Attribute::InReg_i;
}
void addStructRetAttr() {
Bits |= Attribute::StructRet_i;
}
void addNoUnwindAttr() {
Bits |= Attribute::NoUnwind_i;
}
void addNoAliasAttr() {
Bits |= Attribute::NoAlias_i;
}
void addByValAttr() {
Bits |= Attribute::ByVal_i;
}
void addNestAttr() {
Bits |= Attribute::Nest_i;
}
void addReadNoneAttr() {
Bits |= Attribute::ReadNone_i;
}
void addReadOnlyAttr() {
Bits |= Attribute::ReadOnly_i;
}
void addNoInlineAttr() {
Bits |= Attribute::NoInline_i;
}
void addAlwaysInlineAttr() {
Bits |= Attribute::AlwaysInline_i;
}
void addOptimizeForSizeAttr() {
Bits |= Attribute::OptimizeForSize_i;
}
void addStackProtectAttr() {
Bits |= Attribute::StackProtect_i;
}
void addStackProtectReqAttr() {
Bits |= Attribute::StackProtectReq_i;
}
void addAlignmentAttr() {
Bits |= Attribute::Alignment_i;
}
void addNoCaptureAttr() {
Bits |= Attribute::NoCapture_i;
}
void addNoRedZoneAttr() {
Bits |= Attribute::NoRedZone_i;
}
void addNoImplicitFloatAttr() {
Bits |= Attribute::NoImplicitFloat_i;
}
void addNakedAttr() {
Bits |= Attribute::Naked_i;
}
void addInlineHintAttr() {
Bits |= Attribute::InlineHint_i;
}
void addReturnsTwiceAttr() {
Bits |= Attribute::ReturnsTwice_i;
}
void addStackAlignmentAttr() {
Bits |= Attribute::StackAlignment_i;
}
void addUWTableAttr() {
Bits |= Attribute::UWTable_i;
}
void addNonLazyBindAttr() {
Bits |= Attribute::NonLazyBind_i;
}
void addAddressSafetyAttr() {
Bits |= Attribute::AddressSafety_i;
}
void addAlignmentAttr(unsigned Align) {
if (Align == 0) return;
assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
assert(Align <= 0x40000000 && "Alignment too large.");
Bits |= (Log2_32(Align) + 1) << 16;
}
void addStackAlignment(unsigned Align) {
// Default alignment, allow the target to define how to align it.
if (Align == 0) return;
assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
assert(Align <= 0x100 && "Alignment too large.");
Bits |= (Log2_32(Align) + 1) << 26;
}
};
/// get - Return a uniquified Attributes object. This takes the uniquified
/// value from the Builder and wraps it in the Attributes class.
static Attributes get(LLVMContext &Context, Builder &B);
// Attribute query methods.
// FIXME: StackAlignment & Alignment attributes have no predicate methods.
@ -234,20 +346,12 @@ public:
return Bits & Attribute::AddressSafety_i;
}
uint64_t getRawAlignment() const {
return Bits & Attribute::Alignment_i;
}
uint64_t getRawStackAlignment() const {
return Bits & Attribute::StackAlignment_i;
}
/// This returns the alignment field of an attribute as a byte alignment
/// value.
unsigned getAlignment() const {
if (!hasAlignmentAttr())
return 0;
return 1U << ((getRawAlignment() >> 16) - 1);
return 1U << (((Bits & Attribute::Alignment_i) >> 16) - 1);
}
/// This returns the stack alignment field of an attribute as a byte alignment
@ -255,32 +359,7 @@ public:
unsigned getStackAlignment() const {
if (!hasStackAlignmentAttr())
return 0;
return 1U << ((getRawStackAlignment() >> 26) - 1);
}
/// This turns an int alignment (a power of 2, normally) into the form used
/// internally in Attributes.
static Attributes constructAlignmentFromInt(unsigned i) {
// Default alignment, allow the target to define how to align it.
if (i == 0)
return Attribute::None;
assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
assert(i <= 0x40000000 && "Alignment too large.");
return Attributes((Log2_32(i)+1) << 16);
}
/// This turns an int stack alignment (which must be a power of 2) into the
/// form used internally in Attributes.
static Attributes constructStackAlignmentFromInt(unsigned i) {
// Default alignment, allow the target to define how to align it.
if (i == 0)
return Attribute::None;
assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
assert(i <= 0x100 && "Alignment too large.");
return Attributes((Log2_32(i)+1) << 26);
return 1U << (((Bits & Attribute::StackAlignment_i) >> 26) - 1);
}
// This is a "safe bool() operator".
@ -312,11 +391,29 @@ public:
Attributes operator ~ () const { return Attributes(~Bits); }
uint64_t Raw() const { return Bits; }
/// The set of Attributes set in Attributes is converted to a string of
/// equivalent mnemonics. This is, presumably, for writing out the mnemonics
/// for the assembly writer.
/// @brief Convert attribute bits to text
std::string getAsString() const;
/// This turns an int alignment (a power of 2, normally) into the form used
/// internally in Attributes.
static Attributes constructAlignmentFromInt(unsigned i) {
// Default alignment, allow the target to define how to align it.
if (i == 0)
return Attribute::None;
assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
assert(i <= 0x40000000 && "Alignment too large.");
return Attributes((Log2_32(i)+1) << 16);
}
/// This turns an int stack alignment (which must be a power of 2) into the
/// form used internally in Attributes.
static Attributes constructStackAlignmentFromInt(unsigned i) {
// Default alignment, allow the target to define how to align it.
if (i == 0)
return Attribute::None;
assert(isPowerOf2_32(i) && "Alignment must be a power of two.");
assert(i <= 0x100 && "Alignment too large.");
return Attributes((Log2_32(i)+1) << 26);
}
/// @brief Which attributes cannot be applied to a type.
static Attributes typeIncompatible(Type *Ty);
@ -337,10 +434,9 @@ public:
// bits.
uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
if (Attrs.hasAlignmentAttr())
EncodedAttrs |= (1ull << 16) <<
((Attrs.getRawAlignment() - 1) >> 16);
EncodedAttrs |= (Attrs.Raw() & (0xfffull << 21)) << 11;
EncodedAttrs |= (1ULL << 16) <<
(((Attrs.Bits & Attribute::Alignment_i) - 1) >> 16);
EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
return EncodedAttrs;
}
@ -350,26 +446,31 @@ public:
static Attributes decodeLLVMAttributesForBitcode(uint64_t EncodedAttrs) {
// The alignment is stored as a 16-bit raw value from bits 31--16. We shift
// the bits above 31 down by 11 bits.
unsigned Alignment = (EncodedAttrs & (0xffffull << 16)) >> 16;
unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
assert((!Alignment || isPowerOf2_32(Alignment)) &&
"Alignment must be a power of two.");
Attributes Attrs(EncodedAttrs & 0xffff);
if (Alignment)
Attrs |= Attributes::constructAlignmentFromInt(Alignment);
Attrs |= Attributes((EncodedAttrs & (0xfffull << 32)) >> 11);
Attrs |= Attributes((EncodedAttrs & (0xfffULL << 32)) >> 11);
return Attrs;
}
/// The set of Attributes set in Attributes is converted to a string of
/// equivalent mnemonics. This is, presumably, for writing out the mnemonics
/// for the assembly writer.
/// @brief Convert attribute bits to text
std::string getAsString() const;
};
/// This is just a pair of values to associate a set of attributes
/// with an index.
struct AttributeWithIndex {
Attributes Attrs; ///< The attributes that are set, or'd together.
unsigned Index; ///< Index of the parameter for which the attributes apply.
///< Index 0 is used for return value attributes.
///< Index ~0U is used for function attributes.
Attributes Attrs; ///< The attributes that are set, or'd together.
unsigned Index; ///< Index of the parameter for which the attributes apply.
///< Index 0 is used for return value attributes.
///< Index ~0U is used for function attributes.
static AttributeWithIndex get(unsigned Idx, Attributes Attrs) {
AttributeWithIndex P;