@@ -330,10 +330,8 @@ std::pair<unsigned, uint64_t> BinaryFunction::eraseInvalidBBs() {
330
330
assert (BasicBlocks.size () == BasicBlocksLayout.size ());
331
331
332
332
// Update CFG state if needed
333
- if (Count > 0 ) {
334
- updateBBIndices (0 );
335
- recomputeLandingPads (0 , BasicBlocks.size ());
336
- }
333
+ if (Count > 0 )
334
+ recomputeLandingPads ();
337
335
338
336
return std::make_pair (Count, Bytes);
339
337
}
@@ -1457,54 +1455,39 @@ bool BinaryFunction::postProcessIndirectBranches() {
1457
1455
return true ;
1458
1456
}
1459
1457
1460
- void BinaryFunction::clearLandingPads (const unsigned StartIndex,
1461
- const unsigned NumBlocks) {
1462
- // remove all landing pads/throws for the given collection of blocks
1463
- for (auto I = StartIndex; I < StartIndex + NumBlocks; ++I) {
1464
- BasicBlocks[I]->clearLandingPads ();
1465
- }
1466
- }
1458
+ void BinaryFunction::recomputeLandingPads () {
1459
+ updateBBIndices (0 );
1467
1460
1468
- void BinaryFunction::addLandingPads (const unsigned StartIndex,
1469
- const unsigned NumBlocks) {
1470
1461
for (auto *BB : BasicBlocks) {
1471
- if (LandingPads.find (BB->getLabel ()) != LandingPads.end ()) {
1472
- const MCSymbol *LP = BB->getLabel ();
1473
- for (unsigned I : LPToBBIndex[LP]) {
1474
- assert (I < BasicBlocks.size ());
1475
- BinaryBasicBlock *ThrowBB = BasicBlocks[I];
1476
- const unsigned ThrowBBIndex = getIndex (ThrowBB);
1477
- if (ThrowBBIndex >= StartIndex && ThrowBBIndex < StartIndex + NumBlocks)
1478
- ThrowBB->addLandingPad (BB);
1479
- }
1480
- }
1462
+ BB->LandingPads .clear ();
1463
+ BB->Throwers .clear ();
1481
1464
}
1482
1465
1483
- clearList (LPToBBIndex);
1484
- }
1466
+ for (auto *BB : BasicBlocks) {
1467
+ for (auto &Instr : *BB) {
1468
+ if (!BC.MIA ->isInvoke (Instr))
1469
+ continue ;
1485
1470
1486
- void BinaryFunction::recomputeLandingPads (const unsigned StartIndex,
1487
- const unsigned NumBlocks) {
1488
- assert (LPToBBIndex.empty ());
1489
-
1490
- clearLandingPads (StartIndex, NumBlocks);
1491
-
1492
- for (auto I = StartIndex; I < StartIndex + NumBlocks; ++I) {
1493
- auto *BB = BasicBlocks[I];
1494
- for (auto &Instr : BB->instructions ()) {
1495
- // Store info about associated landing pad.
1496
- if (BC.MIA ->isInvoke (Instr)) {
1497
- const MCSymbol *LP;
1498
- uint64_t Action;
1499
- std::tie (LP, Action) = BC.MIA ->getEHInfo (Instr);
1500
- if (LP) {
1501
- LPToBBIndex[LP].push_back (getIndex (BB));
1502
- }
1503
- }
1471
+ const MCSymbol *LPLabel;
1472
+ uint64_t Action;
1473
+ std::tie (LPLabel, Action) = BC.MIA ->getEHInfo (Instr);
1474
+ if (!LPLabel)
1475
+ continue ;
1476
+
1477
+ auto *LPBlock = getBasicBlockForLabel (LPLabel);
1478
+ BB->LandingPads .emplace_back (LPBlock);
1479
+ LPBlock->Throwers .emplace_back (BB);
1504
1480
}
1481
+ std::sort (BB->lp_begin (), BB->lp_end ());
1482
+ auto NewEnd = std::unique (BB->lp_begin (), BB->lp_end ());
1483
+ BB->LandingPads .erase (NewEnd, BB->lp_end ());
1505
1484
}
1506
1485
1507
- addLandingPads (StartIndex, NumBlocks);
1486
+ for (auto *BB : BasicBlocks) {
1487
+ std::sort (BB->throw_begin (), BB->throw_end ());
1488
+ auto NewEnd = std::unique (BB->throw_begin (), BB->throw_end ());
1489
+ BB->Throwers .erase (NewEnd, BB->throw_end ());
1490
+ }
1508
1491
}
1509
1492
1510
1493
bool BinaryFunction::buildCFG () {
@@ -1608,16 +1591,6 @@ bool BinaryFunction::buildCFG() {
1608
1591
CFIOffset = getSize ();
1609
1592
addCFIPlaceholders (CFIOffset, InsertBB);
1610
1593
1611
- // Store info about associated landing pad.
1612
- if (MIA->isInvoke (Instr)) {
1613
- const MCSymbol *LP;
1614
- uint64_t Action;
1615
- std::tie (LP, Action) = MIA->getEHInfo (Instr);
1616
- if (LP) {
1617
- LPToBBIndex[LP].push_back (getIndex (InsertBB));
1618
- }
1619
- }
1620
-
1621
1594
if (MIA->isTerminator (Instr)) {
1622
1595
PrevBB = InsertBB;
1623
1596
InsertBB = nullptr ;
@@ -1810,8 +1783,7 @@ bool BinaryFunction::buildCFG() {
1810
1783
DEBUG (dbgs () << " last block was marked as a fall-through\n " );
1811
1784
}
1812
1785
1813
- // Add associated landing pad blocks to each basic block.
1814
- addLandingPads (0 , BasicBlocks.size ());
1786
+ recomputeLandingPads ();
1815
1787
1816
1788
// Infer frequency for non-taken branches
1817
1789
if (hasValidProfile () && opts::DoMCF != MCF_DISABLE) {
@@ -1873,7 +1845,6 @@ bool BinaryFunction::buildCFG() {
1873
1845
clearList (TakenBranches);
1874
1846
clearList (FTBranches);
1875
1847
clearList (IgnoredBranches);
1876
- clearList (LPToBBIndex);
1877
1848
clearList (EntryOffsets);
1878
1849
1879
1850
// Update the state.
@@ -3033,23 +3004,35 @@ bool BinaryFunction::validateCFG() const {
3033
3004
return Valid;
3034
3005
3035
3006
for (auto *BB : BasicBlocks) {
3036
- std::set<BinaryBasicBlock *> Seen;
3007
+ if (!std::is_sorted (BB->lp_begin (), BB->lp_end ())) {
3008
+ errs () << " BOLT-ERROR: unsorted list of landing pads in "
3009
+ << BB->getName () << " in function " << *this << ' \n ' ;
3010
+ return false ;
3011
+ }
3012
+ if (std::unique (BB->lp_begin (), BB->lp_end ()) != BB->lp_end ()) {
3013
+ errs () << " BOLT-ERROR: duplicate landing pad detected in"
3014
+ << BB->getName () << " in function " << *this << ' \n ' ;
3015
+ return false ;
3016
+ }
3017
+ if (!std::is_sorted (BB->throw_begin (), BB->throw_end ())) {
3018
+ errs () << " BOLT-ERROR: unsorted list of throwers in "
3019
+ << BB->getName () << " in function " << *this << ' \n ' ;
3020
+ return false ;
3021
+ }
3022
+ if (std::unique (BB->throw_begin (), BB->throw_end ()) != BB->throw_end ()) {
3023
+ errs () << " BOLT-ERROR: duplicate thrower detected in"
3024
+ << BB->getName () << " in function " << *this << ' \n ' ;
3025
+ return false ;
3026
+ }
3037
3027
for (auto *LPBlock : BB->LandingPads ) {
3038
- Valid &= Seen.count (LPBlock) == 0 ;
3039
- if (!Valid) {
3040
- errs () << " BOLT-WARNING: Duplicate LP seen " << LPBlock->getName ()
3041
- << " in " << *this << " \n " ;
3042
- break ;
3043
- }
3044
- Seen.insert (LPBlock);
3045
- auto count = LPBlock->Throwers .count (BB);
3046
- Valid &= (count == 1 );
3047
- if (!Valid) {
3048
- errs () << " BOLT-WARNING: Inconsistent landing pad detected in "
3049
- << *this << " : " << LPBlock->getName ()
3050
- << " is in LandingPads but not in " << BB->getName ()
3051
- << " ->Throwers\n " ;
3052
- break ;
3028
+ if (!std::binary_search (LPBlock->throw_begin (),
3029
+ LPBlock->throw_end (),
3030
+ BB)) {
3031
+ errs () << " BOLT-ERROR: inconsistent landing pad detected in "
3032
+ << *this << " : " << BB->getName ()
3033
+ << " is in LandingPads but not in " << LPBlock->getName ()
3034
+ << " Throwers\n " ;
3035
+ return false ;
3053
3036
}
3054
3037
}
3055
3038
}
@@ -3590,12 +3573,7 @@ void BinaryFunction::insertBasicBlocks(
3590
3573
BasicBlocks[I++] = BB.release ();
3591
3574
}
3592
3575
3593
- updateBBIndices (StartIndex);
3594
-
3595
- recomputeLandingPads (StartIndex, NumNewBlocks + 1 );
3596
-
3597
- // Make sure the basic blocks are sorted properly.
3598
- assert (std::is_sorted (begin (), end ()));
3576
+ recomputeLandingPads ();
3599
3577
3600
3578
if (UpdateLayout) {
3601
3579
updateLayout (Start, NumNewBlocks);
@@ -3624,12 +3602,7 @@ BinaryFunction::iterator BinaryFunction::insertBasicBlocks(
3624
3602
BasicBlocks[I++] = BB.release ();
3625
3603
}
3626
3604
3627
- updateBBIndices (StartIndex);
3628
-
3629
- recomputeLandingPads (StartIndex, NumNewBlocks + 1 );
3630
-
3631
- // Make sure the basic blocks are sorted properly.
3632
- assert (std::is_sorted (begin (), end ()));
3605
+ recomputeLandingPads ();
3633
3606
3634
3607
if (UpdateLayout) {
3635
3608
updateLayout (*std::prev (RetIter), NumNewBlocks);
0 commit comments