[llvm] r297733 - Make PredIteratorCache size() logically const. Do not require copying predecessors to get size.

Daniel Berlin via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 04:25:45 PDT 2017


Author: dannyb
Date: Tue Mar 14 06:25:45 2017
New Revision: 297733

URL: http://llvm.org/viewvc/llvm-project?rev=297733&view=rev
Log:
Make PredIteratorCache size() logically const. Do not require copying predecessors to get size.

Summary:
Every single benchmark i can run, on large and small cfgs, fully
connected, etc, across 3 different platforms (x86, arm., and PPC) says
that the current pred iterator cache is a losing proposition.

I can't find a case where it's faster than just walking preds, and in some cases, it's 5-10% slower.

This is due to copying the preds.
It also degrades into copying the entire cfg.

The one operation that is occasionally faster is the cached size.
This makes that operation faster by not relying on having the copies available.

I'm not even sure that is faster enough to be worth it. I, again, have
trouble finding cases where this takes long enough in a pass to be
worth caching compared to a million other things they could cache or
improve.

My suggestion:
We next remove the get() interface.
We do stronger benchmarking of size().
We probably end up killing this entire cache.
/

Reviewers: chandlerc

Subscribers: aemerson, llvm-commits, trentxintong

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

Modified:
    llvm/trunk/include/llvm/IR/PredIteratorCache.h

Modified: llvm/trunk/include/llvm/IR/PredIteratorCache.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PredIteratorCache.h?rev=297733&r1=297732&r2=297733&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PredIteratorCache.h (original)
+++ llvm/trunk/include/llvm/IR/PredIteratorCache.h Tue Mar 14 06:25:45 2017
@@ -27,8 +27,8 @@ namespace llvm {
 /// wants the predecessor list for the same blocks.
 class PredIteratorCache {
   /// BlockToPredsMap - Pointer to null-terminated list.
-  DenseMap<BasicBlock *, BasicBlock **> BlockToPredsMap;
-  DenseMap<BasicBlock *, unsigned> BlockToPredCountMap;
+  mutable DenseMap<BasicBlock *, BasicBlock **> BlockToPredsMap;
+  mutable DenseMap<BasicBlock *, unsigned> BlockToPredCountMap;
 
   /// Memory - This is the space that holds cached preds.
   BumpPtrAllocator Memory;
@@ -55,13 +55,15 @@ private:
     return Entry;
   }
 
-  unsigned GetNumPreds(BasicBlock *BB) {
-    GetPreds(BB);
-    return BlockToPredCountMap[BB];
+  unsigned GetNumPreds(BasicBlock *BB) const {
+    auto Result = BlockToPredCountMap.find(BB);
+    if (Result != BlockToPredCountMap.end())
+      return Result->second;
+    return BlockToPredCountMap[BB] = std::distance(pred_begin(BB), pred_end(BB));
   }
 
 public:
-  size_t size(BasicBlock *BB) { return GetNumPreds(BB); }
+  size_t size(BasicBlock *BB) const { return GetNumPreds(BB); }
   ArrayRef<BasicBlock *> get(BasicBlock *BB) {
     return makeArrayRef(GetPreds(BB), GetNumPreds(BB));
   }




More information about the llvm-commits mailing list