@@ -396,24 +396,26 @@ void MinBranchGreedyClusterAlgorithm::reset() {
396
396
}
397
397
398
398
void OptimalReorderAlgorithm::reorderBasicBlocks (
399
- const BinaryFunction &BF, BasicBlockOrder &Order) const {
399
+ const BinaryFunction &BF, BasicBlockOrder &Order) const {
400
400
std::vector<std::vector<uint64_t >> Weight;
401
- std::unordered_map<const BinaryBasicBlock *, int > BBToIndex;
402
401
std::vector<BinaryBasicBlock *> IndexToBB;
403
402
404
- unsigned N = BF.layout_size ();
403
+ const auto N = BF.layout_size ();
404
+ assert (N <= std::numeric_limits<uint64_t >::digits &&
405
+ " cannot use TSP solution for sizes larger than bits in uint64_t" );
406
+
405
407
// Populating weight map and index map
406
- for (auto BB : BF.layout ()) {
407
- BBToIndex[BB] = IndexToBB.size ();
408
+ for (auto * BB : BF.layout ()) {
409
+ BB-> setLayoutIndex ( IndexToBB.size () );
408
410
IndexToBB.push_back (BB);
409
411
}
410
412
Weight.resize (N);
411
- for (auto BB : BF.layout ()) {
413
+ for (auto * BB : BF.layout ()) {
412
414
auto BI = BB->branch_info_begin ();
413
- Weight[BBToIndex[BB] ].resize (N);
414
- for (auto I : BB->successors ()) {
415
+ Weight[BB-> getLayoutIndex () ].resize (N);
416
+ for (auto *SuccBB : BB->successors ()) {
415
417
if (BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE)
416
- Weight[BBToIndex[BB]][BBToIndex[I] ] = BI->Count ;
418
+ Weight[BB-> getLayoutIndex ()][SuccBB-> getLayoutIndex () ] = BI->Count ;
417
419
++BI;
418
420
}
419
421
}
@@ -427,26 +429,26 @@ void OptimalReorderAlgorithm::reorderBasicBlocks(
427
429
DP[1 ][0 ] = 0 ;
428
430
// Walk through TSP solutions using a bitmask to represent state (current set
429
431
// of BBs in the layout)
430
- unsigned BestSet = 1 ;
431
- unsigned BestLast = 0 ;
432
+ uint64_t BestSet = 1 ;
433
+ uint64_t BestLast = 0 ;
432
434
int64_t BestWeight = 0 ;
433
- for (unsigned Set = 1 ; Set < (1U << N); ++Set) {
435
+ for (uint64_t Set = 1 ; Set < (1ULL << N); ++Set) {
434
436
// Traverse each possibility of Last BB visited in this layout
435
- for (unsigned Last = 0 ; Last < N; ++Last) {
437
+ for (uint64_t Last = 0 ; Last < N; ++Last) {
436
438
// Case 1: There is no possible layout with this BB as Last
437
439
if (DP[Set][Last] == -1 )
438
440
continue ;
439
441
440
442
// Case 2: There is a layout with this Set and this Last, and we try
441
443
// to expand this set with New
442
- for (unsigned New = 1 ; New < N; ++New) {
444
+ for (uint64_t New = 1 ; New < N; ++New) {
443
445
// Case 2a: BB "New" is already in this Set
444
- if ((Set & (1 << New)) != 0 )
446
+ if ((Set & (1ULL << New)) != 0 )
445
447
continue ;
446
448
447
449
// Case 2b: BB "New" is not in this set and we add it to this Set and
448
450
// record total weight of this layout with "New" as the last BB.
449
- unsigned NewSet = (Set | (1 << New));
451
+ uint64_t NewSet = (Set | (1ULL << New));
450
452
if (DP[NewSet][New] == -1 )
451
453
DP[NewSet][New] = DP[Set][Last] + (int64_t )Weight[Last][New];
452
454
DP[NewSet][New] = std::max (DP[NewSet][New],
@@ -462,38 +464,42 @@ void OptimalReorderAlgorithm::reorderBasicBlocks(
462
464
}
463
465
464
466
// Define final function layout based on layout that maximizes weight
465
- unsigned Last = BestLast;
466
- unsigned Set = BestSet;
467
+ uint64_t Last = BestLast;
468
+ uint64_t Set = BestSet;
467
469
std::vector<bool > Visited;
468
470
Visited.resize (N);
469
471
Visited[Last] = true ;
470
472
Order.push_back (IndexToBB[Last]);
471
- Set = Set & ~(1U << Last);
473
+ Set = Set & ~(1ULL << Last);
472
474
while (Set != 0 ) {
473
475
int64_t Best = -1 ;
474
- for (unsigned I = 0 ; I < N; ++I) {
476
+ uint64_t NewLast;
477
+ for (uint64_t I = 0 ; I < N; ++I) {
475
478
if (DP[Set][I] == -1 )
476
479
continue ;
477
- if (DP[Set][I] > Best) {
478
- Last = I;
479
- Best = DP[Set][I];
480
+ int64_t AdjWeight = Weight[I][Last] > 0 ? Weight[I][Last] : 0 ;
481
+ if (DP[Set][I] + AdjWeight > Best) {
482
+ NewLast = I;
483
+ Best = DP[Set][I] + AdjWeight;
480
484
}
481
485
}
486
+ Last = NewLast;
482
487
Visited[Last] = true ;
483
488
Order.push_back (IndexToBB[Last]);
484
- Set = Set & ~(1U << Last);
489
+ Set = Set & ~(1ULL << Last);
485
490
}
486
491
std::reverse (Order.begin (), Order.end ());
487
492
488
- // Finalize layout with BBs that weren't assigned to the layout
489
- for (auto BB : BF.layout ()) {
490
- if (Visited[BBToIndex[BB]] == false )
493
+ // Finalize layout with BBs that weren't assigned to the layout using the
494
+ // input layout.
495
+ for (auto *BB : BF.layout ()) {
496
+ if (Visited[BB->getLayoutIndex ()] == false )
491
497
Order.push_back (BB);
492
498
}
493
499
}
494
500
495
501
void OptimizeReorderAlgorithm::reorderBasicBlocks (
496
- const BinaryFunction &BF, BasicBlockOrder &Order) const {
502
+ const BinaryFunction &BF, BasicBlockOrder &Order) const {
497
503
if (BF.layout_empty ())
498
504
return ;
499
505
@@ -509,7 +515,7 @@ void OptimizeReorderAlgorithm::reorderBasicBlocks(
509
515
}
510
516
511
517
void OptimizeBranchReorderAlgorithm::reorderBasicBlocks (
512
- const BinaryFunction &BF, BasicBlockOrder &Order) const {
518
+ const BinaryFunction &BF, BasicBlockOrder &Order) const {
513
519
if (BF.layout_empty ())
514
520
return ;
515
521
0 commit comments