@@ -39,48 +39,54 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
39
39
SmallVector<IntrinsicInst *, 16 > Remove;
40
40
std::unique_ptr<RandomNumberGenerator> Rng;
41
41
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
+
42
54
for (BasicBlock &BB : F) {
43
55
for (Instruction &I : BB) {
44
56
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
45
57
if (!II)
46
58
continue ;
47
59
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);
71
75
}
72
- std::bernoulli_distribution D (RandomRate);
73
- return D (*Rng);
74
- };
75
76
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 ;
79
85
}
80
86
}
81
87
}
82
88
83
- for (auto *I : Remove)
89
+ for (IntrinsicInst *I : Remove)
84
90
I->eraseFromParent ();
85
91
86
92
return !Remove.empty ();
@@ -96,5 +102,5 @@ PreservedAnalyses RemoveTrapsPass::run(Function &F,
96
102
BlockFrequencyInfo &BFI = AM.getResult <BlockFrequencyAnalysis>(F);
97
103
98
104
return removeUbsanTraps (F, BFI, PSI) ? PreservedAnalyses::none ()
99
- : PreservedAnalyses::all ();
105
+ : PreservedAnalyses::all ();
100
106
}
0 commit comments