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/LowerExpectIntrinsic.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"
28 #include "llvm/Transforms/Scalar.h"
32 #define DEBUG_TYPE "lower-expect-intrinsic"
34 STATISTIC(ExpectIntrinsicsHandled,
35 "Number of 'expect' intrinsic instructions handled");
37 // These default values are chosen to represent an extremely skewed outcome for
38 // a condition, but they leave some room for interpretation by later passes.
40 // If the documentation for __builtin_expect() was made explicit that it should
41 // only be used in extreme cases, we could make this ratio higher. As it stands,
42 // programmers may be using __builtin_expect() / llvm.expect to annotate that a
43 // branch is likely or unlikely to be taken.
45 // There is a known dependency on this ratio in CodeGenPrepare when transforming
46 // 'select' instructions. It may be worthwhile to hoist these values to some
47 // shared space, so they can be used directly by other passes.
49 static cl::opt<uint32_t> LikelyBranchWeight(
50 "likely-branch-weight", cl::Hidden, cl::init(2000),
51 cl::desc("Weight of the branch likely to be taken (default = 2000)"));
52 static cl::opt<uint32_t> UnlikelyBranchWeight(
53 "unlikely-branch-weight", cl::Hidden, cl::init(1),
54 cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
56 static bool handleSwitchExpect(SwitchInst &SI) {
57 CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
61 Function *Fn = CI->getCalledFunction();
62 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
65 Value *ArgValue = CI->getArgOperand(0);
66 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
70 SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
71 unsigned n = SI.getNumCases(); // +1 for default case.
72 SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
74 if (Case == SI.case_default())
75 Weights[0] = LikelyBranchWeight;
77 Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
79 SI.setMetadata(LLVMContext::MD_prof,
80 MDBuilder(CI->getContext()).createBranchWeights(Weights));
82 SI.setCondition(ArgValue);
86 static bool handleBranchExpect(BranchInst &BI) {
87 if (BI.isUnconditional())
90 // Handle non-optimized IR code like:
91 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
92 // %tobool = icmp ne i64 %expval, 0
93 // br i1 %tobool, label %if.then, label %if.end
95 // Or the following simpler case:
96 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
97 // br i1 %expval, label %if.then, label %if.end
101 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition());
103 CI = dyn_cast<CallInst>(BI.getCondition());
105 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
107 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
113 Function *Fn = CI->getCalledFunction();
114 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
117 Value *ArgValue = CI->getArgOperand(0);
118 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
122 MDBuilder MDB(CI->getContext());
125 // If expect value is equal to 1 it means that we are more likely to take
126 // branch 0, in other case more likely is branch 1.
127 if (ExpectedValue->isOne())
128 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
130 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
132 BI.setMetadata(LLVMContext::MD_prof, Node);
135 CmpI->setOperand(0, ArgValue);
137 BI.setCondition(ArgValue);
141 static bool lowerExpectIntrinsic(Function &F) {
142 bool Changed = false;
144 for (BasicBlock &BB : F) {
145 // Create "block_weights" metadata.
146 if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
147 if (handleBranchExpect(*BI))
148 ExpectIntrinsicsHandled++;
149 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
150 if (handleSwitchExpect(*SI))
151 ExpectIntrinsicsHandled++;
154 // Remove llvm.expect intrinsics.
155 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
156 CallInst *CI = dyn_cast<CallInst>(BI++);
160 Function *Fn = CI->getCalledFunction();
161 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
162 Value *Exp = CI->getArgOperand(0);
163 CI->replaceAllUsesWith(Exp);
164 CI->eraseFromParent();
173 PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F,
174 FunctionAnalysisManager &) {
175 if (lowerExpectIntrinsic(F))
176 return PreservedAnalyses::none();
178 return PreservedAnalyses::all();
182 /// \brief Legacy pass for lowering expect intrinsics out of the IR.
184 /// When this pass is run over a function it uses expect intrinsics which feed
185 /// branches and switches to provide branch weight metadata for those
186 /// terminators. It then removes the expect intrinsics from the IR so the rest
187 /// of the optimizer can ignore them.
188 class LowerExpectIntrinsic : public FunctionPass {
191 LowerExpectIntrinsic() : FunctionPass(ID) {
192 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
195 bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
199 char LowerExpectIntrinsic::ID = 0;
200 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
201 "Lower 'expect' Intrinsics", false, false)
203 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
204 return new LowerExpectIntrinsic();