1 //===-- GuardUtils.cpp - Utils for work with guards -------------*- C++ -*-===//
2 //
3 // 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
6 //
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/Analysis/GuardUtils.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/MDBuilder.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
20 
21 using namespace llvm;
22 
23 static cl::opt<uint32_t> PredicatePassBranchWeight(
24     "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
25     cl::desc("The probability of a guard failing is assumed to be the "
26              "reciprocal of this value (default = 1 << 20)"));
27 
28 void llvm::makeGuardControlFlowExplicit(Function *DeoptIntrinsic,
29                                         CallInst *Guard) {
30   OperandBundleDef DeoptOB(*Guard->getOperandBundle(LLVMContext::OB_deopt));
31   SmallVector<Value *, 4> Args(std::next(Guard->arg_begin()), Guard->arg_end());
32 
33   auto *CheckBB = Guard->getParent();
34   auto *DeoptBlockTerm =
35       SplitBlockAndInsertIfThen(Guard->getArgOperand(0), Guard, true);
36 
37   auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
38 
39   // SplitBlockAndInsertIfThen inserts control flow that branches to
40   // DeoptBlockTerm if the condition is true.  We want the opposite.
41   CheckBI->swapSuccessors();
42 
43   CheckBI->getSuccessor(0)->setName("guarded");
44   CheckBI->getSuccessor(1)->setName("deopt");
45 
46   if (auto *MD = Guard->getMetadata(LLVMContext::MD_make_implicit))
47     CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
48 
49   MDBuilder MDB(Guard->getContext());
50   CheckBI->setMetadata(LLVMContext::MD_prof,
51                        MDB.createBranchWeights(PredicatePassBranchWeight, 1));
52 
53   IRBuilder<> B(DeoptBlockTerm);
54   auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
55 
56   if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
57     B.CreateRetVoid();
58   } else {
59     DeoptCall->setName("deoptcall");
60     B.CreateRet(DeoptCall);
61   }
62 
63   DeoptCall->setCallingConv(Guard->getCallingConv());
64   DeoptBlockTerm->eraseFromParent();
65 }
66 
67 
68 void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {
69   assert(isWidenableBranch(WidenableBR) && "precondition");
70 
71   Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
72   // Condition is only guaranteed to dominate branch
73   WCAnd->moveBefore(WidenableBR);
74   Value *OldCond = WCAnd->getOperand(0);
75   IRBuilder<> B(WCAnd);
76   WCAnd->setOperand(0, B.CreateAnd(NewCond, OldCond));
77 
78   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
79 }
80 
81 void llvm::setWidenableBranchCond(BranchInst *WidenableBR, Value *NewCond) {
82   assert(isWidenableBranch(WidenableBR) && "precondition");
83 
84   Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
85   // Condition is only guaranteed to dominate branch
86   WCAnd->moveBefore(WidenableBR);
87   WCAnd->setOperand(0, NewCond);
88 
89   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
90 }
91