@@ -983,33 +983,41 @@ MemDepResult MemoryDependenceResults::getNonLocalInfoForBlock(
983983static void
984984SortNonLocalDepInfoCache (MemoryDependenceResults::NonLocalDepInfo &Cache,
985985 unsigned NumSortedEntries) {
986- switch (Cache.size () - NumSortedEntries) {
987- case 0 :
988- // done, no new entries.
989- break ;
990- case 2 : {
991- // Two new entries, insert the last one into place.
992- NonLocalDepEntry Val = Cache.back ();
993- Cache.pop_back ();
994- MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
995- std::upper_bound (Cache.begin (), Cache.end () - 1 , Val);
996- Cache.insert (Entry, Val);
997- [[fallthrough]];
986+
987+ // Output number of sorted entries and size of cache for each sort.
988+ LLVM_DEBUG (dbgs () << " NumSortedEntries: " << NumSortedEntries
989+ << " , Cache.size: " << Cache.size () << " \n " );
990+
991+ // If only one entry, don't sort.
992+ if (Cache.size () < 2 )
993+ return ;
994+
995+ unsigned s = Cache.size () - NumSortedEntries;
996+
997+ // If the cache is already sorted, don't sort it again.
998+ if (s == 0 )
999+ return ;
1000+
1001+ // If no entry is sorted, sort the whole cache.
1002+ if (NumSortedEntries == 0 ) {
1003+ llvm::sort (Cache);
1004+ return ;
9981005 }
999- case 1 :
1000- // One new entry, Just insert the new value at the appropriate position.
1001- if (Cache.size () != 1 ) {
1006+
1007+ // If the number of unsorted entires is small and the cache size is big, use
1008+ // insertion sort is faster. Here use Log2_32 to quickly choose the sort
1009+ // method.
1010+ if (s < Log2_32 (Cache.size ())) {
1011+ while (s > 0 ) {
10021012 NonLocalDepEntry Val = Cache.back ();
10031013 Cache.pop_back ();
10041014 MemoryDependenceResults::NonLocalDepInfo::iterator Entry =
1005- llvm ::upper_bound (Cache, Val);
1015+ std ::upper_bound (Cache. begin (), Cache. end () - s + 1 , Val);
10061016 Cache.insert (Entry, Val);
1017+ s--;
10071018 }
1008- break ;
1009- default :
1010- // Added many values, do a full scale sort.
1019+ } else {
10111020 llvm::sort (Cache);
1012- break ;
10131021 }
10141022}
10151023
0 commit comments