blob: 34c32d9c0c98803af166c704f73ff229ed586e14 [file] [log] [blame]
Max Kazantsev8b4ffe62018-08-29 10:51:59 +00001//===-- GuardUtils.cpp - Utils for work with guards -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Max Kazantsev8b4ffe62018-08-29 10:51:59 +00006//
7//===----------------------------------------------------------------------===//
8// Utils that are used to perform transformations related to guards and their
9// conditions.
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Transforms/Utils/GuardUtils.h"
13#include "llvm/IR/Function.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/MDBuilder.h"
17#include "llvm/Transforms/Utils/BasicBlockUtils.h"
18
19using namespace llvm;
20
21static cl::opt<uint32_t> PredicatePassBranchWeight(
22 "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
23 cl::desc("The probability of a guard failing is assumed to be the "
24 "reciprocal of this value (default = 1 << 20)"));
25
Max Kazantsev8b4ffe62018-08-29 10:51:59 +000026void llvm::makeGuardControlFlowExplicit(Function *DeoptIntrinsic,
27 CallInst *Guard) {
28 OperandBundleDef DeoptOB(*Guard->getOperandBundle(LLVMContext::OB_deopt));
29 SmallVector<Value *, 4> Args(std::next(Guard->arg_begin()), Guard->arg_end());
30
31 auto *CheckBB = Guard->getParent();
32 auto *DeoptBlockTerm =
33 SplitBlockAndInsertIfThen(Guard->getArgOperand(0), Guard, true);
34
35 auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
36
37 // SplitBlockAndInsertIfThen inserts control flow that branches to
38 // DeoptBlockTerm if the condition is true. We want the opposite.
39 CheckBI->swapSuccessors();
40
41 CheckBI->getSuccessor(0)->setName("guarded");
42 CheckBI->getSuccessor(1)->setName("deopt");
43
44 if (auto *MD = Guard->getMetadata(LLVMContext::MD_make_implicit))
45 CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
46
47 MDBuilder MDB(Guard->getContext());
48 CheckBI->setMetadata(LLVMContext::MD_prof,
49 MDB.createBranchWeights(PredicatePassBranchWeight, 1));
50
51 IRBuilder<> B(DeoptBlockTerm);
52 auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
53
54 if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
55 B.CreateRetVoid();
56 } else {
57 DeoptCall->setName("deoptcall");
58 B.CreateRet(DeoptCall);
59 }
60
61 DeoptCall->setCallingConv(Guard->getCallingConv());
62 DeoptBlockTerm->eraseFromParent();
63}