Skip to content

Commit 9ccb2d7

Browse files
committed
fix division by zero
1 parent 2de14fb commit 9ccb2d7

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

bolt/lib/Passes/ProfileQualityStats.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ void printCFGContinuityStats(raw_ostream &OS,
156156
const size_t NumPosECBBsUnreachableFromEntry =
157157
NumPosECBBs - NumReachableBBs;
158158
const size_t SumUnreachableBBEC = SumAllBBEC - SumReachableBBEC;
159-
const double FractionECUnreachable =
160-
(double)SumUnreachableBBEC / SumAllBBEC;
159+
160+
double FractionECUnreachable = 0.0;
161+
if (SumAllBBEC > 0)
162+
FractionECUnreachable = (double)SumUnreachableBBEC / SumAllBBEC;
161163

162164
if (opts::Verbosity >= 2 && FractionECUnreachable >= 0.05) {
163165
OS << "Non-trivial CFG discontinuity observed in function "
@@ -252,7 +254,9 @@ void printCallGraphFlowConservationStats(
252254
TotalFlowMap.CallGraphIncomingFlows[Function->getFunctionNumber()];
253255
const uint64_t Min = std::min(NetEntryOutflow, CallGraphInflow);
254256
const uint64_t Max = std::max(NetEntryOutflow, CallGraphInflow);
255-
const double CallGraphGap = 1 - (double)Min / Max;
257+
double CallGraphGap = 0.0;
258+
if (Max > 0)
259+
CallGraphGap = 1 - (double)Min / Max;
256260

257261
if (opts::Verbosity >= 2 && CallGraphGap >= 0.5) {
258262
OS << "Non-trivial call graph gap of size "
@@ -334,7 +338,9 @@ void printCFGFlowConservationStats(const BinaryContext &BC, raw_ostream &OS,
334338

335339
const uint64_t Max = MaxCountMaps[BB.getLayoutIndex()];
336340
const uint64_t Min = MinCountMaps[BB.getLayoutIndex()];
337-
const double Gap = 1 - (double)Min / Max;
341+
double Gap = 0.0;
342+
if (Max > 0)
343+
Gap = 1 - (double)Min / Max;
338344
double Weight = BB.getKnownExecutionCount() * BB.getNumNonPseudos();
339345
// We use log to prevent the stats from being dominated by extremely hot
340346
// blocks
@@ -425,8 +431,12 @@ void printExceptionHandlingStats(const BinaryContext &BC, raw_ostream &OS,
425431
LPCountFractionsOfTotalInvokeEC.push_back(0.0);
426432
continue;
427433
}
428-
const double FracTotalBBEC = (double)LPECSum / BBECSum;
429-
const double FracTotalInvokeEC = (double)LPECSum / InvokeECSum;
434+
double FracTotalBBEC = 0.0;
435+
if (BBECSum > 0)
436+
FracTotalBBEC = (double)LPECSum / BBECSum;
437+
double FracTotalInvokeEC = 0.0;
438+
if (InvokeECSum > 0)
439+
FracTotalInvokeEC = (double)LPECSum / InvokeECSum;
430440
LPCountFractionsOfTotalBBEC.push_back(FracTotalBBEC);
431441
LPCountFractionsOfTotalInvokeEC.push_back(FracTotalInvokeEC);
432442

0 commit comments

Comments
 (0)