Skip to content

Commit d6e4042

Browse files
committed
[MemDep] speed up SortNonLocalDepInfoCache with a big cache size
1 parent 470f456 commit d6e4042

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

llvm/lib/Analysis/MemoryDependenceAnalysis.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <cassert>
5252
#include <iterator>
5353
#include <utility>
54+
#include <cmath>
5455

5556
using namespace llvm;
5657

@@ -83,6 +84,9 @@ static cl::opt<unsigned>
8384
// Limit on the number of memdep results to process.
8485
static const unsigned int NumResultsLimit = 100;
8586

87+
// for quickly calculating log
88+
const float ln2 = 0.69314718f;
89+
8690
/// This is a helper function that removes Val from 'Inst's set in ReverseMap.
8791
///
8892
/// If the set becomes empty, remove Inst's entry.
@@ -991,33 +995,20 @@ MemDepResult MemoryDependenceResults::getNonLocalInfoForBlock(
991995
static void
992996
SortNonLocalDepInfoCache(MemoryDependenceResults::NonLocalDepInfo &Cache,
993997
unsigned NumSortedEntries) {
994-
switch (Cache.size() - NumSortedEntries) {
995-
case 0:
996-
// done, no new entries.
997-
break;
998-
case 2: {
999-
// Two new entries, insert the last one into place.
1000-
NonLocalDepEntry Val = Cache.back();
1001-
Cache.pop_back();
1002-
MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
1003-
std::upper_bound(Cache.begin(), Cache.end() - 1, Val);
1004-
Cache.insert(Entry, Val);
1005-
[[fallthrough]];
1006-
}
1007-
case 1:
1008-
// One new entry, Just insert the new value at the appropriate position.
1009-
if (Cache.size() != 1) {
998+
999+
auto s = Cache.size() - NumSortedEntries;
1000+
if (s < log2(Cache.size()) * ln2) {
1001+
while (s>0) {
10101002
NonLocalDepEntry Val = Cache.back();
10111003
Cache.pop_back();
10121004
MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
1013-
llvm::upper_bound(Cache, Val);
1005+
std::upper_bound(Cache.begin(), Cache.end() - 1, Val);
10141006
Cache.insert(Entry, Val);
1007+
s--;
10151008
}
1016-
break;
1017-
default:
1018-
// Added many values, do a full scale sort.
1009+
}
1010+
else {
10191011
llvm::sort(Cache);
1020-
break;
10211012
}
10221013
}
10231014

0 commit comments

Comments
 (0)