@@ -983,27 +983,22 @@ using DirectEdges = SmallVector<PGOUseEdge *, 2>;
983
983
984
984
// This class stores the auxiliary information for each BB.
985
985
struct PGOUseBBInfo : public PGOBBInfo {
986
- uint64_t CountValue = 0 ;
987
- bool CountValid;
986
+ std::optional<uint64_t > Count;
988
987
int32_t UnknownCountInEdge = 0 ;
989
988
int32_t UnknownCountOutEdge = 0 ;
990
989
DirectEdges InEdges;
991
990
DirectEdges OutEdges;
992
991
993
- PGOUseBBInfo (unsigned IX) : PGOBBInfo(IX), CountValid( false ) {}
992
+ PGOUseBBInfo (unsigned IX) : PGOBBInfo(IX) {}
994
993
995
994
// Set the profile count value for this BB.
996
- void setBBInfoCount (uint64_t Value) {
997
- CountValue = Value;
998
- CountValid = true ;
999
- }
995
+ void setBBInfoCount (uint64_t Value) { Count = Value; }
1000
996
1001
997
// Return the information string of this object.
1002
998
std::string infoString () const {
1003
- if (!CountValid )
999
+ if (!Count )
1004
1000
return PGOBBInfo::infoString ();
1005
- return (Twine (PGOBBInfo::infoString ()) + " Count=" + Twine (CountValue))
1006
- .str ();
1001
+ return (Twine (PGOBBInfo::infoString ()) + " Count=" + Twine (*Count)).str ();
1007
1002
}
1008
1003
1009
1004
// Add an OutEdge and update the edge count.
@@ -1216,15 +1211,15 @@ bool PGOUseFunc::setInstrumentedCounts(
1216
1211
1217
1212
// If only one out-edge, the edge profile count should be the same as BB
1218
1213
// profile count.
1219
- if (SrcInfo.CountValid && SrcInfo.OutEdges .size () == 1 )
1220
- setEdgeCount (E.get (), SrcInfo.CountValue );
1214
+ if (SrcInfo.Count && SrcInfo.OutEdges .size () == 1 )
1215
+ setEdgeCount (E.get (), * SrcInfo.Count );
1221
1216
else {
1222
1217
const BasicBlock *DestBB = E->DestBB ;
1223
1218
PGOUseBBInfo &DestInfo = getBBInfo (DestBB);
1224
1219
// If only one in-edge, the edge profile count should be the same as BB
1225
1220
// profile count.
1226
- if (DestInfo.CountValid && DestInfo.InEdges .size () == 1 )
1227
- setEdgeCount (E.get (), DestInfo.CountValue );
1221
+ if (DestInfo.Count && DestInfo.InEdges .size () == 1 )
1222
+ setEdgeCount (E.get (), * DestInfo.Count );
1228
1223
}
1229
1224
if (E->CountValid )
1230
1225
continue ;
@@ -1481,38 +1476,36 @@ void PGOUseFunc::populateCounters() {
1481
1476
// For efficient traversal, it's better to start from the end as most
1482
1477
// of the instrumented edges are at the end.
1483
1478
for (auto &BB : reverse (F)) {
1484
- PGOUseBBInfo *Count = findBBInfo (&BB);
1485
- if (Count == nullptr )
1479
+ PGOUseBBInfo *UseBBInfo = findBBInfo (&BB);
1480
+ if (UseBBInfo == nullptr )
1486
1481
continue ;
1487
- if (!Count->CountValid ) {
1488
- if (Count->UnknownCountOutEdge == 0 ) {
1489
- Count->CountValue = sumEdgeCount (Count->OutEdges );
1490
- Count->CountValid = true ;
1482
+ if (!UseBBInfo->Count ) {
1483
+ if (UseBBInfo->UnknownCountOutEdge == 0 ) {
1484
+ UseBBInfo->Count = sumEdgeCount (UseBBInfo->OutEdges );
1491
1485
Changes = true ;
1492
- } else if (Count->UnknownCountInEdge == 0 ) {
1493
- Count->CountValue = sumEdgeCount (Count->InEdges );
1494
- Count->CountValid = true ;
1486
+ } else if (UseBBInfo->UnknownCountInEdge == 0 ) {
1487
+ UseBBInfo->Count = sumEdgeCount (UseBBInfo->InEdges );
1495
1488
Changes = true ;
1496
1489
}
1497
1490
}
1498
- if (Count-> CountValid ) {
1499
- if (Count ->UnknownCountOutEdge == 1 ) {
1491
+ if (UseBBInfo-> Count ) {
1492
+ if (UseBBInfo ->UnknownCountOutEdge == 1 ) {
1500
1493
uint64_t Total = 0 ;
1501
- uint64_t OutSum = sumEdgeCount (Count ->OutEdges );
1494
+ uint64_t OutSum = sumEdgeCount (UseBBInfo ->OutEdges );
1502
1495
// If the one of the successor block can early terminate (no-return),
1503
1496
// we can end up with situation where out edge sum count is larger as
1504
1497
// the source BB's count is collected by a post-dominated block.
1505
- if (Count-> CountValue > OutSum)
1506
- Total = Count-> CountValue - OutSum;
1507
- setEdgeCount (Count ->OutEdges , Total);
1498
+ if (*UseBBInfo-> Count > OutSum)
1499
+ Total = *UseBBInfo-> Count - OutSum;
1500
+ setEdgeCount (UseBBInfo ->OutEdges , Total);
1508
1501
Changes = true ;
1509
1502
}
1510
- if (Count ->UnknownCountInEdge == 1 ) {
1503
+ if (UseBBInfo ->UnknownCountInEdge == 1 ) {
1511
1504
uint64_t Total = 0 ;
1512
- uint64_t InSum = sumEdgeCount (Count ->InEdges );
1513
- if (Count-> CountValue > InSum)
1514
- Total = Count-> CountValue - InSum;
1515
- setEdgeCount (Count ->InEdges , Total);
1505
+ uint64_t InSum = sumEdgeCount (UseBBInfo ->InEdges );
1506
+ if (*UseBBInfo-> Count > InSum)
1507
+ Total = *UseBBInfo-> Count - InSum;
1508
+ setEdgeCount (UseBBInfo ->InEdges , Total);
1516
1509
Changes = true ;
1517
1510
}
1518
1511
}
@@ -1527,16 +1520,16 @@ void PGOUseFunc::populateCounters() {
1527
1520
auto BI = findBBInfo (&BB);
1528
1521
if (BI == nullptr )
1529
1522
continue ;
1530
- assert (BI->CountValid && " BB count is not valid" );
1523
+ assert (BI->Count && " BB count is not valid" );
1531
1524
}
1532
1525
#endif
1533
- uint64_t FuncEntryCount = getBBInfo (&*F.begin ()).CountValue ;
1526
+ uint64_t FuncEntryCount = * getBBInfo (&*F.begin ()).Count ;
1534
1527
uint64_t FuncMaxCount = FuncEntryCount;
1535
1528
for (auto &BB : F) {
1536
1529
auto BI = findBBInfo (&BB);
1537
1530
if (BI == nullptr )
1538
1531
continue ;
1539
- FuncMaxCount = std::max (FuncMaxCount, BI->CountValue );
1532
+ FuncMaxCount = std::max (FuncMaxCount, * BI->Count );
1540
1533
}
1541
1534
1542
1535
// Fix the obviously inconsistent entry count.
@@ -1566,11 +1559,11 @@ void PGOUseFunc::setBranchWeights() {
1566
1559
isa<CallBrInst>(TI)))
1567
1560
continue ;
1568
1561
1569
- if (getBBInfo (&BB).CountValue == 0 )
1562
+ const PGOUseBBInfo &BBCountInfo = getBBInfo (&BB);
1563
+ if (!*BBCountInfo.Count )
1570
1564
continue ;
1571
1565
1572
1566
// We have a non-zero Branch BB.
1573
- const PGOUseBBInfo &BBCountInfo = getBBInfo (&BB);
1574
1567
unsigned Size = BBCountInfo.OutEdges .size ();
1575
1568
SmallVector<uint64_t , 2 > EdgeCounts (Size, 0 );
1576
1569
uint64_t MaxCount = 0 ;
@@ -1622,7 +1615,7 @@ void PGOUseFunc::annotateIrrLoopHeaderWeights() {
1622
1615
if (BFI->isIrrLoopHeader (&BB) || isIndirectBrTarget (&BB)) {
1623
1616
Instruction *TI = BB.getTerminator ();
1624
1617
const PGOUseBBInfo &BBCountInfo = getBBInfo (&BB);
1625
- setIrrLoopHeaderMetadata (M, TI, BBCountInfo.CountValue );
1618
+ setIrrLoopHeaderMetadata (M, TI, * BBCountInfo.Count );
1626
1619
}
1627
1620
}
1628
1621
}
@@ -1649,7 +1642,7 @@ void SelectInstVisitor::annotateOneSelectInst(SelectInst &SI) {
1649
1642
uint64_t TotalCount = 0 ;
1650
1643
auto BI = UseFunc->findBBInfo (SI.getParent ());
1651
1644
if (BI != nullptr )
1652
- TotalCount = BI->CountValue ;
1645
+ TotalCount = * BI->Count ;
1653
1646
// False Count
1654
1647
SCounts[1 ] = (TotalCount > SCounts[0 ] ? TotalCount - SCounts[0 ] : 0 );
1655
1648
uint64_t MaxCount = std::max (SCounts[0 ], SCounts[1 ]);
@@ -1850,7 +1843,7 @@ static void fixFuncEntryCount(PGOUseFunc &Func, LoopInfo &LI,
1850
1843
if (!Func.findBBInfo (&BBI))
1851
1844
continue ;
1852
1845
auto BFICount = NBFI.getBlockProfileCount (&BBI);
1853
- CountValue = Func.getBBInfo (&BBI).CountValue ;
1846
+ CountValue = * Func.getBBInfo (&BBI).Count ;
1854
1847
BFICountValue = *BFICount;
1855
1848
SumCount.add (APFloat (CountValue * 1.0 ), APFloat::rmNearestTiesToEven);
1856
1849
SumBFICount.add (APFloat (BFICountValue * 1.0 ), APFloat::rmNearestTiesToEven);
@@ -1866,7 +1859,7 @@ static void fixFuncEntryCount(PGOUseFunc &Func, LoopInfo &LI,
1866
1859
if (Scale < 1.001 && Scale > 0.999 )
1867
1860
return ;
1868
1861
1869
- uint64_t FuncEntryCount = Func.getBBInfo (&*F.begin ()).CountValue ;
1862
+ uint64_t FuncEntryCount = * Func.getBBInfo (&*F.begin ()).Count ;
1870
1863
uint64_t NewEntryCount = 0.5 + FuncEntryCount * Scale;
1871
1864
if (NewEntryCount == 0 )
1872
1865
NewEntryCount = 1 ;
@@ -1896,8 +1889,7 @@ static void verifyFuncBFI(PGOUseFunc &Func, LoopInfo &LI,
1896
1889
uint64_t CountValue = 0 ;
1897
1890
uint64_t BFICountValue = 0 ;
1898
1891
1899
- if (Func.getBBInfo (&BBI).CountValid )
1900
- CountValue = Func.getBBInfo (&BBI).CountValue ;
1892
+ CountValue = Func.getBBInfo (&BBI).Count .value_or (CountValue);
1901
1893
1902
1894
BBNum++;
1903
1895
if (CountValue)
@@ -2279,8 +2271,8 @@ template <> struct DOTGraphTraits<PGOUseFunc *> : DefaultDOTGraphTraits {
2279
2271
OS << getSimpleNodeName (Node) << " :\\ l" ;
2280
2272
PGOUseBBInfo *BI = Graph->findBBInfo (Node);
2281
2273
OS << " Count : " ;
2282
- if (BI && BI->CountValid )
2283
- OS << BI->CountValue << " \\ l" ;
2274
+ if (BI && BI->Count )
2275
+ OS << * BI->Count << " \\ l" ;
2284
2276
else
2285
2277
OS << " Unknown\\ l" ;
2286
2278
0 commit comments