blob: df8d68ec8bab2ce2e2b889a980a6eb49418b3f3f [file] [log] [blame]
Chandler Carruth5820d622011-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
Jakub Staszak9da99342011-07-06 18:22:43 +000014#define DEBUG_TYPE "lower-expect-intrinsic"
15#include "llvm/Constants.h"
16#include "llvm/Function.h"
17#include "llvm/BasicBlock.h"
18#include "llvm/LLVMContext.h"
19#include "llvm/Instructions.h"
20#include "llvm/Intrinsics.h"
21#include "llvm/Metadata.h"
22#include "llvm/Pass.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
27#include <vector>
28
29using namespace llvm;
30
31STATISTIC(IfHandled, "Number of 'expect' intrinsic intructions handled");
32
33static cl::opt<uint32_t>
34LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
35 cl::desc("Weight of the branch likely to be taken (default = 64)"));
36static cl::opt<uint32_t>
37UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
38 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
39
40namespace {
41
42 class LowerExpectIntrinsic : public FunctionPass {
43
44 bool HandleSwitchExpect(SwitchInst *SI);
45
46 bool HandleIfExpect(BranchInst *BI);
47
48 public:
49 static char ID;
50 LowerExpectIntrinsic() : FunctionPass(ID) {
51 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
52 }
53
54 bool runOnFunction(Function &F);
55 };
56}
57
58
59bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
60 CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
61 if (!CI)
62 return false;
63
64 Function *Fn = CI->getCalledFunction();
65 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
66 return false;
67
68 Value *ArgValue = CI->getArgOperand(0);
69 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
70 if (!ExpectedValue)
71 return false;
72
73 LLVMContext &Context = CI->getContext();
Chris Lattnerdb125cf2011-07-18 04:54:35 +000074 Type *Int32Ty = Type::getInt32Ty(Context);
Jakub Staszak9da99342011-07-06 18:22:43 +000075
76 unsigned caseNo = SI->findCaseValue(ExpectedValue);
77 std::vector<Value *> Vec;
78 unsigned n = SI->getNumCases();
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +000079 Vec.resize(n + 1 + 1); // +1 for MDString and +1 for default case
Jakub Staszak9da99342011-07-06 18:22:43 +000080
81 Vec[0] = MDString::get(Context, "branch_weights");
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +000082 Vec[1] = ConstantInt::get(Int32Ty, SwitchInst::ErrorIndex == caseNo ?
83 LikelyBranchWeight : UnlikelyBranchWeight);
Jakub Staszak9da99342011-07-06 18:22:43 +000084 for (unsigned i = 0; i < n; ++i) {
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +000085 Vec[i + 1 + 1] = ConstantInt::get(Int32Ty, i == caseNo ?
86 LikelyBranchWeight : UnlikelyBranchWeight);
Jakub Staszak9da99342011-07-06 18:22:43 +000087 }
88
89 MDNode *WeightsNode = llvm::MDNode::get(Context, Vec);
90 SI->setMetadata(LLVMContext::MD_prof, WeightsNode);
91
92 SI->setCondition(ArgValue);
93 return true;
94}
95
96
97bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
98 if (BI->isUnconditional())
99 return false;
100
101 // Handle non-optimized IR code like:
102 // %expval = call i64 @llvm.expect.i64.i64(i64 %conv1, i64 1)
103 // %tobool = icmp ne i64 %expval, 0
104 // br i1 %tobool, label %if.then, label %if.end
105
106 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
107 if (!CmpI || CmpI->getPredicate() != CmpInst::ICMP_NE)
108 return false;
109
110 CallInst *CI = dyn_cast<CallInst>(CmpI->getOperand(0));
111 if (!CI)
112 return false;
113
114 Function *Fn = CI->getCalledFunction();
115 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
116 return false;
117
118 Value *ArgValue = CI->getArgOperand(0);
119 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
120 if (!ExpectedValue)
121 return false;
122
123 LLVMContext &Context = CI->getContext();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000124 Type *Int32Ty = Type::getInt32Ty(Context);
Jakub Staszak9da99342011-07-06 18:22:43 +0000125 bool Likely = ExpectedValue->isOne();
126
127 // If expect value is equal to 1 it means that we are more likely to take
128 // branch 0, in other case more likely is branch 1.
129 Value *Ops[] = {
130 MDString::get(Context, "branch_weights"),
131 ConstantInt::get(Int32Ty, Likely ? LikelyBranchWeight : UnlikelyBranchWeight),
132 ConstantInt::get(Int32Ty, Likely ? UnlikelyBranchWeight : LikelyBranchWeight)
133 };
134
Frits van Bommel037a60f2011-07-15 17:13:23 +0000135 MDNode *WeightsNode = MDNode::get(Context, Ops);
Jakub Staszak9da99342011-07-06 18:22:43 +0000136 BI->setMetadata(LLVMContext::MD_prof, WeightsNode);
137
138 CmpI->setOperand(0, ArgValue);
139 return true;
140}
141
142
143bool LowerExpectIntrinsic::runOnFunction(Function &F) {
144 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
145 BasicBlock *BB = I++;
146
147 // Create "block_weights" metadata.
148 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
149 if (HandleIfExpect(BI))
150 IfHandled++;
151 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
152 if (HandleSwitchExpect(SI))
153 IfHandled++;
154 }
155
156 // remove llvm.expect intrinsics.
157 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
158 BI != BE; ) {
159 CallInst *CI = dyn_cast<CallInst>(BI++);
160 if (!CI)
161 continue;
162
163 Function *Fn = CI->getCalledFunction();
Jakub Staszak447c40c2011-07-06 23:50:16 +0000164 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
165 Value *Exp = CI->getArgOperand(0);
166 CI->replaceAllUsesWith(Exp);
Jakub Staszak9da99342011-07-06 18:22:43 +0000167 CI->eraseFromParent();
Jakub Staszak447c40c2011-07-06 23:50:16 +0000168 }
Jakub Staszak9da99342011-07-06 18:22:43 +0000169 }
170 }
171
172 return false;
173}
174
175
176char LowerExpectIntrinsic::ID = 0;
177INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
178 "Intrinsics", false, false)
179
180FunctionPass *llvm::createLowerExpectIntrinsicPass() {
181 return new LowerExpectIntrinsic();
182}