[llvm] r187682 - Make one of the AttributeSet ctors maintain the invariant that the
Peter Collingbourne
peter at pcc.me.uk
Fri Aug 2 15:29:40 PDT 2013
Author: pcc
Date: Fri Aug 2 17:29:40 2013
New Revision: 187682
URL: http://llvm.org/viewvc/llvm-project?rev=187682&view=rev
Log:
Make one of the AttributeSet ctors maintain the invariant that the
attribute list is ordered by index.
Differential Revision: http://llvm-reviews.chandlerc.com/D1265
Modified:
llvm/trunk/lib/IR/AttributeImpl.h
llvm/trunk/lib/IR/Attributes.cpp
llvm/trunk/unittests/IR/AttributesTest.cpp
Modified: llvm/trunk/lib/IR/AttributeImpl.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/AttributeImpl.h?rev=187682&r1=187681&r2=187682&view=diff
==============================================================================
--- llvm/trunk/lib/IR/AttributeImpl.h (original)
+++ llvm/trunk/lib/IR/AttributeImpl.h Fri Aug 2 17:29:40 2013
@@ -200,6 +200,15 @@ public:
AttributeSetImpl(LLVMContext &C,
ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
: Context(C), NumAttrs(Attrs.size()) {
+#ifndef NDEBUG
+ if (Attrs.size() >= 2) {
+ for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
+ *e = Attrs.end();
+ i != e; ++i) {
+ assert((i-1)->first <= i->first && "Attribute set not ordered!");
+ }
+ }
+#endif
// There's memory after the node where we can store the entries in.
std::copy(Attrs.begin(), Attrs.end(),
reinterpret_cast<IndexAttrPair *>(this + 1));
Modified: llvm/trunk/lib/IR/Attributes.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Attributes.cpp?rev=187682&r1=187681&r2=187682&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Attributes.cpp (original)
+++ llvm/trunk/lib/IR/Attributes.cpp Fri Aug 2 17:29:40 2013
@@ -621,12 +621,30 @@ AttributeSet AttributeSet::get(LLVMConte
AttributeSet AttributeSet::get(LLVMContext &C, ArrayRef<AttributeSet> Attrs) {
if (Attrs.empty()) return AttributeSet();
+ if (Attrs.size() == 1) return Attrs[0];
SmallVector<std::pair<unsigned, AttributeSetNode*>, 8> AttrNodeVec;
- for (unsigned I = 0, E = Attrs.size(); I != E; ++I) {
+ AttributeSetImpl *A0 = Attrs[0].pImpl;
+ if (A0)
+ AttrNodeVec.append(A0->getNode(0), A0->getNode(A0->getNumAttributes()));
+ // Copy all attributes from Attrs into AttrNodeVec while keeping AttrNodeVec
+ // ordered by index. Because we know that each list in Attrs is ordered by
+ // index we only need to merge each successive list in rather than doing a
+ // full sort.
+ for (unsigned I = 1, E = Attrs.size(); I != E; ++I) {
AttributeSetImpl *AS = Attrs[I].pImpl;
if (!AS) continue;
- AttrNodeVec.append(AS->getNode(0), AS->getNode(AS->getNumAttributes()));
+ SmallVector<std::pair<unsigned, AttributeSetNode *>, 8>::iterator
+ ANVI = AttrNodeVec.begin(), ANVE;
+ for (const AttributeSetImpl::IndexAttrPair
+ *AI = AS->getNode(0),
+ *AE = AS->getNode(AS->getNumAttributes());
+ AI != AE; ++AI) {
+ ANVE = AttrNodeVec.end();
+ while (ANVI != ANVE && ANVI->first <= AI->first)
+ ++ANVI;
+ ANVI = AttrNodeVec.insert(ANVI, *AI) + 1;
+ }
}
return getImpl(C, AttrNodeVec);
Modified: llvm/trunk/unittests/IR/AttributesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/AttributesTest.cpp?rev=187682&r1=187681&r2=187682&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/AttributesTest.cpp (original)
+++ llvm/trunk/unittests/IR/AttributesTest.cpp Fri Aug 2 17:29:40 2013
@@ -31,4 +31,17 @@ TEST(Attributes, Uniquing) {
EXPECT_EQ(SetA, SetB);
}
+TEST(Attributes, Ordering) {
+ LLVMContext C;
+
+ AttributeSet ASs[] = {
+ AttributeSet::get(C, 2, Attribute::ZExt),
+ AttributeSet::get(C, 1, Attribute::SExt)
+ };
+
+ AttributeSet SetA = AttributeSet::get(C, ASs);
+ AttributeSet SetB = SetA.removeAttributes(C, 1, ASs[1]);
+ EXPECT_NE(SetA, SetB);
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list