3e61289451998d1e550f1a0da2050a3695bf748a
[oota-llvm.git] / lib / Transforms / Utils / LowerExpectIntrinsic.cpp
1 //===- 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
14 #define DEBUG_TYPE "lower-expect-intrinsic"
15 #include "llvm/Transforms/Scalar.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 <vector>
29
30 using namespace llvm;
31
32 STATISTIC(IfHandled, "Number of 'expect' intrinsic instructions handled");
33
34 static cl::opt<uint32_t>
35 LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
36                    cl::desc("Weight of the branch likely to be taken (default = 64)"));
37 static cl::opt<uint32_t>
38 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
39                    cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
40
41 namespace {
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
55     bool runOnFunction(Function &F) override;
56   };
57 }
58
59
60 bool 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
74   SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
75   unsigned n = SI->getNumCases(); // +1 for default case.
76   std::vector<uint32_t> Weights(n + 1);
77
78   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;
83
84   SI->setMetadata(LLVMContext::MD_prof,
85                   MDBuilder(CI->getContext()).createBranchWeights(Weights));
86
87   SI->setCondition(ArgValue);
88   return true;
89 }
90
91
92 bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
93   if (BI->isUnconditional())
94     return false;
95
96   // Handle non-optimized IR code like:
97   //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
98   //   %tobool = icmp ne i64 %expval, 0
99   //   br i1 %tobool, label %if.then, label %if.end
100   //
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;
106
107   ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
108   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   }
115
116   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
128   MDBuilder MDB(CI->getContext());
129   MDNode *Node;
130
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.
133   if (ExpectedValue->isOne())
134     Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
135   else
136     Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
137
138   BI->setMetadata(LLVMContext::MD_prof, Node);
139
140   if (CmpI)
141     CmpI->setOperand(0, ArgValue);
142   else
143     BI->setCondition(ArgValue);
144   return true;
145 }
146
147
148 bool 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();
169       if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
170         Value *Exp = CI->getArgOperand(0);
171         CI->replaceAllUsesWith(Exp);
172         CI->eraseFromParent();
173       }
174     }
175   }
176
177   return false;
178 }
179
180
181 char LowerExpectIntrinsic::ID = 0;
182 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect", "Lower 'expect' "
183                 "Intrinsics", false, false)
184
185 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
186   return new LowerExpectIntrinsic();
187 }