1 //===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
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/Transforms/Utils/GuardUtils.h"
27 static bool lowerGuardIntrinsic(Function
&F
) {
28 // Check if we can cheaply rule out the possibility of not having any work to
30 auto *GuardDecl
= F
.getParent()->getFunction(
31 Intrinsic::getName(Intrinsic::experimental_guard
));
32 if (!GuardDecl
|| GuardDecl
->use_empty())
35 SmallVector
<CallInst
*, 8> ToLower
;
36 // Traverse through the users of GuardDecl.
37 // This is presumably cheaper than traversing all instructions in the
39 for (auto *U
: GuardDecl
->users())
40 if (auto *CI
= dyn_cast
<CallInst
>(U
))
41 if (CI
->getFunction() == &F
)
42 ToLower
.push_back(CI
);
47 auto *DeoptIntrinsic
= Intrinsic::getDeclaration(
48 F
.getParent(), Intrinsic::experimental_deoptimize
, {F
.getReturnType()});
49 DeoptIntrinsic
->setCallingConv(GuardDecl
->getCallingConv());
51 for (auto *CI
: ToLower
) {
52 makeGuardControlFlowExplicit(DeoptIntrinsic
, CI
, false);
53 CI
->eraseFromParent();
59 PreservedAnalyses
LowerGuardIntrinsicPass::run(Function
&F
,
60 FunctionAnalysisManager
&AM
) {
61 if (lowerGuardIntrinsic(F
))
62 return PreservedAnalyses::none();
64 return PreservedAnalyses::all();