blob: 4f413715ffe68f52e34acbe7908ff8cd1ec2973b [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
Michael Kupersteine45d4d92016-07-28 22:08:41 +000016#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sanjoy Das021de052016-03-31 00:18:46 +000017#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"
Sanjoy Dasfd670382016-05-17 17:51:19 +000024#include "llvm/IR/MDBuilder.h"
Sanjoy Das021de052016-03-31 00:18:46 +000025#include "llvm/IR/Module.h"
26#include "llvm/Pass.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +000027#include "llvm/Transforms/Scalar.h"
Sanjoy Das021de052016-03-31 00:18:46 +000028#include "llvm/Transforms/Utils/BasicBlockUtils.h"
29
30using namespace llvm;
31
Sanjoy Das52bbde22016-05-18 23:16:27 +000032static cl::opt<uint32_t> PredicatePassBranchWeight(
33 "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
Sanjoy Dasfd670382016-05-17 17:51:19 +000034 cl::desc("The probability of a guard failing is assumed to be the "
35 "reciprocal of this value (default = 1 << 20)"));
36
Sanjoy Das021de052016-03-31 00:18:46 +000037namespace {
Michael Kupersteine45d4d92016-07-28 22:08:41 +000038struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
Sanjoy Das021de052016-03-31 00:18:46 +000039 static char ID;
Michael Kupersteine45d4d92016-07-28 22:08:41 +000040 LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
41 initializeLowerGuardIntrinsicLegacyPassPass(
42 *PassRegistry::getPassRegistry());
Sanjoy Das021de052016-03-31 00:18:46 +000043 }
44
45 bool runOnFunction(Function &F) override;
46};
47}
48
49static void MakeGuardControlFlowExplicit(Function *DeoptIntrinsic,
50 CallInst *CI) {
51 OperandBundleDef DeoptOB(*CI->getOperandBundle(LLVMContext::OB_deopt));
52 SmallVector<Value *, 4> Args(std::next(CI->arg_begin()), CI->arg_end());
53
54 auto *CheckBB = CI->getParent();
55 auto *DeoptBlockTerm =
56 SplitBlockAndInsertIfThen(CI->getArgOperand(0), CI, true);
57
58 auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
59
60 // SplitBlockAndInsertIfThen inserts control flow that branches to
61 // DeoptBlockTerm if the condition is true. We want the opposite.
62 CheckBI->swapSuccessors();
63
64 CheckBI->getSuccessor(0)->setName("guarded");
65 CheckBI->getSuccessor(1)->setName("deopt");
66
Sanjoy Das47cf2af2016-04-30 00:55:59 +000067 if (auto *MD = CI->getMetadata(LLVMContext::MD_make_implicit))
68 CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
69
Sanjoy Dasfd670382016-05-17 17:51:19 +000070 MDBuilder MDB(CI->getContext());
71 CheckBI->setMetadata(LLVMContext::MD_prof,
Sanjoy Das52bbde22016-05-18 23:16:27 +000072 MDB.createBranchWeights(PredicatePassBranchWeight, 1));
Sanjoy Dasfd670382016-05-17 17:51:19 +000073
Sanjoy Das021de052016-03-31 00:18:46 +000074 IRBuilder<> B(DeoptBlockTerm);
75 auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
76
77 if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
78 B.CreateRetVoid();
79 } else {
80 DeoptCall->setName("deoptcall");
81 B.CreateRet(DeoptCall);
82 }
83
Sanjoy Das52c68bb2016-04-30 00:17:47 +000084 DeoptCall->setCallingConv(CI->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +000085 DeoptBlockTerm->eraseFromParent();
86}
87
Michael Kupersteine45d4d92016-07-28 22:08:41 +000088static bool lowerGuardIntrinsic(Function &F) {
Sanjoy Das021de052016-03-31 00:18:46 +000089 // Check if we can cheaply rule out the possibility of not having any work to
90 // do.
91 auto *GuardDecl = F.getParent()->getFunction(
92 Intrinsic::getName(Intrinsic::experimental_guard));
93 if (!GuardDecl || GuardDecl->use_empty())
94 return false;
95
96 SmallVector<CallInst *, 8> ToLower;
97 for (auto &I : instructions(F))
98 if (auto *CI = dyn_cast<CallInst>(&I))
99 if (auto *F = CI->getCalledFunction())
100 if (F->getIntrinsicID() == Intrinsic::experimental_guard)
101 ToLower.push_back(CI);
102
103 if (ToLower.empty())
104 return false;
105
106 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
107 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
Sanjoy Das52c68bb2016-04-30 00:17:47 +0000108 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
Sanjoy Das021de052016-03-31 00:18:46 +0000109
110 for (auto *CI : ToLower) {
111 MakeGuardControlFlowExplicit(DeoptIntrinsic, CI);
112 CI->eraseFromParent();
113 }
114
115 return true;
116}
117
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000118bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
119 return lowerGuardIntrinsic(F);
120}
121
122char LowerGuardIntrinsicLegacyPass::ID = 0;
123INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
Sanjoy Das021de052016-03-31 00:18:46 +0000124 "Lower the guard intrinsic to normal control flow", false,
125 false)
126
127Pass *llvm::createLowerGuardIntrinsicPass() {
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000128 return new LowerGuardIntrinsicLegacyPass();
129}
130
131PreservedAnalyses LowerGuardIntrinsicPass::run(Function &F,
132 FunctionAnalysisManager &AM) {
133 if (lowerGuardIntrinsic(F))
134 return PreservedAnalyses::none();
135
136 return PreservedAnalyses::all();
Sanjoy Das021de052016-03-31 00:18:46 +0000137}