[llvm] e9df4c9 - [ADT] Support iterating size-based integer ranges.

Ivan Kosarev via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 26 08:28:47 PDT 2023


Author: Ivan Kosarev
Date: 2023-07-26T16:28:41+01:00
New Revision: e9df4c98927e1ee1838511f8d1a90e9c426e70fd

URL: https://github.com/llvm/llvm-project/commit/e9df4c98927e1ee1838511f8d1a90e9c426e70fd
DIFF: https://github.com/llvm/llvm-project/commit/e9df4c98927e1ee1838511f8d1a90e9c426e70fd.diff

LOG: [ADT] Support iterating size-based integer ranges.

It seems the ranges start with 0 in most cases.

Reviewed By: dblaikie, gchatelet

Differential Revision: https://reviews.llvm.org/D156135

Added: 
    

Modified: 
    llvm/include/llvm/ADT/Sequence.h
    llvm/lib/Analysis/ScalarEvolution.cpp
    llvm/lib/IR/Instructions.cpp
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp
    llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
    llvm/unittests/ADT/SequenceTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Sequence.h b/llvm/include/llvm/ADT/Sequence.h
index ddda9a95a7bc65..ce969ef280b39a 100644
--- a/llvm/include/llvm/ADT/Sequence.h
+++ b/llvm/include/llvm/ADT/Sequence.h
@@ -306,6 +306,16 @@ auto seq(T Begin, T End) {
   return iota_range<T>(Begin, End, false);
 }
 
+/// Iterate over an integral type from 0 up to - but not including - Size.
+/// Note: Size value has to be within [INTMAX_MIN, INTMAX_MAX - 1] for
+/// forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse
+/// iteration).
+template <typename T, typename = std::enable_if_t<std::is_integral<T>::value &&
+                                                  !std::is_enum<T>::value>>
+auto seq(T Size) {
+  return seq<T>(0, Size);
+}
+
 /// Iterate over an integral type from Begin to End inclusive.
 /// Note: Begin and End values have to be within [INTMAX_MIN, INTMAX_MAX - 1]
 /// for forward iteration (resp. [INTMAX_MIN + 1, INTMAX_MAX - 1] for reverse

diff  --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 111d4d30aab961..747f34b4591afc 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -652,7 +652,7 @@ CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
     if (LNumOps != RNumOps)
       return (int)LNumOps - (int)RNumOps;
 
-    for (unsigned Idx : seq(0u, LNumOps)) {
+    for (unsigned Idx : seq(LNumOps)) {
       int Result =
           CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
                                  RInst->getOperand(Idx), Depth + 1);

diff  --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index cb0ac0f8eae6ff..42a927ca8214b7 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -2607,7 +2607,7 @@ static bool isReplicationMaskWithParams(ArrayRef<int> Mask,
   assert(Mask.size() == (unsigned)ReplicationFactor * VF &&
          "Unexpected mask size.");
 
-  for (int CurrElt : seq(0, VF)) {
+  for (int CurrElt : seq(VF)) {
     ArrayRef<int> CurrSubMask = Mask.take_front(ReplicationFactor);
     assert(CurrSubMask.size() == (unsigned)ReplicationFactor &&
            "Run out of mask?");

diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1d4cb851da4a21..3a86ded03b61d0 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1303,7 +1303,7 @@ bool SimplifyCFGOpt::PerformValueComparisonIntoPredecessorFolding(
   }
   for (const std::pair<BasicBlock *, int /*Num*/> &NewSuccessor :
        NewSuccessors) {
-    for (auto I : seq(0, NewSuccessor.second)) {
+    for (auto I : seq(NewSuccessor.second)) {
       (void)I;
       AddPredecessorToBlock(NewSuccessor.first, Pred, BB);
     }

diff  --git a/llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp b/llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp
index 8b0c45219f4244..636dd11ff32658 100644
--- a/llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp
+++ b/llvm/tools/llvm-exegesis/lib/SnippetRepetitor.cpp
@@ -106,7 +106,7 @@ class LoopSnippetRepetitor : public SnippetRepetitor {
         for (const auto &LiveIn : Entry.MBB->liveins())
           Loop.MBB->addLiveIn(LiveIn);
       }
-      for (auto _ : seq(0U, LoopUnrollFactor)) {
+      for (auto _ : seq(LoopUnrollFactor)) {
         (void)_;
         Loop.addInstructions(Instructions);
       }

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp b/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
index 6f9cafb8f73a5b..b048042e6ea5d1 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandBundles.cpp
@@ -55,7 +55,7 @@ class OperandBundleRemapper : public InstVisitor<OperandBundleRemapper> {
     OperandBundlesToKeepIndexes.reserve(Call.getNumOperandBundles());
 
     // Enumerate every operand bundle on this call.
-    for (unsigned BundleIndex : seq(0U, Call.getNumOperandBundles()))
+    for (unsigned BundleIndex : seq(Call.getNumOperandBundles()))
       if (O.shouldKeep()) // Should we keep this one?
         OperandBundlesToKeepIndexes.emplace_back(BundleIndex);
   }

diff  --git a/llvm/unittests/ADT/SequenceTest.cpp b/llvm/unittests/ADT/SequenceTest.cpp
index acc15bf4daae66..7b7dc85cb79bea 100644
--- a/llvm/unittests/ADT/SequenceTest.cpp
+++ b/llvm/unittests/ADT/SequenceTest.cpp
@@ -182,6 +182,8 @@ TEST(SafeIntIteratorTest, Operations) {
 }
 
 TEST(SequenceTest, Iteration) {
+  EXPECT_THAT(seq(5), ElementsAre(0, 1, 2, 3, 4));
+
   EXPECT_THAT(seq(-4, 5), ElementsAre(-4, -3, -2, -1, 0, 1, 2, 3, 4));
   EXPECT_THAT(reverse(seq(-4, 5)), ElementsAre(4, 3, 2, 1, 0, -1, -2, -3, -4));
 


        


More information about the llvm-commits mailing list