blob: 1ba3994eba0ed57aac65aa2a1d98357f9cf257d1 [file] [log] [blame]
Max Kazantsevb9e65cb2018-12-07 14:39:46 +00001//===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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:
14//
15// call void(i1, ...) @llvm.experimental.guard(i1 %old_cond) [ "deopt"() ]
16//
17// transforms to a guard represented as widenable explicit branch:
18//
19// %widenable_cond = call i1 @llvm.experimental.widenable.condition()
20// br i1 (%old_cond & %widenable_cond), label %guarded, label %deopt
21//
22// Here:
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
29// intrinsic.
30//
31// Therefore, this branch preserves the property of widenability.
32//
33//===----------------------------------------------------------------------===//
34
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"
44
45using namespace llvm;
46
47namespace {
48struct MakeGuardsExplicitLegacyPass : public FunctionPass {
49 static char ID;
50 MakeGuardsExplicitLegacyPass() : FunctionPass(ID) {
51 initializeMakeGuardsExplicitLegacyPassPass(*PassRegistry::getPassRegistry());
52 }
53
54 bool runOnFunction(Function &F) override;
55};
56}
57
58static 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!");
64
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
67 // guard's condition.
68 IRBuilder<> B(ExplicitGuard);
69 auto *WidenableCondition =
70 B.CreateIntrinsic(Intrinsic::experimental_widenable_condition,
71 {}, {}, nullptr, "widenable_cond");
72 WidenableCondition->setCallingConv(Guard->getCallingConv());
73 auto *NewCond =
74 B.CreateAnd(ExplicitGuard->getCondition(), WidenableCondition);
75 NewCond->setName("exiplicit_guard_cond");
76 ExplicitGuard->setCondition(NewCond);
77 Guard->eraseFromParent();
78}
79
80static bool explicifyGuards(Function &F) {
81 // Check if we can cheaply rule out the possibility of not having any work to
82 // do.
83 auto *GuardDecl = F.getParent()->getFunction(
84 Intrinsic::getName(Intrinsic::experimental_guard));
85 if (!GuardDecl || GuardDecl->use_empty())
86 return false;
87
88 SmallVector<CallInst *, 8> GuardIntrinsics;
89 for (auto &I : instructions(F))
90 if (isGuard(&I))
91 GuardIntrinsics.push_back(cast<CallInst>(&I));
92
93 if (GuardIntrinsics.empty())
94 return false;
95
96 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
97 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
98 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
99
100 for (auto *Guard : GuardIntrinsics)
101 turnToExplicitForm(Guard, DeoptIntrinsic);
102
103 return true;
104}
105
106bool MakeGuardsExplicitLegacyPass::runOnFunction(Function &F) {
107 return explicifyGuards(F);
108}
109
110char MakeGuardsExplicitLegacyPass::ID = 0;
111INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
112 "Lower the guard intrinsic to explicit control flow form",
113 false, false)
114
115PreservedAnalyses MakeGuardsExplicitPass::run(Function &F,
116 FunctionAnalysisManager &) {
117 if (explicifyGuards(F))
118 return PreservedAnalyses::none();
119 return PreservedAnalyses::all();
120}