1 //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass lowers the 'expect' intrinsic to LLVM metadata.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/MDBuilder.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
31 #define DEBUG_TYPE "lower-expect-intrinsic"
33 STATISTIC(IfHandled, "Number of 'expect' intrinsic instructions handled");
35 static cl::opt<uint32_t>
36 LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
37 cl::desc("Weight of the branch likely to be taken (default = 64)"));
38 static cl::opt<uint32_t>
39 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
40 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
43 /// \brief Legacy pass for lowering expect intrinsics out of the IR.
45 /// When this pass is run over a function it uses expect intrinsics which feed
46 /// branches and switches to provide branch weight metadata for those
47 /// terminators. It then removes the expect intrinsics from the IR so the rest
48 /// of the optimizer can ignore them.
49 class LowerExpectIntrinsic : public FunctionPass {
51 bool HandleSwitchExpect(SwitchInst *SI);
53 bool HandleIfExpect(BranchInst *BI);
57 LowerExpectIntrinsic() : FunctionPass(ID) {
58 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
61 bool runOnFunction(Function &F) override;
65 bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
66 CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
70 Function *Fn = CI->getCalledFunction();
71 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
74 Value *ArgValue = CI->getArgOperand(0);
75 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
79 SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
80 unsigned n = SI->getNumCases(); // +1 for default case.
81 std::vector<uint32_t> Weights(n + 1);
84 Case == SI->case_default() ? LikelyBranchWeight : UnlikelyBranchWeight;
85 for (unsigned i = 0; i != n; ++i)
87 i == Case.getCaseIndex() ? LikelyBranchWeight : UnlikelyBranchWeight;
89 SI->setMetadata(LLVMContext::MD_prof,
90 MDBuilder(CI->getContext()).createBranchWeights(Weights));
92 SI->setCondition(ArgValue);
96 bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
97 if (BI->isUnconditional())
100 // Handle non-optimized IR code like:
101 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
102 // %tobool = icmp ne i64 %expval, 0
103 // br i1 %tobool, label %if.then, label %if.end
105 // Or the following simpler case:
106 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
107 // br i1 %expval, label %if.then, label %if.end
111 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
113 CI = dyn_cast<CallInst>(BI->getCondition());
115 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
117 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
123 Function *Fn = CI->getCalledFunction();
124 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
127 Value *ArgValue = CI->getArgOperand(0);
128 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
132 MDBuilder MDB(CI->getContext());
135 // If expect value is equal to 1 it means that we are more likely to take
136 // branch 0, in other case more likely is branch 1.
137 if (ExpectedValue->isOne())
138 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
140 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
142 BI->setMetadata(LLVMContext::MD_prof, Node);
145 CmpI->setOperand(0, ArgValue);
147 BI->setCondition(ArgValue);
151 bool LowerExpectIntrinsic::runOnFunction(Function &F) {
152 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
153 BasicBlock *BB = I++;
155 // Create "block_weights" metadata.
156 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
157 if (HandleIfExpect(BI))
159 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
160 if (HandleSwitchExpect(SI))
164 // remove llvm.expect intrinsics.
165 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
166 CallInst *CI = dyn_cast<CallInst>(BI++);
170 Function *Fn = CI->getCalledFunction();
171 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
172 Value *Exp = CI->getArgOperand(0);
173 CI->replaceAllUsesWith(Exp);
174 CI->eraseFromParent();
182 char LowerExpectIntrinsic::ID = 0;
183 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
184 "Lower 'expect' Intrinsics", false, false)
186 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
187 return new LowerExpectIntrinsic();