11
11
#include " llvm/ADT/SmallVector.h"
12
12
#include " llvm/ADT/Statistic.h"
13
13
#include " llvm/Analysis/ProfileSummaryInfo.h"
14
+ #include " llvm/IR/Constant.h"
14
15
#include " llvm/IR/Instructions.h"
15
16
#include " llvm/IR/IntrinsicInst.h"
16
17
#include " llvm/IR/Intrinsics.h"
@@ -35,9 +36,11 @@ STATISTIC(NumChecksRemoved, "Number of removed checks");
35
36
36
37
static bool removeUbsanTraps (Function &F, const BlockFrequencyInfo &BFI,
37
38
const ProfileSummaryInfo *PSI) {
38
- SmallVector<IntrinsicInst *, 16 > Remove ;
39
+ SmallVector<std::pair< IntrinsicInst *, bool >, 16 > ReplaceWithValue ;
39
40
std::unique_ptr<RandomNumberGenerator> Rng;
40
41
42
+ // TODO:
43
+ // https://github.com/llvm/llvm-project/pull/84858#discussion_r1520603139
41
44
auto ShouldRemove = [&](bool IsHot) {
42
45
if (!RandomRate.getNumOccurrences ())
43
46
return IsHot;
@@ -54,21 +57,23 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
54
57
continue ;
55
58
auto ID = II->getIntrinsicID ();
56
59
switch (ID) {
57
- case Intrinsic::ubsantrap: {
60
+ case Intrinsic::allow_ubsan_check:
61
+ case Intrinsic::allow_runtime_check: {
58
62
++NumChecksTotal;
59
63
60
64
bool IsHot = false ;
61
65
if (PSI) {
62
- uint64_t Count = 0 ;
63
- for (const auto *PR : predecessors (&BB))
64
- Count += BFI.getBlockProfileCount (PR).value_or (0 );
66
+ uint64_t Count = BFI.getBlockProfileCount (&BB).value_or (0 );
65
67
IsHot = PSI->isHotCountNthPercentile (HotPercentileCutoff, Count);
66
68
}
67
69
68
- if (ShouldRemove (IsHot)) {
69
- Remove.push_back (II);
70
+ bool ToRemove = ShouldRemove (IsHot);
71
+ ReplaceWithValue.push_back ({
72
+ II,
73
+ ToRemove,
74
+ });
75
+ if (ToRemove)
70
76
++NumChecksRemoved;
71
- }
72
77
break ;
73
78
}
74
79
default :
@@ -77,10 +82,12 @@ static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
77
82
}
78
83
}
79
84
80
- for (IntrinsicInst *I : Remove)
85
+ for (auto [I, V] : ReplaceWithValue) {
86
+ I->replaceAllUsesWith (ConstantInt::getBool (I->getType (), !V));
81
87
I->eraseFromParent ();
88
+ }
82
89
83
- return !Remove .empty ();
90
+ return !ReplaceWithValue .empty ();
84
91
}
85
92
86
93
PreservedAnalyses RemoveTrapsPass::run (Function &F,
0 commit comments