Add a method to create an AttributeSet from an AttrBuilder.
authorBill Wendling <isanbard@gmail.com>
Sat, 5 Jan 2013 01:36:54 +0000 (01:36 +0000)
committerBill Wendling <isanbard@gmail.com>
Sat, 5 Jan 2013 01:36:54 +0000 (01:36 +0000)
The Attribute class is eventually going to represent one attribute. So we need
this class to create the set of attributes. Add some iterator methods to the
builder to access its internal bits in a nice way.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171586 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Attributes.h
lib/IR/AttributeImpl.h
lib/IR/Attributes.cpp

index e5a1ad294b59fc4d01a602029fe1c684266efd4c..28ec9db62e7888656755e82376c8edbddb60bf46 100644 (file)
@@ -116,10 +116,16 @@ public:
   /// value.
   unsigned getAlignment() const;
 
+  /// \brief Set the alignment field of an attribute.
+  void setAlignment(unsigned Align);
+
   /// \brief Returns the stack alignment field of an attribute as a byte
   /// alignment value.
   unsigned getStackAlignment() const;
 
+  /// \brief Set the stack alignment field of an attribute.
+  void setStackAlignment(unsigned Align);
+
   /// \brief Equality and non-equality query methods.
   bool operator==(AttrKind K) const;
   bool operator!=(AttrKind K) const;
@@ -233,6 +239,15 @@ public:
   /// the form used internally in Attribute.
   AttrBuilder &addStackAlignmentAttr(unsigned Align);
 
+  typedef DenseSet<Attribute::AttrKind>::iterator       iterator;
+  typedef DenseSet<Attribute::AttrKind>::const_iterator const_iterator;
+
+  iterator begin() { return Attrs.begin(); }
+  iterator end()   { return Attrs.end(); }
+
+  const_iterator begin() const { return Attrs.begin(); }
+  const_iterator end() const   { return Attrs.end(); }
+
   /// \brief Add the raw value to the internal representation.
   /// 
   /// N.B. This should be used ONLY for decoding LLVM bitcode!
@@ -329,6 +344,7 @@ public:
 
   /// \brief Return an AttributeSet with the specified parameters in it.
   static AttributeSet get(LLVMContext &C, ArrayRef<AttributeWithIndex> Attrs);
+  static AttributeSet get(LLVMContext &C, unsigned Idx, AttrBuilder &B);
 
   /// \brief Add the specified attribute at the specified index to this
   /// attribute list.  Since attribute lists are immutable, this returns the new
