From bb30cc0d2f8a3ae6ca6f9250817d44889e0dfb98 Mon Sep 17 00:00:00 2001 From: Stepan Dyatkovskiy Date: Wed, 4 Jul 2012 05:53:05 +0000 Subject: [PATCH] Reverted r156659, due to probable performance regressions, DenseMap should be used here: IntegersSubsetMapping - Replaced type of Items field from std::list with std::map. In neares future I'll test it with DenseMap and do the correspond replacement if possible. llvm-svn: 159703 --- include/llvm/Support/IntegersSubsetMapping.h | 34 ++++++++++++++++--- .../SelectionDAG/SelectionDAGBuilder.cpp | 15 ++++---- lib/Transforms/Utils/LowerSwitch.cpp | 9 +++-- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/include/llvm/Support/IntegersSubsetMapping.h b/include/llvm/Support/IntegersSubsetMapping.h index 667980296d4..7ba3ba17980 100644 --- a/include/llvm/Support/IntegersSubsetMapping.h +++ b/include/llvm/Support/IntegersSubsetMapping.h @@ -58,15 +58,22 @@ public: protected: - typedef std::map CaseItems; + typedef std::list CaseItems; typedef typename CaseItems::iterator CaseItemIt; typedef typename CaseItems::const_iterator CaseItemConstIt; // TODO: Change unclean CRS prefixes to SubsetMap for example. typedef std::map CRSMap; typedef typename CRSMap::iterator CRSMapIt; + + struct ClustersCmp { + bool operator()(const Cluster &C1, const Cluster &C2) { + return C1.first < C2.first; + } + }; CaseItems Items; + bool Sorted; bool SingleNumbersOnly; bool isIntersected(CaseItemIt& LItem, CaseItemIt& RItem) { @@ -85,6 +92,18 @@ protected: return LItem->first.getHigh() >= RLow; } + void sort() { + if (!Sorted) { + std::vector clustersVector; + clustersVector.reserve(Items.size()); + clustersVector.insert(clustersVector.begin(), Items.begin(), Items.end()); + std::sort(clustersVector.begin(), clustersVector.end(), ClustersCmp()); + Items.clear(); + Items.insert(Items.begin(), clustersVector.begin(), clustersVector.end()); + Sorted = true; + } + } + enum DiffProcessState { L_OPENED, INTERSECT_OPENED, @@ -282,7 +301,10 @@ public: typedef std::list Cases; typedef typename Cases::iterator CasesIt; - IntegersSubsetMapping() : SingleNumbersOnly(true) {} + IntegersSubsetMapping() { + Sorted = false; + SingleNumbersOnly = true; + } bool verify() { RangeIterator DummyErrItem; @@ -292,6 +314,7 @@ public: bool verify(RangeIterator& errItem) { if (Items.empty()) return true; + sort(); for (CaseItemIt j = Items.begin(), i = j++, e = Items.end(); j != e; i = j++) { if (isIntersected(i, j) && i->second != j->second) { @@ -332,6 +355,7 @@ public: void optimize() { if (Items.size() < 2) return; + sort(); CaseItems OldItems = Items; Items.clear(); const IntTy *Low = &OldItems.begin()->first.getLow(); @@ -356,6 +380,8 @@ public: } RangeEx R(*Low, *High, Weight); add(R, Successor); + // We recollected the Items, but we kept it sorted. + Sorted = true; } /// Adds a constant value. @@ -374,7 +400,7 @@ public: add(REx, S); } void add(const RangeEx &R, SuccessorClass *S = 0) { - Items.insert(std::make_pair(R, S)); + Items.push_back(std::make_pair(R, S)); if (!R.isSingleNumber()) SingleNumbersOnly = false; } @@ -389,7 +415,7 @@ public: } void add(self& RHS) { - Items.insert(RHS.Items.begin(), RHS.Items.end()); + Items.insert(Items.end(), RHS.Items.begin(), RHS.Items.end()); if (!RHS.SingleNumbersOnly) SingleNumbersOnly = false; } diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 2d074079ccf..42b9099cdc3 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -2450,23 +2450,22 @@ size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases, size_t numCmps = 0; for (Clusterifier::RangeIterator i = TheClusterifier.begin(), e = TheClusterifier.end(); i != e; ++i, ++numCmps) { - const Clusterifier::RangeEx &R = i->first; - MachineBasicBlock *MBB = i->second; + Clusterifier::Cluster &C = *i; unsigned W = 0; if (BPI) { - W = BPI->getEdgeWeight(SI.getParent(), MBB->getBasicBlock()); + W = BPI->getEdgeWeight(SI.getParent(), C.second->getBasicBlock()); if (!W) W = 16; - W *= R.Weight; - BPI->setEdgeWeight(SI.getParent(), MBB->getBasicBlock(), W); + W *= C.first.Weight; + BPI->setEdgeWeight(SI.getParent(), C.second->getBasicBlock(), W); } // FIXME: Currently work with ConstantInt based numbers. // Changing it to APInt based is a pretty heavy for this commit. - Cases.push_back(Case(R.getLow().toConstantInt(), - R.getHigh().toConstantInt(), MBB, W)); + Cases.push_back(Case(C.first.getLow().toConstantInt(), + C.first.getHigh().toConstantInt(), C.second, W)); - if (R.getLow() != R.getHigh()) + if (C.first.getLow() != C.first.getHigh()) // A range counts double, since it requires two compares. ++numCmps; } diff --git a/lib/Transforms/Utils/LowerSwitch.cpp b/lib/Transforms/Utils/LowerSwitch.cpp index d2bd9d02846..1547439b5c6 100644 --- a/lib/Transforms/Utils/LowerSwitch.cpp +++ b/lib/Transforms/Utils/LowerSwitch.cpp @@ -238,14 +238,13 @@ unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) { size_t numCmps = 0; for (IntegersSubsetToBB::RangeIterator i = TheClusterifier.begin(), e = TheClusterifier.end(); i != e; ++i, ++numCmps) { - const IntegersSubsetToBB::RangeTy &R = i->first; - BasicBlock *S = i->second; + IntegersSubsetToBB::Cluster &C = *i; // FIXME: Currently work with ConstantInt based numbers. // Changing it to APInt based is a pretty heavy for this commit. - Cases.push_back(CaseRange(R.getLow().toConstantInt(), - R.getHigh().toConstantInt(), S)); - if (R.isSingleNumber()) + Cases.push_back(CaseRange(C.first.getLow().toConstantInt(), + C.first.getHigh().toConstantInt(), C.second)); + if (C.first.isSingleNumber()) // A range counts double, since it requires two compares. ++numCmps; }