1 //===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass lowers the @llvm.experimental.guard intrinsic to the new form of
11 // guard represented as widenable explicit branch to the deopt block. The
12 // difference between this pass and LowerGuardIntrinsic is that after this pass
13 // the guard represented as intrinsic:
15 // call void(i1, ...) @llvm.experimental.guard(i1 %old_cond) [ "deopt"() ]
17 // transforms to a guard represented as widenable explicit branch:
19 // %widenable_cond = call i1 @llvm.experimental.widenable.condition()
20 // br i1 (%old_cond & %widenable_cond), label %guarded, label %deopt
23 // - The semantics of @llvm.experimental.widenable.condition allows to replace
24 // %widenable_cond with the construction (%widenable_cond & %any_other_cond)
25 // without loss of correctness;
26 // - %guarded is the lower part of old guard intrinsic's parent block split by
27 // the intrinsic call;
28 // - %deopt is a block containing a sole call to @llvm.experimental.deoptimize
31 // Therefore, this branch preserves the property of widenability.
33 //===----------------------------------------------------------------------===//
35 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
36 #include "llvm/Analysis/GuardUtils.h"
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/IRBuilder.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 *BB = Guard->getParent();
61 makeGuardControlFlowExplicit(DeoptIntrinsic, Guard);
62 BranchInst *ExplicitGuard = cast<BranchInst>(BB->getTerminator());
63 assert(ExplicitGuard->isConditional() && "Must be!");
65 // We want the guard to be expressed as explicit control flow, but still be
66 // widenable. For that, we add Widenable Condition intrinsic call to the
68 IRBuilder<> B(ExplicitGuard);
69 auto *WidenableCondition =
70 B.CreateIntrinsic(Intrinsic::experimental_widenable_condition,
71 {}, {}, nullptr, "widenable_cond");
72 WidenableCondition->setCallingConv(Guard->getCallingConv());
74 B.CreateAnd(ExplicitGuard->getCondition(), WidenableCondition);
75 NewCond->setName("exiplicit_guard_cond");
76 ExplicitGuard->setCondition(NewCond);
77 Guard->eraseFromParent();
80 static bool explicifyGuards(Function &F) {
81 // Check if we can cheaply rule out the possibility of not having any work to
83 auto *GuardDecl = F.getParent()->getFunction(
84 Intrinsic::getName(Intrinsic::experimental_guard));
85 if (!GuardDecl || GuardDecl->use_empty())
88 SmallVector<CallInst *, 8> GuardIntrinsics;
89 for (auto &I : instructions(F))
91 GuardIntrinsics.push_back(cast<CallInst>(&I));
93 if (GuardIntrinsics.empty())
96 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
97 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
98 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
100 for (auto *Guard : GuardIntrinsics)
101 turnToExplicitForm(Guard, DeoptIntrinsic);
106 bool MakeGuardsExplicitLegacyPass::runOnFunction(Function &F) {
107 return explicifyGuards(F);
110 char MakeGuardsExplicitLegacyPass::ID = 0;
111 INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
112 "Lower the guard intrinsic to explicit control flow form",
115 PreservedAnalyses MakeGuardsExplicitPass::run(Function &F,
116 FunctionAnalysisManager &) {
117 if (explicifyGuards(F))
118 return PreservedAnalyses::none();
119 return PreservedAnalyses::all();