From: Nick Lewycky Date: Sat, 15 Jan 2011 10:16:23 +0000 (+0000) Subject: Add a cache that protects mergefunc's internals from more surprises in DenseSet. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e8f8139429ffc41ae3a339d4a32e336a74f189c0;p=oota-llvm.git Add a cache that protects mergefunc's internals from more surprises in DenseSet. Also, replace tabs with spaces. Yes, it's 2011. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123535 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp index c678db85900..49ebe65fe81 100644 --- a/lib/Transforms/IPO/MergeFunctions.cpp +++ b/lib/Transforms/IPO/MergeFunctions.cpp @@ -109,10 +109,23 @@ public: Func = NULL; } + bool &getOrInsertCachedComparison(const ComparableFunction &Other, + bool &inserted) const { + typedef DenseMap::iterator iterator; + std::pair 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 CompareResultCache; + AssertingVH Func; unsigned Hash; TargetData *TD; @@ -708,10 +721,10 @@ void MergeFunctions::RemoveUsers(Value *V) { if (Instruction *I = dyn_cast(U.getUser())) { Remove(I->getParent()->getParent()); } else if (isa(U.getUser())) { - // do nothing + // do nothing } else if (Constant *C = dyn_cast(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 @@ bool DenseMapInfo::isEqual(const ComparableFunction &LHS, 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(); }