[NFC] Factor out function to detect if an attribute has an argument.
authorTyker <tyker1@outlook.com>
Sun, 2 Feb 2020 13:47:00 +0000 (14:47 +0100)
committerTyker <tyker1@outlook.com>
Mon, 3 Feb 2020 21:27:24 +0000 (22:27 +0100)
llvm/include/llvm/IR/Attributes.h
llvm/lib/IR/AttributeImpl.h
llvm/lib/IR/Attributes.cpp
llvm/lib/IR/Verifier.cpp
llvm/lib/Transforms/Utils/KnowledgeRetention.cpp

index 3ef8a3b..3871db9 100644 (file)
@@ -111,6 +111,9 @@ public:
 
   static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind);
 
+  /// Return true if and only if the attribute has an Argument.
+  static bool doesAttrKindHaveArgument(Attribute::AttrKind AttrKind);
+
   //===--------------------------------------------------------------------===//
   // Attribute Accessors
   //===--------------------------------------------------------------------===//
index da6e993..8741c0c 100644 (file)
@@ -134,10 +134,7 @@ class IntAttributeImpl : public EnumAttributeImpl {
 public:
   IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
       : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
-    assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
-            Kind == Attribute::Dereferenceable ||
-            Kind == Attribute::DereferenceableOrNull ||
-            Kind == Attribute::AllocSize) &&
+    assert(Attribute::doesAttrKindHaveArgument(Kind) &&
            "Wrong kind for int attribute!");
   }
 
index 0b72aa5..c41d6c8 100644 (file)
@@ -199,6 +199,14 @@ StringRef Attribute::getNameFromAttrKind(Attribute::AttrKind AttrKind) {
   }
 }
 
+bool Attribute::doesAttrKindHaveArgument(Attribute::AttrKind AttrKind) {
+  return AttrKind == Attribute::Alignment ||
+         AttrKind == Attribute::StackAlignment ||
+         AttrKind == Attribute::Dereferenceable ||
+         AttrKind == Attribute::AllocSize ||
+         AttrKind == Attribute::DereferenceableOrNull;
+}
+
 //===----------------------------------------------------------------------===//
 // Attribute Accessor Methods
 //===----------------------------------------------------------------------===//
@@ -1472,8 +1480,7 @@ void AttrBuilder::clear() {
 
 AttrBuilder &AttrBuilder::addAttribute(Attribute::AttrKind Val) {
   assert((unsigned)Val < Attribute::EndAttrKinds && "Attribute out of range!");
-  assert(Val != Attribute::Alignment && Val != Attribute::StackAlignment &&
-         Val != Attribute::Dereferenceable && Val != Attribute::AllocSize &&
+  assert(!Attribute::doesAttrKindHaveArgument(Val) &&
          "Adding integer attribute without adding a value!");
   Attrs[Val] = true;
   return *this;
index 6698a68..6062baa 100644 (file)
@@ -1572,6 +1572,13 @@ void Verifier::verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
     if (A.isStringAttribute())
       continue;
 
+    if (A.isIntAttribute() !=
+        Attribute::doesAttrKindHaveArgument(A.getKindAsEnum())) {
+      CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
+                  V);
+      return;
+    }
+
     if (isFuncOnlyAttr(A.getKindAsEnum())) {
       if (!IsFunction) {
         CheckFailed("Attribute '" + A.getAsString() +
index 2f82f9f..bf8742b 100644 (file)
@@ -128,6 +128,11 @@ struct AssumeBuilderState {
     SmallVector<OperandBundleDef, 8> OpBundle;
     for (const AssumedKnowledge &Elem : AssumedKnowledgeSet) {
       SmallVector<Value *, 2> Args;
+      assert(Attribute::getAttrKindFromName(Elem.Name) ==
+                 Attribute::AttrKind::None ||
+             static_cast<bool>(Elem.Argument) ==
+                 Attribute::doesAttrKindHaveArgument(
+                     Attribute::getAttrKindFromName(Elem.Name)));
       if (Elem.WasOn.getPointer())
         Args.push_back(Elem.WasOn.getPointer());
       if (Elem.Argument)