1//===- LowerAllowCheckPass.cpp ----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
10
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/Statistic.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Analysis/OptimizationRemarkEmitter.h"
15#include "llvm/Analysis/ProfileSummaryInfo.h"
16#include "llvm/IR/Constant.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/DiagnosticInfo.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/IntrinsicInst.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/Metadata.h"
23#include "llvm/Support/RandomNumberGenerator.h"
24#include <memory>
25#include <random>
26
27using namespace llvm;
28
29#define DEBUG_TYPE "lower-allow-check"
30
31static cl::opt<int>
32 HotPercentileCutoff("lower-allow-check-percentile-cutoff-hot",
33 cl::desc("Hot percentile cuttoff."));
34
35static cl::opt<float>
36 RandomRate("lower-allow-check-random-rate",
37 cl::desc("Probability value in the range [0.0, 1.0] of "
38 "unconditional pseudo-random checks."));
39
40STATISTIC(NumChecksTotal, "Number of checks");
41STATISTIC(NumChecksRemoved, "Number of removed checks");
42
43struct RemarkInfo {
44 ore::NV Kind;
45 ore::NV F;
46 ore::NV BB;
47 explicit RemarkInfo(IntrinsicInst *II)
48 : Kind("Kind", II->getArgOperand(i: 0)),
49 F("Function", II->getParent()->getParent()),
50 BB("Block", II->getParent()->getName()) {}
51};
52
53static void emitRemark(IntrinsicInst *II, OptimizationRemarkEmitter &ORE,
54 bool Removed) {
55 if (Removed) {
56 ORE.emit(RemarkBuilder: [&]() {
57 RemarkInfo Info(II);
58 return OptimizationRemark(DEBUG_TYPE, "Removed", II)
59 << "Removed check: Kind=" << Info.Kind << " F=" << Info.F
60 << " BB=" << Info.BB;
61 });
62 } else {
63 ORE.emit(RemarkBuilder: [&]() {
64 RemarkInfo Info(II);
65 return OptimizationRemarkMissed(DEBUG_TYPE, "Allowed", II)
66 << "Allowed check: Kind=" << Info.Kind << " F=" << Info.F
67 << " BB=" << Info.BB;
68 });
69 }
70}
71
72static bool removeUbsanTraps(Function &F, const BlockFrequencyInfo &BFI,
73 const ProfileSummaryInfo *PSI,
74 OptimizationRemarkEmitter &ORE) {
75 SmallVector<std::pair<IntrinsicInst *, bool>, 16> ReplaceWithValue;
76 std::unique_ptr<RandomNumberGenerator> Rng;
77
78 auto ShouldRemove = [&](bool IsHot) {
79 if (!RandomRate.getNumOccurrences())
80 return IsHot;
81 if (!Rng)
82 Rng = F.getParent()->createRNG(Name: F.getName());
83 std::bernoulli_distribution D(RandomRate);
84 return !D(*Rng);
85 };
86
87 for (BasicBlock &BB : F) {
88 for (Instruction &I : BB) {
89 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Val: &I);
90 if (!II)
91 continue;
92 auto ID = II->getIntrinsicID();
93 switch (ID) {
94 case Intrinsic::allow_ubsan_check:
95 case Intrinsic::allow_runtime_check: {
96 ++NumChecksTotal;
97
98 bool IsHot = false;
99 if (PSI) {
100 uint64_t Count = BFI.getBlockProfileCount(BB: &BB).value_or(u: 0);
101 IsHot = PSI->isHotCountNthPercentile(PercentileCutoff: HotPercentileCutoff, C: Count);
102 }
103
104 bool ToRemove = ShouldRemove(IsHot);
105 ReplaceWithValue.push_back(Elt: {
106 II,
107 ToRemove,
108 });
109 if (ToRemove)
110 ++NumChecksRemoved;
111 emitRemark(II, ORE, Removed: ToRemove);
112 break;
113 }
114 default:
115 break;
116 }
117 }
118 }
119
120 for (auto [I, V] : ReplaceWithValue) {
121 I->replaceAllUsesWith(V: ConstantInt::getBool(Ty: I->getType(), V: !V));
122 I->eraseFromParent();
123 }
124
125 return !ReplaceWithValue.empty();
126}
127
128PreservedAnalyses LowerAllowCheckPass::run(Function &F,
129 FunctionAnalysisManager &AM) {
130 if (F.isDeclaration())
131 return PreservedAnalyses::all();
132 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(IR&: F);
133 ProfileSummaryInfo *PSI =
134 MAMProxy.getCachedResult<ProfileSummaryAnalysis>(IR&: *F.getParent());
135 BlockFrequencyInfo &BFI = AM.getResult<BlockFrequencyAnalysis>(IR&: F);
136 OptimizationRemarkEmitter &ORE =
137 AM.getResult<OptimizationRemarkEmitterAnalysis>(IR&: F);
138
139 return removeUbsanTraps(F, BFI, PSI, ORE) ? PreservedAnalyses::none()
140 : PreservedAnalyses::all();
141}
142
143bool LowerAllowCheckPass::IsRequested() {
144 return RandomRate.getNumOccurrences() ||
145 HotPercentileCutoff.getNumOccurrences();
146}
147

source code of llvm/lib/Transforms/Instrumentation/LowerAllowCheckPass.cpp