@@ -39,48 +39,54 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
3939 SmallVector<IntrinsicInst *, 16 > Remove;
4040 std::unique_ptr<RandomNumberGenerator> Rng;
4141
42+ auto ShouldRemove = [&](bool IsHot) {
43+ if (IsHot && !RandomRate.getNumOccurrences ())
44+ return true ;
45+ if (!Rng) {
46+ if (!RandomRate.getNumOccurrences ())
47+ return false ;
48+ Rng = F.getParent ()->createRNG (F.getName ());
49+ }
50+ std::bernoulli_distribution D (RandomRate);
51+ return D (*Rng);
52+ };
53+
4254 for (BasicBlock &BB : F) {
4355 for (Instruction &I : BB) {
4456 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
4557 if (!II)
4658 continue ;
4759 auto ID = II->getIntrinsicID ();
48- if (ID != Intrinsic::ubsantrap)
49- continue ;
50- ++NumChecksTotal;
51-
52- bool IsHot = false ;
53- if (PSI) {
54- uint64_t Count = 0 ;
55- for (const auto *PR : predecessors (&BB))
56- Count += BFI.getBlockProfileCount (PR).value_or (0 );
57-
58- IsHot = HotPercentileCutoff.getNumOccurrences ()
59- ? (HotPercentileCutoff > 0 &&
60- PSI->isHotCountNthPercentile (HotPercentileCutoff, Count))
61- : PSI->isHotCount (Count);
62- }
63-
64- auto ShouldRemove = [&]() {
65- if (IsHot)
66- return true ;
67- if (!Rng) {
68- if (!RandomRate.getNumOccurrences ())
69- return false ;
70- Rng = F.getParent ()->createRNG (F.getName ());
60+ switch (ID) {
61+ case Intrinsic::ubsantrap: {
62+ ++NumChecksTotal;
63+
64+ bool IsHot = false ;
65+ if (PSI) {
66+ uint64_t Count = 0 ;
67+ for (const auto *PR : predecessors (&BB))
68+ Count += BFI.getBlockProfileCount (PR).value_or (0 );
69+
70+ IsHot =
71+ HotPercentileCutoff.getNumOccurrences ()
72+ ? (HotPercentileCutoff > 0 &&
73+ PSI->isHotCountNthPercentile (HotPercentileCutoff, Count))
74+ : PSI->isHotCount (Count);
7175 }
72- std::bernoulli_distribution D (RandomRate);
73- return D (*Rng);
74- };
7576
76- if (ShouldRemove ()) {
77- Remove.push_back (II);
78- ++NumChecksRemoved;
77+ if (ShouldRemove (IsHot)) {
78+ Remove.push_back (II);
79+ ++NumChecksRemoved;
80+ }
81+ break ;
82+ }
83+ default :
84+ break ;
7985 }
8086 }
8187 }
8288
83- for (auto *I : Remove)
89+ for (IntrinsicInst *I : Remove)
8490 I->eraseFromParent ();
8591
8692 return !Remove.empty ();
@@ -96,5 +102,5 @@ PreservedAnalyses RemoveTrapsPass::run(Function &F,
96102 BlockFrequencyInfo &BFI = AM.getResult <BlockFrequencyAnalysis>(F);
97103
98104 return removeUbsanTraps (F, BFI, PSI) ? PreservedAnalyses::none ()
99- : PreservedAnalyses::all ();
105+ : PreservedAnalyses::all ();
100106}
0 commit comments