Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 3f213e7

Browse files
committed
Futureproof AttrBuild if we ever have more than 64 attr enum values.
Currently we're at 34. Bitset should compile into virtually the same code as uint64_t here. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175437 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 32d0b2a commit 3f213e7

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

include/llvm/IR/Attributes.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/FoldingSet.h"
2121
#include "llvm/Support/PointerLikeTypeTraits.h"
22+
#include <bitset>
2223
#include <cassert>
2324
#include <map>
2425
#include <string>
@@ -378,7 +379,7 @@ template<> struct DenseMapInfo<AttributeSet> {
378379
/// value, however, is not. So this can be used as a quick way to test for
379380
/// equality, presence of attributes, etc.
380381
class AttrBuilder {
381-
uint64_t Attrs;
382+
std::bitset<Attribute::EndAttrKinds> Attrs;
382383
std::map<std::string, std::string> TargetDepAttrs;
383384
uint64_t Alignment;
384385
uint64_t StackAlignment;
@@ -422,9 +423,8 @@ class AttrBuilder {
422423

423424
/// \brief Return true if the builder has the specified attribute.
424425
bool contains(Attribute::AttrKind A) const {
425-
assert((unsigned)A < 64 && A < Attribute::EndAttrKinds &&
426-
"Attribute out of range!");
427-
return Attrs & (1ULL << A);
426+
assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
427+
return Attrs[A];
428428
}
429429

430430
/// \brief Return true if the builder has the specified target-dependent
@@ -457,7 +457,7 @@ class AttrBuilder {
457457

458458
/// \brief Return true if the builder contains no target-independent
459459
/// attributes.
460-
bool empty() const { return Attrs == 0; }
460+
bool empty() const { return Attrs.none(); }
461461

462462
// Iterators for target-dependent attributes.
463463
typedef std::pair<std::string, std::string> td_type;

lib/IR/Attributes.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -924,16 +924,15 @@ AttrBuilder::AttrBuilder(AttributeSet AS, unsigned Idx)
924924
}
925925

926926
void AttrBuilder::clear() {
927-
Attrs = 0;
927+
Attrs.reset();
928928
Alignment = StackAlignment = 0;
929929
}
930930

931931
AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
932-
assert((unsigned)Val < 64 && Val < Attribute::EndAttrKinds &&
933-
"Attribute out of range!");
932+
assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
934933
assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
935934
"Adding alignment attribute without adding alignment value!");
936-
Attrs |= 1ULL << Val;
935+
Attrs[Val] = true;
937936
return *this;
938937
}
939938

@@ -944,7 +943,7 @@ AttrBuilder &AttrBuilder::addAttribute(Attribute Attr) {
944943
}
945944

946945
Attribute::AttrKind Kind = Attr.getKindAsEnum();
947-
Attrs |= 1ULL << Kind;
946+
Attrs[Kind] = true;
948947

949948
if (Kind == Attribute::Alignment)
950949
Alignment = Attr.getAlignment();
@@ -959,9 +958,8 @@ AttrBuilder &AttrBuilder::addAttribute(StringRef A, StringRef V) {
959958
}
960959

961960
AttrBuilder &AttrBuilder::removeAttribute(Attribute::AttrKind Val) {
962-
assert((unsigned)Val < 64 && Val < Attribute::EndAttrKinds &&
963-
"Attribute out of range!");
964-
Attrs &= ~(1ULL << Val);
961+
assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
962+
Attrs[Val] = false;
965963

966964
if (Val == Attribute::Alignment)
967965
Alignment = 0;
@@ -985,7 +983,7 @@ AttrBuilder &AttrBuilder::removeAttributes(AttributeSet A, uint64_t Index) {
985983
Attribute Attr = *I;
986984
if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
987985
Attribute::AttrKind Kind = I->getKindAsEnum();
988-
Attrs &= ~(1ULL << Kind);
986+
Attrs[Kind] = false;
989987

990988
if (Kind == Attribute::Alignment)
991989
Alignment = 0;
@@ -1016,7 +1014,7 @@ AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
10161014
assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
10171015
assert(Align <= 0x40000000 && "Alignment too large.");
10181016

1019-
Attrs |= 1ULL << Attribute::Alignment;
1017+
Attrs[Attribute::Alignment] = true;
10201018
Alignment = Align;
10211019
return *this;
10221020
}
@@ -1028,7 +1026,7 @@ AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align) {
10281026
assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
10291027
assert(Align <= 0x100 && "Alignment too large.");
10301028

1031-
Attrs |= 1ULL << Attribute::StackAlignment;
1029+
Attrs[Attribute::StackAlignment] = true;
10321030
StackAlignment = Align;
10331031
return *this;
10341032
}
@@ -1055,7 +1053,7 @@ bool AttrBuilder::contains(StringRef A) const {
10551053
}
10561054

10571055
bool AttrBuilder::hasAttributes() const {
1058-
return Attrs != 0 || !TargetDepAttrs.empty();
1056+
return !Attrs.none() || !TargetDepAttrs.empty();
10591057
}
10601058

10611059
bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
@@ -1072,7 +1070,7 @@ bool AttrBuilder::hasAttributes(AttributeSet A, uint64_t Index) const {
10721070
I != E; ++I) {
10731071
Attribute Attr = *I;
10741072
if (Attr.isEnumAttribute() || Attr.isAlignAttribute()) {
1075-
if (Attrs & (1ULL << I->getKindAsEnum()))
1073+
if (Attrs[I->getKindAsEnum()])
10761074
return true;
10771075
} else {
10781076
assert(Attr.isStringAttribute() && "Invalid attribute kind!");
@@ -1106,7 +1104,7 @@ AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
11061104
for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds;
11071105
I = Attribute::AttrKind(I + 1)) {
11081106
if (uint64_t A = (Val & AttributeImpl::getAttrMask(I))) {
1109-
Attrs |= 1ULL << I;
1107+
Attrs[I] = true;
11101108

11111109
if (I == Attribute::Alignment)
11121110
Alignment = 1ULL << ((A >> 16) - 1);

0 commit comments

Comments
 (0)