blob: 3a0474279a6cebf64f77324ab5b2849ebcfb079d [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"
15#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Intrinsics.h"
21#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/MDBuilder.h"
23#include "llvm/IR/Metadata.h"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000024#include "llvm/Pass.h"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
Jakub Staszak3f158fd2011-07-06 18:22:43 +000027#include <vector>
28
29using namespace llvm;
30
Chandler Carruth964daaa2014-04-22 02:55:47 +000031#define DEBUG_TYPE "lower-expect-intrinsic"
32
Robert Wilhelmf0cfb832013-09-28 11:46:15 +000033STATISTIC(IfHandled, "Number of 'expect' intrinsic instructions handled");
Jakub Staszak3f158fd2011-07-06 18:22:43 +000034
35static cl::opt<uint32_t>
36LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
37 cl::desc("Weight of the branch likely to be taken (default = 64)"));
38static cl::opt<uint32_t>
39UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
40 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
41
42namespace {
Chandler Carruth0ea746b2015-01-24 10:30:14 +000043class LowerExpectIntrinsic : public FunctionPass {
Jakub Staszak3f158fd2011-07-06 18:22:43 +000044
Chandler Carruth0ea746b2015-01-24 10:30:14 +000045 bool HandleSwitchExpect(SwitchInst *SI);
Jakub Staszak3f158fd2011-07-06 18:22:43 +000046
Chandler Carruth0ea746b2015-01-24 10:30:14 +000047 bool HandleIfExpect(BranchInst *BI);
Jakub Staszak3f158fd2011-07-06 18:22:43 +000048
Chandler Carruth0ea746b2015-01-24 10:30:14 +000049public:
50 static char ID;
51 LowerExpectIntrinsic() : FunctionPass(ID) {
52 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
53 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +000054
Chandler Carruth0ea746b2015-01-24 10:30:14 +000055 bool runOnFunction(Function &F) override;
56};
Jakub Staszak3f158fd2011-07-06 18:22:43 +000057}
58
Jakub Staszak3f158fd2011-07-06 18:22:43 +000059bool 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
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +000073 SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
Benjamin Kramer65e75662012-05-26 13:59:43 +000074 unsigned n = SI->getNumCases(); // +1 for default case.
75 std::vector<uint32_t> Weights(n + 1);
Jakub Staszak3f158fd2011-07-06 18:22:43 +000076
Chandler Carruth0ea746b2015-01-24 10:30:14 +000077 Weights[0] =
78 Case == SI->case_default() ? LikelyBranchWeight : UnlikelyBranchWeight;
Benjamin Kramer65e75662012-05-26 13:59:43 +000079 for (unsigned i = 0; i != n; ++i)
Chandler Carruth0ea746b2015-01-24 10:30:14 +000080 Weights[i + 1] =
81 i == Case.getCaseIndex() ? LikelyBranchWeight : UnlikelyBranchWeight;
Jakub Staszak3f158fd2011-07-06 18:22:43 +000082
Benjamin Kramer65e75662012-05-26 13:59:43 +000083 SI->setMetadata(LLVMContext::MD_prof,
84 MDBuilder(CI->getContext()).createBranchWeights(Weights));
Jakub Staszak3f158fd2011-07-06 18:22:43 +000085
86 SI->setCondition(ArgValue);
87 return true;
88}
89
Jakub Staszak3f158fd2011-07-06 18:22:43 +000090bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
91 if (BI->isUnconditional())
92 return false;
93
94 // Handle non-optimized IR code like:
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +000095 // %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
Jakub Staszak3f158fd2011-07-06 18:22:43 +000096 // %tobool = icmp ne i64 %expval, 0
97 // br i1 %tobool, label %if.then, label %if.end
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +000098 //
99 // Or the following simpler case:
100 // %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
101 // br i1 %expval, label %if.then, label %if.end
102
103 CallInst *CI;
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000104
105 ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +0000106 if (!CmpI) {
107 CI = dyn_cast<CallInst>(BI->getCondition());
108 } else {
109 if (CmpI->getPredicate() != CmpInst::ICMP_NE)
110 return false;
111 CI = dyn_cast<CallInst>(CmpI->getOperand(0));
112 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000113
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000114 if (!CI)
115 return false;
116
117 Function *Fn = CI->getCalledFunction();
118 if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
119 return false;
120
121 Value *ArgValue = CI->getArgOperand(0);
122 ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
123 if (!ExpectedValue)
124 return false;
125
Benjamin Kramer65e75662012-05-26 13:59:43 +0000126 MDBuilder MDB(CI->getContext());
127 MDNode *Node;
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000128
129 // If expect value is equal to 1 it means that we are more likely to take
130 // branch 0, in other case more likely is branch 1.
Benjamin Kramer65e75662012-05-26 13:59:43 +0000131 if (ExpectedValue->isOne())
132 Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
133 else
134 Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000135
Benjamin Kramer65e75662012-05-26 13:59:43 +0000136 BI->setMetadata(LLVMContext::MD_prof, Node);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000137
Duncan P. N. Exon Smith1ff08e32014-02-02 22:43:55 +0000138 if (CmpI)
139 CmpI->setOperand(0, ArgValue);
140 else
141 BI->setCondition(ArgValue);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000142 return true;
143}
144
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000145bool LowerExpectIntrinsic::runOnFunction(Function &F) {
146 for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
147 BasicBlock *BB = I++;
148
149 // Create "block_weights" metadata.
150 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
151 if (HandleIfExpect(BI))
152 IfHandled++;
153 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
154 if (HandleSwitchExpect(SI))
155 IfHandled++;
156 }
157
158 // remove llvm.expect intrinsics.
Chandler Carruth0ea746b2015-01-24 10:30:14 +0000159 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000160 CallInst *CI = dyn_cast<CallInst>(BI++);
161 if (!CI)
162 continue;
163
164 Function *Fn = CI->getCalledFunction();
Jakub Staszaka11f7ec2011-07-06 23:50:16 +0000165 if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
166 Value *Exp = CI->getArgOperand(0);
167 CI->replaceAllUsesWith(Exp);
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000168 CI->eraseFromParent();
Jakub Staszaka11f7ec2011-07-06 23:50:16 +0000169 }
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000170 }
171 }
172
173 return false;
174}
175
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000176char LowerExpectIntrinsic::ID = 0;
Chandler Carruth0ea746b2015-01-24 10:30:14 +0000177INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
178 "Lower 'expect' Intrinsics", false, false)
Jakub Staszak3f158fd2011-07-06 18:22:43 +0000179
180FunctionPass *llvm::createLowerExpectIntrinsicPass() {
181 return new LowerExpectIntrinsic();
182}