[llvm] [EquivClasses] Introduce members iterator-helper (NFC) (PR #130139)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 7 02:34:47 PST 2025
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/130139
>From c87c5229689bafb6f9d4378d964975c6c1622f97 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Thu, 6 Mar 2025 16:36:12 +0000
Subject: [PATCH 1/3] [EquivClasses] Introduce members iterator-helper (NFC)
---
llvm/include/llvm/ADT/EquivalenceClasses.h | 3 +++
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 5 ++---
llvm/lib/Analysis/VectorUtils.cpp | 6 +++---
3 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/llvm/include/llvm/ADT/EquivalenceClasses.h b/llvm/include/llvm/ADT/EquivalenceClasses.h
index 4f98b84cf97d2..1987b9ae4a761 100644
--- a/llvm/include/llvm/ADT/EquivalenceClasses.h
+++ b/llvm/include/llvm/ADT/EquivalenceClasses.h
@@ -178,6 +178,9 @@ class EquivalenceClasses {
member_iterator member_end() const {
return member_iterator(nullptr);
}
+ auto members(iterator I) const {
+ return make_range(member_begin(I), member_end());
+ }
/// findValue - Return an iterator to the specified value. If it does not
/// exist, end() is returned.
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 38ee82b77a946..207f5417934e5 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -527,9 +527,8 @@ void RuntimePointerChecking::groupChecks(
// iteration order within an equivalence class member is only dependent on
// the order in which unions and insertions are performed on the
// equivalence class, the iteration order is deterministic.
- for (auto MI = DepCands.member_begin(LeaderI), ME = DepCands.member_end();
- MI != ME; ++MI) {
- auto PointerI = PositionMap.find(MI->getPointer());
+ for (const auto &MI : DepCands.members(LeaderI)) {
+ auto PointerI = PositionMap.find(MI.getPointer());
assert(PointerI != PositionMap.end() &&
"pointer in equivalence class not found in PositionMap");
for (unsigned Pointer : PointerI->second) {
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 91ba68fe03324..c0bc451973c6e 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -845,7 +845,7 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) {
uint64_t LeaderDemandedBits = 0;
- for (Value *M : llvm::make_range(ECs.member_begin(I), ECs.member_end()))
+ for (Value *M : ECs.members(I))
LeaderDemandedBits |= DBits[M];
uint64_t MinBW = llvm::bit_width(LeaderDemandedBits);
@@ -857,7 +857,7 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
// indvars.
// If we are required to shrink a PHI, abandon this entire equivalence class.
bool Abort = false;
- for (Value *M : llvm::make_range(ECs.member_begin(I), ECs.member_end()))
+ for (Value *M : ECs.members(I))
if (isa<PHINode>(M) && MinBW < M->getType()->getScalarSizeInBits()) {
Abort = true;
break;
@@ -865,7 +865,7 @@ llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
if (Abort)
continue;
- for (Value *M : llvm::make_range(ECs.member_begin(I), ECs.member_end())) {
+ for (Value *M : ECs.members(I)) {
auto *MI = dyn_cast<Instruction>(M);
if (!MI)
continue;
>From e66313d65798f4624a71c841f2f11b8f5d4cda1c Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Thu, 6 Mar 2025 17:44:20 +0000
Subject: [PATCH 2/3] [EquivClasses] Specify return type explicitly
---
llvm/include/llvm/ADT/EquivalenceClasses.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/EquivalenceClasses.h b/llvm/include/llvm/ADT/EquivalenceClasses.h
index 1987b9ae4a761..345107c7777b0 100644
--- a/llvm/include/llvm/ADT/EquivalenceClasses.h
+++ b/llvm/include/llvm/ADT/EquivalenceClasses.h
@@ -15,6 +15,7 @@
#ifndef LLVM_ADT_EQUIVALENCECLASSES_H
#define LLVM_ADT_EQUIVALENCECLASSES_H
+#include "llvm/ADT/iterator_range.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
@@ -178,7 +179,7 @@ class EquivalenceClasses {
member_iterator member_end() const {
return member_iterator(nullptr);
}
- auto members(iterator I) const {
+ iterator_range<member_iterator> members(iterator I) const {
return make_range(member_begin(I), member_end());
}
>From 6b88c5bc1be76424eaaf2614ac17f9c6884b1236 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Fri, 7 Mar 2025 10:33:29 +0000
Subject: [PATCH 3/3] [EquivClasses] Add unit-test
---
llvm/unittests/ADT/EquivalenceClassesTest.cpp | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/llvm/unittests/ADT/EquivalenceClassesTest.cpp b/llvm/unittests/ADT/EquivalenceClassesTest.cpp
index 70e161a03d988..0f7dfca861ccb 100644
--- a/llvm/unittests/ADT/EquivalenceClassesTest.cpp
+++ b/llvm/unittests/ADT/EquivalenceClassesTest.cpp
@@ -66,6 +66,22 @@ TEST(EquivalenceClassesTest, TwoSets) {
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
}
+TEST(EquivalenceClassesTest, MembersIterator) {
+ EquivalenceClasses<int> EC;
+ EC.unionSets(1, 2);
+ EC.insert(4);
+ EC.insert(5);
+ EC.unionSets(5, 1);
+ EXPECT_EQ(EC.getNumClasses(), 2u);
+
+ std::set<int> ActualMembers;
+ std::set<int> ExpectedMembers{1, 2, 5};
+ EquivalenceClasses<int>::iterator I = EC.findValue(EC.getLeaderValue(1));
+ for (auto M : EC.members(I))
+ ActualMembers.insert(M);
+ EXPECT_EQ(ActualMembers, ExpectedMembers);
+}
+
// Type-parameterized tests: Run the same test cases with different element
// types.
template <typename T> class ParameterizedTest : public testing::Test {};
More information about the llvm-commits
mailing list