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, bool UseWC) {
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   if (UseWC) {
67     // We want the guard to be expressed as explicit control flow, but still be
68     // widenable. For that, we add Widenable Condition intrinsic call to the
69     // guard's condition.
70     IRBuilder<> B(CheckBI);
71     auto *WC = B.CreateIntrinsic(Intrinsic::experimental_widenable_condition,
72                                  {}, {}, nullptr, "widenable_cond");
73     CheckBI->setCondition(B.CreateAnd(CheckBI->getCondition(), WC,
74                                       "exiplicit_guard_cond"));
75     assert(isWidenableBranch(CheckBI) && "sanity check");
76   }
77 }
78 
79 
80 void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {
81   assert(isWidenableBranch(WidenableBR) && "precondition");
82 
83   Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
84   // Condition is only guaranteed to dominate branch
85   WCAnd->moveBefore(WidenableBR);
86   Value *OldCond = WCAnd->getOperand(0);
87   IRBuilder<> B(WCAnd);
88   WCAnd->setOperand(0, B.CreateAnd(NewCond, OldCond));
89 
90   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
91 }
92 
93 void llvm::setWidenableBranchCond(BranchInst *WidenableBR, Value *NewCond) {
94   assert(isWidenableBranch(WidenableBR) && "precondition");
95 
96   Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
97   // Condition is only guaranteed to dominate branch
98   WCAnd->moveBefore(WidenableBR);
99   WCAnd->setOperand(0, NewCond);
100 
101   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
102 }
103