[PM] Clean up the formatting of the LowerExpectIntrinsic pass prior to
[oota-llvm.git] / lib / Transforms / Scalar / 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 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/ADT/Statistic.h"
16 #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"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Debug.h"
27 #include <vector>
28
29 using namespace llvm;
30
31 #define DEBUG_TYPE "lower-expect-intrinsic"
32
33 STATISTIC(IfHandled, "Number of 'expect' intrinsic instructions handled");
34
35 static cl::opt<uint32_t>
36 LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(64),
37                    cl::desc("Weight of the branch likely to be taken (default = 64)"));
38 static cl::opt<uint32_t>
39 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
40                    cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
41
42 namespace {
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 bool 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   SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
74   unsigned n = SI->getNumCases(); // +1 for default case.
75   std::vector<uint32_t> Weights(n + 1);
76
77   Weights[0] =
78       Case == SI->case_default() ? LikelyBranchWeight : UnlikelyBranchWeight;
79   for (unsigned i = 0; i != n; ++i)
80     Weights[i + 1] =
81         i == Case.getCaseIndex() ? LikelyBranchWeight : UnlikelyBranchWeight;
82
83   SI->setMetadata(LLVMContext::MD_prof,
84                   MDBuilder(CI->getContext()).createBranchWeights(Weights));
85
86   SI->setCondition(ArgValue);
87   return true;
88 }
89
90 bool LowerExpectIntrinsic::HandleIfExpect(BranchInst *BI) {
91   if (BI->isUnconditional())
92     return false;
93
94   // Handle non-optimized IR code like:
95   //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
96   //   %tobool = icmp ne i64 %expval, 0
97   //   br i1 %tobool, label %if.then, label %if.end
98   //
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;
104
105   ICmpInst *CmpI = dyn_cast<ICmpInst>(BI->getCondition());
106   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   }
113
114   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
126   MDBuilder MDB(CI->getContext());
127   MDNode *Node;
128
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.
131   if (ExpectedValue->isOne())
132     Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
133   else
134     Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
135
136   BI->setMetadata(LLVMContext::MD_prof, Node);
137
138   if (CmpI)
139     CmpI->setOperand(0, ArgValue);
140   else
141     BI->setCondition(ArgValue);
142   return true;
143 }
144
145 bool 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.
159     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
160       CallInst *CI = dyn_cast<CallInst>(BI++);
161       if (!CI)
162         continue;
163
164       Function *Fn = CI->getCalledFunction();
165       if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
166         Value *Exp = CI->getArgOperand(0);
167         CI->replaceAllUsesWith(Exp);
168         CI->eraseFromParent();
169       }
170     }
171   }
172
173   return false;
174 }
175
176 char LowerExpectIntrinsic::ID = 0;
177 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
178                 "Lower 'expect' Intrinsics", false, false)
179
180 FunctionPass *llvm::createLowerExpectIntrinsicPass() {
181   return new LowerExpectIntrinsic();
182 }