blob: 3e61289451998d1e550f1a0da2050a3695bf748a [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Transforms/Scalar.h"
16#include "llvm/ADT/Statistic.h"
Chandler Carruth0b8c9a82013-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 Staszak9da99342011-07-06 18:22:43 +000025#include "llvm/Pass.h"
Jakub Staszak9da99342011-07-06 18:22:43 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/Debug.h"
Jakub Staszak9da99342011-07-06 18:22:43 +000028#include <vector>
29
30using namespace llvm;
31
Robert Wilhelmf80a63f2013-09-28 11:46:15 +000032STATISTIC(IfHandled, "Number of 'expect' intrinsic instructions handled");
Jakub Staszak9da99342011-07-06 18:22:43 +000033
34static cl::opt<uint32_t>
35LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
36 cl::desc("Weight of the branch likely to be taken (default = 64)"));
37static cl::opt<uint32_t>
38UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
39 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
40
41namespace {
42
43 class LowerExpectIntrinsic : public FunctionPass {
44
45 bool HandleSwitchExpect(SwitchInst *SI);
46
47 bool HandleIfExpect(BranchInst *BI);
48
49 public:
50 static char ID;
51 LowerExpectIntrinsic() : FunctionPass(ID) {
52 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
53 }
54
Stephen Hines36b56882014-04-23 16:57:46 -070055 bool runOnFunction(Function &F) override;
Jakub Staszak9da99342011-07-06 18:22:43 +000056 };
57}
58
59
60bool LowerExpectIntrinsic::HandleSwitchExpect(SwitchInst *SI) {
61 CallInst *CI = dyn_cast<CallInst>(SI->getCondition());
62 if (!CI)
63 return false;
64
65 Function *Fn = CI->getCalledFunction();
66 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
67 return false;
68
69 Value *ArgValue = CI->getArgOperand(0);
70 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
71 if (!ExpectedValue)
72 return false;
73
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +000074 SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
Benjamin Kramer937338c2012-05-26 13:59:43 +000075 unsigned n = SI->getNumCases(); // +1 for default case.
76 std::vector<uint32_t> Weights(n + 1);
Jakub Staszak9da99342011-07-06 18:22:43 +000077
Benjamin Kramer937338c2012-05-26 13:59:43 +000078 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;
Jakub Staszak9da99342011-07-06 18:22:43 +000083
Benjamin Kramer937338c2012-05-26 13:59:43 +000084 SI->setMetadata(LLVMContext::MD_prof,
85 MDBuilder(CI->getContext()).createBranchWeights(Weights));
Jakub Staszak9da99342011-07-06 18:22:43 +000086
87 SI->setCondition(ArgValue);
88 return true;
89}
90
91
92bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
93 if (BI->isUnconditional())
94 return false;
95
96 // Handle non-optimized IR code like:
Stephen Hines36b56882014-04-23 16:57:46 -070097 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
Jakub Staszak9da99342011-07-06 18:22:43 +000098 // %tobool = icmp ne i64 %expval, 0
99 // br i1 %tobool, label %if.then, label %if.end
Stephen Hines36b56882014-04-23 16:57:46 -0700100 //
101 // Or the following simpler case:
102 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
103 // br i1 %expval, label %if.then, label %if.end
104
105 CallInst *CI;
Jakub Staszak9da99342011-07-06 18:22:43 +0000106
107 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
Stephen Hines36b56882014-04-23 16:57:46 -0700108 if (!CmpI) {
109 CI = dyn_cast<CallInst>(BI->getCondition());
110 } else {
111 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
112 return false;
113 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
114 }
Jakub Staszak9da99342011-07-06 18:22:43 +0000115
Jakub Staszak9da99342011-07-06 18:22:43 +0000116 if (!CI)
117 return false;
118
119 Function *Fn = CI->getCalledFunction();
120 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
121 return false;
122
123 Value *ArgValue = CI->getArgOperand(0);
124 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
125 if (!ExpectedValue)
126 return false;
127
Benjamin Kramer937338c2012-05-26 13:59:43 +0000128 MDBuilder MDB(CI->getContext());
129 MDNode *Node;
Jakub Staszak9da99342011-07-06 18:22:43 +0000130
131 // If expect value is equal to 1 it means that we are more likely to take
132 // branch 0, in other case more likely is branch 1.
Benjamin Kramer937338c2012-05-26 13:59:43 +0000133 if (ExpectedValue->isOne())
134 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
135 else
136 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
Jakub Staszak9da99342011-07-06 18:22:43 +0000137
Benjamin Kramer937338c2012-05-26 13:59:43 +0000138 BI->setMetadata(LLVMContext::MD_prof, Node);
Jakub Staszak9da99342011-07-06 18:22:43 +0000139
Stephen Hines36b56882014-04-23 16:57:46 -0700140 if (CmpI)
141 CmpI->setOperand(0, ArgValue);
142 else
143 BI->setCondition(ArgValue);
Jakub Staszak9da99342011-07-06 18:22:43 +0000144 return true;
145}
146
147
148bool LowerExpectIntrinsic::runOnFunction(Function &F) {
149 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
150 BasicBlock *BB = I++;
151
152 // Create "block_weights" metadata.
153 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
154 if (HandleIfExpect(BI))
155 IfHandled++;
156 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
157 if (HandleSwitchExpect(SI))
158 IfHandled++;
159 }
160
161 // remove llvm.expect intrinsics.
162 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
163 BI != BE; ) {
164 CallInst *CI = dyn_cast<CallInst>(BI++);
165 if (!CI)
166 continue;
167
168 Function *Fn = CI->getCalledFunction();
Jakub Staszak447c40c2011-07-06 23:50:16 +0000169 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
170 Value *Exp = CI->getArgOperand(0);
171 CI->replaceAllUsesWith(Exp);
Jakub Staszak9da99342011-07-06 18:22:43 +0000172 CI->eraseFromParent();
Jakub Staszak447c40c2011-07-06 23:50:16 +0000173 }
Jakub Staszak9da99342011-07-06 18:22:43 +0000174 }
175 }
176
177 return false;
178}
179
180
181char LowerExpectIntrinsic::ID = 0;
182INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
183 "Intrinsics", false, false)
184
185FunctionPass *llvm::createLowerExpectIntrinsicPass() {
186 return new LowerExpectIntrinsic();
187}