[PATCH] D16617: AttributeSetNode: Summarize existing attributes in a bitset.

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 26 19:39:44 PST 2016


MatzeB created this revision.
MatzeB added reviewers: void, bkramer, jyknight, hfinkel.
MatzeB added a subscriber: llvm-commits.
MatzeB set the repository for this revision to rL LLVM.
Herald added a subscriber: mcrosier.

The majority of queries just checks for the existince of an enum
attribute.  We only have 48 of those and can summaryiz them in an
uint64_t bitfield so we can avoid searching the list. This improves
"opt" compile time by 1-4% in my measurements.

Repository:
  rL LLVM

http://reviews.llvm.org/D16617

Files:
  lib/IR/AttributeImpl.h
  lib/IR/Attributes.cpp

Index: lib/IR/Attributes.cpp
===================================================================
--- lib/IR/Attributes.cpp
+++ lib/IR/Attributes.cpp
@@ -498,24 +498,19 @@
   return PA;
 }
 
-bool AttributeSetNode::hasAttribute(Attribute::AttrKind Kind) const {
-  for (iterator I = begin(), E = end(); I != E; ++I)
-    if (I->hasAttribute(Kind))
-      return true;
-  return false;
-}
-
 bool AttributeSetNode::hasAttribute(StringRef Kind) const {
   for (iterator I = begin(), E = end(); I != E; ++I)
     if (I->hasAttribute(Kind))
       return true;
   return false;
 }
 
 Attribute AttributeSetNode::getAttribute(Attribute::AttrKind Kind) const {
-  for (iterator I = begin(), E = end(); I != E; ++I)
-    if (I->hasAttribute(Kind))
-      return *I;
+  if (hasAttribute(Kind)) {
+    for (iterator I = begin(), E = end(); I != E; ++I)
+      if (I->hasAttribute(Kind))
+        return *I;
+  }
   return Attribute();
 }
 
Index: lib/IR/AttributeImpl.h
===================================================================
--- lib/IR/AttributeImpl.h
+++ lib/IR/AttributeImpl.h
@@ -148,19 +148,32 @@
   friend TrailingObjects;
 
   unsigned NumAttrs; ///< Number of attributes in this node.
+  /// Bitset with a bit for each available attribute Attribute::AttrKind.
+  uint64_t AvailableAttrs;
+  static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs)*CHAR_BIT,
+                "Too many attributes for AvailableAttrs");
 
-  AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
+  AttributeSetNode(ArrayRef<Attribute> Attrs)
+    : NumAttrs(Attrs.size()), AvailableAttrs(0) {
     // There's memory after the node where we can store the entries in.
     std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
+
+    for (iterator I = begin(), E = end(); I != E; ++I) {
+      if (!I->isStringAttribute()) {
+        AvailableAttrs |= ((uint64_t)1) << I->getKindAsEnum();
+      }
+    }
   }
 
   // AttributesSetNode is uniqued, these should not be publicly available.
   void operator=(const AttributeSetNode &) = delete;
   AttributeSetNode(const AttributeSetNode &) = delete;
 public:
   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
 
-  bool hasAttribute(Attribute::AttrKind Kind) const;
+  bool hasAttribute(Attribute::AttrKind Kind) const {
+    return AvailableAttrs & ((uint64_t)1) << Kind;
+  }
   bool hasAttribute(StringRef Kind) const;
   bool hasAttributes() const { return NumAttrs != 0; }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16617.46090.patch
Type: text/x-patch
Size: 2485 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160127/26607217/attachment.bin>


More information about the llvm-commits mailing list