index 42b4fe36b22cd907b7daef5d2070eebff4abe00e..a2a5218d8069a2e929151beb84b417d59c6ab7aa 100644 (file)
@@ -29,6 +29,7 @@ class LLVMContext;
 /// \brief This class represents a single, uniqued attribute. That attribute
 /// could be a single enum, a tuple, or a string.
 class AttributeImpl : public FoldingSetNode {
+  LLVMContext &Context;
   Constant *Data;
   SmallVector<Constant*, 0> Vals;
 public:
@@ -47,7 +48,10 @@ public:
   bool hasAttributes() const;
 
   uint64_t getAlignment() const;
+  void setAlignment(unsigned Align);
+
   uint64_t getStackAlignment() const;
+  void setStackAlignment(unsigned Align);
 
   bool operator==(Attribute::AttrKind Kind) const;
   bool operator!=(Attribute::AttrKind Kind) const;
index 068b504294877e529359df4386d8a3a6ffb23142..84f90ade8b2aae92326569c1ddf761f7a3cd0732 100644 (file)
@@ -76,6 +76,12 @@ unsigned Attribute::getAlignment() const {
   return 1U << ((pImpl->getAlignment() >> 16) - 1);
 }
 
+void Attribute::setAlignment(unsigned Align) {
+  assert(hasAttribute(Attribute::Alignment) &&
+         "Trying to set the alignment on a non-alignment attribute!");
+  pImpl->setAlignment(Align);
+}
+
 /// This returns the stack alignment field of an attribute as a byte alignment
 /// value.
 unsigned Attribute::getStackAlignment() const {
@@ -84,6 +90,12 @@ unsigned Attribute::getStackAlignment() const {
   return 1U << ((pImpl->getStackAlignment() >> 26) - 1);
 }
 
+void Attribute::setStackAlignment(unsigned Align) {
+  assert(hasAttribute(Attribute::StackAlignment) &&
+         "Trying to set the stack alignment on a non-alignment attribute!");
+  pImpl->setStackAlignment(Align);
+}
+
 bool Attribute::operator==(AttrKind K) const {
   return pImpl && *pImpl == K;
 }
@@ -367,19 +379,23 @@ bool AttrBuilder::operator==(const AttrBuilder &B) {
 // AttributeImpl Definition
 //===----------------------------------------------------------------------===//
 
-AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data) {
+AttributeImpl::AttributeImpl(LLVMContext &C, uint64_t data)
+  : Context(C) {
   Data = ConstantInt::get(Type::getInt64Ty(C), data);
 }
-AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data) {
+AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data)
+  : Context(C) {
   Data = ConstantInt::get(Type::getInt64Ty(C), data);
 }
 AttributeImpl::AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
-                             ArrayRef<Constant*> values) {
+                             ArrayRef<Constant*> values)
+  : Context(C) {
   Data = ConstantInt::get(Type::getInt64Ty(C), data);
   Vals.reserve(values.size());
   Vals.append(values.begin(), values.end());
 }
-AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data) {
+AttributeImpl::AttributeImpl(LLVMContext &C, StringRef data)
+  : Context(C) {
   Data = ConstantDataArray::getString(C, data);
 }
 
@@ -456,10 +472,18 @@ uint64_t AttributeImpl::getAlignment() const {
   return getBitMask() & getAttrMask(Attribute::Alignment);
 }
 
+void AttributeImpl::setAlignment(unsigned Align) {
+  Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
+}
+
 uint64_t AttributeImpl::getStackAlignment() const {
   return getBitMask() & getAttrMask(Attribute::StackAlignment);
 }
 
+void AttributeImpl::setStackAlignment(unsigned Align) {
+  Vals.push_back(ConstantInt::get(Type::getInt64Ty(Context), Align));
+}
+
 //===----------------------------------------------------------------------===//
 // AttributeSetImpl Definition
 //===----------------------------------------------------------------------===//
@@ -485,8 +509,7 @@ AttributeSet AttributeSet::get(LLVMContext &C,
   AttributeSetImpl::Profile(ID, Attrs);
 
   void *InsertPoint;
-  AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
-                                                                InsertPoint);
+  AttributeSetImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID, InsertPoint);
 
   // If we didn't find any existing attributes of the same shape then
   // create a new one and insert it.
@@ -499,6 +522,23 @@ AttributeSet AttributeSet::get(LLVMContext &C,
   return AttributeSet(PA);
 }
 
+AttributeSet AttributeSet::get(LLVMContext &C, unsigned Idx, AttrBuilder &B) {
+  SmallVector<AttributeWithIndex, 8> Attrs;
+  for (AttrBuilder::iterator I = B.begin(), E = B.end(); I != E; ++I) {
+    Attribute::AttrKind Kind = *I;
+    Attribute A = Attribute::get(C, Kind);
+
+    if (Kind == Attribute::Alignment)
+      A.setAlignment(B.getAlignment());
+    else if (Kind == Attribute::StackAlignment)
+      A.setStackAlignment(B.getStackAlignment());
+
+    Attrs.push_back(AttributeWithIndex::get(Idx, A));
+  }
+
+  return get(C, Attrs);
+}
+
 //===----------------------------------------------------------------------===//
 // AttributeSet Method Implementations
 //===----------------------------------------------------------------------===//