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 #define DEBUG_TYPE "lower-expect-intrinsic"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/MDBuilder.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
32 STATISTIC(IfHandled, "Number of 'expect' intrinsic intructions handled");
34 static cl::opt<uint32_t>
35 LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
36 cl::desc("Weight of the branch likely to be taken (default = 64)"));
37 static cl::opt<uint32_t>
38 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
39 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
43 class LowerExpectIntrinsic : public FunctionPass {
45 bool HandleSwitchExpect(SwitchInst *SI);
47 bool HandleIfExpect(BranchInst *BI);
51 LowerExpectIntrinsic() : FunctionPass(ID) {
52 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
55 bool runOnFunction(Function &F);
60 bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
61 CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
65 Function *Fn = CI->getCalledFunction();
66 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
69 Value *ArgValue = CI->getArgOperand(0);
70 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
74 SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
75 unsigned n = SI->getNumCases(); // +1 for default case.
76 std::vector<uint32_t> Weights(n + 1);
78 Weights[0] = Case == SI->case_default() ? LikelyBranchWeight
79 : UnlikelyBranchWeight;
80 for (unsigned i = 0; i != n; ++i)
81 Weights[i + 1] = i == Case.getCaseIndex() ? LikelyBranchWeight
82 : UnlikelyBranchWeight;
84 SI->setMetadata(LLVMContext::MD_prof,
85 MDBuilder(CI->getContext()).createBranchWeights(Weights));
87 SI->setCondition(ArgValue);
92 bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
93 if (BI->isUnconditional())
96 // Handle non-optimized IR code like:
97 // %expval = call i64 @llvm.expect.i64.i64(i64 %conv1, i64 1)
98 // %tobool = icmp ne i64 %expval, 0
99 // br i1 %tobool, label %if.then, label %if.end
101 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
102 if (!CmpI || CmpI->getPredicate() != CmpInst::ICMP_NE)
105 CallInst *CI = dyn_cast<CallInst>(CmpI->getOperand(0));
109 Function *Fn = CI->getCalledFunction();
110 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
113 Value *ArgValue = CI->getArgOperand(0);
114 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
118 MDBuilder MDB(CI->getContext());
121 // If expect value is equal to 1 it means that we are more likely to take
122 // branch 0, in other case more likely is branch 1.
123 if (ExpectedValue->isOne())
124 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
126 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
128 BI->setMetadata(LLVMContext::MD_prof, Node);
130 CmpI->setOperand(0, ArgValue);
135 bool LowerExpectIntrinsic::runOnFunction(Function &F) {
136 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
137 BasicBlock *BB = I++;
139 // Create "block_weights" metadata.
140 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
141 if (HandleIfExpect(BI))
143 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
144 if (HandleSwitchExpect(SI))
148 // remove llvm.expect intrinsics.
149 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
151 CallInst *CI = dyn_cast<CallInst>(BI++);
155 Function *Fn = CI->getCalledFunction();
156 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
157 Value *Exp = CI->getArgOperand(0);
158 CI->replaceAllUsesWith(Exp);
159 CI->eraseFromParent();
168 char LowerExpectIntrinsic::ID = 0;
169 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
170 "Intrinsics", false, false)
172 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
173 return new LowerExpectIntrinsic();