blob: 5d3bbbc6047cfedc50a2fbcd806622d8a34db68f [file] [log] [blame]
Chandler Carruth47e1db12011-10-16 22:15:07 +00001//===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass lowers the 'expect' intrinsic to LLVM metadata.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Transforms/Scalar.h"
Chandler Carruth3f5e7b12015-01-24 10:47:13 +000015#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#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"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000025#include "llvm/Pass.h"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000028
29using namespace llvm;
30
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "lower-expect-intrinsic"
32
Chandler Carruth6eb60eb2015-01-24 10:57:25 +000033STATISTIC(ExpectIntrinsicsHandled,
34 "Number of 'expect' intrinsic instructions handled");
Jakub Staszak3f158fd2011-07-06 18:22:43 +000035
36static cl::opt<uint32_t>
37LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
38 cl::desc("Weight of the branch likely to be taken (default = 64)"));
39static cl::opt<uint32_t>
40UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
41 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
42
43namespace {
Chandler Carruth579c5c42015-01-24 10:32:53 +000044/// \brief Legacy pass for lowering expect intrinsics out of the IR.
45///
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.
Chandler Carruth0ea746b2015-01-24 10:30:14 +000050class LowerExpectIntrinsic : public FunctionPass {
Chandler Carruth0ea746b2015-01-24 10:30:14 +000051public:
52 static char ID;
53 LowerExpectIntrinsic() : FunctionPass(ID) {
54 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
55 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +000056
Chandler Carruth0ea746b2015-01-24 10:30:14 +000057 bool runOnFunction(Function &F) override;
58};
Jakub Staszak3f158fd2011-07-06 18:22:43 +000059}
60
Chandler Carruth0012c772015-01-24 10:39:24 +000061static bool handleSwitchExpect(SwitchInst &SI) {
62 CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
Jakub Staszak3f158fd2011-07-06 18:22:43 +000063 if (!CI)
64 return false;
65
66 Function *Fn = CI->getCalledFunction();
67 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
68 return false;
69
70 Value *ArgValue = CI->getArgOperand(0);
71 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
72 if (!ExpectedValue)
73 return false;
74
Chandler Carruth0012c772015-01-24 10:39:24 +000075 SwitchInst::CaseIt Case = SI.findCaseValue(ExpectedValue);
76 unsigned n = SI.getNumCases(); // +1 for default case.
Chandler Carruth3f5e7b12015-01-24 10:47:13 +000077 SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
Jakub Staszak3f158fd2011-07-06 18:22:43 +000078
Chandler Carruth3f5e7b12015-01-24 10:47:13 +000079 if (Case == SI.case_default())
80 Weights[0] = LikelyBranchWeight;
81 else
82 Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
Jakub Staszak3f158fd2011-07-06 18:22:43 +000083
Chandler Carruth0012c772015-01-24 10:39:24 +000084 SI.setMetadata(LLVMContext::MD_prof,
85 MDBuilder(CI->getContext()).createBranchWeights(Weights));
Jakub Staszak3f158fd2011-07-06 18:22:43 +000086
Chandler Carruth0012c772015-01-24 10:39:24 +000087 SI.setCondition(ArgValue);
Jakub Staszak3f158fd2011-07-06 18:22:43 +000088 return true;
89}
90
Chandler Carruth0012c772015-01-24 10:39:24 +000091static bool handleBranchExpect(BranchInst &BI) {
92 if (BI.isUnconditional())
Jakub Staszak3f158fd2011-07-06 18:22:43 +000093 return false;
94
95 // Handle non-optimized IR code like:
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +000096 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
Jakub Staszak3f158fd2011-07-06 18:22:43 +000097 // %tobool = icmp ne i64 %expval, 0
98 // br i1 %tobool, label %if.then, label %if.end
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +000099 //
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
103
104 CallInst *CI;
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000105
Chandler Carruth0012c772015-01-24 10:39:24 +0000106 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI.getCondition());
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +0000107 if (!CmpI) {
Chandler Carruth0012c772015-01-24 10:39:24 +0000108 CI = dyn_cast<CallInst>(BI.getCondition());
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +0000109 } else {
110 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
111 return false;
112 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
113 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000114
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000115 if (!CI)
116 return false;
117
118 Function *Fn = CI->getCalledFunction();
119 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
120 return false;
121
122 Value *ArgValue = CI->getArgOperand(0);
123 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
124 if (!ExpectedValue)
125 return false;
126
Benjamin Kramer65e75662012-05-26 13:59:43 +0000127 MDBuilder MDB(CI->getContext());
128 MDNode *Node;
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000129
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.
Benjamin Kramer65e75662012-05-26 13:59:43 +0000132 if (ExpectedValue->isOne())
133 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
134 else
135 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000136
Chandler Carruth0012c772015-01-24 10:39:24 +0000137 BI.setMetadata(LLVMContext::MD_prof, Node);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000138
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +0000139 if (CmpI)
140 CmpI->setOperand(0, ArgValue);
141 else
Chandler Carruth0012c772015-01-24 10:39:24 +0000142 BI.setCondition(ArgValue);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000143 return true;
144}
145
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000146bool LowerExpectIntrinsic::runOnFunction(Function &F) {
Chandler Carruthd12741e2015-01-24 10:57:19 +0000147 for (BasicBlock &BB : F) {
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000148 // Create "block_weights" metadata.
Chandler Carruthd12741e2015-01-24 10:57:19 +0000149 if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
Chandler Carruth0012c772015-01-24 10:39:24 +0000150 if (handleBranchExpect(*BI))
Chandler Carruth6eb60eb2015-01-24 10:57:25 +0000151 ExpectIntrinsicsHandled++;
Chandler Carruthd12741e2015-01-24 10:57:19 +0000152 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
Chandler Carruth0012c772015-01-24 10:39:24 +0000153 if (handleSwitchExpect(*SI))
Chandler Carruth6eb60eb2015-01-24 10:57:25 +0000154 ExpectIntrinsicsHandled++;
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000155 }
156
157 // remove llvm.expect intrinsics.
Chandler Carruthd12741e2015-01-24 10:57:19 +0000158 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000159 CallInst *CI = dyn_cast<CallInst>(BI++);
160 if (!CI)
161 continue;
162
163 Function *Fn = CI->getCalledFunction();
Jakub Staszaka11f7ec2011-07-06 23:50:16 +0000164 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
165 Value *Exp = CI->getArgOperand(0);
166 CI->replaceAllUsesWith(Exp);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000167 CI->eraseFromParent();
Jakub Staszaka11f7ec2011-07-06 23:50:16 +0000168 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000169 }
170 }
171
172 return false;
173}
174
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000175char LowerExpectIntrinsic::ID = 0;
Chandler Carruth0ea746b2015-01-24 10:30:14 +0000176INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
177 "Lower 'expect' Intrinsics", false, false)
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000178
179FunctionPass *llvm::createLowerExpectIntrinsicPass() {
180 return new LowerExpectIntrinsic();
181}