xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/GuardUtils.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
10b57cec5SDimitry Andric //===-- GuardUtils.cpp - Utils for work with guards -------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric // Utils that are used to perform transformations related to guards and their
90b57cec5SDimitry Andric // conditions.
100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "llvm/Transforms/Utils/GuardUtils.h"
13480093f4SDimitry Andric #include "llvm/Analysis/GuardUtils.h"
140b57cec5SDimitry Andric #include "llvm/IR/Function.h"
150b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
16480093f4SDimitry Andric #include "llvm/IR/Instructions.h"
170b57cec5SDimitry Andric #include "llvm/IR/MDBuilder.h"
18480093f4SDimitry Andric #include "llvm/IR/PatternMatch.h"
19480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
23480093f4SDimitry Andric using namespace llvm::PatternMatch;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric static cl::opt<uint32_t> PredicatePassBranchWeight(
260b57cec5SDimitry Andric     "guards-predicate-pass-branch-weight", cl::Hidden, cl::init(1 << 20),
270b57cec5SDimitry Andric     cl::desc("The probability of a guard failing is assumed to be the "
280b57cec5SDimitry Andric              "reciprocal of this value (default = 1 << 20)"));
290b57cec5SDimitry Andric 
makeGuardControlFlowExplicit(Function * DeoptIntrinsic,CallInst * Guard,bool UseWC)300b57cec5SDimitry Andric void llvm::makeGuardControlFlowExplicit(Function *DeoptIntrinsic,
31480093f4SDimitry Andric                                         CallInst *Guard, bool UseWC) {
320b57cec5SDimitry Andric   OperandBundleDef DeoptOB(*Guard->getOperandBundle(LLVMContext::OB_deopt));
33e8d8bef9SDimitry Andric   SmallVector<Value *, 4> Args(drop_begin(Guard->args()));
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   auto *CheckBB = Guard->getParent();
360b57cec5SDimitry Andric   auto *DeoptBlockTerm =
370b57cec5SDimitry Andric       SplitBlockAndInsertIfThen(Guard->getArgOperand(0), Guard, true);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   auto *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // SplitBlockAndInsertIfThen inserts control flow that branches to
420b57cec5SDimitry Andric   // DeoptBlockTerm if the condition is true.  We want the opposite.
430b57cec5SDimitry Andric   CheckBI->swapSuccessors();
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   CheckBI->getSuccessor(0)->setName("guarded");
460b57cec5SDimitry Andric   CheckBI->getSuccessor(1)->setName("deopt");
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   if (auto *MD = Guard->getMetadata(LLVMContext::MD_make_implicit))
490b57cec5SDimitry Andric     CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   MDBuilder MDB(Guard->getContext());
520b57cec5SDimitry Andric   CheckBI->setMetadata(LLVMContext::MD_prof,
530b57cec5SDimitry Andric                        MDB.createBranchWeights(PredicatePassBranchWeight, 1));
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   IRBuilder<> B(DeoptBlockTerm);
560b57cec5SDimitry Andric   auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   if (DeoptIntrinsic->getReturnType()->isVoidTy()) {
590b57cec5SDimitry Andric     B.CreateRetVoid();
600b57cec5SDimitry Andric   } else {
610b57cec5SDimitry Andric     DeoptCall->setName("deoptcall");
620b57cec5SDimitry Andric     B.CreateRet(DeoptCall);
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   DeoptCall->setCallingConv(Guard->getCallingConv());
660b57cec5SDimitry Andric   DeoptBlockTerm->eraseFromParent();
67480093f4SDimitry Andric 
68480093f4SDimitry Andric   if (UseWC) {
69480093f4SDimitry Andric     // We want the guard to be expressed as explicit control flow, but still be
70480093f4SDimitry Andric     // widenable. For that, we add Widenable Condition intrinsic call to the
71480093f4SDimitry Andric     // guard's condition.
72480093f4SDimitry Andric     IRBuilder<> B(CheckBI);
73480093f4SDimitry Andric     auto *WC = B.CreateIntrinsic(Intrinsic::experimental_widenable_condition,
74480093f4SDimitry Andric                                  {}, {}, nullptr, "widenable_cond");
75480093f4SDimitry Andric     CheckBI->setCondition(B.CreateAnd(CheckBI->getCondition(), WC,
76480093f4SDimitry Andric                                       "exiplicit_guard_cond"));
77*4824e7fdSDimitry Andric     assert(isWidenableBranch(CheckBI) && "Branch must be widenable.");
78480093f4SDimitry Andric   }
79480093f4SDimitry Andric }
80480093f4SDimitry Andric 
81480093f4SDimitry Andric 
widenWidenableBranch(BranchInst * WidenableBR,Value * NewCond)82480093f4SDimitry Andric void llvm::widenWidenableBranch(BranchInst *WidenableBR, Value *NewCond) {
83480093f4SDimitry Andric   assert(isWidenableBranch(WidenableBR) && "precondition");
84480093f4SDimitry Andric 
85480093f4SDimitry Andric   // The tempting trivially option is to produce something like this:
86480093f4SDimitry Andric   // br (and oldcond, newcond) where oldcond is assumed to contain a widenable
87480093f4SDimitry Andric   // condition, but that doesn't match the pattern parseWidenableBranch expects
88480093f4SDimitry Andric   // so we have to be more sophisticated.
89480093f4SDimitry Andric 
90480093f4SDimitry Andric   Use *C, *WC;
91480093f4SDimitry Andric   BasicBlock *IfTrueBB, *IfFalseBB;
92480093f4SDimitry Andric   parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);
93480093f4SDimitry Andric   if (!C) {
94480093f4SDimitry Andric     // br (wc()), ... form
95480093f4SDimitry Andric     IRBuilder<> B(WidenableBR);
96480093f4SDimitry Andric     WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));
97480093f4SDimitry Andric   } else {
98480093f4SDimitry Andric     // br (wc & C), ... form
99480093f4SDimitry Andric     IRBuilder<> B(WidenableBR);
100480093f4SDimitry Andric     C->set(B.CreateAnd(NewCond, C->get()));
101480093f4SDimitry Andric     Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
102480093f4SDimitry Andric     // Condition is only guaranteed to dominate branch
103480093f4SDimitry Andric     WCAnd->moveBefore(WidenableBR);
104480093f4SDimitry Andric   }
105480093f4SDimitry Andric   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
106480093f4SDimitry Andric }
107480093f4SDimitry Andric 
setWidenableBranchCond(BranchInst * WidenableBR,Value * NewCond)108480093f4SDimitry Andric void llvm::setWidenableBranchCond(BranchInst *WidenableBR, Value *NewCond) {
109480093f4SDimitry Andric   assert(isWidenableBranch(WidenableBR) && "precondition");
110480093f4SDimitry Andric 
111480093f4SDimitry Andric   Use *C, *WC;
112480093f4SDimitry Andric   BasicBlock *IfTrueBB, *IfFalseBB;
113480093f4SDimitry Andric   parseWidenableBranch(WidenableBR, C, WC, IfTrueBB, IfFalseBB);
114480093f4SDimitry Andric   if (!C) {
115480093f4SDimitry Andric     // br (wc()), ... form
116480093f4SDimitry Andric     IRBuilder<> B(WidenableBR);
117480093f4SDimitry Andric     WidenableBR->setCondition(B.CreateAnd(NewCond, WC->get()));
118480093f4SDimitry Andric   } else {
119480093f4SDimitry Andric     // br (wc & C), ... form
120480093f4SDimitry Andric     Instruction *WCAnd = cast<Instruction>(WidenableBR->getCondition());
121480093f4SDimitry Andric     // Condition is only guaranteed to dominate branch
122480093f4SDimitry Andric     WCAnd->moveBefore(WidenableBR);
123480093f4SDimitry Andric     C->set(NewCond);
124480093f4SDimitry Andric   }
125480093f4SDimitry Andric   assert(isWidenableBranch(WidenableBR) && "preserve widenabiliy");
1260b57cec5SDimitry Andric }
127