@@ -423,24 +423,12 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
423423 }
424424 }
425425
426- // Now we found a bunch of sets of globals used together. We accumulated
427- // the number of times we encountered the sets (i.e., the number of functions
428- // that use that exact set of globals).
429- //
430- // Multiply that by the size of the set to give us a crude profitability
431- // metric.
432- llvm::stable_sort (UsedGlobalSets,
433- [](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
434- return UGS1.Globals .count () * UGS1.UsageCount <
435- UGS2.Globals .count () * UGS2.UsageCount ;
436- });
437-
438426 // We can choose to merge all globals together, but ignore globals never used
439427 // with another global. This catches the obviously non-profitable cases of
440428 // having a single global, but is aggressive enough for any other case.
441429 if (GlobalMergeIgnoreSingleUse) {
442430 BitVector AllGlobals (Globals.size ());
443- for (const UsedGlobalSet &UGS : llvm::reverse ( UsedGlobalSets) ) {
431+ for (const UsedGlobalSet &UGS : UsedGlobalSets) {
444432 if (UGS.UsageCount == 0 )
445433 continue ;
446434 if (UGS.Globals .count () > 1 )
@@ -449,6 +437,18 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
449437 return doMerge (Globals, AllGlobals, M, isConst, AddrSpace);
450438 }
451439
440+ // Now we found a bunch of sets of globals used together. We accumulated
441+ // the number of times we encountered the sets (i.e., the number of functions
442+ // that use that exact set of globals).
443+ //
444+ // Multiply that by the size of the set to give us a crude profitability
445+ // metric.
446+ llvm::stable_sort (UsedGlobalSets,
447+ [](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
448+ return UGS1.Globals .count () * UGS1.UsageCount <
449+ UGS2.Globals .count () * UGS2.UsageCount ;
450+ });
451+
452452 // Starting from the sets with the best (=biggest) profitability, find a
453453 // good combination.
454454 // The ideal (and expensive) solution can only be found by trying all
0 commit comments