1 //===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===//
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 the new form of
10 // guard represented as widenable explicit branch to the deopt block. The
11 // difference between this pass and LowerGuardIntrinsic is that after this pass
12 // the guard represented as intrinsic:
14 // call void(i1, ...) @llvm.experimental.guard(i1 %old_cond) [ "deopt"() ]
16 // transforms to a guard represented as widenable explicit branch:
18 // %widenable_cond = call i1 @llvm.experimental.widenable.condition()
19 // br i1 (%old_cond & %widenable_cond), label %guarded, label %deopt
22 // - The semantics of @llvm.experimental.widenable.condition allows to replace
23 // %widenable_cond with the construction (%widenable_cond & %any_other_cond)
24 // without loss of correctness;
25 // - %guarded is the lower part of old guard intrinsic's parent block split by
26 // the intrinsic call;
27 // - %deopt is a block containing a sole call to @llvm.experimental.deoptimize
30 // Therefore, this branch preserves the property of widenability.
32 //===----------------------------------------------------------------------===//
34 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
35 #include "llvm/Analysis/GuardUtils.h"
36 #include "llvm/IR/IRBuilder.h"
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Transforms/Scalar.h"
43 #include "llvm/Transforms/Utils/GuardUtils.h"
48 struct MakeGuardsExplicitLegacyPass
: public FunctionPass
{
50 MakeGuardsExplicitLegacyPass() : FunctionPass(ID
) {
51 initializeMakeGuardsExplicitLegacyPassPass(*PassRegistry::getPassRegistry());
54 bool runOnFunction(Function
&F
) override
;
58 static void turnToExplicitForm(CallInst
*Guard
, Function
*DeoptIntrinsic
) {
59 // Replace the guard with an explicit branch (just like in GuardWidening).
60 BasicBlock
*OriginalBB
= Guard
->getParent();
62 makeGuardControlFlowExplicit(DeoptIntrinsic
, Guard
, true);
63 assert(isWidenableBranch(OriginalBB
->getTerminator()) && "should hold");
65 Guard
->eraseFromParent();
68 static bool explicifyGuards(Function
&F
) {
69 // Check if we can cheaply rule out the possibility of not having any work to
71 auto *GuardDecl
= F
.getParent()->getFunction(
72 Intrinsic::getName(Intrinsic::experimental_guard
));
73 if (!GuardDecl
|| GuardDecl
->use_empty())
76 SmallVector
<CallInst
*, 8> GuardIntrinsics
;
77 for (auto &I
: instructions(F
))
79 GuardIntrinsics
.push_back(cast
<CallInst
>(&I
));
81 if (GuardIntrinsics
.empty())
84 auto *DeoptIntrinsic
= Intrinsic::getDeclaration(
85 F
.getParent(), Intrinsic::experimental_deoptimize
, {F
.getReturnType()});
86 DeoptIntrinsic
->setCallingConv(GuardDecl
->getCallingConv());
88 for (auto *Guard
: GuardIntrinsics
)
89 turnToExplicitForm(Guard
, DeoptIntrinsic
);
94 bool MakeGuardsExplicitLegacyPass::runOnFunction(Function
&F
) {
95 return explicifyGuards(F
);
98 char MakeGuardsExplicitLegacyPass::ID
= 0;
99 INITIALIZE_PASS(MakeGuardsExplicitLegacyPass
, "make-guards-explicit",
100 "Lower the guard intrinsic to explicit control flow form",
103 PreservedAnalyses
MakeGuardsExplicitPass::run(Function
&F
,
104 FunctionAnalysisManager
&) {
105 if (explicifyGuards(F
))
106 return PreservedAnalyses::none();
107 return PreservedAnalyses::all();