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/SmallVector.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"
31 #define DEBUG_TYPE "lower-expect-intrinsic"
33 STATISTIC(ExpectIntrinsicsHandled,
34 "Number of 'expect' intrinsic instructions handled");
36 static cl::opt<uint32_t>
37 LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
38 cl::desc("Weight of the branch likely to be taken (default = 64)"));
39 static cl::opt<uint32_t>
40 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
41 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
44 /// \brief Legacy pass for lowering expect intrinsics out of the IR.
46 /// When this pass is run over a function it uses expect intrinsics which feed
47 /// branches and switches to provide branch weight metadata for those
48 /// terminators. It then removes the expect intrinsics from the IR so the rest
49 /// of the optimizer can ignore them.
50 class LowerExpectIntrinsic : public FunctionPass {
53 LowerExpectIntrinsic() : FunctionPass(ID) {
54 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
57 bool runOnFunction(Function &F) override;
61 static bool handleSwitchExpect(SwitchInst &SI) {
62 CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
66 Function *Fn = CI->getCalledFunction();
67 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
70 Value *ArgValue = CI->getArgOperand(0);
71 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
75 SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
76 unsigned n = SI.getNumCases(); // +1 for default case.
77 SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
79 if (Case == SI.case_default())
80 Weights[0] = LikelyBranchWeight;
82 Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
84 SI.setMetadata(LLVMContext::MD_prof,
85 MDBuilder(CI->getContext()).createBranchWeights(Weights));
87 SI.setCondition(ArgValue);
91 static bool handleBranchExpect(BranchInst &BI) {
92 if (BI.isUnconditional())
95 // Handle non-optimized IR code like:
96 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
97 // %tobool = icmp ne i64 %expval, 0
98 // br i1 %tobool, label %if.then, label %if.end
100 // Or the following simpler case:
101 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
102 // br i1 %expval, label %if.then, label %if.end
106 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition());
108 CI = dyn_cast<CallInst>(BI.getCondition());
110 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
112 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
118 Function *Fn = CI->getCalledFunction();
119 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
122 Value *ArgValue = CI->getArgOperand(0);
123 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
127 MDBuilder MDB(CI->getContext());
130 // If expect value is equal to 1 it means that we are more likely to take
131 // branch 0, in other case more likely is branch 1.
132 if (ExpectedValue->isOne())
133 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
135 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
137 BI.setMetadata(LLVMContext::MD_prof, Node);
140 CmpI->setOperand(0, ArgValue);
142 BI.setCondition(ArgValue);
146 bool LowerExpectIntrinsic::runOnFunction(Function &F) {
147 for (BasicBlock &BB : F) {
148 // Create "block_weights" metadata.
149 if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
150 if (handleBranchExpect(*BI))
151 ExpectIntrinsicsHandled++;
152 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
153 if (handleSwitchExpect(*SI))
154 ExpectIntrinsicsHandled++;
157 // remove llvm.expect intrinsics.
158 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
159 CallInst *CI = dyn_cast<CallInst>(BI++);
163 Function *Fn = CI->getCalledFunction();
164 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
165 Value *Exp = CI->getArgOperand(0);
166 CI->replaceAllUsesWith(Exp);
167 CI->eraseFromParent();
175 char LowerExpectIntrinsic::ID = 0;
176 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
177 "Lower 'expect' Intrinsics", false, false)
179 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
180 return new LowerExpectIntrinsic();