LLVM  8.0.1
GuardUtils.cpp
Go to the documentation of this file.
1 //===-- GuardUtils.cpp - Utils for work with guards -------------*- C++ -*-===//
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 // Utils that are used to perform transformations related to guards and their
10 // conditions.
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/MDBuilder.h"
19 
20 using namespace llvm;
21 
23  "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
24  cl::desc("The probability of a guard failing is assumed to be the "
25  "reciprocal of this value (default = 1 << 20)"));
26 
28  CallInst *Guard) {
30  SmallVector<Value *, 4> Args(std::next(Guard->arg_begin()), Guard->arg_end());
31 
32  auto *CheckBB = Guard->getParent();
33  auto *DeoptBlockTerm =
34  SplitBlockAndInsertIfThen(Guard->getArgOperand(0), Guard, true);
35 
36  auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
37 
38  // SplitBlockAndInsertIfThen inserts control flow that branches to
39  // DeoptBlockTerm if the condition is true. We want the opposite.
40  CheckBI->swapSuccessors();
41 
42  CheckBI->getSuccessor(0)->setName("guarded");
43  CheckBI->getSuccessor(1)->setName("deopt");
44 
45  if (auto *MD = Guard->getMetadata(LLVMContext::MD_make_implicit))
46  CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
47 
48  MDBuilder MDB(Guard->getContext());
49  CheckBI->setMetadata(LLVMContext::MD_prof,
50  MDB.createBranchWeights(PredicatePassBranchWeight, 1));
51 
52  IRBuilder<> B(DeoptBlockTerm);
53  auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
54 
55  if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
56  B.CreateRetVoid();
57  } else {
58  DeoptCall->setName("deoptcall");
59  B.CreateRet(DeoptCall);
60  }
61 
62  DeoptCall->setCallingConv(Guard->getCallingConv());
63  DeoptBlockTerm->eraseFromParent();
64 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:27
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:1106
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1135
ReturnInst * CreateRet(Value *V)
Create a &#39;ret <val>&#39; instruction.
Definition: IRBuilder.h:829
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
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:221
Optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:1680
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:141
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:169
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:847
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)"))
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1100
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:824
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1225
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1974
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.
const BasicBlock * getParent() const
Definition: Instruction.h:67