blob: 7ba17069f426b42a7914cfe6daac579f2aa485ad [file] [log] [blame]
Sanjoy Das021de052016-03-31 00:18:46 +00001//===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
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 a conditional call
11// to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
12// be widened.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Transforms/Scalar.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/IR/BasicBlock.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/InstIterator.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/Module.h"
25#include "llvm/Pass.h"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
27
28using namespace llvm;
29
30namespace {
31struct LowerGuardIntrinsic : public FunctionPass {
32 static char ID;
33 LowerGuardIntrinsic() : FunctionPass(ID) {
34 initializeLowerGuardIntrinsicPass(*PassRegistry::getPassRegistry());
35 }
36
37 bool runOnFunction(Function &F) override;
38};
39}
40
41static void MakeGuardControlFlowExplicit(Function *DeoptIntrinsic,
42 CallInst *CI) {
43 OperandBundleDef DeoptOB(*CI->getOperandBundle(LLVMContext::OB_deopt));
44 SmallVector<Value *, 4> Args(std::next(CI->arg_begin()), CI->arg_end());
45
46 auto *CheckBB = CI->getParent();
47 auto *DeoptBlockTerm =
48 SplitBlockAndInsertIfThen(CI->getArgOperand(0), CI, true);
49
50 auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
51
52 // SplitBlockAndInsertIfThen inserts control flow that branches to
53 // DeoptBlockTerm if the condition is true. We want the opposite.
54 CheckBI->swapSuccessors();
55
56 CheckBI->getSuccessor(0)->setName("guarded");
57 CheckBI->getSuccessor(1)->setName("deopt");
58
Sanjoy Das47cf2af2016-04-30 00:55:59 +000059 if (auto *MD = CI->getMetadata(LLVMContext::MD_make_implicit))
60 CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
61
Sanjoy Das021de052016-03-31 00:18:46 +000062 IRBuilder<> B(DeoptBlockTerm);
63 auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
64
65 if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
66 B.CreateRetVoid();
67 } else {
68 DeoptCall->setName("deoptcall");
69 B.CreateRet(DeoptCall);
70 }
71
Sanjoy Das52c68bb2016-04-30 00:17:47 +000072 DeoptCall->setCallingConv(CI->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +000073 DeoptBlockTerm->eraseFromParent();
74}
75
76bool LowerGuardIntrinsic::runOnFunction(Function &F) {
77 // Check if we can cheaply rule out the possibility of not having any work to
78 // do.
79 auto *GuardDecl = F.getParent()->getFunction(
80 Intrinsic::getName(Intrinsic::experimental_guard));
81 if (!GuardDecl || GuardDecl->use_empty())
82 return false;
83
84 SmallVector<CallInst *, 8> ToLower;
85 for (auto &I : instructions(F))
86 if (auto *CI = dyn_cast<CallInst>(&I))
87 if (auto *F = CI->getCalledFunction())
88 if (F->getIntrinsicID() == Intrinsic::experimental_guard)
89 ToLower.push_back(CI);
90
91 if (ToLower.empty())
92 return false;
93
94 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
95 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
Sanjoy Das52c68bb2016-04-30 00:17:47 +000096 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +000097
98 for (auto *CI : ToLower) {
99 MakeGuardControlFlowExplicit(DeoptIntrinsic, CI);
100 CI->eraseFromParent();
101 }
102
103 return true;
104}
105
106char LowerGuardIntrinsic::ID = 0;
107INITIALIZE_PASS(LowerGuardIntrinsic, "lower-guard-intrinsic",
108 "Lower the guard intrinsic to normal control flow", false,
109 false)
110
111Pass *llvm::createLowerGuardIntrinsicPass() {
112 return new LowerGuardIntrinsic();
113}