LLVM  9.0.0svn
GuardUtils.cpp
Go to the documentation of this file.
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 
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/MDBuilder.h"
18 
19 using namespace llvm;
20 
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 
27  CallInst *Guard) {
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 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void makeGuardControlFlowExplicit(Function *DeoptIntrinsic, CallInst *Guard)
Splits control flow at point of Guard, replacing it with explicit branch by the condition of guard&#39;s ...
Definition: GuardUtils.cpp:26
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
This class represents a function call, abstracting a target machine&#39;s calling convention.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:705
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1126
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1155
ReturnInst * CreateRet(Value *V)
Create a &#39;ret <val>&#39; instruction.
Definition: IRBuilder.h:828
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:285
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:234
Optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:1716
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:140
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:422
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
static cl::opt< uint32_t > PredicatePassBranchWeight("guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1<< 20), cl::desc("The probability of a guard failing is assumed to be the " "reciprocal of this value (default = 1 << 20)"))
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1120
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:823
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1258
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2009
A container for an operand bundle being viewed as a set of values rather than a set of uses...
Definition: InstrTypes.h:967
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
const BasicBlock * getParent() const
Definition: Instruction.h:66