@@ -423,12 +423,24 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
423
423
}
424
424
}
425
425
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
+
426
438
// We can choose to merge all globals together, but ignore globals never used
427
439
// with another global. This catches the obviously non-profitable cases of
428
440
// having a single global, but is aggressive enough for any other case.
429
441
if (GlobalMergeIgnoreSingleUse) {
430
442
BitVector AllGlobals (Globals.size ());
431
- for (const UsedGlobalSet &UGS : UsedGlobalSets) {
443
+ for (const UsedGlobalSet &UGS : llvm::reverse ( UsedGlobalSets) ) {
432
444
if (UGS.UsageCount == 0 )
433
445
continue ;
434
446
if (UGS.Globals .count () > 1 )
@@ -437,16 +449,6 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
437
449
return doMerge (Globals, AllGlobals, M, isConst, AddrSpace);
438
450
}
439
451
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). Multiply that by the size of the set
443
- // to give us a crude profitability metric.
444
- llvm::stable_sort (UsedGlobalSets,
445
- [](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) {
446
- return UGS1.Globals .count () * UGS1.UsageCount >=
447
- UGS2.Globals .count () * UGS2.UsageCount ;
448
- });
449
-
450
452
// Starting from the sets with the best (=biggest) profitability, find a
451
453
// good combination.
452
454
// The ideal (and expensive) solution can only be found by trying all
@@ -456,7 +458,7 @@ bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
456
458
BitVector PickedGlobals (Globals.size ());
457
459
bool Changed = false ;
458
460
459
- for (const UsedGlobalSet &UGS : UsedGlobalSets) {
461
+ for (const UsedGlobalSet &UGS : llvm::reverse ( UsedGlobalSets) ) {
460
462
if (UGS.UsageCount == 0 )
461
463
continue ;
462
464
if (PickedGlobals.anyCommon (UGS.Globals ))
0 commit comments