|
| 1 | +//===- RemoveTrapsPass.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 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#include "llvm/Transforms/Instrumentation/RemoveTrapsPass.h" |
| 13 | + |
| 14 | +#include "llvm/ADT/SmallVector.h" |
| 15 | +#include "llvm/ADT/Statistic.h" |
| 16 | +#include "llvm/Analysis/ProfileSummaryInfo.h" |
| 17 | +#include "llvm/IR/Instructions.h" |
| 18 | +#include "llvm/IR/IntrinsicInst.h" |
| 19 | +#include "llvm/IR/Intrinsics.h" |
| 20 | +#include <cstdint> |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +#define DEBUG_TYPE "remove-traps" |
| 25 | + |
| 26 | +static constexpr unsigned MaxRandomRate = 1000; |
| 27 | + |
| 28 | +static cl::opt<int> HotPercentileCutoff( |
| 29 | + "remove-traps-percentile-cutoff-hot", cl::init(0), |
| 30 | + cl::desc("Alternative hot percentile cuttoff. By default " |
| 31 | + "`-profile-summary-cutoff-hot` is used.")); |
| 32 | +static cl::opt<float> RandomRate( |
| 33 | + "remove-traps-random-rate", cl::init(0.0), |
| 34 | + cl::desc( |
| 35 | + "Probability to use for pseudorandom unconditional checks removal.")); |
| 36 | + |
| 37 | +STATISTIC(NumChecksTotal, "Number of checks"); |
| 38 | +STATISTIC(NumChecksRemoved, "Number of removed checks"); |
| 39 | + |
| 40 | +static SmallVector<IntrinsicInst *, 16> |
| 41 | +removeUbsanTraps(Function &F, FunctionAnalysisManager &FAM, |
| 42 | + ProfileSummaryInfo *PSI) { |
| 43 | + SmallVector<IntrinsicInst *, 16> Remove; |
| 44 | + |
| 45 | + if (F.isDeclaration()) |
| 46 | + return {}; |
| 47 | + |
| 48 | + auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F); |
| 49 | + |
| 50 | + int BBCounter = 0; |
| 51 | + for (BasicBlock &BB : F) { |
| 52 | + for (Instruction &I : BB) { |
| 53 | + IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); |
| 54 | + if (!II) |
| 55 | + continue; |
| 56 | + auto ID = II->getIntrinsicID(); |
| 57 | + if (ID != Intrinsic::ubsantrap) |
| 58 | + continue; |
| 59 | + ++NumChecksTotal; |
| 60 | + |
| 61 | + bool IsHot = false; |
| 62 | + if (PSI) { |
| 63 | + uint64_t Count = 0; |
| 64 | + for (const auto *PR : predecessors(&BB)) |
| 65 | + Count += BFI.getBlockProfileCount(PR).value_or(0); |
| 66 | + |
| 67 | + IsHot = HotPercentileCutoff.getNumOccurrences() |
| 68 | + ? PSI->isHotCountNthPercentile(HotPercentileCutoff, Count) |
| 69 | + : PSI->isHotCount(Count); |
| 70 | + } |
| 71 | + |
| 72 | + if ((IsHot) || ((F.getGUID() + BBCounter++) % MaxRandomRate) < |
| 73 | + RandomRate * RandomRate) { |
| 74 | + Remove.push_back(II); |
| 75 | + ++NumChecksRemoved; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return Remove; |
| 80 | +} |
| 81 | + |
| 82 | +PreservedAnalyses RemoveTrapsPass::run(Function &F, |
| 83 | + FunctionAnalysisManager &AM) { |
| 84 | + if (F.isDeclaration()) |
| 85 | + return PreservedAnalyses::all(); |
| 86 | + auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F); |
| 87 | + ProfileSummaryInfo *PSI = |
| 88 | + MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent()); |
| 89 | + |
| 90 | + auto Remove = removeUbsanTraps(F, AM, PSI); |
| 91 | + for (auto *I : Remove) |
| 92 | + I->eraseFromParent(); |
| 93 | + |
| 94 | + return Remove.empty() ? PreservedAnalyses::all() : PreservedAnalyses::none(); |
| 95 | +} |
0 commit comments