[PATCH] D146136: [llvm][CycleInfo] Quick look-up for block in cycle.

Sameer Sahasrabuddhe via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 15 06:15:13 PDT 2023


sameerds created this revision.
Herald added a project: All.
sameerds requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Use a SetVector to store blocks in a cycle to ensure a quick loop-up when
querying whether the cycle contains a given block.

This follows along the same lines as the SmallPtrSet in LoopBase, introduced by
commit be640b28c0cb81b77015baaef20ca2941fc61dea


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146136

Files:
  llvm/include/llvm/ADT/GenericCycleImpl.h
  llvm/include/llvm/ADT/GenericCycleInfo.h


Index: llvm/include/llvm/ADT/GenericCycleInfo.h
===================================================================
--- llvm/include/llvm/ADT/GenericCycleInfo.h
+++ llvm/include/llvm/ADT/GenericCycleInfo.h
@@ -32,6 +32,8 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/GenericSSAContext.h"
 #include "llvm/ADT/GraphTraits.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/iterator.h"
 #include "llvm/Support/Debug.h"
@@ -67,7 +69,9 @@
 
   /// Basic blocks that are contained in the cycle, including entry blocks,
   /// and including blocks that are part of a child cycle.
-  std::vector<BlockT *> Blocks;
+  using BlockSetVectorT =
+      SetVector<BlockT *, std::vector<BlockT *>, SmallPtrSet<BlockT *, 8>>;
+  BlockSetVectorT Blocks;
 
   /// Depth of the cycle in the tree. The root "cycle" is at depth 0.
   ///
@@ -85,7 +89,7 @@
   }
 
   void appendEntry(BlockT *Block) { Entries.push_back(Block); }
-  void appendBlock(BlockT *Block) { Blocks.push_back(Block); }
+  void appendBlock(BlockT *Block) { Blocks.insert(Block); }
 
   GenericCycle(const GenericCycle &) = delete;
   GenericCycle &operator=(const GenericCycle &) = delete;
@@ -111,7 +115,10 @@
 
   /// \brief Return whether \p Block is contained in the cycle.
   bool contains(const BlockT *Block) const {
-    return is_contained(Blocks, Block);
+    // We are forced to do a const_cast because our key_type is a pointer, and
+    // the "const" qualifier in SetVector::contains(const key_type&) gets
+    // applied to the wrong part of the type.
+    return Blocks.contains(const_cast<BlockT *>(Block));
   }
 
   /// \brief Returns true iff this cycle contains \p C.
@@ -171,7 +178,7 @@
 
   /// Iteration over blocks in the cycle (including entry blocks).
   //@{
-  using const_block_iterator = typename std::vector<BlockT *>::const_iterator;
+  using const_block_iterator = typename BlockSetVectorT::const_iterator;
 
   const_block_iterator block_begin() const {
     return const_block_iterator{Blocks.begin()};
Index: llvm/include/llvm/ADT/GenericCycleImpl.h
===================================================================
--- llvm/include/llvm/ADT/GenericCycleImpl.h
+++ llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -177,8 +177,7 @@
   CurrentContainer.pop_back();
   Child->ParentCycle = NewParent;
 
-  NewParent->Blocks.insert(NewParent->Blocks.end(), Child->block_begin(),
-                           Child->block_end());
+  NewParent->Blocks.insert(Child->block_begin(), Child->block_end());
 
   for (auto &It : BlockMapTopLevel)
     if (It.second == Child)
@@ -266,7 +265,7 @@
       } else {
         Info.BlockMap.try_emplace(Block, NewCycle.get());
         assert(!is_contained(NewCycle->Blocks, Block));
-        NewCycle->Blocks.push_back(Block);
+        NewCycle->Blocks.insert(Block);
         ProcessPredecessors(Block);
         Info.BlockMapTopLevel.try_emplace(Block, NewCycle.get());
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146136.505465.patch
Type: text/x-patch
Size: 2982 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230315/5761a9e5/attachment.bin>


More information about the llvm-commits mailing list