[llvm] [AssumeBundleQueries] Remove unused code (PR #203927)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 6 03:39:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

Some of the utilities in AssumeBundleQueris was only ever used in tests for them, so there isn't much point in keeping the code around.

---
Full diff: https://github.com/llvm/llvm-project/pull/203927.diff


3 Files Affected:

- (modified) llvm/include/llvm/Analysis/AssumeBundleQueries.h (+2-33) 
- (modified) llvm/lib/Analysis/AssumeBundleQueries.cpp (-33) 
- (modified) llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp (-266) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/AssumeBundleQueries.h b/llvm/include/llvm/Analysis/AssumeBundleQueries.h
index d51287b51d761..10def2ceac76d 100644
--- a/llvm/include/llvm/Analysis/AssumeBundleQueries.h
+++ b/llvm/include/llvm/Analysis/AssumeBundleQueries.h
@@ -31,24 +31,6 @@ enum AssumeBundleArg {
   ABA_Argument = 1,
 };
 
-/// Query the operand bundle of an llvm.assume to find a single attribute of
-/// the specified kind applied on a specified Value.
-///
-/// This has a non-constant complexity. It should only be used when a single
-/// attribute is going to be queried.
-///
-/// Return true iff the queried attribute was found.
-/// If ArgVal is set. the argument will be stored to ArgVal.
-LLVM_ABI bool hasAttributeInAssume(AssumeInst &Assume, Value *IsOn,
-                                   StringRef AttrName,
-                                   uint64_t *ArgVal = nullptr);
-inline bool hasAttributeInAssume(AssumeInst &Assume, Value *IsOn,
-                                 Attribute::AttrKind Kind,
-                                 uint64_t *ArgVal = nullptr) {
-  return hasAttributeInAssume(Assume, IsOn,
-                              Attribute::getNameFromAttrKind(Kind), ArgVal);
-}
-
 template <> struct DenseMapInfo<Attribute::AttrKind> {
   static unsigned getHashValue(Attribute::AttrKind AK) {
     return hash_combine(AK);
@@ -78,9 +60,8 @@ using RetainedKnowledgeMap =
     DenseMap<RetainedKnowledgeKey, Assume2KnowledgeMap>;
 
 /// Insert into the map all the informations contained in the operand bundles of
-/// the llvm.assume. This should be used instead of hasAttributeInAssume when
-/// many queries are going to be made on the same llvm.assume.
-/// String attributes are not inserted in the map.
+/// the llvm.assume. This should be used when many queries are going to be made
+/// on the same llvm.assume. String attributes are not inserted in the map.
 /// If the IR changes the map will be outdated.
 LLVM_ABI void fillMapFromAssume(AssumeInst &Assume,
                                 RetainedKnowledgeMap &Result);
@@ -119,18 +100,6 @@ struct RetainedKnowledge {
   static RetainedKnowledge none() { return RetainedKnowledge{}; }
 };
 
-/// Retreive the information help by Assume on the operand at index Idx.
-/// Assume should be an llvm.assume and Idx should be in the operand bundle.
-LLVM_ABI RetainedKnowledge getKnowledgeFromOperandInAssume(AssumeInst &Assume,
-                                                           unsigned Idx);
-
-/// Retreive the information help by the Use U of an llvm.assume. the use should
-/// be in the operand bundle.
-inline RetainedKnowledge getKnowledgeFromUseInAssume(const Use *U) {
-  return getKnowledgeFromOperandInAssume(*cast<AssumeInst>(U->getUser()),
-                                         U->getOperandNo());
-}
-
 /// Tag in operand bundle indicating that this bundle should be ignored.
 constexpr StringRef IgnoreBundleTag = "ignore";
 
diff --git a/llvm/lib/Analysis/AssumeBundleQueries.cpp b/llvm/lib/Analysis/AssumeBundleQueries.cpp
index aa1cb3c26d63f..044beaf6f4aa1 100644
--- a/llvm/lib/Analysis/AssumeBundleQueries.cpp
+++ b/llvm/lib/Analysis/AssumeBundleQueries.cpp
@@ -40,33 +40,6 @@ static Value *getValueFromBundleOpInfo(AssumeInst &Assume,
   return (Assume.op_begin() + BOI.Begin + Idx)->get();
 }
 
-bool llvm::hasAttributeInAssume(AssumeInst &Assume, Value *IsOn,
-                                StringRef AttrName, uint64_t *ArgVal) {
-  assert(Attribute::isExistingAttribute(AttrName) &&
-         "this attribute doesn't exist");
-  assert((ArgVal == nullptr || Attribute::isIntAttrKind(
-                                   Attribute::getAttrKindFromName(AttrName))) &&
-         "requested value for an attribute that has no argument");
-  if (Assume.bundle_op_infos().empty())
-    return false;
-
-  for (auto &BOI : Assume.bundle_op_infos()) {
-    if (BOI.Tag->getKey() != AttrName)
-      continue;
-    if (IsOn && (BOI.End - BOI.Begin <= ABA_WasOn ||
-                 IsOn != getValueFromBundleOpInfo(Assume, BOI, ABA_WasOn)))
-      continue;
-    if (ArgVal) {
-      assert(BOI.End - BOI.Begin > ABA_Argument);
-      *ArgVal =
-          cast<ConstantInt>(getValueFromBundleOpInfo(Assume, BOI, ABA_Argument))
-              ->getZExtValue();
-    }
-    return true;
-  }
-  return false;
-}
-
 void llvm::fillMapFromAssume(AssumeInst &Assume, RetainedKnowledgeMap &Result) {
   for (auto &Bundles : Assume.bundle_op_infos()) {
     std::pair<Value *, Attribute::AttrKind> Key{
@@ -123,12 +96,6 @@ llvm::getKnowledgeFromBundle(AssumeInst &Assume,
   return Result;
 }
 
-RetainedKnowledge llvm::getKnowledgeFromOperandInAssume(AssumeInst &Assume,
-                                                        unsigned Idx) {
-  CallBase::BundleOpInfo BOI = Assume.getBundleOpInfoForOperand(Idx);
-  return getKnowledgeFromBundle(Assume, BOI);
-}
-
 bool llvm::isAssumeWithEmptyBundle(const AssumeInst &Assume) {
   return none_of(Assume.bundle_op_infos(),
                  [](const CallBase::BundleOpInfo &BOI) {
diff --git a/llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp b/llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp
index e99e2ec4930fc..6a55454ef574a 100644
--- a/llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp
+++ b/llvm/unittests/Analysis/AssumeBundleQueriesTest.cpp
@@ -40,174 +40,6 @@ static void RunTest(
   }
 }
 
-bool hasMatchesExactlyAttributes(AssumeInst *Assume, Value *WasOn,
-                                 StringRef AttrToMatch) {
-  Regex Reg(AttrToMatch);
-  SmallVector<StringRef, 1> Matches;
-  for (StringRef Attr : {
-#define GET_ATTR_NAMES
-#define ATTRIBUTE_ALL(ENUM_NAME, DISPLAY_NAME) StringRef(#DISPLAY_NAME),
-#include "llvm/IR/Attributes.inc"
-       }) {
-    bool ShouldHaveAttr = Reg.match(Attr, &Matches) && Matches[0] == Attr;
-    if (ShouldHaveAttr != hasAttributeInAssume(*Assume, WasOn, Attr))
-      return false;
-  }
-  return true;
-}
-
-bool hasTheRightValue(AssumeInst *Assume, Value *WasOn,
-                      Attribute::AttrKind Kind, unsigned Value) {
-  uint64_t ArgVal = 0;
-  if (!hasAttributeInAssume(*Assume, WasOn, Kind, &ArgVal))
-    return false;
-  if (ArgVal != Value)
-    return false;
-  return true;
-}
-
-TEST(AssumeQueryAPI, hasAttributeInAssume) {
-  EnableKnowledgeRetention.setValue(true);
-  StringRef Head =
-      "declare void @llvm.assume(i1)\n"
-      "declare void @func(ptr, ptr, ptr)\n"
-      "declare void @func1(ptr, ptr, ptr, ptr)\n"
-      "declare void @func_many(ptr) \"no-jump-tables\" nounwind "
-      "\"less-precise-fpmad\" willreturn norecurse\n"
-      "define void @test(ptr %P, ptr %P1, ptr %P2, ptr %P3) {\n";
-  StringRef Tail = "ret void\n"
-                   "}";
-  std::vector<std::pair<StringRef, llvm::function_ref<void(Instruction *)>>>
-      Tests;
-  Tests.push_back(std::make_pair(
-      "call void @func(ptr nonnull align 4 dereferenceable(16) %P, ptr align "
-      "8 noalias %P1, ptr align 8 noundef %P2)\n",
-      [](Instruction *I) {
-        auto *Assume = buildAssumeFromInst(I);
-        Assume->insertBefore(I->getIterator());
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(0),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(1),
-                                       "()"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(2),
-                                       "(align|noundef)"));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Dereferenceable, 16));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Alignment, 4));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Alignment, 4));
-      }));
-  Tests.push_back(std::make_pair(
-      "call void @func1(ptr nonnull align 32 dereferenceable(48) %P, ptr "
-      "nonnull "
-      "align 8 dereferenceable(28) %P, ptr nonnull align 64 "
-      "dereferenceable(4) "
-      "%P, ptr nonnull align 16 dereferenceable(12) %P)\n",
-      [](Instruction *I) {
-        auto *Assume = buildAssumeFromInst(I);
-        Assume->insertBefore(I->getIterator());
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(0),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(1),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(2),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(3),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Dereferenceable, 48));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Alignment, 64));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(1),
-                                     Attribute::AttrKind::Alignment, 64));
-      }));
-  Tests.push_back(
-      std::make_pair("call void @llvm.assume(i1 true)\n", [](Instruction *I) {
-        auto *Assume = cast<AssumeInst>(I);
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, nullptr, ""));
-      }));
-  Tests.push_back(std::make_pair(
-      "call void @func1(ptr readnone align 32 "
-      "dereferenceable(48) noalias %P, ptr "
-      "align 8 dereferenceable(28) %P1, ptr align 64 "
-      "dereferenceable(4) "
-      "%P2, ptr nonnull align 16 dereferenceable(12) %P3)\n",
-      [](Instruction *I) {
-        auto *Assume = buildAssumeFromInst(I);
-        Assume->insertBefore(I->getIterator());
-        ASSERT_TRUE(hasMatchesExactlyAttributes(
-            Assume, I->getOperand(0),
-            "(align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(1),
-                                       "(align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(2),
-                                       "(align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(3),
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Alignment, 32));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Dereferenceable, 48));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(1),
-                                     Attribute::AttrKind::Dereferenceable, 28));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(1),
-                                     Attribute::AttrKind::Alignment, 8));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(2),
-                                     Attribute::AttrKind::Alignment, 64));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(2),
-                                     Attribute::AttrKind::Dereferenceable, 4));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(3),
-                                     Attribute::AttrKind::Alignment, 16));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(3),
-                                     Attribute::AttrKind::Dereferenceable, 12));
-      }));
-
-  Tests.push_back(std::make_pair(
-      "call void @func1(ptr readnone align 32 "
-      "dereferenceable(48) noalias %P, ptr "
-      "align 8 dereferenceable(28) %P1, ptr align 64 "
-      "dereferenceable(4) "
-      "%P2, ptr nonnull align 16 dereferenceable(12) %P3)\n",
-      [](Instruction *I) {
-        auto *Assume = buildAssumeFromInst(I);
-        Assume->insertBefore(I->getIterator());
-        I->getOperand(1)->dropDroppableUses();
-        I->getOperand(2)->dropDroppableUses();
-        I->getOperand(3)->dropDroppableUses();
-        ASSERT_TRUE(hasMatchesExactlyAttributes(
-            Assume, I->getOperand(0),
-            "(align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(1),
-                                       ""));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(2),
-                                       ""));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, I->getOperand(3),
-                                       ""));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Alignment, 32));
-        ASSERT_TRUE(hasTheRightValue(Assume, I->getOperand(0),
-                                     Attribute::AttrKind::Dereferenceable, 48));
-      }));
-  Tests.push_back(std::make_pair(
-      "call void @func(ptr nonnull align 4 dereferenceable(16) %P, ptr align "
-      "8 noalias %P1, ptr %P1)\n",
-      [](Instruction *I) {
-        auto *Assume = buildAssumeFromInst(I);
-        Assume->insertBefore(I->getIterator());
-        Value *New = I->getFunction()->getArg(3);
-        Value *Old = I->getOperand(0);
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, New, ""));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, Old,
-                                       "(nonnull|align|dereferenceable)"));
-        Old->replaceAllUsesWith(New);
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, New,
-                                       "(nonnull|align|dereferenceable)"));
-        ASSERT_TRUE(hasMatchesExactlyAttributes(Assume, Old, ""));
-      }));
-  RunTest(Head, Tail, Tests);
-}
-
 static bool FindExactlyAttributes(RetainedKnowledgeMap &Map, Value *WasOn,
                                  StringRef AttrToMatch) {
   Regex Reg(AttrToMatch);
@@ -377,104 +209,6 @@ TEST(AssumeQueryAPI, fillMapFromAssume) {
   RunTest(Head, Tail, Tests);
 }
 
-static void RunRandTest(uint64_t Seed, int Size, int MinCount, int MaxCount,
-                        unsigned MaxValue) {
-  LLVMContext C;
-  SMDiagnostic Err;
-
-  std::mt19937 Rng(Seed);
-  std::uniform_int_distribution<int> DistCount(MinCount, MaxCount);
-  std::uniform_int_distribution<unsigned> DistValue(0, MaxValue);
-  std::uniform_int_distribution<unsigned> DistAttr(0,
-                                                   Attribute::EndAttrKinds - 1);
-
-  std::unique_ptr<Module> Mod = std::make_unique<Module>("AssumeQueryAPI", C);
-  if (!Mod)
-    Err.print("AssumeQueryAPI", errs());
-
-  std::vector<Type *> TypeArgs;
-  for (int i = 0; i < (Size * 2); i++)
-    TypeArgs.push_back(PointerType::getUnqual(C));
-  FunctionType *FuncType =
-      FunctionType::get(Type::getVoidTy(C), TypeArgs, false);
-
-  Function *F =
-      Function::Create(FuncType, GlobalValue::ExternalLinkage, "test", &*Mod);
-  BasicBlock *BB = BasicBlock::Create(C);
-  BB->insertInto(F);
-  Instruction *Ret = ReturnInst::Create(C);
-  Ret->insertInto(BB, BB->begin());
-  Function *FnAssume =
-      Intrinsic::getOrInsertDeclaration(Mod.get(), Intrinsic::assume);
-
-  std::vector<Argument *> ShuffledArgs;
-  BitVector HasArg;
-  for (auto &Arg : F->args()) {
-    ShuffledArgs.push_back(&Arg);
-    HasArg.push_back(false);
-  }
-
-  std::shuffle(ShuffledArgs.begin(), ShuffledArgs.end(), Rng);
-
-  std::vector<OperandBundleDef> OpBundle;
-  OpBundle.reserve(Size);
-  std::vector<Value *> Args;
-  Args.reserve(2);
-  for (int i = 0; i < Size; i++) {
-    int count = DistCount(Rng);
-    int value = DistValue(Rng);
-    int attr = DistAttr(Rng);
-    std::string str;
-    raw_string_ostream ss(str);
-    ss << Attribute::getNameFromAttrKind(
-        static_cast<Attribute::AttrKind>(attr));
-    Args.clear();
-
-    if (count > 0) {
-      Args.push_back(ShuffledArgs[i]);
-      HasArg[i] = true;
-    }
-    if (count > 1)
-      Args.push_back(ConstantInt::get(Type::getInt32Ty(C), value));
-
-    OpBundle.push_back(OperandBundleDef{str.c_str(), std::move(Args)});
-  }
-
-  auto *Assume = cast<AssumeInst>(CallInst::Create(
-      FnAssume, ArrayRef<Value *>({ConstantInt::getTrue(C)}), OpBundle));
-  Assume->insertBefore(F->begin()->begin());
-  RetainedKnowledgeMap Map;
-  fillMapFromAssume(*Assume, Map);
-  for (int i = 0; i < (Size * 2); i++) {
-    if (!HasArg[i])
-      continue;
-    RetainedKnowledge K =
-        getKnowledgeFromUseInAssume(&*ShuffledArgs[i]->use_begin());
-    auto LookupIt = Map.find(RetainedKnowledgeKey{K.WasOn, K.AttrKind});
-    ASSERT_TRUE(LookupIt != Map.end());
-    MinMax MM = LookupIt->second[Assume];
-    ASSERT_TRUE(MM.Min == MM.Max);
-    ASSERT_TRUE(MM.Min == K.ArgValue);
-  }
-}
-
-TEST(AssumeQueryAPI, getKnowledgeFromUseInAssume) {
-  // // For Fuzzing
-  // std::random_device dev;
-  // std::mt19937 Rng(dev());
-  // while (true) {
-  //   unsigned Seed = Rng();
-  //   dbgs() << Seed << "\n";
-  //   RunRandTest(Seed, 100000, 0, 2, 100);
-  // }
-  RunRandTest(23456, 4, 0, 2, 100);
-  RunRandTest(560987, 25, -3, 2, 100);
-
-  // Large bundles can lead to special cases. this is why this test is soo
-  // large.
-  RunRandTest(9876789, 100000, -0, 7, 100);
-}
-
 TEST(AssumeQueryAPI, AssumptionCache) {
   LLVMContext C;
   SMDiagnostic Err;

``````````

</details>


https://github.com/llvm/llvm-project/pull/203927


More information about the llvm-commits mailing list