1 //===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
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 // This pass lowers the llvm.experimental.guard intrinsic to a conditional call
10 // to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
11 // be widened.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Analysis/GuardUtils.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/Utils/GuardUtils.h"
27
28 using namespace llvm;
29
30 namespace {
31 struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
32 static char ID;
LowerGuardIntrinsicLegacyPass__anon1450dbc00111::LowerGuardIntrinsicLegacyPass33 LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
34 initializeLowerGuardIntrinsicLegacyPassPass(
35 *PassRegistry::getPassRegistry());
36 }
37
38 bool runOnFunction(Function &F) override;
39 };
40 }
41
lowerGuardIntrinsic(Function & F)42 static bool lowerGuardIntrinsic(Function &F) {
43 // Check if we can cheaply rule out the possibility of not having any work to
44 // do.
45 auto *GuardDecl = F.getParent()->getFunction(
46 Intrinsic::getName(Intrinsic::experimental_guard));
47 if (!GuardDecl || GuardDecl->use_empty())
48 return false;
49
50 SmallVector<CallInst *, 8> ToLower;
51 // Traverse through the users of GuardDecl.
52 // This is presumably cheaper than traversing all instructions in the
53 // function.
54 for (auto *U : GuardDecl->users())
55 if (auto *CI = dyn_cast<CallInst>(U))
56 if (CI->getFunction() == &F)
57 ToLower.push_back(CI);
58
59 if (ToLower.empty())
60 return false;
61
62 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
63 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
64 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
65
66 for (auto *CI : ToLower) {
67 makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
68 CI->eraseFromParent();
69 }
70
71 return true;
72 }
73
runOnFunction(Function & F)74 bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
75 return lowerGuardIntrinsic(F);
76 }
77
78 char LowerGuardIntrinsicLegacyPass::ID = 0;
79 INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
80 "Lower the guard intrinsic to normal control flow", false,
81 false)
82
createLowerGuardIntrinsicPass()83 Pass *llvm::createLowerGuardIntrinsicPass() {
84 return new LowerGuardIntrinsicLegacyPass();
85 }
86
run(Function & F,FunctionAnalysisManager & AM)87 PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
88 FunctionAnalysisManager &AM) {
89 if (lowerGuardIntrinsic(F))
90 return PreservedAnalyses::none();
91
92 return PreservedAnalyses::all();
93 }
94