[llvm-commits] [llvm] r123535 - /llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp

Nick Lewycky nicholas at mxc.ca
Sat Jan 15 02:16:23 PST 2011


Author: nicholas
Date: Sat Jan 15 04:16:23 2011
New Revision: 123535

URL: http://llvm.org/viewvc/llvm-project?rev=123535&view=rev
Log:
Add a cache that protects mergefunc's internals from more surprises in DenseSet.

Also, replace tabs with spaces. Yes, it's 2011.

Modified:
    llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp

Modified: llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp?rev=123535&r1=123534&r2=123535&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp Sat Jan 15 04:16:23 2011
@@ -109,10 +109,23 @@
     Func = NULL;
   }
 
+  bool &getOrInsertCachedComparison(const ComparableFunction &Other,
+                                    bool &inserted) const {
+    typedef DenseMap<Function *, bool>::iterator iterator;
+    std::pair<iterator, bool> p =
+        CompareResultCache.insert(std::make_pair(Other.getFunc(), false));
+    inserted = p.second;
+    return p.first->second;
+  }
+
 private:
   explicit ComparableFunction(unsigned Hash)
     : Func(NULL), Hash(Hash), TD(NULL) {}
 
+  // DenseMap::grow() triggers a recomparison of all keys in the map, which is
+  // wildly expensive. This cache tries to preserve known results.
+  mutable DenseMap<Function *, bool> CompareResultCache;
+
   AssertingVH<Function> Func;
   unsigned Hash;
   TargetData *TD;
@@ -708,10 +721,10 @@
       if (Instruction *I = dyn_cast<Instruction>(U.getUser())) {
         Remove(I->getParent()->getParent());
       } else if (isa<GlobalValue>(U.getUser())) {
-	// do nothing
+        // do nothing
       } else if (Constant *C = dyn_cast<Constant>(U.getUser())) {
-	for (Value::use_iterator CUI = C->use_begin(), CUE = C->use_end();
-	     CUI != CUE; ++CUI)
+        for (Value::use_iterator CUI = C->use_begin(), CUE = C->use_end();
+             CUI != CUE; ++CUI)
           Worklist.push_back(*CUI);
       }
     }
@@ -777,6 +790,15 @@
     return false;
   assert(LHS.getTD() == RHS.getTD() &&
          "Comparing functions for different targets");
-  return FunctionComparator(LHS.getTD(),
-                            LHS.getFunc(), RHS.getFunc()).Compare();
+
+  bool inserted;
+  bool &result1 = LHS.getOrInsertCachedComparison(RHS, inserted);
+  if (!inserted)
+    return result1;
+  bool &result2 = RHS.getOrInsertCachedComparison(LHS, inserted);
+  if (!inserted)
+    return result1 = result2;
+
+  return result1 = result2 = FunctionComparator(LHS.getTD(), LHS.getFunc(),
+                                                RHS.getFunc()).Compare();
 }





More information about the llvm-commits mailing list